blob: fdeaf7b7116117dd36ec21527b058a1fd032aa8c [file] [log] [blame]
Owen Anderson30767b12007-11-27 22:47:08 +00001//===- MachineLoopInfo.cpp - Natural Loop Calculator ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Anderson30767b12007-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 Trickcda51d42012-06-20 03:42:09 +000012// the loops identified may actually be several natural loops that share the
Owen Anderson30767b12007-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 Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Analysis/LoopInfoImpl.h"
Owen Anderson30767b12007-11-27 22:47:08 +000019#include "llvm/CodeGen/MachineDominators.h"
Bill Wendling0c209432008-01-04 20:54:55 +000020#include "llvm/CodeGen/Passes.h"
Dan Gohmanc3f21372010-01-05 21:08:02 +000021#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000022#include "llvm/Support/raw_ostream.h"
Owen Anderson30767b12007-11-27 22:47:08 +000023using namespace llvm;
24
Andrew Trickcda51d42012-06-20 03:42:09 +000025// Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
26template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
27template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
Owen Anderson30767b12007-11-27 22:47:08 +000028
Chris Lattner7eac7142008-01-05 23:29:51 +000029char MachineLoopInfo::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000030INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
31 "Machine Natural Loop Construction", true, true)
32INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
33INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
Owen Andersondf7a4f22010-10-07 22:25:06 +000034 "Machine Natural Loop Construction", true, true)
Bill Wendling0c209432008-01-04 20:54:55 +000035
Owen Andersona7aed182010-08-06 18:33:48 +000036char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
Owen Anderson30767b12007-11-27 22:47:08 +000037
38bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
39 releaseMemory();
Cong Houd2c1d912015-07-16 18:23:57 +000040 LI.analyze(getAnalysis<MachineDominatorTree>().getBase());
Owen Anderson30767b12007-11-27 22:47:08 +000041 return false;
42}
43
44void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
46 AU.addRequired<MachineDominatorTree>();
Dan Gohman5ea74d52009-07-31 18:16:33 +000047 MachineFunctionPass::getAnalysisUsage(AU);
Owen Anderson30767b12007-11-27 22:47:08 +000048}
Dan Gohmand383c2f2009-10-20 04:16:37 +000049
50MachineBasicBlock *MachineLoop::getTopBlock() {
51 MachineBasicBlock *TopMBB = getHeader();
52 MachineFunction::iterator Begin = TopMBB->getParent()->begin();
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +000053 if (TopMBB->getIterator() != Begin) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +000054 MachineBasicBlock *PriorMBB = &*std::prev(TopMBB->getIterator());
Dan Gohmand383c2f2009-10-20 04:16:37 +000055 while (contains(PriorMBB)) {
56 TopMBB = PriorMBB;
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +000057 if (TopMBB->getIterator() == Begin)
58 break;
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +000059 PriorMBB = &*std::prev(TopMBB->getIterator());
Dan Gohmand383c2f2009-10-20 04:16:37 +000060 }
61 }
62 return TopMBB;
63}
64
65MachineBasicBlock *MachineLoop::getBottomBlock() {
66 MachineBasicBlock *BotMBB = getHeader();
67 MachineFunction::iterator End = BotMBB->getParent()->end();
Duncan P. N. Exon Smithe9bc5792016-02-21 20:39:50 +000068 if (BotMBB->getIterator() != std::prev(End)) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +000069 MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
Dan Gohmand383c2f2009-10-20 04:16:37 +000070 while (contains(NextMBB)) {
71 BotMBB = NextMBB;
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +000072 if (BotMBB == &*std::next(BotMBB->getIterator()))
73 break;
74 NextMBB = &*std::next(BotMBB->getIterator());
Dan Gohmand383c2f2009-10-20 04:16:37 +000075 }
76 }
77 return BotMBB;
78}
Dan Gohmanc3f21372010-01-05 21:08:02 +000079
Sjoerd Meijer58156712016-08-15 08:22:42 +000080MachineBasicBlock *MachineLoop::findLoopControlBlock() {
81 if (MachineBasicBlock *Latch = getLoopLatch()) {
82 if (isLoopExiting(Latch))
83 return Latch;
84 else
85 return getExitingBlock();
86 }
87 return nullptr;
88}
89
90MachineBasicBlock *
91MachineLoopInfo::findLoopPreheader(MachineLoop *L,
92 bool SpeculativePreheader) const {
93 if (MachineBasicBlock *PB = L->getLoopPreheader())
94 return PB;
95
96 if (!SpeculativePreheader)
97 return nullptr;
98
99 MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
100 if (HB->pred_size() != 2 || HB->hasAddressTaken())
101 return nullptr;
102 // Find the predecessor of the header that is not the latch block.
103 MachineBasicBlock *Preheader = nullptr;
104 for (MachineBasicBlock *P : HB->predecessors()) {
105 if (P == LB)
106 continue;
107 // Sanity.
108 if (Preheader)
109 return nullptr;
110 Preheader = P;
111 }
112
113 // Check if the preheader candidate is a successor of any other loop
114 // headers. We want to avoid having two loop setups in the same block.
115 for (MachineBasicBlock *S : Preheader->successors()) {
116 if (S == HB)
117 continue;
118 MachineLoop *T = getLoopFor(S);
119 if (T && T->getHeader() == S)
120 return nullptr;
121 }
122 return Preheader;
123}
124
Manman Ren19f49ac2012-09-11 22:23:19 +0000125#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000126LLVM_DUMP_METHOD void MachineLoop::dump() const {
Dan Gohmanc3f21372010-01-05 21:08:02 +0000127 print(dbgs());
128}
Manman Ren742534c2012-09-06 19:06:06 +0000129#endif