LLVM 24.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#include "llvm/IR/Analysis.h"
26#include "llvm/Pass.h"
28
29using namespace llvm;
30
31#define DEBUG_TYPE "delay-slot-filler"
32
33STATISTIC(FilledSlots, "Number of delay slots filled");
34
35namespace {
36struct SparcDelaySlotFillerLegacy : public MachineFunctionPass {
37 static char ID;
38 SparcDelaySlotFillerLegacy() : MachineFunctionPass(ID) {}
39
40 StringRef getPassName() const override { return "SPARC Delay Slot Filler"; }
41
42 bool runOnMachineFunction(MachineFunction &MF) override;
43
44 MachineFunctionProperties getRequiredProperties() const override {
45 return MachineFunctionProperties().setNoVRegs();
46 }
47
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 AU.setPreservesCFG();
51 }
52};
53char SparcDelaySlotFillerLegacy::ID = 0;
54} // end of anonymous namespace
55
56/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
57/// slots in Sparc MachineFunctions
58///
60 return new SparcDelaySlotFillerLegacy;
61}
62
66 const TargetInstrInfo *TII) {
67 // Before: add <op0>, <op1>, %i[0-7]
68 // restore %g0, %g0, %i[0-7]
69 //
70 // After : restore <op0>, <op1>, %o[0-7]
71
72 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
73 Register reg = AddMI->getOperand(0).getReg();
74 if (reg < SP::I0 || reg > SP::I7)
75 return false;
76
77 // Check whether it uses %o7 as its source and the corresponding branch
78 // instruction is a call.
79 MachineBasicBlock::iterator LastInst = MBB.getFirstTerminator();
80 bool IsCall = LastInst != MBB.end() && LastInst->isCall();
81
82 if (IsCall && AddMI->getOpcode() == SP::ADDrr &&
83 AddMI->readsRegister(SP::O7, TRI))
84 return false;
85
86 if (IsCall && AddMI->getOpcode() == SP::ADDri &&
87 AddMI->readsRegister(SP::O7, TRI))
88 return false;
89
90 // Erase RESTORE.
91 RestoreMI->eraseFromParent();
92
93 // Change ADD to RESTORE.
94 AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr) ? SP::RESTORErr
95 : SP::RESTOREri));
96
97 // Map the destination register.
98 AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
99
100 return true;
101}
102
106 const TargetInstrInfo *TII) {
107 // Before: or <op0>, <op1>, %i[0-7]
108 // restore %g0, %g0, %i[0-7]
109 // and <op0> or <op1> is zero,
110 //
111 // After : restore <op0>, <op1>, %o[0-7]
112
113 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
114 Register reg = OrMI->getOperand(0).getReg();
115 if (reg < SP::I0 || reg > SP::I7)
116 return false;
117
118 // check whether it is a copy.
119 if (OrMI->getOpcode() == SP::ORrr && OrMI->getOperand(1).getReg() != SP::G0 &&
120 OrMI->getOperand(2).getReg() != SP::G0)
121 return false;
122
123 if (OrMI->getOpcode() == SP::ORri && OrMI->getOperand(1).getReg() != SP::G0 &&
124 (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0))
125 return false;
126
127 // Check whether it uses %o7 as its source and the corresponding branch
128 // instruction is a call.
129 MachineBasicBlock::iterator LastInst = MBB.getFirstTerminator();
130 bool IsCall = LastInst != MBB.end() && LastInst->isCall();
131
132 if (IsCall && OrMI->getOpcode() == SP::ORrr &&
133 OrMI->readsRegister(SP::O7, TRI))
134 return false;
135
136 // Erase RESTORE.
137 RestoreMI->eraseFromParent();
138
139 // Change OR to RESTORE.
140 OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr) ? SP::RESTORErr
141 : SP::RESTOREri));
142
143 // Map the destination register.
144 OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
145
146 return true;
147}
148
151 const TargetInstrInfo *TII) {
152 // Before: sethi imm3, %i[0-7]
153 // restore %g0, %g0, %g0
154 //
155 // After : restore %g0, (imm3<<10), %o[0-7]
156
157 Register reg = SetHiMI->getOperand(0).getReg();
158 if (reg < SP::I0 || reg > SP::I7)
159 return false;
160
161 if (!SetHiMI->getOperand(1).isImm())
162 return false;
163
164 int64_t imm = SetHiMI->getOperand(1).getImm();
165
166 // Is it a 3 bit immediate?
167 if (!isInt<3>(imm))
168 return false;
169
170 // Make it a 13 bit immediate.
171 imm = (imm << 10) & 0x1FFF;
172
173 assert(RestoreMI->getOpcode() == SP::RESTORErr);
174
175 RestoreMI->setDesc(TII->get(SP::RESTOREri));
176
177 RestoreMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
178 RestoreMI->getOperand(1).setReg(SP::G0);
179 RestoreMI->getOperand(2).ChangeToImmediate(imm);
180
181 // Erase the original SETHI.
182 SetHiMI->eraseFromParent();
183
184 return true;
185}
186
189 const SparcSubtarget *Subtarget) {
190 // No previous instruction.
191 if (MBBI == MBB.begin())
192 return false;
193
194 // assert that MBBI is a "restore %g0, %g0, %g0".
195 assert(MBBI->getOpcode() == SP::RESTORErr &&
196 MBBI->getOperand(0).getReg() == SP::G0 &&
197 MBBI->getOperand(1).getReg() == SP::G0 &&
198 MBBI->getOperand(2).getReg() == SP::G0);
199
200 MachineBasicBlock::iterator PrevInst = std::prev(MBBI);
201
202 // It cannot be combined with a bundled instruction.
203 if (PrevInst->isBundledWithSucc())
204 return false;
205
206 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
207
208 switch (PrevInst->getOpcode()) {
209 default:
210 break;
211 case SP::ADDrr:
212 case SP::ADDri:
213 return combineRestoreADD(MBB, MBBI, PrevInst, TII);
214 case SP::ORrr:
215 case SP::ORri:
216 return combineRestoreOR(MBB, MBBI, PrevInst, TII);
217 case SP::SETHIi:
218 return combineRestoreSETHIi(MBBI, PrevInst, TII);
219 break;
220 }
221 // It cannot combine with the previous instruction.
222 return false;
223}
224
225/// The restore before a tail call must run in its delay slot: `jmp %reg`
226/// reads a register of the current window, and `call` writes %o7. For the
227/// same reason a leaf `call` needs the copy of %o7 back from %g1 that
228/// SparcFrameLowering emits.
232 unsigned Opc = slot->getOpcode();
233 if ((Opc != SP::TAIL_CALL && Opc != SP::TAIL_CALLri) || slot == MBB.begin())
234 return MBB.end();
235
237 do {
238 --J;
239 } while (J->isMetaInstruction() && J != MBB.begin());
240
241 if (J->getOpcode() == SP::RESTORErr || J->getOpcode() == SP::RESTOREri)
242 return J;
243 if (Opc == SP::TAIL_CALL && J->getOpcode() == SP::ORrr &&
244 J->getOperand(0).getReg() == SP::O7)
245 return J;
246 return MBB.end();
247}
248
250 SmallSet<unsigned, 32> &RegDefs,
251 SmallSet<unsigned, 32> &RegUses) {
252 // Regular calls define o7, which is visible to the instruction in delay slot.
253 // On the other hand, tail calls preserve it.
254 switch (MI->getOpcode()) {
255 default:
256 llvm_unreachable("Unknown opcode.");
257 case SP::CALL:
258 RegDefs.insert(SP::O7);
259 break;
260 case SP::TAIL_CALL:
261 break;
262 case SP::CALLrr:
263 case SP::CALLri:
264 RegDefs.insert(SP::O7);
265 [[fallthrough]];
266 case SP::TAIL_CALLri:
267 assert(MI->getNumOperands() >= 2);
268 const MachineOperand &Reg = MI->getOperand(0);
269 assert(Reg.isReg() && "CALL first operand is not a register.");
270 assert(Reg.isUse() && "CALL first operand is not a use.");
271 RegUses.insert(Reg.getReg());
272
273 const MachineOperand &Operand1 = MI->getOperand(1);
274 if (Operand1.isImm() || Operand1.isGlobal())
275 break;
276 assert(Operand1.isReg() && "CALLrr second operand is not a register.");
277 assert(Operand1.isUse() && "CALLrr second operand is not a use.");
278 RegUses.insert(Operand1.getReg());
279 break;
280 }
281}
282
283// Insert Defs and Uses of MI into the sets RegDefs and RegUses.
285 SmallSet<unsigned, 32> &RegDefs,
286 SmallSet<unsigned, 32> &RegUses,
287 const SparcSubtarget *Subtarget) {
288 for (const MachineOperand &MO : MI->operands()) {
289 if (!MO.isReg())
290 continue;
291
292 Register Reg = MO.getReg();
293 if (Reg == 0 || Subtarget->getRegisterInfo()->isConstantPhysReg(Reg))
294 continue;
295 if (MO.isDef())
296 RegDefs.insert(Reg);
297 if (MO.isUse()) {
298 // Implicit register uses of retl are return values and
299 // retl does not use them.
300 if (MO.isImplicit() && MI->getOpcode() == SP::RETL)
301 continue;
302 RegUses.insert(Reg);
303 }
304 }
305}
306
307// returns true if the Reg or its alias is in the RegSet.
308static bool IsRegInSet(SmallSet<unsigned, 32> &RegSet, unsigned Reg,
309 const SparcSubtarget *Subtarget) {
310 // Check Reg and all aliased Registers.
311 for (MCRegAliasIterator AI(Reg, Subtarget->getRegisterInfo(), true);
312 AI.isValid(); ++AI)
313 if (RegSet.count(*AI))
314 return true;
315 return false;
316}
317
318static bool delayHasHazard(MachineBasicBlock::iterator candidate, bool &sawLoad,
319 bool &sawStore, SmallSet<unsigned, 32> &RegDefs,
320 SmallSet<unsigned, 32> &RegUses,
321 const SparcSubtarget *Subtarget) {
322
323 if (candidate->isImplicitDef() || candidate->isKill())
324 return true;
325
326 if (candidate->mayLoad()) {
327 sawLoad = true;
328 if (sawStore)
329 return true;
330 }
331
332 if (candidate->mayStore()) {
333 if (sawStore)
334 return true;
335 sawStore = true;
336 if (sawLoad)
337 return true;
338 }
339
340 for (const MachineOperand &MO : candidate->operands()) {
341 if (!MO.isReg())
342 continue; // skip
343
344 Register Reg = MO.getReg();
345
346 if (MO.isDef()) {
347 // check whether Reg is defined or used before delay slot.
348 if (IsRegInSet(RegDefs, Reg, Subtarget) ||
349 IsRegInSet(RegUses, Reg, Subtarget))
350 return true;
351 }
352 if (MO.isUse()) {
353 // check whether Reg is defined before delay slot.
354 if (IsRegInSet(RegDefs, Reg, Subtarget))
355 return true;
356 }
357 }
358
359 unsigned Opcode = candidate->getOpcode();
360 // LD and LDD may have NOPs inserted afterwards in the case of some LEON
361 // processors, so we can't use the delay slot if this feature is switched-on.
362 if (Subtarget->insertNOPLoad()
363 &&
364 Opcode >= SP::LDDArr && Opcode <= SP::LDrr)
365 return true;
366
367 // Same as above for FDIV and FSQRT on some LEON processors.
368 if (Subtarget->fixAllFDIVSQRT()
369 &&
370 Opcode >= SP::FDIVD && Opcode <= SP::FSQRTD)
371 return true;
372
373 if (Subtarget->fixTN0009() && candidate->mayStore())
374 return true;
375
376 if (Subtarget->fixTN0013()) {
377 switch (Opcode) {
378 case SP::FDIVS:
379 case SP::FDIVD:
380 case SP::FSQRTS:
381 case SP::FSQRTD:
382 return true;
383 default:
384 break;
385 }
386 }
387
388 return false;
389}
390
393 const SparcSubtarget *Subtarget) {
396 bool sawLoad = false;
397 bool sawStore = false;
398
399 if (slot == MBB.begin())
400 return MBB.end();
401
402 unsigned Opc = slot->getOpcode();
403
404 if (Opc == SP::RET || Opc == SP::TLS_CALL)
405 return MBB.end();
406
407 if (Opc == SP::RETL || Opc == SP::TAIL_CALL || Opc == SP::TAIL_CALLri) {
409 --J;
410
411 if (J->getOpcode() == SP::RESTORErr || J->getOpcode() == SP::RESTOREri) {
412 // change retl to ret.
413 if (Opc == SP::RETL)
414 slot->setDesc(Subtarget->getInstrInfo()->get(SP::RET));
415 return J;
416 }
417 }
418
419 // Call's delay filler can def some of call's uses.
420 if (slot->isCall())
421 insertCallDefsUses(slot, RegDefs, RegUses);
422 else
423 insertDefsUses(slot, RegDefs, RegUses, Subtarget);
424
425 bool done = false;
426
428
429 while (!done) {
430 done = (I == MBB.begin());
431
432 if (!done)
433 --I;
434
435 // Skip meta instructions.
436 if (I->isMetaInstruction())
437 continue;
438
439 if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
440 I->hasDelaySlot() || I->isBundledWithSucc())
441 break;
442
443 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses, Subtarget)) {
444 insertDefsUses(I, RegDefs, RegUses, Subtarget);
445 continue;
446 }
447
448 return I;
449 }
450 return MBB.end();
451}
452
453/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
454/// We assume there is only one delay slot per delayed instruction.
455///
457 bool Changed = false;
458 const SparcSubtarget *Subtarget =
459 &MBB.getParent()->getSubtarget<SparcSubtarget>();
460 bool FillSlots =
461 !Subtarget->getCLOpts().disable_sparc_delay_filler &&
462 MBB.getParent()->getTarget().getOptLevel() != CodeGenOptLevel::None;
463 const SparcInstrInfo *TII = Subtarget->getInstrInfo();
464
465 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end();) {
467 ++I;
468
469 // If MI is restore, try combining it with previous inst.
470 if (FillSlots && (MI->getOpcode() == SP::RESTORErr ||
471 MI->getOpcode() == SP::RESTOREri)) {
473 continue;
474 }
475
476 // TODO: If we ever want to support v7, this needs to be extended
477 // to cover all floating point operations.
478 if (!Subtarget->isV9() &&
479 (MI->getOpcode() == SP::FCMPS || MI->getOpcode() == SP::FCMPD ||
480 MI->getOpcode() == SP::FCMPQ)) {
481 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
482 Changed = true;
483 continue;
484 }
485
486 // If MI has no delay slot, skip.
487 if (!MI->hasDelaySlot())
488 continue;
489
491
492 if (D == MBB.end() && FillSlots)
493 D = findDelayInstr(MBB, MI, Subtarget);
494
495 ++FilledSlots;
496 Changed = true;
497
498 if (D == MBB.end())
499 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
500 else
501 MBB.splice(I, &MBB, D);
502
503 unsigned structSize = 0;
504 if (TII->needsUnimp(*MI, structSize)) {
506 ++J; // skip the delay filler.
507 assert(J != MBB.end() && "MI needs a delay instruction.");
508 BuildMI(MBB, ++J, MI->getDebugLoc(), TII->get(SP::UNIMP))
509 .addImm(structSize);
510 // Bundle the delay filler and unimp with the instruction.
512 } else {
514 }
515 }
516 return Changed;
517}
518
520 bool Changed = false;
521
522 // This pass invalidates liveness information when it reorders
523 // instructions to fill delay slot.
525
526 for (MachineBasicBlock &MBB : MF)
528 return Changed;
529}
530
531bool SparcDelaySlotFillerLegacy::runOnMachineFunction(MachineFunction &MF) {
533}
534
535PreservedAnalyses
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 tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const SparcSubtarget *Subtarget)
static void insertDefsUses(MachineBasicBlock::iterator MI, SmallSet< unsigned, 32 > &RegDefs, SmallSet< unsigned, 32 > &RegUses, const SparcSubtarget *Subtarget)
static bool combineRestoreADD(MachineBasicBlock &MBB, MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator AddMI, const TargetInstrInfo *TII)
static void insertCallDefsUses(MachineBasicBlock::iterator MI, SmallSet< unsigned, 32 > &RegDefs, SmallSet< unsigned, 32 > &RegUses)
static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator SetHiMI, const TargetInstrInfo *TII)
static MachineBasicBlock::iterator findRequiredDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot)
The restore before a tail call must run in its delay slot: jmp reg reads a register of the current wi...
static bool runDelaySlotFillerOnMachineFunction(MachineFunction &MF)
static MachineBasicBlock::iterator findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot, const SparcSubtarget *Subtarget)
static bool combineRestoreOR(MachineBasicBlock &MBB, MachineBasicBlock::iterator RestoreMI, MachineBasicBlock::iterator OrMI, const TargetInstrInfo *TII)
static bool runOnMachineBasicBlock(MachineBasicBlock &MBB)
runOnMachineBasicBlock - Fill in delay slots for the given basic block.
static bool delayHasHazard(MachineBasicBlock::iterator candidate, bool &sawLoad, bool &sawStore, SmallSet< unsigned, 32 > &RegDefs, SmallSet< unsigned, 32 > &RegUses, const SparcSubtarget *Subtarget)
static bool IsRegInSet(SmallSet< unsigned, 32 > &RegSet, unsigned Reg, const SparcSubtarget *Subtarget)
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
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
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
MCRegAliasIterator enumerates all registers aliasing Reg.
An RAII based helper class to modify MachineFunctionProperties when running pass.
Helper class for constructing bundles of MachineInstrs.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineOperand class - Representation of each machine instruction operand.
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.
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
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:184
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const SparcRegisterInfo * getRegisterInfo() const override
const SparcOptions & getCLOpts() const
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...
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
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:166
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createSparcDelaySlotFillerLegacyPass()
createSparcDelaySlotFillerPass - Returns a pass that fills in delay slots in Sparc MachineFunctions