Move private helper function into the only .cpp file that uses it.
[oota-llvm.git] / include / llvm / BasicBlock.h
1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 //
11 // This file contains the declaration of the BasicBlock class, which represents
12 // a single basic block in the VM.
13 //
14 // Note that basic blocks themselves are Value's, because they are referenced
15 // by instructions like branches and can go in switch tables and stuff...
16 //
17 ///===---------------------------------------------------------------------===//
18 //
19 // Note that well formed basic blocks are formed of a list of instructions 
20 // followed by a single TerminatorInst instruction.  TerminatorInst's may not
21 // occur in the middle of basic blocks, and must terminate the blocks.
22 //
23 // This code allows malformed basic blocks to occur, because it may be useful
24 // in the intermediate stage modification to a program.
25 //
26 //===----------------------------------------------------------------------===//
27
28 #ifndef LLVM_BASICBLOCK_H
29 #define LLVM_BASICBLOCK_H
30
31 #include "llvm/Instruction.h"
32 #include "llvm/SymbolTableListTraits.h"
33 #include "llvm/ADT/ilist"
34
35 namespace llvm {
36
37 class TerminatorInst;
38 template <class Term, class BB> class SuccIterator;  // Successor Iterator
39 template <class Ptr, class USE_iterator> class PredIterator;
40
41 template<> struct ilist_traits<Instruction>
42   : public SymbolTableListTraits<Instruction, BasicBlock, Function> {
43   // createSentinel is used to create a node that marks the end of the list...
44   static Instruction *createSentinel();
45   static void destroySentinel(Instruction *I) { delete I; }
46   static iplist<Instruction> &getList(BasicBlock *BB);
47 };
48
49 class BasicBlock : public Value {       // Basic blocks are data objects also
50 public:
51   typedef iplist<Instruction> InstListType;
52 private :
53   InstListType InstList;
54   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
55
56   void setParent(Function *parent);
57   void setNext(BasicBlock *N) { Next = N; }
58   void setPrev(BasicBlock *N) { Prev = N; }
59   friend class SymbolTableListTraits<BasicBlock, Function, Function>;
60
61   BasicBlock(const BasicBlock &);     // Do not implement
62   void operator=(const BasicBlock &); // Do not implement
63
64 public:
65   /// Instruction iterators...
66   typedef InstListType::iterator                              iterator;
67   typedef InstListType::const_iterator                  const_iterator;
68   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
69   typedef std::reverse_iterator<iterator>             reverse_iterator;
70
71   /// BasicBlock ctor - If the function parameter is specified, the basic block
72   /// is automatically inserted at either the end of the function (if
73   /// InsertBefore is null), or before the specified basic block.
74   ///
75   BasicBlock(const std::string &Name = "", Function *Parent = 0,
76              BasicBlock *InsertBefore = 0);
77   ~BasicBlock();
78
79   // Specialize setName to take care of symbol table majik
80   virtual void setName(const std::string &name, SymbolTable *ST = 0);
81
82   /// getParent - Return the enclosing method, or null if none
83   ///
84   const Function *getParent() const { return InstList.getParent(); }
85         Function *getParent()       { return InstList.getParent(); }
86
87   // getNext/Prev - Return the next or previous basic block in the list.
88         BasicBlock *getNext()       { return Next; }
89   const BasicBlock *getNext() const { return Next; }
90         BasicBlock *getPrev()       { return Prev; }
91   const BasicBlock *getPrev() const { return Prev; }
92
93   /// getTerminator() - If this is a well formed basic block, then this returns
94   /// a pointer to the terminator instruction.  If it is not, then you get a
95   /// null pointer back.
96   ///
97   TerminatorInst *getTerminator();
98   const TerminatorInst *const getTerminator() const;
99   
100   /// removeFromParent - This method unlinks 'this' from the containing
101   /// function, but does not delete it.
102   ///
103   void removeFromParent();
104
105   /// eraseFromParent - This method unlinks 'this' from the containing function
106   /// and deletes it.
107   ///
108   void eraseFromParent();
109
110
111
112   //===--------------------------------------------------------------------===//
113   /// Instruction iterator methods
114   ///
115   inline iterator                begin()       { return InstList.begin(); }
116   inline const_iterator          begin() const { return InstList.begin(); }
117   inline iterator                end  ()       { return InstList.end();   }
118   inline const_iterator          end  () const { return InstList.end();   }
119
120   inline reverse_iterator       rbegin()       { return InstList.rbegin(); }
121   inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
122   inline reverse_iterator       rend  ()       { return InstList.rend();   }
123   inline const_reverse_iterator rend  () const { return InstList.rend();   }
124
125   inline size_t                   size() const { return InstList.size();  }
126   inline bool                    empty() const { return InstList.empty(); }
127   inline const Instruction      &front() const { return InstList.front(); }
128   inline       Instruction      &front()       { return InstList.front(); }
129   inline const Instruction       &back() const { return InstList.back();  }
130   inline       Instruction       &back()       { return InstList.back();  }
131
132   /// getInstList() - Return the underlying instruction list container.  You
133   /// need to access it directly if you want to modify it currently.
134   ///
135   const InstListType &getInstList() const { return InstList; }
136         InstListType &getInstList()       { return InstList; }
137
138   virtual void print(std::ostream &OS) const { print(OS, 0); }
139   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
140
141   /// Methods for support type inquiry through isa, cast, and dyn_cast:
142   static inline bool classof(const BasicBlock *BB) { return true; }
143   static inline bool classof(const Value *V) {
144     return V->getValueType() == Value::BasicBlockVal;
145   }
146
147   /// dropAllReferences() - This function causes all the subinstructions to "let
148   /// go" of all references that they are maintaining.  This allows one to
149   /// 'delete' a whole class at a time, even though there may be circular
150   /// references... first all references are dropped, and all use counts go to
151   /// zero.  Then everything is delete'd for real.  Note that no operations are
152   /// valid on an object that has "dropped all references", except operator 
153   /// delete.
154   ///
155   void dropAllReferences();
156
157   /// removePredecessor - This method is used to notify a BasicBlock that the
158   /// specified Predecessor of the block is no longer able to reach it.  This is
159   /// actually not used to update the Predecessor list, but is actually used to 
160   /// update the PHI nodes that reside in the block.  Note that this should be
161   /// called while the predecessor still refers to this block.
162   ///
163   void removePredecessor(BasicBlock *Pred);
164
165   /// splitBasicBlock - This splits a basic block into two at the specified
166   /// instruction.  Note that all instructions BEFORE the specified iterator
167   /// stay as part of the original basic block, an unconditional branch is added
168   /// to the new BB, and the rest of the instructions in the BB are moved to the
169   /// new BB, including the old terminator.  The newly formed BasicBlock is
170   /// returned.  This function invalidates the specified iterator.
171   ///
172   /// Note that this only works on well formed basic blocks (must have a 
173   /// terminator), and 'I' must not be the end of instruction list (which would
174   /// cause a degenerate basic block to be formed, having a terminator inside of
175   /// the basic block).
176   ///
177   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
178 };
179
180 } // End llvm namespace
181
182 #endif