blob: f5c29c8306b1766c695239c7fd95f2340c4225e6 [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,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000038 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000039 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 Anderson825b72b2009-08-11 20:47:22 +000075 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
76 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::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,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000084 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000085 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();
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000112 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 case MVT::i1:
114 case MVT::i8:
115 case MVT::i16:
116 case MVT::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 Anderson825b72b2009-08-11 20:47:22 +0000123 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
124 if (ObjectVT != MVT::i32) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000125 unsigned AssertOp = ISD::AssertSext;
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 Arg = DAG.getNode(AssertOp, dl, MVT::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 {
David Greene3f2bf852009-11-12 20:49:22 +0000132 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
133 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000135 SDValue Load;
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 if (ObjectVT == MVT::i32) {
David Greene54a7aa82010-02-15 16:57:02 +0000137 Load = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
138 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000139 } else {
140 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
141
142 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000143 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
145 DAG.getConstant(Offset, MVT::i32));
146 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
David Greene54a7aa82010-02-15 16:57:02 +0000147 NULL, 0, ObjectVT, false, false, 0);
Dale Johannesen39355f92009-02-04 02:34:38 +0000148 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000149 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000150 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000151 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000152
Chris Lattner5a65b922008-03-17 05:41:48 +0000153 ArgOffset += 4;
154 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 case MVT::f32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000156 if (!Ins[i].Used) { // Argument is dead.
157 if (CurArgReg < ArgRegEnd) ++CurArgReg;
158 InVals.push_back(DAG.getUNDEF(ObjectVT));
159 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000160 // FP value is passed in an integer register.
161 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
162 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000164
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Arg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000166 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000167 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000168 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
169 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000171 SDValue Load = DAG.getLoad(MVT::f32, dl, Chain, FIPtr, NULL, 0,
172 false, false, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000173 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000174 }
175 ArgOffset += 4;
176 break;
177
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 case MVT::i64:
179 case MVT::f64:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000180 if (!Ins[i].Used) { // Argument is dead.
181 if (CurArgReg < ArgRegEnd) ++CurArgReg;
182 if (CurArgReg < ArgRegEnd) ++CurArgReg;
183 InVals.push_back(DAG.getUNDEF(ObjectVT));
184 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000185 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000186 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
187 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
188 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000190 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000191 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
192 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000194 HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
195 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000196 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000197
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000199 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
200 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
201 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000203 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000204 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4,
205 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000207 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
208 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000209 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000210
Chris Lattner5a65b922008-03-17 05:41:48 +0000211 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000212 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000214
Chris Lattner5a65b922008-03-17 05:41:48 +0000215 // If we want a double, do a bit convert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 if (ObjectVT == MVT::f64)
217 WholeValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000218
Dan Gohman98ca4f22009-08-05 01:29:28 +0000219 InVals.push_back(WholeValue);
Chris Lattner5a65b922008-03-17 05:41:48 +0000220 }
221 ArgOffset += 8;
222 break;
223 }
224 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000225
Chris Lattner5a65b922008-03-17 05:41:48 +0000226 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000227 if (isVarArg) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000228 // Remember the vararg offset for the va_start implementation.
229 VarArgsFrameOffset = ArgOffset;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000230
Eli Friedmana786c7b2009-07-19 19:53:46 +0000231 std::vector<SDValue> OutChains;
232
Chris Lattner5a65b922008-03-17 05:41:48 +0000233 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
234 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
235 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000237
David Greene3f2bf852009-11-12 20:49:22 +0000238 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
239 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000241
David Greene54a7aa82010-02-15 16:57:02 +0000242 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, NULL, 0,
243 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000244 ArgOffset += 4;
245 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000246
247 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000248 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000250 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000251 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000252 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000253
Dan Gohman98ca4f22009-08-05 01:29:28 +0000254 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000255}
256
Dan Gohman98ca4f22009-08-05 01:29:28 +0000257SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000258SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000259 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000260 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000261 const SmallVectorImpl<ISD::OutputArg> &Outs,
262 const SmallVectorImpl<ISD::InputArg> &Ins,
263 DebugLoc dl, SelectionDAG &DAG,
264 SmallVectorImpl<SDValue> &InVals) {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000265 // Sparc target does not yet support tail call optimization.
266 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000267
Chris Lattner315123f2008-03-17 06:58:37 +0000268#if 0
269 // Analyze operands of the call, assigning locations to each operand.
270 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000271 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
272 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000273
Chris Lattner315123f2008-03-17 06:58:37 +0000274 // Get the size of the outgoing arguments stack space requirement.
275 unsigned ArgsSize = CCInfo.getNextStackOffset();
276 // FIXME: We can't use this until f64 is known to take two GPRs.
277#else
278 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000279
Chris Lattner5a65b922008-03-17 05:41:48 +0000280 // Count the size of the outgoing arguments.
281 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000282 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 switch (Outs[i].Val.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000284 default: llvm_unreachable("Unknown value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 case MVT::i1:
286 case MVT::i8:
287 case MVT::i16:
288 case MVT::i32:
289 case MVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000290 ArgsSize += 4;
291 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 case MVT::i64:
293 case MVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000294 ArgsSize += 8;
295 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000296 }
297 }
298 if (ArgsSize > 4*6)
299 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
300 else
301 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000302#endif
303
Chris Lattner5a65b922008-03-17 05:41:48 +0000304 // Keep stack frames 8-byte aligned.
305 ArgsSize = (ArgsSize+7) & ~7;
306
Chris Lattnere563bbc2008-10-11 22:08:30 +0000307 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000308
Dan Gohman475871a2008-07-27 21:46:04 +0000309 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
310 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000311
Chris Lattner315123f2008-03-17 06:58:37 +0000312#if 0
313 // Walk the register/memloc assignments, inserting copies/loads.
314 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
315 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000316 SDValue Arg = Outs[i].Val;
Chris Lattner315123f2008-03-17 06:58:37 +0000317
318 // Promote the value if needed.
319 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000320 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000321 case CCValAssign::Full: break;
322 case CCValAssign::SExt:
323 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
324 break;
325 case CCValAssign::ZExt:
326 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
327 break;
328 case CCValAssign::AExt:
329 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
330 break;
331 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000332
333 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000334 // RegsToPass vector
335 if (VA.isRegLoc()) {
336 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
337 continue;
338 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000339
Chris Lattner315123f2008-03-17 06:58:37 +0000340 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000341
Chris Lattner315123f2008-03-17 06:58:37 +0000342 // Create a store off the stack pointer for this argument.
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000344 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000345 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
David Greene54a7aa82010-02-15 16:57:02 +0000347 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0,
348 false, false, 0));
Chris Lattner315123f2008-03-17 06:58:37 +0000349 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000350
351#else
Chris Lattner315123f2008-03-17 06:58:37 +0000352 static const unsigned ArgRegs[] = {
353 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
354 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000355 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000356
Dan Gohman98ca4f22009-08-05 01:29:28 +0000357 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
358 SDValue Val = Outs[i].Val;
Owen Andersone50ed302009-08-10 22:56:29 +0000359 EVT ObjectVT = Val.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000360 SDValue ValToStore(0, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000361 unsigned ObjSize;
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000363 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 case MVT::i32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000365 ObjSize = 4;
366
Chris Lattner315123f2008-03-17 06:58:37 +0000367 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000368 ValToStore = Val;
369 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000370 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000371 }
372 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 case MVT::f32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000374 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000375 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000376 ValToStore = Val;
377 } else {
378 // Convert this to a FP value in an int reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 Val = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000380 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000381 }
382 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 case MVT::f64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000384 ObjSize = 8;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000385 if (RegsToPass.size() >= 6) {
386 ValToStore = Val; // Whole thing is passed in memory.
387 break;
388 }
389
390 // Break into top and bottom parts by storing to the stack and loading
391 // out the parts as integers. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000393 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
David Greene54a7aa82010-02-15 16:57:02 +0000394 Val, StackPtr, NULL, 0,
395 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000396 // Sparc is big-endian, so the high part comes first.
David Greene54a7aa82010-02-15 16:57:02 +0000397 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0,
398 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000399 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000400 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000401 DAG.getIntPtrConstant(4));
402 // Load the low part.
David Greene54a7aa82010-02-15 16:57:02 +0000403 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0,
404 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000405
406 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
407
408 if (RegsToPass.size() >= 6) {
409 ValToStore = Lo;
410 ArgOffset += 4;
411 ObjSize = 4;
412 } else {
413 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
414 }
415 break;
416 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 case MVT::i64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000418 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000419 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000420 ValToStore = Val; // Whole thing is passed in memory.
421 break;
422 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000423
Chris Lattner5a65b922008-03-17 05:41:48 +0000424 // Split the value into top and bottom part. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
426 DAG.getConstant(1, MVT::i32));
427 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
428 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000429 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000430
Chris Lattner315123f2008-03-17 06:58:37 +0000431 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000432 ValToStore = Lo;
433 ArgOffset += 4;
434 ObjSize = 4;
435 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000436 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000437 }
438 break;
439 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000440 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000441
Gabor Greifba36cb52008-08-28 21:40:38 +0000442 if (ValToStore.getNode()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
444 SDValue PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
445 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000446 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
David Greene54a7aa82010-02-15 16:57:02 +0000447 PtrOff, NULL, 0,
448 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000449 }
450 ArgOffset += ObjSize;
451 }
Chris Lattner315123f2008-03-17 06:58:37 +0000452#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000453
Chris Lattner5a65b922008-03-17 05:41:48 +0000454 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000455 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000457 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000458
459 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000460 // chain and flag operands which copy the outgoing args into registers.
461 // The InFlag in necessary since all emited instructions must be
462 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000463 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000464 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
465 unsigned Reg = RegsToPass[i].first;
466 // Remap I0->I7 -> O0->O7.
467 if (Reg >= SP::I0 && Reg <= SP::I7)
468 Reg = Reg-SP::I0+SP::O0;
469
Dale Johannesen33c960f2009-02-04 20:06:27 +0000470 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000471 InFlag = Chain.getValue(1);
472 }
473
474 // If the callee is a GlobalAddress node (quite common, every direct call is)
475 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000476 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000477 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000479 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000481
Owen Andersone50ed302009-08-10 22:56:29 +0000482 std::vector<EVT> NodeTys;
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 NodeTys.push_back(MVT::Other); // Returns a chain
484 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000485 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000486 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000487 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000488
Chris Lattnere563bbc2008-10-11 22:08:30 +0000489 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
490 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000491 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000492
Chris Lattner98949a62008-03-17 06:01:07 +0000493 // Assign locations to each value returned by this call.
494 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000495 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000496 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000497
Dan Gohman98ca4f22009-08-05 01:29:28 +0000498 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000499
Chris Lattner98949a62008-03-17 06:01:07 +0000500 // Copy all of the result registers out of their specified physreg.
501 for (unsigned i = 0; i != RVLocs.size(); ++i) {
502 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000503
Chris Lattner98949a62008-03-17 06:01:07 +0000504 // Remap I0->I7 -> O0->O7.
505 if (Reg >= SP::I0 && Reg <= SP::I7)
506 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000507
Dale Johannesen33c960f2009-02-04 20:06:27 +0000508 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000509 RVLocs[i].getValVT(), InFlag).getValue(1);
510 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000511 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000512 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000513
Dan Gohman98ca4f22009-08-05 01:29:28 +0000514 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000515}
516
517
518
Chris Lattnerd23405e2008-03-17 03:21:36 +0000519//===----------------------------------------------------------------------===//
520// TargetLowering Implementation
521//===----------------------------------------------------------------------===//
522
523/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
524/// condition.
525static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
526 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000527 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000528 case ISD::SETEQ: return SPCC::ICC_E;
529 case ISD::SETNE: return SPCC::ICC_NE;
530 case ISD::SETLT: return SPCC::ICC_L;
531 case ISD::SETGT: return SPCC::ICC_G;
532 case ISD::SETLE: return SPCC::ICC_LE;
533 case ISD::SETGE: return SPCC::ICC_GE;
534 case ISD::SETULT: return SPCC::ICC_CS;
535 case ISD::SETULE: return SPCC::ICC_LEU;
536 case ISD::SETUGT: return SPCC::ICC_GU;
537 case ISD::SETUGE: return SPCC::ICC_CC;
538 }
539}
540
541/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
542/// FCC condition.
543static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
544 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000545 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000546 case ISD::SETEQ:
547 case ISD::SETOEQ: return SPCC::FCC_E;
548 case ISD::SETNE:
549 case ISD::SETUNE: return SPCC::FCC_NE;
550 case ISD::SETLT:
551 case ISD::SETOLT: return SPCC::FCC_L;
552 case ISD::SETGT:
553 case ISD::SETOGT: return SPCC::FCC_G;
554 case ISD::SETLE:
555 case ISD::SETOLE: return SPCC::FCC_LE;
556 case ISD::SETGE:
557 case ISD::SETOGE: return SPCC::FCC_GE;
558 case ISD::SETULT: return SPCC::FCC_UL;
559 case ISD::SETULE: return SPCC::FCC_ULE;
560 case ISD::SETUGT: return SPCC::FCC_UG;
561 case ISD::SETUGE: return SPCC::FCC_UGE;
562 case ISD::SETUO: return SPCC::FCC_U;
563 case ISD::SETO: return SPCC::FCC_O;
564 case ISD::SETONE: return SPCC::FCC_LG;
565 case ISD::SETUEQ: return SPCC::FCC_UE;
566 }
567}
568
Chris Lattnerd23405e2008-03-17 03:21:36 +0000569SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000570 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000571
Chris Lattnerd23405e2008-03-17 03:21:36 +0000572 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
574 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
575 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000576
577 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000579 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000581 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000583
584 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
586 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
587 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000588
Chris Lattnerd23405e2008-03-17 03:21:36 +0000589 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
591 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
592 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000593
594 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::UREM, MVT::i32, Expand);
596 setOperationAction(ISD::SREM, MVT::i32, Expand);
597 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
598 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000599
600 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
602 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000603
604 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000605 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
606 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000607
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
609 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000610
Chris Lattnerd23405e2008-03-17 03:21:36 +0000611 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::SELECT, MVT::i32, Expand);
613 setOperationAction(ISD::SELECT, MVT::f32, Expand);
614 setOperationAction(ISD::SELECT, MVT::f64, Expand);
615 setOperationAction(ISD::SETCC, MVT::i32, Expand);
616 setOperationAction(ISD::SETCC, MVT::f32, Expand);
617 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000618
Chris Lattnerd23405e2008-03-17 03:21:36 +0000619 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
621 setOperationAction(ISD::BRIND, MVT::Other, Expand);
622 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
623 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
624 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
625 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000626
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
628 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
629 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000630
Chris Lattnerd23405e2008-03-17 03:21:36 +0000631 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000633
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::FSIN , MVT::f64, Expand);
635 setOperationAction(ISD::FCOS , MVT::f64, Expand);
636 setOperationAction(ISD::FREM , MVT::f64, Expand);
637 setOperationAction(ISD::FSIN , MVT::f32, Expand);
638 setOperationAction(ISD::FCOS , MVT::f32, Expand);
639 setOperationAction(ISD::FREM , MVT::f32, Expand);
640 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
641 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
642 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
643 setOperationAction(ISD::ROTL , MVT::i32, Expand);
644 setOperationAction(ISD::ROTR , MVT::i32, Expand);
645 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
647 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
648 setOperationAction(ISD::FPOW , MVT::f64, Expand);
649 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
652 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
653 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000654
655 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
657 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000658
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000660
Chris Lattnerd23405e2008-03-17 03:21:36 +0000661 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000663 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000665
Chris Lattnerd23405e2008-03-17 03:21:36 +0000666 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
668 setOperationAction(ISD::VAEND , MVT::Other, Expand);
669 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
670 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
671 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000672
673 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000675
Chris Lattnerd23405e2008-03-17 03:21:36 +0000676 setStackPointerRegisterToSaveRestore(SP::O6);
677
678 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000680
Chris Lattnerd23405e2008-03-17 03:21:36 +0000681 computeRegisterProperties();
682}
683
684const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
685 switch (Opcode) {
686 default: return 0;
687 case SPISD::CMPICC: return "SPISD::CMPICC";
688 case SPISD::CMPFCC: return "SPISD::CMPFCC";
689 case SPISD::BRICC: return "SPISD::BRICC";
690 case SPISD::BRFCC: return "SPISD::BRFCC";
691 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
692 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
693 case SPISD::Hi: return "SPISD::Hi";
694 case SPISD::Lo: return "SPISD::Lo";
695 case SPISD::FTOI: return "SPISD::FTOI";
696 case SPISD::ITOF: return "SPISD::ITOF";
697 case SPISD::CALL: return "SPISD::CALL";
698 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
699 }
700}
701
702/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
703/// be zero. Op is expected to be a target specific node. Used by DAG
704/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000705void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000706 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000707 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000708 APInt &KnownOne,
709 const SelectionDAG &DAG,
710 unsigned Depth) const {
711 APInt KnownZero2, KnownOne2;
712 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000713
Chris Lattnerd23405e2008-03-17 03:21:36 +0000714 switch (Op.getOpcode()) {
715 default: break;
716 case SPISD::SELECT_ICC:
717 case SPISD::SELECT_FCC:
718 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
719 Depth+1);
720 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
721 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000722 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
723 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
724
Chris Lattnerd23405e2008-03-17 03:21:36 +0000725 // Only known if known in both the LHS and RHS.
726 KnownOne &= KnownOne2;
727 KnownZero &= KnownZero2;
728 break;
729 }
730}
731
Chris Lattnerd23405e2008-03-17 03:21:36 +0000732// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
733// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000734static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000735 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000736 if (isa<ConstantSDNode>(RHS) &&
737 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000738 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000739 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
740 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
741 (LHS.getOpcode() == SPISD::SELECT_FCC &&
742 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
743 isa<ConstantSDNode>(LHS.getOperand(0)) &&
744 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000745 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
746 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
Dan Gohman475871a2008-07-27 21:46:04 +0000747 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000748 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000749 LHS = CMPCC.getOperand(0);
750 RHS = CMPCC.getOperand(1);
751 }
752}
753
Chris Lattnerdb486a62009-09-15 17:46:24 +0000754SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
755 SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000756 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000757 // FIXME there isn't really any debug info here
758 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
760 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
761 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000762
763 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
764 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
765
766 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
767 getPointerTy());
768 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
769 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
770 GlobalBase, RelAddr);
771 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene54a7aa82010-02-15 16:57:02 +0000772 AbsAddr, NULL, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000773}
774
Chris Lattnerdb486a62009-09-15 17:46:24 +0000775SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
776 SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000777 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000778 // FIXME there isn't really any debug info here
779 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000780 Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
782 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
783 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000784 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
785 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
786
787 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
788 getPointerTy());
789 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
790 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
791 GlobalBase, RelAddr);
792 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene54a7aa82010-02-15 16:57:02 +0000793 AbsAddr, NULL, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000794}
795
Dan Gohman475871a2008-07-27 21:46:04 +0000796static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000797 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000798 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 assert(Op.getValueType() == MVT::i32);
800 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
801 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000802}
803
Dan Gohman475871a2008-07-27 21:46:04 +0000804static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000805 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 assert(Op.getOperand(0).getValueType() == MVT::i32);
807 SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000808 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000809 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000810}
811
Dan Gohman475871a2008-07-27 21:46:04 +0000812static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
813 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000814 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000815 SDValue LHS = Op.getOperand(2);
816 SDValue RHS = Op.getOperand(3);
817 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000818 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000819 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000820
Chris Lattnerd23405e2008-03-17 03:21:36 +0000821 // If this is a br_cc of a "setcc", and if the setcc got lowered into
822 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
823 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000824
Chris Lattnerd23405e2008-03-17 03:21:36 +0000825 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000826 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000828 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 VTs.push_back(MVT::i32);
830 VTs.push_back(MVT::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 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
834 Opc = SPISD::BRICC;
835 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000837 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
838 Opc = SPISD::BRFCC;
839 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
841 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000842}
843
Dan Gohman475871a2008-07-27 21:46:04 +0000844static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
845 SDValue LHS = Op.getOperand(0);
846 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000847 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000848 SDValue TrueVal = Op.getOperand(2);
849 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000850 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000851 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000852
Chris Lattnerd23405e2008-03-17 03:21:36 +0000853 // If this is a select_cc of a "setcc", and if the setcc got lowered into
854 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
855 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000856
Dan Gohman475871a2008-07-27 21:46:04 +0000857 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000859 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000860 VTs.push_back(LHS.getValueType()); // subcc returns a value
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000862 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000863 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000864 Opc = SPISD::SELECT_ICC;
865 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
866 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000868 Opc = SPISD::SELECT_FCC;
869 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
870 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000871 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000873}
874
Dan Gohman475871a2008-07-27 21:46:04 +0000875static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000876 SparcTargetLowering &TLI) {
877 // vastart just stores the address of the VarArgsFrameIndex slot into the
878 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000879 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 SDValue Offset = DAG.getNode(ISD::ADD, dl, MVT::i32,
881 DAG.getRegister(SP::I6, MVT::i32),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000882 DAG.getConstant(TLI.getVarArgsFrameOffset(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000884 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene54a7aa82010-02-15 16:57:02 +0000885 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0,
886 false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000887}
888
Dan Gohman475871a2008-07-27 21:46:04 +0000889static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000890 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000891 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000892 SDValue InChain = Node->getOperand(0);
893 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000894 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000895 DebugLoc dl = Node->getDebugLoc();
David Greene54a7aa82010-02-15 16:57:02 +0000896 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr, SV, 0,
897 false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000898 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000900 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000902 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000903 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
David Greene54a7aa82010-02-15 16:57:02 +0000904 VAListPtr, SV, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000905 // Load the actual argument out of the pointer VAList, unless this is an
906 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 if (VT != MVT::f64)
David Greene54a7aa82010-02-15 16:57:02 +0000908 return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000909
Chris Lattnerd23405e2008-03-17 03:21:36 +0000910 // Otherwise, load it as i64, then do a bitconvert.
David Greene54a7aa82010-02-15 16:57:02 +0000911 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, NULL, 0,
912 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000913
Chris Lattnerd23405e2008-03-17 03:21:36 +0000914 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000915 SDValue Ops[2] = {
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000917 V.getValue(1)
918 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000919 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000920}
921
Dan Gohman475871a2008-07-27 21:46:04 +0000922static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
923 SDValue Chain = Op.getOperand(0); // Legalize the chain.
924 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000925 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000926
Chris Lattnerd23405e2008-03-17 03:21:36 +0000927 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
929 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000930 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000931
Chris Lattnerd23405e2008-03-17 03:21:36 +0000932 // The resultant pointer is actually 16 words from the bottom of the stack,
933 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +0000934 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
935 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000937 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000938}
939
Chris Lattnerd23405e2008-03-17 03:21:36 +0000940
Dan Gohman475871a2008-07-27 21:46:04 +0000941SDValue SparcTargetLowering::
942LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000943 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000944 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000945 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000946 case ISD::RETURNADDR: return SDValue();
947 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000948 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +0000949 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +0000950 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
951 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000952 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
953 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
954 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
955 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
956 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
957 case ISD::VAARG: return LowerVAARG(Op, DAG);
958 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000959 }
960}
961
962MachineBasicBlock *
963SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000964 MachineBasicBlock *BB,
965 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000966 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
967 unsigned BROpcode;
968 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +0000969 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000970 // Figure out the conditional branch opcode to use for this select_cc.
971 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000972 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000973 case SP::SELECT_CC_Int_ICC:
974 case SP::SELECT_CC_FP_ICC:
975 case SP::SELECT_CC_DFP_ICC:
976 BROpcode = SP::BCOND;
977 break;
978 case SP::SELECT_CC_Int_FCC:
979 case SP::SELECT_CC_FP_FCC:
980 case SP::SELECT_CC_DFP_FCC:
981 BROpcode = SP::FBCOND;
982 break;
983 }
984
985 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000986
Chris Lattnerd23405e2008-03-17 03:21:36 +0000987 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
988 // control-flow pattern. The incoming instruction knows the destination vreg
989 // to set, the condition code register to branch on, the true/false values to
990 // select between, and a branch opcode to use.
991 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000992 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000993 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000994
Chris Lattnerd23405e2008-03-17 03:21:36 +0000995 // thisMBB:
996 // ...
997 // TrueVal = ...
998 // [f]bCC copy1MBB
999 // fallthrough --> copy0MBB
1000 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001001 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001002 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1003 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesend552eee2009-02-13 02:31:35 +00001004 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001005 F->insert(It, copy0MBB);
1006 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00001007 // Update machine-CFG edges by first adding all successors of the current
Chris Lattnerd23405e2008-03-17 03:21:36 +00001008 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00001009 // Also inform sdisel of the edge changes.
1010 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
1011 E = BB->succ_end(); I != E; ++I) {
1012 EM->insert(std::make_pair(*I, sinkMBB));
1013 sinkMBB->addSuccessor(*I);
1014 }
1015 // Next, remove all successors of the current block, and add the true
1016 // and fallthrough blocks as its successors.
1017 while (!BB->succ_empty())
1018 BB->removeSuccessor(BB->succ_begin());
Dan Gohman0011dc42008-06-21 20:21:19 +00001019 // Next, add the true and fallthrough blocks as its successors.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001020 BB->addSuccessor(copy0MBB);
1021 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001022
Chris Lattnerd23405e2008-03-17 03:21:36 +00001023 // copy0MBB:
1024 // %FalseValue = ...
1025 // # fallthrough to sinkMBB
1026 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001027
Chris Lattnerd23405e2008-03-17 03:21:36 +00001028 // Update machine-CFG edges
1029 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001030
Chris Lattnerd23405e2008-03-17 03:21:36 +00001031 // sinkMBB:
1032 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1033 // ...
1034 BB = sinkMBB;
Dale Johannesend552eee2009-02-13 02:31:35 +00001035 BuildMI(BB, dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001036 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1037 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001038
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001039 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001040 return BB;
1041}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001042
1043//===----------------------------------------------------------------------===//
1044// Sparc Inline Assembly Support
1045//===----------------------------------------------------------------------===//
1046
1047/// getConstraintType - Given a constraint letter, return the type of
1048/// constraint it is for this target.
1049SparcTargetLowering::ConstraintType
1050SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1051 if (Constraint.size() == 1) {
1052 switch (Constraint[0]) {
1053 default: break;
1054 case 'r': return C_RegisterClass;
1055 }
1056 }
1057
1058 return TargetLowering::getConstraintType(Constraint);
1059}
1060
1061std::pair<unsigned, const TargetRegisterClass*>
1062SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001063 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001064 if (Constraint.size() == 1) {
1065 switch (Constraint[0]) {
1066 case 'r':
1067 return std::make_pair(0U, SP::IntRegsRegisterClass);
1068 }
1069 }
1070
1071 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1072}
1073
1074std::vector<unsigned> SparcTargetLowering::
1075getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001076 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001077 if (Constraint.size() != 1)
1078 return std::vector<unsigned>();
1079
1080 switch (Constraint[0]) {
1081 default: break;
1082 case 'r':
1083 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1084 SP::L4, SP::L5, SP::L6, SP::L7,
1085 SP::I0, SP::I1, SP::I2, SP::I3,
1086 SP::I4, SP::I5,
1087 SP::O0, SP::O1, SP::O2, SP::O3,
1088 SP::O4, SP::O5, SP::O7, 0);
1089 }
1090
1091 return std::vector<unsigned>();
1092}
Dan Gohman6520e202008-10-18 02:06:02 +00001093
1094bool
1095SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1096 // The Sparc target isn't yet aware of offsets.
1097 return false;
1098}
Bill Wendling20c568f2009-06-30 22:38:32 +00001099
Bill Wendlingb4202b82009-07-01 18:50:55 +00001100/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001101unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001102 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001103}