0

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

  • Half of your question sounds as if you want to set an operand of an instruction. There is a [setOperand()](https://llvm.org/doxygen/classllvm_1_1User.html#a5fa9b8e1842b354f64c1ba6be0a4a17f). But the other half suggests that you want to replace [the instruction itself](https://llvm.org/doxygen/classllvm_1_1Value.html#a3ab5fc45117b450e8bb04e564cb6e5f2), except its name. – arnt Apr 27 '22 at 16:12
  • In general, you can find many useful things if you look at the member function lists in classes such as [Instruction](https://llvm.org/doxygen/classllvm_1_1Instruction.html). Note that the one you want is often one the public functions of the superclasses, so you have to open those (initially closed) parts of the page. – arnt Apr 27 '22 at 16:15

1 Answers1

2

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

Nick Lewycky
  • 1,182
  • 6
  • 14