1 ; RUN: opt < %s -basicaa -licm -S | FileCheck %s
2 ; RUN: opt -aa-pipeline=basic-aa -passes='require<aa>,require<targetir>,require<scalar-evolution>,require<opt-remark-emit>,loop(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 @_ZTIi = external constant i8*
72 ; In this test, the loop is within a try block. There is an explicit unwind edge out of the loop.
73 ; Make sure this edge is treated as a loop exit, and that the loads and stores are promoted as
75 define void @loop_within_tryblock() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
77 %a = alloca i32, align 4
78 store i32 0, i32* %a, align 4
82 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
83 %cmp = icmp slt i32 %i.0, 1024
84 br i1 %cmp, label %for.body, label %for.end
91 %0 = load i32, i32* %a, align 4
92 %add = add nsw i32 %0, 1
93 store i32 %add, i32* %a, align 4
95 to label %invoke.cont unwind label %lpad
101 %inc = add nsw i32 %i.0, 1
108 %1 = landingpad { i8*, i32 }
109 catch i8* bitcast (i8** @_ZTIi to i8*)
110 %2 = extractvalue { i8*, i32 } %1, 0
111 %3 = extractvalue { i8*, i32 } %1, 1
112 br label %catch.dispatch
115 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) #3
116 %matches = icmp eq i32 %3, %4
117 br i1 %matches, label %catch, label %eh.resume
120 %5 = call i8* @__cxa_begin_catch(i8* %2) #3
121 %6 = bitcast i8* %5 to i32*
122 %7 = load i32, i32* %6, align 4
123 call void @__cxa_end_catch() #3
133 %lpad.val = insertvalue { i8*, i32 } undef, i8* %2, 0
134 %lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %3, 1
135 resume { i8*, i32 } %lpad.val3
139 ; The malloc'ed memory is not capture and therefore promoted.
140 define void @malloc_no_capture() #0 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
142 %call = call i8* @malloc(i64 4)
143 %0 = bitcast i8* %call to i32*
151 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.latch ]
152 %1 = load i32, i32* %0, align 4
153 %add = add nsw i32 %1, 1
154 store i32 %add, i32* %0, align 4
159 to label %invoke.cont unwind label %lpad
165 %inc = add i32 %i.0, 1
166 %cmp = icmp slt i32 %i.0, 1024
167 br i1 %cmp, label %for.body, label %for.end
173 %2 = landingpad { i8*, i32 }
175 %3 = extractvalue { i8*, i32 } %2, 0
176 %4 = extractvalue { i8*, i32 } %2, 1
180 %5 = call i8* @__cxa_begin_catch(i8* %3) #4
181 %6 = bitcast i32* %0 to i8*
182 call void @free(i8* %6)
183 call void @__cxa_end_catch()
190 ; The malloc'ed memory can be captured and therefore not promoted.
191 define void @malloc_capture(i32** noalias %A) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
193 %call = call i8* @malloc(i64 4)
194 %0 = bitcast i8* %call to i32*
202 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.latch ]
203 %1 = load i32, i32* %0, align 4
204 %add = add nsw i32 %1, 1
205 store i32 %add, i32* %0, align 4
209 invoke void @boo_readnone()
210 to label %invoke.cont unwind label %lpad
216 store i32* %0, i32** %A
217 %inc = add i32 %i.0, 1
218 %cmp = icmp slt i32 %i.0, 1024
219 br i1 %cmp, label %for.body, label %for.end
225 %2 = landingpad { i8*, i32 }
227 %3 = extractvalue { i8*, i32 } %2, 0
228 %4 = extractvalue { i8*, i32 } %2, 1
232 %5 = call i8* @__cxa_begin_catch(i8* %3) #4
233 %6 = bitcast i32* %0 to i8*
234 call void @free(i8* %6)
235 call void @__cxa_end_catch()
242 ; Function Attrs: nounwind
243 declare noalias i8* @malloc(i64)
245 ; Function Attrs: nounwind
246 declare void @free(i8* nocapture)
250 ; This is an artifical example, readnone functions by definition cannot unwind
251 ; exceptions by calling the C++ exception throwing methods
252 ; This function should only be used to test malloc_capture.
253 declare void @boo_readnone() readnone
255 declare i32 @__gxx_personality_v0(...)
257 declare i8* @__cxa_begin_catch(i8*)
259 declare void @__cxa_end_catch()
261 declare i32 @llvm.eh.typeid.for(i8*)
263 declare void @f() uwtable