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