[LLVMdev] How to keep FunctionPass analysis result alive in Module Pass?

John Criswell criswell at illinois.edu
Fri Mar 9 14:20:00 PST 2012


On 3/9/12 4:10 PM, Fan Long wrote:
> Hello,
> 	I am trying to write a new ModulePass using LoopInfo analysis result, but it seems I misunderstand some concept about PassManager. Basically I want to keep LoopInfo analysis result alive. Here is an example showing the problem I encountered, assuming I already addRequired<llvm::LoopInfo>() in getAnalysisUsage:
> 	
> 	void foo(llvm::Function *F1, llvm::Function *F2) {
> 		llvm::LoopInfo *LI1, LI2;
> 		LI1 =&getAnalysis<llvm::LoopInfo>(*F1);
> 		llvm::Loop* L1 = LI1->getLoopFor(F1->begin());
> 		LI2 =&getAnalysis<llvm::LoopInfo>(*F2);
> 		llvm::Loop* L2 = LI2->getLoopFor(F2->begin());
> 		L1->dump();  // crash
> 		L2->dump();
> 	}
>
> 	I checked why this program crashes. It is because the getAnalysis returns same LoopInfo instance. Each time it clears previous results and run it on the new function. Thus it invalidate the pointer L1 after calling&getAnalysis<llvm::LoopInfo>(*F2).

To the best of my knowledge, the LLVM pass manager never preserves a 
FunctionPass analysis that is requested by a ModulePass; every time you 
call getAnalysis for a function, the FunctionPass is re-run.
> 	
> 	My questions is whether there is a way to get around this, and to keep the analysis result of Function Pass of all functions alive during my Module Pass? I am using LLVM-3.1-svn version. I would really appreciate your help!

The trick I've used is to structure the code so that getAnalysis<>() is 
only called once per function.  For example, your ModulePass can have a 
std::map that maps between Function * and LoopInfo *.  You then provide 
a method getLoopInfo(Function * F) that checks to see if F is in the 
map.  If it is, it returns what is in the map.  If it isn't, it calls 
getAnalysis on F, stores the result in the map, and returns the LoopInfo 
pointer.

This is important not only for functionality (in your case) but also for 
performance; you don't want to calculate an analysis twice for the same 
function.

-- John T.

>
> Best,
> Fan
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

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


More information about the llvm-dev mailing list