Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 1 | //===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===// |
| 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 | // Implementation of the LiveRangeCalc class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 14 | #include "LiveRangeCalc.h" |
| 15 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 20 | #define DEBUG_TYPE "regalloc" |
| 21 | |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 22 | void LiveRangeCalc::reset(const MachineFunction *mf, |
Jakob Stoklund Olesen | 5ef0e0b2 | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 23 | SlotIndexes *SI, |
| 24 | MachineDominatorTree *MDT, |
| 25 | VNInfo::Allocator *VNIA) { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 26 | MF = mf; |
Jakob Stoklund Olesen | 5ef0e0b2 | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 27 | MRI = &MF->getRegInfo(); |
| 28 | Indexes = SI; |
| 29 | DomTree = MDT; |
| 30 | Alloc = VNIA; |
| 31 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 32 | MainLiveOutData.reset(MF->getNumBlockIDs()); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 33 | LiveIn.clear(); |
| 34 | } |
| 35 | |
| 36 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 37 | static SlotIndex getDefIndex(const SlotIndexes &Indexes, const MachineInstr &MI, |
| 38 | bool EarlyClobber) { |
| 39 | // PHI defs begin at the basic block start index. |
| 40 | if (MI.isPHI()) |
| 41 | return Indexes.getMBBStartIdx(MI.getParent()); |
| 42 | |
| 43 | // Instructions are either normal 'r', or early clobber 'e'. |
| 44 | return Indexes.getInstructionIndex(&MI).getRegSlot(EarlyClobber); |
| 45 | } |
| 46 | |
| 47 | void LiveRangeCalc::createDeadDefs(LiveInterval &LI) { |
| 48 | assert(MRI && Indexes && "call reset() first"); |
| 49 | |
| 50 | // Visit all def operands. If the same instruction has multiple defs of Reg, |
| 51 | // LR.createDeadDef() will deduplicate. |
| 52 | const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo(); |
| 53 | unsigned Reg = LI.reg; |
| 54 | for (const MachineOperand &MO : MRI->def_operands(Reg)) { |
| 55 | const MachineInstr *MI = MO.getParent(); |
| 56 | SlotIndex Idx = getDefIndex(*Indexes, *MI, MO.isEarlyClobber()); |
| 57 | unsigned SubReg = MO.getSubReg(); |
Matthias Braun | e3d3b88 | 2014-12-10 01:12:30 +0000 | [diff] [blame] | 58 | if (LI.hasSubRanges() || (SubReg != 0 && MRI->tracksSubRegLiveness())) { |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 59 | unsigned Mask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg) |
| 60 | : MRI->getMaxLaneMaskForVReg(Reg); |
| 61 | |
| 62 | // If this is the first time we see a subregister def, initialize |
| 63 | // subranges by creating a copy of the main range. |
| 64 | if (!LI.hasSubRanges() && !LI.empty()) { |
| 65 | unsigned ClassMask = MRI->getMaxLaneMaskForVReg(Reg); |
| 66 | LI.createSubRangeFrom(*Alloc, ClassMask, LI); |
| 67 | } |
| 68 | |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 69 | for (LiveInterval::SubRange &S : LI.subranges()) { |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 70 | // A Mask for subregs common to the existing subrange and current def. |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 71 | unsigned Common = S.LaneMask & Mask; |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 72 | if (Common == 0) |
| 73 | continue; |
| 74 | // A Mask for subregs covered by the subrange but not the current def. |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 75 | unsigned LRest = S.LaneMask & ~Mask; |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 76 | LiveInterval::SubRange *CommonRange; |
| 77 | if (LRest != 0) { |
| 78 | // Split current subrange into Common and LRest ranges. |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 79 | S.LaneMask = LRest; |
| 80 | CommonRange = LI.createSubRangeFrom(*Alloc, Common, S); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 81 | } else { |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 82 | assert(Common == S.LaneMask); |
| 83 | CommonRange = &S; |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 84 | } |
| 85 | CommonRange->createDeadDef(Idx, *Alloc); |
| 86 | Mask &= ~Common; |
| 87 | } |
| 88 | if (Mask != 0) { |
| 89 | LiveInterval::SubRange *SubRange = LI.createSubRange(*Alloc, Mask); |
| 90 | SubRange->createDeadDef(Idx, *Alloc); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Create the def in LR. This may find an existing def. |
| 95 | LI.createDeadDef(Idx, *Alloc); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 100 | void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) { |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 101 | assert(MRI && Indexes && "call reset() first"); |
| 102 | |
| 103 | // Visit all def operands. If the same instruction has multiple defs of Reg, |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 104 | // LR.createDeadDef() will deduplicate. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 105 | for (MachineOperand &MO : MRI->def_operands(Reg)) { |
| 106 | const MachineInstr *MI = MO.getParent(); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 107 | SlotIndex Idx = getDefIndex(*Indexes, *MI, MO.isEarlyClobber()); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 108 | // Create the def in LR. This may find an existing def. |
| 109 | LR.createDeadDef(Idx, *Alloc); |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 114 | static SlotIndex getUseIndex(const SlotIndexes &Indexes, |
| 115 | const MachineOperand &MO) { |
| 116 | const MachineInstr *MI = MO.getParent(); |
| 117 | unsigned OpNo = (&MO - &MI->getOperand(0)); |
| 118 | if (MI->isPHI()) { |
| 119 | assert(!MO.isDef() && "Cannot handle PHI def of partial register."); |
| 120 | // The actual place where a phi operand is used is the end of the pred MBB. |
| 121 | // PHI operands are paired: (Reg, PredMBB). |
| 122 | return Indexes.getMBBEndIdx(MI->getOperand(OpNo+1).getMBB()); |
| 123 | } |
| 124 | |
| 125 | // Check for early-clobber redefs. |
| 126 | bool isEarlyClobber = false; |
| 127 | unsigned DefIdx; |
| 128 | if (MO.isDef()) { |
| 129 | isEarlyClobber = MO.isEarlyClobber(); |
| 130 | } else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) { |
| 131 | // FIXME: This would be a lot easier if tied early-clobber uses also |
| 132 | // had an early-clobber flag. |
| 133 | isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber(); |
| 134 | } |
| 135 | return Indexes.getInstructionIndex(MI).getRegSlot(isEarlyClobber); |
| 136 | } |
| 137 | |
| 138 | |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 139 | void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg) { |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 140 | assert(MRI && Indexes && "call reset() first"); |
| 141 | |
| 142 | // Visit all operands that read Reg. This may include partial defs. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 143 | for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) { |
Jakob Stoklund Olesen | 4aed470 | 2012-09-06 18:15:15 +0000 | [diff] [blame] | 144 | // Clear all kill flags. They will be reinserted after register allocation |
| 145 | // by LiveIntervalAnalysis::addKillFlags(). |
| 146 | if (MO.isUse()) |
| 147 | MO.setIsKill(false); |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 148 | if (!MO.readsReg()) |
| 149 | continue; |
| 150 | // MI is reading Reg. We may have visited MI before if it happens to be |
| 151 | // reading Reg multiple times. That is OK, extend() is idempotent. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 152 | SlotIndex Idx = getUseIndex(*Indexes, MO); |
| 153 | extend(LR, Idx, Reg, MainLiveOutData); |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 158 | void LiveRangeCalc::extendToUses(LiveInterval &LI) { |
| 159 | assert(MRI && Indexes && "call reset() first"); |
| 160 | |
| 161 | const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo(); |
| 162 | SmallVector<LiveOutData,2> LiveOuts; |
| 163 | unsigned NumSubRanges = 0; |
Matthias Braun | 09afa1e | 2014-12-11 00:59:06 +0000 | [diff] [blame] | 164 | for (const auto &S : LI.subranges()) { |
| 165 | (void)S; |
| 166 | ++NumSubRanges; |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 167 | LiveOuts.push_back(LiveOutData()); |
| 168 | LiveOuts.back().reset(MF->getNumBlockIDs()); |
| 169 | } |
| 170 | |
| 171 | // Visit all operands that read Reg. This may include partial defs. |
| 172 | unsigned Reg = LI.reg; |
| 173 | for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) { |
| 174 | // Clear all kill flags. They will be reinserted after register allocation |
| 175 | // by LiveIntervalAnalysis::addKillFlags(). |
| 176 | if (MO.isUse()) |
| 177 | MO.setIsKill(false); |
| 178 | if (!MO.readsReg()) |
| 179 | continue; |
| 180 | SlotIndex Idx = getUseIndex(*Indexes, MO); |
| 181 | unsigned SubReg = MO.getSubReg(); |
Matthias Braun | e3d3b88 | 2014-12-10 01:12:30 +0000 | [diff] [blame] | 182 | if (MO.isUse() && (LI.hasSubRanges() || |
| 183 | (MRI->tracksSubRegLiveness() && SubReg != 0))) { |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 184 | unsigned Mask = SubReg != 0 |
| 185 | ? TRI.getSubRegIndexLaneMask(SubReg) |
Aaron Ballman | e5a2a0c | 2014-12-10 14:14:54 +0000 | [diff] [blame] | 186 | : MRI->getMaxLaneMaskForVReg(Reg); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 187 | |
| 188 | // If this is the first time we see a subregister def/use. Initialize |
| 189 | // subranges by creating a copy of the main range. |
| 190 | if (!LI.hasSubRanges()) { |
| 191 | unsigned ClassMask = MRI->getMaxLaneMaskForVReg(Reg); |
| 192 | LI.createSubRangeFrom(*Alloc, ClassMask, LI); |
| 193 | LiveOuts.insert(LiveOuts.begin(), LiveOutData()); |
| 194 | LiveOuts.front().reset(MF->getNumBlockIDs()); |
| 195 | ++NumSubRanges; |
| 196 | } |
| 197 | unsigned SubRangeIdx = 0; |
| 198 | for (LiveInterval::subrange_iterator S = LI.subrange_begin(), |
| 199 | SE = LI.subrange_end(); S != SE; ++S, ++SubRangeIdx) { |
| 200 | // A Mask for subregs common to the existing subrange and current def. |
| 201 | unsigned Common = S->LaneMask & Mask; |
| 202 | if (Common == 0) |
| 203 | continue; |
| 204 | // A Mask for subregs covered by the subrange but not the current def. |
| 205 | unsigned LRest = S->LaneMask & ~Mask; |
| 206 | LiveInterval::SubRange *CommonRange; |
| 207 | unsigned CommonRangeIdx; |
| 208 | if (LRest != 0) { |
| 209 | // Split current subrange into Common and LRest ranges. |
| 210 | S->LaneMask = LRest; |
| 211 | CommonRange = LI.createSubRangeFrom(*Alloc, Common, *S); |
| 212 | CommonRangeIdx = 0; |
| 213 | LiveOuts.insert(LiveOuts.begin(), LiveOutData()); |
| 214 | LiveOuts.front().reset(MF->getNumBlockIDs()); |
| 215 | ++NumSubRanges; |
| 216 | ++SubRangeIdx; |
| 217 | } else { |
| 218 | // The subrange and current def lanemasks match completely. |
| 219 | assert(Common == S->LaneMask); |
| 220 | CommonRange = &*S; |
| 221 | CommonRangeIdx = SubRangeIdx; |
| 222 | } |
| 223 | extend(*CommonRange, Idx, Reg, LiveOuts[CommonRangeIdx]); |
| 224 | Mask &= ~Common; |
| 225 | } |
| 226 | assert(SubRangeIdx == NumSubRanges); |
| 227 | } |
| 228 | extend(LI, Idx, Reg, MainLiveOutData); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
| 233 | void LiveRangeCalc::updateFromLiveIns(LiveOutData &LiveOuts) { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 234 | LiveRangeUpdater Updater; |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 235 | for (const LiveInBlock &I : LiveIn) { |
| 236 | if (!I.DomNode) |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 237 | continue; |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 238 | MachineBasicBlock *MBB = I.DomNode->getBlock(); |
| 239 | assert(I.Value && "No live-in value found"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 240 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 241 | std::tie(Start, End) = Indexes->getMBBRange(MBB); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 242 | |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 243 | if (I.Kill.isValid()) |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 244 | // Value is killed inside this block. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 245 | End = I.Kill; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 246 | else { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 247 | // The value is live-through, update LiveOut as well. |
| 248 | // Defer the Domtree lookup until it is needed. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 249 | assert(LiveOuts.Seen.test(MBB->getNumber())); |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 250 | LiveOuts.Map[MBB] = LiveOutPair(I.Value, nullptr); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 251 | } |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 252 | Updater.setDest(&I.LR); |
| 253 | Updater.add(Start, End, I.Value); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 254 | } |
| 255 | LiveIn.clear(); |
| 256 | } |
| 257 | |
| 258 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 259 | void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg, |
| 260 | LiveOutData &LiveOuts) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 261 | assert(Kill.isValid() && "Invalid SlotIndex"); |
| 262 | assert(Indexes && "Missing SlotIndexes"); |
| 263 | assert(DomTree && "Missing dominator tree"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 264 | |
Jakob Stoklund Olesen | 0494c5c | 2011-09-13 16:47:56 +0000 | [diff] [blame] | 265 | MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot()); |
Lang Hames | 6cee53d | 2011-12-20 20:23:40 +0000 | [diff] [blame] | 266 | assert(KillMBB && "No MBB at Kill"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 267 | |
| 268 | // Is there a def in the same MBB we can extend? |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 269 | if (LR.extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill)) |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 270 | return; |
| 271 | |
| 272 | // Find the single reaching def, or determine if Kill is jointly dominated by |
| 273 | // multiple values, and we may need to create even more phi-defs to preserve |
| 274 | // VNInfo SSA form. Perform a search for all predecessor blocks where we |
| 275 | // know the dominating VNInfo. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 276 | if (findReachingDefs(LR, *KillMBB, Kill, PhysReg, LiveOuts)) |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 277 | return; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 278 | |
| 279 | // When there were multiple different values, we may need new PHIs. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 280 | calculateValues(LiveOuts); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | // This function is called by a client after using the low-level API to add |
| 285 | // live-out and live-in blocks. The unique value optimization is not |
| 286 | // available, SplitEditor::transferValues handles that case directly anyway. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 287 | void LiveRangeCalc::calculateValues(LiveOutData &LiveOuts) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 288 | assert(Indexes && "Missing SlotIndexes"); |
| 289 | assert(DomTree && "Missing dominator tree"); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 290 | updateSSA(LiveOuts); |
| 291 | updateFromLiveIns(LiveOuts); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 295 | bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB, |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 296 | SlotIndex Kill, unsigned PhysReg, |
| 297 | LiveOutData &LiveOuts) { |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 298 | unsigned KillMBBNum = KillMBB.getNumber(); |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 299 | |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 300 | // Block numbers where LR should be live-in. |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 301 | SmallVector<unsigned, 16> WorkList(1, KillMBBNum); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 302 | |
| 303 | // Remember if we have seen more than one value. |
| 304 | bool UniqueVNI = true; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 305 | VNInfo *TheVNI = nullptr; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 306 | |
| 307 | // Using Seen as a visited set, perform a BFS for all reaching defs. |
| 308 | for (unsigned i = 0; i != WorkList.size(); ++i) { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 309 | MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]); |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 310 | |
| 311 | #ifndef NDEBUG |
| 312 | if (MBB->pred_empty()) { |
| 313 | MBB->getParent()->verify(); |
| 314 | llvm_unreachable("Use not jointly dominated by defs."); |
| 315 | } |
| 316 | |
| 317 | if (TargetRegisterInfo::isPhysicalRegister(PhysReg) && |
| 318 | !MBB->isLiveIn(PhysReg)) { |
| 319 | MBB->getParent()->verify(); |
| 320 | errs() << "The register needs to be live in to BB#" << MBB->getNumber() |
| 321 | << ", but is missing from the live-in list.\n"; |
| 322 | llvm_unreachable("Invalid global physical register"); |
| 323 | } |
| 324 | #endif |
| 325 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 326 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 327 | PE = MBB->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 328 | MachineBasicBlock *Pred = *PI; |
| 329 | |
| 330 | // Is this a known live-out block? |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 331 | if (LiveOuts.Seen.test(Pred->getNumber())) { |
| 332 | if (VNInfo *VNI = LiveOuts.Map[Pred].first) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 333 | if (TheVNI && TheVNI != VNI) |
| 334 | UniqueVNI = false; |
| 335 | TheVNI = VNI; |
| 336 | } |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 341 | std::tie(Start, End) = Indexes->getMBBRange(Pred); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 342 | |
| 343 | // First time we see Pred. Try to determine the live-out value, but set |
| 344 | // it as null if Pred is live-through with an unknown value. |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 345 | VNInfo *VNI = LR.extendInBlock(Start, End); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 346 | LiveOuts.setLiveOutValue(Pred, VNI); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 347 | if (VNI) { |
| 348 | if (TheVNI && TheVNI != VNI) |
| 349 | UniqueVNI = false; |
| 350 | TheVNI = VNI; |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | // No, we need a live-in value for Pred as well |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 355 | if (Pred != &KillMBB) |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 356 | WorkList.push_back(Pred->getNumber()); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 357 | else |
| 358 | // Loopback to KillMBB, so value is really live through. |
| 359 | Kill = SlotIndex(); |
| 360 | } |
| 361 | } |
| 362 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 363 | LiveIn.clear(); |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 364 | |
| 365 | // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but |
| 366 | // neither require it. Skip the sorting overhead for small updates. |
| 367 | if (WorkList.size() > 4) |
| 368 | array_pod_sort(WorkList.begin(), WorkList.end()); |
| 369 | |
| 370 | // If a unique reaching def was found, blit in the live ranges immediately. |
| 371 | if (UniqueVNI) { |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 372 | LiveRangeUpdater Updater(&LR); |
| 373 | for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(), |
| 374 | E = WorkList.end(); I != E; ++I) { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 375 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 376 | std::tie(Start, End) = Indexes->getMBBRange(*I); |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 377 | // Trim the live range in KillMBB. |
| 378 | if (*I == KillMBBNum && Kill.isValid()) |
| 379 | End = Kill; |
| 380 | else |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 381 | LiveOuts.Map[MF->getBlockNumbered(*I)] = |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 382 | LiveOutPair(TheVNI, nullptr); |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 383 | Updater.add(Start, End, TheVNI); |
| 384 | } |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | // Multiple values were found, so transfer the work list to the LiveIn array |
| 389 | // where UpdateSSA will use it as a work list. |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 390 | LiveIn.reserve(WorkList.size()); |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 391 | for (SmallVectorImpl<unsigned>::const_iterator |
| 392 | I = WorkList.begin(), E = WorkList.end(); I != E; ++I) { |
| 393 | MachineBasicBlock *MBB = MF->getBlockNumbered(*I); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 394 | addLiveInBlock(LR, DomTree->getNode(MBB)); |
| 395 | if (MBB == &KillMBB) |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 396 | LiveIn.back().Kill = Kill; |
| 397 | } |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 398 | |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 399 | return false; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | |
| 403 | // This is essentially the same iterative algorithm that SSAUpdater uses, |
| 404 | // except we already have a dominator tree, so we don't have to recompute it. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 405 | void LiveRangeCalc::updateSSA(LiveOutData &LiveOuts) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 406 | assert(Indexes && "Missing SlotIndexes"); |
| 407 | assert(DomTree && "Missing dominator tree"); |
| 408 | |
| 409 | // Interate until convergence. |
| 410 | unsigned Changes; |
| 411 | do { |
| 412 | Changes = 0; |
| 413 | // Propagate live-out values down the dominator tree, inserting phi-defs |
| 414 | // when necessary. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 415 | for (LiveInBlock &I : LiveIn) { |
| 416 | MachineDomTreeNode *Node = I.DomNode; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 417 | // Skip block if the live-in value has already been determined. |
| 418 | if (!Node) |
| 419 | continue; |
| 420 | MachineBasicBlock *MBB = Node->getBlock(); |
| 421 | MachineDomTreeNode *IDom = Node->getIDom(); |
| 422 | LiveOutPair IDomValue; |
| 423 | |
| 424 | // We need a live-in value to a block with no immediate dominator? |
| 425 | // This is probably an unreachable block that has survived somehow. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 426 | bool needPHI = !IDom |
| 427 | || !LiveOuts.Seen.test(IDom->getBlock()->getNumber()); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 428 | |
| 429 | // IDom dominates all of our predecessors, but it may not be their |
| 430 | // immediate dominator. Check if any of them have live-out values that are |
| 431 | // properly dominated by IDom. If so, we need a phi-def here. |
| 432 | if (!needPHI) { |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 433 | IDomValue = LiveOuts.Map[IDom->getBlock()]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 434 | |
| 435 | // Cache the DomTree node that defined the value. |
| 436 | if (IDomValue.first && !IDomValue.second) |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 437 | LiveOuts.Map[IDom->getBlock()].second = IDomValue.second = |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 438 | DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def)); |
| 439 | |
| 440 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 441 | PE = MBB->pred_end(); PI != PE; ++PI) { |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 442 | LiveOutPair &Value = LiveOuts.Map[*PI]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 443 | if (!Value.first || Value.first == IDomValue.first) |
| 444 | continue; |
| 445 | |
| 446 | // Cache the DomTree node that defined the value. |
| 447 | if (!Value.second) |
| 448 | Value.second = |
| 449 | DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def)); |
| 450 | |
| 451 | // This predecessor is carrying something other than IDomValue. |
| 452 | // It could be because IDomValue hasn't propagated yet, or it could be |
| 453 | // because MBB is in the dominance frontier of that value. |
| 454 | if (DomTree->dominates(IDom, Value.second)) { |
| 455 | needPHI = true; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // The value may be live-through even if Kill is set, as can happen when |
| 462 | // we are called from extendRange. In that case LiveOutSeen is true, and |
| 463 | // LiveOut indicates a foreign or missing value. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 464 | LiveOutPair &LOP = LiveOuts.Map[MBB]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 465 | |
| 466 | // Create a phi-def if required. |
| 467 | if (needPHI) { |
| 468 | ++Changes; |
| 469 | assert(Alloc && "Need VNInfo allocator to create PHI-defs"); |
| 470 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 471 | std::tie(Start, End) = Indexes->getMBBRange(MBB); |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 472 | LiveRange &LR = I.LR; |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 473 | VNInfo *VNI = LR.getNextValue(Start, *Alloc); |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 474 | I.Value = VNI; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 475 | // This block is done, we know the final value. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 476 | I.DomNode = nullptr; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 477 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 478 | // Add liveness since updateFromLiveIns now skips this node. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 479 | if (I.Kill.isValid()) |
| 480 | LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI)); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 481 | else { |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 482 | LR.addSegment(LiveInterval::Segment(Start, End, VNI)); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 483 | LOP = LiveOutPair(VNI, Node); |
| 484 | } |
| 485 | } else if (IDomValue.first) { |
| 486 | // No phi-def here. Remember incoming value. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 487 | I.Value = IDomValue.first; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 488 | |
| 489 | // If the IDomValue is killed in the block, don't propagate through. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame^] | 490 | if (I.Kill.isValid()) |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 491 | continue; |
| 492 | |
| 493 | // Propagate IDomValue if it isn't killed: |
| 494 | // MBB is live-out and doesn't define its own value. |
| 495 | if (LOP.first == IDomValue.first) |
| 496 | continue; |
| 497 | ++Changes; |
| 498 | LOP = IDomValue; |
| 499 | } |
| 500 | } |
| 501 | } while (Changes); |
| 502 | } |