blob: 605acd6df054b170db65210c19180510b20332eb [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//===----------------------------------------------------------------------===//
37// Calling Convention Implementation
38//===----------------------------------------------------------------------===//
39
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000040static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
41 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
42 ISD::ArgFlagsTy &ArgFlags, CCState &State)
43{
44 assert (ArgFlags.isSRet());
45
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000046 // Assign SRet argument.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000047 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
48 0,
49 LocVT, LocInfo));
50 return true;
51}
52
James Y Knight3994be82015-08-10 19:11:39 +000053static bool CC_Sparc_Assign_Split_64(unsigned &ValNo, MVT &ValVT,
54 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
55 ISD::ArgFlagsTy &ArgFlags, CCState &State)
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000056{
Craig Topper840beec2014-04-04 05:16:06 +000057 static const MCPhysReg RegList[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000058 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
59 };
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000060 // Try to get first reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000061 if (unsigned Reg = State.AllocateReg(RegList)) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000062 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
63 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000064 // Assign whole thing in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000065 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
66 State.AllocateStack(8,4),
67 LocVT, LocInfo));
68 return true;
69 }
70
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000071 // Try to get second reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000072 if (unsigned Reg = State.AllocateReg(RegList))
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000073 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
74 else
75 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
76 State.AllocateStack(4,4),
77 LocVT, LocInfo));
78 return true;
79}
80
James Y Knight3994be82015-08-10 19:11:39 +000081static bool CC_Sparc_Assign_Ret_Split_64(unsigned &ValNo, MVT &ValVT,
82 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
83 ISD::ArgFlagsTy &ArgFlags, CCState &State)
84{
85 static const MCPhysReg RegList[] = {
86 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
87 };
88
89 // Try to get first reg.
90 if (unsigned Reg = State.AllocateReg(RegList))
91 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
92 else
93 return false;
94
95 // Try to get second reg.
96 if (unsigned Reg = State.AllocateReg(RegList))
97 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
98 else
99 return false;
100
101 return true;
102}
103
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000104// Allocate a full-sized argument for the 64-bit ABI.
105static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
106 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
107 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000108 assert((LocVT == MVT::f32 || LocVT == MVT::f128
109 || LocVT.getSizeInBits() == 64) &&
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000110 "Can't handle non-64 bits locations");
111
112 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000113 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
114 unsigned alignment = (LocVT == MVT::f128) ? 16 : 8;
115 unsigned Offset = State.AllocateStack(size, alignment);
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000116 unsigned Reg = 0;
117
118 if (LocVT == MVT::i64 && Offset < 6*8)
119 // Promote integers to %i0-%i5.
120 Reg = SP::I0 + Offset/8;
121 else if (LocVT == MVT::f64 && Offset < 16*8)
122 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
123 Reg = SP::D0 + Offset/8;
124 else if (LocVT == MVT::f32 && Offset < 16*8)
125 // Promote floats to %f1, %f3, ...
126 Reg = SP::F1 + Offset/4;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000127 else if (LocVT == MVT::f128 && Offset < 16*8)
128 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
129 Reg = SP::Q0 + Offset/16;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000130
131 // Promote to register when possible, otherwise use the stack slot.
132 if (Reg) {
133 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
134 return true;
135 }
136
137 // This argument goes on the stack in an 8-byte slot.
138 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
139 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
140 if (LocVT == MVT::f32)
141 Offset += 4;
142
143 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
144 return true;
145}
146
147// Allocate a half-sized argument for the 64-bit ABI.
148//
149// This is used when passing { float, int } structs by value in registers.
150static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
151 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
152 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
153 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
154 unsigned Offset = State.AllocateStack(4, 4);
155
156 if (LocVT == MVT::f32 && Offset < 16*8) {
157 // Promote floats to %f0-%f31.
158 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
159 LocVT, LocInfo));
160 return true;
161 }
162
163 if (LocVT == MVT::i32 && Offset < 6*8) {
164 // Promote integers to %i0-%i5, using half the register.
165 unsigned Reg = SP::I0 + Offset/8;
166 LocVT = MVT::i64;
167 LocInfo = CCValAssign::AExt;
168
169 // Set the Custom bit if this i32 goes in the high bits of a register.
170 if (Offset % 8 == 0)
171 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
172 LocVT, LocInfo));
173 else
174 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
175 return true;
176 }
177
178 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
179 return true;
180}
181
Chris Lattner49b269d2008-03-17 05:41:48 +0000182#include "SparcGenCallingConv.inc"
183
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000184// The calling conventions in SparcCallingConv.td are described in terms of the
185// callee's register window. This function translates registers to the
186// corresponding caller window %o register.
187static unsigned toCallerWindow(unsigned Reg) {
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +0000188 static_assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7,
189 "Unexpected enum");
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000190 if (Reg >= SP::I0 && Reg <= SP::I7)
191 return Reg - SP::I0 + SP::O0;
192 return Reg;
193}
194
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000195SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000196SparcTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
197 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000198 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000199 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000200 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000201 if (Subtarget->is64Bit())
202 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
203 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
204}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000205
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000206SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000207SparcTargetLowering::LowerReturn_32(SDValue Chain, CallingConv::ID CallConv,
208 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000209 const SmallVectorImpl<ISD::OutputArg> &Outs,
210 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000211 const SDLoc &DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000212 MachineFunction &MF = DAG.getMachineFunction();
213
Chris Lattner49b269d2008-03-17 05:41:48 +0000214 // CCValAssign - represent the assignment of the return value to locations.
215 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000216
Chris Lattner49b269d2008-03-17 05:41:48 +0000217 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000218 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
219 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000220
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000221 // Analyze return values.
222 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000223
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000224 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000225 SmallVector<SDValue, 4> RetOps(1, Chain);
226 // Make room for the return address offset.
227 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000228
229 // Copy the result values into the output registers.
James Y Knight3994be82015-08-10 19:11:39 +0000230 for (unsigned i = 0, realRVLocIdx = 0;
231 i != RVLocs.size();
232 ++i, ++realRVLocIdx) {
Chris Lattner49b269d2008-03-17 05:41:48 +0000233 CCValAssign &VA = RVLocs[i];
234 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000235
James Y Knight3994be82015-08-10 19:11:39 +0000236 SDValue Arg = OutVals[realRVLocIdx];
237
238 if (VA.needsCustom()) {
239 assert(VA.getLocVT() == MVT::v2i32);
240 // Legalize ret v2i32 -> ret 2 x i32 (Basically: do what would
241 // happen by default if this wasn't a legal type)
242
243 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
244 Arg,
245 DAG.getConstant(0, DL, getVectorIdxTy(DAG.getDataLayout())));
246 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
247 Arg,
248 DAG.getConstant(1, DL, getVectorIdxTy(DAG.getDataLayout())));
249
250 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part0, Flag);
251 Flag = Chain.getValue(1);
252 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
253 VA = RVLocs[++i]; // skip ahead to next loc
254 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part1,
255 Flag);
256 } else
257 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000258
Chris Lattner49b269d2008-03-17 05:41:48 +0000259 // Guarantee that all emitted copies are stuck together with flags.
260 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000261 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000262 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000263
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000264 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000265 // If the function returns a struct, copy the SRetReturnReg to I0
266 if (MF.getFunction()->hasStructRetAttr()) {
267 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
268 unsigned Reg = SFI->getSRetReturnReg();
269 if (!Reg)
270 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +0000271 auto PtrVT = getPointerTy(DAG.getDataLayout());
272 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, PtrVT);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000273 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000274 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +0000275 RetOps.push_back(DAG.getRegister(SP::I0, PtrVT));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000276 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000277 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000278
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000279 RetOps[0] = Chain; // Update chain.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000280 RetOps[1] = DAG.getConstant(RetAddrOffset, DL, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000281
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000282 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000283 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000284 RetOps.push_back(Flag);
285
Craig Topper48d114b2014-04-26 18:35:24 +0000286 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000287}
288
289// Lower return values for the 64-bit ABI.
290// Return values are passed the exactly the same way as function arguments.
291SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000292SparcTargetLowering::LowerReturn_64(SDValue Chain, CallingConv::ID CallConv,
293 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000294 const SmallVectorImpl<ISD::OutputArg> &Outs,
295 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000296 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000297 // CCValAssign - represent the assignment of the return value to locations.
298 SmallVector<CCValAssign, 16> RVLocs;
299
300 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000301 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
302 *DAG.getContext());
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000303
304 // Analyze return values.
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +0000305 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000306
307 SDValue Flag;
308 SmallVector<SDValue, 4> RetOps(1, Chain);
309
310 // The second operand on the return instruction is the return address offset.
311 // The return address is always %i7+8 with the 64-bit ABI.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000312 RetOps.push_back(DAG.getConstant(8, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000313
314 // Copy the result values into the output registers.
315 for (unsigned i = 0; i != RVLocs.size(); ++i) {
316 CCValAssign &VA = RVLocs[i];
317 assert(VA.isRegLoc() && "Can only return in registers!");
318 SDValue OutVal = OutVals[i];
319
320 // Integer return values must be sign or zero extended by the callee.
321 switch (VA.getLocInfo()) {
Lang Hames06234ec2014-01-14 19:56:36 +0000322 case CCValAssign::Full: break;
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000323 case CCValAssign::SExt:
324 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
325 break;
326 case CCValAssign::ZExt:
327 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
328 break;
329 case CCValAssign::AExt:
330 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000331 break;
Lang Hames06234ec2014-01-14 19:56:36 +0000332 default:
333 llvm_unreachable("Unknown loc info!");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000334 }
335
336 // The custom bit on an i32 return value indicates that it should be passed
337 // in the high bits of the register.
338 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
339 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000340 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000341
342 // The next value may go in the low bits of the same register.
343 // Handle both at once.
344 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
345 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
346 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
347 // Skip the next value, it's already done.
348 ++i;
349 }
350 }
351
352 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
353
354 // Guarantee that all emitted copies are stuck together with flags.
355 Flag = Chain.getValue(1);
356 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
357 }
358
359 RetOps[0] = Chain; // Update chain.
360
361 // Add the flag if we have it.
362 if (Flag.getNode())
363 RetOps.push_back(Flag);
364
Craig Topper48d114b2014-04-26 18:35:24 +0000365 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Chris Lattner49b269d2008-03-17 05:41:48 +0000366}
367
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000368SDValue SparcTargetLowering::LowerFormalArguments(
369 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
370 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
371 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000372 if (Subtarget->is64Bit())
373 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
374 DL, DAG, InVals);
375 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
376 DL, DAG, InVals);
377}
378
379/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000380/// passed in either one or two GPRs, including FP values. TODO: we should
381/// pass FP values in FP registers for fastcc functions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000382SDValue SparcTargetLowering::LowerFormalArguments_32(
383 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
384 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
385 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000386 MachineFunction &MF = DAG.getMachineFunction();
387 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000388 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000389
390 // Assign locations to all of the incoming arguments.
391 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000392 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
393 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000394 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000395
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000396 const unsigned StackOffset = 92;
James Y Knight33beb242015-12-15 19:23:12 +0000397 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000398
Reid Kleckner79418562014-05-09 22:32:13 +0000399 unsigned InIdx = 0;
400 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000401 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000402
Reid Kleckner79418562014-05-09 22:32:13 +0000403 if (Ins[InIdx].Flags.isSRet()) {
404 if (InIdx != 0)
405 report_fatal_error("sparc only supports sret on the first parameter");
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000406 // Get SRet from [%fp+64].
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000407 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
408 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
409 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
410 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000411 false, false, false, 0);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000412 InVals.push_back(Arg);
413 continue;
414 }
415
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000416 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000417 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000418 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
419
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000420 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
421 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
422 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000423
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000424 assert(i+1 < e);
425 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000426
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000427 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000428 if (NextVA.isMemLoc()) {
429 int FrameIdx = MF.getFrameInfo()->
430 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000431 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000432 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
433 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000434 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000435 } else {
436 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000437 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000438 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000439 }
James Y Knight33beb242015-12-15 19:23:12 +0000440
441 if (IsLittleEndian)
442 std::swap(LoVal, HiVal);
443
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000444 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000445 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000446 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000447 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000448 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000449 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000450 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
451 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
452 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
453 if (VA.getLocVT() == MVT::f32)
454 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
455 else if (VA.getLocVT() != MVT::i32) {
456 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
457 DAG.getValueType(VA.getLocVT()));
458 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
459 }
460 InVals.push_back(Arg);
461 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000462 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000463
464 assert(VA.isMemLoc());
465
466 unsigned Offset = VA.getLocMemOffset()+StackOffset;
Mehdi Amini44ede332015-07-09 02:09:04 +0000467 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000468
469 if (VA.needsCustom()) {
Hans Wennborg1f094852016-04-11 20:35:41 +0000470 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000471 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000472 if (Offset % 8 == 0) {
473 int FI = MF.getFrameInfo()->CreateFixedObject(8,
474 Offset,
475 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000476 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000477 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
478 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000479 false,false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000480 InVals.push_back(Load);
481 continue;
482 }
483
484 int FI = MF.getFrameInfo()->CreateFixedObject(4,
485 Offset,
486 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000487 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000488 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
489 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000490 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000491 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
492 Offset+4,
493 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000494 SDValue FIPtr2 = DAG.getFrameIndex(FI2, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000495
496 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
497 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000498 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000499
James Y Knight33beb242015-12-15 19:23:12 +0000500 if (IsLittleEndian)
501 std::swap(LoVal, HiVal);
502
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000503 SDValue WholeValue =
504 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000505 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000506 InVals.push_back(WholeValue);
507 continue;
508 }
509
510 int FI = MF.getFrameInfo()->CreateFixedObject(4,
511 Offset,
512 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000513 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000514 SDValue Load ;
515 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
516 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
517 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000518 false, false, false, 0);
James Y Knight33beb242015-12-15 19:23:12 +0000519 } else if (VA.getValVT() == MVT::f128) {
520 report_fatal_error("SPARCv8 does not handle f128 in calls; "
521 "pass indirectly");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000522 } else {
James Y Knight33beb242015-12-15 19:23:12 +0000523 // We shouldn't see any other value types here.
James Y Knight99fcb722015-12-15 23:07:16 +0000524 llvm_unreachable("Unexpected ValVT encountered in frame lowering.");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000525 }
526 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000527 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000528
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000529 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000530 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000531 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
532 unsigned Reg = SFI->getSRetReturnReg();
533 if (!Reg) {
534 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
535 SFI->setSRetReturnReg(Reg);
536 }
537 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
538 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
539 }
540
Chris Lattner49b269d2008-03-17 05:41:48 +0000541 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000542 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +0000543 static const MCPhysReg ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000544 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
545 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000546 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs);
Craig Topper840beec2014-04-04 05:16:06 +0000547 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000548 unsigned ArgOffset = CCInfo.getNextStackOffset();
549 if (NumAllocated == 6)
550 ArgOffset += StackOffset;
551 else {
552 assert(!ArgOffset);
553 ArgOffset = 68+4*NumAllocated;
554 }
555
Chris Lattner49b269d2008-03-17 05:41:48 +0000556 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000557 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000558
Eli Friedmanbe853b72009-07-19 19:53:46 +0000559 std::vector<SDValue> OutChains;
560
Chris Lattner49b269d2008-03-17 05:41:48 +0000561 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
562 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
563 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000564 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000565
David Greene1fbe0542009-11-12 20:49:22 +0000566 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Cheng0664a672010-07-03 00:40:23 +0000567 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000568 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000569
Chris Lattner676c61d2010-09-21 18:41:36 +0000570 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
571 MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000572 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000573 ArgOffset += 4;
574 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000575
576 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000577 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +0000578 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Eli Friedmanbe853b72009-07-19 19:53:46 +0000579 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000580 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000581
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000582 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000583}
584
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000585// Lower formal arguments for the 64 bit ABI.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000586SDValue SparcTargetLowering::LowerFormalArguments_64(
587 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
588 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
589 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000590 MachineFunction &MF = DAG.getMachineFunction();
591
592 // Analyze arguments according to CC_Sparc64.
593 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000594 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
595 *DAG.getContext());
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000596 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
597
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000598 // The argument array begins at %fp+BIAS+128, after the register save area.
599 const unsigned ArgArea = 128;
600
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000601 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
602 CCValAssign &VA = ArgLocs[i];
603 if (VA.isRegLoc()) {
604 // This argument is passed in a register.
605 // All integer register arguments are promoted by the caller to i64.
606
607 // Create a virtual register for the promoted live-in value.
608 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
609 getRegClassFor(VA.getLocVT()));
610 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
611
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000612 // Get the high bits for i32 struct elements.
613 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
614 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000615 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000616
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000617 // The caller promoted the argument, so insert an Assert?ext SDNode so we
618 // won't promote the value again in this function.
619 switch (VA.getLocInfo()) {
620 case CCValAssign::SExt:
621 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
622 DAG.getValueType(VA.getValVT()));
623 break;
624 case CCValAssign::ZExt:
625 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
626 DAG.getValueType(VA.getValVT()));
627 break;
628 default:
629 break;
630 }
631
632 // Truncate the register down to the argument type.
633 if (VA.isExtInLoc())
634 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
635
636 InVals.push_back(Arg);
637 continue;
638 }
639
640 // The registers are exhausted. This argument was passed on the stack.
641 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000642 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
643 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000644 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000645 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
646 // Adjust offset for extended arguments, SPARC is big-endian.
647 // The caller will have written the full slot with extended bytes, but we
648 // prefer our own extending loads.
649 if (VA.isExtInLoc())
650 Offset += 8 - ValSize;
651 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000652 InVals.push_back(DAG.getLoad(
653 VA.getValVT(), DL, Chain,
654 DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
Alex Lorenze40c8a22015-08-11 23:09:45 +0000655 MachinePointerInfo::getFixedStack(MF, FI), false, false, false, 0));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000656 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000657
658 if (!IsVarArg)
659 return Chain;
660
661 // This function takes variable arguments, some of which may have been passed
662 // in registers %i0-%i5. Variable floating point arguments are never passed
663 // in floating point registers. They go on %i0-%i5 or on the stack like
664 // integer arguments.
665 //
666 // The va_start intrinsic needs to know the offset to the first variable
667 // argument.
668 unsigned ArgOffset = CCInfo.getNextStackOffset();
669 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
670 // Skip the 128 bytes of register save area.
671 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
672 Subtarget->getStackPointerBias());
673
674 // Save the variable arguments that were passed in registers.
675 // The caller is required to reserve stack space for 6 arguments regardless
676 // of how many arguments were actually passed.
677 SmallVector<SDValue, 8> OutChains;
678 for (; ArgOffset < 6*8; ArgOffset += 8) {
679 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
680 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
681 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000682 auto PtrVT = getPointerTy(MF.getDataLayout());
Alex Lorenze40c8a22015-08-11 23:09:45 +0000683 OutChains.push_back(DAG.getStore(
684 Chain, DL, VArg, DAG.getFrameIndex(FI, PtrVT),
685 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000686 }
687
688 if (!OutChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000689 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000690
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000691 return Chain;
692}
693
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000694SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000695SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000696 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000697 if (Subtarget->is64Bit())
698 return LowerCall_64(CLI, InVals);
699 return LowerCall_32(CLI, InVals);
700}
701
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000702static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
703 ImmutableCallSite *CS) {
704 if (CS)
705 return CS->hasFnAttr(Attribute::ReturnsTwice);
706
Craig Topper062a2ba2014-04-25 05:30:21 +0000707 const Function *CalleeFn = nullptr;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000708 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
709 CalleeFn = dyn_cast<Function>(G->getGlobal());
710 } else if (ExternalSymbolSDNode *E =
711 dyn_cast<ExternalSymbolSDNode>(Callee)) {
712 const Function *Fn = DAG.getMachineFunction().getFunction();
713 const Module *M = Fn->getParent();
714 const char *CalleeName = E->getSymbol();
715 CalleeFn = M->getFunction(CalleeName);
716 }
717
718 if (!CalleeFn)
719 return false;
720 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
721}
722
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000723// Lower a call for the 32-bit ABI.
724SDValue
725SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
726 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000727 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000728 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000729 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
730 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
731 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000732 SDValue Chain = CLI.Chain;
733 SDValue Callee = CLI.Callee;
734 bool &isTailCall = CLI.IsTailCall;
735 CallingConv::ID CallConv = CLI.CallConv;
736 bool isVarArg = CLI.IsVarArg;
737
Evan Cheng67a69dd2010-01-27 00:07:07 +0000738 // Sparc target does not yet support tail call optimization.
739 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000740
Chris Lattner7d4152b2008-03-17 06:58:37 +0000741 // Analyze operands of the call, assigning locations to each operand.
742 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000743 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
744 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000745 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000746
Chris Lattner7d4152b2008-03-17 06:58:37 +0000747 // Get the size of the outgoing arguments stack space requirement.
748 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000749
Chris Lattner49b269d2008-03-17 05:41:48 +0000750 // Keep stack frames 8-byte aligned.
751 ArgsSize = (ArgsSize+7) & ~7;
752
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000753 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
754
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000755 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000756 SmallVector<SDValue, 8> ByValArgs;
757 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
758 ISD::ArgFlagsTy Flags = Outs[i].Flags;
759 if (!Flags.isByVal())
760 continue;
761
762 SDValue Arg = OutVals[i];
763 unsigned Size = Flags.getByValSize();
764 unsigned Align = Flags.getByValAlign();
765
Chris Dewhurst53bde952016-06-01 08:48:56 +0000766 if (Size > 0U) {
767 int FI = MFI->CreateStackObject(Size, Align, false);
768 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
769 SDValue SizeNode = DAG.getConstant(Size, dl, MVT::i32);
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000770
Chris Dewhurst53bde952016-06-01 08:48:56 +0000771 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
772 false, // isVolatile,
773 (Size <= 32), // AlwaysInline if size <= 32,
774 false, // isTailCall
775 MachinePointerInfo(), MachinePointerInfo());
776 ByValArgs.push_back(FIPtr);
777 }
778 else {
779 SDValue nullVal;
780 ByValArgs.push_back(nullVal);
781 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000782 }
783
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000784 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000785 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000786
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000787 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
788 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000789
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000790 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000791 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000792 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000793 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000794 i != e;
795 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000796 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000797 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000798
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000799 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
800
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000801 // Use local copy if it is a byval arg.
Chris Dewhurst53bde952016-06-01 08:48:56 +0000802 if (Flags.isByVal()) {
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000803 Arg = ByValArgs[byvalArgIdx++];
Chris Dewhurst53bde952016-06-01 08:48:56 +0000804 if (!Arg) {
805 continue;
806 }
807 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000808
Chris Lattner7d4152b2008-03-17 06:58:37 +0000809 // Promote the value if needed.
810 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000811 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000812 case CCValAssign::Full: break;
813 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000814 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000815 break;
816 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000817 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000818 break;
819 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000820 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
821 break;
822 case CCValAssign::BCvt:
823 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000824 break;
825 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000826
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000827 if (Flags.isSRet()) {
828 assert(VA.needsCustom());
829 // store SRet argument in %sp+64
830 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000831 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000832 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
833 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
834 MachinePointerInfo(),
835 false, false, 0));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000836 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000837 continue;
838 }
839
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000840 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000841 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000842
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000843 if (VA.isMemLoc()) {
844 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000845 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000846 if (Offset % 8 == 0) {
847 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000848 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000849 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
850 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
851 MachinePointerInfo(),
852 false, false, 0));
853 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000854 }
855 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000856
James Y Knight3994be82015-08-10 19:11:39 +0000857 if (VA.getLocVT() == MVT::f64) {
858 // Move from the float value from float registers into the
859 // integer registers.
860
James Y Knight692e0372015-10-09 21:36:19 +0000861 // TODO: The f64 -> v2i32 conversion is super-inefficient for
862 // constants: it sticks them in the constant pool, then loads
863 // to a fp register, then stores to temp memory, then loads to
864 // integer registers.
James Y Knight3994be82015-08-10 19:11:39 +0000865 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
866 }
867
868 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
869 Arg,
870 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
871 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
872 Arg,
873 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000874
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000875 if (VA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000876 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000877 assert(i+1 != e);
878 CCValAssign &NextVA = ArgLocs[++i];
879 if (NextVA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000880 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000881 } else {
James Y Knight3994be82015-08-10 19:11:39 +0000882 // Store the second part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000883 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
884 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000885 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000886 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000887 MemOpChains.push_back(DAG.getStore(Chain, dl, Part1, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000888 MachinePointerInfo(),
889 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000890 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000891 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000892 unsigned Offset = VA.getLocMemOffset() + StackOffset;
James Y Knight3994be82015-08-10 19:11:39 +0000893 // Store the first part.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000894 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000895 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000896 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000897 MemOpChains.push_back(DAG.getStore(Chain, dl, Part0, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000898 MachinePointerInfo(),
899 false, false, 0));
James Y Knight3994be82015-08-10 19:11:39 +0000900 // Store the second part.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000901 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000902 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000903 MemOpChains.push_back(DAG.getStore(Chain, dl, Part1, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000904 MachinePointerInfo(),
905 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000906 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000907 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000908 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000909
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000910 // Arguments that can be passed on register must be kept at
911 // RegsToPass vector
912 if (VA.isRegLoc()) {
913 if (VA.getLocVT() != MVT::f32) {
914 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
915 continue;
916 }
917 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
918 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
919 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000920 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000921
922 assert(VA.isMemLoc());
923
924 // Create a store off the stack pointer for this argument.
925 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000926 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
927 dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000928 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
929 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
930 MachinePointerInfo(),
931 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000932 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000933
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000934
Chris Lattner49b269d2008-03-17 05:41:48 +0000935 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000936 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000937 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000938
939 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000940 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000941 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000942 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000943 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000944 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000945 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000946 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000947 InFlag = Chain.getValue(1);
948 }
949
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000950 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000951 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000952
Chris Lattner49b269d2008-03-17 05:41:48 +0000953 // If the callee is a GlobalAddress node (quite common, every direct call is)
954 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000955 // Likewise ExternalSymbol -> TargetExternalSymbol.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000956 unsigned TF = ((getTargetMachine().getRelocationModel() == Reloc::PIC_)
957 ? SparcMCExpr::VK_Sparc_WPLT30 : 0);
Chris Lattner49b269d2008-03-17 05:41:48 +0000958 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000959 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000960 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000961 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000962
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000963 // Returns a chain & a flag for retval copy to use
964 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
965 SmallVector<SDValue, 8> Ops;
966 Ops.push_back(Chain);
967 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000968 if (hasStructRetAttr)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000969 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000970 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
971 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
972 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000973
974 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000975 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000976 const uint32_t *Mask =
977 ((hasReturnsTwice)
978 ? TRI->getRTCallPreservedMask(CallConv)
979 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000980 assert(Mask && "Missing call preserved mask for calling convention");
981 Ops.push_back(DAG.getRegisterMask(Mask));
982
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000983 if (InFlag.getNode())
984 Ops.push_back(InFlag);
985
Craig Topper48d114b2014-04-26 18:35:24 +0000986 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
Chris Lattner49b269d2008-03-17 05:41:48 +0000987 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000988
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000989 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
990 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000991 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000992
Chris Lattnerdb26db22008-03-17 06:01:07 +0000993 // Assign locations to each value returned by this call.
994 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000995 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
996 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000997
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000998 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000999
Chris Lattnerdb26db22008-03-17 06:01:07 +00001000 // Copy all of the result registers out of their specified physreg.
1001 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Nirav Dave29938542016-02-26 18:55:22 +00001002 if (RVLocs[i].getLocVT() == MVT::v2i32) {
1003 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
1004 SDValue Lo = DAG.getCopyFromReg(
1005 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InFlag);
1006 Chain = Lo.getValue(1);
1007 InFlag = Lo.getValue(2);
1008 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
1009 DAG.getConstant(0, dl, MVT::i32));
1010 SDValue Hi = DAG.getCopyFromReg(
1011 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InFlag);
1012 Chain = Hi.getValue(1);
1013 InFlag = Hi.getValue(2);
1014 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
1015 DAG.getConstant(1, dl, MVT::i32));
1016 InVals.push_back(Vec);
1017 } else {
1018 Chain =
1019 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1020 RVLocs[i].getValVT(), InFlag)
1021 .getValue(1);
1022 InFlag = Chain.getValue(2);
1023 InVals.push_back(Chain.getValue(0));
1024 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001025 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001026
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001027 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +00001028}
1029
Chris Dewhurstad741172016-05-20 10:21:01 +00001030// FIXME? Maybe this could be a TableGen attribute on some registers and
1031// this table could be generated automatically from RegInfo.
1032unsigned SparcTargetLowering::getRegisterByName(const char* RegName, EVT VT,
1033 SelectionDAG &DAG) const {
1034 unsigned Reg = StringSwitch<unsigned>(RegName)
1035 .Case("i0", SP::I0).Case("i1", SP::I1).Case("i2", SP::I2).Case("i3", SP::I3)
1036 .Case("i4", SP::I4).Case("i5", SP::I5).Case("i6", SP::I6).Case("i7", SP::I7)
1037 .Case("o0", SP::O0).Case("o1", SP::O1).Case("o2", SP::O2).Case("o3", SP::O3)
1038 .Case("o4", SP::O4).Case("o5", SP::O5).Case("o6", SP::O6).Case("o7", SP::O7)
1039 .Case("l0", SP::L0).Case("l1", SP::L1).Case("l2", SP::L2).Case("l3", SP::L3)
1040 .Case("l4", SP::L4).Case("l5", SP::L5).Case("l6", SP::L6).Case("l7", SP::L7)
1041 .Case("g0", SP::G0).Case("g1", SP::G1).Case("g2", SP::G2).Case("g3", SP::G3)
1042 .Case("g4", SP::G4).Case("g5", SP::G5).Case("g6", SP::G6).Case("g7", SP::G7)
1043 .Default(0);
1044
1045 if (Reg)
1046 return Reg;
1047
1048 report_fatal_error("Invalid register name global variable");
1049}
1050
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001051// This functions returns true if CalleeName is a ABI function that returns
1052// a long double (fp128).
1053static bool isFP128ABICall(const char *CalleeName)
1054{
1055 static const char *const ABICalls[] =
1056 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
1057 "_Q_sqrt", "_Q_neg",
1058 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001059 "_Q_lltoq", "_Q_ulltoq",
Craig Topper062a2ba2014-04-25 05:30:21 +00001060 nullptr
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001061 };
Craig Topper062a2ba2014-04-25 05:30:21 +00001062 for (const char * const *I = ABICalls; *I != nullptr; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001063 if (strcmp(CalleeName, *I) == 0)
1064 return true;
1065 return false;
1066}
1067
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001068unsigned
1069SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
1070{
Craig Topper062a2ba2014-04-25 05:30:21 +00001071 const Function *CalleeFn = nullptr;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001072 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1073 CalleeFn = dyn_cast<Function>(G->getGlobal());
1074 } else if (ExternalSymbolSDNode *E =
1075 dyn_cast<ExternalSymbolSDNode>(Callee)) {
1076 const Function *Fn = DAG.getMachineFunction().getFunction();
1077 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001078 const char *CalleeName = E->getSymbol();
1079 CalleeFn = M->getFunction(CalleeName);
1080 if (!CalleeFn && isFP128ABICall(CalleeName))
1081 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001082 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001083
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001084 if (!CalleeFn)
1085 return 0;
1086
Joerg Sonnenberger72128092015-10-21 20:05:01 +00001087 // It would be nice to check for the sret attribute on CalleeFn here,
1088 // but since it is not part of the function type, any check will misfire.
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001089
Chris Lattner229907c2011-07-18 04:54:35 +00001090 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
1091 Type *ElementTy = Ty->getElementType();
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001092 return DAG.getDataLayout().getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001093}
Chris Lattner49b269d2008-03-17 05:41:48 +00001094
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001095
1096// Fixup floating point arguments in the ... part of a varargs call.
1097//
1098// The SPARC v9 ABI requires that floating point arguments are treated the same
1099// as integers when calling a varargs function. This does not apply to the
1100// fixed arguments that are part of the function's prototype.
1101//
1102// This function post-processes a CCValAssign array created by
1103// AnalyzeCallOperands().
1104static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1105 ArrayRef<ISD::OutputArg> Outs) {
1106 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1107 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001108 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001109 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1110 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001111 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001112 continue;
1113 // The fixed arguments to a varargs function still go in FP registers.
1114 if (Outs[VA.getValNo()].IsFixed)
1115 continue;
1116
1117 // This floating point argument should be reassigned.
1118 CCValAssign NewVA;
1119
1120 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001121 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1122 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1123 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001124 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1125
1126 if (Offset < 6*8) {
1127 // This argument should go in %i0-%i5.
1128 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001129 if (ValTy == MVT::f64)
1130 // Full register, just bitconvert into i64.
1131 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1132 IReg, MVT::i64, CCValAssign::BCvt);
1133 else {
1134 assert(ValTy == MVT::f128 && "Unexpected type!");
1135 // Full register, just bitconvert into i128 -- We will lower this into
1136 // two i64s in LowerCall_64.
1137 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1138 IReg, MVT::i128, CCValAssign::BCvt);
1139 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001140 } else {
1141 // This needs to go to memory, we're out of integer registers.
1142 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1143 Offset, VA.getLocVT(), VA.getLocInfo());
1144 }
1145 ArgLocs[i] = NewVA;
1146 }
1147}
1148
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001149// Lower a call for the 64-bit ABI.
1150SDValue
1151SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1152 SmallVectorImpl<SDValue> &InVals) const {
1153 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001154 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001155 SDValue Chain = CLI.Chain;
Mehdi Amini44ede332015-07-09 02:09:04 +00001156 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001157
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001158 // Sparc target does not yet support tail call optimization.
1159 CLI.IsTailCall = false;
1160
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001161 // Analyze operands of the call, assigning locations to each operand.
1162 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001163 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1164 *DAG.getContext());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001165 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1166
1167 // Get the size of the outgoing arguments stack space requirement.
1168 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001169 // Called functions expect 6 argument words to exist in the stack frame, used
1170 // or not.
1171 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001172
1173 // Keep stack frames 16-byte aligned.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001174 ArgsSize = alignTo(ArgsSize, 16);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001175
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001176 // Varargs calls require special treatment.
1177 if (CLI.IsVarArg)
1178 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1179
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001180 // Adjust the stack pointer to make room for the arguments.
1181 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1182 // with more than 6 arguments.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001183 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001184 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001185
1186 // Collect the set of registers to pass to the function and their values.
1187 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1188 // instruction.
1189 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1190
1191 // Collect chains from all the memory opeations that copy arguments to the
1192 // stack. They must follow the stack pointer adjustment above and precede the
1193 // call instruction itself.
1194 SmallVector<SDValue, 8> MemOpChains;
1195
1196 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1197 const CCValAssign &VA = ArgLocs[i];
1198 SDValue Arg = CLI.OutVals[i];
1199
1200 // Promote the value if needed.
1201 switch (VA.getLocInfo()) {
1202 default:
1203 llvm_unreachable("Unknown location info!");
1204 case CCValAssign::Full:
1205 break;
1206 case CCValAssign::SExt:
1207 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1208 break;
1209 case CCValAssign::ZExt:
1210 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1211 break;
1212 case CCValAssign::AExt:
1213 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1214 break;
1215 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001216 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1217 // SPARC does not support i128 natively. Lower it into two i64, see below.
1218 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1219 || VA.getLocVT() != MVT::i128)
1220 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001221 break;
1222 }
1223
1224 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001225 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1226 && VA.getLocVT() == MVT::i128) {
1227 // Store and reload into the interger register reg and reg+1.
1228 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1229 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
Mehdi Amini44ede332015-07-09 02:09:04 +00001230 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001231 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001232 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001233 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001234 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001235
1236 // Store to %sp+BIAS+128+Offset
1237 SDValue Store = DAG.getStore(Chain, DL, Arg, HiPtrOff,
1238 MachinePointerInfo(),
1239 false, false, 0);
1240 // Load into Reg and Reg+1
1241 SDValue Hi64 = DAG.getLoad(MVT::i64, DL, Store, HiPtrOff,
1242 MachinePointerInfo(),
1243 false, false, false, 0);
1244 SDValue Lo64 = DAG.getLoad(MVT::i64, DL, Store, LoPtrOff,
1245 MachinePointerInfo(),
1246 false, false, false, 0);
1247 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1248 Hi64));
1249 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1250 Lo64));
1251 continue;
1252 }
1253
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001254 // The custom bit on an i32 return value indicates that it should be
1255 // passed in the high bits of the register.
1256 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1257 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001258 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001259
1260 // The next value may go in the low bits of the same register.
1261 // Handle both at once.
1262 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1263 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1264 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1265 CLI.OutVals[i+1]);
1266 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1267 // Skip the next value, it's already done.
1268 ++i;
1269 }
1270 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001271 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001272 continue;
1273 }
1274
1275 assert(VA.isMemLoc());
1276
1277 // Create a store off the stack pointer for this argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00001278 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001279 // The argument area starts at %fp+BIAS+128 in the callee frame,
1280 // %sp+BIAS+128 in ours.
1281 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1282 Subtarget->getStackPointerBias() +
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001283 128, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001284 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001285 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1286 MachinePointerInfo(),
1287 false, false, 0));
1288 }
1289
1290 // Emit all stores, make sure they occur before the call.
1291 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001292 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001293
1294 // Build a sequence of CopyToReg nodes glued together with token chain and
1295 // glue operands which copy the outgoing args into registers. The InGlue is
1296 // necessary since all emitted instructions must be stuck together in order
1297 // to pass the live physical registers.
1298 SDValue InGlue;
1299 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1300 Chain = DAG.getCopyToReg(Chain, DL,
1301 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1302 InGlue = Chain.getValue(1);
1303 }
1304
1305 // If the callee is a GlobalAddress node (quite common, every direct call is)
1306 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1307 // Likewise ExternalSymbol -> TargetExternalSymbol.
1308 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001309 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001310 unsigned TF = ((getTargetMachine().getRelocationModel() == Reloc::PIC_)
1311 ? SparcMCExpr::VK_Sparc_WPLT30 : 0);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001312 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001313 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001314 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001315 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001316
1317 // Build the operands for the call instruction itself.
1318 SmallVector<SDValue, 8> Ops;
1319 Ops.push_back(Chain);
1320 Ops.push_back(Callee);
1321 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1322 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1323 RegsToPass[i].second.getValueType()));
1324
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001325 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +00001326 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001327 const uint32_t *Mask =
1328 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
Eric Christopher9deb75d2015-03-11 22:42:13 +00001329 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1330 CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001331 assert(Mask && "Missing call preserved mask for calling convention");
1332 Ops.push_back(DAG.getRegisterMask(Mask));
1333
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001334 // Make sure the CopyToReg nodes are glued to the call instruction which
1335 // consumes the registers.
1336 if (InGlue.getNode())
1337 Ops.push_back(InGlue);
1338
1339 // Now the call itself.
1340 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001341 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001342 InGlue = Chain.getValue(1);
1343
1344 // Revert the stack pointer immediately after the call.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001345 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
1346 DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001347 InGlue = Chain.getValue(1);
1348
1349 // Now extract the return values. This is more or less the same as
1350 // LowerFormalArguments_64.
1351
1352 // Assign locations to each value returned by this call.
1353 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001354 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1355 *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001356
1357 // Set inreg flag manually for codegen generated library calls that
1358 // return float.
Craig Topper062a2ba2014-04-25 05:30:21 +00001359 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == nullptr)
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001360 CLI.Ins[0].Flags.setInReg();
1361
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001362 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001363
1364 // Copy all of the result registers out of their specified physreg.
1365 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1366 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001367 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001368
1369 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1370 // reside in the same register in the high and low bits. Reuse the
1371 // CopyFromReg previous node to avoid duplicate copies.
1372 SDValue RV;
1373 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1374 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1375 RV = Chain.getValue(0);
1376
1377 // But usually we'll create a new CopyFromReg for a different register.
1378 if (!RV.getNode()) {
1379 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1380 Chain = RV.getValue(1);
1381 InGlue = Chain.getValue(2);
1382 }
1383
1384 // Get the high bits for i32 struct elements.
1385 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1386 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001387 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001388
1389 // The callee promoted the return value, so insert an Assert?ext SDNode so
1390 // we won't promote the value again in this function.
1391 switch (VA.getLocInfo()) {
1392 case CCValAssign::SExt:
1393 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1394 DAG.getValueType(VA.getValVT()));
1395 break;
1396 case CCValAssign::ZExt:
1397 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1398 DAG.getValueType(VA.getValVT()));
1399 break;
1400 default:
1401 break;
1402 }
1403
1404 // Truncate the register down to the return value type.
1405 if (VA.isExtInLoc())
1406 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1407
1408 InVals.push_back(RV);
1409 }
1410
1411 return Chain;
1412}
1413
Chris Lattner0a1762e2008-03-17 03:21:36 +00001414//===----------------------------------------------------------------------===//
1415// TargetLowering Implementation
1416//===----------------------------------------------------------------------===//
1417
James Y Knight7306cd42016-03-29 19:09:54 +00001418TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1419 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1420 AI->getType()->getPrimitiveSizeInBits() == 32)
1421 return AtomicExpansionKind::None; // Uses xchg instruction
1422
1423 return AtomicExpansionKind::CmpXChg;
1424}
1425
Chris Lattner0a1762e2008-03-17 03:21:36 +00001426/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1427/// condition.
1428static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1429 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001430 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001431 case ISD::SETEQ: return SPCC::ICC_E;
1432 case ISD::SETNE: return SPCC::ICC_NE;
1433 case ISD::SETLT: return SPCC::ICC_L;
1434 case ISD::SETGT: return SPCC::ICC_G;
1435 case ISD::SETLE: return SPCC::ICC_LE;
1436 case ISD::SETGE: return SPCC::ICC_GE;
1437 case ISD::SETULT: return SPCC::ICC_CS;
1438 case ISD::SETULE: return SPCC::ICC_LEU;
1439 case ISD::SETUGT: return SPCC::ICC_GU;
1440 case ISD::SETUGE: return SPCC::ICC_CC;
1441 }
1442}
1443
1444/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1445/// FCC condition.
1446static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1447 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001448 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001449 case ISD::SETEQ:
1450 case ISD::SETOEQ: return SPCC::FCC_E;
1451 case ISD::SETNE:
1452 case ISD::SETUNE: return SPCC::FCC_NE;
1453 case ISD::SETLT:
1454 case ISD::SETOLT: return SPCC::FCC_L;
1455 case ISD::SETGT:
1456 case ISD::SETOGT: return SPCC::FCC_G;
1457 case ISD::SETLE:
1458 case ISD::SETOLE: return SPCC::FCC_LE;
1459 case ISD::SETGE:
1460 case ISD::SETOGE: return SPCC::FCC_GE;
1461 case ISD::SETULT: return SPCC::FCC_UL;
1462 case ISD::SETULE: return SPCC::FCC_ULE;
1463 case ISD::SETUGT: return SPCC::FCC_UG;
1464 case ISD::SETUGE: return SPCC::FCC_UGE;
1465 case ISD::SETUO: return SPCC::FCC_U;
1466 case ISD::SETO: return SPCC::FCC_O;
1467 case ISD::SETONE: return SPCC::FCC_LG;
1468 case ISD::SETUEQ: return SPCC::FCC_UE;
1469 }
1470}
1471
James Y Knightef31eaf2016-05-03 14:57:18 +00001472SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
Eric Christopherf5e94062015-01-30 23:46:43 +00001473 const SparcSubtarget &STI)
1474 : TargetLowering(TM), Subtarget(&STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +00001475 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00001476
James Y Knightd966fb62015-08-19 14:47:04 +00001477 // Instructions which use registers as conditionals examine all the
1478 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1479 // matters much whether it's ZeroOrOneBooleanContent, or
1480 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1481 // former.
1482 setBooleanContents(ZeroOrOneBooleanContent);
1483 setBooleanVectorContents(ZeroOrOneBooleanContent);
1484
Chris Lattner0a1762e2008-03-17 03:21:36 +00001485 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001486 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
Chris Dewhurst68388a02016-05-18 09:14:13 +00001487 if (!Subtarget->useSoftFloat()) {
1488 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1489 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1490 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1491 }
James Y Knight3994be82015-08-10 19:11:39 +00001492 if (Subtarget->is64Bit()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001493 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001494 } else {
1495 // On 32bit sparc, we define a double-register 32bit register
1496 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1497 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1498
1499 // ...but almost all operations must be expanded, so set that as
1500 // the default.
1501 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1502 setOperationAction(Op, MVT::v2i32, Expand);
1503 }
1504 // Truncating/extending stores/loads are also not supported.
1505 for (MVT VT : MVT::integer_vector_valuetypes()) {
1506 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1507 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1508 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1509
1510 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1511 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1512 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1513
1514 setTruncStoreAction(VT, MVT::v2i32, Expand);
1515 setTruncStoreAction(MVT::v2i32, VT, Expand);
1516 }
1517 // However, load and store *are* legal.
1518 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1519 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1520 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1521 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1522
1523 // And we need to promote i64 loads/stores into vector load/store
1524 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1525 setOperationAction(ISD::STORE, MVT::i64, Custom);
1526
1527 // Sadly, this doesn't work:
1528 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1529 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1530 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001531
1532 // Turn FP extload into load/fextend
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001533 for (MVT VT : MVT::fp_valuetypes()) {
1534 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1535 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1536 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001537
Chris Lattner0a1762e2008-03-17 03:21:36 +00001538 // Sparc doesn't have i1 sign extending load
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001539 for (MVT VT : MVT::integer_valuetypes())
1540 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001541
Chris Lattner0a1762e2008-03-17 03:21:36 +00001542 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001543 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001544 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1545 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001546
1547 // Custom legalize GlobalAddress nodes into LO/HI parts.
Mehdi Amini26d48132015-07-24 16:04:22 +00001548 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1549 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1550 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1551 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001552
Chris Lattner0a1762e2008-03-17 03:21:36 +00001553 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001554 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1555 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1556 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001557
1558 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001559 setOperationAction(ISD::UREM, MVT::i32, Expand);
1560 setOperationAction(ISD::SREM, MVT::i32, Expand);
1561 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1562 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001563
Roman Divacky2262cfa2013-10-31 19:22:33 +00001564 // ... nor does SparcV9.
1565 if (Subtarget->is64Bit()) {
1566 setOperationAction(ISD::UREM, MVT::i64, Expand);
1567 setOperationAction(ISD::SREM, MVT::i64, Expand);
1568 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1569 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1570 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001571
1572 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001573 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1574 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001575 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1576 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001577
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001578 // Custom Expand fp<->uint
1579 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1580 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001581 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1582 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001583
Wesley Peck527da1b2010-11-23 03:31:01 +00001584 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1585 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001586
Chris Lattner0a1762e2008-03-17 03:21:36 +00001587 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001588 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1589 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1590 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001591 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1592
Owen Anderson9f944592009-08-11 20:47:22 +00001593 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1594 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1595 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001596 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001597
Chris Lattner0a1762e2008-03-17 03:21:36 +00001598 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001599 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1600 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1601 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1602 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1603 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1604 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001605 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001606
Owen Anderson9f944592009-08-11 20:47:22 +00001607 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1608 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1609 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001610 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001611
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001612 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1613 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1614
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001615 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001616 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1617 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1618 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1619 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001620 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1621 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001622 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1623 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001624 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001625 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001626
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001627 setOperationAction(ISD::CTPOP, MVT::i64,
1628 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001629 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001630 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001631 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001632 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1633 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001634 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001635 }
1636
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001637 // ATOMICs.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001638 // Atomics are supported on SparcV9. 32-bit atomics are also
1639 // supported by some Leon SparcV8 variants. Otherwise, atomics
1640 // are unsupported.
James Y Knight19f6cce2016-04-12 20:18:48 +00001641 if (Subtarget->isV9())
1642 setMaxAtomicSizeInBitsSupported(64);
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001643 else if (false && Subtarget->hasLeonCasa())
Chris Dewhurst4f7cac32016-05-23 10:56:36 +00001644 // Test made to fail pending completion of AtomicExpandPass,
1645 // as this will cause a regression until that work is completed.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001646 setMaxAtomicSizeInBitsSupported(32);
James Y Knight19f6cce2016-04-12 20:18:48 +00001647 else
1648 setMaxAtomicSizeInBitsSupported(0);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001649
James Y Knight148a6462016-06-17 18:11:48 +00001650 setMinCmpXchgSizeInBits(32);
1651
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001652 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001653
1654 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1655
1656 // Custom Lower Atomic LOAD/STORE
1657 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1658 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1659
1660 if (Subtarget->is64Bit()) {
1661 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001662 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001663 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1664 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1665 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001666
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001667 if (!Subtarget->isV9()) {
1668 // SparcV8 does not have FNEGD and FABSD.
1669 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1670 setOperationAction(ISD::FABS, MVT::f64, Custom);
1671 }
1672
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001673 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1674 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1675 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1676 setOperationAction(ISD::FREM , MVT::f128, Expand);
1677 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001678 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1679 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001680 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001681 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001682 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001683 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1684 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001685 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001686 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001687 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001688 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1689 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1690 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1691 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1692 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001693 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001694 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1695 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001696 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001697 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1698 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001699
Owen Anderson9f944592009-08-11 20:47:22 +00001700 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1701 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1702 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001703
1704 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001705 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1706 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001707
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001708 if (Subtarget->is64Bit()) {
1709 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1710 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1711 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1712 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001713
1714 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1715 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001716
1717 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1718 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1719 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001720 }
1721
Chris Lattner0a1762e2008-03-17 03:21:36 +00001722 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001723 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001724 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001725 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001726
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001727 setOperationAction(ISD::TRAP , MVT::Other, Legal);
1728
Chris Lattner0a1762e2008-03-17 03:21:36 +00001729 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001730 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1731 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1732 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1733 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1734 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001735
Chris Lattner0a1762e2008-03-17 03:21:36 +00001736 setStackPointerRegisterToSaveRestore(SP::O6);
1737
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001738 setOperationAction(ISD::CTPOP, MVT::i32,
1739 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001740
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001741 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1742 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1743 setOperationAction(ISD::STORE, MVT::f128, Legal);
1744 } else {
1745 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1746 setOperationAction(ISD::STORE, MVT::f128, Custom);
1747 }
1748
1749 if (Subtarget->hasHardQuad()) {
1750 setOperationAction(ISD::FADD, MVT::f128, Legal);
1751 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1752 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1753 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1754 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1755 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1756 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1757 if (Subtarget->isV9()) {
1758 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1759 setOperationAction(ISD::FABS, MVT::f128, Legal);
1760 } else {
1761 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1762 setOperationAction(ISD::FABS, MVT::f128, Custom);
1763 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001764
1765 if (!Subtarget->is64Bit()) {
1766 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1767 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1768 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1769 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1770 }
1771
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001772 } else {
1773 // Custom legalize f128 operations.
1774
1775 setOperationAction(ISD::FADD, MVT::f128, Custom);
1776 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1777 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1778 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1779 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1780 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1781 setOperationAction(ISD::FABS, MVT::f128, Custom);
1782
1783 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1784 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1785 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1786
1787 // Setup Runtime library names.
Chris Dewhurst68388a02016-05-18 09:14:13 +00001788 if (Subtarget->is64Bit() && !Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001789 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1790 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1791 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1792 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1793 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1794 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001795 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001796 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001797 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001798 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1799 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1800 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1801 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001802 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1803 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1804 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1805 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
Chris Dewhurst68388a02016-05-18 09:14:13 +00001806 } else if (!Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001807 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1808 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1809 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1810 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1811 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1812 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001813 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001814 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001815 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001816 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1817 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1818 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1819 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001820 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1821 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1822 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1823 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1824 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001825 }
1826
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00001827 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1828
Eli Friedman2518f832011-05-06 20:34:06 +00001829 setMinFunctionAlignment(2);
1830
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001831 computeRegisterProperties(Subtarget->getRegisterInfo());
Chris Lattner0a1762e2008-03-17 03:21:36 +00001832}
1833
Chris Dewhurst68388a02016-05-18 09:14:13 +00001834bool SparcTargetLowering::useSoftFloat() const {
1835 return Subtarget->useSoftFloat();
1836}
1837
Chris Lattner0a1762e2008-03-17 03:21:36 +00001838const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001839 switch ((SPISD::NodeType)Opcode) {
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001840 case SPISD::FIRST_NUMBER: break;
1841 case SPISD::CMPICC: return "SPISD::CMPICC";
1842 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1843 case SPISD::BRICC: return "SPISD::BRICC";
1844 case SPISD::BRXCC: return "SPISD::BRXCC";
1845 case SPISD::BRFCC: return "SPISD::BRFCC";
1846 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1847 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1848 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1849 case SPISD::EH_SJLJ_SETJMP: return "SPISD::EH_SJLJ_SETJMP";
1850 case SPISD::EH_SJLJ_LONGJMP: return "SPISD::EH_SJLJ_LONGJMP";
1851 case SPISD::Hi: return "SPISD::Hi";
1852 case SPISD::Lo: return "SPISD::Lo";
1853 case SPISD::FTOI: return "SPISD::FTOI";
1854 case SPISD::ITOF: return "SPISD::ITOF";
1855 case SPISD::FTOX: return "SPISD::FTOX";
1856 case SPISD::XTOF: return "SPISD::XTOF";
1857 case SPISD::CALL: return "SPISD::CALL";
1858 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001859 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001860 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1861 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1862 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1863 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001864 }
Matthias Braund04893f2015-05-07 21:33:59 +00001865 return nullptr;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001866}
1867
Mehdi Amini44ede332015-07-09 02:09:04 +00001868EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
1869 EVT VT) const {
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001870 if (!VT.isVector())
1871 return MVT::i32;
1872 return VT.changeVectorElementTypeToInteger();
1873}
1874
Chris Lattner0a1762e2008-03-17 03:21:36 +00001875/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1876/// be zero. Op is expected to be a target specific node. Used by DAG
1877/// combiner.
Jay Foada0653a32014-05-14 21:14:37 +00001878void SparcTargetLowering::computeKnownBitsForTargetNode
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001879 (const SDValue Op,
1880 APInt &KnownZero,
1881 APInt &KnownOne,
1882 const SelectionDAG &DAG,
1883 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001884 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001885 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001886
Chris Lattner0a1762e2008-03-17 03:21:36 +00001887 switch (Op.getOpcode()) {
1888 default: break;
1889 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001890 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001891 case SPISD::SELECT_FCC:
Jay Foada0653a32014-05-14 21:14:37 +00001892 DAG.computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1893 DAG.computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001894
Chris Lattner0a1762e2008-03-17 03:21:36 +00001895 // Only known if known in both the LHS and RHS.
1896 KnownOne &= KnownOne2;
1897 KnownZero &= KnownZero2;
1898 break;
1899 }
1900}
1901
Chris Lattner0a1762e2008-03-17 03:21:36 +00001902// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1903// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001904static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001905 ISD::CondCode CC, unsigned &SPCC) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00001906 if (isNullConstant(RHS) &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001907 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001908 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1909 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001910 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1911 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1912 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00001913 isOneConstant(LHS.getOperand(0)) &&
1914 isNullConstant(LHS.getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001915 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001916 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001917 LHS = CMPCC.getOperand(0);
1918 RHS = CMPCC.getOperand(1);
1919 }
1920}
1921
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001922// Convert to a target node and set target flags.
1923SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1924 SelectionDAG &DAG) const {
1925 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1926 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001927 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001928 GA->getValueType(0),
1929 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001930
1931 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1932 return DAG.getTargetConstantPool(CP->getConstVal(),
1933 CP->getValueType(0),
1934 CP->getAlignment(),
1935 CP->getOffset(), TF);
1936
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001937 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1938 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1939 Op.getValueType(),
1940 0,
1941 TF);
1942
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001943 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1944 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1945 ES->getValueType(0), TF);
1946
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001947 llvm_unreachable("Unhandled address SDNode");
1948}
1949
1950// Split Op into high and low parts according to HiTF and LoTF.
1951// Return an ADD node combining the parts.
1952SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1953 unsigned HiTF, unsigned LoTF,
1954 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001955 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001956 EVT VT = Op.getValueType();
1957 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1958 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1959 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1960}
1961
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001962// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1963// or ExternalSymbol SDNode.
1964SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001965 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001966 EVT VT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001967
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001968 // Handle PIC mode first.
1969 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1970 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001971 SDValue HiLo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1972 SparcMCExpr::VK_Sparc_GOT10, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001973 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1974 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001975 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1976 // function has calls.
1977 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1978 MFI->setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001979 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001980 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1981 false, false, false, 0);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001982 }
1983
1984 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001985 switch(getTargetMachine().getCodeModel()) {
1986 default:
1987 llvm_unreachable("Unsupported absolute code model");
1988 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001989 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001990 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1991 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001992 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001993 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001994 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
1995 SparcMCExpr::VK_Sparc_M44, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001996 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001997 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001998 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1999 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
2000 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00002001 case CodeModel::Large: {
2002 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002003 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
2004 SparcMCExpr::VK_Sparc_HM, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002005 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002006 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
2007 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00002008 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
2009 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00002010 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002011}
2012
Wesley Peck527da1b2010-11-23 03:31:01 +00002013SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002014 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002015 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002016}
2017
Chris Lattner840c7002009-09-15 17:46:24 +00002018SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002019 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002020 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002021}
2022
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002023SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2024 SelectionDAG &DAG) const {
2025 return makeAddress(Op, DAG);
2026}
2027
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002028SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2029 SelectionDAG &DAG) const {
2030
2031 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002032 if (DAG.getTarget().Options.EmulatedTLS)
2033 return LowerToTLSEmulatedModel(GA, DAG);
2034
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002035 SDLoc DL(GA);
2036 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002037 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002038
2039 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2040
2041 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002042 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
2043 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
2044 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
2045 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
2046 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
2047 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
2048 unsigned addTF = ((model == TLSModel::GeneralDynamic)
2049 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
2050 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
2051 unsigned callTF = ((model == TLSModel::GeneralDynamic)
2052 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
2053 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002054
2055 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2056 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2057 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2058 withTargetFlags(Op, addTF, DAG));
2059
2060 SDValue Chain = DAG.getEntryNode();
2061 SDValue InFlag;
2062
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002063 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, DL, true), DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002064 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
2065 InFlag = Chain.getValue(1);
2066 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2067 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2068
2069 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2070 SmallVector<SDValue, 4> Ops;
2071 Ops.push_back(Chain);
2072 Ops.push_back(Callee);
2073 Ops.push_back(Symbol);
2074 Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
Eric Christopher9deb75d2015-03-11 22:42:13 +00002075 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2076 DAG.getMachineFunction(), CallingConv::C);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002077 assert(Mask && "Missing call preserved mask for calling convention");
2078 Ops.push_back(DAG.getRegisterMask(Mask));
2079 Ops.push_back(InFlag);
Craig Topper48d114b2014-04-26 18:35:24 +00002080 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002081 InFlag = Chain.getValue(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002082 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, DL, true),
2083 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002084 InFlag = Chain.getValue(1);
2085 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
2086
2087 if (model != TLSModel::LocalDynamic)
2088 return Ret;
2089
2090 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002091 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002092 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002093 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002094 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2095 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002096 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002097 }
2098
2099 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002100 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
2101 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002102
2103 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2104
2105 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2106 // function has calls.
2107 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2108 MFI->setHasCalls(true);
2109
2110 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002111 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
2112 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002113 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2114 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2115 DL, PtrVT, Ptr,
2116 withTargetFlags(Op, ldTF, DAG));
2117 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2118 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002119 withTargetFlags(Op,
2120 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002121 }
2122
2123 assert(model == TLSModel::LocalExec);
2124 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002125 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002126 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002127 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002128 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2129
2130 return DAG.getNode(ISD::ADD, DL, PtrVT,
2131 DAG.getRegister(SP::G7, PtrVT), Offset);
2132}
2133
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002134SDValue SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain,
2135 ArgListTy &Args, SDValue Arg,
2136 const SDLoc &DL,
2137 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002138 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2139 EVT ArgVT = Arg.getValueType();
2140 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2141
2142 ArgListEntry Entry;
2143 Entry.Node = Arg;
2144 Entry.Ty = ArgTy;
2145
2146 if (ArgTy->isFP128Ty()) {
2147 // Create a stack object and pass the pointer to the library function.
2148 int FI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002149 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002150 Chain = DAG.getStore(Chain,
2151 DL,
2152 Entry.Node,
2153 FIPtr,
2154 MachinePointerInfo(),
2155 false,
2156 false,
2157 8);
2158
2159 Entry.Node = FIPtr;
2160 Entry.Ty = PointerType::getUnqual(ArgTy);
2161 }
2162 Args.push_back(Entry);
2163 return Chain;
2164}
2165
2166SDValue
2167SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2168 const char *LibFuncName,
2169 unsigned numArgs) const {
2170
2171 ArgListTy Args;
2172
2173 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002174 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002175
Mehdi Amini44ede332015-07-09 02:09:04 +00002176 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002177 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2178 Type *RetTyABI = RetTy;
2179 SDValue Chain = DAG.getEntryNode();
2180 SDValue RetPtr;
2181
2182 if (RetTy->isFP128Ty()) {
2183 // Create a Stack Object to receive the return value of type f128.
2184 ArgListEntry Entry;
2185 int RetFI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002186 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002187 Entry.Node = RetPtr;
2188 Entry.Ty = PointerType::getUnqual(RetTy);
2189 if (!Subtarget->is64Bit())
2190 Entry.isSRet = true;
2191 Entry.isReturned = false;
2192 Args.push_back(Entry);
2193 RetTyABI = Type::getVoidTy(*DAG.getContext());
2194 }
2195
2196 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2197 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2198 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2199 }
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002200 TargetLowering::CallLoweringInfo CLI(DAG);
2201 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002202 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args), 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002203
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002204 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2205
2206 // chain is in second result.
2207 if (RetTyABI == RetTy)
2208 return CallInfo.first;
2209
2210 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2211
2212 Chain = CallInfo.second;
2213
2214 // Load RetPtr to get the return value.
2215 return DAG.getLoad(Op.getValueType(),
2216 SDLoc(Op),
2217 Chain,
2218 RetPtr,
2219 MachinePointerInfo(),
2220 false, false, false, 8);
2221}
2222
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002223SDValue SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2224 unsigned &SPCC, const SDLoc &DL,
2225 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002226
Craig Topper062a2ba2014-04-25 05:30:21 +00002227 const char *LibCall = nullptr;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002228 bool is64Bit = Subtarget->is64Bit();
2229 switch(SPCC) {
2230 default: llvm_unreachable("Unhandled conditional code!");
2231 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2232 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2233 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2234 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2235 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2236 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2237 case SPCC::FCC_UL :
2238 case SPCC::FCC_ULE:
2239 case SPCC::FCC_UG :
2240 case SPCC::FCC_UGE:
2241 case SPCC::FCC_U :
2242 case SPCC::FCC_O :
2243 case SPCC::FCC_LG :
2244 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2245 }
2246
Mehdi Amini44ede332015-07-09 02:09:04 +00002247 auto PtrVT = getPointerTy(DAG.getDataLayout());
2248 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002249 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2250 ArgListTy Args;
2251 SDValue Chain = DAG.getEntryNode();
2252 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2253 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2254
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002255 TargetLowering::CallLoweringInfo CLI(DAG);
2256 CLI.setDebugLoc(DL).setChain(Chain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002257 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args), 0);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002258
2259 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2260
2261 // result is in first, and chain is in second result.
2262 SDValue Result = CallInfo.first;
2263
2264 switch(SPCC) {
2265 default: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002266 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002267 SPCC = SPCC::ICC_NE;
2268 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2269 }
2270 case SPCC::FCC_UL : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002271 SDValue Mask = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002272 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002273 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002274 SPCC = SPCC::ICC_NE;
2275 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2276 }
2277 case SPCC::FCC_ULE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002278 SDValue RHS = DAG.getTargetConstant(2, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002279 SPCC = SPCC::ICC_NE;
2280 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2281 }
2282 case SPCC::FCC_UG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002283 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002284 SPCC = SPCC::ICC_G;
2285 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2286 }
2287 case SPCC::FCC_UGE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002288 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002289 SPCC = SPCC::ICC_NE;
2290 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2291 }
2292
2293 case SPCC::FCC_U : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002294 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002295 SPCC = SPCC::ICC_E;
2296 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2297 }
2298 case SPCC::FCC_O : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002299 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002300 SPCC = SPCC::ICC_NE;
2301 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2302 }
2303 case SPCC::FCC_LG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002304 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002305 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002306 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002307 SPCC = SPCC::ICC_NE;
2308 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2309 }
2310 case SPCC::FCC_UE : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002311 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002312 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002313 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002314 SPCC = SPCC::ICC_E;
2315 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2316 }
2317 }
2318}
2319
2320static SDValue
2321LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2322 const SparcTargetLowering &TLI) {
2323
2324 if (Op.getOperand(0).getValueType() == MVT::f64)
2325 return TLI.LowerF128Op(Op, DAG,
2326 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2327
2328 if (Op.getOperand(0).getValueType() == MVT::f32)
2329 return TLI.LowerF128Op(Op, DAG,
2330 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2331
2332 llvm_unreachable("fpextend with non-float operand!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002333 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002334}
2335
2336static SDValue
2337LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2338 const SparcTargetLowering &TLI) {
2339 // FP_ROUND on f64 and f32 are legal.
2340 if (Op.getOperand(0).getValueType() != MVT::f128)
2341 return Op;
2342
2343 if (Op.getValueType() == MVT::f64)
2344 return TLI.LowerF128Op(Op, DAG,
2345 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2346 if (Op.getValueType() == MVT::f32)
2347 return TLI.LowerF128Op(Op, DAG,
2348 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2349
2350 llvm_unreachable("fpround to non-float!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002351 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002352}
2353
2354static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2355 const SparcTargetLowering &TLI,
2356 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002357 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002358 EVT VT = Op.getValueType();
2359 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002360
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002361 // Expand f128 operations to fp128 abi calls.
2362 if (Op.getOperand(0).getValueType() == MVT::f128
2363 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2364 const char *libName = TLI.getLibcallName(VT == MVT::i32
2365 ? RTLIB::FPTOSINT_F128_I32
2366 : RTLIB::FPTOSINT_F128_I64);
2367 return TLI.LowerF128Op(Op, DAG, libName, 1);
2368 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002369
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002370 // Expand if the resulting type is illegal.
2371 if (!TLI.isTypeLegal(VT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002372 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002373
2374 // Otherwise, Convert the fp value to integer in an FP register.
2375 if (VT == MVT::i32)
2376 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2377 else
2378 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2379
2380 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002381}
2382
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002383static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2384 const SparcTargetLowering &TLI,
2385 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002386 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002387 EVT OpVT = Op.getOperand(0).getValueType();
2388 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2389
2390 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2391
2392 // Expand f128 operations to fp128 ABI calls.
2393 if (Op.getValueType() == MVT::f128
2394 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2395 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2396 ? RTLIB::SINTTOFP_I32_F128
2397 : RTLIB::SINTTOFP_I64_F128);
2398 return TLI.LowerF128Op(Op, DAG, libName, 1);
2399 }
2400
2401 // Expand if the operand type is illegal.
2402 if (!TLI.isTypeLegal(OpVT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002403 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002404
2405 // Otherwise, Convert the int value to FP in an FP register.
2406 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2407 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2408 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002409}
2410
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002411static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2412 const SparcTargetLowering &TLI,
2413 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002414 SDLoc dl(Op);
2415 EVT VT = Op.getValueType();
2416
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002417 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002418 // quad floating point instructions and the resulting type is legal.
2419 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2420 (hasHardQuad && TLI.isTypeLegal(VT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002421 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002422
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002423 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002424
2425 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002426 TLI.getLibcallName(VT == MVT::i32
2427 ? RTLIB::FPTOUINT_F128_I32
2428 : RTLIB::FPTOUINT_F128_I64),
2429 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002430}
2431
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002432static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2433 const SparcTargetLowering &TLI,
2434 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002435 SDLoc dl(Op);
2436 EVT OpVT = Op.getOperand(0).getValueType();
2437 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2438
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002439 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002440 // quad floating point instructions and the operand type is legal.
2441 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002442 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002443
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002444 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002445 TLI.getLibcallName(OpVT == MVT::i32
2446 ? RTLIB::UINTTOFP_I32_F128
2447 : RTLIB::UINTTOFP_I64_F128),
2448 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002449}
2450
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002451static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2452 const SparcTargetLowering &TLI,
2453 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002454 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002455 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002456 SDValue LHS = Op.getOperand(2);
2457 SDValue RHS = Op.getOperand(3);
2458 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002459 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002460 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002461
Chris Lattner0a1762e2008-03-17 03:21:36 +00002462 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2463 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2464 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002465
Chris Lattner0a1762e2008-03-17 03:21:36 +00002466 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002467 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002468 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002469 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002470 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002471 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2472 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002473 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002474 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2475 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2476 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2477 Opc = SPISD::BRICC;
2478 } else {
2479 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2480 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2481 Opc = SPISD::BRFCC;
2482 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002483 }
Owen Anderson9f944592009-08-11 20:47:22 +00002484 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002485 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002486}
2487
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002488static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2489 const SparcTargetLowering &TLI,
2490 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002491 SDValue LHS = Op.getOperand(0);
2492 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002493 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002494 SDValue TrueVal = Op.getOperand(2);
2495 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002496 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002497 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002498
Chris Lattner0a1762e2008-03-17 03:21:36 +00002499 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2500 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2501 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002502
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002503 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002504 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002505 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002506 Opc = LHS.getValueType() == MVT::i32 ?
2507 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002508 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2509 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002510 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2511 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2512 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2513 Opc = SPISD::SELECT_ICC;
2514 } else {
2515 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2516 Opc = SPISD::SELECT_FCC;
2517 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2518 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002519 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002520 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002521 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002522}
2523
Chris Dewhurst69fa1922016-05-04 09:33:30 +00002524SDValue SparcTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG,
2525 const SparcTargetLowering &TLI) const {
2526 SDLoc DL(Op);
2527 return DAG.getNode(SPISD::EH_SJLJ_SETJMP, DL,
2528 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), Op.getOperand(1));
2529
2530}
2531
2532SDValue SparcTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG,
2533 const SparcTargetLowering &TLI) const {
2534 SDLoc DL(Op);
2535 return DAG.getNode(SPISD::EH_SJLJ_LONGJMP, DL, MVT::Other, Op.getOperand(0), Op.getOperand(1));
2536}
2537
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002538static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002539 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002540 MachineFunction &MF = DAG.getMachineFunction();
2541 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002542 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002543
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002544 // Need frame address to find the address of VarArgsFrameIndex.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002545 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2546
Chris Lattner0a1762e2008-03-17 03:21:36 +00002547 // vastart just stores the address of the VarArgsFrameIndex slot into the
2548 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002549 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002550 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00002551 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2552 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002553 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002554 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Chris Lattner676c61d2010-09-21 18:41:36 +00002555 MachinePointerInfo(SV), false, false, 0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002556}
2557
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002558static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002559 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002560 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002561 SDValue InChain = Node->getOperand(0);
2562 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002563 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002564 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002565 SDLoc DL(Node);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002566 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002567 MachinePointerInfo(SV), false, false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002568 // Increment the pointer, VAList, to the next vaarg.
2569 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002570 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2571 DL));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002572 // Store the incremented VAList to the legalized pointer.
2573 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
Chris Lattner676c61d2010-09-21 18:41:36 +00002574 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002575 // Load the actual argument out of the pointer VAList.
2576 // We can't count on greater alignment than the word size.
2577 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2578 false, false, false,
2579 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002580}
2581
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002582static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002583 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002584 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2585 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002586 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002587 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002588
Chris Lattner0a1762e2008-03-17 03:21:36 +00002589 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002590 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2591 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002592 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002593
Chris Lattner0a1762e2008-03-17 03:21:36 +00002594 // The resultant pointer is actually 16 words from the bottom of the stack,
2595 // to provide a register spill area.
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002596 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2597 regSpillArea += Subtarget->getStackPointerBias();
2598
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002599 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002600 DAG.getConstant(regSpillArea, dl, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002601 SDValue Ops[2] = { NewVal, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002602 return DAG.getMergeValues(Ops, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002603}
2604
Chris Lattner0a1762e2008-03-17 03:21:36 +00002605
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002606static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002607 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002608 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002609 dl, MVT::Other, DAG.getEntryNode());
2610 return Chain;
2611}
2612
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002613static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2614 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002615 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2616 MFI->setFrameAddressIsTaken(true);
2617
2618 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002619 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002620 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002621 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002622
2623 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002624
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002625 if (depth == 0) {
2626 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2627 if (Subtarget->is64Bit())
2628 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002629 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002630 return FrameAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002631 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002632
2633 // flush first to make sure the windowed registers' values are in stack
2634 SDValue Chain = getFLUSHW(Op, DAG);
2635 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2636
2637 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2638
2639 while (depth--) {
2640 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002641 DAG.getIntPtrConstant(Offset, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002642 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo(),
2643 false, false, false, 0);
2644 }
2645 if (Subtarget->is64Bit())
2646 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002647 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002648 return FrameAddr;
2649}
2650
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002651
2652static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2653 const SparcSubtarget *Subtarget) {
2654
2655 uint64_t depth = Op.getConstantOperandVal(0);
2656
2657 return getFRAMEADDR(depth, Op, DAG, Subtarget);
2658
2659}
2660
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002661static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002662 const SparcTargetLowering &TLI,
2663 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002664 MachineFunction &MF = DAG.getMachineFunction();
2665 MachineFrameInfo *MFI = MF.getFrameInfo();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002666 MFI->setReturnAddressIsTaken(true);
2667
Bill Wendling908bf812014-01-06 00:43:20 +00002668 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002669 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002670
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002671 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002672 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002673 uint64_t depth = Op.getConstantOperandVal(0);
2674
2675 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002676 if (depth == 0) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002677 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2678 unsigned RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002679 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002680 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002681 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002682
2683 // Need frame address to find return address of the caller.
2684 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2685
2686 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2687 SDValue Ptr = DAG.getNode(ISD::ADD,
2688 dl, VT,
2689 FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002690 DAG.getIntPtrConstant(Offset, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002691 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr,
2692 MachinePointerInfo(), false, false, false, 0);
2693
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002694 return RetAddr;
2695}
2696
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002697static SDValue LowerF64Op(SDValue SrcReg64, const SDLoc &dl, SelectionDAG &DAG,
2698 unsigned opcode) {
James Y Knight51208ea2016-04-25 22:54:09 +00002699 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002700 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002701
2702 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2703 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2704 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2705
James Y Knight51208ea2016-04-25 22:54:09 +00002706 // Note: in little-endian, the floating-point value is stored in the
2707 // registers are in the opposite order, so the subreg with the sign
2708 // bit is the highest-numbered (odd), rather than the
2709 // lowest-numbered (even).
2710
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002711 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2712 SrcReg64);
2713 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2714 SrcReg64);
2715
James Y Knight51208ea2016-04-25 22:54:09 +00002716 if (DAG.getDataLayout().isLittleEndian())
2717 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2718 else
2719 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002720
2721 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2722 dl, MVT::f64), 0);
2723 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2724 DstReg64, Hi32);
2725 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2726 DstReg64, Lo32);
2727 return DstReg64;
2728}
2729
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002730// Lower a f128 load into two f64 loads.
2731static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2732{
2733 SDLoc dl(Op);
2734 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002735 assert(LdNode && LdNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002736 && "Unexpected node type");
2737
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002738 unsigned alignment = LdNode->getAlignment();
2739 if (alignment > 8)
2740 alignment = 8;
2741
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002742 SDValue Hi64 = DAG.getLoad(MVT::f64,
2743 dl,
2744 LdNode->getChain(),
2745 LdNode->getBasePtr(),
2746 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002747 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002748 EVT addrVT = LdNode->getBasePtr().getValueType();
2749 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2750 LdNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002751 DAG.getConstant(8, dl, addrVT));
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002752 SDValue Lo64 = DAG.getLoad(MVT::f64,
2753 dl,
2754 LdNode->getChain(),
2755 LoPtr,
2756 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002757 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002758
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002759 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2760 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002761
2762 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2763 dl, MVT::f128);
2764 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2765 MVT::f128,
2766 SDValue(InFP128, 0),
2767 Hi64,
2768 SubRegEven);
2769 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2770 MVT::f128,
2771 SDValue(InFP128, 0),
2772 Lo64,
2773 SubRegOdd);
2774 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2775 SDValue(Lo64.getNode(), 1) };
Craig Topper48d114b2014-04-26 18:35:24 +00002776 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002777 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
Craig Topper64941d92014-04-27 19:20:57 +00002778 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002779}
2780
James Y Knight3994be82015-08-10 19:11:39 +00002781static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2782{
2783 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2784
2785 EVT MemVT = LdNode->getMemoryVT();
2786 if (MemVT == MVT::f128)
2787 return LowerF128Load(Op, DAG);
2788
2789 return Op;
2790}
2791
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002792// Lower a f128 store into two f64 stores.
2793static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2794 SDLoc dl(Op);
2795 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002796 assert(StNode && StNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002797 && "Unexpected node type");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002798 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2799 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002800
2801 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2802 dl,
2803 MVT::f64,
2804 StNode->getValue(),
2805 SubRegEven);
2806 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2807 dl,
2808 MVT::f64,
2809 StNode->getValue(),
2810 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002811
2812 unsigned alignment = StNode->getAlignment();
2813 if (alignment > 8)
2814 alignment = 8;
2815
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002816 SDValue OutChains[2];
2817 OutChains[0] = DAG.getStore(StNode->getChain(),
2818 dl,
2819 SDValue(Hi64, 0),
2820 StNode->getBasePtr(),
2821 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002822 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002823 EVT addrVT = StNode->getBasePtr().getValueType();
2824 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2825 StNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002826 DAG.getConstant(8, dl, addrVT));
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002827 OutChains[1] = DAG.getStore(StNode->getChain(),
2828 dl,
2829 SDValue(Lo64, 0),
2830 LoPtr,
2831 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002832 false, false, alignment);
Craig Topper48d114b2014-04-26 18:35:24 +00002833 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002834}
2835
James Y Knight3994be82015-08-10 19:11:39 +00002836static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
2837{
2838 SDLoc dl(Op);
2839 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
2840
2841 EVT MemVT = St->getMemoryVT();
2842 if (MemVT == MVT::f128)
2843 return LowerF128Store(Op, DAG);
2844
2845 if (MemVT == MVT::i64) {
2846 // Custom handling for i64 stores: turn it into a bitcast and a
2847 // v2i32 store.
2848 SDValue Val = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, St->getValue());
2849 SDValue Chain = DAG.getStore(
2850 St->getChain(), dl, Val, St->getBasePtr(), St->getPointerInfo(),
2851 St->isVolatile(), St->isNonTemporal(), St->getAlignment(),
2852 St->getAAInfo());
2853 return Chain;
2854 }
2855
2856 return SDValue();
2857}
2858
Roman Divacky7a9c6542014-02-27 19:26:29 +00002859static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
Venkatraman Govindaraju3b6b0e42014-03-01 02:28:34 +00002860 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
2861 && "invalid opcode");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002862
James Y Knight51208ea2016-04-25 22:54:09 +00002863 SDLoc dl(Op);
2864
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002865 if (Op.getValueType() == MVT::f64)
James Y Knight51208ea2016-04-25 22:54:09 +00002866 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002867 if (Op.getValueType() != MVT::f128)
2868 return Op;
2869
Roman Divacky7a9c6542014-02-27 19:26:29 +00002870 // Lower fabs/fneg on f128 to fabs/fneg on f64
2871 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
James Y Knight51208ea2016-04-25 22:54:09 +00002872 // (As with LowerF64Op, on little-endian, we need to negate the odd
2873 // subreg)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002874
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002875 SDValue SrcReg128 = Op.getOperand(0);
2876 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2877 SrcReg128);
2878 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2879 SrcReg128);
James Y Knight51208ea2016-04-25 22:54:09 +00002880
2881 if (DAG.getDataLayout().isLittleEndian()) {
2882 if (isV9)
2883 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
2884 else
2885 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
2886 } else {
2887 if (isV9)
2888 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2889 else
2890 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
2891 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002892
2893 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2894 dl, MVT::f128), 0);
2895 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2896 DstReg128, Hi64);
2897 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2898 DstReg128, Lo64);
2899 return DstReg128;
2900}
2901
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002902static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002903
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002904 if (Op.getValueType() != MVT::i64)
2905 return Op;
2906
2907 SDLoc dl(Op);
2908 SDValue Src1 = Op.getOperand(0);
2909 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2910 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002911 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002912 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2913
2914 SDValue Src2 = Op.getOperand(1);
2915 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2916 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002917 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002918 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2919
2920
2921 bool hasChain = false;
2922 unsigned hiOpc = Op.getOpcode();
2923 switch (Op.getOpcode()) {
2924 default: llvm_unreachable("Invalid opcode");
2925 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2926 case ISD::ADDE: hasChain = true; break;
2927 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2928 case ISD::SUBE: hasChain = true; break;
2929 }
2930 SDValue Lo;
2931 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2932 if (hasChain) {
2933 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2934 Op.getOperand(2));
2935 } else {
2936 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2937 }
2938 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2939 SDValue Carry = Hi.getValue(1);
2940
2941 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2942 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2943 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002944 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002945
2946 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2947 SDValue Ops[2] = { Dst, Carry };
Craig Topper64941d92014-04-27 19:20:57 +00002948 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002949}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002950
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002951// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2952// in LegalizeDAG.cpp except the order of arguments to the library function.
2953static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2954 const SparcTargetLowering &TLI)
2955{
2956 unsigned opcode = Op.getOpcode();
2957 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2958
2959 bool isSigned = (opcode == ISD::SMULO);
2960 EVT VT = MVT::i64;
2961 EVT WideVT = MVT::i128;
2962 SDLoc dl(Op);
2963 SDValue LHS = Op.getOperand(0);
2964
2965 if (LHS.getValueType() != VT)
2966 return Op;
2967
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002968 SDValue ShiftAmt = DAG.getConstant(63, dl, VT);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002969
2970 SDValue RHS = Op.getOperand(1);
2971 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2972 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2973 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2974
2975 SDValue MulResult = TLI.makeLibCall(DAG,
2976 RTLIB::MUL_I128, WideVT,
Craig Topper8fe40e02015-10-22 17:05:00 +00002977 Args, isSigned, dl).first;
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002978 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002979 MulResult, DAG.getIntPtrConstant(0, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002980 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002981 MulResult, DAG.getIntPtrConstant(1, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002982 if (isSigned) {
2983 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2984 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2985 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002986 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, dl, VT),
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002987 ISD::SETNE);
2988 }
2989 // MulResult is a node with an illegal type. Because such things are not
Chandler Carruthee1a1fc2014-08-02 00:24:54 +00002990 // generally permitted during this phase of legalization, ensure that
2991 // nothing is left using the node. The above EXTRACT_ELEMENT nodes should have
2992 // been folded.
2993 assert(MulResult->use_empty() && "Illegally typed node still in use!");
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002994
2995 SDValue Ops[2] = { BottomHalf, TopHalf } ;
Craig Topper64941d92014-04-27 19:20:57 +00002996 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002997}
2998
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002999static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00003000 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
3001 // Expand with a fence.
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003002 return SDValue();
JF Bastien800f87a2016-04-06 21:19:33 +00003003
3004 // Monotonic load/stores are legal.
3005 return Op;
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003006}
3007
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003008SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
3009 SelectionDAG &DAG) const {
3010 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3011 SDLoc dl(Op);
3012 switch (IntNo) {
3013 default: return SDValue(); // Don't custom lower most intrinsics.
3014 case Intrinsic::thread_pointer: {
3015 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3016 return DAG.getRegister(SP::G7, PtrVT);
3017 }
3018 }
3019}
3020
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003021SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00003022LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003023
3024 bool hasHardQuad = Subtarget->hasHardQuad();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003025 bool isV9 = Subtarget->isV9();
3026
Chris Lattner0a1762e2008-03-17 03:21:36 +00003027 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003028 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00003029
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00003030 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
3031 Subtarget);
3032 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
3033 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00003034 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003035 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00003036 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003037 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003038 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
3039 hasHardQuad);
3040 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
3041 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00003042 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
3043 hasHardQuad);
3044 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3045 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003046 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
3047 hasHardQuad);
3048 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
3049 hasHardQuad);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003050 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG, *this);
3051 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG, *this);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003052 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3053 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00003054 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00003055 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00003056
James Y Knight3994be82015-08-10 19:11:39 +00003057 case ISD::LOAD: return LowerLOAD(Op, DAG);
3058 case ISD::STORE: return LowerSTORE(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003059 case ISD::FADD: return LowerF128Op(Op, DAG,
3060 getLibcallName(RTLIB::ADD_F128), 2);
3061 case ISD::FSUB: return LowerF128Op(Op, DAG,
3062 getLibcallName(RTLIB::SUB_F128), 2);
3063 case ISD::FMUL: return LowerF128Op(Op, DAG,
3064 getLibcallName(RTLIB::MUL_F128), 2);
3065 case ISD::FDIV: return LowerF128Op(Op, DAG,
3066 getLibcallName(RTLIB::DIV_F128), 2);
3067 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3068 getLibcallName(RTLIB::SQRT_F128),1);
Roman Divacky7a9c6542014-02-27 19:26:29 +00003069 case ISD::FABS:
3070 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003071 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3072 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00003073 case ISD::ADDC:
3074 case ISD::ADDE:
3075 case ISD::SUBC:
3076 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003077 case ISD::UMULO:
3078 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003079 case ISD::ATOMIC_LOAD:
3080 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003081 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003082 }
3083}
3084
3085MachineBasicBlock *
3086SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00003087 MachineBasicBlock *BB) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00003088 switch (MI->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003089 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00003090 case SP::SELECT_CC_Int_ICC:
3091 case SP::SELECT_CC_FP_ICC:
3092 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003093 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003094 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003095 case SP::SELECT_CC_Int_FCC:
3096 case SP::SELECT_CC_FP_FCC:
3097 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003098 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003099 return expandSelectCC(MI, BB, SP::FBCOND);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003100 case SP::EH_SJLJ_SETJMP32ri:
3101 case SP::EH_SJLJ_SETJMP32rr:
3102 return emitEHSjLjSetJmp(MI, BB);
3103 case SP::EH_SJLJ_LONGJMP32rr:
3104 case SP::EH_SJLJ_LONGJMP32ri:
3105 return emitEHSjLjLongJmp(MI, BB);
3106
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003107 }
3108}
3109
3110MachineBasicBlock*
3111SparcTargetLowering::expandSelectCC(MachineInstr *MI,
3112 MachineBasicBlock *BB,
3113 unsigned BROpcode) const {
Eric Christopherf5e94062015-01-30 23:46:43 +00003114 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003115 DebugLoc dl = MI->getDebugLoc();
3116 unsigned CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003117
Chris Lattner0a1762e2008-03-17 03:21:36 +00003118 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3119 // control-flow pattern. The incoming instruction knows the destination vreg
3120 // to set, the condition code register to branch on, the true/false values to
3121 // select between, and a branch opcode to use.
3122 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc3f79882015-10-20 00:59:43 +00003123 MachineFunction::iterator It = ++BB->getIterator();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003124
Chris Lattner0a1762e2008-03-17 03:21:36 +00003125 // thisMBB:
3126 // ...
3127 // TrueVal = ...
3128 // [f]bCC copy1MBB
3129 // fallthrough --> copy0MBB
3130 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003131 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00003132 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3133 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00003134 F->insert(It, copy0MBB);
3135 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003136
3137 // Transfer the remainder of BB and its successor edges to sinkMBB.
3138 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003139 std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00003140 BB->end());
3141 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3142
3143 // Add the true and fallthrough blocks as its successors.
3144 BB->addSuccessor(copy0MBB);
3145 BB->addSuccessor(sinkMBB);
3146
Dale Johannesen215a9252009-02-13 02:31:35 +00003147 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003148
Chris Lattner0a1762e2008-03-17 03:21:36 +00003149 // copy0MBB:
3150 // %FalseValue = ...
3151 // # fallthrough to sinkMBB
3152 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003153
Chris Lattner0a1762e2008-03-17 03:21:36 +00003154 // Update machine-CFG edges
3155 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003156
Chris Lattner0a1762e2008-03-17 03:21:36 +00003157 // sinkMBB:
3158 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3159 // ...
3160 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00003161 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattner0a1762e2008-03-17 03:21:36 +00003162 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
3163 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003164
Dan Gohman34396292010-07-06 20:24:04 +00003165 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00003166 return BB;
3167}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003168
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003169
3170MachineBasicBlock* SparcTargetLowering::
3171emitEHSjLjLongJmp(MachineInstr *MI,
3172 MachineBasicBlock *MBB) const
3173{
3174 DebugLoc DL = MI->getDebugLoc();
3175 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3176
3177 MachineFunction *MF = MBB->getParent();
3178 MachineRegisterInfo &MRI = MF->getRegInfo();
3179 MachineInstrBuilder MIB;
3180
3181 MVT PVT = getPointerTy(MF->getDataLayout());
3182 unsigned RegSize = PVT.getStoreSize();
3183 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3184
3185 unsigned Buf = MI->getOperand(0).getReg();
3186 unsigned JmpLoc = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3187
3188 // TO DO: If we do 64-bit handling, this perhaps should be FLUSHW, not TA 3
3189 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::TRAPri), SP::G0).addImm(3).addImm(SPCC::ICC_A);
3190
3191 // Instruction to restore FP
3192 const unsigned FP = SP::I6;
3193 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3194 .addReg(FP)
3195 .addReg(Buf)
3196 .addImm(0);
3197
3198 // Instruction to load jmp location
3199 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3200 .addReg(JmpLoc, RegState::Define)
3201 .addReg(Buf)
3202 .addImm(RegSize);
3203
3204 // Instruction to restore SP
3205 const unsigned SP = SP::O6;
3206 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3207 .addReg(SP)
3208 .addReg(Buf)
3209 .addImm(2 * RegSize);
3210
3211 // Instruction to restore I7
3212 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3213 .addReg(SP::I7)
3214 .addReg(Buf, RegState::Kill)
3215 .addImm(3 * RegSize);
3216
3217 // Jump to JmpLoc
3218 BuildMI(*MBB, MI, DL, TII->get(SP::JMPLrr)).addReg(SP::G0).addReg(JmpLoc, RegState::Kill).addReg(SP::G0);
3219
3220 MI->eraseFromParent();
3221 return MBB;
3222}
3223
3224MachineBasicBlock* SparcTargetLowering::
3225emitEHSjLjSetJmp(MachineInstr *MI,
3226 MachineBasicBlock *MBB) const
3227{
3228 DebugLoc DL = MI->getDebugLoc();
3229 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3230
3231 MachineFunction *MF = MBB->getParent();
3232 MachineRegisterInfo &MRI = MF->getRegInfo();
3233 MachineInstrBuilder MIB;
3234
3235 MVT PVT = getPointerTy(MF->getDataLayout());
3236 unsigned RegSize = PVT.getStoreSize();
3237 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3238
3239 unsigned DstReg = MI->getOperand(0).getReg();
3240 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
3241 assert(RC->hasType(MVT::i32) && "Invalid destination!");
3242 unsigned mainDstReg = MRI.createVirtualRegister(RC);
3243 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
3244
3245 // For v = setjmp(buf), we generate
3246 //
3247 // thisMBB:
3248 // buf[0] = FP
3249 // buf[RegSize] = restoreMBB <-- takes address of restoreMBB
3250 // buf[RegSize * 2] = O6
3251 // buf[RegSize * 3] = I7
3252 // Ensure restoreMBB remains in the relocations list (done using a bn instruction)
3253 // b mainMBB
3254 //
3255 // mainMBB:
3256 // v_main = 0
3257 // b sinkMBB
3258 //
3259 // restoreMBB:
3260 // v_restore = 1
3261 // --fall through--
3262 //
3263 // sinkMBB:
3264 // v = phi(main, restore)
3265
3266 const BasicBlock *BB = MBB->getBasicBlock();
3267 MachineFunction::iterator It = ++MBB->getIterator();
3268 MachineBasicBlock *thisMBB = MBB;
3269 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
3270 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
3271 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
3272
3273 MF->insert(It, mainMBB);
3274 MF->insert(It, restoreMBB);
3275 MF->insert(It, sinkMBB);
3276 restoreMBB->setHasAddressTaken();
3277
3278 // Transfer the remainder of BB and its successor edges to sinkMBB.
3279 sinkMBB->splice(sinkMBB->begin(), MBB,
3280 std::next(MachineBasicBlock::iterator(MI)),
3281 MBB->end());
3282 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
3283
3284 unsigned LabelReg = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3285 unsigned LabelReg2 = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3286 unsigned BufReg = MI->getOperand(1).getReg();
3287
3288 // Instruction to store FP
3289 const unsigned FP = SP::I6;
3290 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3291 .addReg(BufReg)
3292 .addImm(0)
3293 .addReg(FP);
3294
3295 // Instructions to store jmp location
3296 MIB = BuildMI(thisMBB, DL, TII->get(SP::SETHIi))
3297 .addReg(LabelReg, RegState::Define)
3298 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_HI);
3299
3300 MIB = BuildMI(thisMBB, DL, TII->get(SP::ORri))
3301 .addReg(LabelReg2, RegState::Define)
3302 .addReg(LabelReg, RegState::Kill)
3303 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_LO);
3304
3305 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3306 .addReg(BufReg)
3307 .addImm(RegSize)
3308 .addReg(LabelReg2, RegState::Kill);
3309
3310 // Instruction to store SP
3311 const unsigned SP = SP::O6;
3312 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3313 .addReg(BufReg)
3314 .addImm(2 * RegSize)
3315 .addReg(SP);
3316
3317 // Instruction to store I7
3318 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3319 .addReg(BufReg)
3320 .addImm(3 * RegSize)
3321 .addReg(SP::I7);
3322
3323
3324 // FIX ME: This next instruction ensures that the restoreMBB block address remains
3325 // valid through optimization passes and serves no other purpose. The ICC_N ensures
3326 // that the branch is never taken. This commented-out code here was an alternative
3327 // attempt to achieve this which brought myriad problems.
3328 //MIB = BuildMI(thisMBB, DL, TII->get(SP::EH_SjLj_Setup)).addMBB(restoreMBB, SparcMCExpr::VK_Sparc_None);
3329 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3330 .addMBB(restoreMBB)
3331 .addImm(SPCC::ICC_N);
3332
3333 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3334 .addMBB(mainMBB)
3335 .addImm(SPCC::ICC_A);
3336
3337 thisMBB->addSuccessor(mainMBB);
3338 thisMBB->addSuccessor(restoreMBB);
3339
3340
3341 // mainMBB:
3342 MIB = BuildMI(mainMBB, DL, TII->get(SP::ORrr))
3343 .addReg(mainDstReg, RegState::Define)
3344 .addReg(SP::G0)
3345 .addReg(SP::G0);
3346 MIB = BuildMI(mainMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3347
3348 mainMBB->addSuccessor(sinkMBB);
3349
3350
3351 // restoreMBB:
3352 MIB = BuildMI(restoreMBB, DL, TII->get(SP::ORri))
3353 .addReg(restoreDstReg, RegState::Define)
3354 .addReg(SP::G0)
3355 .addImm(1);
3356 //MIB = BuildMI(restoreMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3357 restoreMBB->addSuccessor(sinkMBB);
3358
3359 // sinkMBB:
3360 MIB = BuildMI(*sinkMBB, sinkMBB->begin(), DL,
3361 TII->get(SP::PHI), DstReg)
3362 .addReg(mainDstReg).addMBB(mainMBB)
3363 .addReg(restoreDstReg).addMBB(restoreMBB);
3364
3365 MI->eraseFromParent();
3366 return sinkMBB;
3367}
3368
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003369//===----------------------------------------------------------------------===//
3370// Sparc Inline Assembly Support
3371//===----------------------------------------------------------------------===//
3372
3373/// getConstraintType - Given a constraint letter, return the type of
3374/// constraint it is for this target.
3375SparcTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003376SparcTargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003377 if (Constraint.size() == 1) {
3378 switch (Constraint[0]) {
3379 default: break;
3380 case 'r': return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003381 case 'I': // SIMM13
3382 return C_Other;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003383 }
3384 }
3385
3386 return TargetLowering::getConstraintType(Constraint);
3387}
3388
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003389TargetLowering::ConstraintWeight SparcTargetLowering::
3390getSingleConstraintMatchWeight(AsmOperandInfo &info,
3391 const char *constraint) const {
3392 ConstraintWeight weight = CW_Invalid;
3393 Value *CallOperandVal = info.CallOperandVal;
3394 // If we don't have a value, we can't do a match,
3395 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003396 if (!CallOperandVal)
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003397 return CW_Default;
3398
3399 // Look at the constraint type.
3400 switch (*constraint) {
3401 default:
3402 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3403 break;
3404 case 'I': // SIMM13
3405 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3406 if (isInt<13>(C->getSExtValue()))
3407 weight = CW_Constant;
3408 }
3409 break;
3410 }
3411 return weight;
3412}
3413
3414/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3415/// vector. If it is invalid, don't add anything to Ops.
3416void SparcTargetLowering::
3417LowerAsmOperandForConstraint(SDValue Op,
3418 std::string &Constraint,
3419 std::vector<SDValue> &Ops,
3420 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003421 SDValue Result(nullptr, 0);
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003422
3423 // Only support length 1 constraints for now.
3424 if (Constraint.length() > 1)
3425 return;
3426
3427 char ConstraintLetter = Constraint[0];
3428 switch (ConstraintLetter) {
3429 default: break;
3430 case 'I':
3431 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3432 if (isInt<13>(C->getSExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003433 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
3434 Op.getValueType());
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003435 break;
3436 }
3437 return;
3438 }
3439 }
3440
3441 if (Result.getNode()) {
3442 Ops.push_back(Result);
3443 return;
3444 }
3445 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3446}
3447
Eric Christopher11e4df72015-02-26 22:38:43 +00003448std::pair<unsigned, const TargetRegisterClass *>
3449SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003450 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003451 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003452 if (Constraint.size() == 1) {
3453 switch (Constraint[0]) {
3454 case 'r':
James Y Knight3994be82015-08-10 19:11:39 +00003455 if (VT == MVT::v2i32)
3456 return std::make_pair(0U, &SP::IntPairRegClass);
3457 else
3458 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003459 }
James Y Knight3994be82015-08-10 19:11:39 +00003460 } else if (!Constraint.empty() && Constraint.size() <= 5
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003461 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3462 // constraint = '{r<d>}'
3463 // Remove the braces from around the name.
3464 StringRef name(Constraint.data()+1, Constraint.size()-2);
3465 // Handle register aliases:
3466 // r0-r7 -> g0-g7
3467 // r8-r15 -> o0-o7
3468 // r16-r23 -> l0-l7
3469 // r24-r31 -> i0-i7
3470 uint64_t intVal = 0;
3471 if (name.substr(0, 1).equals("r")
3472 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3473 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3474 char regType = regTypes[intVal/8];
3475 char regIdx = '0' + (intVal % 8);
3476 char tmp[] = { '{', regType, regIdx, '}', 0 };
3477 std::string newConstraint = std::string(tmp);
Eric Christopher11e4df72015-02-26 22:38:43 +00003478 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3479 VT);
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003480 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003481 }
3482
Eric Christopher11e4df72015-02-26 22:38:43 +00003483 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003484}
3485
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003486bool
3487SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3488 // The Sparc target isn't yet aware of offsets.
3489 return false;
3490}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003491
3492void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3493 SmallVectorImpl<SDValue>& Results,
3494 SelectionDAG &DAG) const {
3495
3496 SDLoc dl(N);
3497
3498 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3499
3500 switch (N->getOpcode()) {
3501 default:
3502 llvm_unreachable("Do not know how to custom type legalize this operation!");
3503
3504 case ISD::FP_TO_SINT:
3505 case ISD::FP_TO_UINT:
3506 // Custom lower only if it involves f128 or i64.
3507 if (N->getOperand(0).getValueType() != MVT::f128
3508 || N->getValueType(0) != MVT::i64)
3509 return;
3510 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3511 ? RTLIB::FPTOSINT_F128_I64
3512 : RTLIB::FPTOUINT_F128_I64);
3513
3514 Results.push_back(LowerF128Op(SDValue(N, 0),
3515 DAG,
3516 getLibcallName(libCall),
3517 1));
3518 return;
3519
3520 case ISD::SINT_TO_FP:
3521 case ISD::UINT_TO_FP:
3522 // Custom lower only if it involves f128 or i64.
3523 if (N->getValueType(0) != MVT::f128
3524 || N->getOperand(0).getValueType() != MVT::i64)
3525 return;
3526
3527 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3528 ? RTLIB::SINTTOFP_I64_F128
3529 : RTLIB::UINTTOFP_I64_F128);
3530
3531 Results.push_back(LowerF128Op(SDValue(N, 0),
3532 DAG,
3533 getLibcallName(libCall),
3534 1));
3535 return;
James Y Knight3994be82015-08-10 19:11:39 +00003536 case ISD::LOAD: {
3537 LoadSDNode *Ld = cast<LoadSDNode>(N);
3538 // Custom handling only for i64: turn i64 load into a v2i32 load,
3539 // and a bitcast.
3540 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3541 return;
3542
3543 SDLoc dl(N);
3544 SDValue LoadRes = DAG.getExtLoad(
3545 Ld->getExtensionType(), dl, MVT::v2i32,
3546 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
3547 MVT::v2i32, Ld->isVolatile(), Ld->isNonTemporal(),
3548 Ld->isInvariant(), Ld->getAlignment(), Ld->getAAInfo());
3549
3550 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3551 Results.push_back(Res);
3552 Results.push_back(LoadRes.getValue(1));
3553 return;
3554 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003555 }
3556}
Marcin Koscielnicki33571e22016-04-26 10:37:14 +00003557
3558// Override to enable LOAD_STACK_GUARD lowering on Linux.
3559bool SparcTargetLowering::useLoadStackGuardNode() const {
3560 if (!Subtarget->isTargetLinux())
3561 return TargetLowering::useLoadStackGuardNode();
3562 return true;
3563}
3564
3565// Override to disable global variable loading on Linux.
3566void SparcTargetLowering::insertSSPDeclarations(Module &M) const {
3567 if (!Subtarget->isTargetLinux())
3568 return TargetLowering::insertSSPDeclarations(M);
3569}