How to label instructions in LLVM and identify those instructions after compilation? - llvm

My question is how to set a label for instructions in LLVM optimization passes. Actually, my goal is to find a way to extract some marked LLVM IR instructions after compilation in ELF metadata of the produced executable file. What can be the most convenient way to do this? My idea was to add a label to these desired instructions in order to find them after compilation with their labels.
P.S. I am marking these instructions during an LLVM pass.
Thanks

Related

is there a way to recover variable names in llvm generated code?

Is there a way to recover the source code variables in llvm bytecode? I've looked into debug information that llvm provides but I don't think they are stored.

Checking LLVM LTO & ThinLTO optimizations?

Normally, after compiler optimizations we get textual LLVM IR, so that we can compare the IR's before and after optimizations and reason about. In LTO, we usually input IR bitcode files to the linker (lld) and get native object files. Is there any way to get single monolithic LLVM IR (textual representation) after LTO passes? And Is there any effective way to analyze the object code to find out the optimizations other than just seeing text section of object file.
Thanks
Please tell me, if you need more information!
LTO optimizations are more or less the same that gets applied to the code during normal compilation. The difference is that the module being optimized comes from linking all the modules of a program.
So, you can just compile all your source to LLVM IR (with -flto, for instance), then link object files (which actually are bitcode files) using llvm-link and then play with optimizations running opt on this resulting bitcode. The list of passes applied during LTO stage can be seen in lib/Transforms/IPO/PassManagerBuilder.cpp, populateLTOPassManager(). There is also a nice opt option called -print-after to emit textual IR representation after applied given pass.

How do I parse LLVM IR

I have LLVM IR code in text format. What I wanna do is to be able to parse it and modify that code. Is there an API which can help in parsing the LLVM IR code? What libraries should I have in my system? At this moment I have clang compiler installed as well LLVM, as I can use commands such as llc, opt and llvm-link.
LLVM is primarily a C++ library. It has all the tools you can imagine to parse, manipulate and produce IR in both textual and bitcode (binary) formats.
To get started, take a look at the llvm::ParseIRFile function, defined in header include/llvm/Support/IRReader.h.
The best way to proceed would be to download the LLVM source code and build it, following these instructions. It's then easy to write your own code that uses the LLVM libraries.

binary generation from LLVM

How does one generate executable binaries from the c++ side of LLVM?
I'm currently writing a toy compiler, and I'm not quite sure how to do the final step of creating an executable from the IR.
The only solution I currently see is to write out the bitcode and then call llc using system or the like. Is there a way to do this from the c++ interface instead?
This seems like it would be a common question, but I can't find anything on it.
LLVM does not ship the linker necessary to perform this task. It can only write out as assembler and then invoke the system linker to deal with it. You can see the source code of llvm-ld to see how it's done.

How to embed LLVM?

The LLVM Core project consists of:
Compiler - converts source code to LLVM IR
VM - executes compiled IR code
How can I embed the VM to a C++ application?
The LLVM is really a collection of libraries that you can link to, so it's pretty easy to embed. More often the LLVM takes IR that you generate and compiles it directly to machine code. There is also a library available to interpret and execute IR for platforms that do not support JIT compilation.
There's a pretty good tutorial available on the LLVM website here: http://llvm.org/docs/tutorial/. I suggest that you go through that and then ask more specific questions if you have them.
Take a look at the HowToUseJIT example in LLVM.