blob: 959d41ab8b858cdc9b7096289523c6a5a8487a01 [file] [log] [blame]
Chris Lattnerd23405e2008-03-17 03:21:36 +00001//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Sparc uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SparcISelLowering.h"
16#include "SparcTargetMachine.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000017#include "llvm/Function.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000018#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000024#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikov0eefda12008-10-10 20:28:10 +000025#include "llvm/ADT/VectorExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000027using namespace llvm;
28
Chris Lattner5a65b922008-03-17 05:41:48 +000029
30//===----------------------------------------------------------------------===//
31// Calling Convention Implementation
32//===----------------------------------------------------------------------===//
33
34#include "SparcGenCallingConv.inc"
35
Dan Gohman98ca4f22009-08-05 01:29:28 +000036SDValue
37SparcTargetLowering::LowerReturn(SDValue Chain,
38 unsigned CallConv, bool isVarArg,
39 const SmallVectorImpl<ISD::OutputArg> &Outs,
40 DebugLoc dl, SelectionDAG &DAG) {
41
Chris Lattner5a65b922008-03-17 05:41:48 +000042 // CCValAssign - represent the assignment of the return value to locations.
43 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000044
Chris Lattner5a65b922008-03-17 05:41:48 +000045 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +000046 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
47 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000048
Dan Gohman98ca4f22009-08-05 01:29:28 +000049 // Analize return values.
50 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000051
Chris Lattner5a65b922008-03-17 05:41:48 +000052 // If this is the first return lowered for this function, add the regs to the
53 // liveout set for the function.
54 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
55 for (unsigned i = 0; i != RVLocs.size(); ++i)
56 if (RVLocs[i].isRegLoc())
57 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
58 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000059
Dan Gohman475871a2008-07-27 21:46:04 +000060 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +000061
62 // Copy the result values into the output registers.
63 for (unsigned i = 0; i != RVLocs.size(); ++i) {
64 CCValAssign &VA = RVLocs[i];
65 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +000066
Dale Johannesena05dca42009-02-04 23:02:30 +000067 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +000068 Outs[i].Val, Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +000069
Chris Lattner5a65b922008-03-17 05:41:48 +000070 // Guarantee that all emitted copies are stuck together with flags.
71 Flag = Chain.getValue(1);
72 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000073
Gabor Greifba36cb52008-08-28 21:40:38 +000074 if (Flag.getNode())
Owen Andersone50ed302009-08-10 22:56:29 +000075 return DAG.getNode(SPISD::RET_FLAG, dl, EVT::Other, Chain, Flag);
76 return DAG.getNode(SPISD::RET_FLAG, dl, EVT::Other, Chain);
Chris Lattner5a65b922008-03-17 05:41:48 +000077}
78
Dan Gohman98ca4f22009-08-05 01:29:28 +000079/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
80/// passed in either one or two GPRs, including FP values. TODO: we should
81/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +000082SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000083SparcTargetLowering::LowerFormalArguments(SDValue Chain,
84 unsigned CallConv, bool isVarArg,
85 const SmallVectorImpl<ISD::InputArg>
86 &Ins,
87 DebugLoc dl, SelectionDAG &DAG,
88 SmallVectorImpl<SDValue> &InVals) {
89
Chris Lattner5a65b922008-03-17 05:41:48 +000090 MachineFunction &MF = DAG.getMachineFunction();
91 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Eli Friedmana786c7b2009-07-19 19:53:46 +000092
93 // Assign locations to all of the incoming arguments.
94 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +000095 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
96 ArgLocs, *DAG.getContext());
97 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000098
Chris Lattner5a65b922008-03-17 05:41:48 +000099 static const unsigned ArgRegs[] = {
100 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
101 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000102 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
103 unsigned ArgOffset = 68;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000104
Eli Friedmana786c7b2009-07-19 19:53:46 +0000105 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
106 SDValue ArgValue;
107 CCValAssign &VA = ArgLocs[i];
108 // FIXME: We ignore the register assignments of AnalyzeFormalArguments
109 // because it doesn't know how to split a double into two i32 registers.
Owen Andersone50ed302009-08-10 22:56:29 +0000110 EVT ObjectVT = VA.getValVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000111 switch (ObjectVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000112 default: llvm_unreachable("Unhandled argument type!");
Owen Andersone50ed302009-08-10 22:56:29 +0000113 case EVT::i1:
114 case EVT::i8:
115 case EVT::i16:
116 case EVT::i32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000117 if (!Ins[i].Used) { // Argument is dead.
118 if (CurArgReg < ArgRegEnd) ++CurArgReg;
119 InVals.push_back(DAG.getUNDEF(ObjectVT));
120 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000121 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
122 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Andersone50ed302009-08-10 22:56:29 +0000123 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, EVT::i32);
124 if (ObjectVT != EVT::i32) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000125 unsigned AssertOp = ISD::AssertSext;
Owen Andersone50ed302009-08-10 22:56:29 +0000126 Arg = DAG.getNode(AssertOp, dl, EVT::i32, Arg,
Chris Lattner5a65b922008-03-17 05:41:48 +0000127 DAG.getValueType(ObjectVT));
Dale Johannesen39355f92009-02-04 02:34:38 +0000128 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000129 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000130 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000131 } else {
132 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
Owen Andersone50ed302009-08-10 22:56:29 +0000133 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, EVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000134 SDValue Load;
Owen Andersone50ed302009-08-10 22:56:29 +0000135 if (ObjectVT == EVT::i32) {
136 Load = DAG.getLoad(EVT::i32, dl, Chain, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000137 } else {
138 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
139
140 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000141 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Owen Andersone50ed302009-08-10 22:56:29 +0000142 FIPtr = DAG.getNode(ISD::ADD, dl, EVT::i32, FIPtr,
143 DAG.getConstant(Offset, EVT::i32));
144 Load = DAG.getExtLoad(LoadOp, dl, EVT::i32, Chain, FIPtr,
Chris Lattner5a65b922008-03-17 05:41:48 +0000145 NULL, 0, ObjectVT);
Dale Johannesen39355f92009-02-04 02:34:38 +0000146 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000147 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000148 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000149 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000150
Chris Lattner5a65b922008-03-17 05:41:48 +0000151 ArgOffset += 4;
152 break;
Owen Andersone50ed302009-08-10 22:56:29 +0000153 case EVT::f32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000154 if (!Ins[i].Used) { // Argument is dead.
155 if (CurArgReg < ArgRegEnd) ++CurArgReg;
156 InVals.push_back(DAG.getUNDEF(ObjectVT));
157 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000158 // FP value is passed in an integer register.
159 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
160 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Andersone50ed302009-08-10 22:56:29 +0000161 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, EVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000162
Owen Andersone50ed302009-08-10 22:56:29 +0000163 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::f32, Arg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000164 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000165 } else {
166 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
Owen Andersone50ed302009-08-10 22:56:29 +0000167 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, EVT::i32);
168 SDValue Load = DAG.getLoad(EVT::f32, dl, Chain, FIPtr, NULL, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000169 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000170 }
171 ArgOffset += 4;
172 break;
173
Owen Andersone50ed302009-08-10 22:56:29 +0000174 case EVT::i64:
175 case EVT::f64:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000176 if (!Ins[i].Used) { // Argument is dead.
177 if (CurArgReg < ArgRegEnd) ++CurArgReg;
178 if (CurArgReg < ArgRegEnd) ++CurArgReg;
179 InVals.push_back(DAG.getUNDEF(ObjectVT));
180 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000181 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000182 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
183 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
184 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Owen Andersone50ed302009-08-10 22:56:29 +0000185 HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, EVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000186 } else {
187 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
Owen Andersone50ed302009-08-10 22:56:29 +0000188 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, EVT::i32);
189 HiVal = DAG.getLoad(EVT::i32, dl, Chain, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000190 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000191
Dan Gohman475871a2008-07-27 21:46:04 +0000192 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000193 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
194 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
195 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Owen Andersone50ed302009-08-10 22:56:29 +0000196 LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, EVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000197 } else {
198 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
Owen Andersone50ed302009-08-10 22:56:29 +0000199 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, EVT::i32);
200 LoVal = DAG.getLoad(EVT::i32, dl, Chain, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000201 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000202
Chris Lattner5a65b922008-03-17 05:41:48 +0000203 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000204 SDValue WholeValue =
Owen Andersone50ed302009-08-10 22:56:29 +0000205 DAG.getNode(ISD::BUILD_PAIR, dl, EVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000206
Chris Lattner5a65b922008-03-17 05:41:48 +0000207 // If we want a double, do a bit convert.
Owen Andersone50ed302009-08-10 22:56:29 +0000208 if (ObjectVT == EVT::f64)
209 WholeValue = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000210
Dan Gohman98ca4f22009-08-05 01:29:28 +0000211 InVals.push_back(WholeValue);
Chris Lattner5a65b922008-03-17 05:41:48 +0000212 }
213 ArgOffset += 8;
214 break;
215 }
216 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000217
Chris Lattner5a65b922008-03-17 05:41:48 +0000218 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000219 if (isVarArg) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000220 // Remember the vararg offset for the va_start implementation.
221 VarArgsFrameOffset = ArgOffset;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000222
Eli Friedmana786c7b2009-07-19 19:53:46 +0000223 std::vector<SDValue> OutChains;
224
Chris Lattner5a65b922008-03-17 05:41:48 +0000225 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
226 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
227 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Andersone50ed302009-08-10 22:56:29 +0000228 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, EVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000229
230 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
Owen Andersone50ed302009-08-10 22:56:29 +0000231 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, EVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000232
Dale Johannesen39355f92009-02-04 02:34:38 +0000233 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, NULL, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000234 ArgOffset += 4;
235 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000236
237 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000238 OutChains.push_back(Chain);
Owen Andersone50ed302009-08-10 22:56:29 +0000239 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000240 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000241 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000242 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000243
Dan Gohman98ca4f22009-08-05 01:29:28 +0000244 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000245}
246
Dan Gohman98ca4f22009-08-05 01:29:28 +0000247SDValue
248SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
249 unsigned CallConv, bool isVarArg,
250 bool isTailCall,
251 const SmallVectorImpl<ISD::OutputArg> &Outs,
252 const SmallVectorImpl<ISD::InputArg> &Ins,
253 DebugLoc dl, SelectionDAG &DAG,
254 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner98949a62008-03-17 06:01:07 +0000255
Chris Lattner315123f2008-03-17 06:58:37 +0000256#if 0
257 // Analyze operands of the call, assigning locations to each operand.
258 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000259 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
260 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000261
Chris Lattner315123f2008-03-17 06:58:37 +0000262 // Get the size of the outgoing arguments stack space requirement.
263 unsigned ArgsSize = CCInfo.getNextStackOffset();
264 // FIXME: We can't use this until f64 is known to take two GPRs.
265#else
266 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000267
Chris Lattner5a65b922008-03-17 05:41:48 +0000268 // Count the size of the outgoing arguments.
269 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000270 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
271 switch (Outs[i].Val.getValueType().getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000272 default: llvm_unreachable("Unknown value type!");
Owen Andersone50ed302009-08-10 22:56:29 +0000273 case EVT::i1:
274 case EVT::i8:
275 case EVT::i16:
276 case EVT::i32:
277 case EVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000278 ArgsSize += 4;
279 break;
Owen Andersone50ed302009-08-10 22:56:29 +0000280 case EVT::i64:
281 case EVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000282 ArgsSize += 8;
283 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000284 }
285 }
286 if (ArgsSize > 4*6)
287 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
288 else
289 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000290#endif
291
Chris Lattner5a65b922008-03-17 05:41:48 +0000292 // Keep stack frames 8-byte aligned.
293 ArgsSize = (ArgsSize+7) & ~7;
294
Chris Lattnere563bbc2008-10-11 22:08:30 +0000295 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000296
Dan Gohman475871a2008-07-27 21:46:04 +0000297 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
298 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000299
Chris Lattner315123f2008-03-17 06:58:37 +0000300#if 0
301 // Walk the register/memloc assignments, inserting copies/loads.
302 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
303 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000304 SDValue Arg = Outs[i].Val;
Chris Lattner315123f2008-03-17 06:58:37 +0000305
306 // Promote the value if needed.
307 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000308 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000309 case CCValAssign::Full: break;
310 case CCValAssign::SExt:
311 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
312 break;
313 case CCValAssign::ZExt:
314 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
315 break;
316 case CCValAssign::AExt:
317 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
318 break;
319 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000320
321 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000322 // RegsToPass vector
323 if (VA.isRegLoc()) {
324 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
325 continue;
326 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000327
Chris Lattner315123f2008-03-17 06:58:37 +0000328 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000329
Chris Lattner315123f2008-03-17 06:58:37 +0000330 // Create a store off the stack pointer for this argument.
Owen Andersone50ed302009-08-10 22:56:29 +0000331 SDValue StackPtr = DAG.getRegister(SP::O6, EVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000332 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000333 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Andersone50ed302009-08-10 22:56:29 +0000334 PtrOff = DAG.getNode(ISD::ADD, EVT::i32, StackPtr, PtrOff);
Chris Lattner315123f2008-03-17 06:58:37 +0000335 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
336 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000337
338#else
Chris Lattner315123f2008-03-17 06:58:37 +0000339 static const unsigned ArgRegs[] = {
340 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
341 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000342 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000343
Dan Gohman98ca4f22009-08-05 01:29:28 +0000344 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
345 SDValue Val = Outs[i].Val;
Owen Andersone50ed302009-08-10 22:56:29 +0000346 EVT ObjectVT = Val.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000347 SDValue ValToStore(0, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000348 unsigned ObjSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000349 switch (ObjectVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000350 default: llvm_unreachable("Unhandled argument type!");
Owen Andersone50ed302009-08-10 22:56:29 +0000351 case EVT::i32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000352 ObjSize = 4;
353
Chris Lattner315123f2008-03-17 06:58:37 +0000354 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000355 ValToStore = Val;
356 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000357 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000358 }
359 break;
Owen Andersone50ed302009-08-10 22:56:29 +0000360 case EVT::f32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000361 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000362 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000363 ValToStore = Val;
364 } else {
365 // Convert this to a FP value in an int reg.
Owen Andersone50ed302009-08-10 22:56:29 +0000366 Val = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000367 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000368 }
369 break;
Owen Andersone50ed302009-08-10 22:56:29 +0000370 case EVT::f64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000371 ObjSize = 8;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000372 if (RegsToPass.size() >= 6) {
373 ValToStore = Val; // Whole thing is passed in memory.
374 break;
375 }
376
377 // Break into top and bottom parts by storing to the stack and loading
378 // out the parts as integers. Top part goes in a reg.
Owen Andersone50ed302009-08-10 22:56:29 +0000379 SDValue StackPtr = DAG.CreateStackTemporary(EVT::f64, EVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000380 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
381 Val, StackPtr, NULL, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000382 // Sparc is big-endian, so the high part comes first.
Owen Andersone50ed302009-08-10 22:56:29 +0000383 SDValue Hi = DAG.getLoad(EVT::i32, dl, Store, StackPtr, NULL, 0, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000384 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000385 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000386 DAG.getIntPtrConstant(4));
387 // Load the low part.
Owen Andersone50ed302009-08-10 22:56:29 +0000388 SDValue Lo = DAG.getLoad(EVT::i32, dl, Store, StackPtr, NULL, 0, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000389
390 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
391
392 if (RegsToPass.size() >= 6) {
393 ValToStore = Lo;
394 ArgOffset += 4;
395 ObjSize = 4;
396 } else {
397 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
398 }
399 break;
400 }
Owen Andersone50ed302009-08-10 22:56:29 +0000401 case EVT::i64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000402 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000403 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000404 ValToStore = Val; // Whole thing is passed in memory.
405 break;
406 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000407
Chris Lattner5a65b922008-03-17 05:41:48 +0000408 // Split the value into top and bottom part. Top part goes in a reg.
Owen Andersone50ed302009-08-10 22:56:29 +0000409 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32, Val,
410 DAG.getConstant(1, EVT::i32));
411 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32, Val,
412 DAG.getConstant(0, EVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000413 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000414
Chris Lattner315123f2008-03-17 06:58:37 +0000415 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000416 ValToStore = Lo;
417 ArgOffset += 4;
418 ObjSize = 4;
419 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000420 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000421 }
422 break;
423 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000424 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000425
Gabor Greifba36cb52008-08-28 21:40:38 +0000426 if (ValToStore.getNode()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000427 SDValue StackPtr = DAG.getRegister(SP::O6, EVT::i32);
428 SDValue PtrOff = DAG.getConstant(ArgOffset, EVT::i32);
429 PtrOff = DAG.getNode(ISD::ADD, dl, EVT::i32, StackPtr, PtrOff);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000430 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
431 PtrOff, NULL, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000432 }
433 ArgOffset += ObjSize;
434 }
Chris Lattner315123f2008-03-17 06:58:37 +0000435#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000436
Chris Lattner5a65b922008-03-17 05:41:48 +0000437 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000438 if (!MemOpChains.empty())
Owen Andersone50ed302009-08-10 22:56:29 +0000439 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000440 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000441
442 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000443 // chain and flag operands which copy the outgoing args into registers.
444 // The InFlag in necessary since all emited instructions must be
445 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000446 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000447 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
448 unsigned Reg = RegsToPass[i].first;
449 // Remap I0->I7 -> O0->O7.
450 if (Reg >= SP::I0 && Reg <= SP::I7)
451 Reg = Reg-SP::I0+SP::O0;
452
Dale Johannesen33c960f2009-02-04 20:06:27 +0000453 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000454 InFlag = Chain.getValue(1);
455 }
456
457 // If the callee is a GlobalAddress node (quite common, every direct call is)
458 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000459 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000460 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Andersone50ed302009-08-10 22:56:29 +0000461 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), EVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000462 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Andersone50ed302009-08-10 22:56:29 +0000463 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), EVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000464
Owen Andersone50ed302009-08-10 22:56:29 +0000465 std::vector<EVT> NodeTys;
466 NodeTys.push_back(EVT::Other); // Returns a chain
467 NodeTys.push_back(EVT::Flag); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000468 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000469 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000470 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000471
Chris Lattnere563bbc2008-10-11 22:08:30 +0000472 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
473 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000474 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000475
Chris Lattner98949a62008-03-17 06:01:07 +0000476 // Assign locations to each value returned by this call.
477 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000478 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000479 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000480
Dan Gohman98ca4f22009-08-05 01:29:28 +0000481 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000482
Chris Lattner98949a62008-03-17 06:01:07 +0000483 // Copy all of the result registers out of their specified physreg.
484 for (unsigned i = 0; i != RVLocs.size(); ++i) {
485 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000486
Chris Lattner98949a62008-03-17 06:01:07 +0000487 // Remap I0->I7 -> O0->O7.
488 if (Reg >= SP::I0 && Reg <= SP::I7)
489 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000490
Dale Johannesen33c960f2009-02-04 20:06:27 +0000491 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000492 RVLocs[i].getValVT(), InFlag).getValue(1);
493 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000494 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000495 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000496
Dan Gohman98ca4f22009-08-05 01:29:28 +0000497 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000498}
499
500
501
Chris Lattnerd23405e2008-03-17 03:21:36 +0000502//===----------------------------------------------------------------------===//
503// TargetLowering Implementation
504//===----------------------------------------------------------------------===//
505
506/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
507/// condition.
508static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
509 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000510 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000511 case ISD::SETEQ: return SPCC::ICC_E;
512 case ISD::SETNE: return SPCC::ICC_NE;
513 case ISD::SETLT: return SPCC::ICC_L;
514 case ISD::SETGT: return SPCC::ICC_G;
515 case ISD::SETLE: return SPCC::ICC_LE;
516 case ISD::SETGE: return SPCC::ICC_GE;
517 case ISD::SETULT: return SPCC::ICC_CS;
518 case ISD::SETULE: return SPCC::ICC_LEU;
519 case ISD::SETUGT: return SPCC::ICC_GU;
520 case ISD::SETUGE: return SPCC::ICC_CC;
521 }
522}
523
524/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
525/// FCC condition.
526static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
527 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000528 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000529 case ISD::SETEQ:
530 case ISD::SETOEQ: return SPCC::FCC_E;
531 case ISD::SETNE:
532 case ISD::SETUNE: return SPCC::FCC_NE;
533 case ISD::SETLT:
534 case ISD::SETOLT: return SPCC::FCC_L;
535 case ISD::SETGT:
536 case ISD::SETOGT: return SPCC::FCC_G;
537 case ISD::SETLE:
538 case ISD::SETOLE: return SPCC::FCC_LE;
539 case ISD::SETGE:
540 case ISD::SETOGE: return SPCC::FCC_GE;
541 case ISD::SETULT: return SPCC::FCC_UL;
542 case ISD::SETULE: return SPCC::FCC_ULE;
543 case ISD::SETUGT: return SPCC::FCC_UG;
544 case ISD::SETUGE: return SPCC::FCC_UGE;
545 case ISD::SETUO: return SPCC::FCC_U;
546 case ISD::SETO: return SPCC::FCC_O;
547 case ISD::SETONE: return SPCC::FCC_LG;
548 case ISD::SETUEQ: return SPCC::FCC_UE;
549 }
550}
551
Chris Lattnerd23405e2008-03-17 03:21:36 +0000552SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000553 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000554
Chris Lattnerd23405e2008-03-17 03:21:36 +0000555 // Set up the register classes.
Owen Andersone50ed302009-08-10 22:56:29 +0000556 addRegisterClass(EVT::i32, SP::IntRegsRegisterClass);
557 addRegisterClass(EVT::f32, SP::FPRegsRegisterClass);
558 addRegisterClass(EVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000559
560 // Turn FP extload into load/fextend
Owen Andersone50ed302009-08-10 22:56:29 +0000561 setLoadExtAction(ISD::EXTLOAD, EVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000562 // Sparc doesn't have i1 sign extending load
Owen Andersone50ed302009-08-10 22:56:29 +0000563 setLoadExtAction(ISD::SEXTLOAD, EVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000564 // Turn FP truncstore into trunc + store.
Owen Andersone50ed302009-08-10 22:56:29 +0000565 setTruncStoreAction(EVT::f64, EVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000566
567 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Andersone50ed302009-08-10 22:56:29 +0000568 setOperationAction(ISD::GlobalAddress, EVT::i32, Custom);
569 setOperationAction(ISD::GlobalTLSAddress, EVT::i32, Custom);
570 setOperationAction(ISD::ConstantPool , EVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000571
Chris Lattnerd23405e2008-03-17 03:21:36 +0000572 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Andersone50ed302009-08-10 22:56:29 +0000573 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i16, Expand);
574 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i8 , Expand);
575 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000576
577 // Sparc has no REM or DIVREM operations.
Owen Andersone50ed302009-08-10 22:56:29 +0000578 setOperationAction(ISD::UREM, EVT::i32, Expand);
579 setOperationAction(ISD::SREM, EVT::i32, Expand);
580 setOperationAction(ISD::SDIVREM, EVT::i32, Expand);
581 setOperationAction(ISD::UDIVREM, EVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000582
583 // Custom expand fp<->sint
Owen Andersone50ed302009-08-10 22:56:29 +0000584 setOperationAction(ISD::FP_TO_SINT, EVT::i32, Custom);
585 setOperationAction(ISD::SINT_TO_FP, EVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000586
587 // Expand fp<->uint
Owen Andersone50ed302009-08-10 22:56:29 +0000588 setOperationAction(ISD::FP_TO_UINT, EVT::i32, Expand);
589 setOperationAction(ISD::UINT_TO_FP, EVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000590
Owen Andersone50ed302009-08-10 22:56:29 +0000591 setOperationAction(ISD::BIT_CONVERT, EVT::f32, Expand);
592 setOperationAction(ISD::BIT_CONVERT, EVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000593
Chris Lattnerd23405e2008-03-17 03:21:36 +0000594 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +0000595 setOperationAction(ISD::SELECT, EVT::i32, Expand);
596 setOperationAction(ISD::SELECT, EVT::f32, Expand);
597 setOperationAction(ISD::SELECT, EVT::f64, Expand);
598 setOperationAction(ISD::SETCC, EVT::i32, Expand);
599 setOperationAction(ISD::SETCC, EVT::f32, Expand);
600 setOperationAction(ISD::SETCC, EVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000601
Chris Lattnerd23405e2008-03-17 03:21:36 +0000602 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Andersone50ed302009-08-10 22:56:29 +0000603 setOperationAction(ISD::BRCOND, EVT::Other, Expand);
604 setOperationAction(ISD::BRIND, EVT::Other, Expand);
605 setOperationAction(ISD::BR_JT, EVT::Other, Expand);
606 setOperationAction(ISD::BR_CC, EVT::i32, Custom);
607 setOperationAction(ISD::BR_CC, EVT::f32, Custom);
608 setOperationAction(ISD::BR_CC, EVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000609
Owen Andersone50ed302009-08-10 22:56:29 +0000610 setOperationAction(ISD::SELECT_CC, EVT::i32, Custom);
611 setOperationAction(ISD::SELECT_CC, EVT::f32, Custom);
612 setOperationAction(ISD::SELECT_CC, EVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000613
Chris Lattnerd23405e2008-03-17 03:21:36 +0000614 // SPARC has no intrinsics for these particular operations.
Owen Andersone50ed302009-08-10 22:56:29 +0000615 setOperationAction(ISD::MEMBARRIER, EVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000616
Owen Andersone50ed302009-08-10 22:56:29 +0000617 setOperationAction(ISD::FSIN , EVT::f64, Expand);
618 setOperationAction(ISD::FCOS , EVT::f64, Expand);
619 setOperationAction(ISD::FREM , EVT::f64, Expand);
620 setOperationAction(ISD::FSIN , EVT::f32, Expand);
621 setOperationAction(ISD::FCOS , EVT::f32, Expand);
622 setOperationAction(ISD::FREM , EVT::f32, Expand);
623 setOperationAction(ISD::CTPOP, EVT::i32, Expand);
624 setOperationAction(ISD::CTTZ , EVT::i32, Expand);
625 setOperationAction(ISD::CTLZ , EVT::i32, Expand);
626 setOperationAction(ISD::ROTL , EVT::i32, Expand);
627 setOperationAction(ISD::ROTR , EVT::i32, Expand);
628 setOperationAction(ISD::BSWAP, EVT::i32, Expand);
629 setOperationAction(ISD::FCOPYSIGN, EVT::f64, Expand);
630 setOperationAction(ISD::FCOPYSIGN, EVT::f32, Expand);
631 setOperationAction(ISD::FPOW , EVT::f64, Expand);
632 setOperationAction(ISD::FPOW , EVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000633
Owen Andersone50ed302009-08-10 22:56:29 +0000634 setOperationAction(ISD::SHL_PARTS, EVT::i32, Expand);
635 setOperationAction(ISD::SRA_PARTS, EVT::i32, Expand);
636 setOperationAction(ISD::SRL_PARTS, EVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000637
638 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Andersone50ed302009-08-10 22:56:29 +0000639 setOperationAction(ISD::UMUL_LOHI, EVT::i32, Expand);
640 setOperationAction(ISD::SMUL_LOHI, EVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000641
Chris Lattnerd23405e2008-03-17 03:21:36 +0000642 // We don't have line number support yet.
Owen Andersone50ed302009-08-10 22:56:29 +0000643 setOperationAction(ISD::DBG_STOPPOINT, EVT::Other, Expand);
644 setOperationAction(ISD::DEBUG_LOC, EVT::Other, Expand);
645 setOperationAction(ISD::DBG_LABEL, EVT::Other, Expand);
646 setOperationAction(ISD::EH_LABEL, EVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000647
Chris Lattnerd23405e2008-03-17 03:21:36 +0000648 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Andersone50ed302009-08-10 22:56:29 +0000649 setOperationAction(ISD::VASTART , EVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000650 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Andersone50ed302009-08-10 22:56:29 +0000651 setOperationAction(ISD::VAARG , EVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000652
Chris Lattnerd23405e2008-03-17 03:21:36 +0000653 // Use the default implementation.
Owen Andersone50ed302009-08-10 22:56:29 +0000654 setOperationAction(ISD::VACOPY , EVT::Other, Expand);
655 setOperationAction(ISD::VAEND , EVT::Other, Expand);
656 setOperationAction(ISD::STACKSAVE , EVT::Other, Expand);
657 setOperationAction(ISD::STACKRESTORE , EVT::Other, Expand);
658 setOperationAction(ISD::DYNAMIC_STACKALLOC, EVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000659
660 // No debug info support yet.
Owen Andersone50ed302009-08-10 22:56:29 +0000661 setOperationAction(ISD::DBG_STOPPOINT, EVT::Other, Expand);
662 setOperationAction(ISD::DBG_LABEL, EVT::Other, Expand);
663 setOperationAction(ISD::EH_LABEL, EVT::Other, Expand);
664 setOperationAction(ISD::DECLARE, EVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000665
Chris Lattnerd23405e2008-03-17 03:21:36 +0000666 setStackPointerRegisterToSaveRestore(SP::O6);
667
668 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Andersone50ed302009-08-10 22:56:29 +0000669 setOperationAction(ISD::CTPOP, EVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000670
Chris Lattnerd23405e2008-03-17 03:21:36 +0000671 computeRegisterProperties();
672}
673
674const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
675 switch (Opcode) {
676 default: return 0;
677 case SPISD::CMPICC: return "SPISD::CMPICC";
678 case SPISD::CMPFCC: return "SPISD::CMPFCC";
679 case SPISD::BRICC: return "SPISD::BRICC";
680 case SPISD::BRFCC: return "SPISD::BRFCC";
681 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
682 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
683 case SPISD::Hi: return "SPISD::Hi";
684 case SPISD::Lo: return "SPISD::Lo";
685 case SPISD::FTOI: return "SPISD::FTOI";
686 case SPISD::ITOF: return "SPISD::ITOF";
687 case SPISD::CALL: return "SPISD::CALL";
688 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
689 }
690}
691
692/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
693/// be zero. Op is expected to be a target specific node. Used by DAG
694/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000695void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000696 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000697 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000698 APInt &KnownOne,
699 const SelectionDAG &DAG,
700 unsigned Depth) const {
701 APInt KnownZero2, KnownOne2;
702 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000703
Chris Lattnerd23405e2008-03-17 03:21:36 +0000704 switch (Op.getOpcode()) {
705 default: break;
706 case SPISD::SELECT_ICC:
707 case SPISD::SELECT_FCC:
708 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
709 Depth+1);
710 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
711 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000712 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
713 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
714
Chris Lattnerd23405e2008-03-17 03:21:36 +0000715 // Only known if known in both the LHS and RHS.
716 KnownOne &= KnownOne2;
717 KnownZero &= KnownZero2;
718 break;
719 }
720}
721
Chris Lattnerd23405e2008-03-17 03:21:36 +0000722// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
723// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000724static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000725 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000726 if (isa<ConstantSDNode>(RHS) &&
727 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000728 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000729 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
730 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
731 (LHS.getOpcode() == SPISD::SELECT_FCC &&
732 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
733 isa<ConstantSDNode>(LHS.getOperand(0)) &&
734 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000735 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
736 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
Dan Gohman475871a2008-07-27 21:46:04 +0000737 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000738 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000739 LHS = CMPCC.getOperand(0);
740 RHS = CMPCC.getOperand(1);
741 }
742}
743
Dan Gohman475871a2008-07-27 21:46:04 +0000744static SDValue LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000745 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000746 // FIXME there isn't really any debug info here
747 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +0000748 SDValue GA = DAG.getTargetGlobalAddress(GV, EVT::i32);
749 SDValue Hi = DAG.getNode(SPISD::Hi, dl, EVT::i32, GA);
750 SDValue Lo = DAG.getNode(SPISD::Lo, dl, EVT::i32, GA);
751 return DAG.getNode(ISD::ADD, dl, EVT::i32, Lo, Hi);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000752}
753
Dan Gohman475871a2008-07-27 21:46:04 +0000754static SDValue LowerCONSTANTPOOL(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000755 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000756 // FIXME there isn't really any debug info here
757 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000758 Constant *C = N->getConstVal();
Owen Andersone50ed302009-08-10 22:56:29 +0000759 SDValue CP = DAG.getTargetConstantPool(C, EVT::i32, N->getAlignment());
760 SDValue Hi = DAG.getNode(SPISD::Hi, dl, EVT::i32, CP);
761 SDValue Lo = DAG.getNode(SPISD::Lo, dl, EVT::i32, CP);
762 return DAG.getNode(ISD::ADD, dl, EVT::i32, Lo, Hi);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000763}
764
Dan Gohman475871a2008-07-27 21:46:04 +0000765static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000766 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000767 // Convert the fp value to integer in an FP register.
Owen Andersone50ed302009-08-10 22:56:29 +0000768 assert(Op.getValueType() == EVT::i32);
769 Op = DAG.getNode(SPISD::FTOI, dl, EVT::f32, Op.getOperand(0));
770 return DAG.getNode(ISD::BIT_CONVERT, dl, EVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000771}
772
Dan Gohman475871a2008-07-27 21:46:04 +0000773static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000774 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +0000775 assert(Op.getOperand(0).getValueType() == EVT::i32);
776 SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000777 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000778 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000779}
780
Dan Gohman475871a2008-07-27 21:46:04 +0000781static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
782 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000783 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000784 SDValue LHS = Op.getOperand(2);
785 SDValue RHS = Op.getOperand(3);
786 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000787 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000788 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000789
Chris Lattnerd23405e2008-03-17 03:21:36 +0000790 // If this is a br_cc of a "setcc", and if the setcc got lowered into
791 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
792 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000793
Chris Lattnerd23405e2008-03-17 03:21:36 +0000794 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000795 SDValue CompareFlag;
Owen Andersone50ed302009-08-10 22:56:29 +0000796 if (LHS.getValueType() == EVT::i32) {
797 std::vector<EVT> VTs;
798 VTs.push_back(EVT::i32);
799 VTs.push_back(EVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000800 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000801 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000802 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
803 Opc = SPISD::BRICC;
804 } else {
Owen Andersone50ed302009-08-10 22:56:29 +0000805 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, EVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000806 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
807 Opc = SPISD::BRFCC;
808 }
Owen Andersone50ed302009-08-10 22:56:29 +0000809 return DAG.getNode(Opc, dl, EVT::Other, Chain, Dest,
810 DAG.getConstant(SPCC, EVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000811}
812
Dan Gohman475871a2008-07-27 21:46:04 +0000813static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
814 SDValue LHS = Op.getOperand(0);
815 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000816 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000817 SDValue TrueVal = Op.getOperand(2);
818 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000819 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000820 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000821
Chris Lattnerd23405e2008-03-17 03:21:36 +0000822 // If this is a select_cc of a "setcc", and if the setcc got lowered into
823 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
824 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000825
Dan Gohman475871a2008-07-27 21:46:04 +0000826 SDValue CompareFlag;
Owen Andersone50ed302009-08-10 22:56:29 +0000827 if (LHS.getValueType() == EVT::i32) {
828 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000829 VTs.push_back(LHS.getValueType()); // subcc returns a value
Owen Andersone50ed302009-08-10 22:56:29 +0000830 VTs.push_back(EVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000831 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000832 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000833 Opc = SPISD::SELECT_ICC;
834 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
835 } else {
Owen Andersone50ed302009-08-10 22:56:29 +0000836 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, EVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000837 Opc = SPISD::SELECT_FCC;
838 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
839 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000840 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Andersone50ed302009-08-10 22:56:29 +0000841 DAG.getConstant(SPCC, EVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000842}
843
Dan Gohman475871a2008-07-27 21:46:04 +0000844static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000845 SparcTargetLowering &TLI) {
846 // vastart just stores the address of the VarArgsFrameIndex slot into the
847 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000848 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +0000849 SDValue Offset = DAG.getNode(ISD::ADD, dl, EVT::i32,
850 DAG.getRegister(SP::I6, EVT::i32),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000851 DAG.getConstant(TLI.getVarArgsFrameOffset(),
Owen Andersone50ed302009-08-10 22:56:29 +0000852 EVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000853 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000854 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000855}
856
Dan Gohman475871a2008-07-27 21:46:04 +0000857static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000858 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000859 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000860 SDValue InChain = Node->getOperand(0);
861 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000862 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000863 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +0000864 SDValue VAList = DAG.getLoad(EVT::i32, dl, InChain, VAListPtr, SV, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000865 // Increment the pointer, VAList, to the next vaarg
Owen Andersone50ed302009-08-10 22:56:29 +0000866 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, EVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000867 DAG.getConstant(VT.getSizeInBits()/8,
Owen Andersone50ed302009-08-10 22:56:29 +0000868 EVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000869 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000870 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000871 VAListPtr, SV, 0);
872 // Load the actual argument out of the pointer VAList, unless this is an
873 // f64 load.
Owen Andersone50ed302009-08-10 22:56:29 +0000874 if (VT != EVT::f64)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000875 return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000876
Chris Lattnerd23405e2008-03-17 03:21:36 +0000877 // Otherwise, load it as i64, then do a bitconvert.
Owen Andersone50ed302009-08-10 22:56:29 +0000878 SDValue V = DAG.getLoad(EVT::i64, dl, InChain, VAList, NULL, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000879
Chris Lattnerd23405e2008-03-17 03:21:36 +0000880 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SDValue Ops[2] = {
Owen Andersone50ed302009-08-10 22:56:29 +0000882 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000883 V.getValue(1)
884 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000885 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000886}
887
Dan Gohman475871a2008-07-27 21:46:04 +0000888static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
889 SDValue Chain = Op.getOperand(0); // Legalize the chain.
890 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000891 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000892
Chris Lattnerd23405e2008-03-17 03:21:36 +0000893 unsigned SPReg = SP::O6;
Owen Andersone50ed302009-08-10 22:56:29 +0000894 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, EVT::i32);
895 SDValue NewSP = DAG.getNode(ISD::SUB, dl, EVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000896 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000897
Chris Lattnerd23405e2008-03-17 03:21:36 +0000898 // The resultant pointer is actually 16 words from the bottom of the stack,
899 // to provide a register spill area.
Owen Andersone50ed302009-08-10 22:56:29 +0000900 SDValue NewVal = DAG.getNode(ISD::ADD, dl, EVT::i32, NewSP,
901 DAG.getConstant(96, EVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000902 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000903 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000904}
905
Chris Lattnerd23405e2008-03-17 03:21:36 +0000906
Dan Gohman475871a2008-07-27 21:46:04 +0000907SDValue SparcTargetLowering::
908LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000909 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000910 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000911 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000912 case ISD::RETURNADDR: return SDValue();
913 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000914 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +0000915 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000916 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
917 case ISD::ConstantPool: return LowerCONSTANTPOOL(Op, DAG);
918 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
919 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
920 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
921 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
922 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
923 case ISD::VAARG: return LowerVAARG(Op, DAG);
924 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000925 }
926}
927
928MachineBasicBlock *
929SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000930 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000931 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
932 unsigned BROpcode;
933 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +0000934 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000935 // Figure out the conditional branch opcode to use for this select_cc.
936 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000937 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000938 case SP::SELECT_CC_Int_ICC:
939 case SP::SELECT_CC_FP_ICC:
940 case SP::SELECT_CC_DFP_ICC:
941 BROpcode = SP::BCOND;
942 break;
943 case SP::SELECT_CC_Int_FCC:
944 case SP::SELECT_CC_FP_FCC:
945 case SP::SELECT_CC_DFP_FCC:
946 BROpcode = SP::FBCOND;
947 break;
948 }
949
950 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000951
Chris Lattnerd23405e2008-03-17 03:21:36 +0000952 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
953 // control-flow pattern. The incoming instruction knows the destination vreg
954 // to set, the condition code register to branch on, the true/false values to
955 // select between, and a branch opcode to use.
956 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000957 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000958 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000959
Chris Lattnerd23405e2008-03-17 03:21:36 +0000960 // thisMBB:
961 // ...
962 // TrueVal = ...
963 // [f]bCC copy1MBB
964 // fallthrough --> copy0MBB
965 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000966 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000967 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
968 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesend552eee2009-02-13 02:31:35 +0000969 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000970 F->insert(It, copy0MBB);
971 F->insert(It, sinkMBB);
Dan Gohman0011dc42008-06-21 20:21:19 +0000972 // Update machine-CFG edges by transferring all successors of the current
Chris Lattnerd23405e2008-03-17 03:21:36 +0000973 // block to the new block which will contain the Phi node for the select.
Dan Gohman0011dc42008-06-21 20:21:19 +0000974 sinkMBB->transferSuccessors(BB);
975 // Next, add the true and fallthrough blocks as its successors.
Chris Lattnerd23405e2008-03-17 03:21:36 +0000976 BB->addSuccessor(copy0MBB);
977 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000978
Chris Lattnerd23405e2008-03-17 03:21:36 +0000979 // copy0MBB:
980 // %FalseValue = ...
981 // # fallthrough to sinkMBB
982 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000983
Chris Lattnerd23405e2008-03-17 03:21:36 +0000984 // Update machine-CFG edges
985 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000986
Chris Lattnerd23405e2008-03-17 03:21:36 +0000987 // sinkMBB:
988 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
989 // ...
990 BB = sinkMBB;
Dale Johannesend552eee2009-02-13 02:31:35 +0000991 BuildMI(BB, dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +0000992 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
993 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000994
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000995 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +0000996 return BB;
997}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +0000998
999//===----------------------------------------------------------------------===//
1000// Sparc Inline Assembly Support
1001//===----------------------------------------------------------------------===//
1002
1003/// getConstraintType - Given a constraint letter, return the type of
1004/// constraint it is for this target.
1005SparcTargetLowering::ConstraintType
1006SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1007 if (Constraint.size() == 1) {
1008 switch (Constraint[0]) {
1009 default: break;
1010 case 'r': return C_RegisterClass;
1011 }
1012 }
1013
1014 return TargetLowering::getConstraintType(Constraint);
1015}
1016
1017std::pair<unsigned, const TargetRegisterClass*>
1018SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001019 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001020 if (Constraint.size() == 1) {
1021 switch (Constraint[0]) {
1022 case 'r':
1023 return std::make_pair(0U, SP::IntRegsRegisterClass);
1024 }
1025 }
1026
1027 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1028}
1029
1030std::vector<unsigned> SparcTargetLowering::
1031getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001032 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001033 if (Constraint.size() != 1)
1034 return std::vector<unsigned>();
1035
1036 switch (Constraint[0]) {
1037 default: break;
1038 case 'r':
1039 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1040 SP::L4, SP::L5, SP::L6, SP::L7,
1041 SP::I0, SP::I1, SP::I2, SP::I3,
1042 SP::I4, SP::I5,
1043 SP::O0, SP::O1, SP::O2, SP::O3,
1044 SP::O4, SP::O5, SP::O7, 0);
1045 }
1046
1047 return std::vector<unsigned>();
1048}
Dan Gohman6520e202008-10-18 02:06:02 +00001049
1050bool
1051SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1052 // The Sparc target isn't yet aware of offsets.
1053 return false;
1054}
Bill Wendling20c568f2009-06-30 22:38:32 +00001055
Bill Wendlingb4202b82009-07-01 18:50:55 +00001056/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001057unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
1058 return 4;
1059}