]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/include/llvm/BasicBlock.h
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / 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 is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the BasicBlock class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_BASICBLOCK_H
15 #define LLVM_BASICBLOCK_H
16
17 #include "llvm/Instruction.h"
18 #include "llvm/SymbolTableListTraits.h"
19 #include "llvm/ADT/ilist.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/DataTypes.h"
22
23 namespace llvm {
24
25 class TerminatorInst;
26 class LLVMContext;
27 class BlockAddress;
28
29 template<> struct ilist_traits<Instruction>
30   : public SymbolTableListTraits<Instruction, BasicBlock> {
31   // createSentinel is used to get hold of a node that marks the end of
32   // the list...
33   // The sentinel is relative to this instance, so we use a non-static
34   // method.
35   Instruction *createSentinel() const {
36     // since i(p)lists always publicly derive from the corresponding
37     // traits, placing a data member in this class will augment i(p)list.
38     // But since the NodeTy is expected to publicly derive from
39     // ilist_node<NodeTy>, there is a legal viable downcast from it
40     // to NodeTy. We use this trick to superpose i(p)list with a "ghostly"
41     // NodeTy, which becomes the sentinel. Dereferencing the sentinel is
42     // forbidden (save the ilist_node<NodeTy>) so no one will ever notice
43     // the superposition.
44     return static_cast<Instruction*>(&Sentinel);
45   }
46   static void destroySentinel(Instruction*) {}
47
48   Instruction *provideInitialHead() const { return createSentinel(); }
49   Instruction *ensureHead(Instruction*) const { return createSentinel(); }
50   static void noteHead(Instruction*, Instruction*) {}
51 private:
52   mutable ilist_half_node<Instruction> Sentinel;
53 };
54
55 /// This represents a single basic block in LLVM. A basic block is simply a
56 /// container of instructions that execute sequentially. Basic blocks are Values
57 /// because they are referenced by instructions such as branches and switch
58 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
59 /// represents a label to which a branch can jump.
60 ///
61 /// A well formed basic block is formed of a list of non-terminating
62 /// instructions followed by a single TerminatorInst instruction.
63 /// TerminatorInst's may not occur in the middle of basic blocks, and must
64 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
65 /// occur because it may be useful in the intermediate stage of constructing or
66 /// modifying a program. However, the verifier will ensure that basic blocks
67 /// are "well formed".
68 /// @brief LLVM Basic Block Representation
69 class BasicBlock : public Value, // Basic blocks are data objects also
70                    public ilist_node<BasicBlock> {
71   friend class BlockAddress;
72 public:
73   typedef iplist<Instruction> InstListType;
74 private:
75   InstListType InstList;
76   Function *Parent;
77
78   void setParent(Function *parent);
79   friend class SymbolTableListTraits<BasicBlock, Function>;
80
81   BasicBlock(const BasicBlock &);     // Do not implement
82   void operator=(const BasicBlock &); // Do not implement
83
84   /// BasicBlock ctor - If the function parameter is specified, the basic block
85   /// is automatically inserted at either the end of the function (if
86   /// InsertBefore is null), or before the specified basic block.
87   ///
88   explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
89                       Function *Parent = 0, BasicBlock *InsertBefore = 0);
90 public:
91   /// getContext - Get the context in which this basic block lives.
92   LLVMContext &getContext() const;
93
94   /// Instruction iterators...
95   typedef InstListType::iterator                              iterator;
96   typedef InstListType::const_iterator                  const_iterator;
97
98   /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
99   /// the basic block is automatically inserted at either the end of the
100   /// function (if InsertBefore is 0), or before the specified basic block.
101   static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
102                             Function *Parent = 0,BasicBlock *InsertBefore = 0) {
103     return new BasicBlock(Context, Name, Parent, InsertBefore);
104   }
105   ~BasicBlock();
106
107   /// getParent - Return the enclosing method, or null if none
108   ///
109   const Function *getParent() const { return Parent; }
110         Function *getParent()       { return Parent; }
111
112   /// use_back - Specialize the methods defined in Value, as we know that an
113   /// BasicBlock can only be used by Users (specifically terminators
114   /// and BlockAddress's).
115   User       *use_back()       { return cast<User>(*use_begin());}
116   const User *use_back() const { return cast<User>(*use_begin());}
117
118   /// getTerminator() - If this is a well formed basic block, then this returns
119   /// a pointer to the terminator instruction.  If it is not, then you get a
120   /// null pointer back.
121   ///
122   TerminatorInst *getTerminator();
123   const TerminatorInst *getTerminator() const;
124
125   /// Returns a pointer to the first instructon in this block that is not a
126   /// PHINode instruction. When adding instruction to the beginning of the
127   /// basic block, they should be added before the returned value, not before
128   /// the first instruction, which might be PHI.
129   /// Returns 0 is there's no non-PHI instruction.
130   Instruction* getFirstNonPHI();
131   const Instruction* getFirstNonPHI() const {
132     return const_cast<BasicBlock*>(this)->getFirstNonPHI();
133   }
134
135   // Same as above, but also skip debug intrinsics.
136   Instruction* getFirstNonPHIOrDbg();
137   const Instruction* getFirstNonPHIOrDbg() const {
138     return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbg();
139   }
140
141   // Same as above, but also skip lifetime intrinsics.
142   Instruction* getFirstNonPHIOrDbgOrLifetime();
143   const Instruction* getFirstNonPHIOrDbgOrLifetime() const {
144     return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbgOrLifetime();
145   }
146
147   /// removeFromParent - This method unlinks 'this' from the containing
148   /// function, but does not delete it.
149   ///
150   void removeFromParent();
151
152   /// eraseFromParent - This method unlinks 'this' from the containing function
153   /// and deletes it.
154   ///
155   void eraseFromParent();
156
157   /// moveBefore - Unlink this basic block from its current function and
158   /// insert it into the function that MovePos lives in, right before MovePos.
159   void moveBefore(BasicBlock *MovePos);
160
161   /// moveAfter - Unlink this basic block from its current function and
162   /// insert it into the function that MovePos lives in, right after MovePos.
163   void moveAfter(BasicBlock *MovePos);
164
165
166   /// getSinglePredecessor - If this basic block has a single predecessor block,
167   /// return the block, otherwise return a null pointer.
168   BasicBlock *getSinglePredecessor();
169   const BasicBlock *getSinglePredecessor() const {
170     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
171   }
172
173   /// getUniquePredecessor - If this basic block has a unique predecessor block,
174   /// return the block, otherwise return a null pointer.
175   /// Note that unique predecessor doesn't mean single edge, there can be
176   /// multiple edges from the unique predecessor to this block (for example
177   /// a switch statement with multiple cases having the same destination).
178   BasicBlock *getUniquePredecessor();
179   const BasicBlock *getUniquePredecessor() const {
180     return const_cast<BasicBlock*>(this)->getUniquePredecessor();
181   }
182
183   //===--------------------------------------------------------------------===//
184   /// Instruction iterator methods
185   ///
186   inline iterator                begin()       { return InstList.begin(); }
187   inline const_iterator          begin() const { return InstList.begin(); }
188   inline iterator                end  ()       { return InstList.end();   }
189   inline const_iterator          end  () const { return InstList.end();   }
190
191   inline size_t                   size() const { return InstList.size();  }
192   inline bool                    empty() const { return InstList.empty(); }
193   inline const Instruction      &front() const { return InstList.front(); }
194   inline       Instruction      &front()       { return InstList.front(); }
195   inline const Instruction       &back() const { return InstList.back();  }
196   inline       Instruction       &back()       { return InstList.back();  }
197
198   /// getInstList() - Return the underlying instruction list container.  You
199   /// need to access it directly if you want to modify it currently.
200   ///
201   const InstListType &getInstList() const { return InstList; }
202         InstListType &getInstList()       { return InstList; }
203
204   /// getSublistAccess() - returns pointer to member of instruction list
205   static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) {
206     return &BasicBlock::InstList;
207   }
208
209   /// getValueSymbolTable() - returns pointer to symbol table (if any)
210   ValueSymbolTable *getValueSymbolTable();
211
212   /// Methods for support type inquiry through isa, cast, and dyn_cast:
213   static inline bool classof(const BasicBlock *) { return true; }
214   static inline bool classof(const Value *V) {
215     return V->getValueID() == Value::BasicBlockVal;
216   }
217
218   /// dropAllReferences() - This function causes all the subinstructions to "let
219   /// go" of all references that they are maintaining.  This allows one to
220   /// 'delete' a whole class at a time, even though there may be circular
221   /// references... first all references are dropped, and all use counts go to
222   /// zero.  Then everything is delete'd for real.  Note that no operations are
223   /// valid on an object that has "dropped all references", except operator
224   /// delete.
225   ///
226   void dropAllReferences();
227
228   /// removePredecessor - This method is used to notify a BasicBlock that the
229   /// specified Predecessor of the block is no longer able to reach it.  This is
230   /// actually not used to update the Predecessor list, but is actually used to
231   /// update the PHI nodes that reside in the block.  Note that this should be
232   /// called while the predecessor still refers to this block.
233   ///
234   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
235
236   /// splitBasicBlock - This splits a basic block into two at the specified
237   /// instruction.  Note that all instructions BEFORE the specified iterator
238   /// stay as part of the original basic block, an unconditional branch is added
239   /// to the original BB, and the rest of the instructions in the BB are moved
240   /// to the new BB, including the old terminator.  The newly formed BasicBlock
241   /// is returned.  This function invalidates the specified iterator.
242   ///
243   /// Note that this only works on well formed basic blocks (must have a
244   /// terminator), and 'I' must not be the end of instruction list (which would
245   /// cause a degenerate basic block to be formed, having a terminator inside of
246   /// the basic block).
247   ///
248   /// Also note that this doesn't preserve any passes. To split blocks while
249   /// keeping loop information consistent, use the SplitBlock utility function.
250   ///
251   BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "");
252
253   /// hasAddressTaken - returns true if there are any uses of this basic block
254   /// other than direct branches, switches, etc. to it.
255   bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
256
257   /// replaceSuccessorsPhiUsesWith - Update all phi nodes in all our successors
258   /// to refer to basic block New instead of to us.
259   void replaceSuccessorsPhiUsesWith(BasicBlock *New);
260
261 private:
262   /// AdjustBlockAddressRefCount - BasicBlock stores the number of BlockAddress
263   /// objects using it.  This is almost always 0, sometimes one, possibly but
264   /// almost never 2, and inconceivably 3 or more.
265   void AdjustBlockAddressRefCount(int Amt) {
266     setValueSubclassData(getSubclassDataFromValue()+Amt);
267     assert((int)(signed char)getSubclassDataFromValue() >= 0 &&
268            "Refcount wrap-around");
269   }
270   // Shadow Value::setValueSubclassData with a private forwarding method so that
271   // any future subclasses cannot accidentally use it.
272   void setValueSubclassData(unsigned short D) {
273     Value::setValueSubclassData(D);
274   }
275 };
276
277 } // End llvm namespace
278
279 #endif