1//===- LoopNestAnalysis.cpp - Loop Nest Analysis --------------------------==//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//===----------------------------------------------------------------------===//
10/// The implementation for the loop nest analysis.
12//===----------------------------------------------------------------------===//
21 #define DEBUG_TYPE "loopnest"
26/// Determine whether the loops structure violates basic requirements for
28/// - the inner loop should be the outer loop's only child
29/// - the outer loop header should 'flow' into the inner loop preheader
30/// or jump around the inner loop to the outer loop latch
31/// - if the inner loop latch exits the inner loop, it should 'flow' into
32/// the outer loop latch.
33/// Returns true if the loop structure satisfies the basic requirements and
38//===----------------------------------------------------------------------===//
39// LoopNest implementation
49 return std::make_unique<LoopNest>(Root, SE);
55 assert(Latch &&
"Expecting a valid loop latch");
59 "Expecting loop latch terminator to be a branch instruction");
64 dbgs() <<
"Outer loop latch compare instruction: " << *OuterLoopLatchCmp
67 return OuterLoopLatchCmp;
78 dbgs() <<
"Inner loop guard compare instruction: " << *InnerLoopGuardCmp
81 return InnerLoopGuardCmp;
85 const CmpInst *InnerLoopGuardCmp,
86 const CmpInst *OuterLoopLatchCmp,
87 std::optional<Loop::LoopBounds> OuterLoopLB) {
93 // The only binary instruction allowed is the outer loop step instruction,
94 // the only comparison instructions allowed are the inner loop guard
95 // compare instruction and the outer loop latch compare instruction.
97 (
isa<CmpInst>(
I) && &
I != OuterLoopLatchCmp && &
I != InnerLoopGuardCmp)) {
105 return (analyzeLoopNestForPerfectNest(OuterLoop, InnerLoop, SE) ==
109LoopNest::LoopNestEnum LoopNest::analyzeLoopNestForPerfectNest(
115 <<
"' and '" << InnerLoop.
getName()
116 <<
"' are perfectly nested.\n");
118 // Determine whether the loops structure satisfies the following requirements:
119 // - the inner loop should be the outer loop's only child
120 // - the outer loop header should 'flow' into the inner loop preheader
121 // or jump around the inner loop to the outer loop latch
122 // - if the inner loop latch exits the inner loop, it should 'flow' into
123 // the outer loop latch.
125 LLVM_DEBUG(
dbgs() <<
"Not perfectly nested: invalid loop structure.\n");
126 return InvalidLoopStructure;
129 // Bail out if we cannot retrieve the outer loop bounds.
130 auto OuterLoopLB = OuterLoop.
getBounds(SE);
131 if (OuterLoopLB == std::nullopt) {
133 << OuterLoop <<
"\n";);
134 return OuterLoopLowerBoundUnknown;
140 // Determine whether instructions in a basic block are one of:
141 // - the inner loop guard comparison
142 // - the outer loop latch comparison
143 // - the outer loop induction variable increment
144 // - a phi node, a cast or a branch
145 auto containsOnlySafeInstructions = [&](
const BasicBlock &BB) {
148 OuterLoopLatchCmp, OuterLoopLB);
151 dbgs() <<
"Instruction: " <<
I <<
"\nin basic block:" << BB
159 // Check the code surrounding the inner loop for instructions that are deemed
165 if (!containsOnlySafeInstructions(*OuterLoopHeader) ||
166 !containsOnlySafeInstructions(*OuterLoopLatch) ||
167 (InnerLoopPreHeader != OuterLoopHeader &&
168 !containsOnlySafeInstructions(*InnerLoopPreHeader)) ||
169 !containsOnlySafeInstructions(*InnerLoop.
getExitBlock())) {
170 LLVM_DEBUG(
dbgs() <<
"Not perfectly nested: code surrounding inner loop is "
172 return ImperfectLoopNest;
176 << InnerLoop.
getName() <<
"' are perfectly nested.\n");
178 return PerfectLoopNest;
184 switch (analyzeLoopNestForPerfectNest(OuterLoop, InnerLoop, SE)) {
185 case PerfectLoopNest:
187 "instruction vector. \n";);
190 case InvalidLoopStructure:
191 LLVM_DEBUG(
dbgs() <<
"Not perfectly nested: invalid loop structure. "
192 "Instruction vector is empty.\n";);
195 case OuterLoopLowerBoundUnknown:
197 << OuterLoop <<
"\nInstruction vector is empty.\n";);
200 case ImperfectLoopNest:
204 // Identify the outer loop latch comparison instruction.
205 auto OuterLoopLB = OuterLoop.
getBounds(SE);
210 auto GetUnsafeInstructions = [&](
const BasicBlock &BB) {
216 dbgs() <<
"Instruction: " <<
I <<
"\nin basic block:" << BB
223 // Check the code surrounding the inner loop for instructions that are deemed
230 GetUnsafeInstructions(*OuterLoopHeader);
231 GetUnsafeInstructions(*OuterLoopLatch);
232 GetUnsafeInstructions(*InnerLoopExitBlock);
234 if (InnerLoopPreHeader != OuterLoopHeader) {
235 GetUnsafeInstructions(*InnerLoopPreHeader);
246 if (PerfectNest.
empty())
249 auto &SubLoops = L->getSubLoops();
262 LLVM_DEBUG(
dbgs() <<
"Get maximum perfect depth of loop nest rooted by loop '"
265 const Loop *CurrentLoop = &Root;
266 const auto *SubLoops = &CurrentLoop->
getSubLoops();
267 unsigned CurrentDepth = 1;
269 while (SubLoops->size() == 1) {
270 const Loop *InnerLoop = SubLoops->front();
273 dbgs() <<
"Not a perfect nest: loop '" << CurrentLoop->
getName()
274 <<
"' is not perfectly nested with loop '"
275 << InnerLoop->
getName() <<
"'\n";
280 CurrentLoop = InnerLoop;
290 bool CheckUniquePred) {
291 assert(From &&
"Expecting valid From");
292 assert(End &&
"Expecting valid End");
298 return (BB->size() == 1);
301 // Visited is used to avoid running into an infinite loop.
305 while (BB && BB != End && IsEmpty(BB) && !Visited.
count(BB) &&
312 return (BB == End) ? *End : *PredBB;
317 // The inner loop must be the only outer loop's child.
322 // We expect loops in normal form which have a preheader, header, latch...
332 // We expect rotated loops. The inner loop should have a single exit block.
337 // Returns whether the block `ExitBlock` contains at least one LCSSA Phi node.
338 auto ContainsLCSSAPhi = [](
const BasicBlock &ExitBlock) {
340 return PN.getNumIncomingValues() == 1;
344 // Returns whether the block `BB` qualifies for being an extra Phi block. The
345 // extra Phi block is the additional block inserted after the exit block of an
346 // "guarded" inner loop which contains "only" Phi nodes corresponding to the
347 // LCSSA Phi nodes in the exit block.
348 auto IsExtraPhiBlock = [&](
const BasicBlock &BB) {
349 return &*BB.getFirstNonPHIIt() == BB.getTerminator() &&
351 return all_of(PN.blocks(), [&](const BasicBlock *IncomingBlock) {
352 return IncomingBlock == InnerLoopExit ||
353 IncomingBlock == OuterLoopHeader;
359 // Ensure the only branch that may exist between the loops is the inner loop
361 if (OuterLoopHeader != InnerLoopPreHeader) {
365 // no conditional branch present
366 if (&SingleSucc != InnerLoopPreHeader) {
372 bool InnerLoopExitContainsLCSSA = ContainsLCSSAPhi(*InnerLoopExit);
374 // The successors of the inner loop guard should be the inner loop
375 // preheader or the outer loop latch possibly through empty blocks.
377 const BasicBlock *PotentialInnerPreHeader = Succ;
380 // Ensure the inner loop guard successor is empty before skipping
382 if (Succ->size() == 1) {
383 PotentialInnerPreHeader =
385 PotentialOuterLatch =
389 if (PotentialInnerPreHeader == InnerLoopPreHeader)
391 if (PotentialOuterLatch == OuterLoopLatch)
394 // If `InnerLoopExit` contains LCSSA Phi instructions, additional block
395 // may be inserted before the `OuterLoopLatch` to which `BI` jumps. The
396 // loops are still considered perfectly nested if the extra block only
397 // contains Phi instructions from InnerLoopExit and OuterLoopHeader.
398 if (InnerLoopExitContainsLCSSA && IsExtraPhiBlock(*Succ) &&
399 Succ->getSingleSuccessor() == OuterLoopLatch) {
400 // Points to the extra block so that we can reference it later in the
401 // final check. We can also conclude that the inner loop is
402 // guarded and there exists LCSSA Phi node in the exit block later if
403 // we see a non-null `ExtraPhiBlock`.
404 ExtraPhiBlock = Succ;
409 dbgs() <<
"Inner loop guard successor " << Succ->getName()
410 <<
" doesn't lead to inner loop preheader or "
411 "outer loop latch.\n";
418 // Ensure the inner loop exit block lead to the outer loop latch possibly
419 // through empty blocks.
420 if ((!ExtraPhiBlock ||
422 ExtraPhiBlock) != ExtraPhiBlock) &&
424 OuterLoopLatch) != OuterLoopLatch)) {
427 dbgs() <<
"Inner loop exit block " << *InnerLoopExit
428 <<
" does not directly lead to the outer loop latch.\n";);
447 OS << L->getName() <<
" ";
453//===----------------------------------------------------------------------===//
454// LoopNestPrinterPass implementation
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 if the block is well formed or null if the block is not well forme...
Conditional or Unconditional Branch instruction.
iterator_range< succ_op_iterator > successors()
bool isConditional() const
Value * getCondition() const
This class is the base class for the comparison instructions.
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.
std::optional< LoopBounds > getBounds(ScalarEvolution &SE) const
Return the struct LoopBounds collected if all struct members are found, else std::nullopt.
BranchInst * getLoopGuardBranch() const
Return the loop guard branch, if it exists.
StringRef getName() const
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)
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...