21#define DEBUG_TYPE "loopnest"
49 return std::make_unique<LoopNest>(Root, SE);
55 assert(Latch &&
"Expecting a valid loop latch");
58 assert(BI &&
"Expecting loop latch terminator to be a branch instruction");
63 dbgs() <<
"Outer loop latch compare instruction: " << *OuterLoopLatchCmp
66 return OuterLoopLatchCmp;
76 dbgs() <<
"Inner loop guard compare instruction: " << *InnerLoopGuardCmp
79 return InnerLoopGuardCmp;
83 const CmpInst *InnerLoopGuardCmp,
84 const CmpInst *OuterLoopLatchCmp,
85 std::optional<Loop::LoopBounds> OuterLoopLB) {
95 (
isa<CmpInst>(
I) && &
I != OuterLoopLatchCmp && &
I != InnerLoopGuardCmp)) {
103 return (analyzeLoopNestForPerfectNest(OuterLoop, InnerLoop, SE) ==
107LoopNest::LoopNestEnum LoopNest::analyzeLoopNestForPerfectNest(
113 <<
"' and '" << InnerLoop.
getName()
114 <<
"' are perfectly nested.\n");
123 LLVM_DEBUG(
dbgs() <<
"Not perfectly nested: invalid loop structure.\n");
124 return InvalidLoopStructure;
128 auto OuterLoopLB = OuterLoop.
getBounds(SE);
129 if (OuterLoopLB == std::nullopt) {
131 << OuterLoop <<
"\n";);
132 return OuterLoopLowerBoundUnknown;
143 auto containsOnlySafeInstructions = [&](
const BasicBlock &BB) {
146 OuterLoopLatchCmp, OuterLoopLB);
149 dbgs() <<
"Instruction: " <<
I <<
"\nin basic block:" << BB
163 if (!containsOnlySafeInstructions(*OuterLoopHeader) ||
164 !containsOnlySafeInstructions(*OuterLoopLatch) ||
165 (InnerLoopPreHeader != OuterLoopHeader &&
166 !containsOnlySafeInstructions(*InnerLoopPreHeader)) ||
167 !containsOnlySafeInstructions(*InnerLoop.
getExitBlock())) {
168 LLVM_DEBUG(
dbgs() <<
"Not perfectly nested: code surrounding inner loop is "
170 return ImperfectLoopNest;
174 << InnerLoop.
getName() <<
"' are perfectly nested.\n");
176 return PerfectLoopNest;
182 switch (analyzeLoopNestForPerfectNest(OuterLoop, InnerLoop, SE)) {
183 case PerfectLoopNest:
185 "instruction vector. \n";);
188 case InvalidLoopStructure:
189 LLVM_DEBUG(
dbgs() <<
"Not perfectly nested: invalid loop structure. "
190 "Instruction vector is empty.\n";);
193 case OuterLoopLowerBoundUnknown:
195 << OuterLoop <<
"\nInstruction vector is empty.\n";);
198 case ImperfectLoopNest:
203 auto OuterLoopLB = OuterLoop.
getBounds(SE);
208 auto GetUnsafeInstructions = [&](
const BasicBlock &BB) {
214 dbgs() <<
"Instruction: " <<
I <<
"\nin basic block:" << BB
228 GetUnsafeInstructions(*OuterLoopHeader);
229 GetUnsafeInstructions(*OuterLoopLatch);
230 GetUnsafeInstructions(*InnerLoopExitBlock);
232 if (InnerLoopPreHeader != OuterLoopHeader) {
233 GetUnsafeInstructions(*InnerLoopPreHeader);
244 if (PerfectNest.
empty())
247 auto &SubLoops = L->getSubLoops();
260 LLVM_DEBUG(
dbgs() <<
"Get maximum perfect depth of loop nest rooted by loop '"
263 const Loop *CurrentLoop = &Root;
264 const auto *SubLoops = &CurrentLoop->
getSubLoops();
265 unsigned CurrentDepth = 1;
267 while (SubLoops->size() == 1) {
268 const Loop *InnerLoop = SubLoops->front();
271 dbgs() <<
"Not a perfect nest: loop '" << CurrentLoop->
getName()
272 <<
"' is not perfectly nested with loop '"
273 << InnerLoop->
getName() <<
"'\n";
278 CurrentLoop = InnerLoop;
288 bool CheckUniquePred) {
289 assert(From &&
"Expecting valid From");
296 return (BB->size() == 1);
303 while (BB && BB !=
End && IsEmpty(BB) && !Visited.
count(BB) &&
310 return (BB ==
End) ? *
End : *PredBB;
336 auto ContainsLCSSAPhi = [](
const BasicBlock &ExitBlock) {
338 return PN.getNumIncomingValues() == 1;
346 auto IsExtraPhiBlock = [&](
const BasicBlock &BB) {
347 return &*BB.getFirstNonPHIIt() == BB.getTerminator() &&
349 return all_of(PN.blocks(), [&](const BasicBlock *IncomingBlock) {
350 return IncomingBlock == InnerLoopExit ||
351 IncomingBlock == OuterLoopHeader;
359 if (OuterLoopHeader != InnerLoopPreHeader) {
364 if (&SingleSucc != InnerLoopPreHeader) {
370 bool InnerLoopExitContainsLCSSA = ContainsLCSSAPhi(*InnerLoopExit);
375 const BasicBlock *PotentialInnerPreHeader = Succ;
380 if (Succ->size() == 1) {
381 PotentialInnerPreHeader =
383 PotentialOuterLatch =
387 if (PotentialInnerPreHeader == InnerLoopPreHeader)
389 if (PotentialOuterLatch == OuterLoopLatch)
396 if (InnerLoopExitContainsLCSSA && IsExtraPhiBlock(*Succ) &&
397 Succ->getSingleSuccessor() == OuterLoopLatch) {
402 ExtraPhiBlock = Succ;
407 dbgs() <<
"Inner loop guard successor " << Succ->getName()
408 <<
" doesn't lead to inner loop preheader or "
409 "outer loop latch.\n";
418 if ((!ExtraPhiBlock ||
420 ExtraPhiBlock) != ExtraPhiBlock) &&
422 OuterLoopLatch) != OuterLoopLatch)) {
425 dbgs() <<
"Inner loop exit block " << *InnerLoopExit
426 <<
" does not directly lead to the outer loop latch.\n";);
445 OS << L->getName() <<
" ";
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file builds on the ADT/GraphTraits.h file to build a generic breadth first graph iterator.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static CmpInst * getOuterLoopLatchCmp(const Loop &OuterLoop)
static CmpInst * getInnerLoopGuardCmp(const Loop &InnerLoop)
static bool checkSafeInstruction(const Instruction &I, const CmpInst *InnerLoopGuardCmp, const CmpInst *OuterLoopLatchCmp, std::optional< Loop::LoopBounds > OuterLoopLB)
static bool checkLoopsStructure(const Loop &OuterLoop, const Loop &InnerLoop, ScalarEvolution &SE)
Determine whether the loops structure violates basic requirements for perfect nesting:
static const char * VerboseDebug
This file defines the interface for the loop nest analysis.
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
LLVM Basic Block Representation.
LLVM_ABI const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
LLVM_ABI const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
This class is the base class for the comparison instructions.
Conditional Branch instruction.
Value * getCondition() const
iterator_range< succ_iterator > successors()
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
bool isOutermost() const
Return true if the loop does not have a parent (natural) loop.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
bool isInnermost() const
Return true if the loop does not contain any (natural) loops.
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
BlockT * getHeader() const
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
LLVM_ABI PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
This class represents a loop nest and can be used to query its properties.
static const BasicBlock & skipEmptyBlockUntil(const BasicBlock *From, const BasicBlock *End, bool CheckUniquePred=false)
Recursivelly traverse all empty 'single successor' basic blocks of From (if there are any).
ArrayRef< Loop * > getLoops() const
Get the loops in the nest.
unsigned getNestDepth() const
Return the loop nest depth (i.e.
SmallVector< LoopVectorTy, 4 > getPerfectLoops(ScalarEvolution &SE) const
Retrieve a vector of perfect loop nests contained in the current loop nest.
static bool arePerfectlyNested(const Loop &OuterLoop, const Loop &InnerLoop, ScalarEvolution &SE)
Return true if the given loops OuterLoop and InnerLoop are perfectly nested with respect to each othe...
static InstrVectorTy getInterveningInstructions(const Loop &OuterLoop, const Loop &InnerLoop, ScalarEvolution &SE)
Return a vector of instructions that prevent the LoopNest given by loops OuterLoop and InnerLoop from...
const unsigned MaxPerfectDepth
static std::unique_ptr< LoopNest > getLoopNest(Loop &Root, ScalarEvolution &SE)
Construct a LoopNest object.
SmallVector< const Instruction * > InstrVectorTy
unsigned getMaxPerfectDepth() const
Return the maximum perfect nesting depth.
static unsigned getMaxPerfectDepth(const Loop &Root, ScalarEvolution &SE)
Return the maximum nesting depth of the loop nest rooted by loop Root.
Loop & getOutermostLoop() const
Return the outermost loop in the loop nest.
Represents a single loop in the control flow graph.
LLVM_ABI std::optional< LoopBounds > getBounds(ScalarEvolution &SE) const
Return the struct LoopBounds collected if all struct members are found, else std::nullopt.
LLVM_ABI CondBrInst * getLoopGuardBranch() const
Return the loop guard branch, if it exists.
StringRef getName() const
LLVM_ABI bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to,...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
The main scalar evolution driver.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
@ BasicBlock
Various leaf nodes.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
SmallVector< Loop *, 8 > LoopVectorTy
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
iterator_range< bf_iterator< T > > breadth_first(const T &G)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
iterator_range< df_iterator< T > > depth_first(const T &G)
@ End
Run the machine verifier once, at the end of the pipeline.
A special type used by analysis passes to provide an address that identifies that particular analysis...
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...