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