blob: fcf338e7acdf9788afa4d7ef5d0c12850ef0acea [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"
Dan Gohman1e93df62010-04-17 14:41:14 +000017#include "SparcMachineFunctionInfo.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000018#include "llvm/Function.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000019#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000025#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikov0eefda12008-10-10 20:28:10 +000026#include "llvm/ADT/VectorExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000028using namespace llvm;
29
Chris Lattner5a65b922008-03-17 05:41:48 +000030
31//===----------------------------------------------------------------------===//
32// Calling Convention Implementation
33//===----------------------------------------------------------------------===//
34
35#include "SparcGenCallingConv.inc"
36
Dan Gohman98ca4f22009-08-05 01:29:28 +000037SDValue
38SparcTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000039 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000040 const SmallVectorImpl<ISD::OutputArg> &Outs,
41 DebugLoc dl, SelectionDAG &DAG) {
42
Chris Lattner5a65b922008-03-17 05:41:48 +000043 // CCValAssign - represent the assignment of the return value to locations.
44 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000045
Chris Lattner5a65b922008-03-17 05:41:48 +000046 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +000047 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
48 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000049
Dan Gohman98ca4f22009-08-05 01:29:28 +000050 // Analize return values.
51 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000052
Chris Lattner5a65b922008-03-17 05:41:48 +000053 // If this is the first return lowered for this function, add the regs to the
54 // liveout set for the function.
55 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
56 for (unsigned i = 0; i != RVLocs.size(); ++i)
57 if (RVLocs[i].isRegLoc())
58 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
59 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000060
Dan Gohman475871a2008-07-27 21:46:04 +000061 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +000062
63 // Copy the result values into the output registers.
64 for (unsigned i = 0; i != RVLocs.size(); ++i) {
65 CCValAssign &VA = RVLocs[i];
66 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +000067
Dale Johannesena05dca42009-02-04 23:02:30 +000068 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +000069 Outs[i].Val, Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +000070
Chris Lattner5a65b922008-03-17 05:41:48 +000071 // Guarantee that all emitted copies are stuck together with flags.
72 Flag = Chain.getValue(1);
73 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000074
Gabor Greifba36cb52008-08-28 21:40:38 +000075 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +000076 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
77 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner5a65b922008-03-17 05:41:48 +000078}
79
Dan Gohman98ca4f22009-08-05 01:29:28 +000080/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
81/// passed in either one or two GPRs, including FP values. TODO: we should
82/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +000083SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000084SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000085 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000086 const SmallVectorImpl<ISD::InputArg>
87 &Ins,
88 DebugLoc dl, SelectionDAG &DAG,
89 SmallVectorImpl<SDValue> &InVals) {
90
Chris Lattner5a65b922008-03-17 05:41:48 +000091 MachineFunction &MF = DAG.getMachineFunction();
92 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +000093 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +000094
95 // Assign locations to all of the incoming arguments.
96 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +000097 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
98 ArgLocs, *DAG.getContext());
99 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000100
Chris Lattner5a65b922008-03-17 05:41:48 +0000101 static const unsigned ArgRegs[] = {
102 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
103 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000104 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
105 unsigned ArgOffset = 68;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000106
Eli Friedmana786c7b2009-07-19 19:53:46 +0000107 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
108 SDValue ArgValue;
109 CCValAssign &VA = ArgLocs[i];
110 // FIXME: We ignore the register assignments of AnalyzeFormalArguments
111 // because it doesn't know how to split a double into two i32 registers.
Owen Andersone50ed302009-08-10 22:56:29 +0000112 EVT ObjectVT = VA.getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000114 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 case MVT::i1:
116 case MVT::i8:
117 case MVT::i16:
118 case MVT::i32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000119 if (!Ins[i].Used) { // Argument is dead.
120 if (CurArgReg < ArgRegEnd) ++CurArgReg;
121 InVals.push_back(DAG.getUNDEF(ObjectVT));
122 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000123 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
124 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
126 if (ObjectVT != MVT::i32) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000127 unsigned AssertOp = ISD::AssertSext;
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 Arg = DAG.getNode(AssertOp, dl, MVT::i32, Arg,
Chris Lattner5a65b922008-03-17 05:41:48 +0000129 DAG.getValueType(ObjectVT));
Dale Johannesen39355f92009-02-04 02:34:38 +0000130 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000131 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000132 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000133 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000134 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
135 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000137 SDValue Load;
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 if (ObjectVT == MVT::i32) {
David Greene54a7aa82010-02-15 16:57:02 +0000139 Load = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
140 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000141 } else {
142 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
143
144 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000145 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
147 DAG.getConstant(Offset, MVT::i32));
148 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
David Greene54a7aa82010-02-15 16:57:02 +0000149 NULL, 0, ObjectVT, false, false, 0);
Dale Johannesen39355f92009-02-04 02:34:38 +0000150 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000151 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000152 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000153 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000154
Chris Lattner5a65b922008-03-17 05:41:48 +0000155 ArgOffset += 4;
156 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 case MVT::f32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000158 if (!Ins[i].Used) { // Argument is dead.
159 if (CurArgReg < ArgRegEnd) ++CurArgReg;
160 InVals.push_back(DAG.getUNDEF(ObjectVT));
161 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000162 // FP value is passed in an integer register.
163 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
164 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000166
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Arg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000168 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000169 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000170 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
171 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000173 SDValue Load = DAG.getLoad(MVT::f32, dl, Chain, FIPtr, NULL, 0,
174 false, false, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000175 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000176 }
177 ArgOffset += 4;
178 break;
179
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 case MVT::i64:
181 case MVT::f64:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000182 if (!Ins[i].Used) { // Argument is dead.
183 if (CurArgReg < ArgRegEnd) ++CurArgReg;
184 if (CurArgReg < ArgRegEnd) ++CurArgReg;
185 InVals.push_back(DAG.getUNDEF(ObjectVT));
186 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000188 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
189 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
190 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000192 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000193 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
194 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000196 HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
197 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000198 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000199
Dan Gohman475871a2008-07-27 21:46:04 +0000200 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000201 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
202 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
203 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000205 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000206 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4,
207 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000209 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
210 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000211 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000212
Chris Lattner5a65b922008-03-17 05:41:48 +0000213 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000214 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000216
Chris Lattner5a65b922008-03-17 05:41:48 +0000217 // If we want a double, do a bit convert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 if (ObjectVT == MVT::f64)
219 WholeValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000220
Dan Gohman98ca4f22009-08-05 01:29:28 +0000221 InVals.push_back(WholeValue);
Chris Lattner5a65b922008-03-17 05:41:48 +0000222 }
223 ArgOffset += 8;
224 break;
225 }
226 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000227
Chris Lattner5a65b922008-03-17 05:41:48 +0000228 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000229 if (isVarArg) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000230 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000231 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000232
Eli Friedmana786c7b2009-07-19 19:53:46 +0000233 std::vector<SDValue> OutChains;
234
Chris Lattner5a65b922008-03-17 05:41:48 +0000235 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
236 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
237 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000239
David Greene3f2bf852009-11-12 20:49:22 +0000240 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
241 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000243
David Greene54a7aa82010-02-15 16:57:02 +0000244 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, NULL, 0,
245 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000246 ArgOffset += 4;
247 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000248
249 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000250 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000252 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000253 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000254 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000255
Dan Gohman98ca4f22009-08-05 01:29:28 +0000256 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000257}
258
Dan Gohman98ca4f22009-08-05 01:29:28 +0000259SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000260SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000261 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000262 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000263 const SmallVectorImpl<ISD::OutputArg> &Outs,
264 const SmallVectorImpl<ISD::InputArg> &Ins,
265 DebugLoc dl, SelectionDAG &DAG,
266 SmallVectorImpl<SDValue> &InVals) {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000267 // Sparc target does not yet support tail call optimization.
268 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000269
Chris Lattner315123f2008-03-17 06:58:37 +0000270#if 0
271 // Analyze operands of the call, assigning locations to each operand.
272 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000273 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
274 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000275
Chris Lattner315123f2008-03-17 06:58:37 +0000276 // Get the size of the outgoing arguments stack space requirement.
277 unsigned ArgsSize = CCInfo.getNextStackOffset();
278 // FIXME: We can't use this until f64 is known to take two GPRs.
279#else
280 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000281
Chris Lattner5a65b922008-03-17 05:41:48 +0000282 // Count the size of the outgoing arguments.
283 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000284 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 switch (Outs[i].Val.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000286 default: llvm_unreachable("Unknown value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 case MVT::i1:
288 case MVT::i8:
289 case MVT::i16:
290 case MVT::i32:
291 case MVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000292 ArgsSize += 4;
293 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 case MVT::i64:
295 case MVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000296 ArgsSize += 8;
297 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000298 }
299 }
300 if (ArgsSize > 4*6)
301 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
302 else
303 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000304#endif
305
Chris Lattner5a65b922008-03-17 05:41:48 +0000306 // Keep stack frames 8-byte aligned.
307 ArgsSize = (ArgsSize+7) & ~7;
308
Chris Lattnere563bbc2008-10-11 22:08:30 +0000309 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000310
Dan Gohman475871a2008-07-27 21:46:04 +0000311 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
312 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000313
Chris Lattner315123f2008-03-17 06:58:37 +0000314#if 0
315 // Walk the register/memloc assignments, inserting copies/loads.
316 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
317 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000318 SDValue Arg = Outs[i].Val;
Chris Lattner315123f2008-03-17 06:58:37 +0000319
320 // Promote the value if needed.
321 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000322 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000323 case CCValAssign::Full: break;
324 case CCValAssign::SExt:
325 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
326 break;
327 case CCValAssign::ZExt:
328 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
329 break;
330 case CCValAssign::AExt:
331 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
332 break;
333 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000334
335 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000336 // RegsToPass vector
337 if (VA.isRegLoc()) {
338 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
339 continue;
340 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000341
Chris Lattner315123f2008-03-17 06:58:37 +0000342 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000343
Chris Lattner315123f2008-03-17 06:58:37 +0000344 // Create a store off the stack pointer for this argument.
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000346 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000347 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
David Greene54a7aa82010-02-15 16:57:02 +0000349 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0,
350 false, false, 0));
Chris Lattner315123f2008-03-17 06:58:37 +0000351 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000352
353#else
Chris Lattner315123f2008-03-17 06:58:37 +0000354 static const unsigned ArgRegs[] = {
355 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
356 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000357 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000358
Dan Gohman98ca4f22009-08-05 01:29:28 +0000359 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
360 SDValue Val = Outs[i].Val;
Owen Andersone50ed302009-08-10 22:56:29 +0000361 EVT ObjectVT = Val.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000362 SDValue ValToStore(0, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000363 unsigned ObjSize;
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000365 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 case MVT::i32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000367 ObjSize = 4;
368
Chris Lattner315123f2008-03-17 06:58:37 +0000369 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000370 ValToStore = Val;
371 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000372 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000373 }
374 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 case MVT::f32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000376 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000377 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000378 ValToStore = Val;
379 } else {
380 // Convert this to a FP value in an int reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 Val = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000382 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000383 }
384 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 case MVT::f64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000386 ObjSize = 8;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000387 if (RegsToPass.size() >= 6) {
388 ValToStore = Val; // Whole thing is passed in memory.
389 break;
390 }
391
392 // Break into top and bottom parts by storing to the stack and loading
393 // out the parts as integers. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000395 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
David Greene54a7aa82010-02-15 16:57:02 +0000396 Val, StackPtr, NULL, 0,
397 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000398 // Sparc is big-endian, so the high part comes first.
David Greene54a7aa82010-02-15 16:57:02 +0000399 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0,
400 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000401 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000402 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000403 DAG.getIntPtrConstant(4));
404 // Load the low part.
David Greene54a7aa82010-02-15 16:57:02 +0000405 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0,
406 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000407
408 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
409
410 if (RegsToPass.size() >= 6) {
411 ValToStore = Lo;
412 ArgOffset += 4;
413 ObjSize = 4;
414 } else {
415 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
416 }
417 break;
418 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 case MVT::i64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000420 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000421 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000422 ValToStore = Val; // Whole thing is passed in memory.
423 break;
424 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000425
Chris Lattner5a65b922008-03-17 05:41:48 +0000426 // Split the value into top and bottom part. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
428 DAG.getConstant(1, MVT::i32));
429 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
430 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000431 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000432
Chris Lattner315123f2008-03-17 06:58:37 +0000433 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000434 ValToStore = Lo;
435 ArgOffset += 4;
436 ObjSize = 4;
437 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000438 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000439 }
440 break;
441 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000442 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000443
Gabor Greifba36cb52008-08-28 21:40:38 +0000444 if (ValToStore.getNode()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
446 SDValue PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
447 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000448 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
David Greene54a7aa82010-02-15 16:57:02 +0000449 PtrOff, NULL, 0,
450 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000451 }
452 ArgOffset += ObjSize;
453 }
Chris Lattner315123f2008-03-17 06:58:37 +0000454#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000455
Chris Lattner5a65b922008-03-17 05:41:48 +0000456 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000457 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000459 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000460
461 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000462 // chain and flag operands which copy the outgoing args into registers.
463 // The InFlag in necessary since all emited instructions must be
464 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000465 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000466 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
467 unsigned Reg = RegsToPass[i].first;
468 // Remap I0->I7 -> O0->O7.
469 if (Reg >= SP::I0 && Reg <= SP::I7)
470 Reg = Reg-SP::I0+SP::O0;
471
Dale Johannesen33c960f2009-02-04 20:06:27 +0000472 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000473 InFlag = Chain.getValue(1);
474 }
475
476 // If the callee is a GlobalAddress node (quite common, every direct call is)
477 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000478 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000479 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000481 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000483
Owen Andersone50ed302009-08-10 22:56:29 +0000484 std::vector<EVT> NodeTys;
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 NodeTys.push_back(MVT::Other); // Returns a chain
486 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000487 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000488 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000489 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000490
Chris Lattnere563bbc2008-10-11 22:08:30 +0000491 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
492 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000493 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000494
Chris Lattner98949a62008-03-17 06:01:07 +0000495 // Assign locations to each value returned by this call.
496 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000497 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000498 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000499
Dan Gohman98ca4f22009-08-05 01:29:28 +0000500 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000501
Chris Lattner98949a62008-03-17 06:01:07 +0000502 // Copy all of the result registers out of their specified physreg.
503 for (unsigned i = 0; i != RVLocs.size(); ++i) {
504 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000505
Chris Lattner98949a62008-03-17 06:01:07 +0000506 // Remap I0->I7 -> O0->O7.
507 if (Reg >= SP::I0 && Reg <= SP::I7)
508 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000509
Dale Johannesen33c960f2009-02-04 20:06:27 +0000510 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000511 RVLocs[i].getValVT(), InFlag).getValue(1);
512 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000513 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000514 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000515
Dan Gohman98ca4f22009-08-05 01:29:28 +0000516 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000517}
518
519
520
Chris Lattnerd23405e2008-03-17 03:21:36 +0000521//===----------------------------------------------------------------------===//
522// TargetLowering Implementation
523//===----------------------------------------------------------------------===//
524
525/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
526/// condition.
527static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
528 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000529 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000530 case ISD::SETEQ: return SPCC::ICC_E;
531 case ISD::SETNE: return SPCC::ICC_NE;
532 case ISD::SETLT: return SPCC::ICC_L;
533 case ISD::SETGT: return SPCC::ICC_G;
534 case ISD::SETLE: return SPCC::ICC_LE;
535 case ISD::SETGE: return SPCC::ICC_GE;
536 case ISD::SETULT: return SPCC::ICC_CS;
537 case ISD::SETULE: return SPCC::ICC_LEU;
538 case ISD::SETUGT: return SPCC::ICC_GU;
539 case ISD::SETUGE: return SPCC::ICC_CC;
540 }
541}
542
543/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
544/// FCC condition.
545static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
546 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000547 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000548 case ISD::SETEQ:
549 case ISD::SETOEQ: return SPCC::FCC_E;
550 case ISD::SETNE:
551 case ISD::SETUNE: return SPCC::FCC_NE;
552 case ISD::SETLT:
553 case ISD::SETOLT: return SPCC::FCC_L;
554 case ISD::SETGT:
555 case ISD::SETOGT: return SPCC::FCC_G;
556 case ISD::SETLE:
557 case ISD::SETOLE: return SPCC::FCC_LE;
558 case ISD::SETGE:
559 case ISD::SETOGE: return SPCC::FCC_GE;
560 case ISD::SETULT: return SPCC::FCC_UL;
561 case ISD::SETULE: return SPCC::FCC_ULE;
562 case ISD::SETUGT: return SPCC::FCC_UG;
563 case ISD::SETUGE: return SPCC::FCC_UGE;
564 case ISD::SETUO: return SPCC::FCC_U;
565 case ISD::SETO: return SPCC::FCC_O;
566 case ISD::SETONE: return SPCC::FCC_LG;
567 case ISD::SETUEQ: return SPCC::FCC_UE;
568 }
569}
570
Chris Lattnerd23405e2008-03-17 03:21:36 +0000571SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000572 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000573
Chris Lattnerd23405e2008-03-17 03:21:36 +0000574 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
576 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
577 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000578
579 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000581 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000583 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000585
586 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
588 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
589 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000590
Chris Lattnerd23405e2008-03-17 03:21:36 +0000591 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
593 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
594 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000595
596 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::UREM, MVT::i32, Expand);
598 setOperationAction(ISD::SREM, MVT::i32, Expand);
599 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
600 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000601
602 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
604 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000605
606 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
608 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000609
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
611 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000612
Chris Lattnerd23405e2008-03-17 03:21:36 +0000613 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::SELECT, MVT::i32, Expand);
615 setOperationAction(ISD::SELECT, MVT::f32, Expand);
616 setOperationAction(ISD::SELECT, MVT::f64, Expand);
617 setOperationAction(ISD::SETCC, MVT::i32, Expand);
618 setOperationAction(ISD::SETCC, MVT::f32, Expand);
619 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000620
Chris Lattnerd23405e2008-03-17 03:21:36 +0000621 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
623 setOperationAction(ISD::BRIND, MVT::Other, Expand);
624 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
625 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
626 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
627 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000628
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
630 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
631 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000632
Chris Lattnerd23405e2008-03-17 03:21:36 +0000633 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000635
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f64, Expand);
637 setOperationAction(ISD::FCOS , MVT::f64, Expand);
638 setOperationAction(ISD::FREM , MVT::f64, Expand);
639 setOperationAction(ISD::FSIN , MVT::f32, Expand);
640 setOperationAction(ISD::FCOS , MVT::f32, Expand);
641 setOperationAction(ISD::FREM , MVT::f32, Expand);
642 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
643 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
644 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
645 setOperationAction(ISD::ROTL , MVT::i32, Expand);
646 setOperationAction(ISD::ROTR , MVT::i32, Expand);
647 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
648 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
650 setOperationAction(ISD::FPOW , MVT::f64, Expand);
651 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
654 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
655 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000656
657 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
659 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000662
Chris Lattnerd23405e2008-03-17 03:21:36 +0000663 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000665 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000667
Chris Lattnerd23405e2008-03-17 03:21:36 +0000668 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
670 setOperationAction(ISD::VAEND , MVT::Other, Expand);
671 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
672 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
673 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000674
675 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000677
Chris Lattnerd23405e2008-03-17 03:21:36 +0000678 setStackPointerRegisterToSaveRestore(SP::O6);
679
680 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000682
Chris Lattnerd23405e2008-03-17 03:21:36 +0000683 computeRegisterProperties();
684}
685
686const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
687 switch (Opcode) {
688 default: return 0;
689 case SPISD::CMPICC: return "SPISD::CMPICC";
690 case SPISD::CMPFCC: return "SPISD::CMPFCC";
691 case SPISD::BRICC: return "SPISD::BRICC";
692 case SPISD::BRFCC: return "SPISD::BRFCC";
693 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
694 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
695 case SPISD::Hi: return "SPISD::Hi";
696 case SPISD::Lo: return "SPISD::Lo";
697 case SPISD::FTOI: return "SPISD::FTOI";
698 case SPISD::ITOF: return "SPISD::ITOF";
699 case SPISD::CALL: return "SPISD::CALL";
700 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
701 }
702}
703
704/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
705/// be zero. Op is expected to be a target specific node. Used by DAG
706/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000707void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000708 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000709 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000710 APInt &KnownOne,
711 const SelectionDAG &DAG,
712 unsigned Depth) const {
713 APInt KnownZero2, KnownOne2;
714 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000715
Chris Lattnerd23405e2008-03-17 03:21:36 +0000716 switch (Op.getOpcode()) {
717 default: break;
718 case SPISD::SELECT_ICC:
719 case SPISD::SELECT_FCC:
720 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
721 Depth+1);
722 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
723 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000724 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
725 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
726
Chris Lattnerd23405e2008-03-17 03:21:36 +0000727 // Only known if known in both the LHS and RHS.
728 KnownOne &= KnownOne2;
729 KnownZero &= KnownZero2;
730 break;
731 }
732}
733
Chris Lattnerd23405e2008-03-17 03:21:36 +0000734// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
735// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000736static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000737 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000738 if (isa<ConstantSDNode>(RHS) &&
739 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000740 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000741 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
742 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
743 (LHS.getOpcode() == SPISD::SELECT_FCC &&
744 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
745 isa<ConstantSDNode>(LHS.getOperand(0)) &&
746 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000747 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
748 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
Dan Gohman475871a2008-07-27 21:46:04 +0000749 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000750 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000751 LHS = CMPCC.getOperand(0);
752 RHS = CMPCC.getOperand(1);
753 }
754}
755
Chris Lattnerdb486a62009-09-15 17:46:24 +0000756SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
757 SelectionDAG &DAG) {
Dan Gohman46510a72010-04-15 01:51:59 +0000758 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000759 // FIXME there isn't really any debug info here
760 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
762 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
763 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000764
765 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
766 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
767
768 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
769 getPointerTy());
770 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
771 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
772 GlobalBase, RelAddr);
773 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene54a7aa82010-02-15 16:57:02 +0000774 AbsAddr, NULL, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000775}
776
Chris Lattnerdb486a62009-09-15 17:46:24 +0000777SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
778 SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000779 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000780 // FIXME there isn't really any debug info here
781 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000782 const Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
784 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
785 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000786 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
787 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
788
789 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
790 getPointerTy());
791 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
792 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
793 GlobalBase, RelAddr);
794 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene54a7aa82010-02-15 16:57:02 +0000795 AbsAddr, NULL, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000796}
797
Dan Gohman475871a2008-07-27 21:46:04 +0000798static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000799 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000800 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 assert(Op.getValueType() == MVT::i32);
802 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
803 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000804}
805
Dan Gohman475871a2008-07-27 21:46:04 +0000806static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000807 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 assert(Op.getOperand(0).getValueType() == MVT::i32);
809 SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000810 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000811 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000812}
813
Dan Gohman475871a2008-07-27 21:46:04 +0000814static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
815 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000816 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000817 SDValue LHS = Op.getOperand(2);
818 SDValue RHS = Op.getOperand(3);
819 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000820 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000821 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000822
Chris Lattnerd23405e2008-03-17 03:21:36 +0000823 // If this is a br_cc of a "setcc", and if the setcc got lowered into
824 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
825 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000826
Chris Lattnerd23405e2008-03-17 03:21:36 +0000827 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000828 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000830 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 VTs.push_back(MVT::i32);
832 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000833 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000834 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000835 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
836 Opc = SPISD::BRICC;
837 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000839 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
840 Opc = SPISD::BRFCC;
841 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
843 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000844}
845
Dan Gohman475871a2008-07-27 21:46:04 +0000846static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
847 SDValue LHS = Op.getOperand(0);
848 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000849 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000850 SDValue TrueVal = Op.getOperand(2);
851 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000852 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000853 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000854
Chris Lattnerd23405e2008-03-17 03:21:36 +0000855 // If this is a select_cc of a "setcc", and if the setcc got lowered into
856 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
857 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000858
Dan Gohman475871a2008-07-27 21:46:04 +0000859 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000861 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000862 VTs.push_back(LHS.getValueType()); // subcc returns a value
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000864 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000865 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000866 Opc = SPISD::SELECT_ICC;
867 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
868 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000870 Opc = SPISD::SELECT_FCC;
871 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
872 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000873 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000875}
876
Dan Gohman475871a2008-07-27 21:46:04 +0000877static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000878 SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +0000879 MachineFunction &MF = DAG.getMachineFunction();
880 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
881
Chris Lattnerd23405e2008-03-17 03:21:36 +0000882 // vastart just stores the address of the VarArgsFrameIndex slot into the
883 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000884 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000885 SDValue Offset =
886 DAG.getNode(ISD::ADD, dl, MVT::i32,
887 DAG.getRegister(SP::I6, MVT::i32),
888 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
889 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000890 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene54a7aa82010-02-15 16:57:02 +0000891 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0,
892 false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000893}
894
Dan Gohman475871a2008-07-27 21:46:04 +0000895static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000896 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000897 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000898 SDValue InChain = Node->getOperand(0);
899 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000900 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000901 DebugLoc dl = Node->getDebugLoc();
David Greene54a7aa82010-02-15 16:57:02 +0000902 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr, SV, 0,
903 false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000904 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000906 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000908 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000909 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
David Greene54a7aa82010-02-15 16:57:02 +0000910 VAListPtr, SV, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000911 // Load the actual argument out of the pointer VAList, unless this is an
912 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 if (VT != MVT::f64)
David Greene54a7aa82010-02-15 16:57:02 +0000914 return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000915
Chris Lattnerd23405e2008-03-17 03:21:36 +0000916 // Otherwise, load it as i64, then do a bitconvert.
David Greene54a7aa82010-02-15 16:57:02 +0000917 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, NULL, 0,
918 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000919
Chris Lattnerd23405e2008-03-17 03:21:36 +0000920 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000921 SDValue Ops[2] = {
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000923 V.getValue(1)
924 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000925 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000926}
927
Dan Gohman475871a2008-07-27 21:46:04 +0000928static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
929 SDValue Chain = Op.getOperand(0); // Legalize the chain.
930 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000931 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000932
Chris Lattnerd23405e2008-03-17 03:21:36 +0000933 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +0000934 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
935 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000936 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000937
Chris Lattnerd23405e2008-03-17 03:21:36 +0000938 // The resultant pointer is actually 16 words from the bottom of the stack,
939 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
941 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000942 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000943 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000944}
945
Chris Lattnerd23405e2008-03-17 03:21:36 +0000946
Dan Gohman475871a2008-07-27 21:46:04 +0000947SDValue SparcTargetLowering::
948LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000949 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000950 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000951 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000952 case ISD::RETURNADDR: return SDValue();
953 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000954 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +0000955 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +0000956 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
957 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000958 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
959 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
960 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
961 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
962 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
963 case ISD::VAARG: return LowerVAARG(Op, DAG);
964 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000965 }
966}
967
968MachineBasicBlock *
969SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000970 MachineBasicBlock *BB,
971 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000972 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
973 unsigned BROpcode;
974 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +0000975 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000976 // Figure out the conditional branch opcode to use for this select_cc.
977 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000978 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000979 case SP::SELECT_CC_Int_ICC:
980 case SP::SELECT_CC_FP_ICC:
981 case SP::SELECT_CC_DFP_ICC:
982 BROpcode = SP::BCOND;
983 break;
984 case SP::SELECT_CC_Int_FCC:
985 case SP::SELECT_CC_FP_FCC:
986 case SP::SELECT_CC_DFP_FCC:
987 BROpcode = SP::FBCOND;
988 break;
989 }
990
991 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000992
Chris Lattnerd23405e2008-03-17 03:21:36 +0000993 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
994 // control-flow pattern. The incoming instruction knows the destination vreg
995 // to set, the condition code register to branch on, the true/false values to
996 // select between, and a branch opcode to use.
997 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000998 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000999 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001000
Chris Lattnerd23405e2008-03-17 03:21:36 +00001001 // thisMBB:
1002 // ...
1003 // TrueVal = ...
1004 // [f]bCC copy1MBB
1005 // fallthrough --> copy0MBB
1006 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001007 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001008 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1009 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesend552eee2009-02-13 02:31:35 +00001010 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001011 F->insert(It, copy0MBB);
1012 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00001013 // Update machine-CFG edges by first adding all successors of the current
Chris Lattnerd23405e2008-03-17 03:21:36 +00001014 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00001015 // Also inform sdisel of the edge changes.
1016 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
1017 E = BB->succ_end(); I != E; ++I) {
1018 EM->insert(std::make_pair(*I, sinkMBB));
1019 sinkMBB->addSuccessor(*I);
1020 }
1021 // Next, remove all successors of the current block, and add the true
1022 // and fallthrough blocks as its successors.
1023 while (!BB->succ_empty())
1024 BB->removeSuccessor(BB->succ_begin());
Dan Gohman0011dc42008-06-21 20:21:19 +00001025 // Next, add the true and fallthrough blocks as its successors.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001026 BB->addSuccessor(copy0MBB);
1027 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001028
Chris Lattnerd23405e2008-03-17 03:21:36 +00001029 // copy0MBB:
1030 // %FalseValue = ...
1031 // # fallthrough to sinkMBB
1032 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001033
Chris Lattnerd23405e2008-03-17 03:21:36 +00001034 // Update machine-CFG edges
1035 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001036
Chris Lattnerd23405e2008-03-17 03:21:36 +00001037 // sinkMBB:
1038 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1039 // ...
1040 BB = sinkMBB;
Dale Johannesend552eee2009-02-13 02:31:35 +00001041 BuildMI(BB, dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001042 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1043 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001044
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001045 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001046 return BB;
1047}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001048
1049//===----------------------------------------------------------------------===//
1050// Sparc Inline Assembly Support
1051//===----------------------------------------------------------------------===//
1052
1053/// getConstraintType - Given a constraint letter, return the type of
1054/// constraint it is for this target.
1055SparcTargetLowering::ConstraintType
1056SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1057 if (Constraint.size() == 1) {
1058 switch (Constraint[0]) {
1059 default: break;
1060 case 'r': return C_RegisterClass;
1061 }
1062 }
1063
1064 return TargetLowering::getConstraintType(Constraint);
1065}
1066
1067std::pair<unsigned, const TargetRegisterClass*>
1068SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001069 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001070 if (Constraint.size() == 1) {
1071 switch (Constraint[0]) {
1072 case 'r':
1073 return std::make_pair(0U, SP::IntRegsRegisterClass);
1074 }
1075 }
1076
1077 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1078}
1079
1080std::vector<unsigned> SparcTargetLowering::
1081getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001082 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001083 if (Constraint.size() != 1)
1084 return std::vector<unsigned>();
1085
1086 switch (Constraint[0]) {
1087 default: break;
1088 case 'r':
1089 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1090 SP::L4, SP::L5, SP::L6, SP::L7,
1091 SP::I0, SP::I1, SP::I2, SP::I3,
1092 SP::I4, SP::I5,
1093 SP::O0, SP::O1, SP::O2, SP::O3,
1094 SP::O4, SP::O5, SP::O7, 0);
1095 }
1096
1097 return std::vector<unsigned>();
1098}
Dan Gohman6520e202008-10-18 02:06:02 +00001099
1100bool
1101SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1102 // The Sparc target isn't yet aware of offsets.
1103 return false;
1104}
Bill Wendling20c568f2009-06-30 22:38:32 +00001105
Bill Wendlingb4202b82009-07-01 18:50:55 +00001106/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001107unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001108 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001109}