1212793Sdim//===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
2212793Sdim//
3212793Sdim//                     The LLVM Compiler Infrastructure
4212793Sdim//
5212793Sdim// This file is distributed under the University of Illinois Open Source
6212793Sdim// License. See LICENSE.TXT for details.
7212793Sdim//
8212793Sdim//===----------------------------------------------------------------------===//
9212793Sdim//
10212793Sdim// This file contains the SplitAnalysis class as well as mutator functions for
11212793Sdim// live range splitting.
12212793Sdim//
13212793Sdim//===----------------------------------------------------------------------===//
14212793Sdim
15218893Sdim#define DEBUG_TYPE "regalloc"
16212793Sdim#include "SplitKit.h"
17221345Sdim#include "llvm/ADT/Statistic.h"
18212793Sdim#include "llvm/CodeGen/LiveIntervalAnalysis.h"
19235633Sdim#include "llvm/CodeGen/LiveRangeEdit.h"
20218893Sdim#include "llvm/CodeGen/MachineDominators.h"
21212793Sdim#include "llvm/CodeGen/MachineInstrBuilder.h"
22226890Sdim#include "llvm/CodeGen/MachineLoopInfo.h"
23212793Sdim#include "llvm/CodeGen/MachineRegisterInfo.h"
24252723Sdim#include "llvm/CodeGen/VirtRegMap.h"
25212793Sdim#include "llvm/Support/Debug.h"
26212793Sdim#include "llvm/Support/raw_ostream.h"
27212793Sdim#include "llvm/Target/TargetInstrInfo.h"
28212793Sdim#include "llvm/Target/TargetMachine.h"
29212793Sdim
30212793Sdimusing namespace llvm;
31212793Sdim
32221345SdimSTATISTIC(NumFinished, "Number of splits finished");
33221345SdimSTATISTIC(NumSimple,   "Number of splits that were simple");
34223017SdimSTATISTIC(NumCopies,   "Number of copies inserted for splitting");
35223017SdimSTATISTIC(NumRemats,   "Number of rematerialized defs for splitting");
36223017SdimSTATISTIC(NumRepairs,  "Number of invalid live ranges repaired");
37212793Sdim
38212793Sdim//===----------------------------------------------------------------------===//
39212793Sdim//                                 Split Analysis
40212793Sdim//===----------------------------------------------------------------------===//
41212793Sdim
42218893SdimSplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
43212793Sdim                             const LiveIntervals &lis,
44212793Sdim                             const MachineLoopInfo &mli)
45218893Sdim  : MF(vrm.getMachineFunction()),
46218893Sdim    VRM(vrm),
47218893Sdim    LIS(lis),
48218893Sdim    Loops(mli),
49218893Sdim    TII(*MF.getTarget().getInstrInfo()),
50221345Sdim    CurLI(0),
51221345Sdim    LastSplitPoint(MF.getNumBlockIDs()) {}
52212793Sdim
53212793Sdimvoid SplitAnalysis::clear() {
54218893Sdim  UseSlots.clear();
55221345Sdim  UseBlocks.clear();
56221345Sdim  ThroughBlocks.clear();
57218893Sdim  CurLI = 0;
58223017Sdim  DidRepairRange = false;
59212793Sdim}
60212793Sdim
61221345SdimSlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
62221345Sdim  const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
63221345Sdim  const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
64221345Sdim  std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
65235633Sdim  SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
66221345Sdim
67221345Sdim  // Compute split points on the first call. The pair is independent of the
68221345Sdim  // current live interval.
69221345Sdim  if (!LSP.first.isValid()) {
70221345Sdim    MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
71221345Sdim    if (FirstTerm == MBB->end())
72235633Sdim      LSP.first = MBBEnd;
73221345Sdim    else
74221345Sdim      LSP.first = LIS.getInstructionIndex(FirstTerm);
75221345Sdim
76221345Sdim    // If there is a landing pad successor, also find the call instruction.
77221345Sdim    if (!LPad)
78221345Sdim      return LSP.first;
79221345Sdim    // There may not be a call instruction (?) in which case we ignore LPad.
80221345Sdim    LSP.second = LSP.first;
81224145Sdim    for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
82224145Sdim         I != E;) {
83224145Sdim      --I;
84235633Sdim      if (I->isCall()) {
85221345Sdim        LSP.second = LIS.getInstructionIndex(I);
86221345Sdim        break;
87221345Sdim      }
88224145Sdim    }
89221345Sdim  }
90221345Sdim
91221345Sdim  // If CurLI is live into a landing pad successor, move the last split point
92221345Sdim  // back to the call that may throw.
93235633Sdim  if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad))
94221345Sdim    return LSP.first;
95235633Sdim
96235633Sdim  // Find the value leaving MBB.
97235633Sdim  const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
98235633Sdim  if (!VNI)
99235633Sdim    return LSP.first;
100235633Sdim
101235633Sdim  // If the value leaving MBB was defined after the call in MBB, it can't
102235633Sdim  // really be live-in to the landing pad.  This can happen if the landing pad
103235633Sdim  // has a PHI, and this register is undef on the exceptional edge.
104235633Sdim  // <rdar://problem/10664933>
105235633Sdim  if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd)
106235633Sdim    return LSP.first;
107235633Sdim
108235633Sdim  // Value is properly live-in to the landing pad.
109235633Sdim  // Only allow splits before the call.
110235633Sdim  return LSP.second;
111212793Sdim}
112212793Sdim
113235633SdimMachineBasicBlock::iterator
114235633SdimSplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
115235633Sdim  SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
116235633Sdim  if (LSP == LIS.getMBBEndIdx(MBB))
117235633Sdim    return MBB->end();
118235633Sdim  return LIS.getInstructionFromIndex(LSP);
119235633Sdim}
120235633Sdim
121218893Sdim/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
122212793Sdimvoid SplitAnalysis::analyzeUses() {
123221345Sdim  assert(UseSlots.empty() && "Call clear first");
124221345Sdim
125221345Sdim  // First get all the defs from the interval values. This provides the correct
126221345Sdim  // slots for early clobbers.
127221345Sdim  for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(),
128221345Sdim       E = CurLI->vni_end(); I != E; ++I)
129221345Sdim    if (!(*I)->isPHIDef() && !(*I)->isUnused())
130221345Sdim      UseSlots.push_back((*I)->def);
131221345Sdim
132221345Sdim  // Get use slots form the use-def chain.
133218893Sdim  const MachineRegisterInfo &MRI = MF.getRegInfo();
134221345Sdim  for (MachineRegisterInfo::use_nodbg_iterator
135221345Sdim       I = MRI.use_nodbg_begin(CurLI->reg), E = MRI.use_nodbg_end(); I != E;
136221345Sdim       ++I)
137221345Sdim    if (!I.getOperand().isUndef())
138235633Sdim      UseSlots.push_back(LIS.getInstructionIndex(&*I).getRegSlot());
139221345Sdim
140221345Sdim  array_pod_sort(UseSlots.begin(), UseSlots.end());
141221345Sdim
142221345Sdim  // Remove duplicates, keeping the smaller slot for each instruction.
143221345Sdim  // That is what we want for early clobbers.
144221345Sdim  UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
145221345Sdim                             SlotIndex::isSameInstr),
146221345Sdim                 UseSlots.end());
147221345Sdim
148221345Sdim  // Compute per-live block info.
149221345Sdim  if (!calcLiveBlockInfo()) {
150221345Sdim    // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
151224145Sdim    // I am looking at you, RegisterCoalescer!
152223017Sdim    DidRepairRange = true;
153223017Sdim    ++NumRepairs;
154221345Sdim    DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
155221345Sdim    const_cast<LiveIntervals&>(LIS)
156221345Sdim      .shrinkToUses(const_cast<LiveInterval*>(CurLI));
157221345Sdim    UseBlocks.clear();
158221345Sdim    ThroughBlocks.clear();
159221345Sdim    bool fixed = calcLiveBlockInfo();
160221345Sdim    (void)fixed;
161221345Sdim    assert(fixed && "Couldn't fix broken live interval");
162212793Sdim  }
163221345Sdim
164221345Sdim  DEBUG(dbgs() << "Analyze counted "
165221345Sdim               << UseSlots.size() << " instrs in "
166221345Sdim               << UseBlocks.size() << " blocks, through "
167221345Sdim               << NumThroughBlocks << " blocks.\n");
168212793Sdim}
169212793Sdim
170218893Sdim/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
171218893Sdim/// where CurLI is live.
172221345Sdimbool SplitAnalysis::calcLiveBlockInfo() {
173221345Sdim  ThroughBlocks.resize(MF.getNumBlockIDs());
174223017Sdim  NumThroughBlocks = NumGapBlocks = 0;
175218893Sdim  if (CurLI->empty())
176221345Sdim    return true;
177212793Sdim
178218893Sdim  LiveInterval::const_iterator LVI = CurLI->begin();
179218893Sdim  LiveInterval::const_iterator LVE = CurLI->end();
180212793Sdim
181218893Sdim  SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
182218893Sdim  UseI = UseSlots.begin();
183218893Sdim  UseE = UseSlots.end();
184212793Sdim
185218893Sdim  // Loop over basic blocks where CurLI is live.
186218893Sdim  MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
187218893Sdim  for (;;) {
188218893Sdim    BlockInfo BI;
189218893Sdim    BI.MBB = MFI;
190218893Sdim    SlotIndex Start, Stop;
191218893Sdim    tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
192212793Sdim
193223017Sdim    // If the block contains no uses, the range must be live through. At one
194224145Sdim    // point, RegisterCoalescer could create dangling ranges that ended
195223017Sdim    // mid-block.
196223017Sdim    if (UseI == UseE || *UseI >= Stop) {
197223017Sdim      ++NumThroughBlocks;
198223017Sdim      ThroughBlocks.set(BI.MBB->getNumber());
199223017Sdim      // The range shouldn't end mid-block if there are no uses. This shouldn't
200223017Sdim      // happen.
201223017Sdim      if (LVI->end < Stop)
202223017Sdim        return false;
203223017Sdim    } else {
204223017Sdim      // This block has uses. Find the first and last uses in the block.
205226890Sdim      BI.FirstInstr = *UseI;
206226890Sdim      assert(BI.FirstInstr >= Start);
207218893Sdim      do ++UseI;
208218893Sdim      while (UseI != UseE && *UseI < Stop);
209226890Sdim      BI.LastInstr = UseI[-1];
210226890Sdim      assert(BI.LastInstr < Stop);
211212793Sdim
212223017Sdim      // LVI is the first live segment overlapping MBB.
213223017Sdim      BI.LiveIn = LVI->start <= Start;
214223017Sdim
215226890Sdim      // When not live in, the first use should be a def.
216226890Sdim      if (!BI.LiveIn) {
217263509Sdim        assert(LVI->start == LVI->valno->def && "Dangling Segment start");
218226890Sdim        assert(LVI->start == BI.FirstInstr && "First instr should be a def");
219226890Sdim        BI.FirstDef = BI.FirstInstr;
220226890Sdim      }
221226890Sdim
222223017Sdim      // Look for gaps in the live range.
223223017Sdim      BI.LiveOut = true;
224223017Sdim      while (LVI->end < Stop) {
225223017Sdim        SlotIndex LastStop = LVI->end;
226223017Sdim        if (++LVI == LVE || LVI->start >= Stop) {
227223017Sdim          BI.LiveOut = false;
228226890Sdim          BI.LastInstr = LastStop;
229223017Sdim          break;
230223017Sdim        }
231226890Sdim
232223017Sdim        if (LastStop < LVI->start) {
233223017Sdim          // There is a gap in the live range. Create duplicate entries for the
234223017Sdim          // live-in snippet and the live-out snippet.
235223017Sdim          ++NumGapBlocks;
236223017Sdim
237223017Sdim          // Push the Live-in part.
238223017Sdim          BI.LiveOut = false;
239223017Sdim          UseBlocks.push_back(BI);
240226890Sdim          UseBlocks.back().LastInstr = LastStop;
241223017Sdim
242223017Sdim          // Set up BI for the live-out part.
243223017Sdim          BI.LiveIn = false;
244223017Sdim          BI.LiveOut = true;
245226890Sdim          BI.FirstInstr = BI.FirstDef = LVI->start;
246223017Sdim        }
247226890Sdim
248263509Sdim        // A Segment that starts in the middle of the block must be a def.
249263509Sdim        assert(LVI->start == LVI->valno->def && "Dangling Segment start");
250226890Sdim        if (!BI.FirstDef)
251226890Sdim          BI.FirstDef = LVI->start;
252218893Sdim      }
253212793Sdim
254221345Sdim      UseBlocks.push_back(BI);
255223017Sdim
256223017Sdim      // LVI is now at LVE or LVI->end >= Stop.
257223017Sdim      if (LVI == LVE)
258223017Sdim        break;
259221345Sdim    }
260212793Sdim
261218893Sdim    // Live segment ends exactly at Stop. Move to the next segment.
262218893Sdim    if (LVI->end == Stop && ++LVI == LVE)
263218893Sdim      break;
264218893Sdim
265218893Sdim    // Pick the next basic block.
266218893Sdim    if (LVI->start < Stop)
267218893Sdim      ++MFI;
268218893Sdim    else
269218893Sdim      MFI = LIS.getMBBFromIndex(LVI->start);
270212793Sdim  }
271223017Sdim
272223017Sdim  assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
273221345Sdim  return true;
274212793Sdim}
275212793Sdim
276221345Sdimunsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
277221345Sdim  if (cli->empty())
278221345Sdim    return 0;
279221345Sdim  LiveInterval *li = const_cast<LiveInterval*>(cli);
280221345Sdim  LiveInterval::iterator LVI = li->begin();
281221345Sdim  LiveInterval::iterator LVE = li->end();
282221345Sdim  unsigned Count = 0;
283221345Sdim
284221345Sdim  // Loop over basic blocks where li is live.
285221345Sdim  MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
286221345Sdim  SlotIndex Stop = LIS.getMBBEndIdx(MFI);
287221345Sdim  for (;;) {
288221345Sdim    ++Count;
289221345Sdim    LVI = li->advanceTo(LVI, Stop);
290221345Sdim    if (LVI == LVE)
291221345Sdim      return Count;
292221345Sdim    do {
293221345Sdim      ++MFI;
294221345Sdim      Stop = LIS.getMBBEndIdx(MFI);
295221345Sdim    } while (Stop <= LVI->start);
296221345Sdim  }
297221345Sdim}
298221345Sdim
299219077Sdimbool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
300219077Sdim  unsigned OrigReg = VRM.getOriginal(CurLI->reg);
301219077Sdim  const LiveInterval &Orig = LIS.getInterval(OrigReg);
302219077Sdim  assert(!Orig.empty() && "Splitting empty interval?");
303219077Sdim  LiveInterval::const_iterator I = Orig.find(Idx);
304219077Sdim
305219077Sdim  // Range containing Idx should begin at Idx.
306219077Sdim  if (I != Orig.end() && I->start <= Idx)
307219077Sdim    return I->start == Idx;
308219077Sdim
309219077Sdim  // Range does not contain Idx, previous must end at Idx.
310219077Sdim  return I != Orig.begin() && (--I)->end == Idx;
311219077Sdim}
312219077Sdim
313212793Sdimvoid SplitAnalysis::analyze(const LiveInterval *li) {
314212793Sdim  clear();
315218893Sdim  CurLI = li;
316212793Sdim  analyzeUses();
317212793Sdim}
318212793Sdim
319212793Sdim
320218893Sdim//===----------------------------------------------------------------------===//
321221345Sdim//                               Split Editor
322218893Sdim//===----------------------------------------------------------------------===//
323212793Sdim
324221345Sdim/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
325221345SdimSplitEditor::SplitEditor(SplitAnalysis &sa,
326221345Sdim                         LiveIntervals &lis,
327221345Sdim                         VirtRegMap &vrm,
328263509Sdim                         MachineDominatorTree &mdt,
329263509Sdim                         MachineBlockFrequencyInfo &mbfi)
330221345Sdim  : SA(sa), LIS(lis), VRM(vrm),
331221345Sdim    MRI(vrm.getMachineFunction().getRegInfo()),
332221345Sdim    MDT(mdt),
333221345Sdim    TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
334221345Sdim    TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
335263509Sdim    MBFI(mbfi),
336221345Sdim    Edit(0),
337221345Sdim    OpenIdx(0),
338226890Sdim    SpillMode(SM_Partition),
339221345Sdim    RegAssign(Allocator)
340221345Sdim{}
341212793Sdim
342226890Sdimvoid SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
343226890Sdim  Edit = &LRE;
344226890Sdim  SpillMode = SM;
345221345Sdim  OpenIdx = 0;
346221345Sdim  RegAssign.clear();
347218893Sdim  Values.clear();
348221345Sdim
349226890Sdim  // Reset the LiveRangeCalc instances needed for this spill mode.
350245431Sdim  LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
351245431Sdim                  &LIS.getVNInfoAllocator());
352226890Sdim  if (SpillMode)
353245431Sdim    LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
354245431Sdim                    &LIS.getVNInfoAllocator());
355221345Sdim
356221345Sdim  // We don't need an AliasAnalysis since we will only be performing
357221345Sdim  // cheap-as-a-copy remats anyway.
358235633Sdim  Edit->anyRematerializable(0);
359212793Sdim}
360212793Sdim
361245431Sdim#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
362221345Sdimvoid SplitEditor::dump() const {
363221345Sdim  if (RegAssign.empty()) {
364221345Sdim    dbgs() << " empty\n";
365221345Sdim    return;
366221345Sdim  }
367221345Sdim
368221345Sdim  for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
369221345Sdim    dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
370221345Sdim  dbgs() << '\n';
371212793Sdim}
372245431Sdim#endif
373212793Sdim
374221345SdimVNInfo *SplitEditor::defValue(unsigned RegIdx,
375221345Sdim                              const VNInfo *ParentVNI,
376221345Sdim                              SlotIndex Idx) {
377212793Sdim  assert(ParentVNI && "Mapping  NULL value");
378212793Sdim  assert(Idx.isValid() && "Invalid SlotIndex");
379221345Sdim  assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
380263509Sdim  LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
381212793Sdim
382218893Sdim  // Create a new value.
383235633Sdim  VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
384212793Sdim
385218893Sdim  // Use insert for lookup, so we can add missing values with a second lookup.
386221345Sdim  std::pair<ValueMap::iterator, bool> InsP =
387226890Sdim    Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
388226890Sdim                                 ValueForcePair(VNI, false)));
389218893Sdim
390221345Sdim  // This was the first time (RegIdx, ParentVNI) was mapped.
391221345Sdim  // Keep it as a simple def without any liveness.
392221345Sdim  if (InsP.second)
393221345Sdim    return VNI;
394221345Sdim
395221345Sdim  // If the previous value was a simple mapping, add liveness for it now.
396226890Sdim  if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
397221345Sdim    SlotIndex Def = OldVNI->def;
398263509Sdim    LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
399226890Sdim    // No longer a simple mapping.  Switch to a complex, non-forced mapping.
400226890Sdim    InsP.first->second = ValueForcePair();
401221345Sdim  }
402218893Sdim
403221345Sdim  // This is a complex mapping, add liveness for VNI
404221345Sdim  SlotIndex Def = VNI->def;
405263509Sdim  LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
406221345Sdim
407212793Sdim  return VNI;
408212793Sdim}
409212793Sdim
410226890Sdimvoid SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
411212793Sdim  assert(ParentVNI && "Mapping  NULL value");
412226890Sdim  ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
413226890Sdim  VNInfo *VNI = VFP.getPointer();
414212793Sdim
415226890Sdim  // ParentVNI was either unmapped or already complex mapped. Either way, just
416226890Sdim  // set the force bit.
417226890Sdim  if (!VNI) {
418226890Sdim    VFP.setInt(true);
419221345Sdim    return;
420226890Sdim  }
421212793Sdim
422221345Sdim  // This was previously a single mapping. Make sure the old def is represented
423221345Sdim  // by a trivial live range.
424221345Sdim  SlotIndex Def = VNI->def;
425263509Sdim  LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
426263509Sdim  LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
427226890Sdim  // Mark as complex mapped, forced.
428226890Sdim  VFP = ValueForcePair(0, true);
429221345Sdim}
430218893Sdim
431218893SdimVNInfo *SplitEditor::defFromParent(unsigned RegIdx,
432218893Sdim                                   VNInfo *ParentVNI,
433218893Sdim                                   SlotIndex UseIdx,
434218893Sdim                                   MachineBasicBlock &MBB,
435218893Sdim                                   MachineBasicBlock::iterator I) {
436218893Sdim  MachineInstr *CopyMI = 0;
437218893Sdim  SlotIndex Def;
438263509Sdim  LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
439212793Sdim
440221345Sdim  // We may be trying to avoid interference that ends at a deleted instruction,
441221345Sdim  // so always begin RegIdx 0 early and all others late.
442221345Sdim  bool Late = RegIdx != 0;
443221345Sdim
444218893Sdim  // Attempt cheap-as-a-copy rematerialization.
445218893Sdim  LiveRangeEdit::Remat RM(ParentVNI);
446235633Sdim  if (Edit->canRematerializeAt(RM, UseIdx, true)) {
447235633Sdim    Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
448223017Sdim    ++NumRemats;
449218893Sdim  } else {
450218893Sdim    // Can't remat, just insert a copy from parent.
451218893Sdim    CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
452221345Sdim               .addReg(Edit->getReg());
453221345Sdim    Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
454235633Sdim            .getRegSlot();
455223017Sdim    ++NumCopies;
456212793Sdim  }
457212793Sdim
458218893Sdim  // Define the value in Reg.
459235633Sdim  return defValue(RegIdx, ParentVNI, Def);
460212793Sdim}
461212793Sdim
462212793Sdim/// Create a new virtual register and live interval.
463221345Sdimunsigned SplitEditor::openIntv() {
464218893Sdim  // Create the complement as index 0.
465221345Sdim  if (Edit->empty())
466263509Sdim    Edit->createEmptyInterval();
467212793Sdim
468218893Sdim  // Create the open interval.
469221345Sdim  OpenIdx = Edit->size();
470263509Sdim  Edit->createEmptyInterval();
471221345Sdim  return OpenIdx;
472212793Sdim}
473212793Sdim
474221345Sdimvoid SplitEditor::selectIntv(unsigned Idx) {
475221345Sdim  assert(Idx != 0 && "Cannot select the complement interval");
476221345Sdim  assert(Idx < Edit->size() && "Can only select previously opened interval");
477224145Sdim  DEBUG(dbgs() << "    selectIntv " << OpenIdx << " -> " << Idx << '\n');
478221345Sdim  OpenIdx = Idx;
479221345Sdim}
480221345Sdim
481218893SdimSlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
482218893Sdim  assert(OpenIdx && "openIntv not called before enterIntvBefore");
483218893Sdim  DEBUG(dbgs() << "    enterIntvBefore " << Idx);
484218893Sdim  Idx = Idx.getBaseIndex();
485221345Sdim  VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
486218893Sdim  if (!ParentVNI) {
487218893Sdim    DEBUG(dbgs() << ": not live\n");
488218893Sdim    return Idx;
489212793Sdim  }
490218893Sdim  DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
491218893Sdim  MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
492218893Sdim  assert(MI && "enterIntvBefore called with invalid index");
493212793Sdim
494218893Sdim  VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
495218893Sdim  return VNI->def;
496218893Sdim}
497212793Sdim
498224145SdimSlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
499224145Sdim  assert(OpenIdx && "openIntv not called before enterIntvAfter");
500224145Sdim  DEBUG(dbgs() << "    enterIntvAfter " << Idx);
501224145Sdim  Idx = Idx.getBoundaryIndex();
502224145Sdim  VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
503224145Sdim  if (!ParentVNI) {
504224145Sdim    DEBUG(dbgs() << ": not live\n");
505224145Sdim    return Idx;
506224145Sdim  }
507224145Sdim  DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
508224145Sdim  MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
509224145Sdim  assert(MI && "enterIntvAfter called with invalid index");
510224145Sdim
511224145Sdim  VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
512224145Sdim                              llvm::next(MachineBasicBlock::iterator(MI)));
513224145Sdim  return VNI->def;
514224145Sdim}
515224145Sdim
516218893SdimSlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
517218893Sdim  assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
518218893Sdim  SlotIndex End = LIS.getMBBEndIdx(&MBB);
519218893Sdim  SlotIndex Last = End.getPrevSlot();
520218893Sdim  DEBUG(dbgs() << "    enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
521221345Sdim  VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
522218893Sdim  if (!ParentVNI) {
523218893Sdim    DEBUG(dbgs() << ": not live\n");
524218893Sdim    return End;
525212793Sdim  }
526218893Sdim  DEBUG(dbgs() << ": valno " << ParentVNI->id);
527218893Sdim  VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
528235633Sdim                              SA.getLastSplitPointIter(&MBB));
529218893Sdim  RegAssign.insert(VNI->def, End, OpenIdx);
530218893Sdim  DEBUG(dump());
531218893Sdim  return VNI->def;
532212793Sdim}
533212793Sdim
534218893Sdim/// useIntv - indicate that all instructions in MBB should use OpenLI.
535212793Sdimvoid SplitEditor::useIntv(const MachineBasicBlock &MBB) {
536218893Sdim  useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
537212793Sdim}
538212793Sdim
539212793Sdimvoid SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
540218893Sdim  assert(OpenIdx && "openIntv not called before useIntv");
541218893Sdim  DEBUG(dbgs() << "    useIntv [" << Start << ';' << End << "):");
542218893Sdim  RegAssign.insert(Start, End, OpenIdx);
543218893Sdim  DEBUG(dump());
544218893Sdim}
545212793Sdim
546218893SdimSlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
547218893Sdim  assert(OpenIdx && "openIntv not called before leaveIntvAfter");
548218893Sdim  DEBUG(dbgs() << "    leaveIntvAfter " << Idx);
549212793Sdim
550218893Sdim  // The interval must be live beyond the instruction at Idx.
551226890Sdim  SlotIndex Boundary = Idx.getBoundaryIndex();
552226890Sdim  VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
553218893Sdim  if (!ParentVNI) {
554218893Sdim    DEBUG(dbgs() << ": not live\n");
555226890Sdim    return Boundary.getNextSlot();
556212793Sdim  }
557218893Sdim  DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
558226890Sdim  MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
559226890Sdim  assert(MI && "No instruction at index");
560212793Sdim
561226890Sdim  // In spill mode, make live ranges as short as possible by inserting the copy
562226890Sdim  // before MI.  This is only possible if that instruction doesn't redefine the
563226890Sdim  // value.  The inserted COPY is not a kill, and we don't need to recompute
564226890Sdim  // the source live range.  The spiller also won't try to hoist this copy.
565226890Sdim  if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
566226890Sdim      MI->readsVirtualRegister(Edit->getReg())) {
567226890Sdim    forceRecompute(0, ParentVNI);
568226890Sdim    defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
569226890Sdim    return Idx;
570226890Sdim  }
571226890Sdim
572226890Sdim  VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
573218893Sdim                              llvm::next(MachineBasicBlock::iterator(MI)));
574218893Sdim  return VNI->def;
575212793Sdim}
576212793Sdim
577218893SdimSlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
578218893Sdim  assert(OpenIdx && "openIntv not called before leaveIntvBefore");
579218893Sdim  DEBUG(dbgs() << "    leaveIntvBefore " << Idx);
580212793Sdim
581218893Sdim  // The interval must be live into the instruction at Idx.
582226890Sdim  Idx = Idx.getBaseIndex();
583221345Sdim  VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
584218893Sdim  if (!ParentVNI) {
585218893Sdim    DEBUG(dbgs() << ": not live\n");
586218893Sdim    return Idx.getNextSlot();
587212793Sdim  }
588218893Sdim  DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
589212793Sdim
590218893Sdim  MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
591218893Sdim  assert(MI && "No instruction at index");
592218893Sdim  VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
593218893Sdim  return VNI->def;
594212793Sdim}
595212793Sdim
596218893SdimSlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
597218893Sdim  assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
598218893Sdim  SlotIndex Start = LIS.getMBBStartIdx(&MBB);
599218893Sdim  DEBUG(dbgs() << "    leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
600212793Sdim
601221345Sdim  VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
602218893Sdim  if (!ParentVNI) {
603218893Sdim    DEBUG(dbgs() << ": not live\n");
604218893Sdim    return Start;
605212793Sdim  }
606212793Sdim
607218893Sdim  VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
608218893Sdim                              MBB.SkipPHIsAndLabels(MBB.begin()));
609218893Sdim  RegAssign.insert(Start, VNI->def, OpenIdx);
610218893Sdim  DEBUG(dump());
611218893Sdim  return VNI->def;
612218893Sdim}
613212793Sdim
614218893Sdimvoid SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
615218893Sdim  assert(OpenIdx && "openIntv not called before overlapIntv");
616221345Sdim  const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
617235633Sdim  assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
618218893Sdim         "Parent changes value in extended range");
619218893Sdim  assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
620218893Sdim         "Range cannot span basic blocks");
621212793Sdim
622226890Sdim  // The complement interval will be extended as needed by LRCalc.extend().
623221345Sdim  if (ParentVNI)
624226890Sdim    forceRecompute(0, ParentVNI);
625218893Sdim  DEBUG(dbgs() << "    overlapIntv [" << Start << ';' << End << "):");
626218893Sdim  RegAssign.insert(Start, End, OpenIdx);
627218893Sdim  DEBUG(dump());
628212793Sdim}
629212793Sdim
630226890Sdim//===----------------------------------------------------------------------===//
631226890Sdim//                                  Spill modes
632226890Sdim//===----------------------------------------------------------------------===//
633226890Sdim
634226890Sdimvoid SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
635263509Sdim  LiveInterval *LI = &LIS.getInterval(Edit->get(0));
636226890Sdim  DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
637226890Sdim  RegAssignMap::iterator AssignI;
638226890Sdim  AssignI.setMap(RegAssign);
639226890Sdim
640226890Sdim  for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
641226890Sdim    VNInfo *VNI = Copies[i];
642226890Sdim    SlotIndex Def = VNI->def;
643226890Sdim    MachineInstr *MI = LIS.getInstructionFromIndex(Def);
644226890Sdim    assert(MI && "No instruction for back-copy");
645226890Sdim
646226890Sdim    MachineBasicBlock *MBB = MI->getParent();
647226890Sdim    MachineBasicBlock::iterator MBBI(MI);
648226890Sdim    bool AtBegin;
649226890Sdim    do AtBegin = MBBI == MBB->begin();
650226890Sdim    while (!AtBegin && (--MBBI)->isDebugValue());
651226890Sdim
652226890Sdim    DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
653226890Sdim    LI->removeValNo(VNI);
654226890Sdim    LIS.RemoveMachineInstrFromMaps(MI);
655226890Sdim    MI->eraseFromParent();
656226890Sdim
657226890Sdim    // Adjust RegAssign if a register assignment is killed at VNI->def.  We
658226890Sdim    // want to avoid calculating the live range of the source register if
659226890Sdim    // possible.
660245431Sdim    AssignI.find(Def.getPrevSlot());
661226890Sdim    if (!AssignI.valid() || AssignI.start() >= Def)
662226890Sdim      continue;
663226890Sdim    // If MI doesn't kill the assigned register, just leave it.
664226890Sdim    if (AssignI.stop() != Def)
665226890Sdim      continue;
666226890Sdim    unsigned RegIdx = AssignI.value();
667226890Sdim    if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
668226890Sdim      DEBUG(dbgs() << "  cannot find simple kill of RegIdx " << RegIdx << '\n');
669226890Sdim      forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
670226890Sdim    } else {
671235633Sdim      SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
672226890Sdim      DEBUG(dbgs() << "  move kill to " << Kill << '\t' << *MBBI);
673226890Sdim      AssignI.setStop(Kill);
674226890Sdim    }
675226890Sdim  }
676226890Sdim}
677226890Sdim
678226890SdimMachineBasicBlock*
679226890SdimSplitEditor::findShallowDominator(MachineBasicBlock *MBB,
680226890Sdim                                  MachineBasicBlock *DefMBB) {
681226890Sdim  if (MBB == DefMBB)
682226890Sdim    return MBB;
683226890Sdim  assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
684226890Sdim
685226890Sdim  const MachineLoopInfo &Loops = SA.Loops;
686226890Sdim  const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
687226890Sdim  MachineDomTreeNode *DefDomNode = MDT[DefMBB];
688226890Sdim
689226890Sdim  // Best candidate so far.
690226890Sdim  MachineBasicBlock *BestMBB = MBB;
691226890Sdim  unsigned BestDepth = UINT_MAX;
692226890Sdim
693226890Sdim  for (;;) {
694226890Sdim    const MachineLoop *Loop = Loops.getLoopFor(MBB);
695226890Sdim
696226890Sdim    // MBB isn't in a loop, it doesn't get any better.  All dominators have a
697226890Sdim    // higher frequency by definition.
698226890Sdim    if (!Loop) {
699226890Sdim      DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
700226890Sdim                   << MBB->getNumber() << " at depth 0\n");
701226890Sdim      return MBB;
702226890Sdim    }
703226890Sdim
704226890Sdim    // We'll never be able to exit the DefLoop.
705226890Sdim    if (Loop == DefLoop) {
706226890Sdim      DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
707226890Sdim                   << MBB->getNumber() << " in the same loop\n");
708226890Sdim      return MBB;
709226890Sdim    }
710226890Sdim
711226890Sdim    // Least busy dominator seen so far.
712226890Sdim    unsigned Depth = Loop->getLoopDepth();
713226890Sdim    if (Depth < BestDepth) {
714226890Sdim      BestMBB = MBB;
715226890Sdim      BestDepth = Depth;
716226890Sdim      DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
717226890Sdim                   << MBB->getNumber() << " at depth " << Depth << '\n');
718226890Sdim    }
719226890Sdim
720226890Sdim    // Leave loop by going to the immediate dominator of the loop header.
721226890Sdim    // This is a bigger stride than simply walking up the dominator tree.
722226890Sdim    MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
723226890Sdim
724226890Sdim    // Too far up the dominator tree?
725226890Sdim    if (!IDom || !MDT.dominates(DefDomNode, IDom))
726226890Sdim      return BestMBB;
727226890Sdim
728226890Sdim    MBB = IDom->getBlock();
729226890Sdim  }
730226890Sdim}
731226890Sdim
732226890Sdimvoid SplitEditor::hoistCopiesForSize() {
733226890Sdim  // Get the complement interval, always RegIdx 0.
734263509Sdim  LiveInterval *LI = &LIS.getInterval(Edit->get(0));
735226890Sdim  LiveInterval *Parent = &Edit->getParent();
736226890Sdim
737226890Sdim  // Track the nearest common dominator for all back-copies for each ParentVNI,
738226890Sdim  // indexed by ParentVNI->id.
739226890Sdim  typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
740226890Sdim  SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
741226890Sdim
742226890Sdim  // Find the nearest common dominator for parent values with multiple
743226890Sdim  // back-copies.  If a single back-copy dominates, put it in DomPair.second.
744226890Sdim  for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
745226890Sdim       VI != VE; ++VI) {
746226890Sdim    VNInfo *VNI = *VI;
747245431Sdim    if (VNI->isUnused())
748245431Sdim      continue;
749226890Sdim    VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
750226890Sdim    assert(ParentVNI && "Parent not live at complement def");
751226890Sdim
752226890Sdim    // Don't hoist remats.  The complement is probably going to disappear
753226890Sdim    // completely anyway.
754226890Sdim    if (Edit->didRematerialize(ParentVNI))
755226890Sdim      continue;
756226890Sdim
757226890Sdim    MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
758226890Sdim    DomPair &Dom = NearestDom[ParentVNI->id];
759226890Sdim
760226890Sdim    // Keep directly defined parent values.  This is either a PHI or an
761226890Sdim    // instruction in the complement range.  All other copies of ParentVNI
762226890Sdim    // should be eliminated.
763226890Sdim    if (VNI->def == ParentVNI->def) {
764226890Sdim      DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
765226890Sdim      Dom = DomPair(ValMBB, VNI->def);
766226890Sdim      continue;
767226890Sdim    }
768226890Sdim    // Skip the singly mapped values.  There is nothing to gain from hoisting a
769226890Sdim    // single back-copy.
770226890Sdim    if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
771226890Sdim      DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
772226890Sdim      continue;
773226890Sdim    }
774226890Sdim
775226890Sdim    if (!Dom.first) {
776226890Sdim      // First time we see ParentVNI.  VNI dominates itself.
777226890Sdim      Dom = DomPair(ValMBB, VNI->def);
778226890Sdim    } else if (Dom.first == ValMBB) {
779226890Sdim      // Two defs in the same block.  Pick the earlier def.
780226890Sdim      if (!Dom.second.isValid() || VNI->def < Dom.second)
781226890Sdim        Dom.second = VNI->def;
782226890Sdim    } else {
783226890Sdim      // Different basic blocks. Check if one dominates.
784226890Sdim      MachineBasicBlock *Near =
785226890Sdim        MDT.findNearestCommonDominator(Dom.first, ValMBB);
786226890Sdim      if (Near == ValMBB)
787226890Sdim        // Def ValMBB dominates.
788226890Sdim        Dom = DomPair(ValMBB, VNI->def);
789226890Sdim      else if (Near != Dom.first)
790226890Sdim        // None dominate. Hoist to common dominator, need new def.
791226890Sdim        Dom = DomPair(Near, SlotIndex());
792226890Sdim    }
793226890Sdim
794226890Sdim    DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
795226890Sdim                 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
796226890Sdim                 << " hoist to BB#" << Dom.first->getNumber() << ' '
797226890Sdim                 << Dom.second << '\n');
798226890Sdim  }
799226890Sdim
800226890Sdim  // Insert the hoisted copies.
801226890Sdim  for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
802226890Sdim    DomPair &Dom = NearestDom[i];
803226890Sdim    if (!Dom.first || Dom.second.isValid())
804226890Sdim      continue;
805226890Sdim    // This value needs a hoisted copy inserted at the end of Dom.first.
806226890Sdim    VNInfo *ParentVNI = Parent->getValNumInfo(i);
807226890Sdim    MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
808226890Sdim    // Get a less loopy dominator than Dom.first.
809226890Sdim    Dom.first = findShallowDominator(Dom.first, DefMBB);
810226890Sdim    SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
811226890Sdim    Dom.second =
812226890Sdim      defFromParent(0, ParentVNI, Last, *Dom.first,
813235633Sdim                    SA.getLastSplitPointIter(Dom.first))->def;
814226890Sdim  }
815226890Sdim
816226890Sdim  // Remove redundant back-copies that are now known to be dominated by another
817226890Sdim  // def with the same value.
818226890Sdim  SmallVector<VNInfo*, 8> BackCopies;
819226890Sdim  for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
820226890Sdim       VI != VE; ++VI) {
821226890Sdim    VNInfo *VNI = *VI;
822245431Sdim    if (VNI->isUnused())
823245431Sdim      continue;
824226890Sdim    VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
825226890Sdim    const DomPair &Dom = NearestDom[ParentVNI->id];
826226890Sdim    if (!Dom.first || Dom.second == VNI->def)
827226890Sdim      continue;
828226890Sdim    BackCopies.push_back(VNI);
829226890Sdim    forceRecompute(0, ParentVNI);
830226890Sdim  }
831226890Sdim  removeBackCopies(BackCopies);
832226890Sdim}
833226890Sdim
834226890Sdim
835221345Sdim/// transferValues - Transfer all possible values to the new live ranges.
836226890Sdim/// Values that were rematerialized are left alone, they need LRCalc.extend().
837221345Sdimbool SplitEditor::transferValues() {
838221345Sdim  bool Skipped = false;
839221345Sdim  RegAssignMap::const_iterator AssignI = RegAssign.begin();
840221345Sdim  for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
841221345Sdim         ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
842221345Sdim    DEBUG(dbgs() << "  blit " << *ParentI << ':');
843221345Sdim    VNInfo *ParentVNI = ParentI->valno;
844221345Sdim    // RegAssign has holes where RegIdx 0 should be used.
845221345Sdim    SlotIndex Start = ParentI->start;
846221345Sdim    AssignI.advanceTo(Start);
847221345Sdim    do {
848221345Sdim      unsigned RegIdx;
849221345Sdim      SlotIndex End = ParentI->end;
850221345Sdim      if (!AssignI.valid()) {
851221345Sdim        RegIdx = 0;
852221345Sdim      } else if (AssignI.start() <= Start) {
853221345Sdim        RegIdx = AssignI.value();
854221345Sdim        if (AssignI.stop() < End) {
855221345Sdim          End = AssignI.stop();
856221345Sdim          ++AssignI;
857221345Sdim        }
858221345Sdim      } else {
859221345Sdim        RegIdx = 0;
860221345Sdim        End = std::min(End, AssignI.start());
861221345Sdim      }
862221345Sdim
863221345Sdim      // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
864221345Sdim      DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
865263509Sdim      LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
866221345Sdim
867221345Sdim      // Check for a simply defined value that can be blitted directly.
868226890Sdim      ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
869226890Sdim      if (VNInfo *VNI = VFP.getPointer()) {
870221345Sdim        DEBUG(dbgs() << ':' << VNI->id);
871263509Sdim        LR.addSegment(LiveInterval::Segment(Start, End, VNI));
872221345Sdim        Start = End;
873221345Sdim        continue;
874221345Sdim      }
875221345Sdim
876226890Sdim      // Skip values with forced recomputation.
877226890Sdim      if (VFP.getInt()) {
878226890Sdim        DEBUG(dbgs() << "(recalc)");
879221345Sdim        Skipped = true;
880221345Sdim        Start = End;
881221345Sdim        continue;
882221345Sdim      }
883221345Sdim
884226890Sdim      LiveRangeCalc &LRC = getLRCalc(RegIdx);
885221345Sdim
886221345Sdim      // This value has multiple defs in RegIdx, but it wasn't rematerialized,
887221345Sdim      // so the live range is accurate. Add live-in blocks in [Start;End) to the
888221345Sdim      // LiveInBlocks.
889221345Sdim      MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
890221345Sdim      SlotIndex BlockStart, BlockEnd;
891221345Sdim      tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
892221345Sdim
893221345Sdim      // The first block may be live-in, or it may have its own def.
894221345Sdim      if (Start != BlockStart) {
895263509Sdim        VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
896221345Sdim        assert(VNI && "Missing def for complex mapped value");
897221345Sdim        DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
898221345Sdim        // MBB has its own def. Is it also live-out?
899226890Sdim        if (BlockEnd <= End)
900226890Sdim          LRC.setLiveOutValue(MBB, VNI);
901226890Sdim
902221345Sdim        // Skip to the next block for live-in.
903221345Sdim        ++MBB;
904221345Sdim        BlockStart = BlockEnd;
905221345Sdim      }
906221345Sdim
907221345Sdim      // Handle the live-in blocks covered by [Start;End).
908221345Sdim      assert(Start <= BlockStart && "Expected live-in block");
909221345Sdim      while (BlockStart < End) {
910221345Sdim        DEBUG(dbgs() << ">BB#" << MBB->getNumber());
911221345Sdim        BlockEnd = LIS.getMBBEndIdx(MBB);
912221345Sdim        if (BlockStart == ParentVNI->def) {
913221345Sdim          // This block has the def of a parent PHI, so it isn't live-in.
914221345Sdim          assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
915263509Sdim          VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
916221345Sdim          assert(VNI && "Missing def for complex mapped parent PHI");
917226890Sdim          if (End >= BlockEnd)
918226890Sdim            LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
919221345Sdim        } else {
920226890Sdim          // This block needs a live-in value.  The last block covered may not
921226890Sdim          // be live-out.
922221345Sdim          if (End < BlockEnd)
923263509Sdim            LRC.addLiveInBlock(LR, MDT[MBB], End);
924221345Sdim          else {
925226890Sdim            // Live-through, and we don't know the value.
926263509Sdim            LRC.addLiveInBlock(LR, MDT[MBB]);
927226890Sdim            LRC.setLiveOutValue(MBB, 0);
928221345Sdim          }
929221345Sdim        }
930221345Sdim        BlockStart = BlockEnd;
931221345Sdim        ++MBB;
932221345Sdim      }
933221345Sdim      Start = End;
934221345Sdim    } while (Start != ParentI->end);
935221345Sdim    DEBUG(dbgs() << '\n');
936221345Sdim  }
937221345Sdim
938245431Sdim  LRCalc[0].calculateValues();
939226890Sdim  if (SpillMode)
940245431Sdim    LRCalc[1].calculateValues();
941221345Sdim
942221345Sdim  return Skipped;
943212793Sdim}
944212793Sdim
945221345Sdimvoid SplitEditor::extendPHIKillRanges() {
946221345Sdim    // Extend live ranges to be live-out for successor PHI values.
947221345Sdim  for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
948221345Sdim       E = Edit->getParent().vni_end(); I != E; ++I) {
949221345Sdim    const VNInfo *PHIVNI = *I;
950221345Sdim    if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
951221345Sdim      continue;
952221345Sdim    unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
953263509Sdim    LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
954226890Sdim    LiveRangeCalc &LRC = getLRCalc(RegIdx);
955221345Sdim    MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
956221345Sdim    for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
957221345Sdim         PE = MBB->pred_end(); PI != PE; ++PI) {
958226890Sdim      SlotIndex End = LIS.getMBBEndIdx(*PI);
959226890Sdim      SlotIndex LastUse = End.getPrevSlot();
960221345Sdim      // The predecessor may not have a live-out value. That is OK, like an
961221345Sdim      // undef PHI operand.
962226890Sdim      if (Edit->getParent().liveAt(LastUse)) {
963226890Sdim        assert(RegAssign.lookup(LastUse) == RegIdx &&
964221345Sdim               "Different register assignment in phi predecessor");
965263509Sdim        LRC.extend(LR, End);
966221345Sdim      }
967221345Sdim    }
968221345Sdim  }
969221345Sdim}
970221345Sdim
971221345Sdim/// rewriteAssigned - Rewrite all uses of Edit->getReg().
972221345Sdimvoid SplitEditor::rewriteAssigned(bool ExtendRanges) {
973221345Sdim  for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
974218893Sdim       RE = MRI.reg_end(); RI != RE;) {
975212793Sdim    MachineOperand &MO = RI.getOperand();
976212793Sdim    MachineInstr *MI = MO.getParent();
977212793Sdim    ++RI;
978218893Sdim    // LiveDebugVariables should have handled all DBG_VALUE instructions.
979212793Sdim    if (MI->isDebugValue()) {
980212793Sdim      DEBUG(dbgs() << "Zapping " << *MI);
981212793Sdim      MO.setReg(0);
982212793Sdim      continue;
983212793Sdim    }
984218893Sdim
985226890Sdim    // <undef> operands don't really read the register, so it doesn't matter
986226890Sdim    // which register we choose.  When the use operand is tied to a def, we must
987226890Sdim    // use the same register as the def, so just do that always.
988218893Sdim    SlotIndex Idx = LIS.getInstructionIndex(MI);
989226890Sdim    if (MO.isDef() || MO.isUndef())
990235633Sdim      Idx = Idx.getRegSlot(MO.isEarlyClobber());
991218893Sdim
992218893Sdim    // Rewrite to the mapped register at Idx.
993218893Sdim    unsigned RegIdx = RegAssign.lookup(Idx);
994263509Sdim    LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
995226890Sdim    MO.setReg(LI->reg);
996218893Sdim    DEBUG(dbgs() << "  rewr BB#" << MI->getParent()->getNumber() << '\t'
997218893Sdim                 << Idx << ':' << RegIdx << '\t' << *MI);
998218893Sdim
999221345Sdim    // Extend liveness to Idx if the instruction reads reg.
1000226890Sdim    if (!ExtendRanges || MO.isUndef())
1001221345Sdim      continue;
1002221345Sdim
1003221345Sdim    // Skip instructions that don't read Reg.
1004221345Sdim    if (MO.isDef()) {
1005221345Sdim      if (!MO.getSubReg() && !MO.isEarlyClobber())
1006221345Sdim        continue;
1007221345Sdim      // We may wan't to extend a live range for a partial redef, or for a use
1008221345Sdim      // tied to an early clobber.
1009221345Sdim      Idx = Idx.getPrevSlot();
1010221345Sdim      if (!Edit->getParent().liveAt(Idx))
1011221345Sdim        continue;
1012221345Sdim    } else
1013235633Sdim      Idx = Idx.getRegSlot(true);
1014221345Sdim
1015263509Sdim    getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
1016212793Sdim  }
1017218893Sdim}
1018212793Sdim
1019221345Sdimvoid SplitEditor::deleteRematVictims() {
1020221345Sdim  SmallVector<MachineInstr*, 8> Dead;
1021221345Sdim  for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
1022263509Sdim    LiveInterval *LI = &LIS.getInterval(*I);
1023221345Sdim    for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
1024221345Sdim           LII != LIE; ++LII) {
1025235633Sdim      // Dead defs end at the dead slot.
1026235633Sdim      if (LII->end != LII->valno->def.getDeadSlot())
1027221345Sdim        continue;
1028221345Sdim      MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
1029221345Sdim      assert(MI && "Missing instruction for dead def");
1030221345Sdim      MI->addRegisterDead(LI->reg, &TRI);
1031221345Sdim
1032221345Sdim      if (!MI->allDefsAreDead())
1033221345Sdim        continue;
1034221345Sdim
1035221345Sdim      DEBUG(dbgs() << "All defs dead: " << *MI);
1036221345Sdim      Dead.push_back(MI);
1037221345Sdim    }
1038212793Sdim  }
1039221345Sdim
1040221345Sdim  if (Dead.empty())
1041221345Sdim    return;
1042221345Sdim
1043235633Sdim  Edit->eliminateDeadDefs(Dead);
1044212793Sdim}
1045212793Sdim
1046221345Sdimvoid SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
1047221345Sdim  ++NumFinished;
1048212793Sdim
1049218893Sdim  // At this point, the live intervals in Edit contain VNInfos corresponding to
1050218893Sdim  // the inserted copies.
1051212793Sdim
1052218893Sdim  // Add the original defs from the parent interval.
1053221345Sdim  for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
1054221345Sdim         E = Edit->getParent().vni_end(); I != E; ++I) {
1055218893Sdim    const VNInfo *ParentVNI = *I;
1056218893Sdim    if (ParentVNI->isUnused())
1057218893Sdim      continue;
1058221345Sdim    unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
1059245431Sdim    defValue(RegIdx, ParentVNI, ParentVNI->def);
1060221345Sdim
1061226890Sdim    // Force rematted values to be recomputed everywhere.
1062221345Sdim    // The new live ranges may be truncated.
1063221345Sdim    if (Edit->didRematerialize(ParentVNI))
1064221345Sdim      for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1065226890Sdim        forceRecompute(i, ParentVNI);
1066218893Sdim  }
1067212793Sdim
1068226890Sdim  // Hoist back-copies to the complement interval when in spill mode.
1069226890Sdim  switch (SpillMode) {
1070226890Sdim  case SM_Partition:
1071226890Sdim    // Leave all back-copies as is.
1072226890Sdim    break;
1073226890Sdim  case SM_Size:
1074226890Sdim    hoistCopiesForSize();
1075226890Sdim    break;
1076226890Sdim  case SM_Speed:
1077226890Sdim    llvm_unreachable("Spill mode 'speed' not implemented yet");
1078226890Sdim  }
1079226890Sdim
1080221345Sdim  // Transfer the simply mapped values, check if any are skipped.
1081221345Sdim  bool Skipped = transferValues();
1082221345Sdim  if (Skipped)
1083221345Sdim    extendPHIKillRanges();
1084221345Sdim  else
1085221345Sdim    ++NumSimple;
1086212793Sdim
1087221345Sdim  // Rewrite virtual registers, possibly extending ranges.
1088221345Sdim  rewriteAssigned(Skipped);
1089212793Sdim
1090221345Sdim  // Delete defs that were rematted everywhere.
1091221345Sdim  if (Skipped)
1092221345Sdim    deleteRematVictims();
1093212793Sdim
1094218893Sdim  // Get rid of unused values and set phi-kill flags.
1095263509Sdim  for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1096263509Sdim    LiveInterval &LI = LIS.getInterval(*I);
1097263509Sdim    LI.RenumberValues();
1098263509Sdim  }
1099218893Sdim
1100221345Sdim  // Provide a reverse mapping from original indices to Edit ranges.
1101221345Sdim  if (LRMap) {
1102221345Sdim    LRMap->clear();
1103221345Sdim    for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1104221345Sdim      LRMap->push_back(i);
1105221345Sdim  }
1106221345Sdim
1107218893Sdim  // Now check if any registers were separated into multiple components.
1108218893Sdim  ConnectedVNInfoEqClasses ConEQ(LIS);
1109221345Sdim  for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
1110218893Sdim    // Don't use iterators, they are invalidated by create() below.
1111263509Sdim    LiveInterval *li = &LIS.getInterval(Edit->get(i));
1112218893Sdim    unsigned NumComp = ConEQ.Classify(li);
1113218893Sdim    if (NumComp <= 1)
1114218893Sdim      continue;
1115218893Sdim    DEBUG(dbgs() << "  " << NumComp << " components: " << *li << '\n');
1116218893Sdim    SmallVector<LiveInterval*, 8> dups;
1117218893Sdim    dups.push_back(li);
1118221345Sdim    for (unsigned j = 1; j != NumComp; ++j)
1119263509Sdim      dups.push_back(&Edit->createEmptyInterval());
1120221345Sdim    ConEQ.Distribute(&dups[0], MRI);
1121221345Sdim    // The new intervals all map back to i.
1122221345Sdim    if (LRMap)
1123221345Sdim      LRMap->resize(Edit->size(), i);
1124212793Sdim  }
1125212793Sdim
1126218893Sdim  // Calculate spill weight and allocation hints for new intervals.
1127263509Sdim  Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
1128221345Sdim
1129221345Sdim  assert(!LRMap || LRMap->size() == Edit->size());
1130212793Sdim}
1131212793Sdim
1132212793Sdim
1133212793Sdim//===----------------------------------------------------------------------===//
1134212793Sdim//                            Single Block Splitting
1135212793Sdim//===----------------------------------------------------------------------===//
1136212793Sdim
1137226890Sdimbool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1138226890Sdim                                           bool SingleInstrs) const {
1139226890Sdim  // Always split for multiple instructions.
1140226890Sdim  if (!BI.isOneInstr())
1141226890Sdim    return true;
1142226890Sdim  // Don't split for single instructions unless explicitly requested.
1143226890Sdim  if (!SingleInstrs)
1144218893Sdim    return false;
1145226890Sdim  // Splitting a live-through range always makes progress.
1146226890Sdim  if (BI.LiveIn && BI.LiveOut)
1147226890Sdim    return true;
1148226890Sdim  // No point in isolating a copy. It has no register class constraints.
1149226890Sdim  if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1150226890Sdim    return false;
1151226890Sdim  // Finally, don't isolate an end point that was created by earlier splits.
1152226890Sdim  return isOriginalEndpoint(BI.FirstInstr);
1153218893Sdim}
1154212793Sdim
1155221345Sdimvoid SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1156221345Sdim  openIntv();
1157221345Sdim  SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
1158226890Sdim  SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
1159221345Sdim    LastSplitPoint));
1160226890Sdim  if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1161226890Sdim    useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
1162221345Sdim  } else {
1163221345Sdim      // The last use is after the last valid split point.
1164221345Sdim    SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1165221345Sdim    useIntv(SegStart, SegStop);
1166226890Sdim    overlapIntv(SegStop, BI.LastInstr);
1167221345Sdim  }
1168221345Sdim}
1169221345Sdim
1170224145Sdim
1171224145Sdim//===----------------------------------------------------------------------===//
1172224145Sdim//                    Global Live Range Splitting Support
1173224145Sdim//===----------------------------------------------------------------------===//
1174224145Sdim
1175224145Sdim// These methods support a method of global live range splitting that uses a
1176224145Sdim// global algorithm to decide intervals for CFG edges. They will insert split
1177224145Sdim// points and color intervals in basic blocks while avoiding interference.
1178224145Sdim//
1179224145Sdim// Note that splitSingleBlock is also useful for blocks where both CFG edges
1180224145Sdim// are on the stack.
1181224145Sdim
1182224145Sdimvoid SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1183224145Sdim                                        unsigned IntvIn, SlotIndex LeaveBefore,
1184224145Sdim                                        unsigned IntvOut, SlotIndex EnterAfter){
1185224145Sdim  SlotIndex Start, Stop;
1186224145Sdim  tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
1187224145Sdim
1188224145Sdim  DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1189224145Sdim               << ") intf " << LeaveBefore << '-' << EnterAfter
1190224145Sdim               << ", live-through " << IntvIn << " -> " << IntvOut);
1191224145Sdim
1192224145Sdim  assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1193224145Sdim
1194226890Sdim  assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1195226890Sdim  assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1196226890Sdim  assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1197226890Sdim
1198226890Sdim  MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1199226890Sdim
1200224145Sdim  if (!IntvOut) {
1201224145Sdim    DEBUG(dbgs() << ", spill on entry.\n");
1202224145Sdim    //
1203224145Sdim    //        <<<<<<<<<    Possible LeaveBefore interference.
1204224145Sdim    //    |-----------|    Live through.
1205224145Sdim    //    -____________    Spill on entry.
1206224145Sdim    //
1207224145Sdim    selectIntv(IntvIn);
1208224145Sdim    SlotIndex Idx = leaveIntvAtTop(*MBB);
1209224145Sdim    assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1210224145Sdim    (void)Idx;
1211224145Sdim    return;
1212224145Sdim  }
1213224145Sdim
1214224145Sdim  if (!IntvIn) {
1215224145Sdim    DEBUG(dbgs() << ", reload on exit.\n");
1216224145Sdim    //
1217224145Sdim    //    >>>>>>>          Possible EnterAfter interference.
1218224145Sdim    //    |-----------|    Live through.
1219224145Sdim    //    ___________--    Reload on exit.
1220224145Sdim    //
1221224145Sdim    selectIntv(IntvOut);
1222224145Sdim    SlotIndex Idx = enterIntvAtEnd(*MBB);
1223224145Sdim    assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1224224145Sdim    (void)Idx;
1225224145Sdim    return;
1226224145Sdim  }
1227224145Sdim
1228224145Sdim  if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1229224145Sdim    DEBUG(dbgs() << ", straight through.\n");
1230224145Sdim    //
1231224145Sdim    //    |-----------|    Live through.
1232224145Sdim    //    -------------    Straight through, same intv, no interference.
1233224145Sdim    //
1234224145Sdim    selectIntv(IntvOut);
1235224145Sdim    useIntv(Start, Stop);
1236224145Sdim    return;
1237224145Sdim  }
1238224145Sdim
1239224145Sdim  // We cannot legally insert splits after LSP.
1240224145Sdim  SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
1241226890Sdim  assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
1242224145Sdim
1243224145Sdim  if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1244224145Sdim                  LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1245224145Sdim    DEBUG(dbgs() << ", switch avoiding interference.\n");
1246224145Sdim    //
1247224145Sdim    //    >>>>     <<<<    Non-overlapping EnterAfter/LeaveBefore interference.
1248224145Sdim    //    |-----------|    Live through.
1249224145Sdim    //    ------=======    Switch intervals between interference.
1250224145Sdim    //
1251224145Sdim    selectIntv(IntvOut);
1252226890Sdim    SlotIndex Idx;
1253226890Sdim    if (LeaveBefore && LeaveBefore < LSP) {
1254226890Sdim      Idx = enterIntvBefore(LeaveBefore);
1255226890Sdim      useIntv(Idx, Stop);
1256226890Sdim    } else {
1257226890Sdim      Idx = enterIntvAtEnd(*MBB);
1258226890Sdim    }
1259224145Sdim    selectIntv(IntvIn);
1260224145Sdim    useIntv(Start, Idx);
1261224145Sdim    assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1262224145Sdim    assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1263224145Sdim    return;
1264224145Sdim  }
1265224145Sdim
1266224145Sdim  DEBUG(dbgs() << ", create local intv for interference.\n");
1267224145Sdim  //
1268224145Sdim  //    >>><><><><<<<    Overlapping EnterAfter/LeaveBefore interference.
1269224145Sdim  //    |-----------|    Live through.
1270224145Sdim  //    ==---------==    Switch intervals before/after interference.
1271224145Sdim  //
1272224145Sdim  assert(LeaveBefore <= EnterAfter && "Missed case");
1273224145Sdim
1274224145Sdim  selectIntv(IntvOut);
1275224145Sdim  SlotIndex Idx = enterIntvAfter(EnterAfter);
1276224145Sdim  useIntv(Idx, Stop);
1277224145Sdim  assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1278224145Sdim
1279224145Sdim  selectIntv(IntvIn);
1280224145Sdim  Idx = leaveIntvBefore(LeaveBefore);
1281224145Sdim  useIntv(Start, Idx);
1282224145Sdim  assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1283224145Sdim}
1284224145Sdim
1285224145Sdim
1286224145Sdimvoid SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1287224145Sdim                                  unsigned IntvIn, SlotIndex LeaveBefore) {
1288224145Sdim  SlotIndex Start, Stop;
1289224145Sdim  tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1290224145Sdim
1291224145Sdim  DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
1292226890Sdim               << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
1293224145Sdim               << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1294224145Sdim               << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1295224145Sdim
1296224145Sdim  assert(IntvIn && "Must have register in");
1297224145Sdim  assert(BI.LiveIn && "Must be live-in");
1298224145Sdim  assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1299224145Sdim
1300226890Sdim  if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
1301224145Sdim    DEBUG(dbgs() << " before interference.\n");
1302224145Sdim    //
1303224145Sdim    //               <<<    Interference after kill.
1304224145Sdim    //     |---o---x   |    Killed in block.
1305224145Sdim    //     =========        Use IntvIn everywhere.
1306224145Sdim    //
1307224145Sdim    selectIntv(IntvIn);
1308226890Sdim    useIntv(Start, BI.LastInstr);
1309224145Sdim    return;
1310224145Sdim  }
1311224145Sdim
1312224145Sdim  SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1313224145Sdim
1314226890Sdim  if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
1315224145Sdim    //
1316224145Sdim    //               <<<    Possible interference after last use.
1317224145Sdim    //     |---o---o---|    Live-out on stack.
1318224145Sdim    //     =========____    Leave IntvIn after last use.
1319224145Sdim    //
1320224145Sdim    //                 <    Interference after last use.
1321224145Sdim    //     |---o---o--o|    Live-out on stack, late last use.
1322224145Sdim    //     ============     Copy to stack after LSP, overlap IntvIn.
1323224145Sdim    //            \_____    Stack interval is live-out.
1324224145Sdim    //
1325226890Sdim    if (BI.LastInstr < LSP) {
1326224145Sdim      DEBUG(dbgs() << ", spill after last use before interference.\n");
1327224145Sdim      selectIntv(IntvIn);
1328226890Sdim      SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
1329224145Sdim      useIntv(Start, Idx);
1330224145Sdim      assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1331224145Sdim    } else {
1332224145Sdim      DEBUG(dbgs() << ", spill before last split point.\n");
1333224145Sdim      selectIntv(IntvIn);
1334224145Sdim      SlotIndex Idx = leaveIntvBefore(LSP);
1335226890Sdim      overlapIntv(Idx, BI.LastInstr);
1336224145Sdim      useIntv(Start, Idx);
1337224145Sdim      assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1338224145Sdim    }
1339224145Sdim    return;
1340224145Sdim  }
1341224145Sdim
1342224145Sdim  // The interference is overlapping somewhere we wanted to use IntvIn. That
1343224145Sdim  // means we need to create a local interval that can be allocated a
1344224145Sdim  // different register.
1345224145Sdim  unsigned LocalIntv = openIntv();
1346224145Sdim  (void)LocalIntv;
1347224145Sdim  DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1348224145Sdim
1349226890Sdim  if (!BI.LiveOut || BI.LastInstr < LSP) {
1350224145Sdim    //
1351224145Sdim    //           <<<<<<<    Interference overlapping uses.
1352224145Sdim    //     |---o---o---|    Live-out on stack.
1353224145Sdim    //     =====----____    Leave IntvIn before interference, then spill.
1354224145Sdim    //
1355226890Sdim    SlotIndex To = leaveIntvAfter(BI.LastInstr);
1356224145Sdim    SlotIndex From = enterIntvBefore(LeaveBefore);
1357224145Sdim    useIntv(From, To);
1358224145Sdim    selectIntv(IntvIn);
1359224145Sdim    useIntv(Start, From);
1360224145Sdim    assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1361224145Sdim    return;
1362224145Sdim  }
1363224145Sdim
1364224145Sdim  //           <<<<<<<    Interference overlapping uses.
1365224145Sdim  //     |---o---o--o|    Live-out on stack, late last use.
1366224145Sdim  //     =====-------     Copy to stack before LSP, overlap LocalIntv.
1367224145Sdim  //            \_____    Stack interval is live-out.
1368224145Sdim  //
1369224145Sdim  SlotIndex To = leaveIntvBefore(LSP);
1370226890Sdim  overlapIntv(To, BI.LastInstr);
1371224145Sdim  SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1372224145Sdim  useIntv(From, To);
1373224145Sdim  selectIntv(IntvIn);
1374224145Sdim  useIntv(Start, From);
1375224145Sdim  assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1376224145Sdim}
1377224145Sdim
1378224145Sdimvoid SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1379224145Sdim                                   unsigned IntvOut, SlotIndex EnterAfter) {
1380224145Sdim  SlotIndex Start, Stop;
1381224145Sdim  tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1382224145Sdim
1383224145Sdim  DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
1384226890Sdim               << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
1385224145Sdim               << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1386224145Sdim               << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1387224145Sdim
1388224145Sdim  SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1389224145Sdim
1390224145Sdim  assert(IntvOut && "Must have register out");
1391224145Sdim  assert(BI.LiveOut && "Must be live-out");
1392224145Sdim  assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1393224145Sdim
1394226890Sdim  if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
1395224145Sdim    DEBUG(dbgs() << " after interference.\n");
1396224145Sdim    //
1397224145Sdim    //    >>>>             Interference before def.
1398224145Sdim    //    |   o---o---|    Defined in block.
1399224145Sdim    //        =========    Use IntvOut everywhere.
1400224145Sdim    //
1401224145Sdim    selectIntv(IntvOut);
1402226890Sdim    useIntv(BI.FirstInstr, Stop);
1403224145Sdim    return;
1404224145Sdim  }
1405224145Sdim
1406226890Sdim  if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
1407224145Sdim    DEBUG(dbgs() << ", reload after interference.\n");
1408224145Sdim    //
1409224145Sdim    //    >>>>             Interference before def.
1410224145Sdim    //    |---o---o---|    Live-through, stack-in.
1411224145Sdim    //    ____=========    Enter IntvOut before first use.
1412224145Sdim    //
1413224145Sdim    selectIntv(IntvOut);
1414226890Sdim    SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
1415224145Sdim    useIntv(Idx, Stop);
1416224145Sdim    assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1417224145Sdim    return;
1418224145Sdim  }
1419224145Sdim
1420224145Sdim  // The interference is overlapping somewhere we wanted to use IntvOut. That
1421224145Sdim  // means we need to create a local interval that can be allocated a
1422224145Sdim  // different register.
1423224145Sdim  DEBUG(dbgs() << ", interference overlaps uses.\n");
1424224145Sdim  //
1425224145Sdim  //    >>>>>>>          Interference overlapping uses.
1426224145Sdim  //    |---o---o---|    Live-through, stack-in.
1427224145Sdim  //    ____---======    Create local interval for interference range.
1428224145Sdim  //
1429224145Sdim  selectIntv(IntvOut);
1430224145Sdim  SlotIndex Idx = enterIntvAfter(EnterAfter);
1431224145Sdim  useIntv(Idx, Stop);
1432224145Sdim  assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1433224145Sdim
1434224145Sdim  openIntv();
1435226890Sdim  SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
1436224145Sdim  useIntv(From, Idx);
1437224145Sdim}
1438