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."),
39 // This option can be used to silence complaints by machine verifier passes.
40 static cl::opt<bool> SkipDelaySlotFiller(
41 "skip-mips-delay-filler",
43 cl::desc("Skip MIPS' delay slot filling pass."),
47 struct Filler : public MachineFunctionPass {
50 const TargetInstrInfo *TII;
51 MachineBasicBlock::iterator LastFiller;
54 Filler(TargetMachine &tm)
55 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
57 virtual const char *getPassName() const {
58 return "Mips Delay Slot Filler";
61 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
62 bool runOnMachineFunction(MachineFunction &F) {
63 if (SkipDelaySlotFiller)
67 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
69 Changed |= runOnMachineBasicBlock(*FI);
73 bool isDelayFiller(MachineBasicBlock &MBB,
74 MachineBasicBlock::iterator candidate);
76 void insertCallUses(MachineBasicBlock::iterator MI,
77 SmallSet<unsigned, 32>& RegDefs,
78 SmallSet<unsigned, 32>& RegUses);
80 void insertDefsUses(MachineBasicBlock::iterator MI,
81 SmallSet<unsigned, 32>& RegDefs,
82 SmallSet<unsigned, 32>& RegUses);
84 bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
87 bool delayHasHazard(MachineBasicBlock::iterator candidate,
88 bool &sawLoad, bool &sawStore,
89 SmallSet<unsigned, 32> &RegDefs,
90 SmallSet<unsigned, 32> &RegUses);
93 findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot,
94 MachineBasicBlock::iterator &Filler);
99 } // end of anonymous namespace
101 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
102 /// We assume there is only one delay slot per delayed instruction.
104 runOnMachineBasicBlock(MachineBasicBlock &MBB) {
105 bool Changed = false;
106 LastFiller = MBB.end();
108 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
109 if (I->hasDelaySlot()) {
113 MachineBasicBlock::iterator D;
115 if (EnableDelaySlotFiller && findDelayInstr(MBB, I, D)) {
116 MBB.splice(llvm::next(I), &MBB, D);
119 BuildMI(MBB, llvm::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
121 // Record the filler instruction that filled the delay slot.
122 // The instruction after it will be visited in the next iteration.
129 /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
130 /// slots in Mips MachineFunctions
131 FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
132 return new Filler(tm);
135 bool Filler::findDelayInstr(MachineBasicBlock &MBB,
136 MachineBasicBlock::iterator slot,
137 MachineBasicBlock::iterator &Filler) {
138 SmallSet<unsigned, 32> RegDefs;
139 SmallSet<unsigned, 32> RegUses;
141 insertDefsUses(slot, RegDefs, RegUses);
143 bool sawLoad = false;
144 bool sawStore = false;
146 for (MachineBasicBlock::reverse_iterator I(slot); I != MBB.rend(); ++I) {
148 if (I->isDebugValue())
151 // Convert to forward iterator.
152 MachineBasicBlock::iterator FI(llvm::next(I).base());
154 if (I->hasUnmodeledSideEffects()
161 // ERET, DERET or WAIT, PAUSE. Need to add these to instruction
166 if (delayHasHazard(FI, sawLoad, sawStore, RegDefs, RegUses)) {
167 insertDefsUses(FI, RegDefs, RegUses);
178 bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
179 bool &sawLoad, bool &sawStore,
180 SmallSet<unsigned, 32> &RegDefs,
181 SmallSet<unsigned, 32> &RegUses) {
182 if (candidate->isImplicitDef() || candidate->isKill())
185 // Loads or stores cannot be moved past a store to the delay slot
186 // and stores cannot be moved past a load.
187 if (candidate->mayLoad()) {
193 if (candidate->mayStore()) {
201 assert((!candidate->isCall() && !candidate->isReturn()) &&
202 "Cannot put calls or returns in delay slot.");
204 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
205 const MachineOperand &MO = candidate->getOperand(i);
208 if (!MO.isReg() || !(Reg = MO.getReg()))
212 // check whether Reg is defined or used before delay slot.
213 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
217 // check whether Reg is defined before delay slot.
218 if (IsRegInSet(RegDefs, Reg))
225 // Insert Defs and Uses of MI into the sets RegDefs and RegUses.
226 void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
227 SmallSet<unsigned, 32>& RegDefs,
228 SmallSet<unsigned, 32>& RegUses) {
229 // If MI is a call or return, just examine the explicit non-variadic operands.
230 MCInstrDesc MCID = MI->getDesc();
231 unsigned e = MI->isCall() || MI->isReturn() ? MCID.getNumOperands() :
232 MI->getNumOperands();
234 // Add RA to RegDefs to prevent users of RA from going into delay slot.
236 RegDefs.insert(Mips::RA);
238 for (unsigned i = 0; i != e; ++i) {
239 const MachineOperand &MO = MI->getOperand(i);
242 if (!MO.isReg() || !(Reg = MO.getReg()))
252 //returns true if the Reg or its alias is in the RegSet.
253 bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg) {
254 // Check Reg and all aliased Registers.
255 for (MCRegAliasIterator AI(Reg, TM.getRegisterInfo(), true);
257 if (RegSet.count(*AI))