blob: dab35e5e4e6f2b7006a64e2b1df4edffe70f053a [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
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +000082 bool needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize);
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000083
Brian Gaeke20117102004-04-06 23:21:45 +000084 };
Devang Patel19974732007-05-03 01:11:54 +000085 char Filler::ID = 0;
Brian Gaeke20117102004-04-06 23:21:45 +000086} // end of anonymous namespace
87
Chris Lattner7c90f732006-02-05 05:50:24 +000088/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
89/// slots in Sparc MachineFunctions
Brian Gaeke20117102004-04-06 23:21:45 +000090///
Chris Lattner7c90f732006-02-05 05:50:24 +000091FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
92 return new Filler(tm);
Brian Gaeke20117102004-04-06 23:21:45 +000093}
94
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +000095
Brian Gaeke20117102004-04-06 23:21:45 +000096/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000097/// We assume there is only one delay slot per delayed instruction.
Brian Gaeke20117102004-04-06 23:21:45 +000098///
Chris Lattner7c90f732006-02-05 05:50:24 +000099bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Brian Gaeke0f51cc12004-04-07 04:05:12 +0000100 bool Changed = false;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000101
Chris Lattner7c90f732006-02-05 05:50:24 +0000102 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
Chris Lattner749c6f62008-01-07 07:27:27 +0000103 if (I->getDesc().hasDelaySlot()) {
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000104 MachineBasicBlock::iterator D = MBB.end();
Brian Gaeke20117102004-04-06 23:21:45 +0000105 MachineBasicBlock::iterator J = I;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000106
107 if (!DisableDelaySlotFiller)
108 D = findDelayInstr(MBB, I);
109
Brian Gaeke20117102004-04-06 23:21:45 +0000110 ++FilledSlots;
Brian Gaeke0f51cc12004-04-07 04:05:12 +0000111 Changed = true;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000112
113 if (D == MBB.end())
114 BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(SP::NOP));
115 else
116 MBB.splice(++J, &MBB, D);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000117 unsigned structSize = 0;
118 if (needsUnimp(I, structSize)) {
119 MachineBasicBlock::iterator J = I;
120 ++J; //skip the delay filler.
121 BuildMI(MBB, ++J, I->getDebugLoc(),
122 TII->get(SP::UNIMP)).addImm(structSize);
123 }
Brian Gaeke20117102004-04-06 23:21:45 +0000124 }
Brian Gaeke0f51cc12004-04-07 04:05:12 +0000125 return Changed;
Brian Gaeke20117102004-04-06 23:21:45 +0000126}
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000127
128MachineBasicBlock::iterator
129Filler::findDelayInstr(MachineBasicBlock &MBB,
130 MachineBasicBlock::iterator slot)
131{
132 SmallSet<unsigned, 32> RegDefs;
133 SmallSet<unsigned, 32> RegUses;
134 bool sawLoad = false;
135 bool sawStore = false;
136
137 MachineBasicBlock::iterator I = slot;
138
139 if (slot->getOpcode() == SP::RET)
140 return MBB.end();
141
142 if (slot->getOpcode() == SP::RETL) {
143 --I;
144 if (I->getOpcode() != SP::RESTORErr)
145 return MBB.end();
146 //change retl to ret
147 slot->setDesc(TII->get(SP::RET));
148 return I;
149 }
150
151 //Call's delay filler can def some of call's uses.
152 if (slot->getDesc().isCall())
153 insertCallUses(slot, RegUses);
154 else
155 insertDefsUses(slot, RegDefs, RegUses);
156
157 bool done = false;
158
159 while (!done) {
160 done = (I == MBB.begin());
161
162 if (!done)
163 --I;
164
165 // skip debug value
166 if (I->isDebugValue())
167 continue;
168
169
170 if (I->hasUnmodeledSideEffects()
171 || I->isInlineAsm()
172 || I->isLabel()
173 || I->getDesc().hasDelaySlot()
174 || isDelayFiller(MBB, I))
175 break;
176
177 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
178 insertDefsUses(I, RegDefs, RegUses);
179 continue;
180 }
181
182 return I;
183 }
184 return MBB.end();
185}
186
187bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
188 bool &sawLoad,
189 bool &sawStore,
190 SmallSet<unsigned, 32> &RegDefs,
191 SmallSet<unsigned, 32> &RegUses)
192{
193
Venkatraman Govindarajucc5bd4a2011-02-12 19:02:33 +0000194 if (candidate->isImplicitDef() || candidate->isKill())
195 return true;
196
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000197 if (candidate->getDesc().mayLoad()) {
198 sawLoad = true;
199 if (sawStore)
200 return true;
201 }
202
203 if (candidate->getDesc().mayStore()) {
204 if (sawStore)
205 return true;
206 sawStore = true;
207 if (sawLoad)
208 return true;
209 }
210
211 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
212 const MachineOperand &MO = candidate->getOperand(i);
213 if (!MO.isReg())
214 continue; // skip
215
216 unsigned Reg = MO.getReg();
217
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
232
233void Filler::insertCallUses(MachineBasicBlock::iterator MI,
234 SmallSet<unsigned, 32>& RegUses)
235{
236
237 switch(MI->getOpcode()) {
238 default: llvm_unreachable("Unknown opcode.");
239 case SP::CALL: break;
240 case SP::JMPLrr:
241 case SP::JMPLri:
242 assert(MI->getNumOperands() >= 2);
243 const MachineOperand &Reg = MI->getOperand(0);
244 assert(Reg.isReg() && "JMPL first operand is not a register.");
245 assert(Reg.isUse() && "JMPL first operand is not a use.");
246 RegUses.insert(Reg.getReg());
247
248 const MachineOperand &RegOrImm = MI->getOperand(1);
249 if (RegOrImm.isImm())
250 break;
251 assert(RegOrImm.isReg() && "JMPLrr second operand is not a register.");
252 assert(RegOrImm.isUse() && "JMPLrr second operand is not a use.");
253 RegUses.insert(RegOrImm.getReg());
254 break;
255 }
256}
257
258//Insert Defs and Uses of MI into the sets RegDefs and RegUses.
259void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
260 SmallSet<unsigned, 32>& RegDefs,
261 SmallSet<unsigned, 32>& RegUses)
262{
263 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
264 const MachineOperand &MO = MI->getOperand(i);
265 if (!MO.isReg())
266 continue;
267
268 unsigned Reg = MO.getReg();
269 if (Reg == 0)
270 continue;
271 if (MO.isDef())
272 RegDefs.insert(Reg);
273 if (MO.isUse())
274 RegUses.insert(Reg);
275
276 }
277}
278
279//returns true if the Reg or its alias is in the RegSet.
280bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
281{
282 if (RegSet.count(Reg))
283 return true;
284 // check Aliased Registers
285 for (const unsigned *Alias = TM.getRegisterInfo()->getAliasSet(Reg);
286 *Alias; ++ Alias)
287 if (RegSet.count(*Alias))
288 return true;
289
290 return false;
291}
292
293// return true if the candidate is a delay filler.
294bool Filler::isDelayFiller(MachineBasicBlock &MBB,
295 MachineBasicBlock::iterator candidate)
296{
297 if (candidate == MBB.begin())
298 return false;
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000299 if (candidate->getOpcode() == SP::UNIMP)
300 return true;
Evan Chenge837dea2011-06-28 19:10:37 +0000301 const MCInstrDesc &prevdesc = (--candidate)->getDesc();
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000302 return prevdesc.hasDelaySlot();
303}
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000304
305bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
306{
307 if (!I->getDesc().isCall())
308 return false;
309
310 unsigned structSizeOpNum = 0;
311 switch (I->getOpcode()) {
312 default: llvm_unreachable("Unknown call opcode.");
313 case SP::CALL: structSizeOpNum = 1; break;
314 case SP::JMPLrr:
315 case SP::JMPLri: structSizeOpNum = 2; break;
316 }
317
318 const MachineOperand &MO = I->getOperand(structSizeOpNum);
319 if (!MO.isImm())
320 return false;
321 StructSize = MO.getImm();
322 return true;
323}