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.

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