eef9f38daf8a5c983c1917fc970476e80182cd23
[oota-llvm.git] / lib / Target / Mips / MipsDelaySlotFiller.cpp
1 //===-- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler ------------------===//
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 // Simple pass to fill delay slots with useful instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "delay-slot-filler"
15
16 #include "MCTargetDesc/MipsMCNaCl.h"
17 #include "Mips.h"
18 #include "MipsInstrInfo.h"
19 #include "MipsTargetMachine.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/PseudoSourceValue.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Target/TargetRegisterInfo.h"
33
34 using namespace llvm;
35
36 STATISTIC(FilledSlots, "Number of delay slots filled");
37 STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
38                        " are not NOP.");
39
40 static cl::opt<bool> DisableDelaySlotFiller(
41   "disable-mips-delay-filler",
42   cl::init(false),
43   cl::desc("Fill all delay slots with NOPs."),
44   cl::Hidden);
45
46 static cl::opt<bool> DisableForwardSearch(
47   "disable-mips-df-forward-search",
48   cl::init(true),
49   cl::desc("Disallow MIPS delay filler to search forward."),
50   cl::Hidden);
51
52 static cl::opt<bool> DisableSuccBBSearch(
53   "disable-mips-df-succbb-search",
54   cl::init(true),
55   cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
56   cl::Hidden);
57
58 static cl::opt<bool> DisableBackwardSearch(
59   "disable-mips-df-backward-search",
60   cl::init(false),
61   cl::desc("Disallow MIPS delay filler to search backward."),
62   cl::Hidden);
63
64 namespace {
65   typedef MachineBasicBlock::iterator Iter;
66   typedef MachineBasicBlock::reverse_iterator ReverseIter;
67   typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
68
69   class RegDefsUses {
70   public:
71     RegDefsUses(TargetMachine &TM);
72     void init(const MachineInstr &MI);
73
74     /// This function sets all caller-saved registers in Defs.
75     void setCallerSaved(const MachineInstr &MI);
76
77     /// This function sets all unallocatable registers in Defs.
78     void setUnallocatableRegs(const MachineFunction &MF);
79
80     /// Set bits in Uses corresponding to MBB's live-out registers except for
81     /// the registers that are live-in to SuccBB.
82     void addLiveOut(const MachineBasicBlock &MBB,
83                     const MachineBasicBlock &SuccBB);
84
85     bool update(const MachineInstr &MI, unsigned Begin, unsigned End);
86
87   private:
88     bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg,
89                           bool IsDef) const;
90
91     /// Returns true if Reg or its alias is in RegSet.
92     bool isRegInSet(const BitVector &RegSet, unsigned Reg) const;
93
94     const TargetRegisterInfo &TRI;
95     BitVector Defs, Uses;
96   };
97
98   /// Base class for inspecting loads and stores.
99   class InspectMemInstr {
100   public:
101     InspectMemInstr(bool ForbidMemInstr_)
102       : OrigSeenLoad(false), OrigSeenStore(false), SeenLoad(false),
103         SeenStore(false), ForbidMemInstr(ForbidMemInstr_) {}
104
105     /// Return true if MI cannot be moved to delay slot.
106     bool hasHazard(const MachineInstr &MI);
107
108     virtual ~InspectMemInstr() {}
109
110   protected:
111     /// Flags indicating whether loads or stores have been seen.
112     bool OrigSeenLoad, OrigSeenStore, SeenLoad, SeenStore;
113
114     /// Memory instructions are not allowed to move to delay slot if this flag
115     /// is true.
116     bool ForbidMemInstr;
117
118   private:
119     virtual bool hasHazard_(const MachineInstr &MI) = 0;
120   };
121
122   /// This subclass rejects any memory instructions.
123   class NoMemInstr : public InspectMemInstr {
124   public:
125     NoMemInstr() : InspectMemInstr(true) {}
126   private:
127     virtual bool hasHazard_(const MachineInstr &MI) { return true; }
128   };
129
130   /// This subclass accepts loads from stacks and constant loads.
131   class LoadFromStackOrConst : public InspectMemInstr {
132   public:
133     LoadFromStackOrConst() : InspectMemInstr(false) {}
134   private:
135     virtual bool hasHazard_(const MachineInstr &MI);
136   };
137
138   /// This subclass uses memory dependence information to determine whether a
139   /// memory instruction can be moved to a delay slot.
140   class MemDefsUses : public InspectMemInstr {
141   public:
142     MemDefsUses(const MachineFrameInfo *MFI);
143
144   private:
145     virtual bool hasHazard_(const MachineInstr &MI);
146
147     /// Update Defs and Uses. Return true if there exist dependences that
148     /// disqualify the delay slot candidate between V and values in Uses and
149     /// Defs.
150     bool updateDefsUses(const Value *V, bool MayStore);
151
152     /// Get the list of underlying objects of MI's memory operand.
153     bool getUnderlyingObjects(const MachineInstr &MI,
154                               SmallVectorImpl<const Value *> &Objects) const;
155
156     const MachineFrameInfo *MFI;
157     SmallPtrSet<const Value*, 4> Uses, Defs;
158
159     /// Flags indicating whether loads or stores with no underlying objects have
160     /// been seen.
161     bool SeenNoObjLoad, SeenNoObjStore;
162   };
163
164   class Filler : public MachineFunctionPass {
165   public:
166     Filler(TargetMachine &tm)
167       : MachineFunctionPass(ID), TM(tm) { }
168
169     virtual const char *getPassName() const {
170       return "Mips Delay Slot Filler";
171     }
172
173     bool runOnMachineFunction(MachineFunction &F) {
174       bool Changed = false;
175       for (MachineFunction::iterator FI = F.begin(), FE = F.end();
176            FI != FE; ++FI)
177         Changed |= runOnMachineBasicBlock(*FI);
178       return Changed;
179     }
180
181     void getAnalysisUsage(AnalysisUsage &AU) const {
182       AU.addRequired<MachineBranchProbabilityInfo>();
183       MachineFunctionPass::getAnalysisUsage(AU);
184     }
185
186   private:
187     bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
188
189     /// This function checks if it is valid to move Candidate to the delay slot
190     /// and returns true if it isn't. It also updates memory and register
191     /// dependence information.
192     bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
193                         InspectMemInstr &IM) const;
194
195     /// This function searches range [Begin, End) for an instruction that can be
196     /// moved to the delay slot. Returns true on success.
197     template<typename IterTy>
198     bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
199                      RegDefsUses &RegDU, InspectMemInstr &IM,
200                      IterTy &Filler) const;
201
202     /// This function searches in the backward direction for an instruction that
203     /// can be moved to the delay slot. Returns true on success.
204     bool searchBackward(MachineBasicBlock &MBB, Iter Slot) const;
205
206     /// This function searches MBB in the forward direction for an instruction
207     /// that can be moved to the delay slot. Returns true on success.
208     bool searchForward(MachineBasicBlock &MBB, Iter Slot) const;
209
210     /// This function searches one of MBB's successor blocks for an instruction
211     /// that can be moved to the delay slot and inserts clones of the
212     /// instruction into the successor's predecessor blocks.
213     bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
214
215     /// Pick a successor block of MBB. Return NULL if MBB doesn't have a
216     /// successor block that is not a landing pad.
217     MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const;
218
219     /// This function analyzes MBB and returns an instruction with an unoccupied
220     /// slot that branches to Dst.
221     std::pair<MipsInstrInfo::BranchType, MachineInstr *>
222     getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const;
223
224     /// Examine Pred and see if it is possible to insert an instruction into
225     /// one of its branches delay slot or its end.
226     bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
227                      RegDefsUses &RegDU, bool &HasMultipleSuccs,
228                      BB2BrMap &BrMap) const;
229
230     bool terminateSearch(const MachineInstr &Candidate) const;
231
232     TargetMachine &TM;
233
234     static char ID;
235   };
236   char Filler::ID = 0;
237 } // end of anonymous namespace
238
239 static bool hasUnoccupiedSlot(const MachineInstr *MI) {
240   return MI->hasDelaySlot() && !MI->isBundledWithSucc();
241 }
242
243 /// This function inserts clones of Filler into predecessor blocks.
244 static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
245   MachineFunction *MF = Filler->getParent()->getParent();
246
247   for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
248     if (I->second) {
249       MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
250       ++UsefulSlots;
251     } else {
252       I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
253     }
254   }
255 }
256
257 /// This function adds registers Filler defines to MBB's live-in register list.
258 static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) {
259   for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) {
260     const MachineOperand &MO = Filler->getOperand(I);
261     unsigned R;
262
263     if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg()))
264       continue;
265
266 #ifndef NDEBUG
267     const MachineFunction &MF = *MBB.getParent();
268     assert(MF.getTarget().getRegisterInfo()->getAllocatableSet(MF).test(R) &&
269            "Shouldn't move an instruction with unallocatable registers across "
270            "basic block boundaries.");
271 #endif
272
273     if (!MBB.isLiveIn(R))
274       MBB.addLiveIn(R);
275   }
276 }
277
278 RegDefsUses::RegDefsUses(TargetMachine &TM)
279   : TRI(*TM.getRegisterInfo()), Defs(TRI.getNumRegs(), false),
280     Uses(TRI.getNumRegs(), false) {}
281
282 void RegDefsUses::init(const MachineInstr &MI) {
283   // Add all register operands which are explicit and non-variadic.
284   update(MI, 0, MI.getDesc().getNumOperands());
285
286   // If MI is a call, add RA to Defs to prevent users of RA from going into
287   // delay slot.
288   if (MI.isCall())
289     Defs.set(Mips::RA);
290
291   // Add all implicit register operands of branch instructions except
292   // register AT.
293   if (MI.isBranch()) {
294     update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands());
295     Defs.reset(Mips::AT);
296   }
297 }
298
299 void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
300   assert(MI.isCall());
301
302   // If MI is a call, add all caller-saved registers to Defs.
303   BitVector CallerSavedRegs(TRI.getNumRegs(), true);
304
305   CallerSavedRegs.reset(Mips::ZERO);
306   CallerSavedRegs.reset(Mips::ZERO_64);
307
308   for (const MCPhysReg *R = TRI.getCalleeSavedRegs(); *R; ++R)
309     for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
310       CallerSavedRegs.reset(*AI);
311
312   Defs |= CallerSavedRegs;
313 }
314
315 void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) {
316   BitVector AllocSet = TRI.getAllocatableSet(MF);
317
318   for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R))
319     for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
320       AllocSet.set(*AI);
321
322   AllocSet.set(Mips::ZERO);
323   AllocSet.set(Mips::ZERO_64);
324
325   Defs |= AllocSet.flip();
326 }
327
328 void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB,
329                              const MachineBasicBlock &SuccBB) {
330   for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
331        SE = MBB.succ_end(); SI != SE; ++SI)
332     if (*SI != &SuccBB)
333       for (MachineBasicBlock::livein_iterator LI = (*SI)->livein_begin(),
334            LE = (*SI)->livein_end(); LI != LE; ++LI)
335         Uses.set(*LI);
336 }
337
338 bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) {
339   BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs());
340   bool HasHazard = false;
341
342   for (unsigned I = Begin; I != End; ++I) {
343     const MachineOperand &MO = MI.getOperand(I);
344
345     if (MO.isReg() && MO.getReg())
346       HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef());
347   }
348
349   Defs |= NewDefs;
350   Uses |= NewUses;
351
352   return HasHazard;
353 }
354
355 bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses,
356                                    unsigned Reg, bool IsDef) const {
357   if (IsDef) {
358     NewDefs.set(Reg);
359     // check whether Reg has already been defined or used.
360     return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg));
361   }
362
363   NewUses.set(Reg);
364   // check whether Reg has already been defined.
365   return isRegInSet(Defs, Reg);
366 }
367
368 bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const {
369   // Check Reg and all aliased Registers.
370   for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
371     if (RegSet.test(*AI))
372       return true;
373   return false;
374 }
375
376 bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
377   if (!MI.mayStore() && !MI.mayLoad())
378     return false;
379
380   if (ForbidMemInstr)
381     return true;
382
383   OrigSeenLoad = SeenLoad;
384   OrigSeenStore = SeenStore;
385   SeenLoad |= MI.mayLoad();
386   SeenStore |= MI.mayStore();
387
388   // If MI is an ordered or volatile memory reference, disallow moving
389   // subsequent loads and stores to delay slot.
390   if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) {
391     ForbidMemInstr = true;
392     return true;
393   }
394
395   return hasHazard_(MI);
396 }
397
398 bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
399   if (MI.mayStore())
400     return true;
401
402   if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getValue())
403     return true;
404
405   const Value *V = (*MI.memoperands_begin())->getValue();
406
407   if (isa<FixedStackPseudoSourceValue>(V))
408     return false;
409
410   if (const PseudoSourceValue *PSV = dyn_cast<const PseudoSourceValue>(V))
411     return !PSV->isConstant(0) && V != PseudoSourceValue::getStack();
412
413   return true;
414 }
415
416 MemDefsUses::MemDefsUses(const MachineFrameInfo *MFI_)
417   : InspectMemInstr(false), MFI(MFI_), SeenNoObjLoad(false),
418     SeenNoObjStore(false) {}
419
420 bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
421   bool HasHazard = false;
422   SmallVector<const Value *, 4> Objs;
423
424   // Check underlying object list.
425   if (getUnderlyingObjects(MI, Objs)) {
426     for (SmallVectorImpl<const Value *>::const_iterator I = Objs.begin();
427          I != Objs.end(); ++I)
428       HasHazard |= updateDefsUses(*I, MI.mayStore());
429
430     return HasHazard;
431   }
432
433   // No underlying objects found.
434   HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore);
435   HasHazard |= MI.mayLoad() || OrigSeenStore;
436
437   SeenNoObjLoad |= MI.mayLoad();
438   SeenNoObjStore |= MI.mayStore();
439
440   return HasHazard;
441 }
442
443 bool MemDefsUses::updateDefsUses(const Value *V, bool MayStore) {
444   if (MayStore)
445     return !Defs.insert(V) || Uses.count(V) || SeenNoObjStore || SeenNoObjLoad;
446
447   Uses.insert(V);
448   return Defs.count(V) || SeenNoObjStore;
449 }
450
451 bool MemDefsUses::
452 getUnderlyingObjects(const MachineInstr &MI,
453                      SmallVectorImpl<const Value *> &Objects) const {
454   if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getValue())
455     return false;
456
457   const Value *V = (*MI.memoperands_begin())->getValue();
458
459   SmallVector<Value *, 4> Objs;
460   GetUnderlyingObjects(const_cast<Value *>(V), Objs);
461
462   for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end();
463        I != E; ++I) {
464     if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(*I)) {
465       if (PSV->isAliased(MFI))
466         return false;
467     } else if (!isIdentifiedObject(V))
468       return false;
469
470     Objects.push_back(*I);
471   }
472
473   return true;
474 }
475
476 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
477 /// We assume there is only one delay slot per delayed instruction.
478 bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
479   bool Changed = false;
480
481   for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
482     if (!hasUnoccupiedSlot(&*I))
483       continue;
484
485     ++FilledSlots;
486     Changed = true;
487
488     // Delay slot filling is disabled at -O0.
489     if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) {
490       if (searchBackward(MBB, I))
491         continue;
492
493       if (I->isTerminator()) {
494         if (searchSuccBBs(MBB, I))
495           continue;
496       } else if (searchForward(MBB, I)) {
497         continue;
498       }
499     }
500
501     // Bundle the NOP to the instruction with the delay slot.
502     const MipsInstrInfo *TII =
503       static_cast<const MipsInstrInfo*>(TM.getInstrInfo());
504     BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
505     MIBundleBuilder(MBB, I, std::next(I, 2));
506   }
507
508   return Changed;
509 }
510
511 /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
512 /// slots in Mips MachineFunctions
513 FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
514   return new Filler(tm);
515 }
516
517 template<typename IterTy>
518 bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
519                          RegDefsUses &RegDU, InspectMemInstr& IM,
520                          IterTy &Filler) const {
521   for (IterTy I = Begin; I != End; ++I) {
522     // skip debug value
523     if (I->isDebugValue())
524       continue;
525
526     if (terminateSearch(*I))
527       break;
528
529     assert((!I->isCall() && !I->isReturn() && !I->isBranch()) &&
530            "Cannot put calls, returns or branches in delay slot.");
531
532     if (delayHasHazard(*I, RegDU, IM))
533       continue;
534
535     if (TM.getSubtarget<MipsSubtarget>().isTargetNaCl()) {
536       // In NaCl, instructions that must be masked are forbidden in delay slots.
537       // We only check for loads, stores and SP changes.  Calls, returns and
538       // branches are not checked because non-NaCl targets never put them in
539       // delay slots.
540       unsigned AddrIdx;
541       if ((isBasePlusOffsetMemoryAccess(I->getOpcode(), &AddrIdx)
542            && baseRegNeedsLoadStoreMask(I->getOperand(AddrIdx).getReg()))
543           || I->modifiesRegister(Mips::SP, TM.getRegisterInfo()))
544         continue;
545     }
546
547     Filler = I;
548     return true;
549   }
550
551   return false;
552 }
553
554 bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
555   if (DisableBackwardSearch)
556     return false;
557
558   RegDefsUses RegDU(TM);
559   MemDefsUses MemDU(MBB.getParent()->getFrameInfo());
560   ReverseIter Filler;
561
562   RegDU.init(*Slot);
563
564   if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Filler))
565     return false;
566
567   MBB.splice(std::next(Slot), &MBB, std::next(Filler).base());
568   MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
569   ++UsefulSlots;
570   return true;
571 }
572
573 bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
574   // Can handle only calls.
575   if (DisableForwardSearch || !Slot->isCall())
576     return false;
577
578   RegDefsUses RegDU(TM);
579   NoMemInstr NM;
580   Iter Filler;
581
582   RegDU.setCallerSaved(*Slot);
583
584   if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Filler))
585     return false;
586
587   MBB.splice(std::next(Slot), &MBB, Filler);
588   MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
589   ++UsefulSlots;
590   return true;
591 }
592
593 bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
594   if (DisableSuccBBSearch)
595     return false;
596
597   MachineBasicBlock *SuccBB = selectSuccBB(MBB);
598
599   if (!SuccBB)
600     return false;
601
602   RegDefsUses RegDU(TM);
603   bool HasMultipleSuccs = false;
604   BB2BrMap BrMap;
605   OwningPtr<InspectMemInstr> IM;
606   Iter Filler;
607
608   // Iterate over SuccBB's predecessor list.
609   for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(),
610        PE = SuccBB->pred_end(); PI != PE; ++PI)
611     if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap))
612       return false;
613
614   // Do not allow moving instructions which have unallocatable register operands
615   // across basic block boundaries.
616   RegDU.setUnallocatableRegs(*MBB.getParent());
617
618   // Only allow moving loads from stack or constants if any of the SuccBB's
619   // predecessors have multiple successors.
620   if (HasMultipleSuccs) {
621     IM.reset(new LoadFromStackOrConst());
622   } else {
623     const MachineFrameInfo *MFI = MBB.getParent()->getFrameInfo();
624     IM.reset(new MemDefsUses(MFI));
625   }
626
627   if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Filler))
628     return false;
629
630   insertDelayFiller(Filler, BrMap);
631   addLiveInRegs(Filler, *SuccBB);
632   Filler->eraseFromParent();
633
634   return true;
635 }
636
637 MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const {
638   if (B.succ_empty())
639     return NULL;
640
641   // Select the successor with the larget edge weight.
642   auto &Prob = getAnalysis<MachineBranchProbabilityInfo>();
643   MachineBasicBlock *S = *std::max_element(B.succ_begin(), B.succ_end(),
644                                            [&](const MachineBasicBlock *Dst0,
645                                                const MachineBasicBlock *Dst1) {
646     return Prob.getEdgeWeight(&B, Dst0) < Prob.getEdgeWeight(&B, Dst1);
647   });
648   return S->isLandingPad() ? NULL : S;
649 }
650
651 std::pair<MipsInstrInfo::BranchType, MachineInstr *>
652 Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const {
653   const MipsInstrInfo *TII =
654     static_cast<const MipsInstrInfo*>(TM.getInstrInfo());
655   MachineBasicBlock *TrueBB = 0, *FalseBB = 0;
656   SmallVector<MachineInstr*, 2> BranchInstrs;
657   SmallVector<MachineOperand, 2> Cond;
658
659   MipsInstrInfo::BranchType R =
660     TII->AnalyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs);
661
662   if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch))
663     return std::make_pair(R, (MachineInstr*)NULL);
664
665   if (R != MipsInstrInfo::BT_CondUncond) {
666     if (!hasUnoccupiedSlot(BranchInstrs[0]))
667       return std::make_pair(MipsInstrInfo::BT_None, (MachineInstr*)NULL);
668
669     assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst)));
670
671     return std::make_pair(R, BranchInstrs[0]);
672   }
673
674   assert((TrueBB == &Dst) || (FalseBB == &Dst));
675
676   // Examine the conditional branch. See if its slot is occupied.
677   if (hasUnoccupiedSlot(BranchInstrs[0]))
678     return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]);
679
680   // If that fails, try the unconditional branch.
681   if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst))
682     return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]);
683
684   return std::make_pair(MipsInstrInfo::BT_None, (MachineInstr*)NULL);
685 }
686
687 bool Filler::examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
688                          RegDefsUses &RegDU, bool &HasMultipleSuccs,
689                          BB2BrMap &BrMap) const {
690   std::pair<MipsInstrInfo::BranchType, MachineInstr *> P =
691     getBranch(Pred, Succ);
692
693   // Return if either getBranch wasn't able to analyze the branches or there
694   // were no branches with unoccupied slots.
695   if (P.first == MipsInstrInfo::BT_None)
696     return false;
697
698   if ((P.first != MipsInstrInfo::BT_Uncond) &&
699       (P.first != MipsInstrInfo::BT_NoBranch)) {
700     HasMultipleSuccs = true;
701     RegDU.addLiveOut(Pred, Succ);
702   }
703
704   BrMap[&Pred] = P.second;
705   return true;
706 }
707
708 bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
709                             InspectMemInstr &IM) const {
710   bool HasHazard = (Candidate.isImplicitDef() || Candidate.isKill());
711
712   HasHazard |= IM.hasHazard(Candidate);
713   HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
714
715   return HasHazard;
716 }
717
718 bool Filler::terminateSearch(const MachineInstr &Candidate) const {
719   return (Candidate.isTerminator() || Candidate.isCall() ||
720           Candidate.isPosition() || Candidate.isInlineAsm() ||
721           Candidate.hasUnmodeledSideEffects());
722 }