[LLVMdev] Question about Instruction comparison

Misha Brukman brukman at uiuc.edu
Mon Nov 24 14:05:01 PST 2003


On Mon, Nov 24, 2003 at 02:13:04PM -0600, Tanya Brethour wrote:
> > 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.

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.

-- 
Misha Brukman :: http://misha.brukman.net



More information about the llvm-dev mailing list