blob: 38bff44e7542533b6a6e8ed71eebc43816b31aa9 [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 {
41 /// Target machine description which we query for reg. names, data
42 /// layout, etc.
43 ///
44 TargetMachine &TM;
Venkatraman Govindarajuf482d3d2013-10-06 07:06:44 +000045 const SparcSubtarget *Subtarget;
Chris Lattner158e1f52006-02-05 05:50:24 +000046
Devang Patel8c78a0b2007-05-03 01:11:54 +000047 static char ID;
Eric Christopherf5e94062015-01-30 23:46:43 +000048 Filler(TargetMachine &tm) : MachineFunctionPass(ID), TM(tm) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000049
Craig Topperb0c941b2014-04-29 07:57:13 +000050 const char *getPassName() const override {
Chris Lattner158e1f52006-02-05 05:50:24 +000051 return "SPARC Delay Slot Filler";
52 }
53
54 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
Craig Topperb0c941b2014-04-29 07:57:13 +000055 bool runOnMachineFunction(MachineFunction &F) override {
Chris Lattner158e1f52006-02-05 05:50:24 +000056 bool Changed = false;
Eric Christopherf5e94062015-01-30 23:46:43 +000057 Subtarget = &F.getSubtarget<SparcSubtarget>();
Venkatraman Govindaraju06532182014-01-11 19:38:03 +000058
59 // This pass invalidates liveness information when it reorders
60 // instructions to fill delay slot.
61 F.getRegInfo().invalidateLiveness();
62
Chris Lattner158e1f52006-02-05 05:50:24 +000063 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
64 FI != FE; ++FI)
65 Changed |= runOnMachineBasicBlock(*FI);
66 return Changed;
67 }
68
Venkatraman Govindaraju54bf6112013-05-16 23:53:29 +000069 void insertCallDefsUses(MachineBasicBlock::iterator MI,
70 SmallSet<unsigned, 32>& RegDefs,
71 SmallSet<unsigned, 32>& RegUses);
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +000072
73 void insertDefsUses(MachineBasicBlock::iterator MI,
74 SmallSet<unsigned, 32>& RegDefs,
75 SmallSet<unsigned, 32>& RegUses);
76
77 bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
78 unsigned Reg);
79
80 bool delayHasHazard(MachineBasicBlock::iterator candidate,
81 bool &sawLoad, bool &sawStore,
82 SmallSet<unsigned, 32> &RegDefs,
83 SmallSet<unsigned, 32> &RegUses);
84
85 MachineBasicBlock::iterator
86 findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot);
87
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +000088 bool needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize);
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +000089
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +000090 bool tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
91 MachineBasicBlock::iterator MBBI);
92
Chris Lattner158e1f52006-02-05 05:50:24 +000093 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000094 char Filler::ID = 0;
Chris Lattner158e1f52006-02-05 05:50:24 +000095} // end of anonymous namespace
96
97/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
98/// slots in Sparc MachineFunctions
99///
100FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
101 return new Filler(tm);
102}
103
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000104
Chris Lattner158e1f52006-02-05 05:50:24 +0000105/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000106/// We assume there is only one delay slot per delayed instruction.
Chris Lattner158e1f52006-02-05 05:50:24 +0000107///
108bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
109 bool Changed = false;
Eric Christopherf5e94062015-01-30 23:46:43 +0000110 Subtarget = &MBB.getParent()->getSubtarget<SparcSubtarget>();
111 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Venkatraman Govindarajuf482d3d2013-10-06 07:06:44 +0000112
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000113 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
114 MachineBasicBlock::iterator MI = I;
115 ++I;
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000116
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000117 // If MI is restore, try combining it with previous inst.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000118 if (!DisableDelaySlotFiller &&
119 (MI->getOpcode() == SP::RESTORErr
120 || MI->getOpcode() == SP::RESTOREri)) {
121 Changed |= tryCombineRestoreWithPrevInst(MBB, MI);
122 continue;
Chris Lattner158e1f52006-02-05 05:50:24 +0000123 }
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000124
Venkatraman Govindarajuf482d3d2013-10-06 07:06:44 +0000125 if (!Subtarget->isV9() &&
126 (MI->getOpcode() == SP::FCMPS || MI->getOpcode() == SP::FCMPD
127 || MI->getOpcode() == SP::FCMPQ)) {
128 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
129 Changed = true;
130 continue;
131 }
132
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000133 // If MI has no delay slot, skip.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000134 if (!MI->hasDelaySlot())
135 continue;
136
137 MachineBasicBlock::iterator D = MBB.end();
138
139 if (!DisableDelaySlotFiller)
140 D = findDelayInstr(MBB, MI);
141
142 ++FilledSlots;
143 Changed = true;
144
145 if (D == MBB.end())
146 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
147 else
148 MBB.splice(I, &MBB, D);
149
150 unsigned structSize = 0;
151 if (needsUnimp(MI, structSize)) {
152 MachineBasicBlock::iterator J = MI;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000153 ++J; // skip the delay filler.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000154 assert (J != MBB.end() && "MI needs a delay instruction.");
Venkatraman Govindarajufdcc4982013-07-30 02:26:29 +0000155 BuildMI(MBB, ++J, MI->getDebugLoc(),
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000156 TII->get(SP::UNIMP)).addImm(structSize);
Venkatraman Govindaraju06532182014-01-11 19:38:03 +0000157 // Bundle the delay filler and unimp with the instruction.
158 MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), J);
159 } else {
160 MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), I);
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000161 }
162 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000163 return Changed;
164}
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000165
166MachineBasicBlock::iterator
167Filler::findDelayInstr(MachineBasicBlock &MBB,
168 MachineBasicBlock::iterator slot)
169{
170 SmallSet<unsigned, 32> RegDefs;
171 SmallSet<unsigned, 32> RegUses;
172 bool sawLoad = false;
173 bool sawStore = false;
174
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000175 if (slot == MBB.begin())
176 return MBB.end();
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000177
Venkatraman Govindaraju8223c552013-10-08 02:50:29 +0000178 if (slot->getOpcode() == SP::RET || slot->getOpcode() == SP::TLS_CALL)
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000179 return MBB.end();
180
181 if (slot->getOpcode() == SP::RETL) {
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000182 MachineBasicBlock::iterator J = slot;
183 --J;
184
185 if (J->getOpcode() == SP::RESTORErr
186 || J->getOpcode() == SP::RESTOREri) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000187 // change retl to ret.
Eric Christopherf5e94062015-01-30 23:46:43 +0000188 slot->setDesc(Subtarget->getInstrInfo()->get(SP::RET));
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000189 return J;
190 }
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000191 }
192
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000193 // Call's delay filler can def some of call's uses.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000194 if (slot->isCall())
Venkatraman Govindaraju54bf6112013-05-16 23:53:29 +0000195 insertCallDefsUses(slot, RegDefs, RegUses);
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000196 else
197 insertDefsUses(slot, RegDefs, RegUses);
198
199 bool done = false;
200
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000201 MachineBasicBlock::iterator I = slot;
202
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000203 while (!done) {
204 done = (I == MBB.begin());
205
206 if (!done)
207 --I;
208
209 // skip debug value
210 if (I->isDebugValue())
211 continue;
212
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000213 if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
214 I->hasDelaySlot() || I->isBundledWithSucc())
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000215 break;
216
217 if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
218 insertDefsUses(I, RegDefs, RegUses);
219 continue;
220 }
221
222 return I;
223 }
224 return MBB.end();
225}
226
227bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
228 bool &sawLoad,
229 bool &sawStore,
230 SmallSet<unsigned, 32> &RegDefs,
231 SmallSet<unsigned, 32> &RegUses)
232{
233
Venkatraman Govindaraju0c1f6532011-02-12 19:02:33 +0000234 if (candidate->isImplicitDef() || candidate->isKill())
235 return true;
236
Evan Cheng7f8e5632011-12-07 07:15:52 +0000237 if (candidate->mayLoad()) {
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000238 sawLoad = true;
239 if (sawStore)
240 return true;
241 }
242
Evan Cheng7f8e5632011-12-07 07:15:52 +0000243 if (candidate->mayStore()) {
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000244 if (sawStore)
245 return true;
246 sawStore = true;
247 if (sawLoad)
248 return true;
249 }
250
251 for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
252 const MachineOperand &MO = candidate->getOperand(i);
253 if (!MO.isReg())
254 continue; // skip
255
256 unsigned Reg = MO.getReg();
257
258 if (MO.isDef()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000259 // check whether Reg is defined or used before delay slot.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000260 if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
261 return true;
262 }
263 if (MO.isUse()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000264 // check whether Reg is defined before delay slot.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000265 if (IsRegInSet(RegDefs, Reg))
266 return true;
267 }
268 }
269 return false;
270}
271
272
Venkatraman Govindaraju54bf6112013-05-16 23:53:29 +0000273void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI,
274 SmallSet<unsigned, 32>& RegDefs,
275 SmallSet<unsigned, 32>& RegUses)
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000276{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000277 // Call defines o7, which is visible to the instruction in delay slot.
Venkatraman Govindaraju54bf6112013-05-16 23:53:29 +0000278 RegDefs.insert(SP::O7);
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000279
280 switch(MI->getOpcode()) {
281 default: llvm_unreachable("Unknown opcode.");
282 case SP::CALL: break;
Venkatraman Govindaraju0d288d32014-01-10 01:48:17 +0000283 case SP::CALLrr:
284 case SP::CALLri:
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000285 assert(MI->getNumOperands() >= 2);
286 const MachineOperand &Reg = MI->getOperand(0);
Venkatraman Govindaraju0d288d32014-01-10 01:48:17 +0000287 assert(Reg.isReg() && "CALL first operand is not a register.");
288 assert(Reg.isUse() && "CALL first operand is not a use.");
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000289 RegUses.insert(Reg.getReg());
290
291 const MachineOperand &RegOrImm = MI->getOperand(1);
292 if (RegOrImm.isImm())
293 break;
Venkatraman Govindaraju0d288d32014-01-10 01:48:17 +0000294 assert(RegOrImm.isReg() && "CALLrr second operand is not a register.");
295 assert(RegOrImm.isUse() && "CALLrr second operand is not a use.");
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000296 RegUses.insert(RegOrImm.getReg());
297 break;
298 }
299}
300
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000301// Insert Defs and Uses of MI into the sets RegDefs and RegUses.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000302void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
303 SmallSet<unsigned, 32>& RegDefs,
304 SmallSet<unsigned, 32>& RegUses)
305{
306 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
307 const MachineOperand &MO = MI->getOperand(i);
308 if (!MO.isReg())
309 continue;
310
311 unsigned Reg = MO.getReg();
312 if (Reg == 0)
313 continue;
314 if (MO.isDef())
315 RegDefs.insert(Reg);
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000316 if (MO.isUse()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000317 // Implicit register uses of retl are return values and
318 // retl does not use them.
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000319 if (MO.isImplicit() && MI->getOpcode() == SP::RETL)
320 continue;
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000321 RegUses.insert(Reg);
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000322 }
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000323 }
324}
325
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000326// returns true if the Reg or its alias is in the RegSet.
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000327bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
328{
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000329 // Check Reg and all aliased Registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000330 for (MCRegAliasIterator AI(Reg, Subtarget->getRegisterInfo(), true);
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000331 AI.isValid(); ++AI)
332 if (RegSet.count(*AI))
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000333 return true;
Venkatraman Govindaraju058e1242011-01-20 05:08:26 +0000334 return false;
335}
336
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000337bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
338{
Evan Cheng7f8e5632011-12-07 07:15:52 +0000339 if (!I->isCall())
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000340 return false;
341
342 unsigned structSizeOpNum = 0;
343 switch (I->getOpcode()) {
344 default: llvm_unreachable("Unknown call opcode.");
345 case SP::CALL: structSizeOpNum = 1; break;
Venkatraman Govindaraju0d288d32014-01-10 01:48:17 +0000346 case SP::CALLrr:
347 case SP::CALLri: structSizeOpNum = 2; break;
Venkatraman Govindaraju8223c552013-10-08 02:50:29 +0000348 case SP::TLS_CALL: return false;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000349 }
350
351 const MachineOperand &MO = I->getOperand(structSizeOpNum);
352 if (!MO.isImm())
353 return false;
354 StructSize = MO.getImm();
355 return true;
356}
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000357
358static bool combineRestoreADD(MachineBasicBlock::iterator RestoreMI,
359 MachineBasicBlock::iterator AddMI,
360 const TargetInstrInfo *TII)
361{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000362 // Before: add <op0>, <op1>, %i[0-7]
363 // restore %g0, %g0, %i[0-7]
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000364 //
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000365 // After : restore <op0>, <op1>, %o[0-7]
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000366
367 unsigned reg = AddMI->getOperand(0).getReg();
368 if (reg < SP::I0 || reg > SP::I7)
369 return false;
370
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000371 // Erase RESTORE.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000372 RestoreMI->eraseFromParent();
373
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000374 // Change ADD to RESTORE.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000375 AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr)
376 ? SP::RESTORErr
377 : SP::RESTOREri));
378
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000379 // Map the destination register.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000380 AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
381
382 return true;
383}
384
385static bool combineRestoreOR(MachineBasicBlock::iterator RestoreMI,
386 MachineBasicBlock::iterator OrMI,
387 const TargetInstrInfo *TII)
388{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000389 // Before: or <op0>, <op1>, %i[0-7]
390 // restore %g0, %g0, %i[0-7]
391 // and <op0> or <op1> is zero,
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000392 //
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000393 // After : restore <op0>, <op1>, %o[0-7]
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000394
395 unsigned reg = OrMI->getOperand(0).getReg();
396 if (reg < SP::I0 || reg > SP::I7)
397 return false;
398
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000399 // check whether it is a copy.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000400 if (OrMI->getOpcode() == SP::ORrr
401 && OrMI->getOperand(1).getReg() != SP::G0
402 && OrMI->getOperand(2).getReg() != SP::G0)
403 return false;
404
405 if (OrMI->getOpcode() == SP::ORri
406 && OrMI->getOperand(1).getReg() != SP::G0
407 && (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0))
408 return false;
409
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000410 // Erase RESTORE.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000411 RestoreMI->eraseFromParent();
412
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000413 // Change OR to RESTORE.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000414 OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr)
415 ? SP::RESTORErr
416 : SP::RESTOREri));
417
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000418 // Map the destination register.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000419 OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
420
421 return true;
422}
423
424static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI,
425 MachineBasicBlock::iterator SetHiMI,
426 const TargetInstrInfo *TII)
427{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000428 // Before: sethi imm3, %i[0-7]
429 // restore %g0, %g0, %g0
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000430 //
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000431 // After : restore %g0, (imm3<<10), %o[0-7]
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000432
433 unsigned reg = SetHiMI->getOperand(0).getReg();
434 if (reg < SP::I0 || reg > SP::I7)
435 return false;
436
437 if (!SetHiMI->getOperand(1).isImm())
438 return false;
439
440 int64_t imm = SetHiMI->getOperand(1).getImm();
441
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000442 // Is it a 3 bit immediate?
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000443 if (!isInt<3>(imm))
444 return false;
445
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000446 // Make it a 13 bit immediate.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000447 imm = (imm << 10) & 0x1FFF;
448
449 assert(RestoreMI->getOpcode() == SP::RESTORErr);
450
451 RestoreMI->setDesc(TII->get(SP::RESTOREri));
452
453 RestoreMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
454 RestoreMI->getOperand(1).setReg(SP::G0);
455 RestoreMI->getOperand(2).ChangeToImmediate(imm);
456
457
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000458 // Erase the original SETHI.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000459 SetHiMI->eraseFromParent();
460
461 return true;
462}
463
464bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
465 MachineBasicBlock::iterator MBBI)
466{
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000467 // No previous instruction.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000468 if (MBBI == MBB.begin())
469 return false;
470
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000471 // assert that MBBI is a "restore %g0, %g0, %g0".
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000472 assert(MBBI->getOpcode() == SP::RESTORErr
473 && MBBI->getOperand(0).getReg() == SP::G0
474 && MBBI->getOperand(1).getReg() == SP::G0
475 && MBBI->getOperand(2).getReg() == SP::G0);
476
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000477 MachineBasicBlock::iterator PrevInst = std::prev(MBBI);
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000478
Venkatraman Govindaraju06532182014-01-11 19:38:03 +0000479 // It cannot be combined with a bundled instruction.
480 if (PrevInst->isBundledWithSucc())
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000481 return false;
482
Eric Christopherf5e94062015-01-30 23:46:43 +0000483 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling6235c062013-06-07 20:35:25 +0000484
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000485 switch (PrevInst->getOpcode()) {
486 default: break;
487 case SP::ADDrr:
488 case SP::ADDri: return combineRestoreADD(MBBI, PrevInst, TII); break;
489 case SP::ORrr:
490 case SP::ORri: return combineRestoreOR(MBBI, PrevInst, TII); break;
491 case SP::SETHIi: return combineRestoreSETHIi(MBBI, PrevInst, TII); break;
492 }
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000493 // It cannot combine with the previous instruction.
Venkatraman Govindaraju0bbe1b22013-06-02 21:48:17 +0000494 return false;
495}