LLVM API Documentation

MipsDelaySlotFiller.cpp
Go to the documentation of this file.
00001 //===-- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler ------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Simple pass to fill delay slots with useful instructions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MCTargetDesc/MipsMCNaCl.h"
00015 #include "Mips.h"
00016 #include "MipsInstrInfo.h"
00017 #include "MipsTargetMachine.h"
00018 #include "llvm/ADT/BitVector.h"
00019 #include "llvm/ADT/SmallPtrSet.h"
00020 #include "llvm/ADT/Statistic.h"
00021 #include "llvm/Analysis/AliasAnalysis.h"
00022 #include "llvm/Analysis/ValueTracking.h"
00023 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
00024 #include "llvm/CodeGen/MachineFunctionPass.h"
00025 #include "llvm/CodeGen/MachineInstrBuilder.h"
00026 #include "llvm/CodeGen/MachineRegisterInfo.h"
00027 #include "llvm/CodeGen/PseudoSourceValue.h"
00028 #include "llvm/Support/CommandLine.h"
00029 #include "llvm/Target/TargetInstrInfo.h"
00030 #include "llvm/Target/TargetMachine.h"
00031 #include "llvm/Target/TargetRegisterInfo.h"
00032 
00033 using namespace llvm;
00034 
00035 #define DEBUG_TYPE "delay-slot-filler"
00036 
00037 STATISTIC(FilledSlots, "Number of delay slots filled");
00038 STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
00039                        " are not NOP.");
00040 
00041 static cl::opt<bool> DisableDelaySlotFiller(
00042   "disable-mips-delay-filler",
00043   cl::init(false),
00044   cl::desc("Fill all delay slots with NOPs."),
00045   cl::Hidden);
00046 
00047 static cl::opt<bool> DisableForwardSearch(
00048   "disable-mips-df-forward-search",
00049   cl::init(true),
00050   cl::desc("Disallow MIPS delay filler to search forward."),
00051   cl::Hidden);
00052 
00053 static cl::opt<bool> DisableSuccBBSearch(
00054   "disable-mips-df-succbb-search",
00055   cl::init(true),
00056   cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
00057   cl::Hidden);
00058 
00059 static cl::opt<bool> DisableBackwardSearch(
00060   "disable-mips-df-backward-search",
00061   cl::init(false),
00062   cl::desc("Disallow MIPS delay filler to search backward."),
00063   cl::Hidden);
00064 
00065 namespace {
00066   typedef MachineBasicBlock::iterator Iter;
00067   typedef MachineBasicBlock::reverse_iterator ReverseIter;
00068   typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
00069 
00070   class RegDefsUses {
00071   public:
00072     RegDefsUses(TargetMachine &TM);
00073     void init(const MachineInstr &MI);
00074 
00075     /// This function sets all caller-saved registers in Defs.
00076     void setCallerSaved(const MachineInstr &MI);
00077 
00078     /// This function sets all unallocatable registers in Defs.
00079     void setUnallocatableRegs(const MachineFunction &MF);
00080 
00081     /// Set bits in Uses corresponding to MBB's live-out registers except for
00082     /// the registers that are live-in to SuccBB.
00083     void addLiveOut(const MachineBasicBlock &MBB,
00084                     const MachineBasicBlock &SuccBB);
00085 
00086     bool update(const MachineInstr &MI, unsigned Begin, unsigned End);
00087 
00088   private:
00089     bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg,
00090                           bool IsDef) const;
00091 
00092     /// Returns true if Reg or its alias is in RegSet.
00093     bool isRegInSet(const BitVector &RegSet, unsigned Reg) const;
00094 
00095     const TargetRegisterInfo &TRI;
00096     BitVector Defs, Uses;
00097   };
00098 
00099   /// Base class for inspecting loads and stores.
00100   class InspectMemInstr {
00101   public:
00102     InspectMemInstr(bool ForbidMemInstr_)
00103       : OrigSeenLoad(false), OrigSeenStore(false), SeenLoad(false),
00104         SeenStore(false), ForbidMemInstr(ForbidMemInstr_) {}
00105 
00106     /// Return true if MI cannot be moved to delay slot.
00107     bool hasHazard(const MachineInstr &MI);
00108 
00109     virtual ~InspectMemInstr() {}
00110 
00111   protected:
00112     /// Flags indicating whether loads or stores have been seen.
00113     bool OrigSeenLoad, OrigSeenStore, SeenLoad, SeenStore;
00114 
00115     /// Memory instructions are not allowed to move to delay slot if this flag
00116     /// is true.
00117     bool ForbidMemInstr;
00118 
00119   private:
00120     virtual bool hasHazard_(const MachineInstr &MI) = 0;
00121   };
00122 
00123   /// This subclass rejects any memory instructions.
00124   class NoMemInstr : public InspectMemInstr {
00125   public:
00126     NoMemInstr() : InspectMemInstr(true) {}
00127   private:
00128     bool hasHazard_(const MachineInstr &MI) override { return true; }
00129   };
00130 
00131   /// This subclass accepts loads from stacks and constant loads.
00132   class LoadFromStackOrConst : public InspectMemInstr {
00133   public:
00134     LoadFromStackOrConst() : InspectMemInstr(false) {}
00135   private:
00136     bool hasHazard_(const MachineInstr &MI) override;
00137   };
00138 
00139   /// This subclass uses memory dependence information to determine whether a
00140   /// memory instruction can be moved to a delay slot.
00141   class MemDefsUses : public InspectMemInstr {
00142   public:
00143     MemDefsUses(const MachineFrameInfo *MFI);
00144 
00145   private:
00146     typedef PointerUnion<const Value *, const PseudoSourceValue *> ValueType;
00147 
00148     bool hasHazard_(const MachineInstr &MI) override;
00149 
00150     /// Update Defs and Uses. Return true if there exist dependences that
00151     /// disqualify the delay slot candidate between V and values in Uses and
00152     /// Defs.
00153     bool updateDefsUses(ValueType V, bool MayStore);
00154 
00155     /// Get the list of underlying objects of MI's memory operand.
00156     bool getUnderlyingObjects(const MachineInstr &MI,
00157                               SmallVectorImpl<ValueType> &Objects) const;
00158 
00159     const MachineFrameInfo *MFI;
00160     SmallPtrSet<ValueType, 4> Uses, Defs;
00161 
00162     /// Flags indicating whether loads or stores with no underlying objects have
00163     /// been seen.
00164     bool SeenNoObjLoad, SeenNoObjStore;
00165   };
00166 
00167   class Filler : public MachineFunctionPass {
00168   public:
00169     Filler(TargetMachine &tm)
00170       : MachineFunctionPass(ID), TM(tm) { }
00171 
00172     const char *getPassName() const override {
00173       return "Mips Delay Slot Filler";
00174     }
00175 
00176     bool runOnMachineFunction(MachineFunction &F) override {
00177       bool Changed = false;
00178       for (MachineFunction::iterator FI = F.begin(), FE = F.end();
00179            FI != FE; ++FI)
00180         Changed |= runOnMachineBasicBlock(*FI);
00181 
00182       // This pass invalidates liveness information when it reorders
00183       // instructions to fill delay slot. Without this, -verify-machineinstrs
00184       // will fail.
00185       if (Changed)
00186         F.getRegInfo().invalidateLiveness();
00187 
00188       return Changed;
00189     }
00190 
00191     void getAnalysisUsage(AnalysisUsage &AU) const override {
00192       AU.addRequired<MachineBranchProbabilityInfo>();
00193       MachineFunctionPass::getAnalysisUsage(AU);
00194     }
00195 
00196   private:
00197     bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
00198 
00199     /// This function checks if it is valid to move Candidate to the delay slot
00200     /// and returns true if it isn't. It also updates memory and register
00201     /// dependence information.
00202     bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
00203                         InspectMemInstr &IM) const;
00204 
00205     /// This function searches range [Begin, End) for an instruction that can be
00206     /// moved to the delay slot. Returns true on success.
00207     template<typename IterTy>
00208     bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
00209                      RegDefsUses &RegDU, InspectMemInstr &IM,
00210                      IterTy &Filler) const;
00211 
00212     /// This function searches in the backward direction for an instruction that
00213     /// can be moved to the delay slot. Returns true on success.
00214     bool searchBackward(MachineBasicBlock &MBB, Iter Slot) const;
00215 
00216     /// This function searches MBB in the forward direction for an instruction
00217     /// that can be moved to the delay slot. Returns true on success.
00218     bool searchForward(MachineBasicBlock &MBB, Iter Slot) const;
00219 
00220     /// This function searches one of MBB's successor blocks for an instruction
00221     /// that can be moved to the delay slot and inserts clones of the
00222     /// instruction into the successor's predecessor blocks.
00223     bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
00224 
00225     /// Pick a successor block of MBB. Return NULL if MBB doesn't have a
00226     /// successor block that is not a landing pad.
00227     MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const;
00228 
00229     /// This function analyzes MBB and returns an instruction with an unoccupied
00230     /// slot that branches to Dst.
00231     std::pair<MipsInstrInfo::BranchType, MachineInstr *>
00232     getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const;
00233 
00234     /// Examine Pred and see if it is possible to insert an instruction into
00235     /// one of its branches delay slot or its end.
00236     bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
00237                      RegDefsUses &RegDU, bool &HasMultipleSuccs,
00238                      BB2BrMap &BrMap) const;
00239 
00240     bool terminateSearch(const MachineInstr &Candidate) const;
00241 
00242     TargetMachine &TM;
00243 
00244     static char ID;
00245   };
00246   char Filler::ID = 0;
00247 } // end of anonymous namespace
00248 
00249 static bool hasUnoccupiedSlot(const MachineInstr *MI) {
00250   return MI->hasDelaySlot() && !MI->isBundledWithSucc();
00251 }
00252 
00253 /// This function inserts clones of Filler into predecessor blocks.
00254 static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
00255   MachineFunction *MF = Filler->getParent()->getParent();
00256 
00257   for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
00258     if (I->second) {
00259       MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
00260       ++UsefulSlots;
00261     } else {
00262       I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
00263     }
00264   }
00265 }
00266 
00267 /// This function adds registers Filler defines to MBB's live-in register list.
00268 static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) {
00269   for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) {
00270     const MachineOperand &MO = Filler->getOperand(I);
00271     unsigned R;
00272 
00273     if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg()))
00274       continue;
00275 
00276 #ifndef NDEBUG
00277     const MachineFunction &MF = *MBB.getParent();
00278     assert(MF.getTarget()
00279                .getSubtargetImpl()
00280                ->getRegisterInfo()
00281                ->getAllocatableSet(MF)
00282                .test(R) &&
00283            "Shouldn't move an instruction with unallocatable registers across "
00284            "basic block boundaries.");
00285 #endif
00286 
00287     if (!MBB.isLiveIn(R))
00288       MBB.addLiveIn(R);
00289   }
00290 }
00291 
00292 RegDefsUses::RegDefsUses(TargetMachine &TM)
00293     : TRI(*TM.getSubtargetImpl()->getRegisterInfo()),
00294       Defs(TRI.getNumRegs(), false), Uses(TRI.getNumRegs(), false) {}
00295 
00296 void RegDefsUses::init(const MachineInstr &MI) {
00297   // Add all register operands which are explicit and non-variadic.
00298   update(MI, 0, MI.getDesc().getNumOperands());
00299 
00300   // If MI is a call, add RA to Defs to prevent users of RA from going into
00301   // delay slot.
00302   if (MI.isCall())
00303     Defs.set(Mips::RA);
00304 
00305   // Add all implicit register operands of branch instructions except
00306   // register AT.
00307   if (MI.isBranch()) {
00308     update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands());
00309     Defs.reset(Mips::AT);
00310   }
00311 }
00312 
00313 void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
00314   assert(MI.isCall());
00315 
00316   // If MI is a call, add all caller-saved registers to Defs.
00317   BitVector CallerSavedRegs(TRI.getNumRegs(), true);
00318 
00319   CallerSavedRegs.reset(Mips::ZERO);
00320   CallerSavedRegs.reset(Mips::ZERO_64);
00321 
00322   for (const MCPhysReg *R = TRI.getCalleeSavedRegs(); *R; ++R)
00323     for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
00324       CallerSavedRegs.reset(*AI);
00325 
00326   Defs |= CallerSavedRegs;
00327 }
00328 
00329 void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) {
00330   BitVector AllocSet = TRI.getAllocatableSet(MF);
00331 
00332   for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R))
00333     for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
00334       AllocSet.set(*AI);
00335 
00336   AllocSet.set(Mips::ZERO);
00337   AllocSet.set(Mips::ZERO_64);
00338 
00339   Defs |= AllocSet.flip();
00340 }
00341 
00342 void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB,
00343                              const MachineBasicBlock &SuccBB) {
00344   for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
00345        SE = MBB.succ_end(); SI != SE; ++SI)
00346     if (*SI != &SuccBB)
00347       for (MachineBasicBlock::livein_iterator LI = (*SI)->livein_begin(),
00348            LE = (*SI)->livein_end(); LI != LE; ++LI)
00349         Uses.set(*LI);
00350 }
00351 
00352 bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) {
00353   BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs());
00354   bool HasHazard = false;
00355 
00356   for (unsigned I = Begin; I != End; ++I) {
00357     const MachineOperand &MO = MI.getOperand(I);
00358 
00359     if (MO.isReg() && MO.getReg())
00360       HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef());
00361   }
00362 
00363   Defs |= NewDefs;
00364   Uses |= NewUses;
00365 
00366   return HasHazard;
00367 }
00368 
00369 bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses,
00370                                    unsigned Reg, bool IsDef) const {
00371   if (IsDef) {
00372     NewDefs.set(Reg);
00373     // check whether Reg has already been defined or used.
00374     return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg));
00375   }
00376 
00377   NewUses.set(Reg);
00378   // check whether Reg has already been defined.
00379   return isRegInSet(Defs, Reg);
00380 }
00381 
00382 bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const {
00383   // Check Reg and all aliased Registers.
00384   for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
00385     if (RegSet.test(*AI))
00386       return true;
00387   return false;
00388 }
00389 
00390 bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
00391   if (!MI.mayStore() && !MI.mayLoad())
00392     return false;
00393 
00394   if (ForbidMemInstr)
00395     return true;
00396 
00397   OrigSeenLoad = SeenLoad;
00398   OrigSeenStore = SeenStore;
00399   SeenLoad |= MI.mayLoad();
00400   SeenStore |= MI.mayStore();
00401 
00402   // If MI is an ordered or volatile memory reference, disallow moving
00403   // subsequent loads and stores to delay slot.
00404   if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) {
00405     ForbidMemInstr = true;
00406     return true;
00407   }
00408 
00409   return hasHazard_(MI);
00410 }
00411 
00412 bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
00413   if (MI.mayStore())
00414     return true;
00415 
00416   if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getPseudoValue())
00417     return true;
00418 
00419   if (const PseudoSourceValue *PSV =
00420       (*MI.memoperands_begin())->getPseudoValue()) {
00421     if (isa<FixedStackPseudoSourceValue>(PSV))
00422       return false;
00423     return !PSV->isConstant(nullptr) && PSV != PseudoSourceValue::getStack();
00424   }
00425 
00426   return true;
00427 }
00428 
00429 MemDefsUses::MemDefsUses(const MachineFrameInfo *MFI_)
00430   : InspectMemInstr(false), MFI(MFI_), SeenNoObjLoad(false),
00431     SeenNoObjStore(false) {}
00432 
00433 bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
00434   bool HasHazard = false;
00435   SmallVector<ValueType, 4> Objs;
00436 
00437   // Check underlying object list.
00438   if (getUnderlyingObjects(MI, Objs)) {
00439     for (SmallVectorImpl<ValueType>::const_iterator I = Objs.begin();
00440          I != Objs.end(); ++I)
00441       HasHazard |= updateDefsUses(*I, MI.mayStore());
00442 
00443     return HasHazard;
00444   }
00445 
00446   // No underlying objects found.
00447   HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore);
00448   HasHazard |= MI.mayLoad() || OrigSeenStore;
00449 
00450   SeenNoObjLoad |= MI.mayLoad();
00451   SeenNoObjStore |= MI.mayStore();
00452 
00453   return HasHazard;
00454 }
00455 
00456 bool MemDefsUses::updateDefsUses(ValueType V, bool MayStore) {
00457   if (MayStore)
00458     return !Defs.insert(V) || Uses.count(V) || SeenNoObjStore || SeenNoObjLoad;
00459 
00460   Uses.insert(V);
00461   return Defs.count(V) || SeenNoObjStore;
00462 }
00463 
00464 bool MemDefsUses::
00465 getUnderlyingObjects(const MachineInstr &MI,
00466                      SmallVectorImpl<ValueType> &Objects) const {
00467   if (!MI.hasOneMemOperand() ||
00468       (!(*MI.memoperands_begin())->getValue() &&
00469        !(*MI.memoperands_begin())->getPseudoValue()))
00470     return false;
00471 
00472   if (const PseudoSourceValue *PSV =
00473       (*MI.memoperands_begin())->getPseudoValue()) {
00474     if (!PSV->isAliased(MFI))
00475       return false;
00476     Objects.push_back(PSV);
00477     return true;
00478   }
00479 
00480   const Value *V = (*MI.memoperands_begin())->getValue();
00481 
00482   SmallVector<Value *, 4> Objs;
00483   GetUnderlyingObjects(const_cast<Value *>(V), Objs);
00484 
00485   for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end();
00486        I != E; ++I) {
00487     if (!isIdentifiedObject(V))
00488       return false;
00489 
00490     Objects.push_back(*I);
00491   }
00492 
00493   return true;
00494 }
00495 
00496 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
00497 /// We assume there is only one delay slot per delayed instruction.
00498 bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
00499   bool Changed = false;
00500 
00501   for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
00502     if (!hasUnoccupiedSlot(&*I))
00503       continue;
00504 
00505     ++FilledSlots;
00506     Changed = true;
00507 
00508     // Delay slot filling is disabled at -O0.
00509     if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) {
00510       if (searchBackward(MBB, I))
00511         continue;
00512 
00513       if (I->isTerminator()) {
00514         if (searchSuccBBs(MBB, I))
00515           continue;
00516       } else if (searchForward(MBB, I)) {
00517         continue;
00518       }
00519     }
00520 
00521     // Bundle the NOP to the instruction with the delay slot.
00522     const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>(
00523         TM.getSubtargetImpl()->getInstrInfo());
00524     BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
00525     MIBundleBuilder(MBB, I, std::next(I, 2));
00526   }
00527 
00528   return Changed;
00529 }
00530 
00531 /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
00532 /// slots in Mips MachineFunctions
00533 FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
00534   return new Filler(tm);
00535 }
00536 
00537 template<typename IterTy>
00538 bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
00539                          RegDefsUses &RegDU, InspectMemInstr& IM,
00540                          IterTy &Filler) const {
00541   for (IterTy I = Begin; I != End; ++I) {
00542     // skip debug value
00543     if (I->isDebugValue())
00544       continue;
00545 
00546     if (terminateSearch(*I))
00547       break;
00548 
00549     assert((!I->isCall() && !I->isReturn() && !I->isBranch()) &&
00550            "Cannot put calls, returns or branches in delay slot.");
00551 
00552     if (delayHasHazard(*I, RegDU, IM))
00553       continue;
00554 
00555     if (TM.getSubtarget<MipsSubtarget>().isTargetNaCl()) {
00556       // In NaCl, instructions that must be masked are forbidden in delay slots.
00557       // We only check for loads, stores and SP changes.  Calls, returns and
00558       // branches are not checked because non-NaCl targets never put them in
00559       // delay slots.
00560       unsigned AddrIdx;
00561       if ((isBasePlusOffsetMemoryAccess(I->getOpcode(), &AddrIdx) &&
00562            baseRegNeedsLoadStoreMask(I->getOperand(AddrIdx).getReg())) ||
00563           I->modifiesRegister(Mips::SP,
00564                               TM.getSubtargetImpl()->getRegisterInfo()))
00565         continue;
00566     }
00567 
00568     Filler = I;
00569     return true;
00570   }
00571 
00572   return false;
00573 }
00574 
00575 bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
00576   if (DisableBackwardSearch)
00577     return false;
00578 
00579   RegDefsUses RegDU(TM);
00580   MemDefsUses MemDU(MBB.getParent()->getFrameInfo());
00581   ReverseIter Filler;
00582 
00583   RegDU.init(*Slot);
00584 
00585   if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Filler))
00586     return false;
00587 
00588   MBB.splice(std::next(Slot), &MBB, std::next(Filler).base());
00589   MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
00590   ++UsefulSlots;
00591   return true;
00592 }
00593 
00594 bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
00595   // Can handle only calls.
00596   if (DisableForwardSearch || !Slot->isCall())
00597     return false;
00598 
00599   RegDefsUses RegDU(TM);
00600   NoMemInstr NM;
00601   Iter Filler;
00602 
00603   RegDU.setCallerSaved(*Slot);
00604 
00605   if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Filler))
00606     return false;
00607 
00608   MBB.splice(std::next(Slot), &MBB, Filler);
00609   MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
00610   ++UsefulSlots;
00611   return true;
00612 }
00613 
00614 bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
00615   if (DisableSuccBBSearch)
00616     return false;
00617 
00618   MachineBasicBlock *SuccBB = selectSuccBB(MBB);
00619 
00620   if (!SuccBB)
00621     return false;
00622 
00623   RegDefsUses RegDU(TM);
00624   bool HasMultipleSuccs = false;
00625   BB2BrMap BrMap;
00626   std::unique_ptr<InspectMemInstr> IM;
00627   Iter Filler;
00628 
00629   // Iterate over SuccBB's predecessor list.
00630   for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(),
00631        PE = SuccBB->pred_end(); PI != PE; ++PI)
00632     if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap))
00633       return false;
00634 
00635   // Do not allow moving instructions which have unallocatable register operands
00636   // across basic block boundaries.
00637   RegDU.setUnallocatableRegs(*MBB.getParent());
00638 
00639   // Only allow moving loads from stack or constants if any of the SuccBB's
00640   // predecessors have multiple successors.
00641   if (HasMultipleSuccs) {
00642     IM.reset(new LoadFromStackOrConst());
00643   } else {
00644     const MachineFrameInfo *MFI = MBB.getParent()->getFrameInfo();
00645     IM.reset(new MemDefsUses(MFI));
00646   }
00647 
00648   if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Filler))
00649     return false;
00650 
00651   insertDelayFiller(Filler, BrMap);
00652   addLiveInRegs(Filler, *SuccBB);
00653   Filler->eraseFromParent();
00654 
00655   return true;
00656 }
00657 
00658 MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const {
00659   if (B.succ_empty())
00660     return nullptr;
00661 
00662   // Select the successor with the larget edge weight.
00663   auto &Prob = getAnalysis<MachineBranchProbabilityInfo>();
00664   MachineBasicBlock *S = *std::max_element(B.succ_begin(), B.succ_end(),
00665                                            [&](const MachineBasicBlock *Dst0,
00666                                                const MachineBasicBlock *Dst1) {
00667     return Prob.getEdgeWeight(&B, Dst0) < Prob.getEdgeWeight(&B, Dst1);
00668   });
00669   return S->isLandingPad() ? nullptr : S;
00670 }
00671 
00672 std::pair<MipsInstrInfo::BranchType, MachineInstr *>
00673 Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const {
00674   const MipsInstrInfo *TII =
00675       static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo());
00676   MachineBasicBlock *TrueBB = nullptr, *FalseBB = nullptr;
00677   SmallVector<MachineInstr*, 2> BranchInstrs;
00678   SmallVector<MachineOperand, 2> Cond;
00679 
00680   MipsInstrInfo::BranchType R =
00681     TII->AnalyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs);
00682 
00683   if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch))
00684     return std::make_pair(R, nullptr);
00685 
00686   if (R != MipsInstrInfo::BT_CondUncond) {
00687     if (!hasUnoccupiedSlot(BranchInstrs[0]))
00688       return std::make_pair(MipsInstrInfo::BT_None, nullptr);
00689 
00690     assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst)));
00691 
00692     return std::make_pair(R, BranchInstrs[0]);
00693   }
00694 
00695   assert((TrueBB == &Dst) || (FalseBB == &Dst));
00696 
00697   // Examine the conditional branch. See if its slot is occupied.
00698   if (hasUnoccupiedSlot(BranchInstrs[0]))
00699     return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]);
00700 
00701   // If that fails, try the unconditional branch.
00702   if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst))
00703     return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]);
00704 
00705   return std::make_pair(MipsInstrInfo::BT_None, nullptr);
00706 }
00707 
00708 bool Filler::examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
00709                          RegDefsUses &RegDU, bool &HasMultipleSuccs,
00710                          BB2BrMap &BrMap) const {
00711   std::pair<MipsInstrInfo::BranchType, MachineInstr *> P =
00712     getBranch(Pred, Succ);
00713 
00714   // Return if either getBranch wasn't able to analyze the branches or there
00715   // were no branches with unoccupied slots.
00716   if (P.first == MipsInstrInfo::BT_None)
00717     return false;
00718 
00719   if ((P.first != MipsInstrInfo::BT_Uncond) &&
00720       (P.first != MipsInstrInfo::BT_NoBranch)) {
00721     HasMultipleSuccs = true;
00722     RegDU.addLiveOut(Pred, Succ);
00723   }
00724 
00725   BrMap[&Pred] = P.second;
00726   return true;
00727 }
00728 
00729 bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
00730                             InspectMemInstr &IM) const {
00731   bool HasHazard = (Candidate.isImplicitDef() || Candidate.isKill());
00732 
00733   HasHazard |= IM.hasHazard(Candidate);
00734   HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
00735 
00736   return HasHazard;
00737 }
00738 
00739 bool Filler::terminateSearch(const MachineInstr &Candidate) const {
00740   return (Candidate.isTerminator() || Candidate.isCall() ||
00741           Candidate.isPosition() || Candidate.isInlineAsm() ||
00742           Candidate.hasUnmodeledSideEffects());
00743 }