Add a command line option to skip the delay slot filler pass entirely for Mips.
[oota-llvm.git] / lib / Target / Mips / MipsDelaySlotFiller.cpp
1 //===-- DelaySlotFiller.cpp - Mips Delay Slot Filler ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Simple pass to fills delay slots with useful instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "delay-slot-filler"
15
16 #include "Mips.h"
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"
26
27 using namespace llvm;
28
29 STATISTIC(FilledSlots, "Number of delay slots filled");
30 STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
31                        " are not NOP.");
32
33 static cl::opt<bool> EnableDelaySlotFiller(
34   "enable-mips-delay-filler",
35   cl::init(false),
36   cl::desc("Fill the Mips delay slots useful instructions."),
37   cl::Hidden);
38
39 // This option can be used to silence complaints by machine verifier passes.
40 static cl::opt<bool> SkipDelaySlotFiller(
41   "skip-mips-delay-filler",
42   cl::init(false),
43   cl::desc("Skip MIPS' delay slot filling pass."),
44   cl::Hidden);
45
46 namespace {
47   struct Filler : public MachineFunctionPass {
48
49     TargetMachine &TM;
50     const TargetInstrInfo *TII;
51     MachineBasicBlock::iterator LastFiller;
52
53     static char ID;
54     Filler(TargetMachine &tm)
55       : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
56
57     virtual const char *getPassName() const {
58       return "Mips Delay Slot Filler";
59     }
60
61     bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
62     bool runOnMachineFunction(MachineFunction &F) {
63       if (SkipDelaySlotFiller)
64         return false;
65
66       bool Changed = false;
67       for (MachineFunction::iterator FI = F.begin(), FE = F.end();
68            FI != FE; ++FI)
69         Changed |= runOnMachineBasicBlock(*FI);
70       return Changed;
71     }
72
73     bool isDelayFiller(MachineBasicBlock &MBB,
74                        MachineBasicBlock::iterator candidate);
75
76     void insertCallUses(MachineBasicBlock::iterator MI,
77                         SmallSet<unsigned, 32>& RegDefs,
78                         SmallSet<unsigned, 32>& RegUses);
79
80     void insertDefsUses(MachineBasicBlock::iterator MI,
81                         SmallSet<unsigned, 32>& RegDefs,
82                         SmallSet<unsigned, 32>& RegUses);
83
84     bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
85                     unsigned Reg);
86
87     bool delayHasHazard(MachineBasicBlock::iterator candidate,
88                         bool &sawLoad, bool &sawStore,
89                         SmallSet<unsigned, 32> &RegDefs,
90                         SmallSet<unsigned, 32> &RegUses);
91
92     bool
93     findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot,
94                    MachineBasicBlock::iterator &Filler);
95
96
97   };
98   char Filler::ID = 0;
99 } // end of anonymous namespace
100
101 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
102 /// We assume there is only one delay slot per delayed instruction.
103 bool Filler::
104 runOnMachineBasicBlock(MachineBasicBlock &MBB) {
105   bool Changed = false;
106   LastFiller = MBB.end();
107
108   for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
109     if (I->hasDelaySlot()) {
110       ++FilledSlots;
111       Changed = true;
112
113       MachineBasicBlock::iterator D;
114
115       if (EnableDelaySlotFiller && findDelayInstr(MBB, I, D)) {
116         MBB.splice(llvm::next(I), &MBB, D);
117         ++UsefulSlots;
118       } else
119         BuildMI(MBB, llvm::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
120
121       // Record the filler instruction that filled the delay slot.
122       // The instruction after it will be visited in the next iteration.
123       LastFiller = ++I;
124      }
125   return Changed;
126
127 }
128
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);
133 }
134
135 bool Filler::findDelayInstr(MachineBasicBlock &MBB,
136                             MachineBasicBlock::iterator slot,
137                             MachineBasicBlock::iterator &Filler) {
138   SmallSet<unsigned, 32> RegDefs;
139   SmallSet<unsigned, 32> RegUses;
140
141   insertDefsUses(slot, RegDefs, RegUses);
142
143   bool sawLoad = false;
144   bool sawStore = false;
145
146   for (MachineBasicBlock::reverse_iterator I(slot); I != MBB.rend(); ++I) {
147     // skip debug value
148     if (I->isDebugValue())
149       continue;
150
151     // Convert to forward iterator.
152     MachineBasicBlock::iterator FI(llvm::next(I).base());
153
154     if (I->hasUnmodeledSideEffects()
155         || I->isInlineAsm()
156         || I->isLabel()
157         || FI == LastFiller
158         || I->isPseudo()
159         //
160         // Should not allow:
161         // ERET, DERET or WAIT, PAUSE. Need to add these to instruction
162         // list. TBD.
163         )
164       break;
165
166     if (delayHasHazard(FI, sawLoad, sawStore, RegDefs, RegUses)) {
167       insertDefsUses(FI, RegDefs, RegUses);
168       continue;
169     }
170
171     Filler = FI;
172     return true;
173   }
174
175   return false;
176 }
177
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())
183     return true;
184
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()) {
188     if (sawStore)
189       return true;
190     sawLoad = true;
191   }
192
193   if (candidate->mayStore()) {
194     if (sawStore)
195       return true;
196     sawStore = true;
197     if (sawLoad)
198       return true;
199   }
200
201   assert((!candidate->isCall() && !candidate->isReturn()) &&
202          "Cannot put calls or returns in delay slot.");
203
204   for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
205     const MachineOperand &MO = candidate->getOperand(i);
206     unsigned Reg;
207
208     if (!MO.isReg() || !(Reg = MO.getReg()))
209       continue; // skip
210
211     if (MO.isDef()) {
212       // check whether Reg is defined or used before delay slot.
213       if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
214         return true;
215     }
216     if (MO.isUse()) {
217       // check whether Reg is defined before delay slot.
218       if (IsRegInSet(RegDefs, Reg))
219         return true;
220     }
221   }
222   return false;
223 }
224
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();
233
234   // Add RA to RegDefs to prevent users of RA from going into delay slot.
235   if (MI->isCall())
236     RegDefs.insert(Mips::RA);
237
238   for (unsigned i = 0; i != e; ++i) {
239     const MachineOperand &MO = MI->getOperand(i);
240     unsigned Reg;
241
242     if (!MO.isReg() || !(Reg = MO.getReg()))
243       continue;
244
245     if (MO.isDef())
246       RegDefs.insert(Reg);
247     else if (MO.isUse())
248       RegUses.insert(Reg);
249   }
250 }
251
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))
255     return true;
256   // check Aliased Registers
257   for (const uint16_t *Alias = TM.getRegisterInfo()->getAliasSet(Reg);
258        *Alias; ++Alias)
259     if (RegSet.count(*Alias))
260       return true;
261
262   return false;
263 }