1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
4 ;; This tests that the instructions in the entry blocks are sunk into each
7 define i32 @test1(i1 %C, i32 %A, i32 %B) {
10 ; CHECK-NEXT: br i1 [[C:%.*]], label [[THEN:%.*]], label [[ENDIF:%.*]]
12 ; CHECK-NEXT: [[TMP_9:%.*]] = add i32 [[B:%.*]], [[A:%.*]]
13 ; CHECK-NEXT: ret i32 [[TMP_9]]
15 ; CHECK-NEXT: [[TMP_2:%.*]] = sdiv i32 [[A]], [[B]]
16 ; CHECK-NEXT: ret i32 [[TMP_2]]
19 %tmp.2 = sdiv i32 %A, %B ; <i32> [#uses=1]
20 %tmp.9 = add i32 %B, %A ; <i32> [#uses=1]
21 br i1 %C, label %then, label %endif
23 then: ; preds = %entry
26 endif: ; preds = %entry
30 ; We fail to reach a fixpoint, because sunk instructions get revisited too
31 ; early. In @test2 the sunk add is revisited before the dominating condition
32 ; is visited and added to the DomConditionCache.
34 ;; PHI use, sink divide before call.
35 define i32 @test2(i32 %x) nounwind ssp "instcombine-no-verify-fixpoint" {
36 ; CHECK-LABEL: @test2(
38 ; CHECK-NEXT: br label [[BB:%.*]]
40 ; CHECK-NEXT: [[X_ADDR_17:%.*]] = phi i32 [ [[X:%.*]], [[ENTRY:%.*]] ], [ [[X_ADDR_0:%.*]], [[BB2:%.*]] ]
41 ; CHECK-NEXT: [[I_06:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[TMP4:%.*]], [[BB2]] ]
42 ; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i32 [[X_ADDR_17]], 0
43 ; CHECK-NEXT: br i1 [[TMP0]], label [[BB1:%.*]], label [[BB2]]
45 ; CHECK-NEXT: [[TMP1:%.*]] = add nsw i32 [[X_ADDR_17]], 1
46 ; CHECK-NEXT: [[TMP2:%.*]] = sdiv i32 [[TMP1]], [[X_ADDR_17]]
47 ; CHECK-NEXT: [[TMP3:%.*]] = tail call i32 @bar() #[[ATTR3:[0-9]+]]
48 ; CHECK-NEXT: br label [[BB2]]
50 ; CHECK-NEXT: [[X_ADDR_0]] = phi i32 [ [[TMP2]], [[BB1]] ], [ [[X_ADDR_17]], [[BB]] ]
51 ; CHECK-NEXT: [[TMP4]] = add nuw nsw i32 [[I_06]], 1
52 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[TMP4]], 1000000
53 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[BB4:%.*]], label [[BB]]
55 ; CHECK-NEXT: ret i32 [[X_ADDR_0]]
60 bb: ; preds = %bb2, %entry
61 %x_addr.17 = phi i32 [ %x, %entry ], [ %x_addr.0, %bb2 ] ; <i32> [#uses=4]
62 %i.06 = phi i32 [ 0, %entry ], [ %4, %bb2 ] ; <i32> [#uses=1]
63 %0 = add nsw i32 %x_addr.17, 1 ; <i32> [#uses=1]
64 %1 = sdiv i32 %0, %x_addr.17 ; <i32> [#uses=1]
65 %2 = icmp eq i32 %x_addr.17, 0 ; <i1> [#uses=1]
66 br i1 %2, label %bb1, label %bb2
69 %3 = tail call i32 @bar() nounwind ; <i32> [#uses=0]
72 bb2: ; preds = %bb, %bb1
73 %x_addr.0 = phi i32 [ %1, %bb1 ], [ %x_addr.17, %bb ] ; <i32> [#uses=2]
74 %4 = add nsw i32 %i.06, 1 ; <i32> [#uses=2]
75 %exitcond = icmp eq i32 %4, 1000000 ; <i1> [#uses=1]
76 br i1 %exitcond, label %bb4, label %bb
84 define i32 @test3(ptr nocapture readonly %P, i32 %i) {
85 ; CHECK-LABEL: @test3(
87 ; CHECK-NEXT: switch i32 [[I:%.*]], label [[SW_EPILOG:%.*]] [
88 ; CHECK-NEXT: i32 5, label [[SW_BB:%.*]]
89 ; CHECK-NEXT: i32 2, label [[SW_BB]]
92 ; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[I]] to i64
93 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[P:%.*]], i64 [[IDXPROM]]
94 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
95 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP0]], [[I]]
96 ; CHECK-NEXT: br label [[SW_EPILOG]]
98 ; CHECK-NEXT: [[SUM_0:%.*]] = phi i32 [ [[ADD]], [[SW_BB]] ], [ 0, [[ENTRY:%.*]] ]
99 ; CHECK-NEXT: ret i32 [[SUM_0]]
102 %idxprom = sext i32 %i to i64
103 %arrayidx = getelementptr inbounds i32, ptr %P, i64 %idxprom
104 %0 = load i32, ptr %arrayidx, align 4
105 switch i32 %i, label %sw.epilog [
110 sw.bb: ; preds = %entry, %entry
111 %add = add nsw i32 %0, %i
114 sw.epilog: ; preds = %entry, %sw.bb
115 %sum.0 = phi i32 [ %add, %sw.bb ], [ 0, %entry ]
119 declare i32 @foo(i32, i32)
120 ; Two uses in a single user. We can still sink the instruction (tmp.9).
121 define i32 @test4(i32 %A, i32 %B, i1 %C) {
122 ; CHECK-LABEL: @test4(
124 ; CHECK-NEXT: br i1 [[C:%.*]], label [[THEN:%.*]], label [[ENDIF:%.*]]
126 ; CHECK-NEXT: [[TMP_9:%.*]] = add i32 [[B:%.*]], [[A:%.*]]
127 ; CHECK-NEXT: [[RES:%.*]] = call i32 @foo(i32 [[TMP_9]], i32 [[TMP_9]])
128 ; CHECK-NEXT: ret i32 [[RES]]
130 ; CHECK-NEXT: [[TMP_2:%.*]] = sdiv i32 [[A]], [[B]]
131 ; CHECK-NEXT: ret i32 [[TMP_2]]
134 %tmp.2 = sdiv i32 %A, %B ; <i32> [#uses=1]
135 %tmp.9 = add i32 %B, %A ; <i32> [#uses=1]
136 br i1 %C, label %then, label %endif
138 then: ; preds = %entry
139 %res = call i32 @foo(i32 %tmp.9, i32 %tmp.9)
142 endif: ; preds = %entry
146 ; Two uses in a single user (phi node). We just bail out.
147 define i32 @test5(ptr nocapture readonly %P, i32 %i, i1 %cond) {
148 ; CHECK-LABEL: @test5(
150 ; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[I:%.*]] to i64
151 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[P:%.*]], i64 [[IDXPROM]]
152 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
153 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[DISPATCHBB:%.*]], label [[SW_EPILOG:%.*]]
155 ; CHECK-NEXT: [[ADD:%.*]] = shl nsw i32 [[I]], 1
156 ; CHECK-NEXT: br label [[SW_EPILOG]]
158 ; CHECK-NEXT: br label [[SW_EPILOG]]
160 ; CHECK-NEXT: [[SUM_0:%.*]] = phi i32 [ [[TMP0]], [[SW_BB:%.*]] ], [ [[ADD]], [[DISPATCHBB]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
161 ; CHECK-NEXT: ret i32 [[SUM_0]]
164 %idxprom = sext i32 %i to i64
165 %arrayidx = getelementptr inbounds i32, ptr %P, i64 %idxprom
166 %0 = load i32, ptr %arrayidx, align 4
167 br i1 %cond, label %dispatchBB, label %sw.epilog
170 %add = add nsw i32 %i, %i
173 sw.bb: ; preds = %entry, %entry
176 sw.epilog: ; preds = %entry, %sw.bb
177 %sum.0 = phi i32 [ %0, %sw.bb ], [ %add, %dispatchBB ], [ %0, %entry ]
181 ; Multiple uses but from same BB. We can sink.
182 define i32 @test6(ptr nocapture readonly %P, i32 %i, i1 %cond) {
183 ; CHECK-LABEL: @test6(
185 ; CHECK-NEXT: [[ADD:%.*]] = shl nsw i32 [[I:%.*]], 1
186 ; CHECK-NEXT: br label [[DISPATCHBB:%.*]]
188 ; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[I]] to i64
189 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[P:%.*]], i64 [[IDXPROM]]
190 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
191 ; CHECK-NEXT: switch i32 [[I]], label [[SW_BB:%.*]] [
192 ; CHECK-NEXT: i32 5, label [[SW_EPILOG:%.*]]
193 ; CHECK-NEXT: i32 2, label [[SW_EPILOG]]
196 ; CHECK-NEXT: br label [[SW_EPILOG]]
198 ; CHECK-NEXT: [[SUM_0:%.*]] = phi i32 [ [[ADD]], [[SW_BB]] ], [ [[TMP0]], [[DISPATCHBB]] ], [ [[TMP0]], [[DISPATCHBB]] ]
199 ; CHECK-NEXT: ret i32 [[SUM_0]]
202 %idxprom = sext i32 %i to i64
203 %arrayidx = getelementptr inbounds i32, ptr %P, i64 %idxprom
204 %0 = load i32, ptr %arrayidx, align 4
205 %add = add nsw i32 %i, %i
209 switch i32 %i, label %sw.bb [
210 i32 5, label %sw.epilog
211 i32 2, label %sw.epilog
214 sw.bb: ; preds = %entry, %entry
217 sw.epilog: ; preds = %entry, %sw.bb
218 %sum.0 = phi i32 [ %add, %sw.bb ], [ %0, %dispatchBB ], [ %0, %dispatchBB ]
222 declare void @checkd(double)
223 declare double @log(double) willreturn nounwind readnone
224 define void @test7(i1 %cond, double %d) {
225 ; CHECK-LABEL: @test7(
226 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
228 ; CHECK-NEXT: [[A:%.*]] = call double @log(double [[D:%.*]])
229 ; CHECK-NEXT: call void @checkd(double [[A]])
230 ; CHECK-NEXT: ret void
232 ; CHECK-NEXT: ret void
234 %A = call double @log(double %d)
235 br i1 %cond, label %if, label %else
238 call void @checkd(double %A)
244 declare void @abort()
245 declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64)
246 declare void @dummy(i64)
247 ; Two uses in two different users of a single successor block. We can sink.
248 define i64 @test8(i64 %c) {
249 ; CHECK-LABEL: @test8(
251 ; CHECK-NEXT: [[OVERFLOW:%.*]] = icmp ugt i64 [[C:%.*]], 2305843009213693951
252 ; CHECK-NEXT: br i1 [[OVERFLOW]], label [[ABORT:%.*]], label [[BB2:%.*]]
254 ; CHECK-NEXT: call void @dummy(i64 8)
255 ; CHECK-NEXT: ret i64 8
257 ; CHECK-NEXT: call void @abort()
258 ; CHECK-NEXT: unreachable
261 %mul = tail call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %c, i64 8)
262 %overflow = extractvalue { i64, i1 } %mul, 1
263 %select = select i1 %overflow, i64 0, i64 8
264 br i1 %overflow, label %abort, label %bb2
267 call void @dummy(i64 %select)
275 ; Loads marked invariant can be sunk past potential memory writes.
277 define i32 @invariant_load_metadata(ptr %p, i1 %cond) {
278 ; CHECK-LABEL: @invariant_load_metadata(
280 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[BLOCK:%.*]], label [[END:%.*]]
282 ; CHECK-NEXT: call void @fn()
283 ; CHECK-NEXT: br label [[END]]
285 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[P:%.*]], align 4, !invariant.load [[META0:![0-9]+]]
286 ; CHECK-NEXT: ret i32 [[V]]
289 %v = load i32, ptr %p, !invariant.load !0
290 br i1 %cond, label %block, label %end
298 ; Loads not marked invariant cannot be sunk past potential memory writes.
300 define i32 @invariant_load_neg(ptr %p, i1 %cond) {
301 ; CHECK-LABEL: @invariant_load_neg(
303 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[P:%.*]], align 4
304 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[BLOCK:%.*]], label [[END:%.*]]
306 ; CHECK-NEXT: call void @fn()
307 ; CHECK-NEXT: br label [[END]]
309 ; CHECK-NEXT: ret i32 [[V]]
312 %v = load i32, ptr %p
313 br i1 %cond, label %block, label %end
321 ; Loads that aren't marked invariant but used in one branch
322 ; can be sunk to that branch.
324 define void @invariant_load_use_in_br(ptr %p, i1 %cond) {
325 ; CHECK-LABEL: @invariant_load_use_in_br(
327 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[TRUE_BR:%.*]], label [[FALSE_BR:%.*]]
329 ; CHECK-NEXT: call void @fn()
330 ; CHECK-NEXT: br label [[EXIT:%.*]]
332 ; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr [[P:%.*]], align 4
333 ; CHECK-NEXT: call void @fn(i32 [[VAL]])
334 ; CHECK-NEXT: br label [[EXIT]]
336 ; CHECK-NEXT: ret void
339 %val = load i32, ptr %p
340 br i1 %cond, label %true.br, label %false.br
345 call void @fn(i32 %val)
351 ; Invariant loads marked with metadata can be sunk past calls.
353 define void @invariant_load_metadata_call(ptr %p, i1 %cond) {
354 ; CHECK-LABEL: @invariant_load_metadata_call(
356 ; CHECK-NEXT: call void @fn()
357 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[TRUE_BR:%.*]], label [[FALSE_BR:%.*]]
359 ; CHECK-NEXT: call void @fn()
360 ; CHECK-NEXT: br label [[EXIT:%.*]]
362 ; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr [[P:%.*]], align 4, !invariant.load [[META0]]
363 ; CHECK-NEXT: call void @fn(i32 [[VAL]])
364 ; CHECK-NEXT: br label [[EXIT]]
366 ; CHECK-NEXT: ret void
369 %val = load i32, ptr %p, !invariant.load !0
371 br i1 %cond, label %true.br, label %false.br
376 call void @fn(i32 %val)