1; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py 2; RUN: opt < %s -disable-output "-passes=print<scalar-evolution>" 2>&1 | FileCheck %s 3 4; Tests demonstrate the bug reported as PR48225 by Congzhe Cao. 5 6; When %boolcond = false and %cond = 0: 7; - %cond.false.on.first.iter is false on 1st iteration; 8; - %cond.false.on.second.iter is false on 2nd iteration; 9; - Therefore, their AND is false on first two iterations, and the backedge is taken twice. 10; 'constant max backedge-taken count is 1' is a bug caused by wrong treatment of AND 11; condition in the computation logic. It should be 2. 12define void @test_and(i1 %boolcond) { 13; CHECK-LABEL: 'test_and' 14; CHECK-NEXT: Classifying expressions for: @test_and 15; CHECK-NEXT: %conv = zext i1 %boolcond to i32 16; CHECK-NEXT: --> (zext i1 %boolcond to i32) U: [0,2) S: [0,2) 17; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %inc, %backedge ] 18; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,3) S: [0,3) Exits: <<Unknown>> LoopDispositions: { %loop: Computable } 19; CHECK-NEXT: %or.cond = and i1 %cond.false.on.first.iter, %cond.false.on.second.iter 20; CHECK-NEXT: --> (%cond.false.on.first.iter umin %cond.false.on.second.iter) U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Variant } 21; CHECK-NEXT: %inc = add nuw nsw i32 %iv, 1 22; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,4) S: [1,4) Exits: <<Unknown>> LoopDispositions: { %loop: Computable } 23; CHECK-NEXT: Determining loop execution counts for: @test_and 24; CHECK-NEXT: Loop %loop: <multiple exits> Unpredictable backedge-taken count. 25; CHECK-NEXT: exit count for loop: i32 2 26; CHECK-NEXT: exit count for backedge: ***COULDNOTCOMPUTE*** 27; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i32 2 28; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is i32 2 29; CHECK-NEXT: symbolic max exit count for loop: i32 2 30; CHECK-NEXT: symbolic max exit count for backedge: ***COULDNOTCOMPUTE*** 31; 32entry: 33 %conv = zext i1 %boolcond to i32 34 br label %loop 35 36loop: 37 %iv = phi i32 [ 0, %entry ], [ %inc, %backedge ] 38 %cmp = icmp ult i32 %iv, 2 39 br i1 %cmp, label %backedge, label %for.end 40 41backedge: 42 %cond.false.on.first.iter = icmp ne i32 %iv, 0 43 %cond.false.on.second.iter = icmp eq i32 %iv, %conv 44 %or.cond = and i1 %cond.false.on.first.iter, %cond.false.on.second.iter 45 %inc = add nuw nsw i32 %iv, 1 46 br i1 %or.cond, label %exit, label %loop 47 48exit: 49 unreachable 50 51for.end: 52 ret void 53} 54 55; When %boolcond = false and %cond = 0: 56; - %cond.true.on.first.iter is true on 1st iteration; 57; - %cond.true.on.second.iter is true on 2nd iteration; 58; - Therefore, their OR is true on first two iterations, and the backedge is taken twice. 59; 'constant max backedge-taken count is 1' is a bug caused by wrong treatment of OR 60; condition in the computation logic. It should be 2. 61define void @test_or(i1 %boolcond) { 62; CHECK-LABEL: 'test_or' 63; CHECK-NEXT: Classifying expressions for: @test_or 64; CHECK-NEXT: %conv = zext i1 %boolcond to i32 65; CHECK-NEXT: --> (zext i1 %boolcond to i32) U: [0,2) S: [0,2) 66; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %inc, %backedge ] 67; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,3) S: [0,3) Exits: <<Unknown>> LoopDispositions: { %loop: Computable } 68; CHECK-NEXT: %or.cond = or i1 %cond.true.on.first.iter, %cond.true.on.second.iter 69; CHECK-NEXT: --> (%cond.true.on.first.iter umax %cond.true.on.second.iter) U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Variant } 70; CHECK-NEXT: %inc = add nuw nsw i32 %iv, 1 71; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,4) S: [1,4) Exits: <<Unknown>> LoopDispositions: { %loop: Computable } 72; CHECK-NEXT: Determining loop execution counts for: @test_or 73; CHECK-NEXT: Loop %loop: <multiple exits> Unpredictable backedge-taken count. 74; CHECK-NEXT: exit count for loop: i32 2 75; CHECK-NEXT: exit count for backedge: ***COULDNOTCOMPUTE*** 76; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i32 2 77; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is i32 2 78; CHECK-NEXT: symbolic max exit count for loop: i32 2 79; CHECK-NEXT: symbolic max exit count for backedge: ***COULDNOTCOMPUTE*** 80; 81entry: 82 %conv = zext i1 %boolcond to i32 83 br label %loop 84 85loop: 86 %iv = phi i32 [ 0, %entry ], [ %inc, %backedge ] 87 %cmp = icmp ult i32 %iv, 2 88 br i1 %cmp, label %backedge, label %for.end 89 90backedge: 91 %cond.true.on.first.iter = icmp eq i32 %iv, 0 92 %cond.true.on.second.iter = icmp ne i32 %iv, %conv 93 %or.cond = or i1 %cond.true.on.first.iter, %cond.true.on.second.iter 94 %inc = add nuw nsw i32 %iv, 1 95 br i1 %or.cond, label %loop, label %exit 96 97exit: 98 unreachable 99 100for.end: 101 ret void 102} 103