[llvm-dev] Inserting instructions when encountered a specific label

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Wed Oct 23 12:20:28 PDT 2019

Hi Kaarthik,

On Wed, 23 Oct 2019 at 11:37, Kaarthik Alagapan via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> I want to insert a new instruction when I encounter the true, false, and end labels (inside their respective blocks). I tried to detect the label names using " I.printAsOperand(errs(), false);”

This function prints an instruction as it would be referred to in
another instruction, so for example if the IR was

    %2 = alloca i32

then printAsOperand would print %2. It sounds like you want to
introspect the operands, so you'd probably check you're looking at a
BranchInst and check BI->getSuccessor(0)->getName() (& 1 if it's

> when parsing the instructions in SelectionDAGBuilder but it was only visiting instructions in the first/main BB and outputted %2.

I'm not sure what's going on there, it would depend on which function
you're modifying in SelectionDAG.

> Which pass or phase would be the best to see if the current block’s label matches “true”, “false”, or “end” and insert an instruction inside that block?

To be honest, looking at block names is so hacky that it doesn't
really matter where you put it (for example release mode compilers
often don't add names at all, and generally it's perfectly valid as a
transformation to drop them entirely). It's probably easier if it's
before SelectionDAGBulder (or at the very beginning of its
runOnFunction) so you can modify the IR without worrying about whether
SelectionDAG will notice the new instructions.

If you decide to keep doing this transformation in a more rigorous way
long term, it would probably be put in a new pass (since I'd guess it
would be implementing a change in semantics), but it's possible it
could belong in an existing pass. Depends on exactly what the new
instruction does.



More information about the llvm-dev mailing list