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 4target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 5 6@A = weak global [1000 x i32] zeroinitializer, align 32 7 8; The resulting predicate is i16 {0,+,1} <nssw>, meanining 9; that the resulting backedge expression will be valid for: 10; (1 + (-1 smax %M)) <= MAX_INT16 11; 12; At the limit condition for M (MAX_INT16 - 1) we have in the 13; last iteration: 14; i0 <- MAX_INT16 15; i0.ext <- MAX_INT16 16; 17; and therefore no wrapping happend for i0 or i0.ext 18; throughout the execution of the loop. The resulting predicated 19; backedge taken count is correct. 20 21; CHECK: Classifying expressions for: @test1 22; CHECK: %i.0.ext = sext i16 %i.0 to i32 23; CHECK-NEXT: --> (sext i16 {0,+,1}<%bb3> to i32) 24; CHECK: Loop %bb3: Unpredictable backedge-taken count. 25; CHECK-NEXT: Loop %bb3: Unpredictable max backedge-taken count. 26; CHECK-NEXT: Loop %bb3: Predicated backedge-taken count is (1 + (-1 smax %M)) 27; CHECK-NEXT: Predicates: 28; CHECK-NEXT: {0,+,1}<%bb3> Added Flags: <nssw> 29define void @test1(i32 %N, i32 %M) { 30entry: 31 br label %bb3 32 33bb: ; preds = %bb3 34 %tmp = getelementptr [1000 x i32], [1000 x i32]* @A, i32 0, i16 %i.0 ; <i32*> [#uses=1] 35 store i32 123, i32* %tmp 36 %tmp2 = add i16 %i.0, 1 ; <i32> [#uses=1] 37 br label %bb3 38 39bb3: ; preds = %bb, %entry 40 %i.0 = phi i16 [ 0, %entry ], [ %tmp2, %bb ] ; <i32> [#uses=3] 41 %i.0.ext = sext i16 %i.0 to i32 42 %tmp3 = icmp sle i32 %i.0.ext, %M ; <i1> [#uses=1] 43 br i1 %tmp3, label %bb, label %bb5 44 45bb5: ; preds = %bb3 46 br label %return 47 48return: ; preds = %bb5 49 ret void 50} 51 52; The predicated backedge taken count is: 53; (2 + (zext i16 %Start to i32) + ((-2 + (-1 * (sext i16 %Start to i32))) 54; smax (-1 + (-1 * %M))) 55; ) 56 57; -1 + (-1 * %M) <= (-2 + (-1 * (sext i16 %Start to i32)) 58; The predicated backedge taken count is 0. 59; From the IR, this is correct since we will bail out at the 60; first iteration. 61 62 63; * -1 + (-1 * %M) > (-2 + (-1 * (sext i16 %Start to i32)) 64; or: %M < 1 + (sext i16 %Start to i32) 65; 66; The predicated backedge taken count is 1 + (zext i16 %Start to i32) - %M 67; 68; If %M >= MIN_INT + 1, this predicated backedge taken count would be correct (even 69; without predicates). However, for %M < MIN_INT this would be an infinite loop. 70; In these cases, the {%Start,+,-1} <nusw> predicate would be false, as the 71; final value of the expression {%Start,+,-1} expression (%M - 1) would not be 72; representable as an i16. 73 74; There is also a limit case here where the value of %M is MIN_INT. In this case 75; we still have an infinite loop, since icmp sge %x, MIN_INT will always return 76; true. 77 78; CHECK: Classifying expressions for: @test2 79 80; CHECK: %i.0.ext = sext i16 %i.0 to i32 81; CHECK-NEXT: --> (sext i16 {%Start,+,-1}<%bb3> to i32) 82; CHECK: Loop %bb3: Unpredictable backedge-taken count. 83; CHECK-NEXT: Loop %bb3: Unpredictable max backedge-taken count. 84; CHECK-NEXT: Loop %bb3: Predicated backedge-taken count is (1 + (sext i16 %Start to i32) + (-1 * ((1 + (sext i16 %Start to i32))<nsw> smin %M))) 85; CHECK-NEXT: Predicates: 86; CHECK-NEXT: {%Start,+,-1}<%bb3> Added Flags: <nssw> 87 88define void @test2(i32 %N, i32 %M, i16 %Start) { 89entry: 90 br label %bb3 91 92bb: ; preds = %bb3 93 %tmp = getelementptr [1000 x i32], [1000 x i32]* @A, i32 0, i16 %i.0 ; <i32*> [#uses=1] 94 store i32 123, i32* %tmp 95 %tmp2 = sub i16 %i.0, 1 ; <i32> [#uses=1] 96 br label %bb3 97 98bb3: ; preds = %bb, %entry 99 %i.0 = phi i16 [ %Start, %entry ], [ %tmp2, %bb ] ; <i32> [#uses=3] 100 %i.0.ext = sext i16 %i.0 to i32 101 %tmp3 = icmp sge i32 %i.0.ext, %M ; <i1> [#uses=1] 102 br i1 %tmp3, label %bb, label %bb5 103 104bb5: ; preds = %bb3 105 br label %return 106 107return: ; preds = %bb5 108 ret void 109} 110 111