blob: 5ba4ddc7a6e0d9a737073cc6418666eccb897901 [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"
NAKAMURA Takumi0e57b132016-05-20 10:53:56 +000021#include "llvm/ADT/StringSwitch.h"
Chris Lattner49b269d2008-03-17 05:41:48 +000022#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000028#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/Module.h"
Torok Edwin56d06592009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000033using namespace llvm;
34
Chris Lattner49b269d2008-03-17 05:41:48 +000035//===----------------------------------------------------------------------===//
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) {
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +0000187 static_assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7,
188 "Unexpected enum");
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000189 if (Reg >= SP::I0 && Reg <= SP::I7)
190 return Reg - SP::I0 + SP::O0;
191 return Reg;
192}
193
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000194SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000195SparcTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
196 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000197 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000198 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000199 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000200 if (Subtarget->is64Bit())
201 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
202 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
203}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000204
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000205SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000206SparcTargetLowering::LowerReturn_32(SDValue Chain, CallingConv::ID CallConv,
207 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000208 const SmallVectorImpl<ISD::OutputArg> &Outs,
209 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000210 const SDLoc &DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000211 MachineFunction &MF = DAG.getMachineFunction();
212
Chris Lattner49b269d2008-03-17 05:41:48 +0000213 // CCValAssign - represent the assignment of the return value to locations.
214 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000215
Chris Lattner49b269d2008-03-17 05:41:48 +0000216 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000217 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
218 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000219
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000220 // Analyze return values.
221 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000222
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000223 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000224 SmallVector<SDValue, 4> RetOps(1, Chain);
225 // Make room for the return address offset.
226 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000227
228 // Copy the result values into the output registers.
James Y Knight3994be82015-08-10 19:11:39 +0000229 for (unsigned i = 0, realRVLocIdx = 0;
230 i != RVLocs.size();
231 ++i, ++realRVLocIdx) {
Chris Lattner49b269d2008-03-17 05:41:48 +0000232 CCValAssign &VA = RVLocs[i];
233 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000234
James Y Knight3994be82015-08-10 19:11:39 +0000235 SDValue Arg = OutVals[realRVLocIdx];
236
237 if (VA.needsCustom()) {
238 assert(VA.getLocVT() == MVT::v2i32);
239 // Legalize ret v2i32 -> ret 2 x i32 (Basically: do what would
240 // happen by default if this wasn't a legal type)
241
242 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
243 Arg,
244 DAG.getConstant(0, DL, getVectorIdxTy(DAG.getDataLayout())));
245 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
246 Arg,
247 DAG.getConstant(1, DL, getVectorIdxTy(DAG.getDataLayout())));
248
249 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part0, Flag);
250 Flag = Chain.getValue(1);
251 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
252 VA = RVLocs[++i]; // skip ahead to next loc
253 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part1,
254 Flag);
255 } else
256 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000257
Chris Lattner49b269d2008-03-17 05:41:48 +0000258 // Guarantee that all emitted copies are stuck together with flags.
259 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000260 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000261 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000262
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000263 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000264 // If the function returns a struct, copy the SRetReturnReg to I0
265 if (MF.getFunction()->hasStructRetAttr()) {
266 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
267 unsigned Reg = SFI->getSRetReturnReg();
268 if (!Reg)
269 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +0000270 auto PtrVT = getPointerTy(DAG.getDataLayout());
271 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, PtrVT);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000272 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000273 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +0000274 RetOps.push_back(DAG.getRegister(SP::I0, PtrVT));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000275 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000276 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000277
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000278 RetOps[0] = Chain; // Update chain.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000279 RetOps[1] = DAG.getConstant(RetAddrOffset, DL, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000280
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000281 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000282 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000283 RetOps.push_back(Flag);
284
Craig Topper48d114b2014-04-26 18:35:24 +0000285 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000286}
287
288// Lower return values for the 64-bit ABI.
289// Return values are passed the exactly the same way as function arguments.
290SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000291SparcTargetLowering::LowerReturn_64(SDValue Chain, CallingConv::ID CallConv,
292 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000293 const SmallVectorImpl<ISD::OutputArg> &Outs,
294 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000295 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000296 // CCValAssign - represent the assignment of the return value to locations.
297 SmallVector<CCValAssign, 16> RVLocs;
298
299 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000300 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
301 *DAG.getContext());
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000302
303 // Analyze return values.
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +0000304 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000305
306 SDValue Flag;
307 SmallVector<SDValue, 4> RetOps(1, Chain);
308
309 // The second operand on the return instruction is the return address offset.
310 // The return address is always %i7+8 with the 64-bit ABI.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000311 RetOps.push_back(DAG.getConstant(8, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000312
313 // Copy the result values into the output registers.
314 for (unsigned i = 0; i != RVLocs.size(); ++i) {
315 CCValAssign &VA = RVLocs[i];
316 assert(VA.isRegLoc() && "Can only return in registers!");
317 SDValue OutVal = OutVals[i];
318
319 // Integer return values must be sign or zero extended by the callee.
320 switch (VA.getLocInfo()) {
Lang Hames06234ec2014-01-14 19:56:36 +0000321 case CCValAssign::Full: break;
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000322 case CCValAssign::SExt:
323 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
324 break;
325 case CCValAssign::ZExt:
326 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
327 break;
328 case CCValAssign::AExt:
329 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000330 break;
Lang Hames06234ec2014-01-14 19:56:36 +0000331 default:
332 llvm_unreachable("Unknown loc info!");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000333 }
334
335 // The custom bit on an i32 return value indicates that it should be passed
336 // in the high bits of the register.
337 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
338 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000339 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000340
341 // The next value may go in the low bits of the same register.
342 // Handle both at once.
343 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
344 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
345 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
346 // Skip the next value, it's already done.
347 ++i;
348 }
349 }
350
351 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
352
353 // Guarantee that all emitted copies are stuck together with flags.
354 Flag = Chain.getValue(1);
355 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
356 }
357
358 RetOps[0] = Chain; // Update chain.
359
360 // Add the flag if we have it.
361 if (Flag.getNode())
362 RetOps.push_back(Flag);
363
Craig Topper48d114b2014-04-26 18:35:24 +0000364 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Chris Lattner49b269d2008-03-17 05:41:48 +0000365}
366
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000367SDValue SparcTargetLowering::LowerFormalArguments(
368 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
369 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
370 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000371 if (Subtarget->is64Bit())
372 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
373 DL, DAG, InVals);
374 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
375 DL, DAG, InVals);
376}
377
378/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000379/// passed in either one or two GPRs, including FP values. TODO: we should
380/// pass FP values in FP registers for fastcc functions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000381SDValue SparcTargetLowering::LowerFormalArguments_32(
382 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
383 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
384 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000385 MachineFunction &MF = DAG.getMachineFunction();
386 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000387 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000388
389 // Assign locations to all of the incoming arguments.
390 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000391 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
392 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000393 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000394
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000395 const unsigned StackOffset = 92;
James Y Knight33beb242015-12-15 19:23:12 +0000396 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000397
Reid Kleckner79418562014-05-09 22:32:13 +0000398 unsigned InIdx = 0;
399 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000400 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000401
Reid Kleckner79418562014-05-09 22:32:13 +0000402 if (Ins[InIdx].Flags.isSRet()) {
403 if (InIdx != 0)
404 report_fatal_error("sparc only supports sret on the first parameter");
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000405 // Get SRet from [%fp+64].
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000406 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
407 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +0000408 SDValue Arg =
409 DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000410 InVals.push_back(Arg);
411 continue;
412 }
413
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000414 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000415 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000416 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
417
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000418 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
419 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
420 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000421
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000422 assert(i+1 < e);
423 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000424
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000425 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000426 if (NextVA.isMemLoc()) {
427 int FrameIdx = MF.getFrameInfo()->
428 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000429 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +0000430 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000431 } else {
432 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000433 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000434 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000435 }
James Y Knight33beb242015-12-15 19:23:12 +0000436
437 if (IsLittleEndian)
438 std::swap(LoVal, HiVal);
439
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000440 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000441 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000442 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000443 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000444 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000445 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000446 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
447 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
448 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
449 if (VA.getLocVT() == MVT::f32)
450 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
451 else if (VA.getLocVT() != MVT::i32) {
452 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
453 DAG.getValueType(VA.getLocVT()));
454 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
455 }
456 InVals.push_back(Arg);
457 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000458 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000459
460 assert(VA.isMemLoc());
461
462 unsigned Offset = VA.getLocMemOffset()+StackOffset;
Mehdi Amini44ede332015-07-09 02:09:04 +0000463 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000464
465 if (VA.needsCustom()) {
Hans Wennborg1f094852016-04-11 20:35:41 +0000466 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000467 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000468 if (Offset % 8 == 0) {
469 int FI = MF.getFrameInfo()->CreateFixedObject(8,
470 Offset,
471 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000472 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +0000473 SDValue Load =
474 DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000475 InVals.push_back(Load);
476 continue;
477 }
478
479 int FI = MF.getFrameInfo()->CreateFixedObject(4,
480 Offset,
481 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000482 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +0000483 SDValue HiVal =
484 DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000485 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
486 Offset+4,
487 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000488 SDValue FIPtr2 = DAG.getFrameIndex(FI2, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000489
Justin Lebar9c375812016-07-15 18:27:10 +0000490 SDValue LoVal =
491 DAG.getLoad(MVT::i32, dl, Chain, FIPtr2, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000492
James Y Knight33beb242015-12-15 19:23:12 +0000493 if (IsLittleEndian)
494 std::swap(LoVal, HiVal);
495
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000496 SDValue WholeValue =
497 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000498 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000499 InVals.push_back(WholeValue);
500 continue;
501 }
502
503 int FI = MF.getFrameInfo()->CreateFixedObject(4,
504 Offset,
505 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000506 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000507 SDValue Load ;
508 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
Justin Lebar9c375812016-07-15 18:27:10 +0000509 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, MachinePointerInfo());
James Y Knight33beb242015-12-15 19:23:12 +0000510 } else if (VA.getValVT() == MVT::f128) {
511 report_fatal_error("SPARCv8 does not handle f128 in calls; "
512 "pass indirectly");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000513 } else {
James Y Knight33beb242015-12-15 19:23:12 +0000514 // We shouldn't see any other value types here.
James Y Knight99fcb722015-12-15 23:07:16 +0000515 llvm_unreachable("Unexpected ValVT encountered in frame lowering.");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000516 }
517 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000518 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000519
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000520 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000521 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000522 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
523 unsigned Reg = SFI->getSRetReturnReg();
524 if (!Reg) {
525 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
526 SFI->setSRetReturnReg(Reg);
527 }
528 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
529 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
530 }
531
Chris Lattner49b269d2008-03-17 05:41:48 +0000532 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000533 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +0000534 static const MCPhysReg ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000535 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
536 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000537 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs);
Craig Topper840beec2014-04-04 05:16:06 +0000538 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000539 unsigned ArgOffset = CCInfo.getNextStackOffset();
540 if (NumAllocated == 6)
541 ArgOffset += StackOffset;
542 else {
543 assert(!ArgOffset);
544 ArgOffset = 68+4*NumAllocated;
545 }
546
Chris Lattner49b269d2008-03-17 05:41:48 +0000547 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000548 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000549
Eli Friedmanbe853b72009-07-19 19:53:46 +0000550 std::vector<SDValue> OutChains;
551
Chris Lattner49b269d2008-03-17 05:41:48 +0000552 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
553 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
554 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000555 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000556
David Greene1fbe0542009-11-12 20:49:22 +0000557 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Cheng0664a672010-07-03 00:40:23 +0000558 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000559 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000560
Justin Lebar9c375812016-07-15 18:27:10 +0000561 OutChains.push_back(
562 DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, MachinePointerInfo()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000563 ArgOffset += 4;
564 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000565
566 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000567 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +0000568 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Eli Friedmanbe853b72009-07-19 19:53:46 +0000569 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000570 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000571
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000572 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000573}
574
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000575// Lower formal arguments for the 64 bit ABI.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000576SDValue SparcTargetLowering::LowerFormalArguments_64(
577 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
578 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
579 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000580 MachineFunction &MF = DAG.getMachineFunction();
581
582 // Analyze arguments according to CC_Sparc64.
583 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000584 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
585 *DAG.getContext());
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000586 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
587
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000588 // The argument array begins at %fp+BIAS+128, after the register save area.
589 const unsigned ArgArea = 128;
590
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000591 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
592 CCValAssign &VA = ArgLocs[i];
593 if (VA.isRegLoc()) {
594 // This argument is passed in a register.
595 // All integer register arguments are promoted by the caller to i64.
596
597 // Create a virtual register for the promoted live-in value.
598 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
599 getRegClassFor(VA.getLocVT()));
600 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
601
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000602 // Get the high bits for i32 struct elements.
603 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
604 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000605 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000606
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000607 // The caller promoted the argument, so insert an Assert?ext SDNode so we
608 // won't promote the value again in this function.
609 switch (VA.getLocInfo()) {
610 case CCValAssign::SExt:
611 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
612 DAG.getValueType(VA.getValVT()));
613 break;
614 case CCValAssign::ZExt:
615 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
616 DAG.getValueType(VA.getValVT()));
617 break;
618 default:
619 break;
620 }
621
622 // Truncate the register down to the argument type.
623 if (VA.isExtInLoc())
624 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
625
626 InVals.push_back(Arg);
627 continue;
628 }
629
630 // The registers are exhausted. This argument was passed on the stack.
631 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000632 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
633 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000634 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000635 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
636 // Adjust offset for extended arguments, SPARC is big-endian.
637 // The caller will have written the full slot with extended bytes, but we
638 // prefer our own extending loads.
639 if (VA.isExtInLoc())
640 Offset += 8 - ValSize;
641 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
Justin Lebar9c375812016-07-15 18:27:10 +0000642 InVals.push_back(
643 DAG.getLoad(VA.getValVT(), DL, Chain,
644 DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
645 MachinePointerInfo::getFixedStack(MF, FI)));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000646 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000647
648 if (!IsVarArg)
649 return Chain;
650
651 // This function takes variable arguments, some of which may have been passed
652 // in registers %i0-%i5. Variable floating point arguments are never passed
653 // in floating point registers. They go on %i0-%i5 or on the stack like
654 // integer arguments.
655 //
656 // The va_start intrinsic needs to know the offset to the first variable
657 // argument.
658 unsigned ArgOffset = CCInfo.getNextStackOffset();
659 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
660 // Skip the 128 bytes of register save area.
661 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
662 Subtarget->getStackPointerBias());
663
664 // Save the variable arguments that were passed in registers.
665 // The caller is required to reserve stack space for 6 arguments regardless
666 // of how many arguments were actually passed.
667 SmallVector<SDValue, 8> OutChains;
668 for (; ArgOffset < 6*8; ArgOffset += 8) {
669 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
670 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
671 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000672 auto PtrVT = getPointerTy(MF.getDataLayout());
Justin Lebar9c375812016-07-15 18:27:10 +0000673 OutChains.push_back(
674 DAG.getStore(Chain, DL, VArg, DAG.getFrameIndex(FI, PtrVT),
675 MachinePointerInfo::getFixedStack(MF, FI)));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000676 }
677
678 if (!OutChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000679 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000680
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000681 return Chain;
682}
683
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000684SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000685SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000686 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000687 if (Subtarget->is64Bit())
688 return LowerCall_64(CLI, InVals);
689 return LowerCall_32(CLI, InVals);
690}
691
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000692static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
693 ImmutableCallSite *CS) {
694 if (CS)
695 return CS->hasFnAttr(Attribute::ReturnsTwice);
696
Craig Topper062a2ba2014-04-25 05:30:21 +0000697 const Function *CalleeFn = nullptr;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000698 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
699 CalleeFn = dyn_cast<Function>(G->getGlobal());
700 } else if (ExternalSymbolSDNode *E =
701 dyn_cast<ExternalSymbolSDNode>(Callee)) {
702 const Function *Fn = DAG.getMachineFunction().getFunction();
703 const Module *M = Fn->getParent();
704 const char *CalleeName = E->getSymbol();
705 CalleeFn = M->getFunction(CalleeName);
706 }
707
708 if (!CalleeFn)
709 return false;
710 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
711}
712
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000713// Lower a call for the 32-bit ABI.
714SDValue
715SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
716 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000717 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000718 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000719 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
720 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
721 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000722 SDValue Chain = CLI.Chain;
723 SDValue Callee = CLI.Callee;
724 bool &isTailCall = CLI.IsTailCall;
725 CallingConv::ID CallConv = CLI.CallConv;
726 bool isVarArg = CLI.IsVarArg;
727
Evan Cheng67a69dd2010-01-27 00:07:07 +0000728 // Sparc target does not yet support tail call optimization.
729 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000730
Chris Lattner7d4152b2008-03-17 06:58:37 +0000731 // Analyze operands of the call, assigning locations to each operand.
732 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000733 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
734 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000735 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000736
Chris Lattner7d4152b2008-03-17 06:58:37 +0000737 // Get the size of the outgoing arguments stack space requirement.
738 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000739
Chris Lattner49b269d2008-03-17 05:41:48 +0000740 // Keep stack frames 8-byte aligned.
741 ArgsSize = (ArgsSize+7) & ~7;
742
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000743 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
744
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000745 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000746 SmallVector<SDValue, 8> ByValArgs;
747 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
748 ISD::ArgFlagsTy Flags = Outs[i].Flags;
749 if (!Flags.isByVal())
750 continue;
751
752 SDValue Arg = OutVals[i];
753 unsigned Size = Flags.getByValSize();
754 unsigned Align = Flags.getByValAlign();
755
Chris Dewhurst53bde952016-06-01 08:48:56 +0000756 if (Size > 0U) {
757 int FI = MFI->CreateStackObject(Size, Align, false);
758 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
759 SDValue SizeNode = DAG.getConstant(Size, dl, MVT::i32);
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000760
Chris Dewhurst53bde952016-06-01 08:48:56 +0000761 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
762 false, // isVolatile,
763 (Size <= 32), // AlwaysInline if size <= 32,
764 false, // isTailCall
765 MachinePointerInfo(), MachinePointerInfo());
766 ByValArgs.push_back(FIPtr);
767 }
768 else {
769 SDValue nullVal;
770 ByValArgs.push_back(nullVal);
771 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000772 }
773
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000774 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000775 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000776
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000777 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
778 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000779
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000780 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000781 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000782 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000783 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000784 i != e;
785 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000786 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000787 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000788
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000789 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
790
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000791 // Use local copy if it is a byval arg.
Chris Dewhurst53bde952016-06-01 08:48:56 +0000792 if (Flags.isByVal()) {
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000793 Arg = ByValArgs[byvalArgIdx++];
Chris Dewhurst53bde952016-06-01 08:48:56 +0000794 if (!Arg) {
795 continue;
796 }
797 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000798
Chris Lattner7d4152b2008-03-17 06:58:37 +0000799 // Promote the value if needed.
800 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000801 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000802 case CCValAssign::Full: break;
803 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000804 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000805 break;
806 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000807 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000808 break;
809 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000810 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
811 break;
812 case CCValAssign::BCvt:
813 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000814 break;
815 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000816
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000817 if (Flags.isSRet()) {
818 assert(VA.needsCustom());
819 // store SRet argument in %sp+64
820 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000821 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000822 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000823 MemOpChains.push_back(
824 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000825 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000826 continue;
827 }
828
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000829 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000830 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000831
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000832 if (VA.isMemLoc()) {
833 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000834 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000835 if (Offset % 8 == 0) {
836 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000837 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000838 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000839 MemOpChains.push_back(
840 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000841 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000842 }
843 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000844
James Y Knight3994be82015-08-10 19:11:39 +0000845 if (VA.getLocVT() == MVT::f64) {
846 // Move from the float value from float registers into the
847 // integer registers.
848
James Y Knight692e0372015-10-09 21:36:19 +0000849 // TODO: The f64 -> v2i32 conversion is super-inefficient for
850 // constants: it sticks them in the constant pool, then loads
851 // to a fp register, then stores to temp memory, then loads to
852 // integer registers.
James Y Knight3994be82015-08-10 19:11:39 +0000853 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
854 }
855
856 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
857 Arg,
858 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
859 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
860 Arg,
861 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000862
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000863 if (VA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000864 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000865 assert(i+1 != e);
866 CCValAssign &NextVA = ArgLocs[++i];
867 if (NextVA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000868 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000869 } else {
James Y Knight3994be82015-08-10 19:11:39 +0000870 // Store the second part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000871 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
872 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000873 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000874 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000875 MemOpChains.push_back(
876 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000877 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000878 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000879 unsigned Offset = VA.getLocMemOffset() + StackOffset;
James Y Knight3994be82015-08-10 19:11:39 +0000880 // Store the first part.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000881 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000882 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000883 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000884 MemOpChains.push_back(
885 DAG.getStore(Chain, dl, Part0, PtrOff, MachinePointerInfo()));
James Y Knight3994be82015-08-10 19:11:39 +0000886 // Store the second part.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000887 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000888 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000889 MemOpChains.push_back(
890 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000891 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000892 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000893 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000894
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000895 // Arguments that can be passed on register must be kept at
896 // RegsToPass vector
897 if (VA.isRegLoc()) {
898 if (VA.getLocVT() != MVT::f32) {
899 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
900 continue;
901 }
902 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
903 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
904 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000905 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000906
907 assert(VA.isMemLoc());
908
909 // Create a store off the stack pointer for this argument.
910 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000911 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
912 dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000913 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000914 MemOpChains.push_back(
915 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000916 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000917
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000918
Chris Lattner49b269d2008-03-17 05:41:48 +0000919 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000920 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000921 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000922
923 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000924 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000925 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000926 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000927 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000928 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000929 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000930 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000931 InFlag = Chain.getValue(1);
932 }
933
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000934 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000935 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000936
Chris Lattner49b269d2008-03-17 05:41:48 +0000937 // If the callee is a GlobalAddress node (quite common, every direct call is)
938 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000939 // Likewise ExternalSymbol -> TargetExternalSymbol.
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +0000940 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Chris Lattner49b269d2008-03-17 05:41:48 +0000941 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000942 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000943 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000944 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000945
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000946 // Returns a chain & a flag for retval copy to use
947 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
948 SmallVector<SDValue, 8> Ops;
949 Ops.push_back(Chain);
950 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000951 if (hasStructRetAttr)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000952 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000953 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
954 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
955 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000956
957 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000958 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000959 const uint32_t *Mask =
960 ((hasReturnsTwice)
961 ? TRI->getRTCallPreservedMask(CallConv)
962 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000963 assert(Mask && "Missing call preserved mask for calling convention");
964 Ops.push_back(DAG.getRegisterMask(Mask));
965
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000966 if (InFlag.getNode())
967 Ops.push_back(InFlag);
968
Craig Topper48d114b2014-04-26 18:35:24 +0000969 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
Chris Lattner49b269d2008-03-17 05:41:48 +0000970 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000971
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000972 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
973 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000974 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000975
Chris Lattnerdb26db22008-03-17 06:01:07 +0000976 // Assign locations to each value returned by this call.
977 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000978 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
979 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000980
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000981 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000982
Chris Lattnerdb26db22008-03-17 06:01:07 +0000983 // Copy all of the result registers out of their specified physreg.
984 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Nirav Dave29938542016-02-26 18:55:22 +0000985 if (RVLocs[i].getLocVT() == MVT::v2i32) {
986 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
987 SDValue Lo = DAG.getCopyFromReg(
988 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InFlag);
989 Chain = Lo.getValue(1);
990 InFlag = Lo.getValue(2);
991 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
992 DAG.getConstant(0, dl, MVT::i32));
993 SDValue Hi = DAG.getCopyFromReg(
994 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InFlag);
995 Chain = Hi.getValue(1);
996 InFlag = Hi.getValue(2);
997 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
998 DAG.getConstant(1, dl, MVT::i32));
999 InVals.push_back(Vec);
1000 } else {
1001 Chain =
1002 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1003 RVLocs[i].getValVT(), InFlag)
1004 .getValue(1);
1005 InFlag = Chain.getValue(2);
1006 InVals.push_back(Chain.getValue(0));
1007 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001008 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001009
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001010 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +00001011}
1012
Chris Dewhurstad741172016-05-20 10:21:01 +00001013// FIXME? Maybe this could be a TableGen attribute on some registers and
1014// this table could be generated automatically from RegInfo.
1015unsigned SparcTargetLowering::getRegisterByName(const char* RegName, EVT VT,
1016 SelectionDAG &DAG) const {
1017 unsigned Reg = StringSwitch<unsigned>(RegName)
1018 .Case("i0", SP::I0).Case("i1", SP::I1).Case("i2", SP::I2).Case("i3", SP::I3)
1019 .Case("i4", SP::I4).Case("i5", SP::I5).Case("i6", SP::I6).Case("i7", SP::I7)
1020 .Case("o0", SP::O0).Case("o1", SP::O1).Case("o2", SP::O2).Case("o3", SP::O3)
1021 .Case("o4", SP::O4).Case("o5", SP::O5).Case("o6", SP::O6).Case("o7", SP::O7)
1022 .Case("l0", SP::L0).Case("l1", SP::L1).Case("l2", SP::L2).Case("l3", SP::L3)
1023 .Case("l4", SP::L4).Case("l5", SP::L5).Case("l6", SP::L6).Case("l7", SP::L7)
1024 .Case("g0", SP::G0).Case("g1", SP::G1).Case("g2", SP::G2).Case("g3", SP::G3)
1025 .Case("g4", SP::G4).Case("g5", SP::G5).Case("g6", SP::G6).Case("g7", SP::G7)
1026 .Default(0);
1027
1028 if (Reg)
1029 return Reg;
1030
1031 report_fatal_error("Invalid register name global variable");
1032}
1033
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001034// This functions returns true if CalleeName is a ABI function that returns
1035// a long double (fp128).
1036static bool isFP128ABICall(const char *CalleeName)
1037{
1038 static const char *const ABICalls[] =
1039 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
1040 "_Q_sqrt", "_Q_neg",
1041 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001042 "_Q_lltoq", "_Q_ulltoq",
Craig Topper062a2ba2014-04-25 05:30:21 +00001043 nullptr
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001044 };
Craig Topper062a2ba2014-04-25 05:30:21 +00001045 for (const char * const *I = ABICalls; *I != nullptr; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001046 if (strcmp(CalleeName, *I) == 0)
1047 return true;
1048 return false;
1049}
1050
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001051unsigned
1052SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
1053{
Craig Topper062a2ba2014-04-25 05:30:21 +00001054 const Function *CalleeFn = nullptr;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001055 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1056 CalleeFn = dyn_cast<Function>(G->getGlobal());
1057 } else if (ExternalSymbolSDNode *E =
1058 dyn_cast<ExternalSymbolSDNode>(Callee)) {
1059 const Function *Fn = DAG.getMachineFunction().getFunction();
1060 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001061 const char *CalleeName = E->getSymbol();
1062 CalleeFn = M->getFunction(CalleeName);
1063 if (!CalleeFn && isFP128ABICall(CalleeName))
1064 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001065 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001066
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001067 if (!CalleeFn)
1068 return 0;
1069
Joerg Sonnenberger72128092015-10-21 20:05:01 +00001070 // It would be nice to check for the sret attribute on CalleeFn here,
1071 // but since it is not part of the function type, any check will misfire.
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001072
Chris Lattner229907c2011-07-18 04:54:35 +00001073 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
1074 Type *ElementTy = Ty->getElementType();
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001075 return DAG.getDataLayout().getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001076}
Chris Lattner49b269d2008-03-17 05:41:48 +00001077
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001078
1079// Fixup floating point arguments in the ... part of a varargs call.
1080//
1081// The SPARC v9 ABI requires that floating point arguments are treated the same
1082// as integers when calling a varargs function. This does not apply to the
1083// fixed arguments that are part of the function's prototype.
1084//
1085// This function post-processes a CCValAssign array created by
1086// AnalyzeCallOperands().
1087static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1088 ArrayRef<ISD::OutputArg> Outs) {
1089 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1090 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001091 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001092 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1093 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001094 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001095 continue;
1096 // The fixed arguments to a varargs function still go in FP registers.
1097 if (Outs[VA.getValNo()].IsFixed)
1098 continue;
1099
1100 // This floating point argument should be reassigned.
1101 CCValAssign NewVA;
1102
1103 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001104 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1105 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1106 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001107 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1108
1109 if (Offset < 6*8) {
1110 // This argument should go in %i0-%i5.
1111 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001112 if (ValTy == MVT::f64)
1113 // Full register, just bitconvert into i64.
1114 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1115 IReg, MVT::i64, CCValAssign::BCvt);
1116 else {
1117 assert(ValTy == MVT::f128 && "Unexpected type!");
1118 // Full register, just bitconvert into i128 -- We will lower this into
1119 // two i64s in LowerCall_64.
1120 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1121 IReg, MVT::i128, CCValAssign::BCvt);
1122 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001123 } else {
1124 // This needs to go to memory, we're out of integer registers.
1125 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1126 Offset, VA.getLocVT(), VA.getLocInfo());
1127 }
1128 ArgLocs[i] = NewVA;
1129 }
1130}
1131
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001132// Lower a call for the 64-bit ABI.
1133SDValue
1134SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1135 SmallVectorImpl<SDValue> &InVals) const {
1136 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001137 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001138 SDValue Chain = CLI.Chain;
Mehdi Amini44ede332015-07-09 02:09:04 +00001139 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001140
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001141 // Sparc target does not yet support tail call optimization.
1142 CLI.IsTailCall = false;
1143
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001144 // Analyze operands of the call, assigning locations to each operand.
1145 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001146 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1147 *DAG.getContext());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001148 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1149
1150 // Get the size of the outgoing arguments stack space requirement.
1151 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001152 // Called functions expect 6 argument words to exist in the stack frame, used
1153 // or not.
1154 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001155
1156 // Keep stack frames 16-byte aligned.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001157 ArgsSize = alignTo(ArgsSize, 16);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001158
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001159 // Varargs calls require special treatment.
1160 if (CLI.IsVarArg)
1161 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1162
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001163 // Adjust the stack pointer to make room for the arguments.
1164 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1165 // with more than 6 arguments.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001166 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001167 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001168
1169 // Collect the set of registers to pass to the function and their values.
1170 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1171 // instruction.
1172 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1173
1174 // Collect chains from all the memory opeations that copy arguments to the
1175 // stack. They must follow the stack pointer adjustment above and precede the
1176 // call instruction itself.
1177 SmallVector<SDValue, 8> MemOpChains;
1178
1179 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1180 const CCValAssign &VA = ArgLocs[i];
1181 SDValue Arg = CLI.OutVals[i];
1182
1183 // Promote the value if needed.
1184 switch (VA.getLocInfo()) {
1185 default:
1186 llvm_unreachable("Unknown location info!");
1187 case CCValAssign::Full:
1188 break;
1189 case CCValAssign::SExt:
1190 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1191 break;
1192 case CCValAssign::ZExt:
1193 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1194 break;
1195 case CCValAssign::AExt:
1196 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1197 break;
1198 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001199 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1200 // SPARC does not support i128 natively. Lower it into two i64, see below.
1201 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1202 || VA.getLocVT() != MVT::i128)
1203 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001204 break;
1205 }
1206
1207 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001208 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1209 && VA.getLocVT() == MVT::i128) {
1210 // Store and reload into the interger register reg and reg+1.
1211 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1212 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
Mehdi Amini44ede332015-07-09 02:09:04 +00001213 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001214 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001215 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001216 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001217 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001218
1219 // Store to %sp+BIAS+128+Offset
Justin Lebar9c375812016-07-15 18:27:10 +00001220 SDValue Store =
1221 DAG.getStore(Chain, DL, Arg, HiPtrOff, MachinePointerInfo());
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001222 // Load into Reg and Reg+1
Justin Lebar9c375812016-07-15 18:27:10 +00001223 SDValue Hi64 =
1224 DAG.getLoad(MVT::i64, DL, Store, HiPtrOff, MachinePointerInfo());
1225 SDValue Lo64 =
1226 DAG.getLoad(MVT::i64, DL, Store, LoPtrOff, MachinePointerInfo());
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001227 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1228 Hi64));
1229 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1230 Lo64));
1231 continue;
1232 }
1233
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001234 // The custom bit on an i32 return value indicates that it should be
1235 // passed in the high bits of the register.
1236 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1237 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001238 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001239
1240 // The next value may go in the low bits of the same register.
1241 // Handle both at once.
1242 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1243 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1244 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1245 CLI.OutVals[i+1]);
1246 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1247 // Skip the next value, it's already done.
1248 ++i;
1249 }
1250 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001251 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001252 continue;
1253 }
1254
1255 assert(VA.isMemLoc());
1256
1257 // Create a store off the stack pointer for this argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00001258 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001259 // The argument area starts at %fp+BIAS+128 in the callee frame,
1260 // %sp+BIAS+128 in ours.
1261 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1262 Subtarget->getStackPointerBias() +
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001263 128, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001264 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +00001265 MemOpChains.push_back(
1266 DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001267 }
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);
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +00001289 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001290 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001291 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001292 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001293 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001294
1295 // Build the operands for the call instruction itself.
1296 SmallVector<SDValue, 8> Ops;
1297 Ops.push_back(Chain);
1298 Ops.push_back(Callee);
1299 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1300 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1301 RegsToPass[i].second.getValueType()));
1302
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001303 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +00001304 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001305 const uint32_t *Mask =
1306 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
Eric Christopher9deb75d2015-03-11 22:42:13 +00001307 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1308 CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001309 assert(Mask && "Missing call preserved mask for calling convention");
1310 Ops.push_back(DAG.getRegisterMask(Mask));
1311
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001312 // Make sure the CopyToReg nodes are glued to the call instruction which
1313 // consumes the registers.
1314 if (InGlue.getNode())
1315 Ops.push_back(InGlue);
1316
1317 // Now the call itself.
1318 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001319 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001320 InGlue = Chain.getValue(1);
1321
1322 // Revert the stack pointer immediately after the call.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001323 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
1324 DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001325 InGlue = Chain.getValue(1);
1326
1327 // Now extract the return values. This is more or less the same as
1328 // LowerFormalArguments_64.
1329
1330 // Assign locations to each value returned by this call.
1331 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001332 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1333 *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001334
1335 // Set inreg flag manually for codegen generated library calls that
1336 // return float.
Craig Topper062a2ba2014-04-25 05:30:21 +00001337 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == nullptr)
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001338 CLI.Ins[0].Flags.setInReg();
1339
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001340 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001341
1342 // Copy all of the result registers out of their specified physreg.
1343 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1344 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001345 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001346
1347 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1348 // reside in the same register in the high and low bits. Reuse the
1349 // CopyFromReg previous node to avoid duplicate copies.
1350 SDValue RV;
1351 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1352 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1353 RV = Chain.getValue(0);
1354
1355 // But usually we'll create a new CopyFromReg for a different register.
1356 if (!RV.getNode()) {
1357 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1358 Chain = RV.getValue(1);
1359 InGlue = Chain.getValue(2);
1360 }
1361
1362 // Get the high bits for i32 struct elements.
1363 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1364 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001365 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001366
1367 // The callee promoted the return value, so insert an Assert?ext SDNode so
1368 // we won't promote the value again in this function.
1369 switch (VA.getLocInfo()) {
1370 case CCValAssign::SExt:
1371 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1372 DAG.getValueType(VA.getValVT()));
1373 break;
1374 case CCValAssign::ZExt:
1375 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1376 DAG.getValueType(VA.getValVT()));
1377 break;
1378 default:
1379 break;
1380 }
1381
1382 // Truncate the register down to the return value type.
1383 if (VA.isExtInLoc())
1384 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1385
1386 InVals.push_back(RV);
1387 }
1388
1389 return Chain;
1390}
1391
Chris Lattner0a1762e2008-03-17 03:21:36 +00001392//===----------------------------------------------------------------------===//
1393// TargetLowering Implementation
1394//===----------------------------------------------------------------------===//
1395
James Y Knight7306cd42016-03-29 19:09:54 +00001396TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1397 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1398 AI->getType()->getPrimitiveSizeInBits() == 32)
1399 return AtomicExpansionKind::None; // Uses xchg instruction
1400
1401 return AtomicExpansionKind::CmpXChg;
1402}
1403
Chris Lattner0a1762e2008-03-17 03:21:36 +00001404/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1405/// condition.
1406static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1407 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001408 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001409 case ISD::SETEQ: return SPCC::ICC_E;
1410 case ISD::SETNE: return SPCC::ICC_NE;
1411 case ISD::SETLT: return SPCC::ICC_L;
1412 case ISD::SETGT: return SPCC::ICC_G;
1413 case ISD::SETLE: return SPCC::ICC_LE;
1414 case ISD::SETGE: return SPCC::ICC_GE;
1415 case ISD::SETULT: return SPCC::ICC_CS;
1416 case ISD::SETULE: return SPCC::ICC_LEU;
1417 case ISD::SETUGT: return SPCC::ICC_GU;
1418 case ISD::SETUGE: return SPCC::ICC_CC;
1419 }
1420}
1421
1422/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1423/// FCC condition.
1424static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1425 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001426 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001427 case ISD::SETEQ:
1428 case ISD::SETOEQ: return SPCC::FCC_E;
1429 case ISD::SETNE:
1430 case ISD::SETUNE: return SPCC::FCC_NE;
1431 case ISD::SETLT:
1432 case ISD::SETOLT: return SPCC::FCC_L;
1433 case ISD::SETGT:
1434 case ISD::SETOGT: return SPCC::FCC_G;
1435 case ISD::SETLE:
1436 case ISD::SETOLE: return SPCC::FCC_LE;
1437 case ISD::SETGE:
1438 case ISD::SETOGE: return SPCC::FCC_GE;
1439 case ISD::SETULT: return SPCC::FCC_UL;
1440 case ISD::SETULE: return SPCC::FCC_ULE;
1441 case ISD::SETUGT: return SPCC::FCC_UG;
1442 case ISD::SETUGE: return SPCC::FCC_UGE;
1443 case ISD::SETUO: return SPCC::FCC_U;
1444 case ISD::SETO: return SPCC::FCC_O;
1445 case ISD::SETONE: return SPCC::FCC_LG;
1446 case ISD::SETUEQ: return SPCC::FCC_UE;
1447 }
1448}
1449
James Y Knightef31eaf2016-05-03 14:57:18 +00001450SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
Eric Christopherf5e94062015-01-30 23:46:43 +00001451 const SparcSubtarget &STI)
1452 : TargetLowering(TM), Subtarget(&STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +00001453 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00001454
James Y Knightd966fb62015-08-19 14:47:04 +00001455 // Instructions which use registers as conditionals examine all the
1456 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1457 // matters much whether it's ZeroOrOneBooleanContent, or
1458 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1459 // former.
1460 setBooleanContents(ZeroOrOneBooleanContent);
1461 setBooleanVectorContents(ZeroOrOneBooleanContent);
1462
Chris Lattner0a1762e2008-03-17 03:21:36 +00001463 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001464 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
Chris Dewhurst68388a02016-05-18 09:14:13 +00001465 if (!Subtarget->useSoftFloat()) {
1466 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1467 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1468 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1469 }
James Y Knight3994be82015-08-10 19:11:39 +00001470 if (Subtarget->is64Bit()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001471 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001472 } else {
1473 // On 32bit sparc, we define a double-register 32bit register
1474 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1475 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1476
1477 // ...but almost all operations must be expanded, so set that as
1478 // the default.
1479 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1480 setOperationAction(Op, MVT::v2i32, Expand);
1481 }
1482 // Truncating/extending stores/loads are also not supported.
1483 for (MVT VT : MVT::integer_vector_valuetypes()) {
1484 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1485 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1486 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1487
1488 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1489 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1490 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1491
1492 setTruncStoreAction(VT, MVT::v2i32, Expand);
1493 setTruncStoreAction(MVT::v2i32, VT, Expand);
1494 }
1495 // However, load and store *are* legal.
1496 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1497 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1498 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1499 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1500
1501 // And we need to promote i64 loads/stores into vector load/store
1502 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1503 setOperationAction(ISD::STORE, MVT::i64, Custom);
1504
1505 // Sadly, this doesn't work:
1506 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1507 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1508 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001509
1510 // Turn FP extload into load/fextend
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001511 for (MVT VT : MVT::fp_valuetypes()) {
1512 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1513 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1514 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001515
Chris Lattner0a1762e2008-03-17 03:21:36 +00001516 // Sparc doesn't have i1 sign extending load
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001517 for (MVT VT : MVT::integer_valuetypes())
1518 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001519
Chris Lattner0a1762e2008-03-17 03:21:36 +00001520 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001521 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001522 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1523 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001524
1525 // Custom legalize GlobalAddress nodes into LO/HI parts.
Mehdi Amini26d48132015-07-24 16:04:22 +00001526 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1527 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1528 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1529 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001530
Chris Lattner0a1762e2008-03-17 03:21:36 +00001531 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001532 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1533 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1534 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001535
1536 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001537 setOperationAction(ISD::UREM, MVT::i32, Expand);
1538 setOperationAction(ISD::SREM, MVT::i32, Expand);
1539 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1540 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001541
Roman Divacky2262cfa2013-10-31 19:22:33 +00001542 // ... nor does SparcV9.
1543 if (Subtarget->is64Bit()) {
1544 setOperationAction(ISD::UREM, MVT::i64, Expand);
1545 setOperationAction(ISD::SREM, MVT::i64, Expand);
1546 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1547 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1548 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001549
1550 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001551 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1552 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001553 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1554 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001555
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001556 // Custom Expand fp<->uint
1557 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1558 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001559 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1560 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001561
Wesley Peck527da1b2010-11-23 03:31:01 +00001562 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1563 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001564
Chris Lattner0a1762e2008-03-17 03:21:36 +00001565 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001566 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1567 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1568 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001569 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1570
Owen Anderson9f944592009-08-11 20:47:22 +00001571 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1572 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1573 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001574 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001575
Chris Lattner0a1762e2008-03-17 03:21:36 +00001576 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001577 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1578 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1579 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1580 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1581 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1582 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001583 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001584
Owen Anderson9f944592009-08-11 20:47:22 +00001585 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1586 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1587 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001588 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001589
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001590 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1591 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1592
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001593 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001594 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1595 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1596 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1597 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001598 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1599 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001600 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1601 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001602 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001603 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001604
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001605 setOperationAction(ISD::CTPOP, MVT::i64,
1606 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001607 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001608 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001609 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001610 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1611 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001612 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001613 }
1614
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001615 // ATOMICs.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001616 // Atomics are supported on SparcV9. 32-bit atomics are also
1617 // supported by some Leon SparcV8 variants. Otherwise, atomics
1618 // are unsupported.
Chris Dewhurst3202f062016-07-08 15:33:56 +00001619 if (Subtarget->isV9() || Subtarget->hasLeonCasa())
Chris Dewhurstd534d3a2016-06-27 22:11:09 +00001620 setMaxAtomicSizeInBitsSupported(64);
James Y Knight19f6cce2016-04-12 20:18:48 +00001621 else
1622 setMaxAtomicSizeInBitsSupported(0);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001623
James Y Knight148a6462016-06-17 18:11:48 +00001624 setMinCmpXchgSizeInBits(32);
1625
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001626 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001627
1628 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1629
1630 // Custom Lower Atomic LOAD/STORE
1631 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1632 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1633
1634 if (Subtarget->is64Bit()) {
1635 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001636 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001637 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1638 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1639 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001640
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001641 if (!Subtarget->isV9()) {
1642 // SparcV8 does not have FNEGD and FABSD.
1643 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1644 setOperationAction(ISD::FABS, MVT::f64, Custom);
1645 }
1646
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001647 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1648 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1649 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1650 setOperationAction(ISD::FREM , MVT::f128, Expand);
1651 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001652 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1653 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001654 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001655 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001656 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001657 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1658 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001659 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001660 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001661 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001662 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1663 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1664 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1665 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1666 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001667 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001668 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1669 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001670 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001671 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1672 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001673
Owen Anderson9f944592009-08-11 20:47:22 +00001674 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1675 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1676 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001677
1678 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001679 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1680 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001681
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001682 if (Subtarget->is64Bit()) {
1683 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1684 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1685 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1686 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001687
1688 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1689 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001690
1691 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1692 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1693 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001694 }
1695
Chris Lattner0a1762e2008-03-17 03:21:36 +00001696 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001697 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001698 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001699 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001700
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001701 setOperationAction(ISD::TRAP , MVT::Other, Legal);
1702
Chris Lattner0a1762e2008-03-17 03:21:36 +00001703 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001704 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1705 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1706 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1707 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1708 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001709
Chris Lattner0a1762e2008-03-17 03:21:36 +00001710 setStackPointerRegisterToSaveRestore(SP::O6);
1711
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001712 setOperationAction(ISD::CTPOP, MVT::i32,
1713 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001714
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001715 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1716 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1717 setOperationAction(ISD::STORE, MVT::f128, Legal);
1718 } else {
1719 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1720 setOperationAction(ISD::STORE, MVT::f128, Custom);
1721 }
1722
1723 if (Subtarget->hasHardQuad()) {
1724 setOperationAction(ISD::FADD, MVT::f128, Legal);
1725 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1726 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1727 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1728 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1729 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1730 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1731 if (Subtarget->isV9()) {
1732 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1733 setOperationAction(ISD::FABS, MVT::f128, Legal);
1734 } else {
1735 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1736 setOperationAction(ISD::FABS, MVT::f128, Custom);
1737 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001738
1739 if (!Subtarget->is64Bit()) {
1740 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1741 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1742 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1743 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1744 }
1745
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001746 } else {
1747 // Custom legalize f128 operations.
1748
1749 setOperationAction(ISD::FADD, MVT::f128, Custom);
1750 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1751 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1752 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1753 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1754 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1755 setOperationAction(ISD::FABS, MVT::f128, Custom);
1756
1757 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1758 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1759 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1760
1761 // Setup Runtime library names.
Chris Dewhurst68388a02016-05-18 09:14:13 +00001762 if (Subtarget->is64Bit() && !Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001763 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1764 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1765 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1766 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1767 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1768 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001769 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001770 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001771 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001772 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1773 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1774 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1775 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001776 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1777 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1778 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1779 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
Chris Dewhurst68388a02016-05-18 09:14:13 +00001780 } else if (!Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001781 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1782 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1783 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1784 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1785 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1786 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001787 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001788 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001789 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001790 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1791 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1792 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1793 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001794 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1795 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1796 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1797 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1798 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001799 }
1800
Chris Dewhurst0c1e0022016-06-19 11:03:28 +00001801 if (Subtarget->fixAllFDIVSQRT()) {
1802 // Promote FDIVS and FSQRTS to FDIVD and FSQRTD instructions instead as
1803 // the former instructions generate errata on LEON processors.
1804 setOperationAction(ISD::FDIV, MVT::f32, Promote);
1805 setOperationAction(ISD::FSQRT, MVT::f32, Promote);
1806 }
1807
1808 if (Subtarget->replaceFMULS()) {
1809 // Promote FMULS to FMULD instructions instead as
1810 // the former instructions generate errata on LEON processors.
1811 setOperationAction(ISD::FMUL, MVT::f32, Promote);
1812 }
1813
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00001814 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1815
Eli Friedman2518f832011-05-06 20:34:06 +00001816 setMinFunctionAlignment(2);
1817
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001818 computeRegisterProperties(Subtarget->getRegisterInfo());
Chris Lattner0a1762e2008-03-17 03:21:36 +00001819}
1820
Chris Dewhurst68388a02016-05-18 09:14:13 +00001821bool SparcTargetLowering::useSoftFloat() const {
1822 return Subtarget->useSoftFloat();
1823}
1824
Chris Lattner0a1762e2008-03-17 03:21:36 +00001825const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001826 switch ((SPISD::NodeType)Opcode) {
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001827 case SPISD::FIRST_NUMBER: break;
1828 case SPISD::CMPICC: return "SPISD::CMPICC";
1829 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1830 case SPISD::BRICC: return "SPISD::BRICC";
1831 case SPISD::BRXCC: return "SPISD::BRXCC";
1832 case SPISD::BRFCC: return "SPISD::BRFCC";
1833 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1834 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1835 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1836 case SPISD::EH_SJLJ_SETJMP: return "SPISD::EH_SJLJ_SETJMP";
1837 case SPISD::EH_SJLJ_LONGJMP: return "SPISD::EH_SJLJ_LONGJMP";
1838 case SPISD::Hi: return "SPISD::Hi";
1839 case SPISD::Lo: return "SPISD::Lo";
1840 case SPISD::FTOI: return "SPISD::FTOI";
1841 case SPISD::ITOF: return "SPISD::ITOF";
1842 case SPISD::FTOX: return "SPISD::FTOX";
1843 case SPISD::XTOF: return "SPISD::XTOF";
1844 case SPISD::CALL: return "SPISD::CALL";
1845 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001846 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001847 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1848 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1849 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1850 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001851 }
Matthias Braund04893f2015-05-07 21:33:59 +00001852 return nullptr;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001853}
1854
Mehdi Amini44ede332015-07-09 02:09:04 +00001855EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
1856 EVT VT) const {
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001857 if (!VT.isVector())
1858 return MVT::i32;
1859 return VT.changeVectorElementTypeToInteger();
1860}
1861
Chris Lattner0a1762e2008-03-17 03:21:36 +00001862/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1863/// be zero. Op is expected to be a target specific node. Used by DAG
1864/// combiner.
Jay Foada0653a32014-05-14 21:14:37 +00001865void SparcTargetLowering::computeKnownBitsForTargetNode
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001866 (const SDValue Op,
1867 APInt &KnownZero,
1868 APInt &KnownOne,
1869 const SelectionDAG &DAG,
1870 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001871 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001872 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001873
Chris Lattner0a1762e2008-03-17 03:21:36 +00001874 switch (Op.getOpcode()) {
1875 default: break;
1876 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001877 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001878 case SPISD::SELECT_FCC:
Jay Foada0653a32014-05-14 21:14:37 +00001879 DAG.computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1880 DAG.computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001881
Chris Lattner0a1762e2008-03-17 03:21:36 +00001882 // Only known if known in both the LHS and RHS.
1883 KnownOne &= KnownOne2;
1884 KnownZero &= KnownZero2;
1885 break;
1886 }
1887}
1888
Chris Lattner0a1762e2008-03-17 03:21:36 +00001889// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1890// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001891static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001892 ISD::CondCode CC, unsigned &SPCC) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00001893 if (isNullConstant(RHS) &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001894 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001895 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1896 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001897 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1898 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1899 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00001900 isOneConstant(LHS.getOperand(0)) &&
1901 isNullConstant(LHS.getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001902 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001903 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001904 LHS = CMPCC.getOperand(0);
1905 RHS = CMPCC.getOperand(1);
1906 }
1907}
1908
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001909// Convert to a target node and set target flags.
1910SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1911 SelectionDAG &DAG) const {
1912 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1913 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001914 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001915 GA->getValueType(0),
1916 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001917
1918 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1919 return DAG.getTargetConstantPool(CP->getConstVal(),
1920 CP->getValueType(0),
1921 CP->getAlignment(),
1922 CP->getOffset(), TF);
1923
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001924 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1925 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1926 Op.getValueType(),
1927 0,
1928 TF);
1929
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001930 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1931 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1932 ES->getValueType(0), TF);
1933
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001934 llvm_unreachable("Unhandled address SDNode");
1935}
1936
1937// Split Op into high and low parts according to HiTF and LoTF.
1938// Return an ADD node combining the parts.
1939SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1940 unsigned HiTF, unsigned LoTF,
1941 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001942 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001943 EVT VT = Op.getValueType();
1944 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1945 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1946 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1947}
1948
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001949// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1950// or ExternalSymbol SDNode.
1951SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001952 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001953 EVT VT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001954
Rafael Espindola428b3e62016-06-27 19:15:08 +00001955 // Handle PIC mode first. SPARC needs a got load for every variable!
1956 if (isPositionIndependent()) {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001957 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001958 SDValue HiLo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1959 SparcMCExpr::VK_Sparc_GOT10, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001960 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1961 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001962 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1963 // function has calls.
1964 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1965 MFI->setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001966 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001967 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001968 }
1969
1970 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001971 switch(getTargetMachine().getCodeModel()) {
1972 default:
1973 llvm_unreachable("Unsupported absolute code model");
1974 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001975 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001976 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1977 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001978 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001979 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001980 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
1981 SparcMCExpr::VK_Sparc_M44, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001982 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001983 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001984 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1985 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1986 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001987 case CodeModel::Large: {
1988 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001989 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
1990 SparcMCExpr::VK_Sparc_HM, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001991 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001992 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1993 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001994 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1995 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001996 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001997}
1998
Wesley Peck527da1b2010-11-23 03:31:01 +00001999SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002000 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002001 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002002}
2003
Chris Lattner840c7002009-09-15 17:46:24 +00002004SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002005 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002006 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002007}
2008
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002009SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2010 SelectionDAG &DAG) const {
2011 return makeAddress(Op, DAG);
2012}
2013
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002014SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2015 SelectionDAG &DAG) const {
2016
2017 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002018 if (DAG.getTarget().Options.EmulatedTLS)
2019 return LowerToTLSEmulatedModel(GA, DAG);
2020
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002021 SDLoc DL(GA);
2022 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002023 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002024
2025 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2026
2027 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002028 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
2029 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
2030 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
2031 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
2032 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
2033 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
2034 unsigned addTF = ((model == TLSModel::GeneralDynamic)
2035 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
2036 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
2037 unsigned callTF = ((model == TLSModel::GeneralDynamic)
2038 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
2039 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002040
2041 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2042 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2043 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2044 withTargetFlags(Op, addTF, DAG));
2045
2046 SDValue Chain = DAG.getEntryNode();
2047 SDValue InFlag;
2048
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002049 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, DL, true), DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002050 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
2051 InFlag = Chain.getValue(1);
2052 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2053 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2054
2055 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopher9deb75d2015-03-11 22:42:13 +00002056 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2057 DAG.getMachineFunction(), CallingConv::C);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002058 assert(Mask && "Missing call preserved mask for calling convention");
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +00002059 SDValue Ops[] = {Chain,
2060 Callee,
2061 Symbol,
2062 DAG.getRegister(SP::O0, PtrVT),
2063 DAG.getRegisterMask(Mask),
2064 InFlag};
Craig Topper48d114b2014-04-26 18:35:24 +00002065 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002066 InFlag = Chain.getValue(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002067 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, DL, true),
2068 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002069 InFlag = Chain.getValue(1);
2070 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
2071
2072 if (model != TLSModel::LocalDynamic)
2073 return Ret;
2074
2075 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002076 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002077 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002078 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002079 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2080 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002081 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002082 }
2083
2084 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002085 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
2086 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002087
2088 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2089
2090 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2091 // function has calls.
2092 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2093 MFI->setHasCalls(true);
2094
2095 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002096 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
2097 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002098 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2099 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2100 DL, PtrVT, Ptr,
2101 withTargetFlags(Op, ldTF, DAG));
2102 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2103 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002104 withTargetFlags(Op,
2105 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002106 }
2107
2108 assert(model == TLSModel::LocalExec);
2109 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002110 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002111 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002112 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002113 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2114
2115 return DAG.getNode(ISD::ADD, DL, PtrVT,
2116 DAG.getRegister(SP::G7, PtrVT), Offset);
2117}
2118
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002119SDValue SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain,
2120 ArgListTy &Args, SDValue Arg,
2121 const SDLoc &DL,
2122 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002123 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2124 EVT ArgVT = Arg.getValueType();
2125 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2126
2127 ArgListEntry Entry;
2128 Entry.Node = Arg;
2129 Entry.Ty = ArgTy;
2130
2131 if (ArgTy->isFP128Ty()) {
2132 // Create a stack object and pass the pointer to the library function.
2133 int FI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002134 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00002135 Chain = DAG.getStore(Chain, DL, Entry.Node, FIPtr, MachinePointerInfo(),
2136 /* Alignment = */ 8);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002137
2138 Entry.Node = FIPtr;
2139 Entry.Ty = PointerType::getUnqual(ArgTy);
2140 }
2141 Args.push_back(Entry);
2142 return Chain;
2143}
2144
2145SDValue
2146SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2147 const char *LibFuncName,
2148 unsigned numArgs) const {
2149
2150 ArgListTy Args;
2151
2152 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002153 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002154
Mehdi Amini44ede332015-07-09 02:09:04 +00002155 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002156 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2157 Type *RetTyABI = RetTy;
2158 SDValue Chain = DAG.getEntryNode();
2159 SDValue RetPtr;
2160
2161 if (RetTy->isFP128Ty()) {
2162 // Create a Stack Object to receive the return value of type f128.
2163 ArgListEntry Entry;
2164 int RetFI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002165 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002166 Entry.Node = RetPtr;
2167 Entry.Ty = PointerType::getUnqual(RetTy);
2168 if (!Subtarget->is64Bit())
2169 Entry.isSRet = true;
2170 Entry.isReturned = false;
2171 Args.push_back(Entry);
2172 RetTyABI = Type::getVoidTy(*DAG.getContext());
2173 }
2174
2175 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2176 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2177 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2178 }
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002179 TargetLowering::CallLoweringInfo CLI(DAG);
2180 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002181 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002182
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002183 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2184
2185 // chain is in second result.
2186 if (RetTyABI == RetTy)
2187 return CallInfo.first;
2188
2189 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2190
2191 Chain = CallInfo.second;
2192
2193 // Load RetPtr to get the return value.
Justin Lebar9c375812016-07-15 18:27:10 +00002194 return DAG.getLoad(Op.getValueType(), SDLoc(Op), Chain, RetPtr,
2195 MachinePointerInfo(), /* Alignment = */ 8);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002196}
2197
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002198SDValue SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2199 unsigned &SPCC, const SDLoc &DL,
2200 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002201
Craig Topper062a2ba2014-04-25 05:30:21 +00002202 const char *LibCall = nullptr;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002203 bool is64Bit = Subtarget->is64Bit();
2204 switch(SPCC) {
2205 default: llvm_unreachable("Unhandled conditional code!");
2206 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2207 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2208 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2209 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2210 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2211 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2212 case SPCC::FCC_UL :
2213 case SPCC::FCC_ULE:
2214 case SPCC::FCC_UG :
2215 case SPCC::FCC_UGE:
2216 case SPCC::FCC_U :
2217 case SPCC::FCC_O :
2218 case SPCC::FCC_LG :
2219 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2220 }
2221
Mehdi Amini44ede332015-07-09 02:09:04 +00002222 auto PtrVT = getPointerTy(DAG.getDataLayout());
2223 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002224 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2225 ArgListTy Args;
2226 SDValue Chain = DAG.getEntryNode();
2227 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2228 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2229
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002230 TargetLowering::CallLoweringInfo CLI(DAG);
2231 CLI.setDebugLoc(DL).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002232 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002233
2234 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2235
2236 // result is in first, and chain is in second result.
2237 SDValue Result = CallInfo.first;
2238
2239 switch(SPCC) {
2240 default: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002241 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002242 SPCC = SPCC::ICC_NE;
2243 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2244 }
2245 case SPCC::FCC_UL : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002246 SDValue Mask = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002247 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002248 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002249 SPCC = SPCC::ICC_NE;
2250 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2251 }
2252 case SPCC::FCC_ULE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002253 SDValue RHS = DAG.getTargetConstant(2, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002254 SPCC = SPCC::ICC_NE;
2255 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2256 }
2257 case SPCC::FCC_UG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002258 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002259 SPCC = SPCC::ICC_G;
2260 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2261 }
2262 case SPCC::FCC_UGE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002263 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002264 SPCC = SPCC::ICC_NE;
2265 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2266 }
2267
2268 case SPCC::FCC_U : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002269 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002270 SPCC = SPCC::ICC_E;
2271 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2272 }
2273 case SPCC::FCC_O : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002274 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002275 SPCC = SPCC::ICC_NE;
2276 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2277 }
2278 case SPCC::FCC_LG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002279 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002280 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002281 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002282 SPCC = SPCC::ICC_NE;
2283 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2284 }
2285 case SPCC::FCC_UE : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002286 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002287 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002288 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002289 SPCC = SPCC::ICC_E;
2290 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2291 }
2292 }
2293}
2294
2295static SDValue
2296LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2297 const SparcTargetLowering &TLI) {
2298
2299 if (Op.getOperand(0).getValueType() == MVT::f64)
2300 return TLI.LowerF128Op(Op, DAG,
2301 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2302
2303 if (Op.getOperand(0).getValueType() == MVT::f32)
2304 return TLI.LowerF128Op(Op, DAG,
2305 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2306
2307 llvm_unreachable("fpextend with non-float operand!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002308 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002309}
2310
2311static SDValue
2312LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2313 const SparcTargetLowering &TLI) {
2314 // FP_ROUND on f64 and f32 are legal.
2315 if (Op.getOperand(0).getValueType() != MVT::f128)
2316 return Op;
2317
2318 if (Op.getValueType() == MVT::f64)
2319 return TLI.LowerF128Op(Op, DAG,
2320 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2321 if (Op.getValueType() == MVT::f32)
2322 return TLI.LowerF128Op(Op, DAG,
2323 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2324
2325 llvm_unreachable("fpround to non-float!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002326 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002327}
2328
2329static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2330 const SparcTargetLowering &TLI,
2331 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002332 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002333 EVT VT = Op.getValueType();
2334 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002335
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002336 // Expand f128 operations to fp128 abi calls.
2337 if (Op.getOperand(0).getValueType() == MVT::f128
2338 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2339 const char *libName = TLI.getLibcallName(VT == MVT::i32
2340 ? RTLIB::FPTOSINT_F128_I32
2341 : RTLIB::FPTOSINT_F128_I64);
2342 return TLI.LowerF128Op(Op, DAG, libName, 1);
2343 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002344
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002345 // Expand if the resulting type is illegal.
2346 if (!TLI.isTypeLegal(VT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002347 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002348
2349 // Otherwise, Convert the fp value to integer in an FP register.
2350 if (VT == MVT::i32)
2351 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2352 else
2353 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2354
2355 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002356}
2357
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002358static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2359 const SparcTargetLowering &TLI,
2360 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002361 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002362 EVT OpVT = Op.getOperand(0).getValueType();
2363 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2364
2365 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2366
2367 // Expand f128 operations to fp128 ABI calls.
2368 if (Op.getValueType() == MVT::f128
2369 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2370 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2371 ? RTLIB::SINTTOFP_I32_F128
2372 : RTLIB::SINTTOFP_I64_F128);
2373 return TLI.LowerF128Op(Op, DAG, libName, 1);
2374 }
2375
2376 // Expand if the operand type is illegal.
2377 if (!TLI.isTypeLegal(OpVT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002378 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002379
2380 // Otherwise, Convert the int value to FP in an FP register.
2381 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2382 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2383 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002384}
2385
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002386static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2387 const SparcTargetLowering &TLI,
2388 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002389 SDLoc dl(Op);
2390 EVT VT = Op.getValueType();
2391
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002392 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002393 // quad floating point instructions and the resulting type is legal.
2394 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2395 (hasHardQuad && TLI.isTypeLegal(VT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002396 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002397
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002398 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002399
2400 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002401 TLI.getLibcallName(VT == MVT::i32
2402 ? RTLIB::FPTOUINT_F128_I32
2403 : RTLIB::FPTOUINT_F128_I64),
2404 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002405}
2406
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002407static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2408 const SparcTargetLowering &TLI,
2409 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002410 SDLoc dl(Op);
2411 EVT OpVT = Op.getOperand(0).getValueType();
2412 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2413
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002414 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002415 // quad floating point instructions and the operand type is legal.
2416 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002417 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002418
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002419 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002420 TLI.getLibcallName(OpVT == MVT::i32
2421 ? RTLIB::UINTTOFP_I32_F128
2422 : RTLIB::UINTTOFP_I64_F128),
2423 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002424}
2425
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002426static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2427 const SparcTargetLowering &TLI,
2428 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002429 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002430 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002431 SDValue LHS = Op.getOperand(2);
2432 SDValue RHS = Op.getOperand(3);
2433 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002434 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002435 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002436
Chris Lattner0a1762e2008-03-17 03:21:36 +00002437 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2438 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2439 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002440
Chris Lattner0a1762e2008-03-17 03:21:36 +00002441 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002442 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002443 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002444 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002445 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002446 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2447 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002448 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002449 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2450 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2451 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2452 Opc = SPISD::BRICC;
2453 } else {
2454 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2455 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2456 Opc = SPISD::BRFCC;
2457 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002458 }
Owen Anderson9f944592009-08-11 20:47:22 +00002459 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002460 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002461}
2462
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002463static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2464 const SparcTargetLowering &TLI,
2465 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002466 SDValue LHS = Op.getOperand(0);
2467 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002468 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002469 SDValue TrueVal = Op.getOperand(2);
2470 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002471 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002472 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002473
Chris Lattner0a1762e2008-03-17 03:21:36 +00002474 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2475 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2476 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002477
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002478 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002479 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002480 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002481 Opc = LHS.getValueType() == MVT::i32 ?
2482 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002483 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2484 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002485 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2486 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2487 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2488 Opc = SPISD::SELECT_ICC;
2489 } else {
2490 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2491 Opc = SPISD::SELECT_FCC;
2492 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2493 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002494 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002495 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002496 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002497}
2498
Chris Dewhurst69fa1922016-05-04 09:33:30 +00002499SDValue SparcTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG,
2500 const SparcTargetLowering &TLI) const {
2501 SDLoc DL(Op);
2502 return DAG.getNode(SPISD::EH_SJLJ_SETJMP, DL,
2503 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), Op.getOperand(1));
2504
2505}
2506
2507SDValue SparcTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG,
2508 const SparcTargetLowering &TLI) const {
2509 SDLoc DL(Op);
2510 return DAG.getNode(SPISD::EH_SJLJ_LONGJMP, DL, MVT::Other, Op.getOperand(0), Op.getOperand(1));
2511}
2512
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002513static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002514 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002515 MachineFunction &MF = DAG.getMachineFunction();
2516 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002517 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002518
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002519 // Need frame address to find the address of VarArgsFrameIndex.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002520 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2521
Chris Lattner0a1762e2008-03-17 03:21:36 +00002522 // vastart just stores the address of the VarArgsFrameIndex slot into the
2523 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002524 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002525 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00002526 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2527 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002528 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002529 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00002530 MachinePointerInfo(SV));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002531}
2532
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002533static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002534 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002535 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002536 SDValue InChain = Node->getOperand(0);
2537 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002538 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002539 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002540 SDLoc DL(Node);
Justin Lebar9c375812016-07-15 18:27:10 +00002541 SDValue VAList =
2542 DAG.getLoad(PtrVT, DL, InChain, VAListPtr, MachinePointerInfo(SV));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002543 // Increment the pointer, VAList, to the next vaarg.
2544 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002545 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2546 DL));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002547 // Store the incremented VAList to the legalized pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00002548 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr, VAListPtr,
2549 MachinePointerInfo(SV));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002550 // Load the actual argument out of the pointer VAList.
2551 // We can't count on greater alignment than the word size.
2552 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002553 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits()) / 8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002554}
2555
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002556static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002557 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002558 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2559 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002560 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002561 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002562
Chris Lattner0a1762e2008-03-17 03:21:36 +00002563 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002564 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2565 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002566 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002567
Chris Lattner0a1762e2008-03-17 03:21:36 +00002568 // The resultant pointer is actually 16 words from the bottom of the stack,
2569 // to provide a register spill area.
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002570 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2571 regSpillArea += Subtarget->getStackPointerBias();
2572
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002573 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002574 DAG.getConstant(regSpillArea, dl, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002575 SDValue Ops[2] = { NewVal, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002576 return DAG.getMergeValues(Ops, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002577}
2578
Chris Lattner0a1762e2008-03-17 03:21:36 +00002579
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002580static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002581 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002582 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002583 dl, MVT::Other, DAG.getEntryNode());
2584 return Chain;
2585}
2586
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002587static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2588 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002589 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2590 MFI->setFrameAddressIsTaken(true);
2591
2592 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002593 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002594 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002595 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002596
2597 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002598
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002599 if (depth == 0) {
2600 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2601 if (Subtarget->is64Bit())
2602 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002603 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002604 return FrameAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002605 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002606
2607 // flush first to make sure the windowed registers' values are in stack
2608 SDValue Chain = getFLUSHW(Op, DAG);
2609 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2610
2611 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2612
2613 while (depth--) {
2614 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002615 DAG.getIntPtrConstant(Offset, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002616 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo());
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002617 }
2618 if (Subtarget->is64Bit())
2619 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002620 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002621 return FrameAddr;
2622}
2623
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002624
2625static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2626 const SparcSubtarget *Subtarget) {
2627
2628 uint64_t depth = Op.getConstantOperandVal(0);
2629
2630 return getFRAMEADDR(depth, Op, DAG, Subtarget);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002631}
2632
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002633static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002634 const SparcTargetLowering &TLI,
2635 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002636 MachineFunction &MF = DAG.getMachineFunction();
2637 MachineFrameInfo *MFI = MF.getFrameInfo();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002638 MFI->setReturnAddressIsTaken(true);
2639
Bill Wendling908bf812014-01-06 00:43:20 +00002640 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002641 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002642
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002643 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002644 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002645 uint64_t depth = Op.getConstantOperandVal(0);
2646
2647 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002648 if (depth == 0) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002649 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2650 unsigned RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002651 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002652 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002653 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002654
2655 // Need frame address to find return address of the caller.
2656 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2657
2658 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2659 SDValue Ptr = DAG.getNode(ISD::ADD,
2660 dl, VT,
2661 FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002662 DAG.getIntPtrConstant(Offset, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002663 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002664
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002665 return RetAddr;
2666}
2667
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002668static SDValue LowerF64Op(SDValue SrcReg64, const SDLoc &dl, SelectionDAG &DAG,
2669 unsigned opcode) {
James Y Knight51208ea2016-04-25 22:54:09 +00002670 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002671 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002672
2673 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2674 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2675 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2676
James Y Knight51208ea2016-04-25 22:54:09 +00002677 // Note: in little-endian, the floating-point value is stored in the
2678 // registers are in the opposite order, so the subreg with the sign
2679 // bit is the highest-numbered (odd), rather than the
2680 // lowest-numbered (even).
2681
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002682 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2683 SrcReg64);
2684 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2685 SrcReg64);
2686
James Y Knight51208ea2016-04-25 22:54:09 +00002687 if (DAG.getDataLayout().isLittleEndian())
2688 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2689 else
2690 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002691
2692 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2693 dl, MVT::f64), 0);
2694 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2695 DstReg64, Hi32);
2696 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2697 DstReg64, Lo32);
2698 return DstReg64;
2699}
2700
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002701// Lower a f128 load into two f64 loads.
2702static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2703{
2704 SDLoc dl(Op);
2705 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002706 assert(LdNode && LdNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002707 && "Unexpected node type");
2708
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002709 unsigned alignment = LdNode->getAlignment();
2710 if (alignment > 8)
2711 alignment = 8;
2712
Justin Lebar9c375812016-07-15 18:27:10 +00002713 SDValue Hi64 =
2714 DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LdNode->getBasePtr(),
2715 LdNode->getPointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002716 EVT addrVT = LdNode->getBasePtr().getValueType();
2717 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2718 LdNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002719 DAG.getConstant(8, dl, addrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00002720 SDValue Lo64 = DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LoPtr,
2721 LdNode->getPointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002722
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002723 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2724 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002725
2726 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2727 dl, MVT::f128);
2728 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2729 MVT::f128,
2730 SDValue(InFP128, 0),
2731 Hi64,
2732 SubRegEven);
2733 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2734 MVT::f128,
2735 SDValue(InFP128, 0),
2736 Lo64,
2737 SubRegOdd);
2738 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2739 SDValue(Lo64.getNode(), 1) };
Craig Topper48d114b2014-04-26 18:35:24 +00002740 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002741 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
Craig Topper64941d92014-04-27 19:20:57 +00002742 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002743}
2744
James Y Knight3994be82015-08-10 19:11:39 +00002745static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2746{
2747 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2748
2749 EVT MemVT = LdNode->getMemoryVT();
2750 if (MemVT == MVT::f128)
2751 return LowerF128Load(Op, DAG);
2752
2753 return Op;
2754}
2755
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002756// Lower a f128 store into two f64 stores.
2757static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2758 SDLoc dl(Op);
2759 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002760 assert(StNode && StNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002761 && "Unexpected node type");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002762 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2763 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002764
2765 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2766 dl,
2767 MVT::f64,
2768 StNode->getValue(),
2769 SubRegEven);
2770 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2771 dl,
2772 MVT::f64,
2773 StNode->getValue(),
2774 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002775
2776 unsigned alignment = StNode->getAlignment();
2777 if (alignment > 8)
2778 alignment = 8;
2779
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002780 SDValue OutChains[2];
Justin Lebar9c375812016-07-15 18:27:10 +00002781 OutChains[0] =
2782 DAG.getStore(StNode->getChain(), dl, SDValue(Hi64, 0),
2783 StNode->getBasePtr(), MachinePointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002784 EVT addrVT = StNode->getBasePtr().getValueType();
2785 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2786 StNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002787 DAG.getConstant(8, dl, addrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00002788 OutChains[1] = DAG.getStore(StNode->getChain(), dl, SDValue(Lo64, 0), LoPtr,
2789 MachinePointerInfo(), 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(),
Douglas Katzman26cfb6a2016-07-21 23:28:54 +00002808 St->getAlignment(), St->getMemOperand()->getFlags(), St->getAAInfo());
James Y Knight3994be82015-08-10 19:11:39 +00002809 return Chain;
2810 }
2811
2812 return SDValue();
2813}
2814
Roman Divacky7a9c6542014-02-27 19:26:29 +00002815static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
Venkatraman Govindaraju3b6b0e42014-03-01 02:28:34 +00002816 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
2817 && "invalid opcode");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002818
James Y Knight51208ea2016-04-25 22:54:09 +00002819 SDLoc dl(Op);
2820
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002821 if (Op.getValueType() == MVT::f64)
James Y Knight51208ea2016-04-25 22:54:09 +00002822 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002823 if (Op.getValueType() != MVT::f128)
2824 return Op;
2825
Roman Divacky7a9c6542014-02-27 19:26:29 +00002826 // Lower fabs/fneg on f128 to fabs/fneg on f64
2827 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
James Y Knight51208ea2016-04-25 22:54:09 +00002828 // (As with LowerF64Op, on little-endian, we need to negate the odd
2829 // subreg)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002830
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002831 SDValue SrcReg128 = Op.getOperand(0);
2832 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2833 SrcReg128);
2834 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2835 SrcReg128);
James Y Knight51208ea2016-04-25 22:54:09 +00002836
2837 if (DAG.getDataLayout().isLittleEndian()) {
2838 if (isV9)
2839 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
2840 else
2841 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
2842 } else {
2843 if (isV9)
2844 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2845 else
2846 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
2847 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002848
2849 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2850 dl, MVT::f128), 0);
2851 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2852 DstReg128, Hi64);
2853 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2854 DstReg128, Lo64);
2855 return DstReg128;
2856}
2857
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002858static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002859
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002860 if (Op.getValueType() != MVT::i64)
2861 return Op;
2862
2863 SDLoc dl(Op);
2864 SDValue Src1 = Op.getOperand(0);
2865 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2866 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002867 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002868 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2869
2870 SDValue Src2 = Op.getOperand(1);
2871 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2872 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002873 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002874 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2875
2876
2877 bool hasChain = false;
2878 unsigned hiOpc = Op.getOpcode();
2879 switch (Op.getOpcode()) {
2880 default: llvm_unreachable("Invalid opcode");
2881 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2882 case ISD::ADDE: hasChain = true; break;
2883 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2884 case ISD::SUBE: hasChain = true; break;
2885 }
2886 SDValue Lo;
2887 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2888 if (hasChain) {
2889 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2890 Op.getOperand(2));
2891 } else {
2892 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2893 }
2894 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2895 SDValue Carry = Hi.getValue(1);
2896
2897 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2898 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2899 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002900 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002901
2902 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2903 SDValue Ops[2] = { Dst, Carry };
Craig Topper64941d92014-04-27 19:20:57 +00002904 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002905}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002906
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002907// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2908// in LegalizeDAG.cpp except the order of arguments to the library function.
2909static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2910 const SparcTargetLowering &TLI)
2911{
2912 unsigned opcode = Op.getOpcode();
2913 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2914
2915 bool isSigned = (opcode == ISD::SMULO);
2916 EVT VT = MVT::i64;
2917 EVT WideVT = MVT::i128;
2918 SDLoc dl(Op);
2919 SDValue LHS = Op.getOperand(0);
2920
2921 if (LHS.getValueType() != VT)
2922 return Op;
2923
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002924 SDValue ShiftAmt = DAG.getConstant(63, dl, VT);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002925
2926 SDValue RHS = Op.getOperand(1);
2927 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2928 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2929 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2930
2931 SDValue MulResult = TLI.makeLibCall(DAG,
2932 RTLIB::MUL_I128, WideVT,
Craig Topper8fe40e02015-10-22 17:05:00 +00002933 Args, isSigned, dl).first;
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002934 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002935 MulResult, DAG.getIntPtrConstant(0, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002936 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002937 MulResult, DAG.getIntPtrConstant(1, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002938 if (isSigned) {
2939 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2940 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2941 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002942 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, dl, VT),
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002943 ISD::SETNE);
2944 }
2945 // MulResult is a node with an illegal type. Because such things are not
Chandler Carruthee1a1fc2014-08-02 00:24:54 +00002946 // generally permitted during this phase of legalization, ensure that
2947 // nothing is left using the node. The above EXTRACT_ELEMENT nodes should have
2948 // been folded.
2949 assert(MulResult->use_empty() && "Illegally typed node still in use!");
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002950
2951 SDValue Ops[2] = { BottomHalf, TopHalf } ;
Craig Topper64941d92014-04-27 19:20:57 +00002952 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002953}
2954
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002955static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00002956 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
2957 // Expand with a fence.
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002958 return SDValue();
JF Bastien800f87a2016-04-06 21:19:33 +00002959
2960 // Monotonic load/stores are legal.
2961 return Op;
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002962}
2963
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00002964SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2965 SelectionDAG &DAG) const {
2966 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2967 SDLoc dl(Op);
2968 switch (IntNo) {
2969 default: return SDValue(); // Don't custom lower most intrinsics.
2970 case Intrinsic::thread_pointer: {
2971 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2972 return DAG.getRegister(SP::G7, PtrVT);
2973 }
2974 }
2975}
2976
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002977SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00002978LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002979
2980 bool hasHardQuad = Subtarget->hasHardQuad();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002981 bool isV9 = Subtarget->isV9();
2982
Chris Lattner0a1762e2008-03-17 03:21:36 +00002983 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002984 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002985
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002986 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
2987 Subtarget);
2988 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
2989 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002990 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002991 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002992 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002993 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002994 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
2995 hasHardQuad);
2996 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
2997 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002998 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
2999 hasHardQuad);
3000 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3001 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003002 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
3003 hasHardQuad);
3004 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
3005 hasHardQuad);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003006 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG, *this);
3007 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG, *this);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003008 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3009 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00003010 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00003011 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00003012
James Y Knight3994be82015-08-10 19:11:39 +00003013 case ISD::LOAD: return LowerLOAD(Op, DAG);
3014 case ISD::STORE: return LowerSTORE(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003015 case ISD::FADD: return LowerF128Op(Op, DAG,
3016 getLibcallName(RTLIB::ADD_F128), 2);
3017 case ISD::FSUB: return LowerF128Op(Op, DAG,
3018 getLibcallName(RTLIB::SUB_F128), 2);
3019 case ISD::FMUL: return LowerF128Op(Op, DAG,
3020 getLibcallName(RTLIB::MUL_F128), 2);
3021 case ISD::FDIV: return LowerF128Op(Op, DAG,
3022 getLibcallName(RTLIB::DIV_F128), 2);
3023 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3024 getLibcallName(RTLIB::SQRT_F128),1);
Roman Divacky7a9c6542014-02-27 19:26:29 +00003025 case ISD::FABS:
3026 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003027 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3028 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00003029 case ISD::ADDC:
3030 case ISD::ADDE:
3031 case ISD::SUBC:
3032 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003033 case ISD::UMULO:
3034 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003035 case ISD::ATOMIC_LOAD:
3036 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003037 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003038 }
3039}
3040
3041MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003042SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00003043 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003044 switch (MI.getOpcode()) {
Chris Dewhurst3202f062016-07-08 15:33:56 +00003045 default: llvm_unreachable("Unknown Custom Instruction!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00003046 case SP::SELECT_CC_Int_ICC:
3047 case SP::SELECT_CC_FP_ICC:
3048 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003049 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003050 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003051 case SP::SELECT_CC_Int_FCC:
3052 case SP::SELECT_CC_FP_FCC:
3053 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003054 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003055 return expandSelectCC(MI, BB, SP::FBCOND);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003056 case SP::EH_SJLJ_SETJMP32ri:
3057 case SP::EH_SJLJ_SETJMP32rr:
3058 return emitEHSjLjSetJmp(MI, BB);
3059 case SP::EH_SJLJ_LONGJMP32rr:
3060 case SP::EH_SJLJ_LONGJMP32ri:
3061 return emitEHSjLjLongJmp(MI, BB);
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003062 }
3063}
3064
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003065MachineBasicBlock *
3066SparcTargetLowering::expandSelectCC(MachineInstr &MI, MachineBasicBlock *BB,
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003067 unsigned BROpcode) const {
Eric Christopherf5e94062015-01-30 23:46:43 +00003068 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003069 DebugLoc dl = MI.getDebugLoc();
3070 unsigned CC = (SPCC::CondCodes)MI.getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003071
Chris Lattner0a1762e2008-03-17 03:21:36 +00003072 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3073 // control-flow pattern. The incoming instruction knows the destination vreg
3074 // to set, the condition code register to branch on, the true/false values to
3075 // select between, and a branch opcode to use.
3076 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc3f79882015-10-20 00:59:43 +00003077 MachineFunction::iterator It = ++BB->getIterator();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003078
Chris Lattner0a1762e2008-03-17 03:21:36 +00003079 // thisMBB:
3080 // ...
3081 // TrueVal = ...
3082 // [f]bCC copy1MBB
3083 // fallthrough --> copy0MBB
3084 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003085 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00003086 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3087 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00003088 F->insert(It, copy0MBB);
3089 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003090
3091 // Transfer the remainder of BB and its successor edges to sinkMBB.
3092 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003093 std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00003094 BB->end());
3095 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3096
3097 // Add the true and fallthrough blocks as its successors.
3098 BB->addSuccessor(copy0MBB);
3099 BB->addSuccessor(sinkMBB);
3100
Dale Johannesen215a9252009-02-13 02:31:35 +00003101 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003102
Chris Lattner0a1762e2008-03-17 03:21:36 +00003103 // copy0MBB:
3104 // %FalseValue = ...
3105 // # fallthrough to sinkMBB
3106 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003107
Chris Lattner0a1762e2008-03-17 03:21:36 +00003108 // Update machine-CFG edges
3109 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003110
Chris Lattner0a1762e2008-03-17 03:21:36 +00003111 // sinkMBB:
3112 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3113 // ...
3114 BB = sinkMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003115 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI.getOperand(0).getReg())
3116 .addReg(MI.getOperand(2).getReg())
3117 .addMBB(copy0MBB)
3118 .addReg(MI.getOperand(1).getReg())
3119 .addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003120
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003121 MI.eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00003122 return BB;
3123}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003124
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003125MachineBasicBlock *
3126SparcTargetLowering::emitEHSjLjLongJmp(MachineInstr &MI,
3127 MachineBasicBlock *MBB) const {
3128 DebugLoc DL = MI.getDebugLoc();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003129 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3130
3131 MachineFunction *MF = MBB->getParent();
3132 MachineRegisterInfo &MRI = MF->getRegInfo();
3133 MachineInstrBuilder MIB;
3134
3135 MVT PVT = getPointerTy(MF->getDataLayout());
3136 unsigned RegSize = PVT.getStoreSize();
3137 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3138
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003139 unsigned Buf = MI.getOperand(0).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003140 unsigned JmpLoc = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3141
3142 // TO DO: If we do 64-bit handling, this perhaps should be FLUSHW, not TA 3
3143 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::TRAPri), SP::G0).addImm(3).addImm(SPCC::ICC_A);
3144
3145 // Instruction to restore FP
3146 const unsigned FP = SP::I6;
3147 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3148 .addReg(FP)
3149 .addReg(Buf)
3150 .addImm(0);
3151
3152 // Instruction to load jmp location
3153 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3154 .addReg(JmpLoc, RegState::Define)
3155 .addReg(Buf)
3156 .addImm(RegSize);
3157
3158 // Instruction to restore SP
3159 const unsigned SP = SP::O6;
3160 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3161 .addReg(SP)
3162 .addReg(Buf)
3163 .addImm(2 * RegSize);
3164
3165 // Instruction to restore I7
3166 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3167 .addReg(SP::I7)
3168 .addReg(Buf, RegState::Kill)
3169 .addImm(3 * RegSize);
3170
3171 // Jump to JmpLoc
3172 BuildMI(*MBB, MI, DL, TII->get(SP::JMPLrr)).addReg(SP::G0).addReg(JmpLoc, RegState::Kill).addReg(SP::G0);
3173
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003174 MI.eraseFromParent();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003175 return MBB;
3176}
3177
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003178MachineBasicBlock *
3179SparcTargetLowering::emitEHSjLjSetJmp(MachineInstr &MI,
3180 MachineBasicBlock *MBB) const {
3181 DebugLoc DL = MI.getDebugLoc();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003182 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3183
3184 MachineFunction *MF = MBB->getParent();
3185 MachineRegisterInfo &MRI = MF->getRegInfo();
3186 MachineInstrBuilder MIB;
3187
3188 MVT PVT = getPointerTy(MF->getDataLayout());
3189 unsigned RegSize = PVT.getStoreSize();
3190 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3191
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003192 unsigned DstReg = MI.getOperand(0).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003193 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
3194 assert(RC->hasType(MVT::i32) && "Invalid destination!");
3195 unsigned mainDstReg = MRI.createVirtualRegister(RC);
3196 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
3197
3198 // For v = setjmp(buf), we generate
3199 //
3200 // thisMBB:
3201 // buf[0] = FP
3202 // buf[RegSize] = restoreMBB <-- takes address of restoreMBB
3203 // buf[RegSize * 2] = O6
3204 // buf[RegSize * 3] = I7
3205 // Ensure restoreMBB remains in the relocations list (done using a bn instruction)
3206 // b mainMBB
3207 //
3208 // mainMBB:
3209 // v_main = 0
3210 // b sinkMBB
3211 //
3212 // restoreMBB:
3213 // v_restore = 1
3214 // --fall through--
3215 //
3216 // sinkMBB:
3217 // v = phi(main, restore)
3218
3219 const BasicBlock *BB = MBB->getBasicBlock();
3220 MachineFunction::iterator It = ++MBB->getIterator();
3221 MachineBasicBlock *thisMBB = MBB;
3222 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
3223 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
3224 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
3225
3226 MF->insert(It, mainMBB);
3227 MF->insert(It, restoreMBB);
3228 MF->insert(It, sinkMBB);
3229 restoreMBB->setHasAddressTaken();
3230
3231 // Transfer the remainder of BB and its successor edges to sinkMBB.
3232 sinkMBB->splice(sinkMBB->begin(), MBB,
3233 std::next(MachineBasicBlock::iterator(MI)),
3234 MBB->end());
3235 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
3236
3237 unsigned LabelReg = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3238 unsigned LabelReg2 = MRI.createVirtualRegister(&SP::IntRegsRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003239 unsigned BufReg = MI.getOperand(1).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003240
3241 // Instruction to store FP
3242 const unsigned FP = SP::I6;
3243 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3244 .addReg(BufReg)
3245 .addImm(0)
3246 .addReg(FP);
3247
3248 // Instructions to store jmp location
3249 MIB = BuildMI(thisMBB, DL, TII->get(SP::SETHIi))
3250 .addReg(LabelReg, RegState::Define)
3251 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_HI);
3252
3253 MIB = BuildMI(thisMBB, DL, TII->get(SP::ORri))
3254 .addReg(LabelReg2, RegState::Define)
3255 .addReg(LabelReg, RegState::Kill)
3256 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_LO);
3257
3258 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3259 .addReg(BufReg)
3260 .addImm(RegSize)
3261 .addReg(LabelReg2, RegState::Kill);
3262
3263 // Instruction to store SP
3264 const unsigned SP = SP::O6;
3265 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3266 .addReg(BufReg)
3267 .addImm(2 * RegSize)
3268 .addReg(SP);
3269
3270 // Instruction to store I7
3271 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3272 .addReg(BufReg)
3273 .addImm(3 * RegSize)
3274 .addReg(SP::I7);
3275
3276
3277 // FIX ME: This next instruction ensures that the restoreMBB block address remains
3278 // valid through optimization passes and serves no other purpose. The ICC_N ensures
3279 // that the branch is never taken. This commented-out code here was an alternative
3280 // attempt to achieve this which brought myriad problems.
3281 //MIB = BuildMI(thisMBB, DL, TII->get(SP::EH_SjLj_Setup)).addMBB(restoreMBB, SparcMCExpr::VK_Sparc_None);
3282 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3283 .addMBB(restoreMBB)
3284 .addImm(SPCC::ICC_N);
3285
3286 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3287 .addMBB(mainMBB)
3288 .addImm(SPCC::ICC_A);
3289
3290 thisMBB->addSuccessor(mainMBB);
3291 thisMBB->addSuccessor(restoreMBB);
3292
3293
3294 // mainMBB:
3295 MIB = BuildMI(mainMBB, DL, TII->get(SP::ORrr))
3296 .addReg(mainDstReg, RegState::Define)
3297 .addReg(SP::G0)
3298 .addReg(SP::G0);
3299 MIB = BuildMI(mainMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3300
3301 mainMBB->addSuccessor(sinkMBB);
3302
3303
3304 // restoreMBB:
3305 MIB = BuildMI(restoreMBB, DL, TII->get(SP::ORri))
3306 .addReg(restoreDstReg, RegState::Define)
3307 .addReg(SP::G0)
3308 .addImm(1);
3309 //MIB = BuildMI(restoreMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3310 restoreMBB->addSuccessor(sinkMBB);
3311
3312 // sinkMBB:
3313 MIB = BuildMI(*sinkMBB, sinkMBB->begin(), DL,
3314 TII->get(SP::PHI), DstReg)
3315 .addReg(mainDstReg).addMBB(mainMBB)
3316 .addReg(restoreDstReg).addMBB(restoreMBB);
3317
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003318 MI.eraseFromParent();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003319 return sinkMBB;
3320}
3321
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003322//===----------------------------------------------------------------------===//
3323// Sparc Inline Assembly Support
3324//===----------------------------------------------------------------------===//
3325
3326/// getConstraintType - Given a constraint letter, return the type of
3327/// constraint it is for this target.
3328SparcTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003329SparcTargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003330 if (Constraint.size() == 1) {
3331 switch (Constraint[0]) {
Chris Dewhurst3202f062016-07-08 15:33:56 +00003332 default:
3333 break;
3334 case 'f':
3335 case 'r':
3336 return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003337 case 'I': // SIMM13
3338 return C_Other;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003339 }
3340 }
3341
3342 return TargetLowering::getConstraintType(Constraint);
3343}
3344
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003345TargetLowering::ConstraintWeight SparcTargetLowering::
3346getSingleConstraintMatchWeight(AsmOperandInfo &info,
3347 const char *constraint) const {
3348 ConstraintWeight weight = CW_Invalid;
3349 Value *CallOperandVal = info.CallOperandVal;
3350 // If we don't have a value, we can't do a match,
3351 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003352 if (!CallOperandVal)
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003353 return CW_Default;
3354
3355 // Look at the constraint type.
3356 switch (*constraint) {
3357 default:
3358 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3359 break;
3360 case 'I': // SIMM13
3361 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3362 if (isInt<13>(C->getSExtValue()))
3363 weight = CW_Constant;
3364 }
3365 break;
3366 }
3367 return weight;
3368}
3369
3370/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3371/// vector. If it is invalid, don't add anything to Ops.
3372void SparcTargetLowering::
3373LowerAsmOperandForConstraint(SDValue Op,
3374 std::string &Constraint,
3375 std::vector<SDValue> &Ops,
3376 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003377 SDValue Result(nullptr, 0);
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003378
3379 // Only support length 1 constraints for now.
3380 if (Constraint.length() > 1)
3381 return;
3382
3383 char ConstraintLetter = Constraint[0];
3384 switch (ConstraintLetter) {
3385 default: break;
3386 case 'I':
3387 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3388 if (isInt<13>(C->getSExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003389 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
3390 Op.getValueType());
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003391 break;
3392 }
3393 return;
3394 }
3395 }
3396
3397 if (Result.getNode()) {
3398 Ops.push_back(Result);
3399 return;
3400 }
3401 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3402}
3403
Eric Christopher11e4df72015-02-26 22:38:43 +00003404std::pair<unsigned, const TargetRegisterClass *>
3405SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003406 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003407 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003408 if (Constraint.size() == 1) {
3409 switch (Constraint[0]) {
Chris Dewhurst3202f062016-07-08 15:33:56 +00003410 case 'f':
3411 return std::make_pair(0U, &SP::FPRegsRegClass);
3412
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003413 case 'r':
James Y Knight3994be82015-08-10 19:11:39 +00003414 if (VT == MVT::v2i32)
3415 return std::make_pair(0U, &SP::IntPairRegClass);
3416 else
3417 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003418 }
James Y Knight3994be82015-08-10 19:11:39 +00003419 } else if (!Constraint.empty() && Constraint.size() <= 5
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003420 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3421 // constraint = '{r<d>}'
3422 // Remove the braces from around the name.
3423 StringRef name(Constraint.data()+1, Constraint.size()-2);
3424 // Handle register aliases:
3425 // r0-r7 -> g0-g7
3426 // r8-r15 -> o0-o7
3427 // r16-r23 -> l0-l7
3428 // r24-r31 -> i0-i7
3429 uint64_t intVal = 0;
3430 if (name.substr(0, 1).equals("r")
3431 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3432 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3433 char regType = regTypes[intVal/8];
3434 char regIdx = '0' + (intVal % 8);
3435 char tmp[] = { '{', regType, regIdx, '}', 0 };
3436 std::string newConstraint = std::string(tmp);
Eric Christopher11e4df72015-02-26 22:38:43 +00003437 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3438 VT);
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003439 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003440 }
3441
Eric Christopher11e4df72015-02-26 22:38:43 +00003442 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003443}
3444
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003445bool
3446SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3447 // The Sparc target isn't yet aware of offsets.
3448 return false;
3449}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003450
3451void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3452 SmallVectorImpl<SDValue>& Results,
3453 SelectionDAG &DAG) const {
3454
3455 SDLoc dl(N);
3456
3457 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3458
3459 switch (N->getOpcode()) {
3460 default:
3461 llvm_unreachable("Do not know how to custom type legalize this operation!");
3462
3463 case ISD::FP_TO_SINT:
3464 case ISD::FP_TO_UINT:
3465 // Custom lower only if it involves f128 or i64.
3466 if (N->getOperand(0).getValueType() != MVT::f128
3467 || N->getValueType(0) != MVT::i64)
3468 return;
3469 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3470 ? RTLIB::FPTOSINT_F128_I64
3471 : RTLIB::FPTOUINT_F128_I64);
3472
3473 Results.push_back(LowerF128Op(SDValue(N, 0),
3474 DAG,
3475 getLibcallName(libCall),
3476 1));
3477 return;
3478
3479 case ISD::SINT_TO_FP:
3480 case ISD::UINT_TO_FP:
3481 // Custom lower only if it involves f128 or i64.
3482 if (N->getValueType(0) != MVT::f128
3483 || N->getOperand(0).getValueType() != MVT::i64)
3484 return;
3485
3486 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3487 ? RTLIB::SINTTOFP_I64_F128
3488 : RTLIB::UINTTOFP_I64_F128);
3489
3490 Results.push_back(LowerF128Op(SDValue(N, 0),
3491 DAG,
3492 getLibcallName(libCall),
3493 1));
3494 return;
James Y Knight3994be82015-08-10 19:11:39 +00003495 case ISD::LOAD: {
3496 LoadSDNode *Ld = cast<LoadSDNode>(N);
3497 // Custom handling only for i64: turn i64 load into a v2i32 load,
3498 // and a bitcast.
3499 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3500 return;
3501
3502 SDLoc dl(N);
3503 SDValue LoadRes = DAG.getExtLoad(
Justin Lebar9c375812016-07-15 18:27:10 +00003504 Ld->getExtensionType(), dl, MVT::v2i32, Ld->getChain(),
3505 Ld->getBasePtr(), Ld->getPointerInfo(), MVT::v2i32, Ld->getAlignment(),
3506 Ld->getMemOperand()->getFlags(), Ld->getAAInfo());
James Y Knight3994be82015-08-10 19:11:39 +00003507
3508 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3509 Results.push_back(Res);
3510 Results.push_back(LoadRes.getValue(1));
3511 return;
3512 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003513 }
3514}
Marcin Koscielnicki33571e22016-04-26 10:37:14 +00003515
3516// Override to enable LOAD_STACK_GUARD lowering on Linux.
3517bool SparcTargetLowering::useLoadStackGuardNode() const {
3518 if (!Subtarget->isTargetLinux())
3519 return TargetLowering::useLoadStackGuardNode();
3520 return true;
3521}
3522
3523// Override to disable global variable loading on Linux.
3524void SparcTargetLowering::insertSSPDeclarations(Module &M) const {
3525 if (!Subtarget->isTargetLinux())
3526 return TargetLowering::insertSSPDeclarations(M);
3527}