blob: ee292758d186166c0979247c364552c85f6a7470 [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===-- DelaySlotFiller.cpp - SPARC delay slot filler ---------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaeke20117102004-04-06 23:21:45 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaeke20117102004-04-06 23:21:45 +00008//===----------------------------------------------------------------------===//
9//
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000010// This is a simple local pass that attempts to fill delay slots with useful
11// instructions. If no instructions can be moved into the delay slot, then a
12// NOP is placed.
Brian Gaeke20117102004-04-06 23:21:45 +000013//===----------------------------------------------------------------------===//
14
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000015#define DEBUG_TYPE "delay-slot-filler"
Chris Lattner7c90f732006-02-05 05:50:24 +000016#include "Sparc.h"
Brian Gaeke20117102004-04-06 23:21:45 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Brian Gaeke20117102004-04-06 23:21:45 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000019#include "llvm/Support/CommandLine.h"
Brian Gaeke870248b2004-09-30 04:04:47 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/TargetInstrInfo.h"
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000022#include "llvm/Target/TargetRegisterInfo.h"
23#include "llvm/ADT/SmallSet.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000024#include "llvm/ADT/Statistic.h"
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000025
Brian Gaeke20117102004-04-06 23:21:45 +000026using namespace llvm;
27
Chris Lattner95b2c7d2006-12-19 22:59:26 +000028STATISTIC(FilledSlots, "Number of delay slots filled");
Brian Gaeke20117102004-04-06 23:21:45 +000029
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000030static cl::opt<bool> DisableDelaySlotFiller(
31 "disable-sparc-delay-filler",
32 cl::init(false),
33 cl::desc("Disable the Sparc delay slot filler."),
34 cl::Hidden);
35
Chris Lattner95b2c7d2006-12-19 22:59:26 +000036namespace {
Brian Gaeke20117102004-04-06 23:21:45 +000037 struct Filler : public MachineFunctionPass {
38 /// Target machine description which we query for reg. names, data
39 /// layout, etc.
40 ///
41 TargetMachine &TM;
Brian Gaeke870248b2004-09-30 04:04:47 +000042 const TargetInstrInfo *TII;
Brian Gaeke20117102004-04-06 23:21:45 +000043
Devang Patel19974732007-05-03 01:11:54 +000044 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000045 Filler(TargetMachine &tm)
Owen Anderson90c579d2010-08-06 18:33:48 +000046 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
Brian Gaeke20117102004-04-06 23:21:45 +000047
Chris Lattner7c90f732006-02-05 05:50:24 +000048 virtual const char *getPassName() const {
49 return "SPARC Delay Slot Filler";
Brian Gaeke20117102004-04-06 23:21:45 +000050 }
51
Chris Lattner7c90f732006-02-05 05:50:24 +000052 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
53 bool runOnMachineFunction(MachineFunction &F) {
Brian Gaeke20117102004-04-06 23:21:45 +000054 bool Changed = false;
Chris Lattner7c90f732006-02-05 05:50:24 +000055 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
Brian Gaeke20117102004-04-06 23:21:45 +000056 FI != FE; ++FI)
Chris Lattner7c90f732006-02-05 05:50:24 +000057 Changed |= runOnMachineBasicBlock(*FI);
Brian Gaeke20117102004-04-06 23:21:45 +000058 return Changed;
59 }
60
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000061 bool isDelayFiller(MachineBasicBlock &MBB,
62 MachineBasicBlock::iterator candidate);
63
64 void insertCallUses(MachineBasicBlock::iterator MI,
65 SmallSet<unsigned, 32>& RegUses);
66
67 void insertDefsUses(MachineBasicBlock::iterator MI,
68 SmallSet<unsigned, 32>& RegDefs,
69 SmallSet<unsigned, 32>& RegUses);
70
71 bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
72 unsigned Reg);
73
74 bool delayHasHazard(MachineBasicBlock::iterator candidate,
75 bool &sawLoad, bool &sawStore,
76 SmallSet<unsigned, 32> &RegDefs,
77 SmallSet<unsigned, 32> &RegUses);
78
79 MachineBasicBlock::iterator
80 findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot);
81
82
Brian Gaeke20117102004-04-06 23:21:45 +000083 };
Devang Patel19974732007-05-03 01:11:54 +000084 char Filler::ID = 0;
Brian Gaeke20117102004-04-06 23:21:45 +000085} // end of anonymous namespace
86
Chris Lattner7c90f732006-02-05 05:50:24 +000087/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
88/// slots in Sparc MachineFunctions
Brian Gaeke20117102004-04-06 23:21:45 +000089///
Chris Lattner7c90f732006-02-05 05:50:24 +000090FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
91 return new Filler(tm);
Brian Gaeke20117102004-04-06 23:21:45 +000092}
93
Brian Gaeke20117102004-04-06 23:21:45 +000094/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000095/// We assume there is only one delay slot per delayed instruction.
Brian Gaeke20117102004-04-06 23:21:45 +000096///
Chris Lattner7c90f732006-02-05 05:50:24 +000097bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Brian Gaeke0f51cc12004-04-07 04:05:12 +000098 bool Changed = false;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000099
Chris Lattner7c90f732006-02-05 05:50:24 +0000100 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
Chris Lattner749c6f62008-01-07 07:27:27 +0000101 if (I->getDesc().hasDelaySlot()) {
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000102 MachineBasicBlock::iterator D = MBB.end();
Brian Gaeke20117102004-04-06 23:21:45 +0000103 MachineBasicBlock::iterator J = I;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000104
105 if (!DisableDelaySlotFiller)
106 D = findDelayInstr(MBB, I);
107
Brian Gaeke20117102004-04-06 23:21:45 +0000108 ++FilledSlots;
Brian Gaeke0f51cc12004-04-07 04:05:12 +0000109 Changed = true;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000110
111 if (D == MBB.end())
112 BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(SP::NOP));
113 else
114 MBB.splice(++J, &MBB, D);
Brian Gaeke20117102004-04-06 23:21:45 +0000115 }
Brian Gaeke0f51cc12004-04-07 04:05:12 +0000116 return Changed;
Brian Gaeke20117102004-04-06 23:21:45 +0000117}
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000118
119MachineBasicBlock::iterator
120Filler::findDelayInstr(MachineBasicBlock &MBB,
121 MachineBasicBlock::iterator slot)
122{
123 SmallSet<unsigned, 32> RegDefs;
124 SmallSet<unsigned, 32> RegUses;
125 bool sawLoad = false;
126 bool sawStore = false;
127
128 MachineBasicBlock::iterator I = slot;
129
130 if (slot->getOpcode() == SP::RET)
131 return MBB.end();
132
133 if (slot->getOpcode() == SP::RETL) {
134 --I;
135 if (I->getOpcode() != SP::RESTORErr)
136 return MBB.end();
137 //change retl to ret
138 slot->setDesc(TII->get(SP::RET));
139 return I;
140 }
141
142 //Call's delay filler can def some of call's uses.
143 if (slot->getDesc().isCall())
144 insertCallUses(slot, RegUses);
145 else
146 insertDefsUses(slot, RegDefs, RegUses);
147
148 bool done = false;
149
150 while (!done) {
151 done = (I == MBB.begin());
152
153 if (!done)
154 --I;
155
156 // skip debug value
157 if (I->isDebugValue())
158 continue;
159
160
161 if (I->hasUnmodeledSideEffects()
162 || I->isInlineAsm()
163 || I->isLabel()
164 || I->getDesc().hasDelaySlot()
165 || isDelayFiller(MBB, I))
166 break;
167
168 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
169 insertDefsUses(I, RegDefs, RegUses);
170 continue;
171 }
172
173 return I;
174 }
175 return MBB.end();
176}
177
178bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
179 bool &sawLoad,
180 bool &sawStore,
181 SmallSet<unsigned, 32> &RegDefs,
182 SmallSet<unsigned, 32> &RegUses)
183{
184
Venkatraman Govindarajucc5bd4a2011-02-12 19:02:33 +0000185 if (candidate->isImplicitDef() || candidate->isKill())
186 return true;
187
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000188 if (candidate->getDesc().mayLoad()) {
189 sawLoad = true;
190 if (sawStore)
191 return true;
192 }
193
194 if (candidate->getDesc().mayStore()) {
195 if (sawStore)
196 return true;
197 sawStore = true;
198 if (sawLoad)
199 return true;
200 }
201
202 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
203 const MachineOperand &MO = candidate->getOperand(i);
204 if (!MO.isReg())
205 continue; // skip
206
207 unsigned Reg = MO.getReg();
208
209 if (MO.isDef()) {
210 //check whether Reg is defined or used before delay slot.
211 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
212 return true;
213 }
214 if (MO.isUse()) {
215 //check whether Reg is defined before delay slot.
216 if (IsRegInSet(RegDefs, Reg))
217 return true;
218 }
219 }
220 return false;
221}
222
223
224void Filler::insertCallUses(MachineBasicBlock::iterator MI,
225 SmallSet<unsigned, 32>& RegUses)
226{
227
228 switch(MI->getOpcode()) {
229 default: llvm_unreachable("Unknown opcode.");
230 case SP::CALL: break;
231 case SP::JMPLrr:
232 case SP::JMPLri:
233 assert(MI->getNumOperands() >= 2);
234 const MachineOperand &Reg = MI->getOperand(0);
235 assert(Reg.isReg() && "JMPL first operand is not a register.");
236 assert(Reg.isUse() && "JMPL first operand is not a use.");
237 RegUses.insert(Reg.getReg());
238
239 const MachineOperand &RegOrImm = MI->getOperand(1);
240 if (RegOrImm.isImm())
241 break;
242 assert(RegOrImm.isReg() && "JMPLrr second operand is not a register.");
243 assert(RegOrImm.isUse() && "JMPLrr second operand is not a use.");
244 RegUses.insert(RegOrImm.getReg());
245 break;
246 }
247}
248
249//Insert Defs and Uses of MI into the sets RegDefs and RegUses.
250void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
251 SmallSet<unsigned, 32>& RegDefs,
252 SmallSet<unsigned, 32>& RegUses)
253{
254 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
255 const MachineOperand &MO = MI->getOperand(i);
256 if (!MO.isReg())
257 continue;
258
259 unsigned Reg = MO.getReg();
260 if (Reg == 0)
261 continue;
262 if (MO.isDef())
263 RegDefs.insert(Reg);
264 if (MO.isUse())
265 RegUses.insert(Reg);
266
267 }
268}
269
270//returns true if the Reg or its alias is in the RegSet.
271bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
272{
273 if (RegSet.count(Reg))
274 return true;
275 // check Aliased Registers
276 for (const unsigned *Alias = TM.getRegisterInfo()->getAliasSet(Reg);
277 *Alias; ++ Alias)
278 if (RegSet.count(*Alias))
279 return true;
280
281 return false;
282}
283
284// return true if the candidate is a delay filler.
285bool Filler::isDelayFiller(MachineBasicBlock &MBB,
286 MachineBasicBlock::iterator candidate)
287{
288 if (candidate == MBB.begin())
289 return false;
290 const TargetInstrDesc &prevdesc = (--candidate)->getDesc();
291 return prevdesc.hasDelaySlot();
292}