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> DisableDelaySlotFiller(
34 "disable-mips-delay-filler",
36 cl::desc("Disable the delay slot filler, which attempts to fill the Mips"
37 "delay slots with useful instructions."),
40 // This option can be used to silence complaints by machine verifier passes.
41 static cl::opt<bool> SkipDelaySlotFiller(
42 "skip-mips-delay-filler",
44 cl::desc("Skip MIPS' delay slot filling pass."),
48 struct Filler : public MachineFunctionPass {
49 typedef MachineBasicBlock::instr_iterator InstrIter;
50 typedef MachineBasicBlock::reverse_instr_iterator ReverseInstrIter;
53 const TargetInstrInfo *TII;
57 Filler(TargetMachine &tm)
58 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
60 virtual const char *getPassName() const {
61 return "Mips Delay Slot Filler";
64 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
65 bool runOnMachineFunction(MachineFunction &F) {
66 if (SkipDelaySlotFiller)
70 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
72 Changed |= runOnMachineBasicBlock(*FI);
76 bool isDelayFiller(MachineBasicBlock &MBB,
79 void insertCallUses(InstrIter MI,
80 SmallSet<unsigned, 32> &RegDefs,
81 SmallSet<unsigned, 32> &RegUses);
83 void insertDefsUses(InstrIter MI,
84 SmallSet<unsigned, 32> &RegDefs,
85 SmallSet<unsigned, 32> &RegUses);
87 bool IsRegInSet(SmallSet<unsigned, 32> &RegSet,
90 bool delayHasHazard(InstrIter candidate,
91 bool &sawLoad, bool &sawStore,
92 SmallSet<unsigned, 32> &RegDefs,
93 SmallSet<unsigned, 32> &RegUses);
96 findDelayInstr(MachineBasicBlock &MBB, InstrIter slot,
102 } // end of anonymous namespace
104 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
105 /// We assume there is only one delay slot per delayed instruction.
107 runOnMachineBasicBlock(MachineBasicBlock &MBB) {
108 bool Changed = false;
109 LastFiller = MBB.instr_end();
111 for (InstrIter I = MBB.instr_begin(); I != MBB.instr_end(); ++I)
112 if (I->hasDelaySlot()) {
118 if (!DisableDelaySlotFiller && findDelayInstr(MBB, I, D)) {
119 MBB.splice(llvm::next(I), &MBB, D);
122 BuildMI(MBB, llvm::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
124 // Record the filler instruction that filled the delay slot.
125 // The instruction after it will be visited in the next iteration.
128 // Set InsideBundle bit so that the machine verifier doesn't expect this
129 // instruction to be a terminator.
130 LastFiller->setIsInsideBundle();
136 /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
137 /// slots in Mips MachineFunctions
138 FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
139 return new Filler(tm);
142 bool Filler::findDelayInstr(MachineBasicBlock &MBB,
145 SmallSet<unsigned, 32> RegDefs;
146 SmallSet<unsigned, 32> RegUses;
148 insertDefsUses(slot, RegDefs, RegUses);
150 bool sawLoad = false;
151 bool sawStore = false;
153 for (ReverseInstrIter I(slot); I != MBB.instr_rend(); ++I) {
155 if (I->isDebugValue())
158 // Convert to forward iterator.
159 InstrIter FI(llvm::next(I).base());
161 if (I->hasUnmodeledSideEffects()
168 // ERET, DERET or WAIT, PAUSE. Need to add these to instruction
173 if (delayHasHazard(FI, sawLoad, sawStore, RegDefs, RegUses)) {
174 insertDefsUses(FI, RegDefs, RegUses);
185 bool Filler::delayHasHazard(InstrIter candidate,
186 bool &sawLoad, bool &sawStore,
187 SmallSet<unsigned, 32> &RegDefs,
188 SmallSet<unsigned, 32> &RegUses) {
189 if (candidate->isImplicitDef() || candidate->isKill())
192 // Loads or stores cannot be moved past a store to the delay slot
193 // and stores cannot be moved past a load.
194 if (candidate->mayLoad()) {
200 if (candidate->mayStore()) {
208 assert((!candidate->isCall() && !candidate->isReturn()) &&
209 "Cannot put calls or returns in delay slot.");
211 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
212 const MachineOperand &MO = candidate->getOperand(i);
215 if (!MO.isReg() || !(Reg = MO.getReg()))
219 // check whether Reg is defined or used before delay slot.
220 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
224 // check whether Reg is defined before delay slot.
225 if (IsRegInSet(RegDefs, Reg))
232 // Insert Defs and Uses of MI into the sets RegDefs and RegUses.
233 void Filler::insertDefsUses(InstrIter MI,
234 SmallSet<unsigned, 32> &RegDefs,
235 SmallSet<unsigned, 32> &RegUses) {
236 // If MI is a call or return, just examine the explicit non-variadic operands.
237 MCInstrDesc MCID = MI->getDesc();
238 unsigned e = MI->isCall() || MI->isReturn() ? MCID.getNumOperands() :
239 MI->getNumOperands();
241 // Add RA to RegDefs to prevent users of RA from going into delay slot.
243 RegDefs.insert(Mips::RA);
245 for (unsigned i = 0; i != e; ++i) {
246 const MachineOperand &MO = MI->getOperand(i);
249 if (!MO.isReg() || !(Reg = MO.getReg()))
259 //returns true if the Reg or its alias is in the RegSet.
260 bool Filler::IsRegInSet(SmallSet<unsigned, 32> &RegSet, unsigned Reg) {
261 // Check Reg and all aliased Registers.
262 for (MCRegAliasIterator AI(Reg, TM.getRegisterInfo(), true);
264 if (RegSet.count(*AI))