Get the loop trip count variable

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

If you need to instrument the source, you'd probably be better off
doing this with clang purely at the source level; by the time LLVM
loop analyzers can tell "NUM" is the trip count, the name is likely to
be lost due to optimization.

-Eli

Hi Eli,

Could you tell me how to extract the variable name?
The reason I want to do it at the IR level is because I have more
information at this stage, such as, constant propagation and back
edges.

Cheers,
Zheng

You'll have to look at the debug information, if it's still present.
See http://llvm.org/docs/SourceLevelDebugging.html. This is probably
not what you want to do though. The loop trip count may not even be a
simple variable. You might be able to transform the IR to record the
trip count somewhere, but the source is harder.

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

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

You're likely to get bad results if you don't run mem2reg.

-Eli

Hi,

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.

If you need to instrument the source, you'd probably be better off
doing this with clang purely at the source level; by the time LLVM
loop analyzers can tell "NUM" is the trip count, the name is likely to
be lost due to optimization.

You can also use the instrumentation pass (-insert-optimal-edge-instrumenation) to instrument all your code, this adds about 10% runtime overhead.

Also with some tweaking the instrumentation pass could be talked into instrumenting only loop headers so that only the loop counts get recorded but for a code with a whole lot of loops this would be almost equivalent to do the optimal instrumentation. (For a loop with only one block in the body only one counter gets added anyway...)

Andi

Hello Andreas,

Thanks! But I could not find -insert-optimal-edge-instrumenation pass
from "opt -help". Did you mean -insert-edge-profiling?

Cheers,
Zheng

Hi!