blob: 117c390f032f4c5bc2e890538ecaf41ee541f4cf [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();
53 if (TopMBB != 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;
57 if (TopMBB == Begin) break;
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +000058 PriorMBB = &*std::prev(TopMBB->getIterator());
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)) {
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +000068 MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator());
Dan Gohmand383c2f2009-10-20 04:16:37 +000069 while (contains(NextMBB)) {
70 BotMBB = NextMBB;
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +000071 if (BotMBB == &*std::next(BotMBB->getIterator()))
72 break;
73 NextMBB = &*std::next(BotMBB->getIterator());
Dan Gohmand383c2f2009-10-20 04:16:37 +000074 }
75 }
76 return BotMBB;
77}
Dan Gohmanc3f21372010-01-05 21:08:02 +000078
Manman Ren19f49ac2012-09-11 22:23:19 +000079#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000080LLVM_DUMP_METHOD void MachineLoop::dump() const {
Dan Gohmanc3f21372010-01-05 21:08:02 +000081 print(dbgs());
82}
Manman Ren742534c2012-09-06 19:06:06 +000083#endif