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
;
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 (MachineFunction::iterator FI
= F
.begin(), FE
= F
.end();
58 Changed
|= runOnMachineBasicBlock(*FI
);
62 MachineFunctionProperties
getRequiredProperties() const override
{
63 return MachineFunctionProperties().set(
64 MachineFunctionProperties::Property::NoVRegs
);
67 void insertCallDefsUses(MachineBasicBlock::iterator MI
,
68 SmallSet
<unsigned, 32>& RegDefs
,
69 SmallSet
<unsigned, 32>& RegUses
);
71 void insertDefsUses(MachineBasicBlock::iterator MI
,
72 SmallSet
<unsigned, 32>& RegDefs
,
73 SmallSet
<unsigned, 32>& RegUses
);
75 bool IsRegInSet(SmallSet
<unsigned, 32>& RegSet
,
78 bool delayHasHazard(MachineBasicBlock::iterator candidate
,
79 bool &sawLoad
, bool &sawStore
,
80 SmallSet
<unsigned, 32> &RegDefs
,
81 SmallSet
<unsigned, 32> &RegUses
);
83 MachineBasicBlock::iterator
84 findDelayInstr(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator slot
);
86 bool needsUnimp(MachineBasicBlock::iterator I
, unsigned &StructSize
);
88 bool tryCombineRestoreWithPrevInst(MachineBasicBlock
&MBB
,
89 MachineBasicBlock::iterator MBBI
);
93 } // end of anonymous namespace
95 /// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
96 /// slots in Sparc MachineFunctions
98 FunctionPass
*llvm::createSparcDelaySlotFillerPass() {
103 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
104 /// We assume there is only one delay slot per delayed instruction.
106 bool Filler::runOnMachineBasicBlock(MachineBasicBlock
&MBB
) {
107 bool Changed
= false;
108 Subtarget
= &MBB
.getParent()->getSubtarget
<SparcSubtarget
>();
109 const TargetInstrInfo
*TII
= Subtarget
->getInstrInfo();
111 for (MachineBasicBlock::iterator I
= MBB
.begin(); I
!= MBB
.end(); ) {
112 MachineBasicBlock::iterator MI
= I
;
115 // If MI is restore, try combining it with previous inst.
116 if (!DisableDelaySlotFiller
&&
117 (MI
->getOpcode() == SP::RESTORErr
118 || MI
->getOpcode() == SP::RESTOREri
)) {
119 Changed
|= tryCombineRestoreWithPrevInst(MBB
, MI
);
123 // TODO: If we ever want to support v7, this needs to be extended
124 // to cover all floating point operations.
125 if (!Subtarget
->isV9() &&
126 (MI
->getOpcode() == SP::FCMPS
|| MI
->getOpcode() == SP::FCMPD
127 || MI
->getOpcode() == SP::FCMPQ
)) {
128 BuildMI(MBB
, I
, MI
->getDebugLoc(), TII
->get(SP::NOP
));
133 // If MI has no delay slot, skip.
134 if (!MI
->hasDelaySlot())
137 MachineBasicBlock::iterator D
= MBB
.end();
139 if (!DisableDelaySlotFiller
)
140 D
= findDelayInstr(MBB
, MI
);
146 BuildMI(MBB
, I
, MI
->getDebugLoc(), TII
->get(SP::NOP
));
148 MBB
.splice(I
, &MBB
, D
);
150 unsigned structSize
= 0;
151 if (needsUnimp(MI
, structSize
)) {
152 MachineBasicBlock::iterator J
= MI
;
153 ++J
; // skip the delay filler.
154 assert (J
!= MBB
.end() && "MI needs a delay instruction.");
155 BuildMI(MBB
, ++J
, MI
->getDebugLoc(),
156 TII
->get(SP::UNIMP
)).addImm(structSize
);
157 // Bundle the delay filler and unimp with the instruction.
158 MIBundleBuilder(MBB
, MachineBasicBlock::iterator(MI
), J
);
160 MIBundleBuilder(MBB
, MachineBasicBlock::iterator(MI
), I
);
166 MachineBasicBlock::iterator
167 Filler::findDelayInstr(MachineBasicBlock
&MBB
,
168 MachineBasicBlock::iterator slot
)
170 SmallSet
<unsigned, 32> RegDefs
;
171 SmallSet
<unsigned, 32> RegUses
;
172 bool sawLoad
= false;
173 bool sawStore
= false;
175 if (slot
== MBB
.begin())
178 if (slot
->getOpcode() == SP::RET
|| slot
->getOpcode() == SP::TLS_CALL
)
181 if (slot
->getOpcode() == SP::RETL
) {
182 MachineBasicBlock::iterator J
= slot
;
185 if (J
->getOpcode() == SP::RESTORErr
186 || J
->getOpcode() == SP::RESTOREri
) {
187 // change retl to ret.
188 slot
->setDesc(Subtarget
->getInstrInfo()->get(SP::RET
));
193 // Call's delay filler can def some of call's uses.
195 insertCallDefsUses(slot
, RegDefs
, RegUses
);
197 insertDefsUses(slot
, RegDefs
, RegUses
);
201 MachineBasicBlock::iterator I
= slot
;
204 done
= (I
== MBB
.begin());
209 // skip debug instruction
210 if (I
->isDebugInstr())
213 if (I
->hasUnmodeledSideEffects() || I
->isInlineAsm() || I
->isPosition() ||
214 I
->hasDelaySlot() || I
->isBundledWithSucc())
217 if (delayHasHazard(I
, sawLoad
, sawStore
, RegDefs
, RegUses
)) {
218 insertDefsUses(I
, RegDefs
, RegUses
);
227 bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate
,
230 SmallSet
<unsigned, 32> &RegDefs
,
231 SmallSet
<unsigned, 32> &RegUses
)
234 if (candidate
->isImplicitDef() || candidate
->isKill())
237 if (candidate
->mayLoad()) {
243 if (candidate
->mayStore()) {
251 for (unsigned i
= 0, e
= candidate
->getNumOperands(); i
!= e
; ++i
) {
252 const MachineOperand
&MO
= candidate
->getOperand(i
);
256 Register Reg
= MO
.getReg();
259 // check whether Reg is defined or used before delay slot.
260 if (IsRegInSet(RegDefs
, Reg
) || IsRegInSet(RegUses
, Reg
))
264 // check whether Reg is defined before delay slot.
265 if (IsRegInSet(RegDefs
, Reg
))
270 unsigned Opcode
= candidate
->getOpcode();
271 // LD and LDD may have NOPs inserted afterwards in the case of some LEON
272 // processors, so we can't use the delay slot if this feature is switched-on.
273 if (Subtarget
->insertNOPLoad()
275 Opcode
>= SP::LDDArr
&& Opcode
<= SP::LDrr
)
278 // Same as above for FDIV and FSQRT on some LEON processors.
279 if (Subtarget
->fixAllFDIVSQRT()
281 Opcode
>= SP::FDIVD
&& Opcode
<= SP::FSQRTD
)
289 void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI
,
290 SmallSet
<unsigned, 32>& RegDefs
,
291 SmallSet
<unsigned, 32>& RegUses
)
293 // Call defines o7, which is visible to the instruction in delay slot.
294 RegDefs
.insert(SP::O7
);
296 switch(MI
->getOpcode()) {
297 default: llvm_unreachable("Unknown opcode.");
298 case SP::CALL
: break;
301 assert(MI
->getNumOperands() >= 2);
302 const MachineOperand
&Reg
= MI
->getOperand(0);
303 assert(Reg
.isReg() && "CALL first operand is not a register.");
304 assert(Reg
.isUse() && "CALL first operand is not a use.");
305 RegUses
.insert(Reg
.getReg());
307 const MachineOperand
&Operand1
= MI
->getOperand(1);
308 if (Operand1
.isImm() || Operand1
.isGlobal())
310 assert(Operand1
.isReg() && "CALLrr second operand is not a register.");
311 assert(Operand1
.isUse() && "CALLrr second operand is not a use.");
312 RegUses
.insert(Operand1
.getReg());
317 // Insert Defs and Uses of MI into the sets RegDefs and RegUses.
318 void Filler::insertDefsUses(MachineBasicBlock::iterator MI
,
319 SmallSet
<unsigned, 32>& RegDefs
,
320 SmallSet
<unsigned, 32>& RegUses
)
322 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
323 const MachineOperand
&MO
= MI
->getOperand(i
);
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;
367 const MachineOperand
&MO
= I
->getOperand(structSizeOpNum
);
370 StructSize
= MO
.getImm();
374 static bool combineRestoreADD(MachineBasicBlock::iterator RestoreMI
,
375 MachineBasicBlock::iterator AddMI
,
376 const TargetInstrInfo
*TII
)
378 // Before: add <op0>, <op1>, %i[0-7]
379 // restore %g0, %g0, %i[0-7]
381 // After : restore <op0>, <op1>, %o[0-7]
383 Register reg
= AddMI
->getOperand(0).getReg();
384 if (reg
< SP::I0
|| reg
> SP::I7
)
388 RestoreMI
->eraseFromParent();
390 // Change ADD to RESTORE.
391 AddMI
->setDesc(TII
->get((AddMI
->getOpcode() == SP::ADDrr
)
395 // Map the destination register.
396 AddMI
->getOperand(0).setReg(reg
- SP::I0
+ SP::O0
);
401 static bool combineRestoreOR(MachineBasicBlock::iterator RestoreMI
,
402 MachineBasicBlock::iterator OrMI
,
403 const TargetInstrInfo
*TII
)
405 // Before: or <op0>, <op1>, %i[0-7]
406 // restore %g0, %g0, %i[0-7]
407 // and <op0> or <op1> is zero,
409 // After : restore <op0>, <op1>, %o[0-7]
411 Register reg
= OrMI
->getOperand(0).getReg();
412 if (reg
< SP::I0
|| reg
> SP::I7
)
415 // check whether it is a copy.
416 if (OrMI
->getOpcode() == SP::ORrr
417 && OrMI
->getOperand(1).getReg() != SP::G0
418 && OrMI
->getOperand(2).getReg() != SP::G0
)
421 if (OrMI
->getOpcode() == SP::ORri
422 && OrMI
->getOperand(1).getReg() != SP::G0
423 && (!OrMI
->getOperand(2).isImm() || OrMI
->getOperand(2).getImm() != 0))
427 RestoreMI
->eraseFromParent();
429 // Change OR to RESTORE.
430 OrMI
->setDesc(TII
->get((OrMI
->getOpcode() == SP::ORrr
)
434 // Map the destination register.
435 OrMI
->getOperand(0).setReg(reg
- SP::I0
+ SP::O0
);
440 static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI
,
441 MachineBasicBlock::iterator SetHiMI
,
442 const TargetInstrInfo
*TII
)
444 // Before: sethi imm3, %i[0-7]
445 // restore %g0, %g0, %g0
447 // After : restore %g0, (imm3<<10), %o[0-7]
449 Register reg
= SetHiMI
->getOperand(0).getReg();
450 if (reg
< SP::I0
|| reg
> SP::I7
)
453 if (!SetHiMI
->getOperand(1).isImm())
456 int64_t imm
= SetHiMI
->getOperand(1).getImm();
458 // Is it a 3 bit immediate?
462 // Make it a 13 bit immediate.
463 imm
= (imm
<< 10) & 0x1FFF;
465 assert(RestoreMI
->getOpcode() == SP::RESTORErr
);
467 RestoreMI
->setDesc(TII
->get(SP::RESTOREri
));
469 RestoreMI
->getOperand(0).setReg(reg
- SP::I0
+ SP::O0
);
470 RestoreMI
->getOperand(1).setReg(SP::G0
);
471 RestoreMI
->getOperand(2).ChangeToImmediate(imm
);
474 // Erase the original SETHI.
475 SetHiMI
->eraseFromParent();
480 bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock
&MBB
,
481 MachineBasicBlock::iterator MBBI
)
483 // No previous instruction.
484 if (MBBI
== MBB
.begin())
487 // assert that MBBI is a "restore %g0, %g0, %g0".
488 assert(MBBI
->getOpcode() == SP::RESTORErr
489 && MBBI
->getOperand(0).getReg() == SP::G0
490 && MBBI
->getOperand(1).getReg() == SP::G0
491 && MBBI
->getOperand(2).getReg() == SP::G0
);
493 MachineBasicBlock::iterator PrevInst
= std::prev(MBBI
);
495 // It cannot be combined with a bundled instruction.
496 if (PrevInst
->isBundledWithSucc())
499 const TargetInstrInfo
*TII
= Subtarget
->getInstrInfo();
501 switch (PrevInst
->getOpcode()) {
504 case SP::ADDri
: return combineRestoreADD(MBBI
, PrevInst
, TII
); break;
506 case SP::ORri
: return combineRestoreOR(MBBI
, PrevInst
, TII
); break;
507 case SP::SETHIi
: return combineRestoreSETHIi(MBBI
, PrevInst
, TII
); break;
509 // It cannot combine with the previous instruction.