How do I add Reg2Mem pass to addRequired() in LLVM? - c++

I want to remove phi nodes from my llvm IR files, so I've read that Reg2Mem pass is a solution (why?).
I can easily use this pass from the opt tool by specifying -reg2mem as argument.
However, I want this functionality be built into my own pass called FlattenO. Therefore, I try to add the Reg2Mem pass as a required dependency of my pass (it should be called automatically then?).
void FlattenO::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<Reg2Mem>();
}
The code above doesn't work, because it cannot find any Reg2Mem pass. What files should I include to use the Reg2Mem pass? Also, please correct my code, so everything should work as intended.

Related

llvm - Pass arguments to a pass

I need to tell the pass to look out for a specific function in the file. And I want to specify which function to look out for 'on the go' i.e when I run the pass. Any idea how I can do that? It's sort of like passing arguments to a function in theory.
Add a command line option using cl::opt<string> and set it when running your pass.
Alternatively, if you are producing an IR from C or C++ using clang, you can utilize __attribute((__annotate__(("foo")))) to mark functions you are interested in.

Is there a way to make a pass over two llvm-ir?

I want to compare two llvm-ir programs function by function. I thought it will be help full if I do it as an LLVM pass where I can have access to CFG of the program. It seems all the passes(Module, Function, ..) were working on single program, How can I do a pass over two programs simultaneously?
I would just run llvm-link (a command-line tool bundled with LLVM) to merge the IR files together first, then use a regular module pass.
I think the function renaming rule in llvm-link is something like renaming f to f.llvm.X where X is the module ID, so your pass could identify pairs by them having the same name prefix before the module ID.

Available Analysis and Transform passes for LLVM

Is there any document on the list of Analysis and Transform passes available for use in the AnalysisUsage::addRequired<> and Pass::geAnalysis<> functions?
I can get a list of passes in http://llvm.org/docs/Passes.html, but it only shows the command line names for the passes. How can I know the underlying pass classes?
Not really, no. Just look at the source. The header files in include/llvm/Analysis/ and include/llvm/Transforms/ will tell you everything you need to know.
Moreover, grepping over the source for getAnalysis< will tell you which passes are used as analyses inside the LLVM source code.

Specify dependency of my LLVM pass on the mem2reg pass

I am writing a ModulePass and invoke it using opt -load. I would require that alloca has been promoted to registers when my pass runs, using the -mem2reg switch for opt.
There is a link which indicates that the PromoteMemoryToRegsiter pass is a transform pass and as such should not be required by my pass. That's a statement from 2010. Does that still hold?
One of the posts I found suggested something like
AU.addRequiredID(PromoteMemoryToRegister::MemoryToRegisterID);
but that contradicted the post I linked above.
So my question is, how to I express this dependency for my pass, if possible? How do I express, in general, such pass dependencies? And what's the difference between a transform pass and, well, another pass?
What's the difference between a transform pass and another pass?
A transform pass is a pass that may invalidate the results of other passes.
How to I express this dependency for my pass?
First of all, I recommend reading the pass-dependency section of the official "how to write a pass" guide. In any case, the correct way to add a dependency between transformation passes is to add one before the other in your pass manager (see the guide section on the pass manager), or, if you just invoke opt, then add all the passes you want in the order you want them to occur, e.g.:
opt -load mypass.so -mem2reg -mypass

LLVM pass creation related

I want to create a pass that will insert a function (that is in some c file) after every load instruction.I know how to insert a function with no arguments.But a function that also have argument. I am not able to insert that.How should i do that?
Thanks in advance.
Use the CallInst::Create static method. It takes an ArrayRef of arguments. By grepping for this method call in the source of LLVM & Clang you can find many examples for using it.