LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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   // createNode is used to create a node that marks the end of the list...
00044   static Instruction *createNode();
00045   static iplist<Instruction> &getList(BasicBlock *BB);
00046 };
00047 
00048 class BasicBlock : public Value {       // Basic blocks are data objects also
00049 public:
00050   typedef iplist<Instruction> InstListType;
00051 private :
00052   InstListType InstList;
00053   BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
00054 
00055   void setParent(Function *parent);
00056   void setNext(BasicBlock *N) { Next = N; }
00057   void setPrev(BasicBlock *N) { Prev = N; }
00058   friend class SymbolTableListTraits<BasicBlock, Function, Function>;
00059 
00060   BasicBlock(const BasicBlock &);     // Do not implement
00061   void operator=(const BasicBlock &); // Do not implement
00062 
00063 public:
00064   /// Instruction iterators...
00065   typedef InstListType::iterator                              iterator;
00066   typedef InstListType::const_iterator                  const_iterator;
00067   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00068   typedef std::reverse_iterator<iterator>             reverse_iterator;
00069 
00070   /// BasicBlock ctor - If the function parameter is specified, the basic block
00071   /// is automatically inserted at either the end of the function (if
00072   /// InsertBefore is null), or before the specified basic block.
00073   ///
00074   BasicBlock(const std::string &Name = "", Function *Parent = 0,
00075              BasicBlock *InsertBefore = 0);
00076   ~BasicBlock();
00077 
00078   // Specialize setName to take care of symbol table majik
00079   virtual void setName(const std::string &name, SymbolTable *ST = 0);
00080 
00081   /// getParent - Return the enclosing method, or null if none
00082   ///
00083   const Function *getParent() const { return InstList.getParent(); }
00084         Function *getParent()       { return InstList.getParent(); }
00085 
00086   // getNext/Prev - Return the next or previous basic block in the list.
00087         BasicBlock *getNext()       { return Next; }
00088   const BasicBlock *getNext() const { return Next; }
00089         BasicBlock *getPrev()       { return Prev; }
00090   const BasicBlock *getPrev() const { return Prev; }
00091 
00092   /// getTerminator() - If this is a well formed basic block, then this returns
00093   /// a pointer to the terminator instruction.  If it is not, then you get a
00094   /// null pointer back.
00095   ///
00096   TerminatorInst *getTerminator();
00097   const TerminatorInst *const getTerminator() const;
00098   
00099   /// removeFromParent - This method unlinks 'this' from the containing
00100   /// function, but does not delete it.
00101   ///
00102   void removeFromParent();
00103 
00104   /// eraseFromParent - This method unlinks 'this' from the containing function
00105   /// and deletes it.
00106   ///
00107   void eraseFromParent();
00108 
00109 
00110 
00111   //===--------------------------------------------------------------------===//
00112   /// Instruction iterator methods
00113   ///
00114   inline iterator                begin()       { return InstList.begin(); }
00115   inline const_iterator          begin() const { return InstList.begin(); }
00116   inline iterator                end  ()       { return InstList.end();   }
00117   inline const_iterator          end  () const { return InstList.end();   }
00118 
00119   inline reverse_iterator       rbegin()       { return InstList.rbegin(); }
00120   inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
00121   inline reverse_iterator       rend  ()       { return InstList.rend();   }
00122   inline const_reverse_iterator rend  () const { return InstList.rend();   }
00123 
00124   inline size_t                   size() const { return InstList.size();  }
00125   inline bool                    empty() const { return InstList.empty(); }
00126   inline const Instruction      &front() const { return InstList.front(); }
00127   inline       Instruction      &front()       { return InstList.front(); }
00128   inline const Instruction       &back() const { return InstList.back();  }
00129   inline       Instruction       &back()       { return InstList.back();  }
00130 
00131   /// getInstList() - Return the underlying instruction list container.  You
00132   /// need to access it directly if you want to modify it currently.
00133   ///
00134   const InstListType &getInstList() const { return InstList; }
00135         InstListType &getInstList()       { return InstList; }
00136 
00137   virtual void print(std::ostream &OS) const { print(OS, 0); }
00138   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
00139 
00140   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00141   static inline bool classof(const BasicBlock *BB) { return true; }
00142   static inline bool classof(const Value *V) {
00143     return V->getValueType() == Value::BasicBlockVal;
00144   }
00145 
00146   /// dropAllReferences() - This function causes all the subinstructions to "let
00147   /// go" of all references that they are maintaining.  This allows one to
00148   /// 'delete' a whole class at a time, even though there may be circular
00149   /// references... first all references are dropped, and all use counts go to
00150   /// zero.  Then everything is delete'd for real.  Note that no operations are
00151   /// valid on an object that has "dropped all references", except operator 
00152   /// delete.
00153   ///
00154   void dropAllReferences();
00155 
00156   /// removePredecessor - This method is used to notify a BasicBlock that the
00157   /// specified Predecessor of the block is no longer able to reach it.  This is
00158   /// actually not used to update the Predecessor list, but is actually used to 
00159   /// update the PHI nodes that reside in the block.  Note that this should be
00160   /// called while the predecessor still refers to this block.
00161   ///
00162   void removePredecessor(BasicBlock *Pred);
00163 
00164   /// splitBasicBlock - This splits a basic block into two at the specified
00165   /// instruction.  Note that all instructions BEFORE the specified iterator
00166   /// stay as part of the original basic block, an unconditional branch is added
00167   /// to the new BB, and the rest of the instructions in the BB are moved to the
00168   /// new BB, including the old terminator.  The newly formed BasicBlock is
00169   /// returned.  This function invalidates the specified iterator.
00170   ///
00171   /// Note that this only works on well formed basic blocks (must have a 
00172   /// terminator), and 'I' must not be the end of instruction list (which would
00173   /// cause a degenerate basic block to be formed, having a terminator inside of
00174   /// the basic block).
00175   ///
00176   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
00177 };
00178 
00179 } // End llvm namespace
00180 
00181 #endif