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 22 #ifndef LLVM_LIB_CODEGEN_LIVERANGECALC_H 23 #define LLVM_LIB_CODEGEN_LIVERANGECALC_H 24 25 #include "llvm/ADT/ArrayRef.h" 26 #include "llvm/ADT/BitVector.h" 27 #include "llvm/ADT/IndexedMap.h" 28 #include "llvm/CodeGen/LiveInterval.h" 29 30 namespace llvm { 31 32 /// Forward declarations for MachineDominators.h: 33 class MachineDominatorTree; 34 template <class NodeT> class DomTreeNodeBase; 35 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode; 36 37 class LiveRangeCalc { 38 const MachineFunction *MF; 39 const MachineRegisterInfo *MRI; 40 SlotIndexes *Indexes; 41 MachineDominatorTree *DomTree; 42 VNInfo::Allocator *Alloc; 43 44 /// LiveOutPair - A value and the block that defined it. The domtree node is 45 /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)]. 46 typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair; 47 48 /// LiveOutMap - Map basic blocks to the value leaving the block. 49 typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap; 50 51 /// Bit vector of active entries in LiveOut, also used as a visited set by 52 /// findReachingDefs. One entry per basic block, indexed by block number. 53 /// This is kept as a separate bit vector because it can be cleared quickly 54 /// when switching live ranges. 55 BitVector Seen; 56 57 /// Map LiveRange to sets of blocks (represented by bit vectors) that 58 /// in the live range are defined on entry and undefined on entry. 59 /// A block is defined on entry if there is a path from at least one of 60 /// the defs in the live range to the entry of the block, and conversely, 61 /// a block is undefined on entry, if there is no such path (i.e. no 62 /// definition reaches the entry of the block). A single LiveRangeCalc 63 /// object is used to track live-out information for multiple registers 64 /// in live range splitting (which is ok, since the live ranges of these 65 /// registers do not overlap), but the defined/undefined information must 66 /// be kept separate for each individual range. 67 /// By convention, EntryInfoMap[&LR] = { Defined, Undefined }. 68 std::map<LiveRange*,std::pair<BitVector,BitVector>> EntryInfoMap; 69 70 /// Map each basic block where a live range is live out to the live-out value 71 /// and its defining block. 72 /// 73 /// For every basic block, MBB, one of these conditions shall be true: 74 /// 75 /// 1. !Seen.count(MBB->getNumber()) 76 /// Blocks without a Seen bit are ignored. 77 /// 2. LiveOut[MBB].second.getNode() == MBB 78 /// The live-out value is defined in MBB. 79 /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB] 80 /// The live-out value passses through MBB. All predecessors must carry 81 /// the same value. 82 /// 83 /// The domtree node may be null, it can be computed. 84 /// 85 /// The map can be shared by multiple live ranges as long as no two are 86 /// live-out of the same block. 87 LiveOutMap Map; 88 89 /// LiveInBlock - Information about a basic block where a live range is known 90 /// to be live-in, but the value has not yet been determined. 91 struct LiveInBlock { 92 // The live range set that is live-in to this block. The algorithms can 93 // handle multiple non-overlapping live ranges simultaneously. 94 LiveRange &LR; 95 96 // DomNode - Dominator tree node for the block. 97 // Cleared when the final value has been determined and LI has been updated. 98 MachineDomTreeNode *DomNode; 99 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 105 // Live-in value filled in by updateSSA once it is known. 106 VNInfo *Value; 107 108 LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill) 109 : LR(LR), DomNode(node), Kill(kill), Value(nullptr) {} 110 }; 111 112 /// LiveIn - Work list of blocks where the live-in value has yet to be 113 /// determined. This list is typically computed by findReachingDefs() and 114 /// used as a work list by updateSSA(). The low-level interface may also be 115 /// used to add entries directly. 116 SmallVector<LiveInBlock, 16> LiveIn; 117 118 /// Check if the entry to block @p MBB can be reached by any of the defs 119 /// in @p LR. Return true if none of the defs reach the entry to @p MBB. 120 bool isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs, 121 MachineBasicBlock &MBB, BitVector &DefOnEntry, 122 BitVector &UndefOnEntry); 123 124 /// Find the set of defs that can reach @p Kill. @p Kill must belong to 125 /// @p UseMBB. 126 /// 127 /// If exactly one def can reach @p UseMBB, and the def dominates @p Kill, 128 /// all paths from the def to @p UseMBB are added to @p LR, and the function 129 /// returns true. 130 /// 131 /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be 132 /// live in are added to the LiveIn array, and the function returns false. 133 /// 134 /// The array @p Undef provides the locations where the range @p LR becomes 135 /// undefined by <def,read-undef> operands on other subranges. If @p Undef 136 /// is non-empty and @p Kill is jointly dominated only by the entries of 137 /// @p Undef, the function returns false. 138 /// 139 /// PhysReg, when set, is used to verify live-in lists on basic blocks. 140 bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB, 141 SlotIndex Kill, unsigned PhysReg, 142 ArrayRef<SlotIndex> Undefs); 143 144 /// updateSSA - Compute the values that will be live in to all requested 145 /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form. 146 /// 147 /// Every live-in block must be jointly dominated by the added live-out 148 /// blocks. No values are read from the live ranges. 149 void updateSSA(); 150 151 /// Transfer information from the LiveIn vector to the live ranges and update 152 /// the given @p LiveOuts. 153 void updateFromLiveIns(); 154 155 /// Extend the live range of @p LR to reach all uses of Reg. 156 /// 157 /// If @p LR is a main range, or if @p LI is null, then all uses must be 158 /// jointly dominated by the definitions from @p LR. If @p LR is a subrange 159 /// of the live interval @p LI, corresponding to lane mask @p LaneMask, 160 /// all uses must be jointly dominated by the definitions from @p LR 161 /// together with definitions of other lanes where @p LR becomes undefined 162 /// (via <def,read-undef> operands). 163 /// If @p LR is a main range, the @p LaneMask should be set to ~0, i.e. 164 /// LaneBitmask::getAll(). 165 void extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask, 166 LiveInterval *LI = nullptr); 167 168 /// Reset Map and Seen fields. 169 void resetLiveOutMap(); 170 171 public: 172 LiveRangeCalc() : MF(nullptr), MRI(nullptr), Indexes(nullptr), 173 DomTree(nullptr), Alloc(nullptr) {} 174 175 //===--------------------------------------------------------------------===// 176 // High-level interface. 177 //===--------------------------------------------------------------------===// 178 // 179 // Calculate live ranges from scratch. 180 // 181 182 /// reset - Prepare caches for a new set of non-overlapping live ranges. The 183 /// caches must be reset before attempting calculations with a live range 184 /// that may overlap a previously computed live range, and before the first 185 /// live range in a function. If live ranges are not known to be 186 /// non-overlapping, call reset before each. 187 void reset(const MachineFunction *MF, 188 SlotIndexes*, 189 MachineDominatorTree*, 190 VNInfo::Allocator*); 191 192 //===--------------------------------------------------------------------===// 193 // Mid-level interface. 194 //===--------------------------------------------------------------------===// 195 // 196 // Modify existing live ranges. 197 // 198 199 /// Extend the live range of @p LR to reach @p Use. 200 /// 201 /// The existing values in @p LR must be live so they jointly dominate @p Use. 202 /// If @p Use is not dominated by a single existing value, PHI-defs are 203 /// inserted as required to preserve SSA form. 204 /// 205 /// PhysReg, when set, is used to verify live-in lists on basic blocks. 206 void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg, 207 ArrayRef<SlotIndex> Undefs); 208 209 /// createDeadDefs - Create a dead def in LI for every def operand of Reg. 210 /// Each instruction defining Reg gets a new VNInfo with a corresponding 211 /// minimal live range. 212 void createDeadDefs(LiveRange &LR, unsigned Reg); 213 214 /// Extend the live range of @p LR to reach all uses of Reg. 215 /// 216 /// All uses must be jointly dominated by existing liveness. PHI-defs are 217 /// inserted as needed to preserve SSA form. 218 void extendToUses(LiveRange &LR, unsigned PhysReg) { 219 extendToUses(LR, PhysReg, LaneBitmask::getAll()); 220 } 221 222 /// Calculates liveness for the register specified in live interval @p LI. 223 /// Creates subregister live ranges as needed if subreg liveness tracking is 224 /// enabled. 225 void calculate(LiveInterval &LI, bool TrackSubRegs); 226 227 /// For live interval \p LI with correct SubRanges construct matching 228 /// information for the main live range. Expects the main live range to not 229 /// have any segments or value numbers. 230 void constructMainRangeFromSubranges(LiveInterval &LI); 231 232 //===--------------------------------------------------------------------===// 233 // Low-level interface. 234 //===--------------------------------------------------------------------===// 235 // 236 // These functions can be used to compute live ranges where the live-in and 237 // live-out blocks are already known, but the SSA value in each block is 238 // unknown. 239 // 240 // After calling reset(), add known live-out values and known live-in blocks. 241 // Then call calculateValues() to compute the actual value that is 242 // live-in to each block, and add liveness to the live ranges. 243 // 244 245 /// setLiveOutValue - Indicate that VNI is live out from MBB. The 246 /// calculateValues() function will not add liveness for MBB, the caller 247 /// should take care of that. 248 /// 249 /// VNI may be null only if MBB is a live-through block also passed to 250 /// addLiveInBlock(). 251 void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) { 252 Seen.set(MBB->getNumber()); 253 Map[MBB] = LiveOutPair(VNI, nullptr); 254 } 255 256 /// addLiveInBlock - Add a block with an unknown live-in value. This 257 /// function can only be called once per basic block. Once the live-in value 258 /// has been determined, calculateValues() will add liveness to LI. 259 /// 260 /// @param LR The live range that is live-in to the block. 261 /// @param DomNode The domtree node for the block. 262 /// @param Kill Index in block where LI is killed. If the value is 263 /// live-through, set Kill = SLotIndex() and also call 264 /// setLiveOutValue(MBB, 0). 265 void addLiveInBlock(LiveRange &LR, 266 MachineDomTreeNode *DomNode, 267 SlotIndex Kill = SlotIndex()) { 268 LiveIn.push_back(LiveInBlock(LR, DomNode, Kill)); 269 } 270 271 /// calculateValues - Calculate the value that will be live-in to each block 272 /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA 273 /// form. Add liveness to all live-in blocks up to the Kill point, or the 274 /// whole block for live-through blocks. 275 /// 276 /// Every predecessor of a live-in block must have been given a value with 277 /// setLiveOutValue, the value may be null for live-trough blocks. 278 void calculateValues(); 279 }; 280 281 } // end namespace llvm 282 283 #endif 284