LLVM中Loop的getSmallConstantTripCount方法的使用
在我的 pass 中,我添加了 LoopInfo
作为必需的 pass。然后我想打印每个循环的恒定循环行程计数(如果有的话)。然而,每次我调用 getSmallConstantTripCount 时,它都会返回 0,即使对于一个非常简单的循环也是如此:
for(i=0; i<3; ++i) {;}
知道为什么吗?
In my pass, I add LoopInfo
as a required pass. Then I'd like to print the constant loop trip count of each loop if it has one. However, every time I called getSmallConstantTripCount
, it returns 0, even for a very simple loop:
for(i=0; i<3; ++i) {;}
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LLVM有一个原则,就是让每个部分做最少的工作。 LoopInfo::getSmallConstantTripCount 不做任何花哨的分析,它寻找一个具有单个后沿的简单循环,每次将值递增 1,并使用 != 与常量整数进行比较。
当您编译在 -O0 处编写的代码时,每个“i < 3”实际上会导致从内存加载以读取“i”的最新值。 LoopInfo 当然不会进行必要的分析类型来确定不需要内存访问,这是“opt -mem2reg”的工作。尝试运行该优化,也许可以在代码上使用 -instcombine -loopsimplify -loop-rotate 使其达到 getSmallConstantTripCount 将处理的形状。
LLVM has a principle of making each part do the least amount of work. LoopInfo::getSmallConstantTripCount doesn't do any fancy analysis, it looks for a simple loop with a single backedge that increments a value by 1 each time and is compared using != against a constant integer.
When you compile the code you wrote at -O0, every "i < 3" actually causes a load from memory to read in the latest value of 'i'. LoopInfo certainly isn't going to do the type of analysis necessary to figure out that the memory accesses aren't needed, that's the job of "opt -mem2reg". Try running that optimization, and maybe -instcombine -loopsimplify -loop-rotate over the code to get it into the shape the getSmallConstantTripCount will handle.