Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 1 | //===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the MachineLoopInfo class that is used to identify natural |
| 11 | // loops and determine the loop depth of various nodes of the CFG. Note that |
Andrew Trick | cbf24b4 | 2012-06-20 03:42:09 +0000 | [diff] [blame] | 12 | // the loops identified may actually be several natural loops that share the |
Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 13 | // same header node... not just a single natural loop. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopInfoImpl.h" |
Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineDominators.h" |
Bill Wendling | 67d65bb | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/Passes.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 21 | #include "llvm/Config/llvm-config.h" |
Dan Gohman | dda30cd | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 1bfcd1f | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Andrew Trick | cbf24b4 | 2012-06-20 03:42:09 +0000 | [diff] [blame] | 26 | // Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops. |
| 27 | template class llvm::LoopBase<MachineBasicBlock, MachineLoop>; |
| 28 | template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>; |
Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 19033bf | 2008-01-05 23:29:51 +0000 | [diff] [blame] | 30 | char MachineLoopInfo::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 31 | INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops", |
| 32 | "Machine Natural Loop Construction", true, true) |
| 33 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 34 | INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 35 | "Machine Natural Loop Construction", true, true) |
Bill Wendling | 67d65bb | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 36 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 37 | char &llvm::MachineLoopInfoID = MachineLoopInfo::ID; |
Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 38 | |
| 39 | bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) { |
| 40 | releaseMemory(); |
Cong Hou | 204b590 | 2015-07-16 18:23:57 +0000 | [diff] [blame] | 41 | LI.analyze(getAnalysis<MachineDominatorTree>().getBase()); |
Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
| 45 | void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 46 | AU.setPreservesAll(); |
| 47 | AU.addRequired<MachineDominatorTree>(); |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 48 | MachineFunctionPass::getAnalysisUsage(AU); |
Owen Anderson | e4ad9c7 | 2007-11-27 22:47:08 +0000 | [diff] [blame] | 49 | } |
Dan Gohman | 81b16a3 | 2009-10-20 04:16:37 +0000 | [diff] [blame] | 50 | |
| 51 | MachineBasicBlock *MachineLoop::getTopBlock() { |
| 52 | MachineBasicBlock *TopMBB = getHeader(); |
| 53 | MachineFunction::iterator Begin = TopMBB->getParent()->begin(); |
Duncan P. N. Exon Smith | 8de6150 | 2016-02-21 20:39:50 +0000 | [diff] [blame] | 54 | if (TopMBB->getIterator() != Begin) { |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 55 | MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator()); |
Dan Gohman | 81b16a3 | 2009-10-20 04:16:37 +0000 | [diff] [blame] | 56 | while (contains(PriorMBB)) { |
| 57 | TopMBB = PriorMBB; |
Duncan P. N. Exon Smith | 8de6150 | 2016-02-21 20:39:50 +0000 | [diff] [blame] | 58 | if (TopMBB->getIterator() == Begin) |
| 59 | break; |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 60 | PriorMBB = &*std::prev(TopMBB->getIterator()); |
Dan Gohman | 81b16a3 | 2009-10-20 04:16:37 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | return TopMBB; |
| 64 | } |
| 65 | |
| 66 | MachineBasicBlock *MachineLoop::getBottomBlock() { |
| 67 | MachineBasicBlock *BotMBB = getHeader(); |
| 68 | MachineFunction::iterator End = BotMBB->getParent()->end(); |
Duncan P. N. Exon Smith | 8de6150 | 2016-02-21 20:39:50 +0000 | [diff] [blame] | 69 | if (BotMBB->getIterator() != std::prev(End)) { |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 70 | MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator()); |
Dan Gohman | 81b16a3 | 2009-10-20 04:16:37 +0000 | [diff] [blame] | 71 | while (contains(NextMBB)) { |
| 72 | BotMBB = NextMBB; |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 73 | if (BotMBB == &*std::next(BotMBB->getIterator())) |
| 74 | break; |
| 75 | NextMBB = &*std::next(BotMBB->getIterator()); |
Dan Gohman | 81b16a3 | 2009-10-20 04:16:37 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | return BotMBB; |
| 79 | } |
Dan Gohman | dda30cd | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 80 | |
Sjoerd Meijer | 47a3de7 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 81 | MachineBasicBlock *MachineLoop::findLoopControlBlock() { |
| 82 | if (MachineBasicBlock *Latch = getLoopLatch()) { |
| 83 | if (isLoopExiting(Latch)) |
| 84 | return Latch; |
| 85 | else |
| 86 | return getExitingBlock(); |
| 87 | } |
| 88 | return nullptr; |
| 89 | } |
| 90 | |
Adam Nemet | 19925fc | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 91 | DebugLoc MachineLoop::getStartLoc() const { |
| 92 | // Try the pre-header first. |
| 93 | if (MachineBasicBlock *PHeadMBB = getLoopPreheader()) |
| 94 | if (const BasicBlock *PHeadBB = PHeadMBB->getBasicBlock()) |
| 95 | if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc()) |
| 96 | return DL; |
| 97 | |
| 98 | // If we have no pre-header or there are no instructions with debug |
| 99 | // info in it, try the header. |
| 100 | if (MachineBasicBlock *HeadMBB = getHeader()) |
| 101 | if (const BasicBlock *HeadBB = HeadMBB->getBasicBlock()) |
| 102 | return HeadBB->getTerminator()->getDebugLoc(); |
| 103 | |
| 104 | return DebugLoc(); |
| 105 | } |
| 106 | |
Sjoerd Meijer | 47a3de7 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 107 | MachineBasicBlock * |
| 108 | MachineLoopInfo::findLoopPreheader(MachineLoop *L, |
| 109 | bool SpeculativePreheader) const { |
| 110 | if (MachineBasicBlock *PB = L->getLoopPreheader()) |
| 111 | return PB; |
| 112 | |
| 113 | if (!SpeculativePreheader) |
| 114 | return nullptr; |
| 115 | |
| 116 | MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch(); |
| 117 | if (HB->pred_size() != 2 || HB->hasAddressTaken()) |
| 118 | return nullptr; |
| 119 | // Find the predecessor of the header that is not the latch block. |
| 120 | MachineBasicBlock *Preheader = nullptr; |
| 121 | for (MachineBasicBlock *P : HB->predecessors()) { |
| 122 | if (P == LB) |
| 123 | continue; |
| 124 | // Sanity. |
| 125 | if (Preheader) |
| 126 | return nullptr; |
| 127 | Preheader = P; |
| 128 | } |
| 129 | |
| 130 | // Check if the preheader candidate is a successor of any other loop |
| 131 | // headers. We want to avoid having two loop setups in the same block. |
| 132 | for (MachineBasicBlock *S : Preheader->successors()) { |
| 133 | if (S == HB) |
| 134 | continue; |
| 135 | MachineLoop *T = getLoopFor(S); |
| 136 | if (T && T->getHeader() == S) |
| 137 | return nullptr; |
| 138 | } |
| 139 | return Preheader; |
| 140 | } |
| 141 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 142 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 143 | LLVM_DUMP_METHOD void MachineLoop::dump() const { |
Dan Gohman | dda30cd | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 144 | print(dbgs()); |
| 145 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 146 | #endif |