blob: 4e2cfdc4e5689a9bba4069a1c53125fe13c22035 [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"
Dan Gohmandda30cd2010-01-05 21:08:02 +000021#include "llvm/Support/Debug.h"
Owen Andersone4ad9c72007-11-27 22:47:08 +000022using namespace llvm;
23
Andrew Trickcbf24b42012-06-20 03:42:09 +000024// Explicitly instantiate methods in LoopInfoImpl.h for MI-level Loops.
25template class llvm::LoopBase<MachineBasicBlock, MachineLoop>;
26template class llvm::LoopInfoBase<MachineBasicBlock, MachineLoop>;
Owen Andersone4ad9c72007-11-27 22:47:08 +000027
Chris Lattner19033bf2008-01-05 23:29:51 +000028char MachineLoopInfo::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000029INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
30 "Machine Natural Loop Construction", true, true)
31INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
32INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
Owen Andersonce665bd2010-10-07 22:25:06 +000033 "Machine Natural Loop Construction", true, true)
Bill Wendling67d65bb2008-01-04 20:54:55 +000034
Owen Anderson90c579d2010-08-06 18:33:48 +000035char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
Owen Andersone4ad9c72007-11-27 22:47:08 +000036
37bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
38 releaseMemory();
Andrew Trickc9b1e252012-06-26 04:11:38 +000039 LI.Analyze(getAnalysis<MachineDominatorTree>().getBase());
Owen Andersone4ad9c72007-11-27 22:47:08 +000040 return false;
41}
42
43void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.setPreservesAll();
45 AU.addRequired<MachineDominatorTree>();
Dan Gohmanad2afc22009-07-31 18:16:33 +000046 MachineFunctionPass::getAnalysisUsage(AU);
Owen Andersone4ad9c72007-11-27 22:47:08 +000047}
Dan Gohman81b16a32009-10-20 04:16:37 +000048
49MachineBasicBlock *MachineLoop::getTopBlock() {
50 MachineBasicBlock *TopMBB = getHeader();
51 MachineFunction::iterator Begin = TopMBB->getParent()->begin();
52 if (TopMBB != Begin) {
53 MachineBasicBlock *PriorMBB = prior(MachineFunction::iterator(TopMBB));
54 while (contains(PriorMBB)) {
55 TopMBB = PriorMBB;
56 if (TopMBB == Begin) break;
57 PriorMBB = prior(MachineFunction::iterator(TopMBB));
58 }
59 }
60 return TopMBB;
61}
62
63MachineBasicBlock *MachineLoop::getBottomBlock() {
64 MachineBasicBlock *BotMBB = getHeader();
65 MachineFunction::iterator End = BotMBB->getParent()->end();
66 if (BotMBB != prior(End)) {
Chris Lattner7896c9f2009-12-03 00:50:42 +000067 MachineBasicBlock *NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
Dan Gohman81b16a32009-10-20 04:16:37 +000068 while (contains(NextMBB)) {
69 BotMBB = NextMBB;
Chris Lattner7896c9f2009-12-03 00:50:42 +000070 if (BotMBB == llvm::next(MachineFunction::iterator(BotMBB))) break;
71 NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
Dan Gohman81b16a32009-10-20 04:16:37 +000072 }
73 }
74 return BotMBB;
75}
Dan Gohmandda30cd2010-01-05 21:08:02 +000076
Manman Renb720be62012-09-11 22:23:19 +000077#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohmandda30cd2010-01-05 21:08:02 +000078void MachineLoop::dump() const {
79 print(dbgs());
80}
Manman Ren77e300e2012-09-06 19:06:06 +000081#endif