From e0ccd190ae8b3a7e8d79258218703a6ecadbc883 Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Wed, 20 Jul 2022 19:01:43 +0700 Subject: [PATCH] [SCEV][NFC][CT] Do not waste time proving contextual facts for unreached loops and blocks In fact, in unreached code we can say that every fact is true. So do not waste time trying to do something smarter. Formally it's not an NFC because it may change query results in unreached code, but they won't have any impact on execution. Hypothetical CT boost expected but not measured in practice. Differential Revision: https://reviews.llvm.org/D129878 --- llvm/lib/Analysis/ScalarEvolution.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 009b468dd4e6..d46248aa3889 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -11012,8 +11012,10 @@ ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) { // Interpret a null as meaning no loop, where there is obviously no guard - // (interprocedural conditions notwithstanding). - if (!L) return true; + // (interprocedural conditions notwithstanding). Do not bother about + // unreachable loops. + if (!L || !DT.isReachableFromEntry(L->getHeader())) + return true; if (VerifyIR) assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) && @@ -11070,12 +11072,6 @@ ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, return true; } - // If the loop is not reachable from the entry block, we risk running into an - // infinite loop as we walk up into the dom tree. These loops do not matter - // anyway, so we just return a conservative answer when we see them. - if (!DT.isReachableFromEntry(L->getHeader())) - return false; - if (isImpliedViaGuard(Latch, Pred, LHS, RHS)) return true; @@ -11121,6 +11117,9 @@ bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) { + // Do not bother proving facts for unreachable code. + if (!DT.isReachableFromEntry(BB)) + return true; if (VerifyIR) assert(!verifyFunction(*BB->getParent(), &dbgs()) && "This cannot be done on broken IR!");