1 //===-- DelaySlotFiller.cpp - SPARC delay slot filler ---------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This is a simple local pass that attempts to fill delay slots with useful
10 // instructions. If no instructions can be moved into the delay slot, then a
12 //===----------------------------------------------------------------------===//
15 #include "SparcSubtarget.h"
16 #include "llvm/ADT/SmallSet.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/TargetInstrInfo.h"
22 #include "llvm/CodeGen/TargetRegisterInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Target/TargetMachine.h"
28 #define DEBUG_TYPE "delay-slot-filler"
30 STATISTIC(FilledSlots
, "Number of delay slots filled");
32 static cl::opt
<bool> DisableDelaySlotFiller(
33 "disable-sparc-delay-filler",
35 cl::desc("Disable the Sparc delay slot filler."),
39 struct Filler
: public MachineFunctionPass
{
40 const SparcSubtarget
*Subtarget
= nullptr;
43 Filler() : MachineFunctionPass(ID
) {}
45 StringRef
getPassName() const override
{ return "SPARC Delay Slot Filler"; }
47 bool runOnMachineBasicBlock(MachineBasicBlock
&MBB
);
48 bool runOnMachineFunction(MachineFunction
&F
) override
{
50 Subtarget
= &F
.getSubtarget
<SparcSubtarget
>();
52 // This pass invalidates liveness information when it reorders
53 // instructions to fill delay slot.
54 F
.getRegInfo().invalidateLiveness();
56 for (MachineBasicBlock
&MBB
: F
)
57 Changed
|= runOnMachineBasicBlock(MBB
);
61 MachineFunctionProperties
getRequiredProperties() const override
{
62 return MachineFunctionProperties().set(
63 MachineFunctionProperties::Property::NoVRegs
);
66 void insertCallDefsUses(MachineBasicBlock::iterator MI
,
67 SmallSet
<unsigned, 32>& RegDefs
,
68 SmallSet
<unsigned, 32>& RegUses
);
70 void insertDefsUses(MachineBasicBlock::iterator MI
,
71 SmallSet
<unsigned, 32>& RegDefs
,
72 SmallSet
<unsigned, 32>& RegUses
);
74 bool IsRegInSet(SmallSet
<unsigned, 32>& RegSet
,
77 bool delayHasHazard(MachineBasicBlock::iterator candidate
,
78 bool &sawLoad
, bool &sawStore
,
79 SmallSet
<unsigned, 32> &RegDefs
,
80 SmallSet
<unsigned, 32> &RegUses
);
82 MachineBasicBlock::iterator
83 findDelayInstr(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator slot
);
85 bool needsUnimp(MachineBasicBlock::iterator I
, unsigned &StructSize
);
87 bool tryCombineRestoreWithPrevInst(MachineBasicBlock
&MBB
,
88 MachineBasicBlock::iterator MBBI
);
92 } // end of anonymous namespace
94 /// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
95 /// slots in Sparc MachineFunctions
97 FunctionPass
*llvm::createSparcDelaySlotFillerPass() {
102 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
103 /// We assume there is only one delay slot per delayed instruction.
105 bool Filler::runOnMachineBasicBlock(MachineBasicBlock
&MBB
) {
106 bool Changed
= false;
107 Subtarget
= &MBB
.getParent()->getSubtarget
<SparcSubtarget
>();
108 const TargetInstrInfo
*TII
= Subtarget
->getInstrInfo();
110 for (MachineBasicBlock::iterator I
= MBB
.begin(); I
!= MBB
.end(); ) {
111 MachineBasicBlock::iterator MI
= I
;
114 // If MI is restore, try combining it with previous inst.
115 if (!DisableDelaySlotFiller
&&
116 (MI
->getOpcode() == SP::RESTORErr
117 || MI
->getOpcode() == SP::RESTOREri
)) {
118 Changed
|= tryCombineRestoreWithPrevInst(MBB
, MI
);
122 // TODO: If we ever want to support v7, this needs to be extended
123 // to cover all floating point operations.
124 if (!Subtarget
->isV9() &&
125 (MI
->getOpcode() == SP::FCMPS
|| MI
->getOpcode() == SP::FCMPD
126 || MI
->getOpcode() == SP::FCMPQ
)) {
127 BuildMI(MBB
, I
, MI
->getDebugLoc(), TII
->get(SP::NOP
));
132 // If MI has no delay slot, skip.
133 if (!MI
->hasDelaySlot())
136 MachineBasicBlock::iterator D
= MBB
.end();
138 if (!DisableDelaySlotFiller
)
139 D
= findDelayInstr(MBB
, MI
);
145 BuildMI(MBB
, I
, MI
->getDebugLoc(), TII
->get(SP::NOP
));
147 MBB
.splice(I
, &MBB
, D
);
149 unsigned structSize
= 0;
150 if (needsUnimp(MI
, structSize
)) {
151 MachineBasicBlock::iterator J
= MI
;
152 ++J
; // skip the delay filler.
153 assert (J
!= MBB
.end() && "MI needs a delay instruction.");
154 BuildMI(MBB
, ++J
, MI
->getDebugLoc(),
155 TII
->get(SP::UNIMP
)).addImm(structSize
);
156 // Bundle the delay filler and unimp with the instruction.
157 MIBundleBuilder(MBB
, MachineBasicBlock::iterator(MI
), J
);
159 MIBundleBuilder(MBB
, MachineBasicBlock::iterator(MI
), I
);
165 MachineBasicBlock::iterator
166 Filler::findDelayInstr(MachineBasicBlock
&MBB
,
167 MachineBasicBlock::iterator slot
)
169 SmallSet
<unsigned, 32> RegDefs
;
170 SmallSet
<unsigned, 32> RegUses
;
171 bool sawLoad
= false;
172 bool sawStore
= false;
174 if (slot
== MBB
.begin())
177 unsigned Opc
= slot
->getOpcode();
179 if (Opc
== SP::RET
|| Opc
== SP::TLS_CALL
)
182 if (Opc
== SP::RETL
|| Opc
== SP::TAIL_CALL
|| Opc
== SP::TAIL_CALLri
) {
183 MachineBasicBlock::iterator J
= slot
;
186 if (J
->getOpcode() == SP::RESTORErr
187 || J
->getOpcode() == SP::RESTOREri
) {
188 // change retl to ret.
190 slot
->setDesc(Subtarget
->getInstrInfo()->get(SP::RET
));
195 // Call's delay filler can def some of call's uses.
197 insertCallDefsUses(slot
, RegDefs
, RegUses
);
199 insertDefsUses(slot
, RegDefs
, RegUses
);
203 MachineBasicBlock::iterator I
= slot
;
206 done
= (I
== MBB
.begin());
211 // skip debug instruction
212 if (I
->isDebugInstr())
215 if (I
->hasUnmodeledSideEffects() || I
->isInlineAsm() || I
->isPosition() ||
216 I
->hasDelaySlot() || I
->isBundledWithSucc())
219 if (delayHasHazard(I
, sawLoad
, sawStore
, RegDefs
, RegUses
)) {
220 insertDefsUses(I
, RegDefs
, RegUses
);
229 bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate
,
232 SmallSet
<unsigned, 32> &RegDefs
,
233 SmallSet
<unsigned, 32> &RegUses
)
236 if (candidate
->isImplicitDef() || candidate
->isKill())
239 if (candidate
->mayLoad()) {
245 if (candidate
->mayStore()) {
253 for (const MachineOperand
&MO
: candidate
->operands()) {
257 Register Reg
= MO
.getReg();
260 // check whether Reg is defined or used before delay slot.
261 if (IsRegInSet(RegDefs
, Reg
) || IsRegInSet(RegUses
, Reg
))
265 // check whether Reg is defined before delay slot.
266 if (IsRegInSet(RegDefs
, Reg
))
271 unsigned Opcode
= candidate
->getOpcode();
272 // LD and LDD may have NOPs inserted afterwards in the case of some LEON
273 // processors, so we can't use the delay slot if this feature is switched-on.
274 if (Subtarget
->insertNOPLoad()
276 Opcode
>= SP::LDDArr
&& Opcode
<= SP::LDrr
)
279 // Same as above for FDIV and FSQRT on some LEON processors.
280 if (Subtarget
->fixAllFDIVSQRT()
282 Opcode
>= SP::FDIVD
&& Opcode
<= SP::FSQRTD
)
290 void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI
,
291 SmallSet
<unsigned, 32>& RegDefs
,
292 SmallSet
<unsigned, 32>& RegUses
)
294 // Call defines o7, which is visible to the instruction in delay slot.
295 RegDefs
.insert(SP::O7
);
297 switch(MI
->getOpcode()) {
298 default: llvm_unreachable("Unknown opcode.");
299 case SP::CALL
: break;
302 assert(MI
->getNumOperands() >= 2);
303 const MachineOperand
&Reg
= MI
->getOperand(0);
304 assert(Reg
.isReg() && "CALL first operand is not a register.");
305 assert(Reg
.isUse() && "CALL first operand is not a use.");
306 RegUses
.insert(Reg
.getReg());
308 const MachineOperand
&Operand1
= MI
->getOperand(1);
309 if (Operand1
.isImm() || Operand1
.isGlobal())
311 assert(Operand1
.isReg() && "CALLrr second operand is not a register.");
312 assert(Operand1
.isUse() && "CALLrr second operand is not a use.");
313 RegUses
.insert(Operand1
.getReg());
318 // Insert Defs and Uses of MI into the sets RegDefs and RegUses.
319 void Filler::insertDefsUses(MachineBasicBlock::iterator MI
,
320 SmallSet
<unsigned, 32>& RegDefs
,
321 SmallSet
<unsigned, 32>& RegUses
)
323 for (const MachineOperand
&MO
: MI
->operands()) {
327 Register Reg
= MO
.getReg();
333 // Implicit register uses of retl are return values and
334 // retl does not use them.
335 if (MO
.isImplicit() && MI
->getOpcode() == SP::RETL
)
342 // returns true if the Reg or its alias is in the RegSet.
343 bool Filler::IsRegInSet(SmallSet
<unsigned, 32>& RegSet
, unsigned Reg
)
345 // Check Reg and all aliased Registers.
346 for (MCRegAliasIterator
AI(Reg
, Subtarget
->getRegisterInfo(), true);
348 if (RegSet
.count(*AI
))
353 bool Filler::needsUnimp(MachineBasicBlock::iterator I
, unsigned &StructSize
)
358 unsigned structSizeOpNum
= 0;
359 switch (I
->getOpcode()) {
360 default: llvm_unreachable("Unknown call opcode.");
361 case SP::CALL
: structSizeOpNum
= 1; break;
363 case SP::CALLri
: structSizeOpNum
= 2; break;
364 case SP::TLS_CALL
: return false;
365 case SP::TAIL_CALLri
:
366 case SP::TAIL_CALL
: return false;
369 const MachineOperand
&MO
= I
->getOperand(structSizeOpNum
);
372 StructSize
= MO
.getImm();
376 static bool combineRestoreADD(MachineBasicBlock::iterator RestoreMI
,
377 MachineBasicBlock::iterator AddMI
,
378 const TargetInstrInfo
*TII
)
380 // Before: add <op0>, <op1>, %i[0-7]
381 // restore %g0, %g0, %i[0-7]
383 // After : restore <op0>, <op1>, %o[0-7]
385 Register reg
= AddMI
->getOperand(0).getReg();
386 if (reg
< SP::I0
|| reg
> SP::I7
)
390 RestoreMI
->eraseFromParent();
392 // Change ADD to RESTORE.
393 AddMI
->setDesc(TII
->get((AddMI
->getOpcode() == SP::ADDrr
)
397 // Map the destination register.
398 AddMI
->getOperand(0).setReg(reg
- SP::I0
+ SP::O0
);
403 static bool combineRestoreOR(MachineBasicBlock::iterator RestoreMI
,
404 MachineBasicBlock::iterator OrMI
,
405 const TargetInstrInfo
*TII
)
407 // Before: or <op0>, <op1>, %i[0-7]
408 // restore %g0, %g0, %i[0-7]
409 // and <op0> or <op1> is zero,
411 // After : restore <op0>, <op1>, %o[0-7]
413 Register reg
= OrMI
->getOperand(0).getReg();
414 if (reg
< SP::I0
|| reg
> SP::I7
)
417 // check whether it is a copy.
418 if (OrMI
->getOpcode() == SP::ORrr
419 && OrMI
->getOperand(1).getReg() != SP::G0
420 && OrMI
->getOperand(2).getReg() != SP::G0
)
423 if (OrMI
->getOpcode() == SP::ORri
424 && OrMI
->getOperand(1).getReg() != SP::G0
425 && (!OrMI
->getOperand(2).isImm() || OrMI
->getOperand(2).getImm() != 0))
429 RestoreMI
->eraseFromParent();
431 // Change OR to RESTORE.
432 OrMI
->setDesc(TII
->get((OrMI
->getOpcode() == SP::ORrr
)
436 // Map the destination register.
437 OrMI
->getOperand(0).setReg(reg
- SP::I0
+ SP::O0
);
442 static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI
,
443 MachineBasicBlock::iterator SetHiMI
,
444 const TargetInstrInfo
*TII
)
446 // Before: sethi imm3, %i[0-7]
447 // restore %g0, %g0, %g0
449 // After : restore %g0, (imm3<<10), %o[0-7]
451 Register reg
= SetHiMI
->getOperand(0).getReg();
452 if (reg
< SP::I0
|| reg
> SP::I7
)
455 if (!SetHiMI
->getOperand(1).isImm())
458 int64_t imm
= SetHiMI
->getOperand(1).getImm();
460 // Is it a 3 bit immediate?
464 // Make it a 13 bit immediate.
465 imm
= (imm
<< 10) & 0x1FFF;
467 assert(RestoreMI
->getOpcode() == SP::RESTORErr
);
469 RestoreMI
->setDesc(TII
->get(SP::RESTOREri
));
471 RestoreMI
->getOperand(0).setReg(reg
- SP::I0
+ SP::O0
);
472 RestoreMI
->getOperand(1).setReg(SP::G0
);
473 RestoreMI
->getOperand(2).ChangeToImmediate(imm
);
476 // Erase the original SETHI.
477 SetHiMI
->eraseFromParent();
482 bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock
&MBB
,
483 MachineBasicBlock::iterator MBBI
)
485 // No previous instruction.
486 if (MBBI
== MBB
.begin())
489 // assert that MBBI is a "restore %g0, %g0, %g0".
490 assert(MBBI
->getOpcode() == SP::RESTORErr
491 && MBBI
->getOperand(0).getReg() == SP::G0
492 && MBBI
->getOperand(1).getReg() == SP::G0
493 && MBBI
->getOperand(2).getReg() == SP::G0
);
495 MachineBasicBlock::iterator PrevInst
= std::prev(MBBI
);
497 // It cannot be combined with a bundled instruction.
498 if (PrevInst
->isBundledWithSucc())
501 const TargetInstrInfo
*TII
= Subtarget
->getInstrInfo();
503 switch (PrevInst
->getOpcode()) {
506 case SP::ADDri
: return combineRestoreADD(MBBI
, PrevInst
, TII
); break;
508 case SP::ORri
: return combineRestoreOR(MBBI
, PrevInst
, TII
); break;
509 case SP::SETHIi
: return combineRestoreSETHIi(MBBI
, PrevInst
, TII
); break;
511 // It cannot combine with the previous instruction.