[LLVMdev] IR code modification/transformation

Frits van Bommel fvbommel at gmail.com
Thu Aug 11 03:33:54 PDT 2011


On 11 August 2011 12:11, Rinaldini Julien <julien.rinaldini at heig-vd.ch> wrote:
> I have a question about the llvm passes.
>
> I'm iterating over a basicblock and I can get an instruction and print it.
> Now, I want to iterate over the instruction and be able to modify the values of the instruction.
>
> For example, if my instruction is an add "<result> = add i32 4, %var" I want to transform it in a sub "<result> = sub i32 4, %var".
>
> I looked up on getOperands() and getAllMetadata() and some others methods, but none work.
>
> Can you help me with that please?

Don't try to modify an add instruction to turn it into a sub instruction.
Just insert a new sub instruction (inserting it before the add),
replace all uses of the add with the sub, then erase the add.
Something like this:
=====
    Instruction *Add = /* ... */;
    BinaryOperator *Sub =
BinaryOperator::CreateSub(Add->getOperand(0), Add->getOperand(1),
Add->getName() + ".sub", Add);
    Add->replaceAllUsesWith(Sub);
    Add->eraseFromParent();
=====



More information about the llvm-dev mailing list