blob: 8fe3ba6171295e6212790fa26ed979f1282ae372 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- DelaySlotFiller.cpp - Mips Delay Slot Filler ----------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00009//
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000010// Simple pass to fills delay slots with useful instructions.
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000011//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000013
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"
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000022#include "llvm/Target/TargetInstrInfo.h"
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000023#include "llvm/Target/TargetRegisterInfo.h"
24#include "llvm/ADT/SmallSet.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000025#include "llvm/ADT/Statistic.h"
26
27using namespace llvm;
28
29STATISTIC(FilledSlots, "Number of delay slots filled");
Akira Hatanaka9e603442011-10-05 01:19:13 +000030STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
Akira Hatanaka02e760a2011-10-05 02:22:49 +000031 " are not NOP.");
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000032
Akira Hatanaka9d957842012-08-22 02:51:28 +000033static cl::opt<bool> DisableDelaySlotFiller(
34 "disable-mips-delay-filler",
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000035 cl::init(false),
Akira Hatanaka9d957842012-08-22 02:51:28 +000036 cl::desc("Disable the delay slot filler, which attempts to fill the Mips"
37 "delay slots with useful instructions."),
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000038 cl::Hidden);
39
Akira Hatanaka4773e672012-05-14 23:59:17 +000040// This option can be used to silence complaints by machine verifier passes.
41static cl::opt<bool> SkipDelaySlotFiller(
42 "skip-mips-delay-filler",
43 cl::init(false),
44 cl::desc("Skip MIPS' delay slot filling pass."),
45 cl::Hidden);
46
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000047namespace {
48 struct Filler : public MachineFunctionPass {
Akira Hatanaka5ac78682012-06-13 23:25:52 +000049 typedef MachineBasicBlock::instr_iterator InstrIter;
50 typedef MachineBasicBlock::reverse_instr_iterator ReverseInstrIter;
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000051
52 TargetMachine &TM;
53 const TargetInstrInfo *TII;
Akira Hatanaka5ac78682012-06-13 23:25:52 +000054 InstrIter LastFiller;
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000055
56 static char ID;
Bruno Cardoso Lopesfde21cf2010-12-09 17:31:11 +000057 Filler(TargetMachine &tm)
Owen Andersona7aed182010-08-06 18:33:48 +000058 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000059
60 virtual const char *getPassName() const {
61 return "Mips Delay Slot Filler";
62 }
63
64 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
65 bool runOnMachineFunction(MachineFunction &F) {
Akira Hatanaka4773e672012-05-14 23:59:17 +000066 if (SkipDelaySlotFiller)
67 return false;
68
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000069 bool Changed = false;
70 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
71 FI != FE; ++FI)
72 Changed |= runOnMachineBasicBlock(*FI);
73 return Changed;
74 }
75
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000076 bool isDelayFiller(MachineBasicBlock &MBB,
Akira Hatanaka5ac78682012-06-13 23:25:52 +000077 InstrIter candidate);
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000078
Akira Hatanaka5ac78682012-06-13 23:25:52 +000079 void insertCallUses(InstrIter MI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +000080 SmallSet<unsigned, 32> &RegDefs,
81 SmallSet<unsigned, 32> &RegUses);
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000082
Akira Hatanaka5ac78682012-06-13 23:25:52 +000083 void insertDefsUses(InstrIter MI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +000084 SmallSet<unsigned, 32> &RegDefs,
85 SmallSet<unsigned, 32> &RegUses);
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000086
Akira Hatanaka5fd22482012-06-14 21:10:56 +000087 bool IsRegInSet(SmallSet<unsigned, 32> &RegSet,
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000088 unsigned Reg);
89
Akira Hatanaka5ac78682012-06-13 23:25:52 +000090 bool delayHasHazard(InstrIter candidate,
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000091 bool &sawLoad, bool &sawStore,
92 SmallSet<unsigned, 32> &RegDefs,
93 SmallSet<unsigned, 32> &RegUses);
94
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +000095 bool
Akira Hatanaka5ac78682012-06-13 23:25:52 +000096 findDelayInstr(MachineBasicBlock &MBB, InstrIter slot,
97 InstrIter &Filler);
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000098
99
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000100 };
101 char Filler::ID = 0;
102} // end of anonymous namespace
103
104/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000105/// We assume there is only one delay slot per delayed instruction.
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000106bool Filler::
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000107runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000108 bool Changed = false;
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000109 LastFiller = MBB.instr_end();
Akira Hatanakae7b06972011-10-05 01:30:09 +0000110
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000111 for (InstrIter I = MBB.instr_begin(); I != MBB.instr_end(); ++I)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000112 if (I->hasDelaySlot()) {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000113 ++FilledSlots;
114 Changed = true;
Bruno Cardoso Lopesfde21cf2010-12-09 17:31:11 +0000115
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000116 InstrIter D;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000117
Akira Hatanaka9d957842012-08-22 02:51:28 +0000118 if (!DisableDelaySlotFiller && findDelayInstr(MBB, I, D)) {
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000119 MBB.splice(llvm::next(I), &MBB, D);
120 ++UsefulSlots;
Jia Liuf54f60f2012-02-28 07:46:26 +0000121 } else
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000122 BuildMI(MBB, llvm::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
123
Akira Hatanakae7b06972011-10-05 01:30:09 +0000124 // Record the filler instruction that filled the delay slot.
125 // The instruction after it will be visited in the next iteration.
126 LastFiller = ++I;
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000127
128 // Set InsideBundle bit so that the machine verifier doesn't expect this
129 // instruction to be a terminator.
130 LastFiller->setIsInsideBundle();
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000131 }
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000132 return Changed;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000133
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000134}
135
136/// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
137/// slots in Mips MachineFunctions
138FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
139 return new Filler(tm);
140}
141
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000142bool Filler::findDelayInstr(MachineBasicBlock &MBB,
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000143 InstrIter slot,
144 InstrIter &Filler) {
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000145 SmallSet<unsigned, 32> RegDefs;
146 SmallSet<unsigned, 32> RegUses;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000147
Akira Hatanaka7b204682011-10-05 02:04:17 +0000148 insertDefsUses(slot, RegDefs, RegUses);
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000149
Akira Hatanakad9c8aab2011-10-05 01:57:46 +0000150 bool sawLoad = false;
151 bool sawStore = false;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000152
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000153 for (ReverseInstrIter I(slot); I != MBB.instr_rend(); ++I) {
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000154 // skip debug value
155 if (I->isDebugValue())
156 continue;
157
Akira Hatanakad9c8aab2011-10-05 01:57:46 +0000158 // Convert to forward iterator.
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000159 InstrIter FI(llvm::next(I).base());
Akira Hatanakad9c8aab2011-10-05 01:57:46 +0000160
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000161 if (I->hasUnmodeledSideEffects()
162 || I->isInlineAsm()
163 || I->isLabel()
Akira Hatanakad9c8aab2011-10-05 01:57:46 +0000164 || FI == LastFiller
Evan Cheng7f8e5632011-12-07 07:15:52 +0000165 || I->isPseudo()
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000166 //
167 // Should not allow:
168 // ERET, DERET or WAIT, PAUSE. Need to add these to instruction
169 // list. TBD.
170 )
171 break;
172
Akira Hatanakad9c8aab2011-10-05 01:57:46 +0000173 if (delayHasHazard(FI, sawLoad, sawStore, RegDefs, RegUses)) {
174 insertDefsUses(FI, RegDefs, RegUses);
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000175 continue;
176 }
177
Akira Hatanakad9c8aab2011-10-05 01:57:46 +0000178 Filler = FI;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000179 return true;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000180 }
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000181
182 return false;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000183}
184
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000185bool Filler::delayHasHazard(InstrIter candidate,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000186 bool &sawLoad, bool &sawStore,
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000187 SmallSet<unsigned, 32> &RegDefs,
188 SmallSet<unsigned, 32> &RegUses) {
189 if (candidate->isImplicitDef() || candidate->isKill())
190 return true;
191
Akira Hatanaka7d398632011-10-05 01:09:37 +0000192 // Loads or stores cannot be moved past a store to the delay slot
Jia Liuf54f60f2012-02-28 07:46:26 +0000193 // and stores cannot be moved past a load.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000194 if (candidate->mayLoad()) {
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000195 if (sawStore)
196 return true;
Akira Hatanaka7d398632011-10-05 01:09:37 +0000197 sawLoad = true;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000198 }
199
Evan Cheng7f8e5632011-12-07 07:15:52 +0000200 if (candidate->mayStore()) {
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000201 if (sawStore)
202 return true;
203 sawStore = true;
204 if (sawLoad)
205 return true;
206 }
207
Evan Cheng7f8e5632011-12-07 07:15:52 +0000208 assert((!candidate->isCall() && !candidate->isReturn()) &&
Akira Hatanakac6b742f2011-10-05 18:17:49 +0000209 "Cannot put calls or returns in delay slot.");
Akira Hatanaka0d7dfc02011-10-05 02:18:58 +0000210
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000211 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
212 const MachineOperand &MO = candidate->getOperand(i);
Akira Hatanaka0d7dfc02011-10-05 02:18:58 +0000213 unsigned Reg;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000214
Akira Hatanaka0d7dfc02011-10-05 02:18:58 +0000215 if (!MO.isReg() || !(Reg = MO.getReg()))
216 continue; // skip
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000217
218 if (MO.isDef()) {
219 // check whether Reg is defined or used before delay slot.
220 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
221 return true;
222 }
223 if (MO.isUse()) {
224 // check whether Reg is defined before delay slot.
225 if (IsRegInSet(RegDefs, Reg))
226 return true;
227 }
228 }
229 return false;
230}
231
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000232// Insert Defs and Uses of MI into the sets RegDefs and RegUses.
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000233void Filler::insertDefsUses(InstrIter MI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000234 SmallSet<unsigned, 32> &RegDefs,
235 SmallSet<unsigned, 32> &RegUses) {
Akira Hatanaka0d7dfc02011-10-05 02:18:58 +0000236 // If MI is a call or return, just examine the explicit non-variadic operands.
Akira Hatanaka8e532eb2011-10-05 02:21:58 +0000237 MCInstrDesc MCID = MI->getDesc();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000238 unsigned e = MI->isCall() || MI->isReturn() ? MCID.getNumOperands() :
239 MI->getNumOperands();
Jia Liuf54f60f2012-02-28 07:46:26 +0000240
241 // Add RA to RegDefs to prevent users of RA from going into delay slot.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000242 if (MI->isCall())
Akira Hatanaka14e41492011-10-05 18:11:44 +0000243 RegDefs.insert(Mips::RA);
Akira Hatanaka7b204682011-10-05 02:04:17 +0000244
245 for (unsigned i = 0; i != e; ++i) {
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000246 const MachineOperand &MO = MI->getOperand(i);
Akira Hatanaka7b204682011-10-05 02:04:17 +0000247 unsigned Reg;
248
249 if (!MO.isReg() || !(Reg = MO.getReg()))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000250 continue;
251
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000252 if (MO.isDef())
253 RegDefs.insert(Reg);
Akira Hatanaka7b204682011-10-05 02:04:17 +0000254 else if (MO.isUse())
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000255 RegUses.insert(Reg);
256 }
257}
258
259//returns true if the Reg or its alias is in the RegSet.
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000260bool Filler::IsRegInSet(SmallSet<unsigned, 32> &RegSet, unsigned Reg) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000261 // Check Reg and all aliased Registers.
262 for (MCRegAliasIterator AI(Reg, TM.getRegisterInfo(), true);
263 AI.isValid(); ++AI)
264 if (RegSet.count(*AI))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000265 return true;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000266 return false;
267}