1; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py 2; RUN: opt -analyze -enable-new-pm=0 -scalar-evolution -S -scalar-evolution-classify-expressions=0 < %s | FileCheck %s 3; RUN: opt -disable-output "-passes=print<scalar-evolution>" -S -scalar-evolution-classify-expressions=0 < %s 2>&1 | FileCheck %s 4 5; Every combination of 6; - starting at 0, 1, or %x 7; - steping by 1 or 2 8; - stopping at %n or %n*2 9; - using nsw, or not 10 11; Some of these represent missed opportunities. 12 13define void @foo(i4 %n) { 14; CHECK-LABEL: 'foo' 15; CHECK-NEXT: Determining loop execution counts for: @foo 16; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + %n) 17; CHECK-NEXT: Loop %loop: max backedge-taken count is 6 18; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %n) 19; CHECK-NEXT: Predicates: 20; CHECK: Loop %loop: Trip multiple is 1 21; 22entry: 23 %s = icmp sgt i4 %n, 0 24 br i1 %s, label %loop, label %exit 25loop: 26 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ] 27 %i.next = add i4 %i, 1 28 %t = icmp slt i4 %i.next, %n 29 br i1 %t, label %loop, label %exit 30exit: 31 ret void 32} 33 34define void @step2(i4 %n) { 35; CHECK-LABEL: 'step2' 36; CHECK-NEXT: Determining loop execution counts for: @step2 37; CHECK-NEXT: Loop %loop: Unpredictable backedge-taken count. 38; CHECK-NEXT: Loop %loop: Unpredictable max backedge-taken count. 39; CHECK-NEXT: Loop %loop: Unpredictable predicated backedge-taken count. 40; 41entry: 42 %s = icmp sgt i4 %n, 0 43 br i1 %s, label %loop, label %exit 44loop: 45 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ] 46 %i.next = add i4 %i, 2 47 %t = icmp slt i4 %i.next, %n 48 br i1 %t, label %loop, label %exit 49exit: 50 ret void 51} 52 53define void @start1(i4 %n) { 54; CHECK-LABEL: 'start1' 55; CHECK-NEXT: Determining loop execution counts for: @start1 56; CHECK-NEXT: Loop %loop: backedge-taken count is (-2 + (2 smax %n))<nsw> 57; CHECK-NEXT: Loop %loop: max backedge-taken count is 5 58; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-2 + (2 smax %n))<nsw> 59; CHECK-NEXT: Predicates: 60; CHECK: Loop %loop: Trip multiple is 1 61; 62entry: 63 %s = icmp sgt i4 %n, 0 64 br i1 %s, label %loop, label %exit 65loop: 66 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ] 67 %i.next = add i4 %i, 1 68 %t = icmp slt i4 %i.next, %n 69 br i1 %t, label %loop, label %exit 70exit: 71 ret void 72} 73 74define void @start1_step2(i4 %n) { 75; CHECK-LABEL: 'start1_step2' 76; CHECK-NEXT: Determining loop execution counts for: @start1_step2 77; CHECK-NEXT: Loop %loop: Unpredictable backedge-taken count. 78; CHECK-NEXT: Loop %loop: Unpredictable max backedge-taken count. 79; CHECK-NEXT: Loop %loop: Unpredictable predicated backedge-taken count. 80; 81entry: 82 %s = icmp sgt i4 %n, 0 83 br i1 %s, label %loop, label %exit 84loop: 85 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ] 86 %i.next = add i4 %i, 2 87 %t = icmp slt i4 %i.next, %n 88 br i1 %t, label %loop, label %exit 89exit: 90 ret void 91} 92 93define void @startx(i4 %n, i4 %x) { 94; CHECK-LABEL: 'startx' 95; CHECK-NEXT: Determining loop execution counts for: @startx 96; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n)) 97; CHECK-NEXT: Loop %loop: max backedge-taken count is -1 98; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n)) 99; CHECK-NEXT: Predicates: 100; CHECK: Loop %loop: Trip multiple is 1 101; 102entry: 103 %s = icmp sgt i4 %n, 0 104 br i1 %s, label %loop, label %exit 105loop: 106 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ] 107 %i.next = add i4 %i, 1 108 %t = icmp slt i4 %i.next, %n 109 br i1 %t, label %loop, label %exit 110exit: 111 ret void 112} 113 114define void @startx_step2(i4 %n, i4 %x) { 115; CHECK-LABEL: 'startx_step2' 116; CHECK-NEXT: Determining loop execution counts for: @startx_step2 117; CHECK-NEXT: Loop %loop: Unpredictable backedge-taken count. 118; CHECK-NEXT: Loop %loop: Unpredictable max backedge-taken count. 119; CHECK-NEXT: Loop %loop: Unpredictable predicated backedge-taken count. 120; 121entry: 122 %s = icmp sgt i4 %n, 0 123 br i1 %s, label %loop, label %exit 124loop: 125 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ] 126 %i.next = add i4 %i, 2 127 %t = icmp slt i4 %i.next, %n 128 br i1 %t, label %loop, label %exit 129exit: 130 ret void 131} 132 133define void @nsw(i4 %n) { 134; CHECK-LABEL: 'nsw' 135; CHECK-NEXT: Determining loop execution counts for: @nsw 136; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + %n) 137; CHECK-NEXT: Loop %loop: max backedge-taken count is 6 138; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %n) 139; CHECK-NEXT: Predicates: 140; CHECK: Loop %loop: Trip multiple is 1 141; 142entry: 143 %s = icmp sgt i4 %n, 0 144 br i1 %s, label %loop, label %exit 145loop: 146 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ] 147 %i.next = add nsw i4 %i, 1 148 %t = icmp slt i4 %i.next, %n 149 br i1 %t, label %loop, label %exit 150exit: 151 ret void 152} 153 154; If %n is INT4_MAX, %i.next will wrap. The nsw bit says that the 155; result is undefined. Therefore, after the loop's second iteration, 156; we are free to assume that the loop exits. This is valid because: 157; (a) %i.next is a poison value after the second iteration, which can 158; also be considered an undef value. 159; (b) the return instruction enacts a side effect that is control 160; dependent on the poison value. 161; 162define void @nsw_step2(i4 %n) { 163; CHECK-LABEL: 'nsw_step2' 164; CHECK-NEXT: Determining loop execution counts for: @nsw_step2 165; CHECK-NEXT: Loop %loop: backedge-taken count is ((-1 + %n) /u 2) 166; CHECK-NEXT: Loop %loop: max backedge-taken count is 2 167; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-1 + %n) /u 2) 168; CHECK-NEXT: Predicates: 169; CHECK: Loop %loop: Trip multiple is 1 170; 171entry: 172 %s = icmp sgt i4 %n, 0 173 br i1 %s, label %loop, label %exit 174loop: 175 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ] 176 %i.next = add nsw i4 %i, 2 177 %t = icmp slt i4 %i.next, %n 178 br i1 %t, label %loop, label %exit 179exit: 180 ret void 181} 182 183define void @nsw_start1(i4 %n) { 184; CHECK-LABEL: 'nsw_start1' 185; CHECK-NEXT: Determining loop execution counts for: @nsw_start1 186; CHECK-NEXT: Loop %loop: backedge-taken count is (-2 + (2 smax %n))<nsw> 187; CHECK-NEXT: Loop %loop: max backedge-taken count is 5 188; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-2 + (2 smax %n))<nsw> 189; CHECK-NEXT: Predicates: 190; CHECK: Loop %loop: Trip multiple is 1 191; 192entry: 193 %s = icmp sgt i4 %n, 0 194 br i1 %s, label %loop, label %exit 195loop: 196 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ] 197 %i.next = add nsw i4 %i, 1 198 %t = icmp slt i4 %i.next, %n 199 br i1 %t, label %loop, label %exit 200exit: 201 ret void 202} 203 204define void @nsw_start1_step2(i4 %n) { 205; CHECK-LABEL: 'nsw_start1_step2' 206; CHECK-NEXT: Determining loop execution counts for: @nsw_start1_step2 207; CHECK-NEXT: Loop %loop: backedge-taken count is ((-2 + (3 smax %n))<nsw> /u 2) 208; CHECK-NEXT: Loop %loop: max backedge-taken count is 2 209; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-2 + (3 smax %n))<nsw> /u 2) 210; CHECK-NEXT: Predicates: 211; CHECK: Loop %loop: Trip multiple is 1 212; 213entry: 214 %s = icmp sgt i4 %n, 0 215 br i1 %s, label %loop, label %exit 216loop: 217 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ] 218 %i.next = add nsw i4 %i, 2 219 %t = icmp slt i4 %i.next, %n 220 br i1 %t, label %loop, label %exit 221exit: 222 ret void 223} 224 225define void @nsw_startx(i4 %n, i4 %x) { 226; CHECK-LABEL: 'nsw_startx' 227; CHECK-NEXT: Determining loop execution counts for: @nsw_startx 228; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n)) 229; CHECK-NEXT: Loop %loop: max backedge-taken count is -1 230; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n)) 231; CHECK-NEXT: Predicates: 232; CHECK: Loop %loop: Trip multiple is 1 233; 234entry: 235 %s = icmp sgt i4 %n, 0 236 br i1 %s, label %loop, label %exit 237loop: 238 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ] 239 %i.next = add nsw i4 %i, 1 240 %t = icmp slt i4 %i.next, %n 241 br i1 %t, label %loop, label %exit 242exit: 243 ret void 244} 245 246define void @nsw_startx_step2(i4 %n, i4 %x) { 247; CHECK-LABEL: 'nsw_startx_step2' 248; CHECK-NEXT: Determining loop execution counts for: @nsw_startx_step2 249; CHECK-NEXT: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax %n)) /u 2) 250; CHECK-NEXT: Loop %loop: max backedge-taken count is 7 251; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax %n)) /u 2) 252; CHECK-NEXT: Predicates: 253; CHECK: Loop %loop: Trip multiple is 1 254; 255entry: 256 %s = icmp sgt i4 %n, 0 257 br i1 %s, label %loop, label %exit 258loop: 259 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ] 260 %i.next = add nsw i4 %i, 2 261 %t = icmp slt i4 %i.next, %n 262 br i1 %t, label %loop, label %exit 263exit: 264 ret void 265} 266 267define void @even(i4 %n) { 268; CHECK-LABEL: 'even' 269; CHECK-NEXT: Determining loop execution counts for: @even 270; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + (2 * %n)) 271; CHECK-NEXT: Loop %loop: max backedge-taken count is 5 272; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + (2 * %n)) 273; CHECK-NEXT: Predicates: 274; CHECK: Loop %loop: Trip multiple is 2 275; 276entry: 277 %m = shl i4 %n, 1 278 %s = icmp sgt i4 %m, 0 279 br i1 %s, label %loop, label %exit 280loop: 281 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ] 282 %i.next = add i4 %i, 1 283 %t = icmp slt i4 %i.next, %m 284 br i1 %t, label %loop, label %exit 285exit: 286 ret void 287} 288 289define void @even_step2(i4 %n) { 290; CHECK-LABEL: 'even_step2' 291; CHECK-NEXT: Determining loop execution counts for: @even_step2 292; CHECK-NEXT: Loop %loop: backedge-taken count is ((-1 + (2 * %n)) /u 2) 293; CHECK-NEXT: Loop %loop: max backedge-taken count is 2 294; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-1 + (2 * %n)) /u 2) 295; CHECK-NEXT: Predicates: 296; CHECK: Loop %loop: Trip multiple is 1 297; 298entry: 299 %m = shl i4 %n, 1 300 %s = icmp sgt i4 %m, 0 301 br i1 %s, label %loop, label %exit 302loop: 303 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ] 304 %i.next = add i4 %i, 2 305 %t = icmp slt i4 %i.next, %m 306 br i1 %t, label %loop, label %exit 307exit: 308 ret void 309} 310 311define void @even_start1(i4 %n) { 312; CHECK-LABEL: 'even_start1' 313; CHECK-NEXT: Determining loop execution counts for: @even_start1 314; CHECK-NEXT: Loop %loop: backedge-taken count is (-2 + (2 * %n)) 315; CHECK-NEXT: Loop %loop: max backedge-taken count is 4 316; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-2 + (2 * %n)) 317; CHECK-NEXT: Predicates: 318; CHECK: Loop %loop: Trip multiple is 1 319; 320entry: 321 %m = shl i4 %n, 1 322 %s = icmp sgt i4 %m, 0 323 br i1 %s, label %loop, label %exit 324loop: 325 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ] 326 %i.next = add i4 %i, 1 327 %t = icmp slt i4 %i.next, %m 328 br i1 %t, label %loop, label %exit 329exit: 330 ret void 331} 332 333define void @even_start1_step2(i4 %n) { 334; CHECK-LABEL: 'even_start1_step2' 335; CHECK-NEXT: Determining loop execution counts for: @even_start1_step2 336; CHECK-NEXT: Loop %loop: backedge-taken count is ((-2 + (2 * %n)) /u 2) 337; CHECK-NEXT: Loop %loop: max backedge-taken count is 2 338; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-2 + (2 * %n)) /u 2) 339; CHECK-NEXT: Predicates: 340; CHECK: Loop %loop: Trip multiple is 1 341; 342entry: 343 %m = shl i4 %n, 1 344 %s = icmp sgt i4 %m, 0 345 br i1 %s, label %loop, label %exit 346loop: 347 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ] 348 %i.next = add i4 %i, 2 349 %t = icmp slt i4 %i.next, %m 350 br i1 %t, label %loop, label %exit 351exit: 352 ret void 353} 354 355define void @even_startx(i4 %n, i4 %x) { 356; CHECK-LABEL: 'even_startx' 357; CHECK-NEXT: Determining loop execution counts for: @even_startx 358; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n))) 359; CHECK-NEXT: Loop %loop: max backedge-taken count is -2 360; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n))) 361; CHECK-NEXT: Predicates: 362; CHECK: Loop %loop: Trip multiple is 1 363; 364entry: 365 %m = shl i4 %n, 1 366 %s = icmp sgt i4 %m, 0 367 br i1 %s, label %loop, label %exit 368loop: 369 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ] 370 %i.next = add i4 %i, 1 371 %t = icmp slt i4 %i.next, %m 372 br i1 %t, label %loop, label %exit 373exit: 374 ret void 375} 376 377define void @even_startx_step2(i4 %n, i4 %x) { 378; CHECK-LABEL: 'even_startx_step2' 379; CHECK-NEXT: Determining loop execution counts for: @even_startx_step2 380; CHECK-NEXT: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax (2 * %n))) /u 2) 381; CHECK-NEXT: Loop %loop: max backedge-taken count is 7 382; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax (2 * %n))) /u 2) 383; CHECK-NEXT: Predicates: 384; CHECK: Loop %loop: Trip multiple is 1 385; 386entry: 387 %m = shl i4 %n, 1 388 %s = icmp sgt i4 %m, 0 389 br i1 %s, label %loop, label %exit 390loop: 391 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ] 392 %i.next = add i4 %i, 2 393 %t = icmp slt i4 %i.next, %m 394 br i1 %t, label %loop, label %exit 395exit: 396 ret void 397} 398 399define void @even_nsw(i4 %n) { 400; CHECK-LABEL: 'even_nsw' 401; CHECK-NEXT: Determining loop execution counts for: @even_nsw 402; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + (2 * %n)) 403; CHECK-NEXT: Loop %loop: max backedge-taken count is 5 404; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + (2 * %n)) 405; CHECK-NEXT: Predicates: 406; CHECK: Loop %loop: Trip multiple is 2 407; 408entry: 409 %m = shl i4 %n, 1 410 %s = icmp sgt i4 %m, 0 411 br i1 %s, label %loop, label %exit 412loop: 413 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ] 414 %i.next = add nsw i4 %i, 1 415 %t = icmp slt i4 %i.next, %m 416 br i1 %t, label %loop, label %exit 417exit: 418 ret void 419} 420 421define void @even_nsw_step2(i4 %n) { 422; CHECK-LABEL: 'even_nsw_step2' 423; CHECK-NEXT: Determining loop execution counts for: @even_nsw_step2 424; CHECK-NEXT: Loop %loop: backedge-taken count is ((-1 + (2 * %n)) /u 2) 425; CHECK-NEXT: Loop %loop: max backedge-taken count is 2 426; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-1 + (2 * %n)) /u 2) 427; CHECK-NEXT: Predicates: 428; CHECK: Loop %loop: Trip multiple is 1 429; 430entry: 431 %m = shl i4 %n, 1 432 %s = icmp sgt i4 %m, 0 433 br i1 %s, label %loop, label %exit 434loop: 435 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ] 436 %i.next = add nsw i4 %i, 2 437 %t = icmp slt i4 %i.next, %m 438 br i1 %t, label %loop, label %exit 439exit: 440 ret void 441} 442 443define void @even_nsw_start1(i4 %n) { 444; CHECK-LABEL: 'even_nsw_start1' 445; CHECK-NEXT: Determining loop execution counts for: @even_nsw_start1 446; CHECK-NEXT: Loop %loop: backedge-taken count is (-2 + (2 * %n)) 447; CHECK-NEXT: Loop %loop: max backedge-taken count is 4 448; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-2 + (2 * %n)) 449; CHECK-NEXT: Predicates: 450; CHECK: Loop %loop: Trip multiple is 1 451; 452entry: 453 %m = shl i4 %n, 1 454 %s = icmp sgt i4 %m, 0 455 br i1 %s, label %loop, label %exit 456loop: 457 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ] 458 %i.next = add nsw i4 %i, 1 459 %t = icmp slt i4 %i.next, %m 460 br i1 %t, label %loop, label %exit 461exit: 462 ret void 463} 464 465define void @even_nsw_start1_step2(i4 %n) { 466; CHECK-LABEL: 'even_nsw_start1_step2' 467; CHECK-NEXT: Determining loop execution counts for: @even_nsw_start1_step2 468; CHECK-NEXT: Loop %loop: backedge-taken count is ((-2 + (2 * %n)) /u 2) 469; CHECK-NEXT: Loop %loop: max backedge-taken count is 2 470; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-2 + (2 * %n)) /u 2) 471; CHECK-NEXT: Predicates: 472; CHECK: Loop %loop: Trip multiple is 1 473; 474entry: 475 %m = shl i4 %n, 1 476 %s = icmp sgt i4 %m, 0 477 br i1 %s, label %loop, label %exit 478loop: 479 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ] 480 %i.next = add nsw i4 %i, 2 481 %t = icmp slt i4 %i.next, %m 482 br i1 %t, label %loop, label %exit 483exit: 484 ret void 485} 486 487define void @even_nsw_startx(i4 %n, i4 %x) { 488; CHECK-LABEL: 'even_nsw_startx' 489; CHECK-NEXT: Determining loop execution counts for: @even_nsw_startx 490; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n))) 491; CHECK-NEXT: Loop %loop: max backedge-taken count is -2 492; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n))) 493; CHECK-NEXT: Predicates: 494; CHECK: Loop %loop: Trip multiple is 1 495; 496entry: 497 %m = shl i4 %n, 1 498 %s = icmp sgt i4 %m, 0 499 br i1 %s, label %loop, label %exit 500loop: 501 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ] 502 %i.next = add nsw i4 %i, 1 503 %t = icmp slt i4 %i.next, %m 504 br i1 %t, label %loop, label %exit 505exit: 506 ret void 507} 508 509define void @even_nsw_startx_step2(i4 %n, i4 %x) { 510; CHECK-LABEL: 'even_nsw_startx_step2' 511; CHECK-NEXT: Determining loop execution counts for: @even_nsw_startx_step2 512; CHECK-NEXT: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax (2 * %n))) /u 2) 513; CHECK-NEXT: Loop %loop: max backedge-taken count is 7 514; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax (2 * %n))) /u 2) 515; CHECK-NEXT: Predicates: 516; CHECK: Loop %loop: Trip multiple is 1 517; 518entry: 519 %m = shl i4 %n, 1 520 %s = icmp sgt i4 %m, 0 521 br i1 %s, label %loop, label %exit 522loop: 523 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ] 524 %i.next = add nsw i4 %i, 2 525 %t = icmp slt i4 %i.next, %m 526 br i1 %t, label %loop, label %exit 527exit: 528 ret void 529} 530