1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2 ; RUN: opt -S -passes=guard-widening < %s | FileCheck %s
4 ; Function Attrs: nocallback nofree nosync willreturn
5 declare void @llvm.experimental.guard(i1, ...) #0
7 ; Hot loop, frequently entered, should widen.
8 define i32 @test_intrinsic_very_profitable(i32 %n, i1 %cond.1, i1 %cond.2) {
9 ; CHECK-LABEL: define i32 @test_intrinsic_very_profitable
10 ; CHECK-SAME: (i32 [[N:%.*]], i1 [[COND_1:%.*]], i1 [[COND_2:%.*]]) {
12 ; CHECK-NEXT: [[COND_2_GW_FR:%.*]] = freeze i1 [[COND_2]]
13 ; CHECK-NEXT: [[WIDE_CHK:%.*]] = and i1 [[COND_1]], [[COND_2_GW_FR]]
14 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
15 ; CHECK-NEXT: [[LOOP_PRECONDITION:%.*]] = icmp uge i32 [[N]], 100
16 ; CHECK-NEXT: br i1 [[LOOP_PRECONDITION]], label [[LOOP:%.*]], label [[FAILED:%.*]], !prof [[PROF0:![0-9]+]]
18 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
19 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
20 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 100
21 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]], !prof [[PROF1:![0-9]+]]
23 ; CHECK-NEXT: ret i32 0
25 ; CHECK-NEXT: ret i32 -1
28 call void (i1, ...) @llvm.experimental.guard(i1 %cond.1) [ "deopt"() ]
29 %loop.precondition = icmp uge i32 %n, 100
30 br i1 %loop.precondition, label %loop, label %failed, !prof !0
32 loop: ; preds = %loop, %entry
33 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
34 call void (i1, ...) @llvm.experimental.guard(i1 %cond.2) [ "deopt"() ]
35 %iv.next = add nuw nsw i32 %iv, 1
36 %loop.cond = icmp ult i32 %iv.next, 100
37 br i1 %loop.cond, label %loop, label %exit, !prof !1
42 failed: ; preds = %entry
46 ; Even though the loop is rarely entered, it has so many iterations that the widening
47 ; is still profitable.
48 define i32 @test_intrinsic_profitable(i32 %n, i1 %cond.1, i1 %cond.2) {
49 ; CHECK-LABEL: define i32 @test_intrinsic_profitable
50 ; CHECK-SAME: (i32 [[N:%.*]], i1 [[COND_1:%.*]], i1 [[COND_2:%.*]]) {
52 ; CHECK-NEXT: [[COND_2_GW_FR:%.*]] = freeze i1 [[COND_2]]
53 ; CHECK-NEXT: [[WIDE_CHK:%.*]] = and i1 [[COND_1]], [[COND_2_GW_FR]]
54 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
55 ; CHECK-NEXT: [[LOOP_PRECONDITION:%.*]] = icmp uge i32 [[N]], 100
56 ; CHECK-NEXT: br i1 [[LOOP_PRECONDITION]], label [[LOOP:%.*]], label [[FAILED:%.*]], !prof [[PROF2:![0-9]+]]
58 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
59 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
60 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 100
61 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]], !prof [[PROF1]]
63 ; CHECK-NEXT: ret i32 0
65 ; CHECK-NEXT: ret i32 -1
68 call void (i1, ...) @llvm.experimental.guard(i1 %cond.1) [ "deopt"() ]
69 %loop.precondition = icmp uge i32 %n, 100
70 br i1 %loop.precondition, label %loop, label %failed, !prof !2
72 loop: ; preds = %loop, %entry
73 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
74 call void (i1, ...) @llvm.experimental.guard(i1 %cond.2) [ "deopt"() ]
75 %iv.next = add nuw nsw i32 %iv, 1
76 %loop.cond = icmp ult i32 %iv.next, 100
77 br i1 %loop.cond, label %loop, label %exit, !prof !1
82 failed: ; preds = %entry
86 ; Loop's hotness compensates rareness of its entrance. We still want to widen, because
87 ; it may open up some optimization opportunities.
88 define i32 @test_intrinsic_neutral(i32 %n, i1 %cond.1, i1 %cond.2) {
89 ; CHECK-LABEL: define i32 @test_intrinsic_neutral
90 ; CHECK-SAME: (i32 [[N:%.*]], i1 [[COND_1:%.*]], i1 [[COND_2:%.*]]) {
92 ; CHECK-NEXT: [[COND_2_GW_FR:%.*]] = freeze i1 [[COND_2]]
93 ; CHECK-NEXT: [[WIDE_CHK:%.*]] = and i1 [[COND_1]], [[COND_2_GW_FR]]
94 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
95 ; CHECK-NEXT: [[LOOP_PRECONDITION:%.*]] = icmp uge i32 [[N]], 100
96 ; CHECK-NEXT: br i1 [[LOOP_PRECONDITION]], label [[LOOP:%.*]], label [[FAILED:%.*]], !prof [[PROF3:![0-9]+]]
98 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
99 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
100 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 100
101 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]], !prof [[PROF1]]
103 ; CHECK-NEXT: ret i32 0
105 ; CHECK-NEXT: ret i32 -1
108 call void (i1, ...) @llvm.experimental.guard(i1 %cond.1) [ "deopt"() ]
109 %loop.precondition = icmp uge i32 %n, 100
110 br i1 %loop.precondition, label %loop, label %failed, !prof !3
112 loop: ; preds = %loop, %entry
113 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
114 call void (i1, ...) @llvm.experimental.guard(i1 %cond.2) [ "deopt"() ]
115 %iv.next = add nuw nsw i32 %iv, 1
116 %loop.cond = icmp ult i32 %iv.next, 100
117 br i1 %loop.cond, label %loop, label %exit, !prof !1
119 exit: ; preds = %loop
122 failed: ; preds = %entry
126 ; FIXME: This loop is so rarely entered, that we don't want to widen here.
127 define i32 @test_intrinsic_very_unprofitable(i32 %n, i1 %cond.1, i1 %cond.2) {
128 ; CHECK-LABEL: define i32 @test_intrinsic_very_unprofitable
129 ; CHECK-SAME: (i32 [[N:%.*]], i1 [[COND_1:%.*]], i1 [[COND_2:%.*]]) {
131 ; CHECK-NEXT: [[COND_2_GW_FR:%.*]] = freeze i1 [[COND_2]]
132 ; CHECK-NEXT: [[WIDE_CHK:%.*]] = and i1 [[COND_1]], [[COND_2_GW_FR]]
133 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
134 ; CHECK-NEXT: [[LOOP_PRECONDITION:%.*]] = icmp uge i32 [[N]], 100
135 ; CHECK-NEXT: br i1 [[LOOP_PRECONDITION]], label [[LOOP:%.*]], label [[FAILED:%.*]], !prof [[PROF4:![0-9]+]]
137 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
138 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
139 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 100
140 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]], !prof [[PROF1]]
142 ; CHECK-NEXT: ret i32 0
144 ; CHECK-NEXT: ret i32 -1
147 call void (i1, ...) @llvm.experimental.guard(i1 %cond.1) [ "deopt"() ]
148 %loop.precondition = icmp uge i32 %n, 100
149 br i1 %loop.precondition, label %loop, label %failed, !prof !4
151 loop: ; preds = %loop, %entry
152 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
153 call void (i1, ...) @llvm.experimental.guard(i1 %cond.2) [ "deopt"() ]
154 %iv.next = add nuw nsw i32 %iv, 1
155 %loop.cond = icmp ult i32 %iv.next, 100
156 br i1 %loop.cond, label %loop, label %exit, !prof !1
158 exit: ; preds = %loop
161 failed: ; preds = %entry
165 ; FIXME: This loop is so rarely entered, that we don't want to widen here.
166 define i32 @test_intrinsic_unprofitable(i32 %n, i1 %cond.1, i1 %cond.2) {
167 ; CHECK-LABEL: define i32 @test_intrinsic_unprofitable
168 ; CHECK-SAME: (i32 [[N:%.*]], i1 [[COND_1:%.*]], i1 [[COND_2:%.*]]) {
170 ; CHECK-NEXT: [[COND_2_GW_FR:%.*]] = freeze i1 [[COND_2]]
171 ; CHECK-NEXT: [[WIDE_CHK:%.*]] = and i1 [[COND_1]], [[COND_2_GW_FR]]
172 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
173 ; CHECK-NEXT: [[LOOP_PRECONDITION:%.*]] = icmp uge i32 [[N]], 100
174 ; CHECK-NEXT: br i1 [[LOOP_PRECONDITION]], label [[LOOP:%.*]], label [[FAILED:%.*]], !prof [[PROF5:![0-9]+]]
176 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
177 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
178 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 100
179 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]], !prof [[PROF1]]
181 ; CHECK-NEXT: ret i32 0
183 ; CHECK-NEXT: ret i32 -1
186 call void (i1, ...) @llvm.experimental.guard(i1 %cond.1) [ "deopt"() ]
187 %loop.precondition = icmp uge i32 %n, 100
188 br i1 %loop.precondition, label %loop, label %failed, !prof !5
190 loop: ; preds = %loop, %entry
191 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
192 call void (i1, ...) @llvm.experimental.guard(i1 %cond.2) [ "deopt"() ]
193 %iv.next = add nuw nsw i32 %iv, 1
194 %loop.cond = icmp ult i32 %iv.next, 100
195 br i1 %loop.cond, label %loop, label %exit, !prof !1
197 exit: ; preds = %loop
200 failed: ; preds = %entry
204 attributes #0 = { nocallback nofree nosync willreturn }
206 !0 = !{!"branch_weights", i32 1048576, i32 1}
207 !1 = !{!"branch_weights", i32 99, i32 1}
208 !2 = !{!"branch_weights", i32 1, i32 10}
209 !3 = !{!"branch_weights", i32 1, i32 99}
210 !4 = !{!"branch_weights", i32 1, i32 1048576}
211 !5 = !{!"branch_weights", i32 1, i32 1000}