Home | History | Annotate | Download | only in GlobalOpt
      1 ; RUN: opt -globalopt -S < %s | FileCheck %s
      2 
      3 @G1 = internal global i32 5
      4 @G2 = internal global i32 5
      5 @G3 = internal global i32 5
      6 @G4 = internal global i32 5
      7 @G5 = internal global i32 5
      8 
      9 ; CHECK-LABEL: @test1
     10 define internal i32 @test1() norecurse {
     11 ; CHECK-NOT: @G1
     12   store i32 4, i32* @G1
     13   %a = load i32, i32* @G1
     14 ; CHECK: ret
     15   ret i32 %a
     16 }
     17 
     18 ; The load comes before the store which makes @G2 live before the call.
     19 ; CHECK-LABEL: @test2
     20 define internal i32 @test2() norecurse {
     21 ; CHECK-NOT: %G2
     22   %a = load i32, i32* @G2
     23   store i32 4, i32* @G2
     24 ; CHECK: ret
     25   ret i32 %a
     26 }
     27 
     28 ; This global is indexed by a GEP - this makes it partial alias and we bail out.
     29 ; FIXME: We don't actually have to bail out in this case.
     30 
     31 ; CHECK-LABEL: @test3
     32 define internal i32 @test3() norecurse {
     33 ; CHECK-NOT: %G3
     34   %x = getelementptr i32,i32* @G3, i32 0
     35   %a = load i32, i32* %x
     36   store i32 4, i32* @G3
     37 ; CHECK: ret
     38   ret i32 %a
     39 }
     40 
     41 ; The global is casted away to a larger type then loaded. The store only partially
     42 ; covers the load, so we must not demote.
     43 
     44 ; CHECK-LABEL: @test4
     45 define internal i32 @test4() norecurse {
     46 ; CHECK-NOT: %G4
     47   store i32 4, i32* @G4
     48   %x = bitcast i32* @G4 to i64*
     49   %a = load i64, i64* %x
     50   %b = trunc i64 %a to i32
     51 ; CHECK: ret
     52   ret i32 %b
     53 }
     54 
     55 ; The global is casted away to a smaller type then loaded. This one is fine.
     56 
     57 ; CHECK-LABEL: @test5
     58 define internal i32 @test5() norecurse {
     59 ; CHECK-NOT: @G5
     60   store i32 4, i32* @G5
     61   %x = bitcast i32* @G5 to i16*
     62   %a = load i16, i16* %x
     63   %b = zext i16 %a to i32
     64 ; CHECK: ret
     65   ret i32 %b
     66 }
     67 
     68 define i32 @main() norecurse {
     69   %a = call i32 @test1()
     70   %b = call i32 @test2()
     71   %c = call i32 @test3()
     72   %d = call i32 @test4()
     73   %e = call i32 @test5()
     74 
     75   %x = or i32 %a, %b
     76   %y = or i32 %x, %c
     77   %z = or i32 %y, %d
     78   %w = or i32 %z, %e
     79   ret i32 %w
     80 }
     81