MachineBasicBlock.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- 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. // Collect the sequence of machine instructions for a basic block.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
  14. #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
  15. #include "llvm/ADT/GraphTraits.h"
  16. #include "llvm/CodeGen/MachineInstr.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include <functional>
  19. namespace llvm {
  20. class Pass;
  21. class BasicBlock;
  22. class MachineFunction;
  23. class MCSymbol;
  24. class SlotIndexes;
  25. class StringRef;
  26. class raw_ostream;
  27. class MachineBranchProbabilityInfo;
  28. template <>
  29. struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
  30. private:
  31. mutable ilist_half_node<MachineInstr> Sentinel;
  32. // this is only set by the MachineBasicBlock owning the LiveList
  33. friend class MachineBasicBlock;
  34. MachineBasicBlock* Parent;
  35. public:
  36. MachineInstr *createSentinel() const {
  37. return static_cast<MachineInstr*>(&Sentinel);
  38. }
  39. void destroySentinel(MachineInstr *) const {}
  40. MachineInstr *provideInitialHead() const { return createSentinel(); }
  41. MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
  42. static void noteHead(MachineInstr*, MachineInstr*) {}
  43. void addNodeToList(MachineInstr* N);
  44. void removeNodeFromList(MachineInstr* N);
  45. void transferNodesFromList(ilist_traits &SrcTraits,
  46. ilist_iterator<MachineInstr> first,
  47. ilist_iterator<MachineInstr> last);
  48. void deleteNode(MachineInstr *N);
  49. private:
  50. void createNode(const MachineInstr &);
  51. };
  52. class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
  53. typedef ilist<MachineInstr> Instructions;
  54. Instructions Insts;
  55. const BasicBlock *BB;
  56. int Number;
  57. MachineFunction *xParent;
  58. /// Predecessors/Successors - Keep track of the predecessor / successor
  59. /// basicblocks.
  60. std::vector<MachineBasicBlock *> Predecessors;
  61. std::vector<MachineBasicBlock *> Successors;
  62. /// Weights - Keep track of the weights to the successors. This vector
  63. /// has the same order as Successors, or it is empty if we don't use it
  64. /// (disable optimization).
  65. std::vector<uint32_t> Weights;
  66. typedef std::vector<uint32_t>::iterator weight_iterator;
  67. typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
  68. /// LiveIns - Keep track of the physical registers that are livein of
  69. /// the basicblock.
  70. std::vector<unsigned> LiveIns;
  71. /// Alignment - Alignment of the basic block. Zero if the basic block does
  72. /// not need to be aligned.
  73. /// The alignment is specified as log2(bytes).
  74. unsigned Alignment;
  75. /// IsLandingPad - Indicate that this basic block is entered via an
  76. /// exception handler.
  77. bool IsLandingPad;
  78. /// AddressTaken - Indicate that this basic block is potentially the
  79. /// target of an indirect branch.
  80. bool AddressTaken;
  81. /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
  82. /// is only computed once and is cached.
  83. mutable MCSymbol *CachedMCSymbol;
  84. // Intrusive list support
  85. MachineBasicBlock() {}
  86. explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
  87. ~MachineBasicBlock();
  88. // MachineBasicBlocks are allocated and owned by MachineFunction.
  89. friend class MachineFunction;
  90. public:
  91. /// getBasicBlock - Return the LLVM basic block that this instance
  92. /// corresponded to originally. Note that this may be NULL if this instance
  93. /// does not correspond directly to an LLVM basic block.
  94. ///
  95. const BasicBlock *getBasicBlock() const { return BB; }
  96. /// getName - Return the name of the corresponding LLVM basic block, or
  97. /// "(null)".
  98. StringRef getName() const;
  99. /// getFullName - Return a formatted string to identify this block and its
  100. /// parent function.
  101. std::string getFullName() const;
  102. /// hasAddressTaken - Test whether this block is potentially the target
  103. /// of an indirect branch.
  104. bool hasAddressTaken() const { return AddressTaken; }
  105. /// setHasAddressTaken - Set this block to reflect that it potentially
  106. /// is the target of an indirect branch.
  107. void setHasAddressTaken() { AddressTaken = true; }
  108. /// getParent - Return the MachineFunction containing this basic block.
  109. ///
  110. const MachineFunction *getParent() const { return xParent; }
  111. MachineFunction *getParent() { return xParent; }
  112. /// bundle_iterator - MachineBasicBlock iterator that automatically skips over
  113. /// MIs that are inside bundles (i.e. walk top level MIs only).
  114. template<typename Ty, typename IterTy>
  115. class bundle_iterator
  116. : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
  117. IterTy MII;
  118. public:
  119. bundle_iterator(IterTy mii) : MII(mii) {}
  120. bundle_iterator(Ty &mi) : MII(mi) {
  121. assert(!mi.isBundledWithPred() &&
  122. "It's not legal to initialize bundle_iterator with a bundled MI");
  123. }
  124. bundle_iterator(Ty *mi) : MII(mi) {
  125. assert((!mi || !mi->isBundledWithPred()) &&
  126. "It's not legal to initialize bundle_iterator with a bundled MI");
  127. }
  128. // Template allows conversion from const to nonconst.
  129. template<class OtherTy, class OtherIterTy>
  130. bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
  131. : MII(I.getInstrIterator()) {}
  132. bundle_iterator() : MII(nullptr) {}
  133. Ty &operator*() const { return *MII; }
  134. Ty *operator->() const { return &operator*(); }
  135. operator Ty*() const { return MII; }
  136. bool operator==(const bundle_iterator &x) const {
  137. return MII == x.MII;
  138. }
  139. bool operator!=(const bundle_iterator &x) const {
  140. return !operator==(x);
  141. }
  142. // Increment and decrement operators...
  143. bundle_iterator &operator--() { // predecrement - Back up
  144. do --MII;
  145. while (MII->isBundledWithPred());
  146. return *this;
  147. }
  148. bundle_iterator &operator++() { // preincrement - Advance
  149. while (MII->isBundledWithSucc())
  150. ++MII;
  151. ++MII;
  152. return *this;
  153. }
  154. bundle_iterator operator--(int) { // postdecrement operators...
  155. bundle_iterator tmp = *this;
  156. --*this;
  157. return tmp;
  158. }
  159. bundle_iterator operator++(int) { // postincrement operators...
  160. bundle_iterator tmp = *this;
  161. ++*this;
  162. return tmp;
  163. }
  164. IterTy getInstrIterator() const {
  165. return MII;
  166. }
  167. };
  168. typedef Instructions::iterator instr_iterator;
  169. typedef Instructions::const_iterator const_instr_iterator;
  170. typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator;
  171. typedef
  172. std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator;
  173. typedef
  174. bundle_iterator<MachineInstr,instr_iterator> iterator;
  175. typedef
  176. bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator;
  177. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  178. typedef std::reverse_iterator<iterator> reverse_iterator;
  179. unsigned size() const { return (unsigned)Insts.size(); }
  180. bool empty() const { return Insts.empty(); }
  181. MachineInstr &instr_front() { return Insts.front(); }
  182. MachineInstr &instr_back() { return Insts.back(); }
  183. const MachineInstr &instr_front() const { return Insts.front(); }
  184. const MachineInstr &instr_back() const { return Insts.back(); }
  185. MachineInstr &front() { return Insts.front(); }
  186. MachineInstr &back() { return *--end(); }
  187. const MachineInstr &front() const { return Insts.front(); }
  188. const MachineInstr &back() const { return *--end(); }
  189. instr_iterator instr_begin() { return Insts.begin(); }
  190. const_instr_iterator instr_begin() const { return Insts.begin(); }
  191. instr_iterator instr_end() { return Insts.end(); }
  192. const_instr_iterator instr_end() const { return Insts.end(); }
  193. reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
  194. const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
  195. reverse_instr_iterator instr_rend () { return Insts.rend(); }
  196. const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
  197. iterator begin() { return instr_begin(); }
  198. const_iterator begin() const { return instr_begin(); }
  199. iterator end () { return instr_end(); }
  200. const_iterator end () const { return instr_end(); }
  201. reverse_iterator rbegin() { return instr_rbegin(); }
  202. const_reverse_iterator rbegin() const { return instr_rbegin(); }
  203. reverse_iterator rend () { return instr_rend(); }
  204. const_reverse_iterator rend () const { return instr_rend(); }
  205. inline iterator_range<iterator> terminators() {
  206. return iterator_range<iterator>(getFirstTerminator(), end());
  207. }
  208. inline iterator_range<const_iterator> terminators() const {
  209. return iterator_range<const_iterator>(getFirstTerminator(), end());
  210. }
  211. // Machine-CFG iterators
  212. typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
  213. typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
  214. typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
  215. typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
  216. typedef std::vector<MachineBasicBlock *>::reverse_iterator
  217. pred_reverse_iterator;
  218. typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
  219. const_pred_reverse_iterator;
  220. typedef std::vector<MachineBasicBlock *>::reverse_iterator
  221. succ_reverse_iterator;
  222. typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
  223. const_succ_reverse_iterator;
  224. pred_iterator pred_begin() { return Predecessors.begin(); }
  225. const_pred_iterator pred_begin() const { return Predecessors.begin(); }
  226. pred_iterator pred_end() { return Predecessors.end(); }
  227. const_pred_iterator pred_end() const { return Predecessors.end(); }
  228. pred_reverse_iterator pred_rbegin()
  229. { return Predecessors.rbegin();}
  230. const_pred_reverse_iterator pred_rbegin() const
  231. { return Predecessors.rbegin();}
  232. pred_reverse_iterator pred_rend()
  233. { return Predecessors.rend(); }
  234. const_pred_reverse_iterator pred_rend() const
  235. { return Predecessors.rend(); }
  236. unsigned pred_size() const {
  237. return (unsigned)Predecessors.size();
  238. }
  239. bool pred_empty() const { return Predecessors.empty(); }
  240. succ_iterator succ_begin() { return Successors.begin(); }
  241. const_succ_iterator succ_begin() const { return Successors.begin(); }
  242. succ_iterator succ_end() { return Successors.end(); }
  243. const_succ_iterator succ_end() const { return Successors.end(); }
  244. succ_reverse_iterator succ_rbegin()
  245. { return Successors.rbegin(); }
  246. const_succ_reverse_iterator succ_rbegin() const
  247. { return Successors.rbegin(); }
  248. succ_reverse_iterator succ_rend()
  249. { return Successors.rend(); }
  250. const_succ_reverse_iterator succ_rend() const
  251. { return Successors.rend(); }
  252. unsigned succ_size() const {
  253. return (unsigned)Successors.size();
  254. }
  255. bool succ_empty() const { return Successors.empty(); }
  256. inline iterator_range<pred_iterator> predecessors() {
  257. return iterator_range<pred_iterator>(pred_begin(), pred_end());
  258. }
  259. inline iterator_range<const_pred_iterator> predecessors() const {
  260. return iterator_range<const_pred_iterator>(pred_begin(), pred_end());
  261. }
  262. inline iterator_range<succ_iterator> successors() {
  263. return iterator_range<succ_iterator>(succ_begin(), succ_end());
  264. }
  265. inline iterator_range<const_succ_iterator> successors() const {
  266. return iterator_range<const_succ_iterator>(succ_begin(), succ_end());
  267. }
  268. // LiveIn management methods.
  269. /// Adds the specified register as a live in. Note that it is an error to add
  270. /// the same register to the same set more than once unless the intention is
  271. /// to call sortUniqueLiveIns after all registers are added.
  272. void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
  273. /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
  274. /// this than repeatedly calling isLiveIn before calling addLiveIn for every
  275. /// LiveIn insertion.
  276. void sortUniqueLiveIns() {
  277. std::sort(LiveIns.begin(), LiveIns.end());
  278. LiveIns.erase(std::unique(LiveIns.begin(), LiveIns.end()), LiveIns.end());
  279. }
  280. /// Add PhysReg as live in to this block, and ensure that there is a copy of
  281. /// PhysReg to a virtual register of class RC. Return the virtual register
  282. /// that is a copy of the live in PhysReg.
  283. unsigned addLiveIn(unsigned PhysReg, const TargetRegisterClass *RC);
  284. /// removeLiveIn - Remove the specified register from the live in set.
  285. ///
  286. void removeLiveIn(unsigned Reg);
  287. /// isLiveIn - Return true if the specified register is in the live in set.
  288. ///
  289. bool isLiveIn(unsigned Reg) const;
  290. // Iteration support for live in sets. These sets are kept in sorted
  291. // order by their register number.
  292. typedef std::vector<unsigned>::const_iterator livein_iterator;
  293. livein_iterator livein_begin() const { return LiveIns.begin(); }
  294. livein_iterator livein_end() const { return LiveIns.end(); }
  295. bool livein_empty() const { return LiveIns.empty(); }
  296. /// getAlignment - Return alignment of the basic block.
  297. /// The alignment is specified as log2(bytes).
  298. ///
  299. unsigned getAlignment() const { return Alignment; }
  300. /// setAlignment - Set alignment of the basic block.
  301. /// The alignment is specified as log2(bytes).
  302. ///
  303. void setAlignment(unsigned Align) { Alignment = Align; }
  304. /// isLandingPad - Returns true if the block is a landing pad. That is
  305. /// this basic block is entered via an exception handler.
  306. bool isLandingPad() const { return IsLandingPad; }
  307. /// setIsLandingPad - Indicates the block is a landing pad. That is
  308. /// this basic block is entered via an exception handler.
  309. void setIsLandingPad(bool V = true) { IsLandingPad = V; }
  310. /// getLandingPadSuccessor - If this block has a successor that is a landing
  311. /// pad, return it. Otherwise return NULL.
  312. const MachineBasicBlock *getLandingPadSuccessor() const;
  313. // Code Layout methods.
  314. /// moveBefore/moveAfter - move 'this' block before or after the specified
  315. /// block. This only moves the block, it does not modify the CFG or adjust
  316. /// potential fall-throughs at the end of the block.
  317. void moveBefore(MachineBasicBlock *NewAfter);
  318. void moveAfter(MachineBasicBlock *NewBefore);
  319. /// updateTerminator - Update the terminator instructions in block to account
  320. /// for changes to the layout. If the block previously used a fallthrough,
  321. /// it may now need a branch, and if it previously used branching it may now
  322. /// be able to use a fallthrough.
  323. void updateTerminator();
  324. // Machine-CFG mutators
  325. /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
  326. /// The Predecessors list of succ is automatically updated. WEIGHT
  327. /// parameter is stored in Weights list and it may be used by
  328. /// MachineBranchProbabilityInfo analysis to calculate branch probability.
  329. ///
  330. /// Note that duplicate Machine CFG edges are not allowed.
  331. ///
  332. void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
  333. /// Set successor weight of a given iterator.
  334. void setSuccWeight(succ_iterator I, uint32_t weight);
  335. /// removeSuccessor - Remove successor from the successors list of this
  336. /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
  337. ///
  338. void removeSuccessor(MachineBasicBlock *succ);
  339. /// removeSuccessor - Remove specified successor from the successors list of
  340. /// this MachineBasicBlock. The Predecessors list of succ is automatically
  341. /// updated. Return the iterator to the element after the one removed.
  342. ///
  343. succ_iterator removeSuccessor(succ_iterator I);
  344. /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
  345. ///
  346. void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
  347. /// transferSuccessors - Transfers all the successors from MBB to this
  348. /// machine basic block (i.e., copies all the successors fromMBB and
  349. /// remove all the successors from fromMBB).
  350. void transferSuccessors(MachineBasicBlock *fromMBB);
  351. /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
  352. /// in transferSuccessors, and update PHI operands in the successor blocks
  353. /// which refer to fromMBB to refer to this.
  354. void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
  355. /// isPredecessor - Return true if the specified MBB is a predecessor of this
  356. /// block.
  357. bool isPredecessor(const MachineBasicBlock *MBB) const;
  358. /// isSuccessor - Return true if the specified MBB is a successor of this
  359. /// block.
  360. bool isSuccessor(const MachineBasicBlock *MBB) const;
  361. /// isLayoutSuccessor - Return true if the specified MBB will be emitted
  362. /// immediately after this block, such that if this block exits by
  363. /// falling through, control will transfer to the specified MBB. Note
  364. /// that MBB need not be a successor at all, for example if this block
  365. /// ends with an unconditional branch to some other block.
  366. bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
  367. /// canFallThrough - Return true if the block can implicitly transfer
  368. /// control to the block after it by falling off the end of it. This should
  369. /// return false if it can reach the block after it, but it uses an explicit
  370. /// branch to do so (e.g., a table jump). True is a conservative answer.
  371. bool canFallThrough();
  372. /// Returns a pointer to the first instruction in this block that is not a
  373. /// PHINode instruction. When adding instructions to the beginning of the
  374. /// basic block, they should be added before the returned value, not before
  375. /// the first instruction, which might be PHI.
  376. /// Returns end() is there's no non-PHI instruction.
  377. iterator getFirstNonPHI();
  378. /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
  379. /// not a PHI or a label. This is the correct point to insert copies at the
  380. /// beginning of a basic block.
  381. iterator SkipPHIsAndLabels(iterator I);
  382. /// getFirstTerminator - returns an iterator to the first terminator
  383. /// instruction of this basic block. If a terminator does not exist,
  384. /// it returns end()
  385. iterator getFirstTerminator();
  386. const_iterator getFirstTerminator() const {
  387. return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
  388. }
  389. /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
  390. /// and return an instr_iterator instead.
  391. instr_iterator getFirstInstrTerminator();
  392. /// getFirstNonDebugInstr - returns an iterator to the first non-debug
  393. /// instruction in the basic block, or end()
  394. iterator getFirstNonDebugInstr();
  395. const_iterator getFirstNonDebugInstr() const {
  396. return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
  397. }
  398. /// getLastNonDebugInstr - returns an iterator to the last non-debug
  399. /// instruction in the basic block, or end()
  400. iterator getLastNonDebugInstr();
  401. const_iterator getLastNonDebugInstr() const {
  402. return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
  403. }
  404. /// SplitCriticalEdge - Split the critical edge from this block to the
  405. /// given successor block, and return the newly created block, or null
  406. /// if splitting is not possible.
  407. ///
  408. /// This function updates LiveVariables, MachineDominatorTree, and
  409. /// MachineLoopInfo, as applicable.
  410. MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
  411. void pop_front() { Insts.pop_front(); }
  412. void pop_back() { Insts.pop_back(); }
  413. void push_back(MachineInstr *MI) { Insts.push_back(MI); }
  414. /// Insert MI into the instruction list before I, possibly inside a bundle.
  415. ///
  416. /// If the insertion point is inside a bundle, MI will be added to the bundle,
  417. /// otherwise MI will not be added to any bundle. That means this function
  418. /// alone can't be used to prepend or append instructions to bundles. See
  419. /// MIBundleBuilder::insert() for a more reliable way of doing that.
  420. instr_iterator insert(instr_iterator I, MachineInstr *M);
  421. /// Insert a range of instructions into the instruction list before I.
  422. template<typename IT>
  423. void insert(iterator I, IT S, IT E) {
  424. assert((I == end() || I->getParent() == this) &&
  425. "iterator points outside of basic block");
  426. Insts.insert(I.getInstrIterator(), S, E);
  427. }
  428. /// Insert MI into the instruction list before I.
  429. iterator insert(iterator I, MachineInstr *MI) {
  430. assert((I == end() || I->getParent() == this) &&
  431. "iterator points outside of basic block");
  432. assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
  433. "Cannot insert instruction with bundle flags");
  434. return Insts.insert(I.getInstrIterator(), MI);
  435. }
  436. /// Insert MI into the instruction list after I.
  437. iterator insertAfter(iterator I, MachineInstr *MI) {
  438. assert((I == end() || I->getParent() == this) &&
  439. "iterator points outside of basic block");
  440. assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
  441. "Cannot insert instruction with bundle flags");
  442. return Insts.insertAfter(I.getInstrIterator(), MI);
  443. }
  444. /// Remove an instruction from the instruction list and delete it.
  445. ///
  446. /// If the instruction is part of a bundle, the other instructions in the
  447. /// bundle will still be bundled after removing the single instruction.
  448. instr_iterator erase(instr_iterator I);
  449. /// Remove an instruction from the instruction list and delete it.
  450. ///
  451. /// If the instruction is part of a bundle, the other instructions in the
  452. /// bundle will still be bundled after removing the single instruction.
  453. instr_iterator erase_instr(MachineInstr *I) {
  454. return erase(instr_iterator(I));
  455. }
  456. /// Remove a range of instructions from the instruction list and delete them.
  457. iterator erase(iterator I, iterator E) {
  458. return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
  459. }
  460. /// Remove an instruction or bundle from the instruction list and delete it.
  461. ///
  462. /// If I points to a bundle of instructions, they are all erased.
  463. iterator erase(iterator I) {
  464. return erase(I, std::next(I));
  465. }
  466. /// Remove an instruction from the instruction list and delete it.
  467. ///
  468. /// If I is the head of a bundle of instructions, the whole bundle will be
  469. /// erased.
  470. iterator erase(MachineInstr *I) {
  471. return erase(iterator(I));
  472. }
  473. /// Remove the unbundled instruction from the instruction list without
  474. /// deleting it.
  475. ///
  476. /// This function can not be used to remove bundled instructions, use
  477. /// remove_instr to remove individual instructions from a bundle.
  478. MachineInstr *remove(MachineInstr *I) {
  479. assert(!I->isBundled() && "Cannot remove bundled instructions");
  480. return Insts.remove(I);
  481. }
  482. /// Remove the possibly bundled instruction from the instruction list
  483. /// without deleting it.
  484. ///
  485. /// If the instruction is part of a bundle, the other instructions in the
  486. /// bundle will still be bundled after removing the single instruction.
  487. MachineInstr *remove_instr(MachineInstr *I);
  488. void clear() {
  489. Insts.clear();
  490. }
  491. /// Take an instruction from MBB 'Other' at the position From, and insert it
  492. /// into this MBB right before 'Where'.
  493. ///
  494. /// If From points to a bundle of instructions, the whole bundle is moved.
  495. void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
  496. // The range splice() doesn't allow noop moves, but this one does.
  497. if (Where != From)
  498. splice(Where, Other, From, std::next(From));
  499. }
  500. /// Take a block of instructions from MBB 'Other' in the range [From, To),
  501. /// and insert them into this MBB right before 'Where'.
  502. ///
  503. /// The instruction at 'Where' must not be included in the range of
  504. /// instructions to move.
  505. void splice(iterator Where, MachineBasicBlock *Other,
  506. iterator From, iterator To) {
  507. Insts.splice(Where.getInstrIterator(), Other->Insts,
  508. From.getInstrIterator(), To.getInstrIterator());
  509. }
  510. /// removeFromParent - This method unlinks 'this' from the containing
  511. /// function, and returns it, but does not delete it.
  512. MachineBasicBlock *removeFromParent();
  513. /// eraseFromParent - This method unlinks 'this' from the containing
  514. /// function and deletes it.
  515. void eraseFromParent();
  516. /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
  517. /// 'Old', change the code and CFG so that it branches to 'New' instead.
  518. void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
  519. /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
  520. /// the CFG to be inserted. If we have proven that MBB can only branch to
  521. /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
  522. /// DestB can be null. Besides DestA and DestB, retain other edges leading
  523. /// to LandingPads (currently there can be only one; we don't check or require
  524. /// that here). Note it is possible that DestA and/or DestB are LandingPads.
  525. bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
  526. MachineBasicBlock *DestB,
  527. bool isCond);
  528. /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
  529. /// any DBG_VALUE instructions. Return UnknownLoc if there is none.
  530. DebugLoc findDebugLoc(instr_iterator MBBI);
  531. DebugLoc findDebugLoc(iterator MBBI) {
  532. return findDebugLoc(MBBI.getInstrIterator());
  533. }
  534. /// Possible outcome of a register liveness query to computeRegisterLiveness()
  535. enum LivenessQueryResult {
  536. LQR_Live, ///< Register is known to be live.
  537. LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
  538. ///< register is.
  539. LQR_Dead, ///< Register is known to be dead.
  540. LQR_Unknown ///< Register liveness not decidable from local
  541. ///< neighborhood.
  542. };
  543. /// Return whether (physical) register \p Reg has been <def>ined and not
  544. /// <kill>ed as of just before \p Before.
  545. ///
  546. /// Search is localised to a neighborhood of \p Neighborhood instructions
  547. /// before (searching for defs or kills) and \p Neighborhood instructions
  548. /// after (searching just for defs) \p Before.
  549. ///
  550. /// \p Reg must be a physical register.
  551. LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
  552. unsigned Reg,
  553. const_iterator Before,
  554. unsigned Neighborhood=10) const;
  555. // Debugging methods.
  556. void dump() const;
  557. void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
  558. void print(raw_ostream &OS, ModuleSlotTracker &MST,
  559. SlotIndexes * = nullptr) const;
  560. // Printing method used by LoopInfo.
  561. void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
  562. /// getNumber - MachineBasicBlocks are uniquely numbered at the function
  563. /// level, unless they're not in a MachineFunction yet, in which case this
  564. /// will return -1.
  565. ///
  566. int getNumber() const { return Number; }
  567. void setNumber(int N) { Number = N; }
  568. /// getSymbol - Return the MCSymbol for this basic block.
  569. ///
  570. MCSymbol *getSymbol() const;
  571. private:
  572. /// getWeightIterator - Return weight iterator corresponding to the I
  573. /// successor iterator.
  574. weight_iterator getWeightIterator(succ_iterator I);
  575. const_weight_iterator getWeightIterator(const_succ_iterator I) const;
  576. friend class MachineBranchProbabilityInfo;
  577. /// getSuccWeight - Return weight of the edge from this block to MBB. This
  578. /// method should NOT be called directly, but by using getEdgeWeight method
  579. /// from MachineBranchProbabilityInfo class.
  580. uint32_t getSuccWeight(const_succ_iterator Succ) const;
  581. // Methods used to maintain doubly linked list of blocks...
  582. friend struct ilist_traits<MachineBasicBlock>;
  583. // Machine-CFG mutators
  584. /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
  585. /// Don't do this unless you know what you're doing, because it doesn't
  586. /// update pred's successors list. Use pred->addSuccessor instead.
  587. ///
  588. void addPredecessor(MachineBasicBlock *pred);
  589. /// removePredecessor - Remove pred as a predecessor of this
  590. /// MachineBasicBlock. Don't do this unless you know what you're
  591. /// doing, because it doesn't update pred's successors list. Use
  592. /// pred->removeSuccessor instead.
  593. ///
  594. void removePredecessor(MachineBasicBlock *pred);
  595. };
  596. raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
  597. // This is useful when building IndexedMaps keyed on basic block pointers.
  598. struct MBB2NumberFunctor :
  599. public std::unary_function<const MachineBasicBlock*, unsigned> {
  600. unsigned operator()(const MachineBasicBlock *MBB) const {
  601. return MBB->getNumber();
  602. }
  603. };
  604. //===--------------------------------------------------------------------===//
  605. // GraphTraits specializations for machine basic block graphs (machine-CFGs)
  606. //===--------------------------------------------------------------------===//
  607. // Provide specializations of GraphTraits to be able to treat a
  608. // MachineFunction as a graph of MachineBasicBlocks...
  609. //
  610. template <> struct GraphTraits<MachineBasicBlock *> {
  611. typedef MachineBasicBlock NodeType;
  612. typedef MachineBasicBlock::succ_iterator ChildIteratorType;
  613. static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
  614. static inline ChildIteratorType child_begin(NodeType *N) {
  615. return N->succ_begin();
  616. }
  617. static inline ChildIteratorType child_end(NodeType *N) {
  618. return N->succ_end();
  619. }
  620. };
  621. template <> struct GraphTraits<const MachineBasicBlock *> {
  622. typedef const MachineBasicBlock NodeType;
  623. typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
  624. static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
  625. static inline ChildIteratorType child_begin(NodeType *N) {
  626. return N->succ_begin();
  627. }
  628. static inline ChildIteratorType child_end(NodeType *N) {
  629. return N->succ_end();
  630. }
  631. };
  632. // Provide specializations of GraphTraits to be able to treat a
  633. // MachineFunction as a graph of MachineBasicBlocks... and to walk it
  634. // in inverse order. Inverse order for a function is considered
  635. // to be when traversing the predecessor edges of a MBB
  636. // instead of the successor edges.
  637. //
  638. template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
  639. typedef MachineBasicBlock NodeType;
  640. typedef MachineBasicBlock::pred_iterator ChildIteratorType;
  641. static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
  642. return G.Graph;
  643. }
  644. static inline ChildIteratorType child_begin(NodeType *N) {
  645. return N->pred_begin();
  646. }
  647. static inline ChildIteratorType child_end(NodeType *N) {
  648. return N->pred_end();
  649. }
  650. };
  651. template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
  652. typedef const MachineBasicBlock NodeType;
  653. typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
  654. static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
  655. return G.Graph;
  656. }
  657. static inline ChildIteratorType child_begin(NodeType *N) {
  658. return N->pred_begin();
  659. }
  660. static inline ChildIteratorType child_end(NodeType *N) {
  661. return N->pred_end();
  662. }
  663. };
  664. /// MachineInstrSpan provides an interface to get an iteration range
  665. /// containing the instruction it was initialized with, along with all
  666. /// those instructions inserted prior to or following that instruction
  667. /// at some point after the MachineInstrSpan is constructed.
  668. class MachineInstrSpan {
  669. MachineBasicBlock &MBB;
  670. MachineBasicBlock::iterator I, B, E;
  671. public:
  672. MachineInstrSpan(MachineBasicBlock::iterator I)
  673. : MBB(*I->getParent()),
  674. I(I),
  675. B(I == MBB.begin() ? MBB.end() : std::prev(I)),
  676. E(std::next(I)) {}
  677. MachineBasicBlock::iterator begin() {
  678. return B == MBB.end() ? MBB.begin() : std::next(B);
  679. }
  680. MachineBasicBlock::iterator end() { return E; }
  681. bool empty() { return begin() == end(); }
  682. MachineBasicBlock::iterator getInitial() { return I; }
  683. };
  684. } // End llvm namespace
  685. #endif