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
%add = %T1 //psuedo code as I do not know how to this
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, %that
is a single instruction, a single llvm::Instruction*
in memory. If you call I->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, %c
and you'd like to replace this one use of %c
with %d
instead. You may call I->setOperand(1, D)
(where D is the llvm::Value*
for %d
). In another case, suppose you have %e = add i32 %f, 0
and you'd like to remove it. Call E->replaceAllUsesWith(F); E->eraseFromParent();
. The first statement will find everywhere %e
is mentioned and replace it with %f
, the statement will delete the now-unused %e
and free its memory (requires that E
is an llvm::Instruction*
, not a Constant (never deleted) or Argument (deleting it requires changing the type of the function)).
If you could write %add = copy %T1
then it would also be valid to say Add->replaceAllUsesWith(T1);
and remove %add
replacing it with %t1
everywhere.