input and output values from a loop

Hi there,

I want to get input and output values from a loop. For this I am doing something like this:

DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
CodeExtractor Extractor(DT, *L);
Extractor.findInputsOutputs(inputs, outputs);

When I print the input and output values for the following test code:

void aFunc(void)
{
     int sum=0;
     puts("aFunc()");
     for (int i = 0; i < 10; i++)
     {
         int fact=i*2;
         sum=sum+i+fact;
     }
     printf("%d",sum);
}

I get the following:

Inputs
type i32* name i
type i32* name fact
type i32* name sum

Outputs

However, I was expecting the output to be:

Inputs
type i32* name sum

Outputs
type i32* name sum

To my understanding, i and fact are declared inside the loop, hence they are neither input nor output. Secondly, sum is being used outside the loop, hence should appear in outputs.

Is this the right way of doing it or am i doing some thing wrong?

Complete working code can be found at:

Thanks in advance for help.
Kind regards,

Hi Imran,

It works correctly.

Look at IR:

define i32 @aFunc(i32 %par) #0 {
entry:
  %par.addr = alloca i32, align 4
  %sum = alloca i32, align 4
  %books = alloca [25 x %struct.Book], align 16
  %i = alloca i32, align 4
  %fact = alloca i32, align 4
  store i32 %par, i32* %par.addr, align 4
  store i32 0, i32* %sum, align 4
  %call = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]*
@.str, i32 0, i32 0))
  store i32 0, i32* %i, align 4
  br label %for.cond

for.cond: ; preds = %for.inc, %entry
  %0 = load i32, i32* %i, align 4
  %cmp = icmp slt i32 %0, 10
  br i1 %cmp, label %for.body, label %for.end

for.body: ; preds = %for.cond
  %1 = load i32, i32* %par.addr, align 4
  %mul = mul nsw i32 %1, 2
  store i32 %mul, i32* %fact, align 4
  %2 = load i32, i32* %sum, align 4
  %3 = load i32, i32* %i, align 4
  %add = add nsw i32 %2, %3
  %4 = load i32, i32* %fact, align 4
  %add1 = add nsw i32 %add, %4
  store i32 %add1, i32* %sum, align 4
  %5 = load i32, i32* %i, align 4
  %6 = load i32, i32* %i, align 4
  %idxprom = sext i32 %6 to i64
  %arrayidx = getelementptr inbounds [25 x %struct.Book], [25 x
%struct.Book]* %books, i32 0, i64 %idxprom
  %pages = getelementptr inbounds %struct.Book, %struct.Book* %arrayidx, i32
0, i32 0
  store i32 %5, i32* %pages, align 4
  br label %for.inc

for.inc: ; preds = %for.body
  %7 = load i32, i32* %i, align 4
  %inc = add nsw i32 %7, 1
  store i32 %inc, i32* %i, align 4
  br label %for.cond

for.end: ; preds = %for.cond
  %8 = load i32, i32* %sum, align 4
  %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8],
[3 x i8]* @.str.1, i32 0, i32 0), i32 %8)
  %9 = load i32, i32* %sum, align 4
  ret i32 %9
}

You can see they are defined in Entry before the loop:
  %sum = alloca i32, align 4
  %i = alloca i32, align 4
  %fact = alloca i32, align 4

In the loop they are used as:
  %2 = load i32, i32* %sum, align 4
  store i32 %add1, i32* %sum, align 4

  %7 = load i32, i32* %i, align 4
  store i32 %inc, i32* %i, align 4

Nothing defined in the loop is used outside the loop.

You recommend to add '-mem2reg'.

Kind regards,
Evgeny Astigeevich

Hi Imran,

Do I understand you correctly you want to have C/C++ names instead of LLVM
IR names or in other words to access clang AST from the LLVM context?
When you work with LLVM IR everything you see is elements of the LLVM
assembly language, not elements of the source language. Yes, clang tries to
preserve original names during creation of LLVM IR as much as possible but
the names can be changed by LLVM passes. Depending on the specified target
structures/functions can be modified or additional elements can be inserted.

You need to enable source level debugging information to have a relationship
between IR and the original program source code.
You can read about this here:

Source Level Debugging with LLVM:
http://llvm.org/docs/SourceLevelDebugging.html
C/C++ front-end specific debug information:
http://llvm.org/docs/SourceLevelDebugging.html#c-c-front-end-specific-debug-
information

Of course you can modify the Clang front-end if this is not enough for you.

Kind regards,
Evgeny Astigeevich