Does LLVM optimize variable to constant value in the compiling time?

Considering the following codes:

int x = 21;

if(x > 20) {

p = &C;

} else {

p = &E;

}

I looked into the bitcode that LLVM generates, it still considers the ‘else’ branch, which will be never reached during the runtime.

Thanks,

Robert

From: llvmdev-bounces@cs.uiuc.edu [mailto:llvmdev-bounces@cs.uiuc.edu]
On Behalf Of Robert Sandra
Subject: [LLVMdev] Does LLVM optimize variable to constant value in the compiling time?

Considering the following codes:

int x = 21;
if(x > 20) {
p = &C;
} else {
p = &E;
}

I looked into the bitcode that LLVM generates, it still considers the 'else'
branch, which will be never reached during the runtime.

What version of LLVM? What happens if you enable optimization?

- Chuck

THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.

Did you turn on the optimization?

Dmitri

By optimization, do you mean enable the optimization whiling building llvm or use optimization flag while using clang command to get the bitcode file?

In my case, I disable the optimization when I built llvm, and I use the following command to get the bitcode:

clang -O0 -emit-llvm hello.c -c -o hello.bc

To run the pass:

opt -load …/…/…/Debug+Asserts/lib/Hello.so -hello < hello.bc > /dev/null

The llvm version I am using is 3.3svn.

Thanks.

Hello

I looked into the bitcode that LLVM generates, it still considers the 'else'
branch, which will be never reached during the runtime.

Have you forgotten about -O2 option?

Here we go:
$ cat test.c
int C;
int E;

int* foo() {
  int*p;
  int x = 21;
  if(x > 20) {
    p = &C;
  } else {
    p = &E;
  }

  return p;
}

$ clang -S -O2 test.c -emit-llvm -o -
; ModuleID = 'test.c'
target datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.7.0"

@C = common global i32 0, align 4
@E = common global i32 0, align 4

define i32* @foo() nounwind uwtable readnone ssp {
  ret i32* @C
}

From: Robert Sandra [mailto:robert.sandra0712@gmail.com]
Subject: Re: [LLVMdev] Does LLVM optimize variable to constant value in the compiling time?

By optimization, do you mean enable the optimization whiling building llvm or
use optimization flag while using clang command to get the bitcode file?

The optimization when using clang.

I use the following command to get the bitcode:

clang -O0 -emit-llvm hello.c -c -o hello.bc

Then I would expect the output to reflect your exact C statements, with no attempt at optimization. I think even -O1 will clean this up.

The llvm version I am using is 3.3svn.

Always good to state the version you're using.

- Chuck

THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.