|
| 1 | +; RUN: opt < %s -S -passes=instcombine | FileCheck %s |
| 2 | + |
| 3 | +target datalayout = "e-p:32:32" |
| 4 | + |
| 5 | +; CHECK-LABEL: @test1 |
| 6 | +define void @test1(i32 %N, i32 %k, ptr %A) { |
| 7 | +entry: |
| 8 | + br label %for.cond |
| 9 | + |
| 10 | +for.cond: |
| 11 | + %i = phi i32 [ 0, %entry ], [ %inc, %for.body ] |
| 12 | + %cmp = icmp ult i32 %i, %N |
| 13 | + br i1 %cmp, label %for.body, label %for.end |
| 14 | + |
| 15 | +; CHECK-LABEL: for.body: |
| 16 | +; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr %A, i32 %k |
| 17 | +; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr [[GEP]], i32 %i |
| 18 | +for.body: |
| 19 | + %add = add i32 %i, %k |
| 20 | + %arrayidx = getelementptr inbounds i8, ptr %A, i32 %add |
| 21 | + store i8 1, ptr %arrayidx, align 4 |
| 22 | + %inc = add i32 %i, 1 |
| 23 | + br label %for.cond |
| 24 | + |
| 25 | +for.end: |
| 26 | + ret void |
| 27 | +} |
| 28 | + |
| 29 | +; CHECK-LABEL: @test2 |
| 30 | +define void @test2(i32 %N, i32 %k, ptr %A) { |
| 31 | +entry: |
| 32 | + br label %for.cond |
| 33 | + |
| 34 | +for.cond: |
| 35 | + %i = phi i32 [ 0, %entry ], [ %inc, %for.body ] |
| 36 | + %cmp = icmp ult i32 %i, %N |
| 37 | + br i1 %cmp, label %for.body, label %for.end |
| 38 | + |
| 39 | +; CHECK-LABEL: for.body: |
| 40 | +; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr %A, i32 %mul |
| 41 | +; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr [[GEP]], i32 %i |
| 42 | +for.body: |
| 43 | + %mul = mul i32 %k, 42 |
| 44 | + %add = add i32 %i, %mul |
| 45 | + %arrayidx = getelementptr inbounds i8, ptr %A, i32 %add |
| 46 | + store i8 1, ptr %arrayidx, align 4 |
| 47 | + %inc = add i32 %i, 1 |
| 48 | + br label %for.cond |
| 49 | + |
| 50 | +for.end: |
| 51 | + ret void |
| 52 | +} |
| 53 | + |
| 54 | +; CHECK-LABEL: @test3 |
| 55 | +define void @test3(i32 %N, ptr %A, i32 %val) { |
| 56 | +entry: |
| 57 | + br label %for.cond |
| 58 | + |
| 59 | +for.cond: |
| 60 | + %i = phi i32 [ 0, %entry ], [ %inc6, %for.inc5 ] |
| 61 | + %cmp = icmp ult i32 %i, %N |
| 62 | + br i1 %cmp, label %for.body, label %for.end7 |
| 63 | + |
| 64 | +for.body: |
| 65 | + br label %for.cond1 |
| 66 | + |
| 67 | +for.cond1: |
| 68 | + %j = phi i32 [ 0, %for.body ], [ %inc, %for.body3 ] |
| 69 | + %cmp2 = icmp ult i32 %j, %N |
| 70 | + br i1 %cmp2, label %for.body3, label %for.inc5 |
| 71 | + |
| 72 | +; CHECK-LABEL: for.body3: |
| 73 | +; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr %A, i32 %i |
| 74 | +; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr [[GEP]], i32 %j |
| 75 | +for.body3: |
| 76 | + %add = add i32 %i, %j |
| 77 | + %arrayidx = getelementptr inbounds i8, ptr %A, i32 %add |
| 78 | + store i8 1, ptr %arrayidx, align 4 |
| 79 | + %inc = add i32 %j, 1 |
| 80 | + br label %for.cond1 |
| 81 | + |
| 82 | +for.inc5: |
| 83 | + %inc6 = add i32 %i, 1 |
| 84 | + br label %for.cond |
| 85 | + |
| 86 | +for.end7: |
| 87 | + ret void |
| 88 | +} |
| 89 | + |
| 90 | +; CHECK-LABEL: @test4 |
| 91 | +define void @test4(i32 %N, ptr %A, i32 %val) { |
| 92 | +entry: |
| 93 | + br label %for.cond |
| 94 | + |
| 95 | +for.cond: |
| 96 | + %i = phi i32 [ 0, %entry ], [ %inc6, %for.inc5 ] |
| 97 | + %cmp = icmp ult i32 %i, %N |
| 98 | + br i1 %cmp, label %for.body, label %for.end7 |
| 99 | + |
| 100 | +for.body: |
| 101 | + br label %for.cond1 |
| 102 | + |
| 103 | +for.cond1: |
| 104 | + %j = phi i32 [ 0, %for.body ], [ %inc, %for.body3 ] |
| 105 | + %cmp2 = icmp ult i32 %j, %N |
| 106 | + br i1 %cmp2, label %for.body3, label %for.inc5 |
| 107 | + |
| 108 | +; CHECK-LABEL: for.body3: |
| 109 | +; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr %A, i32 %mul |
| 110 | +; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr [[GEP]], i32 %j |
| 111 | +for.body3: |
| 112 | + %mul = mul i32 %i, %N |
| 113 | + %add = add i32 %mul, %j |
| 114 | + %arrayidx = getelementptr inbounds i8, ptr %A, i32 %add |
| 115 | + store i8 1, ptr %arrayidx, align 4 |
| 116 | + %inc = add i32 %j, 1 |
| 117 | + br label %for.cond1 |
| 118 | + |
| 119 | +for.inc5: |
| 120 | + %inc6 = add i32 %i, 1 |
| 121 | + br label %for.cond |
| 122 | + |
| 123 | +for.end7: |
| 124 | + ret void |
| 125 | +} |
| 126 | + |
| 127 | +; We can't use inbounds here because the add operand doesn't dominate the loop |
| 128 | +; CHECK-LABEL: @test5 |
| 129 | +define void @test5(i32 %N, ptr %A, ptr %B) { |
| 130 | +entry: |
| 131 | + br label %for.cond |
| 132 | + |
| 133 | +for.cond: |
| 134 | + %i = phi i32 [ 0, %entry ], [ %inc, %for.body ] |
| 135 | + %cmp = icmp ult i32 %i, %N |
| 136 | + br i1 %cmp, label %for.body, label %for.end |
| 137 | + |
| 138 | +; CHECK-LABEL: for.body: |
| 139 | +; CHECK: [[GEP:%.*]] = getelementptr i8, ptr %A, i32 %i |
| 140 | +; CHECK-NEXT: %arrayidx = getelementptr i8, ptr [[GEP]], i32 %0 |
| 141 | +for.body: |
| 142 | + %0 = load i32, ptr %B, align 4 |
| 143 | + %add = add i32 %i, %0 |
| 144 | + %arrayidx = getelementptr inbounds i8, ptr %A, i32 %add |
| 145 | + store i8 1, ptr %arrayidx, align 4 |
| 146 | + %inc = add i32 %i, 1 |
| 147 | + br label %for.cond |
| 148 | + |
| 149 | +for.end: |
| 150 | + ret void |
| 151 | +} |
| 152 | + |
| 153 | +; We can't use inbounds here because we don't have a loop |
| 154 | +; CHECK-LABEL: @test6 |
| 155 | +define void @test6(i32 %k, i32 %j, ptr %A) { |
| 156 | +entry: |
| 157 | + %cmp = icmp ugt i32 %k, 10 |
| 158 | + br i1 %cmp, label %if.then, label %if.else |
| 159 | + |
| 160 | +if.then: |
| 161 | + br label %if.end |
| 162 | + |
| 163 | +if.else: |
| 164 | + br label %if.end |
| 165 | + |
| 166 | +; CHECK-LABEL: if.end: |
| 167 | +; CHECK: [[GEP:%.*]] = getelementptr i8, ptr %A, i32 %val |
| 168 | +; CHECK-NEXT: %arrayidx = getelementptr i8, ptr [[GEP]], i32 %j |
| 169 | +if.end: |
| 170 | + %val = phi i32 [ 0, %if.then ], [ 1, %if.else ] |
| 171 | + %add = add i32 %val, %j |
| 172 | + %arrayidx = getelementptr inbounds i8, ptr %A, i32 %add |
| 173 | + store i8 1, ptr %arrayidx, align 4 |
| 174 | + ret void |
| 175 | +} |
| 176 | + |
| 177 | +; Inbounds gep would be invalid because of potential overflow in the add, though |
| 178 | +; we don't convert to gep+gep as we insert an explicit sext instead of using i16 |
| 179 | +; gep offset. |
| 180 | +; CHECK-LABEL: @test7 |
| 181 | +define void @test7(i16 %N, i16 %k, ptr %A) { |
| 182 | +entry: |
| 183 | + br label %for.cond |
| 184 | + |
| 185 | +for.cond: |
| 186 | + %i = phi i16 [ 0, %entry ], [ %inc, %for.body ] |
| 187 | + %cmp = icmp ult i16 %i, %N |
| 188 | + br i1 %cmp, label %for.body, label %for.end |
| 189 | + |
| 190 | +; CHECK-LABEL: for.body: |
| 191 | +; CHECK: %add = add i16 %i, %k |
| 192 | +; CHECK-NEXT: [[SEXT:%.*]] = sext i16 %add to i32 |
| 193 | +; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr %A, i32 [[SEXT]] |
| 194 | +for.body: |
| 195 | + %add = add i16 %i, %k |
| 196 | + %arrayidx = getelementptr inbounds i8, ptr %A, i16 %add |
| 197 | + store i8 1, ptr %arrayidx, align 4 |
| 198 | + %inc = add i16 %i, 1 |
| 199 | + br label %for.cond |
| 200 | + |
| 201 | +for.end: |
| 202 | + ret void |
| 203 | +} |
| 204 | + |
| 205 | +; %i starts at 1 so we can't use inbounds |
| 206 | +; CHECK-LABEL: @test8 |
| 207 | +define void @test8(i32 %N, i32 %k, ptr %A) { |
| 208 | +entry: |
| 209 | + br label %for.cond |
| 210 | + |
| 211 | +for.cond: |
| 212 | + %i = phi i32 [ 1, %entry ], [ %inc, %for.body ] |
| 213 | + %cmp = icmp ult i32 %i, %N |
| 214 | + br i1 %cmp, label %for.body, label %for.end |
| 215 | + |
| 216 | +; CHECK-LABEL: for.body: |
| 217 | +; CHECK: [[GEP:%.*]] = getelementptr i8, ptr %A, i32 %i |
| 218 | +; CHECK-NEXT: %arrayidx = getelementptr i8, ptr [[GEP]], i32 %k |
| 219 | +for.body: |
| 220 | + %add = add i32 %i, %k |
| 221 | + %arrayidx = getelementptr inbounds i8, ptr %A, i32 %add |
| 222 | + store i8 1, ptr %arrayidx, align 4 |
| 223 | + %inc = add i32 %i, 1 |
| 224 | + br label %for.cond |
| 225 | + |
| 226 | +for.end: |
| 227 | + ret void |
| 228 | +} |
0 commit comments