llvm.org GIT mirror
[SCEV] Fix an assertion failure in the max backedge taken count Max backedge taken count is always expected to be a constant; and this is usually true by construction -- it is a SCEV expression with constant inputs. However, if the max backedge expression ends up being computed to be a udiv with a constant zero denominator[0], SCEV does not fold the result to a constant since there is no constant it can fold it to (SCEV has no representation for "infinity" or "undef"). However, in computeMaxBECountForLT we already know the denominator is positive, and thus at least 1; and we can use this fact to avoid dividing by zero. [0]: We can end up with a constant zero denominator if the signed range of the stride is more precise than the unsigned range. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316615 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 2 years ago
2 changed file(s) with 36 addition(s) and 11 deletion(s).
 9702 9702 APInt MinStart = 9703 9703 IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start); 9704 9704 9705 APInt StrideForMaxBECount; 9706 9707 bool PositiveStride = isKnownPositive(Stride); 9708 if (PositiveStride) 9709 StrideForMaxBECount = 9710 IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride); 9711 else 9712 // Using a stride of 1 is safe when computing max backedge taken count for a 9713 // loop with unknown stride, since the precondition for this function is 9714 // that it is positive. 9715 StrideForMaxBECount = APInt(BitWidth, 1, IsSigned);⏎ 9705 APInt StrideForMaxBECount =⏎ 9706 IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride); 9707 9708 // We already know that the stride is positive, so we paper over conservatism 9709 // in our range computation by forcing StrideForMaxBECount to be at least one. 9710 // In theory this is unnecessary, but we expect MaxBECount to be a 9711 // SCEVConstant, and (udiv 0) is not constant folded by SCEV (there 9712 // is nothing to constant fold it to). 9713 APInt One(BitWidth, 1, IsSigned); 9714 StrideForMaxBECount = APIntOps::smax(One, StrideForMaxBECount); 9716 9715 9717 9716 APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth) 9718 9717 : APInt::getMaxValue(BitWidth);
 0 ; RUN: opt < %s -analyze -scalar-evolution | FileCheck %s 1 2 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 3 target triple = "x86_64-unknown-linux-gnu" 4 5 ; Previously in this case the max backedge count would be computed as 1/0, which 6 ; is correct but undesirable. It would also not fold as a constant, tripping 7 ; asserts in SCEV. 8 9 define void @pluto(i32 %arg) { 10 ; CHECK-LABEL: Classifying expressions for: @pluto 11 ; CHECK: Loop %bb2: max backedge-taken count is 2 12 bb: 13 %tmp = ashr i32 %arg, 31 14 %tmp1 = add nsw i32 %tmp, 2 15 br label %bb2 16 17 bb2: ; preds = %bb2, %bb 18 %tmp3 = phi i32 [ 0, %bb ], [ %tmp4, %bb2 ] 19 %tmp4 = add nuw nsw i32 %tmp1, %tmp3 20 %tmp5 = icmp ult i32 %tmp4, 2 21 br i1 %tmp5, label %bb2, label %bb6 22 23 bb6: ; preds = %bb2 24 ret void 25 }