blob: 08a13b81effca57cf3688a4432f133708961e99e [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- SparcInstrInfo.cpp - Sparc Instruction Information ----------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaekee785e532004-02-25 19:28:19 +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 Gaekee785e532004-02-25 19:28:19 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattner7c90f732006-02-05 05:50:24 +000010// This file contains the Sparc implementation of the TargetInstrInfo class.
Brian Gaekee785e532004-02-25 19:28:19 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner7c90f732006-02-05 05:50:24 +000014#include "SparcInstrInfo.h"
15#include "Sparc.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000016#include "SparcMachineFunctionInfo.h"
17#include "SparcSubtarget.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
Brian Gaekee785e532004-02-25 19:28:19 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdb486a62009-09-15 17:46:24 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Evan Cheng22fee2d2011-06-28 20:07:07 +000024
Evan Cheng4db3cff2011-07-01 17:57:27 +000025#define GET_INSTRINFO_CTOR
Evan Cheng22fee2d2011-06-28 20:07:07 +000026#include "SparcGenInstrInfo.inc"
27
Chris Lattner1ddf4752004-02-29 05:59:33 +000028using namespace llvm;
Brian Gaekee785e532004-02-25 19:28:19 +000029
Chris Lattner7c90f732006-02-05 05:50:24 +000030SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Evan Cheng4db3cff2011-07-01 17:57:27 +000031 : SparcGenInstrInfo(SP::ADJCALLSTACKDOWN, SP::ADJCALLSTACKUP),
Bill Wendlingc1dcb8d2013-06-07 20:35:25 +000032 RI(ST), Subtarget(ST) {
Brian Gaekee785e532004-02-25 19:28:19 +000033}
34
Chris Lattner5ccc7222006-02-03 06:44:54 +000035/// isLoadFromStackSlot - If the specified machine instruction is a direct
36/// load from a stack slot, return the virtual or physical register number of
37/// the destination along with the FrameIndex of the loaded stack slot. If
38/// not, return 0. This predicate must return 0 if the instruction has
39/// any side effects other than loading from the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000040unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000041 int &FrameIndex) const {
42 if (MI->getOpcode() == SP::LDri ||
Jakob Stoklund Olesen5e5b78c2013-05-20 00:53:25 +000043 MI->getOpcode() == SP::LDXri ||
Chris Lattner7c90f732006-02-05 05:50:24 +000044 MI->getOpcode() == SP::LDFri ||
45 MI->getOpcode() == SP::LDDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000046 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000047 MI->getOperand(2).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000048 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000049 return MI->getOperand(0).getReg();
50 }
51 }
52 return 0;
53}
54
55/// isStoreToStackSlot - If the specified machine instruction is a direct
56/// store to a stack slot, return the virtual or physical register number of
57/// the source reg along with the FrameIndex of the loaded stack slot. If
58/// not, return 0. This predicate must return 0 if the instruction has
59/// any side effects other than storing to the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000060unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000061 int &FrameIndex) const {
62 if (MI->getOpcode() == SP::STri ||
Jakob Stoklund Olesen5e5b78c2013-05-20 00:53:25 +000063 MI->getOpcode() == SP::STXri ||
Chris Lattner7c90f732006-02-05 05:50:24 +000064 MI->getOpcode() == SP::STFri ||
65 MI->getOpcode() == SP::STDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000066 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000067 MI->getOperand(1).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000068 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000069 return MI->getOperand(2).getReg();
70 }
71 }
72 return 0;
73}
Chris Lattnere87146a2006-10-24 16:39:19 +000074
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +000075static bool IsIntegerCC(unsigned CC)
76{
77 return (CC <= SPCC::ICC_VC);
78}
79
80
81static SPCC::CondCodes GetOppositeBranchCondition(SPCC::CondCodes CC)
82{
83 switch(CC) {
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +000084 case SPCC::ICC_NE: return SPCC::ICC_E;
85 case SPCC::ICC_E: return SPCC::ICC_NE;
86 case SPCC::ICC_G: return SPCC::ICC_LE;
87 case SPCC::ICC_LE: return SPCC::ICC_G;
88 case SPCC::ICC_GE: return SPCC::ICC_L;
89 case SPCC::ICC_L: return SPCC::ICC_GE;
90 case SPCC::ICC_GU: return SPCC::ICC_LEU;
91 case SPCC::ICC_LEU: return SPCC::ICC_GU;
92 case SPCC::ICC_CC: return SPCC::ICC_CS;
93 case SPCC::ICC_CS: return SPCC::ICC_CC;
94 case SPCC::ICC_POS: return SPCC::ICC_NEG;
95 case SPCC::ICC_NEG: return SPCC::ICC_POS;
96 case SPCC::ICC_VC: return SPCC::ICC_VS;
97 case SPCC::ICC_VS: return SPCC::ICC_VC;
98
99 case SPCC::FCC_U: return SPCC::FCC_O;
100 case SPCC::FCC_O: return SPCC::FCC_U;
101 case SPCC::FCC_G: return SPCC::FCC_LE;
102 case SPCC::FCC_LE: return SPCC::FCC_G;
103 case SPCC::FCC_UG: return SPCC::FCC_ULE;
104 case SPCC::FCC_ULE: return SPCC::FCC_UG;
105 case SPCC::FCC_L: return SPCC::FCC_GE;
106 case SPCC::FCC_GE: return SPCC::FCC_L;
107 case SPCC::FCC_UL: return SPCC::FCC_UGE;
108 case SPCC::FCC_UGE: return SPCC::FCC_UL;
109 case SPCC::FCC_LG: return SPCC::FCC_UE;
110 case SPCC::FCC_UE: return SPCC::FCC_LG;
111 case SPCC::FCC_NE: return SPCC::FCC_E;
112 case SPCC::FCC_E: return SPCC::FCC_NE;
113 }
Benjamin Kramere4ad5822012-01-10 20:47:20 +0000114 llvm_unreachable("Invalid cond code");
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000115}
116
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000117bool SparcInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
118 MachineBasicBlock *&TBB,
119 MachineBasicBlock *&FBB,
120 SmallVectorImpl<MachineOperand> &Cond,
121 bool AllowModify) const
122{
123
124 MachineBasicBlock::iterator I = MBB.end();
125 MachineBasicBlock::iterator UnCondBrIter = MBB.end();
126 while (I != MBB.begin()) {
127 --I;
128
129 if (I->isDebugValue())
130 continue;
131
Venkatraman Govindaraju1e06bcb2013-06-04 18:33:25 +0000132 // When we see a non-terminator, we are done.
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000133 if (!isUnpredicatedTerminator(I))
134 break;
135
Venkatraman Govindaraju1e06bcb2013-06-04 18:33:25 +0000136 // Terminator is not a branch.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000137 if (!I->isBranch())
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000138 return true;
139
Venkatraman Govindaraju1e06bcb2013-06-04 18:33:25 +0000140 // Handle Unconditional branches.
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000141 if (I->getOpcode() == SP::BA) {
142 UnCondBrIter = I;
143
144 if (!AllowModify) {
145 TBB = I->getOperand(0).getMBB();
146 continue;
147 }
148
149 while (llvm::next(I) != MBB.end())
150 llvm::next(I)->eraseFromParent();
151
152 Cond.clear();
153 FBB = 0;
154
155 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
156 TBB = 0;
157 I->eraseFromParent();
158 I = MBB.end();
159 UnCondBrIter = MBB.end();
160 continue;
161 }
162
163 TBB = I->getOperand(0).getMBB();
164 continue;
165 }
166
167 unsigned Opcode = I->getOpcode();
168 if (Opcode != SP::BCOND && Opcode != SP::FBCOND)
Venkatraman Govindaraju1e06bcb2013-06-04 18:33:25 +0000169 return true; // Unknown Opcode.
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000170
171 SPCC::CondCodes BranchCode = (SPCC::CondCodes)I->getOperand(1).getImm();
172
173 if (Cond.empty()) {
174 MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
175 if (AllowModify && UnCondBrIter != MBB.end() &&
176 MBB.isLayoutSuccessor(TargetBB)) {
177
Venkatraman Govindaraju1e06bcb2013-06-04 18:33:25 +0000178 // Transform the code
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000179 //
180 // brCC L1
181 // ba L2
182 // L1:
183 // ..
184 // L2:
185 //
186 // into
187 //
188 // brnCC L2
189 // L1:
190 // ...
191 // L2:
192 //
193 BranchCode = GetOppositeBranchCondition(BranchCode);
194 MachineBasicBlock::iterator OldInst = I;
195 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(Opcode))
196 .addMBB(UnCondBrIter->getOperand(0).getMBB()).addImm(BranchCode);
197 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(SP::BA))
198 .addMBB(TargetBB);
Venkatraman Govindaraju80b1ae92011-12-03 21:24:48 +0000199
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000200 OldInst->eraseFromParent();
201 UnCondBrIter->eraseFromParent();
202
203 UnCondBrIter = MBB.end();
204 I = MBB.end();
205 continue;
206 }
207 FBB = TBB;
208 TBB = I->getOperand(0).getMBB();
209 Cond.push_back(MachineOperand::CreateImm(BranchCode));
210 continue;
211 }
Venkatraman Govindaraju1e06bcb2013-06-04 18:33:25 +0000212 // FIXME: Handle subsequent conditional branches.
213 // For now, we can't handle multiple conditional branches.
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000214 return true;
215 }
216 return false;
217}
218
Evan Cheng6ae36262007-05-18 00:18:17 +0000219unsigned
220SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
221 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000222 const SmallVectorImpl<MachineOperand> &Cond,
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000223 DebugLoc DL) const {
224 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
225 assert((Cond.size() == 1 || Cond.size() == 0) &&
226 "Sparc branch conditions should have one component!");
227
228 if (Cond.empty()) {
229 assert(!FBB && "Unconditional branch with multiple successors!");
230 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
231 return 1;
232 }
233
Venkatraman Govindaraju1e06bcb2013-06-04 18:33:25 +0000234 // Conditional branch
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000235 unsigned CC = Cond[0].getImm();
236
237 if (IsIntegerCC(CC))
238 BuildMI(&MBB, DL, get(SP::BCOND)).addMBB(TBB).addImm(CC);
239 else
240 BuildMI(&MBB, DL, get(SP::FBCOND)).addMBB(TBB).addImm(CC);
241 if (!FBB)
242 return 1;
243
244 BuildMI(&MBB, DL, get(SP::BA)).addMBB(FBB);
245 return 2;
246}
247
248unsigned SparcInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const
249{
250 MachineBasicBlock::iterator I = MBB.end();
251 unsigned Count = 0;
252 while (I != MBB.begin()) {
253 --I;
254
255 if (I->isDebugValue())
256 continue;
257
258 if (I->getOpcode() != SP::BA
259 && I->getOpcode() != SP::BCOND
260 && I->getOpcode() != SP::FBCOND)
261 break; // Not a branch
262
263 I->eraseFromParent();
264 I = MBB.end();
265 ++Count;
266 }
267 return Count;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000268}
Owen Andersond10fd972007-12-31 06:32:00 +0000269
Jakob Stoklund Olesen8e18a1a2010-07-11 07:56:09 +0000270void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
271 MachineBasicBlock::iterator I, DebugLoc DL,
272 unsigned DestReg, unsigned SrcReg,
273 bool KillSrc) const {
274 if (SP::IntRegsRegClass.contains(DestReg, SrcReg))
275 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0)
276 .addReg(SrcReg, getKillRegState(KillSrc));
277 else if (SP::FPRegsRegClass.contains(DestReg, SrcReg))
278 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg)
279 .addReg(SrcReg, getKillRegState(KillSrc));
Venkatraman Govindaraju17999212013-06-08 15:32:59 +0000280 else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg)) {
281 if (Subtarget.isV9()) {
282 BuildMI(MBB, I, DL, get(SP::FMOVD), DestReg)
283 .addReg(SrcReg, getKillRegState(KillSrc));
284 } else {
285 // Use two FMOVS instructions.
286 const TargetRegisterInfo *TRI = &getRegisterInfo();
287 MachineInstr *MovMI = 0;
288 unsigned subRegIdx[] = {SP::sub_even, SP::sub_odd};
289 for (unsigned i = 0; i != 2; ++i) {
290 unsigned Dst = TRI->getSubReg(DestReg, subRegIdx[i]);
291 unsigned Src = TRI->getSubReg(SrcReg, subRegIdx[i]);
292 assert(Dst && Src && "Bad sub-register");
293
294 MovMI = BuildMI(MBB, I, DL, get(SP::FMOVS), Dst).addReg(Src);
295 }
296 // Add implicit super-register defs and kills to the last MovMI.
297 MovMI->addRegisterDefined(DestReg, TRI);
298 if (KillSrc)
299 MovMI->addRegisterKilled(SrcReg, TRI);
300 }
301 } else
Jakob Stoklund Olesen8e18a1a2010-07-11 07:56:09 +0000302 llvm_unreachable("Impossible reg-to-reg copy");
Owen Andersond10fd972007-12-31 06:32:00 +0000303}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000304
305void SparcInstrInfo::
306storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
307 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000308 const TargetRegisterClass *RC,
309 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000310 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000311 if (I != MBB.end()) DL = I->getDebugLoc();
312
Owen Andersonf6372aa2008-01-01 21:11:32 +0000313 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
Jakob Stoklund Olesen5e5b78c2013-05-20 00:53:25 +0000314 if (RC == &SP::I64RegsRegClass)
315 BuildMI(MBB, I, DL, get(SP::STXri)).addFrameIndex(FI).addImm(0)
316 .addReg(SrcReg, getKillRegState(isKill));
317 else if (RC == &SP::IntRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000318 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000319 .addReg(SrcReg, getKillRegState(isKill));
Craig Topperc9099502012-04-20 06:31:50 +0000320 else if (RC == &SP::FPRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000321 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000322 .addReg(SrcReg, getKillRegState(isKill));
Craig Topperc9099502012-04-20 06:31:50 +0000323 else if (RC == &SP::DFPRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000324 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000325 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000326 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000327 llvm_unreachable("Can't store this register to stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000328}
329
Owen Andersonf6372aa2008-01-01 21:11:32 +0000330void SparcInstrInfo::
331loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
332 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000333 const TargetRegisterClass *RC,
334 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000335 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000336 if (I != MBB.end()) DL = I->getDebugLoc();
337
Jakob Stoklund Olesen5e5b78c2013-05-20 00:53:25 +0000338 if (RC == &SP::I64RegsRegClass)
339 BuildMI(MBB, I, DL, get(SP::LDXri), DestReg).addFrameIndex(FI).addImm(0);
340 else if (RC == &SP::IntRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000341 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Craig Topperc9099502012-04-20 06:31:50 +0000342 else if (RC == &SP::FPRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000343 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Craig Topperc9099502012-04-20 06:31:50 +0000344 else if (RC == &SP::DFPRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000345 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000346 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000347 llvm_unreachable("Can't load this register from stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000348}
349
Chris Lattnerdb486a62009-09-15 17:46:24 +0000350unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const
351{
352 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
353 unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg();
354 if (GlobalBaseReg != 0)
355 return GlobalBaseReg;
356
357 // Insert the set of GlobalBaseReg into the first MBB of the function
358 MachineBasicBlock &FirstMBB = MF->front();
359 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
360 MachineRegisterInfo &RegInfo = MF->getRegInfo();
361
362 GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
363
364
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000365 DebugLoc dl;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000366
367 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
368 SparcFI->setGlobalBaseReg(GlobalBaseReg);
369 return GlobalBaseReg;
370}