Index: include/llvm/Analysis/ScalarEvolution.h =================================================================== --- include/llvm/Analysis/ScalarEvolution.h +++ include/llvm/Analysis/ScalarEvolution.h @@ -1375,6 +1375,9 @@ /// Provide the special handling we need to analyze PHI SCEVs. const SCEV *createNodeForPHI(PHINode *PN); + /// Evaluate ICmpInst to a constant node for special patterns. + const SCEV *evaluateForICmp(ICmpInst *IC); + /// Helper function called from createNodeForPHI. const SCEV *createAddRecFromPHI(PHINode *PN); Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -4987,6 +4987,29 @@ return getUnknown(PN); } +const SCEV *ScalarEvolution::evaluateForICmp(ICmpInst *IC) { + BasicBlock *Latch = nullptr; + const Loop *L = LI.getLoopFor(IC->getParent()); + + // If compare instruction is same or inverse of the compare in the + // branch of the loop latch, then return a constant evolution + // node. This shall facilitate computations of loop exit counts + // in cases where compare appears in the evolution chain of induction + // variables. + if (L && (Latch = L->getLoopLatch())) { + BranchInst *BI = dyn_cast(Latch->getTerminator()); + if (BI && BI->isConditional() && BI->getCondition() == IC) { + if (BI->getSuccessor(0) != L->getHeader()) + return getConstant(Type::getInt1Ty(getContext()), 0, false); + else + return getConstant(Type::getInt1Ty(getContext()), 1, false); + } + } + + return getUnknown(IC); +} + + const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Instruction *I, Value *Cond, Value *TrueVal, @@ -6105,6 +6128,11 @@ case Instruction::PHI: return createNodeForPHI(cast(U)); + case Instruction::ICmp: + if (isa(U)) + return evaluateForICmp(cast(U)); + break; + case Instruction::Select: // U can also be a select constant expr, which let fall through. Since // createNodeForSelect only works for a condition that is an `ICmpInst`, and Index: lib/Transforms/Scalar/LoopStrengthReduce.cpp =================================================================== --- lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -2973,8 +2973,11 @@ // Ignore users that are part of a SCEV expression. This way we only // consider leaf IV Users. This effectively rediscovers a portion of // IVUsers analysis but in program order this time. - if (SE.isSCEVable(I.getType()) && !isa(SE.getSCEV(&I))) - continue; + if (SE.isSCEVable(I.getType())) { + const SCEV *SI = SE.getSCEV(&I); + if (!isa(SI) && !isa(SI)) + continue; + } // Remove this instruction from any NearUsers set it may be in. for (unsigned ChainIdx = 0, NChains = IVChainVec.size(); Index: test/Analysis/ScalarEvolution/pr34538.ll =================================================================== --- /dev/null +++ test/Analysis/ScalarEvolution/pr34538.ll @@ -0,0 +1,19 @@ +; RUN: opt -S -scalar-evolution -loop-deletion -simplifycfg -analyze < %s | FileCheck %s --check-prefix=CHECK-ANALYSIS + +define i32 @foo() local_unnamed_addr #0 { +; CHECK-ANALYSIS: Loop %do.body: backedge-taken count is 10000 +; CHECK-ANALYSIS: Loop %do.body: max backedge-taken count is 10000 +; CHECK-ANALYSIS: Loop %do.body: Predicated backedge-taken count is 10000 +entry: + br label %do.body + +do.body: ; preds = %do.body, %entry + %start.0 = phi i32 [ 0, %entry ], [ %inc.start.0, %do.body ] + %cmp = icmp slt i32 %start.0, 10000 + %inc = zext i1 %cmp to i32 + %inc.start.0 = add nsw i32 %start.0, %inc + br i1 %cmp, label %do.body, label %do.end + +do.end: ; preds = %do.body + ret i32 0 +}