SplitKit.cpp revision 8d0963f72c8922bafffb36ff49b18064098a3cab
1//===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the SplitAnalysis class as well as mutator functions for
11// live range splitting.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
16#include "SplitKit.h"
17#include "LiveRangeEdit.h"
18#include "VirtRegMap.h"
19#include "llvm/CodeGen/CalcSpillWeights.h"
20#include "llvm/CodeGen/LiveIntervalAnalysis.h"
21#include "llvm/CodeGen/MachineDominators.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineLoopInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/GraphWriter.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
31
32using namespace llvm;
33
34static cl::opt<bool>
35AllowSplit("spiller-splits-edges",
36           cl::desc("Allow critical edge splitting during spilling"));
37
38//===----------------------------------------------------------------------===//
39//                                 Edge Bundles
40//===----------------------------------------------------------------------===//
41
42/// compute - Compute the edge bundles for MF. Bundles depend only on the CFG.
43void EdgeBundles::compute(const MachineFunction *mf) {
44  MF = mf;
45  EC.clear();
46  EC.grow(2 * MF->size());
47
48  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
49       ++I) {
50    const MachineBasicBlock &MBB = *I;
51    unsigned OutE = 2 * MBB.getNumber() + 1;
52    // Join the outgoing bundle with the ingoing bundles of all successors.
53    for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
54           SE = MBB.succ_end(); SI != SE; ++SI)
55      EC.join(OutE, 2 * (*SI)->getNumber());
56  }
57  EC.compress();
58}
59
60/// view - Visualize the annotated bipartite CFG with Graphviz.
61void EdgeBundles::view() const {
62  ViewGraph(*this, "EdgeBundles");
63}
64
65/// Specialize WriteGraph, the standard implementation won't work.
66raw_ostream &llvm::WriteGraph(raw_ostream &O, const EdgeBundles &G,
67                              bool ShortNames,
68                              const std::string &Title) {
69  const MachineFunction *MF = G.getMachineFunction();
70
71  O << "digraph {\n";
72  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
73       I != E; ++I) {
74    unsigned BB = I->getNumber();
75    O << "\t\"BB#" << BB << "\" [ shape=box ]\n"
76      << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n"
77      << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n';
78  }
79  O << "}\n";
80  return O;
81}
82
83
84//===----------------------------------------------------------------------===//
85//                                 Split Analysis
86//===----------------------------------------------------------------------===//
87
88SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
89                             const LiveIntervals &lis,
90                             const MachineLoopInfo &mli)
91  : mf_(mf),
92    lis_(lis),
93    loops_(mli),
94    tii_(*mf.getTarget().getInstrInfo()),
95    curli_(0) {}
96
97void SplitAnalysis::clear() {
98  usingInstrs_.clear();
99  usingBlocks_.clear();
100  usingLoops_.clear();
101  curli_ = 0;
102}
103
104bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
105  MachineBasicBlock *T, *F;
106  SmallVector<MachineOperand, 4> Cond;
107  return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
108}
109
110/// analyzeUses - Count instructions, basic blocks, and loops using curli.
111void SplitAnalysis::analyzeUses() {
112  const MachineRegisterInfo &MRI = mf_.getRegInfo();
113  for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
114       MachineInstr *MI = I.skipInstruction();) {
115    if (MI->isDebugValue() || !usingInstrs_.insert(MI))
116      continue;
117    MachineBasicBlock *MBB = MI->getParent();
118    if (usingBlocks_[MBB]++)
119      continue;
120    for (MachineLoop *Loop = loops_.getLoopFor(MBB); Loop;
121         Loop = Loop->getParentLoop())
122      usingLoops_[Loop]++;
123  }
124  DEBUG(dbgs() << "  counted "
125               << usingInstrs_.size() << " instrs, "
126               << usingBlocks_.size() << " blocks, "
127               << usingLoops_.size()  << " loops.\n");
128}
129
130void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
131  for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
132    unsigned count = usingBlocks_.lookup(*I);
133    OS << " BB#" << (*I)->getNumber();
134    if (count)
135      OS << '(' << count << ')';
136  }
137}
138
139// Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
140// predecessor blocks, and exit blocks.
141void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
142  Blocks.clear();
143
144  // Blocks in the loop.
145  Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
146
147  // Predecessor blocks.
148  const MachineBasicBlock *Header = Loop->getHeader();
149  for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
150       E = Header->pred_end(); I != E; ++I)
151    if (!Blocks.Loop.count(*I))
152      Blocks.Preds.insert(*I);
153
154  // Exit blocks.
155  for (MachineLoop::block_iterator I = Loop->block_begin(),
156       E = Loop->block_end(); I != E; ++I) {
157    const MachineBasicBlock *MBB = *I;
158    for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
159       SE = MBB->succ_end(); SI != SE; ++SI)
160      if (!Blocks.Loop.count(*SI))
161        Blocks.Exits.insert(*SI);
162  }
163}
164
165void SplitAnalysis::print(const LoopBlocks &B, raw_ostream &OS) const {
166  OS << "Loop:";
167  print(B.Loop, OS);
168  OS << ", preds:";
169  print(B.Preds, OS);
170  OS << ", exits:";
171  print(B.Exits, OS);
172}
173
174/// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
175/// and around the Loop.
176SplitAnalysis::LoopPeripheralUse SplitAnalysis::
177analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
178  LoopPeripheralUse use = ContainedInLoop;
179  for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
180       I != E; ++I) {
181    const MachineBasicBlock *MBB = I->first;
182    // Is this a peripheral block?
183    if (use < MultiPeripheral &&
184        (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
185      if (I->second > 1) use = MultiPeripheral;
186      else               use = SinglePeripheral;
187      continue;
188    }
189    // Is it a loop block?
190    if (Blocks.Loop.count(MBB))
191      continue;
192    // It must be an unrelated block.
193    DEBUG(dbgs() << ", outside: BB#" << MBB->getNumber());
194    return OutsideLoop;
195  }
196  return use;
197}
198
199/// getCriticalExits - It may be necessary to partially break critical edges
200/// leaving the loop if an exit block has predecessors from outside the loop
201/// periphery.
202void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
203                                     BlockPtrSet &CriticalExits) {
204  CriticalExits.clear();
205
206  // A critical exit block has curli live-in, and has a predecessor that is not
207  // in the loop nor a loop predecessor. For such an exit block, the edges
208  // carrying the new variable must be moved to a new pre-exit block.
209  for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
210       I != E; ++I) {
211    const MachineBasicBlock *Exit = *I;
212    // A single-predecessor exit block is definitely not a critical edge.
213    if (Exit->pred_size() == 1)
214      continue;
215    // This exit may not have curli live in at all. No need to split.
216    if (!lis_.isLiveInToMBB(*curli_, Exit))
217      continue;
218    // Does this exit block have a predecessor that is not a loop block or loop
219    // predecessor?
220    for (MachineBasicBlock::const_pred_iterator PI = Exit->pred_begin(),
221         PE = Exit->pred_end(); PI != PE; ++PI) {
222      const MachineBasicBlock *Pred = *PI;
223      if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
224        continue;
225      // This is a critical exit block, and we need to split the exit edge.
226      CriticalExits.insert(Exit);
227      break;
228    }
229  }
230}
231
232void SplitAnalysis::getCriticalPreds(const SplitAnalysis::LoopBlocks &Blocks,
233                                     BlockPtrSet &CriticalPreds) {
234  CriticalPreds.clear();
235
236  // A critical predecessor block has curli live-out, and has a successor that
237  // has curli live-in and is not in the loop nor a loop exit block. For such a
238  // predecessor block, we must carry the value in both the 'inside' and
239  // 'outside' registers.
240  for (BlockPtrSet::iterator I = Blocks.Preds.begin(), E = Blocks.Preds.end();
241       I != E; ++I) {
242    const MachineBasicBlock *Pred = *I;
243    // Definitely not a critical edge.
244    if (Pred->succ_size() == 1)
245      continue;
246    // This block may not have curli live out at all if there is a PHI.
247    if (!lis_.isLiveOutOfMBB(*curli_, Pred))
248      continue;
249    // Does this block have a successor outside the loop?
250    for (MachineBasicBlock::const_pred_iterator SI = Pred->succ_begin(),
251         SE = Pred->succ_end(); SI != SE; ++SI) {
252      const MachineBasicBlock *Succ = *SI;
253      if (Blocks.Loop.count(Succ) || Blocks.Exits.count(Succ))
254        continue;
255      if (!lis_.isLiveInToMBB(*curli_, Succ))
256        continue;
257      // This is a critical predecessor block.
258      CriticalPreds.insert(Pred);
259      break;
260    }
261  }
262}
263
264/// canSplitCriticalExits - Return true if it is possible to insert new exit
265/// blocks before the blocks in CriticalExits.
266bool
267SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
268                                     BlockPtrSet &CriticalExits) {
269  // If we don't allow critical edge splitting, require no critical exits.
270  if (!AllowSplit)
271    return CriticalExits.empty();
272
273  for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
274       I != E; ++I) {
275    const MachineBasicBlock *Succ = *I;
276    // We want to insert a new pre-exit MBB before Succ, and change all the
277    // in-loop blocks to branch to the pre-exit instead of Succ.
278    // Check that all the in-loop predecessors can be changed.
279    for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
280         PE = Succ->pred_end(); PI != PE; ++PI) {
281      const MachineBasicBlock *Pred = *PI;
282      // The external predecessors won't be altered.
283      if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
284        continue;
285      if (!canAnalyzeBranch(Pred))
286        return false;
287    }
288
289    // If Succ's layout predecessor falls through, that too must be analyzable.
290    // We need to insert the pre-exit block in the gap.
291    MachineFunction::const_iterator MFI = Succ;
292    if (MFI == mf_.begin())
293      continue;
294    if (!canAnalyzeBranch(--MFI))
295      return false;
296  }
297  // No problems found.
298  return true;
299}
300
301void SplitAnalysis::analyze(const LiveInterval *li) {
302  clear();
303  curli_ = li;
304  analyzeUses();
305}
306
307void SplitAnalysis::getSplitLoops(LoopPtrSet &Loops) {
308  assert(curli_ && "Call analyze() before getSplitLoops");
309  if (usingLoops_.empty())
310    return;
311
312  LoopBlocks Blocks;
313  BlockPtrSet CriticalExits;
314
315  // We split around loops where curli is used outside the periphery.
316  for (LoopCountMap::const_iterator I = usingLoops_.begin(),
317       E = usingLoops_.end(); I != E; ++I) {
318    const MachineLoop *Loop = I->first;
319    getLoopBlocks(Loop, Blocks);
320    DEBUG({ dbgs() << "  "; print(Blocks, dbgs()); });
321
322    switch(analyzeLoopPeripheralUse(Blocks)) {
323    case OutsideLoop:
324      break;
325    case MultiPeripheral:
326      // FIXME: We could split a live range with multiple uses in a peripheral
327      // block and still make progress. However, it is possible that splitting
328      // another live range will insert copies into a peripheral block, and
329      // there is a small chance we can enter an infinite loop, inserting copies
330      // forever.
331      // For safety, stick to splitting live ranges with uses outside the
332      // periphery.
333      DEBUG(dbgs() << ": multiple peripheral uses");
334      break;
335    case ContainedInLoop:
336      DEBUG(dbgs() << ": fully contained\n");
337      continue;
338    case SinglePeripheral:
339      DEBUG(dbgs() << ": single peripheral use\n");
340      continue;
341    }
342    // Will it be possible to split around this loop?
343    getCriticalExits(Blocks, CriticalExits);
344    DEBUG(dbgs() << ": " << CriticalExits.size() << " critical exits\n");
345    if (!canSplitCriticalExits(Blocks, CriticalExits))
346      continue;
347    // This is a possible split.
348    Loops.insert(Loop);
349  }
350
351  DEBUG(dbgs() << "  getSplitLoops found " << Loops.size()
352               << " candidate loops.\n");
353}
354
355const MachineLoop *SplitAnalysis::getBestSplitLoop() {
356  LoopPtrSet Loops;
357  getSplitLoops(Loops);
358  if (Loops.empty())
359    return 0;
360
361  // Pick the earliest loop.
362  // FIXME: Are there other heuristics to consider?
363  const MachineLoop *Best = 0;
364  SlotIndex BestIdx;
365  for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
366       ++I) {
367    SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
368    if (!Best || Idx < BestIdx)
369      Best = *I, BestIdx = Idx;
370  }
371  DEBUG(dbgs() << "  getBestSplitLoop found " << *Best);
372  return Best;
373}
374
375/// isBypassLoop - Return true if curli is live through Loop and has no uses
376/// inside the loop. Bypass loops are candidates for splitting because it can
377/// prevent interference inside the loop.
378bool SplitAnalysis::isBypassLoop(const MachineLoop *Loop) {
379  // If curli is live into the loop header and there are no uses in the loop, it
380  // must be live in the entire loop and live on at least one exiting edge.
381  return !usingLoops_.count(Loop) &&
382         lis_.isLiveInToMBB(*curli_, Loop->getHeader());
383}
384
385/// getBypassLoops - Get all the maximal bypass loops. These are the bypass
386/// loops whose parent is not a bypass loop.
387void SplitAnalysis::getBypassLoops(LoopPtrSet &BypassLoops) {
388  SmallVector<MachineLoop*, 8> Todo(loops_.begin(), loops_.end());
389  while (!Todo.empty()) {
390    MachineLoop *Loop = Todo.pop_back_val();
391    if (!usingLoops_.count(Loop)) {
392      // This is either a bypass loop or completely irrelevant.
393      if (lis_.isLiveInToMBB(*curli_, Loop->getHeader()))
394        BypassLoops.insert(Loop);
395      // Either way, skip the child loops.
396      continue;
397    }
398
399    // The child loops may be bypass loops.
400    Todo.append(Loop->begin(), Loop->end());
401  }
402}
403
404
405//===----------------------------------------------------------------------===//
406//                               LiveIntervalMap
407//===----------------------------------------------------------------------===//
408
409// Work around the fact that the std::pair constructors are broken for pointer
410// pairs in some implementations. makeVV(x, 0) works.
411static inline std::pair<const VNInfo*, VNInfo*>
412makeVV(const VNInfo *a, VNInfo *b) {
413  return std::make_pair(a, b);
414}
415
416void LiveIntervalMap::reset(LiveInterval *li) {
417  li_ = li;
418  valueMap_.clear();
419  liveOutCache_.clear();
420}
421
422bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const {
423  ValueMap::const_iterator i = valueMap_.find(ParentVNI);
424  return i != valueMap_.end() && i->second == 0;
425}
426
427// defValue - Introduce a li_ def for ParentVNI that could be later than
428// ParentVNI->def.
429VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) {
430  assert(li_ && "call reset first");
431  assert(ParentVNI && "Mapping  NULL value");
432  assert(Idx.isValid() && "Invalid SlotIndex");
433  assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
434
435  // Create a new value.
436  VNInfo *VNI = li_->getNextValue(Idx, 0, lis_.getVNInfoAllocator());
437
438  // Preserve the PHIDef bit.
439  if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
440    VNI->setIsPHIDef(true);
441
442  // Use insert for lookup, so we can add missing values with a second lookup.
443  std::pair<ValueMap::iterator,bool> InsP =
444    valueMap_.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0));
445
446  // This is now a complex def. Mark with a NULL in valueMap.
447  if (!InsP.second)
448    InsP.first->second = 0;
449
450  return VNI;
451}
452
453
454// mapValue - Find the mapped value for ParentVNI at Idx.
455// Potentially create phi-def values.
456VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
457                                  bool *simple) {
458  assert(li_ && "call reset first");
459  assert(ParentVNI && "Mapping  NULL value");
460  assert(Idx.isValid() && "Invalid SlotIndex");
461  assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
462
463  // Use insert for lookup, so we can add missing values with a second lookup.
464  std::pair<ValueMap::iterator,bool> InsP =
465    valueMap_.insert(makeVV(ParentVNI, 0));
466
467  // This was an unknown value. Create a simple mapping.
468  if (InsP.second) {
469    if (simple) *simple = true;
470    return InsP.first->second = li_->createValueCopy(ParentVNI,
471                                                     lis_.getVNInfoAllocator());
472  }
473
474  // This was a simple mapped value.
475  if (InsP.first->second) {
476    if (simple) *simple = true;
477    return InsP.first->second;
478  }
479
480  // This is a complex mapped value. There may be multiple defs, and we may need
481  // to create phi-defs.
482  if (simple) *simple = false;
483  MachineBasicBlock *IdxMBB = lis_.getMBBFromIndex(Idx);
484  assert(IdxMBB && "No MBB at Idx");
485
486  // Is there a def in the same MBB we can extend?
487  if (VNInfo *VNI = extendTo(IdxMBB, Idx))
488    return VNI;
489
490  // Now for the fun part. We know that ParentVNI potentially has multiple defs,
491  // and we may need to create even more phi-defs to preserve VNInfo SSA form.
492  // Perform a search for all predecessor blocks where we know the dominating
493  // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
494  DEBUG(dbgs() << "\n  Reaching defs for BB#" << IdxMBB->getNumber()
495               << " at " << Idx << " in " << *li_ << '\n');
496
497  // Blocks where li_ should be live-in.
498  SmallVector<MachineDomTreeNode*, 16> LiveIn;
499  LiveIn.push_back(mdt_[IdxMBB]);
500
501  // Using liveOutCache_ as a visited set, perform a BFS for all reaching defs.
502  for (unsigned i = 0; i != LiveIn.size(); ++i) {
503    MachineBasicBlock *MBB = LiveIn[i]->getBlock();
504    for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
505           PE = MBB->pred_end(); PI != PE; ++PI) {
506       MachineBasicBlock *Pred = *PI;
507       // Is this a known live-out block?
508       std::pair<LiveOutMap::iterator,bool> LOIP =
509         liveOutCache_.insert(std::make_pair(Pred, LiveOutPair()));
510       // Yes, we have been here before.
511       if (!LOIP.second) {
512         DEBUG(if (VNInfo *VNI = LOIP.first->second.first)
513                 dbgs() << "    known valno #" << VNI->id
514                        << " at BB#" << Pred->getNumber() << '\n');
515         continue;
516       }
517
518       // Does Pred provide a live-out value?
519       SlotIndex Last = lis_.getMBBEndIdx(Pred).getPrevSlot();
520       if (VNInfo *VNI = extendTo(Pred, Last)) {
521         MachineBasicBlock *DefMBB = lis_.getMBBFromIndex(VNI->def);
522         DEBUG(dbgs() << "    found valno #" << VNI->id
523                      << " from BB#" << DefMBB->getNumber()
524                      << " at BB#" << Pred->getNumber() << '\n');
525         LiveOutPair &LOP = LOIP.first->second;
526         LOP.first = VNI;
527         LOP.second = mdt_[DefMBB];
528         continue;
529       }
530       // No, we need a live-in value for Pred as well
531       if (Pred != IdxMBB)
532         LiveIn.push_back(mdt_[Pred]);
533    }
534  }
535
536  // We may need to add phi-def values to preserve the SSA form.
537  // This is essentially the same iterative algorithm that SSAUpdater uses,
538  // except we already have a dominator tree, so we don't have to recompute it.
539  VNInfo *IdxVNI = 0;
540  unsigned Changes;
541  do {
542    Changes = 0;
543    DEBUG(dbgs() << "  Iterating over " << LiveIn.size() << " blocks.\n");
544    // Propagate live-out values down the dominator tree, inserting phi-defs when
545    // necessary. Since LiveIn was created by a BFS, going backwards makes it more
546    // likely for us to visit immediate dominators before their children.
547    for (unsigned i = LiveIn.size(); i; --i) {
548      MachineDomTreeNode *Node = LiveIn[i-1];
549      MachineBasicBlock *MBB = Node->getBlock();
550      MachineDomTreeNode *IDom = Node->getIDom();
551      LiveOutPair IDomValue;
552      // We need a live-in value to a block with no immediate dominator?
553      // This is probably an unreachable block that has survived somehow.
554      bool needPHI = !IDom;
555
556      // Get the IDom live-out value.
557      if (!needPHI) {
558        LiveOutMap::iterator I = liveOutCache_.find(IDom->getBlock());
559        if (I != liveOutCache_.end())
560          IDomValue = I->second;
561        else
562          // If IDom is outside our set of live-out blocks, there must be new
563          // defs, and we need a phi-def here.
564          needPHI = true;
565      }
566
567      // IDom dominates all of our predecessors, but it may not be the immediate
568      // dominator. Check if any of them have live-out values that are properly
569      // dominated by IDom. If so, we need a phi-def here.
570      if (!needPHI) {
571        for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
572               PE = MBB->pred_end(); PI != PE; ++PI) {
573          LiveOutPair Value = liveOutCache_[*PI];
574          if (!Value.first || Value.first == IDomValue.first)
575            continue;
576          // This predecessor is carrying something other than IDomValue.
577          // It could be because IDomValue hasn't propagated yet, or it could be
578          // because MBB is in the dominance frontier of that value.
579          if (mdt_.dominates(IDom, Value.second)) {
580            needPHI = true;
581            break;
582          }
583        }
584      }
585
586      // Create a phi-def if required.
587      if (needPHI) {
588        ++Changes;
589        SlotIndex Start = lis_.getMBBStartIdx(MBB);
590        VNInfo *VNI = li_->getNextValue(Start, 0, lis_.getVNInfoAllocator());
591        VNI->setIsPHIDef(true);
592        DEBUG(dbgs() << "    - BB#" << MBB->getNumber()
593                     << " phi-def #" << VNI->id << " at " << Start << '\n');
594        // We no longer need li_ to be live-in.
595        LiveIn.erase(LiveIn.begin()+(i-1));
596        // Blocks in LiveIn are either IdxMBB, or have a value live-through.
597        if (MBB == IdxMBB)
598          IdxVNI = VNI;
599        // Check if we need to update live-out info.
600        LiveOutMap::iterator I = liveOutCache_.find(MBB);
601        if (I == liveOutCache_.end() || I->second.second == Node) {
602          // We already have a live-out defined in MBB, so this must be IdxMBB.
603          assert(MBB == IdxMBB && "Adding phi-def to known live-out");
604          li_->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
605        } else {
606          // This phi-def is also live-out, so color the whole block.
607          li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
608          I->second = LiveOutPair(VNI, Node);
609        }
610      } else if (IDomValue.first) {
611        // No phi-def here. Remember incoming value for IdxMBB.
612        if (MBB == IdxMBB)
613          IdxVNI = IDomValue.first;
614        // Propagate IDomValue if needed:
615        // MBB is live-out and doesn't define its own value.
616        LiveOutMap::iterator I = liveOutCache_.find(MBB);
617        if (I != liveOutCache_.end() && I->second.second != Node &&
618            I->second.first != IDomValue.first) {
619          ++Changes;
620          I->second = IDomValue;
621          DEBUG(dbgs() << "    - BB#" << MBB->getNumber()
622                       << " idom valno #" << IDomValue.first->id
623                       << " from BB#" << IDom->getBlock()->getNumber() << '\n');
624        }
625      }
626    }
627    DEBUG(dbgs() << "  - made " << Changes << " changes.\n");
628  } while (Changes);
629
630  assert(IdxVNI && "Didn't find value for Idx");
631
632#ifndef NDEBUG
633  // Check the liveOutCache_ invariants.
634  for (LiveOutMap::iterator I = liveOutCache_.begin(), E = liveOutCache_.end();
635         I != E; ++I) {
636    assert(I->first && "Null MBB entry in cache");
637    assert(I->second.first && "Null VNInfo in cache");
638    assert(I->second.second && "Null DomTreeNode in cache");
639    if (I->second.second->getBlock() == I->first)
640      continue;
641    for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
642           PE = I->first->pred_end(); PI != PE; ++PI)
643      assert(liveOutCache_.lookup(*PI) == I->second && "Bad invariant");
644  }
645#endif
646
647  // Since we went through the trouble of a full BFS visiting all reaching defs,
648  // the values in LiveIn are now accurate. No more phi-defs are needed
649  // for these blocks, so we can color the live ranges.
650  // This makes the next mapValue call much faster.
651  for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
652    MachineBasicBlock *MBB = LiveIn[i]->getBlock();
653    SlotIndex Start = lis_.getMBBStartIdx(MBB);
654    if (MBB == IdxMBB) {
655      li_->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
656      continue;
657    }
658    // Anything in LiveIn other than IdxMBB is live-through.
659    VNInfo *VNI = liveOutCache_.lookup(MBB).first;
660    assert(VNI && "Missing block value");
661    li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
662  }
663
664  return IdxVNI;
665}
666
667// extendTo - Find the last li_ value defined in MBB at or before Idx. The
668// parentli_ is assumed to be live at Idx. Extend the live range to Idx.
669// Return the found VNInfo, or NULL.
670VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) {
671  assert(li_ && "call reset first");
672  LiveInterval::iterator I = std::upper_bound(li_->begin(), li_->end(), Idx);
673  if (I == li_->begin())
674    return 0;
675  --I;
676  if (I->end <= lis_.getMBBStartIdx(MBB))
677    return 0;
678  if (I->end <= Idx)
679    I->end = Idx.getNextSlot();
680  return I->valno;
681}
682
683// addSimpleRange - Add a simple range from parentli_ to li_.
684// ParentVNI must be live in the [Start;End) interval.
685void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
686                                     const VNInfo *ParentVNI) {
687  assert(li_ && "call reset first");
688  bool simple;
689  VNInfo *VNI = mapValue(ParentVNI, Start, &simple);
690  // A simple mapping is easy.
691  if (simple) {
692    li_->addRange(LiveRange(Start, End, VNI));
693    return;
694  }
695
696  // ParentVNI is a complex value. We must map per MBB.
697  MachineFunction::iterator MBB = lis_.getMBBFromIndex(Start);
698  MachineFunction::iterator MBBE = lis_.getMBBFromIndex(End.getPrevSlot());
699
700  if (MBB == MBBE) {
701    li_->addRange(LiveRange(Start, End, VNI));
702    return;
703  }
704
705  // First block.
706  li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
707
708  // Run sequence of full blocks.
709  for (++MBB; MBB != MBBE; ++MBB) {
710    Start = lis_.getMBBStartIdx(MBB);
711    li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB),
712                            mapValue(ParentVNI, Start)));
713  }
714
715  // Final block.
716  Start = lis_.getMBBStartIdx(MBB);
717  if (Start != End)
718    li_->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
719}
720
721/// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
722/// All needed values whose def is not inside [Start;End) must be defined
723/// beforehand so mapValue will work.
724void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) {
725  assert(li_ && "call reset first");
726  LiveInterval::const_iterator B = parentli_.begin(), E = parentli_.end();
727  LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
728
729  // Check if --I begins before Start and overlaps.
730  if (I != B) {
731    --I;
732    if (I->end > Start)
733      addSimpleRange(Start, std::min(End, I->end), I->valno);
734    ++I;
735  }
736
737  // The remaining ranges begin after Start.
738  for (;I != E && I->start < End; ++I)
739    addSimpleRange(I->start, std::min(End, I->end), I->valno);
740}
741
742
743//===----------------------------------------------------------------------===//
744//                               Split Editor
745//===----------------------------------------------------------------------===//
746
747/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
748SplitEditor::SplitEditor(SplitAnalysis &sa,
749                         LiveIntervals &lis,
750                         VirtRegMap &vrm,
751                         MachineDominatorTree &mdt,
752                         LiveRangeEdit &edit)
753  : sa_(sa), lis_(lis), vrm_(vrm),
754    mri_(vrm.getMachineFunction().getRegInfo()),
755    tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()),
756    tri_(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
757    edit_(edit),
758    dupli_(lis_, mdt, edit.getParent()),
759    openli_(lis_, mdt, edit.getParent())
760{
761  // We don't need an AliasAnalysis since we will only be performing
762  // cheap-as-a-copy remats anyway.
763  edit_.anyRematerializable(lis_, tii_, 0);
764}
765
766bool SplitEditor::intervalsLiveAt(SlotIndex Idx) const {
767  for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I)
768    if (*I != dupli_.getLI() && (*I)->liveAt(Idx))
769      return true;
770  return false;
771}
772
773VNInfo *SplitEditor::defFromParent(LiveIntervalMap &Reg,
774                                   VNInfo *ParentVNI,
775                                   SlotIndex UseIdx,
776                                   MachineBasicBlock &MBB,
777                                   MachineBasicBlock::iterator I) {
778  VNInfo *VNI = 0;
779  MachineInstr *CopyMI = 0;
780  SlotIndex Def;
781
782  // Attempt cheap-as-a-copy rematerialization.
783  LiveRangeEdit::Remat RM(ParentVNI);
784  if (edit_.canRematerializeAt(RM, UseIdx, true, lis_)) {
785    Def = edit_.rematerializeAt(MBB, I, Reg.getLI()->reg, RM,
786                                          lis_, tii_, tri_);
787  } else {
788    // Can't remat, just insert a copy from parent.
789    CopyMI = BuildMI(MBB, I, DebugLoc(), tii_.get(TargetOpcode::COPY),
790                     Reg.getLI()->reg).addReg(edit_.getReg());
791    Def = lis_.InsertMachineInstrInMaps(CopyMI).getDefIndex();
792  }
793
794  // Define the value in Reg.
795  VNI = Reg.defValue(ParentVNI, Def);
796  VNI->setCopy(CopyMI);
797
798  // Add minimal liveness for the new value.
799  if (UseIdx < Def)
800    UseIdx = Def;
801  Reg.getLI()->addRange(LiveRange(Def, UseIdx.getNextSlot(), VNI));
802  return VNI;
803}
804
805/// Create a new virtual register and live interval.
806void SplitEditor::openIntv() {
807  assert(!openli_.getLI() && "Previous LI not closed before openIntv");
808  if (!dupli_.getLI())
809    dupli_.reset(&edit_.create(mri_, lis_, vrm_));
810
811  openli_.reset(&edit_.create(mri_, lis_, vrm_));
812}
813
814/// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
815/// not live before Idx, a COPY is not inserted.
816void SplitEditor::enterIntvBefore(SlotIndex Idx) {
817  assert(openli_.getLI() && "openIntv not called before enterIntvBefore");
818  Idx = Idx.getUseIndex();
819  DEBUG(dbgs() << "    enterIntvBefore " << Idx);
820  VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx);
821  if (!ParentVNI) {
822    DEBUG(dbgs() << ": not live\n");
823    return;
824  }
825  DEBUG(dbgs() << ": valno " << ParentVNI->id);
826  truncatedValues.insert(ParentVNI);
827  MachineInstr *MI = lis_.getInstructionFromIndex(Idx);
828  assert(MI && "enterIntvBefore called with invalid index");
829
830  defFromParent(openli_, ParentVNI, Idx, *MI->getParent(), MI);
831
832  DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
833}
834
835/// enterIntvAtEnd - Enter openli at the end of MBB.
836void SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
837  assert(openli_.getLI() && "openIntv not called before enterIntvAtEnd");
838  SlotIndex End = lis_.getMBBEndIdx(&MBB).getPrevSlot();
839  DEBUG(dbgs() << "    enterIntvAtEnd BB#" << MBB.getNumber() << ", " << End);
840  VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(End);
841  if (!ParentVNI) {
842    DEBUG(dbgs() << ": not live\n");
843    return;
844  }
845  DEBUG(dbgs() << ": valno " << ParentVNI->id);
846  truncatedValues.insert(ParentVNI);
847  defFromParent(openli_, ParentVNI, End, MBB, MBB.getFirstTerminator());
848  DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
849}
850
851/// useIntv - indicate that all instructions in MBB should use openli.
852void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
853  useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB));
854}
855
856void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
857  assert(openli_.getLI() && "openIntv not called before useIntv");
858  openli_.addRange(Start, End);
859  DEBUG(dbgs() << "    use [" << Start << ';' << End << "): "
860               << *openli_.getLI() << '\n');
861}
862
863/// leaveIntvAfter - Leave openli after the instruction at Idx.
864void SplitEditor::leaveIntvAfter(SlotIndex Idx) {
865  assert(openli_.getLI() && "openIntv not called before leaveIntvAfter");
866  DEBUG(dbgs() << "    leaveIntvAfter " << Idx);
867
868  // The interval must be live beyond the instruction at Idx.
869  Idx = Idx.getBoundaryIndex();
870  VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx);
871  if (!ParentVNI) {
872    DEBUG(dbgs() << ": not live\n");
873    return;
874  }
875  DEBUG(dbgs() << ": valno " << ParentVNI->id);
876
877  MachineBasicBlock::iterator MII = lis_.getInstructionFromIndex(Idx);
878  VNInfo *VNI = defFromParent(dupli_, ParentVNI, Idx,
879                              *MII->getParent(), llvm::next(MII));
880
881  // Make sure that openli is properly extended from Idx to the new copy.
882  // FIXME: This shouldn't be necessary for remats.
883  openli_.addSimpleRange(Idx, VNI->def, ParentVNI);
884
885  DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
886}
887
888/// leaveIntvAtTop - Leave the interval at the top of MBB.
889/// Currently, only one value can leave the interval.
890void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
891  assert(openli_.getLI() && "openIntv not called before leaveIntvAtTop");
892  SlotIndex Start = lis_.getMBBStartIdx(&MBB);
893  DEBUG(dbgs() << "    leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
894
895  VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Start);
896  if (!ParentVNI) {
897    DEBUG(dbgs() << ": not live\n");
898    return;
899  }
900
901  VNInfo *VNI = defFromParent(dupli_, ParentVNI, Start, MBB,
902                              MBB.SkipPHIsAndLabels(MBB.begin()));
903
904  // Finally we must make sure that openli is properly extended from Start to
905  // the new copy.
906  openli_.addSimpleRange(Start, VNI->def, ParentVNI);
907  DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
908}
909
910/// closeIntv - Indicate that we are done editing the currently open
911/// LiveInterval, and ranges can be trimmed.
912void SplitEditor::closeIntv() {
913  assert(openli_.getLI() && "openIntv not called before closeIntv");
914
915  DEBUG(dbgs() << "    closeIntv cleaning up\n");
916  DEBUG(dbgs() << "    open " << *openli_.getLI() << '\n');
917  openli_.reset(0);
918}
919
920/// rewrite - Rewrite all uses of reg to use the new registers.
921void SplitEditor::rewrite(unsigned reg) {
922  for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(reg),
923       RE = mri_.reg_end(); RI != RE;) {
924    MachineOperand &MO = RI.getOperand();
925    unsigned OpNum = RI.getOperandNo();
926    MachineInstr *MI = MO.getParent();
927    ++RI;
928    if (MI->isDebugValue()) {
929      DEBUG(dbgs() << "Zapping " << *MI);
930      // FIXME: We can do much better with debug values.
931      MO.setReg(0);
932      continue;
933    }
934    SlotIndex Idx = lis_.getInstructionIndex(MI);
935    Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
936    LiveInterval *LI = 0;
937    for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E;
938         ++I) {
939      LiveInterval *testli = *I;
940      if (testli->liveAt(Idx)) {
941        LI = testli;
942        break;
943      }
944    }
945    DEBUG(dbgs() << "  rewr BB#" << MI->getParent()->getNumber() << '\t'<< Idx);
946    assert(LI && "No register was live at use");
947    MO.setReg(LI->reg);
948    if (MO.isUse() && !MI->isRegTiedToDefOperand(OpNum))
949      MO.setIsKill(LI->killedAt(Idx.getDefIndex()));
950    DEBUG(dbgs() << '\t' << *MI);
951  }
952}
953
954void
955SplitEditor::addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
956  // Build vector of iterator pairs from the intervals.
957  typedef std::pair<LiveInterval::const_iterator,
958                    LiveInterval::const_iterator> IIPair;
959  SmallVector<IIPair, 8> Iters;
960  for (LiveRangeEdit::iterator LI = edit_.begin(), LE = edit_.end(); LI != LE;
961       ++LI) {
962    if (*LI == dupli_.getLI())
963      continue;
964    LiveInterval::const_iterator I = (*LI)->find(Start);
965    LiveInterval::const_iterator E = (*LI)->end();
966    if (I != E)
967      Iters.push_back(std::make_pair(I, E));
968  }
969
970  SlotIndex sidx = Start;
971  // Break [Start;End) into segments that don't overlap any intervals.
972  for (;;) {
973    SlotIndex next = sidx, eidx = End;
974    // Find overlapping intervals.
975    for (unsigned i = 0; i != Iters.size() && sidx < eidx; ++i) {
976      LiveInterval::const_iterator I = Iters[i].first;
977      // Interval I is overlapping [sidx;eidx). Trim sidx.
978      if (I->start <= sidx) {
979        sidx = I->end;
980        // Move to the next run, remove iters when all are consumed.
981        I = ++Iters[i].first;
982        if (I == Iters[i].second) {
983          Iters.erase(Iters.begin() + i);
984          --i;
985          continue;
986        }
987      }
988      // Trim eidx too if needed.
989      if (I->start >= eidx)
990        continue;
991      eidx = I->start;
992      next = I->end;
993    }
994    // Now, [sidx;eidx) doesn't overlap anything in intervals_.
995    if (sidx < eidx)
996      dupli_.addSimpleRange(sidx, eidx, VNI);
997    // If the interval end was truncated, we can try again from next.
998    if (next <= sidx)
999      break;
1000    sidx = next;
1001  }
1002}
1003
1004void SplitEditor::computeRemainder() {
1005  // First we need to fill in the live ranges in dupli.
1006  // If values were redefined, we need a full recoloring with SSA update.
1007  // If values were truncated, we only need to truncate the ranges.
1008  // If values were partially rematted, we should shrink to uses.
1009  // If values were fully rematted, they should be omitted.
1010  // FIXME: If a single value is redefined, just move the def and truncate.
1011  LiveInterval &parent = edit_.getParent();
1012
1013  // Values that are fully contained in the split intervals.
1014  SmallPtrSet<const VNInfo*, 8> deadValues;
1015  // Map all curli values that should have live defs in dupli.
1016  for (LiveInterval::const_vni_iterator I = parent.vni_begin(),
1017       E = parent.vni_end(); I != E; ++I) {
1018    const VNInfo *VNI = *I;
1019    // Don't transfer unused values to the new intervals.
1020    if (VNI->isUnused())
1021      continue;
1022    // Original def is contained in the split intervals.
1023    if (intervalsLiveAt(VNI->def)) {
1024      // Did this value escape?
1025      if (dupli_.isMapped(VNI))
1026        truncatedValues.insert(VNI);
1027      else
1028        deadValues.insert(VNI);
1029      continue;
1030    }
1031    // Add minimal live range at the definition.
1032    VNInfo *DVNI = dupli_.defValue(VNI, VNI->def);
1033    dupli_.getLI()->addRange(LiveRange(VNI->def, VNI->def.getNextSlot(), DVNI));
1034  }
1035
1036  // Add all ranges to dupli.
1037  for (LiveInterval::const_iterator I = parent.begin(), E = parent.end();
1038       I != E; ++I) {
1039    const LiveRange &LR = *I;
1040    if (truncatedValues.count(LR.valno)) {
1041      // recolor after removing intervals_.
1042      addTruncSimpleRange(LR.start, LR.end, LR.valno);
1043    } else if (!deadValues.count(LR.valno)) {
1044      // recolor without truncation.
1045      dupli_.addSimpleRange(LR.start, LR.end, LR.valno);
1046    }
1047  }
1048
1049  // Extend dupli_ to be live out of any critical loop predecessors.
1050  // This means we have multiple registers live out of those blocks.
1051  // The alternative would be to split the critical edges.
1052  if (criticalPreds_.empty())
1053    return;
1054  for (SplitAnalysis::BlockPtrSet::iterator I = criticalPreds_.begin(),
1055       E = criticalPreds_.end(); I != E; ++I)
1056     dupli_.extendTo(*I, lis_.getMBBEndIdx(*I).getPrevSlot());
1057   criticalPreds_.clear();
1058}
1059
1060void SplitEditor::finish() {
1061  assert(!openli_.getLI() && "Previous LI not closed before rewrite");
1062  assert(dupli_.getLI() && "No dupli for rewrite. Noop spilt?");
1063
1064  // Complete dupli liveness.
1065  computeRemainder();
1066
1067  // Get rid of unused values and set phi-kill flags.
1068  for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I)
1069    (*I)->RenumberValues(lis_);
1070
1071  // Rewrite instructions.
1072  rewrite(edit_.getReg());
1073
1074  // Now check if any registers were separated into multiple components.
1075  ConnectedVNInfoEqClasses ConEQ(lis_);
1076  for (unsigned i = 0, e = edit_.size(); i != e; ++i) {
1077    // Don't use iterators, they are invalidated by create() below.
1078    LiveInterval *li = edit_.get(i);
1079    unsigned NumComp = ConEQ.Classify(li);
1080    if (NumComp <= 1)
1081      continue;
1082    DEBUG(dbgs() << "  " << NumComp << " components: " << *li << '\n');
1083    SmallVector<LiveInterval*, 8> dups;
1084    dups.push_back(li);
1085    for (unsigned i = 1; i != NumComp; ++i)
1086      dups.push_back(&edit_.create(mri_, lis_, vrm_));
1087    ConEQ.Distribute(&dups[0]);
1088    // Rewrite uses to the new regs.
1089    rewrite(li->reg);
1090  }
1091
1092  // Calculate spill weight and allocation hints for new intervals.
1093  VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_);
1094  for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I){
1095    LiveInterval &li = **I;
1096    vrai.CalculateRegClass(li.reg);
1097    vrai.CalculateWeightAndHint(li);
1098    DEBUG(dbgs() << "  new interval " << mri_.getRegClass(li.reg)->getName()
1099                 << ":" << li << '\n');
1100  }
1101}
1102
1103
1104//===----------------------------------------------------------------------===//
1105//                               Loop Splitting
1106//===----------------------------------------------------------------------===//
1107
1108void SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
1109  SplitAnalysis::LoopBlocks Blocks;
1110  sa_.getLoopBlocks(Loop, Blocks);
1111
1112  DEBUG({
1113    dbgs() << "  splitAround"; sa_.print(Blocks, dbgs()); dbgs() << '\n';
1114  });
1115
1116  // Break critical edges as needed.
1117  SplitAnalysis::BlockPtrSet CriticalExits;
1118  sa_.getCriticalExits(Blocks, CriticalExits);
1119  assert(CriticalExits.empty() && "Cannot break critical exits yet");
1120
1121  // Get critical predecessors so computeRemainder can deal with them.
1122  sa_.getCriticalPreds(Blocks, criticalPreds_);
1123
1124  // Create new live interval for the loop.
1125  openIntv();
1126
1127  // Insert copies in the predecessors if live-in to the header.
1128  if (lis_.isLiveInToMBB(edit_.getParent(), Loop->getHeader())) {
1129    for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(),
1130           E = Blocks.Preds.end(); I != E; ++I) {
1131      MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
1132      enterIntvAtEnd(MBB);
1133    }
1134  }
1135
1136  // Switch all loop blocks.
1137  for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
1138       E = Blocks.Loop.end(); I != E; ++I)
1139     useIntv(**I);
1140
1141  // Insert back copies in the exit blocks.
1142  for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(),
1143       E = Blocks.Exits.end(); I != E; ++I) {
1144    MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
1145    leaveIntvAtTop(MBB);
1146  }
1147
1148  // Done.
1149  closeIntv();
1150  finish();
1151}
1152
1153
1154//===----------------------------------------------------------------------===//
1155//                            Single Block Splitting
1156//===----------------------------------------------------------------------===//
1157
1158/// getMultiUseBlocks - if curli has more than one use in a basic block, it
1159/// may be an advantage to split curli for the duration of the block.
1160bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
1161  // If curli is local to one block, there is no point to splitting it.
1162  if (usingBlocks_.size() <= 1)
1163    return false;
1164  // Add blocks with multiple uses.
1165  for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
1166       I != E; ++I)
1167    switch (I->second) {
1168    case 0:
1169    case 1:
1170      continue;
1171    case 2: {
1172      // When there are only two uses and curli is both live in and live out,
1173      // we don't really win anything by isolating the block since we would be
1174      // inserting two copies.
1175      // The remaing register would still have two uses in the block. (Unless it
1176      // separates into disconnected components).
1177      if (lis_.isLiveInToMBB(*curli_, I->first) &&
1178          lis_.isLiveOutOfMBB(*curli_, I->first))
1179        continue;
1180    } // Fall through.
1181    default:
1182      Blocks.insert(I->first);
1183    }
1184  return !Blocks.empty();
1185}
1186
1187/// splitSingleBlocks - Split curli into a separate live interval inside each
1188/// basic block in Blocks.
1189void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
1190  DEBUG(dbgs() << "  splitSingleBlocks for " << Blocks.size() << " blocks.\n");
1191  // Determine the first and last instruction using curli in each block.
1192  typedef std::pair<SlotIndex,SlotIndex> IndexPair;
1193  typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap;
1194  IndexPairMap MBBRange;
1195  for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1196       E = sa_.usingInstrs_.end(); I != E; ++I) {
1197    const MachineBasicBlock *MBB = (*I)->getParent();
1198    if (!Blocks.count(MBB))
1199      continue;
1200    SlotIndex Idx = lis_.getInstructionIndex(*I);
1201    DEBUG(dbgs() << "  BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I);
1202    IndexPair &IP = MBBRange[MBB];
1203    if (!IP.first.isValid() || Idx < IP.first)
1204      IP.first = Idx;
1205    if (!IP.second.isValid() || Idx > IP.second)
1206      IP.second = Idx;
1207  }
1208
1209  // Create a new interval for each block.
1210  for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(),
1211       E = Blocks.end(); I != E; ++I) {
1212    IndexPair &IP = MBBRange[*I];
1213    DEBUG(dbgs() << "  splitting for BB#" << (*I)->getNumber() << ": ["
1214                 << IP.first << ';' << IP.second << ")\n");
1215    assert(IP.first.isValid() && IP.second.isValid());
1216
1217    openIntv();
1218    enterIntvBefore(IP.first);
1219    useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex());
1220    leaveIntvAfter(IP.second);
1221    closeIntv();
1222  }
1223  finish();
1224}
1225
1226
1227//===----------------------------------------------------------------------===//
1228//                            Sub Block Splitting
1229//===----------------------------------------------------------------------===//
1230
1231/// getBlockForInsideSplit - If curli is contained inside a single basic block,
1232/// and it wou pay to subdivide the interval inside that block, return it.
1233/// Otherwise return NULL. The returned block can be passed to
1234/// SplitEditor::splitInsideBlock.
1235const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
1236  // The interval must be exclusive to one block.
1237  if (usingBlocks_.size() != 1)
1238    return 0;
1239  // Don't to this for less than 4 instructions. We want to be sure that
1240  // splitting actually reduces the instruction count per interval.
1241  if (usingInstrs_.size() < 4)
1242    return 0;
1243  return usingBlocks_.begin()->first;
1244}
1245
1246/// splitInsideBlock - Split curli into multiple intervals inside MBB.
1247void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
1248  SmallVector<SlotIndex, 32> Uses;
1249  Uses.reserve(sa_.usingInstrs_.size());
1250  for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1251       E = sa_.usingInstrs_.end(); I != E; ++I)
1252    if ((*I)->getParent() == MBB)
1253      Uses.push_back(lis_.getInstructionIndex(*I));
1254  DEBUG(dbgs() << "  splitInsideBlock BB#" << MBB->getNumber() << " for "
1255               << Uses.size() << " instructions.\n");
1256  assert(Uses.size() >= 3 && "Need at least 3 instructions");
1257  array_pod_sort(Uses.begin(), Uses.end());
1258
1259  // Simple algorithm: Find the largest gap between uses as determined by slot
1260  // indices. Create new intervals for instructions before the gap and after the
1261  // gap.
1262  unsigned bestPos = 0;
1263  int bestGap = 0;
1264  DEBUG(dbgs() << "    dist (" << Uses[0]);
1265  for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
1266    int g = Uses[i-1].distance(Uses[i]);
1267    DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
1268    if (g > bestGap)
1269      bestPos = i, bestGap = g;
1270  }
1271  DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1272
1273  // bestPos points to the first use after the best gap.
1274  assert(bestPos > 0 && "Invalid gap");
1275
1276  // FIXME: Don't create intervals for low densities.
1277
1278  // First interval before the gap. Don't create single-instr intervals.
1279  if (bestPos > 1) {
1280    openIntv();
1281    enterIntvBefore(Uses.front());
1282    useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex());
1283    leaveIntvAfter(Uses[bestPos-1]);
1284    closeIntv();
1285  }
1286
1287  // Second interval after the gap.
1288  if (bestPos < Uses.size()-1) {
1289    openIntv();
1290    enterIntvBefore(Uses[bestPos]);
1291    useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex());
1292    leaveIntvAfter(Uses.back());
1293    closeIntv();
1294  }
1295
1296  finish();
1297}
1298