[LLVMdev] how to change one operand of an LLVM instruction
clattner at apple.com
Tue Sep 8 14:55:32 PDT 2009
On Sep 8, 2009, at 2:46 PM, Susan Horwitz wrote:
> I am trying to implement node splitting to transform irreducible
> CFGS to
> reducible ones. This means making copies of some basic blocks,
> which in
> turn means making copies of individual instructions. I can use the
> "clone" function to make an exact copy, but then I need to change some
> operands. For example, when I copy
> %1 = ...
> %2 = add %1, 5
> I get
> %3 = ...
> %4 = add %1, 5
> and I need to change the %1 operand in the copy to be %3.
> The only way I see to do this is to create a new instruction instead
> of a
> clone, using a big switch on the opcode (since different instructions'
> constructors have different parameters).
Because LLVM IR is in SSA form, you shouldn't think of the
"name" (like %2) as being a property of the instruction... the name
*is* the instruction. If you need to duplicate blocks like this, you
should probably demote the cross-block values to memory (using the
reg2mem utilities), perform your transformation, then use the mem2reg
pass to eliminate the temporary memory accesses and reconstruct SSA
One example pass that does this is lib/Transforms/Scalar/
JumpThreading.cpp. Search for "DemoteRegToStack".
More information about the llvm-dev