Revert r146184. I am seeing performance regression cause by this patch in one test...
[oota-llvm.git] / lib / CodeGen / MachineSink.cpp
1 //===-- MachineSink.cpp - Sinking for machine instructions ----------------===//
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 pass moves instructions into successor blocks when possible, so that
11 // they aren't executed on paths where their results aren't needed.
12 //
13 // This pass is not intended to be a replacement or a complete alternative
14 // for an LLVM-IR-level sinking pass. It is only designed to sink simple
15 // constructs that are not exposed before lowering and instruction selection.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #define DEBUG_TYPE "machine-sink"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/MachineDominators.h"
23 #include "llvm/CodeGen/MachineLoopInfo.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/ADT/SmallSet.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm;
34
35 static cl::opt<bool> 
36 SplitEdges("machine-sink-split",
37            cl::desc("Split critical edges during machine sinking"),
38            cl::init(true), cl::Hidden);
39
40 STATISTIC(NumSunk,      "Number of machine instructions sunk");
41 STATISTIC(NumSplit,     "Number of critical edges split");
42 STATISTIC(NumCoalesces, "Number of copies coalesced");
43
44 namespace {
45   class MachineSinking : public MachineFunctionPass {
46     const TargetInstrInfo *TII;
47     const TargetRegisterInfo *TRI;
48     MachineRegisterInfo  *MRI;  // Machine register information
49     MachineDominatorTree *DT;   // Machine dominator tree
50     MachineLoopInfo *LI;
51     AliasAnalysis *AA;
52     BitVector AllocatableSet;   // Which physregs are allocatable?
53
54     // Remember which edges have been considered for breaking.
55     SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
56     CEBCandidates;
57
58   public:
59     static char ID; // Pass identification
60     MachineSinking() : MachineFunctionPass(ID) {
61       initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
62     }
63
64     virtual bool runOnMachineFunction(MachineFunction &MF);
65
66     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67       AU.setPreservesCFG();
68       MachineFunctionPass::getAnalysisUsage(AU);
69       AU.addRequired<AliasAnalysis>();
70       AU.addRequired<MachineDominatorTree>();
71       AU.addRequired<MachineLoopInfo>();
72       AU.addPreserved<MachineDominatorTree>();
73       AU.addPreserved<MachineLoopInfo>();
74     }
75
76     virtual void releaseMemory() {
77       CEBCandidates.clear();
78     }
79
80   private:
81     bool ProcessBlock(MachineBasicBlock &MBB);
82     bool isWorthBreakingCriticalEdge(MachineInstr *MI,
83                                      MachineBasicBlock *From,
84                                      MachineBasicBlock *To);
85     MachineBasicBlock *SplitCriticalEdge(MachineInstr *MI,
86                                          MachineBasicBlock *From,
87                                          MachineBasicBlock *To,
88                                          bool BreakPHIEdge);
89     bool SinkInstruction(MachineInstr *MI, bool &SawStore);
90     bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
91                                  MachineBasicBlock *DefMBB,
92                                  bool &BreakPHIEdge, bool &LocalUse) const;
93     MachineBasicBlock *FindSuccToSinkTo(MachineInstr *MI, bool &BreakPHIEdge);
94
95     bool PerformTrivialForwardCoalescing(MachineInstr *MI,
96                                          MachineBasicBlock *MBB);
97   };
98 } // end anonymous namespace
99
100 char MachineSinking::ID = 0;
101 INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink",
102                 "Machine code sinking", false, false)
103 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
104 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
105 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
106 INITIALIZE_PASS_END(MachineSinking, "machine-sink",
107                 "Machine code sinking", false, false)
108
109 FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
110
111 bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
112                                                      MachineBasicBlock *MBB) {
113   if (!MI->isCopy())
114     return false;
115
116   unsigned SrcReg = MI->getOperand(1).getReg();
117   unsigned DstReg = MI->getOperand(0).getReg();
118   if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
119       !TargetRegisterInfo::isVirtualRegister(DstReg) ||
120       !MRI->hasOneNonDBGUse(SrcReg))
121     return false;
122
123   const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
124   const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
125   if (SRC != DRC)
126     return false;
127
128   MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
129   if (DefMI->isCopyLike())
130     return false;
131   DEBUG(dbgs() << "Coalescing: " << *DefMI);
132   DEBUG(dbgs() << "*** to: " << *MI);
133   MRI->replaceRegWith(DstReg, SrcReg);
134   MI->eraseFromParent();
135   ++NumCoalesces;
136   return true;
137 }
138
139 /// AllUsesDominatedByBlock - Return true if all uses of the specified register
140 /// occur in blocks dominated by the specified block. If any use is in the
141 /// definition block, then return false since it is never legal to move def
142 /// after uses.
143 bool
144 MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
145                                         MachineBasicBlock *MBB,
146                                         MachineBasicBlock *DefMBB,
147                                         bool &BreakPHIEdge,
148                                         bool &LocalUse) const {
149   assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
150          "Only makes sense for vregs");
151
152   if (MRI->use_nodbg_empty(Reg))
153     return true;
154
155   // Ignoring debug uses is necessary so debug info doesn't affect the code.
156   // This may leave a referencing dbg_value in the original block, before
157   // the definition of the vreg.  Dwarf generator handles this although the
158   // user might not get the right info at runtime.
159
160   // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
161   // into and they are all PHI nodes. In this case, machine-sink must break
162   // the critical edge first. e.g.
163   //
164   // BB#1: derived from LLVM BB %bb4.preheader
165   //   Predecessors according to CFG: BB#0
166   //     ...
167   //     %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
168   //     ...
169   //     JE_4 <BB#37>, %EFLAGS<imp-use>
170   //   Successors according to CFG: BB#37 BB#2
171   //
172   // BB#2: derived from LLVM BB %bb.nph
173   //   Predecessors according to CFG: BB#0 BB#1
174   //     %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
175   BreakPHIEdge = true;
176   for (MachineRegisterInfo::use_nodbg_iterator
177          I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
178        I != E; ++I) {
179     MachineInstr *UseInst = &*I;
180     MachineBasicBlock *UseBlock = UseInst->getParent();
181     if (!(UseBlock == MBB && UseInst->isPHI() &&
182           UseInst->getOperand(I.getOperandNo()+1).getMBB() == DefMBB)) {
183       BreakPHIEdge = false;
184       break;
185     }
186   }
187   if (BreakPHIEdge)
188     return true;
189
190   for (MachineRegisterInfo::use_nodbg_iterator
191          I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
192        I != E; ++I) {
193     // Determine the block of the use.
194     MachineInstr *UseInst = &*I;
195     MachineBasicBlock *UseBlock = UseInst->getParent();
196     if (UseInst->isPHI()) {
197       // PHI nodes use the operand in the predecessor block, not the block with
198       // the PHI.
199       UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
200     } else if (UseBlock == DefMBB) {
201       LocalUse = true;
202       return false;
203     }
204
205     // Check that it dominates.
206     if (!DT->dominates(MBB, UseBlock))
207       return false;
208   }
209
210   return true;
211 }
212
213 bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
214   DEBUG(dbgs() << "******** Machine Sinking ********\n");
215
216   const TargetMachine &TM = MF.getTarget();
217   TII = TM.getInstrInfo();
218   TRI = TM.getRegisterInfo();
219   MRI = &MF.getRegInfo();
220   DT = &getAnalysis<MachineDominatorTree>();
221   LI = &getAnalysis<MachineLoopInfo>();
222   AA = &getAnalysis<AliasAnalysis>();
223   AllocatableSet = TRI->getAllocatableSet(MF);
224
225   bool EverMadeChange = false;
226
227   while (1) {
228     bool MadeChange = false;
229
230     // Process all basic blocks.
231     CEBCandidates.clear();
232     for (MachineFunction::iterator I = MF.begin(), E = MF.end();
233          I != E; ++I)
234       MadeChange |= ProcessBlock(*I);
235
236     // If this iteration over the code changed anything, keep iterating.
237     if (!MadeChange) break;
238     EverMadeChange = true;
239   }
240   return EverMadeChange;
241 }
242
243 bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
244   // Can't sink anything out of a block that has less than two successors.
245   if (MBB.succ_size() <= 1 || MBB.empty()) return false;
246
247   // Don't bother sinking code out of unreachable blocks. In addition to being
248   // unprofitable, it can also lead to infinite looping, because in an
249   // unreachable loop there may be nowhere to stop.
250   if (!DT->isReachableFromEntry(&MBB)) return false;
251
252   bool MadeChange = false;
253
254   // Walk the basic block bottom-up.  Remember if we saw a store.
255   MachineBasicBlock::iterator I = MBB.end();
256   --I;
257   bool ProcessedBegin, SawStore = false;
258   do {
259     MachineInstr *MI = I;  // The instruction to sink.
260
261     // Predecrement I (if it's not begin) so that it isn't invalidated by
262     // sinking.
263     ProcessedBegin = I == MBB.begin();
264     if (!ProcessedBegin)
265       --I;
266
267     if (MI->isDebugValue())
268       continue;
269
270     bool Joined = PerformTrivialForwardCoalescing(MI, &MBB);
271     if (Joined) {
272       MadeChange = true;
273       continue;
274     }
275
276     if (SinkInstruction(MI, SawStore))
277       ++NumSunk, MadeChange = true;
278
279     // If we just processed the first instruction in the block, we're done.
280   } while (!ProcessedBegin);
281
282   return MadeChange;
283 }
284
285 bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
286                                                  MachineBasicBlock *From,
287                                                  MachineBasicBlock *To) {
288   // FIXME: Need much better heuristics.
289
290   // If the pass has already considered breaking this edge (during this pass
291   // through the function), then let's go ahead and break it. This means
292   // sinking multiple "cheap" instructions into the same block.
293   if (!CEBCandidates.insert(std::make_pair(From, To)))
294     return true;
295
296   if (!MI->isCopy() && !MI->isAsCheapAsAMove())
297     return true;
298
299   // MI is cheap, we probably don't want to break the critical edge for it.
300   // However, if this would allow some definitions of its source operands
301   // to be sunk then it's probably worth it.
302   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
303     const MachineOperand &MO = MI->getOperand(i);
304     if (!MO.isReg()) continue;
305     unsigned Reg = MO.getReg();
306     if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg))
307       continue;
308     if (MRI->hasOneNonDBGUse(Reg))
309       return true;
310   }
311
312   return false;
313 }
314
315 MachineBasicBlock *MachineSinking::SplitCriticalEdge(MachineInstr *MI,
316                                                      MachineBasicBlock *FromBB,
317                                                      MachineBasicBlock *ToBB,
318                                                      bool BreakPHIEdge) {
319   if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
320     return 0;
321
322   // Avoid breaking back edge. From == To means backedge for single BB loop.
323   if (!SplitEdges || FromBB == ToBB)
324     return 0;
325
326   // Check for backedges of more "complex" loops.
327   if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
328       LI->isLoopHeader(ToBB))
329     return 0;
330
331   // It's not always legal to break critical edges and sink the computation
332   // to the edge.
333   //
334   // BB#1:
335   // v1024
336   // Beq BB#3
337   // <fallthrough>
338   // BB#2:
339   // ... no uses of v1024
340   // <fallthrough>
341   // BB#3:
342   // ...
343   //       = v1024
344   //
345   // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
346   //
347   // BB#1:
348   // ...
349   // Bne BB#2
350   // BB#4:
351   // v1024 =
352   // B BB#3
353   // BB#2:
354   // ... no uses of v1024
355   // <fallthrough>
356   // BB#3:
357   // ...
358   //       = v1024
359   //
360   // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
361   // flow. We need to ensure the new basic block where the computation is
362   // sunk to dominates all the uses.
363   // It's only legal to break critical edge and sink the computation to the
364   // new block if all the predecessors of "To", except for "From", are
365   // not dominated by "From". Given SSA property, this means these
366   // predecessors are dominated by "To".
367   //
368   // There is no need to do this check if all the uses are PHI nodes. PHI
369   // sources are only defined on the specific predecessor edges.
370   if (!BreakPHIEdge) {
371     for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
372            E = ToBB->pred_end(); PI != E; ++PI) {
373       if (*PI == FromBB)
374         continue;
375       if (!DT->dominates(ToBB, *PI))
376         return 0;
377     }
378   }
379
380   return FromBB->SplitCriticalEdge(ToBB, this);
381 }
382
383 static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) {
384   return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence();
385 }
386
387 /// collectDebgValues - Scan instructions following MI and collect any 
388 /// matching DBG_VALUEs.
389 static void collectDebugValues(MachineInstr *MI, 
390                                SmallVector<MachineInstr *, 2> & DbgValues) {
391   DbgValues.clear();
392   if (!MI->getOperand(0).isReg())
393     return;
394
395   MachineBasicBlock::iterator DI = MI; ++DI;
396   for (MachineBasicBlock::iterator DE = MI->getParent()->end();
397        DI != DE; ++DI) {
398     if (!DI->isDebugValue())
399       return;
400     if (DI->getOperand(0).isReg() &&
401         DI->getOperand(0).getReg() == MI->getOperand(0).getReg())
402       DbgValues.push_back(DI);
403   }
404 }
405
406 /// FindSuccToSinkTo - Find a successor to sink this instruction to.
407 MachineBasicBlock *MachineSinking::FindSuccToSinkTo(MachineInstr *MI,
408                                                     bool &BreakPHIEdge) {
409
410   // Loop over all the operands of the specified instruction.  If there is
411   // anything we can't handle, bail out.
412   MachineBasicBlock *ParentBlock = MI->getParent();
413
414   // SuccToSinkTo - This is the successor to sink this instruction to, once we
415   // decide.
416   MachineBasicBlock *SuccToSinkTo = 0;
417
418   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
419     const MachineOperand &MO = MI->getOperand(i);
420     if (!MO.isReg()) continue;  // Ignore non-register operands.
421
422     unsigned Reg = MO.getReg();
423     if (Reg == 0) continue;
424
425     if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
426       if (MO.isUse()) {
427         // If the physreg has no defs anywhere, it's just an ambient register
428         // and we can freely move its uses. Alternatively, if it's allocatable,
429         // it could get allocated to something with a def during allocation.
430         if (!MRI->def_empty(Reg))
431           return NULL;
432
433         if (AllocatableSet.test(Reg))
434           return NULL;
435
436         // Check for a def among the register's aliases too.
437         for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
438           unsigned AliasReg = *Alias;
439           if (!MRI->def_empty(AliasReg))
440             return NULL;
441
442           if (AllocatableSet.test(AliasReg))
443             return NULL;
444         }
445       } else if (!MO.isDead()) {
446         // A def that isn't dead. We can't move it.
447         return NULL;
448       }
449     } else {
450       // Virtual register uses are always safe to sink.
451       if (MO.isUse()) continue;
452
453       // If it's not safe to move defs of the register class, then abort.
454       if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
455         return NULL;
456
457       // FIXME: This picks a successor to sink into based on having one
458       // successor that dominates all the uses.  However, there are cases where
459       // sinking can happen but where the sink point isn't a successor.  For
460       // example:
461       //
462       //   x = computation
463       //   if () {} else {}
464       //   use x
465       //
466       // the instruction could be sunk over the whole diamond for the
467       // if/then/else (or loop, etc), allowing it to be sunk into other blocks
468       // after that.
469
470       // Virtual register defs can only be sunk if all their uses are in blocks
471       // dominated by one of the successors.
472       if (SuccToSinkTo) {
473         // If a previous operand picked a block to sink to, then this operand
474         // must be sinkable to the same block.
475         bool LocalUse = false;
476         if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, ParentBlock,
477                                      BreakPHIEdge, LocalUse))
478           return NULL;
479
480         continue;
481       }
482
483       // Otherwise, we should look at all the successors and decide which one
484       // we should sink to.
485       for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
486            E = ParentBlock->succ_end(); SI != E; ++SI) {
487         MachineBasicBlock *SuccBlock = *SI;
488         bool LocalUse = false;
489         if (AllUsesDominatedByBlock(Reg, SuccBlock, ParentBlock,
490                                     BreakPHIEdge, LocalUse)) {
491           SuccToSinkTo = SuccBlock;
492           break;
493         }
494         if (LocalUse)
495           // Def is used locally, it's never safe to move this def.
496           return NULL;
497       }
498
499       // If we couldn't find a block to sink to, ignore this instruction.
500       if (SuccToSinkTo == 0)
501         return NULL;
502     }
503   }
504
505   // It is not possible to sink an instruction into its own block.  This can
506   // happen with loops.
507   if (ParentBlock == SuccToSinkTo)
508     return NULL;
509
510   // It's not safe to sink instructions to EH landing pad. Control flow into
511   // landing pad is implicitly defined.
512   if (SuccToSinkTo && SuccToSinkTo->isLandingPad())
513     return NULL;
514
515   return SuccToSinkTo;
516 }
517
518 /// SinkInstruction - Determine whether it is safe to sink the specified machine
519 /// instruction out of its current block into a successor.
520 bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
521   // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to
522   // be close to the source to make it easier to coalesce.
523   if (AvoidsSinking(MI, MRI))
524     return false;
525
526   // Check if it's safe to move the instruction.
527   if (!MI->isSafeToMove(TII, AA, SawStore))
528     return false;
529
530   // FIXME: This should include support for sinking instructions within the
531   // block they are currently in to shorten the live ranges.  We often get
532   // instructions sunk into the top of a large block, but it would be better to
533   // also sink them down before their first use in the block.  This xform has to
534   // be careful not to *increase* register pressure though, e.g. sinking
535   // "x = y + z" down if it kills y and z would increase the live ranges of y
536   // and z and only shrink the live range of x.
537
538   bool BreakPHIEdge = false;
539   MachineBasicBlock *SuccToSinkTo = FindSuccToSinkTo(MI, BreakPHIEdge);
540
541   // If there are no outputs, it must have side-effects.
542   if (SuccToSinkTo == 0)
543     return false;
544
545
546   // If the instruction to move defines a dead physical register which is live
547   // when leaving the basic block, don't move it because it could turn into a
548   // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
549   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
550     const MachineOperand &MO = MI->getOperand(I);
551     if (!MO.isReg()) continue;
552     unsigned Reg = MO.getReg();
553     if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
554     if (SuccToSinkTo->isLiveIn(Reg))
555       return false;
556   }
557
558   DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
559
560   MachineBasicBlock *ParentBlock = MI->getParent();
561
562   // If the block has multiple predecessors, this would introduce computation on
563   // a path that it doesn't already exist.  We could split the critical edge,
564   // but for now we just punt.
565   if (SuccToSinkTo->pred_size() > 1) {
566     // We cannot sink a load across a critical edge - there may be stores in
567     // other code paths.
568     bool TryBreak = false;
569     bool store = true;
570     if (!MI->isSafeToMove(TII, AA, store)) {
571       DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
572       TryBreak = true;
573     }
574
575     // We don't want to sink across a critical edge if we don't dominate the
576     // successor. We could be introducing calculations to new code paths.
577     if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
578       DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
579       TryBreak = true;
580     }
581
582     // Don't sink instructions into a loop.
583     if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
584       DEBUG(dbgs() << " *** NOTE: Loop header found\n");
585       TryBreak = true;
586     }
587
588     // Otherwise we are OK with sinking along a critical edge.
589     if (!TryBreak)
590       DEBUG(dbgs() << "Sinking along critical edge.\n");
591     else {
592       MachineBasicBlock *NewSucc =
593         SplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
594       if (!NewSucc) {
595         DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
596                         "break critical edge\n");
597         return false;
598       } else {
599         DEBUG(dbgs() << " *** Splitting critical edge:"
600               " BB#" << ParentBlock->getNumber()
601               << " -- BB#" << NewSucc->getNumber()
602               << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
603         SuccToSinkTo = NewSucc;
604         ++NumSplit;
605         BreakPHIEdge = false;
606       }
607     }
608   }
609
610   if (BreakPHIEdge) {
611     // BreakPHIEdge is true if all the uses are in the successor MBB being
612     // sunken into and they are all PHI nodes. In this case, machine-sink must
613     // break the critical edge first.
614     MachineBasicBlock *NewSucc = SplitCriticalEdge(MI, ParentBlock,
615                                                    SuccToSinkTo, BreakPHIEdge);
616     if (!NewSucc) {
617       DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
618             "break critical edge\n");
619       return false;
620     }
621
622     DEBUG(dbgs() << " *** Splitting critical edge:"
623           " BB#" << ParentBlock->getNumber()
624           << " -- BB#" << NewSucc->getNumber()
625           << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
626     SuccToSinkTo = NewSucc;
627     ++NumSplit;
628   }
629
630   // Determine where to insert into. Skip phi nodes.
631   MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
632   while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
633     ++InsertPos;
634
635   // collect matching debug values.
636   SmallVector<MachineInstr *, 2> DbgValuesToSink;
637   collectDebugValues(MI, DbgValuesToSink);
638
639   // Move the instruction.
640   SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
641                        ++MachineBasicBlock::iterator(MI));
642
643   // Move debug values.
644   for (SmallVector<MachineInstr *, 2>::iterator DBI = DbgValuesToSink.begin(),
645          DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) {
646     MachineInstr *DbgMI = *DBI;
647     SuccToSinkTo->splice(InsertPos, ParentBlock,  DbgMI,
648                          ++MachineBasicBlock::iterator(DbgMI));
649   }
650
651   // Conservatively, clear any kill flags, since it's possible that they are no
652   // longer correct.
653   MI->clearKillInfo();
654
655   return true;
656 }