[llvm-dev] Manually insert an instruction in SelectionDAG

19 views
Skip to first unread message

Kaarthik Alagapan via llvm-dev

unread,
Aug 3, 2019, 5:57:21 PM8/3/19
to llvm...@lists.llvm.org
Hello,
I am trying to insert a .byte/.word in the beginning of a specific LLVM IR instruction when it prints out in assembly (the inserted ‘instruction' only appears in assembly, not in LLVM IR), and I am guessing the best way to do that is to insert it in SelectionDAG as it strips down some LLVM IR instructions when it’s lowered. Can I get some guidance on what function I should use to insert a .byte/.word in SelectionDAG so it’s printed out in assembly?

Thank you,
Kaarthik A. Alagappan

Tim Northover via llvm-dev

unread,
Aug 4, 2019, 2:19:18 AM8/4/19
to Kaarthik Alagapan, llvm...@lists.llvm.org
Hi Kaarthik,

On Sat, 3 Aug 2019 at 22:57, Kaarthik Alagapan via llvm-dev
<llvm...@lists.llvm.org> wrote:
> Can I get some guidance on what function I should use to insert a .byte/.word in SelectionDAG so it’s printed out in assembly?

This kind of problem is often solved by selecting the original IR
instruction to a target-specific pseudo-instruction (one with
"isCodeGenOnly = 1" in TableGen) and then expanding it to the real
instruction sequence later.

In most cases that would be a special XYZExpandPseudoInsts.cpp pass
that runs very late and looks through a function replacing any
MachineInstrs with the correct sequence. But in your case .byte and
.word aren't even instructions so you probably want to do it during
conversion to MCInsts in XYZAsmPrinter.cpp (or possibly
XYZMCInstLower.cpp, different targets handle that step slightly
differently). There you can detect your special pseudo and call
EmitIntValue or something to get your directives.

Cheers.

Tim.
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Lorenzo Casalino via llvm-dev

unread,
Aug 4, 2019, 11:46:50 AM8/4/19
to kaar...@knights.ucf.edu, via llvm-dev

> Can I get some guidance on what function I should use to insert a .byte/.word in SelectionDAG so it’s printed out in assembly?

As far as I know, up to the CodeGen phase, LLVM works on a
target-agnostic representation of the original code; low
level concepts, such as ASM directives, are not handled; thus, it is not
possible to add them in the SelectionDAG or
in the subsequent machine specific representation.

As pointed out by Tim, you could print them during the code emission
phase [1].

-- Lorenzo

-------

[1]: https://www.llvm.org/docs/WritingAnLLVMBackend.html#assembly-printer

Kaarthik Alagapan via llvm-dev

unread,
Aug 5, 2019, 10:18:42 AM8/5/19
to Tim Northover, llvm...@lists.llvm.org
I’ll look into that file, thank you for the clarification!

Kaarthik A.

Kaarthik Alagapan via llvm-dev

unread,
Aug 5, 2019, 9:35:54 PM8/5/19
to Lorenzo Casalino, via llvm-dev
Got it Lorenzo, thank you so much!
On Aug 4, 2019, 11:46 AM -0400, Lorenzo Casalino <lorenzo.c...@gmail.com>, wrote:

Can I get some guidance on what function I should use to insert a .byte/.word in SelectionDAG so it’s printed out in assembly?

As far as I know, up to the CodeGen phase, LLVM works on a
target-agnostic representation of the original code; low
level concepts, such as ASM directives, are not handled; thus, it is not
possible to add them in the SelectionDAG or
in the subsequent machine specific representation.

As pointed out by Tim, you could print them during the code emission
phase [1].

-- Lorenzo

-------

Kaarthik Alagapan via llvm-dev

unread,
Aug 6, 2019, 12:32:38 AM8/6/19
to Tim Northover, llvm...@lists.llvm.org
Hi Tim,
I was looking into what you said but then realized that I am working with jump and branch instructions (not pseudo instructions), which I believe are all translated to jumps by the AsmPrinter and InstLower phase, so  I tried to see where I can detect any type of jumps so I call EmitIntValue but wasn’t able to find any other than TAILJMP*. Any tips on where I can find jumps being detected for their code emission?

Kaarthik A.
On Aug 4, 2019, 2:19 AM -0400, Tim Northover <t.p.no...@gmail.com>, wrote:
Reply all
Reply to author
Forward
0 new messages