clang -O is removing statements

Hi,

I am using clang (latest) to compile C code and process the byte code. However, if I use -O to optimize, it is removing statements for some reason. I am wondering if there is a way to do the optimization of (alloca and store) but still keep all the statements un-touched.

-Jiang

Hi Jiang:

Try opt -sroa, I think that is what you want.

For example:
$ cat fib.c
int fib(int n){
  int a = 10 - n; // Dead statement
  if (n <= 1)
    return 1;
  else
    return fib(n-1) + fib(n-2);
}

$ clang -emit-llvm fib.c -o fib.bc -S
$ opt fib.bc -sroa -S -o fib.sroa.bc

$ cat fib.sroa.bc
...
define i32 @fib(i32 %n) #0 {
  %1 = sub nsw i32 10, %n ; Dead statement
  %2 = icmp sle i32 %n, 1
  br i1 %2, label %3, label %4

; <label>:3 ; preds = %0
  br label %10

; <label>:4 ; preds = %0
  %5 = sub nsw i32 %n, 1
  %6 = call i32 @fib(i32 %5)
  %7 = sub nsw i32 %n, 2
  %8 = call i32 @fib(i32 %7)
  %9 = add nsw i32 %6, %8
  br label %10

; <label>:10 ; preds = %4, %3
  %.0 = phi i32 [ 1, %3 ], [ %9, %4 ]
  ret i32 %.0
}
...