/src/llvm-project/llvm/lib/CodeGen/LiveIntervals.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- LiveIntervals.cpp - Live Interval Analysis -------------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | /// \file This file implements the LiveInterval analysis pass which is used |
10 | | /// by the Linear Scan Register allocator. This pass linearizes the |
11 | | /// basic blocks of the function in DFS order and computes live intervals for |
12 | | /// each virtual and physical register. |
13 | | // |
14 | | //===----------------------------------------------------------------------===// |
15 | | |
16 | | #include "llvm/CodeGen/LiveIntervals.h" |
17 | | #include "llvm/ADT/ArrayRef.h" |
18 | | #include "llvm/ADT/DepthFirstIterator.h" |
19 | | #include "llvm/ADT/SmallPtrSet.h" |
20 | | #include "llvm/ADT/SmallVector.h" |
21 | | #include "llvm/ADT/iterator_range.h" |
22 | | #include "llvm/CodeGen/LiveInterval.h" |
23 | | #include "llvm/CodeGen/LiveIntervalCalc.h" |
24 | | #include "llvm/CodeGen/LiveVariables.h" |
25 | | #include "llvm/CodeGen/MachineBasicBlock.h" |
26 | | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
27 | | #include "llvm/CodeGen/MachineDominators.h" |
28 | | #include "llvm/CodeGen/MachineFunction.h" |
29 | | #include "llvm/CodeGen/MachineInstr.h" |
30 | | #include "llvm/CodeGen/MachineInstrBundle.h" |
31 | | #include "llvm/CodeGen/MachineOperand.h" |
32 | | #include "llvm/CodeGen/MachineRegisterInfo.h" |
33 | | #include "llvm/CodeGen/Passes.h" |
34 | | #include "llvm/CodeGen/SlotIndexes.h" |
35 | | #include "llvm/CodeGen/StackMaps.h" |
36 | | #include "llvm/CodeGen/TargetRegisterInfo.h" |
37 | | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
38 | | #include "llvm/CodeGen/VirtRegMap.h" |
39 | | #include "llvm/Config/llvm-config.h" |
40 | | #include "llvm/IR/Statepoint.h" |
41 | | #include "llvm/MC/LaneBitmask.h" |
42 | | #include "llvm/MC/MCRegisterInfo.h" |
43 | | #include "llvm/Pass.h" |
44 | | #include "llvm/Support/CommandLine.h" |
45 | | #include "llvm/Support/Compiler.h" |
46 | | #include "llvm/Support/Debug.h" |
47 | | #include "llvm/Support/MathExtras.h" |
48 | | #include "llvm/Support/raw_ostream.h" |
49 | | #include <algorithm> |
50 | | #include <cassert> |
51 | | #include <cstdint> |
52 | | #include <iterator> |
53 | | #include <tuple> |
54 | | #include <utility> |
55 | | |
56 | | using namespace llvm; |
57 | | |
58 | | #define DEBUG_TYPE "regalloc" |
59 | | |
60 | | char LiveIntervals::ID = 0; |
61 | | char &llvm::LiveIntervalsID = LiveIntervals::ID; |
62 | 62 | INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", "Live Interval Analysis", |
63 | 62 | false, false) |
64 | 62 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
65 | 62 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
66 | 62 | INITIALIZE_PASS_END(LiveIntervals, "liveintervals", |
67 | | "Live Interval Analysis", false, false) |
68 | | |
69 | | #ifndef NDEBUG |
70 | | static cl::opt<bool> EnablePrecomputePhysRegs( |
71 | | "precompute-phys-liveness", cl::Hidden, |
72 | | cl::desc("Eagerly compute live intervals for all physreg units.")); |
73 | | #else |
74 | | static bool EnablePrecomputePhysRegs = false; |
75 | | #endif // NDEBUG |
76 | | |
77 | | namespace llvm { |
78 | | |
79 | | cl::opt<bool> UseSegmentSetForPhysRegs( |
80 | | "use-segment-set-for-physregs", cl::Hidden, cl::init(true), |
81 | | cl::desc( |
82 | | "Use segment set for the computation of the live ranges of physregs.")); |
83 | | |
84 | | } // end namespace llvm |
85 | | |
86 | 59.1k | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { |
87 | 59.1k | AU.setPreservesCFG(); |
88 | 59.1k | AU.addPreserved<LiveVariables>(); |
89 | 59.1k | AU.addPreservedID(MachineLoopInfoID); |
90 | 59.1k | AU.addRequiredTransitiveID(MachineDominatorsID); |
91 | 59.1k | AU.addPreservedID(MachineDominatorsID); |
92 | 59.1k | AU.addPreserved<SlotIndexes>(); |
93 | 59.1k | AU.addRequiredTransitive<SlotIndexes>(); |
94 | 59.1k | MachineFunctionPass::getAnalysisUsage(AU); |
95 | 59.1k | } |
96 | | |
97 | 59.1k | LiveIntervals::LiveIntervals() : MachineFunctionPass(ID) { |
98 | 59.1k | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
99 | 59.1k | } |
100 | | |
101 | 59.1k | LiveIntervals::~LiveIntervals() { delete LICalc; } |
102 | | |
103 | 190k | void LiveIntervals::releaseMemory() { |
104 | | // Free the live intervals themselves. |
105 | 8.25M | for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i) |
106 | 8.06M | delete VirtRegIntervals[Register::index2VirtReg(i)]; |
107 | 190k | VirtRegIntervals.clear(); |
108 | 190k | RegMaskSlots.clear(); |
109 | 190k | RegMaskBits.clear(); |
110 | 190k | RegMaskBlocks.clear(); |
111 | | |
112 | 190k | for (LiveRange *LR : RegUnitRanges) |
113 | 38.7M | delete LR; |
114 | 190k | RegUnitRanges.clear(); |
115 | | |
116 | | // Release VNInfo memory regions, VNInfo objects don't need to be dtor'd. |
117 | 190k | VNInfoAllocator.Reset(); |
118 | 190k | } |
119 | | |
120 | 190k | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
121 | 190k | MF = &fn; |
122 | 190k | MRI = &MF->getRegInfo(); |
123 | 190k | TRI = MF->getSubtarget().getRegisterInfo(); |
124 | 190k | TII = MF->getSubtarget().getInstrInfo(); |
125 | 190k | Indexes = &getAnalysis<SlotIndexes>(); |
126 | 190k | DomTree = &getAnalysis<MachineDominatorTree>(); |
127 | | |
128 | 190k | if (!LICalc) |
129 | 58.5k | LICalc = new LiveIntervalCalc(); |
130 | | |
131 | | // Allocate space for all virtual registers. |
132 | 190k | VirtRegIntervals.resize(MRI->getNumVirtRegs()); |
133 | | |
134 | 190k | computeVirtRegs(); |
135 | 190k | computeRegMasks(); |
136 | 190k | computeLiveInRegUnits(); |
137 | | |
138 | 190k | if (EnablePrecomputePhysRegs) { |
139 | | // For stress testing, precompute live ranges of all physical register |
140 | | // units, including reserved registers. |
141 | 0 | for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) |
142 | 0 | getRegUnit(i); |
143 | 0 | } |
144 | 190k | LLVM_DEBUG(dump()); |
145 | 190k | return false; |
146 | 190k | } |
147 | | |
148 | 0 | void LiveIntervals::print(raw_ostream &OS, const Module* ) const { |
149 | 0 | OS << "********** INTERVALS **********\n"; |
150 | | |
151 | | // Dump the regunits. |
152 | 0 | for (unsigned Unit = 0, UnitE = RegUnitRanges.size(); Unit != UnitE; ++Unit) |
153 | 0 | if (LiveRange *LR = RegUnitRanges[Unit]) |
154 | 0 | OS << printRegUnit(Unit, TRI) << ' ' << *LR << '\n'; |
155 | | |
156 | | // Dump the virtregs. |
157 | 0 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
158 | 0 | Register Reg = Register::index2VirtReg(i); |
159 | 0 | if (hasInterval(Reg)) |
160 | 0 | OS << getInterval(Reg) << '\n'; |
161 | 0 | } |
162 | |
|
163 | 0 | OS << "RegMasks:"; |
164 | 0 | for (SlotIndex Idx : RegMaskSlots) |
165 | 0 | OS << ' ' << Idx; |
166 | 0 | OS << '\n'; |
167 | |
|
168 | 0 | printInstrs(OS); |
169 | 0 | } |
170 | | |
171 | 0 | void LiveIntervals::printInstrs(raw_ostream &OS) const { |
172 | 0 | OS << "********** MACHINEINSTRS **********\n"; |
173 | 0 | MF->print(OS, Indexes); |
174 | 0 | } |
175 | | |
176 | | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
177 | 0 | LLVM_DUMP_METHOD void LiveIntervals::dumpInstrs() const { |
178 | 0 | printInstrs(dbgs()); |
179 | 0 | } |
180 | | #endif |
181 | | |
182 | 5.53M | LiveInterval *LiveIntervals::createInterval(Register reg) { |
183 | 5.53M | float Weight = reg.isPhysical() ? huge_valf : 0.0F; |
184 | 5.53M | return new LiveInterval(reg, Weight); |
185 | 5.53M | } |
186 | | |
187 | | /// Compute the live interval of a virtual register, based on defs and uses. |
188 | 5.18M | bool LiveIntervals::computeVirtRegInterval(LiveInterval &LI) { |
189 | 5.18M | assert(LICalc && "LICalc not initialized."); |
190 | 0 | assert(LI.empty() && "Should only compute empty intervals."); |
191 | 0 | LICalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
192 | 5.18M | LICalc->calculate(LI, MRI->shouldTrackSubRegLiveness(LI.reg())); |
193 | 5.18M | return computeDeadValues(LI, nullptr); |
194 | 5.18M | } |
195 | | |
196 | 190k | void LiveIntervals::computeVirtRegs() { |
197 | 7.61M | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
198 | 7.42M | Register Reg = Register::index2VirtReg(i); |
199 | 7.42M | if (MRI->reg_nodbg_empty(Reg)) |
200 | 2.61M | continue; |
201 | 4.81M | LiveInterval &LI = createEmptyInterval(Reg); |
202 | 4.81M | bool NeedSplit = computeVirtRegInterval(LI); |
203 | 4.81M | if (NeedSplit) { |
204 | 57.1k | SmallVector<LiveInterval*, 8> SplitLIs; |
205 | 57.1k | splitSeparateComponents(LI, SplitLIs); |
206 | 57.1k | } |
207 | 4.81M | } |
208 | 190k | } |
209 | | |
210 | 190k | void LiveIntervals::computeRegMasks() { |
211 | 190k | RegMaskBlocks.resize(MF->getNumBlockIDs()); |
212 | | |
213 | | // Find all instructions with regmask operands. |
214 | 348k | for (const MachineBasicBlock &MBB : *MF) { |
215 | 348k | std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB.getNumber()]; |
216 | 348k | RMB.first = RegMaskSlots.size(); |
217 | | |
218 | | // Some block starts, such as EH funclets, create masks. |
219 | 348k | if (const uint32_t *Mask = MBB.getBeginClobberMask(TRI)) { |
220 | 32 | RegMaskSlots.push_back(Indexes->getMBBStartIdx(&MBB)); |
221 | 32 | RegMaskBits.push_back(Mask); |
222 | 32 | } |
223 | | |
224 | | // Unwinders may clobber additional registers. |
225 | | // FIXME: This functionality can possibly be merged into |
226 | | // MachineBasicBlock::getBeginClobberMask(). |
227 | 348k | if (MBB.isEHPad()) |
228 | 375 | if (auto *Mask = TRI->getCustomEHPadPreservedMask(*MBB.getParent())) { |
229 | 0 | RegMaskSlots.push_back(Indexes->getMBBStartIdx(&MBB)); |
230 | 0 | RegMaskBits.push_back(Mask); |
231 | 0 | } |
232 | | |
233 | 7.18M | for (const MachineInstr &MI : MBB) { |
234 | 22.7M | for (const MachineOperand &MO : MI.operands()) { |
235 | 22.7M | if (!MO.isRegMask()) |
236 | 22.5M | continue; |
237 | 119k | RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot()); |
238 | 119k | RegMaskBits.push_back(MO.getRegMask()); |
239 | 119k | } |
240 | 7.18M | } |
241 | | |
242 | | // Some block ends, such as funclet returns, create masks. Put the mask on |
243 | | // the last instruction of the block, because MBB slot index intervals are |
244 | | // half-open. |
245 | 348k | if (const uint32_t *Mask = MBB.getEndClobberMask(TRI)) { |
246 | 17 | assert(!MBB.empty() && "empty return block?"); |
247 | 0 | RegMaskSlots.push_back( |
248 | 17 | Indexes->getInstructionIndex(MBB.back()).getRegSlot()); |
249 | 17 | RegMaskBits.push_back(Mask); |
250 | 17 | } |
251 | | |
252 | | // Compute the number of register mask instructions in this block. |
253 | 0 | RMB.second = RegMaskSlots.size() - RMB.first; |
254 | 348k | } |
255 | 190k | } |
256 | | |
257 | | //===----------------------------------------------------------------------===// |
258 | | // Register Unit Liveness |
259 | | //===----------------------------------------------------------------------===// |
260 | | // |
261 | | // Fixed interference typically comes from ABI boundaries: Function arguments |
262 | | // and return values are passed in fixed registers, and so are exception |
263 | | // pointers entering landing pads. Certain instructions require values to be |
264 | | // present in specific registers. That is also represented through fixed |
265 | | // interference. |
266 | | // |
267 | | |
268 | | /// Compute the live range of a register unit, based on the uses and defs of |
269 | | /// aliasing registers. The range should be empty, or contain only dead |
270 | | /// phi-defs from ABI blocks. |
271 | 952k | void LiveIntervals::computeRegUnitRange(LiveRange &LR, unsigned Unit) { |
272 | 952k | assert(LICalc && "LICalc not initialized."); |
273 | 0 | LICalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
274 | | |
275 | | // The physregs aliasing Unit are the roots and their super-registers. |
276 | | // Create all values as dead defs before extending to uses. Note that roots |
277 | | // may share super-registers. That's OK because createDeadDefs() is |
278 | | // idempotent. It is very rare for a register unit to have multiple roots, so |
279 | | // uniquing super-registers is probably not worthwhile. |
280 | 952k | bool IsReserved = false; |
281 | 1.99M | for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { |
282 | 1.04M | bool IsRootReserved = true; |
283 | 4.51M | for (MCPhysReg Reg : TRI->superregs_inclusive(*Root)) { |
284 | 4.51M | if (!MRI->reg_empty(Reg)) |
285 | 332k | LICalc->createDeadDefs(LR, Reg); |
286 | | // A register unit is considered reserved if all its roots and all their |
287 | | // super registers are reserved. |
288 | 4.51M | if (!MRI->isReserved(Reg)) |
289 | 4.39M | IsRootReserved = false; |
290 | 4.51M | } |
291 | 1.04M | IsReserved |= IsRootReserved; |
292 | 1.04M | } |
293 | 952k | assert(IsReserved == MRI->isReservedRegUnit(Unit) && |
294 | 952k | "reserved computation mismatch"); |
295 | | |
296 | | // Now extend LR to reach all uses. |
297 | | // Ignore uses of reserved registers. We only track defs of those. |
298 | 952k | if (!IsReserved) { |
299 | 1.89M | for (MCRegUnitRootIterator Root(Unit, TRI); Root.isValid(); ++Root) { |
300 | 4.40M | for (MCPhysReg Reg : TRI->superregs_inclusive(*Root)) { |
301 | 4.40M | if (!MRI->reg_empty(Reg)) |
302 | 321k | LICalc->extendToUses(LR, Reg); |
303 | 4.40M | } |
304 | 976k | } |
305 | 916k | } |
306 | | |
307 | | // Flush the segment set to the segment vector. |
308 | 952k | if (UseSegmentSetForPhysRegs) |
309 | 952k | LR.flushSegmentSet(); |
310 | 952k | } |
311 | | |
312 | | /// Precompute the live ranges of any register units that are live-in to an ABI |
313 | | /// block somewhere. Register values can appear without a corresponding def when |
314 | | /// entering the entry block or a landing pad. |
315 | 190k | void LiveIntervals::computeLiveInRegUnits() { |
316 | 190k | RegUnitRanges.resize(TRI->getNumRegUnits()); |
317 | 190k | LLVM_DEBUG(dbgs() << "Computing live-in reg-units in ABI blocks.\n"); |
318 | | |
319 | | // Keep track of the live range sets allocated. |
320 | 190k | SmallVector<unsigned, 8> NewRanges; |
321 | | |
322 | | // Check all basic blocks for live-ins. |
323 | 348k | for (const MachineBasicBlock &MBB : *MF) { |
324 | | // We only care about ABI blocks: Entry + landing pads. |
325 | 348k | if ((&MBB != &MF->front() && !MBB.isEHPad()) || MBB.livein_empty()) |
326 | 244k | continue; |
327 | | |
328 | | // Create phi-defs at Begin for all live-in registers. |
329 | 103k | SlotIndex Begin = Indexes->getMBBStartIdx(&MBB); |
330 | 103k | LLVM_DEBUG(dbgs() << Begin << "\t" << printMBBReference(MBB)); |
331 | 184k | for (const auto &LI : MBB.liveins()) { |
332 | 217k | for (MCRegUnit Unit : TRI->regunits(LI.PhysReg)) { |
333 | 217k | LiveRange *LR = RegUnitRanges[Unit]; |
334 | 217k | if (!LR) { |
335 | | // Use segment set to speed-up initial computation of the live range. |
336 | 217k | LR = RegUnitRanges[Unit] = new LiveRange(UseSegmentSetForPhysRegs); |
337 | 217k | NewRanges.push_back(Unit); |
338 | 217k | } |
339 | 217k | VNInfo *VNI = LR->createDeadDef(Begin, getVNInfoAllocator()); |
340 | 217k | (void)VNI; |
341 | 217k | LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI) << '#' << VNI->id); |
342 | 217k | } |
343 | 184k | } |
344 | 103k | LLVM_DEBUG(dbgs() << '\n'); |
345 | 103k | } |
346 | 190k | LLVM_DEBUG(dbgs() << "Created " << NewRanges.size() << " new intervals.\n"); |
347 | | |
348 | | // Compute the 'normal' part of the ranges. |
349 | 190k | for (unsigned Unit : NewRanges) |
350 | 217k | computeRegUnitRange(*RegUnitRanges[Unit], Unit); |
351 | 190k | } |
352 | | |
353 | | static void createSegmentsForValues(LiveRange &LR, |
354 | 103k | iterator_range<LiveInterval::vni_iterator> VNIs) { |
355 | 128k | for (VNInfo *VNI : VNIs) { |
356 | 128k | if (VNI->isUnused()) |
357 | 244 | continue; |
358 | 128k | SlotIndex Def = VNI->def; |
359 | 128k | LR.addSegment(LiveRange::Segment(Def, Def.getDeadSlot(), VNI)); |
360 | 128k | } |
361 | 103k | } |
362 | | |
363 | | void LiveIntervals::extendSegmentsToUses(LiveRange &Segments, |
364 | | ShrinkToUsesWorkList &WorkList, |
365 | 103k | Register Reg, LaneBitmask LaneMask) { |
366 | | // Keep track of the PHIs that are in use. |
367 | 103k | SmallPtrSet<VNInfo*, 8> UsedPHIs; |
368 | | // Blocks that have already been added to WorkList as live-out. |
369 | 103k | SmallPtrSet<const MachineBasicBlock*, 16> LiveOut; |
370 | | |
371 | 103k | auto getSubRange = [](const LiveInterval &I, LaneBitmask M) |
372 | 103k | -> const LiveRange& { |
373 | 103k | if (M.none()) |
374 | 101k | return I; |
375 | 2.07k | for (const LiveInterval::SubRange &SR : I.subranges()) { |
376 | 2.07k | if ((SR.LaneMask & M).any()) { |
377 | 1.41k | assert(SR.LaneMask == M && "Expecting lane masks to match exactly"); |
378 | 0 | return SR; |
379 | 1.41k | } |
380 | 2.07k | } |
381 | 0 | llvm_unreachable("Subrange for mask not found"); |
382 | 0 | }; |
383 | | |
384 | 103k | const LiveInterval &LI = getInterval(Reg); |
385 | 103k | const LiveRange &OldRange = getSubRange(LI, LaneMask); |
386 | | |
387 | | // Extend intervals to reach all uses in WorkList. |
388 | 682k | while (!WorkList.empty()) { |
389 | 579k | SlotIndex Idx = WorkList.back().first; |
390 | 579k | VNInfo *VNI = WorkList.back().second; |
391 | 579k | WorkList.pop_back(); |
392 | 579k | const MachineBasicBlock *MBB = Indexes->getMBBFromIndex(Idx.getPrevSlot()); |
393 | 579k | SlotIndex BlockStart = Indexes->getMBBStartIdx(MBB); |
394 | | |
395 | | // Extend the live range for VNI to be live at Idx. |
396 | 579k | if (VNInfo *ExtVNI = Segments.extendInBlock(BlockStart, Idx)) { |
397 | 461k | assert(ExtVNI == VNI && "Unexpected existing value number"); |
398 | 0 | (void)ExtVNI; |
399 | | // Is this a PHIDef we haven't seen before? |
400 | 461k | if (!VNI->isPHIDef() || VNI->def != BlockStart || |
401 | 461k | !UsedPHIs.insert(VNI).second) |
402 | 451k | continue; |
403 | | // The PHI is live, make sure the predecessors are live-out. |
404 | 23.4k | for (const MachineBasicBlock *Pred : MBB->predecessors()) { |
405 | 23.4k | if (!LiveOut.insert(Pred).second) |
406 | 3.35k | continue; |
407 | 20.0k | SlotIndex Stop = Indexes->getMBBEndIdx(Pred); |
408 | | // A predecessor is not required to have a live-out value for a PHI. |
409 | 20.0k | if (VNInfo *PVNI = OldRange.getVNInfoBefore(Stop)) |
410 | 20.0k | WorkList.push_back(std::make_pair(Stop, PVNI)); |
411 | 20.0k | } |
412 | 10.3k | continue; |
413 | 461k | } |
414 | | |
415 | | // VNI is live-in to MBB. |
416 | 117k | LLVM_DEBUG(dbgs() << " live-in at " << BlockStart << '\n'); |
417 | 117k | Segments.addSegment(LiveRange::Segment(BlockStart, Idx, VNI)); |
418 | | |
419 | | // Make sure VNI is live-out from the predecessors. |
420 | 189k | for (const MachineBasicBlock *Pred : MBB->predecessors()) { |
421 | 189k | if (!LiveOut.insert(Pred).second) |
422 | 69.8k | continue; |
423 | 120k | SlotIndex Stop = Indexes->getMBBEndIdx(Pred); |
424 | 120k | if (VNInfo *OldVNI = OldRange.getVNInfoBefore(Stop)) { |
425 | 120k | assert(OldVNI == VNI && "Wrong value out of predecessor"); |
426 | 0 | (void)OldVNI; |
427 | 120k | WorkList.push_back(std::make_pair(Stop, VNI)); |
428 | 120k | } else { |
429 | 0 | #ifndef NDEBUG |
430 | | // There was no old VNI. Verify that Stop is jointly dominated |
431 | | // by <undef>s for this live range. |
432 | 0 | assert(LaneMask.any() && |
433 | 0 | "Missing value out of predecessor for main range"); |
434 | 0 | SmallVector<SlotIndex,8> Undefs; |
435 | 0 | LI.computeSubRangeUndefs(Undefs, LaneMask, *MRI, *Indexes); |
436 | 0 | assert(LiveRangeCalc::isJointlyDominated(Pred, Undefs, *Indexes) && |
437 | 0 | "Missing value out of predecessor for subrange"); |
438 | 0 | #endif |
439 | 0 | } |
440 | 120k | } |
441 | 117k | } |
442 | 103k | } |
443 | | |
444 | | bool LiveIntervals::shrinkToUses(LiveInterval *li, |
445 | 101k | SmallVectorImpl<MachineInstr*> *dead) { |
446 | 101k | LLVM_DEBUG(dbgs() << "Shrink: " << *li << '\n'); |
447 | 101k | assert(li->reg().isVirtual() && "Can only shrink virtual registers"); |
448 | | |
449 | | // Shrink subregister live ranges. |
450 | 0 | bool NeedsCleanup = false; |
451 | 101k | for (LiveInterval::SubRange &S : li->subranges()) { |
452 | 1.26k | shrinkToUses(S, li->reg()); |
453 | 1.26k | if (S.empty()) |
454 | 0 | NeedsCleanup = true; |
455 | 1.26k | } |
456 | 101k | if (NeedsCleanup) |
457 | 0 | li->removeEmptySubRanges(); |
458 | | |
459 | | // Find all the values used, including PHI kills. |
460 | 101k | ShrinkToUsesWorkList WorkList; |
461 | | |
462 | | // Visit all instructions reading li->reg(). |
463 | 101k | Register Reg = li->reg(); |
464 | 547k | for (MachineInstr &UseMI : MRI->reg_instructions(Reg)) { |
465 | 547k | if (UseMI.isDebugInstr() || !UseMI.readsVirtualRegister(Reg)) |
466 | 111k | continue; |
467 | 436k | SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot(); |
468 | 436k | LiveQueryResult LRQ = li->Query(Idx); |
469 | 436k | VNInfo *VNI = LRQ.valueIn(); |
470 | 436k | if (!VNI) { |
471 | | // This shouldn't happen: readsVirtualRegister returns true, but there is |
472 | | // no live value. It is likely caused by a target getting <undef> flags |
473 | | // wrong. |
474 | 0 | LLVM_DEBUG( |
475 | 0 | dbgs() << Idx << '\t' << UseMI |
476 | 0 | << "Warning: Instr claims to read non-existent value in " |
477 | 0 | << *li << '\n'); |
478 | 0 | continue; |
479 | 0 | } |
480 | | // Special case: An early-clobber tied operand reads and writes the |
481 | | // register one slot early. |
482 | 436k | if (VNInfo *DefVNI = LRQ.valueDefined()) |
483 | 8.28k | Idx = DefVNI->def; |
484 | | |
485 | 436k | WorkList.push_back(std::make_pair(Idx, VNI)); |
486 | 436k | } |
487 | | |
488 | | // Create new live ranges with only minimal live segments per def. |
489 | 101k | LiveRange NewLR; |
490 | 101k | createSegmentsForValues(NewLR, li->vnis()); |
491 | 101k | extendSegmentsToUses(NewLR, WorkList, Reg, LaneBitmask::getNone()); |
492 | | |
493 | | // Move the trimmed segments back. |
494 | 101k | li->segments.swap(NewLR.segments); |
495 | | |
496 | | // Handle dead values. |
497 | 101k | bool CanSeparate = computeDeadValues(*li, dead); |
498 | 101k | LLVM_DEBUG(dbgs() << "Shrunk: " << *li << '\n'); |
499 | 101k | return CanSeparate; |
500 | 101k | } |
501 | | |
502 | | bool LiveIntervals::computeDeadValues(LiveInterval &LI, |
503 | 5.28M | SmallVectorImpl<MachineInstr*> *dead) { |
504 | 5.28M | bool MayHaveSplitComponents = false; |
505 | | |
506 | 5.45M | for (VNInfo *VNI : LI.valnos) { |
507 | 5.45M | if (VNI->isUnused()) |
508 | 244 | continue; |
509 | 5.45M | SlotIndex Def = VNI->def; |
510 | 5.45M | LiveRange::iterator I = LI.FindSegmentContaining(Def); |
511 | 5.45M | assert(I != LI.end() && "Missing segment for VNI"); |
512 | | |
513 | | // Is the register live before? Otherwise we may have to add a read-undef |
514 | | // flag for subregister defs. |
515 | 0 | Register VReg = LI.reg(); |
516 | 5.45M | if (MRI->shouldTrackSubRegLiveness(VReg)) { |
517 | 270k | if ((I == LI.begin() || std::prev(I)->end < Def) && !VNI->isPHIDef()) { |
518 | 257k | MachineInstr *MI = getInstructionFromIndex(Def); |
519 | 257k | MI->setRegisterDefReadUndef(VReg); |
520 | 257k | } |
521 | 270k | } |
522 | | |
523 | 5.45M | if (I->end != Def.getDeadSlot()) |
524 | 5.34M | continue; |
525 | 116k | if (VNI->isPHIDef()) { |
526 | | // This is a dead PHI. Remove it. |
527 | 501 | VNI->markUnused(); |
528 | 501 | LI.removeSegment(I); |
529 | 501 | LLVM_DEBUG(dbgs() << "Dead PHI at " << Def << " may separate interval\n"); |
530 | 116k | } else { |
531 | | // This is a dead def. Make sure the instruction knows. |
532 | 116k | MachineInstr *MI = getInstructionFromIndex(Def); |
533 | 116k | assert(MI && "No instruction defining live value"); |
534 | 0 | MI->addRegisterDead(LI.reg(), TRI); |
535 | | |
536 | 116k | if (dead && MI->allDefsAreDead()) { |
537 | 16.3k | LLVM_DEBUG(dbgs() << "All defs dead: " << Def << '\t' << *MI); |
538 | 16.3k | dead->push_back(MI); |
539 | 16.3k | } |
540 | 116k | } |
541 | 0 | MayHaveSplitComponents = true; |
542 | 116k | } |
543 | 5.28M | return MayHaveSplitComponents; |
544 | 5.28M | } |
545 | | |
546 | 1.41k | void LiveIntervals::shrinkToUses(LiveInterval::SubRange &SR, Register Reg) { |
547 | 1.41k | LLVM_DEBUG(dbgs() << "Shrink: " << SR << '\n'); |
548 | 1.41k | assert(Reg.isVirtual() && "Can only shrink virtual registers"); |
549 | | // Find all the values used, including PHI kills. |
550 | 0 | ShrinkToUsesWorkList WorkList; |
551 | | |
552 | | // Visit all instructions reading Reg. |
553 | 1.41k | SlotIndex LastIdx; |
554 | 2.76k | for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { |
555 | | // Skip "undef" uses. |
556 | 2.76k | if (!MO.readsReg()) |
557 | 0 | continue; |
558 | | // Maybe the operand is for a subregister we don't care about. |
559 | 2.76k | unsigned SubReg = MO.getSubReg(); |
560 | 2.76k | if (SubReg != 0) { |
561 | 1.02k | LaneBitmask LaneMask = TRI->getSubRegIndexLaneMask(SubReg); |
562 | 1.02k | if ((LaneMask & SR.LaneMask).none()) |
563 | 461 | continue; |
564 | 1.02k | } |
565 | | // We only need to visit each instruction once. |
566 | 2.30k | MachineInstr *UseMI = MO.getParent(); |
567 | 2.30k | SlotIndex Idx = getInstructionIndex(*UseMI).getRegSlot(); |
568 | 2.30k | if (Idx == LastIdx) |
569 | 9 | continue; |
570 | 2.29k | LastIdx = Idx; |
571 | | |
572 | 2.29k | LiveQueryResult LRQ = SR.Query(Idx); |
573 | 2.29k | VNInfo *VNI = LRQ.valueIn(); |
574 | | // For Subranges it is possible that only undef values are left in that |
575 | | // part of the subregister, so there is no real liverange at the use |
576 | 2.29k | if (!VNI) |
577 | 61 | continue; |
578 | | |
579 | | // Special case: An early-clobber tied operand reads and writes the |
580 | | // register one slot early. |
581 | 2.23k | if (VNInfo *DefVNI = LRQ.valueDefined()) |
582 | 92 | Idx = DefVNI->def; |
583 | | |
584 | 2.23k | WorkList.push_back(std::make_pair(Idx, VNI)); |
585 | 2.23k | } |
586 | | |
587 | | // Create a new live ranges with only minimal live segments per def. |
588 | 1.41k | LiveRange NewLR; |
589 | 1.41k | createSegmentsForValues(NewLR, SR.vnis()); |
590 | 1.41k | extendSegmentsToUses(NewLR, WorkList, Reg, SR.LaneMask); |
591 | | |
592 | | // Move the trimmed ranges back. |
593 | 1.41k | SR.segments.swap(NewLR.segments); |
594 | | |
595 | | // Remove dead PHI value numbers |
596 | 1.61k | for (VNInfo *VNI : SR.valnos) { |
597 | 1.61k | if (VNI->isUnused()) |
598 | 0 | continue; |
599 | 1.61k | const LiveRange::Segment *Segment = SR.getSegmentContaining(VNI->def); |
600 | 1.61k | assert(Segment != nullptr && "Missing segment for VNI"); |
601 | 1.61k | if (Segment->end != VNI->def.getDeadSlot()) |
602 | 1.49k | continue; |
603 | 118 | if (VNI->isPHIDef()) { |
604 | | // This is a dead PHI. Remove it. |
605 | 4 | LLVM_DEBUG(dbgs() << "Dead PHI at " << VNI->def |
606 | 4 | << " may separate interval\n"); |
607 | 4 | VNI->markUnused(); |
608 | 4 | SR.removeSegment(*Segment); |
609 | 4 | } |
610 | 118 | } |
611 | | |
612 | 1.41k | LLVM_DEBUG(dbgs() << "Shrunk: " << SR << '\n'); |
613 | 1.41k | } |
614 | | |
615 | | void LiveIntervals::extendToIndices(LiveRange &LR, |
616 | | ArrayRef<SlotIndex> Indices, |
617 | 12.0k | ArrayRef<SlotIndex> Undefs) { |
618 | 12.0k | assert(LICalc && "LICalc not initialized."); |
619 | 0 | LICalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
620 | 12.0k | for (SlotIndex Idx : Indices) |
621 | 37.1k | LICalc->extend(LR, Idx, /*PhysReg=*/0, Undefs); |
622 | 12.0k | } |
623 | | |
624 | | void LiveIntervals::pruneValue(LiveRange &LR, SlotIndex Kill, |
625 | 23.5k | SmallVectorImpl<SlotIndex> *EndPoints) { |
626 | 23.5k | LiveQueryResult LRQ = LR.Query(Kill); |
627 | 23.5k | VNInfo *VNI = LRQ.valueOutOrDead(); |
628 | 23.5k | if (!VNI) |
629 | 55 | return; |
630 | | |
631 | 23.4k | MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill); |
632 | 23.4k | SlotIndex MBBEnd = Indexes->getMBBEndIdx(KillMBB); |
633 | | |
634 | | // If VNI isn't live out from KillMBB, the value is trivially pruned. |
635 | 23.4k | if (LRQ.endPoint() < MBBEnd) { |
636 | 20.7k | LR.removeSegment(Kill, LRQ.endPoint()); |
637 | 20.7k | if (EndPoints) EndPoints->push_back(LRQ.endPoint()); |
638 | 20.7k | return; |
639 | 20.7k | } |
640 | | |
641 | | // VNI is live out of KillMBB. |
642 | 2.69k | LR.removeSegment(Kill, MBBEnd); |
643 | 2.69k | if (EndPoints) EndPoints->push_back(MBBEnd); |
644 | | |
645 | | // Find all blocks that are reachable from KillMBB without leaving VNI's live |
646 | | // range. It is possible that KillMBB itself is reachable, so start a DFS |
647 | | // from each successor. |
648 | 2.69k | using VisitedTy = df_iterator_default_set<MachineBasicBlock*,9>; |
649 | 2.69k | VisitedTy Visited; |
650 | 4.08k | for (MachineBasicBlock *Succ : KillMBB->successors()) { |
651 | 4.08k | for (df_ext_iterator<MachineBasicBlock*, VisitedTy> |
652 | 4.08k | I = df_ext_begin(Succ, Visited), E = df_ext_end(Succ, Visited); |
653 | 9.90k | I != E;) { |
654 | 5.82k | MachineBasicBlock *MBB = *I; |
655 | | |
656 | | // Check if VNI is live in to MBB. |
657 | 5.82k | SlotIndex MBBStart, MBBEnd; |
658 | 5.82k | std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB); |
659 | 5.82k | LiveQueryResult LRQ = LR.Query(MBBStart); |
660 | 5.82k | if (LRQ.valueIn() != VNI) { |
661 | | // This block isn't part of the VNI segment. Prune the search. |
662 | 3.53k | I.skipChildren(); |
663 | 3.53k | continue; |
664 | 3.53k | } |
665 | | |
666 | | // Prune the search if VNI is killed in MBB. |
667 | 2.29k | if (LRQ.endPoint() < MBBEnd) { |
668 | 475 | LR.removeSegment(MBBStart, LRQ.endPoint()); |
669 | 475 | if (EndPoints) EndPoints->push_back(LRQ.endPoint()); |
670 | 475 | I.skipChildren(); |
671 | 475 | continue; |
672 | 475 | } |
673 | | |
674 | | // VNI is live through MBB. |
675 | 1.81k | LR.removeSegment(MBBStart, MBBEnd); |
676 | 1.81k | if (EndPoints) EndPoints->push_back(MBBEnd); |
677 | 1.81k | ++I; |
678 | 1.81k | } |
679 | 4.08k | } |
680 | 2.69k | } |
681 | | |
682 | | //===----------------------------------------------------------------------===// |
683 | | // Register allocator hooks. |
684 | | // |
685 | | |
686 | 112k | void LiveIntervals::addKillFlags(const VirtRegMap *VRM) { |
687 | | // Keep track of regunit ranges. |
688 | 112k | SmallVector<std::pair<const LiveRange*, LiveRange::const_iterator>, 8> RU; |
689 | | |
690 | 4.74M | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
691 | 4.63M | Register Reg = Register::index2VirtReg(i); |
692 | 4.63M | if (MRI->reg_nodbg_empty(Reg)) |
693 | 1.91M | continue; |
694 | 2.72M | const LiveInterval &LI = getInterval(Reg); |
695 | 2.72M | if (LI.empty()) |
696 | 110k | continue; |
697 | | |
698 | | // Target may have not allocated this yet. |
699 | 2.61M | Register PhysReg = VRM->getPhys(Reg); |
700 | 2.61M | if (!PhysReg) |
701 | 387k | continue; |
702 | | |
703 | | // Find the regunit intervals for the assigned register. They may overlap |
704 | | // the virtual register live range, cancelling any kills. |
705 | 2.22M | RU.clear(); |
706 | 2.51M | for (MCRegUnit Unit : TRI->regunits(PhysReg)) { |
707 | 2.51M | const LiveRange &RURange = getRegUnit(Unit); |
708 | 2.51M | if (RURange.empty()) |
709 | 1.57M | continue; |
710 | 940k | RU.push_back(std::make_pair(&RURange, RURange.find(LI.begin()->end))); |
711 | 940k | } |
712 | | // Every instruction that kills Reg corresponds to a segment range end |
713 | | // point. |
714 | 4.70M | for (LiveInterval::const_iterator RI = LI.begin(), RE = LI.end(); RI != RE; |
715 | 2.48M | ++RI) { |
716 | | // A block index indicates an MBB edge. |
717 | 2.48M | if (RI->end.isBlock()) |
718 | 143k | continue; |
719 | 2.33M | MachineInstr *MI = getInstructionFromIndex(RI->end); |
720 | 2.33M | if (!MI) |
721 | 0 | continue; |
722 | | |
723 | | // Check if any of the regunits are live beyond the end of RI. That could |
724 | | // happen when a physreg is defined as a copy of a virtreg: |
725 | | // |
726 | | // %eax = COPY %5 |
727 | | // FOO %5 <--- MI, cancel kill because %eax is live. |
728 | | // BAR killed %eax |
729 | | // |
730 | | // There should be no kill flag on FOO when %5 is rewritten as %eax. |
731 | 2.33M | for (auto &RUP : RU) { |
732 | 1.01M | const LiveRange &RURange = *RUP.first; |
733 | 1.01M | LiveRange::const_iterator &I = RUP.second; |
734 | 1.01M | if (I == RURange.end()) |
735 | 329k | continue; |
736 | 684k | I = RURange.advanceTo(I, RI->end); |
737 | 684k | if (I == RURange.end() || I->start >= RI->end) |
738 | 680k | continue; |
739 | | // I is overlapping RI. |
740 | 3.69k | goto CancelKill; |
741 | 684k | } |
742 | | |
743 | 2.33M | if (MRI->subRegLivenessEnabled()) { |
744 | | // When reading a partial undefined value we must not add a kill flag. |
745 | | // The regalloc might have used the undef lane for something else. |
746 | | // Example: |
747 | | // %1 = ... ; R32: %1 |
748 | | // %2:high16 = ... ; R64: %2 |
749 | | // = read killed %2 ; R64: %2 |
750 | | // = read %1 ; R32: %1 |
751 | | // The <kill> flag is correct for %2, but the register allocator may |
752 | | // assign R0L to %1, and R0 to %2 because the low 32bits of R0 |
753 | | // are actually never written by %2. After assignment the <kill> |
754 | | // flag at the read instruction is invalid. |
755 | 1.65M | LaneBitmask DefinedLanesMask; |
756 | 1.65M | if (LI.hasSubRanges()) { |
757 | | // Compute a mask of lanes that are defined. |
758 | 59.5k | DefinedLanesMask = LaneBitmask::getNone(); |
759 | 59.5k | for (const LiveInterval::SubRange &SR : LI.subranges()) |
760 | 133k | for (const LiveRange::Segment &Segment : SR.segments) { |
761 | 133k | if (Segment.start >= RI->end) |
762 | 9.02k | break; |
763 | 124k | if (Segment.end == RI->end) { |
764 | 61.8k | DefinedLanesMask |= SR.LaneMask; |
765 | 61.8k | break; |
766 | 61.8k | } |
767 | 124k | } |
768 | 59.5k | } else |
769 | 1.59M | DefinedLanesMask = LaneBitmask::getAll(); |
770 | | |
771 | 1.65M | bool IsFullWrite = false; |
772 | 5.39M | for (const MachineOperand &MO : MI->operands()) { |
773 | 5.39M | if (!MO.isReg() || MO.getReg() != Reg) |
774 | 3.64M | continue; |
775 | 1.74M | if (MO.isUse()) { |
776 | | // Reading any undefined lanes? |
777 | 1.64M | unsigned SubReg = MO.getSubReg(); |
778 | 1.64M | LaneBitmask UseMask = SubReg ? TRI->getSubRegIndexLaneMask(SubReg) |
779 | 1.64M | : MRI->getMaxLaneMaskForVReg(Reg); |
780 | 1.64M | if ((UseMask & ~DefinedLanesMask).any()) |
781 | 542 | goto CancelKill; |
782 | 1.64M | } else if (MO.getSubReg() == 0) { |
783 | | // Writing to the full register? |
784 | 90.2k | assert(MO.isDef()); |
785 | 0 | IsFullWrite = true; |
786 | 90.2k | } |
787 | 1.74M | } |
788 | | |
789 | | // If an instruction writes to a subregister, a new segment starts in |
790 | | // the LiveInterval. But as this is only overriding part of the register |
791 | | // adding kill-flags is not correct here after registers have been |
792 | | // assigned. |
793 | 1.65M | if (!IsFullWrite) { |
794 | | // Next segment has to be adjacent in the subregister write case. |
795 | 1.56M | LiveRange::const_iterator N = std::next(RI); |
796 | 1.56M | if (N != LI.end() && N->start == RI->end) |
797 | 12.6k | goto CancelKill; |
798 | 1.56M | } |
799 | 1.65M | } |
800 | | |
801 | 2.32M | MI->addRegisterKilled(Reg, nullptr); |
802 | 2.32M | continue; |
803 | 16.8k | CancelKill: |
804 | 16.8k | MI->clearRegisterKills(Reg, nullptr); |
805 | 16.8k | } |
806 | 2.22M | } |
807 | 112k | } |
808 | | |
809 | | MachineBasicBlock* |
810 | 23.5M | LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const { |
811 | 23.5M | assert(!LI.empty() && "LiveInterval is empty."); |
812 | | |
813 | | // A local live range must be fully contained inside the block, meaning it is |
814 | | // defined and killed at instructions, not at block boundaries. It is not |
815 | | // live in or out of any block. |
816 | | // |
817 | | // It is technically possible to have a PHI-defined live range identical to a |
818 | | // single block, but we are going to return false in that case. |
819 | | |
820 | 0 | SlotIndex Start = LI.beginIndex(); |
821 | 23.5M | if (Start.isBlock()) |
822 | 694k | return nullptr; |
823 | | |
824 | 22.8M | SlotIndex Stop = LI.endIndex(); |
825 | 22.8M | if (Stop.isBlock()) |
826 | 926k | return nullptr; |
827 | | |
828 | | // getMBBFromIndex doesn't need to search the MBB table when both indexes |
829 | | // belong to proper instructions. |
830 | 21.8M | MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start); |
831 | 21.8M | MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop); |
832 | 21.8M | return MBB1 == MBB2 ? MBB1 : nullptr; |
833 | 22.8M | } |
834 | | |
835 | | bool |
836 | 27 | LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const { |
837 | 69 | for (const VNInfo *PHI : LI.valnos) { |
838 | 69 | if (PHI->isUnused() || !PHI->isPHIDef()) |
839 | 66 | continue; |
840 | 3 | const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def); |
841 | | // Conservatively return true instead of scanning huge predecessor lists. |
842 | 3 | if (PHIMBB->pred_size() > 100) |
843 | 0 | return true; |
844 | 3 | for (const MachineBasicBlock *Pred : PHIMBB->predecessors()) |
845 | 4 | if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(Pred))) |
846 | 3 | return true; |
847 | 3 | } |
848 | 24 | return false; |
849 | 27 | } |
850 | | |
851 | | float LiveIntervals::getSpillWeight(bool isDef, bool isUse, |
852 | | const MachineBlockFrequencyInfo *MBFI, |
853 | 7.30M | const MachineInstr &MI) { |
854 | 7.30M | return getSpillWeight(isDef, isUse, MBFI, MI.getParent()); |
855 | 7.30M | } |
856 | | |
857 | | float LiveIntervals::getSpillWeight(bool isDef, bool isUse, |
858 | | const MachineBlockFrequencyInfo *MBFI, |
859 | 7.30M | const MachineBasicBlock *MBB) { |
860 | 7.30M | return (isDef + isUse) * MBFI->getBlockFreqRelativeToEntryBlock(MBB); |
861 | 7.30M | } |
862 | | |
863 | | LiveRange::Segment |
864 | 0 | LiveIntervals::addSegmentToEndOfBlock(Register Reg, MachineInstr &startInst) { |
865 | 0 | LiveInterval &Interval = getOrCreateEmptyInterval(Reg); |
866 | 0 | VNInfo *VN = Interval.getNextValue( |
867 | 0 | SlotIndex(getInstructionIndex(startInst).getRegSlot()), |
868 | 0 | getVNInfoAllocator()); |
869 | 0 | LiveRange::Segment S(SlotIndex(getInstructionIndex(startInst).getRegSlot()), |
870 | 0 | getMBBEndIdx(startInst.getParent()), VN); |
871 | 0 | Interval.addSegment(S); |
872 | |
|
873 | 0 | return S; |
874 | 0 | } |
875 | | |
876 | | //===----------------------------------------------------------------------===// |
877 | | // Register mask functions |
878 | | //===----------------------------------------------------------------------===// |
879 | | /// Check whether use of reg in MI is live-through. Live-through means that |
880 | | /// the value is alive on exit from Machine instruction. The example of such |
881 | | /// use is a deopt value in statepoint instruction. |
882 | 70 | static bool hasLiveThroughUse(const MachineInstr *MI, Register Reg) { |
883 | 70 | if (MI->getOpcode() != TargetOpcode::STATEPOINT) |
884 | 69 | return false; |
885 | 1 | StatepointOpers SO(MI); |
886 | 1 | if (SO.getFlags() & (uint64_t)StatepointFlags::DeoptLiveIn) |
887 | 0 | return false; |
888 | 7 | for (unsigned Idx = SO.getNumDeoptArgsIdx(), E = SO.getNumGCPtrIdx(); Idx < E; |
889 | 6 | ++Idx) { |
890 | 6 | const MachineOperand &MO = MI->getOperand(Idx); |
891 | 6 | if (MO.isReg() && MO.getReg() == Reg) |
892 | 0 | return true; |
893 | 6 | } |
894 | 1 | return false; |
895 | 1 | } |
896 | | |
897 | | bool LiveIntervals::checkRegMaskInterference(const LiveInterval &LI, |
898 | 3.36M | BitVector &UsableRegs) { |
899 | 3.36M | if (LI.empty()) |
900 | 0 | return false; |
901 | 3.36M | LiveInterval::const_iterator LiveI = LI.begin(), LiveE = LI.end(); |
902 | | |
903 | | // Use a smaller arrays for local live ranges. |
904 | 3.36M | ArrayRef<SlotIndex> Slots; |
905 | 3.36M | ArrayRef<const uint32_t*> Bits; |
906 | 3.36M | if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) { |
907 | 2.99M | Slots = getRegMaskSlotsInBlock(MBB->getNumber()); |
908 | 2.99M | Bits = getRegMaskBitsInBlock(MBB->getNumber()); |
909 | 2.99M | } else { |
910 | 370k | Slots = getRegMaskSlots(); |
911 | 370k | Bits = getRegMaskBits(); |
912 | 370k | } |
913 | | |
914 | | // We are going to enumerate all the register mask slots contained in LI. |
915 | | // Start with a binary search of RegMaskSlots to find a starting point. |
916 | 3.36M | ArrayRef<SlotIndex>::iterator SlotI = llvm::lower_bound(Slots, LiveI->start); |
917 | 3.36M | ArrayRef<SlotIndex>::iterator SlotE = Slots.end(); |
918 | | |
919 | | // No slots in range, LI begins after the last call. |
920 | 3.36M | if (SlotI == SlotE) |
921 | 1.88M | return false; |
922 | | |
923 | 1.47M | bool Found = false; |
924 | | // Utility to union regmasks. |
925 | 3.73M | auto unionBitMask = [&](unsigned Idx) { |
926 | 3.73M | if (!Found) { |
927 | | // This is the first overlap. Initialize UsableRegs to all ones. |
928 | 881k | UsableRegs.clear(); |
929 | 881k | UsableRegs.resize(TRI->getNumRegs(), true); |
930 | 881k | Found = true; |
931 | 881k | } |
932 | | // Remove usable registers clobbered by this mask. |
933 | 3.73M | UsableRegs.clearBitsNotInMask(Bits[Idx]); |
934 | 3.73M | }; |
935 | 1.50M | while (true) { |
936 | 1.50M | assert(*SlotI >= LiveI->start); |
937 | | // Loop over all slots overlapping this segment. |
938 | 4.78M | while (*SlotI < LiveI->end) { |
939 | | // *SlotI overlaps LI. Collect mask bits. |
940 | 3.73M | unionBitMask(SlotI - Slots.begin()); |
941 | 3.73M | if (++SlotI == SlotE) |
942 | 457k | return Found; |
943 | 3.73M | } |
944 | | // If segment ends with live-through use we need to collect its regmask. |
945 | 1.05M | if (*SlotI == LiveI->end) |
946 | 70 | if (MachineInstr *MI = getInstructionFromIndex(*SlotI)) |
947 | 70 | if (hasLiveThroughUse(MI, LI.reg())) |
948 | 0 | unionBitMask(SlotI++ - Slots.begin()); |
949 | | // *SlotI is beyond the current LI segment. |
950 | | // Special advance implementation to not miss next LiveI->end. |
951 | 1.05M | if (++LiveI == LiveE || SlotI == SlotE || *SlotI > LI.endIndex()) |
952 | 1.01M | return Found; |
953 | 43.5k | while (LiveI->end < *SlotI) |
954 | 8.16k | ++LiveI; |
955 | | // Advance SlotI until it overlaps. |
956 | 51.3k | while (*SlotI < LiveI->start) |
957 | 20.3k | if (++SlotI == SlotE) |
958 | 4.39k | return Found; |
959 | 35.3k | } |
960 | 1.47M | } |
961 | | |
962 | | //===----------------------------------------------------------------------===// |
963 | | // IntervalUpdate class. |
964 | | //===----------------------------------------------------------------------===// |
965 | | |
966 | | /// Toolkit used by handleMove to trim or extend live intervals. |
967 | | class LiveIntervals::HMEditor { |
968 | | private: |
969 | | LiveIntervals& LIS; |
970 | | const MachineRegisterInfo& MRI; |
971 | | const TargetRegisterInfo& TRI; |
972 | | SlotIndex OldIdx; |
973 | | SlotIndex NewIdx; |
974 | | SmallPtrSet<LiveRange*, 8> Updated; |
975 | | bool UpdateFlags; |
976 | | |
977 | | public: |
978 | | HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI, |
979 | | const TargetRegisterInfo& TRI, |
980 | | SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags) |
981 | | : LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx), |
982 | 1.05M | UpdateFlags(UpdateFlags) {} |
983 | | |
984 | | // FIXME: UpdateFlags is a workaround that creates live intervals for all |
985 | | // physregs, even those that aren't needed for regalloc, in order to update |
986 | | // kill flags. This is wasteful. Eventually, LiveVariables will strip all kill |
987 | | // flags, and postRA passes will use a live register utility instead. |
988 | 409k | LiveRange *getRegUnitLI(unsigned Unit) { |
989 | 409k | if (UpdateFlags && !MRI.isReservedRegUnit(Unit)) |
990 | 207k | return &LIS.getRegUnit(Unit); |
991 | 202k | return LIS.getCachedRegUnit(Unit); |
992 | 409k | } |
993 | | |
994 | | /// Update all live ranges touched by MI, assuming a move from OldIdx to |
995 | | /// NewIdx. |
996 | 1.05M | void updateAllRanges(MachineInstr *MI) { |
997 | 1.05M | LLVM_DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " |
998 | 1.05M | << *MI); |
999 | 1.05M | bool hasRegMask = false; |
1000 | 3.42M | for (MachineOperand &MO : MI->operands()) { |
1001 | 3.42M | if (MO.isRegMask()) |
1002 | 0 | hasRegMask = true; |
1003 | 3.42M | if (!MO.isReg()) |
1004 | 950k | continue; |
1005 | 2.47M | if (MO.isUse()) { |
1006 | 1.55M | if (!MO.readsReg()) |
1007 | 30.0k | continue; |
1008 | | // Aggressively clear all kill flags. |
1009 | | // They are reinserted by VirtRegRewriter. |
1010 | 1.52M | MO.setIsKill(false); |
1011 | 1.52M | } |
1012 | | |
1013 | 2.44M | Register Reg = MO.getReg(); |
1014 | 2.44M | if (!Reg) |
1015 | 5.14k | continue; |
1016 | 2.44M | if (Reg.isVirtual()) { |
1017 | 2.10M | LiveInterval &LI = LIS.getInterval(Reg); |
1018 | 2.10M | if (LI.hasSubRanges()) { |
1019 | 87.6k | unsigned SubReg = MO.getSubReg(); |
1020 | 87.6k | LaneBitmask LaneMask = SubReg ? TRI.getSubRegIndexLaneMask(SubReg) |
1021 | 87.6k | : MRI.getMaxLaneMaskForVReg(Reg); |
1022 | 183k | for (LiveInterval::SubRange &S : LI.subranges()) { |
1023 | 183k | if ((S.LaneMask & LaneMask).none()) |
1024 | 60.0k | continue; |
1025 | 123k | updateRange(S, Reg, S.LaneMask); |
1026 | 123k | } |
1027 | 87.6k | } |
1028 | 2.10M | updateRange(LI, Reg, LaneBitmask::getNone()); |
1029 | | // If main range has a hole and we are moving a subrange use across |
1030 | | // the hole updateRange() cannot properly handle it since it only |
1031 | | // gets the LiveRange and not the whole LiveInterval. As a result |
1032 | | // we may end up with a main range not covering all subranges. |
1033 | | // This is extremely rare case, so let's check and reconstruct the |
1034 | | // main range. |
1035 | 2.10M | if (LI.hasSubRanges()) { |
1036 | 87.6k | unsigned SubReg = MO.getSubReg(); |
1037 | 87.6k | LaneBitmask LaneMask = SubReg ? TRI.getSubRegIndexLaneMask(SubReg) |
1038 | 87.6k | : MRI.getMaxLaneMaskForVReg(Reg); |
1039 | 183k | for (LiveInterval::SubRange &S : LI.subranges()) { |
1040 | 183k | if ((S.LaneMask & LaneMask).none() || LI.covers(S)) |
1041 | 183k | continue; |
1042 | 0 | LI.clear(); |
1043 | 0 | LIS.constructMainRangeFromSubranges(LI); |
1044 | 0 | break; |
1045 | 183k | } |
1046 | 87.6k | } |
1047 | | |
1048 | 2.10M | continue; |
1049 | 2.10M | } |
1050 | | |
1051 | | // For physregs, only update the regunits that actually have a |
1052 | | // precomputed live range. |
1053 | 335k | for (MCRegUnit Unit : TRI.regunits(Reg.asMCReg())) |
1054 | 409k | if (LiveRange *LR = getRegUnitLI(Unit)) |
1055 | 239k | updateRange(*LR, Unit, LaneBitmask::getNone()); |
1056 | 335k | } |
1057 | 1.05M | if (hasRegMask) |
1058 | 0 | updateRegMaskSlots(); |
1059 | 1.05M | } |
1060 | | |
1061 | | private: |
1062 | | /// Update a single live range, assuming an instruction has been moved from |
1063 | | /// OldIdx to NewIdx. |
1064 | 2.46M | void updateRange(LiveRange &LR, Register Reg, LaneBitmask LaneMask) { |
1065 | 2.46M | if (!Updated.insert(&LR).second) |
1066 | 95.8k | return; |
1067 | 2.37M | LLVM_DEBUG({ |
1068 | 2.37M | dbgs() << " "; |
1069 | 2.37M | if (Reg.isVirtual()) { |
1070 | 2.37M | dbgs() << printReg(Reg); |
1071 | 2.37M | if (LaneMask.any()) |
1072 | 2.37M | dbgs() << " L" << PrintLaneMask(LaneMask); |
1073 | 2.37M | } else { |
1074 | 2.37M | dbgs() << printRegUnit(Reg, &TRI); |
1075 | 2.37M | } |
1076 | 2.37M | dbgs() << ":\t" << LR << '\n'; |
1077 | 2.37M | }); |
1078 | 2.37M | if (SlotIndex::isEarlierInstr(OldIdx, NewIdx)) |
1079 | 2.05M | handleMoveDown(LR); |
1080 | 323k | else |
1081 | 323k | handleMoveUp(LR, Reg, LaneMask); |
1082 | 2.37M | LLVM_DEBUG(dbgs() << " -->\t" << LR << '\n'); |
1083 | 2.37M | LR.verify(); |
1084 | 2.37M | } |
1085 | | |
1086 | | /// Update LR to reflect an instruction has been moved downwards from OldIdx |
1087 | | /// to NewIdx (OldIdx < NewIdx). |
1088 | 2.05M | void handleMoveDown(LiveRange &LR) { |
1089 | 2.05M | LiveRange::iterator E = LR.end(); |
1090 | | // Segment going into OldIdx. |
1091 | 2.05M | LiveRange::iterator OldIdxIn = LR.find(OldIdx.getBaseIndex()); |
1092 | | |
1093 | | // No value live before or after OldIdx? Nothing to do. |
1094 | 2.05M | if (OldIdxIn == E || SlotIndex::isEarlierInstr(OldIdx, OldIdxIn->start)) |
1095 | 8.38k | return; |
1096 | | |
1097 | 2.04M | LiveRange::iterator OldIdxOut; |
1098 | | // Do we have a value live-in to OldIdx? |
1099 | 2.04M | if (SlotIndex::isEarlierInstr(OldIdxIn->start, OldIdx)) { |
1100 | | // If the live-in value already extends to NewIdx, there is nothing to do. |
1101 | 1.30M | if (SlotIndex::isEarlierEqualInstr(NewIdx, OldIdxIn->end)) |
1102 | 452k | return; |
1103 | | // Aggressively remove all kill flags from the old kill point. |
1104 | | // Kill flags shouldn't be used while live intervals exist, they will be |
1105 | | // reinserted by VirtRegRewriter. |
1106 | 855k | if (MachineInstr *KillMI = LIS.getInstructionFromIndex(OldIdxIn->end)) |
1107 | 44.8k | for (MachineOperand &MOP : mi_bundle_ops(*KillMI)) |
1108 | 153k | if (MOP.isReg() && MOP.isUse()) |
1109 | 80.2k | MOP.setIsKill(false); |
1110 | | |
1111 | | // Is there a def before NewIdx which is not OldIdx? |
1112 | 855k | LiveRange::iterator Next = std::next(OldIdxIn); |
1113 | 855k | if (Next != E && !SlotIndex::isSameInstr(OldIdx, Next->start) && |
1114 | 855k | SlotIndex::isEarlierInstr(Next->start, NewIdx)) { |
1115 | | // If we are here then OldIdx was just a use but not a def. We only have |
1116 | | // to ensure liveness extends to NewIdx. |
1117 | 0 | LiveRange::iterator NewIdxIn = |
1118 | 0 | LR.advanceTo(Next, NewIdx.getBaseIndex()); |
1119 | | // Extend the segment before NewIdx if necessary. |
1120 | 0 | if (NewIdxIn == E || |
1121 | 0 | !SlotIndex::isEarlierInstr(NewIdxIn->start, NewIdx)) { |
1122 | 0 | LiveRange::iterator Prev = std::prev(NewIdxIn); |
1123 | 0 | Prev->end = NewIdx.getRegSlot(); |
1124 | 0 | } |
1125 | | // Extend OldIdxIn. |
1126 | 0 | OldIdxIn->end = Next->start; |
1127 | 0 | return; |
1128 | 0 | } |
1129 | | |
1130 | | // Adjust OldIdxIn->end to reach NewIdx. This may temporarily make LR |
1131 | | // invalid by overlapping ranges. |
1132 | 855k | bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); |
1133 | 855k | OldIdxIn->end = NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber()); |
1134 | | // If this was not a kill, then there was no def and we're done. |
1135 | 855k | if (!isKill) |
1136 | 44.8k | return; |
1137 | | |
1138 | | // Did we have a Def at OldIdx? |
1139 | 810k | OldIdxOut = Next; |
1140 | 810k | if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) |
1141 | 736k | return; |
1142 | 810k | } else { |
1143 | 733k | OldIdxOut = OldIdxIn; |
1144 | 733k | } |
1145 | | |
1146 | | // If we are here then there is a Definition at OldIdx. OldIdxOut points |
1147 | | // to the segment starting there. |
1148 | 808k | assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && |
1149 | 808k | "No def?"); |
1150 | 0 | VNInfo *OldIdxVNI = OldIdxOut->valno; |
1151 | 808k | assert(OldIdxVNI->def == OldIdxOut->start && "Inconsistent def"); |
1152 | | |
1153 | | // If the defined value extends beyond NewIdx, just move the beginning |
1154 | | // of the segment to NewIdx. |
1155 | 0 | SlotIndex NewIdxDef = NewIdx.getRegSlot(OldIdxOut->start.isEarlyClobber()); |
1156 | 808k | if (SlotIndex::isEarlierInstr(NewIdxDef, OldIdxOut->end)) { |
1157 | 756k | OldIdxVNI->def = NewIdxDef; |
1158 | 756k | OldIdxOut->start = OldIdxVNI->def; |
1159 | 756k | return; |
1160 | 756k | } |
1161 | | |
1162 | | // If we are here then we have a Definition at OldIdx which ends before |
1163 | | // NewIdx. |
1164 | | |
1165 | | // Is there an existing Def at NewIdx? |
1166 | 51.3k | LiveRange::iterator AfterNewIdx |
1167 | 51.3k | = LR.advanceTo(OldIdxOut, NewIdx.getRegSlot()); |
1168 | 51.3k | bool OldIdxDefIsDead = OldIdxOut->end.isDead(); |
1169 | 51.3k | if (!OldIdxDefIsDead && |
1170 | 51.3k | SlotIndex::isEarlierInstr(OldIdxOut->end, NewIdxDef)) { |
1171 | | // OldIdx is not a dead def, and NewIdxDef is inside a new interval. |
1172 | 0 | VNInfo *DefVNI; |
1173 | 0 | if (OldIdxOut != LR.begin() && |
1174 | 0 | !SlotIndex::isEarlierInstr(std::prev(OldIdxOut)->end, |
1175 | 0 | OldIdxOut->start)) { |
1176 | | // There is no gap between OldIdxOut and its predecessor anymore, |
1177 | | // merge them. |
1178 | 0 | LiveRange::iterator IPrev = std::prev(OldIdxOut); |
1179 | 0 | DefVNI = OldIdxVNI; |
1180 | 0 | IPrev->end = OldIdxOut->end; |
1181 | 0 | } else { |
1182 | | // The value is live in to OldIdx |
1183 | 0 | LiveRange::iterator INext = std::next(OldIdxOut); |
1184 | 0 | assert(INext != E && "Must have following segment"); |
1185 | | // We merge OldIdxOut and its successor. As we're dealing with subreg |
1186 | | // reordering, there is always a successor to OldIdxOut in the same BB |
1187 | | // We don't need INext->valno anymore and will reuse for the new segment |
1188 | | // we create later. |
1189 | 0 | DefVNI = OldIdxVNI; |
1190 | 0 | INext->start = OldIdxOut->end; |
1191 | 0 | INext->valno->def = INext->start; |
1192 | 0 | } |
1193 | | // If NewIdx is behind the last segment, extend that and append a new one. |
1194 | 0 | if (AfterNewIdx == E) { |
1195 | | // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up |
1196 | | // one position. |
1197 | | // |- ?/OldIdxOut -| |- X0 -| ... |- Xn -| end |
1198 | | // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS -| end |
1199 | 0 | std::copy(std::next(OldIdxOut), E, OldIdxOut); |
1200 | | // The last segment is undefined now, reuse it for a dead def. |
1201 | 0 | LiveRange::iterator NewSegment = std::prev(E); |
1202 | 0 | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
1203 | 0 | DefVNI); |
1204 | 0 | DefVNI->def = NewIdxDef; |
1205 | |
|
1206 | 0 | LiveRange::iterator Prev = std::prev(NewSegment); |
1207 | 0 | Prev->end = NewIdxDef; |
1208 | 0 | } else { |
1209 | | // OldIdxOut is undef at this point, Slide (OldIdxOut;AfterNewIdx] up |
1210 | | // one position. |
1211 | | // |- ?/OldIdxOut -| |- X0 -| ... |- Xn/AfterNewIdx -| |- Next -| |
1212 | | // => |- X0/OldIdxOut -| ... |- Xn -| |- Xn/AfterNewIdx -| |- Next -| |
1213 | 0 | std::copy(std::next(OldIdxOut), std::next(AfterNewIdx), OldIdxOut); |
1214 | 0 | LiveRange::iterator Prev = std::prev(AfterNewIdx); |
1215 | | // We have two cases: |
1216 | 0 | if (SlotIndex::isEarlierInstr(Prev->start, NewIdxDef)) { |
1217 | | // Case 1: NewIdx is inside a liverange. Split this liverange at |
1218 | | // NewIdxDef into the segment "Prev" followed by "NewSegment". |
1219 | 0 | LiveRange::iterator NewSegment = AfterNewIdx; |
1220 | 0 | *NewSegment = LiveRange::Segment(NewIdxDef, Prev->end, Prev->valno); |
1221 | 0 | Prev->valno->def = NewIdxDef; |
1222 | |
|
1223 | 0 | *Prev = LiveRange::Segment(Prev->start, NewIdxDef, DefVNI); |
1224 | 0 | DefVNI->def = Prev->start; |
1225 | 0 | } else { |
1226 | | // Case 2: NewIdx is in a lifetime hole. Keep AfterNewIdx as is and |
1227 | | // turn Prev into a segment from NewIdx to AfterNewIdx->start. |
1228 | 0 | *Prev = LiveRange::Segment(NewIdxDef, AfterNewIdx->start, DefVNI); |
1229 | 0 | DefVNI->def = NewIdxDef; |
1230 | 0 | assert(DefVNI != AfterNewIdx->valno); |
1231 | 0 | } |
1232 | 0 | } |
1233 | 0 | return; |
1234 | 0 | } |
1235 | | |
1236 | 51.3k | if (AfterNewIdx != E && |
1237 | 51.3k | SlotIndex::isSameInstr(AfterNewIdx->start, NewIdxDef)) { |
1238 | | // There is an existing def at NewIdx. The def at OldIdx is coalesced into |
1239 | | // that value. |
1240 | 0 | assert(AfterNewIdx->valno != OldIdxVNI && "Multiple defs of value?"); |
1241 | 0 | LR.removeValNo(OldIdxVNI); |
1242 | 51.3k | } else { |
1243 | | // There was no existing def at NewIdx. We need to create a dead def |
1244 | | // at NewIdx. Shift segments over the old OldIdxOut segment, this frees |
1245 | | // a new segment at the place where we want to construct the dead def. |
1246 | | // |- OldIdxOut -| |- X0 -| ... |- Xn -| |- AfterNewIdx -| |
1247 | | // => |- X0/OldIdxOut -| ... |- Xn -| |- undef/NewS. -| |- AfterNewIdx -| |
1248 | 51.3k | assert(AfterNewIdx != OldIdxOut && "Inconsistent iterators"); |
1249 | 0 | std::copy(std::next(OldIdxOut), AfterNewIdx, OldIdxOut); |
1250 | | // We can reuse OldIdxVNI now. |
1251 | 51.3k | LiveRange::iterator NewSegment = std::prev(AfterNewIdx); |
1252 | 51.3k | VNInfo *NewSegmentVNI = OldIdxVNI; |
1253 | 51.3k | NewSegmentVNI->def = NewIdxDef; |
1254 | 51.3k | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
1255 | 51.3k | NewSegmentVNI); |
1256 | 51.3k | } |
1257 | 51.3k | } |
1258 | | |
1259 | | /// Update LR to reflect an instruction has been moved upwards from OldIdx |
1260 | | /// to NewIdx (NewIdx < OldIdx). |
1261 | 323k | void handleMoveUp(LiveRange &LR, Register Reg, LaneBitmask LaneMask) { |
1262 | 323k | LiveRange::iterator E = LR.end(); |
1263 | | // Segment going into OldIdx. |
1264 | 323k | LiveRange::iterator OldIdxIn = LR.find(OldIdx.getBaseIndex()); |
1265 | | |
1266 | | // No value live before or after OldIdx? Nothing to do. |
1267 | 323k | if (OldIdxIn == E || SlotIndex::isEarlierInstr(OldIdx, OldIdxIn->start)) |
1268 | 3.15k | return; |
1269 | | |
1270 | 320k | LiveRange::iterator OldIdxOut; |
1271 | | // Do we have a value live-in to OldIdx? |
1272 | 320k | if (SlotIndex::isEarlierInstr(OldIdxIn->start, OldIdx)) { |
1273 | | // If the live-in value isn't killed here, then we have no Def at |
1274 | | // OldIdx, moreover the value must be live at NewIdx so there is nothing |
1275 | | // to do. |
1276 | 169k | bool isKill = SlotIndex::isSameInstr(OldIdx, OldIdxIn->end); |
1277 | 169k | if (!isKill) |
1278 | 48.9k | return; |
1279 | | |
1280 | | // At this point we have to move OldIdxIn->end back to the nearest |
1281 | | // previous use or (dead-)def but no further than NewIdx. |
1282 | 120k | SlotIndex DefBeforeOldIdx |
1283 | 120k | = std::max(OldIdxIn->start.getDeadSlot(), |
1284 | 120k | NewIdx.getRegSlot(OldIdxIn->end.isEarlyClobber())); |
1285 | 120k | OldIdxIn->end = findLastUseBefore(DefBeforeOldIdx, Reg, LaneMask); |
1286 | | |
1287 | | // Did we have a Def at OldIdx? If not we are done now. |
1288 | 120k | OldIdxOut = std::next(OldIdxIn); |
1289 | 120k | if (OldIdxOut == E || !SlotIndex::isSameInstr(OldIdx, OldIdxOut->start)) |
1290 | 114k | return; |
1291 | 150k | } else { |
1292 | 150k | OldIdxOut = OldIdxIn; |
1293 | 150k | OldIdxIn = OldIdxOut != LR.begin() ? std::prev(OldIdxOut) : E; |
1294 | 150k | } |
1295 | | |
1296 | | // If we are here then there is a Definition at OldIdx. OldIdxOut points |
1297 | | // to the segment starting there. |
1298 | 156k | assert(OldIdxOut != E && SlotIndex::isSameInstr(OldIdx, OldIdxOut->start) && |
1299 | 156k | "No def?"); |
1300 | 0 | VNInfo *OldIdxVNI = OldIdxOut->valno; |
1301 | 156k | assert(OldIdxVNI->def == OldIdxOut->start && "Inconsistent def"); |
1302 | 0 | bool OldIdxDefIsDead = OldIdxOut->end.isDead(); |
1303 | | |
1304 | | // Is there an existing def at NewIdx? |
1305 | 156k | SlotIndex NewIdxDef = NewIdx.getRegSlot(OldIdxOut->start.isEarlyClobber()); |
1306 | 156k | LiveRange::iterator NewIdxOut = LR.find(NewIdx.getRegSlot()); |
1307 | 156k | if (SlotIndex::isSameInstr(NewIdxOut->start, NewIdx)) { |
1308 | 0 | assert(NewIdxOut->valno != OldIdxVNI && |
1309 | 0 | "Same value defined more than once?"); |
1310 | | // If OldIdx was a dead def remove it. |
1311 | 0 | if (!OldIdxDefIsDead) { |
1312 | | // Remove segment starting at NewIdx and move begin of OldIdxOut to |
1313 | | // NewIdx so it can take its place. |
1314 | 0 | OldIdxVNI->def = NewIdxDef; |
1315 | 0 | OldIdxOut->start = NewIdxDef; |
1316 | 0 | LR.removeValNo(NewIdxOut->valno); |
1317 | 0 | } else { |
1318 | | // Simply remove the dead def at OldIdx. |
1319 | 0 | LR.removeValNo(OldIdxVNI); |
1320 | 0 | } |
1321 | 156k | } else { |
1322 | | // Previously nothing was live after NewIdx, so all we have to do now is |
1323 | | // move the begin of OldIdxOut to NewIdx. |
1324 | 156k | if (!OldIdxDefIsDead) { |
1325 | | // Do we have any intermediate Defs between OldIdx and NewIdx? |
1326 | 127k | if (OldIdxIn != E && |
1327 | 127k | SlotIndex::isEarlierInstr(NewIdxDef, OldIdxIn->start)) { |
1328 | | // OldIdx is not a dead def and NewIdx is before predecessor start. |
1329 | 0 | LiveRange::iterator NewIdxIn = NewIdxOut; |
1330 | 0 | assert(NewIdxIn == LR.find(NewIdx.getBaseIndex())); |
1331 | 0 | const SlotIndex SplitPos = NewIdxDef; |
1332 | 0 | OldIdxVNI = OldIdxIn->valno; |
1333 | |
|
1334 | 0 | SlotIndex NewDefEndPoint = std::next(NewIdxIn)->end; |
1335 | 0 | LiveRange::iterator Prev = std::prev(OldIdxIn); |
1336 | 0 | if (OldIdxIn != LR.begin() && |
1337 | 0 | SlotIndex::isEarlierInstr(NewIdx, Prev->end)) { |
1338 | | // If the segment before OldIdx read a value defined earlier than |
1339 | | // NewIdx, the moved instruction also reads and forwards that |
1340 | | // value. Extend the lifetime of the new def point. |
1341 | | |
1342 | | // Extend to where the previous range started, unless there is |
1343 | | // another redef first. |
1344 | 0 | NewDefEndPoint = std::min(OldIdxIn->start, |
1345 | 0 | std::next(NewIdxOut)->start); |
1346 | 0 | } |
1347 | | |
1348 | | // Merge the OldIdxIn and OldIdxOut segments into OldIdxOut. |
1349 | 0 | OldIdxOut->valno->def = OldIdxIn->start; |
1350 | 0 | *OldIdxOut = LiveRange::Segment(OldIdxIn->start, OldIdxOut->end, |
1351 | 0 | OldIdxOut->valno); |
1352 | | // OldIdxIn and OldIdxVNI are now undef and can be overridden. |
1353 | | // We Slide [NewIdxIn, OldIdxIn) down one position. |
1354 | | // |- X0/NewIdxIn -| ... |- Xn-1 -||- Xn/OldIdxIn -||- OldIdxOut -| |
1355 | | // => |- undef/NexIdxIn -| |- X0 -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |
1356 | 0 | std::copy_backward(NewIdxIn, OldIdxIn, OldIdxOut); |
1357 | | // NewIdxIn is now considered undef so we can reuse it for the moved |
1358 | | // value. |
1359 | 0 | LiveRange::iterator NewSegment = NewIdxIn; |
1360 | 0 | LiveRange::iterator Next = std::next(NewSegment); |
1361 | 0 | if (SlotIndex::isEarlierInstr(Next->start, NewIdx)) { |
1362 | | // There is no gap between NewSegment and its predecessor. |
1363 | 0 | *NewSegment = LiveRange::Segment(Next->start, SplitPos, |
1364 | 0 | Next->valno); |
1365 | |
|
1366 | 0 | *Next = LiveRange::Segment(SplitPos, NewDefEndPoint, OldIdxVNI); |
1367 | 0 | Next->valno->def = SplitPos; |
1368 | 0 | } else { |
1369 | | // There is a gap between NewSegment and its predecessor |
1370 | | // Value becomes live in. |
1371 | 0 | *NewSegment = LiveRange::Segment(SplitPos, Next->start, OldIdxVNI); |
1372 | 0 | NewSegment->valno->def = SplitPos; |
1373 | 0 | } |
1374 | 127k | } else { |
1375 | | // Leave the end point of a live def. |
1376 | 127k | OldIdxOut->start = NewIdxDef; |
1377 | 127k | OldIdxVNI->def = NewIdxDef; |
1378 | 127k | if (OldIdxIn != E && SlotIndex::isEarlierInstr(NewIdx, OldIdxIn->end)) |
1379 | 0 | OldIdxIn->end = NewIdxDef; |
1380 | 127k | } |
1381 | 127k | } else if (OldIdxIn != E |
1382 | 29.5k | && SlotIndex::isEarlierInstr(NewIdxOut->start, NewIdx) |
1383 | 29.5k | && SlotIndex::isEarlierInstr(NewIdx, NewIdxOut->end)) { |
1384 | | // OldIdxVNI is a dead def that has been moved into the middle of |
1385 | | // another value in LR. That can happen when LR is a whole register, |
1386 | | // but the dead def is a write to a subreg that is dead at NewIdx. |
1387 | | // The dead def may have been moved across other values |
1388 | | // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut) |
1389 | | // down one position. |
1390 | | // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - | |
1391 | | // => |- X0/NewIdxOut -| |- X0 -| ... |- Xn-1 -| |- next -| |
1392 | 0 | std::copy_backward(NewIdxOut, OldIdxOut, std::next(OldIdxOut)); |
1393 | | // Modify the segment at NewIdxOut and the following segment to meet at |
1394 | | // the point of the dead def, with the following segment getting |
1395 | | // OldIdxVNI as its value number. |
1396 | 0 | *NewIdxOut = LiveRange::Segment( |
1397 | 0 | NewIdxOut->start, NewIdxDef.getRegSlot(), NewIdxOut->valno); |
1398 | 0 | *(NewIdxOut + 1) = LiveRange::Segment( |
1399 | 0 | NewIdxDef.getRegSlot(), (NewIdxOut + 1)->end, OldIdxVNI); |
1400 | 0 | OldIdxVNI->def = NewIdxDef; |
1401 | | // Modify subsequent segments to be defined by the moved def OldIdxVNI. |
1402 | 0 | for (auto *Idx = NewIdxOut + 2; Idx <= OldIdxOut; ++Idx) |
1403 | 0 | Idx->valno = OldIdxVNI; |
1404 | | // Aggressively remove all dead flags from the former dead definition. |
1405 | | // Kill/dead flags shouldn't be used while live intervals exist; they |
1406 | | // will be reinserted by VirtRegRewriter. |
1407 | 0 | if (MachineInstr *KillMI = LIS.getInstructionFromIndex(NewIdx)) |
1408 | 0 | for (MIBundleOperands MO(*KillMI); MO.isValid(); ++MO) |
1409 | 0 | if (MO->isReg() && !MO->isUse()) |
1410 | 0 | MO->setIsDead(false); |
1411 | 29.5k | } else { |
1412 | | // OldIdxVNI is a dead def. It may have been moved across other values |
1413 | | // in LR, so move OldIdxOut up to NewIdxOut. Slide [NewIdxOut;OldIdxOut) |
1414 | | // down one position. |
1415 | | // |- X0/NewIdxOut -| ... |- Xn-1 -| |- Xn/OldIdxOut -| |- next - | |
1416 | | // => |- undef/NewIdxOut -| |- X0 -| ... |- Xn-1 -| |- next -| |
1417 | 29.5k | std::copy_backward(NewIdxOut, OldIdxOut, std::next(OldIdxOut)); |
1418 | | // OldIdxVNI can be reused now to build a new dead def segment. |
1419 | 29.5k | LiveRange::iterator NewSegment = NewIdxOut; |
1420 | 29.5k | VNInfo *NewSegmentVNI = OldIdxVNI; |
1421 | 29.5k | *NewSegment = LiveRange::Segment(NewIdxDef, NewIdxDef.getDeadSlot(), |
1422 | 29.5k | NewSegmentVNI); |
1423 | 29.5k | NewSegmentVNI->def = NewIdxDef; |
1424 | 29.5k | } |
1425 | 156k | } |
1426 | 156k | } |
1427 | | |
1428 | 0 | void updateRegMaskSlots() { |
1429 | 0 | SmallVectorImpl<SlotIndex>::iterator RI = |
1430 | 0 | llvm::lower_bound(LIS.RegMaskSlots, OldIdx); |
1431 | 0 | assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() && |
1432 | 0 | "No RegMask at OldIdx."); |
1433 | 0 | *RI = NewIdx.getRegSlot(); |
1434 | 0 | assert((RI == LIS.RegMaskSlots.begin() || |
1435 | 0 | SlotIndex::isEarlierInstr(*std::prev(RI), *RI)) && |
1436 | 0 | "Cannot move regmask instruction above another call"); |
1437 | 0 | assert((std::next(RI) == LIS.RegMaskSlots.end() || |
1438 | 0 | SlotIndex::isEarlierInstr(*RI, *std::next(RI))) && |
1439 | 0 | "Cannot move regmask instruction below another call"); |
1440 | 0 | } |
1441 | | |
1442 | | // Return the last use of reg between NewIdx and OldIdx. |
1443 | | SlotIndex findLastUseBefore(SlotIndex Before, Register Reg, |
1444 | 120k | LaneBitmask LaneMask) { |
1445 | 120k | if (Reg.isVirtual()) { |
1446 | 71.4k | SlotIndex LastUse = Before; |
1447 | 134k | for (MachineOperand &MO : MRI.use_nodbg_operands(Reg)) { |
1448 | 134k | if (MO.isUndef()) |
1449 | 0 | continue; |
1450 | 134k | unsigned SubReg = MO.getSubReg(); |
1451 | 134k | if (SubReg != 0 && LaneMask.any() |
1452 | 134k | && (TRI.getSubRegIndexLaneMask(SubReg) & LaneMask).none()) |
1453 | 2.97k | continue; |
1454 | | |
1455 | 131k | const MachineInstr &MI = *MO.getParent(); |
1456 | 131k | SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI); |
1457 | 131k | if (InstSlot > LastUse && InstSlot < OldIdx) |
1458 | 4.58k | LastUse = InstSlot.getRegSlot(); |
1459 | 131k | } |
1460 | 71.4k | return LastUse; |
1461 | 71.4k | } |
1462 | | |
1463 | | // This is a regunit interval, so scanning the use list could be very |
1464 | | // expensive. Scan upwards from OldIdx instead. |
1465 | 49.1k | assert(Before < OldIdx && "Expected upwards move"); |
1466 | 0 | SlotIndexes *Indexes = LIS.getSlotIndexes(); |
1467 | 49.1k | MachineBasicBlock *MBB = Indexes->getMBBFromIndex(Before); |
1468 | | |
1469 | | // OldIdx may not correspond to an instruction any longer, so set MII to |
1470 | | // point to the next instruction after OldIdx, or MBB->end(). |
1471 | 49.1k | MachineBasicBlock::iterator MII = MBB->end(); |
1472 | 49.1k | if (MachineInstr *MI = Indexes->getInstructionFromIndex( |
1473 | 49.1k | Indexes->getNextNonNullIndex(OldIdx))) |
1474 | 49.1k | if (MI->getParent() == MBB) |
1475 | 48.6k | MII = MI; |
1476 | | |
1477 | 49.1k | MachineBasicBlock::iterator Begin = MBB->begin(); |
1478 | 213k | while (MII != Begin) { |
1479 | 213k | if ((--MII)->isDebugOrPseudoInstr()) |
1480 | 36 | continue; |
1481 | 213k | SlotIndex Idx = Indexes->getInstructionIndex(*MII); |
1482 | | |
1483 | | // Stop searching when Before is reached. |
1484 | 213k | if (!SlotIndex::isEarlierInstr(Before, Idx)) |
1485 | 49.1k | return Before; |
1486 | | |
1487 | | // Check if MII uses Reg. |
1488 | 721k | for (MIBundleOperands MO(*MII); MO.isValid(); ++MO) |
1489 | 557k | if (MO->isReg() && !MO->isUndef() && MO->getReg().isPhysical() && |
1490 | 557k | TRI.hasRegUnit(MO->getReg(), Reg)) |
1491 | 10 | return Idx.getRegSlot(); |
1492 | 163k | } |
1493 | | // Didn't reach Before. It must be the first instruction in the block. |
1494 | 0 | return Before; |
1495 | 49.1k | } |
1496 | | }; |
1497 | | |
1498 | 1.05M | void LiveIntervals::handleMove(MachineInstr &MI, bool UpdateFlags) { |
1499 | | // It is fine to move a bundle as a whole, but not an individual instruction |
1500 | | // inside it. |
1501 | 1.05M | assert((!MI.isBundled() || MI.getOpcode() == TargetOpcode::BUNDLE) && |
1502 | 1.05M | "Cannot move instruction in bundle"); |
1503 | 0 | SlotIndex OldIndex = Indexes->getInstructionIndex(MI); |
1504 | 1.05M | Indexes->removeMachineInstrFromMaps(MI); |
1505 | 1.05M | SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI); |
1506 | 1.05M | assert(getMBBStartIdx(MI.getParent()) <= OldIndex && |
1507 | 1.05M | OldIndex < getMBBEndIdx(MI.getParent()) && |
1508 | 1.05M | "Cannot handle moves across basic block boundaries."); |
1509 | | |
1510 | 0 | HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); |
1511 | 1.05M | HME.updateAllRanges(&MI); |
1512 | 1.05M | } |
1513 | | |
1514 | | void LiveIntervals::handleMoveIntoNewBundle(MachineInstr &BundleStart, |
1515 | 0 | bool UpdateFlags) { |
1516 | 0 | assert((BundleStart.getOpcode() == TargetOpcode::BUNDLE) && |
1517 | 0 | "Bundle start is not a bundle"); |
1518 | 0 | SmallVector<SlotIndex, 16> ToProcess; |
1519 | 0 | const SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(BundleStart); |
1520 | 0 | auto BundleEnd = getBundleEnd(BundleStart.getIterator()); |
1521 | |
|
1522 | 0 | auto I = BundleStart.getIterator(); |
1523 | 0 | I++; |
1524 | 0 | while (I != BundleEnd) { |
1525 | 0 | if (!Indexes->hasIndex(*I)) |
1526 | 0 | continue; |
1527 | 0 | SlotIndex OldIndex = Indexes->getInstructionIndex(*I, true); |
1528 | 0 | ToProcess.push_back(OldIndex); |
1529 | 0 | Indexes->removeMachineInstrFromMaps(*I, true); |
1530 | 0 | I++; |
1531 | 0 | } |
1532 | 0 | for (SlotIndex OldIndex : ToProcess) { |
1533 | 0 | HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); |
1534 | 0 | HME.updateAllRanges(&BundleStart); |
1535 | 0 | } |
1536 | | |
1537 | | // Fix up dead defs |
1538 | 0 | const SlotIndex Index = getInstructionIndex(BundleStart); |
1539 | 0 | for (unsigned Idx = 0, E = BundleStart.getNumOperands(); Idx != E; ++Idx) { |
1540 | 0 | MachineOperand &MO = BundleStart.getOperand(Idx); |
1541 | 0 | if (!MO.isReg()) |
1542 | 0 | continue; |
1543 | 0 | Register Reg = MO.getReg(); |
1544 | 0 | if (Reg.isVirtual() && hasInterval(Reg) && !MO.isUndef()) { |
1545 | 0 | LiveInterval &LI = getInterval(Reg); |
1546 | 0 | LiveQueryResult LRQ = LI.Query(Index); |
1547 | 0 | if (LRQ.isDeadDef()) |
1548 | 0 | MO.setIsDead(); |
1549 | 0 | } |
1550 | 0 | } |
1551 | 0 | } |
1552 | | |
1553 | | void LiveIntervals::repairOldRegInRange(const MachineBasicBlock::iterator Begin, |
1554 | | const MachineBasicBlock::iterator End, |
1555 | | const SlotIndex EndIdx, LiveRange &LR, |
1556 | | const Register Reg, |
1557 | 0 | LaneBitmask LaneMask) { |
1558 | 0 | LiveInterval::iterator LII = LR.find(EndIdx); |
1559 | 0 | SlotIndex lastUseIdx; |
1560 | 0 | if (LII != LR.end() && LII->start < EndIdx) { |
1561 | 0 | lastUseIdx = LII->end; |
1562 | 0 | } else if (LII == LR.begin()) { |
1563 | | // We may not have a liverange at all if this is a subregister untouched |
1564 | | // between \p Begin and \p End. |
1565 | 0 | } else { |
1566 | 0 | --LII; |
1567 | 0 | } |
1568 | |
|
1569 | 0 | for (MachineBasicBlock::iterator I = End; I != Begin;) { |
1570 | 0 | --I; |
1571 | 0 | MachineInstr &MI = *I; |
1572 | 0 | if (MI.isDebugOrPseudoInstr()) |
1573 | 0 | continue; |
1574 | | |
1575 | 0 | SlotIndex instrIdx = getInstructionIndex(MI); |
1576 | 0 | bool isStartValid = getInstructionFromIndex(LII->start); |
1577 | 0 | bool isEndValid = getInstructionFromIndex(LII->end); |
1578 | | |
1579 | | // FIXME: This doesn't currently handle early-clobber or multiple removed |
1580 | | // defs inside of the region to repair. |
1581 | 0 | for (const MachineOperand &MO : MI.operands()) { |
1582 | 0 | if (!MO.isReg() || MO.getReg() != Reg) |
1583 | 0 | continue; |
1584 | | |
1585 | 0 | unsigned SubReg = MO.getSubReg(); |
1586 | 0 | LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubReg); |
1587 | 0 | if ((Mask & LaneMask).none()) |
1588 | 0 | continue; |
1589 | | |
1590 | 0 | if (MO.isDef()) { |
1591 | 0 | if (!isStartValid) { |
1592 | 0 | if (LII->end.isDead()) { |
1593 | 0 | LII = LR.removeSegment(LII, true); |
1594 | 0 | if (LII != LR.begin()) |
1595 | 0 | --LII; |
1596 | 0 | } else { |
1597 | 0 | LII->start = instrIdx.getRegSlot(); |
1598 | 0 | LII->valno->def = instrIdx.getRegSlot(); |
1599 | 0 | if (MO.getSubReg() && !MO.isUndef()) |
1600 | 0 | lastUseIdx = instrIdx.getRegSlot(); |
1601 | 0 | else |
1602 | 0 | lastUseIdx = SlotIndex(); |
1603 | 0 | continue; |
1604 | 0 | } |
1605 | 0 | } |
1606 | | |
1607 | 0 | if (!lastUseIdx.isValid()) { |
1608 | 0 | VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator); |
1609 | 0 | LiveRange::Segment S(instrIdx.getRegSlot(), |
1610 | 0 | instrIdx.getDeadSlot(), VNI); |
1611 | 0 | LII = LR.addSegment(S); |
1612 | 0 | } else if (LII->start != instrIdx.getRegSlot()) { |
1613 | 0 | VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator); |
1614 | 0 | LiveRange::Segment S(instrIdx.getRegSlot(), lastUseIdx, VNI); |
1615 | 0 | LII = LR.addSegment(S); |
1616 | 0 | } |
1617 | |
|
1618 | 0 | if (MO.getSubReg() && !MO.isUndef()) |
1619 | 0 | lastUseIdx = instrIdx.getRegSlot(); |
1620 | 0 | else |
1621 | 0 | lastUseIdx = SlotIndex(); |
1622 | 0 | } else if (MO.isUse()) { |
1623 | | // FIXME: This should probably be handled outside of this branch, |
1624 | | // either as part of the def case (for defs inside of the region) or |
1625 | | // after the loop over the region. |
1626 | 0 | if (!isEndValid && !LII->end.isBlock()) |
1627 | 0 | LII->end = instrIdx.getRegSlot(); |
1628 | 0 | if (!lastUseIdx.isValid()) |
1629 | 0 | lastUseIdx = instrIdx.getRegSlot(); |
1630 | 0 | } |
1631 | 0 | } |
1632 | 0 | } |
1633 | |
|
1634 | 0 | bool isStartValid = getInstructionFromIndex(LII->start); |
1635 | 0 | if (!isStartValid && LII->end.isDead()) |
1636 | 0 | LR.removeSegment(*LII, true); |
1637 | 0 | } |
1638 | | |
1639 | | void |
1640 | | LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB, |
1641 | | MachineBasicBlock::iterator Begin, |
1642 | | MachineBasicBlock::iterator End, |
1643 | 0 | ArrayRef<Register> OrigRegs) { |
1644 | | // Find anchor points, which are at the beginning/end of blocks or at |
1645 | | // instructions that already have indexes. |
1646 | 0 | while (Begin != MBB->begin() && !Indexes->hasIndex(*std::prev(Begin))) |
1647 | 0 | --Begin; |
1648 | 0 | while (End != MBB->end() && !Indexes->hasIndex(*End)) |
1649 | 0 | ++End; |
1650 | |
|
1651 | 0 | SlotIndex EndIdx; |
1652 | 0 | if (End == MBB->end()) |
1653 | 0 | EndIdx = getMBBEndIdx(MBB).getPrevSlot(); |
1654 | 0 | else |
1655 | 0 | EndIdx = getInstructionIndex(*End); |
1656 | |
|
1657 | 0 | Indexes->repairIndexesInRange(MBB, Begin, End); |
1658 | | |
1659 | | // Make sure a live interval exists for all register operands in the range. |
1660 | 0 | SmallVector<Register> RegsToRepair(OrigRegs.begin(), OrigRegs.end()); |
1661 | 0 | for (MachineBasicBlock::iterator I = End; I != Begin;) { |
1662 | 0 | --I; |
1663 | 0 | MachineInstr &MI = *I; |
1664 | 0 | if (MI.isDebugOrPseudoInstr()) |
1665 | 0 | continue; |
1666 | 0 | for (const MachineOperand &MO : MI.operands()) { |
1667 | 0 | if (MO.isReg() && MO.getReg().isVirtual()) { |
1668 | 0 | Register Reg = MO.getReg(); |
1669 | | // If the new instructions refer to subregs but the old instructions did |
1670 | | // not, throw away any old live interval so it will be recomputed with |
1671 | | // subranges. |
1672 | 0 | if (MO.getSubReg() && hasInterval(Reg) && |
1673 | 0 | !getInterval(Reg).hasSubRanges() && |
1674 | 0 | MRI->shouldTrackSubRegLiveness(Reg)) |
1675 | 0 | removeInterval(Reg); |
1676 | 0 | if (!hasInterval(Reg)) { |
1677 | 0 | createAndComputeVirtRegInterval(Reg); |
1678 | | // Don't bother to repair a freshly calculated live interval. |
1679 | 0 | llvm::erase(RegsToRepair, Reg); |
1680 | 0 | } |
1681 | 0 | } |
1682 | 0 | } |
1683 | 0 | } |
1684 | |
|
1685 | 0 | for (Register Reg : RegsToRepair) { |
1686 | 0 | if (!Reg.isVirtual()) |
1687 | 0 | continue; |
1688 | | |
1689 | 0 | LiveInterval &LI = getInterval(Reg); |
1690 | | // FIXME: Should we support undefs that gain defs? |
1691 | 0 | if (!LI.hasAtLeastOneValue()) |
1692 | 0 | continue; |
1693 | | |
1694 | 0 | for (LiveInterval::SubRange &S : LI.subranges()) |
1695 | 0 | repairOldRegInRange(Begin, End, EndIdx, S, Reg, S.LaneMask); |
1696 | 0 | LI.removeEmptySubRanges(); |
1697 | |
|
1698 | 0 | repairOldRegInRange(Begin, End, EndIdx, LI, Reg); |
1699 | 0 | } |
1700 | 0 | } |
1701 | | |
1702 | 7.67k | void LiveIntervals::removePhysRegDefAt(MCRegister Reg, SlotIndex Pos) { |
1703 | 12.7k | for (MCRegUnit Unit : TRI->regunits(Reg)) { |
1704 | 12.7k | if (LiveRange *LR = getCachedRegUnit(Unit)) |
1705 | 11.3k | if (VNInfo *VNI = LR->getVNInfoAt(Pos)) |
1706 | 11.3k | LR->removeValNo(VNI); |
1707 | 12.7k | } |
1708 | 7.67k | } |
1709 | | |
1710 | 90.1k | void LiveIntervals::removeVRegDefAt(LiveInterval &LI, SlotIndex Pos) { |
1711 | | // LI may not have the main range computed yet, but its subranges may |
1712 | | // be present. |
1713 | 90.1k | VNInfo *VNI = LI.getVNInfoAt(Pos); |
1714 | 90.1k | if (VNI != nullptr) { |
1715 | 90.1k | assert(VNI->def.getBaseIndex() == Pos.getBaseIndex()); |
1716 | 0 | LI.removeValNo(VNI); |
1717 | 90.1k | } |
1718 | | |
1719 | | // Also remove the value defined in subranges. |
1720 | 1.18k | for (LiveInterval::SubRange &S : LI.subranges()) { |
1721 | 1.18k | if (VNInfo *SVNI = S.getVNInfoAt(Pos)) |
1722 | 1.17k | if (SVNI->def.getBaseIndex() == Pos.getBaseIndex()) |
1723 | 1.17k | S.removeValNo(SVNI); |
1724 | 1.18k | } |
1725 | 90.1k | LI.removeEmptySubRanges(); |
1726 | 90.1k | } |
1727 | | |
1728 | | void LiveIntervals::splitSeparateComponents(LiveInterval &LI, |
1729 | 441k | SmallVectorImpl<LiveInterval*> &SplitLIs) { |
1730 | 441k | ConnectedVNInfoEqClasses ConEQ(*this); |
1731 | 441k | unsigned NumComp = ConEQ.Classify(LI); |
1732 | 441k | if (NumComp <= 1) |
1733 | 426k | return; |
1734 | 15.6k | LLVM_DEBUG(dbgs() << " Split " << NumComp << " components: " << LI << '\n'); |
1735 | 15.6k | Register Reg = LI.reg(); |
1736 | 33.4k | for (unsigned I = 1; I < NumComp; ++I) { |
1737 | 17.8k | Register NewVReg = MRI->cloneVirtualRegister(Reg); |
1738 | 17.8k | LiveInterval &NewLI = createEmptyInterval(NewVReg); |
1739 | 17.8k | SplitLIs.push_back(&NewLI); |
1740 | 17.8k | } |
1741 | 15.6k | ConEQ.Distribute(LI, SplitLIs.data(), *MRI); |
1742 | 15.6k | } |
1743 | | |
1744 | 8.07k | void LiveIntervals::constructMainRangeFromSubranges(LiveInterval &LI) { |
1745 | 8.07k | assert(LICalc && "LICalc not initialized."); |
1746 | 0 | LICalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
1747 | 8.07k | LICalc->constructMainRangeFromSubranges(LI); |
1748 | 8.07k | } |