[LLVMdev] Interesting optimization failure.

Richard Pennington rich at pennware.com
Wed Jul 16 02:29:20 PDT 2008


Hi,

I have been working on using arrays and structs as first class objects.

I have this small function:

int main()
{
     char ar[10] = "12";
     return ar[4];
}

If I compile with -O1 I get this bitcode:

; ModuleID = 't0011.bc'
target datalayout = 
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-s0:0:64-f80:32:32"
target triple = "i686-pc-linux-gnu"

define i32 @main() {
entry:
         %retval = alloca i32            ; <i32*> [#uses=3]
         store i32 0, i32* %retval
         %ar = alloca [10 x i8]          ; <[10 x i8]*> [#uses=2]
         store [10 x i8] c"12\00\00\00\00\00\00\00\00", [10 x i8]* %ar
         getelementptr [10 x i8]* %ar, i32 0, i32 4              ; 
<i8*>:0 [#uses=1]
         load i8* %0             ; <i8>:1 [#uses=1]
         sext i8 %1 to i32               ; <i32>:2 [#uses=1]
         store i32 %2, i32* %retval
         br label %return

return:         ; preds = %entry
         load i32* %retval               ; <i32>:3 [#uses=1]
         ret i32 %3
}

i.e. it returns ar[4].

If I compile with -O2 I get:
; ModuleID = 't0011.bc'
target datalayout = 
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-s0:0:64-f80:32:32"
target triple = "i686-pc-linux-gnu"

define i32 @main() nounwind  {
entry:
         ret i32 49
}

i.e. it returns ar[0]!

One of the standard compile optimization passes is acting up. Any hints?

-Rich



More information about the llvm-dev mailing list