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