1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes='default<O3>' -S < %s | FileCheck %s --check-prefixes=ALL,O3
3 ; RUN: opt -passes='default<O2>' -S < %s | FileCheck %s --check-prefixes=ALL,O2
4 ; RUN: opt -passes='default<O1>' -S < %s | FileCheck %s --check-prefixes=ALL,O1
6 ; All these tests should optimize to a single comparison
7 ; of the original argument with null. There should be no loops.
9 %struct.node = type { ptr, i32 }
11 define dso_local zeroext i1 @is_not_empty_variant1(ptr %p) {
12 ; ALL-LABEL: @is_not_empty_variant1(
14 ; ALL-NEXT: [[TOBOOL_NOT3_I:%.*]] = icmp ne ptr [[P:%.*]], null
15 ; ALL-NEXT: ret i1 [[TOBOOL_NOT3_I]]
18 %p.addr = alloca ptr, align 8
19 store ptr %p, ptr %p.addr, align 8
20 %0 = load ptr, ptr %p.addr, align 8
21 %call = call i32 @count_nodes_variant1(ptr %0)
22 %cmp = icmp sgt i32 %call, 0
26 define internal i32 @count_nodes_variant1(ptr %p) {
28 %p.addr = alloca ptr, align 8
29 %size = alloca i32, align 4
30 store ptr %p, ptr %p.addr, align 8
31 store i32 0, ptr %size, align 4
35 %0 = load ptr, ptr %p.addr, align 8
36 %tobool = icmp ne ptr %0, null
37 br i1 %tobool, label %while.body, label %while.end
40 %1 = load ptr, ptr %p.addr, align 8
41 %2 = load ptr, ptr %1, align 8
42 store ptr %2, ptr %p.addr, align 8
43 %3 = load i32, ptr %size, align 4
44 %inc = add nsw i32 %3, 1
45 store i32 %inc, ptr %size, align 4
46 br label %while.cond, !llvm.loop !0
49 %4 = load i32, ptr %size, align 4
53 define dso_local zeroext i1 @is_not_empty_variant2(ptr %p) {
54 ; ALL-LABEL: @is_not_empty_variant2(
56 ; ALL-NEXT: [[TOBOOL_NOT4_I:%.*]] = icmp ne ptr [[P:%.*]], null
57 ; ALL-NEXT: ret i1 [[TOBOOL_NOT4_I]]
60 %p.addr = alloca ptr, align 8
61 store ptr %p, ptr %p.addr, align 8
62 %0 = load ptr, ptr %p.addr, align 8
63 %call = call i64 @count_nodes_variant2(ptr %0)
64 %cmp = icmp ugt i64 %call, 0
68 define internal i64 @count_nodes_variant2(ptr %p) {
70 %p.addr = alloca ptr, align 8
71 %size = alloca i64, align 8
72 store ptr %p, ptr %p.addr, align 8
73 store i64 0, ptr %size, align 8
77 %0 = load ptr, ptr %p.addr, align 8
78 %tobool = icmp ne ptr %0, null
79 br i1 %tobool, label %while.body, label %while.end
82 %1 = load ptr, ptr %p.addr, align 8
83 %2 = load ptr, ptr %1, align 8
84 store ptr %2, ptr %p.addr, align 8
85 %3 = load i64, ptr %size, align 8
87 store i64 %inc, ptr %size, align 8
88 %4 = load i64, ptr %size, align 8
89 %cmp = icmp ne i64 %4, 0
90 call void @_ZL6assumeb(i1 zeroext %cmp)
91 br label %while.cond, !llvm.loop !2
94 %5 = load i64, ptr %size, align 8
98 define dso_local zeroext i1 @is_not_empty_variant3(ptr %p) {
99 ; O3-LABEL: @is_not_empty_variant3(
101 ; O3-NEXT: [[TOBOOL_NOT4_I:%.*]] = icmp ne ptr [[P:%.*]], null
102 ; O3-NEXT: ret i1 [[TOBOOL_NOT4_I]]
104 ; O2-LABEL: @is_not_empty_variant3(
106 ; O2-NEXT: [[TOBOOL_NOT4_I:%.*]] = icmp ne ptr [[P:%.*]], null
107 ; O2-NEXT: ret i1 [[TOBOOL_NOT4_I]]
109 ; O1-LABEL: @is_not_empty_variant3(
111 ; O1-NEXT: [[TOBOOL_NOT4_I:%.*]] = icmp eq ptr [[P:%.*]], null
112 ; O1-NEXT: br i1 [[TOBOOL_NOT4_I]], label [[COUNT_NODES_VARIANT3_EXIT:%.*]], label [[WHILE_BODY_I:%.*]]
114 ; O1-NEXT: [[SIZE_06_I:%.*]] = phi i64 [ [[INC_I:%.*]], [[WHILE_BODY_I]] ], [ 0, [[ENTRY:%.*]] ]
115 ; O1-NEXT: [[P_ADDR_05_I:%.*]] = phi ptr [ [[TMP0:%.*]], [[WHILE_BODY_I]] ], [ [[P]], [[ENTRY]] ]
116 ; O1-NEXT: [[CMP_I:%.*]] = icmp ne i64 [[SIZE_06_I]], -1
117 ; O1-NEXT: call void @llvm.assume(i1 [[CMP_I]])
118 ; O1-NEXT: [[TMP0]] = load ptr, ptr [[P_ADDR_05_I]], align 8
119 ; O1-NEXT: [[INC_I]] = add i64 [[SIZE_06_I]], 1
120 ; O1-NEXT: [[TOBOOL_NOT_I:%.*]] = icmp eq ptr [[TMP0]], null
121 ; O1-NEXT: br i1 [[TOBOOL_NOT_I]], label [[COUNT_NODES_VARIANT3_EXIT_LOOPEXIT:%.*]], label [[WHILE_BODY_I]], !llvm.loop [[LOOP0:![0-9]+]]
122 ; O1: count_nodes_variant3.exit.loopexit:
123 ; O1-NEXT: [[PHI_CMP:%.*]] = icmp ne i64 [[INC_I]], 0
124 ; O1-NEXT: br label [[COUNT_NODES_VARIANT3_EXIT]]
125 ; O1: count_nodes_variant3.exit:
126 ; O1-NEXT: [[SIZE_0_LCSSA_I:%.*]] = phi i1 [ false, [[ENTRY]] ], [ [[PHI_CMP]], [[COUNT_NODES_VARIANT3_EXIT_LOOPEXIT]] ]
127 ; O1-NEXT: ret i1 [[SIZE_0_LCSSA_I]]
130 %p.addr = alloca ptr, align 8
131 store ptr %p, ptr %p.addr, align 8
132 %0 = load ptr, ptr %p.addr, align 8
133 %call = call i64 @count_nodes_variant3(ptr %0)
134 %cmp = icmp ugt i64 %call, 0
138 define internal i64 @count_nodes_variant3(ptr %p) {
140 %p.addr = alloca ptr, align 8
141 %size = alloca i64, align 8
142 store ptr %p, ptr %p.addr, align 8
143 store i64 0, ptr %size, align 8
147 %0 = load ptr, ptr %p.addr, align 8
148 %tobool = icmp ne ptr %0, null
149 br i1 %tobool, label %while.body, label %while.end
152 %1 = load i64, ptr %size, align 8
153 %cmp = icmp ne i64 %1, -1
154 call void @_ZL6assumeb(i1 zeroext %cmp)
155 %2 = load ptr, ptr %p.addr, align 8
156 %3 = load ptr, ptr %2, align 8
157 store ptr %3, ptr %p.addr, align 8
158 %4 = load i64, ptr %size, align 8
160 store i64 %inc, ptr %size, align 8
161 br label %while.cond, !llvm.loop !3
164 %5 = load i64, ptr %size, align 8
168 define internal void @_ZL6assumeb(i1 zeroext %expression) {
170 %expression.addr = alloca i8, align 1
171 %frombool = zext i1 %expression to i8
172 store i8 %frombool, ptr %expression.addr, align 1
173 %0 = load i8, ptr %expression.addr, align 1
174 %tobool = trunc i8 %0 to i1
175 call void @llvm.assume(i1 %tobool)
179 declare void @llvm.assume(i1 noundef)
181 !0 = distinct !{!0, !1}
182 !1 = !{!"llvm.loop.mustprogress"}
183 !2 = distinct !{!2, !1}
184 !3 = distinct !{!3, !1}