blob: 0aa68357329fcfd7ba8dbfc1ee29273a60cf4128 [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
196SparcTargetLowering::LowerReturn(SDValue Chain,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000197 CallingConv::ID CallConv, 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,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000200 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
207SparcTargetLowering::LowerReturn_32(SDValue Chain,
208 CallingConv::ID CallConv, bool IsVarArg,
209 const SmallVectorImpl<ISD::OutputArg> &Outs,
210 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000211 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
292SparcTargetLowering::LowerReturn_64(SDValue Chain,
293 CallingConv::ID CallConv, bool IsVarArg,
294 const SmallVectorImpl<ISD::OutputArg> &Outs,
295 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000296 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
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000368SDValue SparcTargetLowering::
369LowerFormalArguments(SDValue Chain,
370 CallingConv::ID CallConv,
371 bool IsVarArg,
372 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000373 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000374 SelectionDAG &DAG,
375 SmallVectorImpl<SDValue> &InVals) const {
376 if (Subtarget->is64Bit())
377 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
378 DL, DAG, InVals);
379 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
380 DL, DAG, InVals);
381}
382
383/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000384/// passed in either one or two GPRs, including FP values. TODO: we should
385/// pass FP values in FP registers for fastcc functions.
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000386SDValue SparcTargetLowering::
387LowerFormalArguments_32(SDValue Chain,
388 CallingConv::ID CallConv,
389 bool isVarArg,
390 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000391 SDLoc dl,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000392 SelectionDAG &DAG,
393 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000394 MachineFunction &MF = DAG.getMachineFunction();
395 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000396 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000397
398 // Assign locations to all of the incoming arguments.
399 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000400 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
401 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000402 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000403
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000404 const unsigned StackOffset = 92;
James Y Knight33beb242015-12-15 19:23:12 +0000405 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000406
Reid Kleckner79418562014-05-09 22:32:13 +0000407 unsigned InIdx = 0;
408 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000409 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000410
Reid Kleckner79418562014-05-09 22:32:13 +0000411 if (Ins[InIdx].Flags.isSRet()) {
412 if (InIdx != 0)
413 report_fatal_error("sparc only supports sret on the first parameter");
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000414 // Get SRet from [%fp+64].
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000415 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
416 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
417 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
418 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000419 false, false, false, 0);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000420 InVals.push_back(Arg);
421 continue;
422 }
423
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000424 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000425 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000426 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
427
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000428 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
429 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
430 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000431
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000432 assert(i+1 < e);
433 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000434
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000435 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000436 if (NextVA.isMemLoc()) {
437 int FrameIdx = MF.getFrameInfo()->
438 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000439 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000440 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
441 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000442 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000443 } else {
444 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000445 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000446 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000447 }
James Y Knight33beb242015-12-15 19:23:12 +0000448
449 if (IsLittleEndian)
450 std::swap(LoVal, HiVal);
451
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000452 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000453 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000454 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000455 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000456 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000457 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000458 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
459 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
460 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
461 if (VA.getLocVT() == MVT::f32)
462 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
463 else if (VA.getLocVT() != MVT::i32) {
464 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
465 DAG.getValueType(VA.getLocVT()));
466 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
467 }
468 InVals.push_back(Arg);
469 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000470 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000471
472 assert(VA.isMemLoc());
473
474 unsigned Offset = VA.getLocMemOffset()+StackOffset;
Mehdi Amini44ede332015-07-09 02:09:04 +0000475 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000476
477 if (VA.needsCustom()) {
Hans Wennborg1f094852016-04-11 20:35:41 +0000478 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000479 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000480 if (Offset % 8 == 0) {
481 int FI = MF.getFrameInfo()->CreateFixedObject(8,
482 Offset,
483 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000484 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000485 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
486 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000487 false,false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000488 InVals.push_back(Load);
489 continue;
490 }
491
492 int FI = MF.getFrameInfo()->CreateFixedObject(4,
493 Offset,
494 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000495 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000496 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
497 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000498 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000499 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
500 Offset+4,
501 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000502 SDValue FIPtr2 = DAG.getFrameIndex(FI2, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000503
504 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
505 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000506 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000507
James Y Knight33beb242015-12-15 19:23:12 +0000508 if (IsLittleEndian)
509 std::swap(LoVal, HiVal);
510
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000511 SDValue WholeValue =
512 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000513 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000514 InVals.push_back(WholeValue);
515 continue;
516 }
517
518 int FI = MF.getFrameInfo()->CreateFixedObject(4,
519 Offset,
520 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000521 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000522 SDValue Load ;
523 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
524 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
525 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000526 false, false, false, 0);
James Y Knight33beb242015-12-15 19:23:12 +0000527 } else if (VA.getValVT() == MVT::f128) {
528 report_fatal_error("SPARCv8 does not handle f128 in calls; "
529 "pass indirectly");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000530 } else {
James Y Knight33beb242015-12-15 19:23:12 +0000531 // We shouldn't see any other value types here.
James Y Knight99fcb722015-12-15 23:07:16 +0000532 llvm_unreachable("Unexpected ValVT encountered in frame lowering.");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000533 }
534 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000535 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000536
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000537 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000538 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000539 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
540 unsigned Reg = SFI->getSRetReturnReg();
541 if (!Reg) {
542 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
543 SFI->setSRetReturnReg(Reg);
544 }
545 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
546 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
547 }
548
Chris Lattner49b269d2008-03-17 05:41:48 +0000549 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000550 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +0000551 static const MCPhysReg ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000552 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
553 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000554 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs);
Craig Topper840beec2014-04-04 05:16:06 +0000555 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000556 unsigned ArgOffset = CCInfo.getNextStackOffset();
557 if (NumAllocated == 6)
558 ArgOffset += StackOffset;
559 else {
560 assert(!ArgOffset);
561 ArgOffset = 68+4*NumAllocated;
562 }
563
Chris Lattner49b269d2008-03-17 05:41:48 +0000564 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000565 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000566
Eli Friedmanbe853b72009-07-19 19:53:46 +0000567 std::vector<SDValue> OutChains;
568
Chris Lattner49b269d2008-03-17 05:41:48 +0000569 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
570 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
571 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000572 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000573
David Greene1fbe0542009-11-12 20:49:22 +0000574 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Cheng0664a672010-07-03 00:40:23 +0000575 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000576 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000577
Chris Lattner676c61d2010-09-21 18:41:36 +0000578 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
579 MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000580 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000581 ArgOffset += 4;
582 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000583
584 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000585 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +0000586 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Eli Friedmanbe853b72009-07-19 19:53:46 +0000587 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000588 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000589
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000590 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000591}
592
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000593// Lower formal arguments for the 64 bit ABI.
594SDValue SparcTargetLowering::
595LowerFormalArguments_64(SDValue Chain,
596 CallingConv::ID CallConv,
597 bool IsVarArg,
598 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000599 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000600 SelectionDAG &DAG,
601 SmallVectorImpl<SDValue> &InVals) const {
602 MachineFunction &MF = DAG.getMachineFunction();
603
604 // Analyze arguments according to CC_Sparc64.
605 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000606 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
607 *DAG.getContext());
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000608 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
609
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000610 // The argument array begins at %fp+BIAS+128, after the register save area.
611 const unsigned ArgArea = 128;
612
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000613 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
614 CCValAssign &VA = ArgLocs[i];
615 if (VA.isRegLoc()) {
616 // This argument is passed in a register.
617 // All integer register arguments are promoted by the caller to i64.
618
619 // Create a virtual register for the promoted live-in value.
620 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
621 getRegClassFor(VA.getLocVT()));
622 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
623
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000624 // Get the high bits for i32 struct elements.
625 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
626 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000627 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000628
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000629 // The caller promoted the argument, so insert an Assert?ext SDNode so we
630 // won't promote the value again in this function.
631 switch (VA.getLocInfo()) {
632 case CCValAssign::SExt:
633 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
634 DAG.getValueType(VA.getValVT()));
635 break;
636 case CCValAssign::ZExt:
637 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
638 DAG.getValueType(VA.getValVT()));
639 break;
640 default:
641 break;
642 }
643
644 // Truncate the register down to the argument type.
645 if (VA.isExtInLoc())
646 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
647
648 InVals.push_back(Arg);
649 continue;
650 }
651
652 // The registers are exhausted. This argument was passed on the stack.
653 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000654 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
655 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000656 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000657 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
658 // Adjust offset for extended arguments, SPARC is big-endian.
659 // The caller will have written the full slot with extended bytes, but we
660 // prefer our own extending loads.
661 if (VA.isExtInLoc())
662 Offset += 8 - ValSize;
663 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000664 InVals.push_back(DAG.getLoad(
665 VA.getValVT(), DL, Chain,
666 DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
Alex Lorenze40c8a22015-08-11 23:09:45 +0000667 MachinePointerInfo::getFixedStack(MF, FI), false, false, false, 0));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000668 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000669
670 if (!IsVarArg)
671 return Chain;
672
673 // This function takes variable arguments, some of which may have been passed
674 // in registers %i0-%i5. Variable floating point arguments are never passed
675 // in floating point registers. They go on %i0-%i5 or on the stack like
676 // integer arguments.
677 //
678 // The va_start intrinsic needs to know the offset to the first variable
679 // argument.
680 unsigned ArgOffset = CCInfo.getNextStackOffset();
681 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
682 // Skip the 128 bytes of register save area.
683 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
684 Subtarget->getStackPointerBias());
685
686 // Save the variable arguments that were passed in registers.
687 // The caller is required to reserve stack space for 6 arguments regardless
688 // of how many arguments were actually passed.
689 SmallVector<SDValue, 8> OutChains;
690 for (; ArgOffset < 6*8; ArgOffset += 8) {
691 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
692 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
693 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000694 auto PtrVT = getPointerTy(MF.getDataLayout());
Alex Lorenze40c8a22015-08-11 23:09:45 +0000695 OutChains.push_back(DAG.getStore(
696 Chain, DL, VArg, DAG.getFrameIndex(FI, PtrVT),
697 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000698 }
699
700 if (!OutChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000701 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000702
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000703 return Chain;
704}
705
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000706SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000707SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000708 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000709 if (Subtarget->is64Bit())
710 return LowerCall_64(CLI, InVals);
711 return LowerCall_32(CLI, InVals);
712}
713
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000714static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
715 ImmutableCallSite *CS) {
716 if (CS)
717 return CS->hasFnAttr(Attribute::ReturnsTwice);
718
Craig Topper062a2ba2014-04-25 05:30:21 +0000719 const Function *CalleeFn = nullptr;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000720 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
721 CalleeFn = dyn_cast<Function>(G->getGlobal());
722 } else if (ExternalSymbolSDNode *E =
723 dyn_cast<ExternalSymbolSDNode>(Callee)) {
724 const Function *Fn = DAG.getMachineFunction().getFunction();
725 const Module *M = Fn->getParent();
726 const char *CalleeName = E->getSymbol();
727 CalleeFn = M->getFunction(CalleeName);
728 }
729
730 if (!CalleeFn)
731 return false;
732 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
733}
734
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000735// Lower a call for the 32-bit ABI.
736SDValue
737SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
738 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000739 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000740 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000741 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
742 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
743 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000744 SDValue Chain = CLI.Chain;
745 SDValue Callee = CLI.Callee;
746 bool &isTailCall = CLI.IsTailCall;
747 CallingConv::ID CallConv = CLI.CallConv;
748 bool isVarArg = CLI.IsVarArg;
749
Evan Cheng67a69dd2010-01-27 00:07:07 +0000750 // Sparc target does not yet support tail call optimization.
751 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000752
Chris Lattner7d4152b2008-03-17 06:58:37 +0000753 // Analyze operands of the call, assigning locations to each operand.
754 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000755 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
756 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000757 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000758
Chris Lattner7d4152b2008-03-17 06:58:37 +0000759 // Get the size of the outgoing arguments stack space requirement.
760 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000761
Chris Lattner49b269d2008-03-17 05:41:48 +0000762 // Keep stack frames 8-byte aligned.
763 ArgsSize = (ArgsSize+7) & ~7;
764
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000765 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
766
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000767 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000768 SmallVector<SDValue, 8> ByValArgs;
769 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
770 ISD::ArgFlagsTy Flags = Outs[i].Flags;
771 if (!Flags.isByVal())
772 continue;
773
774 SDValue Arg = OutVals[i];
775 unsigned Size = Flags.getByValSize();
776 unsigned Align = Flags.getByValAlign();
777
Chris Dewhurst53bde952016-06-01 08:48:56 +0000778 if (Size > 0U) {
779 int FI = MFI->CreateStackObject(Size, Align, false);
780 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
781 SDValue SizeNode = DAG.getConstant(Size, dl, MVT::i32);
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000782
Chris Dewhurst53bde952016-06-01 08:48:56 +0000783 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
784 false, // isVolatile,
785 (Size <= 32), // AlwaysInline if size <= 32,
786 false, // isTailCall
787 MachinePointerInfo(), MachinePointerInfo());
788 ByValArgs.push_back(FIPtr);
789 }
790 else {
791 SDValue nullVal;
792 ByValArgs.push_back(nullVal);
793 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000794 }
795
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000796 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000797 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000798
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000799 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
800 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000801
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000802 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000803 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000804 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000805 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000806 i != e;
807 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000808 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000809 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000810
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000811 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
812
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000813 // Use local copy if it is a byval arg.
Chris Dewhurst53bde952016-06-01 08:48:56 +0000814 if (Flags.isByVal()) {
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000815 Arg = ByValArgs[byvalArgIdx++];
Chris Dewhurst53bde952016-06-01 08:48:56 +0000816 if (!Arg) {
817 continue;
818 }
819 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000820
Chris Lattner7d4152b2008-03-17 06:58:37 +0000821 // Promote the value if needed.
822 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000823 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000824 case CCValAssign::Full: break;
825 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000826 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000827 break;
828 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000829 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000830 break;
831 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000832 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
833 break;
834 case CCValAssign::BCvt:
835 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000836 break;
837 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000838
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000839 if (Flags.isSRet()) {
840 assert(VA.needsCustom());
841 // store SRet argument in %sp+64
842 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000843 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000844 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
845 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
846 MachinePointerInfo(),
847 false, false, 0));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000848 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000849 continue;
850 }
851
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000852 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000853 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000854
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000855 if (VA.isMemLoc()) {
856 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000857 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000858 if (Offset % 8 == 0) {
859 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000860 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000861 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
862 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
863 MachinePointerInfo(),
864 false, false, 0));
865 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000866 }
867 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000868
James Y Knight3994be82015-08-10 19:11:39 +0000869 if (VA.getLocVT() == MVT::f64) {
870 // Move from the float value from float registers into the
871 // integer registers.
872
James Y Knight692e0372015-10-09 21:36:19 +0000873 // TODO: The f64 -> v2i32 conversion is super-inefficient for
874 // constants: it sticks them in the constant pool, then loads
875 // to a fp register, then stores to temp memory, then loads to
876 // integer registers.
James Y Knight3994be82015-08-10 19:11:39 +0000877 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
878 }
879
880 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
881 Arg,
882 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
883 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
884 Arg,
885 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000886
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000887 if (VA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000888 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000889 assert(i+1 != e);
890 CCValAssign &NextVA = ArgLocs[++i];
891 if (NextVA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000892 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000893 } else {
James Y Knight3994be82015-08-10 19:11:39 +0000894 // Store the second part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000895 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
896 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000897 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000898 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000899 MemOpChains.push_back(DAG.getStore(Chain, dl, Part1, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000900 MachinePointerInfo(),
901 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000902 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000903 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000904 unsigned Offset = VA.getLocMemOffset() + StackOffset;
James Y Knight3994be82015-08-10 19:11:39 +0000905 // Store the first part.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000906 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000907 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000908 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000909 MemOpChains.push_back(DAG.getStore(Chain, dl, Part0, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000910 MachinePointerInfo(),
911 false, false, 0));
James Y Knight3994be82015-08-10 19:11:39 +0000912 // Store the second part.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000913 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000914 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000915 MemOpChains.push_back(DAG.getStore(Chain, dl, Part1, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000916 MachinePointerInfo(),
917 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000918 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000919 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000920 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000921
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000922 // Arguments that can be passed on register must be kept at
923 // RegsToPass vector
924 if (VA.isRegLoc()) {
925 if (VA.getLocVT() != MVT::f32) {
926 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
927 continue;
928 }
929 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
930 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
931 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000932 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000933
934 assert(VA.isMemLoc());
935
936 // Create a store off the stack pointer for this argument.
937 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000938 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
939 dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000940 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
941 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
942 MachinePointerInfo(),
943 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000944 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000945
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000946
Chris Lattner49b269d2008-03-17 05:41:48 +0000947 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000948 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000949 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000950
951 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000952 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000953 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000954 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000955 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000956 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000957 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000958 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000959 InFlag = Chain.getValue(1);
960 }
961
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000962 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000963 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000964
Chris Lattner49b269d2008-03-17 05:41:48 +0000965 // If the callee is a GlobalAddress node (quite common, every direct call is)
966 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000967 // Likewise ExternalSymbol -> TargetExternalSymbol.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000968 unsigned TF = ((getTargetMachine().getRelocationModel() == Reloc::PIC_)
969 ? SparcMCExpr::VK_Sparc_WPLT30 : 0);
Chris Lattner49b269d2008-03-17 05:41:48 +0000970 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000971 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000972 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000973 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000974
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000975 // Returns a chain & a flag for retval copy to use
976 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
977 SmallVector<SDValue, 8> Ops;
978 Ops.push_back(Chain);
979 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000980 if (hasStructRetAttr)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000981 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000982 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
983 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
984 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000985
986 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000987 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000988 const uint32_t *Mask =
989 ((hasReturnsTwice)
990 ? TRI->getRTCallPreservedMask(CallConv)
991 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000992 assert(Mask && "Missing call preserved mask for calling convention");
993 Ops.push_back(DAG.getRegisterMask(Mask));
994
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000995 if (InFlag.getNode())
996 Ops.push_back(InFlag);
997
Craig Topper48d114b2014-04-26 18:35:24 +0000998 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
Chris Lattner49b269d2008-03-17 05:41:48 +0000999 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001000
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001001 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
1002 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +00001003 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001004
Chris Lattnerdb26db22008-03-17 06:01:07 +00001005 // Assign locations to each value returned by this call.
1006 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001007 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1008 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001009
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001010 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001011
Chris Lattnerdb26db22008-03-17 06:01:07 +00001012 // Copy all of the result registers out of their specified physreg.
1013 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Nirav Dave29938542016-02-26 18:55:22 +00001014 if (RVLocs[i].getLocVT() == MVT::v2i32) {
1015 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
1016 SDValue Lo = DAG.getCopyFromReg(
1017 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InFlag);
1018 Chain = Lo.getValue(1);
1019 InFlag = Lo.getValue(2);
1020 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
1021 DAG.getConstant(0, dl, MVT::i32));
1022 SDValue Hi = DAG.getCopyFromReg(
1023 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InFlag);
1024 Chain = Hi.getValue(1);
1025 InFlag = Hi.getValue(2);
1026 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
1027 DAG.getConstant(1, dl, MVT::i32));
1028 InVals.push_back(Vec);
1029 } else {
1030 Chain =
1031 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1032 RVLocs[i].getValVT(), InFlag)
1033 .getValue(1);
1034 InFlag = Chain.getValue(2);
1035 InVals.push_back(Chain.getValue(0));
1036 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001037 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001038
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001039 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +00001040}
1041
Chris Dewhurstad741172016-05-20 10:21:01 +00001042// FIXME? Maybe this could be a TableGen attribute on some registers and
1043// this table could be generated automatically from RegInfo.
1044unsigned SparcTargetLowering::getRegisterByName(const char* RegName, EVT VT,
1045 SelectionDAG &DAG) const {
1046 unsigned Reg = StringSwitch<unsigned>(RegName)
1047 .Case("i0", SP::I0).Case("i1", SP::I1).Case("i2", SP::I2).Case("i3", SP::I3)
1048 .Case("i4", SP::I4).Case("i5", SP::I5).Case("i6", SP::I6).Case("i7", SP::I7)
1049 .Case("o0", SP::O0).Case("o1", SP::O1).Case("o2", SP::O2).Case("o3", SP::O3)
1050 .Case("o4", SP::O4).Case("o5", SP::O5).Case("o6", SP::O6).Case("o7", SP::O7)
1051 .Case("l0", SP::L0).Case("l1", SP::L1).Case("l2", SP::L2).Case("l3", SP::L3)
1052 .Case("l4", SP::L4).Case("l5", SP::L5).Case("l6", SP::L6).Case("l7", SP::L7)
1053 .Case("g0", SP::G0).Case("g1", SP::G1).Case("g2", SP::G2).Case("g3", SP::G3)
1054 .Case("g4", SP::G4).Case("g5", SP::G5).Case("g6", SP::G6).Case("g7", SP::G7)
1055 .Default(0);
1056
1057 if (Reg)
1058 return Reg;
1059
1060 report_fatal_error("Invalid register name global variable");
1061}
1062
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001063// This functions returns true if CalleeName is a ABI function that returns
1064// a long double (fp128).
1065static bool isFP128ABICall(const char *CalleeName)
1066{
1067 static const char *const ABICalls[] =
1068 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
1069 "_Q_sqrt", "_Q_neg",
1070 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001071 "_Q_lltoq", "_Q_ulltoq",
Craig Topper062a2ba2014-04-25 05:30:21 +00001072 nullptr
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001073 };
Craig Topper062a2ba2014-04-25 05:30:21 +00001074 for (const char * const *I = ABICalls; *I != nullptr; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001075 if (strcmp(CalleeName, *I) == 0)
1076 return true;
1077 return false;
1078}
1079
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001080unsigned
1081SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
1082{
Craig Topper062a2ba2014-04-25 05:30:21 +00001083 const Function *CalleeFn = nullptr;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001084 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1085 CalleeFn = dyn_cast<Function>(G->getGlobal());
1086 } else if (ExternalSymbolSDNode *E =
1087 dyn_cast<ExternalSymbolSDNode>(Callee)) {
1088 const Function *Fn = DAG.getMachineFunction().getFunction();
1089 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001090 const char *CalleeName = E->getSymbol();
1091 CalleeFn = M->getFunction(CalleeName);
1092 if (!CalleeFn && isFP128ABICall(CalleeName))
1093 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001094 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001095
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001096 if (!CalleeFn)
1097 return 0;
1098
Joerg Sonnenberger72128092015-10-21 20:05:01 +00001099 // It would be nice to check for the sret attribute on CalleeFn here,
1100 // but since it is not part of the function type, any check will misfire.
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001101
Chris Lattner229907c2011-07-18 04:54:35 +00001102 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
1103 Type *ElementTy = Ty->getElementType();
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001104 return DAG.getDataLayout().getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001105}
Chris Lattner49b269d2008-03-17 05:41:48 +00001106
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001107
1108// Fixup floating point arguments in the ... part of a varargs call.
1109//
1110// The SPARC v9 ABI requires that floating point arguments are treated the same
1111// as integers when calling a varargs function. This does not apply to the
1112// fixed arguments that are part of the function's prototype.
1113//
1114// This function post-processes a CCValAssign array created by
1115// AnalyzeCallOperands().
1116static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1117 ArrayRef<ISD::OutputArg> Outs) {
1118 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1119 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001120 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001121 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1122 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001123 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001124 continue;
1125 // The fixed arguments to a varargs function still go in FP registers.
1126 if (Outs[VA.getValNo()].IsFixed)
1127 continue;
1128
1129 // This floating point argument should be reassigned.
1130 CCValAssign NewVA;
1131
1132 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001133 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1134 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1135 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001136 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1137
1138 if (Offset < 6*8) {
1139 // This argument should go in %i0-%i5.
1140 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001141 if (ValTy == MVT::f64)
1142 // Full register, just bitconvert into i64.
1143 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1144 IReg, MVT::i64, CCValAssign::BCvt);
1145 else {
1146 assert(ValTy == MVT::f128 && "Unexpected type!");
1147 // Full register, just bitconvert into i128 -- We will lower this into
1148 // two i64s in LowerCall_64.
1149 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1150 IReg, MVT::i128, CCValAssign::BCvt);
1151 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001152 } else {
1153 // This needs to go to memory, we're out of integer registers.
1154 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1155 Offset, VA.getLocVT(), VA.getLocInfo());
1156 }
1157 ArgLocs[i] = NewVA;
1158 }
1159}
1160
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001161// Lower a call for the 64-bit ABI.
1162SDValue
1163SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1164 SmallVectorImpl<SDValue> &InVals) const {
1165 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001166 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001167 SDValue Chain = CLI.Chain;
Mehdi Amini44ede332015-07-09 02:09:04 +00001168 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001169
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001170 // Sparc target does not yet support tail call optimization.
1171 CLI.IsTailCall = false;
1172
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001173 // Analyze operands of the call, assigning locations to each operand.
1174 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001175 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1176 *DAG.getContext());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001177 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1178
1179 // Get the size of the outgoing arguments stack space requirement.
1180 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001181 // Called functions expect 6 argument words to exist in the stack frame, used
1182 // or not.
1183 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001184
1185 // Keep stack frames 16-byte aligned.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001186 ArgsSize = alignTo(ArgsSize, 16);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001187
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001188 // Varargs calls require special treatment.
1189 if (CLI.IsVarArg)
1190 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1191
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001192 // Adjust the stack pointer to make room for the arguments.
1193 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1194 // with more than 6 arguments.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001195 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001196 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001197
1198 // Collect the set of registers to pass to the function and their values.
1199 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1200 // instruction.
1201 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1202
1203 // Collect chains from all the memory opeations that copy arguments to the
1204 // stack. They must follow the stack pointer adjustment above and precede the
1205 // call instruction itself.
1206 SmallVector<SDValue, 8> MemOpChains;
1207
1208 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1209 const CCValAssign &VA = ArgLocs[i];
1210 SDValue Arg = CLI.OutVals[i];
1211
1212 // Promote the value if needed.
1213 switch (VA.getLocInfo()) {
1214 default:
1215 llvm_unreachable("Unknown location info!");
1216 case CCValAssign::Full:
1217 break;
1218 case CCValAssign::SExt:
1219 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1220 break;
1221 case CCValAssign::ZExt:
1222 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1223 break;
1224 case CCValAssign::AExt:
1225 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1226 break;
1227 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001228 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1229 // SPARC does not support i128 natively. Lower it into two i64, see below.
1230 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1231 || VA.getLocVT() != MVT::i128)
1232 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001233 break;
1234 }
1235
1236 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001237 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1238 && VA.getLocVT() == MVT::i128) {
1239 // Store and reload into the interger register reg and reg+1.
1240 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1241 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
Mehdi Amini44ede332015-07-09 02:09:04 +00001242 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001243 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001244 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001245 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001246 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001247
1248 // Store to %sp+BIAS+128+Offset
1249 SDValue Store = DAG.getStore(Chain, DL, Arg, HiPtrOff,
1250 MachinePointerInfo(),
1251 false, false, 0);
1252 // Load into Reg and Reg+1
1253 SDValue Hi64 = DAG.getLoad(MVT::i64, DL, Store, HiPtrOff,
1254 MachinePointerInfo(),
1255 false, false, false, 0);
1256 SDValue Lo64 = DAG.getLoad(MVT::i64, DL, Store, LoPtrOff,
1257 MachinePointerInfo(),
1258 false, false, false, 0);
1259 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1260 Hi64));
1261 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1262 Lo64));
1263 continue;
1264 }
1265
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001266 // The custom bit on an i32 return value indicates that it should be
1267 // passed in the high bits of the register.
1268 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1269 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001270 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001271
1272 // The next value may go in the low bits of the same register.
1273 // Handle both at once.
1274 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1275 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1276 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1277 CLI.OutVals[i+1]);
1278 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1279 // Skip the next value, it's already done.
1280 ++i;
1281 }
1282 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001283 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001284 continue;
1285 }
1286
1287 assert(VA.isMemLoc());
1288
1289 // Create a store off the stack pointer for this argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00001290 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001291 // The argument area starts at %fp+BIAS+128 in the callee frame,
1292 // %sp+BIAS+128 in ours.
1293 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1294 Subtarget->getStackPointerBias() +
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001295 128, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001296 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001297 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1298 MachinePointerInfo(),
1299 false, false, 0));
1300 }
1301
1302 // Emit all stores, make sure they occur before the call.
1303 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001304 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001305
1306 // Build a sequence of CopyToReg nodes glued together with token chain and
1307 // glue operands which copy the outgoing args into registers. The InGlue is
1308 // necessary since all emitted instructions must be stuck together in order
1309 // to pass the live physical registers.
1310 SDValue InGlue;
1311 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1312 Chain = DAG.getCopyToReg(Chain, DL,
1313 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1314 InGlue = Chain.getValue(1);
1315 }
1316
1317 // If the callee is a GlobalAddress node (quite common, every direct call is)
1318 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1319 // Likewise ExternalSymbol -> TargetExternalSymbol.
1320 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001321 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001322 unsigned TF = ((getTargetMachine().getRelocationModel() == Reloc::PIC_)
1323 ? SparcMCExpr::VK_Sparc_WPLT30 : 0);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001324 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001325 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001326 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001327 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001328
1329 // Build the operands for the call instruction itself.
1330 SmallVector<SDValue, 8> Ops;
1331 Ops.push_back(Chain);
1332 Ops.push_back(Callee);
1333 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1334 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1335 RegsToPass[i].second.getValueType()));
1336
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001337 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +00001338 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001339 const uint32_t *Mask =
1340 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
Eric Christopher9deb75d2015-03-11 22:42:13 +00001341 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1342 CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001343 assert(Mask && "Missing call preserved mask for calling convention");
1344 Ops.push_back(DAG.getRegisterMask(Mask));
1345
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001346 // Make sure the CopyToReg nodes are glued to the call instruction which
1347 // consumes the registers.
1348 if (InGlue.getNode())
1349 Ops.push_back(InGlue);
1350
1351 // Now the call itself.
1352 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001353 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001354 InGlue = Chain.getValue(1);
1355
1356 // Revert the stack pointer immediately after the call.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001357 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
1358 DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001359 InGlue = Chain.getValue(1);
1360
1361 // Now extract the return values. This is more or less the same as
1362 // LowerFormalArguments_64.
1363
1364 // Assign locations to each value returned by this call.
1365 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001366 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1367 *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001368
1369 // Set inreg flag manually for codegen generated library calls that
1370 // return float.
Craig Topper062a2ba2014-04-25 05:30:21 +00001371 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == nullptr)
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001372 CLI.Ins[0].Flags.setInReg();
1373
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001374 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001375
1376 // Copy all of the result registers out of their specified physreg.
1377 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1378 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001379 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001380
1381 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1382 // reside in the same register in the high and low bits. Reuse the
1383 // CopyFromReg previous node to avoid duplicate copies.
1384 SDValue RV;
1385 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1386 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1387 RV = Chain.getValue(0);
1388
1389 // But usually we'll create a new CopyFromReg for a different register.
1390 if (!RV.getNode()) {
1391 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1392 Chain = RV.getValue(1);
1393 InGlue = Chain.getValue(2);
1394 }
1395
1396 // Get the high bits for i32 struct elements.
1397 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1398 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001399 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001400
1401 // The callee promoted the return value, so insert an Assert?ext SDNode so
1402 // we won't promote the value again in this function.
1403 switch (VA.getLocInfo()) {
1404 case CCValAssign::SExt:
1405 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1406 DAG.getValueType(VA.getValVT()));
1407 break;
1408 case CCValAssign::ZExt:
1409 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1410 DAG.getValueType(VA.getValVT()));
1411 break;
1412 default:
1413 break;
1414 }
1415
1416 // Truncate the register down to the return value type.
1417 if (VA.isExtInLoc())
1418 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1419
1420 InVals.push_back(RV);
1421 }
1422
1423 return Chain;
1424}
1425
Chris Lattner0a1762e2008-03-17 03:21:36 +00001426//===----------------------------------------------------------------------===//
1427// TargetLowering Implementation
1428//===----------------------------------------------------------------------===//
1429
James Y Knight7306cd42016-03-29 19:09:54 +00001430TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1431 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1432 AI->getType()->getPrimitiveSizeInBits() == 32)
1433 return AtomicExpansionKind::None; // Uses xchg instruction
1434
1435 return AtomicExpansionKind::CmpXChg;
1436}
1437
Chris Lattner0a1762e2008-03-17 03:21:36 +00001438/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1439/// condition.
1440static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1441 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001442 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001443 case ISD::SETEQ: return SPCC::ICC_E;
1444 case ISD::SETNE: return SPCC::ICC_NE;
1445 case ISD::SETLT: return SPCC::ICC_L;
1446 case ISD::SETGT: return SPCC::ICC_G;
1447 case ISD::SETLE: return SPCC::ICC_LE;
1448 case ISD::SETGE: return SPCC::ICC_GE;
1449 case ISD::SETULT: return SPCC::ICC_CS;
1450 case ISD::SETULE: return SPCC::ICC_LEU;
1451 case ISD::SETUGT: return SPCC::ICC_GU;
1452 case ISD::SETUGE: return SPCC::ICC_CC;
1453 }
1454}
1455
1456/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1457/// FCC condition.
1458static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1459 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001460 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001461 case ISD::SETEQ:
1462 case ISD::SETOEQ: return SPCC::FCC_E;
1463 case ISD::SETNE:
1464 case ISD::SETUNE: return SPCC::FCC_NE;
1465 case ISD::SETLT:
1466 case ISD::SETOLT: return SPCC::FCC_L;
1467 case ISD::SETGT:
1468 case ISD::SETOGT: return SPCC::FCC_G;
1469 case ISD::SETLE:
1470 case ISD::SETOLE: return SPCC::FCC_LE;
1471 case ISD::SETGE:
1472 case ISD::SETOGE: return SPCC::FCC_GE;
1473 case ISD::SETULT: return SPCC::FCC_UL;
1474 case ISD::SETULE: return SPCC::FCC_ULE;
1475 case ISD::SETUGT: return SPCC::FCC_UG;
1476 case ISD::SETUGE: return SPCC::FCC_UGE;
1477 case ISD::SETUO: return SPCC::FCC_U;
1478 case ISD::SETO: return SPCC::FCC_O;
1479 case ISD::SETONE: return SPCC::FCC_LG;
1480 case ISD::SETUEQ: return SPCC::FCC_UE;
1481 }
1482}
1483
James Y Knightef31eaf2016-05-03 14:57:18 +00001484SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
Eric Christopherf5e94062015-01-30 23:46:43 +00001485 const SparcSubtarget &STI)
1486 : TargetLowering(TM), Subtarget(&STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +00001487 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00001488
James Y Knightd966fb62015-08-19 14:47:04 +00001489 // Instructions which use registers as conditionals examine all the
1490 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1491 // matters much whether it's ZeroOrOneBooleanContent, or
1492 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1493 // former.
1494 setBooleanContents(ZeroOrOneBooleanContent);
1495 setBooleanVectorContents(ZeroOrOneBooleanContent);
1496
Chris Lattner0a1762e2008-03-17 03:21:36 +00001497 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001498 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
Chris Dewhurst68388a02016-05-18 09:14:13 +00001499 if (!Subtarget->useSoftFloat()) {
1500 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1501 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1502 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1503 }
James Y Knight3994be82015-08-10 19:11:39 +00001504 if (Subtarget->is64Bit()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001505 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001506 } else {
1507 // On 32bit sparc, we define a double-register 32bit register
1508 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1509 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1510
1511 // ...but almost all operations must be expanded, so set that as
1512 // the default.
1513 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1514 setOperationAction(Op, MVT::v2i32, Expand);
1515 }
1516 // Truncating/extending stores/loads are also not supported.
1517 for (MVT VT : MVT::integer_vector_valuetypes()) {
1518 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1519 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1520 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1521
1522 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1523 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1524 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1525
1526 setTruncStoreAction(VT, MVT::v2i32, Expand);
1527 setTruncStoreAction(MVT::v2i32, VT, Expand);
1528 }
1529 // However, load and store *are* legal.
1530 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1531 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1532 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1533 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1534
1535 // And we need to promote i64 loads/stores into vector load/store
1536 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1537 setOperationAction(ISD::STORE, MVT::i64, Custom);
1538
1539 // Sadly, this doesn't work:
1540 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1541 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1542 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001543
1544 // Turn FP extload into load/fextend
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001545 for (MVT VT : MVT::fp_valuetypes()) {
1546 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1547 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1548 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001549
Chris Lattner0a1762e2008-03-17 03:21:36 +00001550 // Sparc doesn't have i1 sign extending load
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001551 for (MVT VT : MVT::integer_valuetypes())
1552 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001553
Chris Lattner0a1762e2008-03-17 03:21:36 +00001554 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001555 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001556 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1557 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001558
1559 // Custom legalize GlobalAddress nodes into LO/HI parts.
Mehdi Amini26d48132015-07-24 16:04:22 +00001560 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1561 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1562 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1563 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001564
Chris Lattner0a1762e2008-03-17 03:21:36 +00001565 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001566 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1567 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1568 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001569
1570 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001571 setOperationAction(ISD::UREM, MVT::i32, Expand);
1572 setOperationAction(ISD::SREM, MVT::i32, Expand);
1573 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1574 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001575
Roman Divacky2262cfa2013-10-31 19:22:33 +00001576 // ... nor does SparcV9.
1577 if (Subtarget->is64Bit()) {
1578 setOperationAction(ISD::UREM, MVT::i64, Expand);
1579 setOperationAction(ISD::SREM, MVT::i64, Expand);
1580 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1581 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1582 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001583
1584 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001585 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1586 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001587 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1588 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001589
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001590 // Custom Expand fp<->uint
1591 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1592 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001593 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1594 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001595
Wesley Peck527da1b2010-11-23 03:31:01 +00001596 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1597 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001598
Chris Lattner0a1762e2008-03-17 03:21:36 +00001599 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001600 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1601 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1602 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001603 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1604
Owen Anderson9f944592009-08-11 20:47:22 +00001605 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1606 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1607 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001608 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001609
Chris Lattner0a1762e2008-03-17 03:21:36 +00001610 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001611 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1612 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1613 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1614 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1615 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1616 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001617 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001618
Owen Anderson9f944592009-08-11 20:47:22 +00001619 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1620 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1621 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001622 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001623
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001624 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1625 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1626
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001627 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001628 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1629 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1630 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1631 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001632 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1633 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001634 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1635 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001636 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001637 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001638
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001639 setOperationAction(ISD::CTPOP, MVT::i64,
1640 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001641 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001642 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001643 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001644 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1645 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001646 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001647 }
1648
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001649 // ATOMICs.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001650 // Atomics are supported on SparcV9. 32-bit atomics are also
1651 // supported by some Leon SparcV8 variants. Otherwise, atomics
1652 // are unsupported.
James Y Knight19f6cce2016-04-12 20:18:48 +00001653 if (Subtarget->isV9())
1654 setMaxAtomicSizeInBitsSupported(64);
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001655 else if (false && Subtarget->hasLeonCasa())
Chris Dewhurst4f7cac32016-05-23 10:56:36 +00001656 // Test made to fail pending completion of AtomicExpandPass,
1657 // as this will cause a regression until that work is completed.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001658 setMaxAtomicSizeInBitsSupported(32);
James Y Knight19f6cce2016-04-12 20:18:48 +00001659 else
1660 setMaxAtomicSizeInBitsSupported(0);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001661
1662 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001663
1664 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1665
1666 // Custom Lower Atomic LOAD/STORE
1667 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1668 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1669
1670 if (Subtarget->is64Bit()) {
1671 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001672 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001673 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1674 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1675 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001676
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001677 if (!Subtarget->isV9()) {
1678 // SparcV8 does not have FNEGD and FABSD.
1679 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1680 setOperationAction(ISD::FABS, MVT::f64, Custom);
1681 }
1682
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001683 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1684 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1685 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1686 setOperationAction(ISD::FREM , MVT::f128, Expand);
1687 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001688 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1689 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001690 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001691 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001692 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001693 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1694 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001695 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001696 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001697 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001698 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1699 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1700 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1701 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1702 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001703 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001704 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1705 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001706 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001707 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1708 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001709
Owen Anderson9f944592009-08-11 20:47:22 +00001710 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1711 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1712 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001713
1714 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001715 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1716 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001717
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001718 if (Subtarget->is64Bit()) {
1719 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1720 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1721 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1722 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001723
1724 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1725 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001726
1727 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1728 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1729 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001730 }
1731
Chris Lattner0a1762e2008-03-17 03:21:36 +00001732 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001733 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001734 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001735 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001736
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001737 setOperationAction(ISD::TRAP , MVT::Other, Legal);
1738
Chris Lattner0a1762e2008-03-17 03:21:36 +00001739 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001740 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1741 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1742 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1743 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1744 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001745
Chris Lattner0a1762e2008-03-17 03:21:36 +00001746 setStackPointerRegisterToSaveRestore(SP::O6);
1747
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001748 setOperationAction(ISD::CTPOP, MVT::i32,
1749 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001750
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001751 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1752 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1753 setOperationAction(ISD::STORE, MVT::f128, Legal);
1754 } else {
1755 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1756 setOperationAction(ISD::STORE, MVT::f128, Custom);
1757 }
1758
1759 if (Subtarget->hasHardQuad()) {
1760 setOperationAction(ISD::FADD, MVT::f128, Legal);
1761 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1762 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1763 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1764 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1765 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1766 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1767 if (Subtarget->isV9()) {
1768 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1769 setOperationAction(ISD::FABS, MVT::f128, Legal);
1770 } else {
1771 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1772 setOperationAction(ISD::FABS, MVT::f128, Custom);
1773 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001774
1775 if (!Subtarget->is64Bit()) {
1776 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1777 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1778 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1779 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1780 }
1781
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001782 } else {
1783 // Custom legalize f128 operations.
1784
1785 setOperationAction(ISD::FADD, MVT::f128, Custom);
1786 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1787 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1788 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1789 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1790 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1791 setOperationAction(ISD::FABS, MVT::f128, Custom);
1792
1793 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1794 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1795 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1796
1797 // Setup Runtime library names.
Chris Dewhurst68388a02016-05-18 09:14:13 +00001798 if (Subtarget->is64Bit() && !Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001799 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1800 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1801 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1802 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1803 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1804 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001805 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001806 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001807 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001808 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1809 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1810 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1811 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001812 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1813 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1814 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1815 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
Chris Dewhurst68388a02016-05-18 09:14:13 +00001816 } else if (!Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001817 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1818 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1819 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1820 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1821 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1822 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001823 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001824 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001825 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001826 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1827 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1828 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1829 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001830 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1831 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1832 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1833 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1834 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001835 }
1836
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00001837 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1838
Eli Friedman2518f832011-05-06 20:34:06 +00001839 setMinFunctionAlignment(2);
1840
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001841 computeRegisterProperties(Subtarget->getRegisterInfo());
Chris Lattner0a1762e2008-03-17 03:21:36 +00001842}
1843
Chris Dewhurst68388a02016-05-18 09:14:13 +00001844bool SparcTargetLowering::useSoftFloat() const {
1845 return Subtarget->useSoftFloat();
1846}
1847
Chris Lattner0a1762e2008-03-17 03:21:36 +00001848const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001849 switch ((SPISD::NodeType)Opcode) {
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001850 case SPISD::FIRST_NUMBER: break;
1851 case SPISD::CMPICC: return "SPISD::CMPICC";
1852 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1853 case SPISD::BRICC: return "SPISD::BRICC";
1854 case SPISD::BRXCC: return "SPISD::BRXCC";
1855 case SPISD::BRFCC: return "SPISD::BRFCC";
1856 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1857 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1858 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1859 case SPISD::EH_SJLJ_SETJMP: return "SPISD::EH_SJLJ_SETJMP";
1860 case SPISD::EH_SJLJ_LONGJMP: return "SPISD::EH_SJLJ_LONGJMP";
1861 case SPISD::Hi: return "SPISD::Hi";
1862 case SPISD::Lo: return "SPISD::Lo";
1863 case SPISD::FTOI: return "SPISD::FTOI";
1864 case SPISD::ITOF: return "SPISD::ITOF";
1865 case SPISD::FTOX: return "SPISD::FTOX";
1866 case SPISD::XTOF: return "SPISD::XTOF";
1867 case SPISD::CALL: return "SPISD::CALL";
1868 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001869 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001870 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1871 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1872 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1873 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001874 }
Matthias Braund04893f2015-05-07 21:33:59 +00001875 return nullptr;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001876}
1877
Mehdi Amini44ede332015-07-09 02:09:04 +00001878EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
1879 EVT VT) const {
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001880 if (!VT.isVector())
1881 return MVT::i32;
1882 return VT.changeVectorElementTypeToInteger();
1883}
1884
Chris Lattner0a1762e2008-03-17 03:21:36 +00001885/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1886/// be zero. Op is expected to be a target specific node. Used by DAG
1887/// combiner.
Jay Foada0653a32014-05-14 21:14:37 +00001888void SparcTargetLowering::computeKnownBitsForTargetNode
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001889 (const SDValue Op,
1890 APInt &KnownZero,
1891 APInt &KnownOne,
1892 const SelectionDAG &DAG,
1893 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001894 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001895 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001896
Chris Lattner0a1762e2008-03-17 03:21:36 +00001897 switch (Op.getOpcode()) {
1898 default: break;
1899 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001900 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001901 case SPISD::SELECT_FCC:
Jay Foada0653a32014-05-14 21:14:37 +00001902 DAG.computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1903 DAG.computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001904
Chris Lattner0a1762e2008-03-17 03:21:36 +00001905 // Only known if known in both the LHS and RHS.
1906 KnownOne &= KnownOne2;
1907 KnownZero &= KnownZero2;
1908 break;
1909 }
1910}
1911
Chris Lattner0a1762e2008-03-17 03:21:36 +00001912// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1913// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001914static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001915 ISD::CondCode CC, unsigned &SPCC) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00001916 if (isNullConstant(RHS) &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001917 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001918 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1919 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001920 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1921 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1922 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00001923 isOneConstant(LHS.getOperand(0)) &&
1924 isNullConstant(LHS.getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001925 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001926 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001927 LHS = CMPCC.getOperand(0);
1928 RHS = CMPCC.getOperand(1);
1929 }
1930}
1931
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001932// Convert to a target node and set target flags.
1933SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1934 SelectionDAG &DAG) const {
1935 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1936 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001937 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001938 GA->getValueType(0),
1939 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001940
1941 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1942 return DAG.getTargetConstantPool(CP->getConstVal(),
1943 CP->getValueType(0),
1944 CP->getAlignment(),
1945 CP->getOffset(), TF);
1946
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001947 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1948 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1949 Op.getValueType(),
1950 0,
1951 TF);
1952
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001953 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1954 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1955 ES->getValueType(0), TF);
1956
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001957 llvm_unreachable("Unhandled address SDNode");
1958}
1959
1960// Split Op into high and low parts according to HiTF and LoTF.
1961// Return an ADD node combining the parts.
1962SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1963 unsigned HiTF, unsigned LoTF,
1964 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001965 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001966 EVT VT = Op.getValueType();
1967 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1968 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1969 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1970}
1971
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001972// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1973// or ExternalSymbol SDNode.
1974SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001975 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001976 EVT VT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001977
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001978 // Handle PIC mode first.
1979 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1980 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001981 SDValue HiLo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1982 SparcMCExpr::VK_Sparc_GOT10, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001983 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1984 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001985 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1986 // function has calls.
1987 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1988 MFI->setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001989 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001990 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1991 false, false, false, 0);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001992 }
1993
1994 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001995 switch(getTargetMachine().getCodeModel()) {
1996 default:
1997 llvm_unreachable("Unsupported absolute code model");
1998 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001999 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002000 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
2001 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00002002 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00002003 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002004 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
2005 SparcMCExpr::VK_Sparc_M44, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002006 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002007 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00002008 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
2009 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
2010 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00002011 case CodeModel::Large: {
2012 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002013 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
2014 SparcMCExpr::VK_Sparc_HM, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002015 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002016 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
2017 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00002018 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
2019 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00002020 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002021}
2022
Wesley Peck527da1b2010-11-23 03:31:01 +00002023SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002024 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002025 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002026}
2027
Chris Lattner840c7002009-09-15 17:46:24 +00002028SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002029 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002030 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002031}
2032
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002033SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2034 SelectionDAG &DAG) const {
2035 return makeAddress(Op, DAG);
2036}
2037
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002038SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2039 SelectionDAG &DAG) const {
2040
2041 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002042 if (DAG.getTarget().Options.EmulatedTLS)
2043 return LowerToTLSEmulatedModel(GA, DAG);
2044
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002045 SDLoc DL(GA);
2046 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002047 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002048
2049 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2050
2051 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002052 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
2053 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
2054 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
2055 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
2056 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
2057 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
2058 unsigned addTF = ((model == TLSModel::GeneralDynamic)
2059 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
2060 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
2061 unsigned callTF = ((model == TLSModel::GeneralDynamic)
2062 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
2063 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002064
2065 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2066 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2067 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2068 withTargetFlags(Op, addTF, DAG));
2069
2070 SDValue Chain = DAG.getEntryNode();
2071 SDValue InFlag;
2072
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002073 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, DL, true), DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002074 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
2075 InFlag = Chain.getValue(1);
2076 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2077 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2078
2079 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2080 SmallVector<SDValue, 4> Ops;
2081 Ops.push_back(Chain);
2082 Ops.push_back(Callee);
2083 Ops.push_back(Symbol);
2084 Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
Eric Christopher9deb75d2015-03-11 22:42:13 +00002085 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2086 DAG.getMachineFunction(), CallingConv::C);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002087 assert(Mask && "Missing call preserved mask for calling convention");
2088 Ops.push_back(DAG.getRegisterMask(Mask));
2089 Ops.push_back(InFlag);
Craig Topper48d114b2014-04-26 18:35:24 +00002090 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002091 InFlag = Chain.getValue(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002092 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, DL, true),
2093 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002094 InFlag = Chain.getValue(1);
2095 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
2096
2097 if (model != TLSModel::LocalDynamic)
2098 return Ret;
2099
2100 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002101 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002102 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002103 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002104 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2105 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002106 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002107 }
2108
2109 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002110 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
2111 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002112
2113 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2114
2115 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2116 // function has calls.
2117 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2118 MFI->setHasCalls(true);
2119
2120 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002121 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
2122 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002123 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2124 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2125 DL, PtrVT, Ptr,
2126 withTargetFlags(Op, ldTF, DAG));
2127 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2128 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002129 withTargetFlags(Op,
2130 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002131 }
2132
2133 assert(model == TLSModel::LocalExec);
2134 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002135 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002136 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002137 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002138 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2139
2140 return DAG.getNode(ISD::ADD, DL, PtrVT,
2141 DAG.getRegister(SP::G7, PtrVT), Offset);
2142}
2143
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002144SDValue
2145SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain, ArgListTy &Args,
2146 SDValue Arg, SDLoc DL,
2147 SelectionDAG &DAG) const {
2148 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2149 EVT ArgVT = Arg.getValueType();
2150 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2151
2152 ArgListEntry Entry;
2153 Entry.Node = Arg;
2154 Entry.Ty = ArgTy;
2155
2156 if (ArgTy->isFP128Ty()) {
2157 // Create a stack object and pass the pointer to the library function.
2158 int FI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002159 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002160 Chain = DAG.getStore(Chain,
2161 DL,
2162 Entry.Node,
2163 FIPtr,
2164 MachinePointerInfo(),
2165 false,
2166 false,
2167 8);
2168
2169 Entry.Node = FIPtr;
2170 Entry.Ty = PointerType::getUnqual(ArgTy);
2171 }
2172 Args.push_back(Entry);
2173 return Chain;
2174}
2175
2176SDValue
2177SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2178 const char *LibFuncName,
2179 unsigned numArgs) const {
2180
2181 ArgListTy Args;
2182
2183 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002184 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002185
Mehdi Amini44ede332015-07-09 02:09:04 +00002186 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002187 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2188 Type *RetTyABI = RetTy;
2189 SDValue Chain = DAG.getEntryNode();
2190 SDValue RetPtr;
2191
2192 if (RetTy->isFP128Ty()) {
2193 // Create a Stack Object to receive the return value of type f128.
2194 ArgListEntry Entry;
2195 int RetFI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002196 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002197 Entry.Node = RetPtr;
2198 Entry.Ty = PointerType::getUnqual(RetTy);
2199 if (!Subtarget->is64Bit())
2200 Entry.isSRet = true;
2201 Entry.isReturned = false;
2202 Args.push_back(Entry);
2203 RetTyABI = Type::getVoidTy(*DAG.getContext());
2204 }
2205
2206 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2207 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2208 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2209 }
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002210 TargetLowering::CallLoweringInfo CLI(DAG);
2211 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002212 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args), 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002213
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002214 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2215
2216 // chain is in second result.
2217 if (RetTyABI == RetTy)
2218 return CallInfo.first;
2219
2220 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2221
2222 Chain = CallInfo.second;
2223
2224 // Load RetPtr to get the return value.
2225 return DAG.getLoad(Op.getValueType(),
2226 SDLoc(Op),
2227 Chain,
2228 RetPtr,
2229 MachinePointerInfo(),
2230 false, false, false, 8);
2231}
2232
2233SDValue
2234SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2235 unsigned &SPCC,
2236 SDLoc DL,
2237 SelectionDAG &DAG) const {
2238
Craig Topper062a2ba2014-04-25 05:30:21 +00002239 const char *LibCall = nullptr;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002240 bool is64Bit = Subtarget->is64Bit();
2241 switch(SPCC) {
2242 default: llvm_unreachable("Unhandled conditional code!");
2243 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2244 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2245 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2246 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2247 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2248 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2249 case SPCC::FCC_UL :
2250 case SPCC::FCC_ULE:
2251 case SPCC::FCC_UG :
2252 case SPCC::FCC_UGE:
2253 case SPCC::FCC_U :
2254 case SPCC::FCC_O :
2255 case SPCC::FCC_LG :
2256 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2257 }
2258
Mehdi Amini44ede332015-07-09 02:09:04 +00002259 auto PtrVT = getPointerTy(DAG.getDataLayout());
2260 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002261 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2262 ArgListTy Args;
2263 SDValue Chain = DAG.getEntryNode();
2264 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2265 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2266
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002267 TargetLowering::CallLoweringInfo CLI(DAG);
2268 CLI.setDebugLoc(DL).setChain(Chain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002269 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args), 0);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002270
2271 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2272
2273 // result is in first, and chain is in second result.
2274 SDValue Result = CallInfo.first;
2275
2276 switch(SPCC) {
2277 default: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002278 SDValue RHS = DAG.getTargetConstant(0, 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_UL : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002283 SDValue Mask = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002284 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002285 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002286 SPCC = SPCC::ICC_NE;
2287 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2288 }
2289 case SPCC::FCC_ULE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002290 SDValue RHS = DAG.getTargetConstant(2, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002291 SPCC = SPCC::ICC_NE;
2292 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2293 }
2294 case SPCC::FCC_UG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002295 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002296 SPCC = SPCC::ICC_G;
2297 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2298 }
2299 case SPCC::FCC_UGE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002300 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002301 SPCC = SPCC::ICC_NE;
2302 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2303 }
2304
2305 case SPCC::FCC_U : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002306 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002307 SPCC = SPCC::ICC_E;
2308 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2309 }
2310 case SPCC::FCC_O : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002311 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002312 SPCC = SPCC::ICC_NE;
2313 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2314 }
2315 case SPCC::FCC_LG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002316 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002317 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002318 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002319 SPCC = SPCC::ICC_NE;
2320 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2321 }
2322 case SPCC::FCC_UE : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002323 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002324 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002325 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002326 SPCC = SPCC::ICC_E;
2327 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2328 }
2329 }
2330}
2331
2332static SDValue
2333LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2334 const SparcTargetLowering &TLI) {
2335
2336 if (Op.getOperand(0).getValueType() == MVT::f64)
2337 return TLI.LowerF128Op(Op, DAG,
2338 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2339
2340 if (Op.getOperand(0).getValueType() == MVT::f32)
2341 return TLI.LowerF128Op(Op, DAG,
2342 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2343
2344 llvm_unreachable("fpextend with non-float operand!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002345 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002346}
2347
2348static SDValue
2349LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2350 const SparcTargetLowering &TLI) {
2351 // FP_ROUND on f64 and f32 are legal.
2352 if (Op.getOperand(0).getValueType() != MVT::f128)
2353 return Op;
2354
2355 if (Op.getValueType() == MVT::f64)
2356 return TLI.LowerF128Op(Op, DAG,
2357 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2358 if (Op.getValueType() == MVT::f32)
2359 return TLI.LowerF128Op(Op, DAG,
2360 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2361
2362 llvm_unreachable("fpround to non-float!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002363 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002364}
2365
2366static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2367 const SparcTargetLowering &TLI,
2368 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002369 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002370 EVT VT = Op.getValueType();
2371 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002372
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002373 // Expand f128 operations to fp128 abi calls.
2374 if (Op.getOperand(0).getValueType() == MVT::f128
2375 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2376 const char *libName = TLI.getLibcallName(VT == MVT::i32
2377 ? RTLIB::FPTOSINT_F128_I32
2378 : RTLIB::FPTOSINT_F128_I64);
2379 return TLI.LowerF128Op(Op, DAG, libName, 1);
2380 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002381
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002382 // Expand if the resulting type is illegal.
2383 if (!TLI.isTypeLegal(VT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002384 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002385
2386 // Otherwise, Convert the fp value to integer in an FP register.
2387 if (VT == MVT::i32)
2388 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2389 else
2390 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2391
2392 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002393}
2394
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002395static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2396 const SparcTargetLowering &TLI,
2397 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002398 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002399 EVT OpVT = Op.getOperand(0).getValueType();
2400 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2401
2402 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2403
2404 // Expand f128 operations to fp128 ABI calls.
2405 if (Op.getValueType() == MVT::f128
2406 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2407 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2408 ? RTLIB::SINTTOFP_I32_F128
2409 : RTLIB::SINTTOFP_I64_F128);
2410 return TLI.LowerF128Op(Op, DAG, libName, 1);
2411 }
2412
2413 // Expand if the operand type is illegal.
2414 if (!TLI.isTypeLegal(OpVT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002415 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002416
2417 // Otherwise, Convert the int value to FP in an FP register.
2418 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2419 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2420 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002421}
2422
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002423static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2424 const SparcTargetLowering &TLI,
2425 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002426 SDLoc dl(Op);
2427 EVT VT = Op.getValueType();
2428
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002429 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002430 // quad floating point instructions and the resulting type is legal.
2431 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2432 (hasHardQuad && TLI.isTypeLegal(VT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002433 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002434
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002435 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002436
2437 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002438 TLI.getLibcallName(VT == MVT::i32
2439 ? RTLIB::FPTOUINT_F128_I32
2440 : RTLIB::FPTOUINT_F128_I64),
2441 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002442}
2443
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002444static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2445 const SparcTargetLowering &TLI,
2446 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002447 SDLoc dl(Op);
2448 EVT OpVT = Op.getOperand(0).getValueType();
2449 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2450
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002451 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002452 // quad floating point instructions and the operand type is legal.
2453 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002454 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002455
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002456 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002457 TLI.getLibcallName(OpVT == MVT::i32
2458 ? RTLIB::UINTTOFP_I32_F128
2459 : RTLIB::UINTTOFP_I64_F128),
2460 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002461}
2462
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002463static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2464 const SparcTargetLowering &TLI,
2465 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002466 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002467 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002468 SDValue LHS = Op.getOperand(2);
2469 SDValue RHS = Op.getOperand(3);
2470 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002471 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002472 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002473
Chris Lattner0a1762e2008-03-17 03:21:36 +00002474 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2475 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2476 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002477
Chris Lattner0a1762e2008-03-17 03:21:36 +00002478 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002479 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002480 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002481 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002482 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002483 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2484 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002485 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002486 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2487 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2488 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2489 Opc = SPISD::BRICC;
2490 } else {
2491 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2492 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2493 Opc = SPISD::BRFCC;
2494 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002495 }
Owen Anderson9f944592009-08-11 20:47:22 +00002496 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002497 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002498}
2499
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002500static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2501 const SparcTargetLowering &TLI,
2502 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002503 SDValue LHS = Op.getOperand(0);
2504 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002505 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002506 SDValue TrueVal = Op.getOperand(2);
2507 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002508 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002509 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002510
Chris Lattner0a1762e2008-03-17 03:21:36 +00002511 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2512 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2513 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002514
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002515 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002516 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002517 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002518 Opc = LHS.getValueType() == MVT::i32 ?
2519 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002520 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2521 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002522 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2523 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2524 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2525 Opc = SPISD::SELECT_ICC;
2526 } else {
2527 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2528 Opc = SPISD::SELECT_FCC;
2529 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2530 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002531 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002532 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002533 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002534}
2535
Chris Dewhurst69fa1922016-05-04 09:33:30 +00002536SDValue SparcTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG,
2537 const SparcTargetLowering &TLI) const {
2538 SDLoc DL(Op);
2539 return DAG.getNode(SPISD::EH_SJLJ_SETJMP, DL,
2540 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), Op.getOperand(1));
2541
2542}
2543
2544SDValue SparcTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG,
2545 const SparcTargetLowering &TLI) const {
2546 SDLoc DL(Op);
2547 return DAG.getNode(SPISD::EH_SJLJ_LONGJMP, DL, MVT::Other, Op.getOperand(0), Op.getOperand(1));
2548}
2549
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002550static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002551 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002552 MachineFunction &MF = DAG.getMachineFunction();
2553 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002554 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002555
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002556 // Need frame address to find the address of VarArgsFrameIndex.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002557 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2558
Chris Lattner0a1762e2008-03-17 03:21:36 +00002559 // vastart just stores the address of the VarArgsFrameIndex slot into the
2560 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002561 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002562 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00002563 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2564 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002565 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002566 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Chris Lattner676c61d2010-09-21 18:41:36 +00002567 MachinePointerInfo(SV), false, false, 0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002568}
2569
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002570static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002571 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002572 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002573 SDValue InChain = Node->getOperand(0);
2574 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002575 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002576 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002577 SDLoc DL(Node);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002578 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002579 MachinePointerInfo(SV), false, false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002580 // Increment the pointer, VAList, to the next vaarg.
2581 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002582 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2583 DL));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002584 // Store the incremented VAList to the legalized pointer.
2585 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
Chris Lattner676c61d2010-09-21 18:41:36 +00002586 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002587 // Load the actual argument out of the pointer VAList.
2588 // We can't count on greater alignment than the word size.
2589 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2590 false, false, false,
2591 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002592}
2593
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002594static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002595 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002596 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2597 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002598 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002599 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002600
Chris Lattner0a1762e2008-03-17 03:21:36 +00002601 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002602 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2603 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002604 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002605
Chris Lattner0a1762e2008-03-17 03:21:36 +00002606 // The resultant pointer is actually 16 words from the bottom of the stack,
2607 // to provide a register spill area.
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002608 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2609 regSpillArea += Subtarget->getStackPointerBias();
2610
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002611 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002612 DAG.getConstant(regSpillArea, dl, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002613 SDValue Ops[2] = { NewVal, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002614 return DAG.getMergeValues(Ops, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002615}
2616
Chris Lattner0a1762e2008-03-17 03:21:36 +00002617
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002618static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002619 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002620 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002621 dl, MVT::Other, DAG.getEntryNode());
2622 return Chain;
2623}
2624
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002625static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2626 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002627 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2628 MFI->setFrameAddressIsTaken(true);
2629
2630 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002631 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002632 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002633 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002634
2635 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002636
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002637 if (depth == 0) {
2638 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2639 if (Subtarget->is64Bit())
2640 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002641 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002642 return FrameAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002643 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002644
2645 // flush first to make sure the windowed registers' values are in stack
2646 SDValue Chain = getFLUSHW(Op, DAG);
2647 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2648
2649 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2650
2651 while (depth--) {
2652 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002653 DAG.getIntPtrConstant(Offset, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002654 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo(),
2655 false, false, false, 0);
2656 }
2657 if (Subtarget->is64Bit())
2658 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002659 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002660 return FrameAddr;
2661}
2662
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002663
2664static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2665 const SparcSubtarget *Subtarget) {
2666
2667 uint64_t depth = Op.getConstantOperandVal(0);
2668
2669 return getFRAMEADDR(depth, Op, DAG, Subtarget);
2670
2671}
2672
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002673static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002674 const SparcTargetLowering &TLI,
2675 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002676 MachineFunction &MF = DAG.getMachineFunction();
2677 MachineFrameInfo *MFI = MF.getFrameInfo();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002678 MFI->setReturnAddressIsTaken(true);
2679
Bill Wendling908bf812014-01-06 00:43:20 +00002680 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002681 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002682
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002683 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002684 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002685 uint64_t depth = Op.getConstantOperandVal(0);
2686
2687 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002688 if (depth == 0) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002689 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2690 unsigned RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002691 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002692 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002693 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002694
2695 // Need frame address to find return address of the caller.
2696 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2697
2698 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2699 SDValue Ptr = DAG.getNode(ISD::ADD,
2700 dl, VT,
2701 FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002702 DAG.getIntPtrConstant(Offset, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002703 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr,
2704 MachinePointerInfo(), false, false, false, 0);
2705
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002706 return RetAddr;
2707}
2708
James Y Knight51208ea2016-04-25 22:54:09 +00002709static SDValue LowerF64Op(SDValue SrcReg64, SDLoc dl, SelectionDAG &DAG, unsigned opcode)
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002710{
James Y Knight51208ea2016-04-25 22:54:09 +00002711 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002712 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002713
2714 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2715 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2716 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2717
James Y Knight51208ea2016-04-25 22:54:09 +00002718 // Note: in little-endian, the floating-point value is stored in the
2719 // registers are in the opposite order, so the subreg with the sign
2720 // bit is the highest-numbered (odd), rather than the
2721 // lowest-numbered (even).
2722
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002723 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2724 SrcReg64);
2725 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2726 SrcReg64);
2727
James Y Knight51208ea2016-04-25 22:54:09 +00002728 if (DAG.getDataLayout().isLittleEndian())
2729 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2730 else
2731 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002732
2733 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2734 dl, MVT::f64), 0);
2735 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2736 DstReg64, Hi32);
2737 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2738 DstReg64, Lo32);
2739 return DstReg64;
2740}
2741
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002742// Lower a f128 load into two f64 loads.
2743static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2744{
2745 SDLoc dl(Op);
2746 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002747 assert(LdNode && LdNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002748 && "Unexpected node type");
2749
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002750 unsigned alignment = LdNode->getAlignment();
2751 if (alignment > 8)
2752 alignment = 8;
2753
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002754 SDValue Hi64 = DAG.getLoad(MVT::f64,
2755 dl,
2756 LdNode->getChain(),
2757 LdNode->getBasePtr(),
2758 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002759 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002760 EVT addrVT = LdNode->getBasePtr().getValueType();
2761 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2762 LdNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002763 DAG.getConstant(8, dl, addrVT));
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002764 SDValue Lo64 = DAG.getLoad(MVT::f64,
2765 dl,
2766 LdNode->getChain(),
2767 LoPtr,
2768 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002769 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002770
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002771 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2772 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002773
2774 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2775 dl, MVT::f128);
2776 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2777 MVT::f128,
2778 SDValue(InFP128, 0),
2779 Hi64,
2780 SubRegEven);
2781 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2782 MVT::f128,
2783 SDValue(InFP128, 0),
2784 Lo64,
2785 SubRegOdd);
2786 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2787 SDValue(Lo64.getNode(), 1) };
Craig Topper48d114b2014-04-26 18:35:24 +00002788 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002789 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
Craig Topper64941d92014-04-27 19:20:57 +00002790 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002791}
2792
James Y Knight3994be82015-08-10 19:11:39 +00002793static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2794{
2795 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2796
2797 EVT MemVT = LdNode->getMemoryVT();
2798 if (MemVT == MVT::f128)
2799 return LowerF128Load(Op, DAG);
2800
2801 return Op;
2802}
2803
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002804// Lower a f128 store into two f64 stores.
2805static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2806 SDLoc dl(Op);
2807 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002808 assert(StNode && StNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002809 && "Unexpected node type");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002810 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2811 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002812
2813 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2814 dl,
2815 MVT::f64,
2816 StNode->getValue(),
2817 SubRegEven);
2818 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2819 dl,
2820 MVT::f64,
2821 StNode->getValue(),
2822 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002823
2824 unsigned alignment = StNode->getAlignment();
2825 if (alignment > 8)
2826 alignment = 8;
2827
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002828 SDValue OutChains[2];
2829 OutChains[0] = DAG.getStore(StNode->getChain(),
2830 dl,
2831 SDValue(Hi64, 0),
2832 StNode->getBasePtr(),
2833 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002834 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002835 EVT addrVT = StNode->getBasePtr().getValueType();
2836 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2837 StNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002838 DAG.getConstant(8, dl, addrVT));
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002839 OutChains[1] = DAG.getStore(StNode->getChain(),
2840 dl,
2841 SDValue(Lo64, 0),
2842 LoPtr,
2843 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002844 false, false, alignment);
Craig Topper48d114b2014-04-26 18:35:24 +00002845 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002846}
2847
James Y Knight3994be82015-08-10 19:11:39 +00002848static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
2849{
2850 SDLoc dl(Op);
2851 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
2852
2853 EVT MemVT = St->getMemoryVT();
2854 if (MemVT == MVT::f128)
2855 return LowerF128Store(Op, DAG);
2856
2857 if (MemVT == MVT::i64) {
2858 // Custom handling for i64 stores: turn it into a bitcast and a
2859 // v2i32 store.
2860 SDValue Val = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, St->getValue());
2861 SDValue Chain = DAG.getStore(
2862 St->getChain(), dl, Val, St->getBasePtr(), St->getPointerInfo(),
2863 St->isVolatile(), St->isNonTemporal(), St->getAlignment(),
2864 St->getAAInfo());
2865 return Chain;
2866 }
2867
2868 return SDValue();
2869}
2870
Roman Divacky7a9c6542014-02-27 19:26:29 +00002871static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
Venkatraman Govindaraju3b6b0e42014-03-01 02:28:34 +00002872 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
2873 && "invalid opcode");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002874
James Y Knight51208ea2016-04-25 22:54:09 +00002875 SDLoc dl(Op);
2876
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002877 if (Op.getValueType() == MVT::f64)
James Y Knight51208ea2016-04-25 22:54:09 +00002878 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002879 if (Op.getValueType() != MVT::f128)
2880 return Op;
2881
Roman Divacky7a9c6542014-02-27 19:26:29 +00002882 // Lower fabs/fneg on f128 to fabs/fneg on f64
2883 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
James Y Knight51208ea2016-04-25 22:54:09 +00002884 // (As with LowerF64Op, on little-endian, we need to negate the odd
2885 // subreg)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002886
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002887 SDValue SrcReg128 = Op.getOperand(0);
2888 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2889 SrcReg128);
2890 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2891 SrcReg128);
James Y Knight51208ea2016-04-25 22:54:09 +00002892
2893 if (DAG.getDataLayout().isLittleEndian()) {
2894 if (isV9)
2895 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
2896 else
2897 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
2898 } else {
2899 if (isV9)
2900 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2901 else
2902 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
2903 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002904
2905 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2906 dl, MVT::f128), 0);
2907 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2908 DstReg128, Hi64);
2909 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2910 DstReg128, Lo64);
2911 return DstReg128;
2912}
2913
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002914static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002915
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002916 if (Op.getValueType() != MVT::i64)
2917 return Op;
2918
2919 SDLoc dl(Op);
2920 SDValue Src1 = Op.getOperand(0);
2921 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2922 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002923 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002924 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2925
2926 SDValue Src2 = Op.getOperand(1);
2927 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2928 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002929 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002930 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2931
2932
2933 bool hasChain = false;
2934 unsigned hiOpc = Op.getOpcode();
2935 switch (Op.getOpcode()) {
2936 default: llvm_unreachable("Invalid opcode");
2937 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2938 case ISD::ADDE: hasChain = true; break;
2939 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2940 case ISD::SUBE: hasChain = true; break;
2941 }
2942 SDValue Lo;
2943 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2944 if (hasChain) {
2945 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2946 Op.getOperand(2));
2947 } else {
2948 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2949 }
2950 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2951 SDValue Carry = Hi.getValue(1);
2952
2953 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2954 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2955 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002956 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002957
2958 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2959 SDValue Ops[2] = { Dst, Carry };
Craig Topper64941d92014-04-27 19:20:57 +00002960 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002961}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002962
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002963// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2964// in LegalizeDAG.cpp except the order of arguments to the library function.
2965static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2966 const SparcTargetLowering &TLI)
2967{
2968 unsigned opcode = Op.getOpcode();
2969 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2970
2971 bool isSigned = (opcode == ISD::SMULO);
2972 EVT VT = MVT::i64;
2973 EVT WideVT = MVT::i128;
2974 SDLoc dl(Op);
2975 SDValue LHS = Op.getOperand(0);
2976
2977 if (LHS.getValueType() != VT)
2978 return Op;
2979
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002980 SDValue ShiftAmt = DAG.getConstant(63, dl, VT);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002981
2982 SDValue RHS = Op.getOperand(1);
2983 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2984 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2985 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2986
2987 SDValue MulResult = TLI.makeLibCall(DAG,
2988 RTLIB::MUL_I128, WideVT,
Craig Topper8fe40e02015-10-22 17:05:00 +00002989 Args, isSigned, dl).first;
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002990 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002991 MulResult, DAG.getIntPtrConstant(0, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002992 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002993 MulResult, DAG.getIntPtrConstant(1, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002994 if (isSigned) {
2995 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2996 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2997 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002998 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, dl, VT),
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002999 ISD::SETNE);
3000 }
3001 // MulResult is a node with an illegal type. Because such things are not
Chandler Carruthee1a1fc2014-08-02 00:24:54 +00003002 // generally permitted during this phase of legalization, ensure that
3003 // nothing is left using the node. The above EXTRACT_ELEMENT nodes should have
3004 // been folded.
3005 assert(MulResult->use_empty() && "Illegally typed node still in use!");
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003006
3007 SDValue Ops[2] = { BottomHalf, TopHalf } ;
Craig Topper64941d92014-04-27 19:20:57 +00003008 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003009}
3010
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003011static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00003012 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
3013 // Expand with a fence.
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003014 return SDValue();
JF Bastien800f87a2016-04-06 21:19:33 +00003015
3016 // Monotonic load/stores are legal.
3017 return Op;
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003018}
3019
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003020SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
3021 SelectionDAG &DAG) const {
3022 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3023 SDLoc dl(Op);
3024 switch (IntNo) {
3025 default: return SDValue(); // Don't custom lower most intrinsics.
3026 case Intrinsic::thread_pointer: {
3027 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3028 return DAG.getRegister(SP::G7, PtrVT);
3029 }
3030 }
3031}
3032
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003033SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00003034LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003035
3036 bool hasHardQuad = Subtarget->hasHardQuad();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003037 bool isV9 = Subtarget->isV9();
3038
Chris Lattner0a1762e2008-03-17 03:21:36 +00003039 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003040 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00003041
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00003042 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
3043 Subtarget);
3044 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
3045 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00003046 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003047 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00003048 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003049 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003050 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
3051 hasHardQuad);
3052 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
3053 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00003054 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
3055 hasHardQuad);
3056 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3057 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003058 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
3059 hasHardQuad);
3060 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
3061 hasHardQuad);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003062 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG, *this);
3063 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG, *this);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003064 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3065 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00003066 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00003067 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00003068
James Y Knight3994be82015-08-10 19:11:39 +00003069 case ISD::LOAD: return LowerLOAD(Op, DAG);
3070 case ISD::STORE: return LowerSTORE(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003071 case ISD::FADD: return LowerF128Op(Op, DAG,
3072 getLibcallName(RTLIB::ADD_F128), 2);
3073 case ISD::FSUB: return LowerF128Op(Op, DAG,
3074 getLibcallName(RTLIB::SUB_F128), 2);
3075 case ISD::FMUL: return LowerF128Op(Op, DAG,
3076 getLibcallName(RTLIB::MUL_F128), 2);
3077 case ISD::FDIV: return LowerF128Op(Op, DAG,
3078 getLibcallName(RTLIB::DIV_F128), 2);
3079 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3080 getLibcallName(RTLIB::SQRT_F128),1);
Roman Divacky7a9c6542014-02-27 19:26:29 +00003081 case ISD::FABS:
3082 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003083 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3084 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00003085 case ISD::ADDC:
3086 case ISD::ADDE:
3087 case ISD::SUBC:
3088 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003089 case ISD::UMULO:
3090 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003091 case ISD::ATOMIC_LOAD:
3092 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003093 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003094 }
3095}
3096
3097MachineBasicBlock *
3098SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00003099 MachineBasicBlock *BB) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00003100 switch (MI->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003101 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00003102 case SP::SELECT_CC_Int_ICC:
3103 case SP::SELECT_CC_FP_ICC:
3104 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003105 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003106 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003107 case SP::SELECT_CC_Int_FCC:
3108 case SP::SELECT_CC_FP_FCC:
3109 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003110 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003111 return expandSelectCC(MI, BB, SP::FBCOND);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003112 case SP::EH_SJLJ_SETJMP32ri:
3113 case SP::EH_SJLJ_SETJMP32rr:
3114 return emitEHSjLjSetJmp(MI, BB);
3115 case SP::EH_SJLJ_LONGJMP32rr:
3116 case SP::EH_SJLJ_LONGJMP32ri:
3117 return emitEHSjLjLongJmp(MI, BB);
3118
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003119 }
3120}
3121
3122MachineBasicBlock*
3123SparcTargetLowering::expandSelectCC(MachineInstr *MI,
3124 MachineBasicBlock *BB,
3125 unsigned BROpcode) const {
Eric Christopherf5e94062015-01-30 23:46:43 +00003126 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003127 DebugLoc dl = MI->getDebugLoc();
3128 unsigned CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003129
Chris Lattner0a1762e2008-03-17 03:21:36 +00003130 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3131 // control-flow pattern. The incoming instruction knows the destination vreg
3132 // to set, the condition code register to branch on, the true/false values to
3133 // select between, and a branch opcode to use.
3134 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc3f79882015-10-20 00:59:43 +00003135 MachineFunction::iterator It = ++BB->getIterator();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003136
Chris Lattner0a1762e2008-03-17 03:21:36 +00003137 // thisMBB:
3138 // ...
3139 // TrueVal = ...
3140 // [f]bCC copy1MBB
3141 // fallthrough --> copy0MBB
3142 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003143 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00003144 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3145 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00003146 F->insert(It, copy0MBB);
3147 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003148
3149 // Transfer the remainder of BB and its successor edges to sinkMBB.
3150 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003151 std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00003152 BB->end());
3153 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3154
3155 // Add the true and fallthrough blocks as its successors.
3156 BB->addSuccessor(copy0MBB);
3157 BB->addSuccessor(sinkMBB);
3158
Dale Johannesen215a9252009-02-13 02:31:35 +00003159 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003160
Chris Lattner0a1762e2008-03-17 03:21:36 +00003161 // copy0MBB:
3162 // %FalseValue = ...
3163 // # fallthrough to sinkMBB
3164 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003165
Chris Lattner0a1762e2008-03-17 03:21:36 +00003166 // Update machine-CFG edges
3167 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003168
Chris Lattner0a1762e2008-03-17 03:21:36 +00003169 // sinkMBB:
3170 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3171 // ...
3172 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00003173 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattner0a1762e2008-03-17 03:21:36 +00003174 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
3175 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003176
Dan Gohman34396292010-07-06 20:24:04 +00003177 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00003178 return BB;
3179}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003180
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003181
3182MachineBasicBlock* SparcTargetLowering::
3183emitEHSjLjLongJmp(MachineInstr *MI,
3184 MachineBasicBlock *MBB) const
3185{
3186 DebugLoc DL = MI->getDebugLoc();
3187 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3188
3189 MachineFunction *MF = MBB->getParent();
3190 MachineRegisterInfo &MRI = MF->getRegInfo();
3191 MachineInstrBuilder MIB;
3192
3193 MVT PVT = getPointerTy(MF->getDataLayout());
3194 unsigned RegSize = PVT.getStoreSize();
3195 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3196
3197 unsigned Buf = MI->getOperand(0).getReg();
3198 unsigned JmpLoc = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3199
3200 // TO DO: If we do 64-bit handling, this perhaps should be FLUSHW, not TA 3
3201 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::TRAPri), SP::G0).addImm(3).addImm(SPCC::ICC_A);
3202
3203 // Instruction to restore FP
3204 const unsigned FP = SP::I6;
3205 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3206 .addReg(FP)
3207 .addReg(Buf)
3208 .addImm(0);
3209
3210 // Instruction to load jmp location
3211 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3212 .addReg(JmpLoc, RegState::Define)
3213 .addReg(Buf)
3214 .addImm(RegSize);
3215
3216 // Instruction to restore SP
3217 const unsigned SP = SP::O6;
3218 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3219 .addReg(SP)
3220 .addReg(Buf)
3221 .addImm(2 * RegSize);
3222
3223 // Instruction to restore I7
3224 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3225 .addReg(SP::I7)
3226 .addReg(Buf, RegState::Kill)
3227 .addImm(3 * RegSize);
3228
3229 // Jump to JmpLoc
3230 BuildMI(*MBB, MI, DL, TII->get(SP::JMPLrr)).addReg(SP::G0).addReg(JmpLoc, RegState::Kill).addReg(SP::G0);
3231
3232 MI->eraseFromParent();
3233 return MBB;
3234}
3235
3236MachineBasicBlock* SparcTargetLowering::
3237emitEHSjLjSetJmp(MachineInstr *MI,
3238 MachineBasicBlock *MBB) const
3239{
3240 DebugLoc DL = MI->getDebugLoc();
3241 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3242
3243 MachineFunction *MF = MBB->getParent();
3244 MachineRegisterInfo &MRI = MF->getRegInfo();
3245 MachineInstrBuilder MIB;
3246
3247 MVT PVT = getPointerTy(MF->getDataLayout());
3248 unsigned RegSize = PVT.getStoreSize();
3249 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3250
3251 unsigned DstReg = MI->getOperand(0).getReg();
3252 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
3253 assert(RC->hasType(MVT::i32) && "Invalid destination!");
3254 unsigned mainDstReg = MRI.createVirtualRegister(RC);
3255 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
3256
3257 // For v = setjmp(buf), we generate
3258 //
3259 // thisMBB:
3260 // buf[0] = FP
3261 // buf[RegSize] = restoreMBB <-- takes address of restoreMBB
3262 // buf[RegSize * 2] = O6
3263 // buf[RegSize * 3] = I7
3264 // Ensure restoreMBB remains in the relocations list (done using a bn instruction)
3265 // b mainMBB
3266 //
3267 // mainMBB:
3268 // v_main = 0
3269 // b sinkMBB
3270 //
3271 // restoreMBB:
3272 // v_restore = 1
3273 // --fall through--
3274 //
3275 // sinkMBB:
3276 // v = phi(main, restore)
3277
3278 const BasicBlock *BB = MBB->getBasicBlock();
3279 MachineFunction::iterator It = ++MBB->getIterator();
3280 MachineBasicBlock *thisMBB = MBB;
3281 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
3282 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
3283 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
3284
3285 MF->insert(It, mainMBB);
3286 MF->insert(It, restoreMBB);
3287 MF->insert(It, sinkMBB);
3288 restoreMBB->setHasAddressTaken();
3289
3290 // Transfer the remainder of BB and its successor edges to sinkMBB.
3291 sinkMBB->splice(sinkMBB->begin(), MBB,
3292 std::next(MachineBasicBlock::iterator(MI)),
3293 MBB->end());
3294 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
3295
3296 unsigned LabelReg = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3297 unsigned LabelReg2 = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3298 unsigned BufReg = MI->getOperand(1).getReg();
3299
3300 // Instruction to store FP
3301 const unsigned FP = SP::I6;
3302 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3303 .addReg(BufReg)
3304 .addImm(0)
3305 .addReg(FP);
3306
3307 // Instructions to store jmp location
3308 MIB = BuildMI(thisMBB, DL, TII->get(SP::SETHIi))
3309 .addReg(LabelReg, RegState::Define)
3310 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_HI);
3311
3312 MIB = BuildMI(thisMBB, DL, TII->get(SP::ORri))
3313 .addReg(LabelReg2, RegState::Define)
3314 .addReg(LabelReg, RegState::Kill)
3315 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_LO);
3316
3317 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3318 .addReg(BufReg)
3319 .addImm(RegSize)
3320 .addReg(LabelReg2, RegState::Kill);
3321
3322 // Instruction to store SP
3323 const unsigned SP = SP::O6;
3324 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3325 .addReg(BufReg)
3326 .addImm(2 * RegSize)
3327 .addReg(SP);
3328
3329 // Instruction to store I7
3330 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3331 .addReg(BufReg)
3332 .addImm(3 * RegSize)
3333 .addReg(SP::I7);
3334
3335
3336 // FIX ME: This next instruction ensures that the restoreMBB block address remains
3337 // valid through optimization passes and serves no other purpose. The ICC_N ensures
3338 // that the branch is never taken. This commented-out code here was an alternative
3339 // attempt to achieve this which brought myriad problems.
3340 //MIB = BuildMI(thisMBB, DL, TII->get(SP::EH_SjLj_Setup)).addMBB(restoreMBB, SparcMCExpr::VK_Sparc_None);
3341 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3342 .addMBB(restoreMBB)
3343 .addImm(SPCC::ICC_N);
3344
3345 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3346 .addMBB(mainMBB)
3347 .addImm(SPCC::ICC_A);
3348
3349 thisMBB->addSuccessor(mainMBB);
3350 thisMBB->addSuccessor(restoreMBB);
3351
3352
3353 // mainMBB:
3354 MIB = BuildMI(mainMBB, DL, TII->get(SP::ORrr))
3355 .addReg(mainDstReg, RegState::Define)
3356 .addReg(SP::G0)
3357 .addReg(SP::G0);
3358 MIB = BuildMI(mainMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3359
3360 mainMBB->addSuccessor(sinkMBB);
3361
3362
3363 // restoreMBB:
3364 MIB = BuildMI(restoreMBB, DL, TII->get(SP::ORri))
3365 .addReg(restoreDstReg, RegState::Define)
3366 .addReg(SP::G0)
3367 .addImm(1);
3368 //MIB = BuildMI(restoreMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3369 restoreMBB->addSuccessor(sinkMBB);
3370
3371 // sinkMBB:
3372 MIB = BuildMI(*sinkMBB, sinkMBB->begin(), DL,
3373 TII->get(SP::PHI), DstReg)
3374 .addReg(mainDstReg).addMBB(mainMBB)
3375 .addReg(restoreDstReg).addMBB(restoreMBB);
3376
3377 MI->eraseFromParent();
3378 return sinkMBB;
3379}
3380
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003381//===----------------------------------------------------------------------===//
3382// Sparc Inline Assembly Support
3383//===----------------------------------------------------------------------===//
3384
3385/// getConstraintType - Given a constraint letter, return the type of
3386/// constraint it is for this target.
3387SparcTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003388SparcTargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003389 if (Constraint.size() == 1) {
3390 switch (Constraint[0]) {
3391 default: break;
3392 case 'r': return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003393 case 'I': // SIMM13
3394 return C_Other;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003395 }
3396 }
3397
3398 return TargetLowering::getConstraintType(Constraint);
3399}
3400
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003401TargetLowering::ConstraintWeight SparcTargetLowering::
3402getSingleConstraintMatchWeight(AsmOperandInfo &info,
3403 const char *constraint) const {
3404 ConstraintWeight weight = CW_Invalid;
3405 Value *CallOperandVal = info.CallOperandVal;
3406 // If we don't have a value, we can't do a match,
3407 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003408 if (!CallOperandVal)
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003409 return CW_Default;
3410
3411 // Look at the constraint type.
3412 switch (*constraint) {
3413 default:
3414 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3415 break;
3416 case 'I': // SIMM13
3417 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3418 if (isInt<13>(C->getSExtValue()))
3419 weight = CW_Constant;
3420 }
3421 break;
3422 }
3423 return weight;
3424}
3425
3426/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3427/// vector. If it is invalid, don't add anything to Ops.
3428void SparcTargetLowering::
3429LowerAsmOperandForConstraint(SDValue Op,
3430 std::string &Constraint,
3431 std::vector<SDValue> &Ops,
3432 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003433 SDValue Result(nullptr, 0);
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003434
3435 // Only support length 1 constraints for now.
3436 if (Constraint.length() > 1)
3437 return;
3438
3439 char ConstraintLetter = Constraint[0];
3440 switch (ConstraintLetter) {
3441 default: break;
3442 case 'I':
3443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3444 if (isInt<13>(C->getSExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003445 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
3446 Op.getValueType());
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003447 break;
3448 }
3449 return;
3450 }
3451 }
3452
3453 if (Result.getNode()) {
3454 Ops.push_back(Result);
3455 return;
3456 }
3457 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3458}
3459
Eric Christopher11e4df72015-02-26 22:38:43 +00003460std::pair<unsigned, const TargetRegisterClass *>
3461SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003462 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003463 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003464 if (Constraint.size() == 1) {
3465 switch (Constraint[0]) {
3466 case 'r':
James Y Knight3994be82015-08-10 19:11:39 +00003467 if (VT == MVT::v2i32)
3468 return std::make_pair(0U, &SP::IntPairRegClass);
3469 else
3470 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003471 }
James Y Knight3994be82015-08-10 19:11:39 +00003472 } else if (!Constraint.empty() && Constraint.size() <= 5
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003473 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3474 // constraint = '{r<d>}'
3475 // Remove the braces from around the name.
3476 StringRef name(Constraint.data()+1, Constraint.size()-2);
3477 // Handle register aliases:
3478 // r0-r7 -> g0-g7
3479 // r8-r15 -> o0-o7
3480 // r16-r23 -> l0-l7
3481 // r24-r31 -> i0-i7
3482 uint64_t intVal = 0;
3483 if (name.substr(0, 1).equals("r")
3484 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3485 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3486 char regType = regTypes[intVal/8];
3487 char regIdx = '0' + (intVal % 8);
3488 char tmp[] = { '{', regType, regIdx, '}', 0 };
3489 std::string newConstraint = std::string(tmp);
Eric Christopher11e4df72015-02-26 22:38:43 +00003490 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3491 VT);
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003492 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003493 }
3494
Eric Christopher11e4df72015-02-26 22:38:43 +00003495 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003496}
3497
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003498bool
3499SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3500 // The Sparc target isn't yet aware of offsets.
3501 return false;
3502}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003503
3504void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3505 SmallVectorImpl<SDValue>& Results,
3506 SelectionDAG &DAG) const {
3507
3508 SDLoc dl(N);
3509
3510 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3511
3512 switch (N->getOpcode()) {
3513 default:
3514 llvm_unreachable("Do not know how to custom type legalize this operation!");
3515
3516 case ISD::FP_TO_SINT:
3517 case ISD::FP_TO_UINT:
3518 // Custom lower only if it involves f128 or i64.
3519 if (N->getOperand(0).getValueType() != MVT::f128
3520 || N->getValueType(0) != MVT::i64)
3521 return;
3522 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3523 ? RTLIB::FPTOSINT_F128_I64
3524 : RTLIB::FPTOUINT_F128_I64);
3525
3526 Results.push_back(LowerF128Op(SDValue(N, 0),
3527 DAG,
3528 getLibcallName(libCall),
3529 1));
3530 return;
3531
3532 case ISD::SINT_TO_FP:
3533 case ISD::UINT_TO_FP:
3534 // Custom lower only if it involves f128 or i64.
3535 if (N->getValueType(0) != MVT::f128
3536 || N->getOperand(0).getValueType() != MVT::i64)
3537 return;
3538
3539 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3540 ? RTLIB::SINTTOFP_I64_F128
3541 : RTLIB::UINTTOFP_I64_F128);
3542
3543 Results.push_back(LowerF128Op(SDValue(N, 0),
3544 DAG,
3545 getLibcallName(libCall),
3546 1));
3547 return;
James Y Knight3994be82015-08-10 19:11:39 +00003548 case ISD::LOAD: {
3549 LoadSDNode *Ld = cast<LoadSDNode>(N);
3550 // Custom handling only for i64: turn i64 load into a v2i32 load,
3551 // and a bitcast.
3552 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3553 return;
3554
3555 SDLoc dl(N);
3556 SDValue LoadRes = DAG.getExtLoad(
3557 Ld->getExtensionType(), dl, MVT::v2i32,
3558 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
3559 MVT::v2i32, Ld->isVolatile(), Ld->isNonTemporal(),
3560 Ld->isInvariant(), Ld->getAlignment(), Ld->getAAInfo());
3561
3562 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3563 Results.push_back(Res);
3564 Results.push_back(LoadRes.getValue(1));
3565 return;
3566 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003567 }
3568}
Marcin Koscielnicki33571e22016-04-26 10:37:14 +00003569
3570// Override to enable LOAD_STACK_GUARD lowering on Linux.
3571bool SparcTargetLowering::useLoadStackGuardNode() const {
3572 if (!Subtarget->isTargetLinux())
3573 return TargetLowering::useLoadStackGuardNode();
3574 return true;
3575}
3576
3577// Override to disable global variable loading on Linux.
3578void SparcTargetLowering::insertSSPDeclarations(Module &M) const {
3579 if (!Subtarget->isTargetLinux())
3580 return TargetLowering::insertSSPDeclarations(M);
3581}