LLVM 23.0.0git
DelaySlotFiller.cpp
Go to the documentation of this file.
1//===-- DelaySlotFiller.cpp - SPARC delay slot filler ---------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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
11// NOP is placed.
12//===----------------------------------------------------------------------===//
13
14#include "Sparc.h"
15#include "SparcSubtarget.h"
16#include "llvm/ADT/SmallSet.h"
17#include "llvm/ADT/Statistic.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "delay-slot-filler"
28
29STATISTIC(FilledSlots, "Number of delay slots filled");
30
32 "disable-sparc-delay-filler",
33 cl::init(false),
34 cl::desc("Disable the Sparc delay slot filler."),
36
37namespace {
38 struct Filler : public MachineFunctionPass {
39 const SparcSubtarget *Subtarget = nullptr;
40
41 static char ID;
42 Filler() : MachineFunctionPass(ID) {}
43
44 StringRef getPassName() const override { return "SPARC Delay Slot Filler"; }
45
46 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
47 bool runOnMachineFunction(MachineFunction &F) override {
48 bool Changed = false;
49 Subtarget = &F.getSubtarget<SparcSubtarget>();
50
51 // This pass invalidates liveness information when it reorders
52 // instructions to fill delay slot.
53 F.getRegInfo().invalidateLiveness();
54
55 for (MachineBasicBlock &MBB : F)
56 Changed |= runOnMachineBasicBlock(MBB);
57 return Changed;
58 }
59
60 MachineFunctionProperties getRequiredProperties() const override {
61 return MachineFunctionProperties().setNoVRegs();
62 }
63
64 void insertCallDefsUses(MachineBasicBlock::iterator MI,
65 SmallSet<unsigned, 32>& RegDefs,
66 SmallSet<unsigned, 32>& RegUses);
67
68 void insertDefsUses(MachineBasicBlock::iterator MI,
69 SmallSet<unsigned, 32>& RegDefs,
70 SmallSet<unsigned, 32>& RegUses);
71
72 bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
73 unsigned Reg);
74
75 bool delayHasHazard(MachineBasicBlock::iterator candidate,
76 bool &sawLoad, bool &sawStore,
77 SmallSet<unsigned, 32> &RegDefs,
78 SmallSet<unsigned, 32> &RegUses);
79
81 findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot);
82
83 bool needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize);
84
85 bool tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
87
88 };
89 char Filler::ID = 0;
90} // end of anonymous namespace
91
92/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
93/// slots in Sparc MachineFunctions
94///
98
99
100/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
101/// We assume there is only one delay slot per delayed instruction.
102///
103bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
104 bool Changed = false;
105 Subtarget = &MBB.getParent()->getSubtarget<SparcSubtarget>();
106 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
107
108 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
110 ++I;
111
112 // If MI is restore, try combining it with previous inst.
114 (MI->getOpcode() == SP::RESTORErr
115 || MI->getOpcode() == SP::RESTOREri)) {
116 Changed |= tryCombineRestoreWithPrevInst(MBB, MI);
117 continue;
118 }
119
120 // TODO: If we ever want to support v7, this needs to be extended
121 // to cover all floating point operations.
122 if (!Subtarget->isV9() &&
123 (MI->getOpcode() == SP::FCMPS || MI->getOpcode() == SP::FCMPD
124 || MI->getOpcode() == SP::FCMPQ)) {
125 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
126 Changed = true;
127 continue;
128 }
129
130 // If MI has no delay slot, skip.
131 if (!MI->hasDelaySlot())
132 continue;
133
135
137 D = findDelayInstr(MBB, MI);
138
139 ++FilledSlots;
140 Changed = true;
141
142 if (D == MBB.end())
143 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
144 else
145 MBB.splice(I, &MBB, D);
146
147 unsigned structSize = 0;
148 if (needsUnimp(MI, structSize)) {
150 ++J; // skip the delay filler.
151 assert (J != MBB.end() && "MI needs a delay instruction.");
152 BuildMI(MBB, ++J, MI->getDebugLoc(),
153 TII->get(SP::UNIMP)).addImm(structSize);
154 // Bundle the delay filler and unimp with the instruction.
155 MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), J);
156 } else {
157 MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), I);
158 }
159 }
160 return Changed;
161}
162
164Filler::findDelayInstr(MachineBasicBlock &MBB,
166{
167 SmallSet<unsigned, 32> RegDefs;
168 SmallSet<unsigned, 32> RegUses;
169 bool sawLoad = false;
170 bool sawStore = false;
171
172 if (slot == MBB.begin())
173 return MBB.end();
174
175 unsigned Opc = slot->getOpcode();
176
177 if (Opc == SP::RET || Opc == SP::TLS_CALL)
178 return MBB.end();
179
180 if (Opc == SP::RETL || Opc == SP::TAIL_CALL || Opc == SP::TAIL_CALLri) {
182 --J;
183
184 if (J->getOpcode() == SP::RESTORErr
185 || J->getOpcode() == SP::RESTOREri) {
186 // change retl to ret.
187 if (Opc == SP::RETL)
188 slot->setDesc(Subtarget->getInstrInfo()->get(SP::RET));
189 return J;
190 }
191 }
192
193 // Call's delay filler can def some of call's uses.
194 if (slot->isCall())
195 insertCallDefsUses(slot, RegDefs, RegUses);
196 else
197 insertDefsUses(slot, RegDefs, RegUses);
198
199 bool done = false;
200
202
203 while (!done) {
204 done = (I == MBB.begin());
205
206 if (!done)
207 --I;
208
209 // Skip meta instructions.
210 if (I->isMetaInstruction())
211 continue;
212
213 if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
214 I->hasDelaySlot() || I->isBundledWithSucc())
215 break;
216
217 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
218 insertDefsUses(I, RegDefs, RegUses);
219 continue;
220 }
221
222 return I;
223 }
224 return MBB.end();
225}
226
227bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
228 bool &sawLoad,
229 bool &sawStore,
230 SmallSet<unsigned, 32> &RegDefs,
231 SmallSet<unsigned, 32> &RegUses)
232{
233
234 if (candidate->isImplicitDef() || candidate->isKill())
235 return true;
236
237 if (candidate->mayLoad()) {
238 sawLoad = true;
239 if (sawStore)
240 return true;
241 }
242
243 if (candidate->mayStore()) {
244 if (sawStore)
245 return true;
246 sawStore = true;
247 if (sawLoad)
248 return true;
249 }
250
251 for (const MachineOperand &MO : candidate->operands()) {
252 if (!MO.isReg())
253 continue; // skip
254
255 Register Reg = MO.getReg();
256
257 if (MO.isDef()) {
258 // check whether Reg is defined or used before delay slot.
259 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
260 return true;
261 }
262 if (MO.isUse()) {
263 // check whether Reg is defined before delay slot.
264 if (IsRegInSet(RegDefs, Reg))
265 return true;
266 }
267 }
268
269 unsigned Opcode = candidate->getOpcode();
270 // LD and LDD may have NOPs inserted afterwards in the case of some LEON
271 // processors, so we can't use the delay slot if this feature is switched-on.
272 if (Subtarget->insertNOPLoad()
273 &&
274 Opcode >= SP::LDDArr && Opcode <= SP::LDrr)
275 return true;
276
277 // Same as above for FDIV and FSQRT on some LEON processors.
278 if (Subtarget->fixAllFDIVSQRT()
279 &&
280 Opcode >= SP::FDIVD && Opcode <= SP::FSQRTD)
281 return true;
282
283 if (Subtarget->fixTN0009() && candidate->mayStore())
284 return true;
285
286 if (Subtarget->fixTN0013()) {
287 switch (Opcode) {
288 case SP::FDIVS:
289 case SP::FDIVD:
290 case SP::FSQRTS:
291 case SP::FSQRTD:
292 return true;
293 default:
294 break;
295 }
296 }
297
298 return false;
299}
300
301
302void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI,
303 SmallSet<unsigned, 32>& RegDefs,
304 SmallSet<unsigned, 32>& RegUses)
305{
306 // Regular calls define o7, which is visible to the instruction in delay slot.
307 // On the other hand, tail calls preserve it.
308 switch(MI->getOpcode()) {
309 default: llvm_unreachable("Unknown opcode.");
310 case SP::CALL:
311 RegDefs.insert(SP::O7);
312 break;
313 case SP::TAIL_CALL:
314 break;
315 case SP::CALLrr:
316 case SP::CALLri:
317 RegDefs.insert(SP::O7);
318 [[fallthrough]];
319 case SP::TAIL_CALLri:
320 assert(MI->getNumOperands() >= 2);
321 const MachineOperand &Reg = MI->getOperand(0);
322 assert(Reg.isReg() && "CALL first operand is not a register.");
323 assert(Reg.isUse() && "CALL first operand is not a use.");
324 RegUses.insert(Reg.getReg());
325
326 const MachineOperand &Operand1 = MI->getOperand(1);
327 if (Operand1.isImm() || Operand1.isGlobal())
328 break;
329 assert(Operand1.isReg() && "CALLrr second operand is not a register.");
330 assert(Operand1.isUse() && "CALLrr second operand is not a use.");
331 RegUses.insert(Operand1.getReg());
332 break;
333 }
334}
335
336// Insert Defs and Uses of MI into the sets RegDefs and RegUses.
337void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
338 SmallSet<unsigned, 32>& RegDefs,
339 SmallSet<unsigned, 32>& RegUses)
340{
341 for (const MachineOperand &MO : MI->operands()) {
342 if (!MO.isReg())
343 continue;
344
345 Register Reg = MO.getReg();
346 if (Reg == 0)
347 continue;
348 if (MO.isDef())
349 RegDefs.insert(Reg);
350 if (MO.isUse()) {
351 // Implicit register uses of retl are return values and
352 // retl does not use them.
353 if (MO.isImplicit() && MI->getOpcode() == SP::RETL)
354 continue;
355 RegUses.insert(Reg);
356 }
357 }
358}
359
360// returns true if the Reg or its alias is in the RegSet.
361bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
362{
363 // Check Reg and all aliased Registers.
364 for (MCRegAliasIterator AI(Reg, Subtarget->getRegisterInfo(), true);
365 AI.isValid(); ++AI)
366 if (RegSet.count(*AI))
367 return true;
368 return false;
369}
370
371bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
372{
373 if (!I->isCall())
374 return false;
375
376 unsigned structSizeOpNum = 0;
377 switch (I->getOpcode()) {
378 default: llvm_unreachable("Unknown call opcode.");
379 case SP::CALL:
380 structSizeOpNum = 1;
381 break;
382 case SP::CALLrr:
383 case SP::CALLri:
384 structSizeOpNum = 2;
385 break;
386 case SP::TLS_CALL: return false;
387 case SP::TAIL_CALLri:
388 case SP::TAIL_CALL: return false;
389 }
390
391 const MachineOperand &MO = I->getOperand(structSizeOpNum);
392 if (!MO.isImm())
393 return false;
394 StructSize = MO.getImm();
395 return true;
396}
397
401 const TargetInstrInfo *TII) {
402 // Before: add <op0>, <op1>, %i[0-7]
403 // restore %g0, %g0, %i[0-7]
404 //
405 // After : restore <op0>, <op1>, %o[0-7]
406
407 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
408 Register reg = AddMI->getOperand(0).getReg();
409 if (reg < SP::I0 || reg > SP::I7)
410 return false;
411
412 // Check whether it uses %o7 as its source and the corresponding branch
413 // instruction is a call.
414 MachineBasicBlock::iterator LastInst = MBB.getFirstTerminator();
415 bool IsCall = LastInst != MBB.end() && LastInst->isCall();
416
417 if (IsCall && AddMI->getOpcode() == SP::ADDrr &&
418 AddMI->readsRegister(SP::O7, TRI))
419 return false;
420
421 if (IsCall && AddMI->getOpcode() == SP::ADDri &&
422 AddMI->readsRegister(SP::O7, TRI))
423 return false;
424
425 // Erase RESTORE.
426 RestoreMI->eraseFromParent();
427
428 // Change ADD to RESTORE.
429 AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr)
430 ? SP::RESTORErr
431 : SP::RESTOREri));
432
433 // Map the destination register.
434 AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
435
436 return true;
437}
438
442 const TargetInstrInfo *TII) {
443 // Before: or <op0>, <op1>, %i[0-7]
444 // restore %g0, %g0, %i[0-7]
445 // and <op0> or <op1> is zero,
446 //
447 // After : restore <op0>, <op1>, %o[0-7]
448
449 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
450 Register reg = OrMI->getOperand(0).getReg();
451 if (reg < SP::I0 || reg > SP::I7)
452 return false;
453
454 // check whether it is a copy.
455 if (OrMI->getOpcode() == SP::ORrr
456 && OrMI->getOperand(1).getReg() != SP::G0
457 && OrMI->getOperand(2).getReg() != SP::G0)
458 return false;
459
460 if (OrMI->getOpcode() == SP::ORri
461 && OrMI->getOperand(1).getReg() != SP::G0
462 && (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0))
463 return false;
464
465 // Check whether it uses %o7 as its source and the corresponding branch
466 // instruction is a call.
467 MachineBasicBlock::iterator LastInst = MBB.getFirstTerminator();
468 bool IsCall = LastInst != MBB.end() && LastInst->isCall();
469
470 if (IsCall && OrMI->getOpcode() == SP::ORrr &&
471 OrMI->readsRegister(SP::O7, TRI))
472 return false;
473
474 // Erase RESTORE.
475 RestoreMI->eraseFromParent();
476
477 // Change OR to RESTORE.
478 OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr)
479 ? SP::RESTORErr
480 : SP::RESTOREri));
481
482 // Map the destination register.
483 OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
484
485 return true;
486}
487
490 const TargetInstrInfo *TII)
491{
492 // Before: sethi imm3, %i[0-7]
493 // restore %g0, %g0, %g0
494 //
495 // After : restore %g0, (imm3<<10), %o[0-7]
496
497 Register reg = SetHiMI->getOperand(0).getReg();
498 if (reg < SP::I0 || reg > SP::I7)
499 return false;
500
501 if (!SetHiMI->getOperand(1).isImm())
502 return false;
503
504 int64_t imm = SetHiMI->getOperand(1).getImm();
505
506 // Is it a 3 bit immediate?
507 if (!isInt<3>(imm))
508 return false;
509
510 // Make it a 13 bit immediate.
511 imm = (imm << 10) & 0x1FFF;
512
513 assert(RestoreMI->getOpcode() == SP::RESTORErr);
514
515 RestoreMI->setDesc(TII->get(SP::RESTOREri));
516
517 RestoreMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
518 RestoreMI->getOperand(1).setReg(SP::G0);
519 RestoreMI->getOperand(2).ChangeToImmediate(imm);
520
521
522 // Erase the original SETHI.
523 SetHiMI->eraseFromParent();
524
525 return true;
526}
527
528bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
530{
531 // No previous instruction.
532 if (MBBI == MBB.begin())
533 return false;
534
535 // assert that MBBI is a "restore %g0, %g0, %g0".
536 assert(MBBI->getOpcode() == SP::RESTORErr
537 && MBBI->getOperand(0).getReg() == SP::G0
538 && MBBI->getOperand(1).getReg() == SP::G0
539 && MBBI->getOperand(2).getReg() == SP::G0);
540
541 MachineBasicBlock::iterator PrevInst = std::prev(MBBI);
542
543 // It cannot be combined with a bundled instruction.
544 if (PrevInst->isBundledWithSucc())
545 return false;
546
547 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
548
549 switch (PrevInst->getOpcode()) {
550 default: break;
551 case SP::ADDrr:
552 case SP::ADDri:
553 return combineRestoreADD(MBB, MBBI, PrevInst, TII);
554 case SP::ORrr:
555 case SP::ORri:
556 return combineRestoreOR(MBB, MBBI, PrevInst, TII);
557 case SP::SETHIi: return combineRestoreSETHIi(MBBI, PrevInst, TII); break;
558 }
559 // It cannot combine with the previous instruction.
560 return false;
561}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static bool combineRestoreADD(MachineBasicBlock &MBB, MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator AddMI, const TargetInstrInfo *TII)
static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator SetHiMI, const TargetInstrInfo *TII)
static cl::opt< bool > DisableDelaySlotFiller("disable-sparc-delay-filler", cl::init(false), cl::desc("Disable the Sparc delay slot filler."), cl::Hidden)
static bool combineRestoreOR(MachineBasicBlock &MBB, MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator OrMI, const TargetInstrInfo *TII)
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static cl::opt< bool > DisableDelaySlotFiller("disable-mips-delay-filler", cl::init(false), cl::desc("Fill all delay slots with NOPs."), cl::Hidden)
This file defines the SmallSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Register getReg() const
getReg - Returns the register number.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:175
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:183
const SparcRegisterInfo * getRegisterInfo() const override
const SparcInstrInfo * getInstrInfo() const override
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
FunctionPass * createSparcDelaySlotFillerPass()
createSparcDelaySlotFillerPass - Returns a pass that fills in delay slots in Sparc MachineFunctions