在LLVM字节码中查找循环 [英] Find loops in LLVM bytecode

查看:546
本文介绍了在LLVM字节码中查找循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在LLVM字节码中找到简单循环,并提取循环的基本
信息。

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

例如:

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

我想提取绑定[0,1000),循环变量i b $ b循环体(sum + = i)。

我该怎么办?

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

我阅读LLVM API文档,类Loop,
LoopInfo。

但我不知道如何详细使用它们。

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.

请给我一些帮助?

推荐答案

如果您不想使用传递管理器,可能需要调用在IR中的每个函数的 llvm :: LoopInfoBase 类中分析方法(假设您使用LLVM-3.4)。但是,Analyze方法将每个函数的DominatorTree作为输入,您必须首先生成它。以下代码是我用 LLVM-3.4 (假设您已读取IR文件并将其转换为模块*命名为模块)进行测试:

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());        
}

基本上,生成KLoop后,水平。您可以参考LoopInfoBase类中的API了解详细信息。顺便说一下,您可能想要添加以下标题:
llvm / Analysis / LoopInfo.hllvm / Analysis / Dominators.h

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".

这篇关于在LLVM字节码中查找循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆