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 4 5declare i32 @strcmp(i8*, i8*) 6declare i32 @strncmp(i8*, i8*, i32) 7declare i32 @strcasecmp(i8*, i8*) 8declare i32 @strncasecmp(i8*, i8*, i32) 9declare i32 @memcmp(i8*, i8*) 10declare i32 @bcmp(i8*, i8*) 11declare i32 @nonstrcmp(i8*, i8*) 12 13 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. 16 17define i32 @test_strcmp_eq(i8* %p, i8* %q) { 18; CHECK: Printing analysis {{.*}} for function 'test_strcmp_eq' 19entry: 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% 25 26then: 27 br label %exit 28; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 29 30else: 31 br label %exit 32; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 33 34exit: 35 %result = phi i32 [ 0, %then ], [ 1, %else ] 36 ret i32 %result 37} 38 39define i32 @test_strcmp_eq5(i8* %p, i8* %q) { 40; CHECK: Printing analysis {{.*}} for function 'test_strcmp_eq5' 41entry: 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% 47 48then: 49 br label %exit 50; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 51 52else: 53 br label %exit 54; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 55 56exit: 57 %result = phi i32 [ 0, %then ], [ 1, %else ] 58 ret i32 %result 59} 60 61define i32 @test_strcmp_ne(i8* %p, i8* %q) { 62; CHECK: Printing analysis {{.*}} for function 'test_strcmp_ne' 63entry: 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% 69 70then: 71 br label %exit 72; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 73 74else: 75 br label %exit 76; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 77 78exit: 79 %result = phi i32 [ 0, %then ], [ 1, %else ] 80 ret i32 %result 81} 82 83define i32 @test_strcmp_sgt(i8* %p, i8* %q) { 84; CHECK: Printing analysis {{.*}} for function 'test_strcmp_sgt' 85entry: 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% 91 92then: 93 br label %exit 94; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 95 96else: 97 br label %exit 98; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 99 100exit: 101 %result = phi i32 [ 0, %then ], [ 1, %else ] 102 ret i32 %result 103} 104 105define i32 @test_strcmp_slt(i8* %p, i8* %q) { 106; CHECK: Printing analysis {{.*}} for function 'test_strcmp_slt' 107entry: 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% 113 114then: 115 br label %exit 116; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 117 118else: 119 br label %exit 120; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 121 122exit: 123 %result = phi i32 [ 0, %then ], [ 1, %else ] 124 ret i32 %result 125} 126 127 128; Similarly check other library functions that have the same behaviour 129 130define i32 @test_strncmp_sgt(i8* %p, i8* %q) { 131; CHECK: Printing analysis {{.*}} for function 'test_strncmp_sgt' 132entry: 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% 138 139then: 140 br label %exit 141; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 142 143else: 144 br label %exit 145; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 146 147exit: 148 %result = phi i32 [ 0, %then ], [ 1, %else ] 149 ret i32 %result 150} 151 152define i32 @test_strcasecmp_sgt(i8* %p, i8* %q) { 153; CHECK: Printing analysis {{.*}} for function 'test_strcasecmp_sgt' 154entry: 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% 160 161then: 162 br label %exit 163; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 164 165else: 166 br label %exit 167; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 168 169exit: 170 %result = phi i32 [ 0, %then ], [ 1, %else ] 171 ret i32 %result 172} 173 174define i32 @test_strncasecmp_sgt(i8* %p, i8* %q) { 175; CHECK: Printing analysis {{.*}} for function 'test_strncasecmp_sgt' 176entry: 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% 182 183then: 184 br label %exit 185; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 186 187else: 188 br label %exit 189; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 190 191exit: 192 %result = phi i32 [ 0, %then ], [ 1, %else ] 193 ret i32 %result 194} 195 196define i32 @test_memcmp_sgt(i8* %p, i8* %q) { 197; CHECK: Printing analysis {{.*}} for function 'test_memcmp_sgt' 198entry: 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% 204 205then: 206 br label %exit 207; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 208 209else: 210 br label %exit 211; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 212 213exit: 214 %result = phi i32 [ 0, %then ], [ 1, %else ] 215 ret i32 %result 216} 217 218 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 221; likely than zero. 222 223define i32 @test_nonstrcmp_eq(i8* %p, i8* %q) { 224; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_eq' 225entry: 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% 231 232then: 233 br label %exit 234; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 235 236else: 237 br label %exit 238; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 239 240exit: 241 %result = phi i32 [ 0, %then ], [ 1, %else ] 242 ret i32 %result 243} 244 245define i32 @test_nonstrcmp_ne(i8* %p, i8* %q) { 246; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_ne' 247entry: 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% 253 254then: 255 br label %exit 256; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 257 258else: 259 br label %exit 260; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 261 262exit: 263 %result = phi i32 [ 0, %then ], [ 1, %else ] 264 ret i32 %result 265} 266 267define i32 @test_nonstrcmp_sgt(i8* %p, i8* %q) { 268; CHECK: Printing analysis {{.*}} for function 'test_nonstrcmp_sgt' 269entry: 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% 275 276then: 277 br label %exit 278; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 279 280else: 281 br label %exit 282; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 283 284exit: 285 %result = phi i32 [ 0, %then ], [ 1, %else ] 286 ret i32 %result 287} 288 289 290define i32 @test_bcmp_eq(i8* %p, i8* %q) { 291; CHECK: Printing analysis {{.*}} for function 'test_bcmp_eq' 292entry: 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% 298 299then: 300 br label %exit 301; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 302 303else: 304 br label %exit 305; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 306 307exit: 308 %result = phi i32 [ 0, %then ], [ 1, %else ] 309 ret i32 %result 310} 311 312define i32 @test_bcmp_eq5(i8* %p, i8* %q) { 313; CHECK: Printing analysis {{.*}} for function 'test_bcmp_eq5' 314entry: 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% 320 321then: 322 br label %exit 323; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 324 325else: 326 br label %exit 327; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 328 329exit: 330 %result = phi i32 [ 0, %then ], [ 1, %else ] 331 ret i32 %result 332} 333 334 335 336define i32 @test_bcmp_ne(i8* %p, i8* %q) { 337; CHECK: Printing analysis {{.*}} for function 'test_bcmp_ne' 338entry: 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% 344 345then: 346 br label %exit 347; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 348 349else: 350 br label %exit 351; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] 352 353exit: 354 %result = phi i32 [ 0, %then ], [ 1, %else ] 355 ret i32 %result 356} 357