Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 1 | //===- LiveRangeCalc.cpp - Calculate live ranges --------------------------===// |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Implementation of the LiveRangeCalc class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 13 | #include "LiveRangeCalc.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/BitVector.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/CodeGen/LiveInterval.h" |
| 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstr.h" |
| 23 | #include "llvm/CodeGen/MachineOperand.h" |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 27 | #include "llvm/MC/LaneBitmask.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | #include <cassert> |
| 32 | #include <iterator> |
| 33 | #include <tuple> |
| 34 | #include <utility> |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "regalloc" |
| 39 | |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 40 | // Reserve an address that indicates a value that is known to be "undef". |
| 41 | static VNInfo UndefVNI(0xbad, SlotIndex()); |
| 42 | |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 43 | void LiveRangeCalc::resetLiveOutMap() { |
| 44 | unsigned NumBlocks = MF->getNumBlockIDs(); |
| 45 | Seen.clear(); |
| 46 | Seen.resize(NumBlocks); |
Matthias Braun | a6e7740 | 2017-06-27 18:05:26 +0000 | [diff] [blame] | 47 | EntryInfos.clear(); |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 48 | Map.resize(NumBlocks); |
| 49 | } |
| 50 | |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 51 | void LiveRangeCalc::reset(const MachineFunction *mf, |
Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 52 | SlotIndexes *SI, |
| 53 | MachineDominatorTree *MDT, |
| 54 | VNInfo::Allocator *VNIA) { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 55 | MF = mf; |
Jakob Stoklund Olesen | 5ef0e0b | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 56 | MRI = &MF->getRegInfo(); |
| 57 | Indexes = SI; |
| 58 | DomTree = MDT; |
| 59 | Alloc = VNIA; |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 60 | resetLiveOutMap(); |
Matthias Braun | c3a72c2 | 2014-12-15 21:36:35 +0000 | [diff] [blame] | 61 | LiveIn.clear(); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 64 | static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc, |
| 65 | LiveRange &LR, const MachineOperand &MO) { |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 66 | const MachineInstr &MI = *MO.getParent(); |
| 67 | SlotIndex DefIdx = |
| 68 | Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber()); |
Matthias Braun | c3a72c2 | 2014-12-15 21:36:35 +0000 | [diff] [blame] | 69 | |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 70 | // Create the def in LR. This may find an existing def. |
| 71 | LR.createDeadDef(DefIdx, Alloc); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Matthias Braun | a25e13a | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 74 | void LiveRangeCalc::calculate(LiveInterval &LI, bool TrackSubRegs) { |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 75 | assert(MRI && Indexes && "call reset() first"); |
| 76 | |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 77 | // Step 1: Create minimal live segments for every definition of Reg. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 78 | // 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] | 79 | // createDeadDef() will deduplicate. |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 80 | const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo(); |
| 81 | unsigned Reg = LI.reg; |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 82 | for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) { |
| 83 | if (!MO.isDef() && !MO.readsReg()) |
| 84 | continue; |
| 85 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 86 | unsigned SubReg = MO.getSubReg(); |
Matthias Braun | a25e13a | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 87 | if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) { |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 88 | LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg) |
Krzysztof Parzyszek | 0a955d6 | 2016-08-29 13:15:35 +0000 | [diff] [blame] | 89 | : MRI->getMaxLaneMaskForVReg(Reg); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 90 | // If this is the first time we see a subregister def, initialize |
| 91 | // subranges by creating a copy of the main range. |
| 92 | if (!LI.hasSubRanges() && !LI.empty()) { |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 93 | LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 94 | LI.createSubRangeFrom(*Alloc, ClassMask, LI); |
| 95 | } |
| 96 | |
Matthias Braun | a04d7ad | 2017-03-03 19:05:34 +0000 | [diff] [blame] | 97 | LI.refineSubRanges(*Alloc, SubMask, |
Quentin Colombet | c74271c | 2019-03-26 21:27:15 +0000 | [diff] [blame] | 98 | [&MO, this](LiveInterval::SubRange &SR) { |
| 99 | if (MO.isDef()) |
| 100 | createDeadDef(*Indexes, *Alloc, SR, MO); |
| 101 | }, |
| 102 | *Indexes, TRI); |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 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()) { |
Krzysztof Parzyszek | a7ed090 | 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 | dbcca0d | 2014-12-24 02:11:51 +0000 | [diff] [blame] | 122 | } |
| 123 | LI.clear(); |
Matthias Braun | 71f9564 | 2016-05-20 23:14:56 +0000 | [diff] [blame] | 124 | constructMainRangeFromSubranges(LI); |
Matthias Braun | dbcca0d | 2014-12-24 02:11:51 +0000 | [diff] [blame] | 125 | } else { |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 126 | resetLiveOutMap(); |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 127 | extendToUses(LI, Reg, LaneBitmask::getAll()); |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 128 | } |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Matthias Braun | 71f9564 | 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 | 71f9564 | 2016-05-20 23:14:56 +0000 | [diff] [blame] | 143 | resetLiveOutMap(); |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 144 | extendToUses(MainRange, LI.reg, LaneBitmask::getAll(), &LI); |
Matthias Braun | 71f9564 | 2016-05-20 23:14:56 +0000 | [diff] [blame] | 145 | } |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 146 | |
Matthias Braun | c3a72c2 | 2014-12-15 21:36:35 +0000 | [diff] [blame] | 147 | void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) { |
Jakob Stoklund Olesen | 989b3b1 | 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 | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 151 | // LR.createDeadDef() will deduplicate. |
Matthias Braun | 1aed6ff | 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 | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Krzysztof Parzyszek | a7ed090 | 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 | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 162 | // Visit all operands that read Reg. This may include partial defs. |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 163 | bool IsSubRange = !Mask.all(); |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 164 | const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo(); |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 165 | for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) { |
Jakob Stoklund Olesen | 4aed470 | 2012-09-06 18:15:15 +0000 | [diff] [blame] | 166 | // Clear all kill flags. They will be reinserted after register allocation |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 167 | // by LiveIntervals::addKillFlags(). |
Jakob Stoklund Olesen | 4aed470 | 2012-09-06 18:15:15 +0000 | [diff] [blame] | 168 | if (MO.isUse()) |
| 169 | MO.setIsKill(false); |
Krzysztof Parzyszek | 3bf4aec | 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 | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 175 | continue; |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 176 | |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 177 | unsigned SubReg = MO.getSubReg(); |
| 178 | if (SubReg != 0) { |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 179 | LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg); |
| 180 | if (MO.isDef()) |
Krzysztof Parzyszek | 0a955d6 | 2016-08-29 13:15:35 +0000 | [diff] [blame] | 181 | SLM = ~SLM; |
| 182 | // Ignore uses not reading the current (sub)range. |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 183 | if ((SLM & Mask).none()) |
Matthias Braun | 1aed6ff | 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 | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 207 | UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber); |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Jakob Stoklund Olesen | 989b3b1 | 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 | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 212 | extend(LR, UseIdx, Reg, Undefs); |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 216 | void LiveRangeCalc::updateFromLiveIns() { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 217 | LiveRangeUpdater Updater; |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 218 | for (const LiveInBlock &I : LiveIn) { |
| 219 | if (!I.DomNode) |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 220 | continue; |
Matthias Braun | 42fab34 | 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 | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 223 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 224 | std::tie(Start, End) = Indexes->getMBBRange(MBB); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 225 | |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 226 | if (I.Kill.isValid()) |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 227 | // Value is killed inside this block. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 228 | End = I.Kill; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 229 | else { |
Jakob Stoklund Olesen | b389271 | 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 | 1aed6ff | 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 | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 234 | } |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 235 | Updater.setDest(&I.LR); |
| 236 | Updater.add(Start, End, I.Value); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 237 | } |
| 238 | LiveIn.clear(); |
| 239 | } |
| 240 | |
Krzysztof Parzyszek | a7ed090 | 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 | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 243 | assert(Use.isValid() && "Invalid SlotIndex"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 244 | assert(Indexes && "Missing SlotIndexes"); |
| 245 | assert(DomTree && "Missing dominator tree"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 246 | |
Matthias Braun | 11042c8 | 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 | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 249 | |
| 250 | // Is there a def in the same MBB we can extend? |
Krzysztof Parzyszek | a7ed090 | 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 | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 253 | return; |
| 254 | |
Matthias Braun | 11042c8 | 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 | 487f2a3 | 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 | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 259 | if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs)) |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 260 | return; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 261 | |
| 262 | // When there were multiple different values, we may need new PHIs. |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 263 | calculateValues(); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Jakob Stoklund Olesen | 487f2a3 | 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 | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 269 | void LiveRangeCalc::calculateValues() { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 270 | assert(Indexes && "Missing SlotIndexes"); |
| 271 | assert(DomTree && "Missing dominator tree"); |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 272 | updateSSA(); |
| 273 | updateFromLiveIns(); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Krzysztof Parzyszek | a7ed090 | 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 | 17d266b | 2017-01-13 17:12:16 +0000 | [diff] [blame] | 285 | auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool { |
Krzysztof Parzyszek | a7ed090 | 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 | 0b7688e | 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 | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 307 | SlotIndex Begin, End; |
| 308 | std::tie(Begin, End) = Indexes->getMBBRange(&B); |
Krzysztof Parzyszek | de44c9d | 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 | a7ed090 | 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 | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 346 | bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB, |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 347 | SlotIndex Use, unsigned PhysReg, |
| 348 | ArrayRef<SlotIndex> Undefs) { |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 349 | unsigned UseMBBNum = UseMBB.getNumber(); |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 350 | |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 351 | // Block numbers where LR should be live-in. |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 352 | SmallVector<unsigned, 16> WorkList(1, UseMBBNum); |
Jakob Stoklund Olesen | 487f2a3 | 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 | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 356 | VNInfo *TheVNI = nullptr; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 357 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 358 | bool FoundUndef = false; |
| 359 | |
Jakob Stoklund Olesen | 487f2a3 | 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 | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 362 | MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]); |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 363 | |
| 364 | #ifndef NDEBUG |
Matthias Braun | e1d9628 | 2016-09-19 16:49:45 +0000 | [diff] [blame] | 365 | if (MBB->pred_empty()) { |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 366 | MBB->getParent()->verify(); |
Matt Arsenault | 901a95f | 2018-10-30 01:11:31 +0000 | [diff] [blame] | 367 | errs() << "Use of " << printReg(PhysReg, MRI->getTargetRegisterInfo()) |
Matthias Braun | 5391754 | 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 | e1d9628 | 2016-09-19 16:49:45 +0000 | [diff] [blame] | 372 | report_fatal_error("Use not jointly dominated by defs."); |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 375 | if (Register::isPhysicalRegister(PhysReg) && !MBB->isLiveIn(PhysReg)) { |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 376 | MBB->getParent()->verify(); |
Matt Arsenault | f3d1a1a | 2016-09-03 06:57:49 +0000 | [diff] [blame] | 377 | const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo(); |
Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 378 | errs() << "The register " << printReg(PhysReg, TRI) |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 379 | << " needs to be live in to " << printMBBReference(*MBB) |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 380 | << ", but is missing from the live-in list.\n"; |
Matthias Braun | e1d9628 | 2016-09-19 16:49:45 +0000 | [diff] [blame] | 381 | report_fatal_error("Invalid global physical register"); |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 382 | } |
| 383 | #endif |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 384 | FoundUndef |= MBB->pred_empty(); |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 385 | |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 386 | for (MachineBasicBlock *Pred : MBB->predecessors()) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 387 | // Is this a known live-out block? |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 388 | if (Seen.test(Pred->getNumber())) { |
| 389 | if (VNInfo *VNI = Map[Pred].first) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 390 | if (TheVNI && TheVNI != VNI) |
| 391 | UniqueVNI = false; |
| 392 | TheVNI = VNI; |
| 393 | } |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 398 | std::tie(Start, End) = Indexes->getMBBRange(Pred); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 399 | |
| 400 | // First time we see Pred. Try to determine the live-out value, but set |
| 401 | // it as null if Pred is live-through with an unknown value. |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 402 | auto EP = LR.extendInBlock(Undefs, Start, End); |
| 403 | VNInfo *VNI = EP.first; |
| 404 | FoundUndef |= EP.second; |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 405 | setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 406 | if (VNI) { |
| 407 | if (TheVNI && TheVNI != VNI) |
| 408 | UniqueVNI = false; |
| 409 | TheVNI = VNI; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 410 | } |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 411 | if (VNI || EP.second) |
| 412 | continue; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 413 | |
| 414 | // No, we need a live-in value for Pred as well |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 415 | if (Pred != &UseMBB) |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 416 | WorkList.push_back(Pred->getNumber()); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 417 | else |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 418 | // Loopback to UseMBB, so value is really live through. |
| 419 | Use = SlotIndex(); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 423 | LiveIn.clear(); |
Krzysztof Parzyszek | 3008594 | 2017-06-28 15:46:16 +0000 | [diff] [blame] | 424 | FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI); |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 425 | if (!Undefs.empty() && FoundUndef) |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 426 | UniqueVNI = false; |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 427 | |
| 428 | // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but |
| 429 | // neither require it. Skip the sorting overhead for small updates. |
| 430 | if (WorkList.size() > 4) |
| 431 | array_pod_sort(WorkList.begin(), WorkList.end()); |
| 432 | |
| 433 | // If a unique reaching def was found, blit in the live ranges immediately. |
| 434 | if (UniqueVNI) { |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 435 | assert(TheVNI != nullptr && TheVNI != &UndefVNI); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 436 | LiveRangeUpdater Updater(&LR); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 437 | for (unsigned BN : WorkList) { |
| 438 | SlotIndex Start, End; |
| 439 | std::tie(Start, End) = Indexes->getMBBRange(BN); |
| 440 | // Trim the live range in UseMBB. |
| 441 | if (BN == UseMBBNum && Use.isValid()) |
| 442 | End = Use; |
| 443 | else |
| 444 | Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr); |
| 445 | Updater.add(Start, End, TheVNI); |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 446 | } |
| 447 | return true; |
| 448 | } |
| 449 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 450 | // Prepare the defined/undefined bit vectors. |
Matthias Braun | a6e7740 | 2017-06-27 18:05:26 +0000 | [diff] [blame] | 451 | EntryInfoMap::iterator Entry; |
| 452 | bool DidInsert; |
| 453 | std::tie(Entry, DidInsert) = EntryInfos.insert( |
| 454 | std::make_pair(&LR, std::make_pair(BitVector(), BitVector()))); |
| 455 | if (DidInsert) { |
| 456 | // Initialize newly inserted entries. |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 457 | unsigned N = MF->getNumBlockIDs(); |
Matthias Braun | a6e7740 | 2017-06-27 18:05:26 +0000 | [diff] [blame] | 458 | Entry->second.first.resize(N); |
| 459 | Entry->second.second.resize(N); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 460 | } |
Matthias Braun | a6e7740 | 2017-06-27 18:05:26 +0000 | [diff] [blame] | 461 | BitVector &DefOnEntry = Entry->second.first; |
| 462 | BitVector &UndefOnEntry = Entry->second.second; |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 463 | |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 464 | // Multiple values were found, so transfer the work list to the LiveIn array |
| 465 | // where UpdateSSA will use it as a work list. |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 466 | LiveIn.reserve(WorkList.size()); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 467 | for (unsigned BN : WorkList) { |
| 468 | MachineBasicBlock *MBB = MF->getBlockNumbered(BN); |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 469 | if (!Undefs.empty() && |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 470 | !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry)) |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 471 | continue; |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 472 | addLiveInBlock(LR, DomTree->getNode(MBB)); |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 473 | if (MBB == &UseMBB) |
| 474 | LiveIn.back().Kill = Use; |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 475 | } |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 476 | |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 477 | return false; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 480 | // This is essentially the same iterative algorithm that SSAUpdater uses, |
| 481 | // 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] | 482 | void LiveRangeCalc::updateSSA() { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 483 | assert(Indexes && "Missing SlotIndexes"); |
| 484 | assert(DomTree && "Missing dominator tree"); |
| 485 | |
| 486 | // Interate until convergence. |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 487 | bool Changed; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 488 | do { |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 489 | Changed = false; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 490 | // Propagate live-out values down the dominator tree, inserting phi-defs |
| 491 | // when necessary. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 492 | for (LiveInBlock &I : LiveIn) { |
| 493 | MachineDomTreeNode *Node = I.DomNode; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 494 | // Skip block if the live-in value has already been determined. |
| 495 | if (!Node) |
| 496 | continue; |
| 497 | MachineBasicBlock *MBB = Node->getBlock(); |
| 498 | MachineDomTreeNode *IDom = Node->getIDom(); |
| 499 | LiveOutPair IDomValue; |
| 500 | |
| 501 | // We need a live-in value to a block with no immediate dominator? |
| 502 | // This is probably an unreachable block that has survived somehow. |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 503 | bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber()); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 504 | |
| 505 | // IDom dominates all of our predecessors, but it may not be their |
| 506 | // immediate dominator. Check if any of them have live-out values that are |
| 507 | // properly dominated by IDom. If so, we need a phi-def here. |
| 508 | if (!needPHI) { |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 509 | IDomValue = Map[IDom->getBlock()]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 510 | |
| 511 | // Cache the DomTree node that defined the value. |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 512 | if (IDomValue.first && IDomValue.first != &UndefVNI && |
| 513 | !IDomValue.second) { |
| 514 | Map[IDom->getBlock()].second = IDomValue.second = |
| 515 | DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def)); |
| 516 | } |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 517 | |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 518 | for (MachineBasicBlock *Pred : MBB->predecessors()) { |
| 519 | LiveOutPair &Value = Map[Pred]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 520 | if (!Value.first || Value.first == IDomValue.first) |
| 521 | continue; |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 522 | if (Value.first == &UndefVNI) { |
| 523 | needPHI = true; |
| 524 | break; |
| 525 | } |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 526 | |
| 527 | // Cache the DomTree node that defined the value. |
| 528 | if (!Value.second) |
| 529 | Value.second = |
| 530 | DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def)); |
| 531 | |
| 532 | // This predecessor is carrying something other than IDomValue. |
| 533 | // It could be because IDomValue hasn't propagated yet, or it could be |
| 534 | // because MBB is in the dominance frontier of that value. |
| 535 | if (DomTree->dominates(IDom, Value.second)) { |
| 536 | needPHI = true; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // The value may be live-through even if Kill is set, as can happen when |
| 543 | // we are called from extendRange. In that case LiveOutSeen is true, and |
| 544 | // LiveOut indicates a foreign or missing value. |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 545 | LiveOutPair &LOP = Map[MBB]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 546 | |
| 547 | // Create a phi-def if required. |
| 548 | if (needPHI) { |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 549 | Changed = true; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 550 | assert(Alloc && "Need VNInfo allocator to create PHI-defs"); |
| 551 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 552 | std::tie(Start, End) = Indexes->getMBBRange(MBB); |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 553 | LiveRange &LR = I.LR; |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 554 | VNInfo *VNI = LR.getNextValue(Start, *Alloc); |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 555 | I.Value = VNI; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 556 | // This block is done, we know the final value. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 557 | I.DomNode = nullptr; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 558 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 559 | // Add liveness since updateFromLiveIns now skips this node. |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 560 | if (I.Kill.isValid()) { |
| 561 | if (VNI) |
| 562 | LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI)); |
| 563 | } else { |
| 564 | if (VNI) |
| 565 | LR.addSegment(LiveInterval::Segment(Start, End, VNI)); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 566 | LOP = LiveOutPair(VNI, Node); |
| 567 | } |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 568 | } else if (IDomValue.first && IDomValue.first != &UndefVNI) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 569 | // No phi-def here. Remember incoming value. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 570 | I.Value = IDomValue.first; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 571 | |
| 572 | // If the IDomValue is killed in the block, don't propagate through. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 573 | if (I.Kill.isValid()) |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 574 | continue; |
| 575 | |
| 576 | // Propagate IDomValue if it isn't killed: |
| 577 | // MBB is live-out and doesn't define its own value. |
| 578 | if (LOP.first == IDomValue.first) |
| 579 | continue; |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 580 | Changed = true; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 581 | LOP = IDomValue; |
| 582 | } |
| 583 | } |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 584 | } while (Changed); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 585 | } |
Krzysztof Parzyszek | 70f0270 | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 586 | |
| 587 | bool LiveRangeCalc::isJointlyDominated(const MachineBasicBlock *MBB, |
| 588 | ArrayRef<SlotIndex> Defs, |
| 589 | const SlotIndexes &Indexes) { |
| 590 | const MachineFunction &MF = *MBB->getParent(); |
| 591 | BitVector DefBlocks(MF.getNumBlockIDs()); |
| 592 | for (SlotIndex I : Defs) |
| 593 | DefBlocks.set(Indexes.getMBBFromIndex(I)->getNumber()); |
| 594 | |
| 595 | SetVector<unsigned> PredQueue; |
| 596 | PredQueue.insert(MBB->getNumber()); |
| 597 | for (unsigned i = 0; i != PredQueue.size(); ++i) { |
| 598 | unsigned BN = PredQueue[i]; |
| 599 | if (DefBlocks[BN]) |
| 600 | return true; |
| 601 | const MachineBasicBlock *B = MF.getBlockNumbered(BN); |
| 602 | for (const MachineBasicBlock *P : B->predecessors()) |
| 603 | PredQueue.insert(P->getNumber()); |
| 604 | } |
| 605 | return false; |
| 606 | } |