LiveRangeCalc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LiveRangeCalc.h - Calculate live ranges -----------------*- 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. // The LiveRangeCalc class can be used to implement the computation of
  15. // live ranges from scratch.
  16. // It caches information about values in the CFG to speed up repeated
  17. // operations on the same live range. The cache can be shared by
  18. // non-overlapping live ranges. SplitKit uses that when computing the live
  19. // range of split products.
  20. //
  21. // A low-level interface is available to clients that know where a variable is
  22. // live, but don't know which value it has as every point. LiveRangeCalc will
  23. // propagate values down the dominator tree, and even insert PHI-defs where
  24. // needed. SplitKit uses this faster interface when possible.
  25. //
  26. //===----------------------------------------------------------------------===//
  27. #ifndef LLVM_CODEGEN_LIVERANGECALC_H
  28. #define LLVM_CODEGEN_LIVERANGECALC_H
  29. #include "llvm/ADT/ArrayRef.h"
  30. #include "llvm/ADT/BitVector.h"
  31. #include "llvm/ADT/DenseMap.h"
  32. #include "llvm/ADT/IndexedMap.h"
  33. #include "llvm/ADT/SmallVector.h"
  34. #include "llvm/CodeGen/LiveInterval.h"
  35. #include "llvm/CodeGen/MachineBasicBlock.h"
  36. #include "llvm/CodeGen/SlotIndexes.h"
  37. #include <utility>
  38. namespace llvm {
  39. template <class NodeT> class DomTreeNodeBase;
  40. class MachineDominatorTree;
  41. class MachineFunction;
  42. class MachineRegisterInfo;
  43. using MachineDomTreeNode = DomTreeNodeBase<MachineBasicBlock>;
  44. class LiveRangeCalc {
  45. const MachineFunction *MF = nullptr;
  46. const MachineRegisterInfo *MRI = nullptr;
  47. SlotIndexes *Indexes = nullptr;
  48. MachineDominatorTree *DomTree = nullptr;
  49. VNInfo::Allocator *Alloc = nullptr;
  50. /// LiveOutPair - A value and the block that defined it. The domtree node is
  51. /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
  52. using LiveOutPair = std::pair<VNInfo *, MachineDomTreeNode *>;
  53. /// LiveOutMap - Map basic blocks to the value leaving the block.
  54. using LiveOutMap = IndexedMap<LiveOutPair, MBB2NumberFunctor>;
  55. /// Bit vector of active entries in LiveOut, also used as a visited set by
  56. /// findReachingDefs. One entry per basic block, indexed by block number.
  57. /// This is kept as a separate bit vector because it can be cleared quickly
  58. /// when switching live ranges.
  59. BitVector Seen;
  60. /// Map LiveRange to sets of blocks (represented by bit vectors) that
  61. /// in the live range are defined on entry and undefined on entry.
  62. /// A block is defined on entry if there is a path from at least one of
  63. /// the defs in the live range to the entry of the block, and conversely,
  64. /// a block is undefined on entry, if there is no such path (i.e. no
  65. /// definition reaches the entry of the block). A single LiveRangeCalc
  66. /// object is used to track live-out information for multiple registers
  67. /// in live range splitting (which is ok, since the live ranges of these
  68. /// registers do not overlap), but the defined/undefined information must
  69. /// be kept separate for each individual range.
  70. /// By convention, EntryInfoMap[&LR] = { Defined, Undefined }.
  71. using EntryInfoMap = DenseMap<LiveRange *, std::pair<BitVector, BitVector>>;
  72. EntryInfoMap EntryInfos;
  73. /// Map each basic block where a live range is live out to the live-out value
  74. /// and its defining block.
  75. ///
  76. /// For every basic block, MBB, one of these conditions shall be true:
  77. ///
  78. /// 1. !Seen.count(MBB->getNumber())
  79. /// Blocks without a Seen bit are ignored.
  80. /// 2. LiveOut[MBB].second.getNode() == MBB
  81. /// The live-out value is defined in MBB.
  82. /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
  83. /// The live-out value passses through MBB. All predecessors must carry
  84. /// the same value.
  85. ///
  86. /// The domtree node may be null, it can be computed.
  87. ///
  88. /// The map can be shared by multiple live ranges as long as no two are
  89. /// live-out of the same block.
  90. LiveOutMap Map;
  91. /// LiveInBlock - Information about a basic block where a live range is known
  92. /// to be live-in, but the value has not yet been determined.
  93. struct LiveInBlock {
  94. // The live range set that is live-in to this block. The algorithms can
  95. // handle multiple non-overlapping live ranges simultaneously.
  96. LiveRange &LR;
  97. // DomNode - Dominator tree node for the block.
  98. // Cleared when the final value has been determined and LI has been updated.
  99. MachineDomTreeNode *DomNode;
  100. // Position in block where the live-in range ends, or SlotIndex() if the
  101. // range passes through the block. When the final value has been
  102. // determined, the range from the block start to Kill will be added to LI.
  103. SlotIndex Kill;
  104. // Live-in value filled in by updateSSA once it is known.
  105. VNInfo *Value = nullptr;
  106. LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
  107. : LR(LR), DomNode(node), Kill(kill) {}
  108. };
  109. /// LiveIn - Work list of blocks where the live-in value has yet to be
  110. /// determined. This list is typically computed by findReachingDefs() and
  111. /// used as a work list by updateSSA(). The low-level interface may also be
  112. /// used to add entries directly.
  113. SmallVector<LiveInBlock, 16> LiveIn;
  114. /// Check if the entry to block @p MBB can be reached by any of the defs
  115. /// in @p LR. Return true if none of the defs reach the entry to @p MBB.
  116. bool isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
  117. MachineBasicBlock &MBB, BitVector &DefOnEntry,
  118. BitVector &UndefOnEntry);
  119. /// Find the set of defs that can reach @p Kill. @p Kill must belong to
  120. /// @p UseMBB.
  121. ///
  122. /// If exactly one def can reach @p UseMBB, and the def dominates @p Kill,
  123. /// all paths from the def to @p UseMBB are added to @p LR, and the function
  124. /// returns true.
  125. ///
  126. /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be
  127. /// live in are added to the LiveIn array, and the function returns false.
  128. ///
  129. /// The array @p Undef provides the locations where the range @p LR becomes
  130. /// undefined by <def,read-undef> operands on other subranges. If @p Undef
  131. /// is non-empty and @p Kill is jointly dominated only by the entries of
  132. /// @p Undef, the function returns false.
  133. ///
  134. /// PhysReg, when set, is used to verify live-in lists on basic blocks.
  135. bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB, SlotIndex Use,
  136. unsigned PhysReg, ArrayRef<SlotIndex> Undefs);
  137. /// updateSSA - Compute the values that will be live in to all requested
  138. /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form.
  139. ///
  140. /// Every live-in block must be jointly dominated by the added live-out
  141. /// blocks. No values are read from the live ranges.
  142. void updateSSA();
  143. /// Transfer information from the LiveIn vector to the live ranges and update
  144. /// the given @p LiveOuts.
  145. void updateFromLiveIns();
  146. protected:
  147. /// Some getters to expose in a read-only way some private fields to
  148. /// subclasses.
  149. const MachineFunction *getMachineFunction() { return MF; }
  150. const MachineRegisterInfo *getRegInfo() const { return MRI; }
  151. SlotIndexes *getIndexes() { return Indexes; }
  152. MachineDominatorTree *getDomTree() { return DomTree; }
  153. VNInfo::Allocator *getVNAlloc() { return Alloc; }
  154. /// Reset Map and Seen fields.
  155. void resetLiveOutMap();
  156. public:
  157. LiveRangeCalc() = default;
  158. //===--------------------------------------------------------------------===//
  159. // High-level interface.
  160. //===--------------------------------------------------------------------===//
  161. //
  162. // Calculate live ranges from scratch.
  163. //
  164. /// reset - Prepare caches for a new set of non-overlapping live ranges. The
  165. /// caches must be reset before attempting calculations with a live range
  166. /// that may overlap a previously computed live range, and before the first
  167. /// live range in a function. If live ranges are not known to be
  168. /// non-overlapping, call reset before each.
  169. void reset(const MachineFunction *mf, SlotIndexes *SI,
  170. MachineDominatorTree *MDT, VNInfo::Allocator *VNIA);
  171. //===--------------------------------------------------------------------===//
  172. // Mid-level interface.
  173. //===--------------------------------------------------------------------===//
  174. //
  175. // Modify existing live ranges.
  176. //
  177. /// Extend the live range of @p LR to reach @p Use.
  178. ///
  179. /// The existing values in @p LR must be live so they jointly dominate @p Use.
  180. /// If @p Use is not dominated by a single existing value, PHI-defs are
  181. /// inserted as required to preserve SSA form.
  182. ///
  183. /// PhysReg, when set, is used to verify live-in lists on basic blocks.
  184. void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
  185. ArrayRef<SlotIndex> Undefs);
  186. //===--------------------------------------------------------------------===//
  187. // Low-level interface.
  188. //===--------------------------------------------------------------------===//
  189. //
  190. // These functions can be used to compute live ranges where the live-in and
  191. // live-out blocks are already known, but the SSA value in each block is
  192. // unknown.
  193. //
  194. // After calling reset(), add known live-out values and known live-in blocks.
  195. // Then call calculateValues() to compute the actual value that is
  196. // live-in to each block, and add liveness to the live ranges.
  197. //
  198. /// setLiveOutValue - Indicate that VNI is live out from MBB. The
  199. /// calculateValues() function will not add liveness for MBB, the caller
  200. /// should take care of that.
  201. ///
  202. /// VNI may be null only if MBB is a live-through block also passed to
  203. /// addLiveInBlock().
  204. void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
  205. Seen.set(MBB->getNumber());
  206. Map[MBB] = LiveOutPair(VNI, nullptr);
  207. }
  208. /// addLiveInBlock - Add a block with an unknown live-in value. This
  209. /// function can only be called once per basic block. Once the live-in value
  210. /// has been determined, calculateValues() will add liveness to LI.
  211. ///
  212. /// @param LR The live range that is live-in to the block.
  213. /// @param DomNode The domtree node for the block.
  214. /// @param Kill Index in block where LI is killed. If the value is
  215. /// live-through, set Kill = SLotIndex() and also call
  216. /// setLiveOutValue(MBB, 0).
  217. void addLiveInBlock(LiveRange &LR, MachineDomTreeNode *DomNode,
  218. SlotIndex Kill = SlotIndex()) {
  219. LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
  220. }
  221. /// calculateValues - Calculate the value that will be live-in to each block
  222. /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA
  223. /// form. Add liveness to all live-in blocks up to the Kill point, or the
  224. /// whole block for live-through blocks.
  225. ///
  226. /// Every predecessor of a live-in block must have been given a value with
  227. /// setLiveOutValue, the value may be null for live-trough blocks.
  228. void calculateValues();
  229. /// A diagnostic function to check if the end of the block @p MBB is
  230. /// jointly dominated by the blocks corresponding to the slot indices
  231. /// in @p Defs. This function is mainly for use in self-verification
  232. /// checks.
  233. LLVM_ATTRIBUTE_UNUSED
  234. static bool isJointlyDominated(const MachineBasicBlock *MBB,
  235. ArrayRef<SlotIndex> Defs,
  236. const SlotIndexes &Indexes);
  237. };
  238. } // end namespace llvm
  239. #endif // LLVM_CODEGEN_LIVERANGECALC_H
  240. #ifdef __GNUC__
  241. #pragma GCC diagnostic pop
  242. #endif