1 //===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 // natural loops may actually be several loops that share the same header node.
14 // This analysis calculates the nesting structure of loops in a function. For
15 // each natural loop identified, this analysis identifies natural loops
16 // contained entirely within the loop and the basic blocks the make up the loop.
18 // It can calculate on the fly various bits of information, for example:
20 // * whether there is a preheader for the loop
21 // * the number of back edges to the header
22 // * whether or not a particular block branches out of the loop
23 // * the successor blocks of the loop
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_CODEGEN_MACHINE_LOOP_INFO_H
31 #define LLVM_CODEGEN_MACHINE_LOOP_INFO_H
33 #include "llvm/CodeGen/MachineFunctionPass.h"
34 #include "llvm/Analysis/LoopInfo.h"
40 // Provide overrides for Loop methods that don't make sense for machine loops.
43 LoopBase<MachineBasicBlock, MachineLoop>::getCanonicalInductionVariable() const {
44 assert(0 && "getCanonicalInductionVariable not supported for machine loops!");
48 template<> inline Instruction*
49 LoopBase<MachineBasicBlock,
50 MachineLoop>::getCanonicalInductionVariableIncrement() const {
52 "getCanonicalInductionVariableIncrement not supported for machine loops!");
58 LoopBase<MachineBasicBlock, MachineLoop>::isLoopInvariant(Value *V) const {
59 assert(0 && "isLoopInvariant not supported for machine loops!");
65 LoopBase<MachineBasicBlock, MachineLoop>::getTripCount() const {
66 assert(0 && "getTripCount not supported for machine loops!");
72 LoopBase<MachineBasicBlock, MachineLoop>::isLCSSAForm() const {
73 assert(0 && "isLCSSAForm not supported for machine loops");
77 class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
81 friend class LoopInfoBase<MachineBasicBlock, MachineLoop>;
82 explicit MachineLoop(MachineBasicBlock *MBB)
83 : LoopBase<MachineBasicBlock, MachineLoop>(MBB) {}
86 class MachineLoopInfo : public MachineFunctionPass {
87 LoopInfoBase<MachineBasicBlock, MachineLoop> LI;
88 friend class LoopBase<MachineBasicBlock, MachineLoop>;
90 void operator=(const MachineLoopInfo &); // do not implement
91 MachineLoopInfo(const MachineLoopInfo &); // do not implement
93 LoopInfoBase<MachineBasicBlock, MachineLoop>& getBase() { return LI; }
96 static char ID; // Pass identification, replacement for typeid
98 MachineLoopInfo() : MachineFunctionPass(&ID) {}
100 /// iterator/begin/end - The interface to the top-level loops in the current
103 typedef LoopInfoBase<MachineBasicBlock, MachineLoop>::iterator iterator;
104 inline iterator begin() const { return LI.begin(); }
105 inline iterator end() const { return LI.end(); }
106 bool empty() const { return LI.empty(); }
108 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
109 /// block is in no loop (for example the entry node), null is returned.
111 inline MachineLoop *getLoopFor(const MachineBasicBlock *BB) const {
112 return LI.getLoopFor(BB);
115 /// operator[] - same as getLoopFor...
117 inline const MachineLoop *operator[](const MachineBasicBlock *BB) const {
118 return LI.getLoopFor(BB);
121 /// getLoopDepth - Return the loop nesting level of the specified block...
123 inline unsigned getLoopDepth(const MachineBasicBlock *BB) const {
124 return LI.getLoopDepth(BB);
127 // isLoopHeader - True if the block is a loop header node
128 inline bool isLoopHeader(MachineBasicBlock *BB) const {
129 return LI.isLoopHeader(BB);
132 /// runOnFunction - Calculate the natural loop information.
134 virtual bool runOnMachineFunction(MachineFunction &F);
136 virtual void releaseMemory() { LI.releaseMemory(); }
138 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
140 /// removeLoop - This removes the specified top-level loop from this loop info
141 /// object. The loop is not deleted, as it will presumably be inserted into
143 inline MachineLoop *removeLoop(iterator I) { return LI.removeLoop(I); }
145 /// changeLoopFor - Change the top-level loop that contains BB to the
146 /// specified loop. This should be used by transformations that restructure
147 /// the loop hierarchy tree.
148 inline void changeLoopFor(MachineBasicBlock *BB, MachineLoop *L) {
149 LI.changeLoopFor(BB, L);
152 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
153 /// list with the indicated loop.
154 inline void changeTopLevelLoop(MachineLoop *OldLoop, MachineLoop *NewLoop) {
155 LI.changeTopLevelLoop(OldLoop, NewLoop);
158 /// addTopLevelLoop - This adds the specified loop to the collection of
160 inline void addTopLevelLoop(MachineLoop *New) {
161 LI.addTopLevelLoop(New);
164 /// removeBlock - This method completely removes BB from all data structures,
165 /// including all of the Loop objects it is nested in and our mapping from
166 /// MachineBasicBlocks to loops.
167 void removeBlock(MachineBasicBlock *BB) {
173 // Allow clients to walk the list of nested loops...
174 template <> struct GraphTraits<const MachineLoop*> {
175 typedef const MachineLoop NodeType;
176 typedef MachineLoopInfo::iterator ChildIteratorType;
178 static NodeType *getEntryNode(const MachineLoop *L) { return L; }
179 static inline ChildIteratorType child_begin(NodeType *N) {
182 static inline ChildIteratorType child_end(NodeType *N) {
187 template <> struct GraphTraits<MachineLoop*> {
188 typedef MachineLoop NodeType;
189 typedef MachineLoopInfo::iterator ChildIteratorType;
191 static NodeType *getEntryNode(MachineLoop *L) { return L; }
192 static inline ChildIteratorType child_begin(NodeType *N) {
195 static inline ChildIteratorType child_end(NodeType *N) {
200 } // End llvm namespace