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

Anton Korobeynikov anton at korobeynikov.info
Fri Feb 22 13:46:54 PST 2013


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
}


--
With best regards, Anton Korobeynikov
Faculty of Mathematics and Mechanics, Saint Petersburg State University



More information about the llvm-dev mailing list