blob: 515d9d72524fdf010bb4d3ae88de869838ed67ac [file] [log] [blame]
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +00001
Chris Lattnerd23405e2008-03-17 03:21:36 +00002//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file implements the interfaces that Sparc uses to lower LLVM code into a
12// selection DAG.
13//
14//===----------------------------------------------------------------------===//
15
16#include "SparcISelLowering.h"
17#include "SparcTargetMachine.h"
Dan Gohman1e93df62010-04-17 14:41:14 +000018#include "SparcMachineFunctionInfo.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000019#include "llvm/Function.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000020#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000026#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikov0eefda12008-10-10 20:28:10 +000027#include "llvm/ADT/VectorExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000029using namespace llvm;
30
Chris Lattner5a65b922008-03-17 05:41:48 +000031
32//===----------------------------------------------------------------------===//
33// Calling Convention Implementation
34//===----------------------------------------------------------------------===//
35
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +000036static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
37 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
38 ISD::ArgFlagsTy &ArgFlags, CCState &State)
39{
40 assert (ArgFlags.isSRet());
41
42 //Assign SRet argument
43 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
44 0,
45 LocVT, LocInfo));
46 return true;
47}
48
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +000049static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
50 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
51 ISD::ArgFlagsTy &ArgFlags, CCState &State)
52{
53 static const unsigned RegList[] = {
54 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
55 };
56 //Try to get first reg
57 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
58 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
59 } else {
60 //Assign whole thing in stack
61 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
62 State.AllocateStack(8,4),
63 LocVT, LocInfo));
64 return true;
65 }
66
67 //Try to get second reg
68 if (unsigned Reg = State.AllocateReg(RegList, 6))
69 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
70 else
71 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
72 State.AllocateStack(4,4),
73 LocVT, LocInfo));
74 return true;
75}
76
Chris Lattner5a65b922008-03-17 05:41:48 +000077#include "SparcGenCallingConv.inc"
78
Dan Gohman98ca4f22009-08-05 01:29:28 +000079SDValue
80SparcTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000081 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000082 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +000083 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +000084 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000085
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +000086 MachineFunction &MF = DAG.getMachineFunction();
87
Chris Lattner5a65b922008-03-17 05:41:48 +000088 // CCValAssign - represent the assignment of the return value to locations.
89 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000090
Chris Lattner5a65b922008-03-17 05:41:48 +000091 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +000092 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
93 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000094
Dan Gohman98ca4f22009-08-05 01:29:28 +000095 // Analize return values.
96 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000097
Chris Lattner5a65b922008-03-17 05:41:48 +000098 // If this is the first return lowered for this function, add the regs to the
99 // liveout set for the function.
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000100 if (MF.getRegInfo().liveout_empty()) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000101 for (unsigned i = 0; i != RVLocs.size(); ++i)
102 if (RVLocs[i].isRegLoc())
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000103 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner5a65b922008-03-17 05:41:48 +0000104 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000105
Dan Gohman475871a2008-07-27 21:46:04 +0000106 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +0000107
108 // Copy the result values into the output registers.
109 for (unsigned i = 0; i != RVLocs.size(); ++i) {
110 CCValAssign &VA = RVLocs[i];
111 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +0000112
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000113 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +0000114 OutVals[i], Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000115
Chris Lattner5a65b922008-03-17 05:41:48 +0000116 // Guarantee that all emitted copies are stuck together with flags.
117 Flag = Chain.getValue(1);
118 }
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000119 // If the function returns a struct, copy the SRetReturnReg to I0
120 if (MF.getFunction()->hasStructRetAttr()) {
121 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
122 unsigned Reg = SFI->getSRetReturnReg();
123 if (!Reg)
124 llvm_unreachable("sret virtual register not created in the entry block");
125 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
126 Chain = DAG.getCopyToReg(Chain, dl, SP::I0, Val, Flag);
127 Flag = Chain.getValue(1);
128 if (MF.getRegInfo().liveout_empty())
129 MF.getRegInfo().addLiveOut(SP::I0);
130 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000131
Gabor Greifba36cb52008-08-28 21:40:38 +0000132 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
134 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner5a65b922008-03-17 05:41:48 +0000135}
136
Dan Gohman98ca4f22009-08-05 01:29:28 +0000137/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
138/// passed in either one or two GPRs, including FP values. TODO: we should
139/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000140SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000141SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000142 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000143 const SmallVectorImpl<ISD::InputArg>
144 &Ins,
145 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000146 SmallVectorImpl<SDValue> &InVals)
147 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000148
Chris Lattner5a65b922008-03-17 05:41:48 +0000149 MachineFunction &MF = DAG.getMachineFunction();
150 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +0000151 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +0000152
153 // Assign locations to all of the incoming arguments.
154 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000155 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
156 ArgLocs, *DAG.getContext());
157 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000158
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000159 const unsigned StackOffset = 92;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000160
Eli Friedmana786c7b2009-07-19 19:53:46 +0000161 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Eli Friedmana786c7b2009-07-19 19:53:46 +0000162 CCValAssign &VA = ArgLocs[i];
Chris Lattner5a65b922008-03-17 05:41:48 +0000163
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000164 if (i == 0 && Ins[i].Flags.isSRet()) {
165 //Get SRet from [%fp+64]
166 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
167 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
168 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
169 MachinePointerInfo(),
170 false, false, 0);
171 InVals.push_back(Arg);
172 continue;
173 }
174
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000175 if (VA.isRegLoc()) {
176 EVT RegVT = VA.getLocVT();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000177
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000178 if (VA.needsCustom()) {
179 assert(VA.getLocVT() == MVT::f64);
180 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
181 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
182 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000183
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000184 assert(i+1 < e);
185 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikov53835702008-10-10 20:27:31 +0000186
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDValue LoVal;
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000188 if (NextVA.isMemLoc()) {
189 int FrameIdx = MF.getFrameInfo()->
190 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000192 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
193 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000194 false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000195 } else {
196 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
197 &SP::IntRegsRegClass);
198 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000199 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000200 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000202 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000203 InVals.push_back(WholeValue);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000204 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000205 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000206 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
207 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
208 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
209 if (VA.getLocVT() == MVT::f32)
210 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
211 else if (VA.getLocVT() != MVT::i32) {
212 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
213 DAG.getValueType(VA.getLocVT()));
214 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
215 }
216 InVals.push_back(Arg);
217 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000218 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000219
220 assert(VA.isMemLoc());
221
222 unsigned Offset = VA.getLocMemOffset()+StackOffset;
223
224 if (VA.needsCustom()) {
225 assert(VA.getValVT() == MVT::f64);
226 //If it is double-word aligned, just load.
227 if (Offset % 8 == 0) {
228 int FI = MF.getFrameInfo()->CreateFixedObject(8,
229 Offset,
230 true);
231 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
232 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
233 MachinePointerInfo(),
234 false,false, 0);
235 InVals.push_back(Load);
236 continue;
237 }
238
239 int FI = MF.getFrameInfo()->CreateFixedObject(4,
240 Offset,
241 true);
242 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
243 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
244 MachinePointerInfo(),
245 false, false, 0);
246 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
247 Offset+4,
248 true);
249 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
250
251 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
252 MachinePointerInfo(),
253 false, false, 0);
254
255 SDValue WholeValue =
256 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
257 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
258 InVals.push_back(WholeValue);
259 continue;
260 }
261
262 int FI = MF.getFrameInfo()->CreateFixedObject(4,
263 Offset,
264 true);
265 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
266 SDValue Load ;
267 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
268 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
269 MachinePointerInfo(),
270 false, false, 0);
271 } else {
272 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
273 // Sparc is big endian, so add an offset based on the ObjectVT.
274 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
275 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
276 DAG.getConstant(Offset, MVT::i32));
277 Load = DAG.getExtLoad(LoadOp, MVT::i32, dl, Chain, FIPtr,
278 MachinePointerInfo(),
279 VA.getValVT(), false, false,0);
280 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
281 }
282 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000283 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000284
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000285 if (MF.getFunction()->hasStructRetAttr()) {
286 //Copy the SRet Argument to SRetReturnReg
287 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
288 unsigned Reg = SFI->getSRetReturnReg();
289 if (!Reg) {
290 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
291 SFI->setSRetReturnReg(Reg);
292 }
293 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
294 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
295 }
296
Chris Lattner5a65b922008-03-17 05:41:48 +0000297 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000298 if (isVarArg) {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000299 static const unsigned ArgRegs[] = {
300 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
301 };
302 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
303 const unsigned *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
304 unsigned ArgOffset = CCInfo.getNextStackOffset();
305 if (NumAllocated == 6)
306 ArgOffset += StackOffset;
307 else {
308 assert(!ArgOffset);
309 ArgOffset = 68+4*NumAllocated;
310 }
311
Chris Lattner5a65b922008-03-17 05:41:48 +0000312 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000313 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000314
Eli Friedmana786c7b2009-07-19 19:53:46 +0000315 std::vector<SDValue> OutChains;
316
Chris Lattner5a65b922008-03-17 05:41:48 +0000317 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
318 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
319 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000321
David Greene3f2bf852009-11-12 20:49:22 +0000322 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000323 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000325
Chris Lattner6229d0a2010-09-21 18:41:36 +0000326 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
327 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000328 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000329 ArgOffset += 4;
330 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000331
332 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000333 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000335 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000336 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000337 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000338
Dan Gohman98ca4f22009-08-05 01:29:28 +0000339 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000340}
341
Dan Gohman98ca4f22009-08-05 01:29:28 +0000342SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000343SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000344 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000345 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000346 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000347 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000348 const SmallVectorImpl<ISD::InputArg> &Ins,
349 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000350 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000351 // Sparc target does not yet support tail call optimization.
352 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000353
Chris Lattner315123f2008-03-17 06:58:37 +0000354 // Analyze operands of the call, assigning locations to each operand.
355 SmallVector<CCValAssign, 16> ArgLocs;
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000356 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs,
357 *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000358 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000359
Chris Lattner315123f2008-03-17 06:58:37 +0000360 // Get the size of the outgoing arguments stack space requirement.
361 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000362
Chris Lattner5a65b922008-03-17 05:41:48 +0000363 // Keep stack frames 8-byte aligned.
364 ArgsSize = (ArgsSize+7) & ~7;
365
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000366 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
367
368 //Create local copies for byval args.
369 SmallVector<SDValue, 8> ByValArgs;
370 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
371 ISD::ArgFlagsTy Flags = Outs[i].Flags;
372 if (!Flags.isByVal())
373 continue;
374
375 SDValue Arg = OutVals[i];
376 unsigned Size = Flags.getByValSize();
377 unsigned Align = Flags.getByValAlign();
378
379 int FI = MFI->CreateStackObject(Size, Align, false);
380 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
381 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
382
383 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
384 false, //isVolatile,
385 (Size <= 32), //AlwaysInline if size <= 32
386 MachinePointerInfo(), MachinePointerInfo());
387 ByValArgs.push_back(FIPtr);
388 }
389
Chris Lattnere563bbc2008-10-11 22:08:30 +0000390 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000391
Dan Gohman475871a2008-07-27 21:46:04 +0000392 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
393 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000394
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000395 const unsigned StackOffset = 92;
Chris Lattner315123f2008-03-17 06:58:37 +0000396 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000397 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000398 i != e;
399 ++i, ++realArgIdx) {
Chris Lattner315123f2008-03-17 06:58:37 +0000400 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000401 SDValue Arg = OutVals[realArgIdx];
Chris Lattner315123f2008-03-17 06:58:37 +0000402
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000403 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
404
405 //Use local copy if it is a byval arg.
406 if (Flags.isByVal())
407 Arg = ByValArgs[byvalArgIdx++];
408
Chris Lattner315123f2008-03-17 06:58:37 +0000409 // Promote the value if needed.
410 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000411 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000412 case CCValAssign::Full: break;
413 case CCValAssign::SExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000414 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000415 break;
416 case CCValAssign::ZExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000417 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000418 break;
419 case CCValAssign::AExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000420 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
421 break;
422 case CCValAssign::BCvt:
423 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000424 break;
425 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000426
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000427 if (Flags.isSRet()) {
428 assert(VA.needsCustom());
429 // store SRet argument in %sp+64
430 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
431 SDValue PtrOff = DAG.getIntPtrConstant(64);
432 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
433 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
434 MachinePointerInfo(),
435 false, false, 0));
436 continue;
437 }
438
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000439 if (VA.needsCustom()) {
440 assert(VA.getLocVT() == MVT::f64);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000441
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000442 if (VA.isMemLoc()) {
443 unsigned Offset = VA.getLocMemOffset() + StackOffset;
444 //if it is double-word aligned, just store.
445 if (Offset % 8 == 0) {
446 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
447 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
448 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
449 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
450 MachinePointerInfo(),
451 false, false, 0));
452 continue;
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000453 }
454 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000455
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000457 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000458 Arg, StackPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000459 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000460 // Sparc is big-endian, so the high part comes first.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000461 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
462 MachinePointerInfo(), false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000463 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000464 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000465 DAG.getIntPtrConstant(4));
466 // Load the low part.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000467 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
468 MachinePointerInfo(), false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000469
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000470 if (VA.isRegLoc()) {
471 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
472 assert(i+1 != e);
473 CCValAssign &NextVA = ArgLocs[++i];
474 if (NextVA.isRegLoc()) {
475 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
476 } else {
477 //Store the low part in stack.
478 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
479 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
480 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
481 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
482 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
483 MachinePointerInfo(),
484 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000485 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000486 } else {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000487 unsigned Offset = VA.getLocMemOffset() + StackOffset;
488 // Store the high part.
489 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
490 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
491 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
492 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
493 MachinePointerInfo(),
494 false, false, 0));
495 // Store the low part.
496 PtrOff = DAG.getIntPtrConstant(Offset+4);
497 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
498 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
499 MachinePointerInfo(),
500 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000501 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000502 continue;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000503 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000504
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000505 // Arguments that can be passed on register must be kept at
506 // RegsToPass vector
507 if (VA.isRegLoc()) {
508 if (VA.getLocVT() != MVT::f32) {
509 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
510 continue;
511 }
512 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
513 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
514 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000515 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000516
517 assert(VA.isMemLoc());
518
519 // Create a store off the stack pointer for this argument.
520 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
521 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
522 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
523 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
524 MachinePointerInfo(),
525 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000526 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000527
Anton Korobeynikov53835702008-10-10 20:27:31 +0000528
Chris Lattner5a65b922008-03-17 05:41:48 +0000529 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000530 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000532 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000533
534 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000535 // chain and flag operands which copy the outgoing args into registers.
536 // The InFlag in necessary since all emited instructions must be
537 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000538 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
540 unsigned Reg = RegsToPass[i].first;
541 // Remap I0->I7 -> O0->O7.
542 if (Reg >= SP::I0 && Reg <= SP::I7)
543 Reg = Reg-SP::I0+SP::O0;
544
Dale Johannesen33c960f2009-02-04 20:06:27 +0000545 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000546 InFlag = Chain.getValue(1);
547 }
548
549 // If the callee is a GlobalAddress node (quite common, every direct call is)
550 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000551 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000552 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000553 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000554 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000556
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000557 // Returns a chain & a flag for retval copy to use
558 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
559 SmallVector<SDValue, 8> Ops;
560 Ops.push_back(Chain);
561 Ops.push_back(Callee);
562 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
563 unsigned Reg = RegsToPass[i].first;
564 if (Reg >= SP::I0 && Reg <= SP::I7)
565 Reg = Reg-SP::I0+SP::O0;
566
567 Ops.push_back(DAG.getRegister(Reg, RegsToPass[i].second.getValueType()));
568 }
569 if (InFlag.getNode())
570 Ops.push_back(InFlag);
571
572 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000573 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000574
Chris Lattnere563bbc2008-10-11 22:08:30 +0000575 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
576 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000577 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000578
Chris Lattner98949a62008-03-17 06:01:07 +0000579 // Assign locations to each value returned by this call.
580 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000581 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000582 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000583
Dan Gohman98ca4f22009-08-05 01:29:28 +0000584 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000585
Chris Lattner98949a62008-03-17 06:01:07 +0000586 // Copy all of the result registers out of their specified physreg.
587 for (unsigned i = 0; i != RVLocs.size(); ++i) {
588 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000589
Chris Lattner98949a62008-03-17 06:01:07 +0000590 // Remap I0->I7 -> O0->O7.
591 if (Reg >= SP::I0 && Reg <= SP::I7)
592 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000593
Dale Johannesen33c960f2009-02-04 20:06:27 +0000594 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000595 RVLocs[i].getValVT(), InFlag).getValue(1);
596 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000597 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000598 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000599
Dan Gohman98ca4f22009-08-05 01:29:28 +0000600 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000601}
602
603
604
Chris Lattnerd23405e2008-03-17 03:21:36 +0000605//===----------------------------------------------------------------------===//
606// TargetLowering Implementation
607//===----------------------------------------------------------------------===//
608
609/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
610/// condition.
611static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
612 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000613 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000614 case ISD::SETEQ: return SPCC::ICC_E;
615 case ISD::SETNE: return SPCC::ICC_NE;
616 case ISD::SETLT: return SPCC::ICC_L;
617 case ISD::SETGT: return SPCC::ICC_G;
618 case ISD::SETLE: return SPCC::ICC_LE;
619 case ISD::SETGE: return SPCC::ICC_GE;
620 case ISD::SETULT: return SPCC::ICC_CS;
621 case ISD::SETULE: return SPCC::ICC_LEU;
622 case ISD::SETUGT: return SPCC::ICC_GU;
623 case ISD::SETUGE: return SPCC::ICC_CC;
624 }
625}
626
627/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
628/// FCC condition.
629static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
630 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000631 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000632 case ISD::SETEQ:
633 case ISD::SETOEQ: return SPCC::FCC_E;
634 case ISD::SETNE:
635 case ISD::SETUNE: return SPCC::FCC_NE;
636 case ISD::SETLT:
637 case ISD::SETOLT: return SPCC::FCC_L;
638 case ISD::SETGT:
639 case ISD::SETOGT: return SPCC::FCC_G;
640 case ISD::SETLE:
641 case ISD::SETOLE: return SPCC::FCC_LE;
642 case ISD::SETGE:
643 case ISD::SETOGE: return SPCC::FCC_GE;
644 case ISD::SETULT: return SPCC::FCC_UL;
645 case ISD::SETULE: return SPCC::FCC_ULE;
646 case ISD::SETUGT: return SPCC::FCC_UG;
647 case ISD::SETUGE: return SPCC::FCC_UGE;
648 case ISD::SETUO: return SPCC::FCC_U;
649 case ISD::SETO: return SPCC::FCC_O;
650 case ISD::SETONE: return SPCC::FCC_LG;
651 case ISD::SETUEQ: return SPCC::FCC_UE;
652 }
653}
654
Chris Lattnerd23405e2008-03-17 03:21:36 +0000655SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000656 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000657
Chris Lattnerd23405e2008-03-17 03:21:36 +0000658 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
660 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
661 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000662
663 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000665 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000667 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000669
670 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
672 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
673 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000674
Chris Lattnerd23405e2008-03-17 03:21:36 +0000675 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
677 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
678 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000679
680 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::UREM, MVT::i32, Expand);
682 setOperationAction(ISD::SREM, MVT::i32, Expand);
683 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
684 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000685
686 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
688 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000689
690 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
692 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000693
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000694 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
695 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000696
Chris Lattnerd23405e2008-03-17 03:21:36 +0000697 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::SELECT, MVT::i32, Expand);
699 setOperationAction(ISD::SELECT, MVT::f32, Expand);
700 setOperationAction(ISD::SELECT, MVT::f64, Expand);
701 setOperationAction(ISD::SETCC, MVT::i32, Expand);
702 setOperationAction(ISD::SETCC, MVT::f32, Expand);
703 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000704
Chris Lattnerd23405e2008-03-17 03:21:36 +0000705 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
707 setOperationAction(ISD::BRIND, MVT::Other, Expand);
708 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
709 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
710 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
711 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000712
Owen Anderson825b72b2009-08-11 20:47:22 +0000713 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
714 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
715 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000716
Chris Lattnerd23405e2008-03-17 03:21:36 +0000717 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000719
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::FSIN , MVT::f64, Expand);
721 setOperationAction(ISD::FCOS , MVT::f64, Expand);
722 setOperationAction(ISD::FREM , MVT::f64, Expand);
723 setOperationAction(ISD::FSIN , MVT::f32, Expand);
724 setOperationAction(ISD::FCOS , MVT::f32, Expand);
725 setOperationAction(ISD::FREM , MVT::f32, Expand);
726 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
727 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
728 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
729 setOperationAction(ISD::ROTL , MVT::i32, Expand);
730 setOperationAction(ISD::ROTR , MVT::i32, Expand);
731 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
732 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
733 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
734 setOperationAction(ISD::FPOW , MVT::f64, Expand);
735 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000736
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
738 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
739 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000740
741 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
743 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000744
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000746
Chris Lattnerd23405e2008-03-17 03:21:36 +0000747 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000749 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000751
Chris Lattnerd23405e2008-03-17 03:21:36 +0000752 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
754 setOperationAction(ISD::VAEND , MVT::Other, Expand);
755 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
756 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
757 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000758
759 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000761
Chris Lattnerd23405e2008-03-17 03:21:36 +0000762 setStackPointerRegisterToSaveRestore(SP::O6);
763
764 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000766
Chris Lattnerd23405e2008-03-17 03:21:36 +0000767 computeRegisterProperties();
768}
769
770const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
771 switch (Opcode) {
772 default: return 0;
773 case SPISD::CMPICC: return "SPISD::CMPICC";
774 case SPISD::CMPFCC: return "SPISD::CMPFCC";
775 case SPISD::BRICC: return "SPISD::BRICC";
776 case SPISD::BRFCC: return "SPISD::BRFCC";
777 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
778 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
779 case SPISD::Hi: return "SPISD::Hi";
780 case SPISD::Lo: return "SPISD::Lo";
781 case SPISD::FTOI: return "SPISD::FTOI";
782 case SPISD::ITOF: return "SPISD::ITOF";
783 case SPISD::CALL: return "SPISD::CALL";
784 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +0000785 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +0000786 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Chris Lattnerd23405e2008-03-17 03:21:36 +0000787 }
788}
789
790/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
791/// be zero. Op is expected to be a target specific node. Used by DAG
792/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000793void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000794 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000795 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000796 APInt &KnownOne,
797 const SelectionDAG &DAG,
798 unsigned Depth) const {
799 APInt KnownZero2, KnownOne2;
800 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000801
Chris Lattnerd23405e2008-03-17 03:21:36 +0000802 switch (Op.getOpcode()) {
803 default: break;
804 case SPISD::SELECT_ICC:
805 case SPISD::SELECT_FCC:
806 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
807 Depth+1);
808 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
809 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000810 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
811 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
812
Chris Lattnerd23405e2008-03-17 03:21:36 +0000813 // Only known if known in both the LHS and RHS.
814 KnownOne &= KnownOne2;
815 KnownZero &= KnownZero2;
816 break;
817 }
818}
819
Chris Lattnerd23405e2008-03-17 03:21:36 +0000820// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
821// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000822static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000823 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000824 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000825 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000826 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000827 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
828 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
829 (LHS.getOpcode() == SPISD::SELECT_FCC &&
830 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
831 isa<ConstantSDNode>(LHS.getOperand(0)) &&
832 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000833 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
834 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000835 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000836 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000837 LHS = CMPCC.getOperand(0);
838 RHS = CMPCC.getOperand(1);
839 }
840}
841
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000842SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000843 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +0000844 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000845 // FIXME there isn't really any debug info here
846 DebugLoc dl = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +0000847 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
849 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000850
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000851 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000852 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000853
Chris Lattnerdb486a62009-09-15 17:46:24 +0000854 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
855 getPointerTy());
856 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000857 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000858 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000859 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000860 AbsAddr, MachinePointerInfo(), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000861}
862
Chris Lattnerdb486a62009-09-15 17:46:24 +0000863SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000864 SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000865 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000866 // FIXME there isn't really any debug info here
867 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000868 const Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
870 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
871 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000872 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000873 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
874
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000875 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000876 getPointerTy());
877 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
878 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
879 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000880 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000881 AbsAddr, MachinePointerInfo(), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000882}
883
Dan Gohman475871a2008-07-27 21:46:04 +0000884static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000885 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000886 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 assert(Op.getValueType() == MVT::i32);
888 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000889 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000890}
891
Dan Gohman475871a2008-07-27 21:46:04 +0000892static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000893 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 assert(Op.getOperand(0).getValueType() == MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000895 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000896 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000897 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000898}
899
Dan Gohman475871a2008-07-27 21:46:04 +0000900static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
901 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000902 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000903 SDValue LHS = Op.getOperand(2);
904 SDValue RHS = Op.getOperand(3);
905 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000906 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000907 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000908
Chris Lattnerd23405e2008-03-17 03:21:36 +0000909 // If this is a br_cc of a "setcc", and if the setcc got lowered into
910 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
911 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000912
Chris Lattnerd23405e2008-03-17 03:21:36 +0000913 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000914 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000916 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 VTs.push_back(MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000918 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000919 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000920 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000921 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
922 Opc = SPISD::BRICC;
923 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000924 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000925 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
926 Opc = SPISD::BRFCC;
927 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
929 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000930}
931
Dan Gohman475871a2008-07-27 21:46:04 +0000932static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
933 SDValue LHS = Op.getOperand(0);
934 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000935 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SDValue TrueVal = Op.getOperand(2);
937 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000938 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000939 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000940
Chris Lattnerd23405e2008-03-17 03:21:36 +0000941 // If this is a select_cc of a "setcc", and if the setcc got lowered into
942 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
943 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000944
Dan Gohman475871a2008-07-27 21:46:04 +0000945 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000947 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000948 VTs.push_back(LHS.getValueType()); // subcc returns a value
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000949 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000950 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000951 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000952 Opc = SPISD::SELECT_ICC;
953 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
954 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000955 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000956 Opc = SPISD::SELECT_FCC;
957 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
958 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000959 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000961}
962
Dan Gohman475871a2008-07-27 21:46:04 +0000963static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000964 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +0000965 MachineFunction &MF = DAG.getMachineFunction();
966 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
967
Chris Lattnerd23405e2008-03-17 03:21:36 +0000968 // vastart just stores the address of the VarArgsFrameIndex slot into the
969 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000970 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000971 SDValue Offset =
972 DAG.getNode(ISD::ADD, dl, MVT::i32,
973 DAG.getRegister(SP::I6, MVT::i32),
974 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
975 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000976 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +0000977 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1),
978 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000979}
980
Dan Gohman475871a2008-07-27 21:46:04 +0000981static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000982 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000983 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000984 SDValue InChain = Node->getOperand(0);
985 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000986 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000987 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000988 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr,
989 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000990 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000992 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000994 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000995 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000996 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000997 // Load the actual argument out of the pointer VAList, unless this is an
998 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000999 if (VT != MVT::f64)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001000 return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo(),
1001 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001002
Chris Lattnerd23405e2008-03-17 03:21:36 +00001003 // Otherwise, load it as i64, then do a bitconvert.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001004 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +00001005 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001006
Chris Lattnerd23405e2008-03-17 03:21:36 +00001007 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001008 SDValue Ops[2] = {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001009 DAG.getNode(ISD::BITCAST, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +00001010 V.getValue(1)
1011 };
Dale Johannesen33c960f2009-02-04 20:06:27 +00001012 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001013}
1014
Dan Gohman475871a2008-07-27 21:46:04 +00001015static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1016 SDValue Chain = Op.getOperand(0); // Legalize the chain.
1017 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +00001018 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001019
Chris Lattnerd23405e2008-03-17 03:21:36 +00001020 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
1022 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +00001023 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +00001024
Chris Lattnerd23405e2008-03-17 03:21:36 +00001025 // The resultant pointer is actually 16 words from the bottom of the stack,
1026 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +00001027 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
1028 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00001029 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +00001030 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001031}
1032
Chris Lattnerd23405e2008-03-17 03:21:36 +00001033
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001034static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001035 DebugLoc dl = Op.getDebugLoc();
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001036 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001037 dl, MVT::Other, DAG.getEntryNode());
1038 return Chain;
1039}
1040
1041static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1042 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1043 MFI->setFrameAddressIsTaken(true);
1044
1045 EVT VT = Op.getValueType();
1046 DebugLoc dl = Op.getDebugLoc();
1047 unsigned FrameReg = SP::I6;
1048
1049 uint64_t depth = Op.getConstantOperandVal(0);
1050
1051 SDValue FrameAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001052 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001053 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1054 else {
1055 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001056 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001057 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001058
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001059 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001060 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001061 dl, MVT::i32,
1062 FrameAddr, DAG.getIntPtrConstant(56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001063 FrameAddr = DAG.getLoad(MVT::i32, dl,
1064 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001065 Ptr,
1066 MachinePointerInfo(), false, false, 0);
1067 }
1068 }
1069 return FrameAddr;
1070}
1071
1072static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
1073 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1074 MFI->setReturnAddressIsTaken(true);
1075
1076 EVT VT = Op.getValueType();
1077 DebugLoc dl = Op.getDebugLoc();
1078 unsigned RetReg = SP::I7;
1079
1080 uint64_t depth = Op.getConstantOperandVal(0);
1081
1082 SDValue RetAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001083 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001084 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
1085 else {
1086 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001087 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001088 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001089
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001090 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001091 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001092 dl, MVT::i32,
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001093 RetAddr,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001094 DAG.getIntPtrConstant((i == depth-1)?60:56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001095 RetAddr = DAG.getLoad(MVT::i32, dl,
1096 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001097 Ptr,
1098 MachinePointerInfo(), false, false, 0);
1099 }
1100 }
1101 return RetAddr;
1102}
1103
Dan Gohman475871a2008-07-27 21:46:04 +00001104SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001105LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001106 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001107 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001108 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1109 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001110 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +00001111 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +00001112 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1113 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001114 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1115 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
1116 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
1117 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1118 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
1119 case ISD::VAARG: return LowerVAARG(Op, DAG);
1120 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001121 }
1122}
1123
1124MachineBasicBlock *
1125SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001126 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001127 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1128 unsigned BROpcode;
1129 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +00001130 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001131 // Figure out the conditional branch opcode to use for this select_cc.
1132 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001133 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001134 case SP::SELECT_CC_Int_ICC:
1135 case SP::SELECT_CC_FP_ICC:
1136 case SP::SELECT_CC_DFP_ICC:
1137 BROpcode = SP::BCOND;
1138 break;
1139 case SP::SELECT_CC_Int_FCC:
1140 case SP::SELECT_CC_FP_FCC:
1141 case SP::SELECT_CC_DFP_FCC:
1142 BROpcode = SP::FBCOND;
1143 break;
1144 }
1145
1146 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001147
Chris Lattnerd23405e2008-03-17 03:21:36 +00001148 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1149 // control-flow pattern. The incoming instruction knows the destination vreg
1150 // to set, the condition code register to branch on, the true/false values to
1151 // select between, and a branch opcode to use.
1152 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001153 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001154 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001155
Chris Lattnerd23405e2008-03-17 03:21:36 +00001156 // thisMBB:
1157 // ...
1158 // TrueVal = ...
1159 // [f]bCC copy1MBB
1160 // fallthrough --> copy0MBB
1161 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001162 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001163 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1164 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindarajuf6612772010-12-28 20:39:17 +00001165 F->insert(It, copy0MBB);
1166 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00001167
1168 // Transfer the remainder of BB and its successor edges to sinkMBB.
1169 sinkMBB->splice(sinkMBB->begin(), BB,
1170 llvm::next(MachineBasicBlock::iterator(MI)),
1171 BB->end());
1172 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1173
1174 // Add the true and fallthrough blocks as its successors.
1175 BB->addSuccessor(copy0MBB);
1176 BB->addSuccessor(sinkMBB);
1177
Dale Johannesend552eee2009-02-13 02:31:35 +00001178 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001179
Chris Lattnerd23405e2008-03-17 03:21:36 +00001180 // copy0MBB:
1181 // %FalseValue = ...
1182 // # fallthrough to sinkMBB
1183 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001184
Chris Lattnerd23405e2008-03-17 03:21:36 +00001185 // Update machine-CFG edges
1186 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001187
Chris Lattnerd23405e2008-03-17 03:21:36 +00001188 // sinkMBB:
1189 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1190 // ...
1191 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001192 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001193 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1194 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001195
Dan Gohman14152b42010-07-06 20:24:04 +00001196 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001197 return BB;
1198}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001199
1200//===----------------------------------------------------------------------===//
1201// Sparc Inline Assembly Support
1202//===----------------------------------------------------------------------===//
1203
1204/// getConstraintType - Given a constraint letter, return the type of
1205/// constraint it is for this target.
1206SparcTargetLowering::ConstraintType
1207SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1208 if (Constraint.size() == 1) {
1209 switch (Constraint[0]) {
1210 default: break;
1211 case 'r': return C_RegisterClass;
1212 }
1213 }
1214
1215 return TargetLowering::getConstraintType(Constraint);
1216}
1217
1218std::pair<unsigned, const TargetRegisterClass*>
1219SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001220 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001221 if (Constraint.size() == 1) {
1222 switch (Constraint[0]) {
1223 case 'r':
1224 return std::make_pair(0U, SP::IntRegsRegisterClass);
1225 }
1226 }
1227
1228 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1229}
1230
1231std::vector<unsigned> SparcTargetLowering::
1232getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001233 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001234 if (Constraint.size() != 1)
1235 return std::vector<unsigned>();
1236
1237 switch (Constraint[0]) {
1238 default: break;
1239 case 'r':
1240 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1241 SP::L4, SP::L5, SP::L6, SP::L7,
1242 SP::I0, SP::I1, SP::I2, SP::I3,
1243 SP::I4, SP::I5,
1244 SP::O0, SP::O1, SP::O2, SP::O3,
1245 SP::O4, SP::O5, SP::O7, 0);
1246 }
1247
1248 return std::vector<unsigned>();
1249}
Dan Gohman6520e202008-10-18 02:06:02 +00001250
1251bool
1252SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1253 // The Sparc target isn't yet aware of offsets.
1254 return false;
1255}
Bill Wendling20c568f2009-06-30 22:38:32 +00001256
Bill Wendlingb4202b82009-07-01 18:50:55 +00001257/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001258unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001259 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001260}