1 //===- LoopNestAnalysis.cpp - Loop Nest Analysis --------------------------==//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// The implementation for the loop nest analysis.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Analysis/LoopNestAnalysis.h"
15 #include "llvm/ADT/BreadthFirstIterator.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/Analysis/PostDominators.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "loopnest"
23 #ifndef NDEBUG
24 static const char *VerboseDebug = DEBUG_TYPE "-verbose";
25 #endif
26 
27 //===----------------------------------------------------------------------===//
28 // LoopNest implementation
29 //
30 
31 LoopNest::LoopNest(Loop &Root, ScalarEvolution &SE)
32     : MaxPerfectDepth(getMaxPerfectDepth(Root, SE)) {
33   append_range(Loops, breadth_first(&Root));
34 }
35 
36 std::unique_ptr<LoopNest> LoopNest::getLoopNest(Loop &Root,
37                                                 ScalarEvolution &SE) {
38   return std::make_unique<LoopNest>(Root, SE);
39 }
40 
41 bool LoopNest::arePerfectlyNested(const Loop &OuterLoop, const Loop &InnerLoop,
42                                   ScalarEvolution &SE) {
43   assert(!OuterLoop.isInnermost() && "Outer loop should have subloops");
44   assert(!InnerLoop.isOutermost() && "Inner loop should have a parent");
45   LLVM_DEBUG(dbgs() << "Checking whether loop '" << OuterLoop.getName()
46                     << "' and '" << InnerLoop.getName()
47                     << "' are perfectly nested.\n");
48 
49   // Determine whether the loops structure satisfies the following requirements:
50   //  - the inner loop should be the outer loop's only child
51   //  - the outer loop header should 'flow' into the inner loop preheader
52   //    or jump around the inner loop to the outer loop latch
53   //  - if the inner loop latch exits the inner loop, it should 'flow' into
54   //    the outer loop latch.
55   if (!checkLoopsStructure(OuterLoop, InnerLoop, SE)) {
56     LLVM_DEBUG(dbgs() << "Not perfectly nested: invalid loop structure.\n");
57     return false;
58   }
59 
60   // Bail out if we cannot retrieve the outer loop bounds.
61   auto OuterLoopLB = OuterLoop.getBounds(SE);
62   if (OuterLoopLB == None) {
63     LLVM_DEBUG(dbgs() << "Cannot compute loop bounds of OuterLoop: "
64                       << OuterLoop << "\n";);
65     return false;
66   }
67 
68   // Identify the outer loop latch comparison instruction.
69   const BasicBlock *Latch = OuterLoop.getLoopLatch();
70   assert(Latch && "Expecting a valid loop latch");
71   const BranchInst *BI = dyn_cast<BranchInst>(Latch->getTerminator());
72   assert(BI && BI->isConditional() &&
73          "Expecting loop latch terminator to be a branch instruction");
74 
75   const CmpInst *OuterLoopLatchCmp = dyn_cast<CmpInst>(BI->getCondition());
76   DEBUG_WITH_TYPE(
77       VerboseDebug, if (OuterLoopLatchCmp) {
78         dbgs() << "Outer loop latch compare instruction: " << *OuterLoopLatchCmp
79                << "\n";
80       });
81 
82   // Identify the inner loop guard instruction.
83   BranchInst *InnerGuard = InnerLoop.getLoopGuardBranch();
84   const CmpInst *InnerLoopGuardCmp =
85       (InnerGuard) ? dyn_cast<CmpInst>(InnerGuard->getCondition()) : nullptr;
86 
87   DEBUG_WITH_TYPE(
88       VerboseDebug, if (InnerLoopGuardCmp) {
89         dbgs() << "Inner loop guard compare instruction: " << *InnerLoopGuardCmp
90                << "\n";
91       });
92 
93   // Determine whether instructions in a basic block are one of:
94   //  - the inner loop guard comparison
95   //  - the outer loop latch comparison
96   //  - the outer loop induction variable increment
97   //  - a phi node, a cast or a branch
98   auto containsOnlySafeInstructions = [&](const BasicBlock &BB) {
99     return llvm::all_of(BB, [&](const Instruction &I) {
100       bool isAllowed = isSafeToSpeculativelyExecute(&I) || isa<PHINode>(I) ||
101                        isa<BranchInst>(I);
102       if (!isAllowed) {
103         DEBUG_WITH_TYPE(VerboseDebug, {
104           dbgs() << "Instruction: " << I << "\nin basic block: " << BB
105                  << " is considered unsafe.\n";
106         });
107         return false;
108       }
109 
110       // The only binary instruction allowed is the outer loop step instruction,
111       // the only comparison instructions allowed are the inner loop guard
112       // compare instruction and the outer loop latch compare instruction.
113       if ((isa<BinaryOperator>(I) && &I != &OuterLoopLB->getStepInst()) ||
114           (isa<CmpInst>(I) && &I != OuterLoopLatchCmp &&
115            &I != InnerLoopGuardCmp)) {
116         DEBUG_WITH_TYPE(VerboseDebug, {
117           dbgs() << "Instruction: " << I << "\nin basic block:" << BB
118                  << "is unsafe.\n";
119         });
120         return false;
121       }
122       return true;
123     });
124   };
125 
126   // Check the code surrounding the inner loop for instructions that are deemed
127   // unsafe.
128   const BasicBlock *OuterLoopHeader = OuterLoop.getHeader();
129   const BasicBlock *OuterLoopLatch = OuterLoop.getLoopLatch();
130   const BasicBlock *InnerLoopPreHeader = InnerLoop.getLoopPreheader();
131 
132   if (!containsOnlySafeInstructions(*OuterLoopHeader) ||
133       !containsOnlySafeInstructions(*OuterLoopLatch) ||
134       (InnerLoopPreHeader != OuterLoopHeader &&
135        !containsOnlySafeInstructions(*InnerLoopPreHeader)) ||
136       !containsOnlySafeInstructions(*InnerLoop.getExitBlock())) {
137     LLVM_DEBUG(dbgs() << "Not perfectly nested: code surrounding inner loop is "
138                          "unsafe\n";);
139     return false;
140   }
141 
142   LLVM_DEBUG(dbgs() << "Loop '" << OuterLoop.getName() << "' and '"
143                     << InnerLoop.getName() << "' are perfectly nested.\n");
144 
145   return true;
146 }
147 
148 SmallVector<LoopVectorTy, 4>
149 LoopNest::getPerfectLoops(ScalarEvolution &SE) const {
150   SmallVector<LoopVectorTy, 4> LV;
151   LoopVectorTy PerfectNest;
152 
153   for (Loop *L : depth_first(const_cast<Loop *>(Loops.front()))) {
154     if (PerfectNest.empty())
155       PerfectNest.push_back(L);
156 
157     auto &SubLoops = L->getSubLoops();
158     if (SubLoops.size() == 1 && arePerfectlyNested(*L, *SubLoops.front(), SE)) {
159       PerfectNest.push_back(SubLoops.front());
160     } else {
161       LV.push_back(PerfectNest);
162       PerfectNest.clear();
163     }
164   }
165 
166   return LV;
167 }
168 
169 unsigned LoopNest::getMaxPerfectDepth(const Loop &Root, ScalarEvolution &SE) {
170   LLVM_DEBUG(dbgs() << "Get maximum perfect depth of loop nest rooted by loop '"
171                     << Root.getName() << "'\n");
172 
173   const Loop *CurrentLoop = &Root;
174   const auto *SubLoops = &CurrentLoop->getSubLoops();
175   unsigned CurrentDepth = 1;
176 
177   while (SubLoops->size() == 1) {
178     const Loop *InnerLoop = SubLoops->front();
179     if (!arePerfectlyNested(*CurrentLoop, *InnerLoop, SE)) {
180       LLVM_DEBUG({
181         dbgs() << "Not a perfect nest: loop '" << CurrentLoop->getName()
182                << "' is not perfectly nested with loop '"
183                << InnerLoop->getName() << "'\n";
184       });
185       break;
186     }
187 
188     CurrentLoop = InnerLoop;
189     SubLoops = &CurrentLoop->getSubLoops();
190     ++CurrentDepth;
191   }
192 
193   return CurrentDepth;
194 }
195 
196 const BasicBlock &LoopNest::skipEmptyBlockUntil(const BasicBlock *From,
197                                                 const BasicBlock *End) {
198   assert(From && "Expecting valid From");
199   assert(End && "Expecting valid End");
200 
201   if (From == End || !From->getUniqueSuccessor())
202     return *From;
203 
204   auto IsEmpty = [](const BasicBlock *BB) {
205     return (BB->getInstList().size() == 1);
206   };
207 
208   // Visited is used to avoid running into an infinite loop.
209   SmallPtrSet<const BasicBlock *, 4> Visited;
210   const BasicBlock *BB = From->getUniqueSuccessor();
211   const BasicBlock *PredBB = BB;
212   while (BB && BB != End && IsEmpty(BB) && !Visited.count(BB)) {
213     Visited.insert(BB);
214     PredBB = BB;
215     BB = BB->getUniqueSuccessor();
216   }
217 
218   return (BB == End) ? *End : *PredBB;
219 }
220 
221 bool LoopNest::checkLoopsStructure(const Loop &OuterLoop, const Loop &InnerLoop,
222                                    ScalarEvolution &SE) {
223   // The inner loop must be the only outer loop's child.
224   if ((OuterLoop.getSubLoops().size() != 1) ||
225       (InnerLoop.getParentLoop() != &OuterLoop))
226     return false;
227 
228   // We expect loops in normal form which have a preheader, header, latch...
229   if (!OuterLoop.isLoopSimplifyForm() || !InnerLoop.isLoopSimplifyForm())
230     return false;
231 
232   const BasicBlock *OuterLoopHeader = OuterLoop.getHeader();
233   const BasicBlock *OuterLoopLatch = OuterLoop.getLoopLatch();
234   const BasicBlock *InnerLoopPreHeader = InnerLoop.getLoopPreheader();
235   const BasicBlock *InnerLoopLatch = InnerLoop.getLoopLatch();
236   const BasicBlock *InnerLoopExit = InnerLoop.getExitBlock();
237 
238   // We expect rotated loops. The inner loop should have a single exit block.
239   if (OuterLoop.getExitingBlock() != OuterLoopLatch ||
240       InnerLoop.getExitingBlock() != InnerLoopLatch || !InnerLoopExit)
241     return false;
242 
243   // Returns whether the block `ExitBlock` contains at least one LCSSA Phi node.
244   auto ContainsLCSSAPhi = [](const BasicBlock &ExitBlock) {
245     return any_of(ExitBlock.phis(), [](const PHINode &PN) {
246       return PN.getNumIncomingValues() == 1;
247     });
248   };
249 
250   // Returns whether the block `BB` qualifies for being an extra Phi block. The
251   // extra Phi block is the additional block inserted after the exit block of an
252   // "guarded" inner loop which contains "only" Phi nodes corresponding to the
253   // LCSSA Phi nodes in the exit block.
254   auto IsExtraPhiBlock = [&](const BasicBlock &BB) {
255     return BB.getFirstNonPHI() == BB.getTerminator() &&
256            all_of(BB.phis(), [&](const PHINode &PN) {
257              return all_of(PN.blocks(), [&](const BasicBlock *IncomingBlock) {
258                return IncomingBlock == InnerLoopExit ||
259                       IncomingBlock == OuterLoopHeader;
260              });
261            });
262   };
263 
264   const BasicBlock *ExtraPhiBlock = nullptr;
265   // Ensure the only branch that may exist between the loops is the inner loop
266   // guard.
267   if (OuterLoopHeader != InnerLoopPreHeader) {
268     const BasicBlock &SingleSucc =
269         LoopNest::skipEmptyBlockUntil(OuterLoopHeader, InnerLoopPreHeader);
270 
271     // no conditional branch present
272     if (&SingleSucc != InnerLoopPreHeader) {
273       const BranchInst *BI = dyn_cast<BranchInst>(SingleSucc.getTerminator());
274 
275       if (!BI || BI != InnerLoop.getLoopGuardBranch())
276         return false;
277 
278       bool InnerLoopExitContainsLCSSA = ContainsLCSSAPhi(*InnerLoopExit);
279 
280       // The successors of the inner loop guard should be the inner loop
281       // preheader or the outer loop latch possibly through empty blocks.
282       for (const BasicBlock *Succ : BI->successors()) {
283         const BasicBlock *PotentialInnerPreHeader = Succ;
284         const BasicBlock *PotentialOuterLatch = Succ;
285 
286         // Ensure the inner loop guard successor is empty before skipping
287         // blocks.
288         if (Succ->getInstList().size() == 1) {
289           PotentialInnerPreHeader =
290               &LoopNest::skipEmptyBlockUntil(Succ, InnerLoopPreHeader);
291           PotentialOuterLatch =
292               &LoopNest::skipEmptyBlockUntil(Succ, OuterLoopLatch);
293         }
294 
295         if (PotentialInnerPreHeader == InnerLoopPreHeader)
296           continue;
297         if (PotentialOuterLatch == OuterLoopLatch)
298           continue;
299 
300         // If `InnerLoopExit` contains LCSSA Phi instructions, additional block
301         // may be inserted before the `OuterLoopLatch` to which `BI` jumps. The
302         // loops are still considered perfectly nested if the extra block only
303         // contains Phi instructions from InnerLoopExit and OuterLoopHeader.
304         if (InnerLoopExitContainsLCSSA && IsExtraPhiBlock(*Succ) &&
305             Succ->getSingleSuccessor() == OuterLoopLatch) {
306           // Points to the extra block so that we can reference it later in the
307           // final check. We can also conclude that the inner loop is
308           // guarded and there exists LCSSA Phi node in the exit block later if
309           // we see a non-null `ExtraPhiBlock`.
310           ExtraPhiBlock = Succ;
311           continue;
312         }
313 
314         DEBUG_WITH_TYPE(VerboseDebug, {
315           dbgs() << "Inner loop guard successor " << Succ->getName()
316                  << " doesn't lead to inner loop preheader or "
317                     "outer loop latch.\n";
318         });
319         return false;
320       }
321     }
322   }
323 
324   // Ensure the inner loop exit block lead to the outer loop latch possibly
325   // through empty blocks.
326   const BasicBlock &SuccInner =
327       LoopNest::skipEmptyBlockUntil(InnerLoop.getExitBlock(), OuterLoopLatch);
328   if (&SuccInner != OuterLoopLatch && &SuccInner != ExtraPhiBlock) {
329     DEBUG_WITH_TYPE(
330         VerboseDebug,
331         dbgs() << "Inner loop exit block " << *InnerLoopExit
332                << " does not directly lead to the outer loop latch.\n";);
333     return false;
334   }
335 
336   return true;
337 }
338 
339 AnalysisKey LoopNestAnalysis::Key;
340 
341 raw_ostream &llvm::operator<<(raw_ostream &OS, const LoopNest &LN) {
342   OS << "IsPerfect=";
343   if (LN.getMaxPerfectDepth() == LN.getNestDepth())
344     OS << "true";
345   else
346     OS << "false";
347   OS << ", Depth=" << LN.getNestDepth();
348   OS << ", OutermostLoop: " << LN.getOutermostLoop().getName();
349   OS << ", Loops: ( ";
350   for (const Loop *L : LN.getLoops())
351     OS << L->getName() << " ";
352   OS << ")";
353 
354   return OS;
355 }
356 
357 //===----------------------------------------------------------------------===//
358 // LoopNestPrinterPass implementation
359 //
360 
361 PreservedAnalyses LoopNestPrinterPass::run(Loop &L, LoopAnalysisManager &AM,
362                                            LoopStandardAnalysisResults &AR,
363                                            LPMUpdater &U) {
364   if (auto LN = LoopNest::getLoopNest(L, AR.SE))
365     OS << *LN << "\n";
366 
367   return PreservedAnalyses::all();
368 }
369