|
|
|||
File indexing completed on 2026-05-10 08:43:29
0001 //===- LiveRangeCalc.h - Calculate live ranges -----------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // The LiveRangeCalc class can be used to implement the computation of 0010 // live ranges from scratch. 0011 // It caches information about values in the CFG to speed up repeated 0012 // operations on the same live range. The cache can be shared by 0013 // non-overlapping live ranges. SplitKit uses that when computing the live 0014 // range of split products. 0015 // 0016 // A low-level interface is available to clients that know where a variable is 0017 // live, but don't know which value it has as every point. LiveRangeCalc will 0018 // propagate values down the dominator tree, and even insert PHI-defs where 0019 // needed. SplitKit uses this faster interface when possible. 0020 // 0021 //===----------------------------------------------------------------------===// 0022 0023 #ifndef LLVM_CODEGEN_LIVERANGECALC_H 0024 #define LLVM_CODEGEN_LIVERANGECALC_H 0025 0026 #include "llvm/ADT/ArrayRef.h" 0027 #include "llvm/ADT/BitVector.h" 0028 #include "llvm/ADT/DenseMap.h" 0029 #include "llvm/ADT/IndexedMap.h" 0030 #include "llvm/ADT/SmallVector.h" 0031 #include "llvm/CodeGen/LiveInterval.h" 0032 #include "llvm/CodeGen/MachineBasicBlock.h" 0033 #include "llvm/CodeGen/SlotIndexes.h" 0034 #include <utility> 0035 0036 namespace llvm { 0037 0038 template <class NodeT> class DomTreeNodeBase; 0039 class MachineDominatorTree; 0040 class MachineFunction; 0041 class MachineRegisterInfo; 0042 0043 using MachineDomTreeNode = DomTreeNodeBase<MachineBasicBlock>; 0044 0045 class LiveRangeCalc { 0046 const MachineFunction *MF = nullptr; 0047 const MachineRegisterInfo *MRI = nullptr; 0048 SlotIndexes *Indexes = nullptr; 0049 MachineDominatorTree *DomTree = nullptr; 0050 VNInfo::Allocator *Alloc = nullptr; 0051 0052 /// LiveOutPair - A value and the block that defined it. The domtree node is 0053 /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)]. 0054 using LiveOutPair = std::pair<VNInfo *, MachineDomTreeNode *>; 0055 0056 /// LiveOutMap - Map basic blocks to the value leaving the block. 0057 using LiveOutMap = IndexedMap<LiveOutPair, MBB2NumberFunctor>; 0058 0059 /// Bit vector of active entries in LiveOut, also used as a visited set by 0060 /// findReachingDefs. One entry per basic block, indexed by block number. 0061 /// This is kept as a separate bit vector because it can be cleared quickly 0062 /// when switching live ranges. 0063 BitVector Seen; 0064 0065 /// Map LiveRange to sets of blocks (represented by bit vectors) that 0066 /// in the live range are defined on entry and undefined on entry. 0067 /// A block is defined on entry if there is a path from at least one of 0068 /// the defs in the live range to the entry of the block, and conversely, 0069 /// a block is undefined on entry, if there is no such path (i.e. no 0070 /// definition reaches the entry of the block). A single LiveRangeCalc 0071 /// object is used to track live-out information for multiple registers 0072 /// in live range splitting (which is ok, since the live ranges of these 0073 /// registers do not overlap), but the defined/undefined information must 0074 /// be kept separate for each individual range. 0075 /// By convention, EntryInfoMap[&LR] = { Defined, Undefined }. 0076 using EntryInfoMap = DenseMap<LiveRange *, std::pair<BitVector, BitVector>>; 0077 EntryInfoMap EntryInfos; 0078 0079 /// Map each basic block where a live range is live out to the live-out value 0080 /// and its defining block. 0081 /// 0082 /// For every basic block, MBB, one of these conditions shall be true: 0083 /// 0084 /// 1. !Seen.count(MBB->getNumber()) 0085 /// Blocks without a Seen bit are ignored. 0086 /// 2. LiveOut[MBB].second.getNode() == MBB 0087 /// The live-out value is defined in MBB. 0088 /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB] 0089 /// The live-out value passses through MBB. All predecessors must carry 0090 /// the same value. 0091 /// 0092 /// The domtree node may be null, it can be computed. 0093 /// 0094 /// The map can be shared by multiple live ranges as long as no two are 0095 /// live-out of the same block. 0096 LiveOutMap Map; 0097 0098 /// LiveInBlock - Information about a basic block where a live range is known 0099 /// to be live-in, but the value has not yet been determined. 0100 struct LiveInBlock { 0101 // The live range set that is live-in to this block. The algorithms can 0102 // handle multiple non-overlapping live ranges simultaneously. 0103 LiveRange &LR; 0104 0105 // DomNode - Dominator tree node for the block. 0106 // Cleared when the final value has been determined and LI has been updated. 0107 MachineDomTreeNode *DomNode; 0108 0109 // Position in block where the live-in range ends, or SlotIndex() if the 0110 // range passes through the block. When the final value has been 0111 // determined, the range from the block start to Kill will be added to LI. 0112 SlotIndex Kill; 0113 0114 // Live-in value filled in by updateSSA once it is known. 0115 VNInfo *Value = nullptr; 0116 0117 LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill) 0118 : LR(LR), DomNode(node), Kill(kill) {} 0119 }; 0120 0121 /// LiveIn - Work list of blocks where the live-in value has yet to be 0122 /// determined. This list is typically computed by findReachingDefs() and 0123 /// used as a work list by updateSSA(). The low-level interface may also be 0124 /// used to add entries directly. 0125 SmallVector<LiveInBlock, 16> LiveIn; 0126 0127 /// Check if the entry to block @p MBB can be reached by any of the defs 0128 /// in @p LR. Return true if none of the defs reach the entry to @p MBB. 0129 bool isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs, 0130 MachineBasicBlock &MBB, BitVector &DefOnEntry, 0131 BitVector &UndefOnEntry); 0132 0133 /// Find the set of defs that can reach @p Kill. @p Kill must belong to 0134 /// @p UseMBB. 0135 /// 0136 /// If exactly one def can reach @p UseMBB, and the def dominates @p Kill, 0137 /// all paths from the def to @p UseMBB are added to @p LR, and the function 0138 /// returns true. 0139 /// 0140 /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be 0141 /// live in are added to the LiveIn array, and the function returns false. 0142 /// 0143 /// The array @p Undef provides the locations where the range @p LR becomes 0144 /// undefined by <def,read-undef> operands on other subranges. If @p Undef 0145 /// is non-empty and @p Kill is jointly dominated only by the entries of 0146 /// @p Undef, the function returns false. 0147 /// 0148 /// PhysReg, when set, is used to verify live-in lists on basic blocks. 0149 bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB, SlotIndex Use, 0150 unsigned PhysReg, ArrayRef<SlotIndex> Undefs); 0151 0152 /// updateSSA - Compute the values that will be live in to all requested 0153 /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form. 0154 /// 0155 /// Every live-in block must be jointly dominated by the added live-out 0156 /// blocks. No values are read from the live ranges. 0157 void updateSSA(); 0158 0159 /// Transfer information from the LiveIn vector to the live ranges and update 0160 /// the given @p LiveOuts. 0161 void updateFromLiveIns(); 0162 0163 protected: 0164 /// Some getters to expose in a read-only way some private fields to 0165 /// subclasses. 0166 const MachineFunction *getMachineFunction() { return MF; } 0167 const MachineRegisterInfo *getRegInfo() const { return MRI; } 0168 SlotIndexes *getIndexes() { return Indexes; } 0169 MachineDominatorTree *getDomTree() { return DomTree; } 0170 VNInfo::Allocator *getVNAlloc() { return Alloc; } 0171 0172 /// Reset Map and Seen fields. 0173 void resetLiveOutMap(); 0174 0175 public: 0176 LiveRangeCalc() = default; 0177 0178 //===--------------------------------------------------------------------===// 0179 // High-level interface. 0180 //===--------------------------------------------------------------------===// 0181 // 0182 // Calculate live ranges from scratch. 0183 // 0184 0185 /// reset - Prepare caches for a new set of non-overlapping live ranges. The 0186 /// caches must be reset before attempting calculations with a live range 0187 /// that may overlap a previously computed live range, and before the first 0188 /// live range in a function. If live ranges are not known to be 0189 /// non-overlapping, call reset before each. 0190 void reset(const MachineFunction *mf, SlotIndexes *SI, 0191 MachineDominatorTree *MDT, VNInfo::Allocator *VNIA); 0192 0193 //===--------------------------------------------------------------------===// 0194 // Mid-level interface. 0195 //===--------------------------------------------------------------------===// 0196 // 0197 // Modify existing live ranges. 0198 // 0199 0200 /// Extend the live range of @p LR to reach @p Use. 0201 /// 0202 /// The existing values in @p LR must be live so they jointly dominate @p Use. 0203 /// If @p Use is not dominated by a single existing value, PHI-defs are 0204 /// inserted as required to preserve SSA form. 0205 /// 0206 /// PhysReg, when set, is used to verify live-in lists on basic blocks. 0207 void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg, 0208 ArrayRef<SlotIndex> Undefs); 0209 0210 //===--------------------------------------------------------------------===// 0211 // Low-level interface. 0212 //===--------------------------------------------------------------------===// 0213 // 0214 // These functions can be used to compute live ranges where the live-in and 0215 // live-out blocks are already known, but the SSA value in each block is 0216 // unknown. 0217 // 0218 // After calling reset(), add known live-out values and known live-in blocks. 0219 // Then call calculateValues() to compute the actual value that is 0220 // live-in to each block, and add liveness to the live ranges. 0221 // 0222 0223 /// setLiveOutValue - Indicate that VNI is live out from MBB. The 0224 /// calculateValues() function will not add liveness for MBB, the caller 0225 /// should take care of that. 0226 /// 0227 /// VNI may be null only if MBB is a live-through block also passed to 0228 /// addLiveInBlock(). 0229 void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) { 0230 Seen.set(MBB->getNumber()); 0231 Map[MBB] = LiveOutPair(VNI, nullptr); 0232 } 0233 0234 /// addLiveInBlock - Add a block with an unknown live-in value. This 0235 /// function can only be called once per basic block. Once the live-in value 0236 /// has been determined, calculateValues() will add liveness to LI. 0237 /// 0238 /// @param LR The live range that is live-in to the block. 0239 /// @param DomNode The domtree node for the block. 0240 /// @param Kill Index in block where LI is killed. If the value is 0241 /// live-through, set Kill = SLotIndex() and also call 0242 /// setLiveOutValue(MBB, 0). 0243 void addLiveInBlock(LiveRange &LR, MachineDomTreeNode *DomNode, 0244 SlotIndex Kill = SlotIndex()) { 0245 LiveIn.push_back(LiveInBlock(LR, DomNode, Kill)); 0246 } 0247 0248 /// calculateValues - Calculate the value that will be live-in to each block 0249 /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA 0250 /// form. Add liveness to all live-in blocks up to the Kill point, or the 0251 /// whole block for live-through blocks. 0252 /// 0253 /// Every predecessor of a live-in block must have been given a value with 0254 /// setLiveOutValue, the value may be null for live-trough blocks. 0255 void calculateValues(); 0256 0257 /// A diagnostic function to check if the end of the block @p MBB is 0258 /// jointly dominated by the blocks corresponding to the slot indices 0259 /// in @p Defs. This function is mainly for use in self-verification 0260 /// checks. 0261 LLVM_ATTRIBUTE_UNUSED 0262 static bool isJointlyDominated(const MachineBasicBlock *MBB, 0263 ArrayRef<SlotIndex> Defs, 0264 const SlotIndexes &Indexes); 0265 }; 0266 0267 } // end namespace llvm 0268 0269 #endif // LLVM_CODEGEN_LIVERANGECALC_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|