LLVM API Documentation

BasicBlock.h

Go to the documentation of this file.
00001 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the BasicBlock class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_BASICBLOCK_H
00015 #define LLVM_BASICBLOCK_H
00016 
00017 #include "llvm/Instruction.h"
00018 #include "llvm/SymbolTableListTraits.h"
00019 #include "llvm/ADT/ilist.h"
00020 #include "llvm/Support/DataTypes.h"
00021 
00022 namespace llvm {
00023 
00024 class TerminatorInst;
00025 
00026 template<> struct ilist_traits<Instruction>
00027   : public SymbolTableListTraits<Instruction, BasicBlock> {
00028   // createSentinel is used to create a node that marks the end of the list...
00029   static Instruction *createSentinel();
00030   static void destroySentinel(Instruction *I) { delete I; }
00031   static iplist<Instruction> &getList(BasicBlock *BB);
00032   static ValueSymbolTable *getSymTab(BasicBlock *ItemParent);
00033   static int getListOffset();
00034 };
00035 
00036 /// This represents a single basic block in LLVM. A basic block is simply a
00037 /// container of instructions that execute sequentially. Basic blocks are Values
00038 /// because they are referenced by instructions such as branches and switch
00039 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
00040 /// represents a label to which a branch can jump.
00041 ///
00042 /// A well formed basic block is formed of a list of non-terminating 
00043 /// instructions followed by a single TerminatorInst instruction.  
00044 /// TerminatorInst's may not occur in the middle of basic blocks, and must 
00045 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
00046 /// occur because it may be useful in the intermediate stage of constructing or
00047 /// modifying a program. However, the verifier will ensure that basic blocks
00048 /// are "well formed".
00049 /// @brief LLVM Basic Block Representation
00050 class BasicBlock : public Value, // Basic blocks are data objects also
00051                    public ilist_node<BasicBlock> {
00052 public:
00053   typedef iplist<Instruction> InstListType;
00054 private :
00055   InstListType InstList;
00056   Function *Parent;
00057 
00058   void setParent(Function *parent);
00059   friend class SymbolTableListTraits<BasicBlock, Function>;
00060 
00061   BasicBlock(const BasicBlock &);     // Do not implement
00062   void operator=(const BasicBlock &); // Do not implement
00063 
00064   /// BasicBlock ctor - If the function parameter is specified, the basic block
00065   /// is automatically inserted at either the end of the function (if
00066   /// InsertBefore is null), or before the specified basic block.
00067   ///
00068   explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
00069                       BasicBlock *InsertBefore = 0);
00070 public:
00071   /// Instruction iterators...
00072   typedef InstListType::iterator                              iterator;
00073   typedef InstListType::const_iterator                  const_iterator;
00074 
00075   /// Create - Creates a new BasicBlock. If the Parent parameter is specified,
00076   /// the basic block is automatically inserted at either the end of the
00077   /// function (if InsertBefore is 0), or before the specified basic block.
00078   static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
00079                             BasicBlock *InsertBefore = 0) {
00080     return new BasicBlock(Name, Parent, InsertBefore);
00081   }
00082   ~BasicBlock();
00083 
00084   /// getParent - Return the enclosing method, or null if none
00085   ///
00086   const Function *getParent() const { return Parent; }
00087         Function *getParent()       { return Parent; }
00088 
00089   /// use_back - Specialize the methods defined in Value, as we know that an
00090   /// BasicBlock can only be used by Instructions (specifically PHI and terms).
00091   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
00092   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
00093   
00094   /// getTerminator() - If this is a well formed basic block, then this returns
00095   /// a pointer to the terminator instruction.  If it is not, then you get a
00096   /// null pointer back.
00097   ///
00098   TerminatorInst *getTerminator();
00099   const TerminatorInst *getTerminator() const;
00100   
00101   /// Returns a pointer to the first instructon in this block that is not a 
00102   /// PHINode instruction. When adding instruction to the beginning of the
00103   /// basic block, they should be added before the returned value, not before
00104   /// the first instruction, which might be PHI.
00105   /// Returns 0 is there's no non-PHI instruction.
00106   Instruction* getFirstNonPHI();
00107   const Instruction* getFirstNonPHI() const {
00108     return const_cast<BasicBlock*>(this)->getFirstNonPHI();
00109   }
00110   
00111   /// removeFromParent - This method unlinks 'this' from the containing
00112   /// function, but does not delete it.
00113   ///
00114   void removeFromParent();
00115 
00116   /// eraseFromParent - This method unlinks 'this' from the containing function
00117   /// and deletes it.
00118   ///
00119   void eraseFromParent();
00120   
00121   /// moveBefore - Unlink this basic block from its current function and
00122   /// insert it into the function that MovePos lives in, right before MovePos.
00123   void moveBefore(BasicBlock *MovePos);
00124   
00125   /// moveAfter - Unlink this basic block from its current function and
00126   /// insert it into the function that MovePos lives in, right after MovePos.
00127   void moveAfter(BasicBlock *MovePos);
00128   
00129 
00130   /// getSinglePredecessor - If this basic block has a single predecessor block,
00131   /// return the block, otherwise return a null pointer.
00132   BasicBlock *getSinglePredecessor();
00133   const BasicBlock *getSinglePredecessor() const {
00134     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
00135   }
00136 
00137   /// getUniquePredecessor - If this basic block has a unique predecessor block,
00138   /// return the block, otherwise return a null pointer.
00139   /// Note that unique predecessor doesn't mean single edge, there can be 
00140   /// multiple edges from the unique predecessor to this block (for example 
00141   /// a switch statement with multiple cases having the same destination).
00142   BasicBlock *getUniquePredecessor();
00143   const BasicBlock *getUniquePredecessor() const {
00144     return const_cast<BasicBlock*>(this)->getUniquePredecessor();
00145   }
00146 
00147   //===--------------------------------------------------------------------===//
00148   /// Instruction iterator methods
00149   ///
00150   inline iterator                begin()       { return InstList.begin(); }
00151   inline const_iterator          begin() const { return InstList.begin(); }
00152   inline iterator                end  ()       { return InstList.end();   }
00153   inline const_iterator          end  () const { return InstList.end();   }
00154 
00155   inline size_t                   size() const { return InstList.size();  }
00156   inline bool                    empty() const { return InstList.empty(); }
00157   inline const Instruction      &front() const { return InstList.front(); }
00158   inline       Instruction      &front()       { return InstList.front(); }
00159   inline const Instruction       &back() const { return InstList.back();  }
00160   inline       Instruction       &back()       { return InstList.back();  }
00161 
00162   /// getInstList() - Return the underlying instruction list container.  You
00163   /// need to access it directly if you want to modify it currently.
00164   ///
00165   const InstListType &getInstList() const { return InstList; }
00166         InstListType &getInstList()       { return InstList; }
00167 
00168   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00169   static inline bool classof(const BasicBlock *) { return true; }
00170   static inline bool classof(const Value *V) {
00171     return V->getValueID() == Value::BasicBlockVal;
00172   }
00173 
00174   /// dropAllReferences() - This function causes all the subinstructions to "let
00175   /// go" of all references that they are maintaining.  This allows one to
00176   /// 'delete' a whole class at a time, even though there may be circular
00177   /// references... first all references are dropped, and all use counts go to
00178   /// zero.  Then everything is delete'd for real.  Note that no operations are
00179   /// valid on an object that has "dropped all references", except operator
00180   /// delete.
00181   ///
00182   void dropAllReferences();
00183 
00184   /// removePredecessor - This method is used to notify a BasicBlock that the
00185   /// specified Predecessor of the block is no longer able to reach it.  This is
00186   /// actually not used to update the Predecessor list, but is actually used to
00187   /// update the PHI nodes that reside in the block.  Note that this should be
00188   /// called while the predecessor still refers to this block.
00189   ///
00190   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
00191 
00192   /// splitBasicBlock - This splits a basic block into two at the specified
00193   /// instruction.  Note that all instructions BEFORE the specified iterator
00194   /// stay as part of the original basic block, an unconditional branch is added
00195   /// to the original BB, and the rest of the instructions in the BB are moved
00196   /// to the new BB, including the old terminator.  The newly formed BasicBlock
00197   /// is returned.  This function invalidates the specified iterator.
00198   ///
00199   /// Note that this only works on well formed basic blocks (must have a
00200   /// terminator), and 'I' must not be the end of instruction list (which would
00201   /// cause a degenerate basic block to be formed, having a terminator inside of
00202   /// the basic block).
00203   ///
00204   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
00205   
00206   
00207   static unsigned getInstListOffset() {
00208     BasicBlock *Obj = 0;
00209     return unsigned(reinterpret_cast<uintptr_t>(&Obj->InstList));
00210   }
00211 };
00212 
00213 inline int 
00214 ilist_traits<Instruction>::getListOffset() {
00215   return BasicBlock::getInstListOffset();
00216 }
00217 
00218 } // End llvm namespace
00219 
00220 #endif