[LLVMdev] Get the loop trip count variable

Zheng Wang jason.wangz at gmail.com
Tue Apr 6 10:47:25 PDT 2010


Sorry, I could not the the loop trip count with getTripCount(). I used
a simple program as a test case:

------------------------------------------------------
#include <stdio.h>
int getV(int i)
{
        return i * 2;
}
int main()
{
        int num = 10;
        int sum=0;
        int i;

        for (i=0; i<num; i++)
        {
                sum += getV(i);
        }
        return 0;
}
--------------------------------------------------------

2. Then, I translated it to LLVM bitcode by:

llvm-gcc -emit-llvm -c test.c

3. Then, I performed some optimization passes as:

opt  -loop-rotate -loops -loopvr -indvars -loopsimplify -f < test.o > n.o

4. I tried to dump the loop trip count by my own pass (cfginst):
opt -load lib/libLLVMCFGInst.so -cfginst < n.o

-----------------------------------------------
But I got the following messages:

Function getV doesn't have loops
Function main has 1 loops
loop_trip_count0 0
 could not get the trip count

-------------------------------

My pass looks like:
----------------------------------
virtual bool runOnFunction(Function &F) {
                        LoopInfo &LI = getAnalysis<LoopInfo>();
                        CFGInstCounter++;
                        std::string fname = F.getName();
                        EscapeString(fname);
                        cerr << "Function " << fname;

                        if (LI.empty())
                                cout << " doesn't have loops" << endl;
                        else
                                cout << " has " << LI.end() -
LI.begin() << " loops" << endl;

                        int ii = 0;
                        for (LoopInfo::iterator i = LI.begin(), e =
LI.end(); i != e; ++i) {
                                Loop *TLL = *i;
                                cout << "loop_trip_count is " <<
TLL->getSmallConstantTripCount();
                                Value* v = TLL->getTripCount();
                                if (v != 0)
                                {
                                        cout << "value=" << *v << endl;
                                }
                                else
                                {
                                        cout << "\n could not get the
trip count" << endl;
                                }

   }
----------------------------

Anything wrong?
On 6 April 2010 17:47, Zheng Wang <jason.wangz at gmail.com> wrote:
> 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:
>
> dumpInfo(FILE_NAME, LINE_NUMBER, VALUE_TYPE, Value)
> ...
> 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);
> dumpInfo(...)
>
> for (int i=0; i<tmp_var; i++)
> {
>   ...
>    tmp_var=foo(a,b,c);
> }
>
> 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?
>
> Cheers,
> Zheng
>
>
> 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
>



-- 
Best regards,

WANG Zheng




More information about the llvm-dev mailing list