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

Kaarthik Alagapan via llvm-dev llvm-dev at lists.llvm.org
Wed Oct 23 13:27:53 PDT 2019


Hi Tim,
Thank you for the quick response!

so you'd probably check you're looking at a
BranchInst and check BI->getSuccessor(0)->getName() (& 1 if it's
conditional)
I initially was printing out the result from getName() (I.getParent()->getName()) and it printed out nothing sadly.

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.
I am calling it right after each instruction is visited in the visit(…) function. And it prints out the instruction’s number but it prints out only %2 when I call printAsOperand on the instruction’s parent, hence why I assumed that the visit function was going over only the main block.

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.
That’s true. I’m now trying to detect the specific label in the LLParser and hopefully can insert my instruction there. Would creating a new pseudo instruction be a good way to emit “.byte …” assembly code?

Thank you,
Kaarthik A.
On Oct 23, 2019, 3:20 PM -0400, Tim Northover <t.p.northover at gmail.com>, wrote:
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
conditional).

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.

Cheers.

Tim.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20191023/526ac329/attachment.html>


More information about the llvm-dev mailing list