1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine -S < %s | FileCheck %s
4 ;; This tests that the instructions in the entry blocks are sunk into each
7 define i32 @test1(i1 %C, i32 %A, i32 %B) {
10 ; CHECK-NEXT: br i1 [[C:%.*]], label [[THEN:%.*]], label [[ENDIF:%.*]]
12 ; CHECK-NEXT: [[TMP_9:%.*]] = add i32 [[B:%.*]], [[A:%.*]]
13 ; CHECK-NEXT: ret i32 [[TMP_9]]
15 ; CHECK-NEXT: [[TMP_2:%.*]] = sdiv i32 [[A]], [[B]]
16 ; CHECK-NEXT: ret i32 [[TMP_2]]
19 %tmp.2 = sdiv i32 %A, %B ; <i32> [#uses=1]
20 %tmp.9 = add i32 %B, %A ; <i32> [#uses=1]
21 br i1 %C, label %then, label %endif
23 then: ; preds = %entry
26 endif: ; preds = %entry
31 ;; PHI use, sink divide before call.
32 define i32 @test2(i32 %x) nounwind ssp {
33 ; CHECK-LABEL: @test2(
35 ; CHECK-NEXT: br label [[BB:%.*]]
37 ; CHECK-NEXT: [[X_ADDR_17:%.*]] = phi i32 [ [[X:%.*]], [[ENTRY:%.*]] ], [ [[X_ADDR_0:%.*]], [[BB2:%.*]] ]
38 ; CHECK-NEXT: [[I_06:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[TMP4:%.*]], [[BB2]] ]
39 ; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i32 [[X_ADDR_17]], 0
40 ; CHECK-NEXT: br i1 [[TMP0]], label [[BB1:%.*]], label [[BB2]]
42 ; CHECK-NEXT: [[TMP1:%.*]] = add nsw i32 [[X_ADDR_17]], 1
43 ; CHECK-NEXT: [[TMP2:%.*]] = sdiv i32 [[TMP1]], [[X_ADDR_17]]
44 ; CHECK-NEXT: [[TMP3:%.*]] = tail call i32 @bar() #[[ATTR1:[0-9]+]]
45 ; CHECK-NEXT: br label [[BB2]]
47 ; CHECK-NEXT: [[X_ADDR_0]] = phi i32 [ [[TMP2]], [[BB1]] ], [ [[X_ADDR_17]], [[BB]] ]
48 ; CHECK-NEXT: [[TMP4]] = add nuw nsw i32 [[I_06]], 1
49 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[TMP4]], 1000000
50 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[BB4:%.*]], label [[BB]]
52 ; CHECK-NEXT: ret i32 [[X_ADDR_0]]
57 bb: ; preds = %bb2, %entry
58 %x_addr.17 = phi i32 [ %x, %entry ], [ %x_addr.0, %bb2 ] ; <i32> [#uses=4]
59 %i.06 = phi i32 [ 0, %entry ], [ %4, %bb2 ] ; <i32> [#uses=1]
60 %0 = add nsw i32 %x_addr.17, 1 ; <i32> [#uses=1]
61 %1 = sdiv i32 %0, %x_addr.17 ; <i32> [#uses=1]
62 %2 = icmp eq i32 %x_addr.17, 0 ; <i1> [#uses=1]
63 br i1 %2, label %bb1, label %bb2
66 %3 = tail call i32 @bar() nounwind ; <i32> [#uses=0]
69 bb2: ; preds = %bb, %bb1
70 %x_addr.0 = phi i32 [ %1, %bb1 ], [ %x_addr.17, %bb ] ; <i32> [#uses=2]
71 %4 = add nsw i32 %i.06, 1 ; <i32> [#uses=2]
72 %exitcond = icmp eq i32 %4, 1000000 ; <i1> [#uses=1]
73 br i1 %exitcond, label %bb4, label %bb
81 define i32 @test3(i32* nocapture readonly %P, i32 %i) {
82 ; CHECK-LABEL: @test3(
84 ; CHECK-NEXT: switch i32 [[I:%.*]], label [[SW_EPILOG:%.*]] [
85 ; CHECK-NEXT: i32 5, label [[SW_BB:%.*]]
86 ; CHECK-NEXT: i32 2, label [[SW_BB]]
89 ; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[I]] to i64
90 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[IDXPROM]]
91 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[ARRAYIDX]], align 4
92 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP0]], [[I]]
93 ; CHECK-NEXT: br label [[SW_EPILOG]]
95 ; CHECK-NEXT: [[SUM_0:%.*]] = phi i32 [ [[ADD]], [[SW_BB]] ], [ 0, [[ENTRY:%.*]] ]
96 ; CHECK-NEXT: ret i32 [[SUM_0]]
99 %idxprom = sext i32 %i to i64
100 %arrayidx = getelementptr inbounds i32, i32* %P, i64 %idxprom
101 %0 = load i32, i32* %arrayidx, align 4
102 switch i32 %i, label %sw.epilog [
107 sw.bb: ; preds = %entry, %entry
108 %add = add nsw i32 %0, %i
111 sw.epilog: ; preds = %entry, %sw.bb
112 %sum.0 = phi i32 [ %add, %sw.bb ], [ 0, %entry ]
116 declare i32 @foo(i32, i32)
117 ; Two uses in a single user. We can still sink the instruction (tmp.9).
118 define i32 @test4(i32 %A, i32 %B, i1 %C) {
119 ; CHECK-LABEL: @test4(
121 ; CHECK-NEXT: br i1 [[C:%.*]], label [[THEN:%.*]], label [[ENDIF:%.*]]
123 ; CHECK-NEXT: [[TMP_9:%.*]] = add i32 [[B:%.*]], [[A:%.*]]
124 ; CHECK-NEXT: [[RES:%.*]] = call i32 @foo(i32 [[TMP_9]], i32 [[TMP_9]])
125 ; CHECK-NEXT: ret i32 [[RES]]
127 ; CHECK-NEXT: [[TMP_2:%.*]] = sdiv i32 [[A]], [[B]]
128 ; CHECK-NEXT: ret i32 [[TMP_2]]
131 %tmp.2 = sdiv i32 %A, %B ; <i32> [#uses=1]
132 %tmp.9 = add i32 %B, %A ; <i32> [#uses=1]
133 br i1 %C, label %then, label %endif
135 then: ; preds = %entry
136 %res = call i32 @foo(i32 %tmp.9, i32 %tmp.9)
139 endif: ; preds = %entry
143 ; Two uses in a single user (phi node). We just bail out.
144 define i32 @test5(i32* nocapture readonly %P, i32 %i, i1 %cond) {
145 ; CHECK-LABEL: @test5(
147 ; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[I:%.*]] to i64
148 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[IDXPROM]]
149 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[ARRAYIDX]], align 4
150 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[DISPATCHBB:%.*]], label [[SW_EPILOG:%.*]]
152 ; CHECK-NEXT: [[ADD:%.*]] = shl nsw i32 [[I]], 1
153 ; CHECK-NEXT: br label [[SW_EPILOG]]
155 ; CHECK-NEXT: br label [[SW_EPILOG]]
157 ; CHECK-NEXT: [[SUM_0:%.*]] = phi i32 [ [[TMP0]], [[SW_BB:%.*]] ], [ [[ADD]], [[DISPATCHBB]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
158 ; CHECK-NEXT: ret i32 [[SUM_0]]
161 %idxprom = sext i32 %i to i64
162 %arrayidx = getelementptr inbounds i32, i32* %P, i64 %idxprom
163 %0 = load i32, i32* %arrayidx, align 4
164 br i1 %cond, label %dispatchBB, label %sw.epilog
167 %add = add nsw i32 %i, %i
170 sw.bb: ; preds = %entry, %entry
173 sw.epilog: ; preds = %entry, %sw.bb
174 %sum.0 = phi i32 [ %0, %sw.bb ], [ %add, %dispatchBB ], [ %0, %entry ]
178 ; Multiple uses but from same BB. We can sink.
179 define i32 @test6(i32* nocapture readonly %P, i32 %i, i1 %cond) {
180 ; CHECK-LABEL: @test6(
182 ; CHECK-NEXT: [[ADD:%.*]] = shl nsw i32 [[I]], 1
183 ; CHECK-NEXT: br label [[DISPATCHBB:%.*]]
185 ; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[I:%.*]] to i64
186 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[IDXPROM]]
187 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[ARRAYIDX]], align 4
188 ; CHECK-NEXT: switch i32 [[I]], label [[SW_BB:%.*]] [
189 ; CHECK-NEXT: i32 5, label [[SW_EPILOG:%.*]]
190 ; CHECK-NEXT: i32 2, label [[SW_EPILOG]]
193 ; CHECK-NEXT: br label [[SW_EPILOG]]
195 ; CHECK-NEXT: [[SUM_0:%.*]] = phi i32 [ [[ADD]], [[SW_BB]] ], [ [[TMP0]], [[DISPATCHBB]] ], [ [[TMP0]], [[DISPATCHBB]] ]
196 ; CHECK-NEXT: ret i32 [[SUM_0]]
199 %idxprom = sext i32 %i to i64
200 %arrayidx = getelementptr inbounds i32, i32* %P, i64 %idxprom
201 %0 = load i32, i32* %arrayidx, align 4
202 %add = add nsw i32 %i, %i
206 switch i32 %i, label %sw.bb [
207 i32 5, label %sw.epilog
208 i32 2, label %sw.epilog
211 sw.bb: ; preds = %entry, %entry
214 sw.epilog: ; preds = %entry, %sw.bb
215 %sum.0 = phi i32 [ %add, %sw.bb ], [ %0, %dispatchBB ], [ %0, %dispatchBB ]
219 declare void @checkd(double)
220 declare double @log(double) willreturn nounwind readnone
221 define void @test7(i1 %cond, double %d) {
222 ; CHECK-LABEL: @test7(
223 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
225 ; CHECK-NEXT: [[A:%.*]] = call double @log(double [[D:%.*]])
226 ; CHECK-NEXT: call void @checkd(double [[A]])
227 ; CHECK-NEXT: ret void
229 ; CHECK-NEXT: ret void
231 %A = call double @log(double %d)
232 br i1 %cond, label %if, label %else
235 call void @checkd(double %A)