blob: c323af8625d703ce5b10ac8b108f0afa1599ffa1 [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===- SparcInstrInfo.cpp - Sparc Instruction Information -------*- C++ -*-===//
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"
Owen Andersond10fd972007-12-31 06:32:00 +000015#include "SparcSubtarget.h"
Chris Lattner7c90f732006-02-05 05:50:24 +000016#include "Sparc.h"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000018#include "llvm/ADT/SmallVector.h"
Brian Gaekee785e532004-02-25 19:28:19 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdb486a62009-09-15 17:46:24 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattnerdb486a62009-09-15 17:46:24 +000022#include "SparcMachineFunctionInfo.h"
Evan Cheng22fee2d2011-06-28 20:07:07 +000023
24#define GET_INSTRINFO_MC_DESC
25#include "SparcGenInstrInfo.inc"
26
Chris Lattner1ddf4752004-02-29 05:59:33 +000027using namespace llvm;
Brian Gaekee785e532004-02-25 19:28:19 +000028
Chris Lattner7c90f732006-02-05 05:50:24 +000029SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Chris Lattner64105522008-01-01 01:03:04 +000030 : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)),
Owen Andersond10fd972007-12-31 06:32:00 +000031 RI(ST, *this), Subtarget(ST) {
Brian Gaekee785e532004-02-25 19:28:19 +000032}
33
Chris Lattner5ccc7222006-02-03 06:44:54 +000034/// isLoadFromStackSlot - If the specified machine instruction is a direct
35/// load from a stack slot, return the virtual or physical register number of
36/// the destination along with the FrameIndex of the loaded stack slot. If
37/// not, return 0. This predicate must return 0 if the instruction has
38/// any side effects other than loading from the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000039unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000040 int &FrameIndex) const {
41 if (MI->getOpcode() == SP::LDri ||
42 MI->getOpcode() == SP::LDFri ||
43 MI->getOpcode() == SP::LDDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000044 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000045 MI->getOperand(2).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000046 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000047 return MI->getOperand(0).getReg();
48 }
49 }
50 return 0;
51}
52
53/// isStoreToStackSlot - If the specified machine instruction is a direct
54/// store to a stack slot, return the virtual or physical register number of
55/// the source reg along with the FrameIndex of the loaded stack slot. If
56/// not, return 0. This predicate must return 0 if the instruction has
57/// any side effects other than storing to the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000058unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000059 int &FrameIndex) const {
60 if (MI->getOpcode() == SP::STri ||
61 MI->getOpcode() == SP::STFri ||
62 MI->getOpcode() == SP::STDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000063 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000064 MI->getOperand(1).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000065 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000066 return MI->getOperand(2).getReg();
67 }
68 }
69 return 0;
70}
Chris Lattnere87146a2006-10-24 16:39:19 +000071
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +000072static bool IsIntegerCC(unsigned CC)
73{
74 return (CC <= SPCC::ICC_VC);
75}
76
77
78static SPCC::CondCodes GetOppositeBranchCondition(SPCC::CondCodes CC)
79{
80 switch(CC) {
81 default: llvm_unreachable("Unknown condition code");
82 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 }
112}
113
114
115bool SparcInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
116 MachineBasicBlock *&TBB,
117 MachineBasicBlock *&FBB,
118 SmallVectorImpl<MachineOperand> &Cond,
119 bool AllowModify) const
120{
121
122 MachineBasicBlock::iterator I = MBB.end();
123 MachineBasicBlock::iterator UnCondBrIter = MBB.end();
124 while (I != MBB.begin()) {
125 --I;
126
127 if (I->isDebugValue())
128 continue;
129
130 //When we see a non-terminator, we are done
131 if (!isUnpredicatedTerminator(I))
132 break;
133
134 //Terminator is not a branch
135 if (!I->getDesc().isBranch())
136 return true;
137
138 //Handle Unconditional branches
139 if (I->getOpcode() == SP::BA) {
140 UnCondBrIter = I;
141
142 if (!AllowModify) {
143 TBB = I->getOperand(0).getMBB();
144 continue;
145 }
146
147 while (llvm::next(I) != MBB.end())
148 llvm::next(I)->eraseFromParent();
149
150 Cond.clear();
151 FBB = 0;
152
153 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
154 TBB = 0;
155 I->eraseFromParent();
156 I = MBB.end();
157 UnCondBrIter = MBB.end();
158 continue;
159 }
160
161 TBB = I->getOperand(0).getMBB();
162 continue;
163 }
164
165 unsigned Opcode = I->getOpcode();
166 if (Opcode != SP::BCOND && Opcode != SP::FBCOND)
167 return true; //Unknown Opcode
168
169 SPCC::CondCodes BranchCode = (SPCC::CondCodes)I->getOperand(1).getImm();
170
171 if (Cond.empty()) {
172 MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
173 if (AllowModify && UnCondBrIter != MBB.end() &&
174 MBB.isLayoutSuccessor(TargetBB)) {
175
176 //Transform the code
177 //
178 // brCC L1
179 // ba L2
180 // L1:
181 // ..
182 // L2:
183 //
184 // into
185 //
186 // brnCC L2
187 // L1:
188 // ...
189 // L2:
190 //
191 BranchCode = GetOppositeBranchCondition(BranchCode);
192 MachineBasicBlock::iterator OldInst = I;
193 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(Opcode))
194 .addMBB(UnCondBrIter->getOperand(0).getMBB()).addImm(BranchCode);
195 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(SP::BA))
196 .addMBB(TargetBB);
197 MBB.addSuccessor(TargetBB);
198 OldInst->eraseFromParent();
199 UnCondBrIter->eraseFromParent();
200
201 UnCondBrIter = MBB.end();
202 I = MBB.end();
203 continue;
204 }
205 FBB = TBB;
206 TBB = I->getOperand(0).getMBB();
207 Cond.push_back(MachineOperand::CreateImm(BranchCode));
208 continue;
209 }
210 //FIXME: Handle subsequent conditional branches
211 //For now, we can't handle multiple conditional branches
212 return true;
213 }
214 return false;
215}
216
Evan Cheng6ae36262007-05-18 00:18:17 +0000217unsigned
218SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
219 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000220 const SmallVectorImpl<MachineOperand> &Cond,
Venkatraman Govindarajuc1a62832011-01-16 03:15:11 +0000221 DebugLoc DL) const {
222 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
223 assert((Cond.size() == 1 || Cond.size() == 0) &&
224 "Sparc branch conditions should have one component!");
225
226 if (Cond.empty()) {
227 assert(!FBB && "Unconditional branch with multiple successors!");
228 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
229 return 1;
230 }
231
232 //Conditional branch
233 unsigned CC = Cond[0].getImm();
234
235 if (IsIntegerCC(CC))
236 BuildMI(&MBB, DL, get(SP::BCOND)).addMBB(TBB).addImm(CC);
237 else
238 BuildMI(&MBB, DL, get(SP::FBCOND)).addMBB(TBB).addImm(CC);
239 if (!FBB)
240 return 1;
241
242 BuildMI(&MBB, DL, get(SP::BA)).addMBB(FBB);
243 return 2;
244}
245
246unsigned SparcInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const
247{
248 MachineBasicBlock::iterator I = MBB.end();
249 unsigned Count = 0;
250 while (I != MBB.begin()) {
251 --I;
252
253 if (I->isDebugValue())
254 continue;
255
256 if (I->getOpcode() != SP::BA
257 && I->getOpcode() != SP::BCOND
258 && I->getOpcode() != SP::FBCOND)
259 break; // Not a branch
260
261 I->eraseFromParent();
262 I = MBB.end();
263 ++Count;
264 }
265 return Count;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000266}
Owen Andersond10fd972007-12-31 06:32:00 +0000267
Jakob Stoklund Olesen8e18a1a2010-07-11 07:56:09 +0000268void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
269 MachineBasicBlock::iterator I, DebugLoc DL,
270 unsigned DestReg, unsigned SrcReg,
271 bool KillSrc) const {
272 if (SP::IntRegsRegClass.contains(DestReg, SrcReg))
273 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0)
274 .addReg(SrcReg, getKillRegState(KillSrc));
275 else if (SP::FPRegsRegClass.contains(DestReg, SrcReg))
276 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg)
277 .addReg(SrcReg, getKillRegState(KillSrc));
278 else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg))
279 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD), DestReg)
280 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000281 else
Jakob Stoklund Olesen8e18a1a2010-07-11 07:56:09 +0000282 llvm_unreachable("Impossible reg-to-reg copy");
Owen Andersond10fd972007-12-31 06:32:00 +0000283}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000284
285void SparcInstrInfo::
286storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
287 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000288 const TargetRegisterClass *RC,
289 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000290 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000291 if (I != MBB.end()) DL = I->getDebugLoc();
292
Owen Andersonf6372aa2008-01-01 21:11:32 +0000293 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
294 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000295 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000296 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000297 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000298 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000299 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000300 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000301 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000302 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000303 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000304 llvm_unreachable("Can't store this register to stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000305}
306
Owen Andersonf6372aa2008-01-01 21:11:32 +0000307void SparcInstrInfo::
308loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
309 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000310 const TargetRegisterClass *RC,
311 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000312 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000313 if (I != MBB.end()) DL = I->getDebugLoc();
314
Owen Andersonf6372aa2008-01-01 21:11:32 +0000315 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000316 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000317 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000318 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000319 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000320 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000321 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000322 llvm_unreachable("Can't load this register from stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000323}
324
Chris Lattnerdb486a62009-09-15 17:46:24 +0000325unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const
326{
327 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
328 unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg();
329 if (GlobalBaseReg != 0)
330 return GlobalBaseReg;
331
332 // Insert the set of GlobalBaseReg into the first MBB of the function
333 MachineBasicBlock &FirstMBB = MF->front();
334 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
335 MachineRegisterInfo &RegInfo = MF->getRegInfo();
336
337 GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
338
339
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000340 DebugLoc dl;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000341
342 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
343 SparcFI->setGlobalBaseReg(GlobalBaseReg);
344 return GlobalBaseReg;
345}