2
0

LiveRangeCalc.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //===---- LiveRangeCalc.h - Calculate live ranges ---------------*- 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. // The LiveRangeCalc class can be used to compute live ranges from scratch. It
  11. // caches information about values in the CFG to speed up repeated operations
  12. // on the same live range. The cache can be shared by non-overlapping live
  13. // ranges. SplitKit uses that when computing the live range of split products.
  14. //
  15. // A low-level interface is available to clients that know where a variable is
  16. // live, but don't know which value it has as every point. LiveRangeCalc will
  17. // propagate values down the dominator tree, and even insert PHI-defs where
  18. // needed. SplitKit uses this faster interface when possible.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_LIB_CODEGEN_LIVERANGECALC_H
  22. #define LLVM_LIB_CODEGEN_LIVERANGECALC_H
  23. #include "llvm/ADT/BitVector.h"
  24. #include "llvm/ADT/IndexedMap.h"
  25. #include "llvm/CodeGen/LiveInterval.h"
  26. namespace llvm {
  27. /// Forward declarations for MachineDominators.h:
  28. class MachineDominatorTree;
  29. template <class NodeT> class DomTreeNodeBase;
  30. typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
  31. class LiveRangeCalc {
  32. const MachineFunction *MF;
  33. const MachineRegisterInfo *MRI;
  34. SlotIndexes *Indexes;
  35. MachineDominatorTree *DomTree;
  36. VNInfo::Allocator *Alloc;
  37. /// LiveOutPair - A value and the block that defined it. The domtree node is
  38. /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
  39. typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
  40. /// LiveOutMap - Map basic blocks to the value leaving the block.
  41. typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
  42. /// Bit vector of active entries in LiveOut, also used as a visited set by
  43. /// findReachingDefs. One entry per basic block, indexed by block number.
  44. /// This is kept as a separate bit vector because it can be cleared quickly
  45. /// when switching live ranges.
  46. BitVector Seen;
  47. /// Map each basic block where a live range is live out to the live-out value
  48. /// and its defining block.
  49. ///
  50. /// For every basic block, MBB, one of these conditions shall be true:
  51. ///
  52. /// 1. !Seen.count(MBB->getNumber())
  53. /// Blocks without a Seen bit are ignored.
  54. /// 2. LiveOut[MBB].second.getNode() == MBB
  55. /// The live-out value is defined in MBB.
  56. /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
  57. /// The live-out value passses through MBB. All predecessors must carry
  58. /// the same value.
  59. ///
  60. /// The domtree node may be null, it can be computed.
  61. ///
  62. /// The map can be shared by multiple live ranges as long as no two are
  63. /// live-out of the same block.
  64. LiveOutMap Map;
  65. /// LiveInBlock - Information about a basic block where a live range is known
  66. /// to be live-in, but the value has not yet been determined.
  67. struct LiveInBlock {
  68. // The live range set that is live-in to this block. The algorithms can
  69. // handle multiple non-overlapping live ranges simultaneously.
  70. LiveRange &LR;
  71. // DomNode - Dominator tree node for the block.
  72. // Cleared when the final value has been determined and LI has been updated.
  73. MachineDomTreeNode *DomNode;
  74. // Position in block where the live-in range ends, or SlotIndex() if the
  75. // range passes through the block. When the final value has been
  76. // determined, the range from the block start to Kill will be added to LI.
  77. SlotIndex Kill;
  78. // Live-in value filled in by updateSSA once it is known.
  79. VNInfo *Value;
  80. LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
  81. : LR(LR), DomNode(node), Kill(kill), Value(nullptr) {}
  82. };
  83. /// LiveIn - Work list of blocks where the live-in value has yet to be
  84. /// determined. This list is typically computed by findReachingDefs() and
  85. /// used as a work list by updateSSA(). The low-level interface may also be
  86. /// used to add entries directly.
  87. SmallVector<LiveInBlock, 16> LiveIn;
  88. /// Assuming that @p LR is live-in to @p UseMBB, find the set of defs that can
  89. /// reach it.
  90. ///
  91. /// If only one def can reach @p UseMBB, all paths from the def to @p UseMBB
  92. /// are added to @p LR, and the function returns true.
  93. ///
  94. /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be
  95. /// live in are added to the LiveIn array, and the function returns false.
  96. ///
  97. /// PhysReg, when set, is used to verify live-in lists on basic blocks.
  98. bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
  99. SlotIndex Kill, unsigned PhysReg);
  100. /// updateSSA - Compute the values that will be live in to all requested
  101. /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form.
  102. ///
  103. /// Every live-in block must be jointly dominated by the added live-out
  104. /// blocks. No values are read from the live ranges.
  105. void updateSSA();
  106. /// Transfer information from the LiveIn vector to the live ranges and update
  107. /// the given @p LiveOuts.
  108. void updateFromLiveIns();
  109. /// Extend the live range of @p LR to reach all uses of Reg.
  110. ///
  111. /// All uses must be jointly dominated by existing liveness. PHI-defs are
  112. /// inserted as needed to preserve SSA form.
  113. void extendToUses(LiveRange &LR, unsigned Reg, unsigned LaneMask);
  114. /// Reset Map and Seen fields.
  115. void resetLiveOutMap();
  116. public:
  117. LiveRangeCalc() : MF(nullptr), MRI(nullptr), Indexes(nullptr),
  118. DomTree(nullptr), Alloc(nullptr) {}
  119. //===--------------------------------------------------------------------===//
  120. // High-level interface.
  121. //===--------------------------------------------------------------------===//
  122. //
  123. // Calculate live ranges from scratch.
  124. //
  125. /// reset - Prepare caches for a new set of non-overlapping live ranges. The
  126. /// caches must be reset before attempting calculations with a live range
  127. /// that may overlap a previously computed live range, and before the first
  128. /// live range in a function. If live ranges are not known to be
  129. /// non-overlapping, call reset before each.
  130. void reset(const MachineFunction *MF,
  131. SlotIndexes*,
  132. MachineDominatorTree*,
  133. VNInfo::Allocator*);
  134. //===--------------------------------------------------------------------===//
  135. // Mid-level interface.
  136. //===--------------------------------------------------------------------===//
  137. //
  138. // Modify existing live ranges.
  139. //
  140. /// Extend the live range of @p LR to reach @p Use.
  141. ///
  142. /// The existing values in @p LR must be live so they jointly dominate @p Use.
  143. /// If @p Use is not dominated by a single existing value, PHI-defs are
  144. /// inserted as required to preserve SSA form.
  145. ///
  146. /// PhysReg, when set, is used to verify live-in lists on basic blocks.
  147. void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg = 0);
  148. /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
  149. /// Each instruction defining Reg gets a new VNInfo with a corresponding
  150. /// minimal live range.
  151. void createDeadDefs(LiveRange &LR, unsigned Reg);
  152. /// Extend the live range of @p LR to reach all uses of Reg.
  153. ///
  154. /// All uses must be jointly dominated by existing liveness. PHI-defs are
  155. /// inserted as needed to preserve SSA form.
  156. void extendToUses(LiveRange &LR, unsigned PhysReg) {
  157. extendToUses(LR, PhysReg, ~0u);
  158. }
  159. /// Calculates liveness for the register specified in live interval @p LI.
  160. /// Creates subregister live ranges as needed if subreg liveness tracking is
  161. /// enabled.
  162. void calculate(LiveInterval &LI, bool TrackSubRegs);
  163. //===--------------------------------------------------------------------===//
  164. // Low-level interface.
  165. //===--------------------------------------------------------------------===//
  166. //
  167. // These functions can be used to compute live ranges where the live-in and
  168. // live-out blocks are already known, but the SSA value in each block is
  169. // unknown.
  170. //
  171. // After calling reset(), add known live-out values and known live-in blocks.
  172. // Then call calculateValues() to compute the actual value that is
  173. // live-in to each block, and add liveness to the live ranges.
  174. //
  175. /// setLiveOutValue - Indicate that VNI is live out from MBB. The
  176. /// calculateValues() function will not add liveness for MBB, the caller
  177. /// should take care of that.
  178. ///
  179. /// VNI may be null only if MBB is a live-through block also passed to
  180. /// addLiveInBlock().
  181. void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
  182. Seen.set(MBB->getNumber());
  183. Map[MBB] = LiveOutPair(VNI, nullptr);
  184. }
  185. /// addLiveInBlock - Add a block with an unknown live-in value. This
  186. /// function can only be called once per basic block. Once the live-in value
  187. /// has been determined, calculateValues() will add liveness to LI.
  188. ///
  189. /// @param LR The live range that is live-in to the block.
  190. /// @param DomNode The domtree node for the block.
  191. /// @param Kill Index in block where LI is killed. If the value is
  192. /// live-through, set Kill = SLotIndex() and also call
  193. /// setLiveOutValue(MBB, 0).
  194. void addLiveInBlock(LiveRange &LR,
  195. MachineDomTreeNode *DomNode,
  196. SlotIndex Kill = SlotIndex()) {
  197. LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
  198. }
  199. /// calculateValues - Calculate the value that will be live-in to each block
  200. /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA
  201. /// form. Add liveness to all live-in blocks up to the Kill point, or the
  202. /// whole block for live-through blocks.
  203. ///
  204. /// Every predecessor of a live-in block must have been given a value with
  205. /// setLiveOutValue, the value may be null for live-trough blocks.
  206. void calculateValues();
  207. };
  208. } // end namespace llvm
  209. #endif