blob: 3863e2cc1caf61353d076caafc5d3064877087eb [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"
Dan Gohman1e93df62010-04-17 14:41:14 +000016#include "SparcMachineFunctionInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "SparcTargetMachine.h"
Jakob Stoklund Olesen0ec587e2013-04-14 01:33:32 +000018#include "MCTargetDesc/SparcBaseInfo.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"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/Module.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000030using namespace llvm;
31
Chris Lattner5a65b922008-03-17 05:41:48 +000032
33//===----------------------------------------------------------------------===//
34// Calling Convention Implementation
35//===----------------------------------------------------------------------===//
36
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +000037static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
38 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
39 ISD::ArgFlagsTy &ArgFlags, CCState &State)
40{
41 assert (ArgFlags.isSRet());
42
43 //Assign SRet argument
44 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
45 0,
46 LocVT, LocInfo));
47 return true;
48}
49
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +000050static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
51 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags, CCState &State)
53{
Craig Topperc5eaae42012-03-11 07:57:25 +000054 static const uint16_t RegList[] = {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +000055 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
56 };
57 //Try to get first reg
58 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
59 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
60 } else {
61 //Assign whole thing in stack
62 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
63 State.AllocateStack(8,4),
64 LocVT, LocInfo));
65 return true;
66 }
67
68 //Try to get second reg
69 if (unsigned Reg = State.AllocateReg(RegList, 6))
70 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
71 else
72 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
73 State.AllocateStack(4,4),
74 LocVT, LocInfo));
75 return true;
76}
77
Jakob Stoklund Olesen1f25fe52013-04-06 18:32:12 +000078// Allocate a full-sized argument for the 64-bit ABI.
79static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
80 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
81 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
82 assert((LocVT == MVT::f32 || LocVT.getSizeInBits() == 64) &&
83 "Can't handle non-64 bits locations");
84
85 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
86 unsigned Offset = State.AllocateStack(8, 8);
87 unsigned Reg = 0;
88
89 if (LocVT == MVT::i64 && Offset < 6*8)
90 // Promote integers to %i0-%i5.
91 Reg = SP::I0 + Offset/8;
92 else if (LocVT == MVT::f64 && Offset < 16*8)
93 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
94 Reg = SP::D0 + Offset/8;
95 else if (LocVT == MVT::f32 && Offset < 16*8)
96 // Promote floats to %f1, %f3, ...
97 Reg = SP::F1 + Offset/4;
98
99 // Promote to register when possible, otherwise use the stack slot.
100 if (Reg) {
101 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
102 return true;
103 }
104
105 // This argument goes on the stack in an 8-byte slot.
106 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
107 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
108 if (LocVT == MVT::f32)
109 Offset += 4;
110
111 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
112 return true;
113}
114
115// Allocate a half-sized argument for the 64-bit ABI.
116//
117// This is used when passing { float, int } structs by value in registers.
118static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
119 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
120 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
121 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
122 unsigned Offset = State.AllocateStack(4, 4);
123
124 if (LocVT == MVT::f32 && Offset < 16*8) {
125 // Promote floats to %f0-%f31.
126 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
127 LocVT, LocInfo));
128 return true;
129 }
130
131 if (LocVT == MVT::i32 && Offset < 6*8) {
132 // Promote integers to %i0-%i5, using half the register.
133 unsigned Reg = SP::I0 + Offset/8;
134 LocVT = MVT::i64;
135 LocInfo = CCValAssign::AExt;
136
137 // Set the Custom bit if this i32 goes in the high bits of a register.
138 if (Offset % 8 == 0)
139 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
140 LocVT, LocInfo));
141 else
142 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
143 return true;
144 }
145
146 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
147 return true;
148}
149
Chris Lattner5a65b922008-03-17 05:41:48 +0000150#include "SparcGenCallingConv.inc"
151
Jakob Stoklund Olesen1b133a42013-04-09 05:11:52 +0000152// The calling conventions in SparcCallingConv.td are described in terms of the
153// callee's register window. This function translates registers to the
154// corresponding caller window %o register.
155static unsigned toCallerWindow(unsigned Reg) {
156 assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7 && "Unexpected enum");
157 if (Reg >= SP::I0 && Reg <= SP::I7)
158 return Reg - SP::I0 + SP::O0;
159 return Reg;
160}
161
Dan Gohman98ca4f22009-08-05 01:29:28 +0000162SDValue
163SparcTargetLowering::LowerReturn(SDValue Chain,
Jakob Stoklund Olesen53d4bcf2013-04-06 23:57:33 +0000164 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000165 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000166 const SmallVectorImpl<SDValue> &OutVals,
Jakob Stoklund Olesen53d4bcf2013-04-06 23:57:33 +0000167 DebugLoc DL, SelectionDAG &DAG) const {
168 if (Subtarget->is64Bit())
169 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
170 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
171}
Dan Gohman98ca4f22009-08-05 01:29:28 +0000172
Jakob Stoklund Olesen53d4bcf2013-04-06 23:57:33 +0000173SDValue
174SparcTargetLowering::LowerReturn_32(SDValue Chain,
175 CallingConv::ID CallConv, bool IsVarArg,
176 const SmallVectorImpl<ISD::OutputArg> &Outs,
177 const SmallVectorImpl<SDValue> &OutVals,
178 DebugLoc DL, SelectionDAG &DAG) const {
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000179 MachineFunction &MF = DAG.getMachineFunction();
180
Chris Lattner5a65b922008-03-17 05:41:48 +0000181 // CCValAssign - represent the assignment of the return value to locations.
182 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000183
Chris Lattner5a65b922008-03-17 05:41:48 +0000184 // CCState - Info about the registers and stack slot.
Jakob Stoklund Olesen53d4bcf2013-04-06 23:57:33 +0000185 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000186 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000187
Jakob Stoklund Olesen53d4bcf2013-04-06 23:57:33 +0000188 // Analyze return values.
189 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000190
Dan Gohman475871a2008-07-27 21:46:04 +0000191 SDValue Flag;
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000192 SmallVector<SDValue, 4> RetOps(1, Chain);
193 // Make room for the return address offset.
194 RetOps.push_back(SDValue());
Chris Lattner5a65b922008-03-17 05:41:48 +0000195
196 // Copy the result values into the output registers.
197 for (unsigned i = 0; i != RVLocs.size(); ++i) {
198 CCValAssign &VA = RVLocs[i];
199 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +0000200
Jakob Stoklund Olesen53d4bcf2013-04-06 23:57:33 +0000201 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +0000202 OutVals[i], Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000203
Chris Lattner5a65b922008-03-17 05:41:48 +0000204 // Guarantee that all emitted copies are stuck together with flags.
205 Flag = Chain.getValue(1);
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000206 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner5a65b922008-03-17 05:41:48 +0000207 }
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000208
209 unsigned RetAddrOffset = 8; //Call Inst + Delay Slot
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000210 // If the function returns a struct, copy the SRetReturnReg to I0
211 if (MF.getFunction()->hasStructRetAttr()) {
212 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
213 unsigned Reg = SFI->getSRetReturnReg();
214 if (!Reg)
215 llvm_unreachable("sret virtual register not created in the entry block");
Jakob Stoklund Olesen53d4bcf2013-04-06 23:57:33 +0000216 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
217 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000218 Flag = Chain.getValue(1);
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000219 RetOps.push_back(DAG.getRegister(SP::I0, getPointerTy()));
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000220 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000221 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000222
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000223 RetOps[0] = Chain; // Update chain.
224 RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000225
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000226 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +0000227 if (Flag.getNode())
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000228 RetOps.push_back(Flag);
229
Jakob Stoklund Olesen53d4bcf2013-04-06 23:57:33 +0000230 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
231 &RetOps[0], RetOps.size());
232}
233
234// Lower return values for the 64-bit ABI.
235// Return values are passed the exactly the same way as function arguments.
236SDValue
237SparcTargetLowering::LowerReturn_64(SDValue Chain,
238 CallingConv::ID CallConv, bool IsVarArg,
239 const SmallVectorImpl<ISD::OutputArg> &Outs,
240 const SmallVectorImpl<SDValue> &OutVals,
241 DebugLoc DL, SelectionDAG &DAG) const {
242 // CCValAssign - represent the assignment of the return value to locations.
243 SmallVector<CCValAssign, 16> RVLocs;
244
245 // CCState - Info about the registers and stack slot.
246 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
247 DAG.getTarget(), RVLocs, *DAG.getContext());
248
249 // Analyze return values.
250 CCInfo.AnalyzeReturn(Outs, CC_Sparc64);
251
252 SDValue Flag;
253 SmallVector<SDValue, 4> RetOps(1, Chain);
254
255 // The second operand on the return instruction is the return address offset.
256 // The return address is always %i7+8 with the 64-bit ABI.
257 RetOps.push_back(DAG.getConstant(8, MVT::i32));
258
259 // Copy the result values into the output registers.
260 for (unsigned i = 0; i != RVLocs.size(); ++i) {
261 CCValAssign &VA = RVLocs[i];
262 assert(VA.isRegLoc() && "Can only return in registers!");
263 SDValue OutVal = OutVals[i];
264
265 // Integer return values must be sign or zero extended by the callee.
266 switch (VA.getLocInfo()) {
267 case CCValAssign::SExt:
268 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
269 break;
270 case CCValAssign::ZExt:
271 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
272 break;
273 case CCValAssign::AExt:
274 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
275 default:
276 break;
277 }
278
279 // The custom bit on an i32 return value indicates that it should be passed
280 // in the high bits of the register.
281 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
282 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
283 DAG.getConstant(32, MVT::i32));
284
285 // The next value may go in the low bits of the same register.
286 // Handle both at once.
287 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
288 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
289 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
290 // Skip the next value, it's already done.
291 ++i;
292 }
293 }
294
295 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
296
297 // Guarantee that all emitted copies are stuck together with flags.
298 Flag = Chain.getValue(1);
299 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
300 }
301
302 RetOps[0] = Chain; // Update chain.
303
304 // Add the flag if we have it.
305 if (Flag.getNode())
306 RetOps.push_back(Flag);
307
308 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000309 &RetOps[0], RetOps.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000310}
311
Jakob Stoklund Olesenf37812e2013-04-02 04:09:02 +0000312SDValue SparcTargetLowering::
313LowerFormalArguments(SDValue Chain,
314 CallingConv::ID CallConv,
315 bool IsVarArg,
316 const SmallVectorImpl<ISD::InputArg> &Ins,
317 DebugLoc DL,
318 SelectionDAG &DAG,
319 SmallVectorImpl<SDValue> &InVals) const {
320 if (Subtarget->is64Bit())
321 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
322 DL, DAG, InVals);
323 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
324 DL, DAG, InVals);
325}
326
327/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohman98ca4f22009-08-05 01:29:28 +0000328/// passed in either one or two GPRs, including FP values. TODO: we should
329/// pass FP values in FP registers for fastcc functions.
Jakob Stoklund Olesenf37812e2013-04-02 04:09:02 +0000330SDValue SparcTargetLowering::
331LowerFormalArguments_32(SDValue Chain,
332 CallingConv::ID CallConv,
333 bool isVarArg,
334 const SmallVectorImpl<ISD::InputArg> &Ins,
335 DebugLoc dl,
336 SelectionDAG &DAG,
337 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner5a65b922008-03-17 05:41:48 +0000338 MachineFunction &MF = DAG.getMachineFunction();
339 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +0000340 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +0000341
342 // Assign locations to all of the incoming arguments.
343 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000344 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000345 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000346 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000347
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000348 const unsigned StackOffset = 92;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000349
Eli Friedmana786c7b2009-07-19 19:53:46 +0000350 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Eli Friedmana786c7b2009-07-19 19:53:46 +0000351 CCValAssign &VA = ArgLocs[i];
Chris Lattner5a65b922008-03-17 05:41:48 +0000352
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000353 if (i == 0 && Ins[i].Flags.isSRet()) {
354 //Get SRet from [%fp+64]
355 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
356 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
357 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
358 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000359 false, false, false, 0);
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000360 InVals.push_back(Arg);
361 continue;
362 }
363
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000364 if (VA.isRegLoc()) {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000365 if (VA.needsCustom()) {
366 assert(VA.getLocVT() == MVT::f64);
367 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
368 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
369 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000370
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000371 assert(i+1 < e);
372 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikov53835702008-10-10 20:27:31 +0000373
Dan Gohman475871a2008-07-27 21:46:04 +0000374 SDValue LoVal;
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000375 if (NextVA.isMemLoc()) {
376 int FrameIdx = MF.getFrameInfo()->
377 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000379 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
380 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000381 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000382 } else {
383 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patel68e6bee2011-02-21 23:21:26 +0000384 &SP::IntRegsRegClass);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000385 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000386 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000387 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000389 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000390 InVals.push_back(WholeValue);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000391 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000392 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000393 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
394 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
395 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
396 if (VA.getLocVT() == MVT::f32)
397 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
398 else if (VA.getLocVT() != MVT::i32) {
399 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
400 DAG.getValueType(VA.getLocVT()));
401 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
402 }
403 InVals.push_back(Arg);
404 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000405 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000406
407 assert(VA.isMemLoc());
408
409 unsigned Offset = VA.getLocMemOffset()+StackOffset;
410
411 if (VA.needsCustom()) {
412 assert(VA.getValVT() == MVT::f64);
413 //If it is double-word aligned, just load.
414 if (Offset % 8 == 0) {
415 int FI = MF.getFrameInfo()->CreateFixedObject(8,
416 Offset,
417 true);
418 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
419 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
420 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000421 false,false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000422 InVals.push_back(Load);
423 continue;
424 }
425
426 int FI = MF.getFrameInfo()->CreateFixedObject(4,
427 Offset,
428 true);
429 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
430 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
431 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000432 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000433 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
434 Offset+4,
435 true);
436 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
437
438 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
439 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000440 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000441
442 SDValue WholeValue =
443 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
444 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
445 InVals.push_back(WholeValue);
446 continue;
447 }
448
449 int FI = MF.getFrameInfo()->CreateFixedObject(4,
450 Offset,
451 true);
452 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
453 SDValue Load ;
454 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
455 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
456 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000457 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000458 } else {
459 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
460 // Sparc is big endian, so add an offset based on the ObjectVT.
461 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
462 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
463 DAG.getConstant(Offset, MVT::i32));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000464 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000465 MachinePointerInfo(),
466 VA.getValVT(), false, false,0);
467 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
468 }
469 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000470 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000471
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000472 if (MF.getFunction()->hasStructRetAttr()) {
473 //Copy the SRet Argument to SRetReturnReg
474 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
475 unsigned Reg = SFI->getSRetReturnReg();
476 if (!Reg) {
477 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
478 SFI->setSRetReturnReg(Reg);
479 }
480 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
481 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
482 }
483
Chris Lattner5a65b922008-03-17 05:41:48 +0000484 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000485 if (isVarArg) {
Craig Topperc5eaae42012-03-11 07:57:25 +0000486 static const uint16_t ArgRegs[] = {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000487 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
488 };
489 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
Craig Topperc5eaae42012-03-11 07:57:25 +0000490 const uint16_t *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000491 unsigned ArgOffset = CCInfo.getNextStackOffset();
492 if (NumAllocated == 6)
493 ArgOffset += StackOffset;
494 else {
495 assert(!ArgOffset);
496 ArgOffset = 68+4*NumAllocated;
497 }
498
Chris Lattner5a65b922008-03-17 05:41:48 +0000499 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000500 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000501
Eli Friedmana786c7b2009-07-19 19:53:46 +0000502 std::vector<SDValue> OutChains;
503
Chris Lattner5a65b922008-03-17 05:41:48 +0000504 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
505 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
506 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000508
David Greene3f2bf852009-11-12 20:49:22 +0000509 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000510 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000511 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000512
Chris Lattner6229d0a2010-09-21 18:41:36 +0000513 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
514 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000515 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000516 ArgOffset += 4;
517 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000518
519 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000520 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000522 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000523 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000524 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000525
Dan Gohman98ca4f22009-08-05 01:29:28 +0000526 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000527}
528
Jakob Stoklund Olesenf37812e2013-04-02 04:09:02 +0000529// Lower formal arguments for the 64 bit ABI.
530SDValue SparcTargetLowering::
531LowerFormalArguments_64(SDValue Chain,
532 CallingConv::ID CallConv,
533 bool IsVarArg,
534 const SmallVectorImpl<ISD::InputArg> &Ins,
535 DebugLoc DL,
536 SelectionDAG &DAG,
537 SmallVectorImpl<SDValue> &InVals) const {
538 MachineFunction &MF = DAG.getMachineFunction();
539
540 // Analyze arguments according to CC_Sparc64.
541 SmallVector<CCValAssign, 16> ArgLocs;
542 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
543 getTargetMachine(), ArgLocs, *DAG.getContext());
544 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
545
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +0000546 // The argument array begins at %fp+BIAS+128, after the register save area.
547 const unsigned ArgArea = 128;
548
Jakob Stoklund Olesenf37812e2013-04-02 04:09:02 +0000549 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
550 CCValAssign &VA = ArgLocs[i];
551 if (VA.isRegLoc()) {
552 // This argument is passed in a register.
553 // All integer register arguments are promoted by the caller to i64.
554
555 // Create a virtual register for the promoted live-in value.
556 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
557 getRegClassFor(VA.getLocVT()));
558 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
559
Jakob Stoklund Olesen1f25fe52013-04-06 18:32:12 +0000560 // Get the high bits for i32 struct elements.
561 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
562 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
563 DAG.getConstant(32, MVT::i32));
564
Jakob Stoklund Olesenf37812e2013-04-02 04:09:02 +0000565 // The caller promoted the argument, so insert an Assert?ext SDNode so we
566 // won't promote the value again in this function.
567 switch (VA.getLocInfo()) {
568 case CCValAssign::SExt:
569 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
570 DAG.getValueType(VA.getValVT()));
571 break;
572 case CCValAssign::ZExt:
573 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
574 DAG.getValueType(VA.getValVT()));
575 break;
576 default:
577 break;
578 }
579
580 // Truncate the register down to the argument type.
581 if (VA.isExtInLoc())
582 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
583
584 InVals.push_back(Arg);
585 continue;
586 }
587
588 // The registers are exhausted. This argument was passed on the stack.
589 assert(VA.isMemLoc());
Jakob Stoklund Olesen1f25fe52013-04-06 18:32:12 +0000590 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
591 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +0000592 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1f25fe52013-04-06 18:32:12 +0000593 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
594 // Adjust offset for extended arguments, SPARC is big-endian.
595 // The caller will have written the full slot with extended bytes, but we
596 // prefer our own extending loads.
597 if (VA.isExtInLoc())
598 Offset += 8 - ValSize;
599 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
600 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
601 DAG.getFrameIndex(FI, getPointerTy()),
602 MachinePointerInfo::getFixedStack(FI),
603 false, false, false, 0));
Jakob Stoklund Olesenf37812e2013-04-02 04:09:02 +0000604 }
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +0000605
606 if (!IsVarArg)
607 return Chain;
608
609 // This function takes variable arguments, some of which may have been passed
610 // in registers %i0-%i5. Variable floating point arguments are never passed
611 // in floating point registers. They go on %i0-%i5 or on the stack like
612 // integer arguments.
613 //
614 // The va_start intrinsic needs to know the offset to the first variable
615 // argument.
616 unsigned ArgOffset = CCInfo.getNextStackOffset();
617 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
618 // Skip the 128 bytes of register save area.
619 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
620 Subtarget->getStackPointerBias());
621
622 // Save the variable arguments that were passed in registers.
623 // The caller is required to reserve stack space for 6 arguments regardless
624 // of how many arguments were actually passed.
625 SmallVector<SDValue, 8> OutChains;
626 for (; ArgOffset < 6*8; ArgOffset += 8) {
627 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
628 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
629 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
630 OutChains.push_back(DAG.getStore(Chain, DL, VArg,
631 DAG.getFrameIndex(FI, getPointerTy()),
632 MachinePointerInfo::getFixedStack(FI),
633 false, false, 0));
634 }
635
636 if (!OutChains.empty())
637 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
638 &OutChains[0], OutChains.size());
639
Jakob Stoklund Olesenf37812e2013-04-02 04:09:02 +0000640 return Chain;
641}
642
Dan Gohman98ca4f22009-08-05 01:29:28 +0000643SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000644SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +0000645 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen18fdb392013-04-07 19:10:57 +0000646 if (Subtarget->is64Bit())
647 return LowerCall_64(CLI, InVals);
648 return LowerCall_32(CLI, InVals);
649}
650
651// Lower a call for the 32-bit ABI.
652SDValue
653SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
654 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000655 SelectionDAG &DAG = CLI.DAG;
656 DebugLoc &dl = CLI.DL;
657 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
658 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
659 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
660 SDValue Chain = CLI.Chain;
661 SDValue Callee = CLI.Callee;
662 bool &isTailCall = CLI.IsTailCall;
663 CallingConv::ID CallConv = CLI.CallConv;
664 bool isVarArg = CLI.IsVarArg;
665
Evan Cheng0c439eb2010-01-27 00:07:07 +0000666 // Sparc target does not yet support tail call optimization.
667 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000668
Chris Lattner315123f2008-03-17 06:58:37 +0000669 // Analyze operands of the call, assigning locations to each operand.
670 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000671 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000672 DAG.getTarget(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000673 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000674
Chris Lattner315123f2008-03-17 06:58:37 +0000675 // Get the size of the outgoing arguments stack space requirement.
676 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000677
Chris Lattner5a65b922008-03-17 05:41:48 +0000678 // Keep stack frames 8-byte aligned.
679 ArgsSize = (ArgsSize+7) & ~7;
680
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000681 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
682
683 //Create local copies for byval args.
684 SmallVector<SDValue, 8> ByValArgs;
685 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
686 ISD::ArgFlagsTy Flags = Outs[i].Flags;
687 if (!Flags.isByVal())
688 continue;
689
690 SDValue Arg = OutVals[i];
691 unsigned Size = Flags.getByValSize();
692 unsigned Align = Flags.getByValAlign();
693
694 int FI = MFI->CreateStackObject(Size, Align, false);
695 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
696 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
697
698 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
699 false, //isVolatile,
700 (Size <= 32), //AlwaysInline if size <= 32
701 MachinePointerInfo(), MachinePointerInfo());
702 ByValArgs.push_back(FIPtr);
703 }
704
Chris Lattnere563bbc2008-10-11 22:08:30 +0000705 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000706
Dan Gohman475871a2008-07-27 21:46:04 +0000707 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
708 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000709
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000710 const unsigned StackOffset = 92;
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000711 bool hasStructRetAttr = false;
Chris Lattner315123f2008-03-17 06:58:37 +0000712 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000713 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000714 i != e;
715 ++i, ++realArgIdx) {
Chris Lattner315123f2008-03-17 06:58:37 +0000716 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000717 SDValue Arg = OutVals[realArgIdx];
Chris Lattner315123f2008-03-17 06:58:37 +0000718
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000719 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
720
721 //Use local copy if it is a byval arg.
722 if (Flags.isByVal())
723 Arg = ByValArgs[byvalArgIdx++];
724
Chris Lattner315123f2008-03-17 06:58:37 +0000725 // Promote the value if needed.
726 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000727 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000728 case CCValAssign::Full: break;
729 case CCValAssign::SExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000730 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000731 break;
732 case CCValAssign::ZExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000733 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000734 break;
735 case CCValAssign::AExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000736 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
737 break;
738 case CCValAssign::BCvt:
739 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000740 break;
741 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000742
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000743 if (Flags.isSRet()) {
744 assert(VA.needsCustom());
745 // store SRet argument in %sp+64
746 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
747 SDValue PtrOff = DAG.getIntPtrConstant(64);
748 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
749 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
750 MachinePointerInfo(),
751 false, false, 0));
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000752 hasStructRetAttr = true;
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000753 continue;
754 }
755
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000756 if (VA.needsCustom()) {
757 assert(VA.getLocVT() == MVT::f64);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000758
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000759 if (VA.isMemLoc()) {
760 unsigned Offset = VA.getLocMemOffset() + StackOffset;
761 //if it is double-word aligned, just store.
762 if (Offset % 8 == 0) {
763 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
764 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
765 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
766 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
767 MachinePointerInfo(),
768 false, false, 0));
769 continue;
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000770 }
771 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000772
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000774 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000775 Arg, StackPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000776 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000777 // Sparc is big-endian, so the high part comes first.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000778 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000779 MachinePointerInfo(), false, false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000780 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000781 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000782 DAG.getIntPtrConstant(4));
783 // Load the low part.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000784 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000785 MachinePointerInfo(), false, false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000786
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000787 if (VA.isRegLoc()) {
788 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
789 assert(i+1 != e);
790 CCValAssign &NextVA = ArgLocs[++i];
791 if (NextVA.isRegLoc()) {
792 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
793 } else {
794 //Store the low part in stack.
795 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
796 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
797 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
798 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
799 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
800 MachinePointerInfo(),
801 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000802 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000803 } else {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000804 unsigned Offset = VA.getLocMemOffset() + StackOffset;
805 // Store the high part.
806 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
807 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
808 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
809 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
810 MachinePointerInfo(),
811 false, false, 0));
812 // Store the low part.
813 PtrOff = DAG.getIntPtrConstant(Offset+4);
814 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
815 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
816 MachinePointerInfo(),
817 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000818 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000819 continue;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000820 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000821
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000822 // Arguments that can be passed on register must be kept at
823 // RegsToPass vector
824 if (VA.isRegLoc()) {
825 if (VA.getLocVT() != MVT::f32) {
826 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
827 continue;
828 }
829 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
830 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
831 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000832 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000833
834 assert(VA.isMemLoc());
835
836 // Create a store off the stack pointer for this argument.
837 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
838 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
839 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
840 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
841 MachinePointerInfo(),
842 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000843 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000844
Anton Korobeynikov53835702008-10-10 20:27:31 +0000845
Chris Lattner5a65b922008-03-17 05:41:48 +0000846 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000847 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000849 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000850
851 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000852 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000853 // The InFlag in necessary since all emitted instructions must be
Chris Lattner315123f2008-03-17 06:58:37 +0000854 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000855 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000856 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesen1b133a42013-04-09 05:11:52 +0000857 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000858 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000859 InFlag = Chain.getValue(1);
860 }
861
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000862 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
863
Chris Lattner5a65b922008-03-17 05:41:48 +0000864 // If the callee is a GlobalAddress node (quite common, every direct call is)
865 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000866 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000867 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000868 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000869 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000871
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000872 // Returns a chain & a flag for retval copy to use
873 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
874 SmallVector<SDValue, 8> Ops;
875 Ops.push_back(Chain);
876 Ops.push_back(Callee);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000877 if (hasStructRetAttr)
878 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
Jakob Stoklund Olesen1b133a42013-04-09 05:11:52 +0000879 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
880 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
881 RegsToPass[i].second.getValueType()));
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000882 if (InFlag.getNode())
883 Ops.push_back(InFlag);
884
885 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000886 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000887
Chris Lattnere563bbc2008-10-11 22:08:30 +0000888 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
889 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000890 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000891
Chris Lattner98949a62008-03-17 06:01:07 +0000892 // Assign locations to each value returned by this call.
893 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000894 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000895 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000896
Dan Gohman98ca4f22009-08-05 01:29:28 +0000897 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000898
Chris Lattner98949a62008-03-17 06:01:07 +0000899 // Copy all of the result registers out of their specified physreg.
900 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Jakob Stoklund Olesen1b133a42013-04-09 05:11:52 +0000901 Chain = DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
Chris Lattner98949a62008-03-17 06:01:07 +0000902 RVLocs[i].getValVT(), InFlag).getValue(1);
903 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000904 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000905 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000906
Dan Gohman98ca4f22009-08-05 01:29:28 +0000907 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000908}
909
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000910unsigned
911SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
912{
913 const Function *CalleeFn = 0;
914 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
915 CalleeFn = dyn_cast<Function>(G->getGlobal());
916 } else if (ExternalSymbolSDNode *E =
917 dyn_cast<ExternalSymbolSDNode>(Callee)) {
918 const Function *Fn = DAG.getMachineFunction().getFunction();
919 const Module *M = Fn->getParent();
920 CalleeFn = M->getFunction(E->getSymbol());
921 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000922
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000923 if (!CalleeFn)
924 return 0;
925
926 assert(CalleeFn->hasStructRetAttr() &&
927 "Callee does not have the StructRet attribute.");
928
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000929 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
930 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +0000931 return getDataLayout()->getTypeAllocSize(ElementTy);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000932}
Chris Lattner5a65b922008-03-17 05:41:48 +0000933
Jakob Stoklund Olesenddb14ce2013-04-21 21:36:49 +0000934
935// Fixup floating point arguments in the ... part of a varargs call.
936//
937// The SPARC v9 ABI requires that floating point arguments are treated the same
938// as integers when calling a varargs function. This does not apply to the
939// fixed arguments that are part of the function's prototype.
940//
941// This function post-processes a CCValAssign array created by
942// AnalyzeCallOperands().
943static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
944 ArrayRef<ISD::OutputArg> Outs) {
945 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
946 const CCValAssign &VA = ArgLocs[i];
947 // FIXME: What about f32 arguments? C promotes them to f64 when calling
948 // varargs functions.
949 if (!VA.isRegLoc() || VA.getLocVT() != MVT::f64)
950 continue;
951 // The fixed arguments to a varargs function still go in FP registers.
952 if (Outs[VA.getValNo()].IsFixed)
953 continue;
954
955 // This floating point argument should be reassigned.
956 CCValAssign NewVA;
957
958 // Determine the offset into the argument array.
959 unsigned Offset = 8 * (VA.getLocReg() - SP::D0);
960 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
961
962 if (Offset < 6*8) {
963 // This argument should go in %i0-%i5.
964 unsigned IReg = SP::I0 + Offset/8;
965 // Full register, just bitconvert into i64.
966 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
967 IReg, MVT::i64, CCValAssign::BCvt);
968 } else {
969 // This needs to go to memory, we're out of integer registers.
970 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
971 Offset, VA.getLocVT(), VA.getLocInfo());
972 }
973 ArgLocs[i] = NewVA;
974 }
975}
976
Jakob Stoklund Olesen18fdb392013-04-07 19:10:57 +0000977// Lower a call for the 64-bit ABI.
978SDValue
979SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
980 SmallVectorImpl<SDValue> &InVals) const {
981 SelectionDAG &DAG = CLI.DAG;
982 DebugLoc DL = CLI.DL;
983 SDValue Chain = CLI.Chain;
984
985 // Analyze operands of the call, assigning locations to each operand.
986 SmallVector<CCValAssign, 16> ArgLocs;
987 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
988 DAG.getTarget(), ArgLocs, *DAG.getContext());
989 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
990
991 // Get the size of the outgoing arguments stack space requirement.
992 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen6ed92842013-04-09 04:37:47 +0000993 // Called functions expect 6 argument words to exist in the stack frame, used
994 // or not.
995 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesen18fdb392013-04-07 19:10:57 +0000996
997 // Keep stack frames 16-byte aligned.
998 ArgsSize = RoundUpToAlignment(ArgsSize, 16);
999
Jakob Stoklund Olesenddb14ce2013-04-21 21:36:49 +00001000 // Varargs calls require special treatment.
1001 if (CLI.IsVarArg)
1002 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1003
Jakob Stoklund Olesen18fdb392013-04-07 19:10:57 +00001004 // Adjust the stack pointer to make room for the arguments.
1005 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1006 // with more than 6 arguments.
1007 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
1008
1009 // Collect the set of registers to pass to the function and their values.
1010 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1011 // instruction.
1012 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1013
1014 // Collect chains from all the memory opeations that copy arguments to the
1015 // stack. They must follow the stack pointer adjustment above and precede the
1016 // call instruction itself.
1017 SmallVector<SDValue, 8> MemOpChains;
1018
1019 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1020 const CCValAssign &VA = ArgLocs[i];
1021 SDValue Arg = CLI.OutVals[i];
1022
1023 // Promote the value if needed.
1024 switch (VA.getLocInfo()) {
1025 default:
1026 llvm_unreachable("Unknown location info!");
1027 case CCValAssign::Full:
1028 break;
1029 case CCValAssign::SExt:
1030 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1031 break;
1032 case CCValAssign::ZExt:
1033 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1034 break;
1035 case CCValAssign::AExt:
1036 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1037 break;
1038 case CCValAssign::BCvt:
1039 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1040 break;
1041 }
1042
1043 if (VA.isRegLoc()) {
1044 // The custom bit on an i32 return value indicates that it should be
1045 // passed in the high bits of the register.
1046 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1047 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
1048 DAG.getConstant(32, MVT::i32));
1049
1050 // The next value may go in the low bits of the same register.
1051 // Handle both at once.
1052 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1053 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1054 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1055 CLI.OutVals[i+1]);
1056 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1057 // Skip the next value, it's already done.
1058 ++i;
1059 }
1060 }
Jakob Stoklund Olesen1b133a42013-04-09 05:11:52 +00001061 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesen18fdb392013-04-07 19:10:57 +00001062 continue;
1063 }
1064
1065 assert(VA.isMemLoc());
1066
1067 // Create a store off the stack pointer for this argument.
1068 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1069 // The argument area starts at %fp+BIAS+128 in the callee frame,
1070 // %sp+BIAS+128 in ours.
1071 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1072 Subtarget->getStackPointerBias() +
1073 128);
1074 PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, PtrOff);
1075 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1076 MachinePointerInfo(),
1077 false, false, 0));
1078 }
1079
1080 // Emit all stores, make sure they occur before the call.
1081 if (!MemOpChains.empty())
1082 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1083 &MemOpChains[0], MemOpChains.size());
1084
1085 // Build a sequence of CopyToReg nodes glued together with token chain and
1086 // glue operands which copy the outgoing args into registers. The InGlue is
1087 // necessary since all emitted instructions must be stuck together in order
1088 // to pass the live physical registers.
1089 SDValue InGlue;
1090 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1091 Chain = DAG.getCopyToReg(Chain, DL,
1092 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1093 InGlue = Chain.getValue(1);
1094 }
1095
1096 // If the callee is a GlobalAddress node (quite common, every direct call is)
1097 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1098 // Likewise ExternalSymbol -> TargetExternalSymbol.
1099 SDValue Callee = CLI.Callee;
1100 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1101 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy());
1102 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1103 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
1104
1105 // Build the operands for the call instruction itself.
1106 SmallVector<SDValue, 8> Ops;
1107 Ops.push_back(Chain);
1108 Ops.push_back(Callee);
1109 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1110 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1111 RegsToPass[i].second.getValueType()));
1112
1113 // Make sure the CopyToReg nodes are glued to the call instruction which
1114 // consumes the registers.
1115 if (InGlue.getNode())
1116 Ops.push_back(InGlue);
1117
1118 // Now the call itself.
1119 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1120 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
1121 InGlue = Chain.getValue(1);
1122
1123 // Revert the stack pointer immediately after the call.
1124 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1125 DAG.getIntPtrConstant(0, true), InGlue);
1126 InGlue = Chain.getValue(1);
1127
1128 // Now extract the return values. This is more or less the same as
1129 // LowerFormalArguments_64.
1130
1131 // Assign locations to each value returned by this call.
1132 SmallVector<CCValAssign, 16> RVLocs;
1133 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1134 DAG.getTarget(), RVLocs, *DAG.getContext());
1135 RVInfo.AnalyzeCallResult(CLI.Ins, CC_Sparc64);
1136
1137 // Copy all of the result registers out of their specified physreg.
1138 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1139 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesen1b133a42013-04-09 05:11:52 +00001140 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesen18fdb392013-04-07 19:10:57 +00001141
1142 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1143 // reside in the same register in the high and low bits. Reuse the
1144 // CopyFromReg previous node to avoid duplicate copies.
1145 SDValue RV;
1146 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1147 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1148 RV = Chain.getValue(0);
1149
1150 // But usually we'll create a new CopyFromReg for a different register.
1151 if (!RV.getNode()) {
1152 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1153 Chain = RV.getValue(1);
1154 InGlue = Chain.getValue(2);
1155 }
1156
1157 // Get the high bits for i32 struct elements.
1158 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1159 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
1160 DAG.getConstant(32, MVT::i32));
1161
1162 // The callee promoted the return value, so insert an Assert?ext SDNode so
1163 // we won't promote the value again in this function.
1164 switch (VA.getLocInfo()) {
1165 case CCValAssign::SExt:
1166 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1167 DAG.getValueType(VA.getValVT()));
1168 break;
1169 case CCValAssign::ZExt:
1170 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1171 DAG.getValueType(VA.getValVT()));
1172 break;
1173 default:
1174 break;
1175 }
1176
1177 // Truncate the register down to the return value type.
1178 if (VA.isExtInLoc())
1179 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1180
1181 InVals.push_back(RV);
1182 }
1183
1184 return Chain;
1185}
1186
Chris Lattnerd23405e2008-03-17 03:21:36 +00001187//===----------------------------------------------------------------------===//
1188// TargetLowering Implementation
1189//===----------------------------------------------------------------------===//
1190
1191/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1192/// condition.
1193static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1194 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001195 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001196 case ISD::SETEQ: return SPCC::ICC_E;
1197 case ISD::SETNE: return SPCC::ICC_NE;
1198 case ISD::SETLT: return SPCC::ICC_L;
1199 case ISD::SETGT: return SPCC::ICC_G;
1200 case ISD::SETLE: return SPCC::ICC_LE;
1201 case ISD::SETGE: return SPCC::ICC_GE;
1202 case ISD::SETULT: return SPCC::ICC_CS;
1203 case ISD::SETULE: return SPCC::ICC_LEU;
1204 case ISD::SETUGT: return SPCC::ICC_GU;
1205 case ISD::SETUGE: return SPCC::ICC_CC;
1206 }
1207}
1208
1209/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1210/// FCC condition.
1211static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1212 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001213 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001214 case ISD::SETEQ:
1215 case ISD::SETOEQ: return SPCC::FCC_E;
1216 case ISD::SETNE:
1217 case ISD::SETUNE: return SPCC::FCC_NE;
1218 case ISD::SETLT:
1219 case ISD::SETOLT: return SPCC::FCC_L;
1220 case ISD::SETGT:
1221 case ISD::SETOGT: return SPCC::FCC_G;
1222 case ISD::SETLE:
1223 case ISD::SETOLE: return SPCC::FCC_LE;
1224 case ISD::SETGE:
1225 case ISD::SETOGE: return SPCC::FCC_GE;
1226 case ISD::SETULT: return SPCC::FCC_UL;
1227 case ISD::SETULE: return SPCC::FCC_ULE;
1228 case ISD::SETUGT: return SPCC::FCC_UG;
1229 case ISD::SETUGE: return SPCC::FCC_UGE;
1230 case ISD::SETUO: return SPCC::FCC_U;
1231 case ISD::SETO: return SPCC::FCC_O;
1232 case ISD::SETONE: return SPCC::FCC_LG;
1233 case ISD::SETUEQ: return SPCC::FCC_UE;
1234 }
1235}
1236
Chris Lattnerd23405e2008-03-17 03:21:36 +00001237SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +00001238 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Jakob Stoklund Olesenfcb25e62013-04-02 04:08:54 +00001239 Subtarget = &TM.getSubtarget<SparcSubtarget>();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001240
Chris Lattnerd23405e2008-03-17 03:21:36 +00001241 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +00001242 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1243 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1244 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
Jakob Stoklund Olesenfcb25e62013-04-02 04:08:54 +00001245 if (Subtarget->is64Bit())
1246 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001247
1248 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +00001249 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001250 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001252 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001254
1255 // Custom legalize GlobalAddress nodes into LO/HI parts.
Jakob Stoklund Olesen41d59c62013-04-13 19:02:23 +00001256 setOperationAction(ISD::GlobalAddress, getPointerTy(), Custom);
1257 setOperationAction(ISD::GlobalTLSAddress, getPointerTy(), Custom);
1258 setOperationAction(ISD::ConstantPool, getPointerTy(), Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001259
Chris Lattnerd23405e2008-03-17 03:21:36 +00001260 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1262 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1263 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001264
1265 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +00001266 setOperationAction(ISD::UREM, MVT::i32, Expand);
1267 setOperationAction(ISD::SREM, MVT::i32, Expand);
1268 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1269 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001270
1271 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1273 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001274
1275 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +00001276 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
1277 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001278
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001279 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1280 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001281
Chris Lattnerd23405e2008-03-17 03:21:36 +00001282 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +00001283 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1284 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1285 setOperationAction(ISD::SELECT, MVT::f64, Expand);
1286 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1287 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1288 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001289
Chris Lattnerd23405e2008-03-17 03:21:36 +00001290 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1292 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1293 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1294 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1295 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1296 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001297
Owen Anderson825b72b2009-08-11 20:47:22 +00001298 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1299 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1300 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001301
Jakob Stoklund Olesen8534e992013-04-03 04:41:44 +00001302 if (Subtarget->is64Bit()) {
1303 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen0e164882013-04-04 03:08:00 +00001304 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8534e992013-04-03 04:41:44 +00001305 }
1306
Eli Friedman14648462011-07-27 22:21:52 +00001307 // FIXME: There are instructions available for ATOMIC_FENCE
1308 // on SparcV8 and later.
Eli Friedman14648462011-07-27 22:21:52 +00001309 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001310
Owen Anderson825b72b2009-08-11 20:47:22 +00001311 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1312 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +00001313 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +00001315 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1317 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +00001318 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +00001320 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +00001321 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1322 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +00001323 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +00001324 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +00001325 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +00001326 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1327 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1328 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1329 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1330 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1331 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1332 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001333
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1335 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1336 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001337
1338 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +00001339 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1340 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001341
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001343
Chris Lattnerd23405e2008-03-17 03:21:36 +00001344 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +00001345 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001346 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +00001347 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001348
Chris Lattnerd23405e2008-03-17 03:21:36 +00001349 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +00001350 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1351 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1352 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1353 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1354 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001355
1356 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +00001357 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001358
Chris Lattnerd23405e2008-03-17 03:21:36 +00001359 setStackPointerRegisterToSaveRestore(SP::O6);
1360
1361 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +00001362 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001363
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001364 setMinFunctionAlignment(2);
1365
Chris Lattnerd23405e2008-03-17 03:21:36 +00001366 computeRegisterProperties();
1367}
1368
1369const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
1370 switch (Opcode) {
1371 default: return 0;
1372 case SPISD::CMPICC: return "SPISD::CMPICC";
1373 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1374 case SPISD::BRICC: return "SPISD::BRICC";
Jakob Stoklund Olesen8534e992013-04-03 04:41:44 +00001375 case SPISD::BRXCC: return "SPISD::BRXCC";
Chris Lattnerd23405e2008-03-17 03:21:36 +00001376 case SPISD::BRFCC: return "SPISD::BRFCC";
1377 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
Jakob Stoklund Olesen0e164882013-04-04 03:08:00 +00001378 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
Chris Lattnerd23405e2008-03-17 03:21:36 +00001379 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1380 case SPISD::Hi: return "SPISD::Hi";
1381 case SPISD::Lo: return "SPISD::Lo";
1382 case SPISD::FTOI: return "SPISD::FTOI";
1383 case SPISD::ITOF: return "SPISD::ITOF";
1384 case SPISD::CALL: return "SPISD::CALL";
1385 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001386 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001387 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Chris Lattnerd23405e2008-03-17 03:21:36 +00001388 }
1389}
1390
1391/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1392/// be zero. Op is expected to be a target specific node. Used by DAG
1393/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +00001394void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Anton Korobeynikov53835702008-10-10 20:27:31 +00001395 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +00001396 APInt &KnownOne,
1397 const SelectionDAG &DAG,
1398 unsigned Depth) const {
1399 APInt KnownZero2, KnownOne2;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001400 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001401
Chris Lattnerd23405e2008-03-17 03:21:36 +00001402 switch (Op.getOpcode()) {
1403 default: break;
1404 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen0e164882013-04-04 03:08:00 +00001405 case SPISD::SELECT_XCC:
Chris Lattnerd23405e2008-03-17 03:21:36 +00001406 case SPISD::SELECT_FCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001407 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1408 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001409 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1410 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1411
Chris Lattnerd23405e2008-03-17 03:21:36 +00001412 // Only known if known in both the LHS and RHS.
1413 KnownOne &= KnownOne2;
1414 KnownZero &= KnownZero2;
1415 break;
1416 }
1417}
1418
Chris Lattnerd23405e2008-03-17 03:21:36 +00001419// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1420// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +00001421static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +00001422 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001423 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmane368b462010-06-18 14:22:04 +00001424 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikov53835702008-10-10 20:27:31 +00001425 CC == ISD::SETNE &&
Jakob Stoklund Olesen0e164882013-04-04 03:08:00 +00001426 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1427 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattnerd23405e2008-03-17 03:21:36 +00001428 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1429 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1430 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
1431 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1432 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmane368b462010-06-18 14:22:04 +00001433 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
1434 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001435 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001436 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001437 LHS = CMPCC.getOperand(0);
1438 RHS = CMPCC.getOperand(1);
1439 }
1440}
1441
Jakob Stoklund Olesen0ec587e2013-04-14 01:33:32 +00001442// Convert to a target node and set target flags.
1443SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1444 SelectionDAG &DAG) const {
1445 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1446 return DAG.getTargetGlobalAddress(GA->getGlobal(),
1447 GA->getDebugLoc(),
1448 GA->getValueType(0),
1449 GA->getOffset(), TF);
Jakob Stoklund Olesen26932102013-04-14 04:35:16 +00001450
1451 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1452 return DAG.getTargetConstantPool(CP->getConstVal(),
1453 CP->getValueType(0),
1454 CP->getAlignment(),
1455 CP->getOffset(), TF);
1456
1457 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1458 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1459 ES->getValueType(0), TF);
1460
Jakob Stoklund Olesen0ec587e2013-04-14 01:33:32 +00001461 llvm_unreachable("Unhandled address SDNode");
1462}
1463
1464// Split Op into high and low parts according to HiTF and LoTF.
1465// Return an ADD node combining the parts.
1466SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1467 unsigned HiTF, unsigned LoTF,
1468 SelectionDAG &DAG) const {
1469 DebugLoc DL = Op.getDebugLoc();
1470 EVT VT = Op.getValueType();
1471 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1472 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1473 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1474}
1475
Jakob Stoklund Olesen26932102013-04-14 04:35:16 +00001476// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1477// or ExternalSymbol SDNode.
1478SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Jakob Stoklund Olesen87ce0172013-04-14 04:57:51 +00001479 DebugLoc DL = Op.getDebugLoc();
1480 EVT VT = getPointerTy();
1481
Jakob Stoklund Olesen26932102013-04-14 04:35:16 +00001482 // Handle PIC mode first.
1483 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1484 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
1485 SDValue HiLo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
Jakob Stoklund Olesen26932102013-04-14 04:35:16 +00001486 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1487 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
1488 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
1489 MachinePointerInfo::getGOT(), false, false, false, 0);
1490 }
1491
1492 // This is one of the absolute code models.
Jakob Stoklund Olesen87ce0172013-04-14 04:57:51 +00001493 switch(getTargetMachine().getCodeModel()) {
1494 default:
1495 llvm_unreachable("Unsupported absolute code model");
1496 case CodeModel::Small:
Jakob Stoklund Olesen618eda72013-04-14 05:10:36 +00001497 // abs32.
Jakob Stoklund Olesen87ce0172013-04-14 04:57:51 +00001498 return makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1499 case CodeModel::Medium: {
Jakob Stoklund Olesen618eda72013-04-14 05:10:36 +00001500 // abs44.
Jakob Stoklund Olesen87ce0172013-04-14 04:57:51 +00001501 SDValue H44 = makeHiLoPair(Op, SPII::MO_H44, SPII::MO_M44, DAG);
Jakob Stoklund Olesend9f88da2013-04-14 05:48:50 +00001502 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, MVT::i32));
Jakob Stoklund Olesen87ce0172013-04-14 04:57:51 +00001503 SDValue L44 = withTargetFlags(Op, SPII::MO_L44, DAG);
1504 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1505 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1506 }
Jakob Stoklund Olesen618eda72013-04-14 05:10:36 +00001507 case CodeModel::Large: {
1508 // abs64.
1509 SDValue Hi = makeHiLoPair(Op, SPII::MO_HH, SPII::MO_HM, DAG);
Jakob Stoklund Olesend9f88da2013-04-14 05:48:50 +00001510 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, MVT::i32));
Jakob Stoklund Olesen618eda72013-04-14 05:10:36 +00001511 SDValue Lo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1512 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1513 }
Jakob Stoklund Olesen87ce0172013-04-14 04:57:51 +00001514 }
Jakob Stoklund Olesen26932102013-04-14 04:35:16 +00001515}
1516
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001517SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001518 SelectionDAG &DAG) const {
Jakob Stoklund Olesen26932102013-04-14 04:35:16 +00001519 return makeAddress(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001520}
1521
Chris Lattnerdb486a62009-09-15 17:46:24 +00001522SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001523 SelectionDAG &DAG) const {
Jakob Stoklund Olesen26932102013-04-14 04:35:16 +00001524 return makeAddress(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001525}
1526
Dan Gohman475871a2008-07-27 21:46:04 +00001527static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001528 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001529 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 assert(Op.getValueType() == MVT::i32);
1531 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001532 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001533}
1534
Dan Gohman475871a2008-07-27 21:46:04 +00001535static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001536 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00001537 assert(Op.getOperand(0).getValueType() == MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001538 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +00001539 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001540 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001541}
1542
Dan Gohman475871a2008-07-27 21:46:04 +00001543static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1544 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001545 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001546 SDValue LHS = Op.getOperand(2);
1547 SDValue RHS = Op.getOperand(3);
1548 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +00001549 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001550 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001551
Chris Lattnerd23405e2008-03-17 03:21:36 +00001552 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1553 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1554 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001555
Chris Lattnerd23405e2008-03-17 03:21:36 +00001556 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +00001557 SDValue CompareFlag;
Jakob Stoklund Olesen8534e992013-04-03 04:41:44 +00001558 if (LHS.getValueType().isInteger()) {
1559 EVT VTs[] = { LHS.getValueType(), MVT::Glue };
Dan Gohman475871a2008-07-27 21:46:04 +00001560 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +00001561 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001562 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesen8534e992013-04-03 04:41:44 +00001563 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
1564 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001565 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001566 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001567 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
1568 Opc = SPISD::BRFCC;
1569 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
1571 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001572}
1573
Dan Gohman475871a2008-07-27 21:46:04 +00001574static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1575 SDValue LHS = Op.getOperand(0);
1576 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001577 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001578 SDValue TrueVal = Op.getOperand(2);
1579 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +00001580 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001581 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001582
Chris Lattnerd23405e2008-03-17 03:21:36 +00001583 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1584 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1585 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001586
Dan Gohman475871a2008-07-27 21:46:04 +00001587 SDValue CompareFlag;
Jakob Stoklund Olesen0e164882013-04-04 03:08:00 +00001588 if (LHS.getValueType().isInteger()) {
Benjamin Kramer3853f742013-03-07 20:33:29 +00001589 // subcc returns a value
1590 EVT VTs[] = { LHS.getValueType(), MVT::Glue };
Dan Gohman475871a2008-07-27 21:46:04 +00001591 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +00001592 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Jakob Stoklund Olesen0e164882013-04-04 03:08:00 +00001593 Opc = LHS.getValueType() == MVT::i32 ?
1594 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001595 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
1596 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001597 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001598 Opc = SPISD::SELECT_FCC;
1599 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
1600 }
Dale Johannesen3484c092009-02-05 22:07:54 +00001601 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001603}
1604
Dan Gohman475871a2008-07-27 21:46:04 +00001605static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001606 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001607 MachineFunction &MF = DAG.getMachineFunction();
1608 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
1609
Chris Lattnerd23405e2008-03-17 03:21:36 +00001610 // vastart just stores the address of the VarArgsFrameIndex slot into the
1611 // memory location argument.
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +00001612 DebugLoc DL = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001613 SDValue Offset =
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +00001614 DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(),
1615 DAG.getRegister(SP::I6, TLI.getPointerTy()),
1616 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset()));
Chris Lattnerd23405e2008-03-17 03:21:36 +00001617 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +00001618 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Chris Lattner6229d0a2010-09-21 18:41:36 +00001619 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001620}
1621
Dan Gohman475871a2008-07-27 21:46:04 +00001622static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001623 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001624 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001625 SDValue InChain = Node->getOperand(0);
1626 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +00001627 EVT PtrVT = VAListPtr.getValueType();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001628 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +00001629 DebugLoc DL = Node->getDebugLoc();
1630 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001631 MachinePointerInfo(SV), false, false, false, 0);
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +00001632 // Increment the pointer, VAList, to the next vaarg.
1633 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
1634 DAG.getIntPtrConstant(VT.getSizeInBits()/8));
1635 // Store the incremented VAList to the legalized pointer.
1636 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00001637 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Jakob Stoklund Olesenda8768b2013-04-20 22:49:16 +00001638 // Load the actual argument out of the pointer VAList.
1639 // We can't count on greater alignment than the word size.
1640 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
1641 false, false, false,
1642 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001643}
1644
Dan Gohman475871a2008-07-27 21:46:04 +00001645static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1646 SDValue Chain = Op.getOperand(0); // Legalize the chain.
1647 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +00001648 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001649
Chris Lattnerd23405e2008-03-17 03:21:36 +00001650 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
1652 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +00001653 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +00001654
Chris Lattnerd23405e2008-03-17 03:21:36 +00001655 // The resultant pointer is actually 16 words from the bottom of the stack,
1656 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +00001657 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
1658 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00001659 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +00001660 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001661}
1662
Chris Lattnerd23405e2008-03-17 03:21:36 +00001663
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001664static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001665 DebugLoc dl = Op.getDebugLoc();
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001666 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001667 dl, MVT::Other, DAG.getEntryNode());
1668 return Chain;
1669}
1670
1671static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1672 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1673 MFI->setFrameAddressIsTaken(true);
1674
1675 EVT VT = Op.getValueType();
1676 DebugLoc dl = Op.getDebugLoc();
1677 unsigned FrameReg = SP::I6;
1678
1679 uint64_t depth = Op.getConstantOperandVal(0);
1680
1681 SDValue FrameAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001682 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001683 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1684 else {
1685 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001686 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001687 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001688
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001689 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001690 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001691 dl, MVT::i32,
1692 FrameAddr, DAG.getIntPtrConstant(56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001693 FrameAddr = DAG.getLoad(MVT::i32, dl,
1694 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001695 Ptr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001696 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001697 }
1698 }
1699 return FrameAddr;
1700}
1701
1702static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
1703 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1704 MFI->setReturnAddressIsTaken(true);
1705
1706 EVT VT = Op.getValueType();
1707 DebugLoc dl = Op.getDebugLoc();
1708 unsigned RetReg = SP::I7;
1709
1710 uint64_t depth = Op.getConstantOperandVal(0);
1711
1712 SDValue RetAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001713 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001714 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
1715 else {
1716 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001717 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001718 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001719
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001720 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001721 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001722 dl, MVT::i32,
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001723 RetAddr,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001724 DAG.getIntPtrConstant((i == depth-1)?60:56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001725 RetAddr = DAG.getLoad(MVT::i32, dl,
1726 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001727 Ptr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001728 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001729 }
1730 }
1731 return RetAddr;
1732}
1733
Dan Gohman475871a2008-07-27 21:46:04 +00001734SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001735LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001736 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001737 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001738 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1739 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001740 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +00001741 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +00001742 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1743 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001744 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1745 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
1746 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
1747 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1748 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
1749 case ISD::VAARG: return LowerVAARG(Op, DAG);
1750 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001751 }
1752}
1753
1754MachineBasicBlock *
1755SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001756 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001757 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1758 unsigned BROpcode;
1759 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +00001760 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001761 // Figure out the conditional branch opcode to use for this select_cc.
1762 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001763 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001764 case SP::SELECT_CC_Int_ICC:
1765 case SP::SELECT_CC_FP_ICC:
1766 case SP::SELECT_CC_DFP_ICC:
1767 BROpcode = SP::BCOND;
1768 break;
1769 case SP::SELECT_CC_Int_FCC:
1770 case SP::SELECT_CC_FP_FCC:
1771 case SP::SELECT_CC_DFP_FCC:
1772 BROpcode = SP::FBCOND;
1773 break;
1774 }
1775
1776 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001777
Chris Lattnerd23405e2008-03-17 03:21:36 +00001778 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1779 // control-flow pattern. The incoming instruction knows the destination vreg
1780 // to set, the condition code register to branch on, the true/false values to
1781 // select between, and a branch opcode to use.
1782 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001783 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001784 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001785
Chris Lattnerd23405e2008-03-17 03:21:36 +00001786 // thisMBB:
1787 // ...
1788 // TrueVal = ...
1789 // [f]bCC copy1MBB
1790 // fallthrough --> copy0MBB
1791 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001792 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001793 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1794 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindarajuf6612772010-12-28 20:39:17 +00001795 F->insert(It, copy0MBB);
1796 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00001797
1798 // Transfer the remainder of BB and its successor edges to sinkMBB.
1799 sinkMBB->splice(sinkMBB->begin(), BB,
1800 llvm::next(MachineBasicBlock::iterator(MI)),
1801 BB->end());
1802 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1803
1804 // Add the true and fallthrough blocks as its successors.
1805 BB->addSuccessor(copy0MBB);
1806 BB->addSuccessor(sinkMBB);
1807
Dale Johannesend552eee2009-02-13 02:31:35 +00001808 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001809
Chris Lattnerd23405e2008-03-17 03:21:36 +00001810 // copy0MBB:
1811 // %FalseValue = ...
1812 // # fallthrough to sinkMBB
1813 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001814
Chris Lattnerd23405e2008-03-17 03:21:36 +00001815 // Update machine-CFG edges
1816 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001817
Chris Lattnerd23405e2008-03-17 03:21:36 +00001818 // sinkMBB:
1819 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1820 // ...
1821 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001822 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001823 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1824 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001825
Dan Gohman14152b42010-07-06 20:24:04 +00001826 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001827 return BB;
1828}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001829
1830//===----------------------------------------------------------------------===//
1831// Sparc Inline Assembly Support
1832//===----------------------------------------------------------------------===//
1833
1834/// getConstraintType - Given a constraint letter, return the type of
1835/// constraint it is for this target.
1836SparcTargetLowering::ConstraintType
1837SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1838 if (Constraint.size() == 1) {
1839 switch (Constraint[0]) {
1840 default: break;
1841 case 'r': return C_RegisterClass;
1842 }
1843 }
1844
1845 return TargetLowering::getConstraintType(Constraint);
1846}
1847
1848std::pair<unsigned, const TargetRegisterClass*>
1849SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001850 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001851 if (Constraint.size() == 1) {
1852 switch (Constraint[0]) {
1853 case 'r':
Craig Topperc9099502012-04-20 06:31:50 +00001854 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001855 }
1856 }
1857
1858 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1859}
1860
Dan Gohman6520e202008-10-18 02:06:02 +00001861bool
1862SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1863 // The Sparc target isn't yet aware of offsets.
1864 return false;
1865}