1 //===- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Simple pass to fill delay slots with useful instructions.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/MipsMCNaCl.h"
16 #include "MipsInstrInfo.h"
17 #include "MipsRegisterInfo.h"
18 #include "MipsSubtarget.h"
19 #include "llvm/ADT/BitVector.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Analysis/AliasAnalysis.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/CodeGen/MachineBasicBlock.h"
29 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineOperand.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/PseudoSourceValue.h"
37 #include "llvm/CodeGen/TargetRegisterInfo.h"
38 #include "llvm/CodeGen/TargetSubtargetInfo.h"
39 #include "llvm/MC/MCInstrDesc.h"
40 #include "llvm/MC/MCRegisterInfo.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CodeGen.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Target/TargetMachine.h"
54 #define DEBUG_TYPE "mips-delay-slot-filler"
56 STATISTIC(FilledSlots
, "Number of delay slots filled");
57 STATISTIC(UsefulSlots
, "Number of delay slots filled with instructions that"
60 static cl::opt
<bool> DisableDelaySlotFiller(
61 "disable-mips-delay-filler",
63 cl::desc("Fill all delay slots with NOPs."),
66 static cl::opt
<bool> DisableForwardSearch(
67 "disable-mips-df-forward-search",
69 cl::desc("Disallow MIPS delay filler to search forward."),
72 static cl::opt
<bool> DisableSuccBBSearch(
73 "disable-mips-df-succbb-search",
75 cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
78 static cl::opt
<bool> DisableBackwardSearch(
79 "disable-mips-df-backward-search",
81 cl::desc("Disallow MIPS delay filler to search backward."),
84 enum CompactBranchPolicy
{
85 CB_Never
, ///< The policy 'never' may in some circumstances or for some
86 ///< ISAs not be absolutely adhered to.
87 CB_Optimal
, ///< Optimal is the default and will produce compact branches
88 ///< when delay slots cannot be filled.
89 CB_Always
///< 'always' may in some circumstances may not be
90 ///< absolutely adhered to there may not be a corresponding
91 ///< compact form of a branch.
94 static cl::opt
<CompactBranchPolicy
> MipsCompactBranchPolicy(
95 "mips-compact-branches",cl::Optional
,
97 cl::desc("MIPS Specific: Compact branch policy."),
99 clEnumValN(CB_Never
, "never", "Do not use compact branches if possible."),
100 clEnumValN(CB_Optimal
, "optimal", "Use compact branches where appropiate (default)."),
101 clEnumValN(CB_Always
, "always", "Always use compact branches if possible.")
107 using Iter
= MachineBasicBlock::iterator
;
108 using ReverseIter
= MachineBasicBlock::reverse_iterator
;
109 using BB2BrMap
= SmallDenseMap
<MachineBasicBlock
*, MachineInstr
*, 2>;
113 RegDefsUses(const TargetRegisterInfo
&TRI
);
115 void init(const MachineInstr
&MI
);
117 /// This function sets all caller-saved registers in Defs.
118 void setCallerSaved(const MachineInstr
&MI
);
120 /// This function sets all unallocatable registers in Defs.
121 void setUnallocatableRegs(const MachineFunction
&MF
);
123 /// Set bits in Uses corresponding to MBB's live-out registers except for
124 /// the registers that are live-in to SuccBB.
125 void addLiveOut(const MachineBasicBlock
&MBB
,
126 const MachineBasicBlock
&SuccBB
);
128 bool update(const MachineInstr
&MI
, unsigned Begin
, unsigned End
);
131 bool checkRegDefsUses(BitVector
&NewDefs
, BitVector
&NewUses
, unsigned Reg
,
134 /// Returns true if Reg or its alias is in RegSet.
135 bool isRegInSet(const BitVector
&RegSet
, unsigned Reg
) const;
137 const TargetRegisterInfo
&TRI
;
138 BitVector Defs
, Uses
;
141 /// Base class for inspecting loads and stores.
142 class InspectMemInstr
{
144 InspectMemInstr(bool ForbidMemInstr_
) : ForbidMemInstr(ForbidMemInstr_
) {}
145 virtual ~InspectMemInstr() = default;
147 /// Return true if MI cannot be moved to delay slot.
148 bool hasHazard(const MachineInstr
&MI
);
151 /// Flags indicating whether loads or stores have been seen.
152 bool OrigSeenLoad
= false;
153 bool OrigSeenStore
= false;
154 bool SeenLoad
= false;
155 bool SeenStore
= false;
157 /// Memory instructions are not allowed to move to delay slot if this flag
162 virtual bool hasHazard_(const MachineInstr
&MI
) = 0;
165 /// This subclass rejects any memory instructions.
166 class NoMemInstr
: public InspectMemInstr
{
168 NoMemInstr() : InspectMemInstr(true) {}
171 bool hasHazard_(const MachineInstr
&MI
) override
{ return true; }
174 /// This subclass accepts loads from stacks and constant loads.
175 class LoadFromStackOrConst
: public InspectMemInstr
{
177 LoadFromStackOrConst() : InspectMemInstr(false) {}
180 bool hasHazard_(const MachineInstr
&MI
) override
;
183 /// This subclass uses memory dependence information to determine whether a
184 /// memory instruction can be moved to a delay slot.
185 class MemDefsUses
: public InspectMemInstr
{
187 MemDefsUses(const DataLayout
&DL
, const MachineFrameInfo
*MFI
);
190 using ValueType
= PointerUnion
<const Value
*, const PseudoSourceValue
*>;
192 bool hasHazard_(const MachineInstr
&MI
) override
;
194 /// Update Defs and Uses. Return true if there exist dependences that
195 /// disqualify the delay slot candidate between V and values in Uses and
197 bool updateDefsUses(ValueType V
, bool MayStore
);
199 /// Get the list of underlying objects of MI's memory operand.
200 bool getUnderlyingObjects(const MachineInstr
&MI
,
201 SmallVectorImpl
<ValueType
> &Objects
) const;
203 const MachineFrameInfo
*MFI
;
204 SmallPtrSet
<ValueType
, 4> Uses
, Defs
;
205 const DataLayout
&DL
;
207 /// Flags indicating whether loads or stores with no underlying objects have
209 bool SeenNoObjLoad
= false;
210 bool SeenNoObjStore
= false;
213 class MipsDelaySlotFiller
: public MachineFunctionPass
{
215 MipsDelaySlotFiller() : MachineFunctionPass(ID
) {
216 initializeMipsDelaySlotFillerPass(*PassRegistry::getPassRegistry());
219 StringRef
getPassName() const override
{ return "Mips Delay Slot Filler"; }
221 bool runOnMachineFunction(MachineFunction
&F
) override
{
223 bool Changed
= false;
224 for (MachineFunction::iterator FI
= F
.begin(), FE
= F
.end();
226 Changed
|= runOnMachineBasicBlock(*FI
);
228 // This pass invalidates liveness information when it reorders
229 // instructions to fill delay slot. Without this, -verify-machineinstrs
232 F
.getRegInfo().invalidateLiveness();
237 MachineFunctionProperties
getRequiredProperties() const override
{
238 return MachineFunctionProperties().set(
239 MachineFunctionProperties::Property::NoVRegs
);
242 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
243 AU
.addRequired
<MachineBranchProbabilityInfo
>();
244 MachineFunctionPass::getAnalysisUsage(AU
);
250 bool runOnMachineBasicBlock(MachineBasicBlock
&MBB
);
252 Iter
replaceWithCompactBranch(MachineBasicBlock
&MBB
, Iter Branch
,
255 /// This function checks if it is valid to move Candidate to the delay slot
256 /// and returns true if it isn't. It also updates memory and register
257 /// dependence information.
258 bool delayHasHazard(const MachineInstr
&Candidate
, RegDefsUses
&RegDU
,
259 InspectMemInstr
&IM
) const;
261 /// This function searches range [Begin, End) for an instruction that can be
262 /// moved to the delay slot. Returns true on success.
263 template<typename IterTy
>
264 bool searchRange(MachineBasicBlock
&MBB
, IterTy Begin
, IterTy End
,
265 RegDefsUses
&RegDU
, InspectMemInstr
&IM
, Iter Slot
,
266 IterTy
&Filler
) const;
268 /// This function searches in the backward direction for an instruction that
269 /// can be moved to the delay slot. Returns true on success.
270 bool searchBackward(MachineBasicBlock
&MBB
, MachineInstr
&Slot
) const;
272 /// This function searches MBB in the forward direction for an instruction
273 /// that can be moved to the delay slot. Returns true on success.
274 bool searchForward(MachineBasicBlock
&MBB
, Iter Slot
) const;
276 /// This function searches one of MBB's successor blocks for an instruction
277 /// that can be moved to the delay slot and inserts clones of the
278 /// instruction into the successor's predecessor blocks.
279 bool searchSuccBBs(MachineBasicBlock
&MBB
, Iter Slot
) const;
281 /// Pick a successor block of MBB. Return NULL if MBB doesn't have a
282 /// successor block that is not a landing pad.
283 MachineBasicBlock
*selectSuccBB(MachineBasicBlock
&B
) const;
285 /// This function analyzes MBB and returns an instruction with an unoccupied
286 /// slot that branches to Dst.
287 std::pair
<MipsInstrInfo::BranchType
, MachineInstr
*>
288 getBranch(MachineBasicBlock
&MBB
, const MachineBasicBlock
&Dst
) const;
290 /// Examine Pred and see if it is possible to insert an instruction into
291 /// one of its branches delay slot or its end.
292 bool examinePred(MachineBasicBlock
&Pred
, const MachineBasicBlock
&Succ
,
293 RegDefsUses
&RegDU
, bool &HasMultipleSuccs
,
294 BB2BrMap
&BrMap
) const;
296 bool terminateSearch(const MachineInstr
&Candidate
) const;
298 const TargetMachine
*TM
= nullptr;
301 } // end anonymous namespace
303 char MipsDelaySlotFiller::ID
= 0;
305 static bool hasUnoccupiedSlot(const MachineInstr
*MI
) {
306 return MI
->hasDelaySlot() && !MI
->isBundledWithSucc();
309 INITIALIZE_PASS(MipsDelaySlotFiller
, DEBUG_TYPE
,
310 "Fill delay slot for MIPS", false, false)
312 /// This function inserts clones of Filler into predecessor blocks.
313 static void insertDelayFiller(Iter Filler
, const BB2BrMap
&BrMap
) {
314 MachineFunction
*MF
= Filler
->getParent()->getParent();
316 for (BB2BrMap::const_iterator I
= BrMap
.begin(); I
!= BrMap
.end(); ++I
) {
318 MIBundleBuilder(I
->second
).append(MF
->CloneMachineInstr(&*Filler
));
321 I
->first
->insert(I
->first
->end(), MF
->CloneMachineInstr(&*Filler
));
326 /// This function adds registers Filler defines to MBB's live-in register list.
327 static void addLiveInRegs(Iter Filler
, MachineBasicBlock
&MBB
) {
328 for (unsigned I
= 0, E
= Filler
->getNumOperands(); I
!= E
; ++I
) {
329 const MachineOperand
&MO
= Filler
->getOperand(I
);
332 if (!MO
.isReg() || !MO
.isDef() || !(R
= MO
.getReg()))
336 const MachineFunction
&MF
= *MBB
.getParent();
337 assert(MF
.getSubtarget().getRegisterInfo()->getAllocatableSet(MF
).test(R
) &&
338 "Shouldn't move an instruction with unallocatable registers across "
339 "basic block boundaries.");
342 if (!MBB
.isLiveIn(R
))
347 RegDefsUses::RegDefsUses(const TargetRegisterInfo
&TRI
)
348 : TRI(TRI
), Defs(TRI
.getNumRegs(), false), Uses(TRI
.getNumRegs(), false) {}
350 void RegDefsUses::init(const MachineInstr
&MI
) {
351 // Add all register operands which are explicit and non-variadic.
352 update(MI
, 0, MI
.getDesc().getNumOperands());
354 // If MI is a call, add RA to Defs to prevent users of RA from going into
359 // Add all implicit register operands of branch instructions except
362 update(MI
, MI
.getDesc().getNumOperands(), MI
.getNumOperands());
363 Defs
.reset(Mips::AT
);
367 void RegDefsUses::setCallerSaved(const MachineInstr
&MI
) {
370 // Add RA/RA_64 to Defs to prevent users of RA/RA_64 from going into
371 // the delay slot. The reason is that RA/RA_64 must not be changed
372 // in the delay slot so that the callee can return to the caller.
373 if (MI
.definesRegister(Mips::RA
) || MI
.definesRegister(Mips::RA_64
)) {
375 Defs
.set(Mips::RA_64
);
378 // If MI is a call, add all caller-saved registers to Defs.
379 BitVector
CallerSavedRegs(TRI
.getNumRegs(), true);
381 CallerSavedRegs
.reset(Mips::ZERO
);
382 CallerSavedRegs
.reset(Mips::ZERO_64
);
384 for (const MCPhysReg
*R
= TRI
.getCalleeSavedRegs(MI
.getParent()->getParent());
386 for (MCRegAliasIterator
AI(*R
, &TRI
, true); AI
.isValid(); ++AI
)
387 CallerSavedRegs
.reset(*AI
);
389 Defs
|= CallerSavedRegs
;
392 void RegDefsUses::setUnallocatableRegs(const MachineFunction
&MF
) {
393 BitVector AllocSet
= TRI
.getAllocatableSet(MF
);
395 for (unsigned R
: AllocSet
.set_bits())
396 for (MCRegAliasIterator
AI(R
, &TRI
, false); AI
.isValid(); ++AI
)
399 AllocSet
.set(Mips::ZERO
);
400 AllocSet
.set(Mips::ZERO_64
);
402 Defs
|= AllocSet
.flip();
405 void RegDefsUses::addLiveOut(const MachineBasicBlock
&MBB
,
406 const MachineBasicBlock
&SuccBB
) {
407 for (MachineBasicBlock::const_succ_iterator SI
= MBB
.succ_begin(),
408 SE
= MBB
.succ_end(); SI
!= SE
; ++SI
)
410 for (const auto &LI
: (*SI
)->liveins())
411 Uses
.set(LI
.PhysReg
);
414 bool RegDefsUses::update(const MachineInstr
&MI
, unsigned Begin
, unsigned End
) {
415 BitVector
NewDefs(TRI
.getNumRegs()), NewUses(TRI
.getNumRegs());
416 bool HasHazard
= false;
418 for (unsigned I
= Begin
; I
!= End
; ++I
) {
419 const MachineOperand
&MO
= MI
.getOperand(I
);
421 if (MO
.isReg() && MO
.getReg())
422 HasHazard
|= checkRegDefsUses(NewDefs
, NewUses
, MO
.getReg(), MO
.isDef());
431 bool RegDefsUses::checkRegDefsUses(BitVector
&NewDefs
, BitVector
&NewUses
,
432 unsigned Reg
, bool IsDef
) const {
435 // check whether Reg has already been defined or used.
436 return (isRegInSet(Defs
, Reg
) || isRegInSet(Uses
, Reg
));
440 // check whether Reg has already been defined.
441 return isRegInSet(Defs
, Reg
);
444 bool RegDefsUses::isRegInSet(const BitVector
&RegSet
, unsigned Reg
) const {
445 // Check Reg and all aliased Registers.
446 for (MCRegAliasIterator
AI(Reg
, &TRI
, true); AI
.isValid(); ++AI
)
447 if (RegSet
.test(*AI
))
452 bool InspectMemInstr::hasHazard(const MachineInstr
&MI
) {
453 if (!MI
.mayStore() && !MI
.mayLoad())
459 OrigSeenLoad
= SeenLoad
;
460 OrigSeenStore
= SeenStore
;
461 SeenLoad
|= MI
.mayLoad();
462 SeenStore
|= MI
.mayStore();
464 // If MI is an ordered or volatile memory reference, disallow moving
465 // subsequent loads and stores to delay slot.
466 if (MI
.hasOrderedMemoryRef() && (OrigSeenLoad
|| OrigSeenStore
)) {
467 ForbidMemInstr
= true;
471 return hasHazard_(MI
);
474 bool LoadFromStackOrConst::hasHazard_(const MachineInstr
&MI
) {
478 if (!MI
.hasOneMemOperand() || !(*MI
.memoperands_begin())->getPseudoValue())
481 if (const PseudoSourceValue
*PSV
=
482 (*MI
.memoperands_begin())->getPseudoValue()) {
483 if (isa
<FixedStackPseudoSourceValue
>(PSV
))
485 return !PSV
->isConstant(nullptr) && !PSV
->isStack();
491 MemDefsUses::MemDefsUses(const DataLayout
&DL
, const MachineFrameInfo
*MFI_
)
492 : InspectMemInstr(false), MFI(MFI_
), DL(DL
) {}
494 bool MemDefsUses::hasHazard_(const MachineInstr
&MI
) {
495 bool HasHazard
= false;
496 SmallVector
<ValueType
, 4> Objs
;
498 // Check underlying object list.
499 if (getUnderlyingObjects(MI
, Objs
)) {
500 for (SmallVectorImpl
<ValueType
>::const_iterator I
= Objs
.begin();
501 I
!= Objs
.end(); ++I
)
502 HasHazard
|= updateDefsUses(*I
, MI
.mayStore());
507 // No underlying objects found.
508 HasHazard
= MI
.mayStore() && (OrigSeenLoad
|| OrigSeenStore
);
509 HasHazard
|= MI
.mayLoad() || OrigSeenStore
;
511 SeenNoObjLoad
|= MI
.mayLoad();
512 SeenNoObjStore
|= MI
.mayStore();
517 bool MemDefsUses::updateDefsUses(ValueType V
, bool MayStore
) {
519 return !Defs
.insert(V
).second
|| Uses
.count(V
) || SeenNoObjStore
||
523 return Defs
.count(V
) || SeenNoObjStore
;
527 getUnderlyingObjects(const MachineInstr
&MI
,
528 SmallVectorImpl
<ValueType
> &Objects
) const {
529 if (!MI
.hasOneMemOperand() ||
530 (!(*MI
.memoperands_begin())->getValue() &&
531 !(*MI
.memoperands_begin())->getPseudoValue()))
534 if (const PseudoSourceValue
*PSV
=
535 (*MI
.memoperands_begin())->getPseudoValue()) {
536 if (!PSV
->isAliased(MFI
))
538 Objects
.push_back(PSV
);
542 const Value
*V
= (*MI
.memoperands_begin())->getValue();
544 SmallVector
<Value
*, 4> Objs
;
545 GetUnderlyingObjects(const_cast<Value
*>(V
), Objs
, DL
);
547 for (SmallVectorImpl
<Value
*>::iterator I
= Objs
.begin(), E
= Objs
.end();
549 if (!isIdentifiedObject(V
))
552 Objects
.push_back(*I
);
558 // Replace Branch with the compact branch instruction.
559 Iter
MipsDelaySlotFiller::replaceWithCompactBranch(MachineBasicBlock
&MBB
,
561 const DebugLoc
&DL
) {
562 const MipsSubtarget
&STI
= MBB
.getParent()->getSubtarget
<MipsSubtarget
>();
563 const MipsInstrInfo
*TII
= STI
.getInstrInfo();
565 unsigned NewOpcode
= TII
->getEquivalentCompactForm(Branch
);
566 Branch
= TII
->genInstrWithNewOpc(NewOpcode
, Branch
);
568 std::next(Branch
)->eraseFromParent();
572 // For given opcode returns opcode of corresponding instruction with short
574 // For the pseudo TAILCALL*_MM instructions return the short delay slot
575 // form. Unfortunately, TAILCALL<->b16 is denied as b16 has a limited range
576 // that is too short to make use of for tail calls.
577 static int getEquivalentCallShort(int Opcode
) {
580 return Mips::BGEZALS_MM
;
582 return Mips::BLTZALS_MM
;
585 return Mips::JALS_MM
;
587 return Mips::JALRS_MM
;
588 case Mips::JALR16_MM
:
589 return Mips::JALRS16_MM
;
590 case Mips::TAILCALL_MM
:
591 llvm_unreachable("Attempting to shorten the TAILCALL_MM pseudo!");
592 case Mips::TAILCALLREG
:
593 return Mips::JR16_MM
;
595 llvm_unreachable("Unexpected call instruction for microMIPS.");
599 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
600 /// We assume there is only one delay slot per delayed instruction.
601 bool MipsDelaySlotFiller::runOnMachineBasicBlock(MachineBasicBlock
&MBB
) {
602 bool Changed
= false;
603 const MipsSubtarget
&STI
= MBB
.getParent()->getSubtarget
<MipsSubtarget
>();
604 bool InMicroMipsMode
= STI
.inMicroMipsMode();
605 const MipsInstrInfo
*TII
= STI
.getInstrInfo();
607 for (Iter I
= MBB
.begin(); I
!= MBB
.end(); ++I
) {
608 if (!hasUnoccupiedSlot(&*I
))
611 // Delay slot filling is disabled at -O0, or in microMIPS32R6.
612 if (!DisableDelaySlotFiller
&& (TM
->getOptLevel() != CodeGenOpt::None
) &&
613 !(InMicroMipsMode
&& STI
.hasMips32r6())) {
617 if (MipsCompactBranchPolicy
.getValue() != CB_Always
||
618 !TII
->getEquivalentCompactForm(I
)) {
619 if (searchBackward(MBB
, *I
)) {
621 } else if (I
->isTerminator()) {
622 if (searchSuccBBs(MBB
, I
)) {
625 } else if (searchForward(MBB
, I
)) {
631 // Get instruction with delay slot.
632 MachineBasicBlock::instr_iterator DSI
= I
.getInstrIterator();
634 if (InMicroMipsMode
&& TII
->getInstSizeInBytes(*std::next(DSI
)) == 2 &&
636 // If instruction in delay slot is 16b change opcode to
637 // corresponding instruction with short delay slot.
639 // TODO: Implement an instruction mapping table of 16bit opcodes to
640 // 32bit opcodes so that an instruction can be expanded. This would
641 // save 16 bits as a TAILCALL_MM pseudo requires a fullsized nop.
642 // TODO: Permit b16 when branching backwards to the same function
643 // if it is in range.
644 DSI
->setDesc(TII
->get(getEquivalentCallShort(DSI
->getOpcode())));
652 // For microMIPS if instruction is BEQ or BNE with one ZERO register, then
653 // instead of adding NOP replace this instruction with the corresponding
654 // compact branch instruction, i.e. BEQZC or BNEZC. Additionally
655 // PseudoReturn and PseudoIndirectBranch are expanded to JR_MM, so they can
656 // be replaced with JRC16_MM.
658 // For MIPSR6 attempt to produce the corresponding compact (no delay slot)
659 // form of the CTI. For indirect jumps this will not require inserting a
660 // NOP and for branches will hopefully avoid requiring a NOP.
661 if ((InMicroMipsMode
||
662 (STI
.hasMips32r6() && MipsCompactBranchPolicy
!= CB_Never
)) &&
663 TII
->getEquivalentCompactForm(I
)) {
664 I
= replaceWithCompactBranch(MBB
, I
, I
->getDebugLoc());
669 // Bundle the NOP to the instruction with the delay slot.
670 BuildMI(MBB
, std::next(I
), I
->getDebugLoc(), TII
->get(Mips::NOP
));
671 MIBundleBuilder(MBB
, I
, std::next(I
, 2));
679 template <typename IterTy
>
680 bool MipsDelaySlotFiller::searchRange(MachineBasicBlock
&MBB
, IterTy Begin
,
681 IterTy End
, RegDefsUses
&RegDU
,
682 InspectMemInstr
&IM
, Iter Slot
,
683 IterTy
&Filler
) const {
684 for (IterTy I
= Begin
; I
!= End
;) {
689 if (CurrI
->isDebugInstr())
692 if (terminateSearch(*CurrI
))
695 assert((!CurrI
->isCall() && !CurrI
->isReturn() && !CurrI
->isBranch()) &&
696 "Cannot put calls, returns or branches in delay slot.");
698 if (CurrI
->isKill()) {
699 CurrI
->eraseFromParent();
703 if (delayHasHazard(*CurrI
, RegDU
, IM
))
706 const MipsSubtarget
&STI
= MBB
.getParent()->getSubtarget
<MipsSubtarget
>();
707 if (STI
.isTargetNaCl()) {
708 // In NaCl, instructions that must be masked are forbidden in delay slots.
709 // We only check for loads, stores and SP changes. Calls, returns and
710 // branches are not checked because non-NaCl targets never put them in
713 if ((isBasePlusOffsetMemoryAccess(CurrI
->getOpcode(), &AddrIdx
) &&
714 baseRegNeedsLoadStoreMask(CurrI
->getOperand(AddrIdx
).getReg())) ||
715 CurrI
->modifiesRegister(Mips::SP
, STI
.getRegisterInfo()))
719 bool InMicroMipsMode
= STI
.inMicroMipsMode();
720 const MipsInstrInfo
*TII
= STI
.getInstrInfo();
721 unsigned Opcode
= (*Slot
).getOpcode();
722 // This is complicated by the tail call optimization. For non-PIC code
723 // there is only a 32bit sized unconditional branch which can be assumed
724 // to be able to reach the target. b16 only has a range of +/- 1 KB.
725 // It's entirely possible that the target function is reachable with b16
726 // but we don't have enough information to make that decision.
727 if (InMicroMipsMode
&& TII
->getInstSizeInBytes(*CurrI
) == 2 &&
728 (Opcode
== Mips::JR
|| Opcode
== Mips::PseudoIndirectBranch
||
729 Opcode
== Mips::PseudoReturn
|| Opcode
== Mips::TAILCALL
))
731 // Instructions LWP/SWP and MOVEP should not be in a delay slot as that
732 // results in unpredictable behaviour
733 if (InMicroMipsMode
&& (Opcode
== Mips::LWP_MM
|| Opcode
== Mips::SWP_MM
||
734 Opcode
== Mips::MOVEP_MM
))
744 bool MipsDelaySlotFiller::searchBackward(MachineBasicBlock
&MBB
,
745 MachineInstr
&Slot
) const {
746 if (DisableBackwardSearch
)
749 auto *Fn
= MBB
.getParent();
750 RegDefsUses
RegDU(*Fn
->getSubtarget().getRegisterInfo());
751 MemDefsUses
MemDU(Fn
->getDataLayout(), &Fn
->getFrameInfo());
756 MachineBasicBlock::iterator SlotI
= Slot
;
757 if (!searchRange(MBB
, ++SlotI
.getReverse(), MBB
.rend(), RegDU
, MemDU
, Slot
,
761 MBB
.splice(std::next(SlotI
), &MBB
, Filler
.getReverse());
762 MIBundleBuilder(MBB
, SlotI
, std::next(SlotI
, 2));
767 bool MipsDelaySlotFiller::searchForward(MachineBasicBlock
&MBB
,
769 // Can handle only calls.
770 if (DisableForwardSearch
|| !Slot
->isCall())
773 RegDefsUses
RegDU(*MBB
.getParent()->getSubtarget().getRegisterInfo());
777 RegDU
.setCallerSaved(*Slot
);
779 if (!searchRange(MBB
, std::next(Slot
), MBB
.end(), RegDU
, NM
, Slot
, Filler
))
782 MBB
.splice(std::next(Slot
), &MBB
, Filler
);
783 MIBundleBuilder(MBB
, Slot
, std::next(Slot
, 2));
788 bool MipsDelaySlotFiller::searchSuccBBs(MachineBasicBlock
&MBB
,
790 if (DisableSuccBBSearch
)
793 MachineBasicBlock
*SuccBB
= selectSuccBB(MBB
);
798 RegDefsUses
RegDU(*MBB
.getParent()->getSubtarget().getRegisterInfo());
799 bool HasMultipleSuccs
= false;
801 std::unique_ptr
<InspectMemInstr
> IM
;
803 auto *Fn
= MBB
.getParent();
805 // Iterate over SuccBB's predecessor list.
806 for (MachineBasicBlock::pred_iterator PI
= SuccBB
->pred_begin(),
807 PE
= SuccBB
->pred_end(); PI
!= PE
; ++PI
)
808 if (!examinePred(**PI
, *SuccBB
, RegDU
, HasMultipleSuccs
, BrMap
))
811 // Do not allow moving instructions which have unallocatable register operands
812 // across basic block boundaries.
813 RegDU
.setUnallocatableRegs(*Fn
);
815 // Only allow moving loads from stack or constants if any of the SuccBB's
816 // predecessors have multiple successors.
817 if (HasMultipleSuccs
) {
818 IM
.reset(new LoadFromStackOrConst());
820 const MachineFrameInfo
&MFI
= Fn
->getFrameInfo();
821 IM
.reset(new MemDefsUses(Fn
->getDataLayout(), &MFI
));
824 if (!searchRange(MBB
, SuccBB
->begin(), SuccBB
->end(), RegDU
, *IM
, Slot
,
828 insertDelayFiller(Filler
, BrMap
);
829 addLiveInRegs(Filler
, *SuccBB
);
830 Filler
->eraseFromParent();
836 MipsDelaySlotFiller::selectSuccBB(MachineBasicBlock
&B
) const {
840 // Select the successor with the larget edge weight.
841 auto &Prob
= getAnalysis
<MachineBranchProbabilityInfo
>();
842 MachineBasicBlock
*S
= *std::max_element(
843 B
.succ_begin(), B
.succ_end(),
844 [&](const MachineBasicBlock
*Dst0
, const MachineBasicBlock
*Dst1
) {
845 return Prob
.getEdgeProbability(&B
, Dst0
) <
846 Prob
.getEdgeProbability(&B
, Dst1
);
848 return S
->isEHPad() ? nullptr : S
;
851 std::pair
<MipsInstrInfo::BranchType
, MachineInstr
*>
852 MipsDelaySlotFiller::getBranch(MachineBasicBlock
&MBB
,
853 const MachineBasicBlock
&Dst
) const {
854 const MipsInstrInfo
*TII
=
855 MBB
.getParent()->getSubtarget
<MipsSubtarget
>().getInstrInfo();
856 MachineBasicBlock
*TrueBB
= nullptr, *FalseBB
= nullptr;
857 SmallVector
<MachineInstr
*, 2> BranchInstrs
;
858 SmallVector
<MachineOperand
, 2> Cond
;
860 MipsInstrInfo::BranchType R
=
861 TII
->analyzeBranch(MBB
, TrueBB
, FalseBB
, Cond
, false, BranchInstrs
);
863 if ((R
== MipsInstrInfo::BT_None
) || (R
== MipsInstrInfo::BT_NoBranch
))
864 return std::make_pair(R
, nullptr);
866 if (R
!= MipsInstrInfo::BT_CondUncond
) {
867 if (!hasUnoccupiedSlot(BranchInstrs
[0]))
868 return std::make_pair(MipsInstrInfo::BT_None
, nullptr);
870 assert(((R
!= MipsInstrInfo::BT_Uncond
) || (TrueBB
== &Dst
)));
872 return std::make_pair(R
, BranchInstrs
[0]);
875 assert((TrueBB
== &Dst
) || (FalseBB
== &Dst
));
877 // Examine the conditional branch. See if its slot is occupied.
878 if (hasUnoccupiedSlot(BranchInstrs
[0]))
879 return std::make_pair(MipsInstrInfo::BT_Cond
, BranchInstrs
[0]);
881 // If that fails, try the unconditional branch.
882 if (hasUnoccupiedSlot(BranchInstrs
[1]) && (FalseBB
== &Dst
))
883 return std::make_pair(MipsInstrInfo::BT_Uncond
, BranchInstrs
[1]);
885 return std::make_pair(MipsInstrInfo::BT_None
, nullptr);
888 bool MipsDelaySlotFiller::examinePred(MachineBasicBlock
&Pred
,
889 const MachineBasicBlock
&Succ
,
891 bool &HasMultipleSuccs
,
892 BB2BrMap
&BrMap
) const {
893 std::pair
<MipsInstrInfo::BranchType
, MachineInstr
*> P
=
894 getBranch(Pred
, Succ
);
896 // Return if either getBranch wasn't able to analyze the branches or there
897 // were no branches with unoccupied slots.
898 if (P
.first
== MipsInstrInfo::BT_None
)
901 if ((P
.first
!= MipsInstrInfo::BT_Uncond
) &&
902 (P
.first
!= MipsInstrInfo::BT_NoBranch
)) {
903 HasMultipleSuccs
= true;
904 RegDU
.addLiveOut(Pred
, Succ
);
907 BrMap
[&Pred
] = P
.second
;
911 bool MipsDelaySlotFiller::delayHasHazard(const MachineInstr
&Candidate
,
913 InspectMemInstr
&IM
) const {
914 assert(!Candidate
.isKill() &&
915 "KILL instructions should have been eliminated at this point.");
917 bool HasHazard
= Candidate
.isImplicitDef();
919 HasHazard
|= IM
.hasHazard(Candidate
);
920 HasHazard
|= RegDU
.update(Candidate
, 0, Candidate
.getNumOperands());
925 bool MipsDelaySlotFiller::terminateSearch(const MachineInstr
&Candidate
) const {
926 return (Candidate
.isTerminator() || Candidate
.isCall() ||
927 Candidate
.isPosition() || Candidate
.isInlineAsm() ||
928 Candidate
.hasUnmodeledSideEffects());
931 /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
932 /// slots in Mips MachineFunctions
933 FunctionPass
*llvm::createMipsDelaySlotFillerPass() { return new MipsDelaySlotFiller(); }