1 ; RUN: opt < %s -basic-aa -licm -S | FileCheck %s
2 ; RUN: opt -aa-pipeline=basic-aa -passes='require<aa>,require<targetir>,require<scalar-evolution>,require<opt-remark-emit>,loop-mssa(licm)' -S %s | FileCheck %s
4 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-unknown-linux-gnu"
7 ; Make sure we don't hoist the store out of the loop; %a would
8 ; have the wrong value if f() unwinds
10 define void @test1(i32* nocapture noalias %a, i1 zeroext %y) uwtable {
15 %i.03 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
16 %0 = load i32, i32* %a, align 4
17 %add = add nsw i32 %0, 1
18 store i32 %add, i32* %a, align 4
19 br i1 %y, label %if.then, label %for.inc
21 ; CHECK: define void @test1
22 ; CHECK: load i32, i32*
24 ; CHECK-NEXT: store i32
31 %inc = add nuw nsw i32 %i.03, 1
32 %exitcond = icmp eq i32 %inc, 10000
33 br i1 %exitcond, label %for.cond.cleanup, label %for.body
39 ; We can hoist the store out of the loop here; if f() unwinds,
40 ; the lifetime of %a ends.
42 define void @test2(i1 zeroext %y) uwtable {
48 %i.03 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
49 %0 = load i32, i32* %a, align 4
50 %add = add nsw i32 %0, 1
51 store i32 %add, i32* %a, align 4
52 br i1 %y, label %if.then, label %for.inc
59 %inc = add nuw nsw i32 %i.03, 1
60 %exitcond = icmp eq i32 %inc, 10000
61 br i1 %exitcond, label %for.cond.cleanup, label %for.body
64 ; CHECK: define void @test2
66 ; CHECK-NEXT: ret void
70 ;; We can promote if the load can be proven safe to speculate, and the
71 ;; store safe to sink, even if the the store *isn't* must execute.
72 define void @test3(i1 zeroext %y) uwtable {
76 ; CHECK-NEXT: %a = alloca i32
77 ; CHECK-NEXT: %a.promoted = load i32, i32* %a, align 4
82 %i.03 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
83 %0 = load i32, i32* %a, align 4
84 %add = add nsw i32 %0, 1
86 store i32 %add, i32* %a, align 4
87 %inc = add nuw nsw i32 %i.03, 1
88 %exitcond = icmp eq i32 %inc, 10000
89 br i1 %exitcond, label %for.cond.cleanup, label %for.body
92 ; CHECK-LABEL: for.cond.cleanup:
93 ; CHECK: store i32 %add.lcssa, i32* %a, align 4
94 ; CHECK-NEXT: ret void
98 ;; Same as test3, but with unordered atomics
99 define void @test3b(i1 zeroext %y) uwtable {
100 ; CHECK-LABEL: @test3
102 ; CHECK-LABEL: entry:
103 ; CHECK-NEXT: %a = alloca i32
104 ; CHECK-NEXT: %a.promoted = load atomic i32, i32* %a unordered, align 4
109 %i.03 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
110 %0 = load atomic i32, i32* %a unordered, align 4
111 %add = add nsw i32 %0, 1
113 store atomic i32 %add, i32* %a unordered, align 4
114 %inc = add nuw nsw i32 %i.03, 1
115 %exitcond = icmp eq i32 %inc, 10000
116 br i1 %exitcond, label %for.cond.cleanup, label %for.body
119 ; CHECK-LABEL: for.cond.cleanup:
120 ; CHECK: store atomic i32 %add.lcssa, i32* %a unordered, align 4
121 ; CHECK-NEXT: ret void
125 @_ZTIi = external constant i8*
127 ; In this test, the loop is within a try block. There is an explicit unwind edge out of the loop.
128 ; Make sure this edge is treated as a loop exit, and that the loads and stores are promoted as
130 define void @loop_within_tryblock() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
132 %a = alloca i32, align 4
133 store i32 0, i32* %a, align 4
137 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
138 %cmp = icmp slt i32 %i.0, 1024
139 br i1 %cmp, label %for.body, label %for.end
146 %0 = load i32, i32* %a, align 4
147 %add = add nsw i32 %0, 1
148 store i32 %add, i32* %a, align 4
150 to label %invoke.cont unwind label %lpad
156 %inc = add nsw i32 %i.0, 1
163 %1 = landingpad { i8*, i32 }
164 catch i8* bitcast (i8** @_ZTIi to i8*)
165 %2 = extractvalue { i8*, i32 } %1, 0
166 %3 = extractvalue { i8*, i32 } %1, 1
167 br label %catch.dispatch
170 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) #3
171 %matches = icmp eq i32 %3, %4
172 br i1 %matches, label %catch, label %eh.resume
175 %5 = call i8* @__cxa_begin_catch(i8* %2) #3
176 %6 = bitcast i8* %5 to i32*
177 %7 = load i32, i32* %6, align 4
178 call void @__cxa_end_catch() #3
188 %lpad.val = insertvalue { i8*, i32 } undef, i8* %2, 0
189 %lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %3, 1
190 resume { i8*, i32 } %lpad.val3
194 ; The malloc'ed memory is not capture and therefore promoted.
195 define void @malloc_no_capture() #0 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
197 %call = call i8* @malloc(i64 4)
198 %0 = bitcast i8* %call to i32*
206 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.latch ]
207 %1 = load i32, i32* %0, align 4
208 %add = add nsw i32 %1, 1
209 store i32 %add, i32* %0, align 4
214 to label %invoke.cont unwind label %lpad
220 %inc = add i32 %i.0, 1
221 %cmp = icmp slt i32 %i.0, 1024
222 br i1 %cmp, label %for.body, label %for.end
228 %2 = landingpad { i8*, i32 }
230 %3 = extractvalue { i8*, i32 } %2, 0
231 %4 = extractvalue { i8*, i32 } %2, 1
235 %5 = call i8* @__cxa_begin_catch(i8* %3) #4
236 %6 = bitcast i32* %0 to i8*
237 call void @free(i8* %6)
238 call void @__cxa_end_catch()
245 ; The malloc'ed memory can be captured and therefore not promoted.
246 define void @malloc_capture(i32** noalias %A) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
248 %call = call i8* @malloc(i64 4)
249 %0 = bitcast i8* %call to i32*
257 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.latch ]
258 %1 = load i32, i32* %0, align 4
259 %add = add nsw i32 %1, 1
260 store i32 %add, i32* %0, align 4
264 invoke void @boo_readnone()
265 to label %invoke.cont unwind label %lpad
271 store i32* %0, i32** %A
272 %inc = add i32 %i.0, 1
273 %cmp = icmp slt i32 %i.0, 1024
274 br i1 %cmp, label %for.body, label %for.end
280 %2 = landingpad { i8*, i32 }
282 %3 = extractvalue { i8*, i32 } %2, 0
283 %4 = extractvalue { i8*, i32 } %2, 1
287 %5 = call i8* @__cxa_begin_catch(i8* %3) #4
288 %6 = bitcast i32* %0 to i8*
289 call void @free(i8* %6)
290 call void @__cxa_end_catch()
297 ; Function Attrs: nounwind
298 declare noalias i8* @malloc(i64)
300 ; Function Attrs: nounwind
301 declare void @free(i8* nocapture)
305 ; This is an artifical example, readnone functions by definition cannot unwind
306 ; exceptions by calling the C++ exception throwing methods
307 ; This function should only be used to test malloc_capture.
308 declare void @boo_readnone() readnone
310 declare i32 @__gxx_personality_v0(...)
312 declare i8* @__cxa_begin_catch(i8*)
314 declare void @__cxa_end_catch()
316 declare i32 @llvm.eh.typeid.for(i8*)
318 declare void @f() uwtable