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 "splitter"
16 #include "SplitKit.h"
17 #include "VirtRegMap.h"
18 #include "llvm/CodeGen/CalcSpillWeights.h"
19 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineLoopInfo.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetMachine.h"
28 
29 using namespace llvm;
30 
31 static cl::opt<bool>
32 AllowSplit("spiller-splits-edges",
33            cl::desc("Allow critical edge splitting during spilling"));
34 
35 //===----------------------------------------------------------------------===//
36 //                                 Split Analysis
37 //===----------------------------------------------------------------------===//
38 
39 SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
40                              const LiveIntervals &lis,
41                              const MachineLoopInfo &mli)
42   : mf_(mf),
43     lis_(lis),
44     loops_(mli),
45     tii_(*mf.getTarget().getInstrInfo()),
46     curli_(0) {}
47 
48 void SplitAnalysis::clear() {
49   usingInstrs_.clear();
50   usingBlocks_.clear();
51   usingLoops_.clear();
52   curli_ = 0;
53 }
54 
55 bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
56   MachineBasicBlock *T, *F;
57   SmallVector<MachineOperand, 4> Cond;
58   return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
59 }
60 
61 /// analyzeUses - Count instructions, basic blocks, and loops using curli.
62 void SplitAnalysis::analyzeUses() {
63   const MachineRegisterInfo &MRI = mf_.getRegInfo();
64   for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
65        MachineInstr *MI = I.skipInstruction();) {
66     if (MI->isDebugValue() || !usingInstrs_.insert(MI))
67       continue;
68     MachineBasicBlock *MBB = MI->getParent();
69     if (usingBlocks_[MBB]++)
70       continue;
71     if (MachineLoop *Loop = loops_.getLoopFor(MBB))
72       usingLoops_[Loop]++;
73   }
74   DEBUG(dbgs() << "  counted "
75                << usingInstrs_.size() << " instrs, "
76                << usingBlocks_.size() << " blocks, "
77                << usingLoops_.size()  << " loops.\n");
78 }
79 
80 /// removeUse - Update statistics by noting that MI no longer uses curli.
81 void SplitAnalysis::removeUse(const MachineInstr *MI) {
82   if (!usingInstrs_.erase(MI))
83     return;
84 
85   // Decrement MBB count.
86   const MachineBasicBlock *MBB = MI->getParent();
87   BlockCountMap::iterator bi = usingBlocks_.find(MBB);
88   assert(bi != usingBlocks_.end() && "MBB missing");
89   assert(bi->second && "0 count in map");
90   if (--bi->second)
91     return;
92   // No more uses in MBB.
93   usingBlocks_.erase(bi);
94 
95   // Decrement loop count.
96   MachineLoop *Loop = loops_.getLoopFor(MBB);
97   if (!Loop)
98     return;
99   LoopCountMap::iterator li = usingLoops_.find(Loop);
100   assert(li != usingLoops_.end() && "Loop missing");
101   assert(li->second && "0 count in map");
102   if (--li->second)
103     return;
104   // No more blocks in Loop.
105   usingLoops_.erase(li);
106 }
107 
108 // Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
109 // predecessor blocks, and exit blocks.
110 void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
111   Blocks.clear();
112 
113   // Blocks in the loop.
114   Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
115 
116   // Predecessor blocks.
117   const MachineBasicBlock *Header = Loop->getHeader();
118   for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
119        E = Header->pred_end(); I != E; ++I)
120     if (!Blocks.Loop.count(*I))
121       Blocks.Preds.insert(*I);
122 
123   // Exit blocks.
124   for (MachineLoop::block_iterator I = Loop->block_begin(),
125        E = Loop->block_end(); I != E; ++I) {
126     const MachineBasicBlock *MBB = *I;
127     for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
128        SE = MBB->succ_end(); SI != SE; ++SI)
129       if (!Blocks.Loop.count(*SI))
130         Blocks.Exits.insert(*SI);
131   }
132 }
133 
134 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
135 /// and around the Loop.
136 SplitAnalysis::LoopPeripheralUse SplitAnalysis::
137 analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
138   LoopPeripheralUse use = ContainedInLoop;
139   for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
140        I != E; ++I) {
141     const MachineBasicBlock *MBB = I->first;
142     // Is this a peripheral block?
143     if (use < MultiPeripheral &&
144         (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
145       if (I->second > 1) use = MultiPeripheral;
146       else               use = SinglePeripheral;
147       continue;
148     }
149     // Is it a loop block?
150     if (Blocks.Loop.count(MBB))
151       continue;
152     // It must be an unrelated block.
153     return OutsideLoop;
154   }
155   return use;
156 }
157 
158 /// getCriticalExits - It may be necessary to partially break critical edges
159 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
160 /// blocks that need special treatment into CriticalExits.
161 void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
162                                      BlockPtrSet &CriticalExits) {
163   CriticalExits.clear();
164 
165   // A critical exit block contains a phi def of curli, and has a predecessor
166   // that is not in the loop nor a loop predecessor.
167   // For such an exit block, the edges carrying the new variable must be moved
168   // to a new pre-exit block.
169   for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
170        I != E; ++I) {
171     const MachineBasicBlock *Succ = *I;
172     SlotIndex SuccIdx = lis_.getMBBStartIdx(Succ);
173     VNInfo *SuccVNI = curli_->getVNInfoAt(SuccIdx);
174     // This exit may not have curli live in at all. No need to split.
175     if (!SuccVNI)
176       continue;
177     // If this is not a PHI def, it is either using a value from before the
178     // loop, or a value defined inside the loop. Both are safe.
179     if (!SuccVNI->isPHIDef() || SuccVNI->def.getBaseIndex() != SuccIdx)
180       continue;
181     // This exit block does have a PHI. Does it also have a predecessor that is
182     // not a loop block or loop predecessor?
183     for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
184          PE = Succ->pred_end(); PI != PE; ++PI) {
185       const MachineBasicBlock *Pred = *PI;
186       if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
187         continue;
188       // This is a critical exit block, and we need to split the exit edge.
189       CriticalExits.insert(Succ);
190       break;
191     }
192   }
193 }
194 
195 /// canSplitCriticalExits - Return true if it is possible to insert new exit
196 /// blocks before the blocks in CriticalExits.
197 bool
198 SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
199                                      BlockPtrSet &CriticalExits) {
200   // If we don't allow critical edge splitting, require no critical exits.
201   if (!AllowSplit)
202     return CriticalExits.empty();
203 
204   for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
205        I != E; ++I) {
206     const MachineBasicBlock *Succ = *I;
207     // We want to insert a new pre-exit MBB before Succ, and change all the
208     // in-loop blocks to branch to the pre-exit instead of Succ.
209     // Check that all the in-loop predecessors can be changed.
210     for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
211          PE = Succ->pred_end(); PI != PE; ++PI) {
212       const MachineBasicBlock *Pred = *PI;
213       // The external predecessors won't be altered.
214       if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
215         continue;
216       if (!canAnalyzeBranch(Pred))
217         return false;
218     }
219 
220     // If Succ's layout predecessor falls through, that too must be analyzable.
221     // We need to insert the pre-exit block in the gap.
222     MachineFunction::const_iterator MFI = Succ;
223     if (MFI == mf_.begin())
224       continue;
225     if (!canAnalyzeBranch(--MFI))
226       return false;
227   }
228   // No problems found.
229   return true;
230 }
231 
232 void SplitAnalysis::analyze(const LiveInterval *li) {
233   clear();
234   curli_ = li;
235   analyzeUses();
236 }
237 
238 const MachineLoop *SplitAnalysis::getBestSplitLoop() {
239   assert(curli_ && "Call analyze() before getBestSplitLoop");
240   if (usingLoops_.empty())
241     return 0;
242 
243   LoopPtrSet Loops, SecondLoops;
244   LoopBlocks Blocks;
245   BlockPtrSet CriticalExits;
246 
247   // Find first-class and second class candidate loops.
248   // We prefer to split around loops where curli is used outside the periphery.
249   for (LoopCountMap::const_iterator I = usingLoops_.begin(),
250        E = usingLoops_.end(); I != E; ++I) {
251     const MachineLoop *Loop = I->first;
252     getLoopBlocks(Loop, Blocks);
253 
254     // FIXME: We need an SSA updater to properly handle multiple exit blocks.
255     if (Blocks.Exits.size() > 1) {
256       DEBUG(dbgs() << "  multiple exits from " << *Loop);
257       continue;
258     }
259 
260     LoopPtrSet *LPS = 0;
261     switch(analyzeLoopPeripheralUse(Blocks)) {
262     case OutsideLoop:
263       LPS = &Loops;
264       break;
265     case MultiPeripheral:
266       LPS = &SecondLoops;
267       break;
268     case ContainedInLoop:
269       DEBUG(dbgs() << "  contained in " << *Loop);
270       continue;
271     case SinglePeripheral:
272       DEBUG(dbgs() << "  single peripheral use in " << *Loop);
273       continue;
274     }
275     // Will it be possible to split around this loop?
276     getCriticalExits(Blocks, CriticalExits);
277     DEBUG(dbgs() << "  " << CriticalExits.size() << " critical exits from "
278                  << *Loop);
279     if (!canSplitCriticalExits(Blocks, CriticalExits))
280       continue;
281     // This is a possible split.
282     assert(LPS);
283     LPS->insert(Loop);
284   }
285 
286   DEBUG(dbgs() << "  getBestSplitLoop found " << Loops.size() << " + "
287                << SecondLoops.size() << " candidate loops.\n");
288 
289   // If there are no first class loops available, look at second class loops.
290   if (Loops.empty())
291     Loops = SecondLoops;
292 
293   if (Loops.empty())
294     return 0;
295 
296   // Pick the earliest loop.
297   // FIXME: Are there other heuristics to consider?
298   const MachineLoop *Best = 0;
299   SlotIndex BestIdx;
300   for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
301        ++I) {
302     SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
303     if (!Best || Idx < BestIdx)
304       Best = *I, BestIdx = Idx;
305   }
306   DEBUG(dbgs() << "  getBestSplitLoop found " << *Best);
307   return Best;
308 }
309 
310 /// getMultiUseBlocks - if curli has more than one use in a basic block, it
311 /// may be an advantage to split curli for the duration of the block.
312 bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
313   // If curli is local to one block, there is no point to splitting it.
314   if (usingBlocks_.size() <= 1)
315     return false;
316   // Add blocks with multiple uses.
317   for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
318        I != E; ++I)
319     switch (I->second) {
320     case 0:
321     case 1:
322       continue;
323     case 2: {
324       // It doesn't pay to split a 2-instr block if it redefines curli.
325       VNInfo *VN1 = curli_->getVNInfoAt(lis_.getMBBStartIdx(I->first));
326       VNInfo *VN2 =
327         curli_->getVNInfoAt(lis_.getMBBEndIdx(I->first).getPrevIndex());
328       // live-in and live-out with a different value.
329       if (VN1 && VN2 && VN1 != VN2)
330         continue;
331     } // Fall through.
332     default:
333       Blocks.insert(I->first);
334     }
335   return !Blocks.empty();
336 }
337 
338 //===----------------------------------------------------------------------===//
339 //                               LiveIntervalMap
340 //===----------------------------------------------------------------------===//
341 
342 // Work around the fact that the std::pair constructors are broken for pointer
343 // pairs in some implementations. makeVV(x, 0) works.
344 static inline std::pair<const VNInfo*, VNInfo*>
345 makeVV(const VNInfo *a, VNInfo *b) {
346   return std::make_pair(a, b);
347 }
348 
349 // defValue - Introduce a li_ def for ParentVNI that could be later than
350 // ParentVNI->def.
351 VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) {
352   assert(ParentVNI && "Mapping  NULL value");
353   assert(Idx.isValid() && "Invalid SlotIndex");
354   assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
355 
356   // Is this a simple 1-1 mapping? Not likely.
357   if (Idx == ParentVNI->def)
358     return mapValue(ParentVNI, Idx);
359 
360   // This is a complex def. Mark with a NULL in valueMap.
361   VNInfo *&OldVNI = valueMap_[ParentVNI];
362   assert(!OldVNI && "Simple/Complex values mixed");
363   OldVNI = 0;
364 
365   // Should we insert a minimal snippet of VNI LiveRange, or can we count on
366   // callers to do that? We need it for lookups of complex values.
367   VNInfo *VNI = li_.getNextValue(Idx, 0, true, lis_.getVNInfoAllocator());
368   return VNI;
369 }
370 
371 // mapValue - Find the mapped value for ParentVNI at Idx.
372 // Potentially create phi-def values.
373 VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx) {
374   assert(ParentVNI && "Mapping  NULL value");
375   assert(Idx.isValid() && "Invalid SlotIndex");
376   assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
377 
378   // Use insert for lookup, so we can add missing values with a second lookup.
379   std::pair<ValueMap::iterator,bool> InsP =
380     valueMap_.insert(makeVV(ParentVNI, 0));
381 
382   // This was an unknown value. Create a simple mapping.
383   if (InsP.second)
384     return InsP.first->second = li_.createValueCopy(ParentVNI,
385                                                     lis_.getVNInfoAllocator());
386   // This was a simple mapped value.
387   if (InsP.first->second)
388     return InsP.first->second;
389 
390   // This is a complex mapped value. There may be multiple defs, and we may need
391   // to create phi-defs.
392   MachineBasicBlock *IdxMBB = lis_.getMBBFromIndex(Idx);
393   assert(IdxMBB && "No MBB at Idx");
394 
395   // Is there a def in the same MBB we can extend?
396   if (VNInfo *VNI = extendTo(IdxMBB, Idx))
397     return VNI;
398 
399   // Now for the fun part. We know that ParentVNI potentially has multiple defs,
400   // and we may need to create even more phi-defs to preserve VNInfo SSA form.
401   // Perform a depth-first search for predecessor blocks where we know the
402   // dominating VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
403 
404   // Track MBBs where we have created or learned the dominating value.
405   // This may change during the DFS as we create new phi-defs.
406   typedef DenseMap<MachineBasicBlock*, VNInfo*> MBBValueMap;
407   MBBValueMap DomValue;
408 
409   for (idf_iterator<MachineBasicBlock*>
410          IDFI = idf_begin(IdxMBB),
411          IDFE = idf_end(IdxMBB); IDFI != IDFE;) {
412     MachineBasicBlock *MBB = *IDFI;
413     SlotIndex End = lis_.getMBBEndIdx(MBB);
414 
415     // We are operating on the restricted CFG where ParentVNI is live.
416     if (parentli_.getVNInfoAt(End.getPrevSlot()) != ParentVNI) {
417       IDFI.skipChildren();
418       continue;
419     }
420 
421     // Do we have a dominating value in this block?
422     VNInfo *VNI = extendTo(MBB, End);
423     if (!VNI) {
424       ++IDFI;
425       continue;
426     }
427 
428     // Yes, VNI dominates MBB. Track the path back to IdxMBB, creating phi-defs
429     // as needed along the way.
430     for (unsigned PI = IDFI.getPathLength()-1; PI != 0; --PI) {
431       // Start from MBB's immediate successor. End at IdxMBB.
432       MachineBasicBlock *Succ = IDFI.getPath(PI-1);
433       std::pair<MBBValueMap::iterator, bool> InsP =
434         DomValue.insert(MBBValueMap::value_type(Succ, VNI));
435 
436       // This is the first time we backtrack to Succ.
437       if (InsP.second)
438         continue;
439 
440       // We reached Succ again with the same VNI. Nothing is going to change.
441       VNInfo *OVNI = InsP.first->second;
442       if (OVNI == VNI)
443         break;
444 
445       // Succ already has a phi-def. No need to continue.
446       SlotIndex Start = lis_.getMBBStartIdx(Succ);
447       if (OVNI->def == Start)
448         break;
449 
450       // We have a collision between the old and new VNI at Succ. That means
451       // neither dominates and we need a new phi-def.
452       VNI = li_.getNextValue(Start, 0, true, lis_.getVNInfoAllocator());
453       VNI->setIsPHIDef(true);
454       InsP.first->second = VNI;
455 
456       // Replace OVNI with VNI in the remaining path.
457       for (; PI > 1 ; --PI) {
458         MBBValueMap::iterator I = DomValue.find(IDFI.getPath(PI-2));
459         if (I == DomValue.end() || I->second != OVNI)
460           break;
461         I->second = VNI;
462       }
463     }
464 
465     // No need to search the children, we found a dominating value.
466     IDFI.skipChildren();
467   }
468 
469   // The search should at least find a dominating value for IdxMBB.
470   assert(!DomValue.empty() && "Couldn't find a reaching definition");
471 
472   // Since we went through the trouble of a full DFS visiting all reaching defs,
473   // the values in DomValue are now accurate. No more phi-defs are needed for
474   // these blocks, so we can color the live ranges.
475   // This makes the next mapValue call much faster.
476   VNInfo *IdxVNI = 0;
477   for (MBBValueMap::iterator I = DomValue.begin(), E = DomValue.end(); I != E;
478        ++I) {
479      MachineBasicBlock *MBB = I->first;
480      VNInfo *VNI = I->second;
481      SlotIndex Start = lis_.getMBBStartIdx(MBB);
482      if (MBB == IdxMBB) {
483        // Don't add full liveness to IdxMBB, stop at Idx.
484        if (Start != Idx)
485          li_.addRange(LiveRange(Start, Idx, VNI));
486        // The caller had better add some liveness to IdxVNI, or it leaks.
487        IdxVNI = VNI;
488      } else
489       li_.addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
490   }
491 
492   assert(IdxVNI && "Didn't find value for Idx");
493   return IdxVNI;
494 }
495 
496 // extendTo - Find the last li_ value defined in MBB at or before Idx. The
497 // parentli_ is assumed to be live at Idx. Extend the live range to Idx.
498 // Return the found VNInfo, or NULL.
499 VNInfo *LiveIntervalMap::extendTo(MachineBasicBlock *MBB, SlotIndex Idx) {
500   LiveInterval::iterator I = std::upper_bound(li_.begin(), li_.end(), Idx);
501   if (I == li_.begin())
502     return 0;
503   --I;
504   if (I->start < lis_.getMBBStartIdx(MBB))
505     return 0;
506   if (I->end < Idx)
507     I->end = Idx;
508   return I->valno;
509 }
510 
511 // addSimpleRange - Add a simple range from parentli_ to li_.
512 // ParentVNI must be live in the [Start;End) interval.
513 void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
514                                      const VNInfo *ParentVNI) {
515   VNInfo *VNI = mapValue(ParentVNI, Start);
516   // A simple mappoing is easy.
517   if (VNI->def == ParentVNI->def) {
518     li_.addRange(LiveRange(Start, End, VNI));
519     return;
520   }
521 
522   // ParentVNI is a complex value. We must map per MBB.
523   MachineFunction::iterator MBB = lis_.getMBBFromIndex(Start);
524   MachineFunction::iterator MBBE = lis_.getMBBFromIndex(End);
525 
526   if (MBB == MBBE) {
527     li_.addRange(LiveRange(Start, End, VNI));
528     return;
529   }
530 
531   // First block.
532   li_.addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
533 
534   // Run sequence of full blocks.
535   for (++MBB; MBB != MBBE; ++MBB) {
536     Start = lis_.getMBBStartIdx(MBB);
537     li_.addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB),
538                            mapValue(ParentVNI, Start)));
539   }
540 
541   // Final block.
542   Start = lis_.getMBBStartIdx(MBB);
543   if (Start != End)
544     li_.addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
545 }
546 
547 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
548 /// All needed values whose def is not inside [Start;End) must be defined
549 /// beforehand so mapValue will work.
550 void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) {
551   LiveInterval::const_iterator B = parentli_.begin(), E = parentli_.end();
552   LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
553 
554   // Check if --I begins before Start and overlaps.
555   if (I != B) {
556     --I;
557     if (I->end > Start)
558       addSimpleRange(Start, std::min(End, I->end), I->valno);
559     ++I;
560   }
561 
562   // The remaining ranges begin after Start.
563   for (;I != E && I->start < End; ++I)
564     addSimpleRange(I->start, std::min(End, I->end), I->valno);
565 }
566 
567 //===----------------------------------------------------------------------===//
568 //                               Split Editor
569 //===----------------------------------------------------------------------===//
570 
571 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
572 SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
573                          SmallVectorImpl<LiveInterval*> &intervals)
574   : sa_(sa), lis_(lis), vrm_(vrm),
575     mri_(vrm.getMachineFunction().getRegInfo()),
576     tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()),
577     curli_(sa_.getCurLI()),
578     dupli_(0), openli_(0),
579     intervals_(intervals),
580     firstInterval(intervals_.size())
581 {
582   assert(curli_ && "SplitEditor created from empty SplitAnalysis");
583 
584   // Make sure curli_ is assigned a stack slot, so all our intervals get the
585   // same slot as curli_.
586   if (vrm_.getStackSlot(curli_->reg) == VirtRegMap::NO_STACK_SLOT)
587     vrm_.assignVirt2StackSlot(curli_->reg);
588 
589 }
590 
591 LiveInterval *SplitEditor::createInterval() {
592   unsigned curli = sa_.getCurLI()->reg;
593   unsigned Reg = mri_.createVirtualRegister(mri_.getRegClass(curli));
594   LiveInterval &Intv = lis_.getOrCreateInterval(Reg);
595   vrm_.grow();
596   vrm_.assignVirt2StackSlot(Reg, vrm_.getStackSlot(curli));
597   return &Intv;
598 }
599 
600 LiveInterval *SplitEditor::getDupLI() {
601   if (!dupli_) {
602     // Create an interval for dupli that is a copy of curli.
603     dupli_ = createInterval();
604     dupli_->Copy(*curli_, &mri_, lis_.getVNInfoAllocator());
605   }
606   return dupli_;
607 }
608 
609 VNInfo *SplitEditor::mapValue(const VNInfo *curliVNI) {
610   VNInfo *&VNI = valueMap_[curliVNI];
611   if (!VNI)
612     VNI = openli_->createValueCopy(curliVNI, lis_.getVNInfoAllocator());
613   return VNI;
614 }
615 
616 /// Insert a COPY instruction curli -> li. Allocate a new value from li
617 /// defined by the COPY. Note that rewrite() will deal with the curli
618 /// register, so this function can be used to copy from any interval - openli,
619 /// curli, or dupli.
620 VNInfo *SplitEditor::insertCopy(LiveInterval &LI,
621                                 MachineBasicBlock &MBB,
622                                 MachineBasicBlock::iterator I) {
623   MachineInstr *MI = BuildMI(MBB, I, DebugLoc(), tii_.get(TargetOpcode::COPY),
624                              LI.reg).addReg(curli_->reg);
625   SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
626   return LI.getNextValue(DefIdx, MI, true, lis_.getVNInfoAllocator());
627 }
628 
629 /// Create a new virtual register and live interval.
630 void SplitEditor::openIntv() {
631   assert(!openli_ && "Previous LI not closed before openIntv");
632   openli_ = createInterval();
633   intervals_.push_back(openli_);
634   liveThrough_ = false;
635 }
636 
637 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
638 /// not live before Idx, a COPY is not inserted.
639 void SplitEditor::enterIntvBefore(SlotIndex Idx) {
640   assert(openli_ && "openIntv not called before enterIntvBefore");
641 
642   // Copy from curli_ if it is live.
643   if (VNInfo *CurVNI = curli_->getVNInfoAt(Idx.getUseIndex())) {
644     MachineInstr *MI = lis_.getInstructionFromIndex(Idx);
645     assert(MI && "enterIntvBefore called with invalid index");
646     VNInfo *VNI = insertCopy(*openli_, *MI->getParent(), MI);
647     openli_->addRange(LiveRange(VNI->def, Idx.getDefIndex(), VNI));
648 
649     // Make sure CurVNI is properly mapped.
650     VNInfo *&mapVNI = valueMap_[CurVNI];
651     // We dont have SSA update yet, so only one entry per value is allowed.
652     assert(!mapVNI && "enterIntvBefore called more than once for the same value");
653     mapVNI = VNI;
654   }
655   DEBUG(dbgs() << "    enterIntvBefore " << Idx << ": " << *openli_ << '\n');
656 }
657 
658 /// enterIntvAtEnd - Enter openli at the end of MBB.
659 /// PhiMBB is a successor inside openli where a PHI value is created.
660 /// Currently, all entries must share the same PhiMBB.
661 void SplitEditor::enterIntvAtEnd(MachineBasicBlock &A, MachineBasicBlock &B) {
662   assert(openli_ && "openIntv not called before enterIntvAtEnd");
663 
664   SlotIndex EndA = lis_.getMBBEndIdx(&A);
665   VNInfo *CurVNIA = curli_->getVNInfoAt(EndA.getPrevIndex());
666   if (!CurVNIA) {
667     DEBUG(dbgs() << "    enterIntvAtEnd, curli not live out of BB#"
668                  << A.getNumber() << ".\n");
669     return;
670   }
671 
672   // Add a phi kill value and live range out of A.
673   VNInfo *VNIA = insertCopy(*openli_, A, A.getFirstTerminator());
674   openli_->addRange(LiveRange(VNIA->def, EndA, VNIA));
675 
676   // FIXME: If this is the only entry edge, we don't need the extra PHI value.
677   // FIXME: If there are multiple entry blocks (so not a loop), we need proper
678   // SSA update.
679 
680   // Now look at the start of B.
681   SlotIndex StartB = lis_.getMBBStartIdx(&B);
682   SlotIndex EndB = lis_.getMBBEndIdx(&B);
683   const LiveRange *CurB = curli_->getLiveRangeContaining(StartB);
684   if (!CurB) {
685     DEBUG(dbgs() << "    enterIntvAtEnd: curli not live in to BB#"
686                  << B.getNumber() << ".\n");
687     return;
688   }
689 
690   VNInfo *VNIB = openli_->getVNInfoAt(StartB);
691   if (!VNIB) {
692     // Create a phi value.
693     VNIB = openli_->getNextValue(SlotIndex(StartB, true), 0, false,
694                                  lis_.getVNInfoAllocator());
695     VNIB->setIsPHIDef(true);
696     VNInfo *&mapVNI = valueMap_[CurB->valno];
697     if (mapVNI) {
698       // Multiple copies - must create PHI value.
699       abort();
700     } else {
701       // This is the first copy of dupLR. Mark the mapping.
702       mapVNI = VNIB;
703     }
704 
705   }
706 
707   DEBUG(dbgs() << "    enterIntvAtEnd: " << *openli_ << '\n');
708 }
709 
710 /// useIntv - indicate that all instructions in MBB should use openli.
711 void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
712   useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB));
713 }
714 
715 void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
716   assert(openli_ && "openIntv not called before useIntv");
717 
718   // Map the curli values from the interval into openli_
719   LiveInterval::const_iterator B = curli_->begin(), E = curli_->end();
720   LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
721 
722   if (I != B) {
723     --I;
724     // I begins before Start, but overlaps.
725     if (I->end > Start)
726       openli_->addRange(LiveRange(Start, std::min(End, I->end),
727                         mapValue(I->valno)));
728     ++I;
729   }
730 
731   // The remaining ranges begin after Start.
732   for (;I != E && I->start < End; ++I)
733     openli_->addRange(LiveRange(I->start, std::min(End, I->end),
734                                 mapValue(I->valno)));
735   DEBUG(dbgs() << "    use [" << Start << ';' << End << "): " << *openli_
736                << '\n');
737 }
738 
739 /// leaveIntvAfter - Leave openli after the instruction at Idx.
740 void SplitEditor::leaveIntvAfter(SlotIndex Idx) {
741   assert(openli_ && "openIntv not called before leaveIntvAfter");
742 
743   const LiveRange *CurLR = curli_->getLiveRangeContaining(Idx.getDefIndex());
744   if (!CurLR || CurLR->end <= Idx.getBoundaryIndex()) {
745     DEBUG(dbgs() << "    leaveIntvAfter " << Idx << ": not live\n");
746     return;
747   }
748 
749   // Was this value of curli live through openli?
750   if (!openli_->liveAt(CurLR->valno->def)) {
751     DEBUG(dbgs() << "    leaveIntvAfter " << Idx << ": using external value\n");
752     liveThrough_ = true;
753     return;
754   }
755 
756   // We are going to insert a back copy, so we must have a dupli_.
757   LiveRange *DupLR = getDupLI()->getLiveRangeContaining(Idx.getDefIndex());
758   assert(DupLR && "dupli not live into black, but curli is?");
759 
760   // Insert the COPY instruction.
761   MachineBasicBlock::iterator I = lis_.getInstructionFromIndex(Idx);
762   MachineInstr *MI = BuildMI(*I->getParent(), llvm::next(I), I->getDebugLoc(),
763                              tii_.get(TargetOpcode::COPY), dupli_->reg)
764                        .addReg(openli_->reg);
765   SlotIndex CopyIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
766   openli_->addRange(LiveRange(Idx.getDefIndex(), CopyIdx,
767                     mapValue(CurLR->valno)));
768   DupLR->valno->def = CopyIdx;
769   DEBUG(dbgs() << "    leaveIntvAfter " << Idx << ": " << *openli_ << '\n');
770 }
771 
772 /// leaveIntvAtTop - Leave the interval at the top of MBB.
773 /// Currently, only one value can leave the interval.
774 void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
775   assert(openli_ && "openIntv not called before leaveIntvAtTop");
776 
777   SlotIndex Start = lis_.getMBBStartIdx(&MBB);
778   const LiveRange *CurLR = curli_->getLiveRangeContaining(Start);
779 
780   // Is curli even live-in to MBB?
781   if (!CurLR) {
782     DEBUG(dbgs() << "    leaveIntvAtTop at " << Start << ": not live\n");
783     return;
784   }
785 
786   // Is curli defined by PHI at the beginning of MBB?
787   bool isPHIDef = CurLR->valno->isPHIDef() &&
788                   CurLR->valno->def.getBaseIndex() == Start;
789 
790   // If MBB is using a value of curli that was defined outside the openli range,
791   // we don't want to copy it back here.
792   if (!isPHIDef && !openli_->liveAt(CurLR->valno->def)) {
793     DEBUG(dbgs() << "    leaveIntvAtTop at " << Start
794                  << ": using external value\n");
795     liveThrough_ = true;
796     return;
797   }
798 
799   // We are going to insert a back copy, so we must have a dupli_.
800   LiveRange *DupLR = getDupLI()->getLiveRangeContaining(Start);
801   assert(DupLR && "dupli not live into black, but curli is?");
802 
803   // Insert the COPY instruction.
804   MachineInstr *MI = BuildMI(MBB, MBB.begin(), DebugLoc(),
805                              tii_.get(TargetOpcode::COPY), dupli_->reg)
806                        .addReg(openli_->reg);
807   SlotIndex Idx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
808 
809   // Adjust dupli and openli values.
810   if (isPHIDef) {
811     // dupli was already a PHI on entry to MBB. Simply insert an openli PHI,
812     // and shift the dupli def down to the COPY.
813     VNInfo *VNI = openli_->getNextValue(SlotIndex(Start, true), 0, false,
814                                         lis_.getVNInfoAllocator());
815     VNI->setIsPHIDef(true);
816     openli_->addRange(LiveRange(VNI->def, Idx, VNI));
817 
818     dupli_->removeRange(Start, Idx);
819     DupLR->valno->def = Idx;
820     DupLR->valno->setIsPHIDef(false);
821   } else {
822     // The dupli value was defined somewhere inside the openli range.
823     DEBUG(dbgs() << "    leaveIntvAtTop source value defined at "
824                  << DupLR->valno->def << "\n");
825     // FIXME: We may not need a PHI here if all predecessors have the same
826     // value.
827     VNInfo *VNI = openli_->getNextValue(SlotIndex(Start, true), 0, false,
828                                         lis_.getVNInfoAllocator());
829     VNI->setIsPHIDef(true);
830     openli_->addRange(LiveRange(VNI->def, Idx, VNI));
831 
832     // FIXME: What if DupLR->valno is used by multiple exits? SSA Update.
833 
834     // closeIntv is going to remove the superfluous live ranges.
835     DupLR->valno->def = Idx;
836     DupLR->valno->setIsPHIDef(false);
837   }
838 
839   DEBUG(dbgs() << "    leaveIntvAtTop at " << Idx << ": " << *openli_ << '\n');
840 }
841 
842 /// closeIntv - Indicate that we are done editing the currently open
843 /// LiveInterval, and ranges can be trimmed.
844 void SplitEditor::closeIntv() {
845   assert(openli_ && "openIntv not called before closeIntv");
846 
847   DEBUG(dbgs() << "    closeIntv cleaning up\n");
848   DEBUG(dbgs() << "    open " << *openli_ << '\n');
849 
850   if (liveThrough_) {
851     DEBUG(dbgs() << "    value live through region, leaving dupli as is.\n");
852   } else {
853     // live out with copies inserted, or killed by region. Either way we need to
854     // remove the overlapping region from dupli.
855     getDupLI();
856     for (LiveInterval::iterator I = openli_->begin(), E = openli_->end();
857          I != E; ++I) {
858       dupli_->removeRange(I->start, I->end);
859     }
860     // FIXME: A block branching to the entry block may also branch elsewhere
861     // curli is live. We need both openli and curli to be live in that case.
862     DEBUG(dbgs() << "    dup2 " << *dupli_ << '\n');
863   }
864   openli_ = 0;
865   valueMap_.clear();
866 }
867 
868 /// rewrite - after all the new live ranges have been created, rewrite
869 /// instructions using curli to use the new intervals.
870 void SplitEditor::rewrite() {
871   assert(!openli_ && "Previous LI not closed before rewrite");
872   const LiveInterval *curli = sa_.getCurLI();
873   for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(curli->reg),
874        RE = mri_.reg_end(); RI != RE;) {
875     MachineOperand &MO = RI.getOperand();
876     MachineInstr *MI = MO.getParent();
877     ++RI;
878     if (MI->isDebugValue()) {
879       DEBUG(dbgs() << "Zapping " << *MI);
880       // FIXME: We can do much better with debug values.
881       MO.setReg(0);
882       continue;
883     }
884     SlotIndex Idx = lis_.getInstructionIndex(MI);
885     Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
886     LiveInterval *LI = dupli_;
887     for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) {
888       LiveInterval *testli = intervals_[i];
889       if (testli->liveAt(Idx)) {
890         LI = testli;
891         break;
892       }
893     }
894     if (LI) {
895       MO.setReg(LI->reg);
896       sa_.removeUse(MI);
897       DEBUG(dbgs() << "  rewrite " << Idx << '\t' << *MI);
898     }
899   }
900 
901   // dupli_ goes in last, after rewriting.
902   if (dupli_) {
903     if (dupli_->empty()) {
904       DEBUG(dbgs() << "  dupli became empty?\n");
905       lis_.removeInterval(dupli_->reg);
906       dupli_ = 0;
907     } else {
908       dupli_->RenumberValues(lis_);
909       intervals_.push_back(dupli_);
910     }
911   }
912 
913   // Calculate spill weight and allocation hints for new intervals.
914   VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_);
915   for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) {
916     LiveInterval &li = *intervals_[i];
917     vrai.CalculateRegClass(li.reg);
918     vrai.CalculateWeightAndHint(li);
919     DEBUG(dbgs() << "  new interval " << mri_.getRegClass(li.reg)->getName()
920                  << ":" << li << '\n');
921   }
922 }
923 
924 
925 //===----------------------------------------------------------------------===//
926 //                               Loop Splitting
927 //===----------------------------------------------------------------------===//
928 
929 bool SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
930   SplitAnalysis::LoopBlocks Blocks;
931   sa_.getLoopBlocks(Loop, Blocks);
932 
933   // Break critical edges as needed.
934   SplitAnalysis::BlockPtrSet CriticalExits;
935   sa_.getCriticalExits(Blocks, CriticalExits);
936   assert(CriticalExits.empty() && "Cannot break critical exits yet");
937 
938   // Create new live interval for the loop.
939   openIntv();
940 
941   // Insert copies in the predecessors.
942   for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(),
943        E = Blocks.Preds.end(); I != E; ++I) {
944     MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
945     enterIntvAtEnd(MBB, *Loop->getHeader());
946   }
947 
948   // Switch all loop blocks.
949   for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
950        E = Blocks.Loop.end(); I != E; ++I)
951      useIntv(**I);
952 
953   // Insert back copies in the exit blocks.
954   for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(),
955        E = Blocks.Exits.end(); I != E; ++I) {
956     MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
957     leaveIntvAtTop(MBB);
958   }
959 
960   // Done.
961   closeIntv();
962   rewrite();
963   return dupli_;
964 }
965 
966 
967 //===----------------------------------------------------------------------===//
968 //                            Single Block Splitting
969 //===----------------------------------------------------------------------===//
970 
971 /// splitSingleBlocks - Split curli into a separate live interval inside each
972 /// basic block in Blocks. Return true if curli has been completely replaced,
973 /// false if curli is still intact, and needs to be spilled or split further.
974 bool SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
975   DEBUG(dbgs() << "  splitSingleBlocks for " << Blocks.size() << " blocks.\n");
976   // Determine the first and last instruction using curli in each block.
977   typedef std::pair<SlotIndex,SlotIndex> IndexPair;
978   typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap;
979   IndexPairMap MBBRange;
980   for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
981        E = sa_.usingInstrs_.end(); I != E; ++I) {
982     const MachineBasicBlock *MBB = (*I)->getParent();
983     if (!Blocks.count(MBB))
984       continue;
985     SlotIndex Idx = lis_.getInstructionIndex(*I);
986     DEBUG(dbgs() << "  BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I);
987     IndexPair &IP = MBBRange[MBB];
988     if (!IP.first.isValid() || Idx < IP.first)
989       IP.first = Idx;
990     if (!IP.second.isValid() || Idx > IP.second)
991       IP.second = Idx;
992   }
993 
994   // Create a new interval for each block.
995   for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(),
996        E = Blocks.end(); I != E; ++I) {
997     IndexPair &IP = MBBRange[*I];
998     DEBUG(dbgs() << "  splitting for BB#" << (*I)->getNumber() << ": ["
999                  << IP.first << ';' << IP.second << ")\n");
1000     assert(IP.first.isValid() && IP.second.isValid());
1001 
1002     openIntv();
1003     enterIntvBefore(IP.first);
1004     useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex());
1005     leaveIntvAfter(IP.second);
1006     closeIntv();
1007   }
1008   rewrite();
1009   return dupli_;
1010 }
1011 
1012 
1013 //===----------------------------------------------------------------------===//
1014 //                            Sub Block Splitting
1015 //===----------------------------------------------------------------------===//
1016 
1017 /// getBlockForInsideSplit - If curli is contained inside a single basic block,
1018 /// and it wou pay to subdivide the interval inside that block, return it.
1019 /// Otherwise return NULL. The returned block can be passed to
1020 /// SplitEditor::splitInsideBlock.
1021 const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
1022   // The interval must be exclusive to one block.
1023   if (usingBlocks_.size() != 1)
1024     return 0;
1025   // Don't to this for less than 4 instructions. We want to be sure that
1026   // splitting actually reduces the instruction count per interval.
1027   if (usingInstrs_.size() < 4)
1028     return 0;
1029   return usingBlocks_.begin()->first;
1030 }
1031 
1032 /// splitInsideBlock - Split curli into multiple intervals inside MBB. Return
1033 /// true if curli has been completely replaced, false if curli is still
1034 /// intact, and needs to be spilled or split further.
1035 bool SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
1036   SmallVector<SlotIndex, 32> Uses;
1037   Uses.reserve(sa_.usingInstrs_.size());
1038   for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1039        E = sa_.usingInstrs_.end(); I != E; ++I)
1040     if ((*I)->getParent() == MBB)
1041       Uses.push_back(lis_.getInstructionIndex(*I));
1042   DEBUG(dbgs() << "  splitInsideBlock BB#" << MBB->getNumber() << " for "
1043                << Uses.size() << " instructions.\n");
1044   assert(Uses.size() >= 3 && "Need at least 3 instructions");
1045   array_pod_sort(Uses.begin(), Uses.end());
1046 
1047   // Simple algorithm: Find the largest gap between uses as determined by slot
1048   // indices. Create new intervals for instructions before the gap and after the
1049   // gap.
1050   unsigned bestPos = 0;
1051   int bestGap = 0;
1052   DEBUG(dbgs() << "    dist (" << Uses[0]);
1053   for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
1054     int g = Uses[i-1].distance(Uses[i]);
1055     DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
1056     if (g > bestGap)
1057       bestPos = i, bestGap = g;
1058   }
1059   DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1060 
1061   // bestPos points to the first use after the best gap.
1062   assert(bestPos > 0 && "Invalid gap");
1063 
1064   // FIXME: Don't create intervals for low densities.
1065 
1066   // First interval before the gap. Don't create single-instr intervals.
1067   if (bestPos > 1) {
1068     openIntv();
1069     enterIntvBefore(Uses.front());
1070     useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex());
1071     leaveIntvAfter(Uses[bestPos-1]);
1072     closeIntv();
1073   }
1074 
1075   // Second interval after the gap.
1076   if (bestPos < Uses.size()-1) {
1077     openIntv();
1078     enterIntvBefore(Uses[bestPos]);
1079     useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex());
1080     leaveIntvAfter(Uses.back());
1081     closeIntv();
1082   }
1083 
1084   rewrite();
1085   return dupli_;
1086 }
1087