blob: c4b0a1ce86bade971fe9198f46a4abff95a98f68 [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,
Dan Gohmand858e902010-04-17 15:26:15 +000041 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000042
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,
Dan Gohmand858e902010-04-17 15:26:15 +000089 SmallVectorImpl<SDValue> &InVals)
90 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000091
Chris Lattner5a65b922008-03-17 05:41:48 +000092 MachineFunction &MF = DAG.getMachineFunction();
93 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +000094 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +000095
96 // Assign locations to all of the incoming arguments.
97 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +000098 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
99 ArgLocs, *DAG.getContext());
100 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000101
Chris Lattner5a65b922008-03-17 05:41:48 +0000102 static const unsigned ArgRegs[] = {
103 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
104 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000105 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
106 unsigned ArgOffset = 68;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000107
Eli Friedmana786c7b2009-07-19 19:53:46 +0000108 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
109 SDValue ArgValue;
110 CCValAssign &VA = ArgLocs[i];
111 // FIXME: We ignore the register assignments of AnalyzeFormalArguments
112 // because it doesn't know how to split a double into two i32 registers.
Owen Andersone50ed302009-08-10 22:56:29 +0000113 EVT ObjectVT = VA.getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000115 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 case MVT::i1:
117 case MVT::i8:
118 case MVT::i16:
119 case MVT::i32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000120 if (!Ins[i].Used) { // Argument is dead.
121 if (CurArgReg < ArgRegEnd) ++CurArgReg;
122 InVals.push_back(DAG.getUNDEF(ObjectVT));
123 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000124 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
125 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
127 if (ObjectVT != MVT::i32) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000128 unsigned AssertOp = ISD::AssertSext;
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 Arg = DAG.getNode(AssertOp, dl, MVT::i32, Arg,
Chris Lattner5a65b922008-03-17 05:41:48 +0000130 DAG.getValueType(ObjectVT));
Dale Johannesen39355f92009-02-04 02:34:38 +0000131 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000132 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000133 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000134 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000135 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000136 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000138 SDValue Load;
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 if (ObjectVT == MVT::i32) {
David Greene54a7aa82010-02-15 16:57:02 +0000140 Load = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
141 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000142 } else {
143 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
144
145 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000146 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000147 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
148 DAG.getConstant(Offset, MVT::i32));
149 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
David Greene54a7aa82010-02-15 16:57:02 +0000150 NULL, 0, ObjectVT, false, false, 0);
Dale Johannesen39355f92009-02-04 02:34:38 +0000151 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000152 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000153 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000154 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000155
Chris Lattner5a65b922008-03-17 05:41:48 +0000156 ArgOffset += 4;
157 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 case MVT::f32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000159 if (!Ins[i].Used) { // Argument is dead.
160 if (CurArgReg < ArgRegEnd) ++CurArgReg;
161 InVals.push_back(DAG.getUNDEF(ObjectVT));
162 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000163 // FP value is passed in an integer register.
164 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
165 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000167
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Arg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000169 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000170 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000171 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000172 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000174 SDValue Load = DAG.getLoad(MVT::f32, dl, Chain, FIPtr, NULL, 0,
175 false, false, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000176 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000177 }
178 ArgOffset += 4;
179 break;
180
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 case MVT::i64:
182 case MVT::f64:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000183 if (!Ins[i].Used) { // Argument is dead.
184 if (CurArgReg < ArgRegEnd) ++CurArgReg;
185 if (CurArgReg < ArgRegEnd) ++CurArgReg;
186 InVals.push_back(DAG.getUNDEF(ObjectVT));
187 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000188 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000189 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
190 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
191 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000193 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000194 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000195 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000197 HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
198 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000199 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000200
Dan Gohman475871a2008-07-27 21:46:04 +0000201 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000202 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
203 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
204 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000206 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000207 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4,
Evan Chenged2ae132010-07-03 00:40:23 +0000208 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000210 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
211 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000212 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000213
Chris Lattner5a65b922008-03-17 05:41:48 +0000214 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000215 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000217
Chris Lattner5a65b922008-03-17 05:41:48 +0000218 // If we want a double, do a bit convert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 if (ObjectVT == MVT::f64)
220 WholeValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000221
Dan Gohman98ca4f22009-08-05 01:29:28 +0000222 InVals.push_back(WholeValue);
Chris Lattner5a65b922008-03-17 05:41:48 +0000223 }
224 ArgOffset += 8;
225 break;
226 }
227 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000228
Chris Lattner5a65b922008-03-17 05:41:48 +0000229 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000230 if (isVarArg) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000231 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000232 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000233
Eli Friedmana786c7b2009-07-19 19:53:46 +0000234 std::vector<SDValue> OutChains;
235
Chris Lattner5a65b922008-03-17 05:41:48 +0000236 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
237 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
238 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000240
David Greene3f2bf852009-11-12 20:49:22 +0000241 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000242 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000244
David Greene54a7aa82010-02-15 16:57:02 +0000245 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, NULL, 0,
246 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000247 ArgOffset += 4;
248 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000249
250 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000251 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000253 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000254 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000255 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000256
Dan Gohman98ca4f22009-08-05 01:29:28 +0000257 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000258}
259
Dan Gohman98ca4f22009-08-05 01:29:28 +0000260SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000261SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000262 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000263 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000264 const SmallVectorImpl<ISD::OutputArg> &Outs,
265 const SmallVectorImpl<ISD::InputArg> &Ins,
266 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000267 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000268 // Sparc target does not yet support tail call optimization.
269 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000270
Chris Lattner315123f2008-03-17 06:58:37 +0000271#if 0
272 // Analyze operands of the call, assigning locations to each operand.
273 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000274 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
275 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000276
Chris Lattner315123f2008-03-17 06:58:37 +0000277 // Get the size of the outgoing arguments stack space requirement.
278 unsigned ArgsSize = CCInfo.getNextStackOffset();
279 // FIXME: We can't use this until f64 is known to take two GPRs.
280#else
281 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000282
Chris Lattner5a65b922008-03-17 05:41:48 +0000283 // Count the size of the outgoing arguments.
284 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000285 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 switch (Outs[i].Val.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000287 default: llvm_unreachable("Unknown value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 case MVT::i1:
289 case MVT::i8:
290 case MVT::i16:
291 case MVT::i32:
292 case MVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000293 ArgsSize += 4;
294 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 case MVT::i64:
296 case MVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000297 ArgsSize += 8;
298 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000299 }
300 }
301 if (ArgsSize > 4*6)
302 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
303 else
304 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000305#endif
306
Chris Lattner5a65b922008-03-17 05:41:48 +0000307 // Keep stack frames 8-byte aligned.
308 ArgsSize = (ArgsSize+7) & ~7;
309
Chris Lattnere563bbc2008-10-11 22:08:30 +0000310 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000311
Dan Gohman475871a2008-07-27 21:46:04 +0000312 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
313 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000314
Chris Lattner315123f2008-03-17 06:58:37 +0000315#if 0
316 // Walk the register/memloc assignments, inserting copies/loads.
317 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
318 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000319 SDValue Arg = Outs[i].Val;
Chris Lattner315123f2008-03-17 06:58:37 +0000320
321 // Promote the value if needed.
322 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000323 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000324 case CCValAssign::Full: break;
325 case CCValAssign::SExt:
326 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
327 break;
328 case CCValAssign::ZExt:
329 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
330 break;
331 case CCValAssign::AExt:
332 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
333 break;
334 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000335
336 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000337 // RegsToPass vector
338 if (VA.isRegLoc()) {
339 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
340 continue;
341 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000342
Chris Lattner315123f2008-03-17 06:58:37 +0000343 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000344
Chris Lattner315123f2008-03-17 06:58:37 +0000345 // Create a store off the stack pointer for this argument.
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000347 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000348 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
David Greene54a7aa82010-02-15 16:57:02 +0000350 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0,
351 false, false, 0));
Chris Lattner315123f2008-03-17 06:58:37 +0000352 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000353
354#else
Chris Lattner315123f2008-03-17 06:58:37 +0000355 static const unsigned ArgRegs[] = {
356 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
357 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000358 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000359
Dan Gohman98ca4f22009-08-05 01:29:28 +0000360 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
361 SDValue Val = Outs[i].Val;
Owen Andersone50ed302009-08-10 22:56:29 +0000362 EVT ObjectVT = Val.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000363 SDValue ValToStore(0, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000364 unsigned ObjSize;
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000366 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 case MVT::i32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000368 ObjSize = 4;
369
Chris Lattner315123f2008-03-17 06:58:37 +0000370 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000371 ValToStore = Val;
372 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000373 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000374 }
375 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 case MVT::f32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000377 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000378 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000379 ValToStore = Val;
380 } else {
381 // Convert this to a FP value in an int reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 Val = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000383 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000384 }
385 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 case MVT::f64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000387 ObjSize = 8;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000388 if (RegsToPass.size() >= 6) {
389 ValToStore = Val; // Whole thing is passed in memory.
390 break;
391 }
392
393 // Break into top and bottom parts by storing to the stack and loading
394 // out the parts as integers. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000396 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
David Greene54a7aa82010-02-15 16:57:02 +0000397 Val, StackPtr, NULL, 0,
398 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000399 // Sparc is big-endian, so the high part comes first.
David Greene54a7aa82010-02-15 16:57:02 +0000400 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0,
401 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000402 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000403 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000404 DAG.getIntPtrConstant(4));
405 // Load the low part.
David Greene54a7aa82010-02-15 16:57:02 +0000406 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0,
407 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000408
409 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
410
411 if (RegsToPass.size() >= 6) {
412 ValToStore = Lo;
413 ArgOffset += 4;
414 ObjSize = 4;
415 } else {
416 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
417 }
418 break;
419 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 case MVT::i64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000421 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000422 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000423 ValToStore = Val; // Whole thing is passed in memory.
424 break;
425 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000426
Chris Lattner5a65b922008-03-17 05:41:48 +0000427 // Split the value into top and bottom part. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
429 DAG.getConstant(1, MVT::i32));
430 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
431 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000432 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000433
Chris Lattner315123f2008-03-17 06:58:37 +0000434 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000435 ValToStore = Lo;
436 ArgOffset += 4;
437 ObjSize = 4;
438 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000439 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000440 }
441 break;
442 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000443 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000444
Gabor Greifba36cb52008-08-28 21:40:38 +0000445 if (ValToStore.getNode()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
447 SDValue PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
448 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000449 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
David Greene54a7aa82010-02-15 16:57:02 +0000450 PtrOff, NULL, 0,
451 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000452 }
453 ArgOffset += ObjSize;
454 }
Chris Lattner315123f2008-03-17 06:58:37 +0000455#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000456
Chris Lattner5a65b922008-03-17 05:41:48 +0000457 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000458 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000460 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000461
462 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000463 // chain and flag operands which copy the outgoing args into registers.
464 // The InFlag in necessary since all emited instructions must be
465 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000466 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000467 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
468 unsigned Reg = RegsToPass[i].first;
469 // Remap I0->I7 -> O0->O7.
470 if (Reg >= SP::I0 && Reg <= SP::I7)
471 Reg = Reg-SP::I0+SP::O0;
472
Dale Johannesen33c960f2009-02-04 20:06:27 +0000473 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000474 InFlag = Chain.getValue(1);
475 }
476
477 // If the callee is a GlobalAddress node (quite common, every direct call is)
478 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000479 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000480 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000482 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000484
Owen Andersone50ed302009-08-10 22:56:29 +0000485 std::vector<EVT> NodeTys;
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 NodeTys.push_back(MVT::Other); // Returns a chain
487 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000488 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000489 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000490 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000491
Chris Lattnere563bbc2008-10-11 22:08:30 +0000492 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
493 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000494 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000495
Chris Lattner98949a62008-03-17 06:01:07 +0000496 // Assign locations to each value returned by this call.
497 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000498 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000499 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000500
Dan Gohman98ca4f22009-08-05 01:29:28 +0000501 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000502
Chris Lattner98949a62008-03-17 06:01:07 +0000503 // Copy all of the result registers out of their specified physreg.
504 for (unsigned i = 0; i != RVLocs.size(); ++i) {
505 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000506
Chris Lattner98949a62008-03-17 06:01:07 +0000507 // Remap I0->I7 -> O0->O7.
508 if (Reg >= SP::I0 && Reg <= SP::I7)
509 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000510
Dale Johannesen33c960f2009-02-04 20:06:27 +0000511 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000512 RVLocs[i].getValVT(), InFlag).getValue(1);
513 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000514 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000515 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000516
Dan Gohman98ca4f22009-08-05 01:29:28 +0000517 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000518}
519
520
521
Chris Lattnerd23405e2008-03-17 03:21:36 +0000522//===----------------------------------------------------------------------===//
523// TargetLowering Implementation
524//===----------------------------------------------------------------------===//
525
526/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
527/// condition.
528static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
529 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000530 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000531 case ISD::SETEQ: return SPCC::ICC_E;
532 case ISD::SETNE: return SPCC::ICC_NE;
533 case ISD::SETLT: return SPCC::ICC_L;
534 case ISD::SETGT: return SPCC::ICC_G;
535 case ISD::SETLE: return SPCC::ICC_LE;
536 case ISD::SETGE: return SPCC::ICC_GE;
537 case ISD::SETULT: return SPCC::ICC_CS;
538 case ISD::SETULE: return SPCC::ICC_LEU;
539 case ISD::SETUGT: return SPCC::ICC_GU;
540 case ISD::SETUGE: return SPCC::ICC_CC;
541 }
542}
543
544/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
545/// FCC condition.
546static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
547 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000548 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000549 case ISD::SETEQ:
550 case ISD::SETOEQ: return SPCC::FCC_E;
551 case ISD::SETNE:
552 case ISD::SETUNE: return SPCC::FCC_NE;
553 case ISD::SETLT:
554 case ISD::SETOLT: return SPCC::FCC_L;
555 case ISD::SETGT:
556 case ISD::SETOGT: return SPCC::FCC_G;
557 case ISD::SETLE:
558 case ISD::SETOLE: return SPCC::FCC_LE;
559 case ISD::SETGE:
560 case ISD::SETOGE: return SPCC::FCC_GE;
561 case ISD::SETULT: return SPCC::FCC_UL;
562 case ISD::SETULE: return SPCC::FCC_ULE;
563 case ISD::SETUGT: return SPCC::FCC_UG;
564 case ISD::SETUGE: return SPCC::FCC_UGE;
565 case ISD::SETUO: return SPCC::FCC_U;
566 case ISD::SETO: return SPCC::FCC_O;
567 case ISD::SETONE: return SPCC::FCC_LG;
568 case ISD::SETUEQ: return SPCC::FCC_UE;
569 }
570}
571
Chris Lattnerd23405e2008-03-17 03:21:36 +0000572SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000573 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000574
Chris Lattnerd23405e2008-03-17 03:21:36 +0000575 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
577 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
578 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000579
580 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000582 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000584 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000586
587 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
589 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
590 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000591
Chris Lattnerd23405e2008-03-17 03:21:36 +0000592 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
594 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
595 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000596
597 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::UREM, MVT::i32, Expand);
599 setOperationAction(ISD::SREM, MVT::i32, Expand);
600 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
601 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000602
603 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
605 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000606
607 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
609 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000610
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
612 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000613
Chris Lattnerd23405e2008-03-17 03:21:36 +0000614 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::SELECT, MVT::i32, Expand);
616 setOperationAction(ISD::SELECT, MVT::f32, Expand);
617 setOperationAction(ISD::SELECT, MVT::f64, Expand);
618 setOperationAction(ISD::SETCC, MVT::i32, Expand);
619 setOperationAction(ISD::SETCC, MVT::f32, Expand);
620 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000621
Chris Lattnerd23405e2008-03-17 03:21:36 +0000622 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
624 setOperationAction(ISD::BRIND, MVT::Other, Expand);
625 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
626 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
627 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
628 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
631 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
632 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000633
Chris Lattnerd23405e2008-03-17 03:21:36 +0000634 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000636
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::FSIN , MVT::f64, Expand);
638 setOperationAction(ISD::FCOS , MVT::f64, Expand);
639 setOperationAction(ISD::FREM , MVT::f64, Expand);
640 setOperationAction(ISD::FSIN , MVT::f32, Expand);
641 setOperationAction(ISD::FCOS , MVT::f32, Expand);
642 setOperationAction(ISD::FREM , MVT::f32, Expand);
643 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
644 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
645 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
646 setOperationAction(ISD::ROTL , MVT::i32, Expand);
647 setOperationAction(ISD::ROTR , MVT::i32, Expand);
648 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
650 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
651 setOperationAction(ISD::FPOW , MVT::f64, Expand);
652 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000653
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
655 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
656 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000657
658 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
660 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000663
Chris Lattnerd23405e2008-03-17 03:21:36 +0000664 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000666 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000668
Chris Lattnerd23405e2008-03-17 03:21:36 +0000669 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
671 setOperationAction(ISD::VAEND , MVT::Other, Expand);
672 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
673 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
674 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000675
676 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000678
Chris Lattnerd23405e2008-03-17 03:21:36 +0000679 setStackPointerRegisterToSaveRestore(SP::O6);
680
681 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000683
Chris Lattnerd23405e2008-03-17 03:21:36 +0000684 computeRegisterProperties();
685}
686
687const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
688 switch (Opcode) {
689 default: return 0;
690 case SPISD::CMPICC: return "SPISD::CMPICC";
691 case SPISD::CMPFCC: return "SPISD::CMPFCC";
692 case SPISD::BRICC: return "SPISD::BRICC";
693 case SPISD::BRFCC: return "SPISD::BRFCC";
694 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
695 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
696 case SPISD::Hi: return "SPISD::Hi";
697 case SPISD::Lo: return "SPISD::Lo";
698 case SPISD::FTOI: return "SPISD::FTOI";
699 case SPISD::ITOF: return "SPISD::ITOF";
700 case SPISD::CALL: return "SPISD::CALL";
701 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
702 }
703}
704
705/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
706/// be zero. Op is expected to be a target specific node. Used by DAG
707/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000708void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000709 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000710 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000711 APInt &KnownOne,
712 const SelectionDAG &DAG,
713 unsigned Depth) const {
714 APInt KnownZero2, KnownOne2;
715 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000716
Chris Lattnerd23405e2008-03-17 03:21:36 +0000717 switch (Op.getOpcode()) {
718 default: break;
719 case SPISD::SELECT_ICC:
720 case SPISD::SELECT_FCC:
721 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
722 Depth+1);
723 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
724 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000725 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
726 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
727
Chris Lattnerd23405e2008-03-17 03:21:36 +0000728 // Only known if known in both the LHS and RHS.
729 KnownOne &= KnownOne2;
730 KnownZero &= KnownZero2;
731 break;
732 }
733}
734
Chris Lattnerd23405e2008-03-17 03:21:36 +0000735// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
736// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000737static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000738 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000739 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000740 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000741 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000742 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
743 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
744 (LHS.getOpcode() == SPISD::SELECT_FCC &&
745 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
746 isa<ConstantSDNode>(LHS.getOperand(0)) &&
747 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000748 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
749 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000750 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000751 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000752 LHS = CMPCC.getOperand(0);
753 RHS = CMPCC.getOperand(1);
754 }
755}
756
Chris Lattnerdb486a62009-09-15 17:46:24 +0000757SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000758 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +0000759 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000760 // FIXME there isn't really any debug info here
761 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
763 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
764 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000765
766 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
767 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
768
769 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
770 getPointerTy());
771 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
772 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
773 GlobalBase, RelAddr);
774 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene54a7aa82010-02-15 16:57:02 +0000775 AbsAddr, NULL, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000776}
777
Chris Lattnerdb486a62009-09-15 17:46:24 +0000778SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000779 SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000780 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000781 // FIXME there isn't really any debug info here
782 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000783 const Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
785 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
786 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000787 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
788 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
789
790 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
791 getPointerTy());
792 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
793 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
794 GlobalBase, RelAddr);
795 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene54a7aa82010-02-15 16:57:02 +0000796 AbsAddr, NULL, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000797}
798
Dan Gohman475871a2008-07-27 21:46:04 +0000799static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000800 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000801 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 assert(Op.getValueType() == MVT::i32);
803 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
804 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000805}
806
Dan Gohman475871a2008-07-27 21:46:04 +0000807static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000808 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 assert(Op.getOperand(0).getValueType() == MVT::i32);
810 SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000811 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000812 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000813}
814
Dan Gohman475871a2008-07-27 21:46:04 +0000815static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
816 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000817 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000818 SDValue LHS = Op.getOperand(2);
819 SDValue RHS = Op.getOperand(3);
820 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000821 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000822 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000823
Chris Lattnerd23405e2008-03-17 03:21:36 +0000824 // If this is a br_cc of a "setcc", and if the setcc got lowered into
825 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
826 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000827
Chris Lattnerd23405e2008-03-17 03:21:36 +0000828 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000829 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000831 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 VTs.push_back(MVT::i32);
833 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000834 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000835 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000836 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
837 Opc = SPISD::BRICC;
838 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000840 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
841 Opc = SPISD::BRFCC;
842 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000843 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
844 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000845}
846
Dan Gohman475871a2008-07-27 21:46:04 +0000847static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
848 SDValue LHS = Op.getOperand(0);
849 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000850 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000851 SDValue TrueVal = Op.getOperand(2);
852 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000853 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000854 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000855
Chris Lattnerd23405e2008-03-17 03:21:36 +0000856 // If this is a select_cc of a "setcc", and if the setcc got lowered into
857 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
858 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000859
Dan Gohman475871a2008-07-27 21:46:04 +0000860 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000862 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000863 VTs.push_back(LHS.getValueType()); // subcc returns a value
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000865 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000866 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000867 Opc = SPISD::SELECT_ICC;
868 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
869 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000871 Opc = SPISD::SELECT_FCC;
872 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
873 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000874 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000876}
877
Dan Gohman475871a2008-07-27 21:46:04 +0000878static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000879 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +0000880 MachineFunction &MF = DAG.getMachineFunction();
881 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
882
Chris Lattnerd23405e2008-03-17 03:21:36 +0000883 // vastart just stores the address of the VarArgsFrameIndex slot into the
884 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000885 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000886 SDValue Offset =
887 DAG.getNode(ISD::ADD, dl, MVT::i32,
888 DAG.getRegister(SP::I6, MVT::i32),
889 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
890 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000891 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene54a7aa82010-02-15 16:57:02 +0000892 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0,
893 false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000894}
895
Dan Gohman475871a2008-07-27 21:46:04 +0000896static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000897 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000898 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000899 SDValue InChain = Node->getOperand(0);
900 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000901 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000902 DebugLoc dl = Node->getDebugLoc();
David Greene54a7aa82010-02-15 16:57:02 +0000903 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr, SV, 0,
904 false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000905 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000907 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000909 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000910 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
David Greene54a7aa82010-02-15 16:57:02 +0000911 VAListPtr, SV, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000912 // Load the actual argument out of the pointer VAList, unless this is an
913 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 if (VT != MVT::f64)
David Greene54a7aa82010-02-15 16:57:02 +0000915 return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000916
Chris Lattnerd23405e2008-03-17 03:21:36 +0000917 // Otherwise, load it as i64, then do a bitconvert.
David Greene54a7aa82010-02-15 16:57:02 +0000918 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, NULL, 0,
919 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000920
Chris Lattnerd23405e2008-03-17 03:21:36 +0000921 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000922 SDValue Ops[2] = {
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000924 V.getValue(1)
925 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000926 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000927}
928
Dan Gohman475871a2008-07-27 21:46:04 +0000929static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
930 SDValue Chain = Op.getOperand(0); // Legalize the chain.
931 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000932 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000933
Chris Lattnerd23405e2008-03-17 03:21:36 +0000934 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
936 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000937 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000938
Chris Lattnerd23405e2008-03-17 03:21:36 +0000939 // The resultant pointer is actually 16 words from the bottom of the stack,
940 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
942 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000943 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000944 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000945}
946
Chris Lattnerd23405e2008-03-17 03:21:36 +0000947
Dan Gohman475871a2008-07-27 21:46:04 +0000948SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000949LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000950 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000951 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000952 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000953 case ISD::RETURNADDR: return SDValue();
954 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000955 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +0000956 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +0000957 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
958 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000959 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
960 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
961 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
962 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
963 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
964 case ISD::VAARG: return LowerVAARG(Op, DAG);
965 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000966 }
967}
968
969MachineBasicBlock *
970SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000971 MachineBasicBlock *BB) 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);
Dan Gohman258c58c2010-07-06 15:49:48 +00001013 // Update machine-CFG edges by first adding all successors of the current
1014 // block to the new block which will contain the Phi node for the select.
1015 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
1016 E = BB->succ_end(); I != E; ++I)
1017 sinkMBB->addSuccessor(*I);
1018 // Next, remove all successors of the current block, and add the true
1019 // and fallthrough blocks as its successors.
1020 while (!BB->succ_empty())
1021 BB->removeSuccessor(BB->succ_begin());
1022 // Next, add the true and fallthrough blocks as its successors.
1023 BB->addSuccessor(copy0MBB);
1024 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001025
Chris Lattnerd23405e2008-03-17 03:21:36 +00001026 // copy0MBB:
1027 // %FalseValue = ...
1028 // # fallthrough to sinkMBB
1029 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001030
Chris Lattnerd23405e2008-03-17 03:21:36 +00001031 // Update machine-CFG edges
1032 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001033
Chris Lattnerd23405e2008-03-17 03:21:36 +00001034 // sinkMBB:
1035 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1036 // ...
1037 BB = sinkMBB;
Dan Gohman258c58c2010-07-06 15:49:48 +00001038 BuildMI(BB, dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001039 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1040 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001041
Dan Gohman258c58c2010-07-06 15:49:48 +00001042 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001043 return BB;
1044}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001045
1046//===----------------------------------------------------------------------===//
1047// Sparc Inline Assembly Support
1048//===----------------------------------------------------------------------===//
1049
1050/// getConstraintType - Given a constraint letter, return the type of
1051/// constraint it is for this target.
1052SparcTargetLowering::ConstraintType
1053SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1054 if (Constraint.size() == 1) {
1055 switch (Constraint[0]) {
1056 default: break;
1057 case 'r': return C_RegisterClass;
1058 }
1059 }
1060
1061 return TargetLowering::getConstraintType(Constraint);
1062}
1063
1064std::pair<unsigned, const TargetRegisterClass*>
1065SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001066 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001067 if (Constraint.size() == 1) {
1068 switch (Constraint[0]) {
1069 case 'r':
1070 return std::make_pair(0U, SP::IntRegsRegisterClass);
1071 }
1072 }
1073
1074 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1075}
1076
1077std::vector<unsigned> SparcTargetLowering::
1078getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001079 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001080 if (Constraint.size() != 1)
1081 return std::vector<unsigned>();
1082
1083 switch (Constraint[0]) {
1084 default: break;
1085 case 'r':
1086 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1087 SP::L4, SP::L5, SP::L6, SP::L7,
1088 SP::I0, SP::I1, SP::I2, SP::I3,
1089 SP::I4, SP::I5,
1090 SP::O0, SP::O1, SP::O2, SP::O3,
1091 SP::O4, SP::O5, SP::O7, 0);
1092 }
1093
1094 return std::vector<unsigned>();
1095}
Dan Gohman6520e202008-10-18 02:06:02 +00001096
1097bool
1098SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1099 // The Sparc target isn't yet aware of offsets.
1100 return false;
1101}
Bill Wendling20c568f2009-06-30 22:38:32 +00001102
Bill Wendlingb4202b82009-07-01 18:50:55 +00001103/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001104unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001105 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001106}