1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=jump-threading,verify < %s | FileCheck %s
4 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-unknown-linux-gnu"
7 @a = global i32 0, align 4
9 ; Verify that we branch (twice) on cond2 without checking ptr.
10 ; Verify that we eliminate "bb.file".
12 define void @foo(i32 %cond1, i32 %cond2) {
15 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[COND1:%.*]], 0
16 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[BB_COND2_THREAD:%.*]], label [[BB_COND2:%.*]]
18 ; CHECK-NEXT: call void @f1()
19 ; CHECK-NEXT: [[TOBOOL1:%.*]] = icmp eq i32 [[COND2:%.*]], 0
20 ; CHECK-NEXT: br i1 [[TOBOOL1]], label [[BB_F4:%.*]], label [[BB_F2:%.*]]
21 ; CHECK: bb.cond2.thread:
22 ; CHECK-NEXT: [[TOBOOL12:%.*]] = icmp eq i32 [[COND2]], 0
23 ; CHECK-NEXT: br i1 [[TOBOOL12]], label [[BB_F3:%.*]], label [[BB_F2]]
25 ; CHECK-NEXT: call void @f2()
26 ; CHECK-NEXT: br label [[EXIT:%.*]]
28 ; CHECK-NEXT: call void @f3()
29 ; CHECK-NEXT: br label [[EXIT]]
31 ; CHECK-NEXT: [[PTR3:%.*]] = phi ptr [ null, [[BB_COND2]] ]
32 ; CHECK-NEXT: call void @f4()
33 ; CHECK-NEXT: br label [[EXIT]]
35 ; CHECK-NEXT: ret void
38 %tobool = icmp eq i32 %cond1, 0
39 br i1 %tobool, label %bb.cond2, label %bb.f1
46 %ptr = phi ptr [ null, %bb.f1 ], [ @a, %entry ]
47 %tobool1 = icmp eq i32 %cond2, 0
48 br i1 %tobool1, label %bb.file, label %bb.f2
55 %cmp = icmp eq ptr %ptr, null
56 br i1 %cmp, label %bb.f4, label %bb.f3
76 ; Verify that we branch (twice) on cond2 without checking tobool again.
77 ; Verify that we eliminate "bb.cond1again".
79 define void @foo2(i32 %cond1, i32 %cond2) {
82 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[COND1:%.*]], 0
83 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[BB_COND2:%.*]], label [[BB_COND2_THREAD:%.*]]
85 ; CHECK-NEXT: call void @f1()
86 ; CHECK-NEXT: [[TOBOOL1:%.*]] = icmp eq i32 [[COND2:%.*]], 0
87 ; CHECK-NEXT: br i1 [[TOBOOL1]], label [[EXIT:%.*]], label [[BB_F3:%.*]]
88 ; CHECK: bb.cond2.thread:
89 ; CHECK-NEXT: call void @f2()
90 ; CHECK-NEXT: [[TOBOOL11:%.*]] = icmp eq i32 [[COND2]], 0
91 ; CHECK-NEXT: br i1 [[TOBOOL11]], label [[EXIT]], label [[BB_F4:%.*]]
93 ; CHECK-NEXT: call void @f3()
94 ; CHECK-NEXT: br label [[EXIT]]
96 ; CHECK-NEXT: call void @f4()
97 ; CHECK-NEXT: br label [[EXIT]]
99 ; CHECK-NEXT: ret void
102 %tobool = icmp ne i32 %cond1, 0
103 br i1 %tobool, label %bb.f1, label %bb.f2
114 %tobool1 = icmp eq i32 %cond2, 0
115 br i1 %tobool1, label %exit, label %bb.cond1again
118 br i1 %tobool, label %bb.f3, label %bb.f4
133 ; Verify that we thread the edge correctly. We used to evaluate constant
136 ; icmp ugt ptr null, inttoptr (i64 4 to ptr)
138 ; as "true", causing jump threading to a wrong destination.
140 define void @icmp_ult_null_constexpr(ptr %arg1, ptr %arg2) {
141 ; CHECK-LABEL: @icmp_ult_null_constexpr(
143 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq ptr [[ARG1:%.*]], null
144 ; CHECK-NEXT: br i1 [[CMP1]], label [[BB_END_THREAD:%.*]], label [[BB_END:%.*]]
146 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne ptr [[ARG2:%.*]], null
147 ; CHECK-NEXT: br i1 [[CMP2]], label [[BB_CONT:%.*]], label [[BB_BAR2:%.*]]
148 ; CHECK: bb_end.thread:
149 ; CHECK-NEXT: call void @bar(i32 1)
150 ; CHECK-NEXT: [[CMP21:%.*]] = icmp ne ptr [[ARG2]], null
151 ; CHECK-NEXT: br i1 [[CMP21]], label [[BB_EXIT:%.*]], label [[BB_BAR2]]
153 ; CHECK-NEXT: call void @bar(i32 2)
154 ; CHECK-NEXT: br label [[BB_EXIT]]
156 ; CHECK-NEXT: [[CMP3:%.*]] = icmp ult ptr [[ARG1]], inttoptr (i64 4 to ptr)
157 ; CHECK-NEXT: br i1 [[CMP3]], label [[BB_EXIT]], label [[BB_BAR3:%.*]]
159 ; CHECK-NEXT: call void @bar(i32 3)
160 ; CHECK-NEXT: br label [[BB_EXIT]]
162 ; CHECK-NEXT: ret void
165 %cmp1 = icmp eq ptr %arg1, null
166 br i1 %cmp1, label %bb_bar1, label %bb_end
169 call void @bar(i32 1)
173 %cmp2 = icmp ne ptr %arg2, null
174 br i1 %cmp2, label %bb_cont, label %bb_bar2
177 call void @bar(i32 2)
181 %cmp3 = icmp ult ptr %arg1, inttoptr (i64 4 to ptr)
182 br i1 %cmp3, label %bb_exit, label %bb_bar3
185 call void @bar(i32 3)
192 ; This is a special-case of the above pattern:
193 ; Null is guaranteed to be unsigned <= all values.
195 define void @icmp_ule_null_constexpr(ptr %arg1, ptr %arg2) {
196 ; CHECK-LABEL: @icmp_ule_null_constexpr(
198 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq ptr [[ARG1:%.*]], null
199 ; CHECK-NEXT: br i1 [[CMP1]], label [[BB_END_THREAD:%.*]], label [[BB_END:%.*]]
201 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne ptr [[ARG2:%.*]], null
202 ; CHECK-NEXT: br i1 [[CMP2]], label [[BB_CONT:%.*]], label [[BB_BAR2:%.*]]
203 ; CHECK: bb_end.thread:
204 ; CHECK-NEXT: call void @bar(i32 1)
205 ; CHECK-NEXT: [[CMP21:%.*]] = icmp ne ptr [[ARG2]], null
206 ; CHECK-NEXT: br i1 [[CMP21]], label [[BB_EXIT:%.*]], label [[BB_BAR2]]
208 ; CHECK-NEXT: call void @bar(i32 2)
209 ; CHECK-NEXT: br label [[BB_EXIT]]
211 ; CHECK-NEXT: [[CMP3:%.*]] = icmp ule ptr [[ARG1]], inttoptr (i64 4 to ptr)
212 ; CHECK-NEXT: br i1 [[CMP3]], label [[BB_EXIT]], label [[BB_BAR3:%.*]]
214 ; CHECK-NEXT: call void @bar(i32 3)
215 ; CHECK-NEXT: br label [[BB_EXIT]]
217 ; CHECK-NEXT: ret void
220 %cmp1 = icmp eq ptr %arg1, null
221 br i1 %cmp1, label %bb_bar1, label %bb_end
224 call void @bar(i32 1)
228 %cmp2 = icmp ne ptr %arg2, null
229 br i1 %cmp2, label %bb_cont, label %bb_bar2
232 call void @bar(i32 2)
236 %cmp3 = icmp ule ptr %arg1, inttoptr (i64 4 to ptr)
237 br i1 %cmp3, label %bb_exit, label %bb_bar3
240 call void @bar(i32 3)
247 declare void @bar(i32)
250 ;; Test that we skip unconditional PredBB when threading jumps through two
251 ;; successive basic blocks.
253 define i32 @foo4(ptr %0) {
254 ; CHECK-LABEL: @foo4(
256 ; CHECK-NEXT: [[SIZE:%.*]] = call i64 @get_size(ptr [[TMP0:%.*]])
257 ; CHECK-NEXT: [[GOOD:%.*]] = icmp ugt i64 [[SIZE]], 3
258 ; CHECK-NEXT: br i1 [[GOOD]], label [[PRED_BB:%.*]], label [[PRED_PRED_BB:%.*]]
259 ; CHECK: pred.pred.bb:
260 ; CHECK-NEXT: call void @effect()
261 ; CHECK-NEXT: br label [[PRED_BB]]
263 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[TMP0]], align 4
264 ; CHECK-NEXT: br label [[BB:%.*]]
266 ; CHECK-NEXT: call void @effect1(ptr blockaddress(@foo4, [[BB]]))
267 ; CHECK-NEXT: br i1 [[GOOD]], label [[EXIT:%.*]], label [[EXIT]]
269 ; CHECK-NEXT: ret i32 [[V]]
272 %size = call i64 @get_size(ptr %0)
273 %good = icmp ugt i64 %size, 3
274 br i1 %good, label %pred.bb, label %pred.pred.bb
276 pred.pred.bb: ; preds = %entry
279 pred.bb: ; preds = %pred.pred.bb, %entry
280 %v = load i32, ptr %0
283 bb: ; preds = %pred.bb
284 call void @effect1(ptr blockaddress(@foo4, %bb))
285 br i1 %good, label %cont2, label %cont1
288 br i1 %good, label %exit, label %cont2
291 exit: ; preds = %cont1, %cont2
295 declare i64 @get_size(ptr)
296 declare void @effect()
297 declare void @effect1(ptr)