Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find loops in LLVM bytecode

Tags:

llvm

clang

I want to find simple loops in LLVM bytecode, and extract the basic information of the loop.

For example:

 for (i=0; i<1000; i++)
    sum += i;

I want to extract the bound [0, 1000), the loop variable "i" and the loop body (sum += i).
What should I do?

I read the LLVM API document, and find some useful classes like "Loop", "LoopInfo".
But I do not know how to use them in detail.

Could you please give me some help? A detailed usage may be more helpful.

like image 532
Napoleon Avatar asked Sep 23 '15 00:09

Napoleon


1 Answers

If you do not want to use the pass manager, you might need to call the Analyze method in the llvm::LoopInfoBase class on each function in the IR (assuming you are using LLVM-3.4). However, the Analyze method takes the DominatorTree of each function as input, which you have to generate at first. Following codes are what I tested with LLVM-3.4 (assuming you have read the IR file and converted it into a Module* named as module):

for(llvm::Module::iterator func = module->begin(), y=module->end(); func!=y; func++){
        //get the dominatortree of the current function
        llvm::DominatorTree* DT = new llvm::DominatorTree();         
        DT->DT->recalculate(*func);
        //generate the LoopInfoBase for the current function
        llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>* KLoop = new llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>();
        KLoop->releaseMemory();
        KLoop->Analyze(DT->getBase());        
}

Basically, with KLoop generated, you get all kinds of LOOP information in the IR level. You can refer APIs in the LoopInfoBase class for details. By the way, you might want to add following headers: "llvm/Analysis/LoopInfo.h" "llvm/Analysis/Dominators.h".

like image 198
Junxzm Avatar answered Oct 01 '22 13:10

Junxzm