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