[llvm-dev] Convert MachineInstr to MCInst in AsmPrinter.cpp

Francis Visoiu Mistrih via llvm-dev llvm-dev at lists.llvm.org
Tue Jan 2 01:55:58 PST 2018


Hi Malhar,

> On 27 Dec 2017, at 00:56, Malhar Thakkar via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hello everyone,
> 
> In the file lib/CodeGen/AsmPrinter/AsmPrinter.cpp, I would like to obtain an MCInst corresponding to its MachineInstr. Can anyone tell me a way to do that?

Usually, it happens in lib/Target/X86/X86MCInstLower.cpp, the X86MCInstLower class is used by the X86AsmPrinter (subclass of the AsmPrinter).

> 
> If that is not possible, then, I would like to know if a given MachineInstr is an lea instruction and I would like to know if the symbol involved with this lea instruction is a jump-table.

You can identify if a MachineInstr is a lea instruction by using something similar to the function isLEA from lib/Target/X86/X86OptimizeLEAs.cpp.
You can also identify if any of the operands of a MachineInstruction is a jump-table reference by iterating over all the MachineOperands and checking if it’s a jump-table index using the function isJTI.

> For instance, given a MachineInstr, I would like to know if it is of the following form.
> 
> leaq	LJTI0_0(%rip), %rax
> 
> Also, say, I want to add custom labels (some string) while generating assembly right before emitting such lea instructions, how do I do that given that OutStreamer->EmitLabel() takes MCSymbol as an argument and not a string/StringRef?

You will need to create an MCSymbol from a string. The class MCContext is able to create symbols and manage them, so you should be able to create a symbol and pass it to EmitLabel using getOrCreateSymbol or any of the functions in include/llvm/MC/MCContext.h.

> 
> My goal: To find lea instructions corresponding to jump-tables and emit a custom label before such instructions and also maintain a count of how many such lea instructions are there for each jump-table (using a DenseMap).

> 
> I have a workaround for all the problems stated above but I want to write a cleaner solution.
> For finding an lea instruction and emitting a label before actually emitting it, I have modified the EmitInstruction() function in MCAsmStreamer.cpp wherein I keep track of how many lea instructions are there for each jump-table using a DenseMap.
How are you planning to consume that DenseMap? If you only want to dump it you can probably keep track of everything in X86AsmPrinter::EmitInstruction (lib/Target/X86/X86MCInstLower.cpp).

I recommend doing everything in the target-specific code as you are only interested in x86 specifics.

Cheers,

— 
Francis Visoiu Mistrih
> As I require the number of lea instructions per jump-table in AsmPrinter.cpp, I have created a function in MCAsmStreamer.cpp which returns the DenseMap (added a declaration of this function in MCStreamer.h).
> 
> Is there a way to do both of the aforementioned bullet points inside AsmPrinter.cpp without having to call a function in MCAsmStreamer?
> 
> Thank you.
> 
> Regards,
> Malhar
> 
>> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180102/d387c736/attachment.html>


More information about the llvm-dev mailing list