From 17c20a6dfb7c89ac4eb13990308b731263345918 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 5 Oct 2021 23:21:25 +0200 Subject: [PATCH] [SCEV] Avoid unnecessary domination checks (NFC) When determining the defining scope, avoid repeatedly querying dominationg against the function entry instruction. This ends up begin a very common case that we can handle more efficiently. --- llvm/include/llvm/Analysis/ScalarEvolution.h | 5 +++-- llvm/lib/Analysis/ScalarEvolution.cpp | 19 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h index df1b7c2ad157..a38b1a299163 100644 --- a/llvm/include/llvm/Analysis/ScalarEvolution.h +++ b/llvm/include/llvm/Analysis/ScalarEvolution.h @@ -1921,9 +1921,10 @@ private: SCEV::NoWrapFlags getNoWrapFlagsFromUB(const Value *V); /// Return a scope which provides an upper bound on the defining scope of - /// 'S'. Specifically, return the first instruction in said bounding scope. + /// 'S'. Specifically, return the first instruction in said bounding scope. + /// Return nullptr if the scope is trivial (function entry). /// (See scope definition rules associated with flag discussion above) - const Instruction *getDefiningScopeBound(const SCEV *S); + const Instruction *getNonTrivialDefiningScopeBound(const SCEV *S); /// Return a scope which provides an upper bound on the defining scope for /// a SCEV with the operands in Ops. diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index b767adac6a59..14f5dd51c406 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -6585,25 +6585,24 @@ SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) { return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap; } -const Instruction *ScalarEvolution::getDefiningScopeBound(const SCEV *S) { +const Instruction * +ScalarEvolution::getNonTrivialDefiningScopeBound(const SCEV *S) { if (auto *AddRec = dyn_cast(S)) return &*AddRec->getLoop()->getHeader()->begin(); if (auto *U = dyn_cast(S)) if (auto *I = dyn_cast(U->getValue())) return I; - // All SCEVs are bound by the entry to F - return &*F.getEntryBlock().begin(); + return nullptr; } const Instruction * ScalarEvolution::getDefiningScopeBound(ArrayRef Ops) { - const Instruction *Bound = &*F.getEntryBlock().begin(); - for (auto *S : Ops) { - auto *DefI = getDefiningScopeBound(S); - if (DT.dominates(Bound, DefI)) - Bound = DefI; - } - return Bound; + const Instruction *Bound = nullptr; + for (auto *S : Ops) + if (auto *DefI = getNonTrivialDefiningScopeBound(S)) + if (!Bound || DT.dominates(Bound, DefI)) + Bound = DefI; + return Bound ? Bound : &*F.getEntryBlock().begin(); }