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.

730 lines
29 KiB

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