blob: f8674d0bd660fb8b73f0d33448332b26e9430e69 [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"
Brian Gaekee785e532004-02-25 19:28:19 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdb486a62009-09-15 17:46:24 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000021#include "llvm/Support/TargetRegistry.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000022#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SmallVector.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),
Owen Andersond10fd972007-12-31 06:32:00 +000032 RI(ST, *this), 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 ||
43 MI->getOpcode() == SP::LDFri ||
44 MI->getOpcode() == SP::LDDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000045 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000046 MI->getOperand(2).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000047 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000048 return MI->getOperand(0).getReg();
49 }
50 }
51 return 0;
52}
53
54/// isStoreToStackSlot - If the specified machine instruction is a direct
55/// store to a stack slot, return the virtual or physical register number of
56/// the source reg along with the FrameIndex of the loaded stack slot. If
57/// not, return 0. This predicate must return 0 if the instruction has
58/// any side effects other than storing to the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000059unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000060 int &FrameIndex) const {
61 if (MI->getOpcode() == SP::STri ||
62 MI->getOpcode() == SP::STFri ||
63 MI->getOpcode() == SP::STDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000064 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000065 MI->getOperand(1).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000066 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000067 return MI->getOperand(2).getReg();
68 }
69 }
70 return 0;
71}
Chris Lattnere87146a2006-10-24 16:39:19 +000072
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +000073static bool IsIntegerCC(unsigned CC)
74{
75 return (CC <= SPCC::ICC_VC);
76}
77
78
79static SPCC::CondCodes GetOppositeBranchCondition(SPCC::CondCodes CC)
80{
81 switch(CC) {
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +000082 case SPCC::ICC_NE: return SPCC::ICC_E;
83 case SPCC::ICC_E: return SPCC::ICC_NE;
84 case SPCC::ICC_G: return SPCC::ICC_LE;
85 case SPCC::ICC_LE: return SPCC::ICC_G;
86 case SPCC::ICC_GE: return SPCC::ICC_L;
87 case SPCC::ICC_L: return SPCC::ICC_GE;
88 case SPCC::ICC_GU: return SPCC::ICC_LEU;
89 case SPCC::ICC_LEU: return SPCC::ICC_GU;
90 case SPCC::ICC_CC: return SPCC::ICC_CS;
91 case SPCC::ICC_CS: return SPCC::ICC_CC;
92 case SPCC::ICC_POS: return SPCC::ICC_NEG;
93 case SPCC::ICC_NEG: return SPCC::ICC_POS;
94 case SPCC::ICC_VC: return SPCC::ICC_VS;
95 case SPCC::ICC_VS: return SPCC::ICC_VC;
96
97 case SPCC::FCC_U: return SPCC::FCC_O;
98 case SPCC::FCC_O: return SPCC::FCC_U;
99 case SPCC::FCC_G: return SPCC::FCC_LE;
100 case SPCC::FCC_LE: return SPCC::FCC_G;
101 case SPCC::FCC_UG: return SPCC::FCC_ULE;
102 case SPCC::FCC_ULE: return SPCC::FCC_UG;
103 case SPCC::FCC_L: return SPCC::FCC_GE;
104 case SPCC::FCC_GE: return SPCC::FCC_L;
105 case SPCC::FCC_UL: return SPCC::FCC_UGE;
106 case SPCC::FCC_UGE: return SPCC::FCC_UL;
107 case SPCC::FCC_LG: return SPCC::FCC_UE;
108 case SPCC::FCC_UE: return SPCC::FCC_LG;
109 case SPCC::FCC_NE: return SPCC::FCC_E;
110 case SPCC::FCC_E: return SPCC::FCC_NE;
111 }
Benjamin Kramere4ad5822012-01-10 20:47:20 +0000112 llvm_unreachable("Invalid cond code");
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000113}
114
Venkatraman Govindaraju55caf9c2011-12-25 18:50:24 +0000115MachineInstr *
116SparcInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
117 int FrameIx,
118 uint64_t Offset,
119 const MDNode *MDPtr,
120 DebugLoc dl) const {
121 MachineInstrBuilder MIB = BuildMI(MF, dl, get(SP::DBG_VALUE))
122 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
123 return &*MIB;
124}
125
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000126
127bool SparcInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
128 MachineBasicBlock *&TBB,
129 MachineBasicBlock *&FBB,
130 SmallVectorImpl<MachineOperand> &Cond,
131 bool AllowModify) const
132{
133
134 MachineBasicBlock::iterator I = MBB.end();
135 MachineBasicBlock::iterator UnCondBrIter = MBB.end();
136 while (I != MBB.begin()) {
137 --I;
138
139 if (I->isDebugValue())
140 continue;
141
142 //When we see a non-terminator, we are done
143 if (!isUnpredicatedTerminator(I))
144 break;
145
146 //Terminator is not a branch
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000147 if (!I->isBranch())
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000148 return true;
149
150 //Handle Unconditional branches
151 if (I->getOpcode() == SP::BA) {
152 UnCondBrIter = I;
153
154 if (!AllowModify) {
155 TBB = I->getOperand(0).getMBB();
156 continue;
157 }
158
159 while (llvm::next(I) != MBB.end())
160 llvm::next(I)->eraseFromParent();
161
162 Cond.clear();
163 FBB = 0;
164
165 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
166 TBB = 0;
167 I->eraseFromParent();
168 I = MBB.end();
169 UnCondBrIter = MBB.end();
170 continue;
171 }
172
173 TBB = I->getOperand(0).getMBB();
174 continue;
175 }
176
177 unsigned Opcode = I->getOpcode();
178 if (Opcode != SP::BCOND && Opcode != SP::FBCOND)
179 return true; //Unknown Opcode
180
181 SPCC::CondCodes BranchCode = (SPCC::CondCodes)I->getOperand(1).getImm();
182
183 if (Cond.empty()) {
184 MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
185 if (AllowModify && UnCondBrIter != MBB.end() &&
186 MBB.isLayoutSuccessor(TargetBB)) {
187
188 //Transform the code
189 //
190 // brCC L1
191 // ba L2
192 // L1:
193 // ..
194 // L2:
195 //
196 // into
197 //
198 // brnCC L2
199 // L1:
200 // ...
201 // L2:
202 //
203 BranchCode = GetOppositeBranchCondition(BranchCode);
204 MachineBasicBlock::iterator OldInst = I;
205 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(Opcode))
206 .addMBB(UnCondBrIter->getOperand(0).getMBB()).addImm(BranchCode);
207 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(SP::BA))
208 .addMBB(TargetBB);
Venkatraman Govindaraju80b1ae92011-12-03 21:24:48 +0000209
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000210 OldInst->eraseFromParent();
211 UnCondBrIter->eraseFromParent();
212
213 UnCondBrIter = MBB.end();
214 I = MBB.end();
215 continue;
216 }
217 FBB = TBB;
218 TBB = I->getOperand(0).getMBB();
219 Cond.push_back(MachineOperand::CreateImm(BranchCode));
220 continue;
221 }
222 //FIXME: Handle subsequent conditional branches
223 //For now, we can't handle multiple conditional branches
224 return true;
225 }
226 return false;
227}
228
Evan Cheng6ae36262007-05-18 00:18:17 +0000229unsigned
230SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
231 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000232 const SmallVectorImpl<MachineOperand> &Cond,
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000233 DebugLoc DL) const {
234 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
235 assert((Cond.size() == 1 || Cond.size() == 0) &&
236 "Sparc branch conditions should have one component!");
237
238 if (Cond.empty()) {
239 assert(!FBB && "Unconditional branch with multiple successors!");
240 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
241 return 1;
242 }
243
244 //Conditional branch
245 unsigned CC = Cond[0].getImm();
246
247 if (IsIntegerCC(CC))
248 BuildMI(&MBB, DL, get(SP::BCOND)).addMBB(TBB).addImm(CC);
249 else
250 BuildMI(&MBB, DL, get(SP::FBCOND)).addMBB(TBB).addImm(CC);
251 if (!FBB)
252 return 1;
253
254 BuildMI(&MBB, DL, get(SP::BA)).addMBB(FBB);
255 return 2;
256}
257
258unsigned SparcInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const
259{
260 MachineBasicBlock::iterator I = MBB.end();
261 unsigned Count = 0;
262 while (I != MBB.begin()) {
263 --I;
264
265 if (I->isDebugValue())
266 continue;
267
268 if (I->getOpcode() != SP::BA
269 && I->getOpcode() != SP::BCOND
270 && I->getOpcode() != SP::FBCOND)
271 break; // Not a branch
272
273 I->eraseFromParent();
274 I = MBB.end();
275 ++Count;
276 }
277 return Count;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000278}
Owen Andersond10fd972007-12-31 06:32:00 +0000279
Jakob Stoklund Olesen8e18a1a2010-07-11 07:56:09 +0000280void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
281 MachineBasicBlock::iterator I, DebugLoc DL,
282 unsigned DestReg, unsigned SrcReg,
283 bool KillSrc) const {
284 if (SP::IntRegsRegClass.contains(DestReg, SrcReg))
285 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0)
286 .addReg(SrcReg, getKillRegState(KillSrc));
287 else if (SP::FPRegsRegClass.contains(DestReg, SrcReg))
288 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg)
289 .addReg(SrcReg, getKillRegState(KillSrc));
290 else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg))
291 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD), DestReg)
292 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000293 else
Jakob Stoklund Olesen8e18a1a2010-07-11 07:56:09 +0000294 llvm_unreachable("Impossible reg-to-reg copy");
Owen Andersond10fd972007-12-31 06:32:00 +0000295}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000296
297void SparcInstrInfo::
298storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
299 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000300 const TargetRegisterClass *RC,
301 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000302 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000303 if (I != MBB.end()) DL = I->getDebugLoc();
304
Owen Andersonf6372aa2008-01-01 21:11:32 +0000305 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
Craig Topperc9099502012-04-20 06:31:50 +0000306 if (RC == &SP::IntRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000307 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000308 .addReg(SrcReg, getKillRegState(isKill));
Craig Topperc9099502012-04-20 06:31:50 +0000309 else if (RC == &SP::FPRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000310 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000311 .addReg(SrcReg, getKillRegState(isKill));
Craig Topperc9099502012-04-20 06:31:50 +0000312 else if (RC == &SP::DFPRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000313 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000314 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000315 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000316 llvm_unreachable("Can't store this register to stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000317}
318
Owen Andersonf6372aa2008-01-01 21:11:32 +0000319void SparcInstrInfo::
320loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
321 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000322 const TargetRegisterClass *RC,
323 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000324 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000325 if (I != MBB.end()) DL = I->getDebugLoc();
326
Craig Topperc9099502012-04-20 06:31:50 +0000327 if (RC == &SP::IntRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000328 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Craig Topperc9099502012-04-20 06:31:50 +0000329 else if (RC == &SP::FPRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000330 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Craig Topperc9099502012-04-20 06:31:50 +0000331 else if (RC == &SP::DFPRegsRegClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000332 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000333 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000334 llvm_unreachable("Can't load this register from stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000335}
336
Chris Lattnerdb486a62009-09-15 17:46:24 +0000337unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const
338{
339 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
340 unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg();
341 if (GlobalBaseReg != 0)
342 return GlobalBaseReg;
343
344 // Insert the set of GlobalBaseReg into the first MBB of the function
345 MachineBasicBlock &FirstMBB = MF->front();
346 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
347 MachineRegisterInfo &RegInfo = MF->getRegInfo();
348
349 GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
350
351
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000352 DebugLoc dl;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000353
354 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
355 SparcFI->setGlobalBaseReg(GlobalBaseReg);
356 return GlobalBaseReg;
357}