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