LLVM 20.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"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "delay-slot-filler"
29
30STATISTIC(FilledSlots, "Number of delay slots filled");
31
33 "disable-sparc-delay-filler",
34 cl::init(false),
35 cl::desc("Disable the Sparc delay slot filler."),
37
38namespace {
39 struct Filler : public MachineFunctionPass {
40 const SparcSubtarget *Subtarget = nullptr;
41
42 static char ID;
43 Filler() : MachineFunctionPass(ID) {}
44
45 StringRef getPassName() const override { return "SPARC Delay Slot Filler"; }
46
47 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
48 bool runOnMachineFunction(MachineFunction &F) override {
49 bool Changed = false;
50 Subtarget = &F.getSubtarget<SparcSubtarget>();
51
52 // This pass invalidates liveness information when it reorders
53 // instructions to fill delay slot.
54 F.getRegInfo().invalidateLiveness();
55
56 for (MachineBasicBlock &MBB : F)
57 Changed |= runOnMachineBasicBlock(MBB);
58 return Changed;
59 }
60
63 MachineFunctionProperties::Property::NoVRegs);
64 }
65
66 void insertCallDefsUses(MachineBasicBlock::iterator MI,
68 SmallSet<unsigned, 32>& RegUses);
69
70 void insertDefsUses(MachineBasicBlock::iterator MI,
72 SmallSet<unsigned, 32>& RegUses);
73
74 bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
75 unsigned Reg);
76
77 bool delayHasHazard(MachineBasicBlock::iterator candidate,
78 bool &sawLoad, bool &sawStore,
80 SmallSet<unsigned, 32> &RegUses);
81
84
85 bool needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize);
86
87 bool tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
89
90 };
91 char Filler::ID = 0;
92} // end of anonymous namespace
93
94/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
95/// slots in Sparc MachineFunctions
96///
98 return new Filler;
99}
100
101
102/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
103/// We assume there is only one delay slot per delayed instruction.
104///
105bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
106 bool Changed = false;
107 Subtarget = &MBB.getParent()->getSubtarget<SparcSubtarget>();
108 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
109
110 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
112 ++I;
113
114 // If MI is restore, try combining it with previous inst.
116 (MI->getOpcode() == SP::RESTORErr
117 || MI->getOpcode() == SP::RESTOREri)) {
118 Changed |= tryCombineRestoreWithPrevInst(MBB, MI);
119 continue;
120 }
121
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));
128 Changed = true;
129 continue;
130 }
131
132 // If MI has no delay slot, skip.
133 if (!MI->hasDelaySlot())
134 continue;
135
137
139 D = findDelayInstr(MBB, MI);
140
141 ++FilledSlots;
142 Changed = true;
143
144 if (D == MBB.end())
145 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
146 else
147 MBB.splice(I, &MBB, D);
148
149 unsigned structSize = 0;
150 if (needsUnimp(MI, structSize)) {
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.
158 } else {
160 }
161 }
162 return Changed;
163}
164
166Filler::findDelayInstr(MachineBasicBlock &MBB,
168{
171 bool sawLoad = false;
172 bool sawStore = false;
173
174 if (slot == MBB.begin())
175 return MBB.end();
176
177 unsigned Opc = slot->getOpcode();
178
179 if (Opc == SP::RET || Opc == SP::TLS_CALL)
180 return MBB.end();
181
182 if (Opc == SP::RETL || Opc == SP::TAIL_CALL || Opc == SP::TAIL_CALLri) {
184 --J;
185
186 if (J->getOpcode() == SP::RESTORErr
187 || J->getOpcode() == SP::RESTOREri) {
188 // change retl to ret.
189 if (Opc == SP::RETL)
190 slot->setDesc(Subtarget->getInstrInfo()->get(SP::RET));
191 return J;
192 }
193 }
194
195 // Call's delay filler can def some of call's uses.
196 if (slot->isCall())
197 insertCallDefsUses(slot, RegDefs, RegUses);
198 else
199 insertDefsUses(slot, RegDefs, RegUses);
200
201 bool done = false;
202
204
205 while (!done) {
206 done = (I == MBB.begin());
207
208 if (!done)
209 --I;
210
211 // skip debug instruction
212 if (I->isDebugInstr())
213 continue;
214
215 if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
216 I->hasDelaySlot() || I->isBundledWithSucc())
217 break;
218
219 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
220 insertDefsUses(I, RegDefs, RegUses);
221 continue;
222 }
223
224 return I;
225 }
226 return MBB.end();
227}
228
229bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
230 bool &sawLoad,
231 bool &sawStore,
232 SmallSet<unsigned, 32> &RegDefs,
233 SmallSet<unsigned, 32> &RegUses)
234{
235
236 if (candidate->isImplicitDef() || candidate->isKill())
237 return true;
238
239 if (candidate->mayLoad()) {
240 sawLoad = true;
241 if (sawStore)
242 return true;
243 }
244
245 if (candidate->mayStore()) {
246 if (sawStore)
247 return true;
248 sawStore = true;
249 if (sawLoad)
250 return true;
251 }
252
253 for (const MachineOperand &MO : candidate->operands()) {
254 if (!MO.isReg())
255 continue; // skip
256
257 Register Reg = MO.getReg();
258
259 if (MO.isDef()) {
260 // check whether Reg is defined or used before delay slot.
261 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
262 return true;
263 }
264 if (MO.isUse()) {
265 // check whether Reg is defined before delay slot.
266 if (IsRegInSet(RegDefs, Reg))
267 return true;
268 }
269 }
270
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()
275 &&
276 Opcode >= SP::LDDArr && Opcode <= SP::LDrr)
277 return true;
278
279 // Same as above for FDIV and FSQRT on some LEON processors.
280 if (Subtarget->fixAllFDIVSQRT()
281 &&
282 Opcode >= SP::FDIVD && Opcode <= SP::FSQRTD)
283 return true;
284
285 if (Subtarget->fixTN0009() && candidate->mayStore())
286 return true;
287
288 if (Subtarget->fixTN0013()) {
289 switch (Opcode) {
290 case SP::FDIVS:
291 case SP::FDIVD:
292 case SP::FSQRTS:
293 case SP::FSQRTD:
294 return true;
295 default:
296 break;
297 }
298 }
299
300 return false;
301}
302
303
304void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI,
305 SmallSet<unsigned, 32>& RegDefs,
306 SmallSet<unsigned, 32>& RegUses)
307{
308 // Call defines o7, which is visible to the instruction in delay slot.
309 RegDefs.insert(SP::O7);
310
311 switch(MI->getOpcode()) {
312 default: llvm_unreachable("Unknown opcode.");
313 case SP::CALL: break;
314 case SP::CALLrr:
315 case SP::CALLri:
316 assert(MI->getNumOperands() >= 2);
317 const MachineOperand &Reg = MI->getOperand(0);
318 assert(Reg.isReg() && "CALL first operand is not a register.");
319 assert(Reg.isUse() && "CALL first operand is not a use.");
320 RegUses.insert(Reg.getReg());
321
322 const MachineOperand &Operand1 = MI->getOperand(1);
323 if (Operand1.isImm() || Operand1.isGlobal())
324 break;
325 assert(Operand1.isReg() && "CALLrr second operand is not a register.");
326 assert(Operand1.isUse() && "CALLrr second operand is not a use.");
327 RegUses.insert(Operand1.getReg());
328 break;
329 }
330}
331
332// Insert Defs and Uses of MI into the sets RegDefs and RegUses.
333void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
334 SmallSet<unsigned, 32>& RegDefs,
335 SmallSet<unsigned, 32>& RegUses)
336{
337 for (const MachineOperand &MO : MI->operands()) {
338 if (!MO.isReg())
339 continue;
340
341 Register Reg = MO.getReg();
342 if (Reg == 0)
343 continue;
344 if (MO.isDef())
345 RegDefs.insert(Reg);
346 if (MO.isUse()) {
347 // Implicit register uses of retl are return values and
348 // retl does not use them.
349 if (MO.isImplicit() && MI->getOpcode() == SP::RETL)
350 continue;
351 RegUses.insert(Reg);
352 }
353 }
354}
355
356// returns true if the Reg or its alias is in the RegSet.
357bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
358{
359 // Check Reg and all aliased Registers.
360 for (MCRegAliasIterator AI(Reg, Subtarget->getRegisterInfo(), true);
361 AI.isValid(); ++AI)
362 if (RegSet.count(*AI))
363 return true;
364 return false;
365}
366
367bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
368{
369 if (!I->isCall())
370 return false;
371
372 unsigned structSizeOpNum = 0;
373 switch (I->getOpcode()) {
374 default: llvm_unreachable("Unknown call opcode.");
375 case SP::CALL: structSizeOpNum = 1; break;
376 case SP::CALLrr:
377 case SP::CALLri: structSizeOpNum = 2; break;
378 case SP::TLS_CALL: return false;
379 case SP::TAIL_CALLri:
380 case SP::TAIL_CALL: return false;
381 }
382
383 const MachineOperand &MO = I->getOperand(structSizeOpNum);
384 if (!MO.isImm())
385 return false;
386 StructSize = MO.getImm();
387 return true;
388}
389
392 const TargetInstrInfo *TII)
393{
394 // Before: add <op0>, <op1>, %i[0-7]
395 // restore %g0, %g0, %i[0-7]
396 //
397 // After : restore <op0>, <op1>, %o[0-7]
398
399 Register reg = AddMI->getOperand(0).getReg();
400 if (reg < SP::I0 || reg > SP::I7)
401 return false;
402
403 // Erase RESTORE.
404 RestoreMI->eraseFromParent();
405
406 // Change ADD to RESTORE.
407 AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr)
408 ? SP::RESTORErr
409 : SP::RESTOREri));
410
411 // Map the destination register.
412 AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
413
414 return true;
415}
416
419 const TargetInstrInfo *TII)
420{
421 // Before: or <op0>, <op1>, %i[0-7]
422 // restore %g0, %g0, %i[0-7]
423 // and <op0> or <op1> is zero,
424 //
425 // After : restore <op0>, <op1>, %o[0-7]
426
427 Register reg = OrMI->getOperand(0).getReg();
428 if (reg < SP::I0 || reg > SP::I7)
429 return false;
430
431 // check whether it is a copy.
432 if (OrMI->getOpcode() == SP::ORrr
433 && OrMI->getOperand(1).getReg() != SP::G0
434 && OrMI->getOperand(2).getReg() != SP::G0)
435 return false;
436
437 if (OrMI->getOpcode() == SP::ORri
438 && OrMI->getOperand(1).getReg() != SP::G0
439 && (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0))
440 return false;
441
442 // Erase RESTORE.
443 RestoreMI->eraseFromParent();
444
445 // Change OR to RESTORE.
446 OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr)
447 ? SP::RESTORErr
448 : SP::RESTOREri));
449
450 // Map the destination register.
451 OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
452
453 return true;
454}
455
458 const TargetInstrInfo *TII)
459{
460 // Before: sethi imm3, %i[0-7]
461 // restore %g0, %g0, %g0
462 //
463 // After : restore %g0, (imm3<<10), %o[0-7]
464
465 Register reg = SetHiMI->getOperand(0).getReg();
466 if (reg < SP::I0 || reg > SP::I7)
467 return false;
468
469 if (!SetHiMI->getOperand(1).isImm())
470 return false;
471
472 int64_t imm = SetHiMI->getOperand(1).getImm();
473
474 // Is it a 3 bit immediate?
475 if (!isInt<3>(imm))
476 return false;
477
478 // Make it a 13 bit immediate.
479 imm = (imm << 10) & 0x1FFF;
480
481 assert(RestoreMI->getOpcode() == SP::RESTORErr);
482
483 RestoreMI->setDesc(TII->get(SP::RESTOREri));
484
485 RestoreMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
486 RestoreMI->getOperand(1).setReg(SP::G0);
487 RestoreMI->getOperand(2).ChangeToImmediate(imm);
488
489
490 // Erase the original SETHI.
491 SetHiMI->eraseFromParent();
492
493 return true;
494}
495
496bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
498{
499 // No previous instruction.
500 if (MBBI == MBB.begin())
501 return false;
502
503 // assert that MBBI is a "restore %g0, %g0, %g0".
504 assert(MBBI->getOpcode() == SP::RESTORErr
505 && MBBI->getOperand(0).getReg() == SP::G0
506 && MBBI->getOperand(1).getReg() == SP::G0
507 && MBBI->getOperand(2).getReg() == SP::G0);
508
509 MachineBasicBlock::iterator PrevInst = std::prev(MBBI);
510
511 // It cannot be combined with a bundled instruction.
512 if (PrevInst->isBundledWithSucc())
513 return false;
514
515 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
516
517 switch (PrevInst->getOpcode()) {
518 default: break;
519 case SP::ADDrr:
520 case SP::ADDri: return combineRestoreADD(MBBI, PrevInst, TII); break;
521 case SP::ORrr:
522 case SP::ORri: return combineRestoreOR(MBBI, PrevInst, TII); break;
523 case SP::SETHIi: return combineRestoreSETHIi(MBBI, PrevInst, TII); break;
524 }
525 // It cannot combine with the previous instruction.
526 return false;
527}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator SetHiMI, const TargetInstrInfo *TII)
static bool combineRestoreOR(MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator OrMI, 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 combineRestoreADD(MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator AddMI, const TargetInstrInfo *TII)
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:166
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
MCRegAliasIterator enumerates all registers aliasing Reg.
Helper class for constructing bundles of MachineInstrs.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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 '...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineOperand class - Representation of each machine instruction 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.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:166
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:179
const SparcRegisterInfo * getRegisterInfo() const override
const SparcInstrInfo * getInstrInfo() const override
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TargetInstrInfo - Interface to description of machine instruction set.
#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
Reg
All possible values of the reg field in the ModR/M byte.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createSparcDelaySlotFillerPass()
createSparcDelaySlotFillerPass - Returns a pass that fills in delay slots in Sparc MachineFunctions