BasicBlock.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains the declaration of the BasicBlock class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_IR_BASICBLOCK_H
  18. #define LLVM_IR_BASICBLOCK_H
  19. #include "llvm-c/Types.h"
  20. #include "llvm/ADT/Twine.h"
  21. #include "llvm/ADT/ilist.h"
  22. #include "llvm/ADT/ilist_node.h"
  23. #include "llvm/ADT/iterator.h"
  24. #include "llvm/ADT/iterator_range.h"
  25. #include "llvm/IR/Instruction.h"
  26. #include "llvm/IR/SymbolTableListTraits.h"
  27. #include "llvm/IR/Value.h"
  28. #include "llvm/Support/CBindingWrapping.h"
  29. #include "llvm/Support/Casting.h"
  30. #include "llvm/Support/Compiler.h"
  31. #include <cassert>
  32. #include <cstddef>
  33. #include <iterator>
  34. namespace llvm {
  35. class AssemblyAnnotationWriter;
  36. class CallInst;
  37. class Function;
  38. class LandingPadInst;
  39. class LLVMContext;
  40. class Module;
  41. class PHINode;
  42. class ValueSymbolTable;
  43. /// LLVM Basic Block Representation
  44. ///
  45. /// This represents a single basic block in LLVM. A basic block is simply a
  46. /// container of instructions that execute sequentially. Basic blocks are Values
  47. /// because they are referenced by instructions such as branches and switch
  48. /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block
  49. /// represents a label to which a branch can jump.
  50. ///
  51. /// A well formed basic block is formed of a list of non-terminating
  52. /// instructions followed by a single terminator instruction. Terminator
  53. /// instructions may not occur in the middle of basic blocks, and must terminate
  54. /// the blocks. The BasicBlock class allows malformed basic blocks to occur
  55. /// because it may be useful in the intermediate stage of constructing or
  56. /// modifying a program. However, the verifier will ensure that basic blocks are
  57. /// "well formed".
  58. class BasicBlock final : public Value, // Basic blocks are data objects also
  59. public ilist_node_with_parent<BasicBlock, Function> {
  60. public:
  61. using InstListType = SymbolTableList<Instruction>;
  62. private:
  63. friend class BlockAddress;
  64. friend class SymbolTableListTraits<BasicBlock>;
  65. InstListType InstList;
  66. Function *Parent;
  67. void setParent(Function *parent);
  68. /// Constructor.
  69. ///
  70. /// If the function parameter is specified, the basic block is automatically
  71. /// inserted at either the end of the function (if InsertBefore is null), or
  72. /// before the specified basic block.
  73. explicit BasicBlock(LLVMContext &C, const Twine &Name = "",
  74. Function *Parent = nullptr,
  75. BasicBlock *InsertBefore = nullptr);
  76. public:
  77. BasicBlock(const BasicBlock &) = delete;
  78. BasicBlock &operator=(const BasicBlock &) = delete;
  79. ~BasicBlock();
  80. /// Get the context in which this basic block lives.
  81. LLVMContext &getContext() const;
  82. /// Instruction iterators...
  83. using iterator = InstListType::iterator;
  84. using const_iterator = InstListType::const_iterator;
  85. using reverse_iterator = InstListType::reverse_iterator;
  86. using const_reverse_iterator = InstListType::const_reverse_iterator;
  87. /// Creates a new BasicBlock.
  88. ///
  89. /// If the Parent parameter is specified, the basic block is automatically
  90. /// inserted at either the end of the function (if InsertBefore is 0), or
  91. /// before the specified basic block.
  92. static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "",
  93. Function *Parent = nullptr,
  94. BasicBlock *InsertBefore = nullptr) {
  95. return new BasicBlock(Context, Name, Parent, InsertBefore);
  96. }
  97. /// Return the enclosing method, or null if none.
  98. const Function *getParent() const { return Parent; }
  99. Function *getParent() { return Parent; }
  100. /// Return the module owning the function this basic block belongs to, or
  101. /// nullptr if the function does not have a module.
  102. ///
  103. /// Note: this is undefined behavior if the block does not have a parent.
  104. const Module *getModule() const;
  105. Module *getModule() {
  106. return const_cast<Module *>(
  107. static_cast<const BasicBlock *>(this)->getModule());
  108. }
  109. /// Returns the terminator instruction if the block is well formed or null
  110. /// if the block is not well formed.
  111. const Instruction *getTerminator() const LLVM_READONLY;
  112. Instruction *getTerminator() {
  113. return const_cast<Instruction *>(
  114. static_cast<const BasicBlock *>(this)->getTerminator());
  115. }
  116. /// Returns the call instruction calling \@llvm.experimental.deoptimize
  117. /// prior to the terminating return instruction of this basic block, if such
  118. /// a call is present. Otherwise, returns null.
  119. const CallInst *getTerminatingDeoptimizeCall() const;
  120. CallInst *getTerminatingDeoptimizeCall() {
  121. return const_cast<CallInst *>(
  122. static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall());
  123. }
  124. /// Returns the call instruction calling \@llvm.experimental.deoptimize
  125. /// that is present either in current basic block or in block that is a unique
  126. /// successor to current block, if such call is present. Otherwise, returns null.
  127. const CallInst *getPostdominatingDeoptimizeCall() const;
  128. CallInst *getPostdominatingDeoptimizeCall() {
  129. return const_cast<CallInst *>(
  130. static_cast<const BasicBlock *>(this)->getPostdominatingDeoptimizeCall());
  131. }
  132. /// Returns the call instruction marked 'musttail' prior to the terminating
  133. /// return instruction of this basic block, if such a call is present.
  134. /// Otherwise, returns null.
  135. const CallInst *getTerminatingMustTailCall() const;
  136. CallInst *getTerminatingMustTailCall() {
  137. return const_cast<CallInst *>(
  138. static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall());
  139. }
  140. /// Returns a pointer to the first instruction in this block that is not a
  141. /// PHINode instruction.
  142. ///
  143. /// When adding instructions to the beginning of the basic block, they should
  144. /// be added before the returned value, not before the first instruction,
  145. /// which might be PHI. Returns 0 is there's no non-PHI instruction.
  146. const Instruction* getFirstNonPHI() const;
  147. Instruction* getFirstNonPHI() {
  148. return const_cast<Instruction *>(
  149. static_cast<const BasicBlock *>(this)->getFirstNonPHI());
  150. }
  151. /// Returns a pointer to the first instruction in this block that is not a
  152. /// PHINode or a debug intrinsic, or any pseudo operation if \c SkipPseudoOp
  153. /// is true.
  154. const Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) const;
  155. Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) {
  156. return const_cast<Instruction *>(
  157. static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg(
  158. SkipPseudoOp));
  159. }
  160. /// Returns a pointer to the first instruction in this block that is not a
  161. /// PHINode, a debug intrinsic, or a lifetime intrinsic, or any pseudo
  162. /// operation if \c SkipPseudoOp is true.
  163. const Instruction *
  164. getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) const;
  165. Instruction *getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) {
  166. return const_cast<Instruction *>(
  167. static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime(
  168. SkipPseudoOp));
  169. }
  170. /// Returns an iterator to the first instruction in this block that is
  171. /// suitable for inserting a non-PHI instruction.
  172. ///
  173. /// In particular, it skips all PHIs and LandingPad instructions.
  174. const_iterator getFirstInsertionPt() const;
  175. iterator getFirstInsertionPt() {
  176. return static_cast<const BasicBlock *>(this)
  177. ->getFirstInsertionPt().getNonConst();
  178. }
  179. /// Return a const iterator range over the instructions in the block, skipping
  180. /// any debug instructions. Skip any pseudo operations as well if \c
  181. /// SkipPseudoOp is true.
  182. iterator_range<filter_iterator<BasicBlock::const_iterator,
  183. std::function<bool(const Instruction &)>>>
  184. instructionsWithoutDebug(bool SkipPseudoOp = true) const;
  185. /// Return an iterator range over the instructions in the block, skipping any
  186. /// debug instructions. Skip and any pseudo operations as well if \c
  187. /// SkipPseudoOp is true.
  188. iterator_range<
  189. filter_iterator<BasicBlock::iterator, std::function<bool(Instruction &)>>>
  190. instructionsWithoutDebug(bool SkipPseudoOp = true);
  191. /// Return the size of the basic block ignoring debug instructions
  192. filter_iterator<BasicBlock::const_iterator,
  193. std::function<bool(const Instruction &)>>::difference_type
  194. sizeWithoutDebug() const;
  195. /// Unlink 'this' from the containing function, but do not delete it.
  196. void removeFromParent();
  197. /// Unlink 'this' from the containing function and delete it.
  198. ///
  199. // \returns an iterator pointing to the element after the erased one.
  200. SymbolTableList<BasicBlock>::iterator eraseFromParent();
  201. /// Unlink this basic block from its current function and insert it into
  202. /// the function that \p MovePos lives in, right before \p MovePos.
  203. void moveBefore(BasicBlock *MovePos);
  204. /// Unlink this basic block from its current function and insert it
  205. /// right after \p MovePos in the function \p MovePos lives in.
  206. void moveAfter(BasicBlock *MovePos);
  207. /// Insert unlinked basic block into a function.
  208. ///
  209. /// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is
  210. /// provided, inserts before that basic block, otherwise inserts at the end.
  211. ///
  212. /// \pre \a getParent() is \c nullptr.
  213. void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr);
  214. /// Return the predecessor of this block if it has a single predecessor
  215. /// block. Otherwise return a null pointer.
  216. const BasicBlock *getSinglePredecessor() const;
  217. BasicBlock *getSinglePredecessor() {
  218. return const_cast<BasicBlock *>(
  219. static_cast<const BasicBlock *>(this)->getSinglePredecessor());
  220. }
  221. /// Return the predecessor of this block if it has a unique predecessor
  222. /// block. Otherwise return a null pointer.
  223. ///
  224. /// Note that unique predecessor doesn't mean single edge, there can be
  225. /// multiple edges from the unique predecessor to this block (for example a
  226. /// switch statement with multiple cases having the same destination).
  227. const BasicBlock *getUniquePredecessor() const;
  228. BasicBlock *getUniquePredecessor() {
  229. return const_cast<BasicBlock *>(
  230. static_cast<const BasicBlock *>(this)->getUniquePredecessor());
  231. }
  232. /// Return true if this block has exactly N predecessors.
  233. bool hasNPredecessors(unsigned N) const;
  234. /// Return true if this block has N predecessors or more.
  235. bool hasNPredecessorsOrMore(unsigned N) const;
  236. /// Return the successor of this block if it has a single successor.
  237. /// Otherwise return a null pointer.
  238. ///
  239. /// This method is analogous to getSinglePredecessor above.
  240. const BasicBlock *getSingleSuccessor() const;
  241. BasicBlock *getSingleSuccessor() {
  242. return const_cast<BasicBlock *>(
  243. static_cast<const BasicBlock *>(this)->getSingleSuccessor());
  244. }
  245. /// Return the successor of this block if it has a unique successor.
  246. /// Otherwise return a null pointer.
  247. ///
  248. /// This method is analogous to getUniquePredecessor above.
  249. const BasicBlock *getUniqueSuccessor() const;
  250. BasicBlock *getUniqueSuccessor() {
  251. return const_cast<BasicBlock *>(
  252. static_cast<const BasicBlock *>(this)->getUniqueSuccessor());
  253. }
  254. /// Print the basic block to an output stream with an optional
  255. /// AssemblyAnnotationWriter.
  256. void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr,
  257. bool ShouldPreserveUseListOrder = false,
  258. bool IsForDebug = false) const;
  259. //===--------------------------------------------------------------------===//
  260. /// Instruction iterator methods
  261. ///
  262. inline iterator begin() { return InstList.begin(); }
  263. inline const_iterator begin() const { return InstList.begin(); }
  264. inline iterator end () { return InstList.end(); }
  265. inline const_iterator end () const { return InstList.end(); }
  266. inline reverse_iterator rbegin() { return InstList.rbegin(); }
  267. inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
  268. inline reverse_iterator rend () { return InstList.rend(); }
  269. inline const_reverse_iterator rend () const { return InstList.rend(); }
  270. inline size_t size() const { return InstList.size(); }
  271. inline bool empty() const { return InstList.empty(); }
  272. inline const Instruction &front() const { return InstList.front(); }
  273. inline Instruction &front() { return InstList.front(); }
  274. inline const Instruction &back() const { return InstList.back(); }
  275. inline Instruction &back() { return InstList.back(); }
  276. /// Iterator to walk just the phi nodes in the basic block.
  277. template <typename PHINodeT = PHINode, typename BBIteratorT = iterator>
  278. class phi_iterator_impl
  279. : public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>,
  280. std::forward_iterator_tag, PHINodeT> {
  281. friend BasicBlock;
  282. PHINodeT *PN;
  283. phi_iterator_impl(PHINodeT *PN) : PN(PN) {}
  284. public:
  285. // Allow default construction to build variables, but this doesn't build
  286. // a useful iterator.
  287. phi_iterator_impl() = default;
  288. // Allow conversion between instantiations where valid.
  289. template <typename PHINodeU, typename BBIteratorU,
  290. typename = std::enable_if_t<
  291. std::is_convertible<PHINodeU *, PHINodeT *>::value>>
  292. phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg)
  293. : PN(Arg.PN) {}
  294. bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; }
  295. PHINodeT &operator*() const { return *PN; }
  296. using phi_iterator_impl::iterator_facade_base::operator++;
  297. phi_iterator_impl &operator++() {
  298. assert(PN && "Cannot increment the end iterator!");
  299. PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN)));
  300. return *this;
  301. }
  302. };
  303. using phi_iterator = phi_iterator_impl<>;
  304. using const_phi_iterator =
  305. phi_iterator_impl<const PHINode, BasicBlock::const_iterator>;
  306. /// Returns a range that iterates over the phis in the basic block.
  307. ///
  308. /// Note that this cannot be used with basic blocks that have no terminator.
  309. iterator_range<const_phi_iterator> phis() const {
  310. return const_cast<BasicBlock *>(this)->phis();
  311. }
  312. iterator_range<phi_iterator> phis();
  313. /// Return the underlying instruction list container.
  314. ///
  315. /// Currently you need to access the underlying instruction list container
  316. /// directly if you want to modify it.
  317. const InstListType &getInstList() const { return InstList; }
  318. InstListType &getInstList() { return InstList; }
  319. /// Returns a pointer to a member of the instruction list.
  320. static InstListType BasicBlock::*getSublistAccess(Instruction*) {
  321. return &BasicBlock::InstList;
  322. }
  323. /// Returns a pointer to the symbol table if one exists.
  324. ValueSymbolTable *getValueSymbolTable();
  325. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  326. static bool classof(const Value *V) {
  327. return V->getValueID() == Value::BasicBlockVal;
  328. }
  329. /// Cause all subinstructions to "let go" of all the references that said
  330. /// subinstructions are maintaining.
  331. ///
  332. /// This allows one to 'delete' a whole class at a time, even though there may
  333. /// be circular references... first all references are dropped, and all use
  334. /// counts go to zero. Then everything is delete'd for real. Note that no
  335. /// operations are valid on an object that has "dropped all references",
  336. /// except operator delete.
  337. void dropAllReferences();
  338. /// Update PHI nodes in this BasicBlock before removal of predecessor \p Pred.
  339. /// Note that this function does not actually remove the predecessor.
  340. ///
  341. /// If \p KeepOneInputPHIs is true then don't remove PHIs that are left with
  342. /// zero or one incoming values, and don't simplify PHIs with all incoming
  343. /// values the same.
  344. void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs = false);
  345. bool canSplitPredecessors() const;
  346. /// Split the basic block into two basic blocks at the specified instruction.
  347. ///
  348. /// If \p Before is true, splitBasicBlockBefore handles the
  349. /// block splitting. Otherwise, execution proceeds as described below.
  350. ///
  351. /// Note that all instructions BEFORE the specified iterator
  352. /// stay as part of the original basic block, an unconditional branch is added
  353. /// to the original BB, and the rest of the instructions in the BB are moved
  354. /// to the new BB, including the old terminator. The newly formed basic block
  355. /// is returned. This function invalidates the specified iterator.
  356. ///
  357. /// Note that this only works on well formed basic blocks (must have a
  358. /// terminator), and \p 'I' must not be the end of instruction list (which
  359. /// would cause a degenerate basic block to be formed, having a terminator
  360. /// inside of the basic block).
  361. ///
  362. /// Also note that this doesn't preserve any passes. To split blocks while
  363. /// keeping loop information consistent, use the SplitBlock utility function.
  364. BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "",
  365. bool Before = false);
  366. BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "",
  367. bool Before = false) {
  368. return splitBasicBlock(I->getIterator(), BBName, Before);
  369. }
  370. /// Split the basic block into two basic blocks at the specified instruction
  371. /// and insert the new basic blocks as the predecessor of the current block.
  372. ///
  373. /// This function ensures all instructions AFTER and including the specified
  374. /// iterator \p I are part of the original basic block. All Instructions
  375. /// BEFORE the iterator \p I are moved to the new BB and an unconditional
  376. /// branch is added to the new BB. The new basic block is returned.
  377. ///
  378. /// Note that this only works on well formed basic blocks (must have a
  379. /// terminator), and \p 'I' must not be the end of instruction list (which
  380. /// would cause a degenerate basic block to be formed, having a terminator
  381. /// inside of the basic block). \p 'I' cannot be a iterator for a PHINode
  382. /// with multiple incoming blocks.
  383. ///
  384. /// Also note that this doesn't preserve any passes. To split blocks while
  385. /// keeping loop information consistent, use the SplitBlockBefore utility
  386. /// function.
  387. BasicBlock *splitBasicBlockBefore(iterator I, const Twine &BBName = "");
  388. BasicBlock *splitBasicBlockBefore(Instruction *I, const Twine &BBName = "") {
  389. return splitBasicBlockBefore(I->getIterator(), BBName);
  390. }
  391. /// Returns true if there are any uses of this basic block other than
  392. /// direct branches, switches, etc. to it.
  393. bool hasAddressTaken() const {
  394. return getBasicBlockBits().BlockAddressRefCount != 0;
  395. }
  396. /// Update all phi nodes in this basic block to refer to basic block \p New
  397. /// instead of basic block \p Old.
  398. void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New);
  399. /// Update all phi nodes in this basic block's successors to refer to basic
  400. /// block \p New instead of basic block \p Old.
  401. void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New);
  402. /// Update all phi nodes in this basic block's successors to refer to basic
  403. /// block \p New instead of to it.
  404. void replaceSuccessorsPhiUsesWith(BasicBlock *New);
  405. /// Return true if this basic block is an exception handling block.
  406. bool isEHPad() const { return getFirstNonPHI()->isEHPad(); }
  407. /// Return true if this basic block is a landing pad.
  408. ///
  409. /// Being a ``landing pad'' means that the basic block is the destination of
  410. /// the 'unwind' edge of an invoke instruction.
  411. bool isLandingPad() const;
  412. /// Return the landingpad instruction associated with the landing pad.
  413. const LandingPadInst *getLandingPadInst() const;
  414. LandingPadInst *getLandingPadInst() {
  415. return const_cast<LandingPadInst *>(
  416. static_cast<const BasicBlock *>(this)->getLandingPadInst());
  417. }
  418. /// Return true if it is legal to hoist instructions into this block.
  419. bool isLegalToHoistInto() const;
  420. /// Return true if this is the entry block of the containing function.
  421. /// This method can only be used on blocks that have a parent function.
  422. bool isEntryBlock() const;
  423. Optional<uint64_t> getIrrLoopHeaderWeight() const;
  424. /// Returns true if the Order field of child Instructions is valid.
  425. bool isInstrOrderValid() const {
  426. return getBasicBlockBits().InstrOrderValid;
  427. }
  428. /// Mark instruction ordering invalid. Done on every instruction insert.
  429. void invalidateOrders() {
  430. validateInstrOrdering();
  431. BasicBlockBits Bits = getBasicBlockBits();
  432. Bits.InstrOrderValid = false;
  433. setBasicBlockBits(Bits);
  434. }
  435. /// Renumber instructions and mark the ordering as valid.
  436. void renumberInstructions();
  437. /// Asserts that instruction order numbers are marked invalid, or that they
  438. /// are in ascending order. This is constant time if the ordering is invalid,
  439. /// and linear in the number of instructions if the ordering is valid. Callers
  440. /// should be careful not to call this in ways that make common operations
  441. /// O(n^2). For example, it takes O(n) time to assign order numbers to
  442. /// instructions, so the order should be validated no more than once after
  443. /// each ordering to ensure that transforms have the same algorithmic
  444. /// complexity when asserts are enabled as when they are disabled.
  445. void validateInstrOrdering() const;
  446. private:
  447. #if defined(_AIX) && (!defined(__GNUC__) || defined(__clang__))
  448. // Except for GCC; by default, AIX compilers store bit-fields in 4-byte words
  449. // and give the `pack` pragma push semantics.
  450. #define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)")
  451. #define END_TWO_BYTE_PACK() _Pragma("pack(pop)")
  452. #else
  453. #define BEGIN_TWO_BYTE_PACK()
  454. #define END_TWO_BYTE_PACK()
  455. #endif
  456. BEGIN_TWO_BYTE_PACK()
  457. /// Bitfield to help interpret the bits in Value::SubclassData.
  458. struct BasicBlockBits {
  459. unsigned short BlockAddressRefCount : 15;
  460. unsigned short InstrOrderValid : 1;
  461. };
  462. END_TWO_BYTE_PACK()
  463. #undef BEGIN_TWO_BYTE_PACK
  464. #undef END_TWO_BYTE_PACK
  465. /// Safely reinterpret the subclass data bits to a more useful form.
  466. BasicBlockBits getBasicBlockBits() const {
  467. static_assert(sizeof(BasicBlockBits) == sizeof(unsigned short),
  468. "too many bits for Value::SubclassData");
  469. unsigned short ValueData = getSubclassDataFromValue();
  470. BasicBlockBits AsBits;
  471. memcpy(&AsBits, &ValueData, sizeof(AsBits));
  472. return AsBits;
  473. }
  474. /// Reinterpret our subclass bits and store them back into Value.
  475. void setBasicBlockBits(BasicBlockBits AsBits) {
  476. unsigned short D;
  477. memcpy(&D, &AsBits, sizeof(D));
  478. Value::setValueSubclassData(D);
  479. }
  480. /// Increment the internal refcount of the number of BlockAddresses
  481. /// referencing this BasicBlock by \p Amt.
  482. ///
  483. /// This is almost always 0, sometimes one possibly, but almost never 2, and
  484. /// inconceivably 3 or more.
  485. void AdjustBlockAddressRefCount(int Amt) {
  486. BasicBlockBits Bits = getBasicBlockBits();
  487. Bits.BlockAddressRefCount += Amt;
  488. setBasicBlockBits(Bits);
  489. assert(Bits.BlockAddressRefCount < 255 && "Refcount wrap-around");
  490. }
  491. /// Shadow Value::setValueSubclassData with a private forwarding method so
  492. /// that any future subclasses cannot accidentally use it.
  493. void setValueSubclassData(unsigned short D) {
  494. Value::setValueSubclassData(D);
  495. }
  496. };
  497. // Create wrappers for C Binding types (see CBindingWrapping.h).
  498. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef)
  499. /// Advance \p It while it points to a debug instruction and return the result.
  500. /// This assumes that \p It is not at the end of a block.
  501. BasicBlock::iterator skipDebugIntrinsics(BasicBlock::iterator It);
  502. #ifdef NDEBUG
  503. /// In release builds, this is a no-op. For !NDEBUG builds, the checks are
  504. /// implemented in the .cpp file to avoid circular header deps.
  505. inline void BasicBlock::validateInstrOrdering() const {}
  506. #endif
  507. } // end namespace llvm
  508. #endif // LLVM_IR_BASICBLOCK_H
  509. #ifdef __GNUC__
  510. #pragma GCC diagnostic pop
  511. #endif