BasicBlock.h 15 KB

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