[LLVMdev] clang -O is removing statements

Kito Cheng kito.cheng at gmail.com
Sun Nov 9 22:10:05 PST 2014


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
}
...

On Mon, Nov 10, 2014 at 9:09 AM, Jiang LONG <jiang.long at berkeley.edu> wrote:
> 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
>
> _______________________________________________
> 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