1 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s
2 ; RUN: opt < %s -analyze -lazy-branch-prob | 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 @nonstrcmp(i8*, i8*)
13 ; Check that the result of strcmp is considered more likely to be nonzero than
14 ; zero, and equally likely to be (nonzero) positive or negative.
16 define i32 @test_strcmp_eq(i8* %p, i8* %q) {
17 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_eq'
19 %val = call i32 @strcmp(i8* %p, i8* %q)
20 %cond = icmp eq i32 %val, 0
21 br i1 %cond, label %then, label %else
22 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
23 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
27 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
31 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
34 %result = phi i32 [ 0, %then ], [ 1, %else ]
38 define i32 @test_strcmp_ne(i8* %p, i8* %q) {
39 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_ne'
41 %val = call i32 @strcmp(i8* %p, i8* %q)
42 %cond = icmp ne i32 %val, 0
43 br i1 %cond, label %then, label %else
44 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
45 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
49 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
53 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
56 %result = phi i32 [ 0, %then ], [ 1, %else ]
60 define i32 @test_strcmp_sgt(i8* %p, i8* %q) {
61 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_sgt'
63 %val = call i32 @strcmp(i8* %p, i8* %q)
64 %cond = icmp sgt i32 %val, 0
65 br i1 %cond, label %then, label %else
66 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
67 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
71 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
75 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
78 %result = phi i32 [ 0, %then ], [ 1, %else ]
82 define i32 @test_strcmp_slt(i8* %p, i8* %q) {
83 ; CHECK: Printing analysis {{.*}} for function 'test_strcmp_slt'
85 %val = call i32 @strcmp(i8* %p, i8* %q)
86 %cond = icmp slt i32 %val, 0
87 br i1 %cond, label %then, label %else
88 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
89 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
93 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
97 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
100 %result = phi i32 [ 0, %then ], [ 1, %else ]
105 ; Similarly check other library functions that have the same behaviour
107 define i32 @test_strncmp_sgt(i8* %p, i8* %q) {
108 ; CHECK: Printing analysis {{.*}} for function 'test_strncmp_sgt'
110 %val = call i32 @strncmp(i8* %p, i8* %q, i32 4)
111 %cond = icmp sgt i32 %val, 0
112 br i1 %cond, label %then, label %else
113 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
114 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
118 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
122 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
125 %result = phi i32 [ 0, %then ], [ 1, %else ]
129 define i32 @test_strcasecmp_sgt(i8* %p, i8* %q) {
130 ; CHECK: Printing analysis {{.*}} for function 'test_strcasecmp_sgt'
132 %val = call i32 @strcasecmp(i8* %p, i8* %q)
133 %cond = icmp sgt i32 %val, 0
134 br i1 %cond, label %then, label %else
135 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
136 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
140 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
144 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
147 %result = phi i32 [ 0, %then ], [ 1, %else ]
151 define i32 @test_strncasecmp_sgt(i8* %p, i8* %q) {
152 ; CHECK: Printing analysis {{.*}} for function 'test_strncasecmp_sgt'
154 %val = call i32 @strncasecmp(i8* %p, i8* %q, i32 4)
155 %cond = icmp sgt i32 %val, 0
156 br i1 %cond, label %then, label %else
157 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
158 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
162 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
166 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
169 %result = phi i32 [ 0, %then ], [ 1, %else ]
173 define i32 @test_memcmp_sgt(i8* %p, i8* %q) {
174 ; CHECK: Printing analysis {{.*}} for function 'test_memcmp_sgt'
176 %val = call i32 @memcmp(i8* %p, i8* %q)
177 %cond = icmp sgt i32 %val, 0
178 br i1 %cond, label %then, label %else
179 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
180 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
184 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
188 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
191 %result = phi i32 [ 0, %then ], [ 1, %else ]
196 ; Check that for the result of a call to a non-library function the default
197 ; heuristic is applied, i.e. positive more likely than negative, nonzero more
200 define i32 @test_nonstrcmp_eq(i8* %p, i8* %q) {
201 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_eq'
203 %val = call i32 @nonstrcmp(i8* %p, i8* %q)
204 %cond = icmp eq i32 %val, 0
205 br i1 %cond, label %then, label %else
206 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
207 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
211 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
215 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
218 %result = phi i32 [ 0, %then ], [ 1, %else ]
222 define i32 @test_nonstrcmp_ne(i8* %p, i8* %q) {
223 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_ne'
225 %val = call i32 @nonstrcmp(i8* %p, i8* %q)
226 %cond = icmp ne i32 %val, 0
227 br i1 %cond, label %then, label %else
228 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
229 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
233 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
237 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
240 %result = phi i32 [ 0, %then ], [ 1, %else ]
244 define i32 @test_nonstrcmp_sgt(i8* %p, i8* %q) {
245 ; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_sgt'
247 %val = call i32 @nonstrcmp(i8* %p, i8* %q)
248 %cond = icmp sgt i32 %val, 0
249 br i1 %cond, label %then, label %else
250 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
251 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
255 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
259 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
262 %result = phi i32 [ 0, %then ], [ 1, %else ]