blob: 9961232cbcc391998bfb7af2309927e87b86b5a0 [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
Venkatraman Govindaraju53008692013-05-29 04:46:31 +0000138 if (slot == MBB.begin())
139 return MBB.end();
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000140
141 if (slot->getOpcode() == SP::RET)
142 return MBB.end();
143
144 if (slot->getOpcode() == SP::RETL) {
Venkatraman Govindaraju53008692013-05-29 04:46:31 +0000145 MachineBasicBlock::iterator J = slot;
146 --J;
147
148 if (J->getOpcode() == SP::RESTORErr
149 || J->getOpcode() == SP::RESTOREri) {
150 //change retl to ret
151 slot->setDesc(TII->get(SP::RET));
152 return J;
153 }
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000154 }
155
156 //Call's delay filler can def some of call's uses.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000157 if (slot->isCall())
Venkatraman Govindarajud6b4caf2013-05-16 23:53:29 +0000158 insertCallDefsUses(slot, RegDefs, RegUses);
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000159 else
160 insertDefsUses(slot, RegDefs, RegUses);
161
162 bool done = false;
163
Venkatraman Govindaraju53008692013-05-29 04:46:31 +0000164 MachineBasicBlock::iterator I = slot;
165
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000166 while (!done) {
167 done = (I == MBB.begin());
168
169 if (!done)
170 --I;
171
172 // skip debug value
173 if (I->isDebugValue())
174 continue;
175
176
177 if (I->hasUnmodeledSideEffects()
178 || I->isInlineAsm()
179 || I->isLabel()
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000180 || I->hasDelaySlot()
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000181 || isDelayFiller(MBB, I))
182 break;
183
184 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
185 insertDefsUses(I, RegDefs, RegUses);
186 continue;
187 }
188
189 return I;
190 }
191 return MBB.end();
192}
193
194bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
195 bool &sawLoad,
196 bool &sawStore,
197 SmallSet<unsigned, 32> &RegDefs,
198 SmallSet<unsigned, 32> &RegUses)
199{
200
Venkatraman Govindarajucc5bd4a2011-02-12 19:02:33 +0000201 if (candidate->isImplicitDef() || candidate->isKill())
202 return true;
203
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000204 if (candidate->mayLoad()) {
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000205 sawLoad = true;
206 if (sawStore)
207 return true;
208 }
209
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000210 if (candidate->mayStore()) {
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000211 if (sawStore)
212 return true;
213 sawStore = true;
214 if (sawLoad)
215 return true;
216 }
217
218 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
219 const MachineOperand &MO = candidate->getOperand(i);
220 if (!MO.isReg())
221 continue; // skip
222
223 unsigned Reg = MO.getReg();
224
225 if (MO.isDef()) {
226 //check whether Reg is defined or used before delay slot.
227 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
228 return true;
229 }
230 if (MO.isUse()) {
231 //check whether Reg is defined before delay slot.
232 if (IsRegInSet(RegDefs, Reg))
233 return true;
234 }
235 }
236 return false;
237}
238
239
Venkatraman Govindarajud6b4caf2013-05-16 23:53:29 +0000240void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI,
241 SmallSet<unsigned, 32>& RegDefs,
242 SmallSet<unsigned, 32>& RegUses)
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000243{
Venkatraman Govindarajud6b4caf2013-05-16 23:53:29 +0000244 //Call defines o7, which is visible to the instruction in delay slot.
245 RegDefs.insert(SP::O7);
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000246
247 switch(MI->getOpcode()) {
248 default: llvm_unreachable("Unknown opcode.");
249 case SP::CALL: break;
250 case SP::JMPLrr:
251 case SP::JMPLri:
252 assert(MI->getNumOperands() >= 2);
253 const MachineOperand &Reg = MI->getOperand(0);
254 assert(Reg.isReg() && "JMPL first operand is not a register.");
255 assert(Reg.isUse() && "JMPL first operand is not a use.");
256 RegUses.insert(Reg.getReg());
257
258 const MachineOperand &RegOrImm = MI->getOperand(1);
259 if (RegOrImm.isImm())
260 break;
261 assert(RegOrImm.isReg() && "JMPLrr second operand is not a register.");
262 assert(RegOrImm.isUse() && "JMPLrr second operand is not a use.");
263 RegUses.insert(RegOrImm.getReg());
264 break;
265 }
266}
267
268//Insert Defs and Uses of MI into the sets RegDefs and RegUses.
269void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
270 SmallSet<unsigned, 32>& RegDefs,
271 SmallSet<unsigned, 32>& RegUses)
272{
273 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
274 const MachineOperand &MO = MI->getOperand(i);
275 if (!MO.isReg())
276 continue;
277
278 unsigned Reg = MO.getReg();
279 if (Reg == 0)
280 continue;
281 if (MO.isDef())
282 RegDefs.insert(Reg);
Venkatraman Govindaraju53008692013-05-29 04:46:31 +0000283 if (MO.isUse()) {
284 //Implicit register uses of retl are return values and
285 //retl does not use them.
286 if (MO.isImplicit() && MI->getOpcode() == SP::RETL)
287 continue;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000288 RegUses.insert(Reg);
Venkatraman Govindaraju53008692013-05-29 04:46:31 +0000289 }
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000290 }
291}
292
293//returns true if the Reg or its alias is in the RegSet.
294bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
295{
Jakob Stoklund Olesenf152fe82012-06-01 20:36:54 +0000296 // Check Reg and all aliased Registers.
297 for (MCRegAliasIterator AI(Reg, TM.getRegisterInfo(), true);
298 AI.isValid(); ++AI)
299 if (RegSet.count(*AI))
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000300 return true;
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000301 return false;
302}
303
304// return true if the candidate is a delay filler.
305bool Filler::isDelayFiller(MachineBasicBlock &MBB,
306 MachineBasicBlock::iterator candidate)
307{
308 if (candidate == MBB.begin())
309 return false;
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000310 if (candidate->getOpcode() == SP::UNIMP)
311 return true;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000312 --candidate;
313 return candidate->hasDelaySlot();
Venkatraman Govindaraju71e39da2011-01-20 05:08:26 +0000314}
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000315
316bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
317{
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000318 if (!I->isCall())
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000319 return false;
320
321 unsigned structSizeOpNum = 0;
322 switch (I->getOpcode()) {
323 default: llvm_unreachable("Unknown call opcode.");
324 case SP::CALL: structSizeOpNum = 1; break;
325 case SP::JMPLrr:
326 case SP::JMPLri: structSizeOpNum = 2; break;
327 }
328
329 const MachineOperand &MO = I->getOperand(structSizeOpNum);
330 if (!MO.isImm())
331 return false;
332 StructSize = MO.getImm();
333 return true;
334}