1 ; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
3 declare i32 @strcmp(ptr, ptr)
4 declare i32 @strncmp(ptr, ptr, i32)
5 declare i32 @strcasecmp(ptr, ptr)
6 declare i32 @strncasecmp(ptr, ptr, i32)
7 declare i32 @memcmp(ptr, ptr)
8 declare i32 @bcmp(ptr, ptr)
9 declare i32 @nonstrcmp(ptr, ptr)
12 ; Check that the result of strcmp is considered more likely to be nonzero than
13 ; zero, and equally likely to be (nonzero) positive or negative.
15 define i32 @test_strcmp_eq(ptr %p, ptr %q) {
16 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_eq'
18 %val = call i32 @strcmp(ptr %p, ptr %q)
19 %cond = icmp eq i32 %val, 0
20 br i1 %cond, label %then, label %else
21 ; CHECK: edge %entry -> %then probability is 0x30000000 / 0x80000000 = 37.50%
22 ; CHECK: edge %entry -> %else probability is 0x50000000 / 0x80000000 = 62.50%
26 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
30 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
33 %result = phi i32 [ 0, %then ], [ 1, %else ]
37 define i32 @test_strcmp_eq5(ptr %p, ptr %q) {
38 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_eq5'
40 %val = call i32 @strcmp(ptr %p, ptr %q)
41 %cond = icmp eq i32 %val, 5
42 br i1 %cond, label %then, label %else
43 ; CHECK: edge %entry -> %then probability is 0x30000000 / 0x80000000 = 37.50%
44 ; CHECK: edge %entry -> %else probability is 0x50000000 / 0x80000000 = 62.50%
48 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
52 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
55 %result = phi i32 [ 0, %then ], [ 1, %else ]
59 define i32 @test_strcmp_ne(ptr %p, ptr %q) {
60 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_ne'
62 %val = call i32 @strcmp(ptr %p, ptr %q)
63 %cond = icmp ne i32 %val, 0
64 br i1 %cond, label %then, label %else
65 ; CHECK: edge %entry -> %then probability is 0x50000000 / 0x80000000 = 62.50%
66 ; CHECK: edge %entry -> %else probability is 0x30000000 / 0x80000000 = 37.50%
70 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
74 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
77 %result = phi i32 [ 0, %then ], [ 1, %else ]
81 define i32 @test_strcmp_sgt(ptr %p, ptr %q) {
82 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_sgt'
84 %val = call i32 @strcmp(ptr %p, ptr %q)
85 %cond = icmp sgt i32 %val, 0
86 br i1 %cond, label %then, label %else
87 ; CHECK: edge %entry -> %then probability is 0x40000000 / 0x80000000 = 50.00%
88 ; CHECK: edge %entry -> %else probability is 0x40000000 / 0x80000000 = 50.00%
92 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
96 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
99 %result = phi i32 [ 0, %then ], [ 1, %else ]
103 define i32 @test_strcmp_slt(ptr %p, ptr %q) {
104 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_slt'
106 %val = call i32 @strcmp(ptr %p, ptr %q)
107 %cond = icmp slt i32 %val, 0
108 br i1 %cond, label %then, label %else
109 ; CHECK: edge %entry -> %then probability is 0x40000000 / 0x80000000 = 50.00%
110 ; CHECK: edge %entry -> %else probability is 0x40000000 / 0x80000000 = 50.00%
114 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
118 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
121 %result = phi i32 [ 0, %then ], [ 1, %else ]
126 ; Similarly check other library functions that have the same behaviour
128 define i32 @test_strncmp_sgt(ptr %p, ptr %q) {
129 ; CHECK: Printing analysis {{.*}} for function 'test_strncmp_sgt'
131 %val = call i32 @strncmp(ptr %p, ptr %q, i32 4)
132 %cond = icmp sgt i32 %val, 0
133 br i1 %cond, label %then, label %else
134 ; CHECK: edge %entry -> %then probability is 0x40000000 / 0x80000000 = 50.00%
135 ; CHECK: edge %entry -> %else probability is 0x40000000 / 0x80000000 = 50.00%
139 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
143 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
146 %result = phi i32 [ 0, %then ], [ 1, %else ]
150 define i32 @test_strcasecmp_sgt(ptr %p, ptr %q) {
151 ; CHECK: Printing analysis {{.*}} for function 'test_strcasecmp_sgt'
153 %val = call i32 @strcasecmp(ptr %p, ptr %q)
154 %cond = icmp sgt i32 %val, 0
155 br i1 %cond, label %then, label %else
156 ; CHECK: edge %entry -> %then probability is 0x40000000 / 0x80000000 = 50.00%
157 ; CHECK: edge %entry -> %else probability is 0x40000000 / 0x80000000 = 50.00%
161 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
165 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
168 %result = phi i32 [ 0, %then ], [ 1, %else ]
172 define i32 @test_strncasecmp_sgt(ptr %p, ptr %q) {
173 ; CHECK: Printing analysis {{.*}} for function 'test_strncasecmp_sgt'
175 %val = call i32 @strncasecmp(ptr %p, ptr %q, i32 4)
176 %cond = icmp sgt i32 %val, 0
177 br i1 %cond, label %then, label %else
178 ; CHECK: edge %entry -> %then probability is 0x40000000 / 0x80000000 = 50.00%
179 ; CHECK: edge %entry -> %else probability is 0x40000000 / 0x80000000 = 50.00%
183 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
187 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
190 %result = phi i32 [ 0, %then ], [ 1, %else ]
194 define i32 @test_memcmp_sgt(ptr %p, ptr %q) {
195 ; CHECK: Printing analysis {{.*}} for function 'test_memcmp_sgt'
197 %val = call i32 @memcmp(ptr %p, ptr %q)
198 %cond = icmp sgt i32 %val, 0
199 br i1 %cond, label %then, label %else
200 ; CHECK: edge %entry -> %then probability is 0x40000000 / 0x80000000 = 50.00%
201 ; CHECK: edge %entry -> %else probability is 0x40000000 / 0x80000000 = 50.00%
205 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
209 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
212 %result = phi i32 [ 0, %then ], [ 1, %else ]
217 ; Check that for the result of a call to a non-library function the default
218 ; heuristic is applied, i.e. positive more likely than negative, nonzero more
221 define i32 @test_nonstrcmp_eq(ptr %p, ptr %q) {
222 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_eq'
224 %val = call i32 @nonstrcmp(ptr %p, ptr %q)
225 %cond = icmp eq i32 %val, 0
226 br i1 %cond, label %then, label %else
227 ; CHECK: edge %entry -> %then probability is 0x30000000 / 0x80000000 = 37.50%
228 ; CHECK: edge %entry -> %else probability is 0x50000000 / 0x80000000 = 62.50%
232 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
236 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
239 %result = phi i32 [ 0, %then ], [ 1, %else ]
243 define i32 @test_nonstrcmp_ne(ptr %p, ptr %q) {
244 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_ne'
246 %val = call i32 @nonstrcmp(ptr %p, ptr %q)
247 %cond = icmp ne i32 %val, 0
248 br i1 %cond, label %then, label %else
249 ; CHECK: edge %entry -> %then probability is 0x50000000 / 0x80000000 = 62.50%
250 ; CHECK: edge %entry -> %else probability is 0x30000000 / 0x80000000 = 37.50%
254 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
258 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
261 %result = phi i32 [ 0, %then ], [ 1, %else ]
265 define i32 @test_nonstrcmp_sgt(ptr %p, ptr %q) {
266 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_sgt'
268 %val = call i32 @nonstrcmp(ptr %p, ptr %q)
269 %cond = icmp sgt i32 %val, 0
270 br i1 %cond, label %then, label %else
271 ; CHECK: edge %entry -> %then probability is 0x50000000 / 0x80000000 = 62.50%
272 ; CHECK: edge %entry -> %else probability is 0x30000000 / 0x80000000 = 37.50%
276 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
280 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
283 %result = phi i32 [ 0, %then ], [ 1, %else ]
288 define i32 @test_bcmp_eq(ptr %p, ptr %q) {
289 ; CHECK: Printing analysis {{.*}} for function 'test_bcmp_eq'
291 %val = call i32 @bcmp(ptr %p, ptr %q)
292 %cond = icmp eq i32 %val, 0
293 br i1 %cond, label %then, label %else
294 ; CHECK: edge %entry -> %then probability is 0x30000000 / 0x80000000 = 37.50%
295 ; CHECK: edge %entry -> %else probability is 0x50000000 / 0x80000000 = 62.50%
299 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
303 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
306 %result = phi i32 [ 0, %then ], [ 1, %else ]
310 define i32 @test_bcmp_eq5(ptr %p, ptr %q) {
311 ; CHECK: Printing analysis {{.*}} for function 'test_bcmp_eq5'
313 %val = call i32 @bcmp(ptr %p, ptr %q)
314 %cond = icmp eq i32 %val, 5
315 br i1 %cond, label %then, label %else
316 ; CHECK: edge %entry -> %then probability is 0x30000000 / 0x80000000 = 37.50%
317 ; CHECK: edge %entry -> %else probability is 0x50000000 / 0x80000000 = 62.50%
321 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
325 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
328 %result = phi i32 [ 0, %then ], [ 1, %else ]
334 define i32 @test_bcmp_ne(ptr %p, ptr %q) {
335 ; CHECK: Printing analysis {{.*}} for function 'test_bcmp_ne'
337 %val = call i32 @bcmp(ptr %p, ptr %q)
338 %cond = icmp ne i32 %val, 0
339 br i1 %cond, label %then, label %else
340 ; CHECK: edge %entry -> %then probability is 0x50000000 / 0x80000000 = 62.50%
341 ; CHECK: edge %entry -> %else probability is 0x30000000 / 0x80000000 = 37.50%
345 ; CHECK: edge %then -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
349 ; CHECK: edge %else -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
352 %result = phi i32 [ 0, %then ], [ 1, %else ]