LoopNestAnalysis.cpp 17 KB

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