blob: 6f9cc314e37648367f4b13e9c1757057d109f68a [file] [log] [blame]
Chris Lattner158e1f52006-02-05 05:50:24 +00001//===-- DelaySlotFiller.cpp - SPARC delay slot filler ---------------------===//
2//
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.
Chris Lattner158e1f52006-02-05 05:50:24 +00007//
8//===----------------------------------------------------------------------===//
9//
Venkatraman Govindaraju058e1242011-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.
Chris Lattner158e1f52006-02-05 05:50:24 +000013//===----------------------------------------------------------------------===//
14
15#include "Sparc.h"
Venkatraman Govindarajuf482d3d2013-10-06 07:06:44 +000016#include "SparcSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallSet.h"
18#include "llvm/ADT/Statistic.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
Venkatraman Govindaraju06532182014-01-11 19:38:03 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000023#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Target/TargetMachine.h"
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +000025#include "llvm/Target/TargetRegisterInfo.h"
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +000026
Chris Lattner158e1f52006-02-05 05:50:24 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "delay-slot-filler"
30
Chris Lattner1ef9cd42006-12-19 22:59:26 +000031STATISTIC(FilledSlots, "Number of delay slots filled");
Chris Lattner158e1f52006-02-05 05:50:24 +000032
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +000033static cl::opt<bool> DisableDelaySlotFiller(
34 "disable-sparc-delay-filler",
35 cl::init(false),
36 cl::desc("Disable the Sparc delay slot filler."),
37 cl::Hidden);
38
Chris Lattner1ef9cd42006-12-19 22:59:26 +000039namespace {
Chris Lattner158e1f52006-02-05 05:50:24 +000040 struct Filler : public MachineFunctionPass {
Venkatraman Govindarajuf482d3d2013-10-06 07:06:44 +000041 const SparcSubtarget *Subtarget;
Chris Lattner158e1f52006-02-05 05:50:24 +000042
Devang Patel8c78a0b2007-05-03 01:11:54 +000043 static char ID;
Benjamin Kramer4ec6e9d2016-05-27 10:19:03 +000044 Filler() : MachineFunctionPass(ID) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000045
Mehdi Amini117296c2016-10-01 02:56:57 +000046 StringRef getPassName() const override { return "SPARC Delay Slot Filler"; }
Chris Lattner158e1f52006-02-05 05:50:24 +000047
48 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
Craig Topperb0c941b2014-04-29 07:57:13 +000049 bool runOnMachineFunction(MachineFunction &F) override {
Chris Lattner158e1f52006-02-05 05:50:24 +000050 bool Changed = false;
Eric Christopherf5e94062015-01-30 23:46:43 +000051 Subtarget = &F.getSubtarget<SparcSubtarget>();
Venkatraman Govindaraju06532182014-01-11 19:38:03 +000052
53 // This pass invalidates liveness information when it reorders
54 // instructions to fill delay slot.
55 F.getRegInfo().invalidateLiveness();
56
Chris Lattner158e1f52006-02-05 05:50:24 +000057 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
58 FI != FE; ++FI)
59 Changed |= runOnMachineBasicBlock(*FI);
60 return Changed;
61 }
62
Derek Schuff1dbf7a52016-04-04 17:09:25 +000063 MachineFunctionProperties getRequiredProperties() const override {
64 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000065 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000066 }
67
Venkatraman Govindaraju54bf6112013-05-16 23:53:29 +000068 void insertCallDefsUses(MachineBasicBlock::iterator MI,
69 SmallSet<unsigned, 32>& RegDefs,
70 SmallSet<unsigned, 32>& RegUses);
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +000071
72 void insertDefsUses(MachineBasicBlock::iterator MI,
73 SmallSet<unsigned, 32>& RegDefs,
74 SmallSet<unsigned, 32>& RegUses);
75
76 bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
77 unsigned Reg);
78
79 bool delayHasHazard(MachineBasicBlock::iterator candidate,
80 bool &sawLoad, bool &sawStore,
81 SmallSet<unsigned, 32> &RegDefs,
82 SmallSet<unsigned, 32> &RegUses);
83
84 MachineBasicBlock::iterator
85 findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot);
86
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +000087 bool needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize);
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +000088
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +000089 bool tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
90 MachineBasicBlock::iterator MBBI);
91
Chris Lattner158e1f52006-02-05 05:50:24 +000092 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000093 char Filler::ID = 0;
Chris Lattner158e1f52006-02-05 05:50:24 +000094} // end of anonymous namespace
95
96/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
97/// slots in Sparc MachineFunctions
98///
99FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
Benjamin Kramer4ec6e9d2016-05-27 10:19:03 +0000100 return new Filler;
Chris Lattner158e1f52006-02-05 05:50:24 +0000101}
102
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000103
Chris Lattner158e1f52006-02-05 05:50:24 +0000104/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000105/// We assume there is only one delay slot per delayed instruction.
Chris Lattner158e1f52006-02-05 05:50:24 +0000106///
107bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
108 bool Changed = false;
Eric Christopherf5e94062015-01-30 23:46:43 +0000109 Subtarget = &MBB.getParent()->getSubtarget<SparcSubtarget>();
110 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Venkatraman Govindarajuf482d3d2013-10-06 07:06:44 +0000111
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000112 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
113 MachineBasicBlock::iterator MI = I;
114 ++I;
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000115
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000116 // If MI is restore, try combining it with previous inst.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000117 if (!DisableDelaySlotFiller &&
118 (MI->getOpcode() == SP::RESTORErr
119 || MI->getOpcode() == SP::RESTOREri)) {
120 Changed |= tryCombineRestoreWithPrevInst(MBB, MI);
121 continue;
Chris Lattner158e1f52006-02-05 05:50:24 +0000122 }
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000123
Joerg Sonnenberger48eb19742015-12-03 02:35:24 +0000124 // TODO: If we ever want to support v7, this needs to be extended
125 // to cover all floating point operations.
Venkatraman Govindarajuf482d3d2013-10-06 07:06:44 +0000126 if (!Subtarget->isV9() &&
127 (MI->getOpcode() == SP::FCMPS || MI->getOpcode() == SP::FCMPD
128 || MI->getOpcode() == SP::FCMPQ)) {
129 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
130 Changed = true;
131 continue;
132 }
133
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000134 // If MI has no delay slot, skip.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000135 if (!MI->hasDelaySlot())
136 continue;
137
138 MachineBasicBlock::iterator D = MBB.end();
139
140 if (!DisableDelaySlotFiller)
141 D = findDelayInstr(MBB, MI);
142
143 ++FilledSlots;
144 Changed = true;
145
146 if (D == MBB.end())
147 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
148 else
149 MBB.splice(I, &MBB, D);
150
151 unsigned structSize = 0;
152 if (needsUnimp(MI, structSize)) {
153 MachineBasicBlock::iterator J = MI;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000154 ++J; // skip the delay filler.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000155 assert (J != MBB.end() && "MI needs a delay instruction.");
Venkatraman Govindarajufdcc4982013-07-30 02:26:29 +0000156 BuildMI(MBB, ++J, MI->getDebugLoc(),
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000157 TII->get(SP::UNIMP)).addImm(structSize);
Venkatraman Govindaraju06532182014-01-11 19:38:03 +0000158 // Bundle the delay filler and unimp with the instruction.
159 MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), J);
160 } else {
161 MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), I);
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000162 }
163 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000164 return Changed;
165}
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000166
167MachineBasicBlock::iterator
168Filler::findDelayInstr(MachineBasicBlock &MBB,
169 MachineBasicBlock::iterator slot)
170{
171 SmallSet<unsigned, 32> RegDefs;
172 SmallSet<unsigned, 32> RegUses;
173 bool sawLoad = false;
174 bool sawStore = false;
175
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000176 if (slot == MBB.begin())
177 return MBB.end();
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000178
Venkatraman Govindaraju8223c552013-10-08 02:50:29 +0000179 if (slot->getOpcode() == SP::RET || slot->getOpcode() == SP::TLS_CALL)
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000180 return MBB.end();
181
182 if (slot->getOpcode() == SP::RETL) {
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000183 MachineBasicBlock::iterator J = slot;
184 --J;
185
186 if (J->getOpcode() == SP::RESTORErr
187 || J->getOpcode() == SP::RESTOREri) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000188 // change retl to ret.
Eric Christopherf5e94062015-01-30 23:46:43 +0000189 slot->setDesc(Subtarget->getInstrInfo()->get(SP::RET));
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000190 return J;
191 }
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000192 }
193
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000194 // Call's delay filler can def some of call's uses.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000195 if (slot->isCall())
Venkatraman Govindaraju54bf6112013-05-16 23:53:29 +0000196 insertCallDefsUses(slot, RegDefs, RegUses);
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000197 else
198 insertDefsUses(slot, RegDefs, RegUses);
199
200 bool done = false;
201
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000202 MachineBasicBlock::iterator I = slot;
203
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000204 while (!done) {
205 done = (I == MBB.begin());
206
207 if (!done)
208 --I;
209
210 // skip debug value
211 if (I->isDebugValue())
212 continue;
213
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000214 if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
215 I->hasDelaySlot() || I->isBundledWithSucc())
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000216 break;
217
218 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
219 insertDefsUses(I, RegDefs, RegUses);
220 continue;
221 }
222
223 return I;
224 }
225 return MBB.end();
226}
227
228bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
229 bool &sawLoad,
230 bool &sawStore,
231 SmallSet<unsigned, 32> &RegDefs,
232 SmallSet<unsigned, 32> &RegUses)
233{
234
Venkatraman Govindaraju0c1f6532011-02-12 19:02:33 +0000235 if (candidate->isImplicitDef() || candidate->isKill())
236 return true;
237
Evan Cheng7f8e5632011-12-07 07:15:52 +0000238 if (candidate->mayLoad()) {
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000239 sawLoad = true;
240 if (sawStore)
241 return true;
242 }
243
Evan Cheng7f8e5632011-12-07 07:15:52 +0000244 if (candidate->mayStore()) {
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000245 if (sawStore)
246 return true;
247 sawStore = true;
248 if (sawLoad)
249 return true;
250 }
251
252 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
253 const MachineOperand &MO = candidate->getOperand(i);
254 if (!MO.isReg())
255 continue; // skip
256
257 unsigned Reg = MO.getReg();
258
259 if (MO.isDef()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000260 // check whether Reg is defined or used before delay slot.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000261 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
262 return true;
263 }
264 if (MO.isUse()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000265 // check whether Reg is defined before delay slot.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000266 if (IsRegInSet(RegDefs, Reg))
267 return true;
268 }
269 }
Chris Dewhurst6bc3e132016-05-23 11:52:28 +0000270
271 unsigned Opcode = candidate->getOpcode();
272 // LD and LDD may have NOPs inserted afterwards in the case of some LEON
273 // processors, so we can't use the delay slot if this feature is switched-on.
274 if (Subtarget->insertNOPLoad()
275 &&
276 Opcode >= SP::LDDArr && Opcode <= SP::LDrr)
277 return true;
278
Chris Dewhurstd03d5652016-06-19 12:56:42 +0000279 // Same as above for FDIV and FSQRT on some LEON processors.
280 if (Subtarget->fixAllFDIVSQRT()
281 &&
282 Opcode >= SP::FDIVD && Opcode <= SP::FSQRTD)
283 return true;
284
285
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000286 return false;
287}
288
289
Venkatraman Govindaraju54bf6112013-05-16 23:53:29 +0000290void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI,
291 SmallSet<unsigned, 32>& RegDefs,
292 SmallSet<unsigned, 32>& RegUses)
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000293{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000294 // Call defines o7, which is visible to the instruction in delay slot.
Venkatraman Govindaraju54bf6112013-05-16 23:53:29 +0000295 RegDefs.insert(SP::O7);
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000296
297 switch(MI->getOpcode()) {
298 default: llvm_unreachable("Unknown opcode.");
299 case SP::CALL: break;
Venkatraman Govindaraju0d288d32014-01-10 01:48:17 +0000300 case SP::CALLrr:
301 case SP::CALLri:
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000302 assert(MI->getNumOperands() >= 2);
303 const MachineOperand &Reg = MI->getOperand(0);
Venkatraman Govindaraju0d288d32014-01-10 01:48:17 +0000304 assert(Reg.isReg() && "CALL first operand is not a register.");
305 assert(Reg.isUse() && "CALL first operand is not a use.");
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000306 RegUses.insert(Reg.getReg());
307
Chris Dewhurst8338d902016-05-04 12:11:05 +0000308 const MachineOperand &Operand1 = MI->getOperand(1);
309 if (Operand1.isImm() || Operand1.isGlobal())
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000310 break;
Chris Dewhurst8338d902016-05-04 12:11:05 +0000311 assert(Operand1.isReg() && "CALLrr second operand is not a register.");
312 assert(Operand1.isUse() && "CALLrr second operand is not a use.");
313 RegUses.insert(Operand1.getReg());
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000314 break;
315 }
316}
317
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000318// Insert Defs and Uses of MI into the sets RegDefs and RegUses.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000319void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
320 SmallSet<unsigned, 32>& RegDefs,
321 SmallSet<unsigned, 32>& RegUses)
322{
323 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
324 const MachineOperand &MO = MI->getOperand(i);
325 if (!MO.isReg())
326 continue;
327
328 unsigned Reg = MO.getReg();
329 if (Reg == 0)
330 continue;
331 if (MO.isDef())
332 RegDefs.insert(Reg);
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000333 if (MO.isUse()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000334 // Implicit register uses of retl are return values and
335 // retl does not use them.
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000336 if (MO.isImplicit() && MI->getOpcode() == SP::RETL)
337 continue;
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000338 RegUses.insert(Reg);
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000339 }
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000340 }
341}
342
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000343// returns true if the Reg or its alias is in the RegSet.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000344bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
345{
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000346 // Check Reg and all aliased Registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000347 for (MCRegAliasIterator AI(Reg, Subtarget->getRegisterInfo(), true);
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000348 AI.isValid(); ++AI)
349 if (RegSet.count(*AI))
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000350 return true;
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000351 return false;
352}
353
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000354bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
355{
Evan Cheng7f8e5632011-12-07 07:15:52 +0000356 if (!I->isCall())
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000357 return false;
358
359 unsigned structSizeOpNum = 0;
360 switch (I->getOpcode()) {
361 default: llvm_unreachable("Unknown call opcode.");
362 case SP::CALL: structSizeOpNum = 1; break;
Venkatraman Govindaraju0d288d32014-01-10 01:48:17 +0000363 case SP::CALLrr:
364 case SP::CALLri: structSizeOpNum = 2; break;
Venkatraman Govindaraju8223c552013-10-08 02:50:29 +0000365 case SP::TLS_CALL: return false;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000366 }
367
368 const MachineOperand &MO = I->getOperand(structSizeOpNum);
369 if (!MO.isImm())
370 return false;
371 StructSize = MO.getImm();
372 return true;
373}
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000374
375static bool combineRestoreADD(MachineBasicBlock::iterator RestoreMI,
376 MachineBasicBlock::iterator AddMI,
377 const TargetInstrInfo *TII)
378{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000379 // Before: add <op0>, <op1>, %i[0-7]
380 // restore %g0, %g0, %i[0-7]
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000381 //
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000382 // After : restore <op0>, <op1>, %o[0-7]
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000383
384 unsigned reg = AddMI->getOperand(0).getReg();
385 if (reg < SP::I0 || reg > SP::I7)
386 return false;
387
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000388 // Erase RESTORE.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000389 RestoreMI->eraseFromParent();
390
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000391 // Change ADD to RESTORE.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000392 AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr)
393 ? SP::RESTORErr
394 : SP::RESTOREri));
395
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000396 // Map the destination register.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000397 AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
398
399 return true;
400}
401
402static bool combineRestoreOR(MachineBasicBlock::iterator RestoreMI,
403 MachineBasicBlock::iterator OrMI,
404 const TargetInstrInfo *TII)
405{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000406 // Before: or <op0>, <op1>, %i[0-7]
407 // restore %g0, %g0, %i[0-7]
408 // and <op0> or <op1> is zero,
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000409 //
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000410 // After : restore <op0>, <op1>, %o[0-7]
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000411
412 unsigned reg = OrMI->getOperand(0).getReg();
413 if (reg < SP::I0 || reg > SP::I7)
414 return false;
415
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000416 // check whether it is a copy.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000417 if (OrMI->getOpcode() == SP::ORrr
418 && OrMI->getOperand(1).getReg() != SP::G0
419 && OrMI->getOperand(2).getReg() != SP::G0)
420 return false;
421
422 if (OrMI->getOpcode() == SP::ORri
423 && OrMI->getOperand(1).getReg() != SP::G0
424 && (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0))
425 return false;
426
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000427 // Erase RESTORE.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000428 RestoreMI->eraseFromParent();
429
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000430 // Change OR to RESTORE.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000431 OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr)
432 ? SP::RESTORErr
433 : SP::RESTOREri));
434
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000435 // Map the destination register.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000436 OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
437
438 return true;
439}
440
441static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI,
442 MachineBasicBlock::iterator SetHiMI,
443 const TargetInstrInfo *TII)
444{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000445 // Before: sethi imm3, %i[0-7]
446 // restore %g0, %g0, %g0
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000447 //
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000448 // After : restore %g0, (imm3<<10), %o[0-7]
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000449
450 unsigned reg = SetHiMI->getOperand(0).getReg();
451 if (reg < SP::I0 || reg > SP::I7)
452 return false;
453
454 if (!SetHiMI->getOperand(1).isImm())
455 return false;
456
457 int64_t imm = SetHiMI->getOperand(1).getImm();
458
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000459 // Is it a 3 bit immediate?
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000460 if (!isInt<3>(imm))
461 return false;
462
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000463 // Make it a 13 bit immediate.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000464 imm = (imm << 10) & 0x1FFF;
465
466 assert(RestoreMI->getOpcode() == SP::RESTORErr);
467
468 RestoreMI->setDesc(TII->get(SP::RESTOREri));
469
470 RestoreMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
471 RestoreMI->getOperand(1).setReg(SP::G0);
472 RestoreMI->getOperand(2).ChangeToImmediate(imm);
473
474
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000475 // Erase the original SETHI.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000476 SetHiMI->eraseFromParent();
477
478 return true;
479}
480
481bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
482 MachineBasicBlock::iterator MBBI)
483{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000484 // No previous instruction.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000485 if (MBBI == MBB.begin())
486 return false;
487
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000488 // assert that MBBI is a "restore %g0, %g0, %g0".
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000489 assert(MBBI->getOpcode() == SP::RESTORErr
490 && MBBI->getOperand(0).getReg() == SP::G0
491 && MBBI->getOperand(1).getReg() == SP::G0
492 && MBBI->getOperand(2).getReg() == SP::G0);
493
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000494 MachineBasicBlock::iterator PrevInst = std::prev(MBBI);
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000495
Venkatraman Govindaraju06532182014-01-11 19:38:03 +0000496 // It cannot be combined with a bundled instruction.
497 if (PrevInst->isBundledWithSucc())
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000498 return false;
499
Eric Christopherf5e94062015-01-30 23:46:43 +0000500 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling6235c062013-06-07 20:35:25 +0000501
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000502 switch (PrevInst->getOpcode()) {
503 default: break;
504 case SP::ADDrr:
505 case SP::ADDri: return combineRestoreADD(MBBI, PrevInst, TII); break;
506 case SP::ORrr:
507 case SP::ORri: return combineRestoreOR(MBBI, PrevInst, TII); break;
508 case SP::SETHIi: return combineRestoreSETHIi(MBBI, PrevInst, TII); break;
509 }
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000510 // It cannot combine with the previous instruction.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000511 return false;
512}