blob: 527d052258dcc4e5e4654e1e6da04f97c94a104f [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"
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +000016#include "MCTargetDesc/SparcMCExpr.h"
Dan Gohman31ae5862010-04-17 14:41:14 +000017#include "SparcMachineFunctionInfo.h"
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +000018#include "SparcRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "SparcTargetMachine.h"
Venkatraman Govindarajufd5c1f92014-01-29 04:51:35 +000020#include "SparcTargetObjectFile.h"
Chris Lattner49b269d2008-03-17 05:41:48 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/Module.h"
Torok Edwin56d06592009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000032using namespace llvm;
33
Chris Lattner49b269d2008-03-17 05:41:48 +000034
35//===----------------------------------------------------------------------===//
36// Calling Convention Implementation
37//===----------------------------------------------------------------------===//
38
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000039static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
40 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
41 ISD::ArgFlagsTy &ArgFlags, CCState &State)
42{
43 assert (ArgFlags.isSRet());
44
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000045 // Assign SRet argument.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000046 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
47 0,
48 LocVT, LocInfo));
49 return true;
50}
51
James Y Knight3994be82015-08-10 19:11:39 +000052static bool CC_Sparc_Assign_Split_64(unsigned &ValNo, MVT &ValVT,
53 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
54 ISD::ArgFlagsTy &ArgFlags, CCState &State)
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000055{
Craig Topper840beec2014-04-04 05:16:06 +000056 static const MCPhysReg RegList[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000057 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
58 };
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000059 // Try to get first reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000060 if (unsigned Reg = State.AllocateReg(RegList)) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000061 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
62 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000063 // Assign whole thing in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000064 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
65 State.AllocateStack(8,4),
66 LocVT, LocInfo));
67 return true;
68 }
69
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000070 // Try to get second reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000071 if (unsigned Reg = State.AllocateReg(RegList))
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000072 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
73 else
74 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
75 State.AllocateStack(4,4),
76 LocVT, LocInfo));
77 return true;
78}
79
James Y Knight3994be82015-08-10 19:11:39 +000080static bool CC_Sparc_Assign_Ret_Split_64(unsigned &ValNo, MVT &ValVT,
81 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
82 ISD::ArgFlagsTy &ArgFlags, CCState &State)
83{
84 static const MCPhysReg RegList[] = {
85 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
86 };
87
88 // Try to get first reg.
89 if (unsigned Reg = State.AllocateReg(RegList))
90 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
91 else
92 return false;
93
94 // Try to get second reg.
95 if (unsigned Reg = State.AllocateReg(RegList))
96 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
97 else
98 return false;
99
100 return true;
101}
102
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000103// Allocate a full-sized argument for the 64-bit ABI.
104static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
105 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
106 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000107 assert((LocVT == MVT::f32 || LocVT == MVT::f128
108 || LocVT.getSizeInBits() == 64) &&
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000109 "Can't handle non-64 bits locations");
110
111 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000112 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
113 unsigned alignment = (LocVT == MVT::f128) ? 16 : 8;
114 unsigned Offset = State.AllocateStack(size, alignment);
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000115 unsigned Reg = 0;
116
117 if (LocVT == MVT::i64 && Offset < 6*8)
118 // Promote integers to %i0-%i5.
119 Reg = SP::I0 + Offset/8;
120 else if (LocVT == MVT::f64 && Offset < 16*8)
121 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
122 Reg = SP::D0 + Offset/8;
123 else if (LocVT == MVT::f32 && Offset < 16*8)
124 // Promote floats to %f1, %f3, ...
125 Reg = SP::F1 + Offset/4;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000126 else if (LocVT == MVT::f128 && Offset < 16*8)
127 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
128 Reg = SP::Q0 + Offset/16;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000129
130 // Promote to register when possible, otherwise use the stack slot.
131 if (Reg) {
132 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
133 return true;
134 }
135
136 // This argument goes on the stack in an 8-byte slot.
137 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
138 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
139 if (LocVT == MVT::f32)
140 Offset += 4;
141
142 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
143 return true;
144}
145
146// Allocate a half-sized argument for the 64-bit ABI.
147//
148// This is used when passing { float, int } structs by value in registers.
149static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
150 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
151 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
152 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
153 unsigned Offset = State.AllocateStack(4, 4);
154
155 if (LocVT == MVT::f32 && Offset < 16*8) {
156 // Promote floats to %f0-%f31.
157 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
158 LocVT, LocInfo));
159 return true;
160 }
161
162 if (LocVT == MVT::i32 && Offset < 6*8) {
163 // Promote integers to %i0-%i5, using half the register.
164 unsigned Reg = SP::I0 + Offset/8;
165 LocVT = MVT::i64;
166 LocInfo = CCValAssign::AExt;
167
168 // Set the Custom bit if this i32 goes in the high bits of a register.
169 if (Offset % 8 == 0)
170 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
171 LocVT, LocInfo));
172 else
173 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
174 return true;
175 }
176
177 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
178 return true;
179}
180
Chris Lattner49b269d2008-03-17 05:41:48 +0000181#include "SparcGenCallingConv.inc"
182
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000183// The calling conventions in SparcCallingConv.td are described in terms of the
184// callee's register window. This function translates registers to the
185// corresponding caller window %o register.
186static unsigned toCallerWindow(unsigned Reg) {
187 assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7 && "Unexpected enum");
188 if (Reg >= SP::I0 && Reg <= SP::I7)
189 return Reg - SP::I0 + SP::O0;
190 return Reg;
191}
192
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000193SDValue
194SparcTargetLowering::LowerReturn(SDValue Chain,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000195 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000196 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000197 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000198 SDLoc DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000199 if (Subtarget->is64Bit())
200 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
201 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
202}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000203
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000204SDValue
205SparcTargetLowering::LowerReturn_32(SDValue Chain,
206 CallingConv::ID CallConv, bool IsVarArg,
207 const SmallVectorImpl<ISD::OutputArg> &Outs,
208 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000209 SDLoc DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000210 MachineFunction &MF = DAG.getMachineFunction();
211
Chris Lattner49b269d2008-03-17 05:41:48 +0000212 // CCValAssign - represent the assignment of the return value to locations.
213 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000214
Chris Lattner49b269d2008-03-17 05:41:48 +0000215 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000216 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
217 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000218
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000219 // Analyze return values.
220 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000221
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000222 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000223 SmallVector<SDValue, 4> RetOps(1, Chain);
224 // Make room for the return address offset.
225 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000226
227 // Copy the result values into the output registers.
James Y Knight3994be82015-08-10 19:11:39 +0000228 for (unsigned i = 0, realRVLocIdx = 0;
229 i != RVLocs.size();
230 ++i, ++realRVLocIdx) {
Chris Lattner49b269d2008-03-17 05:41:48 +0000231 CCValAssign &VA = RVLocs[i];
232 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000233
James Y Knight3994be82015-08-10 19:11:39 +0000234 SDValue Arg = OutVals[realRVLocIdx];
235
236 if (VA.needsCustom()) {
237 assert(VA.getLocVT() == MVT::v2i32);
238 // Legalize ret v2i32 -> ret 2 x i32 (Basically: do what would
239 // happen by default if this wasn't a legal type)
240
241 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
242 Arg,
243 DAG.getConstant(0, DL, getVectorIdxTy(DAG.getDataLayout())));
244 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
245 Arg,
246 DAG.getConstant(1, DL, getVectorIdxTy(DAG.getDataLayout())));
247
248 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part0, Flag);
249 Flag = Chain.getValue(1);
250 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
251 VA = RVLocs[++i]; // skip ahead to next loc
252 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part1,
253 Flag);
254 } else
255 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000256
Chris Lattner49b269d2008-03-17 05:41:48 +0000257 // Guarantee that all emitted copies are stuck together with flags.
258 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000259 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000260 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000261
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000262 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000263 // If the function returns a struct, copy the SRetReturnReg to I0
264 if (MF.getFunction()->hasStructRetAttr()) {
265 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
266 unsigned Reg = SFI->getSRetReturnReg();
267 if (!Reg)
268 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +0000269 auto PtrVT = getPointerTy(DAG.getDataLayout());
270 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, PtrVT);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000271 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000272 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +0000273 RetOps.push_back(DAG.getRegister(SP::I0, PtrVT));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000274 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000275 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000276
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000277 RetOps[0] = Chain; // Update chain.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000278 RetOps[1] = DAG.getConstant(RetAddrOffset, DL, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000279
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000280 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000281 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000282 RetOps.push_back(Flag);
283
Craig Topper48d114b2014-04-26 18:35:24 +0000284 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000285}
286
287// Lower return values for the 64-bit ABI.
288// Return values are passed the exactly the same way as function arguments.
289SDValue
290SparcTargetLowering::LowerReturn_64(SDValue Chain,
291 CallingConv::ID CallConv, bool IsVarArg,
292 const SmallVectorImpl<ISD::OutputArg> &Outs,
293 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000294 SDLoc DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000295 // CCValAssign - represent the assignment of the return value to locations.
296 SmallVector<CCValAssign, 16> RVLocs;
297
298 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000299 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
300 *DAG.getContext());
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000301
302 // Analyze return values.
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +0000303 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000304
305 SDValue Flag;
306 SmallVector<SDValue, 4> RetOps(1, Chain);
307
308 // The second operand on the return instruction is the return address offset.
309 // The return address is always %i7+8 with the 64-bit ABI.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000310 RetOps.push_back(DAG.getConstant(8, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000311
312 // Copy the result values into the output registers.
313 for (unsigned i = 0; i != RVLocs.size(); ++i) {
314 CCValAssign &VA = RVLocs[i];
315 assert(VA.isRegLoc() && "Can only return in registers!");
316 SDValue OutVal = OutVals[i];
317
318 // Integer return values must be sign or zero extended by the callee.
319 switch (VA.getLocInfo()) {
Lang Hames06234ec2014-01-14 19:56:36 +0000320 case CCValAssign::Full: break;
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000321 case CCValAssign::SExt:
322 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
323 break;
324 case CCValAssign::ZExt:
325 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
326 break;
327 case CCValAssign::AExt:
328 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000329 break;
Lang Hames06234ec2014-01-14 19:56:36 +0000330 default:
331 llvm_unreachable("Unknown loc info!");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000332 }
333
334 // The custom bit on an i32 return value indicates that it should be passed
335 // in the high bits of the register.
336 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
337 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000338 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000339
340 // The next value may go in the low bits of the same register.
341 // Handle both at once.
342 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
343 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
344 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
345 // Skip the next value, it's already done.
346 ++i;
347 }
348 }
349
350 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
351
352 // Guarantee that all emitted copies are stuck together with flags.
353 Flag = Chain.getValue(1);
354 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
355 }
356
357 RetOps[0] = Chain; // Update chain.
358
359 // Add the flag if we have it.
360 if (Flag.getNode())
361 RetOps.push_back(Flag);
362
Craig Topper48d114b2014-04-26 18:35:24 +0000363 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Chris Lattner49b269d2008-03-17 05:41:48 +0000364}
365
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000366SDValue SparcTargetLowering::
367LowerFormalArguments(SDValue Chain,
368 CallingConv::ID CallConv,
369 bool IsVarArg,
370 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000371 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000372 SelectionDAG &DAG,
373 SmallVectorImpl<SDValue> &InVals) const {
374 if (Subtarget->is64Bit())
375 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
376 DL, DAG, InVals);
377 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
378 DL, DAG, InVals);
379}
380
381/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000382/// passed in either one or two GPRs, including FP values. TODO: we should
383/// pass FP values in FP registers for fastcc functions.
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000384SDValue SparcTargetLowering::
385LowerFormalArguments_32(SDValue Chain,
386 CallingConv::ID CallConv,
387 bool isVarArg,
388 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000389 SDLoc dl,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000390 SelectionDAG &DAG,
391 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000392 MachineFunction &MF = DAG.getMachineFunction();
393 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000394 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000395
396 // Assign locations to all of the incoming arguments.
397 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000398 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
399 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000400 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000401
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000402 const unsigned StackOffset = 92;
James Y Knight33beb242015-12-15 19:23:12 +0000403 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000404
Reid Kleckner79418562014-05-09 22:32:13 +0000405 unsigned InIdx = 0;
406 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000407 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000408
Reid Kleckner79418562014-05-09 22:32:13 +0000409 if (Ins[InIdx].Flags.isSRet()) {
410 if (InIdx != 0)
411 report_fatal_error("sparc only supports sret on the first parameter");
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000412 // Get SRet from [%fp+64].
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000413 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
414 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
415 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
416 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000417 false, false, false, 0);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000418 InVals.push_back(Arg);
419 continue;
420 }
421
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000422 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000423 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000424 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
425
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000426 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
427 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
428 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000429
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000430 assert(i+1 < e);
431 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000432
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000433 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000434 if (NextVA.isMemLoc()) {
435 int FrameIdx = MF.getFrameInfo()->
436 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000437 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000438 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
439 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000440 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000441 } else {
442 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000443 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000444 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000445 }
James Y Knight33beb242015-12-15 19:23:12 +0000446
447 if (IsLittleEndian)
448 std::swap(LoVal, HiVal);
449
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000450 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000451 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000452 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000453 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000454 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000455 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000456 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
457 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
458 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
459 if (VA.getLocVT() == MVT::f32)
460 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
461 else if (VA.getLocVT() != MVT::i32) {
462 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
463 DAG.getValueType(VA.getLocVT()));
464 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
465 }
466 InVals.push_back(Arg);
467 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000468 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000469
470 assert(VA.isMemLoc());
471
472 unsigned Offset = VA.getLocMemOffset()+StackOffset;
Mehdi Amini44ede332015-07-09 02:09:04 +0000473 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000474
475 if (VA.needsCustom()) {
Hans Wennborg1f094852016-04-11 20:35:41 +0000476 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000477 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000478 if (Offset % 8 == 0) {
479 int FI = MF.getFrameInfo()->CreateFixedObject(8,
480 Offset,
481 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000482 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000483 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
484 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000485 false,false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000486 InVals.push_back(Load);
487 continue;
488 }
489
490 int FI = MF.getFrameInfo()->CreateFixedObject(4,
491 Offset,
492 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000493 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000494 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
495 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000496 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000497 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
498 Offset+4,
499 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000500 SDValue FIPtr2 = DAG.getFrameIndex(FI2, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000501
502 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
503 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000504 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000505
James Y Knight33beb242015-12-15 19:23:12 +0000506 if (IsLittleEndian)
507 std::swap(LoVal, HiVal);
508
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000509 SDValue WholeValue =
510 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000511 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000512 InVals.push_back(WholeValue);
513 continue;
514 }
515
516 int FI = MF.getFrameInfo()->CreateFixedObject(4,
517 Offset,
518 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000519 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000520 SDValue Load ;
521 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
522 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
523 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000524 false, false, false, 0);
James Y Knight33beb242015-12-15 19:23:12 +0000525 } else if (VA.getValVT() == MVT::f128) {
526 report_fatal_error("SPARCv8 does not handle f128 in calls; "
527 "pass indirectly");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000528 } else {
James Y Knight33beb242015-12-15 19:23:12 +0000529 // We shouldn't see any other value types here.
James Y Knight99fcb722015-12-15 23:07:16 +0000530 llvm_unreachable("Unexpected ValVT encountered in frame lowering.");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000531 }
532 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000533 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000534
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000535 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000536 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000537 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
538 unsigned Reg = SFI->getSRetReturnReg();
539 if (!Reg) {
540 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
541 SFI->setSRetReturnReg(Reg);
542 }
543 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
544 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
545 }
546
Chris Lattner49b269d2008-03-17 05:41:48 +0000547 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000548 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +0000549 static const MCPhysReg ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000550 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
551 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000552 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs);
Craig Topper840beec2014-04-04 05:16:06 +0000553 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000554 unsigned ArgOffset = CCInfo.getNextStackOffset();
555 if (NumAllocated == 6)
556 ArgOffset += StackOffset;
557 else {
558 assert(!ArgOffset);
559 ArgOffset = 68+4*NumAllocated;
560 }
561
Chris Lattner49b269d2008-03-17 05:41:48 +0000562 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000563 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000564
Eli Friedmanbe853b72009-07-19 19:53:46 +0000565 std::vector<SDValue> OutChains;
566
Chris Lattner49b269d2008-03-17 05:41:48 +0000567 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
568 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
569 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000570 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000571
David Greene1fbe0542009-11-12 20:49:22 +0000572 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Cheng0664a672010-07-03 00:40:23 +0000573 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000574 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000575
Chris Lattner676c61d2010-09-21 18:41:36 +0000576 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
577 MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000578 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000579 ArgOffset += 4;
580 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000581
582 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000583 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +0000584 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Eli Friedmanbe853b72009-07-19 19:53:46 +0000585 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000586 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000587
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000588 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000589}
590
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000591// Lower formal arguments for the 64 bit ABI.
592SDValue SparcTargetLowering::
593LowerFormalArguments_64(SDValue Chain,
594 CallingConv::ID CallConv,
595 bool IsVarArg,
596 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000597 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000598 SelectionDAG &DAG,
599 SmallVectorImpl<SDValue> &InVals) const {
600 MachineFunction &MF = DAG.getMachineFunction();
601
602 // Analyze arguments according to CC_Sparc64.
603 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000604 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
605 *DAG.getContext());
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000606 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
607
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000608 // The argument array begins at %fp+BIAS+128, after the register save area.
609 const unsigned ArgArea = 128;
610
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000611 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
612 CCValAssign &VA = ArgLocs[i];
613 if (VA.isRegLoc()) {
614 // This argument is passed in a register.
615 // All integer register arguments are promoted by the caller to i64.
616
617 // Create a virtual register for the promoted live-in value.
618 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
619 getRegClassFor(VA.getLocVT()));
620 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
621
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000622 // Get the high bits for i32 struct elements.
623 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
624 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000625 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000626
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000627 // The caller promoted the argument, so insert an Assert?ext SDNode so we
628 // won't promote the value again in this function.
629 switch (VA.getLocInfo()) {
630 case CCValAssign::SExt:
631 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
632 DAG.getValueType(VA.getValVT()));
633 break;
634 case CCValAssign::ZExt:
635 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
636 DAG.getValueType(VA.getValVT()));
637 break;
638 default:
639 break;
640 }
641
642 // Truncate the register down to the argument type.
643 if (VA.isExtInLoc())
644 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
645
646 InVals.push_back(Arg);
647 continue;
648 }
649
650 // The registers are exhausted. This argument was passed on the stack.
651 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000652 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
653 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000654 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000655 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
656 // Adjust offset for extended arguments, SPARC is big-endian.
657 // The caller will have written the full slot with extended bytes, but we
658 // prefer our own extending loads.
659 if (VA.isExtInLoc())
660 Offset += 8 - ValSize;
661 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000662 InVals.push_back(DAG.getLoad(
663 VA.getValVT(), DL, Chain,
664 DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
Alex Lorenze40c8a22015-08-11 23:09:45 +0000665 MachinePointerInfo::getFixedStack(MF, FI), false, false, false, 0));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000666 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000667
668 if (!IsVarArg)
669 return Chain;
670
671 // This function takes variable arguments, some of which may have been passed
672 // in registers %i0-%i5. Variable floating point arguments are never passed
673 // in floating point registers. They go on %i0-%i5 or on the stack like
674 // integer arguments.
675 //
676 // The va_start intrinsic needs to know the offset to the first variable
677 // argument.
678 unsigned ArgOffset = CCInfo.getNextStackOffset();
679 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
680 // Skip the 128 bytes of register save area.
681 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
682 Subtarget->getStackPointerBias());
683
684 // Save the variable arguments that were passed in registers.
685 // The caller is required to reserve stack space for 6 arguments regardless
686 // of how many arguments were actually passed.
687 SmallVector<SDValue, 8> OutChains;
688 for (; ArgOffset < 6*8; ArgOffset += 8) {
689 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
690 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
691 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000692 auto PtrVT = getPointerTy(MF.getDataLayout());
Alex Lorenze40c8a22015-08-11 23:09:45 +0000693 OutChains.push_back(DAG.getStore(
694 Chain, DL, VArg, DAG.getFrameIndex(FI, PtrVT),
695 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000696 }
697
698 if (!OutChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000699 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000700
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000701 return Chain;
702}
703
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000704SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000705SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000706 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000707 if (Subtarget->is64Bit())
708 return LowerCall_64(CLI, InVals);
709 return LowerCall_32(CLI, InVals);
710}
711
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000712static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
713 ImmutableCallSite *CS) {
714 if (CS)
715 return CS->hasFnAttr(Attribute::ReturnsTwice);
716
Craig Topper062a2ba2014-04-25 05:30:21 +0000717 const Function *CalleeFn = nullptr;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000718 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
719 CalleeFn = dyn_cast<Function>(G->getGlobal());
720 } else if (ExternalSymbolSDNode *E =
721 dyn_cast<ExternalSymbolSDNode>(Callee)) {
722 const Function *Fn = DAG.getMachineFunction().getFunction();
723 const Module *M = Fn->getParent();
724 const char *CalleeName = E->getSymbol();
725 CalleeFn = M->getFunction(CalleeName);
726 }
727
728 if (!CalleeFn)
729 return false;
730 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
731}
732
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000733// Lower a call for the 32-bit ABI.
734SDValue
735SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
736 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000737 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000738 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000739 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
740 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
741 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000742 SDValue Chain = CLI.Chain;
743 SDValue Callee = CLI.Callee;
744 bool &isTailCall = CLI.IsTailCall;
745 CallingConv::ID CallConv = CLI.CallConv;
746 bool isVarArg = CLI.IsVarArg;
747
Evan Cheng67a69dd2010-01-27 00:07:07 +0000748 // Sparc target does not yet support tail call optimization.
749 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000750
Chris Lattner7d4152b2008-03-17 06:58:37 +0000751 // Analyze operands of the call, assigning locations to each operand.
752 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000753 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
754 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000755 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000756
Chris Lattner7d4152b2008-03-17 06:58:37 +0000757 // Get the size of the outgoing arguments stack space requirement.
758 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000759
Chris Lattner49b269d2008-03-17 05:41:48 +0000760 // Keep stack frames 8-byte aligned.
761 ArgsSize = (ArgsSize+7) & ~7;
762
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000763 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
764
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000765 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000766 SmallVector<SDValue, 8> ByValArgs;
767 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
768 ISD::ArgFlagsTy Flags = Outs[i].Flags;
769 if (!Flags.isByVal())
770 continue;
771
772 SDValue Arg = OutVals[i];
773 unsigned Size = Flags.getByValSize();
774 unsigned Align = Flags.getByValAlign();
775
776 int FI = MFI->CreateStackObject(Size, Align, false);
Mehdi Amini44ede332015-07-09 02:09:04 +0000777 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000778 SDValue SizeNode = DAG.getConstant(Size, dl, MVT::i32);
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000779
780 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000781 false, // isVolatile,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000782 (Size <= 32), // AlwaysInline if size <= 32,
783 false, // isTailCall
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000784 MachinePointerInfo(), MachinePointerInfo());
785 ByValArgs.push_back(FIPtr);
786 }
787
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000788 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000789 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000790
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000791 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
792 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000793
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000794 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000795 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000796 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000797 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000798 i != e;
799 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000800 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000801 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000802
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000803 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
804
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000805 // Use local copy if it is a byval arg.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000806 if (Flags.isByVal())
807 Arg = ByValArgs[byvalArgIdx++];
808
Chris Lattner7d4152b2008-03-17 06:58:37 +0000809 // Promote the value if needed.
810 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000811 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000812 case CCValAssign::Full: break;
813 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000814 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000815 break;
816 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000817 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000818 break;
819 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000820 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
821 break;
822 case CCValAssign::BCvt:
823 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000824 break;
825 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000826
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000827 if (Flags.isSRet()) {
828 assert(VA.needsCustom());
829 // store SRet argument in %sp+64
830 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000831 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000832 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
833 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
834 MachinePointerInfo(),
835 false, false, 0));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000836 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000837 continue;
838 }
839
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000840 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000841 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000842
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000843 if (VA.isMemLoc()) {
844 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000845 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000846 if (Offset % 8 == 0) {
847 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000848 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000849 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
850 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
851 MachinePointerInfo(),
852 false, false, 0));
853 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000854 }
855 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000856
James Y Knight3994be82015-08-10 19:11:39 +0000857 if (VA.getLocVT() == MVT::f64) {
858 // Move from the float value from float registers into the
859 // integer registers.
860
James Y Knight692e0372015-10-09 21:36:19 +0000861 // TODO: The f64 -> v2i32 conversion is super-inefficient for
862 // constants: it sticks them in the constant pool, then loads
863 // to a fp register, then stores to temp memory, then loads to
864 // integer registers.
James Y Knight3994be82015-08-10 19:11:39 +0000865 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
866 }
867
868 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
869 Arg,
870 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
871 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
872 Arg,
873 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000874
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000875 if (VA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000876 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000877 assert(i+1 != e);
878 CCValAssign &NextVA = ArgLocs[++i];
879 if (NextVA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000880 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000881 } else {
James Y Knight3994be82015-08-10 19:11:39 +0000882 // Store the second part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000883 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
884 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000885 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000886 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000887 MemOpChains.push_back(DAG.getStore(Chain, dl, Part1, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000888 MachinePointerInfo(),
889 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000890 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000891 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000892 unsigned Offset = VA.getLocMemOffset() + StackOffset;
James Y Knight3994be82015-08-10 19:11:39 +0000893 // Store the first part.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000894 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000895 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000896 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000897 MemOpChains.push_back(DAG.getStore(Chain, dl, Part0, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000898 MachinePointerInfo(),
899 false, false, 0));
James Y Knight3994be82015-08-10 19:11:39 +0000900 // Store the second part.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000901 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000902 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000903 MemOpChains.push_back(DAG.getStore(Chain, dl, Part1, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000904 MachinePointerInfo(),
905 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000906 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000907 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000908 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000909
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000910 // Arguments that can be passed on register must be kept at
911 // RegsToPass vector
912 if (VA.isRegLoc()) {
913 if (VA.getLocVT() != MVT::f32) {
914 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
915 continue;
916 }
917 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
918 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
919 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000920 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000921
922 assert(VA.isMemLoc());
923
924 // Create a store off the stack pointer for this argument.
925 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000926 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
927 dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000928 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
929 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
930 MachinePointerInfo(),
931 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000932 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000933
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000934
Chris Lattner49b269d2008-03-17 05:41:48 +0000935 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000936 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000937 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000938
939 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000940 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000941 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000942 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000943 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000944 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000945 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000946 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000947 InFlag = Chain.getValue(1);
948 }
949
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000950 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000951 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000952
Chris Lattner49b269d2008-03-17 05:41:48 +0000953 // If the callee is a GlobalAddress node (quite common, every direct call is)
954 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000955 // Likewise ExternalSymbol -> TargetExternalSymbol.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000956 unsigned TF = ((getTargetMachine().getRelocationModel() == Reloc::PIC_)
957 ? SparcMCExpr::VK_Sparc_WPLT30 : 0);
Chris Lattner49b269d2008-03-17 05:41:48 +0000958 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000959 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000960 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000961 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000962
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000963 // Returns a chain & a flag for retval copy to use
964 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
965 SmallVector<SDValue, 8> Ops;
966 Ops.push_back(Chain);
967 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000968 if (hasStructRetAttr)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000969 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000970 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
971 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
972 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000973
974 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000975 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000976 const uint32_t *Mask =
977 ((hasReturnsTwice)
978 ? TRI->getRTCallPreservedMask(CallConv)
979 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000980 assert(Mask && "Missing call preserved mask for calling convention");
981 Ops.push_back(DAG.getRegisterMask(Mask));
982
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000983 if (InFlag.getNode())
984 Ops.push_back(InFlag);
985
Craig Topper48d114b2014-04-26 18:35:24 +0000986 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
Chris Lattner49b269d2008-03-17 05:41:48 +0000987 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000988
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000989 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
990 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000991 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000992
Chris Lattnerdb26db22008-03-17 06:01:07 +0000993 // Assign locations to each value returned by this call.
994 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000995 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
996 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000997
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000998 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000999
Chris Lattnerdb26db22008-03-17 06:01:07 +00001000 // Copy all of the result registers out of their specified physreg.
1001 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Nirav Dave29938542016-02-26 18:55:22 +00001002 if (RVLocs[i].getLocVT() == MVT::v2i32) {
1003 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
1004 SDValue Lo = DAG.getCopyFromReg(
1005 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InFlag);
1006 Chain = Lo.getValue(1);
1007 InFlag = Lo.getValue(2);
1008 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
1009 DAG.getConstant(0, dl, MVT::i32));
1010 SDValue Hi = DAG.getCopyFromReg(
1011 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InFlag);
1012 Chain = Hi.getValue(1);
1013 InFlag = Hi.getValue(2);
1014 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
1015 DAG.getConstant(1, dl, MVT::i32));
1016 InVals.push_back(Vec);
1017 } else {
1018 Chain =
1019 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1020 RVLocs[i].getValVT(), InFlag)
1021 .getValue(1);
1022 InFlag = Chain.getValue(2);
1023 InVals.push_back(Chain.getValue(0));
1024 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001025 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001026
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001027 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +00001028}
1029
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001030// This functions returns true if CalleeName is a ABI function that returns
1031// a long double (fp128).
1032static bool isFP128ABICall(const char *CalleeName)
1033{
1034 static const char *const ABICalls[] =
1035 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
1036 "_Q_sqrt", "_Q_neg",
1037 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001038 "_Q_lltoq", "_Q_ulltoq",
Craig Topper062a2ba2014-04-25 05:30:21 +00001039 nullptr
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001040 };
Craig Topper062a2ba2014-04-25 05:30:21 +00001041 for (const char * const *I = ABICalls; *I != nullptr; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001042 if (strcmp(CalleeName, *I) == 0)
1043 return true;
1044 return false;
1045}
1046
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001047unsigned
1048SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
1049{
Craig Topper062a2ba2014-04-25 05:30:21 +00001050 const Function *CalleeFn = nullptr;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001051 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1052 CalleeFn = dyn_cast<Function>(G->getGlobal());
1053 } else if (ExternalSymbolSDNode *E =
1054 dyn_cast<ExternalSymbolSDNode>(Callee)) {
1055 const Function *Fn = DAG.getMachineFunction().getFunction();
1056 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001057 const char *CalleeName = E->getSymbol();
1058 CalleeFn = M->getFunction(CalleeName);
1059 if (!CalleeFn && isFP128ABICall(CalleeName))
1060 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001061 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001062
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001063 if (!CalleeFn)
1064 return 0;
1065
Joerg Sonnenberger72128092015-10-21 20:05:01 +00001066 // It would be nice to check for the sret attribute on CalleeFn here,
1067 // but since it is not part of the function type, any check will misfire.
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001068
Chris Lattner229907c2011-07-18 04:54:35 +00001069 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
1070 Type *ElementTy = Ty->getElementType();
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001071 return DAG.getDataLayout().getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001072}
Chris Lattner49b269d2008-03-17 05:41:48 +00001073
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001074
1075// Fixup floating point arguments in the ... part of a varargs call.
1076//
1077// The SPARC v9 ABI requires that floating point arguments are treated the same
1078// as integers when calling a varargs function. This does not apply to the
1079// fixed arguments that are part of the function's prototype.
1080//
1081// This function post-processes a CCValAssign array created by
1082// AnalyzeCallOperands().
1083static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1084 ArrayRef<ISD::OutputArg> Outs) {
1085 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1086 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001087 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001088 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1089 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001090 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001091 continue;
1092 // The fixed arguments to a varargs function still go in FP registers.
1093 if (Outs[VA.getValNo()].IsFixed)
1094 continue;
1095
1096 // This floating point argument should be reassigned.
1097 CCValAssign NewVA;
1098
1099 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001100 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1101 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1102 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001103 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1104
1105 if (Offset < 6*8) {
1106 // This argument should go in %i0-%i5.
1107 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001108 if (ValTy == MVT::f64)
1109 // Full register, just bitconvert into i64.
1110 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1111 IReg, MVT::i64, CCValAssign::BCvt);
1112 else {
1113 assert(ValTy == MVT::f128 && "Unexpected type!");
1114 // Full register, just bitconvert into i128 -- We will lower this into
1115 // two i64s in LowerCall_64.
1116 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1117 IReg, MVT::i128, CCValAssign::BCvt);
1118 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001119 } else {
1120 // This needs to go to memory, we're out of integer registers.
1121 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1122 Offset, VA.getLocVT(), VA.getLocInfo());
1123 }
1124 ArgLocs[i] = NewVA;
1125 }
1126}
1127
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001128// Lower a call for the 64-bit ABI.
1129SDValue
1130SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1131 SmallVectorImpl<SDValue> &InVals) const {
1132 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001133 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001134 SDValue Chain = CLI.Chain;
Mehdi Amini44ede332015-07-09 02:09:04 +00001135 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001136
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001137 // Sparc target does not yet support tail call optimization.
1138 CLI.IsTailCall = false;
1139
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001140 // Analyze operands of the call, assigning locations to each operand.
1141 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001142 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1143 *DAG.getContext());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001144 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1145
1146 // Get the size of the outgoing arguments stack space requirement.
1147 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001148 // Called functions expect 6 argument words to exist in the stack frame, used
1149 // or not.
1150 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001151
1152 // Keep stack frames 16-byte aligned.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001153 ArgsSize = alignTo(ArgsSize, 16);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001154
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001155 // Varargs calls require special treatment.
1156 if (CLI.IsVarArg)
1157 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1158
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001159 // Adjust the stack pointer to make room for the arguments.
1160 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1161 // with more than 6 arguments.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001162 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001163 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001164
1165 // Collect the set of registers to pass to the function and their values.
1166 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1167 // instruction.
1168 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1169
1170 // Collect chains from all the memory opeations that copy arguments to the
1171 // stack. They must follow the stack pointer adjustment above and precede the
1172 // call instruction itself.
1173 SmallVector<SDValue, 8> MemOpChains;
1174
1175 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1176 const CCValAssign &VA = ArgLocs[i];
1177 SDValue Arg = CLI.OutVals[i];
1178
1179 // Promote the value if needed.
1180 switch (VA.getLocInfo()) {
1181 default:
1182 llvm_unreachable("Unknown location info!");
1183 case CCValAssign::Full:
1184 break;
1185 case CCValAssign::SExt:
1186 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1187 break;
1188 case CCValAssign::ZExt:
1189 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1190 break;
1191 case CCValAssign::AExt:
1192 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1193 break;
1194 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001195 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1196 // SPARC does not support i128 natively. Lower it into two i64, see below.
1197 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1198 || VA.getLocVT() != MVT::i128)
1199 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001200 break;
1201 }
1202
1203 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001204 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1205 && VA.getLocVT() == MVT::i128) {
1206 // Store and reload into the interger register reg and reg+1.
1207 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1208 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
Mehdi Amini44ede332015-07-09 02:09:04 +00001209 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001210 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001211 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001212 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001213 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001214
1215 // Store to %sp+BIAS+128+Offset
1216 SDValue Store = DAG.getStore(Chain, DL, Arg, HiPtrOff,
1217 MachinePointerInfo(),
1218 false, false, 0);
1219 // Load into Reg and Reg+1
1220 SDValue Hi64 = DAG.getLoad(MVT::i64, DL, Store, HiPtrOff,
1221 MachinePointerInfo(),
1222 false, false, false, 0);
1223 SDValue Lo64 = DAG.getLoad(MVT::i64, DL, Store, LoPtrOff,
1224 MachinePointerInfo(),
1225 false, false, false, 0);
1226 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1227 Hi64));
1228 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1229 Lo64));
1230 continue;
1231 }
1232
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001233 // The custom bit on an i32 return value indicates that it should be
1234 // passed in the high bits of the register.
1235 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1236 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001237 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001238
1239 // The next value may go in the low bits of the same register.
1240 // Handle both at once.
1241 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1242 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1243 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1244 CLI.OutVals[i+1]);
1245 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1246 // Skip the next value, it's already done.
1247 ++i;
1248 }
1249 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001250 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001251 continue;
1252 }
1253
1254 assert(VA.isMemLoc());
1255
1256 // Create a store off the stack pointer for this argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00001257 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001258 // The argument area starts at %fp+BIAS+128 in the callee frame,
1259 // %sp+BIAS+128 in ours.
1260 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1261 Subtarget->getStackPointerBias() +
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001262 128, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001263 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001264 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1265 MachinePointerInfo(),
1266 false, false, 0));
1267 }
1268
1269 // Emit all stores, make sure they occur before the call.
1270 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001271 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001272
1273 // Build a sequence of CopyToReg nodes glued together with token chain and
1274 // glue operands which copy the outgoing args into registers. The InGlue is
1275 // necessary since all emitted instructions must be stuck together in order
1276 // to pass the live physical registers.
1277 SDValue InGlue;
1278 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1279 Chain = DAG.getCopyToReg(Chain, DL,
1280 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1281 InGlue = Chain.getValue(1);
1282 }
1283
1284 // If the callee is a GlobalAddress node (quite common, every direct call is)
1285 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1286 // Likewise ExternalSymbol -> TargetExternalSymbol.
1287 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001288 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001289 unsigned TF = ((getTargetMachine().getRelocationModel() == Reloc::PIC_)
1290 ? SparcMCExpr::VK_Sparc_WPLT30 : 0);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001291 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001292 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001293 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001294 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001295
1296 // Build the operands for the call instruction itself.
1297 SmallVector<SDValue, 8> Ops;
1298 Ops.push_back(Chain);
1299 Ops.push_back(Callee);
1300 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1301 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1302 RegsToPass[i].second.getValueType()));
1303
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001304 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +00001305 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001306 const uint32_t *Mask =
1307 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
Eric Christopher9deb75d2015-03-11 22:42:13 +00001308 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1309 CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001310 assert(Mask && "Missing call preserved mask for calling convention");
1311 Ops.push_back(DAG.getRegisterMask(Mask));
1312
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001313 // Make sure the CopyToReg nodes are glued to the call instruction which
1314 // consumes the registers.
1315 if (InGlue.getNode())
1316 Ops.push_back(InGlue);
1317
1318 // Now the call itself.
1319 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001320 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001321 InGlue = Chain.getValue(1);
1322
1323 // Revert the stack pointer immediately after the call.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001324 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
1325 DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001326 InGlue = Chain.getValue(1);
1327
1328 // Now extract the return values. This is more or less the same as
1329 // LowerFormalArguments_64.
1330
1331 // Assign locations to each value returned by this call.
1332 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001333 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1334 *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001335
1336 // Set inreg flag manually for codegen generated library calls that
1337 // return float.
Craig Topper062a2ba2014-04-25 05:30:21 +00001338 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == nullptr)
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001339 CLI.Ins[0].Flags.setInReg();
1340
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001341 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001342
1343 // Copy all of the result registers out of their specified physreg.
1344 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1345 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001346 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001347
1348 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1349 // reside in the same register in the high and low bits. Reuse the
1350 // CopyFromReg previous node to avoid duplicate copies.
1351 SDValue RV;
1352 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1353 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1354 RV = Chain.getValue(0);
1355
1356 // But usually we'll create a new CopyFromReg for a different register.
1357 if (!RV.getNode()) {
1358 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1359 Chain = RV.getValue(1);
1360 InGlue = Chain.getValue(2);
1361 }
1362
1363 // Get the high bits for i32 struct elements.
1364 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1365 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001366 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001367
1368 // The callee promoted the return value, so insert an Assert?ext SDNode so
1369 // we won't promote the value again in this function.
1370 switch (VA.getLocInfo()) {
1371 case CCValAssign::SExt:
1372 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1373 DAG.getValueType(VA.getValVT()));
1374 break;
1375 case CCValAssign::ZExt:
1376 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1377 DAG.getValueType(VA.getValVT()));
1378 break;
1379 default:
1380 break;
1381 }
1382
1383 // Truncate the register down to the return value type.
1384 if (VA.isExtInLoc())
1385 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1386
1387 InVals.push_back(RV);
1388 }
1389
1390 return Chain;
1391}
1392
Chris Lattner0a1762e2008-03-17 03:21:36 +00001393//===----------------------------------------------------------------------===//
1394// TargetLowering Implementation
1395//===----------------------------------------------------------------------===//
1396
James Y Knight7306cd42016-03-29 19:09:54 +00001397TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1398 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1399 AI->getType()->getPrimitiveSizeInBits() == 32)
1400 return AtomicExpansionKind::None; // Uses xchg instruction
1401
1402 return AtomicExpansionKind::CmpXChg;
1403}
1404
Chris Lattner0a1762e2008-03-17 03:21:36 +00001405/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1406/// condition.
1407static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1408 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001409 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001410 case ISD::SETEQ: return SPCC::ICC_E;
1411 case ISD::SETNE: return SPCC::ICC_NE;
1412 case ISD::SETLT: return SPCC::ICC_L;
1413 case ISD::SETGT: return SPCC::ICC_G;
1414 case ISD::SETLE: return SPCC::ICC_LE;
1415 case ISD::SETGE: return SPCC::ICC_GE;
1416 case ISD::SETULT: return SPCC::ICC_CS;
1417 case ISD::SETULE: return SPCC::ICC_LEU;
1418 case ISD::SETUGT: return SPCC::ICC_GU;
1419 case ISD::SETUGE: return SPCC::ICC_CC;
1420 }
1421}
1422
1423/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1424/// FCC condition.
1425static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1426 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001427 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001428 case ISD::SETEQ:
1429 case ISD::SETOEQ: return SPCC::FCC_E;
1430 case ISD::SETNE:
1431 case ISD::SETUNE: return SPCC::FCC_NE;
1432 case ISD::SETLT:
1433 case ISD::SETOLT: return SPCC::FCC_L;
1434 case ISD::SETGT:
1435 case ISD::SETOGT: return SPCC::FCC_G;
1436 case ISD::SETLE:
1437 case ISD::SETOLE: return SPCC::FCC_LE;
1438 case ISD::SETGE:
1439 case ISD::SETOGE: return SPCC::FCC_GE;
1440 case ISD::SETULT: return SPCC::FCC_UL;
1441 case ISD::SETULE: return SPCC::FCC_ULE;
1442 case ISD::SETUGT: return SPCC::FCC_UG;
1443 case ISD::SETUGE: return SPCC::FCC_UGE;
1444 case ISD::SETUO: return SPCC::FCC_U;
1445 case ISD::SETO: return SPCC::FCC_O;
1446 case ISD::SETONE: return SPCC::FCC_LG;
1447 case ISD::SETUEQ: return SPCC::FCC_UE;
1448 }
1449}
1450
Eric Christopherf5e94062015-01-30 23:46:43 +00001451SparcTargetLowering::SparcTargetLowering(TargetMachine &TM,
1452 const SparcSubtarget &STI)
1453 : TargetLowering(TM), Subtarget(&STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +00001454 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00001455
James Y Knightd966fb62015-08-19 14:47:04 +00001456 // Instructions which use registers as conditionals examine all the
1457 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1458 // matters much whether it's ZeroOrOneBooleanContent, or
1459 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1460 // former.
1461 setBooleanContents(ZeroOrOneBooleanContent);
1462 setBooleanVectorContents(ZeroOrOneBooleanContent);
1463
Chris Lattner0a1762e2008-03-17 03:21:36 +00001464 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001465 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1466 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1467 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001468 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001469 if (Subtarget->is64Bit()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001470 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001471 } else {
1472 // On 32bit sparc, we define a double-register 32bit register
1473 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1474 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1475
1476 // ...but almost all operations must be expanded, so set that as
1477 // the default.
1478 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1479 setOperationAction(Op, MVT::v2i32, Expand);
1480 }
1481 // Truncating/extending stores/loads are also not supported.
1482 for (MVT VT : MVT::integer_vector_valuetypes()) {
1483 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1484 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1485 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1486
1487 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1488 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1489 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1490
1491 setTruncStoreAction(VT, MVT::v2i32, Expand);
1492 setTruncStoreAction(MVT::v2i32, VT, Expand);
1493 }
1494 // However, load and store *are* legal.
1495 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1496 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1497 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1498 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1499
1500 // And we need to promote i64 loads/stores into vector load/store
1501 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1502 setOperationAction(ISD::STORE, MVT::i64, Custom);
1503
1504 // Sadly, this doesn't work:
1505 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1506 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1507 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001508
1509 // Turn FP extload into load/fextend
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001510 for (MVT VT : MVT::fp_valuetypes()) {
1511 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1512 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1513 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001514
Chris Lattner0a1762e2008-03-17 03:21:36 +00001515 // Sparc doesn't have i1 sign extending load
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001516 for (MVT VT : MVT::integer_valuetypes())
1517 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001518
Chris Lattner0a1762e2008-03-17 03:21:36 +00001519 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001520 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001521 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1522 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001523
1524 // Custom legalize GlobalAddress nodes into LO/HI parts.
Mehdi Amini26d48132015-07-24 16:04:22 +00001525 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1526 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1527 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1528 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001529
Chris Lattner0a1762e2008-03-17 03:21:36 +00001530 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001531 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1532 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1533 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001534
1535 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001536 setOperationAction(ISD::UREM, MVT::i32, Expand);
1537 setOperationAction(ISD::SREM, MVT::i32, Expand);
1538 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1539 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001540
Roman Divacky2262cfa2013-10-31 19:22:33 +00001541 // ... nor does SparcV9.
1542 if (Subtarget->is64Bit()) {
1543 setOperationAction(ISD::UREM, MVT::i64, Expand);
1544 setOperationAction(ISD::SREM, MVT::i64, Expand);
1545 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1546 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1547 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001548
1549 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001550 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1551 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001552 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1553 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001554
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001555 // Custom Expand fp<->uint
1556 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1557 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001558 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1559 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001560
Wesley Peck527da1b2010-11-23 03:31:01 +00001561 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1562 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001563
Chris Lattner0a1762e2008-03-17 03:21:36 +00001564 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001565 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1566 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1567 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001568 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1569
Owen Anderson9f944592009-08-11 20:47:22 +00001570 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1571 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1572 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001573 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001574
Chris Lattner0a1762e2008-03-17 03:21:36 +00001575 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001576 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1577 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1578 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1579 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1580 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1581 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001582 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001583
Owen Anderson9f944592009-08-11 20:47:22 +00001584 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1585 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1586 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001587 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001588
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001589 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001590 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1591 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1592 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1593 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001594 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1595 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001596 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1597 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001598 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001599 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001600
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001601 setOperationAction(ISD::CTPOP, MVT::i64,
1602 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001603 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
1604 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1605 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
1606 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1607 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001608 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1609 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001610 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001611 }
1612
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001613 // ATOMICs.
James Y Knight19f6cce2016-04-12 20:18:48 +00001614 // Atomics are only supported on Sparcv9. (32bit atomics are also
1615 // supported by the Leon sparcv8 variant, but we don't support that
1616 // yet.)
1617 if (Subtarget->isV9())
1618 setMaxAtomicSizeInBitsSupported(64);
1619 else
1620 setMaxAtomicSizeInBitsSupported(0);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001621
1622 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
1623 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32,
1624 (Subtarget->isV9() ? Legal: Expand));
1625
1626
1627 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1628
1629 // Custom Lower Atomic LOAD/STORE
1630 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1631 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1632
1633 if (Subtarget->is64Bit()) {
1634 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001635 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001636 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1637 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1638 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001639
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001640 if (!Subtarget->isV9()) {
1641 // SparcV8 does not have FNEGD and FABSD.
1642 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1643 setOperationAction(ISD::FABS, MVT::f64, Custom);
1644 }
1645
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001646 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1647 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1648 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1649 setOperationAction(ISD::FREM , MVT::f128, Expand);
1650 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001651 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1652 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001653 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001654 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001655 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001656 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1657 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001658 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001659 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001660 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001661 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001662 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001663 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001664 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001665 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1666 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1667 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001668 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001669 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1670 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001671 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001672 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1673 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001674
Owen Anderson9f944592009-08-11 20:47:22 +00001675 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1676 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1677 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001678
1679 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001680 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1681 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001682
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001683 if (Subtarget->is64Bit()) {
1684 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1685 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1686 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1687 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001688
1689 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1690 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001691
1692 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1693 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1694 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001695 }
1696
Chris Lattner0a1762e2008-03-17 03:21:36 +00001697 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001698 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001699 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001700 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001701
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001702 setOperationAction(ISD::TRAP , MVT::Other, Legal);
1703
Chris Lattner0a1762e2008-03-17 03:21:36 +00001704 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001705 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1706 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1707 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1708 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1709 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001710
Chris Lattner0a1762e2008-03-17 03:21:36 +00001711 setStackPointerRegisterToSaveRestore(SP::O6);
1712
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001713 setOperationAction(ISD::CTPOP, MVT::i32,
1714 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001715
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001716 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1717 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1718 setOperationAction(ISD::STORE, MVT::f128, Legal);
1719 } else {
1720 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1721 setOperationAction(ISD::STORE, MVT::f128, Custom);
1722 }
1723
1724 if (Subtarget->hasHardQuad()) {
1725 setOperationAction(ISD::FADD, MVT::f128, Legal);
1726 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1727 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1728 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1729 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1730 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1731 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1732 if (Subtarget->isV9()) {
1733 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1734 setOperationAction(ISD::FABS, MVT::f128, Legal);
1735 } else {
1736 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1737 setOperationAction(ISD::FABS, MVT::f128, Custom);
1738 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001739
1740 if (!Subtarget->is64Bit()) {
1741 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1742 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1743 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1744 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1745 }
1746
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001747 } else {
1748 // Custom legalize f128 operations.
1749
1750 setOperationAction(ISD::FADD, MVT::f128, Custom);
1751 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1752 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1753 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1754 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1755 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1756 setOperationAction(ISD::FABS, MVT::f128, Custom);
1757
1758 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1759 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1760 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1761
1762 // Setup Runtime library names.
1763 if (Subtarget->is64Bit()) {
1764 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1765 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1766 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1767 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1768 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1769 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001770 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001771 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001772 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001773 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1774 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1775 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1776 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001777 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1778 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1779 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1780 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
1781 } else {
1782 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1783 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1784 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1785 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1786 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1787 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001788 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001789 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001790 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001791 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1792 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1793 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1794 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001795 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1796 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1797 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1798 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1799 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001800 }
1801
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00001802 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1803
Eli Friedman2518f832011-05-06 20:34:06 +00001804 setMinFunctionAlignment(2);
1805
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001806 computeRegisterProperties(Subtarget->getRegisterInfo());
Chris Lattner0a1762e2008-03-17 03:21:36 +00001807}
1808
1809const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001810 switch ((SPISD::NodeType)Opcode) {
1811 case SPISD::FIRST_NUMBER: break;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001812 case SPISD::CMPICC: return "SPISD::CMPICC";
1813 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1814 case SPISD::BRICC: return "SPISD::BRICC";
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001815 case SPISD::BRXCC: return "SPISD::BRXCC";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001816 case SPISD::BRFCC: return "SPISD::BRFCC";
1817 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001818 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001819 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1820 case SPISD::Hi: return "SPISD::Hi";
1821 case SPISD::Lo: return "SPISD::Lo";
1822 case SPISD::FTOI: return "SPISD::FTOI";
1823 case SPISD::ITOF: return "SPISD::ITOF";
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001824 case SPISD::FTOX: return "SPISD::FTOX";
1825 case SPISD::XTOF: return "SPISD::XTOF";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001826 case SPISD::CALL: return "SPISD::CALL";
1827 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001828 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00001829 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001830 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1831 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1832 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001833 }
Matthias Braund04893f2015-05-07 21:33:59 +00001834 return nullptr;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001835}
1836
Mehdi Amini44ede332015-07-09 02:09:04 +00001837EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
1838 EVT VT) const {
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001839 if (!VT.isVector())
1840 return MVT::i32;
1841 return VT.changeVectorElementTypeToInteger();
1842}
1843
Chris Lattner0a1762e2008-03-17 03:21:36 +00001844/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1845/// be zero. Op is expected to be a target specific node. Used by DAG
1846/// combiner.
Jay Foada0653a32014-05-14 21:14:37 +00001847void SparcTargetLowering::computeKnownBitsForTargetNode
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001848 (const SDValue Op,
1849 APInt &KnownZero,
1850 APInt &KnownOne,
1851 const SelectionDAG &DAG,
1852 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001853 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001854 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001855
Chris Lattner0a1762e2008-03-17 03:21:36 +00001856 switch (Op.getOpcode()) {
1857 default: break;
1858 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001859 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001860 case SPISD::SELECT_FCC:
Jay Foada0653a32014-05-14 21:14:37 +00001861 DAG.computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1862 DAG.computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001863
Chris Lattner0a1762e2008-03-17 03:21:36 +00001864 // Only known if known in both the LHS and RHS.
1865 KnownOne &= KnownOne2;
1866 KnownZero &= KnownZero2;
1867 break;
1868 }
1869}
1870
Chris Lattner0a1762e2008-03-17 03:21:36 +00001871// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1872// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001873static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001874 ISD::CondCode CC, unsigned &SPCC) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00001875 if (isNullConstant(RHS) &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001876 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001877 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1878 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001879 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1880 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1881 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00001882 isOneConstant(LHS.getOperand(0)) &&
1883 isNullConstant(LHS.getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001884 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001885 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001886 LHS = CMPCC.getOperand(0);
1887 RHS = CMPCC.getOperand(1);
1888 }
1889}
1890
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001891// Convert to a target node and set target flags.
1892SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1893 SelectionDAG &DAG) const {
1894 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1895 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001896 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001897 GA->getValueType(0),
1898 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001899
1900 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1901 return DAG.getTargetConstantPool(CP->getConstVal(),
1902 CP->getValueType(0),
1903 CP->getAlignment(),
1904 CP->getOffset(), TF);
1905
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001906 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1907 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1908 Op.getValueType(),
1909 0,
1910 TF);
1911
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001912 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1913 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1914 ES->getValueType(0), TF);
1915
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001916 llvm_unreachable("Unhandled address SDNode");
1917}
1918
1919// Split Op into high and low parts according to HiTF and LoTF.
1920// Return an ADD node combining the parts.
1921SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1922 unsigned HiTF, unsigned LoTF,
1923 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001924 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001925 EVT VT = Op.getValueType();
1926 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1927 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1928 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1929}
1930
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001931// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1932// or ExternalSymbol SDNode.
1933SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001934 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001935 EVT VT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001936
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001937 // Handle PIC mode first.
1938 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1939 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001940 SDValue HiLo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1941 SparcMCExpr::VK_Sparc_GOT10, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001942 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1943 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001944 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1945 // function has calls.
1946 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1947 MFI->setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001948 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001949 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1950 false, false, false, 0);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001951 }
1952
1953 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001954 switch(getTargetMachine().getCodeModel()) {
1955 default:
1956 llvm_unreachable("Unsupported absolute code model");
1957 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001958 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001959 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1960 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001961 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001962 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001963 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
1964 SparcMCExpr::VK_Sparc_M44, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001965 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001966 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001967 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1968 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1969 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001970 case CodeModel::Large: {
1971 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001972 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
1973 SparcMCExpr::VK_Sparc_HM, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001974 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001975 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1976 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001977 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1978 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001979 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001980}
1981
Wesley Peck527da1b2010-11-23 03:31:01 +00001982SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001983 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001984 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001985}
1986
Chris Lattner840c7002009-09-15 17:46:24 +00001987SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001988 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001989 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001990}
1991
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001992SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
1993 SelectionDAG &DAG) const {
1994 return makeAddress(Op, DAG);
1995}
1996
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001997SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1998 SelectionDAG &DAG) const {
1999
2000 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002001 if (DAG.getTarget().Options.EmulatedTLS)
2002 return LowerToTLSEmulatedModel(GA, DAG);
2003
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002004 SDLoc DL(GA);
2005 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002006 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002007
2008 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2009
2010 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002011 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
2012 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
2013 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
2014 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
2015 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
2016 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
2017 unsigned addTF = ((model == TLSModel::GeneralDynamic)
2018 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
2019 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
2020 unsigned callTF = ((model == TLSModel::GeneralDynamic)
2021 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
2022 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002023
2024 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2025 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2026 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2027 withTargetFlags(Op, addTF, DAG));
2028
2029 SDValue Chain = DAG.getEntryNode();
2030 SDValue InFlag;
2031
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002032 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, DL, true), DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002033 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
2034 InFlag = Chain.getValue(1);
2035 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2036 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2037
2038 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2039 SmallVector<SDValue, 4> Ops;
2040 Ops.push_back(Chain);
2041 Ops.push_back(Callee);
2042 Ops.push_back(Symbol);
2043 Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
Eric Christopher9deb75d2015-03-11 22:42:13 +00002044 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2045 DAG.getMachineFunction(), CallingConv::C);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002046 assert(Mask && "Missing call preserved mask for calling convention");
2047 Ops.push_back(DAG.getRegisterMask(Mask));
2048 Ops.push_back(InFlag);
Craig Topper48d114b2014-04-26 18:35:24 +00002049 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002050 InFlag = Chain.getValue(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002051 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, DL, true),
2052 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002053 InFlag = Chain.getValue(1);
2054 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
2055
2056 if (model != TLSModel::LocalDynamic)
2057 return Ret;
2058
2059 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002060 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002061 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002062 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002063 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2064 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002065 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002066 }
2067
2068 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002069 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
2070 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002071
2072 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2073
2074 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2075 // function has calls.
2076 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2077 MFI->setHasCalls(true);
2078
2079 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002080 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
2081 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002082 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2083 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2084 DL, PtrVT, Ptr,
2085 withTargetFlags(Op, ldTF, DAG));
2086 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2087 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002088 withTargetFlags(Op,
2089 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002090 }
2091
2092 assert(model == TLSModel::LocalExec);
2093 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002094 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002095 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002096 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002097 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2098
2099 return DAG.getNode(ISD::ADD, DL, PtrVT,
2100 DAG.getRegister(SP::G7, PtrVT), Offset);
2101}
2102
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002103SDValue
2104SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain, ArgListTy &Args,
2105 SDValue Arg, SDLoc DL,
2106 SelectionDAG &DAG) const {
2107 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2108 EVT ArgVT = Arg.getValueType();
2109 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2110
2111 ArgListEntry Entry;
2112 Entry.Node = Arg;
2113 Entry.Ty = ArgTy;
2114
2115 if (ArgTy->isFP128Ty()) {
2116 // Create a stack object and pass the pointer to the library function.
2117 int FI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002118 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002119 Chain = DAG.getStore(Chain,
2120 DL,
2121 Entry.Node,
2122 FIPtr,
2123 MachinePointerInfo(),
2124 false,
2125 false,
2126 8);
2127
2128 Entry.Node = FIPtr;
2129 Entry.Ty = PointerType::getUnqual(ArgTy);
2130 }
2131 Args.push_back(Entry);
2132 return Chain;
2133}
2134
2135SDValue
2136SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2137 const char *LibFuncName,
2138 unsigned numArgs) const {
2139
2140 ArgListTy Args;
2141
2142 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002143 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002144
Mehdi Amini44ede332015-07-09 02:09:04 +00002145 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002146 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2147 Type *RetTyABI = RetTy;
2148 SDValue Chain = DAG.getEntryNode();
2149 SDValue RetPtr;
2150
2151 if (RetTy->isFP128Ty()) {
2152 // Create a Stack Object to receive the return value of type f128.
2153 ArgListEntry Entry;
2154 int RetFI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002155 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002156 Entry.Node = RetPtr;
2157 Entry.Ty = PointerType::getUnqual(RetTy);
2158 if (!Subtarget->is64Bit())
2159 Entry.isSRet = true;
2160 Entry.isReturned = false;
2161 Args.push_back(Entry);
2162 RetTyABI = Type::getVoidTy(*DAG.getContext());
2163 }
2164
2165 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2166 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2167 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2168 }
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002169 TargetLowering::CallLoweringInfo CLI(DAG);
2170 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002171 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args), 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002172
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002173 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2174
2175 // chain is in second result.
2176 if (RetTyABI == RetTy)
2177 return CallInfo.first;
2178
2179 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2180
2181 Chain = CallInfo.second;
2182
2183 // Load RetPtr to get the return value.
2184 return DAG.getLoad(Op.getValueType(),
2185 SDLoc(Op),
2186 Chain,
2187 RetPtr,
2188 MachinePointerInfo(),
2189 false, false, false, 8);
2190}
2191
2192SDValue
2193SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2194 unsigned &SPCC,
2195 SDLoc DL,
2196 SelectionDAG &DAG) const {
2197
Craig Topper062a2ba2014-04-25 05:30:21 +00002198 const char *LibCall = nullptr;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002199 bool is64Bit = Subtarget->is64Bit();
2200 switch(SPCC) {
2201 default: llvm_unreachable("Unhandled conditional code!");
2202 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2203 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2204 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2205 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2206 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2207 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2208 case SPCC::FCC_UL :
2209 case SPCC::FCC_ULE:
2210 case SPCC::FCC_UG :
2211 case SPCC::FCC_UGE:
2212 case SPCC::FCC_U :
2213 case SPCC::FCC_O :
2214 case SPCC::FCC_LG :
2215 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2216 }
2217
Mehdi Amini44ede332015-07-09 02:09:04 +00002218 auto PtrVT = getPointerTy(DAG.getDataLayout());
2219 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002220 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2221 ArgListTy Args;
2222 SDValue Chain = DAG.getEntryNode();
2223 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2224 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2225
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002226 TargetLowering::CallLoweringInfo CLI(DAG);
2227 CLI.setDebugLoc(DL).setChain(Chain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002228 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args), 0);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002229
2230 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2231
2232 // result is in first, and chain is in second result.
2233 SDValue Result = CallInfo.first;
2234
2235 switch(SPCC) {
2236 default: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002237 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002238 SPCC = SPCC::ICC_NE;
2239 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2240 }
2241 case SPCC::FCC_UL : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002242 SDValue Mask = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002243 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002244 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002245 SPCC = SPCC::ICC_NE;
2246 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2247 }
2248 case SPCC::FCC_ULE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002249 SDValue RHS = DAG.getTargetConstant(2, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002250 SPCC = SPCC::ICC_NE;
2251 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2252 }
2253 case SPCC::FCC_UG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002254 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002255 SPCC = SPCC::ICC_G;
2256 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2257 }
2258 case SPCC::FCC_UGE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002259 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002260 SPCC = SPCC::ICC_NE;
2261 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2262 }
2263
2264 case SPCC::FCC_U : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002265 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002266 SPCC = SPCC::ICC_E;
2267 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2268 }
2269 case SPCC::FCC_O : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002270 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002271 SPCC = SPCC::ICC_NE;
2272 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2273 }
2274 case SPCC::FCC_LG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002275 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002276 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002277 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002278 SPCC = SPCC::ICC_NE;
2279 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2280 }
2281 case SPCC::FCC_UE : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002282 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002283 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002284 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002285 SPCC = SPCC::ICC_E;
2286 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2287 }
2288 }
2289}
2290
2291static SDValue
2292LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2293 const SparcTargetLowering &TLI) {
2294
2295 if (Op.getOperand(0).getValueType() == MVT::f64)
2296 return TLI.LowerF128Op(Op, DAG,
2297 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2298
2299 if (Op.getOperand(0).getValueType() == MVT::f32)
2300 return TLI.LowerF128Op(Op, DAG,
2301 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2302
2303 llvm_unreachable("fpextend with non-float operand!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002304 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002305}
2306
2307static SDValue
2308LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2309 const SparcTargetLowering &TLI) {
2310 // FP_ROUND on f64 and f32 are legal.
2311 if (Op.getOperand(0).getValueType() != MVT::f128)
2312 return Op;
2313
2314 if (Op.getValueType() == MVT::f64)
2315 return TLI.LowerF128Op(Op, DAG,
2316 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2317 if (Op.getValueType() == MVT::f32)
2318 return TLI.LowerF128Op(Op, DAG,
2319 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2320
2321 llvm_unreachable("fpround to non-float!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002322 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002323}
2324
2325static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2326 const SparcTargetLowering &TLI,
2327 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002328 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002329 EVT VT = Op.getValueType();
2330 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002331
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002332 // Expand f128 operations to fp128 abi calls.
2333 if (Op.getOperand(0).getValueType() == MVT::f128
2334 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2335 const char *libName = TLI.getLibcallName(VT == MVT::i32
2336 ? RTLIB::FPTOSINT_F128_I32
2337 : RTLIB::FPTOSINT_F128_I64);
2338 return TLI.LowerF128Op(Op, DAG, libName, 1);
2339 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002340
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002341 // Expand if the resulting type is illegal.
2342 if (!TLI.isTypeLegal(VT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002343 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002344
2345 // Otherwise, Convert the fp value to integer in an FP register.
2346 if (VT == MVT::i32)
2347 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2348 else
2349 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2350
2351 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002352}
2353
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002354static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2355 const SparcTargetLowering &TLI,
2356 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002357 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002358 EVT OpVT = Op.getOperand(0).getValueType();
2359 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2360
2361 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2362
2363 // Expand f128 operations to fp128 ABI calls.
2364 if (Op.getValueType() == MVT::f128
2365 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2366 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2367 ? RTLIB::SINTTOFP_I32_F128
2368 : RTLIB::SINTTOFP_I64_F128);
2369 return TLI.LowerF128Op(Op, DAG, libName, 1);
2370 }
2371
2372 // Expand if the operand type is illegal.
2373 if (!TLI.isTypeLegal(OpVT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002374 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002375
2376 // Otherwise, Convert the int value to FP in an FP register.
2377 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2378 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2379 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002380}
2381
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002382static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2383 const SparcTargetLowering &TLI,
2384 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002385 SDLoc dl(Op);
2386 EVT VT = Op.getValueType();
2387
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002388 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002389 // quad floating point instructions and the resulting type is legal.
2390 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2391 (hasHardQuad && TLI.isTypeLegal(VT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002392 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002393
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002394 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002395
2396 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002397 TLI.getLibcallName(VT == MVT::i32
2398 ? RTLIB::FPTOUINT_F128_I32
2399 : RTLIB::FPTOUINT_F128_I64),
2400 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002401}
2402
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002403static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2404 const SparcTargetLowering &TLI,
2405 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002406 SDLoc dl(Op);
2407 EVT OpVT = Op.getOperand(0).getValueType();
2408 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2409
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002410 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002411 // quad floating point instructions and the operand type is legal.
2412 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002413 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002414
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002415 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002416 TLI.getLibcallName(OpVT == MVT::i32
2417 ? RTLIB::UINTTOFP_I32_F128
2418 : RTLIB::UINTTOFP_I64_F128),
2419 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002420}
2421
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002422static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2423 const SparcTargetLowering &TLI,
2424 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002425 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002426 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002427 SDValue LHS = Op.getOperand(2);
2428 SDValue RHS = Op.getOperand(3);
2429 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002430 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002431 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002432
Chris Lattner0a1762e2008-03-17 03:21:36 +00002433 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2434 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2435 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002436
Chris Lattner0a1762e2008-03-17 03:21:36 +00002437 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002438 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002439 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002440 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002441 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002442 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2443 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002444 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002445 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2446 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2447 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2448 Opc = SPISD::BRICC;
2449 } else {
2450 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2451 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2452 Opc = SPISD::BRFCC;
2453 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002454 }
Owen Anderson9f944592009-08-11 20:47:22 +00002455 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002456 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002457}
2458
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002459static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2460 const SparcTargetLowering &TLI,
2461 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002462 SDValue LHS = Op.getOperand(0);
2463 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002464 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002465 SDValue TrueVal = Op.getOperand(2);
2466 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002467 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002468 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002469
Chris Lattner0a1762e2008-03-17 03:21:36 +00002470 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2471 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2472 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002473
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002474 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002475 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002476 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002477 Opc = LHS.getValueType() == MVT::i32 ?
2478 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002479 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2480 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002481 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2482 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2483 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2484 Opc = SPISD::SELECT_ICC;
2485 } else {
2486 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2487 Opc = SPISD::SELECT_FCC;
2488 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2489 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002490 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002491 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002492 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002493}
2494
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002495static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002496 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002497 MachineFunction &MF = DAG.getMachineFunction();
2498 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002499 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002500
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002501 // Need frame address to find the address of VarArgsFrameIndex.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002502 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2503
Chris Lattner0a1762e2008-03-17 03:21:36 +00002504 // vastart just stores the address of the VarArgsFrameIndex slot into the
2505 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002506 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002507 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00002508 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2509 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002510 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002511 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Chris Lattner676c61d2010-09-21 18:41:36 +00002512 MachinePointerInfo(SV), false, false, 0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002513}
2514
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002515static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002516 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002517 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002518 SDValue InChain = Node->getOperand(0);
2519 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002520 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002521 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002522 SDLoc DL(Node);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002523 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002524 MachinePointerInfo(SV), false, false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002525 // Increment the pointer, VAList, to the next vaarg.
2526 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002527 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2528 DL));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002529 // Store the incremented VAList to the legalized pointer.
2530 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
Chris Lattner676c61d2010-09-21 18:41:36 +00002531 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002532 // Load the actual argument out of the pointer VAList.
2533 // We can't count on greater alignment than the word size.
2534 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2535 false, false, false,
2536 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002537}
2538
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002539static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002540 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002541 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2542 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002543 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002544 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002545
Chris Lattner0a1762e2008-03-17 03:21:36 +00002546 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002547 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2548 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002549 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002550
Chris Lattner0a1762e2008-03-17 03:21:36 +00002551 // The resultant pointer is actually 16 words from the bottom of the stack,
2552 // to provide a register spill area.
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002553 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2554 regSpillArea += Subtarget->getStackPointerBias();
2555
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002556 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002557 DAG.getConstant(regSpillArea, dl, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002558 SDValue Ops[2] = { NewVal, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002559 return DAG.getMergeValues(Ops, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002560}
2561
Chris Lattner0a1762e2008-03-17 03:21:36 +00002562
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002563static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002564 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002565 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002566 dl, MVT::Other, DAG.getEntryNode());
2567 return Chain;
2568}
2569
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002570static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2571 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002572 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2573 MFI->setFrameAddressIsTaken(true);
2574
2575 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002576 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002577 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002578 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002579
2580 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002581
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002582 if (depth == 0) {
2583 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2584 if (Subtarget->is64Bit())
2585 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002586 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002587 return FrameAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002588 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002589
2590 // flush first to make sure the windowed registers' values are in stack
2591 SDValue Chain = getFLUSHW(Op, DAG);
2592 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2593
2594 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2595
2596 while (depth--) {
2597 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002598 DAG.getIntPtrConstant(Offset, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002599 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo(),
2600 false, false, false, 0);
2601 }
2602 if (Subtarget->is64Bit())
2603 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002604 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002605 return FrameAddr;
2606}
2607
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002608
2609static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2610 const SparcSubtarget *Subtarget) {
2611
2612 uint64_t depth = Op.getConstantOperandVal(0);
2613
2614 return getFRAMEADDR(depth, Op, DAG, Subtarget);
2615
2616}
2617
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002618static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002619 const SparcTargetLowering &TLI,
2620 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002621 MachineFunction &MF = DAG.getMachineFunction();
2622 MachineFrameInfo *MFI = MF.getFrameInfo();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002623 MFI->setReturnAddressIsTaken(true);
2624
Bill Wendling908bf812014-01-06 00:43:20 +00002625 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002626 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002627
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002628 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002629 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002630 uint64_t depth = Op.getConstantOperandVal(0);
2631
2632 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002633 if (depth == 0) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002634 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2635 unsigned RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002636 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002637 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002638 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002639
2640 // Need frame address to find return address of the caller.
2641 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2642
2643 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2644 SDValue Ptr = DAG.getNode(ISD::ADD,
2645 dl, VT,
2646 FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002647 DAG.getIntPtrConstant(Offset, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002648 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr,
2649 MachinePointerInfo(), false, false, false, 0);
2650
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002651 return RetAddr;
2652}
2653
James Y Knight51208ea2016-04-25 22:54:09 +00002654static SDValue LowerF64Op(SDValue SrcReg64, SDLoc dl, SelectionDAG &DAG, unsigned opcode)
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002655{
James Y Knight51208ea2016-04-25 22:54:09 +00002656 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002657 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002658
2659 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2660 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2661 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2662
James Y Knight51208ea2016-04-25 22:54:09 +00002663 // Note: in little-endian, the floating-point value is stored in the
2664 // registers are in the opposite order, so the subreg with the sign
2665 // bit is the highest-numbered (odd), rather than the
2666 // lowest-numbered (even).
2667
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002668 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2669 SrcReg64);
2670 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2671 SrcReg64);
2672
James Y Knight51208ea2016-04-25 22:54:09 +00002673 if (DAG.getDataLayout().isLittleEndian())
2674 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2675 else
2676 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002677
2678 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2679 dl, MVT::f64), 0);
2680 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2681 DstReg64, Hi32);
2682 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2683 DstReg64, Lo32);
2684 return DstReg64;
2685}
2686
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002687// Lower a f128 load into two f64 loads.
2688static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2689{
2690 SDLoc dl(Op);
2691 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002692 assert(LdNode && LdNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002693 && "Unexpected node type");
2694
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002695 unsigned alignment = LdNode->getAlignment();
2696 if (alignment > 8)
2697 alignment = 8;
2698
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002699 SDValue Hi64 = DAG.getLoad(MVT::f64,
2700 dl,
2701 LdNode->getChain(),
2702 LdNode->getBasePtr(),
2703 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002704 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002705 EVT addrVT = LdNode->getBasePtr().getValueType();
2706 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2707 LdNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002708 DAG.getConstant(8, dl, addrVT));
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002709 SDValue Lo64 = DAG.getLoad(MVT::f64,
2710 dl,
2711 LdNode->getChain(),
2712 LoPtr,
2713 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002714 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002715
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002716 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2717 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002718
2719 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2720 dl, MVT::f128);
2721 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2722 MVT::f128,
2723 SDValue(InFP128, 0),
2724 Hi64,
2725 SubRegEven);
2726 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2727 MVT::f128,
2728 SDValue(InFP128, 0),
2729 Lo64,
2730 SubRegOdd);
2731 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2732 SDValue(Lo64.getNode(), 1) };
Craig Topper48d114b2014-04-26 18:35:24 +00002733 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002734 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
Craig Topper64941d92014-04-27 19:20:57 +00002735 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002736}
2737
James Y Knight3994be82015-08-10 19:11:39 +00002738static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2739{
2740 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2741
2742 EVT MemVT = LdNode->getMemoryVT();
2743 if (MemVT == MVT::f128)
2744 return LowerF128Load(Op, DAG);
2745
2746 return Op;
2747}
2748
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002749// Lower a f128 store into two f64 stores.
2750static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2751 SDLoc dl(Op);
2752 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002753 assert(StNode && StNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002754 && "Unexpected node type");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002755 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2756 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002757
2758 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2759 dl,
2760 MVT::f64,
2761 StNode->getValue(),
2762 SubRegEven);
2763 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2764 dl,
2765 MVT::f64,
2766 StNode->getValue(),
2767 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002768
2769 unsigned alignment = StNode->getAlignment();
2770 if (alignment > 8)
2771 alignment = 8;
2772
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002773 SDValue OutChains[2];
2774 OutChains[0] = DAG.getStore(StNode->getChain(),
2775 dl,
2776 SDValue(Hi64, 0),
2777 StNode->getBasePtr(),
2778 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002779 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002780 EVT addrVT = StNode->getBasePtr().getValueType();
2781 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2782 StNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002783 DAG.getConstant(8, dl, addrVT));
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002784 OutChains[1] = DAG.getStore(StNode->getChain(),
2785 dl,
2786 SDValue(Lo64, 0),
2787 LoPtr,
2788 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002789 false, false, alignment);
Craig Topper48d114b2014-04-26 18:35:24 +00002790 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002791}
2792
James Y Knight3994be82015-08-10 19:11:39 +00002793static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
2794{
2795 SDLoc dl(Op);
2796 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
2797
2798 EVT MemVT = St->getMemoryVT();
2799 if (MemVT == MVT::f128)
2800 return LowerF128Store(Op, DAG);
2801
2802 if (MemVT == MVT::i64) {
2803 // Custom handling for i64 stores: turn it into a bitcast and a
2804 // v2i32 store.
2805 SDValue Val = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, St->getValue());
2806 SDValue Chain = DAG.getStore(
2807 St->getChain(), dl, Val, St->getBasePtr(), St->getPointerInfo(),
2808 St->isVolatile(), St->isNonTemporal(), St->getAlignment(),
2809 St->getAAInfo());
2810 return Chain;
2811 }
2812
2813 return SDValue();
2814}
2815
Roman Divacky7a9c6542014-02-27 19:26:29 +00002816static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
Venkatraman Govindaraju3b6b0e42014-03-01 02:28:34 +00002817 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
2818 && "invalid opcode");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002819
James Y Knight51208ea2016-04-25 22:54:09 +00002820 SDLoc dl(Op);
2821
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002822 if (Op.getValueType() == MVT::f64)
James Y Knight51208ea2016-04-25 22:54:09 +00002823 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002824 if (Op.getValueType() != MVT::f128)
2825 return Op;
2826
Roman Divacky7a9c6542014-02-27 19:26:29 +00002827 // Lower fabs/fneg on f128 to fabs/fneg on f64
2828 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
James Y Knight51208ea2016-04-25 22:54:09 +00002829 // (As with LowerF64Op, on little-endian, we need to negate the odd
2830 // subreg)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002831
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002832 SDValue SrcReg128 = Op.getOperand(0);
2833 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2834 SrcReg128);
2835 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2836 SrcReg128);
James Y Knight51208ea2016-04-25 22:54:09 +00002837
2838 if (DAG.getDataLayout().isLittleEndian()) {
2839 if (isV9)
2840 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
2841 else
2842 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
2843 } else {
2844 if (isV9)
2845 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2846 else
2847 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
2848 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002849
2850 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2851 dl, MVT::f128), 0);
2852 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2853 DstReg128, Hi64);
2854 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2855 DstReg128, Lo64);
2856 return DstReg128;
2857}
2858
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002859static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002860
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002861 if (Op.getValueType() != MVT::i64)
2862 return Op;
2863
2864 SDLoc dl(Op);
2865 SDValue Src1 = Op.getOperand(0);
2866 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2867 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002868 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002869 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2870
2871 SDValue Src2 = Op.getOperand(1);
2872 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2873 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002874 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002875 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2876
2877
2878 bool hasChain = false;
2879 unsigned hiOpc = Op.getOpcode();
2880 switch (Op.getOpcode()) {
2881 default: llvm_unreachable("Invalid opcode");
2882 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2883 case ISD::ADDE: hasChain = true; break;
2884 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2885 case ISD::SUBE: hasChain = true; break;
2886 }
2887 SDValue Lo;
2888 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2889 if (hasChain) {
2890 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2891 Op.getOperand(2));
2892 } else {
2893 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2894 }
2895 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2896 SDValue Carry = Hi.getValue(1);
2897
2898 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2899 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2900 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002901 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002902
2903 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2904 SDValue Ops[2] = { Dst, Carry };
Craig Topper64941d92014-04-27 19:20:57 +00002905 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002906}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002907
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002908// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2909// in LegalizeDAG.cpp except the order of arguments to the library function.
2910static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2911 const SparcTargetLowering &TLI)
2912{
2913 unsigned opcode = Op.getOpcode();
2914 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2915
2916 bool isSigned = (opcode == ISD::SMULO);
2917 EVT VT = MVT::i64;
2918 EVT WideVT = MVT::i128;
2919 SDLoc dl(Op);
2920 SDValue LHS = Op.getOperand(0);
2921
2922 if (LHS.getValueType() != VT)
2923 return Op;
2924
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002925 SDValue ShiftAmt = DAG.getConstant(63, dl, VT);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002926
2927 SDValue RHS = Op.getOperand(1);
2928 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2929 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2930 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2931
2932 SDValue MulResult = TLI.makeLibCall(DAG,
2933 RTLIB::MUL_I128, WideVT,
Craig Topper8fe40e02015-10-22 17:05:00 +00002934 Args, isSigned, dl).first;
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002935 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002936 MulResult, DAG.getIntPtrConstant(0, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002937 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002938 MulResult, DAG.getIntPtrConstant(1, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002939 if (isSigned) {
2940 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2941 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2942 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002943 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, dl, VT),
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002944 ISD::SETNE);
2945 }
2946 // MulResult is a node with an illegal type. Because such things are not
Chandler Carruthee1a1fc2014-08-02 00:24:54 +00002947 // generally permitted during this phase of legalization, ensure that
2948 // nothing is left using the node. The above EXTRACT_ELEMENT nodes should have
2949 // been folded.
2950 assert(MulResult->use_empty() && "Illegally typed node still in use!");
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002951
2952 SDValue Ops[2] = { BottomHalf, TopHalf } ;
Craig Topper64941d92014-04-27 19:20:57 +00002953 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002954}
2955
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002956static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00002957 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
2958 // Expand with a fence.
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002959 return SDValue();
JF Bastien800f87a2016-04-06 21:19:33 +00002960
2961 // Monotonic load/stores are legal.
2962 return Op;
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002963}
2964
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00002965SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2966 SelectionDAG &DAG) const {
2967 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2968 SDLoc dl(Op);
2969 switch (IntNo) {
2970 default: return SDValue(); // Don't custom lower most intrinsics.
2971 case Intrinsic::thread_pointer: {
2972 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2973 return DAG.getRegister(SP::G7, PtrVT);
2974 }
2975 }
2976}
2977
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002978SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00002979LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002980
2981 bool hasHardQuad = Subtarget->hasHardQuad();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002982 bool isV9 = Subtarget->isV9();
2983
Chris Lattner0a1762e2008-03-17 03:21:36 +00002984 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002985 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002986
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002987 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
2988 Subtarget);
2989 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
2990 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002991 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002992 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002993 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002994 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002995 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
2996 hasHardQuad);
2997 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
2998 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002999 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
3000 hasHardQuad);
3001 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3002 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003003 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
3004 hasHardQuad);
3005 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
3006 hasHardQuad);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003007 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3008 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00003009 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00003010 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00003011
James Y Knight3994be82015-08-10 19:11:39 +00003012 case ISD::LOAD: return LowerLOAD(Op, DAG);
3013 case ISD::STORE: return LowerSTORE(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003014 case ISD::FADD: return LowerF128Op(Op, DAG,
3015 getLibcallName(RTLIB::ADD_F128), 2);
3016 case ISD::FSUB: return LowerF128Op(Op, DAG,
3017 getLibcallName(RTLIB::SUB_F128), 2);
3018 case ISD::FMUL: return LowerF128Op(Op, DAG,
3019 getLibcallName(RTLIB::MUL_F128), 2);
3020 case ISD::FDIV: return LowerF128Op(Op, DAG,
3021 getLibcallName(RTLIB::DIV_F128), 2);
3022 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3023 getLibcallName(RTLIB::SQRT_F128),1);
Roman Divacky7a9c6542014-02-27 19:26:29 +00003024 case ISD::FABS:
3025 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003026 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3027 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00003028 case ISD::ADDC:
3029 case ISD::ADDE:
3030 case ISD::SUBC:
3031 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003032 case ISD::UMULO:
3033 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003034 case ISD::ATOMIC_LOAD:
3035 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003036 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003037 }
3038}
3039
3040MachineBasicBlock *
3041SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00003042 MachineBasicBlock *BB) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00003043 switch (MI->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003044 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00003045 case SP::SELECT_CC_Int_ICC:
3046 case SP::SELECT_CC_FP_ICC:
3047 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003048 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003049 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003050 case SP::SELECT_CC_Int_FCC:
3051 case SP::SELECT_CC_FP_FCC:
3052 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003053 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003054 return expandSelectCC(MI, BB, SP::FBCOND);
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003055 }
3056}
3057
3058MachineBasicBlock*
3059SparcTargetLowering::expandSelectCC(MachineInstr *MI,
3060 MachineBasicBlock *BB,
3061 unsigned BROpcode) const {
Eric Christopherf5e94062015-01-30 23:46:43 +00003062 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003063 DebugLoc dl = MI->getDebugLoc();
3064 unsigned CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003065
Chris Lattner0a1762e2008-03-17 03:21:36 +00003066 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3067 // control-flow pattern. The incoming instruction knows the destination vreg
3068 // to set, the condition code register to branch on, the true/false values to
3069 // select between, and a branch opcode to use.
3070 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc3f79882015-10-20 00:59:43 +00003071 MachineFunction::iterator It = ++BB->getIterator();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003072
Chris Lattner0a1762e2008-03-17 03:21:36 +00003073 // thisMBB:
3074 // ...
3075 // TrueVal = ...
3076 // [f]bCC copy1MBB
3077 // fallthrough --> copy0MBB
3078 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003079 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00003080 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3081 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00003082 F->insert(It, copy0MBB);
3083 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003084
3085 // Transfer the remainder of BB and its successor edges to sinkMBB.
3086 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003087 std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00003088 BB->end());
3089 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3090
3091 // Add the true and fallthrough blocks as its successors.
3092 BB->addSuccessor(copy0MBB);
3093 BB->addSuccessor(sinkMBB);
3094
Dale Johannesen215a9252009-02-13 02:31:35 +00003095 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003096
Chris Lattner0a1762e2008-03-17 03:21:36 +00003097 // copy0MBB:
3098 // %FalseValue = ...
3099 // # fallthrough to sinkMBB
3100 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003101
Chris Lattner0a1762e2008-03-17 03:21:36 +00003102 // Update machine-CFG edges
3103 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003104
Chris Lattner0a1762e2008-03-17 03:21:36 +00003105 // sinkMBB:
3106 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3107 // ...
3108 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00003109 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattner0a1762e2008-03-17 03:21:36 +00003110 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
3111 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003112
Dan Gohman34396292010-07-06 20:24:04 +00003113 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00003114 return BB;
3115}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003116
3117//===----------------------------------------------------------------------===//
3118// Sparc Inline Assembly Support
3119//===----------------------------------------------------------------------===//
3120
3121/// getConstraintType - Given a constraint letter, return the type of
3122/// constraint it is for this target.
3123SparcTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003124SparcTargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003125 if (Constraint.size() == 1) {
3126 switch (Constraint[0]) {
3127 default: break;
3128 case 'r': return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003129 case 'I': // SIMM13
3130 return C_Other;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003131 }
3132 }
3133
3134 return TargetLowering::getConstraintType(Constraint);
3135}
3136
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003137TargetLowering::ConstraintWeight SparcTargetLowering::
3138getSingleConstraintMatchWeight(AsmOperandInfo &info,
3139 const char *constraint) const {
3140 ConstraintWeight weight = CW_Invalid;
3141 Value *CallOperandVal = info.CallOperandVal;
3142 // If we don't have a value, we can't do a match,
3143 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003144 if (!CallOperandVal)
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003145 return CW_Default;
3146
3147 // Look at the constraint type.
3148 switch (*constraint) {
3149 default:
3150 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3151 break;
3152 case 'I': // SIMM13
3153 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3154 if (isInt<13>(C->getSExtValue()))
3155 weight = CW_Constant;
3156 }
3157 break;
3158 }
3159 return weight;
3160}
3161
3162/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3163/// vector. If it is invalid, don't add anything to Ops.
3164void SparcTargetLowering::
3165LowerAsmOperandForConstraint(SDValue Op,
3166 std::string &Constraint,
3167 std::vector<SDValue> &Ops,
3168 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003169 SDValue Result(nullptr, 0);
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003170
3171 // Only support length 1 constraints for now.
3172 if (Constraint.length() > 1)
3173 return;
3174
3175 char ConstraintLetter = Constraint[0];
3176 switch (ConstraintLetter) {
3177 default: break;
3178 case 'I':
3179 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3180 if (isInt<13>(C->getSExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003181 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
3182 Op.getValueType());
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003183 break;
3184 }
3185 return;
3186 }
3187 }
3188
3189 if (Result.getNode()) {
3190 Ops.push_back(Result);
3191 return;
3192 }
3193 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3194}
3195
Eric Christopher11e4df72015-02-26 22:38:43 +00003196std::pair<unsigned, const TargetRegisterClass *>
3197SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003198 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003199 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003200 if (Constraint.size() == 1) {
3201 switch (Constraint[0]) {
3202 case 'r':
James Y Knight3994be82015-08-10 19:11:39 +00003203 if (VT == MVT::v2i32)
3204 return std::make_pair(0U, &SP::IntPairRegClass);
3205 else
3206 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003207 }
James Y Knight3994be82015-08-10 19:11:39 +00003208 } else if (!Constraint.empty() && Constraint.size() <= 5
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003209 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3210 // constraint = '{r<d>}'
3211 // Remove the braces from around the name.
3212 StringRef name(Constraint.data()+1, Constraint.size()-2);
3213 // Handle register aliases:
3214 // r0-r7 -> g0-g7
3215 // r8-r15 -> o0-o7
3216 // r16-r23 -> l0-l7
3217 // r24-r31 -> i0-i7
3218 uint64_t intVal = 0;
3219 if (name.substr(0, 1).equals("r")
3220 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3221 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3222 char regType = regTypes[intVal/8];
3223 char regIdx = '0' + (intVal % 8);
3224 char tmp[] = { '{', regType, regIdx, '}', 0 };
3225 std::string newConstraint = std::string(tmp);
Eric Christopher11e4df72015-02-26 22:38:43 +00003226 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3227 VT);
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003228 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003229 }
3230
Eric Christopher11e4df72015-02-26 22:38:43 +00003231 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003232}
3233
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003234bool
3235SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3236 // The Sparc target isn't yet aware of offsets.
3237 return false;
3238}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003239
3240void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3241 SmallVectorImpl<SDValue>& Results,
3242 SelectionDAG &DAG) const {
3243
3244 SDLoc dl(N);
3245
3246 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3247
3248 switch (N->getOpcode()) {
3249 default:
3250 llvm_unreachable("Do not know how to custom type legalize this operation!");
3251
3252 case ISD::FP_TO_SINT:
3253 case ISD::FP_TO_UINT:
3254 // Custom lower only if it involves f128 or i64.
3255 if (N->getOperand(0).getValueType() != MVT::f128
3256 || N->getValueType(0) != MVT::i64)
3257 return;
3258 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3259 ? RTLIB::FPTOSINT_F128_I64
3260 : RTLIB::FPTOUINT_F128_I64);
3261
3262 Results.push_back(LowerF128Op(SDValue(N, 0),
3263 DAG,
3264 getLibcallName(libCall),
3265 1));
3266 return;
3267
3268 case ISD::SINT_TO_FP:
3269 case ISD::UINT_TO_FP:
3270 // Custom lower only if it involves f128 or i64.
3271 if (N->getValueType(0) != MVT::f128
3272 || N->getOperand(0).getValueType() != MVT::i64)
3273 return;
3274
3275 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3276 ? RTLIB::SINTTOFP_I64_F128
3277 : RTLIB::UINTTOFP_I64_F128);
3278
3279 Results.push_back(LowerF128Op(SDValue(N, 0),
3280 DAG,
3281 getLibcallName(libCall),
3282 1));
3283 return;
James Y Knight3994be82015-08-10 19:11:39 +00003284 case ISD::LOAD: {
3285 LoadSDNode *Ld = cast<LoadSDNode>(N);
3286 // Custom handling only for i64: turn i64 load into a v2i32 load,
3287 // and a bitcast.
3288 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3289 return;
3290
3291 SDLoc dl(N);
3292 SDValue LoadRes = DAG.getExtLoad(
3293 Ld->getExtensionType(), dl, MVT::v2i32,
3294 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
3295 MVT::v2i32, Ld->isVolatile(), Ld->isNonTemporal(),
3296 Ld->isInvariant(), Ld->getAlignment(), Ld->getAAInfo());
3297
3298 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3299 Results.push_back(Res);
3300 Results.push_back(LoadRes.getValue(1));
3301 return;
3302 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003303 }
3304}