how to change one operand of an LLVM instruction

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).

Anyone know of an easier way to do this?

Hi Susan,

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 form.

One example pass that does this is lib/Transforms/Scalar/JumpThreading.cpp. Search for "DemoteRegToStack".

-Chris

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).

Anyone know of an easier way to do this?

Are you using CloneBasicBlock? If so, you should pass it a ValueMap. You can then iterate over the instructions in the new basicblock and update the instruction operand references.

Check out CloneFunction.cpp for some examples.

-Tanya