Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
13 KiB

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