blob: ce6abdd870b3e87a1d5e6491efc6edc492c1db19 [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();
Andrew Trickfb2ba3e2012-06-26 04:11:38 +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();
53 if (TopMBB != Begin) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000054 MachineBasicBlock *PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
Dan Gohmand383c2f2009-10-20 04:16:37 +000055 while (contains(PriorMBB)) {
56 TopMBB = PriorMBB;
57 if (TopMBB == Begin) break;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000058 PriorMBB = std::prev(MachineFunction::iterator(TopMBB));
Dan Gohmand383c2f2009-10-20 04:16:37 +000059 }
60 }
61 return TopMBB;
62}
63
64MachineBasicBlock *MachineLoop::getBottomBlock() {
65 MachineBasicBlock *BotMBB = getHeader();
66 MachineFunction::iterator End = BotMBB->getParent()->end();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000067 if (BotMBB != std::prev(End)) {
68 MachineBasicBlock *NextMBB = std::next(MachineFunction::iterator(BotMBB));
Dan Gohmand383c2f2009-10-20 04:16:37 +000069 while (contains(NextMBB)) {
70 BotMBB = NextMBB;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000071 if (BotMBB == std::next(MachineFunction::iterator(BotMBB))) break;
72 NextMBB = std::next(MachineFunction::iterator(BotMBB));
Dan Gohmand383c2f2009-10-20 04:16:37 +000073 }
74 }
75 return BotMBB;
76}
Dan Gohmanc3f21372010-01-05 21:08:02 +000077
Manman Ren19f49ac2012-09-11 22:23:19 +000078#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohmanc3f21372010-01-05 21:08:02 +000079void MachineLoop::dump() const {
80 print(dbgs());
81}
Manman Ren742534c2012-09-06 19:06:06 +000082#endif