1 //===-- DelaySlotFiller.cpp - Mips Delay Slot Filler ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Simple pass to fills delay slots with useful instructions.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "delay-slot-filler"
17 #include "MipsTargetMachine.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/Statistic.h"
29 STATISTIC(FilledSlots, "Number of delay slots filled");
30 STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
33 static cl::opt<bool> EnableDelaySlotFiller(
34 "enable-mips-delay-filler",
36 cl::desc("Fill the Mips delay slots useful instructions."),
40 struct Filler : public MachineFunctionPass {
43 const TargetInstrInfo *TII;
44 MachineBasicBlock::iterator LastFiller;
47 Filler(TargetMachine &tm)
48 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
50 virtual const char *getPassName() const {
51 return "Mips Delay Slot Filler";
54 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
55 bool runOnMachineFunction(MachineFunction &F) {
57 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
59 Changed |= runOnMachineBasicBlock(*FI);
63 bool isDelayFiller(MachineBasicBlock &MBB,
64 MachineBasicBlock::iterator candidate);
66 void insertCallUses(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);
83 findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot,
84 MachineBasicBlock::iterator &Filler);
89 } // end of anonymous namespace
91 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
92 /// We assume there is only one delay slot per delayed instruction.
94 runOnMachineBasicBlock(MachineBasicBlock &MBB) {
96 LastFiller = MBB.end();
98 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
99 if (I->hasDelaySlot()) {
103 MachineBasicBlock::iterator D;
105 if (EnableDelaySlotFiller && findDelayInstr(MBB, I, D)) {
106 MBB.splice(llvm::next(I), &MBB, D);
109 BuildMI(MBB, llvm::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
111 // Record the filler instruction that filled the delay slot.
112 // The instruction after it will be visited in the next iteration.
119 /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
120 /// slots in Mips MachineFunctions
121 FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
122 return new Filler(tm);
125 bool Filler::findDelayInstr(MachineBasicBlock &MBB,
126 MachineBasicBlock::iterator slot,
127 MachineBasicBlock::iterator &Filler) {
128 SmallSet<unsigned, 32> RegDefs;
129 SmallSet<unsigned, 32> RegUses;
131 insertDefsUses(slot, RegDefs, RegUses);
133 bool sawLoad = false;
134 bool sawStore = false;
136 for (MachineBasicBlock::reverse_iterator I(slot); I != MBB.rend(); ++I) {
138 if (I->isDebugValue())
141 // Convert to forward iterator.
142 MachineBasicBlock::iterator FI(llvm::next(I).base());
144 if (I->hasUnmodeledSideEffects()
151 // ERET, DERET or WAIT, PAUSE. Need to add these to instruction
156 if (delayHasHazard(FI, sawLoad, sawStore, RegDefs, RegUses)) {
157 insertDefsUses(FI, RegDefs, RegUses);
168 bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
169 bool &sawLoad, bool &sawStore,
170 SmallSet<unsigned, 32> &RegDefs,
171 SmallSet<unsigned, 32> &RegUses) {
172 if (candidate->isImplicitDef() || candidate->isKill())
175 // Loads or stores cannot be moved past a store to the delay slot
176 // and stores cannot be moved past a load.
177 if (candidate->mayLoad()) {
183 if (candidate->mayStore()) {
191 assert((!candidate->isCall() && !candidate->isReturn()) &&
192 "Cannot put calls or returns in delay slot.");
194 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
195 const MachineOperand &MO = candidate->getOperand(i);
198 if (!MO.isReg() || !(Reg = MO.getReg()))
202 // check whether Reg is defined or used before delay slot.
203 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
207 // check whether Reg is defined before delay slot.
208 if (IsRegInSet(RegDefs, Reg))
215 // Insert Defs and Uses of MI into the sets RegDefs and RegUses.
216 void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
217 SmallSet<unsigned, 32>& RegDefs,
218 SmallSet<unsigned, 32>& RegUses) {
219 // If MI is a call or return, just examine the explicit non-variadic operands.
220 MCInstrDesc MCID = MI->getDesc();
221 unsigned e = MI->isCall() || MI->isReturn() ? MCID.getNumOperands() :
222 MI->getNumOperands();
224 // Add RA to RegDefs to prevent users of RA from going into delay slot.
226 RegDefs.insert(Mips::RA);
228 for (unsigned i = 0; i != e; ++i) {
229 const MachineOperand &MO = MI->getOperand(i);
232 if (!MO.isReg() || !(Reg = MO.getReg()))
242 //returns true if the Reg or its alias is in the RegSet.
243 bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg) {
244 if (RegSet.count(Reg))
246 // check Aliased Registers
247 for (const uint16_t *Alias = TM.getRegisterInfo()->getAliasSet(Reg);
249 if (RegSet.count(*Alias))