循环头的执行频率
在 LLVM 过程中,我想记录循环的循环次数。我所做的一部分是在每个循环的头块的终止符之前插入一个函数调用。我发现这个方法效果不太好,因为如果一个循环有n次迭代,那么头块就会被执行n+1次。这个事实适用于“for”和“while”循环,但不适用于“do while”循环。我将函数调用插入到“for”和“while”循环的第二个块以及“do while”循环的第一个块。仅当我先验地知道目标循环的类型时,这才有效,而通过纯位码这是不可能的。我的问题是:是否有一种通用的方法(处理各种循环)来插入函数调用,该函数调用的调用次数与迭代次数完全相同。
In a LLVM pass, I'd like to record the loop trip count of a loop. One part I did was inserting a function call before the terminator of the header block of each loop. I found that this method did not work well, because if a loop has n iterations, the header block will be executed n+1 times. This fact holds for "for" and "while" loop, but it does not hold for "do while" loop. I insert the function call to the second block of "for" and "while" loops, and to the first block of "do while" loops. This works only if I know a priori the kind the targeted loop, which is impossible through purely bitcode. My question is: is there a universal way (handling all kinds of loops) to insert a function call, which will be called exactly the same times as the number of iterations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将函数调用放在循环体的开头。你为什么要把它放在标题中?
Put the function call at the start of the body of the loop. Why are you trying to put it in the header?
如果将函数调用插入循环体的开头,您将获得正确的计数。
If you insert the function call in the beginning of the loop body, you will get the correct count.