[LLVMdev] input and output values from a loop

Evgeny Astigeevich evgeny.astigeevich at arm.com
Thu Jul 2 10:01:12 PDT 2015


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

-----Original Message-----
From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On
Behalf Of Imran Ashraf
Sent: 02 July 2015 17:40
To: llvmdev at cs.uiuc.edu
Subject: [LLVMdev] 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:
https://github.com/imranashraf/llvm-pass-exercise1

Thanks in advance for help.
Kind regards,

_______________________________________________
LLVM Developers mailing list
LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev







More information about the llvm-dev mailing list