blob: 028017e4ff73babd23c4f00e9907b7a5794666fb [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 Gohmanc9403652010-07-07 15:54:55 +000041 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +000042 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000043
Chris Lattner5a65b922008-03-17 05:41:48 +000044 // CCValAssign - represent the assignment of the return value to locations.
45 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000046
Chris Lattner5a65b922008-03-17 05:41:48 +000047 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +000048 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
49 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000050
Dan Gohman98ca4f22009-08-05 01:29:28 +000051 // Analize return values.
52 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000053
Chris Lattner5a65b922008-03-17 05:41:48 +000054 // If this is the first return lowered for this function, add the regs to the
55 // liveout set for the function.
56 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
57 for (unsigned i = 0; i != RVLocs.size(); ++i)
58 if (RVLocs[i].isRegLoc())
59 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
60 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000061
Dan Gohman475871a2008-07-27 21:46:04 +000062 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +000063
64 // Copy the result values into the output registers.
65 for (unsigned i = 0; i != RVLocs.size(); ++i) {
66 CCValAssign &VA = RVLocs[i];
67 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +000068
Dale Johannesena05dca42009-02-04 23:02:30 +000069 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +000070 OutVals[i], Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +000071
Chris Lattner5a65b922008-03-17 05:41:48 +000072 // Guarantee that all emitted copies are stuck together with flags.
73 Flag = Chain.getValue(1);
74 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000075
Gabor Greifba36cb52008-08-28 21:40:38 +000076 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +000077 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
78 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner5a65b922008-03-17 05:41:48 +000079}
80
Dan Gohman98ca4f22009-08-05 01:29:28 +000081/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
82/// passed in either one or two GPRs, including FP values. TODO: we should
83/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +000084SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000085SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000086 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000087 const SmallVectorImpl<ISD::InputArg>
88 &Ins,
89 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +000090 SmallVectorImpl<SDValue> &InVals)
91 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000092
Chris Lattner5a65b922008-03-17 05:41:48 +000093 MachineFunction &MF = DAG.getMachineFunction();
94 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +000095 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +000096
97 // Assign locations to all of the incoming arguments.
98 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +000099 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
100 ArgLocs, *DAG.getContext());
101 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000102
Chris Lattner5a65b922008-03-17 05:41:48 +0000103 static const unsigned ArgRegs[] = {
104 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
105 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000106 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
107 unsigned ArgOffset = 68;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000108
Eli Friedmana786c7b2009-07-19 19:53:46 +0000109 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
110 SDValue ArgValue;
111 CCValAssign &VA = ArgLocs[i];
112 // FIXME: We ignore the register assignments of AnalyzeFormalArguments
113 // because it doesn't know how to split a double into two i32 registers.
Owen Andersone50ed302009-08-10 22:56:29 +0000114 EVT ObjectVT = VA.getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000116 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 case MVT::i1:
118 case MVT::i8:
119 case MVT::i16:
120 case MVT::i32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000121 if (!Ins[i].Used) { // Argument is dead.
122 if (CurArgReg < ArgRegEnd) ++CurArgReg;
123 InVals.push_back(DAG.getUNDEF(ObjectVT));
124 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000125 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
126 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
128 if (ObjectVT != MVT::i32) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000129 unsigned AssertOp = ISD::AssertSext;
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 Arg = DAG.getNode(AssertOp, dl, MVT::i32, Arg,
Chris Lattner5a65b922008-03-17 05:41:48 +0000131 DAG.getValueType(ObjectVT));
Dale Johannesen39355f92009-02-04 02:34:38 +0000132 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000133 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000134 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000135 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000136 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000137 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000139 SDValue Load;
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 if (ObjectVT == MVT::i32) {
David Greene54a7aa82010-02-15 16:57:02 +0000141 Load = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
142 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000143 } else {
144 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
145
146 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000147 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
149 DAG.getConstant(Offset, MVT::i32));
150 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
David Greene54a7aa82010-02-15 16:57:02 +0000151 NULL, 0, ObjectVT, false, false, 0);
Dale Johannesen39355f92009-02-04 02:34:38 +0000152 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000153 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000154 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000155 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000156
Chris Lattner5a65b922008-03-17 05:41:48 +0000157 ArgOffset += 4;
158 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 case MVT::f32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000160 if (!Ins[i].Used) { // Argument is dead.
161 if (CurArgReg < ArgRegEnd) ++CurArgReg;
162 InVals.push_back(DAG.getUNDEF(ObjectVT));
163 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000164 // FP value is passed in an integer register.
165 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
166 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000168
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Arg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000170 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000171 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000172 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000173 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000175 SDValue Load = DAG.getLoad(MVT::f32, dl, Chain, FIPtr, NULL, 0,
176 false, false, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000177 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000178 }
179 ArgOffset += 4;
180 break;
181
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 case MVT::i64:
183 case MVT::f64:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000184 if (!Ins[i].Used) { // Argument is dead.
185 if (CurArgReg < ArgRegEnd) ++CurArgReg;
186 if (CurArgReg < ArgRegEnd) ++CurArgReg;
187 InVals.push_back(DAG.getUNDEF(ObjectVT));
188 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000189 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000190 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
191 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
192 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000194 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000195 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000196 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000198 HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
199 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000200 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000201
Dan Gohman475871a2008-07-27 21:46:04 +0000202 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000203 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
204 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
205 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000207 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000208 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4,
Evan Chenged2ae132010-07-03 00:40:23 +0000209 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
David Greene54a7aa82010-02-15 16:57:02 +0000211 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0,
212 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000213 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000214
Chris Lattner5a65b922008-03-17 05:41:48 +0000215 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000216 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000218
Chris Lattner5a65b922008-03-17 05:41:48 +0000219 // If we want a double, do a bit convert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 if (ObjectVT == MVT::f64)
221 WholeValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000222
Dan Gohman98ca4f22009-08-05 01:29:28 +0000223 InVals.push_back(WholeValue);
Chris Lattner5a65b922008-03-17 05:41:48 +0000224 }
225 ArgOffset += 8;
226 break;
227 }
228 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000229
Chris Lattner5a65b922008-03-17 05:41:48 +0000230 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000231 if (isVarArg) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000232 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000233 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000234
Eli Friedmana786c7b2009-07-19 19:53:46 +0000235 std::vector<SDValue> OutChains;
236
Chris Lattner5a65b922008-03-17 05:41:48 +0000237 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
238 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
239 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000241
David Greene3f2bf852009-11-12 20:49:22 +0000242 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000243 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000245
David Greene54a7aa82010-02-15 16:57:02 +0000246 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, NULL, 0,
247 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000248 ArgOffset += 4;
249 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000250
251 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000252 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000254 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000255 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000256 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000257
Dan Gohman98ca4f22009-08-05 01:29:28 +0000258 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000259}
260
Dan Gohman98ca4f22009-08-05 01:29:28 +0000261SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000262SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000263 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000264 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000265 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000266 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000267 const SmallVectorImpl<ISD::InputArg> &Ins,
268 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000269 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000270 // Sparc target does not yet support tail call optimization.
271 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000272
Chris Lattner315123f2008-03-17 06:58:37 +0000273#if 0
274 // Analyze operands of the call, assigning locations to each operand.
275 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000276 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
277 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000278
Chris Lattner315123f2008-03-17 06:58:37 +0000279 // Get the size of the outgoing arguments stack space requirement.
280 unsigned ArgsSize = CCInfo.getNextStackOffset();
281 // FIXME: We can't use this until f64 is known to take two GPRs.
282#else
283 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000284
Chris Lattner5a65b922008-03-17 05:41:48 +0000285 // Count the size of the outgoing arguments.
286 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000287 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +0000288 switch (Outs[i].VT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000289 default: llvm_unreachable("Unknown value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 case MVT::i1:
291 case MVT::i8:
292 case MVT::i16:
293 case MVT::i32:
294 case MVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000295 ArgsSize += 4;
296 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 case MVT::i64:
298 case MVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000299 ArgsSize += 8;
300 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000301 }
302 }
303 if (ArgsSize > 4*6)
304 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
305 else
306 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000307#endif
308
Chris Lattner5a65b922008-03-17 05:41:48 +0000309 // Keep stack frames 8-byte aligned.
310 ArgsSize = (ArgsSize+7) & ~7;
311
Chris Lattnere563bbc2008-10-11 22:08:30 +0000312 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000313
Dan Gohman475871a2008-07-27 21:46:04 +0000314 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
315 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000316
Chris Lattner315123f2008-03-17 06:58:37 +0000317#if 0
318 // Walk the register/memloc assignments, inserting copies/loads.
319 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
320 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +0000321 SDValue Arg = OutVals[i];
Chris Lattner315123f2008-03-17 06:58:37 +0000322
323 // Promote the value if needed.
324 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000325 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000326 case CCValAssign::Full: break;
327 case CCValAssign::SExt:
328 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
329 break;
330 case CCValAssign::ZExt:
331 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
332 break;
333 case CCValAssign::AExt:
334 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
335 break;
336 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000337
338 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000339 // RegsToPass vector
340 if (VA.isRegLoc()) {
341 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
342 continue;
343 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000344
Chris Lattner315123f2008-03-17 06:58:37 +0000345 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000346
Chris Lattner315123f2008-03-17 06:58:37 +0000347 // Create a store off the stack pointer for this argument.
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000349 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000350 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
David Greene54a7aa82010-02-15 16:57:02 +0000352 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0,
353 false, false, 0));
Chris Lattner315123f2008-03-17 06:58:37 +0000354 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000355
356#else
Chris Lattner315123f2008-03-17 06:58:37 +0000357 static const unsigned ArgRegs[] = {
358 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
359 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000360 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000361
Dan Gohman98ca4f22009-08-05 01:29:28 +0000362 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +0000363 SDValue Val = OutVals[i];
364 EVT ObjectVT = Outs[i].VT;
Dan Gohman475871a2008-07-27 21:46:04 +0000365 SDValue ValToStore(0, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000366 unsigned ObjSize;
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000368 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 case MVT::i32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000370 ObjSize = 4;
371
Chris Lattner315123f2008-03-17 06:58:37 +0000372 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000373 ValToStore = Val;
374 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000375 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000376 }
377 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 case MVT::f32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000379 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000380 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000381 ValToStore = Val;
382 } else {
383 // Convert this to a FP value in an int reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 Val = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000385 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000386 }
387 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 case MVT::f64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000389 ObjSize = 8;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000390 if (RegsToPass.size() >= 6) {
391 ValToStore = Val; // Whole thing is passed in memory.
392 break;
393 }
394
395 // Break into top and bottom parts by storing to the stack and loading
396 // out the parts as integers. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000398 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
David Greene54a7aa82010-02-15 16:57:02 +0000399 Val, StackPtr, NULL, 0,
400 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000401 // Sparc is big-endian, so the high part comes first.
David Greene54a7aa82010-02-15 16:57:02 +0000402 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0,
403 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000404 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000405 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000406 DAG.getIntPtrConstant(4));
407 // Load the low part.
David Greene54a7aa82010-02-15 16:57:02 +0000408 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0,
409 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000410
411 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
412
413 if (RegsToPass.size() >= 6) {
414 ValToStore = Lo;
415 ArgOffset += 4;
416 ObjSize = 4;
417 } else {
418 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
419 }
420 break;
421 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 case MVT::i64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000423 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000424 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000425 ValToStore = Val; // Whole thing is passed in memory.
426 break;
427 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000428
Chris Lattner5a65b922008-03-17 05:41:48 +0000429 // Split the value into top and bottom part. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
431 DAG.getConstant(1, MVT::i32));
432 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
433 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000434 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000435
Chris Lattner315123f2008-03-17 06:58:37 +0000436 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000437 ValToStore = Lo;
438 ArgOffset += 4;
439 ObjSize = 4;
440 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000441 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000442 }
443 break;
444 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000445 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000446
Gabor Greifba36cb52008-08-28 21:40:38 +0000447 if (ValToStore.getNode()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
449 SDValue PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
450 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000451 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
David Greene54a7aa82010-02-15 16:57:02 +0000452 PtrOff, NULL, 0,
453 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000454 }
455 ArgOffset += ObjSize;
456 }
Chris Lattner315123f2008-03-17 06:58:37 +0000457#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000458
Chris Lattner5a65b922008-03-17 05:41:48 +0000459 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000460 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000462 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000463
464 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000465 // chain and flag operands which copy the outgoing args into registers.
466 // The InFlag in necessary since all emited instructions must be
467 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000468 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000469 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
470 unsigned Reg = RegsToPass[i].first;
471 // Remap I0->I7 -> O0->O7.
472 if (Reg >= SP::I0 && Reg <= SP::I7)
473 Reg = Reg-SP::I0+SP::O0;
474
Dale Johannesen33c960f2009-02-04 20:06:27 +0000475 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000476 InFlag = Chain.getValue(1);
477 }
478
479 // If the callee is a GlobalAddress node (quite common, every direct call is)
480 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000481 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000482 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000483 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000484 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000486
Owen Andersone50ed302009-08-10 22:56:29 +0000487 std::vector<EVT> NodeTys;
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 NodeTys.push_back(MVT::Other); // Returns a chain
489 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000490 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000491 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000492 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000493
Chris Lattnere563bbc2008-10-11 22:08:30 +0000494 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
495 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000496 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000497
Chris Lattner98949a62008-03-17 06:01:07 +0000498 // Assign locations to each value returned by this call.
499 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000500 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000501 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000502
Dan Gohman98ca4f22009-08-05 01:29:28 +0000503 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000504
Chris Lattner98949a62008-03-17 06:01:07 +0000505 // Copy all of the result registers out of their specified physreg.
506 for (unsigned i = 0; i != RVLocs.size(); ++i) {
507 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000508
Chris Lattner98949a62008-03-17 06:01:07 +0000509 // Remap I0->I7 -> O0->O7.
510 if (Reg >= SP::I0 && Reg <= SP::I7)
511 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000512
Dale Johannesen33c960f2009-02-04 20:06:27 +0000513 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000514 RVLocs[i].getValVT(), InFlag).getValue(1);
515 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000516 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000517 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000518
Dan Gohman98ca4f22009-08-05 01:29:28 +0000519 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000520}
521
522
523
Chris Lattnerd23405e2008-03-17 03:21:36 +0000524//===----------------------------------------------------------------------===//
525// TargetLowering Implementation
526//===----------------------------------------------------------------------===//
527
528/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
529/// condition.
530static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
531 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000532 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000533 case ISD::SETEQ: return SPCC::ICC_E;
534 case ISD::SETNE: return SPCC::ICC_NE;
535 case ISD::SETLT: return SPCC::ICC_L;
536 case ISD::SETGT: return SPCC::ICC_G;
537 case ISD::SETLE: return SPCC::ICC_LE;
538 case ISD::SETGE: return SPCC::ICC_GE;
539 case ISD::SETULT: return SPCC::ICC_CS;
540 case ISD::SETULE: return SPCC::ICC_LEU;
541 case ISD::SETUGT: return SPCC::ICC_GU;
542 case ISD::SETUGE: return SPCC::ICC_CC;
543 }
544}
545
546/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
547/// FCC condition.
548static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
549 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000550 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000551 case ISD::SETEQ:
552 case ISD::SETOEQ: return SPCC::FCC_E;
553 case ISD::SETNE:
554 case ISD::SETUNE: return SPCC::FCC_NE;
555 case ISD::SETLT:
556 case ISD::SETOLT: return SPCC::FCC_L;
557 case ISD::SETGT:
558 case ISD::SETOGT: return SPCC::FCC_G;
559 case ISD::SETLE:
560 case ISD::SETOLE: return SPCC::FCC_LE;
561 case ISD::SETGE:
562 case ISD::SETOGE: return SPCC::FCC_GE;
563 case ISD::SETULT: return SPCC::FCC_UL;
564 case ISD::SETULE: return SPCC::FCC_ULE;
565 case ISD::SETUGT: return SPCC::FCC_UG;
566 case ISD::SETUGE: return SPCC::FCC_UGE;
567 case ISD::SETUO: return SPCC::FCC_U;
568 case ISD::SETO: return SPCC::FCC_O;
569 case ISD::SETONE: return SPCC::FCC_LG;
570 case ISD::SETUEQ: return SPCC::FCC_UE;
571 }
572}
573
Chris Lattnerd23405e2008-03-17 03:21:36 +0000574SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000575 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000576
Chris Lattnerd23405e2008-03-17 03:21:36 +0000577 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
579 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
580 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000581
582 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000584 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000586 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000588
589 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
591 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
592 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000593
Chris Lattnerd23405e2008-03-17 03:21:36 +0000594 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
596 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
597 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000598
599 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::UREM, MVT::i32, Expand);
601 setOperationAction(ISD::SREM, MVT::i32, Expand);
602 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
603 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000604
605 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
607 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000608
609 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
611 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000612
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
614 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000615
Chris Lattnerd23405e2008-03-17 03:21:36 +0000616 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::SELECT, MVT::i32, Expand);
618 setOperationAction(ISD::SELECT, MVT::f32, Expand);
619 setOperationAction(ISD::SELECT, MVT::f64, Expand);
620 setOperationAction(ISD::SETCC, MVT::i32, Expand);
621 setOperationAction(ISD::SETCC, MVT::f32, Expand);
622 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000623
Chris Lattnerd23405e2008-03-17 03:21:36 +0000624 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
626 setOperationAction(ISD::BRIND, MVT::Other, Expand);
627 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
628 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
629 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
630 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000631
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
633 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
634 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000635
Chris Lattnerd23405e2008-03-17 03:21:36 +0000636 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000638
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::FSIN , MVT::f64, Expand);
640 setOperationAction(ISD::FCOS , MVT::f64, Expand);
641 setOperationAction(ISD::FREM , MVT::f64, Expand);
642 setOperationAction(ISD::FSIN , MVT::f32, Expand);
643 setOperationAction(ISD::FCOS , MVT::f32, Expand);
644 setOperationAction(ISD::FREM , MVT::f32, Expand);
645 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
646 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
647 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
648 setOperationAction(ISD::ROTL , MVT::i32, Expand);
649 setOperationAction(ISD::ROTR , MVT::i32, Expand);
650 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
651 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
652 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
653 setOperationAction(ISD::FPOW , MVT::f64, Expand);
654 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
657 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
658 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000659
660 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
662 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000663
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000665
Chris Lattnerd23405e2008-03-17 03:21:36 +0000666 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000668 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000670
Chris Lattnerd23405e2008-03-17 03:21:36 +0000671 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
673 setOperationAction(ISD::VAEND , MVT::Other, Expand);
674 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
675 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
676 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000677
678 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000680
Chris Lattnerd23405e2008-03-17 03:21:36 +0000681 setStackPointerRegisterToSaveRestore(SP::O6);
682
683 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000685
Chris Lattnerd23405e2008-03-17 03:21:36 +0000686 computeRegisterProperties();
687}
688
689const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
690 switch (Opcode) {
691 default: return 0;
692 case SPISD::CMPICC: return "SPISD::CMPICC";
693 case SPISD::CMPFCC: return "SPISD::CMPFCC";
694 case SPISD::BRICC: return "SPISD::BRICC";
695 case SPISD::BRFCC: return "SPISD::BRFCC";
696 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
697 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
698 case SPISD::Hi: return "SPISD::Hi";
699 case SPISD::Lo: return "SPISD::Lo";
700 case SPISD::FTOI: return "SPISD::FTOI";
701 case SPISD::ITOF: return "SPISD::ITOF";
702 case SPISD::CALL: return "SPISD::CALL";
703 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
704 }
705}
706
707/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
708/// be zero. Op is expected to be a target specific node. Used by DAG
709/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000710void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000711 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000712 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000713 APInt &KnownOne,
714 const SelectionDAG &DAG,
715 unsigned Depth) const {
716 APInt KnownZero2, KnownOne2;
717 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000718
Chris Lattnerd23405e2008-03-17 03:21:36 +0000719 switch (Op.getOpcode()) {
720 default: break;
721 case SPISD::SELECT_ICC:
722 case SPISD::SELECT_FCC:
723 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
724 Depth+1);
725 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
726 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000727 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
728 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
729
Chris Lattnerd23405e2008-03-17 03:21:36 +0000730 // Only known if known in both the LHS and RHS.
731 KnownOne &= KnownOne2;
732 KnownZero &= KnownZero2;
733 break;
734 }
735}
736
Chris Lattnerd23405e2008-03-17 03:21:36 +0000737// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
738// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000739static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000740 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000741 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000742 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000743 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000744 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
745 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
746 (LHS.getOpcode() == SPISD::SELECT_FCC &&
747 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
748 isa<ConstantSDNode>(LHS.getOperand(0)) &&
749 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000750 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
751 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000752 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000753 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000754 LHS = CMPCC.getOperand(0);
755 RHS = CMPCC.getOperand(1);
756 }
757}
758
Chris Lattnerdb486a62009-09-15 17:46:24 +0000759SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000760 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +0000761 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000762 // FIXME there isn't really any debug info here
763 DebugLoc dl = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +0000764 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
766 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000767
768 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
769 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
770
771 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
772 getPointerTy());
773 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
774 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
775 GlobalBase, RelAddr);
776 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene54a7aa82010-02-15 16:57:02 +0000777 AbsAddr, NULL, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000778}
779
Chris Lattnerdb486a62009-09-15 17:46:24 +0000780SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000781 SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000782 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000783 // FIXME there isn't really any debug info here
784 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000785 const Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
787 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
788 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000789 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
790 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
791
792 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
793 getPointerTy());
794 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
795 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
796 GlobalBase, RelAddr);
797 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene54a7aa82010-02-15 16:57:02 +0000798 AbsAddr, NULL, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000799}
800
Dan Gohman475871a2008-07-27 21:46:04 +0000801static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000802 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000803 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 assert(Op.getValueType() == MVT::i32);
805 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
806 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000807}
808
Dan Gohman475871a2008-07-27 21:46:04 +0000809static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000810 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 assert(Op.getOperand(0).getValueType() == MVT::i32);
812 SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000813 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000814 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000815}
816
Dan Gohman475871a2008-07-27 21:46:04 +0000817static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
818 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000819 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000820 SDValue LHS = Op.getOperand(2);
821 SDValue RHS = Op.getOperand(3);
822 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000823 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000824 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000825
Chris Lattnerd23405e2008-03-17 03:21:36 +0000826 // If this is a br_cc of a "setcc", and if the setcc got lowered into
827 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
828 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000829
Chris Lattnerd23405e2008-03-17 03:21:36 +0000830 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000831 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000833 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 VTs.push_back(MVT::i32);
835 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000836 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000837 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000838 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
839 Opc = SPISD::BRICC;
840 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000842 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
843 Opc = SPISD::BRFCC;
844 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
846 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000847}
848
Dan Gohman475871a2008-07-27 21:46:04 +0000849static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
850 SDValue LHS = Op.getOperand(0);
851 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000852 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000853 SDValue TrueVal = Op.getOperand(2);
854 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000855 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000856 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000857
Chris Lattnerd23405e2008-03-17 03:21:36 +0000858 // If this is a select_cc of a "setcc", and if the setcc got lowered into
859 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
860 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000861
Dan Gohman475871a2008-07-27 21:46:04 +0000862 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000864 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000865 VTs.push_back(LHS.getValueType()); // subcc returns a value
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000867 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000868 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000869 Opc = SPISD::SELECT_ICC;
870 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
871 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000873 Opc = SPISD::SELECT_FCC;
874 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
875 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000876 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000878}
879
Dan Gohman475871a2008-07-27 21:46:04 +0000880static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000881 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +0000882 MachineFunction &MF = DAG.getMachineFunction();
883 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
884
Chris Lattnerd23405e2008-03-17 03:21:36 +0000885 // vastart just stores the address of the VarArgsFrameIndex slot into the
886 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000887 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000888 SDValue Offset =
889 DAG.getNode(ISD::ADD, dl, MVT::i32,
890 DAG.getRegister(SP::I6, MVT::i32),
891 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
892 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000893 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
David Greene54a7aa82010-02-15 16:57:02 +0000894 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0,
895 false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000896}
897
Dan Gohman475871a2008-07-27 21:46:04 +0000898static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000899 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000900 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000901 SDValue InChain = Node->getOperand(0);
902 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000903 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000904 DebugLoc dl = Node->getDebugLoc();
David Greene54a7aa82010-02-15 16:57:02 +0000905 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr, SV, 0,
906 false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000907 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000909 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000911 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000912 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
David Greene54a7aa82010-02-15 16:57:02 +0000913 VAListPtr, SV, 0, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000914 // Load the actual argument out of the pointer VAList, unless this is an
915 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 if (VT != MVT::f64)
David Greene54a7aa82010-02-15 16:57:02 +0000917 return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000918
Chris Lattnerd23405e2008-03-17 03:21:36 +0000919 // Otherwise, load it as i64, then do a bitconvert.
David Greene54a7aa82010-02-15 16:57:02 +0000920 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, NULL, 0,
921 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000922
Chris Lattnerd23405e2008-03-17 03:21:36 +0000923 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000924 SDValue Ops[2] = {
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000926 V.getValue(1)
927 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000928 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000929}
930
Dan Gohman475871a2008-07-27 21:46:04 +0000931static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
932 SDValue Chain = Op.getOperand(0); // Legalize the chain.
933 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000934 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000935
Chris Lattnerd23405e2008-03-17 03:21:36 +0000936 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
938 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000939 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000940
Chris Lattnerd23405e2008-03-17 03:21:36 +0000941 // The resultant pointer is actually 16 words from the bottom of the stack,
942 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
944 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000945 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000946 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000947}
948
Chris Lattnerd23405e2008-03-17 03:21:36 +0000949
Dan Gohman475871a2008-07-27 21:46:04 +0000950SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000951LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000952 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000953 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000954 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000955 case ISD::RETURNADDR: return SDValue();
956 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000957 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +0000958 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +0000959 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
960 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000961 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
962 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
963 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
964 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
965 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
966 case ISD::VAARG: return LowerVAARG(Op, DAG);
967 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000968 }
969}
970
971MachineBasicBlock *
972SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000973 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000974 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
975 unsigned BROpcode;
976 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +0000977 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000978 // Figure out the conditional branch opcode to use for this select_cc.
979 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000980 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000981 case SP::SELECT_CC_Int_ICC:
982 case SP::SELECT_CC_FP_ICC:
983 case SP::SELECT_CC_DFP_ICC:
984 BROpcode = SP::BCOND;
985 break;
986 case SP::SELECT_CC_Int_FCC:
987 case SP::SELECT_CC_FP_FCC:
988 case SP::SELECT_CC_DFP_FCC:
989 BROpcode = SP::FBCOND;
990 break;
991 }
992
993 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000994
Chris Lattnerd23405e2008-03-17 03:21:36 +0000995 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
996 // control-flow pattern. The incoming instruction knows the destination vreg
997 // to set, the condition code register to branch on, the true/false values to
998 // select between, and a branch opcode to use.
999 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001000 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001001 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001002
Chris Lattnerd23405e2008-03-17 03:21:36 +00001003 // thisMBB:
1004 // ...
1005 // TrueVal = ...
1006 // [f]bCC copy1MBB
1007 // fallthrough --> copy0MBB
1008 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001009 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001010 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1011 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman14152b42010-07-06 20:24:04 +00001012
1013 // Transfer the remainder of BB and its successor edges to sinkMBB.
1014 sinkMBB->splice(sinkMBB->begin(), BB,
1015 llvm::next(MachineBasicBlock::iterator(MI)),
1016 BB->end());
1017 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1018
1019 // Add the true and fallthrough blocks as its successors.
1020 BB->addSuccessor(copy0MBB);
1021 BB->addSuccessor(sinkMBB);
1022
Dale Johannesend552eee2009-02-13 02:31:35 +00001023 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001024 F->insert(It, copy0MBB);
1025 F->insert(It, sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001026
Chris Lattnerd23405e2008-03-17 03:21:36 +00001027 // copy0MBB:
1028 // %FalseValue = ...
1029 // # fallthrough to sinkMBB
1030 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001031
Chris Lattnerd23405e2008-03-17 03:21:36 +00001032 // Update machine-CFG edges
1033 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001034
Chris Lattnerd23405e2008-03-17 03:21:36 +00001035 // sinkMBB:
1036 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1037 // ...
1038 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001039 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001040 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1041 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001042
Dan Gohman14152b42010-07-06 20:24:04 +00001043 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001044 return BB;
1045}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001046
1047//===----------------------------------------------------------------------===//
1048// Sparc Inline Assembly Support
1049//===----------------------------------------------------------------------===//
1050
1051/// getConstraintType - Given a constraint letter, return the type of
1052/// constraint it is for this target.
1053SparcTargetLowering::ConstraintType
1054SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1055 if (Constraint.size() == 1) {
1056 switch (Constraint[0]) {
1057 default: break;
1058 case 'r': return C_RegisterClass;
1059 }
1060 }
1061
1062 return TargetLowering::getConstraintType(Constraint);
1063}
1064
1065std::pair<unsigned, const TargetRegisterClass*>
1066SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001067 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001068 if (Constraint.size() == 1) {
1069 switch (Constraint[0]) {
1070 case 'r':
1071 return std::make_pair(0U, SP::IntRegsRegisterClass);
1072 }
1073 }
1074
1075 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1076}
1077
1078std::vector<unsigned> SparcTargetLowering::
1079getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001080 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001081 if (Constraint.size() != 1)
1082 return std::vector<unsigned>();
1083
1084 switch (Constraint[0]) {
1085 default: break;
1086 case 'r':
1087 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1088 SP::L4, SP::L5, SP::L6, SP::L7,
1089 SP::I0, SP::I1, SP::I2, SP::I3,
1090 SP::I4, SP::I5,
1091 SP::O0, SP::O1, SP::O2, SP::O3,
1092 SP::O4, SP::O5, SP::O7, 0);
1093 }
1094
1095 return std::vector<unsigned>();
1096}
Dan Gohman6520e202008-10-18 02:06:02 +00001097
1098bool
1099SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1100 // The Sparc target isn't yet aware of offsets.
1101 return false;
1102}
Bill Wendling20c568f2009-06-30 22:38:32 +00001103
Bill Wendlingb4202b82009-07-01 18:50:55 +00001104/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001105unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001106 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001107}