1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S < %s | FileCheck %s
4 define void @ifconvertstore(ptr %A, i32 %B, i32 %C, i32 %D) {
5 ; CHECK-LABEL: @ifconvertstore(
7 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A:%.*]], align 4
8 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[D:%.*]], 42
9 ; CHECK-NEXT: [[SPEC_STORE_SELECT:%.*]] = select i1 [[CMP]], i32 [[C:%.*]], i32 [[B]], !prof [[PROF0:![0-9]+]]
10 ; CHECK-NEXT: store i32 [[SPEC_STORE_SELECT]], ptr [[A]], align 4
11 ; CHECK-NEXT: ret void
14 ; First store to the location.
16 %cmp = icmp sgt i32 %D, 42
17 br i1 %cmp, label %if.then, label %ret.end, !prof !0
19 ; Make sure we speculate stores like the following one. It is cheap compared to
20 ; a mispredicated branch.
29 ; Store to a different location.
31 define void @noifconvertstore1(ptr %A1, ptr %A2, i32 %B, i32 %C, i32 %D) {
32 ; CHECK-LABEL: @noifconvertstore1(
34 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A1:%.*]], align 4
35 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[D:%.*]], 42
36 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
38 ; CHECK-NEXT: store i32 [[C:%.*]], ptr [[A2:%.*]], align 4
39 ; CHECK-NEXT: br label [[RET_END]]
41 ; CHECK-NEXT: ret void
45 %cmp = icmp sgt i32 %D, 42
46 br i1 %cmp, label %if.then, label %ret.end
56 ; This function could store to our address, so we can't repeat the first store a second time.
57 declare void @unknown_fun()
59 define void @noifconvertstore2(ptr %A, i32 %B, i32 %C, i32 %D) {
60 ; CHECK-LABEL: @noifconvertstore2(
62 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A:%.*]], align 4
63 ; CHECK-NEXT: call void @unknown_fun()
64 ; CHECK-NEXT: [[CMP6:%.*]] = icmp sgt i32 [[D:%.*]], 42
65 ; CHECK-NEXT: br i1 [[CMP6]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
67 ; CHECK-NEXT: store i32 [[C:%.*]], ptr [[A]], align 4
68 ; CHECK-NEXT: br label [[RET_END]]
70 ; CHECK-NEXT: ret void
73 ; First store to the location.
75 call void @unknown_fun()
76 %cmp6 = icmp sgt i32 %D, 42
77 br i1 %cmp6, label %if.then, label %ret.end
87 ; Make sure we don't speculate volatile stores.
89 define void @noifconvertstore_volatile(ptr %A, i32 %B, i32 %C, i32 %D) {
90 ; CHECK-LABEL: @noifconvertstore_volatile(
92 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A:%.*]], align 4
93 ; CHECK-NEXT: [[CMP6:%.*]] = icmp sgt i32 [[D:%.*]], 42
94 ; CHECK-NEXT: br i1 [[CMP6]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
96 ; CHECK-NEXT: store volatile i32 [[C:%.*]], ptr [[A]], align 4
97 ; CHECK-NEXT: br label [[RET_END]]
99 ; CHECK-NEXT: ret void
102 ; First store to the location.
104 %cmp6 = icmp sgt i32 %D, 42
105 br i1 %cmp6, label %if.then, label %ret.end
108 store volatile i32 %C, ptr %A
116 ;; Speculate a store, preceded by a local, non-escaping load
117 define i32 @load_before_store_noescape(i64 %i, i32 %b) {
118 ; CHECK-LABEL: @load_before_store_noescape(
120 ; CHECK-NEXT: [[A:%.*]] = alloca [2 x i32], align 8
121 ; CHECK-NEXT: store i64 4294967296, ptr [[A]], align 8
122 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
123 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
124 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[B:%.*]]
125 ; CHECK-NEXT: [[SPEC_STORE_SELECT:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[TMP0]]
126 ; CHECK-NEXT: store i32 [[SPEC_STORE_SELECT]], ptr [[ARRAYIDX]], align 4
127 ; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[A]], align 4
128 ; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
129 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
130 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP2]]
131 ; CHECK-NEXT: ret i32 [[ADD]]
134 %a = alloca [2 x i32], align 8
135 store i64 4294967296, ptr %a, align 8
136 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 %i
137 %0 = load i32, ptr %arrayidx, align 4
138 %cmp = icmp slt i32 %0, %b
139 br i1 %cmp, label %if.then, label %if.end
142 store i32 %b, ptr %arrayidx, align 4
146 %1 = load i32, ptr %a, align 4
147 %arrayidx2 = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
148 %2 = load i32, ptr %arrayidx2, align 4
149 %add = add nsw i32 %1, %2
153 ;; Don't speculate a store, preceded by a local, escaping load
154 define i32 @load_before_store_escape(i64 %i, i32 %b) {
155 ; CHECK-LABEL: @load_before_store_escape(
157 ; CHECK-NEXT: [[A:%.*]] = alloca [2 x i32], align 8
158 ; CHECK-NEXT: store i64 4294967296, ptr [[A]], align 8
159 ; CHECK-NEXT: call void @fork_some_threads(ptr [[A]])
160 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
161 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
162 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[B:%.*]]
163 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
165 ; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
166 ; CHECK-NEXT: br label [[IF_END]]
168 ; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[A]], align 4
169 ; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
170 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
171 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP2]]
172 ; CHECK-NEXT: call void @join_some_threads()
173 ; CHECK-NEXT: ret i32 [[ADD]]
176 %a = alloca [2 x i32], align 8
177 store i64 4294967296, ptr %a, align 8
178 call void @fork_some_threads(ptr %a)
179 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 %i
180 %0 = load i32, ptr %arrayidx, align 4
181 %cmp = icmp slt i32 %0, %b
182 br i1 %cmp, label %if.then, label %if.end
185 store i32 %b, ptr %arrayidx, align 4
189 %1 = load i32, ptr %a, align 4
190 %arrayidx2 = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
191 %2 = load i32, ptr %arrayidx2, align 4
192 %add = add nsw i32 %1, %2
193 call void @join_some_threads()
197 declare void @fork_some_threads(ptr);
198 declare void @join_some_threads();
200 ; Don't speculate if it's not the only instruction in the block (not counting
202 define i32 @not_alone_in_block(i64 %i, i32 %b) {
203 ; CHECK-LABEL: @not_alone_in_block(
205 ; CHECK-NEXT: [[A:%.*]] = alloca [2 x i32], align 8
206 ; CHECK-NEXT: store i64 4294967296, ptr [[A]], align 8
207 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
208 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
209 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[B:%.*]]
210 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
212 ; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
213 ; CHECK-NEXT: store i32 [[B]], ptr [[A]], align 4
214 ; CHECK-NEXT: br label [[IF_END]]
216 ; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[A]], align 4
217 ; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
218 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
219 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP2]]
220 ; CHECK-NEXT: ret i32 [[ADD]]
223 %a = alloca [2 x i32], align 8
224 store i64 4294967296, ptr %a, align 8
225 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 %i
226 %0 = load i32, ptr %arrayidx, align 4
227 %cmp = icmp slt i32 %0, %b
228 br i1 %cmp, label %if.then, label %if.end
231 store i32 %b, ptr %arrayidx, align 4
232 store i32 %b, ptr %a, align 4
236 %1 = load i32, ptr %a, align 4
237 %arrayidx2 = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
238 %2 = load i32, ptr %arrayidx2, align 4
239 %add = add nsw i32 %1, %2
243 define void @wrong_align_store(ptr %A, i32 %B, i32 %C, i32 %D) {
244 ; CHECK-LABEL: @wrong_align_store(
246 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A:%.*]], align 4
247 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[D:%.*]], 42
248 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
250 ; CHECK-NEXT: store i32 [[C:%.*]], ptr [[A]], align 8
251 ; CHECK-NEXT: br label [[RET_END]]
253 ; CHECK-NEXT: ret void
256 store i32 %B, ptr %A, align 4
257 %cmp = icmp sgt i32 %D, 42
258 br i1 %cmp, label %if.then, label %ret.end
261 store i32 %C, ptr %A, align 8
268 define void @wrong_align_load(i32 %C, i32 %D) {
269 ; CHECK-LABEL: @wrong_align_load(
271 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
272 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A]], align 4
273 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[D:%.*]], 42
274 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
276 ; CHECK-NEXT: store i32 [[C:%.*]], ptr [[A]], align 8
277 ; CHECK-NEXT: br label [[RET_END]]
279 ; CHECK-NEXT: ret void
282 %A = alloca i32, align 4
283 load i32, ptr %A, align 4
284 %cmp = icmp sgt i32 %D, 42
285 br i1 %cmp, label %if.then, label %ret.end
288 store i32 %C, ptr %A, align 8
295 ; CHECK: !0 = !{!"branch_weights", i32 3, i32 5}
296 !0 = !{!"branch_weights", i32 3, i32 5}