[LLVMdev] Get the loop trip count variable

Zheng Wang jason.wangz at gmail.com
Tue Apr 6 09:47:23 PDT 2010

Thanks a lot for your guys' help!!!

I guess once I am able to get *V* (which probably is a pointer to a
Value object), then, I can instrument some code at the IR level to
dump V. As long as I maintain V at this pass stage, I should be able
to dump the loop trip count. This is true, isn't it?

Basically, what I am going to do is to add a function call before the
loop body, such as:

for (int i=0; i<num; i++)

of course, this may have some side effects. If the trip count is a
function call, and I need to rewrite the loop header to avoid calling
the function twice. For example, if the loop header is:

for (int i=0; i<foo(a,b,c); i++)

then I should rewrite it as:

tmp_var = foo(a,b,c);

for (int i=0; i<tmp_var; i++)

This looks quite complicated!

Does anyone has any ideal about how easy to insert some basic blocks
and to rewrite the loop header in LLVM?


On 6 April 2010 17:27, Zakk <zakk0610 at gmail.com> wrote:
> Hi, you should run some loop transformation passes(loop rotate, loop
> simplify, -indvar), and then get TripCount.
> loopinfo.h
> /// getTripCount - Return a loop-invariant LLVM value indicating the number
> of
> /// times the loop will be executed. Note that this means that the backedge
> /// of the loop executes N-1 times. If the trip-count cannot be determined,
> /// this returns null.
> inline Value *getTripCount() const {
> Canonical loops will end with a 'cmp ne I, V', where I is the incremented
> canonical induction variable and V is the trip count of the loop.
> 2010/4/6 Zheng Wang <jason.wangz at gmail.com>
>> Hello,
>> I am wondering whether I can get the variable name of loop trip count in
>> LLVM?
>> For example,
>> int NUM;
>> NUM=atoi(argv[i]);
>> for (int i=0; i<NUM; i++)
>> {
>>    ...
>> }
>> How can I get the corresponding variable name for "NUM"? Then, I can
>> instrument something in the source code to record the loop trip count
>> for a given input data set.
>> BasicBlock* b = L->getHeader();
>> returns the basicblock of the loop header, but I don't know how to
>> extract "NUM" from basicblock b.
>> Cheers,
>> Zheng
>> _______________________________________________
>> LLVM Developers mailing list
>> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
> --
> Best regards,
> Kuan-Hsu
> P

Best regards,

WANG Zheng

More information about the llvm-dev mailing list