1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
5 define i32 @foo1(i32* %a) #0 {
7 %0 = load i32, i32* %a, align 4
9 ; Check that the alignment has been upgraded and that the assume has not
12 ; CHECK-DAG: load i32, i32* %a, align 32
13 ; CHECK-DAG: call void @llvm.assume
16 %ptrint = ptrtoint i32* %a to i64
17 %maskedptr = and i64 %ptrint, 31
18 %maskcond = icmp eq i64 %maskedptr, 0
19 tail call void @llvm.assume(i1 %maskcond)
24 define i32 @foo2(i32* %a) #0 {
26 ; Same check as in @foo1, but make sure it works if the assume is first too.
28 ; CHECK-DAG: load i32, i32* %a, align 32
29 ; CHECK-DAG: call void @llvm.assume
32 %ptrint = ptrtoint i32* %a to i64
33 %maskedptr = and i64 %ptrint, 31
34 %maskcond = icmp eq i64 %maskedptr, 0
35 tail call void @llvm.assume(i1 %maskcond)
37 %0 = load i32, i32* %a, align 4
41 declare void @llvm.assume(i1) #1
43 define i32 @simple(i32 %a) #1 {
46 ; CHECK-LABEL: @simple
47 ; CHECK: call void @llvm.assume
50 %cmp = icmp eq i32 %a, 4
51 tail call void @llvm.assume(i1 %cmp)
55 define i32 @can1(i1 %a, i1 %b, i1 %c) {
58 %and = and i1 %and1, %c
59 tail call void @llvm.assume(i1 %and)
62 ; CHECK: call void @llvm.assume(i1 %a)
63 ; CHECK: call void @llvm.assume(i1 %b)
64 ; CHECK: call void @llvm.assume(i1 %c)
70 define i32 @can2(i1 %a, i1 %b, i1 %c) {
74 tail call void @llvm.assume(i1 %w)
77 ; CHECK: %[[V1:[^ ]+]] = xor i1 %a, true
78 ; CHECK: call void @llvm.assume(i1 %[[V1]])
79 ; CHECK: %[[V2:[^ ]+]] = xor i1 %b, true
80 ; CHECK: call void @llvm.assume(i1 %[[V2]])
86 define i32 @bar1(i32 %a) #0 {
91 ; CHECK: call void @llvm.assume
95 %cmp = icmp eq i32 %and, 1
96 tail call void @llvm.assume(i1 %cmp)
101 define i32 @bar2(i32 %a) #0 {
104 ; CHECK: call void @llvm.assume
108 %cmp = icmp eq i32 %and, 1
109 tail call void @llvm.assume(i1 %cmp)
111 %and1 = and i32 %a, 3
115 define i32 @bar3(i32 %a, i1 %x, i1 %y) #0 {
117 %and1 = and i32 %a, 3
119 ; Don't be fooled by other assumes around.
121 ; CHECK: call void @llvm.assume
124 tail call void @llvm.assume(i1 %x)
127 %cmp = icmp eq i32 %and, 1
128 tail call void @llvm.assume(i1 %cmp)
130 tail call void @llvm.assume(i1 %y)
135 define i32 @bar4(i32 %a, i32 %b) {
137 %and1 = and i32 %b, 3
140 ; CHECK: call void @llvm.assume
141 ; CHECK: call void @llvm.assume
145 %cmp = icmp eq i32 %and, 1
146 tail call void @llvm.assume(i1 %cmp)
148 %cmp2 = icmp eq i32 %a, %b
149 tail call void @llvm.assume(i1 %cmp2)
154 define i32 @icmp1(i32 %a) #0 {
155 ; CHECK-LABEL: @icmp1(
156 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], 5
157 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
158 ; CHECK-NEXT: ret i32 1
160 %cmp = icmp sgt i32 %a, 5
161 tail call void @llvm.assume(i1 %cmp)
162 %conv = zext i1 %cmp to i32
166 define i32 @icmp2(i32 %a) #0 {
167 ; CHECK-LABEL: @icmp2(
168 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], 5
169 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
170 ; CHECK-NEXT: ret i32 0
172 %cmp = icmp sgt i32 %a, 5
173 tail call void @llvm.assume(i1 %cmp)
174 %t0 = zext i1 %cmp to i32
175 %lnot.ext = xor i32 %t0, 1
179 ; If the 'not' of a condition is known true, then the condition must be false.
181 define i1 @assume_not(i1 %cond) {
182 ; CHECK-LABEL: @assume_not(
183 ; CHECK-NEXT: [[NOTCOND:%.*]] = xor i1 [[COND:%.*]], true
184 ; CHECK-NEXT: call void @llvm.assume(i1 [[NOTCOND]])
185 ; CHECK-NEXT: ret i1 false
187 %notcond = xor i1 %cond, true
188 call void @llvm.assume(i1 %notcond)
192 declare void @escape(i32* %a)
194 ; Canonicalize a nonnull assumption on a load into metadata form.
196 define i1 @nonnull1(i32** %a) {
197 ; CHECK-LABEL: @nonnull1(
198 ; CHECK-NEXT: [[LOAD:%.*]] = load i32*, i32** %a, align 8, !nonnull !6
199 ; CHECK-NEXT: tail call void @escape(i32* nonnull [[LOAD]])
200 ; CHECK-NEXT: ret i1 false
202 %load = load i32*, i32** %a
203 %cmp = icmp ne i32* %load, null
204 tail call void @llvm.assume(i1 %cmp)
205 tail call void @escape(i32* %load)
206 %rval = icmp eq i32* %load, null
210 ; Make sure the above canonicalization applies only
211 ; to pointer types. Doing otherwise would be illegal.
213 define i1 @nonnull2(i32* %a) {
214 ; CHECK-LABEL: @nonnull2(
215 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, i32* %a, align 4
216 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[LOAD]], 0
217 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
218 ; CHECK-NEXT: [[RVAL:%.*]] = icmp eq i32 [[LOAD]], 0
219 ; CHECK-NEXT: ret i1 [[RVAL]]
221 %load = load i32, i32* %a
222 %cmp = icmp ne i32 %load, 0
223 tail call void @llvm.assume(i1 %cmp)
224 %rval = icmp eq i32 %load, 0
228 ; Make sure the above canonicalization does not trigger
229 ; if the assume is control dependent on something else
231 define i1 @nonnull3(i32** %a, i1 %control) {
232 ; CHECK-LABEL: @nonnull3(
234 ; CHECK-NEXT: [[LOAD:%.*]] = load i32*, i32** %a, align 8
235 ; CHECK-NEXT: br i1 %control, label %taken, label %not_taken
237 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32* [[LOAD]], null
238 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
239 ; CHECK-NEXT: [[RVAL:%.*]] = icmp eq i32* [[LOAD]], null
240 ; CHECK-NEXT: ret i1 [[RVAL]]
242 ; CHECK-NEXT: ret i1 true
245 %load = load i32*, i32** %a
246 %cmp = icmp ne i32* %load, null
247 br i1 %control, label %taken, label %not_taken
249 tail call void @llvm.assume(i1 %cmp)
250 %rval = icmp eq i32* %load, null
256 ; Make sure the above canonicalization does not trigger
257 ; if the path from the load to the assume is potentially
258 ; interrupted by an exception being thrown
260 define i1 @nonnull4(i32** %a) {
261 ; CHECK-LABEL: @nonnull4(
262 ; CHECK-NEXT: [[LOAD:%.*]] = load i32*, i32** %a, align 8
263 ; CHECK-NEXT: tail call void @escape(i32* [[LOAD]])
264 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32* [[LOAD]], null
265 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
266 ; CHECK-NEXT: [[RVAL:%.*]] = icmp eq i32* [[LOAD]], null
267 ; CHECK-NEXT: ret i1 [[RVAL]]
269 %load = load i32*, i32** %a
270 ;; This call may throw!
271 tail call void @escape(i32* %load)
272 %cmp = icmp ne i32* %load, null
273 tail call void @llvm.assume(i1 %cmp)
274 %rval = icmp eq i32* %load, null
278 ; PR35846 - https://bugs.llvm.org/show_bug.cgi?id=35846
280 define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
281 ; CHECK-LABEL: @assumption_conflicts_with_known_bits(
282 ; CHECK-NEXT: tail call void @llvm.assume(i1 false)
283 ; CHECK-NEXT: ret i32 0
285 %and1 = and i32 %b, 3
286 %B1 = lshr i32 %and1, %and1
287 %B3 = shl nuw nsw i32 %and1, %B1
288 %cmp = icmp eq i32 %B3, 1
289 tail call void @llvm.assume(i1 %cmp)
290 %cmp2 = icmp eq i32 %B1, %B3
291 tail call void @llvm.assume(i1 %cmp2)
295 ; PR37726 - https://bugs.llvm.org/show_bug.cgi?id=37726
296 ; There's a loophole in eliminating a redundant assumption when
297 ; we have conflicting assumptions. Verify that debuginfo doesn't
298 ; get in the way of the fold.
300 define void @debug_interference(i8 %x) {
301 ; CHECK-LABEL: @debug_interference(
302 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
303 ; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata !7, metadata !DIExpression()), !dbg !9
304 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP1]])
305 ; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata !7, metadata !DIExpression()), !dbg !9
306 ; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata !7, metadata !DIExpression()), !dbg !9
307 ; CHECK-NEXT: tail call void @llvm.assume(i1 false)
308 ; CHECK-NEXT: ret void
310 %cmp1 = icmp eq i8 %x, 0
311 %cmp2 = icmp ne i8 %x, 0
312 tail call void @llvm.assume(i1 %cmp1)
313 tail call void @llvm.dbg.value(metadata i32 5, metadata !1, metadata !DIExpression()), !dbg !9
314 tail call void @llvm.assume(i1 %cmp1)
315 tail call void @llvm.dbg.value(metadata i32 5, metadata !1, metadata !DIExpression()), !dbg !9
316 tail call void @llvm.assume(i1 %cmp2)
317 tail call void @llvm.dbg.value(metadata i32 5, metadata !1, metadata !DIExpression()), !dbg !9
318 tail call void @llvm.assume(i1 %cmp2)
323 ; Does it ever make sense to peek through a bitcast of the icmp operand?
325 define i32 @PR40940(<4 x i8> %x) {
326 ; CHECK-LABEL: @PR40940(
327 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i8> [[X:%.*]], <4 x i8> undef, <4 x i32> <i32 1, i32 1, i32 2, i32 3>
328 ; CHECK-NEXT: [[T2:%.*]] = bitcast <4 x i8> [[SHUF]] to i32
329 ; CHECK-NEXT: [[T3:%.*]] = icmp ult i32 [[T2]], 65536
330 ; CHECK-NEXT: call void @llvm.assume(i1 [[T3]])
331 ; CHECK-NEXT: ret i32 [[T2]]
333 %shuf = shufflevector <4 x i8> %x, <4 x i8> undef, <4 x i32> <i32 1, i32 1, i32 2, i32 3>
334 %t2 = bitcast <4 x i8> %shuf to i32
335 %t3 = icmp ult i32 %t2, 65536
336 call void @llvm.assume(i1 %t3)
340 declare void @llvm.dbg.value(metadata, metadata, metadata)
343 !llvm.module.flags = !{!5, !6, !7, !8}
345 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "Me", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: null, retainedTypes: null, imports: null)
346 !1 = !DILocalVariable(name: "", arg: 1, scope: !2, file: null, line: 1, type: null)
347 !2 = distinct !DISubprogram(name: "debug", linkageName: "debug", scope: null, file: null, line: 0, type: null, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: true, unit: !0)
348 !3 = !DIFile(filename: "consecutive-fences.ll", directory: "")
349 !5 = !{i32 2, !"Dwarf Version", i32 4}
350 !6 = !{i32 2, !"Debug Info Version", i32 3}
351 !7 = !{i32 1, !"wchar_size", i32 4}
352 !8 = !{i32 7, !"PIC Level", i32 2}
353 !9 = !DILocation(line: 0, column: 0, scope: !2)
356 attributes #0 = { nounwind uwtable }
357 attributes #1 = { nounwind }