blob: db3f15900bbc4143f6c17b4f6997c6d957016650 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/SmallSet.h"
18#include "llvm/ADT/Statistic.h"
Brian Gaeke20117102004-04-06 23:21:45 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Brian Gaeke20117102004-04-06 23:21:45 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000021#include "llvm/Support/CommandLine.h"
Brian Gaeke870248b2004-09-30 04:04:47 +000022#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Target/TargetMachine.h"
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000024#include "llvm/Target/TargetRegisterInfo.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
Venkatraman Govindarajud6b4caf2013-05-16 23:53:29 +000064 void insertCallDefsUses(MachineBasicBlock::iterator MI,
65 SmallSet<unsigned, 32>& RegDefs,
66 SmallSet<unsigned, 32>& RegUses);
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000067
68 void insertDefsUses(MachineBasicBlock::iterator MI,
69 SmallSet<unsigned, 32>& RegDefs,
70 SmallSet<unsigned, 32>& RegUses);
71
72 bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
73 unsigned Reg);
74
75 bool delayHasHazard(MachineBasicBlock::iterator candidate,
76 bool &sawLoad, bool &sawStore,
77 SmallSet<unsigned, 32> &RegDefs,
78 SmallSet<unsigned, 32> &RegUses);
79
80 MachineBasicBlock::iterator
81 findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot);
82
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +000083 bool needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize);
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000084
Brian Gaeke20117102004-04-06 23:21:45 +000085 };
Devang Patel19974732007-05-03 01:11:54 +000086 char Filler::ID = 0;
Brian Gaeke20117102004-04-06 23:21:45 +000087} // end of anonymous namespace
88
Chris Lattner7c90f732006-02-05 05:50:24 +000089/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
90/// slots in Sparc MachineFunctions
Brian Gaeke20117102004-04-06 23:21:45 +000091///
Chris Lattner7c90f732006-02-05 05:50:24 +000092FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
93 return new Filler(tm);
Brian Gaeke20117102004-04-06 23:21:45 +000094}
95
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +000096
Brian Gaeke20117102004-04-06 23:21:45 +000097/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +000098/// We assume there is only one delay slot per delayed instruction.
Brian Gaeke20117102004-04-06 23:21:45 +000099///
Chris Lattner7c90f732006-02-05 05:50:24 +0000100bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Brian Gaeke0f51cc12004-04-07 04:05:12 +0000101 bool Changed = false;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000102
Chris Lattner7c90f732006-02-05 05:50:24 +0000103 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000104 if (I->hasDelaySlot()) {
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000105 MachineBasicBlock::iterator D = MBB.end();
Brian Gaeke20117102004-04-06 23:21:45 +0000106 MachineBasicBlock::iterator J = I;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000107
108 if (!DisableDelaySlotFiller)
109 D = findDelayInstr(MBB, I);
110
Brian Gaeke20117102004-04-06 23:21:45 +0000111 ++FilledSlots;
Brian Gaeke0f51cc12004-04-07 04:05:12 +0000112 Changed = true;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000113
114 if (D == MBB.end())
115 BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(SP::NOP));
116 else
117 MBB.splice(++J, &MBB, D);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000118 unsigned structSize = 0;
119 if (needsUnimp(I, structSize)) {
120 MachineBasicBlock::iterator J = I;
121 ++J; //skip the delay filler.
122 BuildMI(MBB, ++J, I->getDebugLoc(),
123 TII->get(SP::UNIMP)).addImm(structSize);
124 }
Brian Gaeke20117102004-04-06 23:21:45 +0000125 }
Brian Gaeke0f51cc12004-04-07 04:05:12 +0000126 return Changed;
Brian Gaeke20117102004-04-06 23:21:45 +0000127}
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000128
129MachineBasicBlock::iterator
130Filler::findDelayInstr(MachineBasicBlock &MBB,
131 MachineBasicBlock::iterator slot)
132{
133 SmallSet<unsigned, 32> RegDefs;
134 SmallSet<unsigned, 32> RegUses;
135 bool sawLoad = false;
136 bool sawStore = false;
137
138 MachineBasicBlock::iterator I = slot;
139
140 if (slot->getOpcode() == SP::RET)
141 return MBB.end();
142
143 if (slot->getOpcode() == SP::RETL) {
144 --I;
145 if (I->getOpcode() != SP::RESTORErr)
146 return MBB.end();
147 //change retl to ret
148 slot->setDesc(TII->get(SP::RET));
149 return I;
150 }
151
152 //Call's delay filler can def some of call's uses.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000153 if (slot->isCall())
Venkatraman Govindarajud6b4caf2013-05-16 23:53:29 +0000154 insertCallDefsUses(slot, RegDefs, RegUses);
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000155 else
156 insertDefsUses(slot, RegDefs, RegUses);
157
158 bool done = false;
159
160 while (!done) {
161 done = (I == MBB.begin());
162
163 if (!done)
164 --I;
165
166 // skip debug value
167 if (I->isDebugValue())
168 continue;
169
170
171 if (I->hasUnmodeledSideEffects()
172 || I->isInlineAsm()
173 || I->isLabel()
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000174 || I->hasDelaySlot()
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000175 || isDelayFiller(MBB, I))
176 break;
177
178 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
179 insertDefsUses(I, RegDefs, RegUses);
180 continue;
181 }
182
183 return I;
184 }
185 return MBB.end();
186}
187
188bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
189 bool &sawLoad,
190 bool &sawStore,
191 SmallSet<unsigned, 32> &RegDefs,
192 SmallSet<unsigned, 32> &RegUses)
193{
194
Venkatraman Govindarajucc5bd4a2011-02-12 19:02:33 +0000195 if (candidate->isImplicitDef() || candidate->isKill())
196 return true;
197
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000198 if (candidate->mayLoad()) {
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000199 sawLoad = true;
200 if (sawStore)
201 return true;
202 }
203
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000204 if (candidate->mayStore()) {
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000205 if (sawStore)
206 return true;
207 sawStore = true;
208 if (sawLoad)
209 return true;
210 }
211
212 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
213 const MachineOperand &MO = candidate->getOperand(i);
214 if (!MO.isReg())
215 continue; // skip
216
217 unsigned Reg = MO.getReg();
218
219 if (MO.isDef()) {
220 //check whether Reg is defined or used before delay slot.
221 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
222 return true;
223 }
224 if (MO.isUse()) {
225 //check whether Reg is defined before delay slot.
226 if (IsRegInSet(RegDefs, Reg))
227 return true;
228 }
229 }
230 return false;
231}
232
233
Venkatraman Govindarajud6b4caf2013-05-16 23:53:29 +0000234void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI,
235 SmallSet<unsigned, 32>& RegDefs,
236 SmallSet<unsigned, 32>& RegUses)
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000237{
Venkatraman Govindarajud6b4caf2013-05-16 23:53:29 +0000238 //Call defines o7, which is visible to the instruction in delay slot.
239 RegDefs.insert(SP::O7);
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000240
241 switch(MI->getOpcode()) {
242 default: llvm_unreachable("Unknown opcode.");
243 case SP::CALL: break;
244 case SP::JMPLrr:
245 case SP::JMPLri:
246 assert(MI->getNumOperands() >= 2);
247 const MachineOperand &Reg = MI->getOperand(0);
248 assert(Reg.isReg() && "JMPL first operand is not a register.");
249 assert(Reg.isUse() && "JMPL first operand is not a use.");
250 RegUses.insert(Reg.getReg());
251
252 const MachineOperand &RegOrImm = MI->getOperand(1);
253 if (RegOrImm.isImm())
254 break;
255 assert(RegOrImm.isReg() && "JMPLrr second operand is not a register.");
256 assert(RegOrImm.isUse() && "JMPLrr second operand is not a use.");
257 RegUses.insert(RegOrImm.getReg());
258 break;
259 }
260}
261
262//Insert Defs and Uses of MI into the sets RegDefs and RegUses.
263void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
264 SmallSet<unsigned, 32>& RegDefs,
265 SmallSet<unsigned, 32>& RegUses)
266{
267 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
268 const MachineOperand &MO = MI->getOperand(i);
269 if (!MO.isReg())
270 continue;
271
272 unsigned Reg = MO.getReg();
273 if (Reg == 0)
274 continue;
275 if (MO.isDef())
276 RegDefs.insert(Reg);
277 if (MO.isUse())
278 RegUses.insert(Reg);
279
280 }
281}
282
283//returns true if the Reg or its alias is in the RegSet.
284bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
285{
Jakob Stoklund Olesenf152fe82012-06-01 20:36:54 +0000286 // Check Reg and all aliased Registers.
287 for (MCRegAliasIterator AI(Reg, TM.getRegisterInfo(), true);
288 AI.isValid(); ++AI)
289 if (RegSet.count(*AI))
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000290 return true;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000291 return false;
292}
293
294// return true if the candidate is a delay filler.
295bool Filler::isDelayFiller(MachineBasicBlock &MBB,
296 MachineBasicBlock::iterator candidate)
297{
298 if (candidate == MBB.begin())
299 return false;
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000300 if (candidate->getOpcode() == SP::UNIMP)
301 return true;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000302 --candidate;
303 return candidate->hasDelaySlot();
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000304}
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000305
306bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
307{
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000308 if (!I->isCall())
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000309 return false;
310
311 unsigned structSizeOpNum = 0;
312 switch (I->getOpcode()) {
313 default: llvm_unreachable("Unknown call opcode.");
314 case SP::CALL: structSizeOpNum = 1; break;
315 case SP::JMPLrr:
316 case SP::JMPLri: structSizeOpNum = 2; break;
317 }
318
319 const MachineOperand &MO = I->getOperand(structSizeOpNum);
320 if (!MO.isImm())
321 return false;
322 StructSize = MO.getImm();
323 return true;
324}