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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //
00011 // This file contains the declaration of the BasicBlock class, which represents
00012 // a single basic block in the VM.
00013 //
00014 // Note that basic blocks themselves are Value's, because they are referenced
00015 // by instructions like branches and can go in switch tables and stuff...
00016 //
00017 ///===---------------------------------------------------------------------===//
00018 //
00019 // Note that well formed basic blocks are formed of a list of instructions
00020 // followed by a single TerminatorInst instruction.  TerminatorInst's may not
00021 // occur in the middle of basic blocks, and must terminate the blocks.
00022 //
00023 // This code allows malformed basic blocks to occur, because it may be useful
00024 // in the intermediate stage modification to a program.
00025 //
00026 //===----------------------------------------------------------------------===//
00027 
00028 #ifndef LLVM_BASICBLOCK_H
00029 #define LLVM_BASICBLOCK_H
00030 
00031 #include "llvm/Instruction.h"
00032 #include "llvm/SymbolTableListTraits.h"
00033 #include "llvm/ADT/ilist"
00034 
00035 namespace llvm {
00036 
00037 class TerminatorInst;
00038 template <class Term, class BB> class SuccIterator;  // Successor Iterator
00039 template <class Ptr, class USE_iterator> class PredIterator;
00040 
00041 template<> struct ilist_traits<Instruction>
00042   : public SymbolTableListTraits<Instruction, BasicBlock, Function> {
00043   // createSentinel is used to create a node that marks the end of the list...
00044   static Instruction *createSentinel();
00045   static void destroySentinel(Instruction *I) { delete I; }
00046   static iplist<Instruction> &getList(BasicBlock *BB);
00047 };
00048 
00049 class BasicBlock : public Value {       // Basic blocks are data objects also
00050 public:
00051   typedef iplist<Instruction> InstListType;
00052 private :
00053   InstListType InstList;
00054   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
00055 
00056   void setParent(Function *parent);
00057   void setNext(BasicBlock *N) { Next = N; }
00058   void setPrev(BasicBlock *N) { Prev = N; }
00059   friend class SymbolTableListTraits<BasicBlock, Function, Function>;
00060 
00061   BasicBlock(const BasicBlock &);     // Do not implement
00062   void operator=(const BasicBlock &); // Do not implement
00063 
00064 public:
00065   /// Instruction iterators...
00066   typedef InstListType::iterator                              iterator;
00067   typedef InstListType::const_iterator                  const_iterator;
00068 
00069   /// BasicBlock ctor - If the function parameter is specified, the basic block
00070   /// is automatically inserted at either the end of the function (if
00071   /// InsertBefore is null), or before the specified basic block.
00072   ///
00073   BasicBlock(const std::string &Name = "", Function *Parent = 0,
00074              BasicBlock *InsertBefore = 0);
00075   ~BasicBlock();
00076 
00077   /// getParent - Return the enclosing method, or null if none
00078   ///
00079   const Function *getParent() const { return InstList.getParent(); }
00080         Function *getParent()       { return InstList.getParent(); }
00081 
00082   // getNext/Prev - Return the next or previous basic block in the list.
00083         BasicBlock *getNext()       { return Next; }
00084   const BasicBlock *getNext() const { return Next; }
00085         BasicBlock *getPrev()       { return Prev; }
00086   const BasicBlock *getPrev() const { return Prev; }
00087 
00088   /// getTerminator() - If this is a well formed basic block, then this returns
00089   /// a pointer to the terminator instruction.  If it is not, then you get a
00090   /// null pointer back.
00091   ///
00092   TerminatorInst *getTerminator();
00093   const TerminatorInst *const getTerminator() const;
00094 
00095   /// removeFromParent - This method unlinks 'this' from the containing
00096   /// function, but does not delete it.
00097   ///
00098   void removeFromParent();
00099 
00100   /// eraseFromParent - This method unlinks 'this' from the containing function
00101   /// and deletes it.
00102   ///
00103   void eraseFromParent();
00104   
00105   /// moveBefore - Unlink this instruction from its current function and
00106   /// insert it into the function that MovePos lives in, right before
00107   /// MovePos.
00108   void moveBefore(BasicBlock *MovePos);
00109 
00110   /// getSinglePredecessor - If this basic block has a single predecessor block,
00111   /// return the block, otherwise return a null pointer.
00112   BasicBlock *getSinglePredecessor();
00113   const BasicBlock *getSinglePredecessor() const {
00114     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
00115   }
00116 
00117   //===--------------------------------------------------------------------===//
00118   /// Instruction iterator methods
00119   ///
00120   inline iterator                begin()       { return InstList.begin(); }
00121   inline const_iterator          begin() const { return InstList.begin(); }
00122   inline iterator                end  ()       { return InstList.end();   }
00123   inline const_iterator          end  () const { return InstList.end();   }
00124 
00125   inline size_t                   size() const { return InstList.size();  }
00126   inline bool                    empty() const { return InstList.empty(); }
00127   inline const Instruction      &front() const { return InstList.front(); }
00128   inline       Instruction      &front()       { return InstList.front(); }
00129   inline const Instruction       &back() const { return InstList.back();  }
00130   inline       Instruction       &back()       { return InstList.back();  }
00131 
00132   /// getInstList() - Return the underlying instruction list container.  You
00133   /// need to access it directly if you want to modify it currently.
00134   ///
00135   const InstListType &getInstList() const { return InstList; }
00136         InstListType &getInstList()       { return InstList; }
00137 
00138   virtual void print(std::ostream &OS) const { print(OS, 0); }
00139   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
00140 
00141   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00142   static inline bool classof(const BasicBlock *) { return true; }
00143   static inline bool classof(const Value *V) {
00144     return V->getValueType() == Value::BasicBlockVal;
00145   }
00146 
00147   /// dropAllReferences() - This function causes all the subinstructions to "let
00148   /// go" of all references that they are maintaining.  This allows one to
00149   /// 'delete' a whole class at a time, even though there may be circular
00150   /// references... first all references are dropped, and all use counts go to
00151   /// zero.  Then everything is delete'd for real.  Note that no operations are
00152   /// valid on an object that has "dropped all references", except operator
00153   /// delete.
00154   ///
00155   void dropAllReferences();
00156 
00157   /// removePredecessor - This method is used to notify a BasicBlock that the
00158   /// specified Predecessor of the block is no longer able to reach it.  This is
00159   /// actually not used to update the Predecessor list, but is actually used to
00160   /// update the PHI nodes that reside in the block.  Note that this should be
00161   /// called while the predecessor still refers to this block.
00162   ///
00163   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
00164 
00165   /// splitBasicBlock - This splits a basic block into two at the specified
00166   /// instruction.  Note that all instructions BEFORE the specified iterator
00167   /// stay as part of the original basic block, an unconditional branch is added
00168   /// to the original BB, and the rest of the instructions in the BB are moved
00169   /// to the new BB, including the old terminator.  The newly formed BasicBlock
00170   /// is returned.  This function invalidates the specified iterator.
00171   ///
00172   /// Note that this only works on well formed basic blocks (must have a
00173   /// terminator), and 'I' must not be the end of instruction list (which would
00174   /// cause a degenerate basic block to be formed, having a terminator inside of
00175   /// the basic block).
00176   ///
00177   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
00178 };
00179 
00180 } // End llvm namespace
00181 
00182 #endif