Question about Instruction comparison

I have been looking over the doxygen documentation as well as the llvm code. Is there a standard way to check to see if two instructions are identical (i.e., Inst1 == Inst2 does not work)?

Thanks,
Brian

Brian M. Fahs
Graduate Student
University of Illinois

I have been looking over the doxygen documentation as well as the llvm
code. Is there a standard way to check to see if two instructions are
identical (i.e., Inst1 == Inst2 does not work)?

If you want to check if two instructions are the same instruction, you
should be able to just compare pointers.

-Tanya

Comparing pointers will tell you if they are the SAME instruction. I
think what Brian is looking for is "equivalent" instructions, as I am
familiar with his project -- providing binary diffs of programs and
on-line patching/upgrading of code.

Since (I'm assuming) the instructions he's comparing do not come from
the same Module, they cannot be the "SAME" instruction, and hence (Inst1
== Inst2) will always be false.

The answer is that there is no built-in functionality to test
equivalence, you need to check the opcode of the instructions, and then
do an argument-by-argument comparison (based on the instruction type)
and compare their types and values.

One thing you want to think about, Brian, is that when diffing two Modules,
instructions will refer to previous instructions, i.e., an Instruction
is really a Value, and can be used as an operand in another instruction.

Thus, when comparing:

  a = add b, c x = add y, z

you need to make sure that the other instructions have the "equivalent"
side-effect semantically. Perhaps caching such information about
previous instructions will be useful, I'm not sure.

If that's true, then there is no simple way to do this. You really are
looking at comparing/diffing two different SSA graphs for two functions.
What you will need to do is build up an equivalence map. For the two
functions you want to diff, you would insert the arguments into the map,
the first of each mapping to each other, the second of each mapping to
each other, etc.

then you process the instructions. When you get to an 'add' for example,
you would have to check the map to see if the operands are equivalent
after the mapping has taken effect. This is not trivial, but it is very
possible to implement. Similar functionality exists in the function
cloning routines (lib/Transforms/Utils/*Cloning*), I would take a look at
how that handles the ValueMap stuff.

-Chris