LiveRangeCalc.h 12 KB

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