1; RUN: opt < %s -analyze -enable-new-pm=0 -scalar-evolution | FileCheck %s 2; RUN: opt < %s -disable-output "-passes=print<scalar-evolution>" 2>&1 | FileCheck %s 3 4; ScalarEvolution should be able to understand the loop and eliminate the casts. 5 6; CHECK: {%d,+,4} 7 8define void @foo(i32* nocapture %d, i32 %n) nounwind { 9entry: 10 %0 = icmp sgt i32 %n, 0 ; <i1> [#uses=1] 11 br i1 %0, label %bb.nph, label %return 12 13bb.nph: ; preds = %entry 14 br label %bb 15 16bb: ; preds = %bb1, %bb.nph 17 %i.02 = phi i32 [ %5, %bb1 ], [ 0, %bb.nph ] ; <i32> [#uses=2] 18 %p.01 = phi i8 [ %4, %bb1 ], [ -1, %bb.nph ] ; <i8> [#uses=2] 19 %1 = sext i8 %p.01 to i32 ; <i32> [#uses=1] 20 %2 = sext i32 %i.02 to i64 ; <i64> [#uses=1] 21 %3 = getelementptr i32, i32* %d, i64 %2 ; <i32*> [#uses=1] 22 store i32 %1, i32* %3, align 4 23 %4 = add i8 %p.01, 1 ; <i8> [#uses=1] 24 %5 = add i32 %i.02, 1 ; <i32> [#uses=2] 25 br label %bb1 26 27bb1: ; preds = %bb 28 %6 = icmp slt i32 %5, %n ; <i1> [#uses=1] 29 br i1 %6, label %bb, label %bb1.return_crit_edge 30 31bb1.return_crit_edge: ; preds = %bb1 32 br label %return 33 34return: ; preds = %bb1.return_crit_edge, %entry 35 ret void 36} 37 38; ScalarEvolution should be able to find the maximum tripcount 39; of this multiple-exit loop, and if it doesn't know the exact 40; count, it should say so. 41 42; PR7845 43; CHECK: Loop %for.cond: <multiple exits> Unpredictable backedge-taken count. 44; CHECK: Loop %for.cond: max backedge-taken count is 5 45 46@.str = private constant [4 x i8] c"%d\0A\00" ; <[4 x i8]*> [#uses=2] 47 48define i32 @main() nounwind { 49entry: 50 br label %for.cond 51 52for.cond: ; preds = %for.inc, %entry 53 %g_4.0 = phi i32 [ 0, %entry ], [ %add, %for.inc ] ; <i32> [#uses=5] 54 %cmp = icmp slt i32 %g_4.0, 5 ; <i1> [#uses=1] 55 br i1 %cmp, label %for.body, label %for.end 56 57for.body: ; preds = %for.cond 58 %conv = trunc i32 %g_4.0 to i16 ; <i16> [#uses=1] 59 %tobool.not = icmp eq i16 %conv, 0 ; <i1> [#uses=1] 60 %tobool3 = icmp ne i32 %g_4.0, 0 ; <i1> [#uses=1] 61 %or.cond = and i1 %tobool.not, %tobool3 ; <i1> [#uses=1] 62 br i1 %or.cond, label %for.end, label %for.inc 63 64for.inc: ; preds = %for.body 65 %add = add nsw i32 %g_4.0, 1 ; <i32> [#uses=1] 66 br label %for.cond 67 68for.end: ; preds = %for.body, %for.cond 69 %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %g_4.0) nounwind ; <i32> [#uses=0] 70 ret i32 0 71} 72 73declare i32 @printf(i8*, ...) 74 75define void @test(i8* %a, i32 %n) nounwind { 76entry: 77 %cmp1 = icmp sgt i32 %n, 0 78 br i1 %cmp1, label %for.body.lr.ph, label %for.end 79 80for.body.lr.ph: ; preds = %entry 81 %tmp = zext i32 %n to i64 82 br label %for.body 83 84for.body: ; preds = %for.body, %for.body.lr.ph 85 %indvar = phi i64 [ %indvar.next, %for.body ], [ 0, %for.body.lr.ph ] 86 %arrayidx = getelementptr i8, i8* %a, i64 %indvar 87 store i8 0, i8* %arrayidx, align 1 88 %indvar.next = add i64 %indvar, 1 89 %exitcond = icmp ne i64 %indvar.next, %tmp 90 br i1 %exitcond, label %for.body, label %for.cond.for.end_crit_edge 91 92for.cond.for.end_crit_edge: ; preds = %for.body 93 br label %for.end 94 95for.end: ; preds = %for.cond.for.end_crit_edge, %entry 96 ret void 97} 98 99; CHECK: Determining loop execution counts for: @test 100; CHECK-NEXT: backedge-taken count is 101; CHECK-NEXT: max backedge-taken count is 4294967294 102 103; PR19799: Indvars miscompile due to an incorrect max backedge taken count from SCEV. 104; CHECK-LABEL: @pr19799 105; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count. 106; CHECK: Loop %for.body.i: max backedge-taken count is 1 107@a = common global i32 0, align 4 108 109define i32 @pr19799() { 110entry: 111 store i32 -1, i32* @a, align 4 112 br label %for.body.i 113 114for.body.i: ; preds = %for.cond.i, %entry 115 %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ] 116 %tobool.i = icmp eq i32 %storemerge1.i, 0 117 %add.i.i = add nsw i32 %storemerge1.i, 2 118 br i1 %tobool.i, label %bar.exit, label %for.cond.i 119 120for.cond.i: ; preds = %for.body.i 121 store i32 %add.i.i, i32* @a, align 4 122 %cmp.i = icmp slt i32 %storemerge1.i, 0 123 br i1 %cmp.i, label %for.body.i, label %bar.exit 124 125bar.exit: ; preds = %for.cond.i, %for.body.i 126 ret i32 0 127} 128 129; PR18886: Indvars miscompile due to an incorrect max backedge taken count from SCEV. 130; CHECK-LABEL: @pr18886 131; CHECK: Loop %for.body: <multiple exits> Unpredictable backedge-taken count. 132; CHECK: Loop %for.body: max backedge-taken count is 3 133@aa = global i64 0, align 8 134 135define i32 @pr18886() { 136entry: 137 store i64 -21, i64* @aa, align 8 138 br label %for.body 139 140for.body: 141 %storemerge1 = phi i64 [ -21, %entry ], [ %add, %for.cond ] 142 %tobool = icmp eq i64 %storemerge1, 0 143 %add = add nsw i64 %storemerge1, 8 144 br i1 %tobool, label %return, label %for.cond 145 146for.cond: 147 store i64 %add, i64* @aa, align 8 148 %cmp = icmp slt i64 %add, 9 149 br i1 %cmp, label %for.body, label %return 150 151return: 152 %retval.0 = phi i32 [ 1, %for.body ], [ 0, %for.cond ] 153 ret i32 %retval.0 154} 155 156; Here we have a must-exit loop latch that is not computable and a 157; may-exit early exit that can only have one non-exiting iteration 158; before the check is forever skipped. 159; 160; CHECK-LABEL: @cannot_compute_mustexit 161; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count. 162; CHECK: Loop %for.body.i: Unpredictable max backedge-taken count. 163@b = common global i32 0, align 4 164 165define i32 @cannot_compute_mustexit() { 166entry: 167 store i32 -1, i32* @a, align 4 168 br label %for.body.i 169 170for.body.i: ; preds = %for.cond.i, %entry 171 %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ] 172 %tobool.i = icmp eq i32 %storemerge1.i, 0 173 %add.i.i = add nsw i32 %storemerge1.i, 2 174 br i1 %tobool.i, label %bar.exit, label %for.cond.i 175 176for.cond.i: ; preds = %for.body.i 177 store i32 %add.i.i, i32* @a, align 4 178 %ld = load volatile i32, i32* @b 179 %cmp.i = icmp ne i32 %ld, 0 180 br i1 %cmp.i, label %for.body.i, label %bar.exit 181 182bar.exit: ; preds = %for.cond.i, %for.body.i 183 ret i32 0 184} 185 186; This loop has two must-exits, both of which dominate the latch. The 187; MaxBECount should be the minimum of them. 188; 189; CHECK-LABEL: @two_mustexit 190; CHECK: Loop %for.body.i: <multiple exits> backedge-taken count is 1 191; CHECK: Loop %for.body.i: max backedge-taken count is 1 192define i32 @two_mustexit() { 193entry: 194 store i32 -1, i32* @a, align 4 195 br label %for.body.i 196 197for.body.i: ; preds = %for.cond.i, %entry 198 %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ] 199 %tobool.i = icmp sgt i32 %storemerge1.i, 0 200 %add.i.i = add nsw i32 %storemerge1.i, 2 201 br i1 %tobool.i, label %bar.exit, label %for.cond.i 202 203for.cond.i: ; preds = %for.body.i 204 store i32 %add.i.i, i32* @a, align 4 205 %cmp.i = icmp slt i32 %storemerge1.i, 3 206 br i1 %cmp.i, label %for.body.i, label %bar.exit 207 208bar.exit: ; preds = %for.cond.i, %for.body.i 209 ret i32 0 210} 211 212; CHECK-LABEL: @ne_max_trip_count_1 213; CHECK: Loop %for.body: max backedge-taken count is 7 214define i32 @ne_max_trip_count_1(i32 %n) { 215entry: 216 %masked = and i32 %n, 7 217 br label %for.body 218 219for.body: 220 %i = phi i32 [ 0, %entry ], [ %add, %for.body ] 221 %add = add nsw i32 %i, 1 222 %cmp = icmp ne i32 %i, %masked 223 br i1 %cmp, label %for.body, label %bar.exit 224 225bar.exit: 226 ret i32 0 227} 228 229; CHECK-LABEL: @ne_max_trip_count_2 230; CHECK: Loop %for.body: max backedge-taken count is -1 231define i32 @ne_max_trip_count_2(i32 %n) { 232entry: 233 %masked = and i32 %n, 7 234 br label %for.body 235 236for.body: 237 %i = phi i32 [ 0, %entry ], [ %add, %for.body ] 238 %add = add nsw i32 %i, 1 239 %cmp = icmp ne i32 %add, %masked 240 br i1 %cmp, label %for.body, label %bar.exit 241 242bar.exit: 243 ret i32 0 244} 245 246; CHECK-LABEL: @ne_max_trip_count_3 247; CHECK: Loop %for.body: max backedge-taken count is 6 248define i32 @ne_max_trip_count_3(i32 %n) { 249entry: 250 %masked = and i32 %n, 7 251 %guard = icmp eq i32 %masked, 0 252 br i1 %guard, label %exit, label %for.preheader 253 254for.preheader: 255 br label %for.body 256 257for.body: 258 %i = phi i32 [ 0, %for.preheader ], [ %add, %for.body ] 259 %add = add nsw i32 %i, 1 260 %cmp = icmp ne i32 %add, %masked 261 br i1 %cmp, label %for.body, label %loop.exit 262 263loop.exit: 264 br label %exit 265 266exit: 267 ret i32 0 268} 269 270; CHECK-LABEL: @ne_max_trip_count_4 271; CHECK: Loop %for.body: max backedge-taken count is -2 272define i32 @ne_max_trip_count_4(i32 %n) { 273entry: 274 %guard = icmp eq i32 %n, 0 275 br i1 %guard, label %exit, label %for.preheader 276 277for.preheader: 278 br label %for.body 279 280for.body: 281 %i = phi i32 [ 0, %for.preheader ], [ %add, %for.body ] 282 %add = add nsw i32 %i, 1 283 %cmp = icmp ne i32 %add, %n 284 br i1 %cmp, label %for.body, label %loop.exit 285 286loop.exit: 287 br label %exit 288 289exit: 290 ret i32 0 291} 292 293; The end bound of the loop can change between iterations, so the exact trip 294; count is unknown, but SCEV can calculate the max trip count. 295define void @changing_end_bound(i32* %n_addr, i32* %addr) { 296; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound 297; CHECK: Loop %loop: Unpredictable backedge-taken count. 298; CHECK: Loop %loop: max backedge-taken count is 2147483646 299entry: 300 br label %loop 301 302loop: 303 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ] 304 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ] 305 %val = load atomic i32, i32* %addr unordered, align 4 306 fence acquire 307 %acc.next = add i32 %acc, %val 308 %iv.next = add nsw i32 %iv, 1 309 %n = load atomic i32, i32* %n_addr unordered, align 4 310 %cmp = icmp slt i32 %iv.next, %n 311 br i1 %cmp, label %loop, label %loop.exit 312 313loop.exit: 314 ret void 315} 316 317; Similar test as above, but unknown start value. 318; Also, there's no nsw on the iv.next, but SCEV knows 319; the termination condition is LT, so the IV cannot wrap. 320define void @changing_end_bound2(i32 %start, i32* %n_addr, i32* %addr) { 321; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound2 322; CHECK: Loop %loop: Unpredictable backedge-taken count. 323; CHECK: Loop %loop: max backedge-taken count is -1 324entry: 325 br label %loop 326 327loop: 328 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ] 329 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ] 330 %val = load atomic i32, i32* %addr unordered, align 4 331 fence acquire 332 %acc.next = add i32 %acc, %val 333 %iv.next = add i32 %iv, 1 334 %n = load atomic i32, i32* %n_addr unordered, align 4 335 %cmp = icmp slt i32 %iv.next, %n 336 br i1 %cmp, label %loop, label %loop.exit 337 338loop.exit: 339 ret void 340} 341 342; changing end bound and greater than one stride 343define void @changing_end_bound3(i32 %start, i32* %n_addr, i32* %addr) { 344; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound3 345; CHECK: Loop %loop: Unpredictable backedge-taken count. 346; CHECK: Loop %loop: max backedge-taken count is 1073741823 347entry: 348 br label %loop 349 350loop: 351 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ] 352 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ] 353 %val = load atomic i32, i32* %addr unordered, align 4 354 fence acquire 355 %acc.next = add i32 %acc, %val 356 %iv.next = add nsw i32 %iv, 4 357 %n = load atomic i32, i32* %n_addr unordered, align 4 358 %cmp = icmp slt i32 %iv.next, %n 359 br i1 %cmp, label %loop, label %loop.exit 360 361loop.exit: 362 ret void 363} 364 365; same as above test, but the IV can wrap around. 366; so the max backedge taken count is unpredictable. 367define void @changing_end_bound4(i32 %start, i32* %n_addr, i32* %addr) { 368; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound4 369; CHECK: Loop %loop: Unpredictable backedge-taken count. 370; CHECK: Loop %loop: Unpredictable max backedge-taken count. 371entry: 372 br label %loop 373 374loop: 375 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ] 376 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ] 377 %val = load atomic i32, i32* %addr unordered, align 4 378 fence acquire 379 %acc.next = add i32 %acc, %val 380 %iv.next = add i32 %iv, 4 381 %n = load atomic i32, i32* %n_addr unordered, align 4 382 %cmp = icmp slt i32 %iv.next, %n 383 br i1 %cmp, label %loop, label %loop.exit 384 385loop.exit: 386 ret void 387} 388 389; unknown stride. Since it's not knownPositive, we do not estimate the max 390; backedge taken count. 391define void @changing_end_bound5(i32 %stride, i32 %start, i32* %n_addr, i32* %addr) { 392; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound5 393; CHECK: Loop %loop: Unpredictable backedge-taken count. 394; CHECK: Loop %loop: Unpredictable max backedge-taken count. 395entry: 396 br label %loop 397 398loop: 399 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ] 400 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ] 401 %val = load atomic i32, i32* %addr unordered, align 4 402 fence acquire 403 %acc.next = add i32 %acc, %val 404 %iv.next = add nsw i32 %iv, %stride 405 %n = load atomic i32, i32* %n_addr unordered, align 4 406 %cmp = icmp slt i32 %iv.next, %n 407 br i1 %cmp, label %loop, label %loop.exit 408 409loop.exit: 410 ret void 411} 412 413; negative stride value 414define void @changing_end_bound6(i32 %start, i32* %n_addr, i32* %addr) { 415; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound6 416; CHECK: Loop %loop: Unpredictable backedge-taken count. 417; CHECK: Loop %loop: Unpredictable max backedge-taken count. 418entry: 419 br label %loop 420 421loop: 422 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ] 423 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ] 424 %val = load atomic i32, i32* %addr unordered, align 4 425 fence acquire 426 %acc.next = add i32 %acc, %val 427 %iv.next = add nsw i32 %iv, -1 428 %n = load atomic i32, i32* %n_addr unordered, align 4 429 %cmp = icmp slt i32 %iv.next, %n 430 br i1 %cmp, label %loop, label %loop.exit 431 432loop.exit: 433 ret void 434} 435