LLVM  mainline
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_IR_BASICBLOCK_H
00015 #define LLVM_IR_BASICBLOCK_H
00016 
00017 #include "llvm/ADT/Twine.h"
00018 #include "llvm/ADT/ilist.h"
00019 #include "llvm/IR/Instruction.h"
00020 #include "llvm/IR/SymbolTableListTraits.h"
00021 #include "llvm/Support/CBindingWrapping.h"
00022 #include "llvm/Support/DataTypes.h"
00023 
00024 namespace llvm {
00025 
00026 class CallInst;
00027 class LandingPadInst;
00028 class TerminatorInst;
00029 class LLVMContext;
00030 class BlockAddress;
00031 
00032 
00033 /// \brief LLVM Basic Block Representation
00034 ///
00035 /// This represents a single basic block in LLVM. A basic block is simply a
00036 /// container of instructions that execute sequentially. Basic blocks are Values
00037 /// because they are referenced by instructions such as branches and switch
00038 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
00039 /// represents a label to which a branch can jump.
00040 ///
00041 /// A well formed basic block is formed of a list of non-terminating
00042 /// instructions followed by a single TerminatorInst instruction.
00043 /// TerminatorInst's may not occur in the middle of basic blocks, and must
00044 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to
00045 /// occur because it may be useful in the intermediate stage of constructing or
00046 /// modifying a program. However, the verifier will ensure that basic blocks
00047 /// are "well formed".
00048 class BasicBlock : public Value, // Basic blocks are data objects also
00049                    public ilist_node<BasicBlock> {
00050   friend class BlockAddress;
00051 public:
00052   typedef iplist<Instruction> InstListType;
00053 private:
00054   InstListType InstList;
00055   Function *Parent;
00056 
00057   void setParent(Function *parent);
00058   friend class SymbolTableListTraits<BasicBlock, Function>;
00059 
00060   BasicBlock(const BasicBlock &) = delete;
00061   void operator=(const BasicBlock &) = delete;
00062 
00063   /// \brief Constructor.
00064   ///
00065   /// If the function parameter is specified, the basic block is automatically
00066   /// inserted at either the end of the function (if InsertBefore is null), or
00067   /// before the specified basic block.
00068   explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
00069                       Function *Parent = nullptr,
00070                       BasicBlock *InsertBefore = nullptr);
00071 public:
00072   /// \brief Get the context in which this basic block lives.
00073   LLVMContext &getContext() const;
00074 
00075   /// Instruction iterators...
00076   typedef InstListType::iterator iterator;
00077   typedef InstListType::const_iterator const_iterator;
00078   typedef InstListType::reverse_iterator reverse_iterator;
00079   typedef InstListType::const_reverse_iterator const_reverse_iterator;
00080 
00081   /// \brief Creates a new BasicBlock.
00082   ///
00083   /// If the Parent parameter is specified, the basic block is automatically
00084   /// inserted at either the end of the function (if InsertBefore is 0), or
00085   /// before the specified basic block.
00086   static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
00087                             Function *Parent = nullptr,
00088                             BasicBlock *InsertBefore = nullptr) {
00089     return new BasicBlock(Context, Name, Parent, InsertBefore);
00090   }
00091   ~BasicBlock();
00092 
00093   /// \brief Return the enclosing method, or null if none.
00094   const Function *getParent() const { return Parent; }
00095         Function *getParent()       { return Parent; }
00096 
00097   /// \brief Return the module owning the function this basic block belongs to,
00098   /// or nullptr it the function does not have a module.
00099   ///
00100   /// Note: this is undefined behavior if the block does not have a parent.
00101   const Module *getModule() const;
00102 
00103   /// \brief Returns the terminator instruction if the block is well formed or
00104   /// null if the block is not well formed.
00105   TerminatorInst *getTerminator();
00106   const TerminatorInst *getTerminator() const;
00107 
00108   /// \brief Returns the call instruction marked 'musttail' prior to the
00109   /// terminating return instruction of this basic block, if such a call is
00110   /// present.  Otherwise, returns null.
00111   CallInst *getTerminatingMustTailCall();
00112   const CallInst *getTerminatingMustTailCall() const {
00113     return const_cast<BasicBlock *>(this)->getTerminatingMustTailCall();
00114   }
00115 
00116   /// \brief Returns a pointer to the first instruction in this block that is
00117   /// not a PHINode instruction.
00118   ///
00119   /// When adding instructions to the beginning of the basic block, they should
00120   /// be added before the returned value, not before the first instruction,
00121   /// which might be PHI. Returns 0 is there's no non-PHI instruction.
00122   Instruction* getFirstNonPHI();
00123   const Instruction* getFirstNonPHI() const {
00124     return const_cast<BasicBlock*>(this)->getFirstNonPHI();
00125   }
00126 
00127   /// \brief Returns a pointer to the first instruction in this block that is not
00128   /// a PHINode or a debug intrinsic.
00129   Instruction* getFirstNonPHIOrDbg();
00130   const Instruction* getFirstNonPHIOrDbg() const {
00131     return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbg();
00132   }
00133 
00134   /// \brief Returns a pointer to the first instruction in this block that is not
00135   /// a PHINode, a debug intrinsic, or a lifetime intrinsic.
00136   Instruction* getFirstNonPHIOrDbgOrLifetime();
00137   const Instruction* getFirstNonPHIOrDbgOrLifetime() const {
00138     return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbgOrLifetime();
00139   }
00140 
00141   /// \brief Returns an iterator to the first instruction in this block that is
00142   /// suitable for inserting a non-PHI instruction.
00143   ///
00144   /// In particular, it skips all PHIs and LandingPad instructions.
00145   iterator getFirstInsertionPt();
00146   const_iterator getFirstInsertionPt() const {
00147     return const_cast<BasicBlock*>(this)->getFirstInsertionPt();
00148   }
00149 
00150   /// \brief Unlink 'this' from the containing function, but do not delete it.
00151   void removeFromParent();
00152 
00153   /// \brief Unlink 'this' from the containing function and delete it.
00154   void eraseFromParent();
00155 
00156   /// \brief Unlink this basic block from its current function and insert it
00157   /// into the function that \p MovePos lives in, right before \p MovePos.
00158   void moveBefore(BasicBlock *MovePos);
00159 
00160   /// \brief Unlink this basic block from its current function and insert it
00161   /// right after \p MovePos in the function \p MovePos lives in.
00162   void moveAfter(BasicBlock *MovePos);
00163 
00164   /// \brief Insert unlinked basic block into a function.
00165   ///
00166   /// Inserts an unlinked basic block into \c Parent.  If \c InsertBefore is
00167   /// provided, inserts before that basic block, otherwise inserts at the end.
00168   ///
00169   /// \pre \a getParent() is \c nullptr.
00170   void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr);
00171 
00172   /// \brief Return the predecessor of this block if it has a single predecessor
00173   /// block. Otherwise return a null pointer.
00174   BasicBlock *getSinglePredecessor();
00175   const BasicBlock *getSinglePredecessor() const {
00176     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
00177   }
00178 
00179   /// \brief Return the predecessor of this block if it has a unique predecessor
00180   /// block. Otherwise return a null pointer.
00181   ///
00182   /// Note that unique predecessor doesn't mean single edge, there can be
00183   /// multiple edges from the unique predecessor to this block (for example a
00184   /// switch statement with multiple cases having the same destination).
00185   BasicBlock *getUniquePredecessor();
00186   const BasicBlock *getUniquePredecessor() const {
00187     return const_cast<BasicBlock*>(this)->getUniquePredecessor();
00188   }
00189 
00190   /// Return the successor of this block if it has a unique successor.
00191   /// Otherwise return a null pointer.  This method is analogous to
00192   /// getUniquePredeccessor above.
00193   BasicBlock *getUniqueSuccessor();
00194   const BasicBlock *getUniqueSuccessor() const {
00195     return const_cast<BasicBlock*>(this)->getUniqueSuccessor();
00196   }
00197 
00198   //===--------------------------------------------------------------------===//
00199   /// Instruction iterator methods
00200   ///
00201   inline iterator                begin()       { return InstList.begin(); }
00202   inline const_iterator          begin() const { return InstList.begin(); }
00203   inline iterator                end  ()       { return InstList.end();   }
00204   inline const_iterator          end  () const { return InstList.end();   }
00205 
00206   inline reverse_iterator        rbegin()       { return InstList.rbegin(); }
00207   inline const_reverse_iterator  rbegin() const { return InstList.rbegin(); }
00208   inline reverse_iterator        rend  ()       { return InstList.rend();   }
00209   inline const_reverse_iterator  rend  () const { return InstList.rend();   }
00210 
00211   inline size_t                   size() const { return InstList.size();  }
00212   inline bool                    empty() const { return InstList.empty(); }
00213   inline const Instruction      &front() const { return InstList.front(); }
00214   inline       Instruction      &front()       { return InstList.front(); }
00215   inline const Instruction       &back() const { return InstList.back();  }
00216   inline       Instruction       &back()       { return InstList.back();  }
00217 
00218   /// \brief Return the underlying instruction list container.
00219   ///
00220   /// Currently you need to access the underlying instruction list container
00221   /// directly if you want to modify it.
00222   const InstListType &getInstList() const { return InstList; }
00223         InstListType &getInstList()       { return InstList; }
00224 
00225   /// \brief Returns a pointer to a member of the instruction list.
00226   static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) {
00227     return &BasicBlock::InstList;
00228   }
00229 
00230   /// \brief Returns a pointer to the symbol table if one exists.
00231   ValueSymbolTable *getValueSymbolTable();
00232 
00233   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
00234   static inline bool classof(const Value *V) {
00235     return V->getValueID() == Value::BasicBlockVal;
00236   }
00237 
00238   /// \brief Cause all subinstructions to "let go" of all the references that
00239   /// said subinstructions are maintaining.
00240   ///
00241   /// This allows one to 'delete' a whole class at a time, even though there may
00242   /// be circular references... first all references are dropped, and all use
00243   /// counts go to zero.  Then everything is delete'd for real.  Note that no
00244   /// operations are valid on an object that has "dropped all references",
00245   /// except operator delete.
00246   void dropAllReferences();
00247 
00248   /// \brief Notify the BasicBlock that the predecessor \p Pred is no longer
00249   /// able to reach it.
00250   ///
00251   /// This is actually not used to update the Predecessor list, but is actually
00252   /// used to update the PHI nodes that reside in the block.  Note that this
00253   /// should be called while the predecessor still refers to this block.
00254   void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
00255 
00256   /// \brief Split the basic block into two basic blocks at the specified
00257   /// instruction.
00258   ///
00259   /// Note that all instructions BEFORE the specified iterator stay as part of
00260   /// the original basic block, an unconditional branch is added to the original
00261   /// BB, and the rest of the instructions in the BB are moved to the new BB,
00262   /// including the old terminator.  The newly formed BasicBlock is returned.
00263   /// This function invalidates the specified iterator.
00264   ///
00265   /// Note that this only works on well formed basic blocks (must have a
00266   /// terminator), and 'I' must not be the end of instruction list (which would
00267   /// cause a degenerate basic block to be formed, having a terminator inside of
00268   /// the basic block).
00269   ///
00270   /// Also note that this doesn't preserve any passes. To split blocks while
00271   /// keeping loop information consistent, use the SplitBlock utility function.
00272   BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "");
00273 
00274   /// \brief Returns true if there are any uses of this basic block other than
00275   /// direct branches, switches, etc. to it.
00276   bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
00277 
00278   /// \brief Update all phi nodes in this basic block's successors to refer to
00279   /// basic block \p New instead of to it.
00280   void replaceSuccessorsPhiUsesWith(BasicBlock *New);
00281 
00282   /// \brief Return true if this basic block is a landing pad.
00283   ///
00284   /// Being a ``landing pad'' means that the basic block is the destination of
00285   /// the 'unwind' edge of an invoke instruction.
00286   bool isLandingPad() const;
00287 
00288   /// \brief Return the landingpad instruction associated with the landing pad.
00289   LandingPadInst *getLandingPadInst();
00290   const LandingPadInst *getLandingPadInst() const;
00291 
00292 private:
00293   /// \brief Increment the internal refcount of the number of BlockAddresses
00294   /// referencing this BasicBlock by \p Amt.
00295   ///
00296   /// This is almost always 0, sometimes one possibly, but almost never 2, and
00297   /// inconceivably 3 or more.
00298   void AdjustBlockAddressRefCount(int Amt) {
00299     setValueSubclassData(getSubclassDataFromValue()+Amt);
00300     assert((int)(signed char)getSubclassDataFromValue() >= 0 &&
00301            "Refcount wrap-around");
00302   }
00303   /// \brief Shadow Value::setValueSubclassData with a private forwarding method
00304   /// so that any future subclasses cannot accidentally use it.
00305   void setValueSubclassData(unsigned short D) {
00306     Value::setValueSubclassData(D);
00307   }
00308 };
00309 
00310 // Create wrappers for C Binding types (see CBindingWrapping.h).
00311 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef)
00312 
00313 } // End llvm namespace
00314 
00315 #endif