blob: d561a5bb8c998cc14e454bb2edeec48a725f3340 [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
12// the loops identified may actually be several natural loops that share the
13// same header node... not just a single natural loop.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/CodeGen/MachineLoopInfo.h"
18#include "llvm/CodeGen/MachineDominators.h"
Bill Wendling67d65bb2008-01-04 20:54:55 +000019#include "llvm/CodeGen/Passes.h"
Owen Andersone4ad9c72007-11-27 22:47:08 +000020using namespace llvm;
21
John McCallf32616e2009-12-16 00:13:24 +000022namespace llvm {
Dan Gohmanc8d76d52009-07-13 21:51:15 +000023#define MLB class LoopBase<MachineBasicBlock, MachineLoop>
24TEMPLATE_INSTANTIATION(MLB);
25#undef MLB
26#define MLIB class LoopInfoBase<MachineBasicBlock, MachineLoop>
27TEMPLATE_INSTANTIATION(MLIB);
28#undef MLIB
John McCallf32616e2009-12-16 00:13:24 +000029}
Owen Andersone4ad9c72007-11-27 22:47:08 +000030
Chris Lattner19033bf2008-01-05 23:29:51 +000031char MachineLoopInfo::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000032static RegisterPass<MachineLoopInfo>
33X("machine-loops", "Machine Natural Loop Construction", true);
Bill Wendling67d65bb2008-01-04 20:54:55 +000034
Dan Gohman6ddba2b2008-05-13 02:05:11 +000035const PassInfo *const llvm::MachineLoopInfoID = &X;
Owen Andersone4ad9c72007-11-27 22:47:08 +000036
37bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
38 releaseMemory();
Dan Gohman9d59d9f2009-06-27 21:22:48 +000039 LI.Calculate(getAnalysis<MachineDominatorTree>().getBase()); // Update
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}