From: Mindong Chen Date: Tue, 26 Jan 2021 16:10:37 +0000 (+0800) Subject: [SCEV] Fix incorrect loop exit count analysis. X-Git-Tag: llvmorg-14-init~16824 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=00fcc036873164029d30819354fa923b7254cd81;p=platform%2Fupstream%2Fllvm.git [SCEV] Fix incorrect loop exit count analysis. In computeLoadConstantCompareExitLimit, the addrec used to compute the exit count should be from the loop which the exiting block belongs to. Reviewed by: mkazantsev Differential Revision: https://reviews.llvm.org/D92367 --- diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index fe9d829..0b575ae 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -7843,9 +7843,10 @@ ScalarEvolution::computeLoadConstantCompareExitLimit( Idx = getSCEVAtScope(Idx, L); // We can only recognize very limited forms of loop index expressions, in - // particular, only affine AddRec's like {C1,+,C2}. + // particular, only affine AddRec's like {C1,+,C2}. const SCEVAddRecExpr *IdxExpr = dyn_cast(Idx); - if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) || + if (!IdxExpr || IdxExpr->getLoop() != L || !IdxExpr->isAffine() || + isLoopInvariant(IdxExpr, L) || !isa(IdxExpr->getOperand(0)) || !isa(IdxExpr->getOperand(1))) return getCouldNotCompute(); diff --git a/llvm/test/Analysis/ScalarEvolution/incorrect-exit-count.ll b/llvm/test/Analysis/ScalarEvolution/incorrect-exit-count.ll index 6766be4..7ae35e7 100644 --- a/llvm/test/Analysis/ScalarEvolution/incorrect-exit-count.ll +++ b/llvm/test/Analysis/ScalarEvolution/incorrect-exit-count.ll @@ -16,7 +16,7 @@ define dso_local i32 @f() { ; CHECK-LABEL: 'f' ; CHECK-NEXT: Classifying expressions for: @f ; CHECK-NEXT: %storemerge23 = phi i32 [ 3, %entry ], [ %dec16, %for.inc13.3 ] -; CHECK-NEXT: --> {3,+,-1}<%outer.loop> U: [-2147483648,4) S: [-2147483648,4) Exits: 3 LoopDispositions: { %outer.loop: Computable, %for.cond6: Invariant, %inner.loop: Invariant } +; CHECK-NEXT: --> {3,+,-1}<%outer.loop> U: [1,4) S: [1,4) Exits: <> LoopDispositions: { %outer.loop: Computable, %for.cond6: Invariant, %inner.loop: Invariant } ; CHECK-NEXT: %storemerge1921 = phi i32 [ 3, %outer.loop ], [ %dec, %for.end ] ; CHECK-NEXT: --> {3,+,-1}<%for.cond6> U: [3,4) S: [3,4) Exits: <> LoopDispositions: { %for.cond6: Computable, %outer.loop: Variant } ; CHECK-NEXT: %idxprom20 = zext i32 %storemerge1921 to i64 @@ -56,7 +56,7 @@ define dso_local i32 @f() { ; CHECK-NEXT: %storemerge1921.lcssa25.3 = phi i32 [ %storemerge1921.3, %for.end.3 ] ; CHECK-NEXT: --> %storemerge1921.lcssa25.3 U: [3,4) S: [3,4) Exits: <> LoopDispositions: { %outer.loop: Variant, %for.cond6: Invariant, %inner.loop: Invariant } ; CHECK-NEXT: %dec16 = add nsw i32 %storemerge23, -1 -; CHECK-NEXT: --> {2,+,-1}<%outer.loop> U: full-set S: full-set Exits: 2 LoopDispositions: { %outer.loop: Computable, %for.cond6: Invariant, %inner.loop: Invariant } +; CHECK-NEXT: --> {2,+,-1}<%outer.loop> U: [0,3) S: [0,3) Exits: <> LoopDispositions: { %outer.loop: Computable, %for.cond6: Invariant, %inner.loop: Invariant } ; CHECK-NEXT: Determining loop execution counts for: @f ; CHECK-NEXT: Loop %for.cond6: Unpredictable backedge-taken count. ; CHECK-NEXT: exit count for for.cond6: 0 @@ -68,14 +68,12 @@ define dso_local i32 @f() { ; CHECK-NEXT: exit count for for.end.3: ***COULDNOTCOMPUTE*** ; CHECK-NEXT: Loop %inner.loop: max backedge-taken count is 0 ; CHECK-NEXT: Loop %inner.loop: Unpredictable predicated backedge-taken count. -; CHECK-NEXT: Loop %outer.loop: backedge-taken count is 0 -; CHECK-NEXT: exit count for for.cond6: 0 -; CHECK-NEXT: exit count for inner.loop: 0 +; CHECK-NEXT: Loop %outer.loop: Unpredictable backedge-taken count. +; CHECK-NEXT: exit count for for.cond6: ***COULDNOTCOMPUTE*** +; CHECK-NEXT: exit count for inner.loop: ***COULDNOTCOMPUTE*** ; CHECK-NEXT: exit count for for.inc13.3: 2 -; CHECK-NEXT: Loop %outer.loop: max backedge-taken count is 0 -; CHECK-NEXT: Loop %outer.loop: Predicated backedge-taken count is 0 -; CHECK-NEXT: Predicates: -; CHECK: Loop %outer.loop: Trip multiple is 0 +; CHECK-NEXT: Loop %outer.loop: max backedge-taken count is 2 +; CHECK-NEXT: Loop %outer.loop: Unpredictable predicated backedge-taken count. ; entry: store i32 3, i32* @a, align 4