blob: 707fe7b4a605b9de36bd9038d76038f7819d2f04 [file] [log] [blame]
Chris Lattner0a1762e2008-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 Gohman31ae5862010-04-17 14:41:14 +000016#include "SparcMachineFunctionInfo.h"
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +000017#include "SparcRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "SparcTargetMachine.h"
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +000019#include "MCTargetDesc/SparcBaseInfo.h"
Chris Lattner49b269d2008-03-17 05:41:48 +000020#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000026#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/Module.h"
Torok Edwin56d06592009-07-11 20:10:48 +000030#include "llvm/Support/ErrorHandling.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000031using namespace llvm;
32
Chris Lattner49b269d2008-03-17 05:41:48 +000033
34//===----------------------------------------------------------------------===//
35// Calling Convention Implementation
36//===----------------------------------------------------------------------===//
37
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000038static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
39 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
40 ISD::ArgFlagsTy &ArgFlags, CCState &State)
41{
42 assert (ArgFlags.isSRet());
43
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000044 // Assign SRet argument.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000045 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
46 0,
47 LocVT, LocInfo));
48 return true;
49}
50
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000051static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
52 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
53 ISD::ArgFlagsTy &ArgFlags, CCState &State)
54{
Craig Topperbef78fc2012-03-11 07:57:25 +000055 static const uint16_t RegList[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000056 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
57 };
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000058 // Try to get first reg.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000059 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
60 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
61 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000062 // Assign whole thing in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000063 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
64 State.AllocateStack(8,4),
65 LocVT, LocInfo));
66 return true;
67 }
68
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000069 // Try to get second reg.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000070 if (unsigned Reg = State.AllocateReg(RegList, 6))
71 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
72 else
73 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
74 State.AllocateStack(4,4),
75 LocVT, LocInfo));
76 return true;
77}
78
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +000079// Allocate a full-sized argument for the 64-bit ABI.
80static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
81 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
82 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
83 assert((LocVT == MVT::f32 || LocVT.getSizeInBits() == 64) &&
84 "Can't handle non-64 bits locations");
85
86 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
87 unsigned Offset = State.AllocateStack(8, 8);
88 unsigned Reg = 0;
89
90 if (LocVT == MVT::i64 && Offset < 6*8)
91 // Promote integers to %i0-%i5.
92 Reg = SP::I0 + Offset/8;
93 else if (LocVT == MVT::f64 && Offset < 16*8)
94 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
95 Reg = SP::D0 + Offset/8;
96 else if (LocVT == MVT::f32 && Offset < 16*8)
97 // Promote floats to %f1, %f3, ...
98 Reg = SP::F1 + Offset/4;
99
100 // Promote to register when possible, otherwise use the stack slot.
101 if (Reg) {
102 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
103 return true;
104 }
105
106 // This argument goes on the stack in an 8-byte slot.
107 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
108 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
109 if (LocVT == MVT::f32)
110 Offset += 4;
111
112 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
113 return true;
114}
115
116// Allocate a half-sized argument for the 64-bit ABI.
117//
118// This is used when passing { float, int } structs by value in registers.
119static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
120 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
121 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
122 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
123 unsigned Offset = State.AllocateStack(4, 4);
124
125 if (LocVT == MVT::f32 && Offset < 16*8) {
126 // Promote floats to %f0-%f31.
127 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
128 LocVT, LocInfo));
129 return true;
130 }
131
132 if (LocVT == MVT::i32 && Offset < 6*8) {
133 // Promote integers to %i0-%i5, using half the register.
134 unsigned Reg = SP::I0 + Offset/8;
135 LocVT = MVT::i64;
136 LocInfo = CCValAssign::AExt;
137
138 // Set the Custom bit if this i32 goes in the high bits of a register.
139 if (Offset % 8 == 0)
140 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
141 LocVT, LocInfo));
142 else
143 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
144 return true;
145 }
146
147 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
148 return true;
149}
150
Chris Lattner49b269d2008-03-17 05:41:48 +0000151#include "SparcGenCallingConv.inc"
152
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000153// The calling conventions in SparcCallingConv.td are described in terms of the
154// callee's register window. This function translates registers to the
155// corresponding caller window %o register.
156static unsigned toCallerWindow(unsigned Reg) {
157 assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7 && "Unexpected enum");
158 if (Reg >= SP::I0 && Reg <= SP::I7)
159 return Reg - SP::I0 + SP::O0;
160 return Reg;
161}
162
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000163SDValue
164SparcTargetLowering::LowerReturn(SDValue Chain,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000165 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000166 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000167 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000168 SDLoc DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000169 if (Subtarget->is64Bit())
170 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
171 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
172}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000173
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000174SDValue
175SparcTargetLowering::LowerReturn_32(SDValue Chain,
176 CallingConv::ID CallConv, bool IsVarArg,
177 const SmallVectorImpl<ISD::OutputArg> &Outs,
178 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000179 SDLoc DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000180 MachineFunction &MF = DAG.getMachineFunction();
181
Chris Lattner49b269d2008-03-17 05:41:48 +0000182 // CCValAssign - represent the assignment of the return value to locations.
183 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000184
Chris Lattner49b269d2008-03-17 05:41:48 +0000185 // CCState - Info about the registers and stack slot.
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000186 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000187 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000188
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000189 // Analyze return values.
190 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000191
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000192 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000193 SmallVector<SDValue, 4> RetOps(1, Chain);
194 // Make room for the return address offset.
195 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000196
197 // Copy the result values into the output registers.
198 for (unsigned i = 0; i != RVLocs.size(); ++i) {
199 CCValAssign &VA = RVLocs[i];
200 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000201
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000202 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000203 OutVals[i], Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000204
Chris Lattner49b269d2008-03-17 05:41:48 +0000205 // Guarantee that all emitted copies are stuck together with flags.
206 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000207 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000208 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000209
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000210 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000211 // If the function returns a struct, copy the SRetReturnReg to I0
212 if (MF.getFunction()->hasStructRetAttr()) {
213 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
214 unsigned Reg = SFI->getSRetReturnReg();
215 if (!Reg)
216 llvm_unreachable("sret virtual register not created in the entry block");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000217 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
218 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000219 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000220 RetOps.push_back(DAG.getRegister(SP::I0, getPointerTy()));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000221 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000222 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000223
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000224 RetOps[0] = Chain; // Update chain.
225 RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000226
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000227 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000228 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000229 RetOps.push_back(Flag);
230
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000231 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
232 &RetOps[0], RetOps.size());
233}
234
235// Lower return values for the 64-bit ABI.
236// Return values are passed the exactly the same way as function arguments.
237SDValue
238SparcTargetLowering::LowerReturn_64(SDValue Chain,
239 CallingConv::ID CallConv, bool IsVarArg,
240 const SmallVectorImpl<ISD::OutputArg> &Outs,
241 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000242 SDLoc DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000243 // CCValAssign - represent the assignment of the return value to locations.
244 SmallVector<CCValAssign, 16> RVLocs;
245
246 // CCState - Info about the registers and stack slot.
247 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
248 DAG.getTarget(), RVLocs, *DAG.getContext());
249
250 // Analyze return values.
251 CCInfo.AnalyzeReturn(Outs, CC_Sparc64);
252
253 SDValue Flag;
254 SmallVector<SDValue, 4> RetOps(1, Chain);
255
256 // The second operand on the return instruction is the return address offset.
257 // The return address is always %i7+8 with the 64-bit ABI.
258 RetOps.push_back(DAG.getConstant(8, MVT::i32));
259
260 // Copy the result values into the output registers.
261 for (unsigned i = 0; i != RVLocs.size(); ++i) {
262 CCValAssign &VA = RVLocs[i];
263 assert(VA.isRegLoc() && "Can only return in registers!");
264 SDValue OutVal = OutVals[i];
265
266 // Integer return values must be sign or zero extended by the callee.
267 switch (VA.getLocInfo()) {
268 case CCValAssign::SExt:
269 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
270 break;
271 case CCValAssign::ZExt:
272 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
273 break;
274 case CCValAssign::AExt:
275 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
276 default:
277 break;
278 }
279
280 // The custom bit on an i32 return value indicates that it should be passed
281 // in the high bits of the register.
282 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
283 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
284 DAG.getConstant(32, MVT::i32));
285
286 // The next value may go in the low bits of the same register.
287 // Handle both at once.
288 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
289 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
290 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
291 // Skip the next value, it's already done.
292 ++i;
293 }
294 }
295
296 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
297
298 // Guarantee that all emitted copies are stuck together with flags.
299 Flag = Chain.getValue(1);
300 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
301 }
302
303 RetOps[0] = Chain; // Update chain.
304
305 // Add the flag if we have it.
306 if (Flag.getNode())
307 RetOps.push_back(Flag);
308
309 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000310 &RetOps[0], RetOps.size());
Chris Lattner49b269d2008-03-17 05:41:48 +0000311}
312
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000313SDValue SparcTargetLowering::
314LowerFormalArguments(SDValue Chain,
315 CallingConv::ID CallConv,
316 bool IsVarArg,
317 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000318 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000319 SelectionDAG &DAG,
320 SmallVectorImpl<SDValue> &InVals) const {
321 if (Subtarget->is64Bit())
322 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
323 DL, DAG, InVals);
324 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
325 DL, DAG, InVals);
326}
327
328/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000329/// passed in either one or two GPRs, including FP values. TODO: we should
330/// pass FP values in FP registers for fastcc functions.
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000331SDValue SparcTargetLowering::
332LowerFormalArguments_32(SDValue Chain,
333 CallingConv::ID CallConv,
334 bool isVarArg,
335 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000336 SDLoc dl,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000337 SelectionDAG &DAG,
338 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000339 MachineFunction &MF = DAG.getMachineFunction();
340 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000341 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000342
343 // Assign locations to all of the incoming arguments.
344 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000345 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000346 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000347 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000348
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000349 const unsigned StackOffset = 92;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000350
Eli Friedmanbe853b72009-07-19 19:53:46 +0000351 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000352 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000353
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000354 if (i == 0 && Ins[i].Flags.isSRet()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000355 // Get SRet from [%fp+64].
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000356 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
357 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
358 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
359 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000360 false, false, false, 0);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000361 InVals.push_back(Arg);
362 continue;
363 }
364
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000365 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000366 if (VA.needsCustom()) {
367 assert(VA.getLocVT() == MVT::f64);
368 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
369 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
370 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000371
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000372 assert(i+1 < e);
373 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000374
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000375 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000376 if (NextVA.isMemLoc()) {
377 int FrameIdx = MF.getFrameInfo()->
378 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000379 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000380 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
381 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000382 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000383 } else {
384 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000385 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000386 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000387 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000388 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000389 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000390 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000391 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000392 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000393 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000394 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
395 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
396 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
397 if (VA.getLocVT() == MVT::f32)
398 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
399 else if (VA.getLocVT() != MVT::i32) {
400 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
401 DAG.getValueType(VA.getLocVT()));
402 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
403 }
404 InVals.push_back(Arg);
405 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000406 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000407
408 assert(VA.isMemLoc());
409
410 unsigned Offset = VA.getLocMemOffset()+StackOffset;
411
412 if (VA.needsCustom()) {
413 assert(VA.getValVT() == MVT::f64);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000414 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000415 if (Offset % 8 == 0) {
416 int FI = MF.getFrameInfo()->CreateFixedObject(8,
417 Offset,
418 true);
419 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
420 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
421 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000422 false,false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000423 InVals.push_back(Load);
424 continue;
425 }
426
427 int FI = MF.getFrameInfo()->CreateFixedObject(4,
428 Offset,
429 true);
430 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
431 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
432 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000433 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000434 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
435 Offset+4,
436 true);
437 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
438
439 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
440 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000441 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000442
443 SDValue WholeValue =
444 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
445 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
446 InVals.push_back(WholeValue);
447 continue;
448 }
449
450 int FI = MF.getFrameInfo()->CreateFixedObject(4,
451 Offset,
452 true);
453 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
454 SDValue Load ;
455 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
456 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
457 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000458 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000459 } else {
460 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
461 // Sparc is big endian, so add an offset based on the ObjectVT.
462 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
463 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
464 DAG.getConstant(Offset, MVT::i32));
Stuart Hastings81c43062011-02-16 16:23:55 +0000465 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000466 MachinePointerInfo(),
467 VA.getValVT(), false, false,0);
468 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
469 }
470 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000471 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000472
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000473 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000474 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000475 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
476 unsigned Reg = SFI->getSRetReturnReg();
477 if (!Reg) {
478 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
479 SFI->setSRetReturnReg(Reg);
480 }
481 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
482 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
483 }
484
Chris Lattner49b269d2008-03-17 05:41:48 +0000485 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000486 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +0000487 static const uint16_t ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000488 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
489 };
490 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
Craig Topperbef78fc2012-03-11 07:57:25 +0000491 const uint16_t *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000492 unsigned ArgOffset = CCInfo.getNextStackOffset();
493 if (NumAllocated == 6)
494 ArgOffset += StackOffset;
495 else {
496 assert(!ArgOffset);
497 ArgOffset = 68+4*NumAllocated;
498 }
499
Chris Lattner49b269d2008-03-17 05:41:48 +0000500 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000501 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000502
Eli Friedmanbe853b72009-07-19 19:53:46 +0000503 std::vector<SDValue> OutChains;
504
Chris Lattner49b269d2008-03-17 05:41:48 +0000505 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
506 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
507 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000508 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000509
David Greene1fbe0542009-11-12 20:49:22 +0000510 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Cheng0664a672010-07-03 00:40:23 +0000511 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000512 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000513
Chris Lattner676c61d2010-09-21 18:41:36 +0000514 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
515 MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000516 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000517 ArgOffset += 4;
518 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000519
520 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000521 OutChains.push_back(Chain);
Owen Anderson9f944592009-08-11 20:47:22 +0000522 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000523 &OutChains[0], OutChains.size());
Eli Friedmanbe853b72009-07-19 19:53:46 +0000524 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000525 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000526
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000527 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000528}
529
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000530// Lower formal arguments for the 64 bit ABI.
531SDValue SparcTargetLowering::
532LowerFormalArguments_64(SDValue Chain,
533 CallingConv::ID CallConv,
534 bool IsVarArg,
535 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000536 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000537 SelectionDAG &DAG,
538 SmallVectorImpl<SDValue> &InVals) const {
539 MachineFunction &MF = DAG.getMachineFunction();
540
541 // Analyze arguments according to CC_Sparc64.
542 SmallVector<CCValAssign, 16> ArgLocs;
543 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
544 getTargetMachine(), ArgLocs, *DAG.getContext());
545 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
546
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000547 // The argument array begins at %fp+BIAS+128, after the register save area.
548 const unsigned ArgArea = 128;
549
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000550 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
551 CCValAssign &VA = ArgLocs[i];
552 if (VA.isRegLoc()) {
553 // This argument is passed in a register.
554 // All integer register arguments are promoted by the caller to i64.
555
556 // Create a virtual register for the promoted live-in value.
557 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
558 getRegClassFor(VA.getLocVT()));
559 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
560
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000561 // Get the high bits for i32 struct elements.
562 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
563 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
564 DAG.getConstant(32, MVT::i32));
565
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000566 // The caller promoted the argument, so insert an Assert?ext SDNode so we
567 // won't promote the value again in this function.
568 switch (VA.getLocInfo()) {
569 case CCValAssign::SExt:
570 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
571 DAG.getValueType(VA.getValVT()));
572 break;
573 case CCValAssign::ZExt:
574 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
575 DAG.getValueType(VA.getValVT()));
576 break;
577 default:
578 break;
579 }
580
581 // Truncate the register down to the argument type.
582 if (VA.isExtInLoc())
583 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
584
585 InVals.push_back(Arg);
586 continue;
587 }
588
589 // The registers are exhausted. This argument was passed on the stack.
590 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000591 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
592 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000593 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000594 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
595 // Adjust offset for extended arguments, SPARC is big-endian.
596 // The caller will have written the full slot with extended bytes, but we
597 // prefer our own extending loads.
598 if (VA.isExtInLoc())
599 Offset += 8 - ValSize;
600 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
601 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
602 DAG.getFrameIndex(FI, getPointerTy()),
603 MachinePointerInfo::getFixedStack(FI),
604 false, false, false, 0));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000605 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000606
607 if (!IsVarArg)
608 return Chain;
609
610 // This function takes variable arguments, some of which may have been passed
611 // in registers %i0-%i5. Variable floating point arguments are never passed
612 // in floating point registers. They go on %i0-%i5 or on the stack like
613 // integer arguments.
614 //
615 // The va_start intrinsic needs to know the offset to the first variable
616 // argument.
617 unsigned ArgOffset = CCInfo.getNextStackOffset();
618 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
619 // Skip the 128 bytes of register save area.
620 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
621 Subtarget->getStackPointerBias());
622
623 // Save the variable arguments that were passed in registers.
624 // The caller is required to reserve stack space for 6 arguments regardless
625 // of how many arguments were actually passed.
626 SmallVector<SDValue, 8> OutChains;
627 for (; ArgOffset < 6*8; ArgOffset += 8) {
628 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
629 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
630 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
631 OutChains.push_back(DAG.getStore(Chain, DL, VArg,
632 DAG.getFrameIndex(FI, getPointerTy()),
633 MachinePointerInfo::getFixedStack(FI),
634 false, false, 0));
635 }
636
637 if (!OutChains.empty())
638 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
639 &OutChains[0], OutChains.size());
640
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000641 return Chain;
642}
643
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000644SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000645SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000646 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000647 if (Subtarget->is64Bit())
648 return LowerCall_64(CLI, InVals);
649 return LowerCall_32(CLI, InVals);
650}
651
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000652static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
653 ImmutableCallSite *CS) {
654 if (CS)
655 return CS->hasFnAttr(Attribute::ReturnsTwice);
656
657 const Function *CalleeFn = 0;
658 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
659 CalleeFn = dyn_cast<Function>(G->getGlobal());
660 } else if (ExternalSymbolSDNode *E =
661 dyn_cast<ExternalSymbolSDNode>(Callee)) {
662 const Function *Fn = DAG.getMachineFunction().getFunction();
663 const Module *M = Fn->getParent();
664 const char *CalleeName = E->getSymbol();
665 CalleeFn = M->getFunction(CalleeName);
666 }
667
668 if (!CalleeFn)
669 return false;
670 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
671}
672
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000673// Lower a call for the 32-bit ABI.
674SDValue
675SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
676 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000677 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000678 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000679 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
680 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
681 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000682 SDValue Chain = CLI.Chain;
683 SDValue Callee = CLI.Callee;
684 bool &isTailCall = CLI.IsTailCall;
685 CallingConv::ID CallConv = CLI.CallConv;
686 bool isVarArg = CLI.IsVarArg;
687
Evan Cheng67a69dd2010-01-27 00:07:07 +0000688 // Sparc target does not yet support tail call optimization.
689 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000690
Chris Lattner7d4152b2008-03-17 06:58:37 +0000691 // Analyze operands of the call, assigning locations to each operand.
692 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000693 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000694 DAG.getTarget(), ArgLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000695 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000696
Chris Lattner7d4152b2008-03-17 06:58:37 +0000697 // Get the size of the outgoing arguments stack space requirement.
698 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000699
Chris Lattner49b269d2008-03-17 05:41:48 +0000700 // Keep stack frames 8-byte aligned.
701 ArgsSize = (ArgsSize+7) & ~7;
702
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000703 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
704
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000705 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000706 SmallVector<SDValue, 8> ByValArgs;
707 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
708 ISD::ArgFlagsTy Flags = Outs[i].Flags;
709 if (!Flags.isByVal())
710 continue;
711
712 SDValue Arg = OutVals[i];
713 unsigned Size = Flags.getByValSize();
714 unsigned Align = Flags.getByValAlign();
715
716 int FI = MFI->CreateStackObject(Size, Align, false);
717 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
718 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
719
720 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000721 false, // isVolatile,
722 (Size <= 32), // AlwaysInline if size <= 32
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000723 MachinePointerInfo(), MachinePointerInfo());
724 ByValArgs.push_back(FIPtr);
725 }
726
Andrew Trickad6d08a2013-05-29 22:03:55 +0000727 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
728 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000729
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000730 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
731 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000732
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000733 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000734 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000735 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000736 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000737 i != e;
738 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000739 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000740 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000741
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000742 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
743
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000744 // Use local copy if it is a byval arg.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000745 if (Flags.isByVal())
746 Arg = ByValArgs[byvalArgIdx++];
747
Chris Lattner7d4152b2008-03-17 06:58:37 +0000748 // Promote the value if needed.
749 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000750 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000751 case CCValAssign::Full: break;
752 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000753 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000754 break;
755 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000756 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000757 break;
758 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000759 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
760 break;
761 case CCValAssign::BCvt:
762 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000763 break;
764 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000765
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000766 if (Flags.isSRet()) {
767 assert(VA.needsCustom());
768 // store SRet argument in %sp+64
769 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
770 SDValue PtrOff = DAG.getIntPtrConstant(64);
771 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
772 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
773 MachinePointerInfo(),
774 false, false, 0));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000775 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000776 continue;
777 }
778
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000779 if (VA.needsCustom()) {
780 assert(VA.getLocVT() == MVT::f64);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000781
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000782 if (VA.isMemLoc()) {
783 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000784 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000785 if (Offset % 8 == 0) {
786 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
787 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
788 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
789 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
790 MachinePointerInfo(),
791 false, false, 0));
792 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000793 }
794 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000795
Owen Anderson9f944592009-08-11 20:47:22 +0000796 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +0000797 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000798 Arg, StackPtr, MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000799 false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000800 // Sparc is big-endian, so the high part comes first.
Chris Lattner7727d052010-09-21 06:44:06 +0000801 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000802 MachinePointerInfo(), false, false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000803 // Increment the pointer to the other half.
Dale Johannesen021052a2009-02-04 20:06:27 +0000804 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000805 DAG.getIntPtrConstant(4));
806 // Load the low part.
Chris Lattner7727d052010-09-21 06:44:06 +0000807 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000808 MachinePointerInfo(), false, false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000809
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000810 if (VA.isRegLoc()) {
811 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
812 assert(i+1 != e);
813 CCValAssign &NextVA = ArgLocs[++i];
814 if (NextVA.isRegLoc()) {
815 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
816 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000817 // Store the low part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000818 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
819 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
820 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
821 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
822 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
823 MachinePointerInfo(),
824 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000825 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000826 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000827 unsigned Offset = VA.getLocMemOffset() + StackOffset;
828 // Store the high part.
829 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
830 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
831 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
832 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
833 MachinePointerInfo(),
834 false, false, 0));
835 // Store the low part.
836 PtrOff = DAG.getIntPtrConstant(Offset+4);
837 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
838 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
839 MachinePointerInfo(),
840 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000841 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000842 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000843 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000844
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000845 // Arguments that can be passed on register must be kept at
846 // RegsToPass vector
847 if (VA.isRegLoc()) {
848 if (VA.getLocVT() != MVT::f32) {
849 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
850 continue;
851 }
852 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
853 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
854 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000855 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000856
857 assert(VA.isMemLoc());
858
859 // Create a store off the stack pointer for this argument.
860 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
861 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
862 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
863 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
864 MachinePointerInfo(),
865 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000866 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000867
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000868
Chris Lattner49b269d2008-03-17 05:41:48 +0000869 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000870 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +0000871 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner7d4152b2008-03-17 06:58:37 +0000872 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000873
874 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000875 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000876 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000877 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000878 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000879 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000880 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000881 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000882 InFlag = Chain.getValue(1);
883 }
884
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000885 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000886 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000887
Chris Lattner49b269d2008-03-17 05:41:48 +0000888 // If the callee is a GlobalAddress node (quite common, every direct call is)
889 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000890 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner49b269d2008-03-17 05:41:48 +0000891 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patela3ca21b2010-07-06 22:08:15 +0000892 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling24c79f22008-09-16 21:48:12 +0000893 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson9f944592009-08-11 20:47:22 +0000894 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000895
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000896 // Returns a chain & a flag for retval copy to use
897 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
898 SmallVector<SDValue, 8> Ops;
899 Ops.push_back(Chain);
900 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000901 if (hasStructRetAttr)
902 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000903 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
904 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
905 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000906
907 // Add a register mask operand representing the call-preserved registers.
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000908 const SparcRegisterInfo *TRI =
909 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
910 const uint32_t *Mask = ((hasReturnsTwice)
911 ? TRI->getRTCallPreservedMask(CallConv)
912 : TRI->getCallPreservedMask(CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000913 assert(Mask && "Missing call preserved mask for calling convention");
914 Ops.push_back(DAG.getRegisterMask(Mask));
915
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000916 if (InFlag.getNode())
917 Ops.push_back(InFlag);
918
919 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner49b269d2008-03-17 05:41:48 +0000920 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000921
Chris Lattner27539552008-10-11 22:08:30 +0000922 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000923 DAG.getIntPtrConstant(0, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000924 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000925
Chris Lattnerdb26db22008-03-17 06:01:07 +0000926 // Assign locations to each value returned by this call.
927 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000928 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000929 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000930
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000931 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000932
Chris Lattnerdb26db22008-03-17 06:01:07 +0000933 // Copy all of the result registers out of their specified physreg.
934 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000935 Chain = DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
Chris Lattnerdb26db22008-03-17 06:01:07 +0000936 RVLocs[i].getValVT(), InFlag).getValue(1);
937 InFlag = Chain.getValue(2);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000938 InVals.push_back(Chain.getValue(0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000939 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000940
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000941 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000942}
943
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000944// This functions returns true if CalleeName is a ABI function that returns
945// a long double (fp128).
946static bool isFP128ABICall(const char *CalleeName)
947{
948 static const char *const ABICalls[] =
949 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
950 "_Q_sqrt", "_Q_neg",
951 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +0000952 "_Q_lltoq", "_Q_ulltoq",
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000953 0
954 };
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +0000955 for (const char * const *I = ABICalls; *I != 0; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000956 if (strcmp(CalleeName, *I) == 0)
957 return true;
958 return false;
959}
960
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000961unsigned
962SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
963{
964 const Function *CalleeFn = 0;
965 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
966 CalleeFn = dyn_cast<Function>(G->getGlobal());
967 } else if (ExternalSymbolSDNode *E =
968 dyn_cast<ExternalSymbolSDNode>(Callee)) {
969 const Function *Fn = DAG.getMachineFunction().getFunction();
970 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000971 const char *CalleeName = E->getSymbol();
972 CalleeFn = M->getFunction(CalleeName);
973 if (!CalleeFn && isFP128ABICall(CalleeName))
974 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000975 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000976
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000977 if (!CalleeFn)
978 return 0;
979
980 assert(CalleeFn->hasStructRetAttr() &&
981 "Callee does not have the StructRet attribute.");
982
Chris Lattner229907c2011-07-18 04:54:35 +0000983 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
984 Type *ElementTy = Ty->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000985 return getDataLayout()->getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000986}
Chris Lattner49b269d2008-03-17 05:41:48 +0000987
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +0000988
989// Fixup floating point arguments in the ... part of a varargs call.
990//
991// The SPARC v9 ABI requires that floating point arguments are treated the same
992// as integers when calling a varargs function. This does not apply to the
993// fixed arguments that are part of the function's prototype.
994//
995// This function post-processes a CCValAssign array created by
996// AnalyzeCallOperands().
997static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
998 ArrayRef<ISD::OutputArg> Outs) {
999 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1000 const CCValAssign &VA = ArgLocs[i];
1001 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1002 // varargs functions.
1003 if (!VA.isRegLoc() || VA.getLocVT() != MVT::f64)
1004 continue;
1005 // The fixed arguments to a varargs function still go in FP registers.
1006 if (Outs[VA.getValNo()].IsFixed)
1007 continue;
1008
1009 // This floating point argument should be reassigned.
1010 CCValAssign NewVA;
1011
1012 // Determine the offset into the argument array.
1013 unsigned Offset = 8 * (VA.getLocReg() - SP::D0);
1014 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1015
1016 if (Offset < 6*8) {
1017 // This argument should go in %i0-%i5.
1018 unsigned IReg = SP::I0 + Offset/8;
1019 // Full register, just bitconvert into i64.
1020 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1021 IReg, MVT::i64, CCValAssign::BCvt);
1022 } else {
1023 // This needs to go to memory, we're out of integer registers.
1024 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1025 Offset, VA.getLocVT(), VA.getLocInfo());
1026 }
1027 ArgLocs[i] = NewVA;
1028 }
1029}
1030
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001031// Lower a call for the 64-bit ABI.
1032SDValue
1033SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1034 SmallVectorImpl<SDValue> &InVals) const {
1035 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001036 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001037 SDValue Chain = CLI.Chain;
1038
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001039 // Sparc target does not yet support tail call optimization.
1040 CLI.IsTailCall = false;
1041
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001042 // Analyze operands of the call, assigning locations to each operand.
1043 SmallVector<CCValAssign, 16> ArgLocs;
1044 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1045 DAG.getTarget(), ArgLocs, *DAG.getContext());
1046 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1047
1048 // Get the size of the outgoing arguments stack space requirement.
1049 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001050 // Called functions expect 6 argument words to exist in the stack frame, used
1051 // or not.
1052 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001053
1054 // Keep stack frames 16-byte aligned.
1055 ArgsSize = RoundUpToAlignment(ArgsSize, 16);
1056
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001057 // Varargs calls require special treatment.
1058 if (CLI.IsVarArg)
1059 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1060
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001061 // Adjust the stack pointer to make room for the arguments.
1062 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1063 // with more than 6 arguments.
Andrew Trickad6d08a2013-05-29 22:03:55 +00001064 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1065 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001066
1067 // Collect the set of registers to pass to the function and their values.
1068 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1069 // instruction.
1070 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1071
1072 // Collect chains from all the memory opeations that copy arguments to the
1073 // stack. They must follow the stack pointer adjustment above and precede the
1074 // call instruction itself.
1075 SmallVector<SDValue, 8> MemOpChains;
1076
1077 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1078 const CCValAssign &VA = ArgLocs[i];
1079 SDValue Arg = CLI.OutVals[i];
1080
1081 // Promote the value if needed.
1082 switch (VA.getLocInfo()) {
1083 default:
1084 llvm_unreachable("Unknown location info!");
1085 case CCValAssign::Full:
1086 break;
1087 case CCValAssign::SExt:
1088 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1089 break;
1090 case CCValAssign::ZExt:
1091 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1092 break;
1093 case CCValAssign::AExt:
1094 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1095 break;
1096 case CCValAssign::BCvt:
1097 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1098 break;
1099 }
1100
1101 if (VA.isRegLoc()) {
1102 // The custom bit on an i32 return value indicates that it should be
1103 // passed in the high bits of the register.
1104 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1105 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
1106 DAG.getConstant(32, MVT::i32));
1107
1108 // The next value may go in the low bits of the same register.
1109 // Handle both at once.
1110 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1111 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1112 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1113 CLI.OutVals[i+1]);
1114 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1115 // Skip the next value, it's already done.
1116 ++i;
1117 }
1118 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001119 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001120 continue;
1121 }
1122
1123 assert(VA.isMemLoc());
1124
1125 // Create a store off the stack pointer for this argument.
1126 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1127 // The argument area starts at %fp+BIAS+128 in the callee frame,
1128 // %sp+BIAS+128 in ours.
1129 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1130 Subtarget->getStackPointerBias() +
1131 128);
1132 PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, PtrOff);
1133 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1134 MachinePointerInfo(),
1135 false, false, 0));
1136 }
1137
1138 // Emit all stores, make sure they occur before the call.
1139 if (!MemOpChains.empty())
1140 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1141 &MemOpChains[0], MemOpChains.size());
1142
1143 // Build a sequence of CopyToReg nodes glued together with token chain and
1144 // glue operands which copy the outgoing args into registers. The InGlue is
1145 // necessary since all emitted instructions must be stuck together in order
1146 // to pass the live physical registers.
1147 SDValue InGlue;
1148 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1149 Chain = DAG.getCopyToReg(Chain, DL,
1150 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1151 InGlue = Chain.getValue(1);
1152 }
1153
1154 // If the callee is a GlobalAddress node (quite common, every direct call is)
1155 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1156 // Likewise ExternalSymbol -> TargetExternalSymbol.
1157 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001158 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001159 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1160 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy());
1161 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1162 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
1163
1164 // Build the operands for the call instruction itself.
1165 SmallVector<SDValue, 8> Ops;
1166 Ops.push_back(Chain);
1167 Ops.push_back(Callee);
1168 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1169 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1170 RegsToPass[i].second.getValueType()));
1171
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001172 // Add a register mask operand representing the call-preserved registers.
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001173 const SparcRegisterInfo *TRI =
1174 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
1175 const uint32_t *Mask = ((hasReturnsTwice)
1176 ? TRI->getRTCallPreservedMask(CLI.CallConv)
1177 : TRI->getCallPreservedMask(CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001178 assert(Mask && "Missing call preserved mask for calling convention");
1179 Ops.push_back(DAG.getRegisterMask(Mask));
1180
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001181 // Make sure the CopyToReg nodes are glued to the call instruction which
1182 // consumes the registers.
1183 if (InGlue.getNode())
1184 Ops.push_back(InGlue);
1185
1186 // Now the call itself.
1187 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1188 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
1189 InGlue = Chain.getValue(1);
1190
1191 // Revert the stack pointer immediately after the call.
1192 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001193 DAG.getIntPtrConstant(0, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001194 InGlue = Chain.getValue(1);
1195
1196 // Now extract the return values. This is more or less the same as
1197 // LowerFormalArguments_64.
1198
1199 // Assign locations to each value returned by this call.
1200 SmallVector<CCValAssign, 16> RVLocs;
1201 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1202 DAG.getTarget(), RVLocs, *DAG.getContext());
1203 RVInfo.AnalyzeCallResult(CLI.Ins, CC_Sparc64);
1204
1205 // Copy all of the result registers out of their specified physreg.
1206 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1207 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001208 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001209
1210 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1211 // reside in the same register in the high and low bits. Reuse the
1212 // CopyFromReg previous node to avoid duplicate copies.
1213 SDValue RV;
1214 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1215 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1216 RV = Chain.getValue(0);
1217
1218 // But usually we'll create a new CopyFromReg for a different register.
1219 if (!RV.getNode()) {
1220 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1221 Chain = RV.getValue(1);
1222 InGlue = Chain.getValue(2);
1223 }
1224
1225 // Get the high bits for i32 struct elements.
1226 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1227 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
1228 DAG.getConstant(32, MVT::i32));
1229
1230 // The callee promoted the return value, so insert an Assert?ext SDNode so
1231 // we won't promote the value again in this function.
1232 switch (VA.getLocInfo()) {
1233 case CCValAssign::SExt:
1234 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1235 DAG.getValueType(VA.getValVT()));
1236 break;
1237 case CCValAssign::ZExt:
1238 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1239 DAG.getValueType(VA.getValVT()));
1240 break;
1241 default:
1242 break;
1243 }
1244
1245 // Truncate the register down to the return value type.
1246 if (VA.isExtInLoc())
1247 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1248
1249 InVals.push_back(RV);
1250 }
1251
1252 return Chain;
1253}
1254
Chris Lattner0a1762e2008-03-17 03:21:36 +00001255//===----------------------------------------------------------------------===//
1256// TargetLowering Implementation
1257//===----------------------------------------------------------------------===//
1258
1259/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1260/// condition.
1261static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1262 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001263 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001264 case ISD::SETEQ: return SPCC::ICC_E;
1265 case ISD::SETNE: return SPCC::ICC_NE;
1266 case ISD::SETLT: return SPCC::ICC_L;
1267 case ISD::SETGT: return SPCC::ICC_G;
1268 case ISD::SETLE: return SPCC::ICC_LE;
1269 case ISD::SETGE: return SPCC::ICC_GE;
1270 case ISD::SETULT: return SPCC::ICC_CS;
1271 case ISD::SETULE: return SPCC::ICC_LEU;
1272 case ISD::SETUGT: return SPCC::ICC_GU;
1273 case ISD::SETUGE: return SPCC::ICC_CC;
1274 }
1275}
1276
1277/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1278/// FCC condition.
1279static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1280 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001281 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001282 case ISD::SETEQ:
1283 case ISD::SETOEQ: return SPCC::FCC_E;
1284 case ISD::SETNE:
1285 case ISD::SETUNE: return SPCC::FCC_NE;
1286 case ISD::SETLT:
1287 case ISD::SETOLT: return SPCC::FCC_L;
1288 case ISD::SETGT:
1289 case ISD::SETOGT: return SPCC::FCC_G;
1290 case ISD::SETLE:
1291 case ISD::SETOLE: return SPCC::FCC_LE;
1292 case ISD::SETGE:
1293 case ISD::SETOGE: return SPCC::FCC_GE;
1294 case ISD::SETULT: return SPCC::FCC_UL;
1295 case ISD::SETULE: return SPCC::FCC_ULE;
1296 case ISD::SETUGT: return SPCC::FCC_UG;
1297 case ISD::SETUGE: return SPCC::FCC_UGE;
1298 case ISD::SETUO: return SPCC::FCC_U;
1299 case ISD::SETO: return SPCC::FCC_O;
1300 case ISD::SETONE: return SPCC::FCC_LG;
1301 case ISD::SETUEQ: return SPCC::FCC_UE;
1302 }
1303}
1304
Chris Lattner0a1762e2008-03-17 03:21:36 +00001305SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattnerc9ea8fd2009-08-08 20:43:12 +00001306 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001307 Subtarget = &TM.getSubtarget<SparcSubtarget>();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001308
Chris Lattner0a1762e2008-03-17 03:21:36 +00001309 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001310 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1311 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1312 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001313 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001314 if (Subtarget->is64Bit())
1315 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001316
1317 // Turn FP extload into load/fextend
Owen Anderson9f944592009-08-11 20:47:22 +00001318 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001319 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
1320
Chris Lattner0a1762e2008-03-17 03:21:36 +00001321 // Sparc doesn't have i1 sign extending load
Owen Anderson9f944592009-08-11 20:47:22 +00001322 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001323
Chris Lattner0a1762e2008-03-17 03:21:36 +00001324 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001325 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001326 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1327 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001328
1329 // Custom legalize GlobalAddress nodes into LO/HI parts.
Jakob Stoklund Olesen15b3e902013-04-13 19:02:23 +00001330 setOperationAction(ISD::GlobalAddress, getPointerTy(), Custom);
1331 setOperationAction(ISD::GlobalTLSAddress, getPointerTy(), Custom);
1332 setOperationAction(ISD::ConstantPool, getPointerTy(), Custom);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001333 setOperationAction(ISD::BlockAddress, getPointerTy(), Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001334
Chris Lattner0a1762e2008-03-17 03:21:36 +00001335 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001336 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1337 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1338 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001339
1340 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001341 setOperationAction(ISD::UREM, MVT::i32, Expand);
1342 setOperationAction(ISD::SREM, MVT::i32, Expand);
1343 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1344 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001345
Roman Divacky2262cfa2013-10-31 19:22:33 +00001346 // ... nor does SparcV9.
1347 if (Subtarget->is64Bit()) {
1348 setOperationAction(ISD::UREM, MVT::i64, Expand);
1349 setOperationAction(ISD::SREM, MVT::i64, Expand);
1350 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1351 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1352 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001353
1354 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001355 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1356 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001357 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1358 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001359
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001360 // Custom Expand fp<->uint
1361 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1362 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001363 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1364 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001365
Wesley Peck527da1b2010-11-23 03:31:01 +00001366 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1367 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001368
Chris Lattner0a1762e2008-03-17 03:21:36 +00001369 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001370 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1371 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1372 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001373 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1374
Owen Anderson9f944592009-08-11 20:47:22 +00001375 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1376 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1377 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001378 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001379
Chris Lattner0a1762e2008-03-17 03:21:36 +00001380 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001381 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1382 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1383 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1384 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1385 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1386 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001387 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001388
Owen Anderson9f944592009-08-11 20:47:22 +00001389 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1390 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1391 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001392 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001393
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001394 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001395 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1396 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1397 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1398 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001399 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1400 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001401 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1402 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001403 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001404 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001405
1406 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1407 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
1408 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1409 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
1410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1411 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001412 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1413 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001414 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001415 }
1416
Eli Friedman26a48482011-07-27 22:21:52 +00001417 // FIXME: There are instructions available for ATOMIC_FENCE
1418 // on SparcV8 and later.
Eli Friedman26a48482011-07-27 22:21:52 +00001419 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001420
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001421 if (!Subtarget->isV9()) {
1422 // SparcV8 does not have FNEGD and FABSD.
1423 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1424 setOperationAction(ISD::FABS, MVT::f64, Custom);
1425 }
1426
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001427 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1428 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1429 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1430 setOperationAction(ISD::FREM , MVT::f128, Expand);
1431 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001432 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1433 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001434 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001435 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001436 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001437 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1438 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001439 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001440 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001441 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001442 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1443 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001444 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001445 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001446 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001447 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1448 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1449 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001450 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001451 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1452 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001453 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001454 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1455 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001456
Owen Anderson9f944592009-08-11 20:47:22 +00001457 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1458 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1459 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001460
1461 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001462 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1463 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001464
Chris Lattner0a1762e2008-03-17 03:21:36 +00001465 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001466 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001467 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001468 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001469
Chris Lattner0a1762e2008-03-17 03:21:36 +00001470 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001471 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1472 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1473 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1474 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1475 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001476
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +00001477 setExceptionPointerRegister(SP::I0);
1478 setExceptionSelectorRegister(SP::I1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001479
Chris Lattner0a1762e2008-03-17 03:21:36 +00001480 setStackPointerRegisterToSaveRestore(SP::O6);
1481
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001482 if (Subtarget->isV9())
Owen Anderson9f944592009-08-11 20:47:22 +00001483 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001484
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001485 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1486 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1487 setOperationAction(ISD::STORE, MVT::f128, Legal);
1488 } else {
1489 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1490 setOperationAction(ISD::STORE, MVT::f128, Custom);
1491 }
1492
1493 if (Subtarget->hasHardQuad()) {
1494 setOperationAction(ISD::FADD, MVT::f128, Legal);
1495 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1496 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1497 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1498 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1499 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1500 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1501 if (Subtarget->isV9()) {
1502 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1503 setOperationAction(ISD::FABS, MVT::f128, Legal);
1504 } else {
1505 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1506 setOperationAction(ISD::FABS, MVT::f128, Custom);
1507 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001508
1509 if (!Subtarget->is64Bit()) {
1510 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1511 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1512 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1513 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1514 }
1515
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001516 } else {
1517 // Custom legalize f128 operations.
1518
1519 setOperationAction(ISD::FADD, MVT::f128, Custom);
1520 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1521 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1522 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1523 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1524 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1525 setOperationAction(ISD::FABS, MVT::f128, Custom);
1526
1527 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1528 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1529 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1530
1531 // Setup Runtime library names.
1532 if (Subtarget->is64Bit()) {
1533 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1534 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1535 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1536 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1537 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1538 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001539 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001540 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001541 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001542 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1543 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1544 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1545 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001546 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1547 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1548 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1549 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
1550 } else {
1551 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1552 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1553 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1554 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1555 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1556 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001557 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001558 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001559 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001560 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1561 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1562 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1563 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001564 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1565 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1566 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1567 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1568 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001569 }
1570
Eli Friedman2518f832011-05-06 20:34:06 +00001571 setMinFunctionAlignment(2);
1572
Chris Lattner0a1762e2008-03-17 03:21:36 +00001573 computeRegisterProperties();
1574}
1575
1576const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
1577 switch (Opcode) {
1578 default: return 0;
1579 case SPISD::CMPICC: return "SPISD::CMPICC";
1580 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1581 case SPISD::BRICC: return "SPISD::BRICC";
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001582 case SPISD::BRXCC: return "SPISD::BRXCC";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001583 case SPISD::BRFCC: return "SPISD::BRFCC";
1584 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001585 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001586 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1587 case SPISD::Hi: return "SPISD::Hi";
1588 case SPISD::Lo: return "SPISD::Lo";
1589 case SPISD::FTOI: return "SPISD::FTOI";
1590 case SPISD::ITOF: return "SPISD::ITOF";
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001591 case SPISD::FTOX: return "SPISD::FTOX";
1592 case SPISD::XTOF: return "SPISD::XTOF";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001593 case SPISD::CALL: return "SPISD::CALL";
1594 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001595 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00001596 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001597 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1598 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1599 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001600 }
1601}
1602
1603/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1604/// be zero. Op is expected to be a target specific node. Used by DAG
1605/// combiner.
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001606void SparcTargetLowering::computeMaskedBitsForTargetNode
1607 (const SDValue Op,
1608 APInt &KnownZero,
1609 APInt &KnownOne,
1610 const SelectionDAG &DAG,
1611 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001612 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001613 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001614
Chris Lattner0a1762e2008-03-17 03:21:36 +00001615 switch (Op.getOpcode()) {
1616 default: break;
1617 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001618 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001619 case SPISD::SELECT_FCC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001620 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1621 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001622 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1623 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1624
Chris Lattner0a1762e2008-03-17 03:21:36 +00001625 // Only known if known in both the LHS and RHS.
1626 KnownOne &= KnownOne2;
1627 KnownZero &= KnownZero2;
1628 break;
1629 }
1630}
1631
Chris Lattner0a1762e2008-03-17 03:21:36 +00001632// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1633// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001634static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001635 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001636 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmanf1d83042010-06-18 14:22:04 +00001637 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001638 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001639 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1640 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001641 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1642 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1643 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
1644 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1645 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf1d83042010-06-18 14:22:04 +00001646 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
1647 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001648 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001649 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001650 LHS = CMPCC.getOperand(0);
1651 RHS = CMPCC.getOperand(1);
1652 }
1653}
1654
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001655// Convert to a target node and set target flags.
1656SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1657 SelectionDAG &DAG) const {
1658 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1659 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001660 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001661 GA->getValueType(0),
1662 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001663
1664 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1665 return DAG.getTargetConstantPool(CP->getConstVal(),
1666 CP->getValueType(0),
1667 CP->getAlignment(),
1668 CP->getOffset(), TF);
1669
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001670 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1671 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1672 Op.getValueType(),
1673 0,
1674 TF);
1675
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001676 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1677 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1678 ES->getValueType(0), TF);
1679
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001680 llvm_unreachable("Unhandled address SDNode");
1681}
1682
1683// Split Op into high and low parts according to HiTF and LoTF.
1684// Return an ADD node combining the parts.
1685SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1686 unsigned HiTF, unsigned LoTF,
1687 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001688 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001689 EVT VT = Op.getValueType();
1690 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1691 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1692 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1693}
1694
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001695// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1696// or ExternalSymbol SDNode.
1697SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001698 SDLoc DL(Op);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001699 EVT VT = getPointerTy();
1700
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001701 // Handle PIC mode first.
1702 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1703 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
1704 SDValue HiLo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001705 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1706 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001707 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1708 // function has calls.
1709 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1710 MFI->setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001711 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
1712 MachinePointerInfo::getGOT(), false, false, false, 0);
1713 }
1714
1715 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001716 switch(getTargetMachine().getCodeModel()) {
1717 default:
1718 llvm_unreachable("Unsupported absolute code model");
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +00001719 case CodeModel::JITDefault:
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001720 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001721 // abs32.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001722 return makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1723 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001724 // abs44.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001725 SDValue H44 = makeHiLoPair(Op, SPII::MO_H44, SPII::MO_M44, DAG);
Jakob Stoklund Oleseneed10722013-04-14 05:48:50 +00001726 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, MVT::i32));
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001727 SDValue L44 = withTargetFlags(Op, SPII::MO_L44, DAG);
1728 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1729 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1730 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001731 case CodeModel::Large: {
1732 // abs64.
1733 SDValue Hi = makeHiLoPair(Op, SPII::MO_HH, SPII::MO_HM, DAG);
Jakob Stoklund Oleseneed10722013-04-14 05:48:50 +00001734 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, MVT::i32));
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001735 SDValue Lo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1736 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1737 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001738 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001739}
1740
Wesley Peck527da1b2010-11-23 03:31:01 +00001741SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001742 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001743 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001744}
1745
Chris Lattner840c7002009-09-15 17:46:24 +00001746SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001747 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001748 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001749}
1750
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001751SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
1752 SelectionDAG &DAG) const {
1753 return makeAddress(Op, DAG);
1754}
1755
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001756SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1757 SelectionDAG &DAG) const {
1758
1759 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1760 SDLoc DL(GA);
1761 const GlobalValue *GV = GA->getGlobal();
1762 EVT PtrVT = getPointerTy();
1763
1764 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1765
1766 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1767 unsigned HiTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_HI22
1768 : SPII::MO_TLS_LDM_HI22);
1769 unsigned LoTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_LO10
1770 : SPII::MO_TLS_LDM_LO10);
1771 unsigned addTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_ADD
1772 : SPII::MO_TLS_LDM_ADD);
1773 unsigned callTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_CALL
1774 : SPII::MO_TLS_LDM_CALL);
1775
1776 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
1777 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1778 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
1779 withTargetFlags(Op, addTF, DAG));
1780
1781 SDValue Chain = DAG.getEntryNode();
1782 SDValue InFlag;
1783
1784 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, true), DL);
1785 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
1786 InFlag = Chain.getValue(1);
1787 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
1788 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
1789
1790 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1791 SmallVector<SDValue, 4> Ops;
1792 Ops.push_back(Chain);
1793 Ops.push_back(Callee);
1794 Ops.push_back(Symbol);
1795 Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
1796 const uint32_t *Mask = getTargetMachine()
1797 .getRegisterInfo()->getCallPreservedMask(CallingConv::C);
1798 assert(Mask && "Missing call preserved mask for calling convention");
1799 Ops.push_back(DAG.getRegisterMask(Mask));
1800 Ops.push_back(InFlag);
1801 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, &Ops[0], Ops.size());
1802 InFlag = Chain.getValue(1);
1803 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, true),
1804 DAG.getIntPtrConstant(0, true), InFlag, DL);
1805 InFlag = Chain.getValue(1);
1806 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
1807
1808 if (model != TLSModel::LocalDynamic)
1809 return Ret;
1810
1811 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1812 withTargetFlags(Op, SPII::MO_TLS_LDO_HIX22, DAG));
1813 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1814 withTargetFlags(Op, SPII::MO_TLS_LDO_LOX10, DAG));
1815 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1816 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
1817 withTargetFlags(Op, SPII::MO_TLS_LDO_ADD, DAG));
1818 }
1819
1820 if (model == TLSModel::InitialExec) {
1821 unsigned ldTF = ((PtrVT == MVT::i64)? SPII::MO_TLS_IE_LDX
1822 : SPII::MO_TLS_IE_LD);
1823
1824 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1825
1826 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1827 // function has calls.
1828 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1829 MFI->setHasCalls(true);
1830
1831 SDValue TGA = makeHiLoPair(Op,
1832 SPII::MO_TLS_IE_HI22, SPII::MO_TLS_IE_LO10, DAG);
1833 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
1834 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
1835 DL, PtrVT, Ptr,
1836 withTargetFlags(Op, ldTF, DAG));
1837 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
1838 DAG.getRegister(SP::G7, PtrVT), Offset,
1839 withTargetFlags(Op, SPII::MO_TLS_IE_ADD, DAG));
1840 }
1841
1842 assert(model == TLSModel::LocalExec);
1843 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1844 withTargetFlags(Op, SPII::MO_TLS_LE_HIX22, DAG));
1845 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1846 withTargetFlags(Op, SPII::MO_TLS_LE_LOX10, DAG));
1847 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1848
1849 return DAG.getNode(ISD::ADD, DL, PtrVT,
1850 DAG.getRegister(SP::G7, PtrVT), Offset);
1851}
1852
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001853SDValue
1854SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain, ArgListTy &Args,
1855 SDValue Arg, SDLoc DL,
1856 SelectionDAG &DAG) const {
1857 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1858 EVT ArgVT = Arg.getValueType();
1859 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1860
1861 ArgListEntry Entry;
1862 Entry.Node = Arg;
1863 Entry.Ty = ArgTy;
1864
1865 if (ArgTy->isFP128Ty()) {
1866 // Create a stack object and pass the pointer to the library function.
1867 int FI = MFI->CreateStackObject(16, 8, false);
1868 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
1869 Chain = DAG.getStore(Chain,
1870 DL,
1871 Entry.Node,
1872 FIPtr,
1873 MachinePointerInfo(),
1874 false,
1875 false,
1876 8);
1877
1878 Entry.Node = FIPtr;
1879 Entry.Ty = PointerType::getUnqual(ArgTy);
1880 }
1881 Args.push_back(Entry);
1882 return Chain;
1883}
1884
1885SDValue
1886SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
1887 const char *LibFuncName,
1888 unsigned numArgs) const {
1889
1890 ArgListTy Args;
1891
1892 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1893
1894 SDValue Callee = DAG.getExternalSymbol(LibFuncName, getPointerTy());
1895 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
1896 Type *RetTyABI = RetTy;
1897 SDValue Chain = DAG.getEntryNode();
1898 SDValue RetPtr;
1899
1900 if (RetTy->isFP128Ty()) {
1901 // Create a Stack Object to receive the return value of type f128.
1902 ArgListEntry Entry;
1903 int RetFI = MFI->CreateStackObject(16, 8, false);
1904 RetPtr = DAG.getFrameIndex(RetFI, getPointerTy());
1905 Entry.Node = RetPtr;
1906 Entry.Ty = PointerType::getUnqual(RetTy);
1907 if (!Subtarget->is64Bit())
1908 Entry.isSRet = true;
1909 Entry.isReturned = false;
1910 Args.push_back(Entry);
1911 RetTyABI = Type::getVoidTy(*DAG.getContext());
1912 }
1913
1914 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
1915 for (unsigned i = 0, e = numArgs; i != e; ++i) {
1916 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
1917 }
1918 TargetLowering::
1919 CallLoweringInfo CLI(Chain,
1920 RetTyABI,
1921 false, false, false, false,
1922 0, CallingConv::C,
1923 false, false, true,
1924 Callee, Args, DAG, SDLoc(Op));
1925 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
1926
1927 // chain is in second result.
1928 if (RetTyABI == RetTy)
1929 return CallInfo.first;
1930
1931 assert (RetTy->isFP128Ty() && "Unexpected return type!");
1932
1933 Chain = CallInfo.second;
1934
1935 // Load RetPtr to get the return value.
1936 return DAG.getLoad(Op.getValueType(),
1937 SDLoc(Op),
1938 Chain,
1939 RetPtr,
1940 MachinePointerInfo(),
1941 false, false, false, 8);
1942}
1943
1944SDValue
1945SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
1946 unsigned &SPCC,
1947 SDLoc DL,
1948 SelectionDAG &DAG) const {
1949
1950 const char *LibCall = 0;
1951 bool is64Bit = Subtarget->is64Bit();
1952 switch(SPCC) {
1953 default: llvm_unreachable("Unhandled conditional code!");
1954 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
1955 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
1956 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
1957 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
1958 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
1959 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
1960 case SPCC::FCC_UL :
1961 case SPCC::FCC_ULE:
1962 case SPCC::FCC_UG :
1963 case SPCC::FCC_UGE:
1964 case SPCC::FCC_U :
1965 case SPCC::FCC_O :
1966 case SPCC::FCC_LG :
1967 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
1968 }
1969
1970 SDValue Callee = DAG.getExternalSymbol(LibCall, getPointerTy());
1971 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
1972 ArgListTy Args;
1973 SDValue Chain = DAG.getEntryNode();
1974 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
1975 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
1976
1977 TargetLowering::
1978 CallLoweringInfo CLI(Chain,
1979 RetTy,
1980 false, false, false, false,
1981 0, CallingConv::C,
1982 false, false, true,
1983 Callee, Args, DAG, DL);
1984
1985 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
1986
1987 // result is in first, and chain is in second result.
1988 SDValue Result = CallInfo.first;
1989
1990 switch(SPCC) {
1991 default: {
1992 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
1993 SPCC = SPCC::ICC_NE;
1994 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1995 }
1996 case SPCC::FCC_UL : {
1997 SDValue Mask = DAG.getTargetConstant(1, Result.getValueType());
1998 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
1999 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2000 SPCC = SPCC::ICC_NE;
2001 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2002 }
2003 case SPCC::FCC_ULE: {
Venkatraman Govindarajub803cec2013-09-04 15:15:20 +00002004 SDValue RHS = DAG.getTargetConstant(2, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002005 SPCC = SPCC::ICC_NE;
2006 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2007 }
2008 case SPCC::FCC_UG : {
2009 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2010 SPCC = SPCC::ICC_G;
2011 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2012 }
2013 case SPCC::FCC_UGE: {
2014 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2015 SPCC = SPCC::ICC_NE;
2016 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2017 }
2018
2019 case SPCC::FCC_U : {
2020 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2021 SPCC = SPCC::ICC_E;
2022 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2023 }
2024 case SPCC::FCC_O : {
2025 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2026 SPCC = SPCC::ICC_NE;
2027 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2028 }
2029 case SPCC::FCC_LG : {
2030 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
2031 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2032 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2033 SPCC = SPCC::ICC_NE;
2034 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2035 }
2036 case SPCC::FCC_UE : {
2037 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
2038 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2039 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2040 SPCC = SPCC::ICC_E;
2041 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2042 }
2043 }
2044}
2045
2046static SDValue
2047LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2048 const SparcTargetLowering &TLI) {
2049
2050 if (Op.getOperand(0).getValueType() == MVT::f64)
2051 return TLI.LowerF128Op(Op, DAG,
2052 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2053
2054 if (Op.getOperand(0).getValueType() == MVT::f32)
2055 return TLI.LowerF128Op(Op, DAG,
2056 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2057
2058 llvm_unreachable("fpextend with non-float operand!");
2059 return SDValue(0, 0);
2060}
2061
2062static SDValue
2063LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2064 const SparcTargetLowering &TLI) {
2065 // FP_ROUND on f64 and f32 are legal.
2066 if (Op.getOperand(0).getValueType() != MVT::f128)
2067 return Op;
2068
2069 if (Op.getValueType() == MVT::f64)
2070 return TLI.LowerF128Op(Op, DAG,
2071 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2072 if (Op.getValueType() == MVT::f32)
2073 return TLI.LowerF128Op(Op, DAG,
2074 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2075
2076 llvm_unreachable("fpround to non-float!");
2077 return SDValue(0, 0);
2078}
2079
2080static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2081 const SparcTargetLowering &TLI,
2082 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002083 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002084 EVT VT = Op.getValueType();
2085 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002086
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002087 // Expand f128 operations to fp128 abi calls.
2088 if (Op.getOperand(0).getValueType() == MVT::f128
2089 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2090 const char *libName = TLI.getLibcallName(VT == MVT::i32
2091 ? RTLIB::FPTOSINT_F128_I32
2092 : RTLIB::FPTOSINT_F128_I64);
2093 return TLI.LowerF128Op(Op, DAG, libName, 1);
2094 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002095
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002096 // Expand if the resulting type is illegal.
2097 if (!TLI.isTypeLegal(VT))
2098 return SDValue(0, 0);
2099
2100 // Otherwise, Convert the fp value to integer in an FP register.
2101 if (VT == MVT::i32)
2102 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2103 else
2104 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2105
2106 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002107}
2108
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002109static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2110 const SparcTargetLowering &TLI,
2111 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002112 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002113 EVT OpVT = Op.getOperand(0).getValueType();
2114 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2115
2116 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2117
2118 // Expand f128 operations to fp128 ABI calls.
2119 if (Op.getValueType() == MVT::f128
2120 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2121 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2122 ? RTLIB::SINTTOFP_I32_F128
2123 : RTLIB::SINTTOFP_I64_F128);
2124 return TLI.LowerF128Op(Op, DAG, libName, 1);
2125 }
2126
2127 // Expand if the operand type is illegal.
2128 if (!TLI.isTypeLegal(OpVT))
2129 return SDValue(0, 0);
2130
2131 // Otherwise, Convert the int value to FP in an FP register.
2132 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2133 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2134 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002135}
2136
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002137static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2138 const SparcTargetLowering &TLI,
2139 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002140 SDLoc dl(Op);
2141 EVT VT = Op.getValueType();
2142
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002143 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002144 // quad floating point instructions and the resulting type is legal.
2145 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2146 (hasHardQuad && TLI.isTypeLegal(VT)))
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002147 return SDValue(0, 0);
2148
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002149 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002150
2151 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002152 TLI.getLibcallName(VT == MVT::i32
2153 ? RTLIB::FPTOUINT_F128_I32
2154 : RTLIB::FPTOUINT_F128_I64),
2155 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002156}
2157
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002158static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2159 const SparcTargetLowering &TLI,
2160 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002161 SDLoc dl(Op);
2162 EVT OpVT = Op.getOperand(0).getValueType();
2163 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2164
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002165 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002166 // quad floating point instructions and the operand type is legal.
2167 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002168 return SDValue(0, 0);
2169
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002170 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002171 TLI.getLibcallName(OpVT == MVT::i32
2172 ? RTLIB::UINTTOFP_I32_F128
2173 : RTLIB::UINTTOFP_I64_F128),
2174 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002175}
2176
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002177static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2178 const SparcTargetLowering &TLI,
2179 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002180 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002181 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002182 SDValue LHS = Op.getOperand(2);
2183 SDValue RHS = Op.getOperand(3);
2184 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002185 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002186 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002187
Chris Lattner0a1762e2008-03-17 03:21:36 +00002188 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2189 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2190 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002191
Chris Lattner0a1762e2008-03-17 03:21:36 +00002192 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002193 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002194 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002195 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002196 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002197 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2198 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002199 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002200 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2201 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2202 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2203 Opc = SPISD::BRICC;
2204 } else {
2205 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2206 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2207 Opc = SPISD::BRFCC;
2208 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002209 }
Owen Anderson9f944592009-08-11 20:47:22 +00002210 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
2211 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002212}
2213
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002214static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2215 const SparcTargetLowering &TLI,
2216 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002217 SDValue LHS = Op.getOperand(0);
2218 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002219 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002220 SDValue TrueVal = Op.getOperand(2);
2221 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002222 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002223 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002224
Chris Lattner0a1762e2008-03-17 03:21:36 +00002225 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2226 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2227 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002228
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002229 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002230 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002231 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002232 Opc = LHS.getValueType() == MVT::i32 ?
2233 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002234 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2235 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002236 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2237 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2238 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2239 Opc = SPISD::SELECT_ICC;
2240 } else {
2241 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2242 Opc = SPISD::SELECT_FCC;
2243 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2244 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002245 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002246 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson9f944592009-08-11 20:47:22 +00002247 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002248}
2249
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002250static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002251 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002252 MachineFunction &MF = DAG.getMachineFunction();
2253 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
2254
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002255 // Need frame address to find the address of VarArgsFrameIndex.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002256 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2257
Chris Lattner0a1762e2008-03-17 03:21:36 +00002258 // vastart just stores the address of the VarArgsFrameIndex slot into the
2259 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002260 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002261 SDValue Offset =
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002262 DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(),
2263 DAG.getRegister(SP::I6, TLI.getPointerTy()),
2264 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset()));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002265 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002266 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Chris Lattner676c61d2010-09-21 18:41:36 +00002267 MachinePointerInfo(SV), false, false, 0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002268}
2269
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002270static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002271 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002272 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002273 SDValue InChain = Node->getOperand(0);
2274 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002275 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002276 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002277 SDLoc DL(Node);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002278 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002279 MachinePointerInfo(SV), false, false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002280 // Increment the pointer, VAList, to the next vaarg.
2281 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
2282 DAG.getIntPtrConstant(VT.getSizeInBits()/8));
2283 // Store the incremented VAList to the legalized pointer.
2284 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
Chris Lattner676c61d2010-09-21 18:41:36 +00002285 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002286 // Load the actual argument out of the pointer VAList.
2287 // We can't count on greater alignment than the word size.
2288 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2289 false, false, false,
2290 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002291}
2292
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002293static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
2294 bool is64Bit) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002295 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2296 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002297 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002298 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002299
Chris Lattner0a1762e2008-03-17 03:21:36 +00002300 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002301 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2302 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002303 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002304
Chris Lattner0a1762e2008-03-17 03:21:36 +00002305 // The resultant pointer is actually 16 words from the bottom of the stack,
2306 // to provide a register spill area.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002307 unsigned regSpillArea = (is64Bit) ? 128 : 96;
2308 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
2309 DAG.getConstant(regSpillArea, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002310 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002311 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002312}
2313
Chris Lattner0a1762e2008-03-17 03:21:36 +00002314
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002315static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002316 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002317 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002318 dl, MVT::Other, DAG.getEntryNode());
2319 return Chain;
2320}
2321
2322static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
2323 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2324 MFI->setFrameAddressIsTaken(true);
2325
2326 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002327 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002328 unsigned FrameReg = SP::I6;
2329
2330 uint64_t depth = Op.getConstantOperandVal(0);
2331
2332 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002333 if (depth == 0)
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002334 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2335 else {
2336 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002337 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002338 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002339
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002340 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002341 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002342 dl, MVT::i32,
2343 FrameAddr, DAG.getIntPtrConstant(56));
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002344 FrameAddr = DAG.getLoad(MVT::i32, dl,
2345 Chain,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002346 Ptr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002347 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002348 }
2349 }
2350 return FrameAddr;
2351}
2352
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002353static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
2354 const SparcTargetLowering &TLI) {
2355 MachineFunction &MF = DAG.getMachineFunction();
2356 MachineFrameInfo *MFI = MF.getFrameInfo();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002357 MFI->setReturnAddressIsTaken(true);
2358
2359 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002360 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002361 uint64_t depth = Op.getConstantOperandVal(0);
2362
2363 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002364 if (depth == 0) {
2365 unsigned RetReg = MF.addLiveIn(SP::I7,
2366 TLI.getRegClassFor(TLI.getPointerTy()));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002367 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002368 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002369 // Need frame address to find return address of the caller.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002370 MFI->setFrameAddressIsTaken(true);
2371
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002372 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002373 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002374 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002375
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002376 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002377 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002378 dl, MVT::i32,
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002379 RetAddr,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002380 DAG.getIntPtrConstant((i == depth-1)?60:56));
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002381 RetAddr = DAG.getLoad(MVT::i32, dl,
2382 Chain,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002383 Ptr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002384 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002385 }
2386 }
2387 return RetAddr;
2388}
2389
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002390static SDValue LowerF64Op(SDValue Op, SelectionDAG &DAG, unsigned opcode)
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002391{
2392 SDLoc dl(Op);
2393
2394 assert(Op.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002395 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002396
2397 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2398 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2399 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2400
2401 SDValue SrcReg64 = Op.getOperand(0);
2402 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2403 SrcReg64);
2404 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2405 SrcReg64);
2406
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002407 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002408
2409 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2410 dl, MVT::f64), 0);
2411 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2412 DstReg64, Hi32);
2413 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2414 DstReg64, Lo32);
2415 return DstReg64;
2416}
2417
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002418// Lower a f128 load into two f64 loads.
2419static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2420{
2421 SDLoc dl(Op);
2422 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
2423 assert(LdNode && LdNode->getOffset().getOpcode() == ISD::UNDEF
2424 && "Unexpected node type");
2425
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002426 unsigned alignment = LdNode->getAlignment();
2427 if (alignment > 8)
2428 alignment = 8;
2429
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002430 SDValue Hi64 = DAG.getLoad(MVT::f64,
2431 dl,
2432 LdNode->getChain(),
2433 LdNode->getBasePtr(),
2434 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002435 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002436 EVT addrVT = LdNode->getBasePtr().getValueType();
2437 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2438 LdNode->getBasePtr(),
2439 DAG.getConstant(8, addrVT));
2440 SDValue Lo64 = DAG.getLoad(MVT::f64,
2441 dl,
2442 LdNode->getChain(),
2443 LoPtr,
2444 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002445 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002446
2447 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2448 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2449
2450 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2451 dl, MVT::f128);
2452 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2453 MVT::f128,
2454 SDValue(InFP128, 0),
2455 Hi64,
2456 SubRegEven);
2457 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2458 MVT::f128,
2459 SDValue(InFP128, 0),
2460 Lo64,
2461 SubRegOdd);
2462 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2463 SDValue(Lo64.getNode(), 1) };
2464 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2465 &OutChains[0], 2);
2466 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
2467 return DAG.getMergeValues(Ops, 2, dl);
2468}
2469
2470// Lower a f128 store into two f64 stores.
2471static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2472 SDLoc dl(Op);
2473 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
2474 assert(StNode && StNode->getOffset().getOpcode() == ISD::UNDEF
2475 && "Unexpected node type");
2476 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2477 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2478
2479 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2480 dl,
2481 MVT::f64,
2482 StNode->getValue(),
2483 SubRegEven);
2484 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2485 dl,
2486 MVT::f64,
2487 StNode->getValue(),
2488 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002489
2490 unsigned alignment = StNode->getAlignment();
2491 if (alignment > 8)
2492 alignment = 8;
2493
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002494 SDValue OutChains[2];
2495 OutChains[0] = DAG.getStore(StNode->getChain(),
2496 dl,
2497 SDValue(Hi64, 0),
2498 StNode->getBasePtr(),
2499 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002500 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002501 EVT addrVT = StNode->getBasePtr().getValueType();
2502 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2503 StNode->getBasePtr(),
2504 DAG.getConstant(8, addrVT));
2505 OutChains[1] = DAG.getStore(StNode->getChain(),
2506 dl,
2507 SDValue(Lo64, 0),
2508 LoPtr,
2509 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002510 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002511 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2512 &OutChains[0], 2);
2513}
2514
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002515static SDValue LowerFNEG(SDValue Op, SelectionDAG &DAG,
2516 const SparcTargetLowering &TLI,
2517 bool is64Bit) {
2518 if (Op.getValueType() == MVT::f64)
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002519 return LowerF64Op(Op, DAG, ISD::FNEG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002520 if (Op.getValueType() == MVT::f128)
2521 return TLI.LowerF128Op(Op, DAG, ((is64Bit) ? "_Qp_neg" : "_Q_neg"), 1);
2522 return Op;
2523}
2524
2525static SDValue LowerFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
2526 if (Op.getValueType() == MVT::f64)
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002527 return LowerF64Op(Op, DAG, ISD::FABS);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002528 if (Op.getValueType() != MVT::f128)
2529 return Op;
2530
2531 // Lower fabs on f128 to fabs on f64
2532 // fabs f128 => fabs f64:sub_even64, fmov f64:sub_odd64
2533
2534 SDLoc dl(Op);
2535 SDValue SrcReg128 = Op.getOperand(0);
2536 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2537 SrcReg128);
2538 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2539 SrcReg128);
2540 if (isV9)
2541 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2542 else
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002543 Hi64 = LowerF64Op(Hi64, DAG, ISD::FABS);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002544
2545 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2546 dl, MVT::f128), 0);
2547 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2548 DstReg128, Hi64);
2549 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2550 DstReg128, Lo64);
2551 return DstReg128;
2552}
2553
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002554static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002555
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002556 if (Op.getValueType() != MVT::i64)
2557 return Op;
2558
2559 SDLoc dl(Op);
2560 SDValue Src1 = Op.getOperand(0);
2561 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2562 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
2563 DAG.getConstant(32, MVT::i64));
2564 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2565
2566 SDValue Src2 = Op.getOperand(1);
2567 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2568 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
2569 DAG.getConstant(32, MVT::i64));
2570 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2571
2572
2573 bool hasChain = false;
2574 unsigned hiOpc = Op.getOpcode();
2575 switch (Op.getOpcode()) {
2576 default: llvm_unreachable("Invalid opcode");
2577 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2578 case ISD::ADDE: hasChain = true; break;
2579 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2580 case ISD::SUBE: hasChain = true; break;
2581 }
2582 SDValue Lo;
2583 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2584 if (hasChain) {
2585 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2586 Op.getOperand(2));
2587 } else {
2588 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2589 }
2590 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2591 SDValue Carry = Hi.getValue(1);
2592
2593 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2594 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2595 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
2596 DAG.getConstant(32, MVT::i64));
2597
2598 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2599 SDValue Ops[2] = { Dst, Carry };
2600 return DAG.getMergeValues(Ops, 2, dl);
2601}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002602
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002603SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00002604LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002605
2606 bool hasHardQuad = Subtarget->hasHardQuad();
2607 bool is64Bit = Subtarget->is64Bit();
2608 bool isV9 = Subtarget->isV9();
2609
Chris Lattner0a1762e2008-03-17 03:21:36 +00002610 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002611 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002612
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002613 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002614 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002615 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002616 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002617 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002618 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002619 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
2620 hasHardQuad);
2621 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
2622 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002623 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
2624 hasHardQuad);
2625 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
2626 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002627 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
2628 hasHardQuad);
2629 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
2630 hasHardQuad);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002631 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
2632 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002633 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
2634 is64Bit);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002635
2636 case ISD::LOAD: return LowerF128Load(Op, DAG);
2637 case ISD::STORE: return LowerF128Store(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002638 case ISD::FADD: return LowerF128Op(Op, DAG,
2639 getLibcallName(RTLIB::ADD_F128), 2);
2640 case ISD::FSUB: return LowerF128Op(Op, DAG,
2641 getLibcallName(RTLIB::SUB_F128), 2);
2642 case ISD::FMUL: return LowerF128Op(Op, DAG,
2643 getLibcallName(RTLIB::MUL_F128), 2);
2644 case ISD::FDIV: return LowerF128Op(Op, DAG,
2645 getLibcallName(RTLIB::DIV_F128), 2);
2646 case ISD::FSQRT: return LowerF128Op(Op, DAG,
2647 getLibcallName(RTLIB::SQRT_F128),1);
2648 case ISD::FNEG: return LowerFNEG(Op, DAG, *this, is64Bit);
2649 case ISD::FABS: return LowerFABS(Op, DAG, isV9);
2650 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
2651 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002652 case ISD::ADDC:
2653 case ISD::ADDE:
2654 case ISD::SUBC:
2655 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002656 }
2657}
2658
2659MachineBasicBlock *
2660SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00002661 MachineBasicBlock *BB) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00002662 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
2663 unsigned BROpcode;
2664 unsigned CC;
Dale Johannesen215a9252009-02-13 02:31:35 +00002665 DebugLoc dl = MI->getDebugLoc();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002666 // Figure out the conditional branch opcode to use for this select_cc.
2667 switch (MI->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002668 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00002669 case SP::SELECT_CC_Int_ICC:
2670 case SP::SELECT_CC_FP_ICC:
2671 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002672 case SP::SELECT_CC_QFP_ICC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00002673 BROpcode = SP::BCOND;
2674 break;
2675 case SP::SELECT_CC_Int_FCC:
2676 case SP::SELECT_CC_FP_FCC:
2677 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002678 case SP::SELECT_CC_QFP_FCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00002679 BROpcode = SP::FBCOND;
2680 break;
2681 }
2682
2683 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002684
Chris Lattner0a1762e2008-03-17 03:21:36 +00002685 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2686 // control-flow pattern. The incoming instruction knows the destination vreg
2687 // to set, the condition code register to branch on, the true/false values to
2688 // select between, and a branch opcode to use.
2689 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00002690 MachineFunction::iterator It = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002691 ++It;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002692
Chris Lattner0a1762e2008-03-17 03:21:36 +00002693 // thisMBB:
2694 // ...
2695 // TrueVal = ...
2696 // [f]bCC copy1MBB
2697 // fallthrough --> copy0MBB
2698 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002699 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00002700 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2701 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00002702 F->insert(It, copy0MBB);
2703 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00002704
2705 // Transfer the remainder of BB and its successor edges to sinkMBB.
2706 sinkMBB->splice(sinkMBB->begin(), BB,
2707 llvm::next(MachineBasicBlock::iterator(MI)),
2708 BB->end());
2709 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
2710
2711 // Add the true and fallthrough blocks as its successors.
2712 BB->addSuccessor(copy0MBB);
2713 BB->addSuccessor(sinkMBB);
2714
Dale Johannesen215a9252009-02-13 02:31:35 +00002715 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002716
Chris Lattner0a1762e2008-03-17 03:21:36 +00002717 // copy0MBB:
2718 // %FalseValue = ...
2719 // # fallthrough to sinkMBB
2720 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002721
Chris Lattner0a1762e2008-03-17 03:21:36 +00002722 // Update machine-CFG edges
2723 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002724
Chris Lattner0a1762e2008-03-17 03:21:36 +00002725 // sinkMBB:
2726 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2727 // ...
2728 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00002729 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattner0a1762e2008-03-17 03:21:36 +00002730 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
2731 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002732
Dan Gohman34396292010-07-06 20:24:04 +00002733 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00002734 return BB;
2735}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00002736
2737//===----------------------------------------------------------------------===//
2738// Sparc Inline Assembly Support
2739//===----------------------------------------------------------------------===//
2740
2741/// getConstraintType - Given a constraint letter, return the type of
2742/// constraint it is for this target.
2743SparcTargetLowering::ConstraintType
2744SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
2745 if (Constraint.size() == 1) {
2746 switch (Constraint[0]) {
2747 default: break;
2748 case 'r': return C_RegisterClass;
2749 }
2750 }
2751
2752 return TargetLowering::getConstraintType(Constraint);
2753}
2754
2755std::pair<unsigned, const TargetRegisterClass*>
2756SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00002757 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00002758 if (Constraint.size() == 1) {
2759 switch (Constraint[0]) {
2760 case 'r':
Craig Topperabadc662012-04-20 06:31:50 +00002761 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00002762 }
2763 }
2764
2765 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2766}
2767
Dan Gohman2fe6bee2008-10-18 02:06:02 +00002768bool
2769SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2770 // The Sparc target isn't yet aware of offsets.
2771 return false;
2772}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002773
2774void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
2775 SmallVectorImpl<SDValue>& Results,
2776 SelectionDAG &DAG) const {
2777
2778 SDLoc dl(N);
2779
2780 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
2781
2782 switch (N->getOpcode()) {
2783 default:
2784 llvm_unreachable("Do not know how to custom type legalize this operation!");
2785
2786 case ISD::FP_TO_SINT:
2787 case ISD::FP_TO_UINT:
2788 // Custom lower only if it involves f128 or i64.
2789 if (N->getOperand(0).getValueType() != MVT::f128
2790 || N->getValueType(0) != MVT::i64)
2791 return;
2792 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
2793 ? RTLIB::FPTOSINT_F128_I64
2794 : RTLIB::FPTOUINT_F128_I64);
2795
2796 Results.push_back(LowerF128Op(SDValue(N, 0),
2797 DAG,
2798 getLibcallName(libCall),
2799 1));
2800 return;
2801
2802 case ISD::SINT_TO_FP:
2803 case ISD::UINT_TO_FP:
2804 // Custom lower only if it involves f128 or i64.
2805 if (N->getValueType(0) != MVT::f128
2806 || N->getOperand(0).getValueType() != MVT::i64)
2807 return;
2808
2809 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
2810 ? RTLIB::SINTTOFP_I64_F128
2811 : RTLIB::UINTTOFP_I64_F128);
2812
2813 Results.push_back(LowerF128Op(SDValue(N, 0),
2814 DAG,
2815 getLibcallName(libCall),
2816 1));
2817 return;
2818 }
2819}