1 //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the SplitAnalysis class as well as mutator functions for
11 // live range splitting.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "regalloc"
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"
35 AllowSplit("spiller-splits-edges",
36 cl::desc("Allow critical edge splitting during spilling"));
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
42 /// compute - Compute the edge bundles for MF. Bundles depend only on the CFG.
43 void EdgeBundles::compute(const MachineFunction *mf) {
46 EC.grow(2 * MF->size());
48 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
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());
60 /// view - Visualize the annotated bipartite CFG with Graphviz.
61 void EdgeBundles::view() const {
62 ViewGraph(*this, "EdgeBundles");
65 /// Specialize WriteGraph, the standard implementation won't work.
66 raw_ostream &llvm::WriteGraph(raw_ostream &O, const EdgeBundles &G,
68 const std::string &Title) {
69 const MachineFunction *MF = G.getMachineFunction();
72 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
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';
84 //===----------------------------------------------------------------------===//
86 //===----------------------------------------------------------------------===//
88 SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
89 const LiveIntervals &lis,
90 const MachineLoopInfo &mli)
94 tii_(*mf.getTarget().getInstrInfo()),
97 void SplitAnalysis::clear() {
104 bool 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);
110 /// analyzeUses - Count instructions, basic blocks, and loops using curli.
111 void 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))
117 MachineBasicBlock *MBB = MI->getParent();
118 if (usingBlocks_[MBB]++)
120 for (MachineLoop *Loop = loops_.getLoopFor(MBB); Loop;
121 Loop = Loop->getParentLoop())
124 DEBUG(dbgs() << " counted "
125 << usingInstrs_.size() << " instrs, "
126 << usingBlocks_.size() << " blocks, "
127 << usingLoops_.size() << " loops.\n");
130 void 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();
135 OS << '(' << count << ')';
139 // Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
140 // predecessor blocks, and exit blocks.
141 void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
144 // Blocks in the loop.
145 Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
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);
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);
165 void SplitAnalysis::print(const LoopBlocks &B, raw_ostream &OS) const {
174 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
175 /// and around the Loop.
176 SplitAnalysis::LoopPeripheralUse SplitAnalysis::
177 analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
178 LoopPeripheralUse use = ContainedInLoop;
179 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
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;
189 // Is it a loop block?
190 if (Blocks.Loop.count(MBB))
192 // It must be an unrelated block.
193 DEBUG(dbgs() << ", outside: BB#" << MBB->getNumber());
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
202 void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
203 BlockPtrSet &CriticalExits) {
204 CriticalExits.clear();
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();
211 const MachineBasicBlock *Exit = *I;
212 // A single-predecessor exit block is definitely not a critical edge.
213 if (Exit->pred_size() == 1)
215 // This exit may not have curli live in at all. No need to split.
216 if (!lis_.isLiveInToMBB(*curli_, Exit))
218 // Does this exit block have a predecessor that is not a loop block or loop
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))
225 // This is a critical exit block, and we need to split the exit edge.
226 CriticalExits.insert(Exit);
232 void SplitAnalysis::getCriticalPreds(const SplitAnalysis::LoopBlocks &Blocks,
233 BlockPtrSet &CriticalPreds) {
234 CriticalPreds.clear();
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();
242 const MachineBasicBlock *Pred = *I;
243 // Definitely not a critical edge.
244 if (Pred->succ_size() == 1)
246 // This block may not have curli live out at all if there is a PHI.
247 if (!lis_.isLiveOutOfMBB(*curli_, Pred))
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))
255 if (!lis_.isLiveInToMBB(*curli_, Succ))
257 // This is a critical predecessor block.
258 CriticalPreds.insert(Pred);
264 /// canSplitCriticalExits - Return true if it is possible to insert new exit
265 /// blocks before the blocks in CriticalExits.
267 SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
268 BlockPtrSet &CriticalExits) {
269 // If we don't allow critical edge splitting, require no critical exits.
271 return CriticalExits.empty();
273 for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
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))
285 if (!canAnalyzeBranch(Pred))
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())
294 if (!canAnalyzeBranch(--MFI))
297 // No problems found.
301 void SplitAnalysis::analyze(const LiveInterval *li) {
307 void SplitAnalysis::getSplitLoops(LoopPtrSet &Loops) {
308 assert(curli_ && "Call analyze() before getSplitLoops");
309 if (usingLoops_.empty())
313 BlockPtrSet CriticalExits;
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()); });
322 switch(analyzeLoopPeripheralUse(Blocks)) {
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
331 // For safety, stick to splitting live ranges with uses outside the
333 DEBUG(dbgs() << ": multiple peripheral uses");
335 case ContainedInLoop:
336 DEBUG(dbgs() << ": fully contained\n");
338 case SinglePeripheral:
339 DEBUG(dbgs() << ": single peripheral use\n");
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))
347 // This is a possible split.
351 DEBUG(dbgs() << " getSplitLoops found " << Loops.size()
352 << " candidate loops.\n");
355 const MachineLoop *SplitAnalysis::getBestSplitLoop() {
357 getSplitLoops(Loops);
361 // Pick the earliest loop.
362 // FIXME: Are there other heuristics to consider?
363 const MachineLoop *Best = 0;
365 for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
367 SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
368 if (!Best || Idx < BestIdx)
369 Best = *I, BestIdx = Idx;
371 DEBUG(dbgs() << " getBestSplitLoop found " << *Best);
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.
378 bool 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());
385 /// getBypassLoops - Get all the maximal bypass loops. These are the bypass
386 /// loops whose parent is not a bypass loop.
387 void 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.
399 // The child loops may be bypass loops.
400 Todo.append(Loop->begin(), Loop->end());
405 //===----------------------------------------------------------------------===//
407 //===----------------------------------------------------------------------===//
409 // Work around the fact that the std::pair constructors are broken for pointer
410 // pairs in some implementations. makeVV(x, 0) works.
411 static inline std::pair<const VNInfo*, VNInfo*>
412 makeVV(const VNInfo *a, VNInfo *b) {
413 return std::make_pair(a, b);
416 void LiveIntervalMap::reset(LiveInterval *li) {
419 liveOutCache_.clear();
422 bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const {
423 ValueMap::const_iterator i = valueMap_.find(ParentVNI);
424 return i != valueMap_.end() && i->second == 0;
427 // defValue - Introduce a li_ def for ParentVNI that could be later than
429 VNInfo *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");
435 // Create a new value.
436 VNInfo *VNI = li_->getNextValue(Idx, 0, lis_.getVNInfoAllocator());
438 // Preserve the PHIDef bit.
439 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
440 VNI->setIsPHIDef(true);
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));
446 // This is now a complex def. Mark with a NULL in valueMap.
448 InsP.first->second = 0;
454 // mapValue - Find the mapped value for ParentVNI at Idx.
455 // Potentially create phi-def values.
456 VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
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");
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));
467 // This was an unknown value. Create a simple mapping.
469 if (simple) *simple = true;
470 return InsP.first->second = li_->createValueCopy(ParentVNI,
471 lis_.getVNInfoAllocator());
474 // This was a simple mapped value.
475 if (InsP.first->second) {
476 if (simple) *simple = true;
477 return InsP.first->second;
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");
486 // Is there a def in the same MBB we can extend?
487 if (VNInfo *VNI = extendTo(IdxMBB, Idx))
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');
497 // Blocks where li_ should be live-in.
498 SmallVector<MachineDomTreeNode*, 16> LiveIn;
499 LiveIn.push_back(mdt_[IdxMBB]);
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.
512 DEBUG(if (VNInfo *VNI = LOIP.first->second.first)
513 dbgs() << " known valno #" << VNI->id
514 << " at BB#" << Pred->getNumber() << '\n');
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;
527 LOP.second = mdt_[DefMBB];
530 // No, we need a live-in value for Pred as well
532 LiveIn.push_back(mdt_[Pred]);
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.
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;
556 // Get the IDom live-out value.
558 LiveOutMap::iterator I = liveOutCache_.find(IDom->getBlock());
559 if (I != liveOutCache_.end())
560 IDomValue = I->second;
562 // If IDom is outside our set of live-out blocks, there must be new
563 // defs, and we need a phi-def here.
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.
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)
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)) {
586 // Create a phi-def if required.
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.
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));
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);
610 } else if (IDomValue.first) {
611 // No phi-def here. Remember incoming value for 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) {
620 I->second = IDomValue;
621 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
622 << " idom valno #" << IDomValue.first->id
623 << " from BB#" << IDom->getBlock()->getNumber() << '\n');
627 DEBUG(dbgs() << " - made " << Changes << " changes.\n");
630 assert(IdxVNI && "Didn't find value for Idx");
633 // Check the liveOutCache_ invariants.
634 for (LiveOutMap::iterator I = liveOutCache_.begin(), E = liveOutCache_.end();
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)
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");
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);
655 li_->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
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));
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.
670 VNInfo *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())
676 if (I->end <= lis_.getMBBStartIdx(MBB))
679 I->end = Idx.getNextSlot();
683 // addSimpleRange - Add a simple range from parentli_ to li_.
684 // ParentVNI must be live in the [Start;End) interval.
685 void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
686 const VNInfo *ParentVNI) {
687 assert(li_ && "call reset first");
689 VNInfo *VNI = mapValue(ParentVNI, Start, &simple);
690 // A simple mapping is easy.
692 li_->addRange(LiveRange(Start, End, VNI));
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());
701 li_->addRange(LiveRange(Start, End, VNI));
706 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
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)));
716 Start = lis_.getMBBStartIdx(MBB);
718 li_->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
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.
724 void 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);
729 // Check if --I begins before Start and overlaps.
733 addSimpleRange(Start, std::min(End, I->end), I->valno);
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);
743 //===----------------------------------------------------------------------===//
745 //===----------------------------------------------------------------------===//
747 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
748 SplitEditor::SplitEditor(SplitAnalysis &sa,
751 MachineDominatorTree &mdt,
753 : sa_(sa), lis_(lis), vrm_(vrm),
754 mri_(vrm.getMachineFunction().getRegInfo()),
755 tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()),
756 tri_(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
758 dupli_(lis_, mdt, edit.getParent()),
759 openli_(lis_, mdt, edit.getParent())
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);
766 bool 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))
773 VNInfo *SplitEditor::defFromParent(LiveIntervalMap &Reg,
776 MachineBasicBlock &MBB,
777 MachineBasicBlock::iterator I) {
779 MachineInstr *CopyMI = 0;
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,
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();
794 // Define the value in Reg.
795 VNI = Reg.defValue(ParentVNI, Def);
796 VNI->setCopy(CopyMI);
798 // Add minimal liveness for the new value.
801 Reg.getLI()->addRange(LiveRange(Def, UseIdx.getNextSlot(), VNI));
805 /// Create a new virtual register and live interval.
806 void SplitEditor::openIntv() {
807 assert(!openli_.getLI() && "Previous LI not closed before openIntv");
809 dupli_.reset(&edit_.create(mri_, lis_, vrm_));
811 openli_.reset(&edit_.create(mri_, lis_, vrm_));
814 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
815 /// not live before Idx, a COPY is not inserted.
816 void 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);
822 DEBUG(dbgs() << ": not live\n");
825 DEBUG(dbgs() << ": valno " << ParentVNI->id);
826 truncatedValues.insert(ParentVNI);
827 MachineInstr *MI = lis_.getInstructionFromIndex(Idx);
828 assert(MI && "enterIntvBefore called with invalid index");
830 defFromParent(openli_, ParentVNI, Idx, *MI->getParent(), MI);
832 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
835 /// enterIntvAtEnd - Enter openli at the end of MBB.
836 void 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);
842 DEBUG(dbgs() << ": not live\n");
845 DEBUG(dbgs() << ": valno " << ParentVNI->id);
846 truncatedValues.insert(ParentVNI);
847 defFromParent(openli_, ParentVNI, End, MBB, MBB.getFirstTerminator());
848 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
851 /// useIntv - indicate that all instructions in MBB should use openli.
852 void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
853 useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB));
856 void 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');
863 /// leaveIntvAfter - Leave openli after the instruction at Idx.
864 void SplitEditor::leaveIntvAfter(SlotIndex Idx) {
865 assert(openli_.getLI() && "openIntv not called before leaveIntvAfter");
866 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
868 // The interval must be live beyond the instruction at Idx.
869 Idx = Idx.getBoundaryIndex();
870 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx);
872 DEBUG(dbgs() << ": not live\n");
875 DEBUG(dbgs() << ": valno " << ParentVNI->id);
877 MachineBasicBlock::iterator MII = lis_.getInstructionFromIndex(Idx);
878 VNInfo *VNI = defFromParent(dupli_, ParentVNI, Idx,
879 *MII->getParent(), llvm::next(MII));
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);
885 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
888 /// leaveIntvAtTop - Leave the interval at the top of MBB.
889 /// Currently, only one value can leave the interval.
890 void 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);
895 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Start);
897 DEBUG(dbgs() << ": not live\n");
901 VNInfo *VNI = defFromParent(dupli_, ParentVNI, Start, MBB,
902 MBB.SkipPHIsAndLabels(MBB.begin()));
904 // Finally we must make sure that openli is properly extended from Start to
906 openli_.addSimpleRange(Start, VNI->def, ParentVNI);
907 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
910 /// closeIntv - Indicate that we are done editing the currently open
911 /// LiveInterval, and ranges can be trimmed.
912 void SplitEditor::closeIntv() {
913 assert(openli_.getLI() && "openIntv not called before closeIntv");
915 DEBUG(dbgs() << " closeIntv cleaning up\n");
916 DEBUG(dbgs() << " open " << *openli_.getLI() << '\n');
920 /// rewrite - Rewrite all uses of reg to use the new registers.
921 void 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();
928 if (MI->isDebugValue()) {
929 DEBUG(dbgs() << "Zapping " << *MI);
930 // FIXME: We can do much better with debug values.
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;
939 LiveInterval *testli = *I;
940 if (testli->liveAt(Idx)) {
945 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'<< Idx);
946 assert(LI && "No register was live at use");
948 if (MO.isUse() && !MI->isRegTiedToDefOperand(OpNum))
949 MO.setIsKill(LI->killedAt(Idx.getDefIndex()));
950 DEBUG(dbgs() << '\t' << *MI);
955 SplitEditor::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;
962 if (*LI == dupli_.getLI())
964 LiveInterval::const_iterator I = (*LI)->find(Start);
965 LiveInterval::const_iterator E = (*LI)->end();
967 Iters.push_back(std::make_pair(I, E));
970 SlotIndex sidx = Start;
971 // Break [Start;End) into segments that don't overlap any intervals.
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) {
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);
988 // Trim eidx too if needed.
989 if (I->start >= eidx)
994 // Now, [sidx;eidx) doesn't overlap anything in intervals_.
996 dupli_.addSimpleRange(sidx, eidx, VNI);
997 // If the interval end was truncated, we can try again from next.
1004 void 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();
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())
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);
1028 deadValues.insert(VNI);
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));
1036 // Add all ranges to dupli.
1037 for (LiveInterval::const_iterator I = parent.begin(), E = parent.end();
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);
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())
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();
1060 void SplitEditor::finish() {
1061 assert(!openli_.getLI() && "Previous LI not closed before rewrite");
1062 assert(dupli_.getLI() && "No dupli for rewrite. Noop spilt?");
1064 // Complete dupli liveness.
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_);
1071 // Rewrite instructions.
1072 rewrite(edit_.getReg());
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);
1082 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
1083 SmallVector<LiveInterval*, 8> dups;
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.
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');
1104 //===----------------------------------------------------------------------===//
1106 //===----------------------------------------------------------------------===//
1108 void SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
1109 SplitAnalysis::LoopBlocks Blocks;
1110 sa_.getLoopBlocks(Loop, Blocks);
1113 dbgs() << " splitAround"; sa_.print(Blocks, dbgs()); dbgs() << '\n';
1116 // Break critical edges as needed.
1117 SplitAnalysis::BlockPtrSet CriticalExits;
1118 sa_.getCriticalExits(Blocks, CriticalExits);
1119 assert(CriticalExits.empty() && "Cannot break critical exits yet");
1121 // Get critical predecessors so computeRemainder can deal with them.
1122 sa_.getCriticalPreds(Blocks, criticalPreds_);
1124 // Create new live interval for the loop.
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);
1136 // Switch all loop blocks.
1137 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
1138 E = Blocks.Loop.end(); I != E; ++I)
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);
1154 //===----------------------------------------------------------------------===//
1155 // Single Block Splitting
1156 //===----------------------------------------------------------------------===//
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.
1160 bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
1161 // If curli is local to one block, there is no point to splitting it.
1162 if (usingBlocks_.size() <= 1)
1164 // Add blocks with multiple uses.
1165 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
1167 switch (I->second) {
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))
1182 Blocks.insert(I->first);
1184 return !Blocks.empty();
1187 /// splitSingleBlocks - Split curli into a separate live interval inside each
1188 /// basic block in Blocks.
1189 void 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))
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)
1205 if (!IP.second.isValid() || Idx > IP.second)
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());
1218 enterIntvBefore(IP.first);
1219 useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex());
1220 leaveIntvAfter(IP.second);
1227 //===----------------------------------------------------------------------===//
1228 // Sub Block Splitting
1229 //===----------------------------------------------------------------------===//
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.
1235 const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
1236 // The interval must be exclusive to one block.
1237 if (usingBlocks_.size() != 1)
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)
1243 return usingBlocks_.begin()->first;
1246 /// splitInsideBlock - Split curli into multiple intervals inside MBB.
1247 void 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());
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
1262 unsigned bestPos = 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]);
1269 bestPos = i, bestGap = g;
1271 DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1273 // bestPos points to the first use after the best gap.
1274 assert(bestPos > 0 && "Invalid gap");
1276 // FIXME: Don't create intervals for low densities.
1278 // First interval before the gap. Don't create single-instr intervals.
1281 enterIntvBefore(Uses.front());
1282 useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex());
1283 leaveIntvAfter(Uses[bestPos-1]);
1287 // Second interval after the gap.
1288 if (bestPos < Uses.size()-1) {
1290 enterIntvBefore(Uses[bestPos]);
1291 useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex());
1292 leaveIntvAfter(Uses.back());