xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/SplitKit.cpp (revision 770cf0a5f02dc8983a89c6568d741fbc25baa999)
1 //===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
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 // This file contains the SplitAnalysis class as well as mutator functions for
10 // live range splitting.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "SplitKit.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/CodeGen/LiveRangeEdit.h"
18 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
19 #include "llvm/CodeGen/MachineDominators.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineLoopInfo.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/TargetInstrInfo.h"
26 #include "llvm/CodeGen/TargetOpcodes.h"
27 #include "llvm/CodeGen/TargetRegisterInfo.h"
28 #include "llvm/CodeGen/TargetSubtargetInfo.h"
29 #include "llvm/CodeGen/VirtRegMap.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/IR/DebugLoc.h"
32 #include "llvm/Support/Allocator.h"
33 #include "llvm/Support/BlockFrequency.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <algorithm>
38 #include <cassert>
39 #include <iterator>
40 #include <limits>
41 #include <tuple>
42 
43 using namespace llvm;
44 
45 #define DEBUG_TYPE "regalloc"
46 
47 static cl::opt<bool>
48     EnableLoopIVHeuristic("enable-split-loopiv-heuristic",
49                           cl::desc("Enable loop iv regalloc heuristic"),
50                           cl::init(true));
51 
52 STATISTIC(NumFinished, "Number of splits finished");
53 STATISTIC(NumSimple,   "Number of splits that were simple");
54 STATISTIC(NumCopies,   "Number of copies inserted for splitting");
55 STATISTIC(NumRemats,   "Number of rematerialized defs for splitting");
56 
57 //===----------------------------------------------------------------------===//
58 //                     Last Insert Point Analysis
59 //===----------------------------------------------------------------------===//
60 
61 InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
62                                          unsigned BBNum)
63     : LIS(lis), LastInsertPoint(BBNum) {}
64 
65 SlotIndex
66 InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
67                                             const MachineBasicBlock &MBB) {
68   unsigned Num = MBB.getNumber();
69   std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
70   SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
71 
72   SmallVector<const MachineBasicBlock *, 1> ExceptionalSuccessors;
73   bool EHPadSuccessor = false;
74   for (const MachineBasicBlock *SMBB : MBB.successors()) {
75     if (SMBB->isEHPad()) {
76       ExceptionalSuccessors.push_back(SMBB);
77       EHPadSuccessor = true;
78     } else if (SMBB->isInlineAsmBrIndirectTarget())
79       ExceptionalSuccessors.push_back(SMBB);
80   }
81 
82   // Compute insert points on the first call. The pair is independent of the
83   // current live interval.
84   if (!LIP.first.isValid()) {
85     MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
86     if (FirstTerm == MBB.end())
87       LIP.first = MBBEnd;
88     else
89       LIP.first = LIS.getInstructionIndex(*FirstTerm);
90 
91     // If there is a landing pad or inlineasm_br successor, also find the
92     // instruction. If there is no such instruction, we don't need to do
93     // anything special.  We assume there cannot be multiple instructions that
94     // are Calls with EHPad successors or INLINEASM_BR in a block. Further, we
95     // assume that if there are any, they will be after any other call
96     // instructions in the block.
97     if (ExceptionalSuccessors.empty())
98       return LIP.first;
99     for (const MachineInstr &MI : llvm::reverse(MBB)) {
100       if ((EHPadSuccessor && MI.isCall()) ||
101           MI.getOpcode() == TargetOpcode::INLINEASM_BR) {
102         LIP.second = LIS.getInstructionIndex(MI);
103         break;
104       }
105     }
106   }
107 
108   // If CurLI is live into a landing pad successor, move the last insert point
109   // back to the call that may throw.
110   if (!LIP.second)
111     return LIP.first;
112 
113   if (none_of(ExceptionalSuccessors, [&](const MachineBasicBlock *EHPad) {
114         return LIS.isLiveInToMBB(CurLI, EHPad);
115       }))
116     return LIP.first;
117 
118   // Find the value leaving MBB.
119   const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
120   if (!VNI)
121     return LIP.first;
122 
123   // The def of statepoint instruction is a gc relocation and it should be alive
124   // in landing pad. So we cannot split interval after statepoint instruction.
125   if (SlotIndex::isSameInstr(VNI->def, LIP.second))
126     if (auto *I = LIS.getInstructionFromIndex(LIP.second))
127       if (I->getOpcode() == TargetOpcode::STATEPOINT)
128         return LIP.second;
129 
130   // If the value leaving MBB was defined after the call in MBB, it can't
131   // really be live-in to the landing pad.  This can happen if the landing pad
132   // has a PHI, and this register is undef on the exceptional edge.
133   if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
134     return LIP.first;
135 
136   // Value is properly live-in to the landing pad.
137   // Only allow inserts before the call.
138   return LIP.second;
139 }
140 
141 MachineBasicBlock::iterator
142 InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
143                                             MachineBasicBlock &MBB) {
144   SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
145   if (LIP == LIS.getMBBEndIdx(&MBB))
146     return MBB.end();
147   return LIS.getInstructionFromIndex(LIP);
148 }
149 
150 //===----------------------------------------------------------------------===//
151 //                                 Split Analysis
152 //===----------------------------------------------------------------------===//
153 
154 SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
155                              const MachineLoopInfo &mli)
156     : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
157       TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
158 
159 void SplitAnalysis::clear() {
160   UseSlots.clear();
161   UseBlocks.clear();
162   ThroughBlocks.clear();
163   CurLI = nullptr;
164 }
165 
166 /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
167 void SplitAnalysis::analyzeUses() {
168   assert(UseSlots.empty() && "Call clear first");
169 
170   // First get all the defs from the interval values. This provides the correct
171   // slots for early clobbers.
172   for (const VNInfo *VNI : CurLI->valnos)
173     if (!VNI->isPHIDef() && !VNI->isUnused())
174       UseSlots.push_back(VNI->def);
175 
176   // Get use slots form the use-def chain.
177   const MachineRegisterInfo &MRI = MF.getRegInfo();
178   for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg()))
179     if (!MO.isUndef())
180       UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
181 
182   array_pod_sort(UseSlots.begin(), UseSlots.end());
183 
184   // Remove duplicates, keeping the smaller slot for each instruction.
185   // That is what we want for early clobbers.
186   UseSlots.erase(llvm::unique(UseSlots, SlotIndex::isSameInstr),
187                  UseSlots.end());
188 
189   // Compute per-live block info.
190   calcLiveBlockInfo();
191 
192   LLVM_DEBUG(dbgs() << "Analyze counted " << UseSlots.size() << " instrs in "
193                     << UseBlocks.size() << " blocks, through "
194                     << NumThroughBlocks << " blocks.\n");
195 }
196 
197 /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
198 /// where CurLI is live.
199 void SplitAnalysis::calcLiveBlockInfo() {
200   ThroughBlocks.resize(MF.getNumBlockIDs());
201   NumThroughBlocks = NumGapBlocks = 0;
202   if (CurLI->empty())
203     return;
204 
205   LiveInterval::const_iterator LVI = CurLI->begin();
206   LiveInterval::const_iterator LVE = CurLI->end();
207 
208   SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
209   UseI = UseSlots.begin();
210   UseE = UseSlots.end();
211 
212   // Loop over basic blocks where CurLI is live.
213   MachineFunction::iterator MFI =
214       LIS.getMBBFromIndex(LVI->start)->getIterator();
215   while (true) {
216     BlockInfo BI;
217     BI.MBB = &*MFI;
218     SlotIndex Start, Stop;
219     std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
220 
221     // If the block contains no uses, the range must be live through. At one
222     // point, RegisterCoalescer could create dangling ranges that ended
223     // mid-block.
224     if (UseI == UseE || *UseI >= Stop) {
225       ++NumThroughBlocks;
226       ThroughBlocks.set(BI.MBB->getNumber());
227       // The range shouldn't end mid-block if there are no uses. This shouldn't
228       // happen.
229       assert(LVI->end >= Stop && "range ends mid block with no uses");
230     } else {
231       // This block has uses. Find the first and last uses in the block.
232       BI.FirstInstr = *UseI;
233       assert(BI.FirstInstr >= Start);
234       do ++UseI;
235       while (UseI != UseE && *UseI < Stop);
236       BI.LastInstr = UseI[-1];
237       assert(BI.LastInstr < Stop);
238 
239       // LVI is the first live segment overlapping MBB.
240       BI.LiveIn = LVI->start <= Start;
241 
242       // When not live in, the first use should be a def.
243       if (!BI.LiveIn) {
244         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
245         assert(LVI->start == BI.FirstInstr && "First instr should be a def");
246         BI.FirstDef = BI.FirstInstr;
247       }
248 
249       // Look for gaps in the live range.
250       BI.LiveOut = true;
251       while (LVI->end < Stop) {
252         SlotIndex LastStop = LVI->end;
253         if (++LVI == LVE || LVI->start >= Stop) {
254           BI.LiveOut = false;
255           BI.LastInstr = LastStop;
256           break;
257         }
258 
259         if (LastStop < LVI->start) {
260           // There is a gap in the live range. Create duplicate entries for the
261           // live-in snippet and the live-out snippet.
262           ++NumGapBlocks;
263 
264           // Push the Live-in part.
265           BI.LiveOut = false;
266           UseBlocks.push_back(BI);
267           UseBlocks.back().LastInstr = LastStop;
268 
269           // Set up BI for the live-out part.
270           BI.LiveIn = false;
271           BI.LiveOut = true;
272           BI.FirstInstr = BI.FirstDef = LVI->start;
273         }
274 
275         // A Segment that starts in the middle of the block must be a def.
276         assert(LVI->start == LVI->valno->def && "Dangling Segment start");
277         if (!BI.FirstDef)
278           BI.FirstDef = LVI->start;
279       }
280 
281       UseBlocks.push_back(BI);
282 
283       // LVI is now at LVE or LVI->end >= Stop.
284       if (LVI == LVE)
285         break;
286     }
287 
288     // Live segment ends exactly at Stop. Move to the next segment.
289     if (LVI->end == Stop && ++LVI == LVE)
290       break;
291 
292     // Pick the next basic block.
293     if (LVI->start < Stop)
294       ++MFI;
295     else
296       MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
297   }
298 
299   LooksLikeLoopIV = EnableLoopIVHeuristic && UseBlocks.size() == 2 &&
300                     any_of(UseBlocks, [this](BlockInfo &BI) {
301                       MachineLoop *L = Loops.getLoopFor(BI.MBB);
302                       return BI.LiveIn && BI.LiveOut && BI.FirstDef && L &&
303                              L->isLoopLatch(BI.MBB);
304                     });
305 
306   assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
307 }
308 
309 unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
310   if (cli->empty())
311     return 0;
312   LiveInterval *li = const_cast<LiveInterval*>(cli);
313   LiveInterval::iterator LVI = li->begin();
314   LiveInterval::iterator LVE = li->end();
315   unsigned Count = 0;
316 
317   // Loop over basic blocks where li is live.
318   MachineFunction::const_iterator MFI =
319       LIS.getMBBFromIndex(LVI->start)->getIterator();
320   SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
321   while (true) {
322     ++Count;
323     LVI = li->advanceTo(LVI, Stop);
324     if (LVI == LVE)
325       return Count;
326     do {
327       ++MFI;
328       Stop = LIS.getMBBEndIdx(&*MFI);
329     } while (Stop <= LVI->start);
330   }
331 }
332 
333 bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
334   Register OrigReg = VRM.getOriginal(CurLI->reg());
335   const LiveInterval &Orig = LIS.getInterval(OrigReg);
336   assert(!Orig.empty() && "Splitting empty interval?");
337   LiveInterval::const_iterator I = Orig.find(Idx);
338 
339   // Range containing Idx should begin at Idx.
340   if (I != Orig.end() && I->start <= Idx)
341     return I->start == Idx;
342 
343   // Range does not contain Idx, previous must end at Idx.
344   return I != Orig.begin() && (--I)->end == Idx;
345 }
346 
347 void SplitAnalysis::analyze(const LiveInterval *li) {
348   clear();
349   CurLI = li;
350   analyzeUses();
351 }
352 
353 //===----------------------------------------------------------------------===//
354 //                               Split Editor
355 //===----------------------------------------------------------------------===//
356 
357 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
358 SplitEditor::SplitEditor(SplitAnalysis &SA, LiveIntervals &LIS, VirtRegMap &VRM,
359                          MachineDominatorTree &MDT,
360                          MachineBlockFrequencyInfo &MBFI, VirtRegAuxInfo &VRAI)
361     : SA(SA), LIS(LIS), VRM(VRM), MRI(VRM.getMachineFunction().getRegInfo()),
362       MDT(MDT), TII(*VRM.getMachineFunction().getSubtarget().getInstrInfo()),
363       TRI(*VRM.getMachineFunction().getSubtarget().getRegisterInfo()),
364       MBFI(MBFI), VRAI(VRAI), RegAssign(Allocator) {}
365 
366 void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
367   Edit = &LRE;
368   SpillMode = SM;
369   OpenIdx = 0;
370   RegAssign.clear();
371   Values.clear();
372 
373   // Reset the LiveIntervalCalc instances needed for this spill mode.
374   LICalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
375                   &LIS.getVNInfoAllocator());
376   if (SpillMode)
377     LICalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
378                     &LIS.getVNInfoAllocator());
379 
380   Edit->anyRematerializable();
381 }
382 
383 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
384 LLVM_DUMP_METHOD void SplitEditor::dump() const {
385   if (RegAssign.empty()) {
386     dbgs() << " empty\n";
387     return;
388   }
389 
390   for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
391     dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
392   dbgs() << '\n';
393 }
394 #endif
395 
396 /// Find a subrange corresponding to the exact lane mask @p LM in the live
397 /// interval @p LI. The interval @p LI is assumed to contain such a subrange.
398 /// This function is used to find corresponding subranges between the
399 /// original interval and the new intervals.
400 template <typename T> auto &getSubrangeImpl(LaneBitmask LM, T &LI) {
401   for (auto &S : LI.subranges())
402     if (S.LaneMask == LM)
403       return S;
404   llvm_unreachable("SubRange for this mask not found");
405 }
406 
407 LiveInterval::SubRange &getSubRangeForMaskExact(LaneBitmask LM,
408                                                 LiveInterval &LI) {
409   return getSubrangeImpl(LM, LI);
410 }
411 
412 const LiveInterval::SubRange &getSubRangeForMaskExact(LaneBitmask LM,
413                                                       const LiveInterval &LI) {
414   return getSubrangeImpl(LM, LI);
415 }
416 
417 /// Find a subrange corresponding to the lane mask @p LM, or a superset of it,
418 /// in the live interval @p LI. The interval @p LI is assumed to contain such
419 /// a subrange.  This function is used to find corresponding subranges between
420 /// the original interval and the new intervals.
421 const LiveInterval::SubRange &getSubRangeForMask(LaneBitmask LM,
422                                                  const LiveInterval &LI) {
423   for (const LiveInterval::SubRange &S : LI.subranges())
424     if ((S.LaneMask & LM) == LM)
425       return S;
426   llvm_unreachable("SubRange for this mask not found");
427 }
428 
429 void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
430   if (!LI.hasSubRanges()) {
431     LI.createDeadDef(VNI);
432     return;
433   }
434 
435   SlotIndex Def = VNI->def;
436   if (Original) {
437     // If we are transferring a def from the original interval, make sure
438     // to only update the subranges for which the original subranges had
439     // a def at this location.
440     for (LiveInterval::SubRange &S : LI.subranges()) {
441       auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
442       VNInfo *PV = PS.getVNInfoAt(Def);
443       if (PV != nullptr && PV->def == Def)
444         S.createDeadDef(Def, LIS.getVNInfoAllocator());
445     }
446   } else {
447     // This is a new def: either from rematerialization, or from an inserted
448     // copy. Since rematerialization can regenerate a definition of a sub-
449     // register, we need to check which subranges need to be updated.
450     const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
451     assert(DefMI != nullptr);
452     LaneBitmask LM;
453     for (const MachineOperand &DefOp : DefMI->defs()) {
454       Register R = DefOp.getReg();
455       if (R != LI.reg())
456         continue;
457       if (unsigned SR = DefOp.getSubReg())
458         LM |= TRI.getSubRegIndexLaneMask(SR);
459       else {
460         LM = MRI.getMaxLaneMaskForVReg(R);
461         break;
462       }
463     }
464     for (LiveInterval::SubRange &S : LI.subranges())
465       if ((S.LaneMask & LM).any())
466         S.createDeadDef(Def, LIS.getVNInfoAllocator());
467   }
468 }
469 
470 VNInfo *SplitEditor::defValue(unsigned RegIdx,
471                               const VNInfo *ParentVNI,
472                               SlotIndex Idx,
473                               bool Original) {
474   assert(ParentVNI && "Mapping  NULL value");
475   assert(Idx.isValid() && "Invalid SlotIndex");
476   assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
477   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
478 
479   // Create a new value.
480   VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
481 
482   bool Force = LI->hasSubRanges();
483   ValueForcePair FP(Force ? nullptr : VNI, Force);
484   // Use insert for lookup, so we can add missing values with a second lookup.
485   std::pair<ValueMap::iterator, bool> InsP =
486     Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
487 
488   // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
489   // forced. Keep it as a simple def without any liveness.
490   if (!Force && InsP.second)
491     return VNI;
492 
493   // If the previous value was a simple mapping, add liveness for it now.
494   if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
495     addDeadDef(*LI, OldVNI, Original);
496 
497     // No longer a simple mapping.  Switch to a complex mapping. If the
498     // interval has subranges, make it a forced mapping.
499     InsP.first->second = ValueForcePair(nullptr, Force);
500   }
501 
502   // This is a complex mapping, add liveness for VNI
503   addDeadDef(*LI, VNI, Original);
504   return VNI;
505 }
506 
507 void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
508   ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI.id)];
509   VNInfo *VNI = VFP.getPointer();
510 
511   // ParentVNI was either unmapped or already complex mapped. Either way, just
512   // set the force bit.
513   if (!VNI) {
514     VFP.setInt(true);
515     return;
516   }
517 
518   // This was previously a single mapping. Make sure the old def is represented
519   // by a trivial live range.
520   addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
521 
522   // Mark as complex mapped, forced.
523   VFP = ValueForcePair(nullptr, true);
524 }
525 
526 SlotIndex SplitEditor::buildSingleSubRegCopy(
527     Register FromReg, Register ToReg, MachineBasicBlock &MBB,
528     MachineBasicBlock::iterator InsertBefore, unsigned SubIdx,
529     LiveInterval &DestLI, bool Late, SlotIndex Def, const MCInstrDesc &Desc) {
530   bool FirstCopy = !Def.isValid();
531   MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
532       .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
533               | getInternalReadRegState(!FirstCopy), SubIdx)
534       .addReg(FromReg, 0, SubIdx);
535 
536   SlotIndexes &Indexes = *LIS.getSlotIndexes();
537   if (FirstCopy) {
538     Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
539   } else {
540     CopyMI->bundleWithPred();
541   }
542   return Def;
543 }
544 
545 SlotIndex SplitEditor::buildCopy(Register FromReg, Register ToReg,
546     LaneBitmask LaneMask, MachineBasicBlock &MBB,
547     MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
548   const MCInstrDesc &Desc =
549       TII.get(TII.getLiveRangeSplitOpcode(FromReg, *MBB.getParent()));
550   SlotIndexes &Indexes = *LIS.getSlotIndexes();
551   if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
552     // The full vreg is copied.
553     MachineInstr *CopyMI =
554         BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
555     return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
556   }
557 
558   // Only a subset of lanes needs to be copied. The following is a simple
559   // heuristic to construct a sequence of COPYs. We could add a target
560   // specific callback if this turns out to be suboptimal.
561   LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
562 
563   // First pass: Try to find a perfectly matching subregister index. If none
564   // exists find the one covering the most lanemask bits.
565   const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
566   assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
567 
568   SmallVector<unsigned, 8> SubIndexes;
569 
570   // Abort if we cannot possibly implement the COPY with the given indexes.
571   if (!TRI.getCoveringSubRegIndexes(RC, LaneMask, SubIndexes))
572     report_fatal_error("Impossible to implement partial COPY");
573 
574   SlotIndex Def;
575   for (unsigned BestIdx : SubIndexes) {
576     Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
577                                 DestLI, Late, Def, Desc);
578   }
579 
580   BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
581   DestLI.refineSubRanges(
582       Allocator, LaneMask,
583       [Def, &Allocator](LiveInterval::SubRange &SR) {
584         SR.createDeadDef(Def, Allocator);
585       },
586       Indexes, TRI);
587 
588   return Def;
589 }
590 
591 bool SplitEditor::rematWillIncreaseRestriction(const MachineInstr *DefMI,
592                                                MachineBasicBlock &MBB,
593                                                SlotIndex UseIdx) const {
594   const MachineInstr *UseMI = LIS.getInstructionFromIndex(UseIdx);
595   if (!UseMI)
596     return false;
597 
598   // Currently code assumes rematerialization only happens for a def at 0.
599   const unsigned DefOperandIdx = 0;
600   // We want to compute the static register class constraint for the instruction
601   // def. If it is a smaller subclass than getLargestLegalSuperClass at the use
602   // site, then rematerializing it will increase the constraints.
603   const TargetRegisterClass *DefConstrainRC =
604       DefMI->getRegClassConstraint(DefOperandIdx, &TII, &TRI);
605   if (!DefConstrainRC)
606     return false;
607 
608   const TargetRegisterClass *RC = MRI.getRegClass(Edit->getReg());
609 
610   // We want to find the register class that can be inflated to after the split
611   // occurs in recomputeRegClass
612   const TargetRegisterClass *SuperRC =
613       TRI.getLargestLegalSuperClass(RC, *MBB.getParent());
614 
615   Register DefReg = DefMI->getOperand(DefOperandIdx).getReg();
616   const TargetRegisterClass *UseConstrainRC =
617       UseMI->getRegClassConstraintEffectForVReg(DefReg, SuperRC, &TII, &TRI,
618                                                 /*ExploreBundle=*/true);
619   return UseConstrainRC->hasSubClass(DefConstrainRC);
620 }
621 
622 VNInfo *SplitEditor::defFromParent(unsigned RegIdx, const VNInfo *ParentVNI,
623                                    SlotIndex UseIdx, MachineBasicBlock &MBB,
624                                    MachineBasicBlock::iterator I) {
625   LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
626 
627   // We may be trying to avoid interference that ends at a deleted instruction,
628   // so always begin RegIdx 0 early and all others late.
629   bool Late = RegIdx != 0;
630 
631   // Attempt cheap-as-a-copy rematerialization.
632   Register Original = VRM.getOriginal(Edit->get(RegIdx));
633   LiveInterval &OrigLI = LIS.getInterval(Original);
634   VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
635 
636   Register Reg = LI->reg();
637   if (OrigVNI) {
638     LiveRangeEdit::Remat RM(ParentVNI);
639     RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
640     if (RM.OrigMI && TII.isAsCheapAsAMove(*RM.OrigMI) &&
641         Edit->canRematerializeAt(RM, OrigVNI, UseIdx)) {
642       if (!rematWillIncreaseRestriction(RM.OrigMI, MBB, UseIdx)) {
643         SlotIndex Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
644         ++NumRemats;
645         // Define the value in Reg.
646         return defValue(RegIdx, ParentVNI, Def, false);
647       }
648       LLVM_DEBUG(
649           dbgs() << "skipping rematerialize of " << printReg(Reg) << " at "
650                  << UseIdx
651                  << " since it will increase register class restrictions\n");
652     }
653   }
654 
655   LaneBitmask LaneMask;
656   if (OrigLI.hasSubRanges()) {
657     LaneMask = LaneBitmask::getNone();
658     for (LiveInterval::SubRange &S : OrigLI.subranges()) {
659       if (S.liveAt(UseIdx))
660         LaneMask |= S.LaneMask;
661     }
662   } else {
663     LaneMask = LaneBitmask::getAll();
664   }
665 
666   SlotIndex Def;
667   if (LaneMask.none()) {
668     const MCInstrDesc &Desc = TII.get(TargetOpcode::IMPLICIT_DEF);
669     MachineInstr *ImplicitDef = BuildMI(MBB, I, DebugLoc(), Desc, Reg);
670     SlotIndexes &Indexes = *LIS.getSlotIndexes();
671     Def = Indexes.insertMachineInstrInMaps(*ImplicitDef, Late).getRegSlot();
672   } else {
673     ++NumCopies;
674     Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
675   }
676 
677   // Define the value in Reg.
678   return defValue(RegIdx, ParentVNI, Def, false);
679 }
680 
681 /// Create a new virtual register and live interval.
682 unsigned SplitEditor::openIntv() {
683   // Create the complement as index 0.
684   if (Edit->empty())
685     Edit->createEmptyInterval();
686 
687   // Create the open interval.
688   OpenIdx = Edit->size();
689   Edit->createEmptyInterval();
690   return OpenIdx;
691 }
692 
693 void SplitEditor::selectIntv(unsigned Idx) {
694   assert(Idx != 0 && "Cannot select the complement interval");
695   assert(Idx < Edit->size() && "Can only select previously opened interval");
696   LLVM_DEBUG(dbgs() << "    selectIntv " << OpenIdx << " -> " << Idx << '\n');
697   OpenIdx = Idx;
698 }
699 
700 SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
701   assert(OpenIdx && "openIntv not called before enterIntvBefore");
702   LLVM_DEBUG(dbgs() << "    enterIntvBefore " << Idx);
703   Idx = Idx.getBaseIndex();
704   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
705   if (!ParentVNI) {
706     LLVM_DEBUG(dbgs() << ": not live\n");
707     return Idx;
708   }
709   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
710   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
711   assert(MI && "enterIntvBefore called with invalid index");
712 
713   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
714   return VNI->def;
715 }
716 
717 SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
718   assert(OpenIdx && "openIntv not called before enterIntvAfter");
719   LLVM_DEBUG(dbgs() << "    enterIntvAfter " << Idx);
720   Idx = Idx.getBoundaryIndex();
721   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
722   if (!ParentVNI) {
723     LLVM_DEBUG(dbgs() << ": not live\n");
724     return Idx;
725   }
726   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
727   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
728   assert(MI && "enterIntvAfter called with invalid index");
729 
730   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
731                               std::next(MachineBasicBlock::iterator(MI)));
732   return VNI->def;
733 }
734 
735 SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
736   assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
737   SlotIndex End = LIS.getMBBEndIdx(&MBB);
738   SlotIndex Last = End.getPrevSlot();
739   LLVM_DEBUG(dbgs() << "    enterIntvAtEnd " << printMBBReference(MBB) << ", "
740                     << Last);
741   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
742   if (!ParentVNI) {
743     LLVM_DEBUG(dbgs() << ": not live\n");
744     return End;
745   }
746   SlotIndex LSP = SA.getLastSplitPoint(&MBB);
747   if (LSP < Last) {
748     // It could be that the use after LSP is a def, and thus the ParentVNI
749     // just selected starts at that def.  For this case to exist, the def
750     // must be part of a tied def/use pair (as otherwise we'd have split
751     // distinct live ranges into individual live intervals), and thus we
752     // can insert the def into the VNI of the use and the tied def/use
753     // pair can live in the resulting interval.
754     Last = LSP;
755     ParentVNI = Edit->getParent().getVNInfoAt(Last);
756     if (!ParentVNI) {
757       // undef use --> undef tied def
758       LLVM_DEBUG(dbgs() << ": tied use not live\n");
759       return End;
760     }
761   }
762 
763   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id);
764   VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
765                               SA.getLastSplitPointIter(&MBB));
766   RegAssign.insert(VNI->def, End, OpenIdx);
767   LLVM_DEBUG(dump());
768   return VNI->def;
769 }
770 
771 /// useIntv - indicate that all instructions in MBB should use OpenLI.
772 void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
773   useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
774 }
775 
776 void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
777   assert(OpenIdx && "openIntv not called before useIntv");
778   LLVM_DEBUG(dbgs() << "    useIntv [" << Start << ';' << End << "):");
779   RegAssign.insert(Start, End, OpenIdx);
780   LLVM_DEBUG(dump());
781 }
782 
783 SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
784   assert(OpenIdx && "openIntv not called before leaveIntvAfter");
785   LLVM_DEBUG(dbgs() << "    leaveIntvAfter " << Idx);
786 
787   // The interval must be live beyond the instruction at Idx.
788   SlotIndex Boundary = Idx.getBoundaryIndex();
789   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
790   if (!ParentVNI) {
791     LLVM_DEBUG(dbgs() << ": not live\n");
792     return Boundary.getNextSlot();
793   }
794   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
795   MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
796   assert(MI && "No instruction at index");
797 
798   // In spill mode, make live ranges as short as possible by inserting the copy
799   // before MI.  This is only possible if that instruction doesn't redefine the
800   // value.  The inserted COPY is not a kill, and we don't need to recompute
801   // the source live range.  The spiller also won't try to hoist this copy.
802   if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
803       MI->readsVirtualRegister(Edit->getReg())) {
804     forceRecompute(0, *ParentVNI);
805     defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
806     return Idx;
807   }
808 
809   VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
810                               std::next(MachineBasicBlock::iterator(MI)));
811   return VNI->def;
812 }
813 
814 SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
815   assert(OpenIdx && "openIntv not called before leaveIntvBefore");
816   LLVM_DEBUG(dbgs() << "    leaveIntvBefore " << Idx);
817 
818   // The interval must be live into the instruction at Idx.
819   Idx = Idx.getBaseIndex();
820   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
821   if (!ParentVNI) {
822     LLVM_DEBUG(dbgs() << ": not live\n");
823     return Idx.getNextSlot();
824   }
825   LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
826 
827   MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
828   assert(MI && "No instruction at index");
829   VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
830   return VNI->def;
831 }
832 
833 SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
834   assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
835   SlotIndex Start = LIS.getMBBStartIdx(&MBB);
836   LLVM_DEBUG(dbgs() << "    leaveIntvAtTop " << printMBBReference(MBB) << ", "
837                     << Start);
838 
839   VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
840   if (!ParentVNI) {
841     LLVM_DEBUG(dbgs() << ": not live\n");
842     return Start;
843   }
844 
845   unsigned RegIdx = 0;
846   Register Reg = LIS.getInterval(Edit->get(RegIdx)).reg();
847   VNInfo *VNI = defFromParent(RegIdx, ParentVNI, Start, MBB,
848                               MBB.SkipPHIsLabelsAndDebug(MBB.begin(), Reg));
849   RegAssign.insert(Start, VNI->def, OpenIdx);
850   LLVM_DEBUG(dump());
851   return VNI->def;
852 }
853 
854 static bool hasTiedUseOf(MachineInstr &MI, Register Reg) {
855   return any_of(MI.defs(), [Reg](const MachineOperand &MO) {
856     return MO.isReg() && MO.isTied() && MO.getReg() == Reg;
857   });
858 }
859 
860 void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
861   assert(OpenIdx && "openIntv not called before overlapIntv");
862   const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
863   assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
864          "Parent changes value in extended range");
865   assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
866          "Range cannot span basic blocks");
867 
868   // The complement interval will be extended as needed by LICalc.extend().
869   if (ParentVNI)
870     forceRecompute(0, *ParentVNI);
871 
872   // If the last use is tied to a def, we can't mark it as live for the
873   // interval which includes only the use.  That would cause the tied pair
874   // to end up in two different intervals.
875   if (auto *MI = LIS.getInstructionFromIndex(End))
876     if (hasTiedUseOf(*MI, Edit->getReg())) {
877       LLVM_DEBUG(dbgs() << "skip overlap due to tied def at end\n");
878       return;
879     }
880 
881   LLVM_DEBUG(dbgs() << "    overlapIntv [" << Start << ';' << End << "):");
882   RegAssign.insert(Start, End, OpenIdx);
883   LLVM_DEBUG(dump());
884 }
885 
886 //===----------------------------------------------------------------------===//
887 //                                  Spill modes
888 //===----------------------------------------------------------------------===//
889 
890 void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
891   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
892   LLVM_DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
893   RegAssignMap::iterator AssignI;
894   AssignI.setMap(RegAssign);
895 
896   for (const VNInfo *C : Copies) {
897     SlotIndex Def = C->def;
898     MachineInstr *MI = LIS.getInstructionFromIndex(Def);
899     assert(MI && "No instruction for back-copy");
900 
901     MachineBasicBlock *MBB = MI->getParent();
902     MachineBasicBlock::iterator MBBI(MI);
903     bool AtBegin;
904     do AtBegin = MBBI == MBB->begin();
905     while (!AtBegin && (--MBBI)->isDebugOrPseudoInstr());
906 
907     LLVM_DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
908     LIS.removeVRegDefAt(*LI, Def);
909     LIS.RemoveMachineInstrFromMaps(*MI);
910     MI->eraseFromParent();
911 
912     // Adjust RegAssign if a register assignment is killed at Def. We want to
913     // avoid calculating the live range of the source register if possible.
914     AssignI.find(Def.getPrevSlot());
915     if (!AssignI.valid() || AssignI.start() >= Def)
916       continue;
917     // If MI doesn't kill the assigned register, just leave it.
918     if (AssignI.stop() != Def)
919       continue;
920     unsigned RegIdx = AssignI.value();
921     // We could hoist back-copy right after another back-copy. As a result
922     // MMBI points to copy instruction which is actually dead now.
923     // We cannot set its stop to MBBI which will be the same as start and
924     // interval does not support that.
925     SlotIndex Kill =
926         AtBegin ? SlotIndex() : LIS.getInstructionIndex(*MBBI).getRegSlot();
927     if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg()) ||
928         Kill <= AssignI.start()) {
929       LLVM_DEBUG(dbgs() << "  cannot find simple kill of RegIdx " << RegIdx
930                         << '\n');
931       forceRecompute(RegIdx, *Edit->getParent().getVNInfoAt(Def));
932     } else {
933       LLVM_DEBUG(dbgs() << "  move kill to " << Kill << '\t' << *MBBI);
934       AssignI.setStop(Kill);
935     }
936   }
937 }
938 
939 MachineBasicBlock*
940 SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
941                                   MachineBasicBlock *DefMBB) {
942   if (MBB == DefMBB)
943     return MBB;
944   assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
945 
946   const MachineLoopInfo &Loops = SA.Loops;
947   const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
948   MachineDomTreeNode *DefDomNode = MDT[DefMBB];
949 
950   // Best candidate so far.
951   MachineBasicBlock *BestMBB = MBB;
952   unsigned BestDepth = std::numeric_limits<unsigned>::max();
953 
954   while (true) {
955     const MachineLoop *Loop = Loops.getLoopFor(MBB);
956 
957     // MBB isn't in a loop, it doesn't get any better.  All dominators have a
958     // higher frequency by definition.
959     if (!Loop) {
960       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
961                         << " dominates " << printMBBReference(*MBB)
962                         << " at depth 0\n");
963       return MBB;
964     }
965 
966     // We'll never be able to exit the DefLoop.
967     if (Loop == DefLoop) {
968       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
969                         << " dominates " << printMBBReference(*MBB)
970                         << " in the same loop\n");
971       return MBB;
972     }
973 
974     // Least busy dominator seen so far.
975     unsigned Depth = Loop->getLoopDepth();
976     if (Depth < BestDepth) {
977       BestMBB = MBB;
978       BestDepth = Depth;
979       LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
980                         << " dominates " << printMBBReference(*MBB)
981                         << " at depth " << Depth << '\n');
982     }
983 
984     // Leave loop by going to the immediate dominator of the loop header.
985     // This is a bigger stride than simply walking up the dominator tree.
986     MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
987 
988     // Too far up the dominator tree?
989     if (!IDom || !MDT.dominates(DefDomNode, IDom))
990       return BestMBB;
991 
992     MBB = IDom->getBlock();
993   }
994 }
995 
996 void SplitEditor::computeRedundantBackCopies(
997     DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
998   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
999   const LiveInterval *Parent = &Edit->getParent();
1000   SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
1001   SmallPtrSet<VNInfo *, 8> DominatedVNIs;
1002 
1003   // Aggregate VNIs having the same value as ParentVNI.
1004   for (VNInfo *VNI : LI->valnos) {
1005     if (VNI->isUnused())
1006       continue;
1007     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
1008     EqualVNs[ParentVNI->id].insert(VNI);
1009   }
1010 
1011   // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
1012   // redundant VNIs to BackCopies.
1013   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
1014     const VNInfo *ParentVNI = Parent->getValNumInfo(i);
1015     if (!NotToHoistSet.count(ParentVNI->id))
1016       continue;
1017     SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
1018     SmallPtrSetIterator<VNInfo *> It2 = It1;
1019     for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
1020       It2 = It1;
1021       for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
1022         if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
1023           continue;
1024 
1025         MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
1026         MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
1027         if (MBB1 == MBB2) {
1028           DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
1029         } else if (MDT.dominates(MBB1, MBB2)) {
1030           DominatedVNIs.insert(*It2);
1031         } else if (MDT.dominates(MBB2, MBB1)) {
1032           DominatedVNIs.insert(*It1);
1033         }
1034       }
1035     }
1036     if (!DominatedVNIs.empty()) {
1037       forceRecompute(0, *ParentVNI);
1038       append_range(BackCopies, DominatedVNIs);
1039       DominatedVNIs.clear();
1040     }
1041   }
1042 }
1043 
1044 /// For SM_Size mode, find a common dominator for all the back-copies for
1045 /// the same ParentVNI and hoist the backcopies to the dominator BB.
1046 /// For SM_Speed mode, if the common dominator is hot and it is not beneficial
1047 /// to do the hoisting, simply remove the dominated backcopies for the same
1048 /// ParentVNI.
1049 void SplitEditor::hoistCopies() {
1050   // Get the complement interval, always RegIdx 0.
1051   LiveInterval *LI = &LIS.getInterval(Edit->get(0));
1052   const LiveInterval *Parent = &Edit->getParent();
1053 
1054   // Track the nearest common dominator for all back-copies for each ParentVNI,
1055   // indexed by ParentVNI->id.
1056   using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
1057   SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
1058   // The total cost of all the back-copies for each ParentVNI.
1059   SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
1060   // The ParentVNI->id set for which hoisting back-copies are not beneficial
1061   // for Speed.
1062   DenseSet<unsigned> NotToHoistSet;
1063 
1064   // Find the nearest common dominator for parent values with multiple
1065   // back-copies.  If a single back-copy dominates, put it in DomPair.second.
1066   for (VNInfo *VNI : LI->valnos) {
1067     if (VNI->isUnused())
1068       continue;
1069     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
1070     assert(ParentVNI && "Parent not live at complement def");
1071 
1072     // Don't hoist remats.  The complement is probably going to disappear
1073     // completely anyway.
1074     if (Edit->didRematerialize(ParentVNI))
1075       continue;
1076 
1077     MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
1078 
1079     DomPair &Dom = NearestDom[ParentVNI->id];
1080 
1081     // Keep directly defined parent values.  This is either a PHI or an
1082     // instruction in the complement range.  All other copies of ParentVNI
1083     // should be eliminated.
1084     if (VNI->def == ParentVNI->def) {
1085       LLVM_DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
1086       Dom = DomPair(ValMBB, VNI->def);
1087       continue;
1088     }
1089     // Skip the singly mapped values.  There is nothing to gain from hoisting a
1090     // single back-copy.
1091     if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
1092       LLVM_DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
1093       continue;
1094     }
1095 
1096     if (!Dom.first) {
1097       // First time we see ParentVNI.  VNI dominates itself.
1098       Dom = DomPair(ValMBB, VNI->def);
1099     } else if (Dom.first == ValMBB) {
1100       // Two defs in the same block.  Pick the earlier def.
1101       if (!Dom.second.isValid() || VNI->def < Dom.second)
1102         Dom.second = VNI->def;
1103     } else {
1104       // Different basic blocks. Check if one dominates.
1105       MachineBasicBlock *Near =
1106         MDT.findNearestCommonDominator(Dom.first, ValMBB);
1107       if (Near == ValMBB)
1108         // Def ValMBB dominates.
1109         Dom = DomPair(ValMBB, VNI->def);
1110       else if (Near != Dom.first)
1111         // None dominate. Hoist to common dominator, need new def.
1112         Dom = DomPair(Near, SlotIndex());
1113       Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
1114     }
1115 
1116     LLVM_DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@'
1117                       << VNI->def << " for parent " << ParentVNI->id << '@'
1118                       << ParentVNI->def << " hoist to "
1119                       << printMBBReference(*Dom.first) << ' ' << Dom.second
1120                       << '\n');
1121   }
1122 
1123   // Insert the hoisted copies.
1124   for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
1125     DomPair &Dom = NearestDom[i];
1126     if (!Dom.first || Dom.second.isValid())
1127       continue;
1128     // This value needs a hoisted copy inserted at the end of Dom.first.
1129     const VNInfo *ParentVNI = Parent->getValNumInfo(i);
1130     MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
1131     // Get a less loopy dominator than Dom.first.
1132     Dom.first = findShallowDominator(Dom.first, DefMBB);
1133     if (SpillMode == SM_Speed &&
1134         MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
1135       NotToHoistSet.insert(ParentVNI->id);
1136       continue;
1137     }
1138     SlotIndex LSP = SA.getLastSplitPoint(Dom.first);
1139     if (LSP <= ParentVNI->def) {
1140       NotToHoistSet.insert(ParentVNI->id);
1141       continue;
1142     }
1143     Dom.second = defFromParent(0, ParentVNI, LSP, *Dom.first,
1144                                SA.getLastSplitPointIter(Dom.first))->def;
1145   }
1146 
1147   // Remove redundant back-copies that are now known to be dominated by another
1148   // def with the same value.
1149   SmallVector<VNInfo*, 8> BackCopies;
1150   for (VNInfo *VNI : LI->valnos) {
1151     if (VNI->isUnused())
1152       continue;
1153     VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
1154     const DomPair &Dom = NearestDom[ParentVNI->id];
1155     if (!Dom.first || Dom.second == VNI->def ||
1156         NotToHoistSet.count(ParentVNI->id))
1157       continue;
1158     BackCopies.push_back(VNI);
1159     forceRecompute(0, *ParentVNI);
1160   }
1161 
1162   // If it is not beneficial to hoist all the BackCopies, simply remove
1163   // redundant BackCopies in speed mode.
1164   if (SpillMode == SM_Speed && !NotToHoistSet.empty())
1165     computeRedundantBackCopies(NotToHoistSet, BackCopies);
1166 
1167   removeBackCopies(BackCopies);
1168 }
1169 
1170 /// transferValues - Transfer all possible values to the new live ranges.
1171 /// Values that were rematerialized are left alone, they need LICalc.extend().
1172 bool SplitEditor::transferValues() {
1173   bool Skipped = false;
1174   RegAssignMap::const_iterator AssignI = RegAssign.begin();
1175   for (const LiveRange::Segment &S : Edit->getParent()) {
1176     LLVM_DEBUG(dbgs() << "  blit " << S << ':');
1177     VNInfo *ParentVNI = S.valno;
1178     // RegAssign has holes where RegIdx 0 should be used.
1179     SlotIndex Start = S.start;
1180     AssignI.advanceTo(Start);
1181     do {
1182       unsigned RegIdx;
1183       SlotIndex End = S.end;
1184       if (!AssignI.valid()) {
1185         RegIdx = 0;
1186       } else if (AssignI.start() <= Start) {
1187         RegIdx = AssignI.value();
1188         if (AssignI.stop() < End) {
1189           End = AssignI.stop();
1190           ++AssignI;
1191         }
1192       } else {
1193         RegIdx = 0;
1194         End = std::min(End, AssignI.start());
1195       }
1196 
1197       // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
1198       LLVM_DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx << '('
1199                         << printReg(Edit->get(RegIdx)) << ')');
1200       LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1201 
1202       // Check for a simply defined value that can be blitted directly.
1203       ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
1204       if (VNInfo *VNI = VFP.getPointer()) {
1205         LLVM_DEBUG(dbgs() << ':' << VNI->id);
1206         LI.addSegment(LiveInterval::Segment(Start, End, VNI));
1207         Start = End;
1208         continue;
1209       }
1210 
1211       // Skip values with forced recomputation.
1212       if (VFP.getInt()) {
1213         LLVM_DEBUG(dbgs() << "(recalc)");
1214         Skipped = true;
1215         Start = End;
1216         continue;
1217       }
1218 
1219       LiveIntervalCalc &LIC = getLICalc(RegIdx);
1220 
1221       // This value has multiple defs in RegIdx, but it wasn't rematerialized,
1222       // so the live range is accurate. Add live-in blocks in [Start;End) to the
1223       // LiveInBlocks.
1224       MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1225       SlotIndex BlockStart, BlockEnd;
1226       std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
1227 
1228       // The first block may be live-in, or it may have its own def.
1229       if (Start != BlockStart) {
1230         VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
1231         assert(VNI && "Missing def for complex mapped value");
1232         LLVM_DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
1233         // MBB has its own def. Is it also live-out?
1234         if (BlockEnd <= End)
1235           LIC.setLiveOutValue(&*MBB, VNI);
1236 
1237         // Skip to the next block for live-in.
1238         ++MBB;
1239         BlockStart = BlockEnd;
1240       }
1241 
1242       // Handle the live-in blocks covered by [Start;End).
1243       assert(Start <= BlockStart && "Expected live-in block");
1244       while (BlockStart < End) {
1245         LLVM_DEBUG(dbgs() << ">" << printMBBReference(*MBB));
1246         BlockEnd = LIS.getMBBEndIdx(&*MBB);
1247         if (BlockStart == ParentVNI->def) {
1248           // This block has the def of a parent PHI, so it isn't live-in.
1249           assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
1250           VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
1251           assert(VNI && "Missing def for complex mapped parent PHI");
1252           if (End >= BlockEnd)
1253             LIC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
1254         } else {
1255           // This block needs a live-in value.  The last block covered may not
1256           // be live-out.
1257           if (End < BlockEnd)
1258             LIC.addLiveInBlock(LI, MDT[&*MBB], End);
1259           else {
1260             // Live-through, and we don't know the value.
1261             LIC.addLiveInBlock(LI, MDT[&*MBB]);
1262             LIC.setLiveOutValue(&*MBB, nullptr);
1263           }
1264         }
1265         BlockStart = BlockEnd;
1266         ++MBB;
1267       }
1268       Start = End;
1269     } while (Start != S.end);
1270     LLVM_DEBUG(dbgs() << '\n');
1271   }
1272 
1273   LICalc[0].calculateValues();
1274   if (SpillMode)
1275     LICalc[1].calculateValues();
1276 
1277   return Skipped;
1278 }
1279 
1280 static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
1281   const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
1282   if (Seg == nullptr)
1283     return true;
1284   if (Seg->end != Def.getDeadSlot())
1285     return false;
1286   // This is a dead PHI. Remove it.
1287   LR.removeSegment(*Seg, true);
1288   return true;
1289 }
1290 
1291 void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveIntervalCalc &LIC,
1292                                  LiveRange &LR, LaneBitmask LM,
1293                                  ArrayRef<SlotIndex> Undefs) {
1294   for (MachineBasicBlock *P : B.predecessors()) {
1295     SlotIndex End = LIS.getMBBEndIdx(P);
1296     SlotIndex LastUse = End.getPrevSlot();
1297     // The predecessor may not have a live-out value. That is OK, like an
1298     // undef PHI operand.
1299     const LiveInterval &PLI = Edit->getParent();
1300     // Need the cast because the inputs to ?: would otherwise be deemed
1301     // "incompatible": SubRange vs LiveInterval.
1302     const LiveRange &PSR = !LM.all() ? getSubRangeForMaskExact(LM, PLI)
1303                                      : static_cast<const LiveRange &>(PLI);
1304     if (PSR.liveAt(LastUse))
1305       LIC.extend(LR, End, /*PhysReg=*/0, Undefs);
1306   }
1307 }
1308 
1309 void SplitEditor::extendPHIKillRanges() {
1310   // Extend live ranges to be live-out for successor PHI values.
1311 
1312   // Visit each PHI def slot in the parent live interval. If the def is dead,
1313   // remove it. Otherwise, extend the live interval to reach the end indexes
1314   // of all predecessor blocks.
1315 
1316   const LiveInterval &ParentLI = Edit->getParent();
1317   for (const VNInfo *V : ParentLI.valnos) {
1318     if (V->isUnused() || !V->isPHIDef())
1319       continue;
1320 
1321     unsigned RegIdx = RegAssign.lookup(V->def);
1322     LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1323     LiveIntervalCalc &LIC = getLICalc(RegIdx);
1324     MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1325     if (!removeDeadSegment(V->def, LI))
1326       extendPHIRange(B, LIC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
1327   }
1328 
1329   SmallVector<SlotIndex, 4> Undefs;
1330   LiveIntervalCalc SubLIC;
1331 
1332   for (const LiveInterval::SubRange &PS : ParentLI.subranges()) {
1333     for (const VNInfo *V : PS.valnos) {
1334       if (V->isUnused() || !V->isPHIDef())
1335         continue;
1336       unsigned RegIdx = RegAssign.lookup(V->def);
1337       LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1338       LiveInterval::SubRange &S = getSubRangeForMaskExact(PS.LaneMask, LI);
1339       if (removeDeadSegment(V->def, S))
1340         continue;
1341 
1342       MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1343       SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1344                    &LIS.getVNInfoAllocator());
1345       Undefs.clear();
1346       LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
1347       extendPHIRange(B, SubLIC, S, PS.LaneMask, Undefs);
1348     }
1349   }
1350 }
1351 
1352 /// rewriteAssigned - Rewrite all uses of Edit->getReg().
1353 void SplitEditor::rewriteAssigned(bool ExtendRanges) {
1354   struct ExtPoint {
1355     ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
1356       : MO(O), RegIdx(R), Next(N) {}
1357 
1358     MachineOperand MO;
1359     unsigned RegIdx;
1360     SlotIndex Next;
1361   };
1362 
1363   SmallVector<ExtPoint,4> ExtPoints;
1364 
1365   for (MachineOperand &MO :
1366        llvm::make_early_inc_range(MRI.reg_operands(Edit->getReg()))) {
1367     MachineInstr *MI = MO.getParent();
1368     // LiveDebugVariables should have handled all DBG_VALUE instructions.
1369     if (MI->isDebugValue()) {
1370       LLVM_DEBUG(dbgs() << "Zapping " << *MI);
1371       MO.setReg(0);
1372       continue;
1373     }
1374 
1375     // <undef> operands don't really read the register, so it doesn't matter
1376     // which register we choose.  When the use operand is tied to a def, we must
1377     // use the same register as the def, so just do that always.
1378     SlotIndex Idx = LIS.getInstructionIndex(*MI);
1379     if (MO.isDef() || MO.isUndef())
1380       Idx = Idx.getRegSlot(MO.isEarlyClobber());
1381 
1382     // Rewrite to the mapped register at Idx.
1383     unsigned RegIdx = RegAssign.lookup(Idx);
1384     LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1385     MO.setReg(LI.reg());
1386     LLVM_DEBUG(dbgs() << "  rewr " << printMBBReference(*MI->getParent())
1387                       << '\t' << Idx << ':' << RegIdx << '\t' << *MI);
1388 
1389     // Extend liveness to Idx if the instruction reads reg.
1390     if (!ExtendRanges || MO.isUndef())
1391       continue;
1392 
1393     // Skip instructions that don't read Reg.
1394     if (MO.isDef()) {
1395       if (!MO.getSubReg() && !MO.isEarlyClobber())
1396         continue;
1397       // We may want to extend a live range for a partial redef, or for a use
1398       // tied to an early clobber.
1399       if (!Edit->getParent().liveAt(Idx.getPrevSlot()))
1400         continue;
1401     } else {
1402       assert(MO.isUse());
1403       bool IsEarlyClobber = false;
1404       if (MO.isTied()) {
1405         // We want to extend a live range into `e` slot rather than `r` slot if
1406         // tied-def is early clobber, because the `e` slot already contained
1407         // in the live range of early-clobber tied-def operand, give an example
1408         // here:
1409         //  0  %0 = ...
1410         // 16  early-clobber %0 = Op %0 (tied-def 0), ...
1411         // 32  ... = Op %0
1412         // Before extend:
1413         //   %0 = [0r, 0d) [16e, 32d)
1414         // The point we want to extend is 0d to 16e not 16r in this case, but if
1415         // we use 16r here we will extend nothing because that already contained
1416         // in [16e, 32d).
1417         unsigned OpIdx = MO.getOperandNo();
1418         unsigned DefOpIdx = MI->findTiedOperandIdx(OpIdx);
1419         const MachineOperand &DefOp = MI->getOperand(DefOpIdx);
1420         IsEarlyClobber = DefOp.isEarlyClobber();
1421       }
1422 
1423       Idx = Idx.getRegSlot(IsEarlyClobber);
1424     }
1425 
1426     SlotIndex Next = Idx;
1427     if (LI.hasSubRanges()) {
1428       // We have to delay extending subranges until we have seen all operands
1429       // defining the register. This is because a <def,read-undef> operand
1430       // will create an "undef" point, and we cannot extend any subranges
1431       // until all of them have been accounted for.
1432       if (MO.isUse())
1433         ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
1434     } else {
1435       LiveIntervalCalc &LIC = getLICalc(RegIdx);
1436       LIC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
1437     }
1438   }
1439 
1440   for (ExtPoint &EP : ExtPoints) {
1441     LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
1442     assert(LI.hasSubRanges());
1443 
1444     LiveIntervalCalc SubLIC;
1445     Register Reg = EP.MO.getReg();
1446     unsigned Sub = EP.MO.getSubReg();
1447     LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
1448                               : MRI.getMaxLaneMaskForVReg(Reg);
1449     for (LiveInterval::SubRange &S : LI.subranges()) {
1450       if ((S.LaneMask & LM).none())
1451         continue;
1452       // The problem here can be that the new register may have been created
1453       // for a partially defined original register. For example:
1454       //   %0:subreg_hireg<def,read-undef> = ...
1455       //   ...
1456       //   %1 = COPY %0
1457       if (S.empty())
1458         continue;
1459       SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1460                    &LIS.getVNInfoAllocator());
1461       SmallVector<SlotIndex, 4> Undefs;
1462       LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
1463       SubLIC.extend(S, EP.Next, 0, Undefs);
1464     }
1465   }
1466 
1467   for (Register R : *Edit) {
1468     LiveInterval &LI = LIS.getInterval(R);
1469     if (!LI.hasSubRanges())
1470       continue;
1471     LI.clear();
1472     LI.removeEmptySubRanges();
1473     LIS.constructMainRangeFromSubranges(LI);
1474   }
1475 }
1476 
1477 void SplitEditor::deleteRematVictims() {
1478   SmallVector<MachineInstr*, 8> Dead;
1479   for (const Register &R : *Edit) {
1480     LiveInterval *LI = &LIS.getInterval(R);
1481     for (const LiveRange::Segment &S : LI->segments) {
1482       // Dead defs end at the dead slot.
1483       if (S.end != S.valno->def.getDeadSlot())
1484         continue;
1485       if (S.valno->isPHIDef())
1486         continue;
1487       MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
1488       assert(MI && "Missing instruction for dead def");
1489       MI->addRegisterDead(LI->reg(), &TRI);
1490 
1491       if (!MI->allDefsAreDead())
1492         continue;
1493 
1494       LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
1495       Dead.push_back(MI);
1496     }
1497   }
1498 
1499   if (Dead.empty())
1500     return;
1501 
1502   Edit->eliminateDeadDefs(Dead, {});
1503 }
1504 
1505 void SplitEditor::forceRecomputeVNI(const VNInfo &ParentVNI) {
1506   // Fast-path for common case.
1507   if (!ParentVNI.isPHIDef()) {
1508     for (unsigned I = 0, E = Edit->size(); I != E; ++I)
1509       forceRecompute(I, ParentVNI);
1510     return;
1511   }
1512 
1513   // Trace value through phis.
1514   SmallPtrSet<const VNInfo *, 8> Visited; ///< whether VNI was/is in worklist.
1515   SmallVector<const VNInfo *, 4> WorkList;
1516   Visited.insert(&ParentVNI);
1517   WorkList.push_back(&ParentVNI);
1518 
1519   const LiveInterval &ParentLI = Edit->getParent();
1520   const SlotIndexes &Indexes = *LIS.getSlotIndexes();
1521   do {
1522     const VNInfo &VNI = *WorkList.pop_back_val();
1523     for (unsigned I = 0, E = Edit->size(); I != E; ++I)
1524       forceRecompute(I, VNI);
1525     if (!VNI.isPHIDef())
1526       continue;
1527 
1528     MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(VNI.def);
1529     for (const MachineBasicBlock *Pred : MBB.predecessors()) {
1530       SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
1531       VNInfo *PredVNI = ParentLI.getVNInfoBefore(PredEnd);
1532       assert(PredVNI && "Value available in PhiVNI predecessor");
1533       if (Visited.insert(PredVNI).second)
1534         WorkList.push_back(PredVNI);
1535     }
1536   } while(!WorkList.empty());
1537 }
1538 
1539 void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
1540   ++NumFinished;
1541 
1542   // At this point, the live intervals in Edit contain VNInfos corresponding to
1543   // the inserted copies.
1544 
1545   // Add the original defs from the parent interval.
1546   for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
1547     if (ParentVNI->isUnused())
1548       continue;
1549     unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
1550     defValue(RegIdx, ParentVNI, ParentVNI->def, true);
1551 
1552     // Force rematted values to be recomputed everywhere.
1553     // The new live ranges may be truncated.
1554     if (Edit->didRematerialize(ParentVNI))
1555       forceRecomputeVNI(*ParentVNI);
1556   }
1557 
1558   // Hoist back-copies to the complement interval when in spill mode.
1559   switch (SpillMode) {
1560   case SM_Partition:
1561     // Leave all back-copies as is.
1562     break;
1563   case SM_Size:
1564   case SM_Speed:
1565     // hoistCopies will behave differently between size and speed.
1566     hoistCopies();
1567   }
1568 
1569   // Transfer the simply mapped values, check if any are skipped.
1570   bool Skipped = transferValues();
1571 
1572   // Rewrite virtual registers, possibly extending ranges.
1573   rewriteAssigned(Skipped);
1574 
1575   if (Skipped)
1576     extendPHIKillRanges();
1577   else
1578     ++NumSimple;
1579 
1580   // Delete defs that were rematted everywhere.
1581   if (Skipped)
1582     deleteRematVictims();
1583 
1584   // Get rid of unused values and set phi-kill flags.
1585   for (Register Reg : *Edit) {
1586     LiveInterval &LI = LIS.getInterval(Reg);
1587     LI.removeEmptySubRanges();
1588     LI.RenumberValues();
1589   }
1590 
1591   // Provide a reverse mapping from original indices to Edit ranges.
1592   if (LRMap) {
1593     auto Seq = llvm::seq<unsigned>(0, Edit->size());
1594     LRMap->assign(Seq.begin(), Seq.end());
1595   }
1596 
1597   // Now check if any registers were separated into multiple components.
1598   ConnectedVNInfoEqClasses ConEQ(LIS);
1599   for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
1600     // Don't use iterators, they are invalidated by create() below.
1601     Register VReg = Edit->get(i);
1602     LiveInterval &LI = LIS.getInterval(VReg);
1603     SmallVector<LiveInterval*, 8> SplitLIs;
1604     LIS.splitSeparateComponents(LI, SplitLIs);
1605     Register Original = VRM.getOriginal(VReg);
1606     for (LiveInterval *SplitLI : SplitLIs)
1607       VRM.setIsSplitFromReg(SplitLI->reg(), Original);
1608 
1609     // The new intervals all map back to i.
1610     if (LRMap)
1611       LRMap->resize(Edit->size(), i);
1612   }
1613 
1614   // Calculate spill weight and allocation hints for new intervals.
1615   Edit->calculateRegClassAndHint(VRM.getMachineFunction(), VRAI);
1616 
1617   assert(!LRMap || LRMap->size() == Edit->size());
1618 }
1619 
1620 //===----------------------------------------------------------------------===//
1621 //                            Single Block Splitting
1622 //===----------------------------------------------------------------------===//
1623 
1624 bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1625                                            bool SingleInstrs) const {
1626   // Always split for multiple instructions.
1627   if (!BI.isOneInstr())
1628     return true;
1629   // Don't split for single instructions unless explicitly requested.
1630   if (!SingleInstrs)
1631     return false;
1632   // Splitting a live-through range always makes progress.
1633   if (BI.LiveIn && BI.LiveOut)
1634     return true;
1635   // No point in isolating a copy. It has no register class constraints.
1636   MachineInstr *MI = LIS.getInstructionFromIndex(BI.FirstInstr);
1637   bool copyLike = TII.isCopyInstr(*MI) || MI->isSubregToReg();
1638   if (copyLike)
1639     return false;
1640   // Finally, don't isolate an end point that was created by earlier splits.
1641   return isOriginalEndpoint(BI.FirstInstr);
1642 }
1643 
1644 void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1645   openIntv();
1646   SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB);
1647   SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
1648     LastSplitPoint));
1649   if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1650     useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
1651   } else {
1652       // The last use is after the last valid split point.
1653     SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1654     useIntv(SegStart, SegStop);
1655     overlapIntv(SegStop, BI.LastInstr);
1656   }
1657 }
1658 
1659 //===----------------------------------------------------------------------===//
1660 //                    Global Live Range Splitting Support
1661 //===----------------------------------------------------------------------===//
1662 
1663 // These methods support a method of global live range splitting that uses a
1664 // global algorithm to decide intervals for CFG edges. They will insert split
1665 // points and color intervals in basic blocks while avoiding interference.
1666 //
1667 // Note that splitSingleBlock is also useful for blocks where both CFG edges
1668 // are on the stack.
1669 
1670 void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1671                                         unsigned IntvIn, SlotIndex LeaveBefore,
1672                                         unsigned IntvOut, SlotIndex EnterAfter){
1673   SlotIndex Start, Stop;
1674   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
1675 
1676   LLVM_DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop
1677                     << ") intf " << LeaveBefore << '-' << EnterAfter
1678                     << ", live-through " << IntvIn << " -> " << IntvOut);
1679 
1680   assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1681 
1682   assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1683   assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1684   assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1685 
1686   MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1687 
1688   if (!IntvOut) {
1689     LLVM_DEBUG(dbgs() << ", spill on entry.\n");
1690     //
1691     //        <<<<<<<<<    Possible LeaveBefore interference.
1692     //    |-----------|    Live through.
1693     //    -____________    Spill on entry.
1694     //
1695     selectIntv(IntvIn);
1696     SlotIndex Idx = leaveIntvAtTop(*MBB);
1697     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1698     (void)Idx;
1699     return;
1700   }
1701 
1702   if (!IntvIn) {
1703     LLVM_DEBUG(dbgs() << ", reload on exit.\n");
1704     //
1705     //    >>>>>>>          Possible EnterAfter interference.
1706     //    |-----------|    Live through.
1707     //    ___________--    Reload on exit.
1708     //
1709     selectIntv(IntvOut);
1710     SlotIndex Idx = enterIntvAtEnd(*MBB);
1711     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1712     (void)Idx;
1713     return;
1714   }
1715 
1716   if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1717     LLVM_DEBUG(dbgs() << ", straight through.\n");
1718     //
1719     //    |-----------|    Live through.
1720     //    -------------    Straight through, same intv, no interference.
1721     //
1722     selectIntv(IntvOut);
1723     useIntv(Start, Stop);
1724     return;
1725   }
1726 
1727   // We cannot legally insert splits after LSP.
1728   SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
1729   assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
1730 
1731   if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1732                   LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1733     LLVM_DEBUG(dbgs() << ", switch avoiding interference.\n");
1734     //
1735     //    >>>>     <<<<    Non-overlapping EnterAfter/LeaveBefore interference.
1736     //    |-----------|    Live through.
1737     //    ------=======    Switch intervals between interference.
1738     //
1739     selectIntv(IntvOut);
1740     SlotIndex Idx;
1741     if (LeaveBefore && LeaveBefore < LSP) {
1742       Idx = enterIntvBefore(LeaveBefore);
1743       useIntv(Idx, Stop);
1744     } else {
1745       Idx = enterIntvAtEnd(*MBB);
1746     }
1747     selectIntv(IntvIn);
1748     useIntv(Start, Idx);
1749     assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1750     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1751     return;
1752   }
1753 
1754   LLVM_DEBUG(dbgs() << ", create local intv for interference.\n");
1755   //
1756   //    >>><><><><<<<    Overlapping EnterAfter/LeaveBefore interference.
1757   //    |-----------|    Live through.
1758   //    ==---------==    Switch intervals before/after interference.
1759   //
1760   assert(LeaveBefore <= EnterAfter && "Missed case");
1761 
1762   selectIntv(IntvOut);
1763   SlotIndex Idx = enterIntvAfter(EnterAfter);
1764   useIntv(Idx, Stop);
1765   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1766 
1767   selectIntv(IntvIn);
1768   Idx = leaveIntvBefore(LeaveBefore);
1769   useIntv(Start, Idx);
1770   assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1771 }
1772 
1773 void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1774                                   unsigned IntvIn, SlotIndex LeaveBefore) {
1775   SlotIndex Start, Stop;
1776   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1777 
1778   LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
1779                     << Stop << "), uses " << BI.FirstInstr << '-'
1780                     << BI.LastInstr << ", reg-in " << IntvIn
1781                     << ", leave before " << LeaveBefore
1782                     << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1783 
1784   assert(IntvIn && "Must have register in");
1785   assert(BI.LiveIn && "Must be live-in");
1786   assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1787 
1788   if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
1789     LLVM_DEBUG(dbgs() << " before interference.\n");
1790     //
1791     //               <<<    Interference after kill.
1792     //     |---o---x   |    Killed in block.
1793     //     =========        Use IntvIn everywhere.
1794     //
1795     selectIntv(IntvIn);
1796     useIntv(Start, BI.LastInstr);
1797     return;
1798   }
1799 
1800   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
1801 
1802   if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
1803     //
1804     //               <<<    Possible interference after last use.
1805     //     |---o---o---|    Live-out on stack.
1806     //     =========____    Leave IntvIn after last use.
1807     //
1808     //                 <    Interference after last use.
1809     //     |---o---o--o|    Live-out on stack, late last use.
1810     //     ============     Copy to stack after LSP, overlap IntvIn.
1811     //            \_____    Stack interval is live-out.
1812     //
1813     if (BI.LastInstr < LSP) {
1814       LLVM_DEBUG(dbgs() << ", spill after last use before interference.\n");
1815       selectIntv(IntvIn);
1816       SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
1817       useIntv(Start, Idx);
1818       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1819     } else {
1820       LLVM_DEBUG(dbgs() << ", spill before last split point.\n");
1821       selectIntv(IntvIn);
1822       SlotIndex Idx = leaveIntvBefore(LSP);
1823       overlapIntv(Idx, BI.LastInstr);
1824       useIntv(Start, Idx);
1825       assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1826     }
1827     return;
1828   }
1829 
1830   // The interference is overlapping somewhere we wanted to use IntvIn. That
1831   // means we need to create a local interval that can be allocated a
1832   // different register.
1833   unsigned LocalIntv = openIntv();
1834   (void)LocalIntv;
1835   LLVM_DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1836 
1837   if (!BI.LiveOut || BI.LastInstr < LSP) {
1838     //
1839     //           <<<<<<<    Interference overlapping uses.
1840     //     |---o---o---|    Live-out on stack.
1841     //     =====----____    Leave IntvIn before interference, then spill.
1842     //
1843     SlotIndex To = leaveIntvAfter(BI.LastInstr);
1844     SlotIndex From = enterIntvBefore(LeaveBefore);
1845     useIntv(From, To);
1846     selectIntv(IntvIn);
1847     useIntv(Start, From);
1848     assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1849     return;
1850   }
1851 
1852   //           <<<<<<<    Interference overlapping uses.
1853   //     |---o---o--o|    Live-out on stack, late last use.
1854   //     =====-------     Copy to stack before LSP, overlap LocalIntv.
1855   //            \_____    Stack interval is live-out.
1856   //
1857   SlotIndex To = leaveIntvBefore(LSP);
1858   overlapIntv(To, BI.LastInstr);
1859   SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1860   useIntv(From, To);
1861   selectIntv(IntvIn);
1862   useIntv(Start, From);
1863   assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1864 }
1865 
1866 void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1867                                    unsigned IntvOut, SlotIndex EnterAfter) {
1868   SlotIndex Start, Stop;
1869   std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1870 
1871   LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
1872                     << Stop << "), uses " << BI.FirstInstr << '-'
1873                     << BI.LastInstr << ", reg-out " << IntvOut
1874                     << ", enter after " << EnterAfter
1875                     << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1876 
1877   SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
1878 
1879   assert(IntvOut && "Must have register out");
1880   assert(BI.LiveOut && "Must be live-out");
1881   assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1882 
1883   if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
1884     LLVM_DEBUG(dbgs() << " after interference.\n");
1885     //
1886     //    >>>>             Interference before def.
1887     //    |   o---o---|    Defined in block.
1888     //        =========    Use IntvOut everywhere.
1889     //
1890     selectIntv(IntvOut);
1891     useIntv(BI.FirstInstr, Stop);
1892     return;
1893   }
1894 
1895   if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
1896     LLVM_DEBUG(dbgs() << ", reload after interference.\n");
1897     //
1898     //    >>>>             Interference before def.
1899     //    |---o---o---|    Live-through, stack-in.
1900     //    ____=========    Enter IntvOut before first use.
1901     //
1902     selectIntv(IntvOut);
1903     SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
1904     useIntv(Idx, Stop);
1905     assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1906     return;
1907   }
1908 
1909   // The interference is overlapping somewhere we wanted to use IntvOut. That
1910   // means we need to create a local interval that can be allocated a
1911   // different register.
1912   LLVM_DEBUG(dbgs() << ", interference overlaps uses.\n");
1913   //
1914   //    >>>>>>>          Interference overlapping uses.
1915   //    |---o---o---|    Live-through, stack-in.
1916   //    ____---======    Create local interval for interference range.
1917   //
1918   selectIntv(IntvOut);
1919   SlotIndex Idx = enterIntvAfter(EnterAfter);
1920   useIntv(Idx, Stop);
1921   assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1922 
1923   openIntv();
1924   SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
1925   useIntv(From, Idx);
1926 }
1927 
1928 void SplitAnalysis::BlockInfo::print(raw_ostream &OS) const {
1929   OS << "{" << printMBBReference(*MBB) << ", "
1930      << "uses " << FirstInstr << " to " << LastInstr << ", "
1931      << "1st def " << FirstDef << ", "
1932      << (LiveIn ? "live in" : "dead in") << ", "
1933      << (LiveOut ? "live out" : "dead out") << "}";
1934 }
1935 
1936 void SplitAnalysis::BlockInfo::dump() const {
1937   print(dbgs());
1938   dbgs() << "\n";
1939 }
1940