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