1 ; RUN: opt < %s -analyze -branch-prob -enable-new-pm=0 | FileCheck %s
2 ; RUN: opt < %s -analyze -lazy-branch-prob -enable-new-pm=0 | FileCheck %s
3 ; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
5 declare i32 @strcmp(i8*, i8*)
6 declare i32 @strncmp(i8*, i8*, i32)
7 declare i32 @strcasecmp(i8*, i8*)
8 declare i32 @strncasecmp(i8*, i8*, i32)
9 declare i32 @memcmp(i8*, i8*)
10 declare i32 @bcmp(i8*, i8*)
11 declare i32 @nonstrcmp(i8*, i8*)
14 ; Check that the result of strcmp is considered more likely to be nonzero than
15 ; zero, and equally likely to be (nonzero) positive or negative.
17 define i32 @test_strcmp_eq(i8* %p, i8* %q) {
18 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_eq'
20 %val = call i32 @strcmp(i8* %p, i8* %q)
21 %cond = icmp eq i32 %val, 0
22 br i1 %cond, label %then, label %else
23 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
24 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
28 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
32 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
35 %result = phi i32 [ 0, %then ], [ 1, %else ]
39 define i32 @test_strcmp_eq5(i8* %p, i8* %q) {
40 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_eq5'
42 %val = call i32 @strcmp(i8* %p, i8* %q)
43 %cond = icmp eq i32 %val, 5
44 br i1 %cond, label %then, label %else
45 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
46 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
50 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
54 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
57 %result = phi i32 [ 0, %then ], [ 1, %else ]
61 define i32 @test_strcmp_ne(i8* %p, i8* %q) {
62 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_ne'
64 %val = call i32 @strcmp(i8* %p, i8* %q)
65 %cond = icmp ne i32 %val, 0
66 br i1 %cond, label %then, label %else
67 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
68 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
72 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
76 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
79 %result = phi i32 [ 0, %then ], [ 1, %else ]
83 define i32 @test_strcmp_sgt(i8* %p, i8* %q) {
84 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_sgt'
86 %val = call i32 @strcmp(i8* %p, i8* %q)
87 %cond = icmp sgt i32 %val, 0
88 br i1 %cond, label %then, label %else
89 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
90 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
94 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
98 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
101 %result = phi i32 [ 0, %then ], [ 1, %else ]
105 define i32 @test_strcmp_slt(i8* %p, i8* %q) {
106 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_slt'
108 %val = call i32 @strcmp(i8* %p, i8* %q)
109 %cond = icmp slt i32 %val, 0
110 br i1 %cond, label %then, label %else
111 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
112 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
116 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
120 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
123 %result = phi i32 [ 0, %then ], [ 1, %else ]
128 ; Similarly check other library functions that have the same behaviour
130 define i32 @test_strncmp_sgt(i8* %p, i8* %q) {
131 ; CHECK: Printing analysis {{.*}} for function 'test_strncmp_sgt'
133 %val = call i32 @strncmp(i8* %p, i8* %q, i32 4)
134 %cond = icmp sgt i32 %val, 0
135 br i1 %cond, label %then, label %else
136 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
137 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
141 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
145 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
148 %result = phi i32 [ 0, %then ], [ 1, %else ]
152 define i32 @test_strcasecmp_sgt(i8* %p, i8* %q) {
153 ; CHECK: Printing analysis {{.*}} for function 'test_strcasecmp_sgt'
155 %val = call i32 @strcasecmp(i8* %p, i8* %q)
156 %cond = icmp sgt i32 %val, 0
157 br i1 %cond, label %then, label %else
158 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
159 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
163 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
167 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
170 %result = phi i32 [ 0, %then ], [ 1, %else ]
174 define i32 @test_strncasecmp_sgt(i8* %p, i8* %q) {
175 ; CHECK: Printing analysis {{.*}} for function 'test_strncasecmp_sgt'
177 %val = call i32 @strncasecmp(i8* %p, i8* %q, i32 4)
178 %cond = icmp sgt i32 %val, 0
179 br i1 %cond, label %then, label %else
180 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
181 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
185 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
189 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
192 %result = phi i32 [ 0, %then ], [ 1, %else ]
196 define i32 @test_memcmp_sgt(i8* %p, i8* %q) {
197 ; CHECK: Printing analysis {{.*}} for function 'test_memcmp_sgt'
199 %val = call i32 @memcmp(i8* %p, i8* %q)
200 %cond = icmp sgt i32 %val, 0
201 br i1 %cond, label %then, label %else
202 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
203 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
207 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
211 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
214 %result = phi i32 [ 0, %then ], [ 1, %else ]
219 ; Check that for the result of a call to a non-library function the default
220 ; heuristic is applied, i.e. positive more likely than negative, nonzero more
223 define i32 @test_nonstrcmp_eq(i8* %p, i8* %q) {
224 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_eq'
226 %val = call i32 @nonstrcmp(i8* %p, i8* %q)
227 %cond = icmp eq i32 %val, 0
228 br i1 %cond, label %then, label %else
229 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
230 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
234 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
238 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
241 %result = phi i32 [ 0, %then ], [ 1, %else ]
245 define i32 @test_nonstrcmp_ne(i8* %p, i8* %q) {
246 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_ne'
248 %val = call i32 @nonstrcmp(i8* %p, i8* %q)
249 %cond = icmp ne i32 %val, 0
250 br i1 %cond, label %then, label %else
251 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
252 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
256 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
260 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
263 %result = phi i32 [ 0, %then ], [ 1, %else ]
267 define i32 @test_nonstrcmp_sgt(i8* %p, i8* %q) {
268 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_sgt'
270 %val = call i32 @nonstrcmp(i8* %p, i8* %q)
271 %cond = icmp sgt i32 %val, 0
272 br i1 %cond, label %then, label %else
273 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
274 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
278 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
282 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
285 %result = phi i32 [ 0, %then ], [ 1, %else ]
290 define i32 @test_bcmp_eq(i8* %p, i8* %q) {
291 ; CHECK: Printing analysis {{.*}} for function 'test_bcmp_eq'
293 %val = call i32 @bcmp(i8* %p, i8* %q)
294 %cond = icmp eq i32 %val, 0
295 br i1 %cond, label %then, label %else
296 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
297 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
301 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
305 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
308 %result = phi i32 [ 0, %then ], [ 1, %else ]
312 define i32 @test_bcmp_eq5(i8* %p, i8* %q) {
313 ; CHECK: Printing analysis {{.*}} for function 'test_bcmp_eq5'
315 %val = call i32 @bcmp(i8* %p, i8* %q)
316 %cond = icmp eq i32 %val, 5
317 br i1 %cond, label %then, label %else
318 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
319 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
323 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
327 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
330 %result = phi i32 [ 0, %then ], [ 1, %else ]
336 define i32 @test_bcmp_ne(i8* %p, i8* %q) {
337 ; CHECK: Printing analysis {{.*}} for function 'test_bcmp_ne'
339 %val = call i32 @bcmp(i8* %p, i8* %q)
340 %cond = icmp ne i32 %val, 0
341 br i1 %cond, label %then, label %else
342 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
343 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
347 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
351 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
354 %result = phi i32 [ 0, %then ], [ 1, %else ]