1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=gvn < %s | FileCheck %s
3 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32"
4 target triple = "i386-apple-darwin11.0.0"
6 @sortlist = external global [5001 x i32], align 4
8 define void @Bubble() nounwind noinline {
9 ; CHECK-LABEL: @Bubble(
11 ; CHECK-NEXT: [[TMP7_PRE:%.*]] = load i32, ptr getelementptr inbounds (i8, ptr @sortlist, i32 4), align 4
12 ; CHECK-NEXT: br label [[WHILE_BODY5:%.*]]
14 ; CHECK-NEXT: [[TMP7:%.*]] = phi i32 [ [[TMP7_PRE]], [[ENTRY:%.*]] ], [ [[TMP71:%.*]], [[IF_END:%.*]] ]
15 ; CHECK-NEXT: [[INDVAR:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[TMP6:%.*]], [[IF_END]] ]
16 ; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[INDVAR]], 2
17 ; CHECK-NEXT: [[ARRAYIDX9:%.*]] = getelementptr [5001 x i32], ptr @sortlist, i32 0, i32 [[TMP5]]
18 ; CHECK-NEXT: [[TMP6]] = add i32 [[INDVAR]], 1
19 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr [5001 x i32], ptr @sortlist, i32 0, i32 [[TMP6]]
20 ; CHECK-NEXT: [[TMP10:%.*]] = load i32, ptr [[ARRAYIDX9]], align 4
21 ; CHECK-NEXT: [[CMP11:%.*]] = icmp sgt i32 [[TMP7]], [[TMP10]]
22 ; CHECK-NEXT: br i1 [[CMP11]], label [[IF_THEN:%.*]], label [[IF_END]]
24 ; CHECK-NEXT: store i32 [[TMP10]], ptr [[ARRAYIDX]], align 4
25 ; CHECK-NEXT: store i32 [[TMP7]], ptr [[ARRAYIDX9]], align 4
26 ; CHECK-NEXT: br label [[IF_END]]
28 ; CHECK-NEXT: [[TMP71]] = phi i32 [ [[TMP7]], [[IF_THEN]] ], [ [[TMP10]], [[WHILE_BODY5]] ]
29 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[TMP6]], 100
30 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[WHILE_END_LOOPEXIT:%.*]], label [[WHILE_BODY5]]
31 ; CHECK: while.end.loopexit:
32 ; CHECK-NEXT: ret void
38 %indvar = phi i32 [ 0, %entry ], [ %tmp6, %if.end ]
39 %tmp5 = add i32 %indvar, 2
40 %arrayidx9 = getelementptr [5001 x i32], ptr @sortlist, i32 0, i32 %tmp5
41 %tmp6 = add i32 %indvar, 1
42 %arrayidx = getelementptr [5001 x i32], ptr @sortlist, i32 0, i32 %tmp6
43 %tmp7 = load i32, ptr %arrayidx, align 4
44 %tmp10 = load i32, ptr %arrayidx9, align 4
45 %cmp11 = icmp sgt i32 %tmp7, %tmp10
46 br i1 %cmp11, label %if.then, label %if.end
49 store i32 %tmp10, ptr %arrayidx, align 4
50 store i32 %tmp7, ptr %arrayidx9, align 4
54 %exitcond = icmp eq i32 %tmp6, 100
55 br i1 %exitcond, label %while.end.loopexit, label %while.body5
61 declare void @hold(i32) readonly
62 declare void @clobber()
64 ; This is a classic LICM case
65 define i32 @test1(i1 %cnd, ptr %p) {
66 ; CHECK-LABEL: @test1(
68 ; CHECK-NEXT: [[V1_PRE:%.*]] = load i32, ptr [[P:%.*]], align 4
69 ; CHECK-NEXT: br label [[HEADER:%.*]]
71 ; CHECK-NEXT: call void @hold(i32 [[V1_PRE]])
72 ; CHECK-NEXT: br label [[HEADER]]
78 %v1 = load i32, ptr %p
79 call void @hold(i32 %v1)
84 ; Slightly more complicated case to highlight that MemoryDependenceAnalysis
85 ; can compute availability for internal control flow. In this case, because
86 ; the value is fully available across the backedge, we only need to establish
87 ; anticipation for the preheader block (which is trivial in this case.)
88 define i32 @test2(i1 %cnd, ptr %p) {
89 ; CHECK-LABEL: @test2(
91 ; CHECK-NEXT: [[V1_PRE:%.*]] = load i32, ptr [[P:%.*]], align 4
92 ; CHECK-NEXT: br label [[HEADER:%.*]]
94 ; CHECK-NEXT: call void @hold(i32 [[V1_PRE]])
95 ; CHECK-NEXT: br i1 [[CND:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
97 ; CHECK-NEXT: br label [[MERGE:%.*]]
99 ; CHECK-NEXT: br label [[MERGE]]
101 ; CHECK-NEXT: br label [[HEADER]]
107 %v1 = load i32, ptr %p
108 call void @hold(i32 %v1)
109 br i1 %cnd, label %bb1, label %bb2
122 ; TODO: at the moment, our anticipation check does not handle anything
123 ; other than straight-line unconditional fallthrough. This particular
124 ; case could be solved through either a backwards anticipation walk or
125 ; use of the "safe to speculate" status (if we annotate the param)
126 define i32 @test3(i1 %cnd, ptr %p) {
127 ; CHECK-LABEL: @test3(
129 ; CHECK-NEXT: br label [[HEADER:%.*]]
131 ; CHECK-NEXT: br i1 [[CND:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
133 ; CHECK-NEXT: br label [[MERGE:%.*]]
135 ; CHECK-NEXT: br label [[MERGE]]
137 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
138 ; CHECK-NEXT: call void @hold(i32 [[V1]])
139 ; CHECK-NEXT: br label [[HEADER]]
145 br i1 %cnd, label %bb1, label %bb2
154 %v1 = load i32, ptr %p
155 call void @hold(i32 %v1)
159 ; Highlight that we can PRE into a latch block when there are multiple
160 ; latches only one of which clobbers an otherwise invariant value.
161 define i32 @test4(i1 %cnd, ptr %p) {
162 ; CHECK-LABEL: @test4(
164 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
165 ; CHECK-NEXT: call void @hold(i32 [[V1]])
166 ; CHECK-NEXT: br label [[HEADER:%.*]]
168 ; CHECK-NEXT: [[V2:%.*]] = phi i32 [ [[V2_PRE:%.*]], [[BB2:%.*]] ], [ [[V2]], [[BB1:%.*]] ], [ [[V1]], [[ENTRY:%.*]] ]
169 ; CHECK-NEXT: call void @hold(i32 [[V2]])
170 ; CHECK-NEXT: br i1 [[CND:%.*]], label [[BB1]], label [[BB2]]
172 ; CHECK-NEXT: br label [[HEADER]]
174 ; CHECK-NEXT: call void @clobber()
175 ; CHECK-NEXT: [[V2_PRE]] = load i32, ptr [[P]], align 4
176 ; CHECK-NEXT: br label [[HEADER]]
179 %v1 = load i32, ptr %p
180 call void @hold(i32 %v1)
184 %v2 = load i32, ptr %p
185 call void @hold(i32 %v2)
186 br i1 %cnd, label %bb1, label %bb2
197 ; Highlight the fact that we can PRE into a single clobbering latch block
198 ; even in loop simplify form (though multiple applications of the same
200 define i32 @test5(i1 %cnd, ptr %p) {
201 ; CHECK-LABEL: @test5(
203 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
204 ; CHECK-NEXT: call void @hold(i32 [[V1]])
205 ; CHECK-NEXT: br label [[HEADER:%.*]]
207 ; CHECK-NEXT: [[V2_PRE2:%.*]] = phi i32 [ [[V2_PRE:%.*]], [[MERGE:%.*]] ], [ [[V1]], [[ENTRY:%.*]] ]
208 ; CHECK-NEXT: call void @hold(i32 [[V2_PRE2]])
209 ; CHECK-NEXT: br i1 [[CND:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
211 ; CHECK-NEXT: br label [[MERGE]]
213 ; CHECK-NEXT: call void @clobber()
214 ; CHECK-NEXT: [[V2_PRE_PRE:%.*]] = load i32, ptr [[P]], align 4
215 ; CHECK-NEXT: br label [[MERGE]]
217 ; CHECK-NEXT: [[V2_PRE]] = phi i32 [ [[V2_PRE_PRE]], [[BB2]] ], [ [[V2_PRE2]], [[BB1]] ]
218 ; CHECK-NEXT: br label [[HEADER]]
221 %v1 = load i32, ptr %p
222 call void @hold(i32 %v1)
226 %v2 = load i32, ptr %p
227 call void @hold(i32 %v2)
228 br i1 %cnd, label %bb1, label %bb2
242 declare void @llvm.experimental.guard(i1 %cnd, ...)
244 ; These two tests highlight speculation safety when we can not establish
245 ; anticipation (since the original load might actually not execcute)
246 define i32 @test6a(i1 %cnd, ptr %p) {
247 ; CHECK-LABEL: @test6a(
249 ; CHECK-NEXT: br label [[HEADER:%.*]]
251 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[CND:%.*]]) [ "deopt"() ]
252 ; CHECK-NEXT: [[V1:%.*]] = load i32, ptr [[P:%.*]], align 4
253 ; CHECK-NEXT: call void @hold(i32 [[V1]])
254 ; CHECK-NEXT: br label [[HEADER]]
260 call void (i1, ...) @llvm.experimental.guard(i1 %cnd) ["deopt"()]
261 %v1 = load i32, ptr %p
262 call void @hold(i32 %v1)
266 define i32 @test6b(i1 %cnd, ptr dereferenceable(8) align 4 %p) nofree nosync {
267 ; CHECK-LABEL: @test6b(
269 ; CHECK-NEXT: [[V1_PRE:%.*]] = load i32, ptr [[P:%.*]], align 4
270 ; CHECK-NEXT: br label [[HEADER:%.*]]
272 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[CND:%.*]]) [ "deopt"() ]
273 ; CHECK-NEXT: call void @hold(i32 [[V1_PRE]])
274 ; CHECK-NEXT: br label [[HEADER]]
280 call void (i1, ...) @llvm.experimental.guard(i1 %cnd) ["deopt"()]
281 %v1 = load i32, ptr %p
282 call void @hold(i32 %v1)