Be a bit more efficient when processing the active and inactive
[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 "Support/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   // createNode is used to create a node that marks the end of the list...
44   static Instruction *createNode();
45   static iplist<Instruction> &getList(BasicBlock *BB);
46 };
47
48 struct BasicBlock : public Value {       // Basic blocks are data objects also
49   typedef iplist<Instruction> InstListType;
50 private :
51   InstListType InstList;
52   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
53
54   void setParent(Function *parent);
55   void setNext(BasicBlock *N) { Next = N; }
56   void setPrev(BasicBlock *N) { Prev = N; }
57   friend class SymbolTableListTraits<BasicBlock, Function, Function>;
58
59   BasicBlock(const BasicBlock &);     // Do not implement
60   void operator=(const BasicBlock &); // Do not implement
61
62 public:
63   /// Instruction iterators...
64   typedef InstListType::iterator                              iterator;
65   typedef InstListType::const_iterator                  const_iterator;
66   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
67   typedef std::reverse_iterator<iterator>             reverse_iterator;
68
69   /// BasicBlock ctor - If the function parameter is specified, the basic block
70   /// is automatically inserted at either the end of the function (if
71   /// InsertBefore is null), or before the specified basic block.
72   ///
73   BasicBlock(const std::string &Name = "", Function *Parent = 0,
74              BasicBlock *InsertBefore = 0);
75   ~BasicBlock();
76
77   // Specialize setName to take care of symbol table majik
78   virtual void setName(const std::string &name, SymbolTable *ST = 0);
79
80   /// getParent - Return the enclosing method, or null if none
81   ///
82   const Function *getParent() const { return InstList.getParent(); }
83         Function *getParent()       { return InstList.getParent(); }
84
85   // getNext/Prev - Return the next or previous basic block in the list.
86         BasicBlock *getNext()       { return Next; }
87   const BasicBlock *getNext() const { return Next; }
88         BasicBlock *getPrev()       { return Prev; }
89   const BasicBlock *getPrev() const { return Prev; }
90
91   /// getTerminator() - If this is a well formed basic block, then this returns
92   /// a pointer to the terminator instruction.  If it is not, then you get a
93   /// null pointer back.
94   ///
95   TerminatorInst *getTerminator();
96   const TerminatorInst *const getTerminator() const;
97   
98   //===--------------------------------------------------------------------===//
99   /// Instruction iterator methods
100   ///
101   inline iterator                begin()       { return InstList.begin(); }
102   inline const_iterator          begin() const { return InstList.begin(); }
103   inline iterator                end  ()       { return InstList.end();   }
104   inline const_iterator          end  () const { return InstList.end();   }
105
106   inline reverse_iterator       rbegin()       { return InstList.rbegin(); }
107   inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
108   inline reverse_iterator       rend  ()       { return InstList.rend();   }
109   inline const_reverse_iterator rend  () const { return InstList.rend();   }
110
111   inline unsigned                 size() const { return InstList.size();  }
112   inline bool                    empty() const { return InstList.empty(); }
113   inline const Instruction      &front() const { return InstList.front(); }
114   inline       Instruction      &front()       { return InstList.front(); }
115   inline const Instruction       &back() const { return InstList.back();  }
116   inline       Instruction       &back()       { return InstList.back();  }
117
118   /// getInstList() - Return the underlying instruction list container.  You
119   /// need to access it directly if you want to modify it currently.
120   ///
121   const InstListType &getInstList() const { return InstList; }
122         InstListType &getInstList()       { return InstList; }
123
124   virtual void print(std::ostream &OS) const { print(OS, 0); }
125   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
126
127   /// Methods for support type inquiry through isa, cast, and dyn_cast:
128   static inline bool classof(const BasicBlock *BB) { return true; }
129   static inline bool classof(const Value *V) {
130     return V->getValueType() == Value::BasicBlockVal;
131   }
132
133   /// dropAllReferences() - This function causes all the subinstructions to "let
134   /// go" of all references that they are maintaining.  This allows one to
135   /// 'delete' a whole class at a time, even though there may be circular
136   /// references... first all references are dropped, and all use counts go to
137   /// zero.  Then everything is delete'd for real.  Note that no operations are
138   /// valid on an object that has "dropped all references", except operator 
139   /// delete.
140   ///
141   void dropAllReferences();
142
143   /// removePredecessor - This method is used to notify a BasicBlock that the
144   /// specified Predecessor of the block is no longer able to reach it.  This is
145   /// actually not used to update the Predecessor list, but is actually used to 
146   /// update the PHI nodes that reside in the block.  Note that this should be
147   /// called while the predecessor still refers to this block.
148   ///
149   void removePredecessor(BasicBlock *Pred);
150
151   /// splitBasicBlock - This splits a basic block into two at the specified
152   /// instruction.  Note that all instructions BEFORE the specified iterator
153   /// stay as part of the original basic block, an unconditional branch is added
154   /// to the new BB, and the rest of the instructions in the BB are moved to the
155   /// new BB, including the old terminator.  The newly formed BasicBlock is
156   /// returned.  This function invalidates the specified iterator.
157   ///
158   /// Note that this only works on well formed basic blocks (must have a 
159   /// terminator), and 'I' must not be the end of instruction list (which would
160   /// cause a degenerate basic block to be formed, having a terminator inside of
161   /// the basic block).
162   ///
163   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
164 };
165
166 } // End llvm namespace
167
168 #endif