From a5051f2fa2f20405a7cd0630067afc338f6c6d44 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 24 Apr 2021 21:43:46 +0200 Subject: [PATCH] [SCEV] Fix applyLoopGuards() chaining for ne predicates ICMP_NE predicates directly overwrote the rewritten result, instead of chaining it with previous rewrites, as was done for ICMP_ULT and ICMP_ULE. This means that some guards were effectively discarded, depending on their order. --- llvm/lib/Analysis/ScalarEvolution.cpp | 34 +++++++++------------- .../max-backedge-taken-count-guard-info.ll | 8 ++--- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 43ec432..488f60d 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -13433,30 +13433,22 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) { if (!LHSUnknown) return; + // Check whether LHS has already been rewritten. In that case we want to + // chain further rewrites onto the already rewritten value. + auto I = RewriteMap.find(LHSUnknown->getValue()); + const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHS; + // TODO: use information from more predicates. switch (Predicate) { - case CmpInst::ICMP_ULT: { - if (!containsAddRecurrence(RHS)) { - const SCEV *Base = LHS; - auto I = RewriteMap.find(LHSUnknown->getValue()); - if (I != RewriteMap.end()) - Base = I->second; - - RewriteMap[LHSUnknown->getValue()] = - getUMinExpr(Base, getMinusSCEV(RHS, getOne(RHS->getType()))); - } + case CmpInst::ICMP_ULT: + if (!containsAddRecurrence(RHS)) + RewriteMap[LHSUnknown->getValue()] = getUMinExpr( + RewrittenLHS, getMinusSCEV(RHS, getOne(RHS->getType()))); break; - } - case CmpInst::ICMP_ULE: { - if (!containsAddRecurrence(RHS)) { - const SCEV *Base = LHS; - auto I = RewriteMap.find(LHSUnknown->getValue()); - if (I != RewriteMap.end()) - Base = I->second; - RewriteMap[LHSUnknown->getValue()] = getUMinExpr(Base, RHS); - } + case CmpInst::ICMP_ULE: + if (!containsAddRecurrence(RHS)) + RewriteMap[LHSUnknown->getValue()] = getUMinExpr(RewrittenLHS, RHS); break; - } case CmpInst::ICMP_EQ: if (isa(RHS)) RewriteMap[LHSUnknown->getValue()] = RHS; @@ -13465,7 +13457,7 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) { if (isa(RHS) && cast(RHS)->getValue()->isNullValue()) RewriteMap[LHSUnknown->getValue()] = - getUMaxExpr(LHS, getOne(RHS->getType())); + getUMaxExpr(RewrittenLHS, getOne(RHS->getType())); break; default: break; diff --git a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll index 4a176be..7419fa9 100644 --- a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll +++ b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll @@ -451,14 +451,14 @@ define void @test_guard_ne_ult(i32* nocapture readonly %data, i64 %count) { ; CHECK-LABEL: 'test_guard_ne_ult' ; CHECK-NEXT: Classifying expressions for: @test_guard_ne_ult ; CHECK-NEXT: %iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ] -; CHECK-NEXT: --> {0,+,1}<%loop> U: [0,-1) S: [0,-1) Exits: (-1 + %count) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: --> {0,+,1}<%loop> U: [0,4) S: [0,4) Exits: (-1 + %count) LoopDispositions: { %loop: Computable } ; CHECK-NEXT: %idx = getelementptr inbounds i32, i32* %data, i64 %iv -; CHECK-NEXT: --> {%data,+,4}<%loop> U: full-set S: full-set Exits: (-4 + (4 * %count) + %data) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: --> {%data,+,4}<%loop> U: full-set S: full-set Exits: (-4 + (4 * %count) + %data) LoopDispositions: { %loop: Computable } ; CHECK-NEXT: %iv.next = add nuw i64 %iv, 1 -; CHECK-NEXT: --> {1,+,1}<%loop> U: [1,0) S: [1,0) Exits: %count LoopDispositions: { %loop: Computable } +; CHECK-NEXT: --> {1,+,1}<%loop> U: [1,5) S: [1,5) Exits: %count LoopDispositions: { %loop: Computable } ; CHECK-NEXT: Determining loop execution counts for: @test_guard_ne_ult ; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + %count) -; CHECK-NEXT: Loop %loop: max backedge-taken count is -2 +; CHECK-NEXT: Loop %loop: max backedge-taken count is 3 ; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %count) ; CHECK-NEXT: Predicates: ; CHECK: Loop %loop: Trip multiple is 1 -- 2.7.4