Here is the problem,
Lets say suppose, I have the following instruction
%add = add nsw i32 %argc, 50
I would like at first, create a Binary instruction something like below
%T1 = add nsw i32 %argc, 50
%add = add nsw i32 %argc, 50
And then somehow replace %add's instruction with an assignment to T1 as below
%add = %T1 //psuedo code as I do not know how to this
Need some suggestion to how to implement "%add = %T1"
Bakcground: I need this kind of implemenatation for my Lazy Code Motion analysis
There is no assignment operator in LLVM IR so you can't write that as-is, but the good news is that you never need to.
%name = add i32 %this, %thatis a single instruction, a singlellvm::Instruction*in memory. If you callI->getName()on that instruction you will get the name "name". LLVM has no assignment operator at all, the equals sign in the printed text form is a fiction to make it easier to read. There's no need for an assignment operator because LLVM is in SSA form, every register is assigned to once when created and can never be mutated or reassigned to.Here's two common situations. Suppose you have
%a = add i32 %b, %cand you'd like to replace this one use of%cwith%dinstead. You may callI->setOperand(1, D)(where D is thellvm::Value*for%d). In another case, suppose you have%e = add i32 %f, 0and you'd like to remove it. CallE->replaceAllUsesWith(F); E->eraseFromParent();. The first statement will find everywhere%eis mentioned and replace it with%f, the statement will delete the now-unused%eand free its memory (requires thatEis anllvm::Instruction*, not a Constant (never deleted) or Argument (deleting it requires changing the type of the function)).If you could write
%add = copy %T1then it would also be valid to sayAdd->replaceAllUsesWith(T1);and remove%addreplacing it with%t1everywhere.