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