1 ; RUN: opt < %s -basicaa -licm -S | FileCheck %s
3 declare i32 @strlen(i8*) readonly
7 ; Sink readonly function.
8 define i32 @test1(i8* %P) {
11 Loop: ; preds = %Loop, %0
12 %A = call i32 @strlen( i8* %P ) readonly
13 br i1 false, label %Loop, label %Out
19 ; CHECK-NEXT: call i32 @strlen
20 ; CHECK-NEXT: ret i32 %A
23 declare double @sin(double) readnone
25 ; Sink readnone function out of loop with unknown memory behavior.
26 define double @test2(double %X) {
29 Loop: ; preds = %Loop, %0
31 %A = call double @sin( double %X ) readnone
32 br i1 true, label %Loop, label %Out
38 ; CHECK-NEXT: call double @sin
39 ; CHECK-NEXT: ret double %A
42 ; This testcase checks to make sure the sinker does not cause problems with
44 define void @test3() {
46 br i1 false, label %Loop, label %Exit
49 br i1 false, label %Loop, label %Exit
51 %Y = phi i32 [ 0, %Entry ], [ %X, %Loop ]
55 ; CHECK: Exit.loopexit:
56 ; CHECK-NEXT: %X = add i32 0, 1
57 ; CHECK-NEXT: br label %Exit
61 ; If the result of an instruction is only used outside of the loop, sink
62 ; the instruction to the exit blocks instead of executing it on every
63 ; iteration of the loop.
65 define i32 @test4(i32 %N) {
68 Loop: ; preds = %Loop, %Entry
69 %N_addr.0.pn = phi i32 [ %dec, %Loop ], [ %N, %Entry ]
70 %tmp.6 = mul i32 %N, %N_addr.0.pn ; <i32> [#uses=1]
71 %tmp.7 = sub i32 %tmp.6, %N ; <i32> [#uses=1]
72 %dec = add i32 %N_addr.0.pn, -1 ; <i32> [#uses=1]
73 %tmp.1 = icmp ne i32 %N_addr.0.pn, 1 ; <i1> [#uses=1]
74 br i1 %tmp.1, label %Loop, label %Out
79 ; CHECK-NEXT: mul i32 %N, %N_addr.0.pn
80 ; CHECK-NEXT: sub i32 %tmp.6, %N
84 ; To reduce register pressure, if a load is hoistable out of the loop, and the
85 ; result of the load is only used outside of the loop, sink the load instead of
88 @X = global i32 5 ; <i32*> [#uses=1]
90 define i32 @test5(i32 %N) {
93 Loop: ; preds = %Loop, %Entry
94 %N_addr.0.pn = phi i32 [ %dec, %Loop ], [ %N, %Entry ]
95 %tmp.6 = load i32* @X ; <i32> [#uses=1]
96 %dec = add i32 %N_addr.0.pn, -1 ; <i32> [#uses=1]
97 %tmp.1 = icmp ne i32 %N_addr.0.pn, 1 ; <i1> [#uses=1]
98 br i1 %tmp.1, label %Loop, label %Out
103 ; CHECK-NEXT: %tmp.6 = load i32* @X
104 ; CHECK-NEXT: ret i32 %tmp.6
109 ; The loop sinker was running from the bottom of the loop to the top, causing
110 ; it to miss opportunities to sink instructions that depended on sinking other
111 ; instructions from the loop. Instead they got hoisted, which is better than
112 ; leaving them in the loop, but increases register pressure pointlessly.
114 %Ty = type { i32, i32 }
115 @X2 = external global %Ty
117 define i32 @test6() {
120 %dead = getelementptr %Ty* @X2, i64 0, i32 0
121 %sunk2 = load i32* %dead
122 br i1 false, label %Loop, label %Out
127 ; CHECK-NEXT: %dead = getelementptr %Ty* @X2, i64 0, i32 0
128 ; CHECK-NEXT: %sunk2 = load i32* %dead
129 ; CHECK-NEXT: ret i32 %sunk2
134 ; This testcase ensures that we can sink instructions from loops with
137 define i32 @test7(i32 %N, i1 %C) {
140 Loop: ; preds = %ContLoop, %Entry
141 %N_addr.0.pn = phi i32 [ %dec, %ContLoop ], [ %N, %Entry ]
142 %tmp.6 = mul i32 %N, %N_addr.0.pn
143 %tmp.7 = sub i32 %tmp.6, %N ; <i32> [#uses=2]
144 %dec = add i32 %N_addr.0.pn, -1 ; <i32> [#uses=1]
145 br i1 %C, label %ContLoop, label %Out1
147 %tmp.1 = icmp ne i32 %N_addr.0.pn, 1
148 br i1 %tmp.1, label %Loop, label %Out2
149 Out1: ; preds = %Loop
151 Out2: ; preds = %ContLoop
155 ; CHECK-NEXT: mul i32 %N, %N_addr.0.pn
156 ; CHECK-NEXT: sub i32 %tmp.6, %N
159 ; CHECK-NEXT: mul i32 %N, %N_addr.0.pn
160 ; CHECK-NEXT: sub i32 %tmp.6
165 ; This testcase checks to make sure we can sink values which are only live on
166 ; some exits out of the loop, and that we can do so without breaking dominator
168 define i32 @test8(i1 %C1, i1 %C2, i32* %P, i32* %Q) {
171 Loop: ; preds = %Cont, %Entry
172 br i1 %C1, label %Cont, label %exit1
173 Cont: ; preds = %Loop
174 %X = load i32* %P ; <i32> [#uses=2]
175 store i32 %X, i32* %Q
176 %V = add i32 %X, 1 ; <i32> [#uses=1]
177 br i1 %C2, label %Loop, label %exit2
178 exit1: ; preds = %Loop
180 exit2: ; preds = %Cont
184 ; CHECK-NEXT: ret i32 0
186 ; CHECK-NEXT: %V = add i32 %X, 1
187 ; CHECK-NEXT: ret i32 %V
191 define void @test9() {
193 br i1 false, label %no_exit.1.i.preheader, label %loopentry.3.i.preheader
194 no_exit.1.i.preheader: ; preds = %loopentry.2.i
195 br label %no_exit.1.i
196 no_exit.1.i: ; preds = %endif.8.i, %no_exit.1.i.preheader
197 br i1 false, label %return.i, label %endif.8.i
198 endif.8.i: ; preds = %no_exit.1.i
199 %inc.1.i = add i32 0, 1 ; <i32> [#uses=1]
200 br i1 false, label %no_exit.1.i, label %loopentry.3.i.preheader.loopexit
201 loopentry.3.i.preheader.loopexit: ; preds = %endif.8.i
202 br label %loopentry.3.i.preheader
203 loopentry.3.i.preheader: ; preds = %loopentry.3.i.preheader.loopexit, %loopentry.2.i
204 %arg_num.0.i.ph13000 = phi i32 [ 0, %loopentry.2.i ], [ %inc.1.i, %loopentry.3.i.preheader.loopexit ] ; <i32> [#uses=0]
206 return.i: ; preds = %no_exit.1.i
210 ; CHECK: loopentry.3.i.preheader.loopexit:
211 ; CHECK-NEXT: %inc.1.i = add i32 0, 1
212 ; CHECK-NEXT: br label %loopentry.3.i.preheader
216 ; Potentially trapping instructions may be sunk as long as they are guaranteed
218 define i32 @test10(i32 %N) {
221 Loop: ; preds = %Loop, %Entry
222 %N_addr.0.pn = phi i32 [ %dec, %Loop ], [ %N, %Entry ] ; <i32> [#uses=3]
223 %tmp.6 = sdiv i32 %N, %N_addr.0.pn ; <i32> [#uses=1]
224 %dec = add i32 %N_addr.0.pn, -1 ; <i32> [#uses=1]
225 %tmp.1 = icmp ne i32 %N_addr.0.pn, 0 ; <i1> [#uses=1]
226 br i1 %tmp.1, label %Loop, label %Out
232 ; CHECK-NEXT: %tmp.6 = sdiv i32 %N, %N_addr.0.pn
233 ; CHECK-NEXT: ret i32 %tmp.6