0

I was checking llvm's constant propagation pass -sccp, with the following program

int a,b,c;
a=1;
b=2;
c=a+b;

I was expecting an output

  %a = alloca i32, align 4
  %b = alloca i32, align 4
  %c = alloca i32, align 4
  store i32 1, i32* %a, align 4
  store i32 2, i32* %b, align 4
  store i32 3, i32* %c, align 4 //constant propagation

But I am getting the following output

  %a = alloca i32, align 4
  %b = alloca i32, align 4
  %c = alloca i32, align 4
  store i32 1, i32* %a, align 4
  store i32 2, i32* %b, align 4
  %0 = load i32, i32* %a, align 4
  %1 = load i32, i32* %b, align 4
  %add = add nsw i32 %0, %1 //no constant propagation
  store i32 %add, i32* %c, align 4

What I am doing wrong here?

javaDeveloper
  • 1,403
  • 3
  • 28
  • 42

1 Answers1

1

The constant propagation pass, like many other passes, assumes mem2reg has been used on the code first.

Oak
  • 26,231
  • 8
  • 93
  • 152
  • Oak got it thanks. But is there a case in which sccp will fail to detect a constant. – javaDeveloper Apr 26 '16 at 07:07
  • 1
    @RakeshNair I'm sure there are cases - you just demonstrated it doesn't look through stores and loads - but I believe that in practice it's rare for it to fail. – Oak Apr 26 '16 at 07:50