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;
46 Filler(TargetMachine &tm)
47 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
49 virtual const char *getPassName() const {
50 return "Mips Delay Slot Filler";
53 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
54 bool runOnMachineFunction(MachineFunction &F) {
56 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
58 Changed |= runOnMachineBasicBlock(*FI);
62 bool isDelayFiller(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator candidate);
65 void insertCallUses(MachineBasicBlock::iterator MI,
66 SmallSet<unsigned, 32>& RegDefs,
67 SmallSet<unsigned, 32>& RegUses);
69 void insertDefsUses(MachineBasicBlock::iterator MI,
70 SmallSet<unsigned, 32>& RegDefs,
71 SmallSet<unsigned, 32>& RegUses);
73 bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
76 bool delayHasHazard(MachineBasicBlock::iterator candidate,
77 bool &sawLoad, bool &sawStore,
78 SmallSet<unsigned, 32> &RegDefs,
79 SmallSet<unsigned, 32> &RegUses);
81 MachineBasicBlock::iterator
82 findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot);
87 } // end of anonymous namespace
89 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
90 /// We assume there is only one delay slot per delayed instruction.
92 runOnMachineBasicBlock(MachineBasicBlock &MBB) {
94 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
95 if (I->getDesc().hasDelaySlot()) {
96 MachineBasicBlock::iterator D = MBB.end();
97 MachineBasicBlock::iterator J = I;
99 if (EnableDelaySlotFiller)
100 D = findDelayInstr(MBB, I);
106 BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(Mips::NOP));
108 MBB.splice(++J, &MBB, D);
114 /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
115 /// slots in Mips MachineFunctions
116 FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
117 return new Filler(tm);
120 MachineBasicBlock::iterator
121 Filler::findDelayInstr(MachineBasicBlock &MBB,
122 MachineBasicBlock::iterator slot) {
123 SmallSet<unsigned, 32> RegDefs;
124 SmallSet<unsigned, 32> RegUses;
125 bool sawLoad = false;
126 bool sawStore = false;
128 MachineBasicBlock::iterator I = slot;
130 // Call's delay filler can def some of call's uses.
131 if (slot->getDesc().isCall())
132 insertCallUses(slot, RegDefs, RegUses);
134 insertDefsUses(slot, RegDefs, RegUses);
139 done = (I == MBB.begin());
145 if (I->isDebugValue())
148 if (I->hasUnmodeledSideEffects()
151 || isDelayFiller(MBB, I)
152 || I->getDesc().isPseudo()
155 // ERET, DERET or WAIT, PAUSE. Need to add these to instruction
160 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
161 insertDefsUses(I, RegDefs, RegUses);
170 bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
173 SmallSet<unsigned, 32> &RegDefs,
174 SmallSet<unsigned, 32> &RegUses) {
175 if (candidate->isImplicitDef() || candidate->isKill())
178 // Loads or stores cannot be moved past a store to the delay slot
179 // and stores cannot be moved past a load.
180 if (candidate->getDesc().mayLoad()) {
186 if (candidate->getDesc().mayStore()) {
194 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
195 const MachineOperand &MO = candidate->getOperand(i);
199 unsigned 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 void Filler::insertCallUses(MachineBasicBlock::iterator MI,
216 SmallSet<unsigned, 32>& RegDefs,
217 SmallSet<unsigned, 32>& RegUses) {
218 switch(MI->getOpcode()) {
219 default: llvm_unreachable("Unknown opcode.");
224 assert(MI->getNumOperands() >= 1);
225 const MachineOperand &Reg = MI->getOperand(0);
226 assert(Reg.isReg() && "JALR first operand is not a register.");
227 RegUses.insert(Reg.getReg());
233 // Insert Defs and Uses of MI into the sets RegDefs and RegUses.
234 void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
235 SmallSet<unsigned, 32>& RegDefs,
236 SmallSet<unsigned, 32>& RegUses) {
237 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
238 const MachineOperand &MO = MI->getOperand(i);
242 unsigned 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 if (RegSet.count(Reg))
256 // check Aliased Registers
257 for (const unsigned *Alias = TM.getRegisterInfo()->getAliasSet(Reg);
259 if (RegSet.count(*Alias))
265 // return true if the candidate is a delay filler.
266 bool Filler::isDelayFiller(MachineBasicBlock &MBB,
267 MachineBasicBlock::iterator candidate) {
268 if (candidate == MBB.begin())
270 const MCInstrDesc &prevdesc = (--candidate)->getDesc();
271 return prevdesc.hasDelaySlot();