| 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 |  | 
|  | 22 | #ifndef LLVM_CODEGEN_LIVERANGECALC_H | 
|  | 23 | #define LLVM_CODEGEN_LIVERANGECALC_H | 
|  | 24 |  | 
|  | 25 | #include "llvm/ADT/BitVector.h" | 
|  | 26 | #include "llvm/ADT/IndexedMap.h" | 
|  | 27 | #include "llvm/CodeGen/LiveInterval.h" | 
|  | 28 |  | 
|  | 29 | namespace llvm { | 
|  | 30 |  | 
|  | 31 | /// Forward declarations for MachineDominators.h: | 
|  | 32 | class MachineDominatorTree; | 
|  | 33 | template <class NodeT> class DomTreeNodeBase; | 
|  | 34 | typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode; | 
|  | 35 |  | 
|  | 36 | class LiveRangeCalc { | 
| Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 37 | const MachineRegisterInfo *MRI; | 
|  | 38 | SlotIndexes *Indexes; | 
|  | 39 | MachineDominatorTree *DomTree; | 
|  | 40 | VNInfo::Allocator *Alloc; | 
|  | 41 |  | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 42 | /// Seen - Bit vector of active entries in LiveOut, also used as a visited | 
|  | 43 | /// set by findReachingDefs.  One entry per basic block, indexed by block | 
|  | 44 | /// number.  This is kept as a separate bit vector because it can be cleared | 
|  | 45 | /// quickly when switching live ranges. | 
|  | 46 | BitVector Seen; | 
|  | 47 |  | 
|  | 48 | /// LiveOutPair - A value and the block that defined it.  The domtree node is | 
|  | 49 | /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)]. | 
|  | 50 | typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair; | 
|  | 51 |  | 
|  | 52 | /// LiveOutMap - Map basic blocks to the value leaving the block. | 
|  | 53 | typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap; | 
|  | 54 |  | 
|  | 55 | /// LiveOut - Map each basic block where a live range is live out to the | 
|  | 56 | /// live-out value and its defining block. | 
|  | 57 | /// | 
|  | 58 | /// For every basic block, MBB, one of these conditions shall be true: | 
|  | 59 | /// | 
|  | 60 | ///  1. !Seen.count(MBB->getNumber()) | 
|  | 61 | ///     Blocks without a Seen bit are ignored. | 
|  | 62 | ///  2. LiveOut[MBB].second.getNode() == MBB | 
|  | 63 | ///     The live-out value is defined in MBB. | 
|  | 64 | ///  3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB] | 
|  | 65 | ///     The live-out value passses through MBB. All predecessors must carry | 
|  | 66 | ///     the same value. | 
|  | 67 | /// | 
|  | 68 | /// The domtree node may be null, it can be computed. | 
|  | 69 | /// | 
|  | 70 | /// The map can be shared by multiple live ranges as long as no two are | 
|  | 71 | /// live-out of the same block. | 
|  | 72 | LiveOutMap LiveOut; | 
|  | 73 |  | 
|  | 74 | /// LiveInBlock - Information about a basic block where a live range is known | 
|  | 75 | /// to be live-in, but the value has not yet been determined. | 
|  | 76 | struct LiveInBlock { | 
|  | 77 | // LI - The live range that is live-in to this block.  The algorithms can | 
|  | 78 | // handle multiple non-overlapping live ranges simultaneously. | 
|  | 79 | LiveInterval *LI; | 
|  | 80 |  | 
|  | 81 | // DomNode - Dominator tree node for the block. | 
|  | 82 | // Cleared when the final value has been determined and LI has been updated. | 
|  | 83 | MachineDomTreeNode *DomNode; | 
|  | 84 |  | 
|  | 85 | // Position in block where the live-in range ends, or SlotIndex() if the | 
|  | 86 | // range passes through the block.  When the final value has been | 
|  | 87 | // determined, the range from the block start to Kill will be added to LI. | 
|  | 88 | SlotIndex Kill; | 
|  | 89 |  | 
|  | 90 | // Live-in value filled in by updateSSA once it is known. | 
|  | 91 | VNInfo *Value; | 
|  | 92 |  | 
|  | 93 | LiveInBlock(LiveInterval *li, MachineDomTreeNode *node, SlotIndex kill) | 
|  | 94 | : LI(li), DomNode(node), Kill(kill), Value(0) {} | 
|  | 95 | }; | 
|  | 96 |  | 
|  | 97 | /// LiveIn - Work list of blocks where the live-in value has yet to be | 
|  | 98 | /// determined.  This list is typically computed by findReachingDefs() and | 
|  | 99 | /// used as a work list by updateSSA().  The low-level interface may also be | 
|  | 100 | /// used to add entries directly. | 
|  | 101 | SmallVector<LiveInBlock, 16> LiveIn; | 
|  | 102 |  | 
|  | 103 | /// findReachingDefs - Assuming that LI is live-in to KillMBB and killed at | 
|  | 104 | /// Kill, search for values that can reach KillMBB.  All blocks that need LI | 
|  | 105 | /// to be live-in are added to LiveIn.  If a unique reaching def is found, | 
|  | 106 | /// its value is returned, if Kill is jointly dominated by multiple values, | 
|  | 107 | /// NULL is returned. | 
| Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 108 | /// | 
|  | 109 | /// PhysReg, when set, is used to verify live-in lists on basic blocks. | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 110 | VNInfo *findReachingDefs(LiveInterval *LI, | 
|  | 111 | MachineBasicBlock *KillMBB, | 
| Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 112 | SlotIndex Kill, | 
|  | 113 | unsigned PhysReg); | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 114 |  | 
|  | 115 | /// updateSSA - Compute the values that will be live in to all requested | 
|  | 116 | /// blocks in LiveIn.  Create PHI-def values as required to preserve SSA form. | 
|  | 117 | /// | 
|  | 118 | /// Every live-in block must be jointly dominated by the added live-out | 
|  | 119 | /// blocks.  No values are read from the live ranges. | 
| Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 120 | void updateSSA(); | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 121 |  | 
|  | 122 | /// updateLiveIns - Add liveness as specified in the LiveIn vector, using VNI | 
|  | 123 | /// as a wildcard value for LiveIn entries without a value. | 
| Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 124 | void updateLiveIns(VNInfo *VNI); | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 125 |  | 
|  | 126 | public: | 
| Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 127 | LiveRangeCalc() : MRI(0), Indexes(0), DomTree(0), Alloc(0) {} | 
|  | 128 |  | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 129 | //===--------------------------------------------------------------------===// | 
|  | 130 | // High-level interface. | 
|  | 131 | //===--------------------------------------------------------------------===// | 
|  | 132 | // | 
|  | 133 | // Calculate live ranges from scratch. | 
|  | 134 | // | 
|  | 135 |  | 
|  | 136 | /// reset - Prepare caches for a new set of non-overlapping live ranges.  The | 
|  | 137 | /// caches must be reset before attempting calculations with a live range | 
|  | 138 | /// that may overlap a previously computed live range, and before the first | 
|  | 139 | /// live range in a function.  If live ranges are not known to be | 
|  | 140 | /// non-overlapping, call reset before each. | 
| Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 141 | void reset(const MachineFunction *MF, | 
|  | 142 | SlotIndexes*, | 
|  | 143 | MachineDominatorTree*, | 
|  | 144 | VNInfo::Allocator*); | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 145 |  | 
|  | 146 | /// calculate - Calculate the live range of a virtual register from its defs | 
|  | 147 | /// and uses.  LI must be empty with no values. | 
| Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 148 | void calculate(LiveInterval *LI); | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 149 |  | 
|  | 150 | //===--------------------------------------------------------------------===// | 
|  | 151 | // Mid-level interface. | 
|  | 152 | //===--------------------------------------------------------------------===// | 
|  | 153 | // | 
|  | 154 | // Modify existing live ranges. | 
|  | 155 | // | 
|  | 156 |  | 
|  | 157 | /// extend - Extend the live range of LI to reach Kill. | 
|  | 158 | /// | 
|  | 159 | /// The existing values in LI must be live so they jointly dominate Kill.  If | 
|  | 160 | /// Kill is not dominated by a single existing value, PHI-defs are inserted | 
|  | 161 | /// as required to preserve SSA form.  If Kill is known to be dominated by a | 
|  | 162 | /// single existing value, Alloc may be null. | 
| Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 163 | /// | 
|  | 164 | /// PhysReg, when set, is used to verify live-in lists on basic blocks. | 
|  | 165 | void extend(LiveInterval *LI, SlotIndex Kill, unsigned PhysReg = 0); | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 166 |  | 
| Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 167 | /// createDeadDefs - Create a dead def in LI for every def operand of Reg. | 
|  | 168 | /// Each instruction defining Reg gets a new VNInfo with a corresponding | 
|  | 169 | /// minimal live range. | 
|  | 170 | void createDeadDefs(LiveInterval *LI, unsigned Reg); | 
|  | 171 |  | 
|  | 172 | /// createDeadDefs - Create a dead def in LI for every def of LI->reg. | 
|  | 173 | void createDeadDefs(LiveInterval *LI) { | 
|  | 174 | createDeadDefs(LI, LI->reg); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | /// extendToUses - Extend the live range of LI to reach all uses of Reg. | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 178 | /// | 
|  | 179 | /// All uses must be jointly dominated by existing liveness.  PHI-defs are | 
|  | 180 | /// inserted as needed to preserve SSA form. | 
| Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 181 | void extendToUses(LiveInterval *LI, unsigned Reg); | 
|  | 182 |  | 
|  | 183 | /// extendToUses - Extend the live range of LI to reach all uses of LI->reg. | 
|  | 184 | void extendToUses(LiveInterval *LI) { | 
|  | 185 | extendToUses(LI, LI->reg); | 
|  | 186 | } | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 187 |  | 
|  | 188 | //===--------------------------------------------------------------------===// | 
|  | 189 | // Low-level interface. | 
|  | 190 | //===--------------------------------------------------------------------===// | 
|  | 191 | // | 
|  | 192 | // These functions can be used to compute live ranges where the live-in and | 
|  | 193 | // live-out blocks are already known, but the SSA value in each block is | 
|  | 194 | // unknown. | 
|  | 195 | // | 
|  | 196 | // After calling reset(), add known live-out values and known live-in blocks. | 
|  | 197 | // Then call calculateValues() to compute the actual value that is | 
|  | 198 | // live-in to each block, and add liveness to the live ranges. | 
|  | 199 | // | 
|  | 200 |  | 
|  | 201 | /// setLiveOutValue - Indicate that VNI is live out from MBB.  The | 
|  | 202 | /// calculateValues() function will not add liveness for MBB, the caller | 
|  | 203 | /// should take care of that. | 
|  | 204 | /// | 
|  | 205 | /// VNI may be null only if MBB is a live-through block also passed to | 
|  | 206 | /// addLiveInBlock(). | 
|  | 207 | void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) { | 
|  | 208 | Seen.set(MBB->getNumber()); | 
| NAKAMURA Takumi | cac923b | 2011-09-13 03:58:34 +0000 | [diff] [blame] | 209 | LiveOut[MBB] = LiveOutPair(VNI, (MachineDomTreeNode *)0); | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 210 | } | 
|  | 211 |  | 
|  | 212 | /// addLiveInBlock - Add a block with an unknown live-in value.  This | 
|  | 213 | /// function can only be called once per basic block.  Once the live-in value | 
|  | 214 | /// has been determined, calculateValues() will add liveness to LI. | 
|  | 215 | /// | 
|  | 216 | /// @param LI      The live range that is live-in to the block. | 
|  | 217 | /// @param DomNode The domtree node for the block. | 
|  | 218 | /// @param Kill    Index in block where LI is killed.  If the value is | 
|  | 219 | ///                live-through, set Kill = SLotIndex() and also call | 
|  | 220 | ///                setLiveOutValue(MBB, 0). | 
|  | 221 | void addLiveInBlock(LiveInterval *LI, | 
|  | 222 | MachineDomTreeNode *DomNode, | 
|  | 223 | SlotIndex Kill = SlotIndex()) { | 
|  | 224 | LiveIn.push_back(LiveInBlock(LI, DomNode, Kill)); | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | /// calculateValues - Calculate the value that will be live-in to each block | 
|  | 228 | /// added with addLiveInBlock.  Add PHI-def values as needed to preserve SSA | 
|  | 229 | /// form.  Add liveness to all live-in blocks up to the Kill point, or the | 
|  | 230 | /// whole block for live-through blocks. | 
|  | 231 | /// | 
|  | 232 | /// Every predecessor of a live-in block must have been given a value with | 
|  | 233 | /// setLiveOutValue, the value may be null for live-trough blocks. | 
| Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 234 | void calculateValues(); | 
| Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 235 | }; | 
|  | 236 |  | 
|  | 237 | } // end namespace llvm | 
|  | 238 |  | 
|  | 239 | #endif |