blob: 2bce59235057ed07c13462b2a43b7f2701ed3110 [file] [log] [blame]
Owen Andersone4ad9c72007-11-27 22:47:08 +00001//===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Andersone4ad9c72007-11-27 22:47:08 +00007//
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 Trickcbf24b42012-06-20 03:42:09 +000012// the loops identified may actually be several natural loops that share the
Owen Andersone4ad9c72007-11-27 22:47:08 +000013// same header node... not just a single natural loop.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/CodeGen/MachineLoopInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/Analysis/LoopInfoImpl.h"
Owen Andersone4ad9c72007-11-27 22:47:08 +000019#include "llvm/CodeGen/MachineDominators.h"
Bill Wendling67d65bb2008-01-04 20:54:55 +000020#include "llvm/CodeGen/Passes.h"
Nico Weber0f38c602018-04-30 14:59:11 +000021#include "llvm/Config/llvm-config.h"
Dan Gohmandda30cd2010-01-05 21:08:02 +000022#include "llvm/Support/Debug.h"
Benjamin Kramer1bfcd1f2015-03-23 19:32:43 +000023#include "llvm/Support/raw_ostream.h"
Owen Andersone4ad9c72007-11-27 22:47:08 +000024using namespace llvm;
25
Andrew Trickcbf24b42012-06-20 03:42:09 +000026// Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
27template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
28template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
Owen Andersone4ad9c72007-11-27 22:47:08 +000029
Chris Lattner19033bf2008-01-05 23:29:51 +000030char MachineLoopInfo::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000031INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
32 "Machine Natural Loop Construction", true, true)
33INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
34INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
Owen Andersonce665bd2010-10-07 22:25:06 +000035 "Machine Natural Loop Construction", true, true)
Bill Wendling67d65bb2008-01-04 20:54:55 +000036
Owen Anderson90c579d2010-08-06 18:33:48 +000037char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
Owen Andersone4ad9c72007-11-27 22:47:08 +000038
39bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
40 releaseMemory();
Cong Hou204b5902015-07-16 18:23:57 +000041 LI.analyze(getAnalysis<MachineDominatorTree>().getBase());
Owen Andersone4ad9c72007-11-27 22:47:08 +000042 return false;
43}
44
45void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesAll();
47 AU.addRequired<MachineDominatorTree>();
Dan Gohmanad2afc22009-07-31 18:16:33 +000048 MachineFunctionPass::getAnalysisUsage(AU);
Owen Andersone4ad9c72007-11-27 22:47:08 +000049}
Dan Gohman81b16a32009-10-20 04:16:37 +000050
51MachineBasicBlock *MachineLoop::getTopBlock() {
52 MachineBasicBlock *TopMBB = getHeader();
53 MachineFunction::iterator Begin = TopMBB->getParent()->begin();
Duncan P. N. Exon Smith8de61502016-02-21 20:39:50 +000054 if (TopMBB->getIterator() != Begin) {
Duncan P. N. Exon Smith9731c602015-10-09 19:40:45 +000055 MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
Dan Gohman81b16a32009-10-20 04:16:37 +000056 while (contains(PriorMBB)) {
57 TopMBB = PriorMBB;
Duncan P. N. Exon Smith8de61502016-02-21 20:39:50 +000058 if (TopMBB->getIterator() == Begin)
59 break;
Duncan P. N. Exon Smith9731c602015-10-09 19:40:45 +000060 PriorMBB = &*std::prev(TopMBB->getIterator());
Dan Gohman81b16a32009-10-20 04:16:37 +000061 }
62 }
63 return TopMBB;
64}
65
66MachineBasicBlock *MachineLoop::getBottomBlock() {
67 MachineBasicBlock *BotMBB = getHeader();
68 MachineFunction::iterator End = BotMBB->getParent()->end();
Duncan P. N. Exon Smith8de61502016-02-21 20:39:50 +000069 if (BotMBB->getIterator() != std::prev(End)) {
Duncan P. N. Exon Smith9731c602015-10-09 19:40:45 +000070 MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
Dan Gohman81b16a32009-10-20 04:16:37 +000071 while (contains(NextMBB)) {
72 BotMBB = NextMBB;
Duncan P. N. Exon Smith9731c602015-10-09 19:40:45 +000073 if (BotMBB == &*std::next(BotMBB->getIterator()))
74 break;
75 NextMBB = &*std::next(BotMBB->getIterator());
Dan Gohman81b16a32009-10-20 04:16:37 +000076 }
77 }
78 return BotMBB;
79}
Dan Gohmandda30cd2010-01-05 21:08:02 +000080
Sjoerd Meijer47a3de72016-08-15 08:22:42 +000081MachineBasicBlock *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 Nemet19925fc2017-01-25 23:20:33 +000091DebugLoc 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 Meijer47a3de72016-08-15 08:22:42 +0000107MachineBasicBlock *
108MachineLoopInfo::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 Ballman1d03d382017-10-15 14:32:27 +0000142#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000143LLVM_DUMP_METHOD void MachineLoop::dump() const {
Dan Gohmandda30cd2010-01-05 21:08:02 +0000144 print(dbgs());
145}
Manman Ren77e300e2012-09-06 19:06:06 +0000146#endif