1 ; RUN: opt < %s -basicaa -tbaa -licm -S | FileCheck %s 2 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128" 3 4 @X = global i32 7 ; <i32*> [#uses=4] 5 6 define void @test1(i32 %i) { 7 Entry: 8 br label %Loop 9 ; CHECK: @test1 10 ; CHECK: Entry: 11 ; CHECK-NEXT: load i32* @X 12 ; CHECK-NEXT: br label %Loop 13 14 15 Loop: ; preds = %Loop, %0 16 %j = phi i32 [ 0, %Entry ], [ %Next, %Loop ] ; <i32> [#uses=1] 17 %x = load i32* @X ; <i32> [#uses=1] 18 %x2 = add i32 %x, 1 ; <i32> [#uses=1] 19 store i32 %x2, i32* @X 20 %Next = add i32 %j, 1 ; <i32> [#uses=2] 21 %cond = icmp eq i32 %Next, 0 ; <i1> [#uses=1] 22 br i1 %cond, label %Out, label %Loop 23 24 Out: 25 ret void 26 ; CHECK: Out: 27 ; CHECK-NEXT: store i32 %x2, i32* @X 28 ; CHECK-NEXT: ret void 29 30 } 31 32 define void @test2(i32 %i) { 33 Entry: 34 br label %Loop 35 ; CHECK: @test2 36 ; CHECK: Entry: 37 ; CHECK-NEXT: %.promoted = load i32* getelementptr inbounds (i32* @X, i64 1) 38 ; CHECK-NEXT: br label %Loop 39 40 Loop: ; preds = %Loop, %0 41 %X1 = getelementptr i32* @X, i64 1 ; <i32*> [#uses=1] 42 %A = load i32* %X1 ; <i32> [#uses=1] 43 %V = add i32 %A, 1 ; <i32> [#uses=1] 44 %X2 = getelementptr i32* @X, i64 1 ; <i32*> [#uses=1] 45 store i32 %V, i32* %X2 46 br i1 false, label %Loop, label %Exit 47 48 Exit: ; preds = %Loop 49 ret void 50 ; CHECK: Exit: 51 ; CHECK-NEXT: store i32 %V, i32* getelementptr inbounds (i32* @X, i64 1) 52 ; CHECK-NEXT: ret void 53 } 54 55 56 57 define void @test3(i32 %i) { 58 ; CHECK: @test3 59 br label %Loop 60 Loop: 61 ; Should not promote this to a register 62 %x = load volatile i32* @X 63 %x2 = add i32 %x, 1 64 store i32 %x2, i32* @X 65 br i1 true, label %Out, label %Loop 66 67 ; CHECK: Loop: 68 ; CHECK-NEXT: load volatile 69 70 Out: ; preds = %Loop 71 ret void 72 } 73 74 ; PR8041 75 define void @test4(i8* %x, i8 %n) { 76 ; CHECK: @test4 77 %handle1 = alloca i8* 78 %handle2 = alloca i8* 79 store i8* %x, i8** %handle1 80 br label %loop 81 82 loop: 83 %tmp = getelementptr i8* %x, i64 8 84 store i8* %tmp, i8** %handle2 85 br label %subloop 86 87 subloop: 88 %count = phi i8 [ 0, %loop ], [ %nextcount, %subloop ] 89 %offsetx2 = load i8** %handle2 90 store i8 %n, i8* %offsetx2 91 %newoffsetx2 = getelementptr i8* %offsetx2, i64 -1 92 store i8* %newoffsetx2, i8** %handle2 93 %nextcount = add i8 %count, 1 94 %innerexitcond = icmp sge i8 %nextcount, 8 95 br i1 %innerexitcond, label %innerexit, label %subloop 96 97 ; Should have promoted 'handle2' accesses. 98 ; CHECK: subloop: 99 ; CHECK-NEXT: phi i8* [ 100 ; CHECK-NEXT: %count = phi i8 [ 101 ; CHECK-NEXT: store i8 %n 102 ; CHECK-NOT: store 103 ; CHECK: br i1 104 105 innerexit: 106 %offsetx1 = load i8** %handle1 107 %val = load i8* %offsetx1 108 %cond = icmp eq i8 %val, %n 109 br i1 %cond, label %exit, label %loop 110 111 ; Should not have promoted offsetx1 loads. 112 ; CHECK: innerexit: 113 ; CHECK: %val = load i8* %offsetx1 114 ; CHECK: %cond = icmp eq i8 %val, %n 115 ; CHECK: br i1 %cond, label %exit, label %loop 116 117 exit: 118 ret void 119 } 120 121 define void @test5(i32 %i, i32** noalias %P2) { 122 Entry: 123 br label %Loop 124 ; CHECK: @test5 125 ; CHECK: Entry: 126 ; CHECK-NEXT: load i32* @X 127 ; CHECK-NEXT: br label %Loop 128 129 130 Loop: ; preds = %Loop, %0 131 %j = phi i32 [ 0, %Entry ], [ %Next, %Loop ] ; <i32> [#uses=1] 132 %x = load i32* @X ; <i32> [#uses=1] 133 %x2 = add i32 %x, 1 ; <i32> [#uses=1] 134 store i32 %x2, i32* @X 135 136 store volatile i32* @X, i32** %P2 137 138 %Next = add i32 %j, 1 ; <i32> [#uses=2] 139 %cond = icmp eq i32 %Next, 0 ; <i1> [#uses=1] 140 br i1 %cond, label %Out, label %Loop 141 142 Out: 143 ret void 144 ; CHECK: Out: 145 ; CHECK-NEXT: store i32 %x2, i32* @X 146 ; CHECK-NEXT: ret void 147 148 } 149 150 151 ; PR14753 - Preserve TBAA tags when promoting values in a loop. 152 define void @test6(i32 %n, float* nocapture %a, i32* %gi) { 153 entry: 154 store i32 0, i32* %gi, align 4, !tbaa !0 155 %cmp1 = icmp slt i32 0, %n 156 br i1 %cmp1, label %for.body.lr.ph, label %for.end 157 158 for.body.lr.ph: ; preds = %entry 159 br label %for.body 160 161 for.body: ; preds = %for.body.lr.ph, %for.body 162 %storemerge2 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ] 163 %idxprom = sext i32 %storemerge2 to i64 164 %arrayidx = getelementptr inbounds float* %a, i64 %idxprom 165 store float 0.000000e+00, float* %arrayidx, align 4, !tbaa !3 166 %0 = load i32* %gi, align 4, !tbaa !0 167 %inc = add nsw i32 %0, 1 168 store i32 %inc, i32* %gi, align 4, !tbaa !0 169 %cmp = icmp slt i32 %inc, %n 170 br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge 171 172 for.cond.for.end_crit_edge: ; preds = %for.body 173 br label %for.end 174 175 for.end: ; preds = %for.cond.for.end_crit_edge, %entry 176 ret void 177 178 ; CHECK: for.body.lr.ph: 179 ; CHECK-NEXT: %gi.promoted = load i32* %gi, align 4, !tbaa !0 180 ; CHECK: for.cond.for.end_crit_edge: 181 ; CHECK-NEXT: store i32 %inc, i32* %gi, align 4, !tbaa !0 182 } 183 184 !0 = metadata !{metadata !"int", metadata !1} 185 !1 = metadata !{metadata !"omnipotent char", metadata !2} 186 !2 = metadata !{metadata !"Simple C/C++ TBAA"} 187 !3 = metadata !{metadata !"float", metadata !1} 188