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().getRegisterInfo()->getAllocatableSet(MF).test(R) && 279 "Shouldn't move an instruction with unallocatable registers across " 280 "basic block boundaries."); 281 #endif 282 283 if (!MBB.isLiveIn(R)) 284 MBB.addLiveIn(R); 285 } 286 } 287 288 RegDefsUses::RegDefsUses(TargetMachine &TM) 289 : TRI(*TM.getRegisterInfo()), Defs(TRI.getNumRegs(), false), 290 Uses(TRI.getNumRegs(), false) {} 291 292 void RegDefsUses::init(const MachineInstr &MI) { 293 // Add all register operands which are explicit and non-variadic. 294 update(MI, 0, MI.getDesc().getNumOperands()); 295 296 // If MI is a call, add RA to Defs to prevent users of RA from going into 297 // delay slot. 298 if (MI.isCall()) 299 Defs.set(Mips::RA); 300 301 // Add all implicit register operands of branch instructions except 302 // register AT. 303 if (MI.isBranch()) { 304 update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands()); 305 Defs.reset(Mips::AT); 306 } 307 } 308 309 void RegDefsUses::setCallerSaved(const MachineInstr &MI) { 310 assert(MI.isCall()); 311 312 // If MI is a call, add all caller-saved registers to Defs. 313 BitVector CallerSavedRegs(TRI.getNumRegs(), true); 314 315 CallerSavedRegs.reset(Mips::ZERO); 316 CallerSavedRegs.reset(Mips::ZERO_64); 317 318 for (const MCPhysReg *R = TRI.getCalleeSavedRegs(); *R; ++R) 319 for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI) 320 CallerSavedRegs.reset(*AI); 321 322 Defs |= CallerSavedRegs; 323 } 324 325 void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) { 326 BitVector AllocSet = TRI.getAllocatableSet(MF); 327 328 for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R)) 329 for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI) 330 AllocSet.set(*AI); 331 332 AllocSet.set(Mips::ZERO); 333 AllocSet.set(Mips::ZERO_64); 334 335 Defs |= AllocSet.flip(); 336 } 337 338 void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB, 339 const MachineBasicBlock &SuccBB) { 340 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(), 341 SE = MBB.succ_end(); SI != SE; ++SI) 342 if (*SI != &SuccBB) 343 for (MachineBasicBlock::livein_iterator LI = (*SI)->livein_begin(), 344 LE = (*SI)->livein_end(); LI != LE; ++LI) 345 Uses.set(*LI); 346 } 347 348 bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) { 349 BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs()); 350 bool HasHazard = false; 351 352 for (unsigned I = Begin; I != End; ++I) { 353 const MachineOperand &MO = MI.getOperand(I); 354 355 if (MO.isReg() && MO.getReg()) 356 HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef()); 357 } 358 359 Defs |= NewDefs; 360 Uses |= NewUses; 361 362 return HasHazard; 363 } 364 365 bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, 366 unsigned Reg, bool IsDef) const { 367 if (IsDef) { 368 NewDefs.set(Reg); 369 // check whether Reg has already been defined or used. 370 return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg)); 371 } 372 373 NewUses.set(Reg); 374 // check whether Reg has already been defined. 375 return isRegInSet(Defs, Reg); 376 } 377 378 bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const { 379 // Check Reg and all aliased Registers. 380 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI) 381 if (RegSet.test(*AI)) 382 return true; 383 return false; 384 } 385 386 bool InspectMemInstr::hasHazard(const MachineInstr &MI) { 387 if (!MI.mayStore() && !MI.mayLoad()) 388 return false; 389 390 if (ForbidMemInstr) 391 return true; 392 393 OrigSeenLoad = SeenLoad; 394 OrigSeenStore = SeenStore; 395 SeenLoad |= MI.mayLoad(); 396 SeenStore |= MI.mayStore(); 397 398 // If MI is an ordered or volatile memory reference, disallow moving 399 // subsequent loads and stores to delay slot. 400 if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) { 401 ForbidMemInstr = true; 402 return true; 403 } 404 405 return hasHazard_(MI); 406 } 407 408 bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) { 409 if (MI.mayStore()) 410 return true; 411 412 if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getPseudoValue()) 413 return true; 414 415 if (const PseudoSourceValue *PSV = 416 (*MI.memoperands_begin())->getPseudoValue()) { 417 if (isa<FixedStackPseudoSourceValue>(PSV)) 418 return false; 419 return !PSV->isConstant(nullptr) && PSV != PseudoSourceValue::getStack(); 420 } 421 422 return true; 423 } 424 425 MemDefsUses::MemDefsUses(const MachineFrameInfo *MFI_) 426 : InspectMemInstr(false), MFI(MFI_), SeenNoObjLoad(false), 427 SeenNoObjStore(false) {} 428 429 bool MemDefsUses::hasHazard_(const MachineInstr &MI) { 430 bool HasHazard = false; 431 SmallVector<ValueType, 4> Objs; 432 433 // Check underlying object list. 434 if (getUnderlyingObjects(MI, Objs)) { 435 for (SmallVectorImpl<ValueType>::const_iterator I = Objs.begin(); 436 I != Objs.end(); ++I) 437 HasHazard |= updateDefsUses(*I, MI.mayStore()); 438 439 return HasHazard; 440 } 441 442 // No underlying objects found. 443 HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore); 444 HasHazard |= MI.mayLoad() || OrigSeenStore; 445 446 SeenNoObjLoad |= MI.mayLoad(); 447 SeenNoObjStore |= MI.mayStore(); 448 449 return HasHazard; 450 } 451 452 bool MemDefsUses::updateDefsUses(ValueType V, bool MayStore) { 453 if (MayStore) 454 return !Defs.insert(V) || Uses.count(V) || SeenNoObjStore || SeenNoObjLoad; 455 456 Uses.insert(V); 457 return Defs.count(V) || SeenNoObjStore; 458 } 459 460 bool MemDefsUses:: 461 getUnderlyingObjects(const MachineInstr &MI, 462 SmallVectorImpl<ValueType> &Objects) const { 463 if (!MI.hasOneMemOperand() || 464 (!(*MI.memoperands_begin())->getValue() && 465 !(*MI.memoperands_begin())->getPseudoValue())) 466 return false; 467 468 if (const PseudoSourceValue *PSV = 469 (*MI.memoperands_begin())->getPseudoValue()) { 470 if (!PSV->isAliased(MFI)) 471 return false; 472 Objects.push_back(PSV); 473 return true; 474 } 475 476 const Value *V = (*MI.memoperands_begin())->getValue(); 477 478 SmallVector<Value *, 4> Objs; 479 GetUnderlyingObjects(const_cast<Value *>(V), Objs); 480 481 for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end(); 482 I != E; ++I) { 483 if (!isIdentifiedObject(V)) 484 return false; 485 486 Objects.push_back(*I); 487 } 488 489 return true; 490 } 491 492 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block. 493 /// We assume there is only one delay slot per delayed instruction. 494 bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) { 495 bool Changed = false; 496 497 for (Iter I = MBB.begin(); I != MBB.end(); ++I) { 498 if (!hasUnoccupiedSlot(&*I)) 499 continue; 500 501 ++FilledSlots; 502 Changed = true; 503 504 // Delay slot filling is disabled at -O0. 505 if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) { 506 if (searchBackward(MBB, I)) 507 continue; 508 509 if (I->isTerminator()) { 510 if (searchSuccBBs(MBB, I)) 511 continue; 512 } else if (searchForward(MBB, I)) { 513 continue; 514 } 515 } 516 517 // Bundle the NOP to the instruction with the delay slot. 518 const MipsInstrInfo *TII = 519 static_cast<const MipsInstrInfo*>(TM.getInstrInfo()); 520 BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP)); 521 MIBundleBuilder(MBB, I, std::next(I, 2)); 522 } 523 524 return Changed; 525 } 526 527 /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay 528 /// slots in Mips MachineFunctions 529 FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) { 530 return new Filler(tm); 531 } 532 533 template<typename IterTy> 534 bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End, 535 RegDefsUses &RegDU, InspectMemInstr& IM, 536 IterTy &Filler) const { 537 for (IterTy I = Begin; I != End; ++I) { 538 // skip debug value 539 if (I->isDebugValue()) 540 continue; 541 542 if (terminateSearch(*I)) 543 break; 544 545 assert((!I->isCall() && !I->isReturn() && !I->isBranch()) && 546 "Cannot put calls, returns or branches in delay slot."); 547 548 if (delayHasHazard(*I, RegDU, IM)) 549 continue; 550 551 if (TM.getSubtarget<MipsSubtarget>().isTargetNaCl()) { 552 // In NaCl, instructions that must be masked are forbidden in delay slots. 553 // We only check for loads, stores and SP changes. Calls, returns and 554 // branches are not checked because non-NaCl targets never put them in 555 // delay slots. 556 unsigned AddrIdx; 557 if ((isBasePlusOffsetMemoryAccess(I->getOpcode(), &AddrIdx) 558 && baseRegNeedsLoadStoreMask(I->getOperand(AddrIdx).getReg())) 559 || I->modifiesRegister(Mips::SP, TM.getRegisterInfo())) 560 continue; 561 } 562 563 Filler = I; 564 return true; 565 } 566 567 return false; 568 } 569 570 bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const { 571 if (DisableBackwardSearch) 572 return false; 573 574 RegDefsUses RegDU(TM); 575 MemDefsUses MemDU(MBB.getParent()->getFrameInfo()); 576 ReverseIter Filler; 577 578 RegDU.init(*Slot); 579 580 if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Filler)) 581 return false; 582 583 MBB.splice(std::next(Slot), &MBB, std::next(Filler).base()); 584 MIBundleBuilder(MBB, Slot, std::next(Slot, 2)); 585 ++UsefulSlots; 586 return true; 587 } 588 589 bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const { 590 // Can handle only calls. 591 if (DisableForwardSearch || !Slot->isCall()) 592 return false; 593 594 RegDefsUses RegDU(TM); 595 NoMemInstr NM; 596 Iter Filler; 597 598 RegDU.setCallerSaved(*Slot); 599 600 if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Filler)) 601 return false; 602 603 MBB.splice(std::next(Slot), &MBB, Filler); 604 MIBundleBuilder(MBB, Slot, std::next(Slot, 2)); 605 ++UsefulSlots; 606 return true; 607 } 608 609 bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const { 610 if (DisableSuccBBSearch) 611 return false; 612 613 MachineBasicBlock *SuccBB = selectSuccBB(MBB); 614 615 if (!SuccBB) 616 return false; 617 618 RegDefsUses RegDU(TM); 619 bool HasMultipleSuccs = false; 620 BB2BrMap BrMap; 621 std::unique_ptr<InspectMemInstr> IM; 622 Iter Filler; 623 624 // Iterate over SuccBB's predecessor list. 625 for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(), 626 PE = SuccBB->pred_end(); PI != PE; ++PI) 627 if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap)) 628 return false; 629 630 // Do not allow moving instructions which have unallocatable register operands 631 // across basic block boundaries. 632 RegDU.setUnallocatableRegs(*MBB.getParent()); 633 634 // Only allow moving loads from stack or constants if any of the SuccBB's 635 // predecessors have multiple successors. 636 if (HasMultipleSuccs) { 637 IM.reset(new LoadFromStackOrConst()); 638 } else { 639 const MachineFrameInfo *MFI = MBB.getParent()->getFrameInfo(); 640 IM.reset(new MemDefsUses(MFI)); 641 } 642 643 if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Filler)) 644 return false; 645 646 insertDelayFiller(Filler, BrMap); 647 addLiveInRegs(Filler, *SuccBB); 648 Filler->eraseFromParent(); 649 650 return true; 651 } 652 653 MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const { 654 if (B.succ_empty()) 655 return nullptr; 656 657 // Select the successor with the larget edge weight. 658 auto &Prob = getAnalysis<MachineBranchProbabilityInfo>(); 659 MachineBasicBlock *S = *std::max_element(B.succ_begin(), B.succ_end(), 660 [&](const MachineBasicBlock *Dst0, 661 const MachineBasicBlock *Dst1) { 662 return Prob.getEdgeWeight(&B, Dst0) < Prob.getEdgeWeight(&B, Dst1); 663 }); 664 return S->isLandingPad() ? nullptr : S; 665 } 666 667 std::pair<MipsInstrInfo::BranchType, MachineInstr *> 668 Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const { 669 const MipsInstrInfo *TII = 670 static_cast<const MipsInstrInfo*>(TM.getInstrInfo()); 671 MachineBasicBlock *TrueBB = nullptr, *FalseBB = nullptr; 672 SmallVector<MachineInstr*, 2> BranchInstrs; 673 SmallVector<MachineOperand, 2> Cond; 674 675 MipsInstrInfo::BranchType R = 676 TII->AnalyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs); 677 678 if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch)) 679 return std::make_pair(R, nullptr); 680 681 if (R != MipsInstrInfo::BT_CondUncond) { 682 if (!hasUnoccupiedSlot(BranchInstrs[0])) 683 return std::make_pair(MipsInstrInfo::BT_None, nullptr); 684 685 assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst))); 686 687 return std::make_pair(R, BranchInstrs[0]); 688 } 689 690 assert((TrueBB == &Dst) || (FalseBB == &Dst)); 691 692 // Examine the conditional branch. See if its slot is occupied. 693 if (hasUnoccupiedSlot(BranchInstrs[0])) 694 return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]); 695 696 // If that fails, try the unconditional branch. 697 if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst)) 698 return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]); 699 700 return std::make_pair(MipsInstrInfo::BT_None, nullptr); 701 } 702 703 bool Filler::examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ, 704 RegDefsUses &RegDU, bool &HasMultipleSuccs, 705 BB2BrMap &BrMap) const { 706 std::pair<MipsInstrInfo::BranchType, MachineInstr *> P = 707 getBranch(Pred, Succ); 708 709 // Return if either getBranch wasn't able to analyze the branches or there 710 // were no branches with unoccupied slots. 711 if (P.first == MipsInstrInfo::BT_None) 712 return false; 713 714 if ((P.first != MipsInstrInfo::BT_Uncond) && 715 (P.first != MipsInstrInfo::BT_NoBranch)) { 716 HasMultipleSuccs = true; 717 RegDU.addLiveOut(Pred, Succ); 718 } 719 720 BrMap[&Pred] = P.second; 721 return true; 722 } 723 724 bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU, 725 InspectMemInstr &IM) const { 726 bool HasHazard = (Candidate.isImplicitDef() || Candidate.isKill()); 727 728 HasHazard |= IM.hasHazard(Candidate); 729 HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands()); 730 731 return HasHazard; 732 } 733 734 bool Filler::terminateSearch(const MachineInstr &Candidate) const { 735 return (Candidate.isTerminator() || Candidate.isCall() || 736 Candidate.isPosition() || Candidate.isInlineAsm() || 737 Candidate.hasUnmodeledSideEffects()); 738 } 739