blob: b7298e48eefc87bf63d8ec5989a7260087a883c7 [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"
18#include "llvm/CodeGen/MachineDominators.h"
Bill Wendling67d65bb2008-01-04 20:54:55 +000019#include "llvm/CodeGen/Passes.h"
Andrew Trickcbf24b42012-06-20 03:42:09 +000020#include "llvm/Analysis/LoopInfoImpl.h"
Andrew Trick37aa33b2012-06-20 05:23:33 +000021#include "llvm/Support/CommandLine.h"
Dan Gohmandda30cd2010-01-05 21:08:02 +000022#include "llvm/Support/Debug.h"
Owen Andersone4ad9c72007-11-27 22:47:08 +000023using namespace llvm;
24
Andrew Trickcbf24b42012-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 Andersone4ad9c72007-11-27 22:47:08 +000028
Andrew Trick37aa33b2012-06-20 05:23:33 +000029static cl::opt<bool>
30StableLoopInfo("stable-machine-loops", cl::Hidden, cl::init(false),
31 cl::desc("Compute a stable loop tree."));
32
Chris Lattner19033bf2008-01-05 23:29:51 +000033char MachineLoopInfo::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000034INITIALIZE_PASS_BEGIN(MachineLoopInfo, "machine-loops",
35 "Machine Natural Loop Construction", true, true)
36INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
37INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
Owen Andersonce665bd2010-10-07 22:25:06 +000038 "Machine Natural Loop Construction", true, true)
Bill Wendling67d65bb2008-01-04 20:54:55 +000039
Owen Anderson90c579d2010-08-06 18:33:48 +000040char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
Owen Andersone4ad9c72007-11-27 22:47:08 +000041
42bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
43 releaseMemory();
Andrew Trick37aa33b2012-06-20 05:23:33 +000044 if (StableLoopInfo)
45 LI.Analyze(getAnalysis<MachineDominatorTree>().getBase());
46 else
47 LI.Calculate(getAnalysis<MachineDominatorTree>().getBase()); // Update
Owen Andersone4ad9c72007-11-27 22:47:08 +000048 return false;
49}
50
51void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesAll();
53 AU.addRequired<MachineDominatorTree>();
Dan Gohmanad2afc22009-07-31 18:16:33 +000054 MachineFunctionPass::getAnalysisUsage(AU);
Owen Andersone4ad9c72007-11-27 22:47:08 +000055}
Dan Gohman81b16a32009-10-20 04:16:37 +000056
57MachineBasicBlock *MachineLoop::getTopBlock() {
58 MachineBasicBlock *TopMBB = getHeader();
59 MachineFunction::iterator Begin = TopMBB->getParent()->begin();
60 if (TopMBB != Begin) {
61 MachineBasicBlock *PriorMBB = prior(MachineFunction::iterator(TopMBB));
62 while (contains(PriorMBB)) {
63 TopMBB = PriorMBB;
64 if (TopMBB == Begin) break;
65 PriorMBB = prior(MachineFunction::iterator(TopMBB));
66 }
67 }
68 return TopMBB;
69}
70
71MachineBasicBlock *MachineLoop::getBottomBlock() {
72 MachineBasicBlock *BotMBB = getHeader();
73 MachineFunction::iterator End = BotMBB->getParent()->end();
74 if (BotMBB != prior(End)) {
Chris Lattner7896c9f2009-12-03 00:50:42 +000075 MachineBasicBlock *NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
Dan Gohman81b16a32009-10-20 04:16:37 +000076 while (contains(NextMBB)) {
77 BotMBB = NextMBB;
Chris Lattner7896c9f2009-12-03 00:50:42 +000078 if (BotMBB == llvm::next(MachineFunction::iterator(BotMBB))) break;
79 NextMBB = llvm::next(MachineFunction::iterator(BotMBB));
Dan Gohman81b16a32009-10-20 04:16:37 +000080 }
81 }
82 return BotMBB;
83}
Dan Gohmandda30cd2010-01-05 21:08:02 +000084
85void MachineLoop::dump() const {
86 print(dbgs());
87}