blob: 3d743448e795dbd2763eb22d7ab7ce86c70362f2 [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
James Y Knight2cc9da92016-08-12 14:48:09 +000035
Chris Lattner49b269d2008-03-17 05:41:48 +000036//===----------------------------------------------------------------------===//
37// Calling Convention Implementation
38//===----------------------------------------------------------------------===//
39
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000040static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
41 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
42 ISD::ArgFlagsTy &ArgFlags, CCState &State)
43{
44 assert (ArgFlags.isSRet());
45
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000046 // Assign SRet argument.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000047 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
48 0,
49 LocVT, LocInfo));
50 return true;
51}
52
James Y Knight3994be82015-08-10 19:11:39 +000053static bool CC_Sparc_Assign_Split_64(unsigned &ValNo, MVT &ValVT,
54 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
55 ISD::ArgFlagsTy &ArgFlags, CCState &State)
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000056{
Craig Topper840beec2014-04-04 05:16:06 +000057 static const MCPhysReg RegList[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000058 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
59 };
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000060 // Try to get first reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000061 if (unsigned Reg = State.AllocateReg(RegList)) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000062 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
63 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000064 // Assign whole thing in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000065 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
66 State.AllocateStack(8,4),
67 LocVT, LocInfo));
68 return true;
69 }
70
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000071 // Try to get second reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000072 if (unsigned Reg = State.AllocateReg(RegList))
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000073 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
74 else
75 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
76 State.AllocateStack(4,4),
77 LocVT, LocInfo));
78 return true;
79}
80
James Y Knight3994be82015-08-10 19:11:39 +000081static bool CC_Sparc_Assign_Ret_Split_64(unsigned &ValNo, MVT &ValVT,
82 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
83 ISD::ArgFlagsTy &ArgFlags, CCState &State)
84{
85 static const MCPhysReg RegList[] = {
86 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
87 };
88
89 // Try to get first reg.
90 if (unsigned Reg = State.AllocateReg(RegList))
91 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
92 else
93 return false;
94
95 // Try to get second reg.
96 if (unsigned Reg = State.AllocateReg(RegList))
97 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
98 else
99 return false;
100
101 return true;
102}
103
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000104// Allocate a full-sized argument for the 64-bit ABI.
105static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
106 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
107 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000108 assert((LocVT == MVT::f32 || LocVT == MVT::f128
109 || LocVT.getSizeInBits() == 64) &&
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000110 "Can't handle non-64 bits locations");
111
112 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000113 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
114 unsigned alignment = (LocVT == MVT::f128) ? 16 : 8;
115 unsigned Offset = State.AllocateStack(size, alignment);
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000116 unsigned Reg = 0;
117
118 if (LocVT == MVT::i64 && Offset < 6*8)
119 // Promote integers to %i0-%i5.
120 Reg = SP::I0 + Offset/8;
121 else if (LocVT == MVT::f64 && Offset < 16*8)
122 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
123 Reg = SP::D0 + Offset/8;
124 else if (LocVT == MVT::f32 && Offset < 16*8)
125 // Promote floats to %f1, %f3, ...
126 Reg = SP::F1 + Offset/4;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000127 else if (LocVT == MVT::f128 && Offset < 16*8)
128 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
129 Reg = SP::Q0 + Offset/16;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000130
131 // Promote to register when possible, otherwise use the stack slot.
132 if (Reg) {
133 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
134 return true;
135 }
136
137 // This argument goes on the stack in an 8-byte slot.
138 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
139 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
140 if (LocVT == MVT::f32)
141 Offset += 4;
142
143 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
144 return true;
145}
146
147// Allocate a half-sized argument for the 64-bit ABI.
148//
149// This is used when passing { float, int } structs by value in registers.
150static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
151 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
152 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
153 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
154 unsigned Offset = State.AllocateStack(4, 4);
155
156 if (LocVT == MVT::f32 && Offset < 16*8) {
157 // Promote floats to %f0-%f31.
158 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
159 LocVT, LocInfo));
160 return true;
161 }
162
163 if (LocVT == MVT::i32 && Offset < 6*8) {
164 // Promote integers to %i0-%i5, using half the register.
165 unsigned Reg = SP::I0 + Offset/8;
166 LocVT = MVT::i64;
167 LocInfo = CCValAssign::AExt;
168
169 // Set the Custom bit if this i32 goes in the high bits of a register.
170 if (Offset % 8 == 0)
171 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
172 LocVT, LocInfo));
173 else
174 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
175 return true;
176 }
177
178 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
179 return true;
180}
181
Chris Lattner49b269d2008-03-17 05:41:48 +0000182#include "SparcGenCallingConv.inc"
183
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000184// The calling conventions in SparcCallingConv.td are described in terms of the
185// callee's register window. This function translates registers to the
186// corresponding caller window %o register.
187static unsigned toCallerWindow(unsigned Reg) {
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +0000188 static_assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7,
189 "Unexpected enum");
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000190 if (Reg >= SP::I0 && Reg <= SP::I7)
191 return Reg - SP::I0 + SP::O0;
192 return Reg;
193}
194
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000195SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000196SparcTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
197 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000198 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000199 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000200 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000201 if (Subtarget->is64Bit())
202 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
203 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
204}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000205
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000206SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000207SparcTargetLowering::LowerReturn_32(SDValue Chain, CallingConv::ID CallConv,
208 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000209 const SmallVectorImpl<ISD::OutputArg> &Outs,
210 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000211 const SDLoc &DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000212 MachineFunction &MF = DAG.getMachineFunction();
213
Chris Lattner49b269d2008-03-17 05:41:48 +0000214 // CCValAssign - represent the assignment of the return value to locations.
215 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000216
Chris Lattner49b269d2008-03-17 05:41:48 +0000217 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000218 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
219 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000220
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000221 // Analyze return values.
222 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000223
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000224 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000225 SmallVector<SDValue, 4> RetOps(1, Chain);
226 // Make room for the return address offset.
227 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000228
229 // Copy the result values into the output registers.
James Y Knight3994be82015-08-10 19:11:39 +0000230 for (unsigned i = 0, realRVLocIdx = 0;
231 i != RVLocs.size();
232 ++i, ++realRVLocIdx) {
Chris Lattner49b269d2008-03-17 05:41:48 +0000233 CCValAssign &VA = RVLocs[i];
234 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000235
James Y Knight3994be82015-08-10 19:11:39 +0000236 SDValue Arg = OutVals[realRVLocIdx];
237
238 if (VA.needsCustom()) {
239 assert(VA.getLocVT() == MVT::v2i32);
240 // Legalize ret v2i32 -> ret 2 x i32 (Basically: do what would
241 // happen by default if this wasn't a legal type)
242
243 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
244 Arg,
245 DAG.getConstant(0, DL, getVectorIdxTy(DAG.getDataLayout())));
246 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
247 Arg,
248 DAG.getConstant(1, DL, getVectorIdxTy(DAG.getDataLayout())));
249
250 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part0, Flag);
251 Flag = Chain.getValue(1);
252 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
253 VA = RVLocs[++i]; // skip ahead to next loc
254 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part1,
255 Flag);
256 } else
257 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000258
Chris Lattner49b269d2008-03-17 05:41:48 +0000259 // Guarantee that all emitted copies are stuck together with flags.
260 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000261 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000262 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000263
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000264 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000265 // If the function returns a struct, copy the SRetReturnReg to I0
266 if (MF.getFunction()->hasStructRetAttr()) {
267 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
268 unsigned Reg = SFI->getSRetReturnReg();
269 if (!Reg)
270 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +0000271 auto PtrVT = getPointerTy(DAG.getDataLayout());
272 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, PtrVT);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000273 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000274 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +0000275 RetOps.push_back(DAG.getRegister(SP::I0, PtrVT));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000276 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000277 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000278
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000279 RetOps[0] = Chain; // Update chain.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000280 RetOps[1] = DAG.getConstant(RetAddrOffset, DL, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000281
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000282 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000283 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000284 RetOps.push_back(Flag);
285
Craig Topper48d114b2014-04-26 18:35:24 +0000286 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000287}
288
289// Lower return values for the 64-bit ABI.
290// Return values are passed the exactly the same way as function arguments.
291SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000292SparcTargetLowering::LowerReturn_64(SDValue Chain, CallingConv::ID CallConv,
293 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000294 const SmallVectorImpl<ISD::OutputArg> &Outs,
295 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000296 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000297 // CCValAssign - represent the assignment of the return value to locations.
298 SmallVector<CCValAssign, 16> RVLocs;
299
300 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000301 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
302 *DAG.getContext());
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000303
304 // Analyze return values.
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +0000305 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000306
307 SDValue Flag;
308 SmallVector<SDValue, 4> RetOps(1, Chain);
309
310 // The second operand on the return instruction is the return address offset.
311 // The return address is always %i7+8 with the 64-bit ABI.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000312 RetOps.push_back(DAG.getConstant(8, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000313
314 // Copy the result values into the output registers.
315 for (unsigned i = 0; i != RVLocs.size(); ++i) {
316 CCValAssign &VA = RVLocs[i];
317 assert(VA.isRegLoc() && "Can only return in registers!");
318 SDValue OutVal = OutVals[i];
319
320 // Integer return values must be sign or zero extended by the callee.
321 switch (VA.getLocInfo()) {
Lang Hames06234ec2014-01-14 19:56:36 +0000322 case CCValAssign::Full: break;
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000323 case CCValAssign::SExt:
324 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
325 break;
326 case CCValAssign::ZExt:
327 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
328 break;
329 case CCValAssign::AExt:
330 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000331 break;
Lang Hames06234ec2014-01-14 19:56:36 +0000332 default:
333 llvm_unreachable("Unknown loc info!");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000334 }
335
336 // The custom bit on an i32 return value indicates that it should be passed
337 // in the high bits of the register.
338 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
339 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000340 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000341
342 // The next value may go in the low bits of the same register.
343 // Handle both at once.
344 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
345 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
346 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
347 // Skip the next value, it's already done.
348 ++i;
349 }
350 }
351
352 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
353
354 // Guarantee that all emitted copies are stuck together with flags.
355 Flag = Chain.getValue(1);
356 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
357 }
358
359 RetOps[0] = Chain; // Update chain.
360
361 // Add the flag if we have it.
362 if (Flag.getNode())
363 RetOps.push_back(Flag);
364
Craig Topper48d114b2014-04-26 18:35:24 +0000365 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Chris Lattner49b269d2008-03-17 05:41:48 +0000366}
367
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000368SDValue SparcTargetLowering::LowerFormalArguments(
369 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
370 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
371 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000372 if (Subtarget->is64Bit())
373 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
374 DL, DAG, InVals);
375 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
376 DL, DAG, InVals);
377}
378
379/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000380/// passed in either one or two GPRs, including FP values. TODO: we should
381/// pass FP values in FP registers for fastcc functions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000382SDValue SparcTargetLowering::LowerFormalArguments_32(
383 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
384 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
385 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000386 MachineFunction &MF = DAG.getMachineFunction();
387 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000388 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000389
390 // Assign locations to all of the incoming arguments.
391 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000392 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
393 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000394 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000395
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000396 const unsigned StackOffset = 92;
James Y Knight33beb242015-12-15 19:23:12 +0000397 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000398
Reid Kleckner79418562014-05-09 22:32:13 +0000399 unsigned InIdx = 0;
400 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000401 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000402
Reid Kleckner79418562014-05-09 22:32:13 +0000403 if (Ins[InIdx].Flags.isSRet()) {
404 if (InIdx != 0)
405 report_fatal_error("sparc only supports sret on the first parameter");
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000406 // Get SRet from [%fp+64].
Matthias Braun941a7052016-07-28 18:40:00 +0000407 int FrameIdx = MF.getFrameInfo().CreateFixedObject(4, 64, true);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000408 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +0000409 SDValue Arg =
410 DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000411 InVals.push_back(Arg);
412 continue;
413 }
414
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000415 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000416 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000417 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
418
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000419 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
420 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
421 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000422
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000423 assert(i+1 < e);
424 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000425
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000426 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000427 if (NextVA.isMemLoc()) {
Matthias Braun941a7052016-07-28 18:40:00 +0000428 int FrameIdx = MF.getFrameInfo().
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000429 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000430 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +0000431 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000432 } else {
433 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000434 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000435 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000436 }
James Y Knight33beb242015-12-15 19:23:12 +0000437
438 if (IsLittleEndian)
439 std::swap(LoVal, HiVal);
440
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000441 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000442 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000443 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000444 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000445 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000446 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000447 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
448 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
449 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
450 if (VA.getLocVT() == MVT::f32)
451 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
452 else if (VA.getLocVT() != MVT::i32) {
453 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
454 DAG.getValueType(VA.getLocVT()));
455 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
456 }
457 InVals.push_back(Arg);
458 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000459 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000460
461 assert(VA.isMemLoc());
462
463 unsigned Offset = VA.getLocMemOffset()+StackOffset;
Mehdi Amini44ede332015-07-09 02:09:04 +0000464 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000465
466 if (VA.needsCustom()) {
Hans Wennborg1f094852016-04-11 20:35:41 +0000467 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000468 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000469 if (Offset % 8 == 0) {
Matthias Braun941a7052016-07-28 18:40:00 +0000470 int FI = MF.getFrameInfo().CreateFixedObject(8,
471 Offset,
472 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000473 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +0000474 SDValue Load =
475 DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000476 InVals.push_back(Load);
477 continue;
478 }
479
Matthias Braun941a7052016-07-28 18:40:00 +0000480 int FI = MF.getFrameInfo().CreateFixedObject(4,
481 Offset,
482 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000483 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +0000484 SDValue HiVal =
485 DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Matthias Braun941a7052016-07-28 18:40:00 +0000486 int FI2 = MF.getFrameInfo().CreateFixedObject(4,
487 Offset+4,
488 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000489 SDValue FIPtr2 = DAG.getFrameIndex(FI2, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000490
Justin Lebar9c375812016-07-15 18:27:10 +0000491 SDValue LoVal =
492 DAG.getLoad(MVT::i32, dl, Chain, FIPtr2, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000493
James Y Knight33beb242015-12-15 19:23:12 +0000494 if (IsLittleEndian)
495 std::swap(LoVal, HiVal);
496
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000497 SDValue WholeValue =
498 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000499 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000500 InVals.push_back(WholeValue);
501 continue;
502 }
503
Matthias Braun941a7052016-07-28 18:40:00 +0000504 int FI = MF.getFrameInfo().CreateFixedObject(4,
505 Offset,
506 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000507 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000508 SDValue Load ;
509 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
Justin Lebar9c375812016-07-15 18:27:10 +0000510 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, MachinePointerInfo());
James Y Knight33beb242015-12-15 19:23:12 +0000511 } else if (VA.getValVT() == MVT::f128) {
512 report_fatal_error("SPARCv8 does not handle f128 in calls; "
513 "pass indirectly");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000514 } else {
James Y Knight33beb242015-12-15 19:23:12 +0000515 // We shouldn't see any other value types here.
James Y Knight99fcb722015-12-15 23:07:16 +0000516 llvm_unreachable("Unexpected ValVT encountered in frame lowering.");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000517 }
518 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000519 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000520
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000521 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000522 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000523 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
524 unsigned Reg = SFI->getSRetReturnReg();
525 if (!Reg) {
526 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
527 SFI->setSRetReturnReg(Reg);
528 }
529 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
530 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
531 }
532
Chris Lattner49b269d2008-03-17 05:41:48 +0000533 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000534 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +0000535 static const MCPhysReg ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000536 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
537 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000538 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs);
Craig Topper840beec2014-04-04 05:16:06 +0000539 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000540 unsigned ArgOffset = CCInfo.getNextStackOffset();
541 if (NumAllocated == 6)
542 ArgOffset += StackOffset;
543 else {
544 assert(!ArgOffset);
545 ArgOffset = 68+4*NumAllocated;
546 }
547
Chris Lattner49b269d2008-03-17 05:41:48 +0000548 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000549 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000550
Eli Friedmanbe853b72009-07-19 19:53:46 +0000551 std::vector<SDValue> OutChains;
552
Chris Lattner49b269d2008-03-17 05:41:48 +0000553 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
554 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
555 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000556 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000557
Matthias Braun941a7052016-07-28 18:40:00 +0000558 int FrameIdx = MF.getFrameInfo().CreateFixedObject(4, ArgOffset,
559 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000560 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000561
Justin Lebar9c375812016-07-15 18:27:10 +0000562 OutChains.push_back(
563 DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, MachinePointerInfo()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000564 ArgOffset += 4;
565 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000566
567 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000568 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +0000569 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Eli Friedmanbe853b72009-07-19 19:53:46 +0000570 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000571 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000572
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000573 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000574}
575
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000576// Lower formal arguments for the 64 bit ABI.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000577SDValue SparcTargetLowering::LowerFormalArguments_64(
578 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
579 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
580 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000581 MachineFunction &MF = DAG.getMachineFunction();
582
583 // Analyze arguments according to CC_Sparc64.
584 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000585 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
586 *DAG.getContext());
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000587 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
588
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000589 // The argument array begins at %fp+BIAS+128, after the register save area.
590 const unsigned ArgArea = 128;
591
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000592 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
593 CCValAssign &VA = ArgLocs[i];
594 if (VA.isRegLoc()) {
595 // This argument is passed in a register.
596 // All integer register arguments are promoted by the caller to i64.
597
598 // Create a virtual register for the promoted live-in value.
599 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
600 getRegClassFor(VA.getLocVT()));
601 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
602
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000603 // Get the high bits for i32 struct elements.
604 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
605 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000606 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000607
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000608 // The caller promoted the argument, so insert an Assert?ext SDNode so we
609 // won't promote the value again in this function.
610 switch (VA.getLocInfo()) {
611 case CCValAssign::SExt:
612 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
613 DAG.getValueType(VA.getValVT()));
614 break;
615 case CCValAssign::ZExt:
616 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
617 DAG.getValueType(VA.getValVT()));
618 break;
619 default:
620 break;
621 }
622
623 // Truncate the register down to the argument type.
624 if (VA.isExtInLoc())
625 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
626
627 InVals.push_back(Arg);
628 continue;
629 }
630
631 // The registers are exhausted. This argument was passed on the stack.
632 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000633 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
634 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000635 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000636 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
637 // Adjust offset for extended arguments, SPARC is big-endian.
638 // The caller will have written the full slot with extended bytes, but we
639 // prefer our own extending loads.
640 if (VA.isExtInLoc())
641 Offset += 8 - ValSize;
Matthias Braun941a7052016-07-28 18:40:00 +0000642 int FI = MF.getFrameInfo().CreateFixedObject(ValSize, Offset, true);
Justin Lebar9c375812016-07-15 18:27:10 +0000643 InVals.push_back(
644 DAG.getLoad(VA.getValVT(), DL, Chain,
645 DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
646 MachinePointerInfo::getFixedStack(MF, FI)));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000647 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000648
649 if (!IsVarArg)
650 return Chain;
651
652 // This function takes variable arguments, some of which may have been passed
653 // in registers %i0-%i5. Variable floating point arguments are never passed
654 // in floating point registers. They go on %i0-%i5 or on the stack like
655 // integer arguments.
656 //
657 // The va_start intrinsic needs to know the offset to the first variable
658 // argument.
659 unsigned ArgOffset = CCInfo.getNextStackOffset();
660 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
661 // Skip the 128 bytes of register save area.
662 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
663 Subtarget->getStackPointerBias());
664
665 // Save the variable arguments that were passed in registers.
666 // The caller is required to reserve stack space for 6 arguments regardless
667 // of how many arguments were actually passed.
668 SmallVector<SDValue, 8> OutChains;
669 for (; ArgOffset < 6*8; ArgOffset += 8) {
670 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
671 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
Matthias Braun941a7052016-07-28 18:40:00 +0000672 int FI = MF.getFrameInfo().CreateFixedObject(8, ArgOffset + ArgArea, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000673 auto PtrVT = getPointerTy(MF.getDataLayout());
Justin Lebar9c375812016-07-15 18:27:10 +0000674 OutChains.push_back(
675 DAG.getStore(Chain, DL, VArg, DAG.getFrameIndex(FI, PtrVT),
676 MachinePointerInfo::getFixedStack(MF, FI)));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000677 }
678
679 if (!OutChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000680 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000681
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000682 return Chain;
683}
684
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000685SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000686SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000687 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000688 if (Subtarget->is64Bit())
689 return LowerCall_64(CLI, InVals);
690 return LowerCall_32(CLI, InVals);
691}
692
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000693static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
694 ImmutableCallSite *CS) {
695 if (CS)
696 return CS->hasFnAttr(Attribute::ReturnsTwice);
697
Craig Topper062a2ba2014-04-25 05:30:21 +0000698 const Function *CalleeFn = nullptr;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000699 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
700 CalleeFn = dyn_cast<Function>(G->getGlobal());
701 } else if (ExternalSymbolSDNode *E =
702 dyn_cast<ExternalSymbolSDNode>(Callee)) {
703 const Function *Fn = DAG.getMachineFunction().getFunction();
704 const Module *M = Fn->getParent();
705 const char *CalleeName = E->getSymbol();
706 CalleeFn = M->getFunction(CalleeName);
707 }
708
709 if (!CalleeFn)
710 return false;
711 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
712}
713
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000714// Lower a call for the 32-bit ABI.
715SDValue
716SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
717 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000718 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000719 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000720 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
721 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
722 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000723 SDValue Chain = CLI.Chain;
724 SDValue Callee = CLI.Callee;
725 bool &isTailCall = CLI.IsTailCall;
726 CallingConv::ID CallConv = CLI.CallConv;
727 bool isVarArg = CLI.IsVarArg;
728
Evan Cheng67a69dd2010-01-27 00:07:07 +0000729 // Sparc target does not yet support tail call optimization.
730 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000731
Chris Lattner7d4152b2008-03-17 06:58:37 +0000732 // Analyze operands of the call, assigning locations to each operand.
733 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000734 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
735 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000736 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000737
Chris Lattner7d4152b2008-03-17 06:58:37 +0000738 // Get the size of the outgoing arguments stack space requirement.
739 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000740
Chris Lattner49b269d2008-03-17 05:41:48 +0000741 // Keep stack frames 8-byte aligned.
742 ArgsSize = (ArgsSize+7) & ~7;
743
Matthias Braun941a7052016-07-28 18:40:00 +0000744 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000745
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000746 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000747 SmallVector<SDValue, 8> ByValArgs;
748 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
749 ISD::ArgFlagsTy Flags = Outs[i].Flags;
750 if (!Flags.isByVal())
751 continue;
752
753 SDValue Arg = OutVals[i];
754 unsigned Size = Flags.getByValSize();
755 unsigned Align = Flags.getByValAlign();
756
Chris Dewhurst53bde952016-06-01 08:48:56 +0000757 if (Size > 0U) {
Matthias Braun941a7052016-07-28 18:40:00 +0000758 int FI = MFI.CreateStackObject(Size, Align, false);
Chris Dewhurst53bde952016-06-01 08:48:56 +0000759 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
760 SDValue SizeNode = DAG.getConstant(Size, dl, MVT::i32);
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000761
Chris Dewhurst53bde952016-06-01 08:48:56 +0000762 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
763 false, // isVolatile,
764 (Size <= 32), // AlwaysInline if size <= 32,
765 false, // isTailCall
766 MachinePointerInfo(), MachinePointerInfo());
767 ByValArgs.push_back(FIPtr);
768 }
769 else {
770 SDValue nullVal;
771 ByValArgs.push_back(nullVal);
772 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000773 }
774
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000775 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000776 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000777
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000778 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
779 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000780
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000781 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000782 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000783 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000784 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000785 i != e;
786 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000787 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000788 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000789
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000790 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
791
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000792 // Use local copy if it is a byval arg.
Chris Dewhurst53bde952016-06-01 08:48:56 +0000793 if (Flags.isByVal()) {
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000794 Arg = ByValArgs[byvalArgIdx++];
Chris Dewhurst53bde952016-06-01 08:48:56 +0000795 if (!Arg) {
796 continue;
797 }
798 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000799
Chris Lattner7d4152b2008-03-17 06:58:37 +0000800 // Promote the value if needed.
801 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000802 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000803 case CCValAssign::Full: break;
804 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000805 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000806 break;
807 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000808 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000809 break;
810 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000811 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
812 break;
813 case CCValAssign::BCvt:
814 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000815 break;
816 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000817
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000818 if (Flags.isSRet()) {
819 assert(VA.needsCustom());
820 // store SRet argument in %sp+64
821 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000822 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000823 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000824 MemOpChains.push_back(
825 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000826 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000827 continue;
828 }
829
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000830 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000831 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000832
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000833 if (VA.isMemLoc()) {
834 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000835 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000836 if (Offset % 8 == 0) {
837 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000838 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000839 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000840 MemOpChains.push_back(
841 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000842 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000843 }
844 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000845
James Y Knight3994be82015-08-10 19:11:39 +0000846 if (VA.getLocVT() == MVT::f64) {
847 // Move from the float value from float registers into the
848 // integer registers.
849
James Y Knight692e0372015-10-09 21:36:19 +0000850 // TODO: The f64 -> v2i32 conversion is super-inefficient for
851 // constants: it sticks them in the constant pool, then loads
852 // to a fp register, then stores to temp memory, then loads to
853 // integer registers.
James Y Knight3994be82015-08-10 19:11:39 +0000854 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
855 }
856
857 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
858 Arg,
859 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
860 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
861 Arg,
862 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000863
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000864 if (VA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000865 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000866 assert(i+1 != e);
867 CCValAssign &NextVA = ArgLocs[++i];
868 if (NextVA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000869 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000870 } else {
James Y Knight3994be82015-08-10 19:11:39 +0000871 // Store the second part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000872 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
873 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000874 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000875 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000876 MemOpChains.push_back(
877 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000878 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000879 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000880 unsigned Offset = VA.getLocMemOffset() + StackOffset;
James Y Knight3994be82015-08-10 19:11:39 +0000881 // Store the first part.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000882 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000883 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000884 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000885 MemOpChains.push_back(
886 DAG.getStore(Chain, dl, Part0, PtrOff, MachinePointerInfo()));
James Y Knight3994be82015-08-10 19:11:39 +0000887 // Store the second part.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000888 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000889 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000890 MemOpChains.push_back(
891 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000892 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000893 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000894 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000895
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000896 // Arguments that can be passed on register must be kept at
897 // RegsToPass vector
898 if (VA.isRegLoc()) {
899 if (VA.getLocVT() != MVT::f32) {
900 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
901 continue;
902 }
903 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
904 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
905 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000906 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000907
908 assert(VA.isMemLoc());
909
910 // Create a store off the stack pointer for this argument.
911 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000912 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
913 dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000914 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000915 MemOpChains.push_back(
916 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000917 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000918
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000919
Chris Lattner49b269d2008-03-17 05:41:48 +0000920 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000921 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000922 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000923
924 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000925 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000926 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000927 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000928 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000929 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000930 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000931 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000932 InFlag = Chain.getValue(1);
933 }
934
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000935 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000936 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000937
Chris Lattner49b269d2008-03-17 05:41:48 +0000938 // If the callee is a GlobalAddress node (quite common, every direct call is)
939 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000940 // Likewise ExternalSymbol -> TargetExternalSymbol.
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +0000941 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Chris Lattner49b269d2008-03-17 05:41:48 +0000942 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000943 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000944 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000945 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000946
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000947 // Returns a chain & a flag for retval copy to use
948 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
949 SmallVector<SDValue, 8> Ops;
950 Ops.push_back(Chain);
951 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000952 if (hasStructRetAttr)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000953 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000954 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
955 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
956 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000957
958 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000959 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000960 const uint32_t *Mask =
961 ((hasReturnsTwice)
962 ? TRI->getRTCallPreservedMask(CallConv)
963 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000964 assert(Mask && "Missing call preserved mask for calling convention");
965 Ops.push_back(DAG.getRegisterMask(Mask));
966
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000967 if (InFlag.getNode())
968 Ops.push_back(InFlag);
969
Craig Topper48d114b2014-04-26 18:35:24 +0000970 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
Chris Lattner49b269d2008-03-17 05:41:48 +0000971 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000972
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000973 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
974 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000975 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000976
Chris Lattnerdb26db22008-03-17 06:01:07 +0000977 // Assign locations to each value returned by this call.
978 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000979 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
980 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000981
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000982 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000983
Chris Lattnerdb26db22008-03-17 06:01:07 +0000984 // Copy all of the result registers out of their specified physreg.
985 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Nirav Dave29938542016-02-26 18:55:22 +0000986 if (RVLocs[i].getLocVT() == MVT::v2i32) {
987 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
988 SDValue Lo = DAG.getCopyFromReg(
989 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InFlag);
990 Chain = Lo.getValue(1);
991 InFlag = Lo.getValue(2);
992 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
993 DAG.getConstant(0, dl, MVT::i32));
994 SDValue Hi = DAG.getCopyFromReg(
995 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InFlag);
996 Chain = Hi.getValue(1);
997 InFlag = Hi.getValue(2);
998 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
999 DAG.getConstant(1, dl, MVT::i32));
1000 InVals.push_back(Vec);
1001 } else {
1002 Chain =
1003 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1004 RVLocs[i].getValVT(), InFlag)
1005 .getValue(1);
1006 InFlag = Chain.getValue(2);
1007 InVals.push_back(Chain.getValue(0));
1008 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001009 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001010
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001011 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +00001012}
1013
Chris Dewhurstad741172016-05-20 10:21:01 +00001014// FIXME? Maybe this could be a TableGen attribute on some registers and
1015// this table could be generated automatically from RegInfo.
1016unsigned SparcTargetLowering::getRegisterByName(const char* RegName, EVT VT,
1017 SelectionDAG &DAG) const {
1018 unsigned Reg = StringSwitch<unsigned>(RegName)
1019 .Case("i0", SP::I0).Case("i1", SP::I1).Case("i2", SP::I2).Case("i3", SP::I3)
1020 .Case("i4", SP::I4).Case("i5", SP::I5).Case("i6", SP::I6).Case("i7", SP::I7)
1021 .Case("o0", SP::O0).Case("o1", SP::O1).Case("o2", SP::O2).Case("o3", SP::O3)
1022 .Case("o4", SP::O4).Case("o5", SP::O5).Case("o6", SP::O6).Case("o7", SP::O7)
1023 .Case("l0", SP::L0).Case("l1", SP::L1).Case("l2", SP::L2).Case("l3", SP::L3)
1024 .Case("l4", SP::L4).Case("l5", SP::L5).Case("l6", SP::L6).Case("l7", SP::L7)
1025 .Case("g0", SP::G0).Case("g1", SP::G1).Case("g2", SP::G2).Case("g3", SP::G3)
1026 .Case("g4", SP::G4).Case("g5", SP::G5).Case("g6", SP::G6).Case("g7", SP::G7)
1027 .Default(0);
1028
1029 if (Reg)
1030 return Reg;
1031
1032 report_fatal_error("Invalid register name global variable");
1033}
1034
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001035// This functions returns true if CalleeName is a ABI function that returns
1036// a long double (fp128).
1037static bool isFP128ABICall(const char *CalleeName)
1038{
1039 static const char *const ABICalls[] =
1040 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
1041 "_Q_sqrt", "_Q_neg",
1042 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001043 "_Q_lltoq", "_Q_ulltoq",
Craig Topper062a2ba2014-04-25 05:30:21 +00001044 nullptr
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001045 };
Craig Topper062a2ba2014-04-25 05:30:21 +00001046 for (const char * const *I = ABICalls; *I != nullptr; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001047 if (strcmp(CalleeName, *I) == 0)
1048 return true;
1049 return false;
1050}
1051
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001052unsigned
1053SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
1054{
Craig Topper062a2ba2014-04-25 05:30:21 +00001055 const Function *CalleeFn = nullptr;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001056 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1057 CalleeFn = dyn_cast<Function>(G->getGlobal());
1058 } else if (ExternalSymbolSDNode *E =
1059 dyn_cast<ExternalSymbolSDNode>(Callee)) {
1060 const Function *Fn = DAG.getMachineFunction().getFunction();
1061 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001062 const char *CalleeName = E->getSymbol();
1063 CalleeFn = M->getFunction(CalleeName);
1064 if (!CalleeFn && isFP128ABICall(CalleeName))
1065 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001066 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001067
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001068 if (!CalleeFn)
1069 return 0;
1070
Joerg Sonnenberger72128092015-10-21 20:05:01 +00001071 // It would be nice to check for the sret attribute on CalleeFn here,
1072 // but since it is not part of the function type, any check will misfire.
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001073
Chris Lattner229907c2011-07-18 04:54:35 +00001074 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
1075 Type *ElementTy = Ty->getElementType();
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001076 return DAG.getDataLayout().getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001077}
Chris Lattner49b269d2008-03-17 05:41:48 +00001078
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001079
1080// Fixup floating point arguments in the ... part of a varargs call.
1081//
1082// The SPARC v9 ABI requires that floating point arguments are treated the same
1083// as integers when calling a varargs function. This does not apply to the
1084// fixed arguments that are part of the function's prototype.
1085//
1086// This function post-processes a CCValAssign array created by
1087// AnalyzeCallOperands().
1088static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1089 ArrayRef<ISD::OutputArg> Outs) {
1090 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1091 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001092 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001093 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1094 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001095 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001096 continue;
1097 // The fixed arguments to a varargs function still go in FP registers.
1098 if (Outs[VA.getValNo()].IsFixed)
1099 continue;
1100
1101 // This floating point argument should be reassigned.
1102 CCValAssign NewVA;
1103
1104 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001105 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1106 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1107 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001108 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1109
1110 if (Offset < 6*8) {
1111 // This argument should go in %i0-%i5.
1112 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001113 if (ValTy == MVT::f64)
1114 // Full register, just bitconvert into i64.
1115 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1116 IReg, MVT::i64, CCValAssign::BCvt);
1117 else {
1118 assert(ValTy == MVT::f128 && "Unexpected type!");
1119 // Full register, just bitconvert into i128 -- We will lower this into
1120 // two i64s in LowerCall_64.
1121 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1122 IReg, MVT::i128, CCValAssign::BCvt);
1123 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001124 } else {
1125 // This needs to go to memory, we're out of integer registers.
1126 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1127 Offset, VA.getLocVT(), VA.getLocInfo());
1128 }
1129 ArgLocs[i] = NewVA;
1130 }
1131}
1132
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001133// Lower a call for the 64-bit ABI.
1134SDValue
1135SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1136 SmallVectorImpl<SDValue> &InVals) const {
1137 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001138 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001139 SDValue Chain = CLI.Chain;
Mehdi Amini44ede332015-07-09 02:09:04 +00001140 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001141
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001142 // Sparc target does not yet support tail call optimization.
1143 CLI.IsTailCall = false;
1144
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001145 // Analyze operands of the call, assigning locations to each operand.
1146 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001147 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1148 *DAG.getContext());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001149 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1150
1151 // Get the size of the outgoing arguments stack space requirement.
1152 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001153 // Called functions expect 6 argument words to exist in the stack frame, used
1154 // or not.
1155 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001156
1157 // Keep stack frames 16-byte aligned.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001158 ArgsSize = alignTo(ArgsSize, 16);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001159
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001160 // Varargs calls require special treatment.
1161 if (CLI.IsVarArg)
1162 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1163
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001164 // Adjust the stack pointer to make room for the arguments.
1165 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1166 // with more than 6 arguments.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001167 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001168 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001169
1170 // Collect the set of registers to pass to the function and their values.
1171 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1172 // instruction.
1173 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1174
1175 // Collect chains from all the memory opeations that copy arguments to the
1176 // stack. They must follow the stack pointer adjustment above and precede the
1177 // call instruction itself.
1178 SmallVector<SDValue, 8> MemOpChains;
1179
1180 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1181 const CCValAssign &VA = ArgLocs[i];
1182 SDValue Arg = CLI.OutVals[i];
1183
1184 // Promote the value if needed.
1185 switch (VA.getLocInfo()) {
1186 default:
1187 llvm_unreachable("Unknown location info!");
1188 case CCValAssign::Full:
1189 break;
1190 case CCValAssign::SExt:
1191 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1192 break;
1193 case CCValAssign::ZExt:
1194 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1195 break;
1196 case CCValAssign::AExt:
1197 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1198 break;
1199 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001200 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1201 // SPARC does not support i128 natively. Lower it into two i64, see below.
1202 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1203 || VA.getLocVT() != MVT::i128)
1204 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001205 break;
1206 }
1207
1208 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001209 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1210 && VA.getLocVT() == MVT::i128) {
1211 // Store and reload into the interger register reg and reg+1.
1212 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1213 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
Mehdi Amini44ede332015-07-09 02:09:04 +00001214 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001216 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001217 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001218 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001219
1220 // Store to %sp+BIAS+128+Offset
Justin Lebar9c375812016-07-15 18:27:10 +00001221 SDValue Store =
1222 DAG.getStore(Chain, DL, Arg, HiPtrOff, MachinePointerInfo());
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001223 // Load into Reg and Reg+1
Justin Lebar9c375812016-07-15 18:27:10 +00001224 SDValue Hi64 =
1225 DAG.getLoad(MVT::i64, DL, Store, HiPtrOff, MachinePointerInfo());
1226 SDValue Lo64 =
1227 DAG.getLoad(MVT::i64, DL, Store, LoPtrOff, MachinePointerInfo());
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001228 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1229 Hi64));
1230 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1231 Lo64));
1232 continue;
1233 }
1234
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001235 // The custom bit on an i32 return value indicates that it should be
1236 // passed in the high bits of the register.
1237 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1238 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001239 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001240
1241 // The next value may go in the low bits of the same register.
1242 // Handle both at once.
1243 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1244 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1245 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1246 CLI.OutVals[i+1]);
1247 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1248 // Skip the next value, it's already done.
1249 ++i;
1250 }
1251 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001252 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001253 continue;
1254 }
1255
1256 assert(VA.isMemLoc());
1257
1258 // Create a store off the stack pointer for this argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00001259 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001260 // The argument area starts at %fp+BIAS+128 in the callee frame,
1261 // %sp+BIAS+128 in ours.
1262 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1263 Subtarget->getStackPointerBias() +
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001264 128, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001265 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +00001266 MemOpChains.push_back(
1267 DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001268 }
1269
1270 // Emit all stores, make sure they occur before the call.
1271 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001272 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001273
1274 // Build a sequence of CopyToReg nodes glued together with token chain and
1275 // glue operands which copy the outgoing args into registers. The InGlue is
1276 // necessary since all emitted instructions must be stuck together in order
1277 // to pass the live physical registers.
1278 SDValue InGlue;
1279 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1280 Chain = DAG.getCopyToReg(Chain, DL,
1281 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1282 InGlue = Chain.getValue(1);
1283 }
1284
1285 // If the callee is a GlobalAddress node (quite common, every direct call is)
1286 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1287 // Likewise ExternalSymbol -> TargetExternalSymbol.
1288 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001289 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +00001290 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001291 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001292 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001293 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001294 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001295
1296 // Build the operands for the call instruction itself.
1297 SmallVector<SDValue, 8> Ops;
1298 Ops.push_back(Chain);
1299 Ops.push_back(Callee);
1300 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1301 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1302 RegsToPass[i].second.getValueType()));
1303
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001304 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +00001305 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001306 const uint32_t *Mask =
1307 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
Eric Christopher9deb75d2015-03-11 22:42:13 +00001308 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1309 CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001310 assert(Mask && "Missing call preserved mask for calling convention");
1311 Ops.push_back(DAG.getRegisterMask(Mask));
1312
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001313 // Make sure the CopyToReg nodes are glued to the call instruction which
1314 // consumes the registers.
1315 if (InGlue.getNode())
1316 Ops.push_back(InGlue);
1317
1318 // Now the call itself.
1319 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001320 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001321 InGlue = Chain.getValue(1);
1322
1323 // Revert the stack pointer immediately after the call.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001324 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
1325 DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001326 InGlue = Chain.getValue(1);
1327
1328 // Now extract the return values. This is more or less the same as
1329 // LowerFormalArguments_64.
1330
1331 // Assign locations to each value returned by this call.
1332 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001333 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1334 *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001335
1336 // Set inreg flag manually for codegen generated library calls that
1337 // return float.
Craig Topper062a2ba2014-04-25 05:30:21 +00001338 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == nullptr)
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001339 CLI.Ins[0].Flags.setInReg();
1340
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001341 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001342
1343 // Copy all of the result registers out of their specified physreg.
1344 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1345 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001346 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001347
1348 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1349 // reside in the same register in the high and low bits. Reuse the
1350 // CopyFromReg previous node to avoid duplicate copies.
1351 SDValue RV;
1352 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1353 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1354 RV = Chain.getValue(0);
1355
1356 // But usually we'll create a new CopyFromReg for a different register.
1357 if (!RV.getNode()) {
1358 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1359 Chain = RV.getValue(1);
1360 InGlue = Chain.getValue(2);
1361 }
1362
1363 // Get the high bits for i32 struct elements.
1364 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1365 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001366 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001367
1368 // The callee promoted the return value, so insert an Assert?ext SDNode so
1369 // we won't promote the value again in this function.
1370 switch (VA.getLocInfo()) {
1371 case CCValAssign::SExt:
1372 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1373 DAG.getValueType(VA.getValVT()));
1374 break;
1375 case CCValAssign::ZExt:
1376 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1377 DAG.getValueType(VA.getValVT()));
1378 break;
1379 default:
1380 break;
1381 }
1382
1383 // Truncate the register down to the return value type.
1384 if (VA.isExtInLoc())
1385 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1386
1387 InVals.push_back(RV);
1388 }
1389
1390 return Chain;
1391}
1392
Chris Lattner0a1762e2008-03-17 03:21:36 +00001393//===----------------------------------------------------------------------===//
1394// TargetLowering Implementation
1395//===----------------------------------------------------------------------===//
1396
James Y Knight7306cd42016-03-29 19:09:54 +00001397TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1398 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1399 AI->getType()->getPrimitiveSizeInBits() == 32)
1400 return AtomicExpansionKind::None; // Uses xchg instruction
1401
1402 return AtomicExpansionKind::CmpXChg;
1403}
1404
Chris Lattner0a1762e2008-03-17 03:21:36 +00001405/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1406/// condition.
1407static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1408 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001409 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001410 case ISD::SETEQ: return SPCC::ICC_E;
1411 case ISD::SETNE: return SPCC::ICC_NE;
1412 case ISD::SETLT: return SPCC::ICC_L;
1413 case ISD::SETGT: return SPCC::ICC_G;
1414 case ISD::SETLE: return SPCC::ICC_LE;
1415 case ISD::SETGE: return SPCC::ICC_GE;
1416 case ISD::SETULT: return SPCC::ICC_CS;
1417 case ISD::SETULE: return SPCC::ICC_LEU;
1418 case ISD::SETUGT: return SPCC::ICC_GU;
1419 case ISD::SETUGE: return SPCC::ICC_CC;
1420 }
1421}
1422
1423/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1424/// FCC condition.
1425static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1426 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001427 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001428 case ISD::SETEQ:
1429 case ISD::SETOEQ: return SPCC::FCC_E;
1430 case ISD::SETNE:
1431 case ISD::SETUNE: return SPCC::FCC_NE;
1432 case ISD::SETLT:
1433 case ISD::SETOLT: return SPCC::FCC_L;
1434 case ISD::SETGT:
1435 case ISD::SETOGT: return SPCC::FCC_G;
1436 case ISD::SETLE:
1437 case ISD::SETOLE: return SPCC::FCC_LE;
1438 case ISD::SETGE:
1439 case ISD::SETOGE: return SPCC::FCC_GE;
1440 case ISD::SETULT: return SPCC::FCC_UL;
1441 case ISD::SETULE: return SPCC::FCC_ULE;
1442 case ISD::SETUGT: return SPCC::FCC_UG;
1443 case ISD::SETUGE: return SPCC::FCC_UGE;
1444 case ISD::SETUO: return SPCC::FCC_U;
1445 case ISD::SETO: return SPCC::FCC_O;
1446 case ISD::SETONE: return SPCC::FCC_LG;
1447 case ISD::SETUEQ: return SPCC::FCC_UE;
1448 }
1449}
1450
James Y Knightef31eaf2016-05-03 14:57:18 +00001451SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
Eric Christopherf5e94062015-01-30 23:46:43 +00001452 const SparcSubtarget &STI)
1453 : TargetLowering(TM), Subtarget(&STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +00001454 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00001455
James Y Knightd966fb62015-08-19 14:47:04 +00001456 // Instructions which use registers as conditionals examine all the
1457 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1458 // matters much whether it's ZeroOrOneBooleanContent, or
1459 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1460 // former.
1461 setBooleanContents(ZeroOrOneBooleanContent);
1462 setBooleanVectorContents(ZeroOrOneBooleanContent);
1463
Chris Lattner0a1762e2008-03-17 03:21:36 +00001464 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001465 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
Chris Dewhurst68388a02016-05-18 09:14:13 +00001466 if (!Subtarget->useSoftFloat()) {
1467 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1468 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1469 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1470 }
James Y Knight3994be82015-08-10 19:11:39 +00001471 if (Subtarget->is64Bit()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001472 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001473 } else {
1474 // On 32bit sparc, we define a double-register 32bit register
1475 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1476 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1477
1478 // ...but almost all operations must be expanded, so set that as
1479 // the default.
1480 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1481 setOperationAction(Op, MVT::v2i32, Expand);
1482 }
1483 // Truncating/extending stores/loads are also not supported.
1484 for (MVT VT : MVT::integer_vector_valuetypes()) {
1485 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1486 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1487 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1488
1489 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1490 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1491 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1492
1493 setTruncStoreAction(VT, MVT::v2i32, Expand);
1494 setTruncStoreAction(MVT::v2i32, VT, Expand);
1495 }
1496 // However, load and store *are* legal.
1497 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1498 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1499 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1500 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1501
1502 // And we need to promote i64 loads/stores into vector load/store
1503 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1504 setOperationAction(ISD::STORE, MVT::i64, Custom);
1505
1506 // Sadly, this doesn't work:
1507 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1508 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1509 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001510
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001511 // Turn FP extload into load/fpextend
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001512 for (MVT VT : MVT::fp_valuetypes()) {
1513 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1514 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1515 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001516
Chris Lattner0a1762e2008-03-17 03:21:36 +00001517 // Sparc doesn't have i1 sign extending load
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001518 for (MVT VT : MVT::integer_valuetypes())
1519 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001520
Chris Lattner0a1762e2008-03-17 03:21:36 +00001521 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001522 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001523 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1524 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001525
1526 // Custom legalize GlobalAddress nodes into LO/HI parts.
Mehdi Amini26d48132015-07-24 16:04:22 +00001527 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1528 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1529 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1530 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001531
Chris Lattner0a1762e2008-03-17 03:21:36 +00001532 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001533 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1534 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1535 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001536
1537 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001538 setOperationAction(ISD::UREM, MVT::i32, Expand);
1539 setOperationAction(ISD::SREM, MVT::i32, Expand);
1540 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1541 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001542
Roman Divacky2262cfa2013-10-31 19:22:33 +00001543 // ... nor does SparcV9.
1544 if (Subtarget->is64Bit()) {
1545 setOperationAction(ISD::UREM, MVT::i64, Expand);
1546 setOperationAction(ISD::SREM, MVT::i64, Expand);
1547 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1548 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1549 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001550
1551 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001552 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1553 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001554 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1555 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001556
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001557 // Custom Expand fp<->uint
1558 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1559 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001560 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1561 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001562
Wesley Peck527da1b2010-11-23 03:31:01 +00001563 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1564 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001565
Chris Lattner0a1762e2008-03-17 03:21:36 +00001566 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001567 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1568 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1569 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001570 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1571
Owen Anderson9f944592009-08-11 20:47:22 +00001572 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1573 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1574 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001575 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001576
Chris Lattner0a1762e2008-03-17 03:21:36 +00001577 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001578 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1579 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1580 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1581 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1582 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1583 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001584 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001585
Owen Anderson9f944592009-08-11 20:47:22 +00001586 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1587 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1588 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001589 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001590
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001591 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1592 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1593
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001594 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001595 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1596 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1597 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1598 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001599 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1600 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001601 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1602 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001603 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001604 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001605
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001606 setOperationAction(ISD::CTPOP, MVT::i64,
1607 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001608 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001609 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001610 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001611 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1612 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001613 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001614 }
1615
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001616 // ATOMICs.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001617 // Atomics are supported on SparcV9. 32-bit atomics are also
1618 // supported by some Leon SparcV8 variants. Otherwise, atomics
1619 // are unsupported.
James Y Knight2cc9da92016-08-12 14:48:09 +00001620 if (Subtarget->isV9())
1621 setMaxAtomicSizeInBitsSupported(64);
1622 else if (Subtarget->hasLeonCasa())
Chris Dewhurstd534d3a2016-06-27 22:11:09 +00001623 setMaxAtomicSizeInBitsSupported(64);
James Y Knight19f6cce2016-04-12 20:18:48 +00001624 else
1625 setMaxAtomicSizeInBitsSupported(0);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001626
James Y Knight148a6462016-06-17 18:11:48 +00001627 setMinCmpXchgSizeInBits(32);
1628
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001629 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001630
1631 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1632
1633 // Custom Lower Atomic LOAD/STORE
1634 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1635 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1636
1637 if (Subtarget->is64Bit()) {
1638 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001639 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001640 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1641 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1642 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001643
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001644 if (!Subtarget->isV9()) {
1645 // SparcV8 does not have FNEGD and FABSD.
1646 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1647 setOperationAction(ISD::FABS, MVT::f64, Custom);
1648 }
1649
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001650 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1651 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1652 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1653 setOperationAction(ISD::FREM , MVT::f128, Expand);
1654 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001655 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1656 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001657 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001658 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001659 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001660 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1661 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001662 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001663 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001664 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001665 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1666 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1667 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1668 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1669 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001670 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001671 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1672 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001673 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001674 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1675 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001676
Owen Anderson9f944592009-08-11 20:47:22 +00001677 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1678 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1679 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001680
1681 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001682 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1683 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001684
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001685 if (Subtarget->is64Bit()) {
1686 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1687 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1688 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1689 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001690
1691 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1692 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001693
1694 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1695 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1696 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001697 }
1698
Chris Lattner0a1762e2008-03-17 03:21:36 +00001699 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001700 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001701 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001702 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001703
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001704 setOperationAction(ISD::TRAP , MVT::Other, Legal);
1705
Chris Lattner0a1762e2008-03-17 03:21:36 +00001706 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001707 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1708 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1709 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1710 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1711 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001712
Chris Lattner0a1762e2008-03-17 03:21:36 +00001713 setStackPointerRegisterToSaveRestore(SP::O6);
1714
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001715 setOperationAction(ISD::CTPOP, MVT::i32,
1716 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001717
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001718 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1719 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1720 setOperationAction(ISD::STORE, MVT::f128, Legal);
1721 } else {
1722 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1723 setOperationAction(ISD::STORE, MVT::f128, Custom);
1724 }
1725
1726 if (Subtarget->hasHardQuad()) {
1727 setOperationAction(ISD::FADD, MVT::f128, Legal);
1728 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1729 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1730 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1731 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1732 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1733 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1734 if (Subtarget->isV9()) {
1735 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1736 setOperationAction(ISD::FABS, MVT::f128, Legal);
1737 } else {
1738 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1739 setOperationAction(ISD::FABS, MVT::f128, Custom);
1740 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001741
1742 if (!Subtarget->is64Bit()) {
1743 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1744 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1745 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1746 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1747 }
1748
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001749 } else {
1750 // Custom legalize f128 operations.
1751
1752 setOperationAction(ISD::FADD, MVT::f128, Custom);
1753 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1754 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1755 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1756 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1757 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1758 setOperationAction(ISD::FABS, MVT::f128, Custom);
1759
1760 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1761 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1762 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1763
1764 // Setup Runtime library names.
Chris Dewhurst68388a02016-05-18 09:14:13 +00001765 if (Subtarget->is64Bit() && !Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001766 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1767 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1768 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1769 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1770 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1771 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001772 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001773 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001774 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001775 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1776 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1777 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1778 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001779 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1780 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1781 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1782 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
Chris Dewhurst68388a02016-05-18 09:14:13 +00001783 } else if (!Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001784 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1785 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1786 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1787 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1788 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1789 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001790 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001791 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001792 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001793 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1794 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1795 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1796 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001797 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1798 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1799 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1800 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1801 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001802 }
1803
Chris Dewhurst0c1e0022016-06-19 11:03:28 +00001804 if (Subtarget->fixAllFDIVSQRT()) {
1805 // Promote FDIVS and FSQRTS to FDIVD and FSQRTD instructions instead as
1806 // the former instructions generate errata on LEON processors.
1807 setOperationAction(ISD::FDIV, MVT::f32, Promote);
1808 setOperationAction(ISD::FSQRT, MVT::f32, Promote);
1809 }
1810
1811 if (Subtarget->replaceFMULS()) {
1812 // Promote FMULS to FMULD instructions instead as
1813 // the former instructions generate errata on LEON processors.
1814 setOperationAction(ISD::FMUL, MVT::f32, Promote);
1815 }
1816
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00001817 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1818
Eli Friedman2518f832011-05-06 20:34:06 +00001819 setMinFunctionAlignment(2);
1820
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001821 computeRegisterProperties(Subtarget->getRegisterInfo());
Chris Lattner0a1762e2008-03-17 03:21:36 +00001822}
1823
Chris Dewhurst68388a02016-05-18 09:14:13 +00001824bool SparcTargetLowering::useSoftFloat() const {
1825 return Subtarget->useSoftFloat();
1826}
1827
Chris Lattner0a1762e2008-03-17 03:21:36 +00001828const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001829 switch ((SPISD::NodeType)Opcode) {
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001830 case SPISD::FIRST_NUMBER: break;
1831 case SPISD::CMPICC: return "SPISD::CMPICC";
1832 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1833 case SPISD::BRICC: return "SPISD::BRICC";
1834 case SPISD::BRXCC: return "SPISD::BRXCC";
1835 case SPISD::BRFCC: return "SPISD::BRFCC";
1836 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1837 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1838 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1839 case SPISD::EH_SJLJ_SETJMP: return "SPISD::EH_SJLJ_SETJMP";
1840 case SPISD::EH_SJLJ_LONGJMP: return "SPISD::EH_SJLJ_LONGJMP";
1841 case SPISD::Hi: return "SPISD::Hi";
1842 case SPISD::Lo: return "SPISD::Lo";
1843 case SPISD::FTOI: return "SPISD::FTOI";
1844 case SPISD::ITOF: return "SPISD::ITOF";
1845 case SPISD::FTOX: return "SPISD::FTOX";
1846 case SPISD::XTOF: return "SPISD::XTOF";
1847 case SPISD::CALL: return "SPISD::CALL";
1848 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001849 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001850 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1851 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1852 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1853 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001854 }
Matthias Braund04893f2015-05-07 21:33:59 +00001855 return nullptr;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001856}
1857
Mehdi Amini44ede332015-07-09 02:09:04 +00001858EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
1859 EVT VT) const {
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001860 if (!VT.isVector())
1861 return MVT::i32;
1862 return VT.changeVectorElementTypeToInteger();
1863}
1864
Chris Lattner0a1762e2008-03-17 03:21:36 +00001865/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1866/// be zero. Op is expected to be a target specific node. Used by DAG
1867/// combiner.
Jay Foada0653a32014-05-14 21:14:37 +00001868void SparcTargetLowering::computeKnownBitsForTargetNode
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001869 (const SDValue Op,
1870 APInt &KnownZero,
1871 APInt &KnownOne,
1872 const SelectionDAG &DAG,
1873 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001874 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001875 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001876
Chris Lattner0a1762e2008-03-17 03:21:36 +00001877 switch (Op.getOpcode()) {
1878 default: break;
1879 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001880 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001881 case SPISD::SELECT_FCC:
Jay Foada0653a32014-05-14 21:14:37 +00001882 DAG.computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1883 DAG.computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001884
Chris Lattner0a1762e2008-03-17 03:21:36 +00001885 // Only known if known in both the LHS and RHS.
1886 KnownOne &= KnownOne2;
1887 KnownZero &= KnownZero2;
1888 break;
1889 }
1890}
1891
Chris Lattner0a1762e2008-03-17 03:21:36 +00001892// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1893// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001894static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001895 ISD::CondCode CC, unsigned &SPCC) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00001896 if (isNullConstant(RHS) &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001897 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001898 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1899 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001900 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1901 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1902 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00001903 isOneConstant(LHS.getOperand(0)) &&
1904 isNullConstant(LHS.getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001905 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001906 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001907 LHS = CMPCC.getOperand(0);
1908 RHS = CMPCC.getOperand(1);
1909 }
1910}
1911
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001912// Convert to a target node and set target flags.
1913SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1914 SelectionDAG &DAG) const {
1915 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1916 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001917 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001918 GA->getValueType(0),
1919 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001920
1921 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1922 return DAG.getTargetConstantPool(CP->getConstVal(),
1923 CP->getValueType(0),
1924 CP->getAlignment(),
1925 CP->getOffset(), TF);
1926
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001927 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1928 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1929 Op.getValueType(),
1930 0,
1931 TF);
1932
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001933 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1934 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1935 ES->getValueType(0), TF);
1936
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001937 llvm_unreachable("Unhandled address SDNode");
1938}
1939
1940// Split Op into high and low parts according to HiTF and LoTF.
1941// Return an ADD node combining the parts.
1942SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1943 unsigned HiTF, unsigned LoTF,
1944 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001945 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001946 EVT VT = Op.getValueType();
1947 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1948 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1949 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1950}
1951
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001952// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1953// or ExternalSymbol SDNode.
1954SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001955 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001956 EVT VT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001957
Rafael Espindola428b3e62016-06-27 19:15:08 +00001958 // Handle PIC mode first. SPARC needs a got load for every variable!
1959 if (isPositionIndependent()) {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001960 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001961 SDValue HiLo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1962 SparcMCExpr::VK_Sparc_GOT10, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001963 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1964 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001965 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1966 // function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001967 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
1968 MFI.setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001969 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001970 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001971 }
1972
1973 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001974 switch(getTargetMachine().getCodeModel()) {
1975 default:
1976 llvm_unreachable("Unsupported absolute code model");
1977 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001978 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001979 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1980 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001981 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001982 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001983 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
1984 SparcMCExpr::VK_Sparc_M44, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001985 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001986 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001987 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1988 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1989 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001990 case CodeModel::Large: {
1991 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001992 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
1993 SparcMCExpr::VK_Sparc_HM, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001994 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001995 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1996 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001997 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1998 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001999 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002000}
2001
Wesley Peck527da1b2010-11-23 03:31:01 +00002002SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002003 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002004 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002005}
2006
Chris Lattner840c7002009-09-15 17:46:24 +00002007SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002008 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002009 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002010}
2011
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002012SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2013 SelectionDAG &DAG) const {
2014 return makeAddress(Op, DAG);
2015}
2016
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002017SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2018 SelectionDAG &DAG) const {
2019
2020 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002021 if (DAG.getTarget().Options.EmulatedTLS)
2022 return LowerToTLSEmulatedModel(GA, DAG);
2023
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002024 SDLoc DL(GA);
2025 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002026 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002027
2028 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2029
2030 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002031 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
2032 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
2033 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
2034 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
2035 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
2036 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
2037 unsigned addTF = ((model == TLSModel::GeneralDynamic)
2038 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
2039 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
2040 unsigned callTF = ((model == TLSModel::GeneralDynamic)
2041 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
2042 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002043
2044 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2045 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2046 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2047 withTargetFlags(Op, addTF, DAG));
2048
2049 SDValue Chain = DAG.getEntryNode();
2050 SDValue InFlag;
2051
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002052 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, DL, true), DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002053 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
2054 InFlag = Chain.getValue(1);
2055 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2056 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2057
2058 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopher9deb75d2015-03-11 22:42:13 +00002059 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2060 DAG.getMachineFunction(), CallingConv::C);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002061 assert(Mask && "Missing call preserved mask for calling convention");
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +00002062 SDValue Ops[] = {Chain,
2063 Callee,
2064 Symbol,
2065 DAG.getRegister(SP::O0, PtrVT),
2066 DAG.getRegisterMask(Mask),
2067 InFlag};
Craig Topper48d114b2014-04-26 18:35:24 +00002068 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002069 InFlag = Chain.getValue(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002070 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, DL, true),
2071 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002072 InFlag = Chain.getValue(1);
2073 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
2074
2075 if (model != TLSModel::LocalDynamic)
2076 return Ret;
2077
2078 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002079 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002080 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002081 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002082 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2083 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002084 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002085 }
2086
2087 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002088 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
2089 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002090
2091 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2092
2093 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2094 // function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00002095 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2096 MFI.setHasCalls(true);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002097
2098 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002099 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
2100 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002101 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2102 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2103 DL, PtrVT, Ptr,
2104 withTargetFlags(Op, ldTF, DAG));
2105 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2106 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002107 withTargetFlags(Op,
2108 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002109 }
2110
2111 assert(model == TLSModel::LocalExec);
2112 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002113 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002114 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002115 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002116 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2117
2118 return DAG.getNode(ISD::ADD, DL, PtrVT,
2119 DAG.getRegister(SP::G7, PtrVT), Offset);
2120}
2121
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002122SDValue SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain,
2123 ArgListTy &Args, SDValue Arg,
2124 const SDLoc &DL,
2125 SelectionDAG &DAG) const {
Matthias Braun941a7052016-07-28 18:40:00 +00002126 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002127 EVT ArgVT = Arg.getValueType();
2128 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2129
2130 ArgListEntry Entry;
2131 Entry.Node = Arg;
2132 Entry.Ty = ArgTy;
2133
2134 if (ArgTy->isFP128Ty()) {
2135 // Create a stack object and pass the pointer to the library function.
Matthias Braun941a7052016-07-28 18:40:00 +00002136 int FI = MFI.CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002137 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00002138 Chain = DAG.getStore(Chain, DL, Entry.Node, FIPtr, MachinePointerInfo(),
2139 /* Alignment = */ 8);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002140
2141 Entry.Node = FIPtr;
2142 Entry.Ty = PointerType::getUnqual(ArgTy);
2143 }
2144 Args.push_back(Entry);
2145 return Chain;
2146}
2147
2148SDValue
2149SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2150 const char *LibFuncName,
2151 unsigned numArgs) const {
2152
2153 ArgListTy Args;
2154
Matthias Braun941a7052016-07-28 18:40:00 +00002155 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002156 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002157
Mehdi Amini44ede332015-07-09 02:09:04 +00002158 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002159 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2160 Type *RetTyABI = RetTy;
2161 SDValue Chain = DAG.getEntryNode();
2162 SDValue RetPtr;
2163
2164 if (RetTy->isFP128Ty()) {
2165 // Create a Stack Object to receive the return value of type f128.
2166 ArgListEntry Entry;
Matthias Braun941a7052016-07-28 18:40:00 +00002167 int RetFI = MFI.CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002168 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002169 Entry.Node = RetPtr;
2170 Entry.Ty = PointerType::getUnqual(RetTy);
2171 if (!Subtarget->is64Bit())
2172 Entry.isSRet = true;
2173 Entry.isReturned = false;
2174 Args.push_back(Entry);
2175 RetTyABI = Type::getVoidTy(*DAG.getContext());
2176 }
2177
2178 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2179 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2180 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2181 }
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002182 TargetLowering::CallLoweringInfo CLI(DAG);
2183 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002184 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002185
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002186 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2187
2188 // chain is in second result.
2189 if (RetTyABI == RetTy)
2190 return CallInfo.first;
2191
2192 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2193
2194 Chain = CallInfo.second;
2195
2196 // Load RetPtr to get the return value.
Justin Lebar9c375812016-07-15 18:27:10 +00002197 return DAG.getLoad(Op.getValueType(), SDLoc(Op), Chain, RetPtr,
2198 MachinePointerInfo(), /* Alignment = */ 8);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002199}
2200
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002201SDValue SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2202 unsigned &SPCC, const SDLoc &DL,
2203 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002204
Craig Topper062a2ba2014-04-25 05:30:21 +00002205 const char *LibCall = nullptr;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002206 bool is64Bit = Subtarget->is64Bit();
2207 switch(SPCC) {
2208 default: llvm_unreachable("Unhandled conditional code!");
2209 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2210 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2211 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2212 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2213 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2214 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2215 case SPCC::FCC_UL :
2216 case SPCC::FCC_ULE:
2217 case SPCC::FCC_UG :
2218 case SPCC::FCC_UGE:
2219 case SPCC::FCC_U :
2220 case SPCC::FCC_O :
2221 case SPCC::FCC_LG :
2222 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2223 }
2224
Mehdi Amini44ede332015-07-09 02:09:04 +00002225 auto PtrVT = getPointerTy(DAG.getDataLayout());
2226 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002227 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2228 ArgListTy Args;
2229 SDValue Chain = DAG.getEntryNode();
2230 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2231 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2232
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002233 TargetLowering::CallLoweringInfo CLI(DAG);
2234 CLI.setDebugLoc(DL).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002235 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002236
2237 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2238
2239 // result is in first, and chain is in second result.
2240 SDValue Result = CallInfo.first;
2241
2242 switch(SPCC) {
2243 default: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002244 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002245 SPCC = SPCC::ICC_NE;
2246 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2247 }
2248 case SPCC::FCC_UL : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002249 SDValue Mask = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002250 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002251 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002252 SPCC = SPCC::ICC_NE;
2253 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2254 }
2255 case SPCC::FCC_ULE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002256 SDValue RHS = DAG.getTargetConstant(2, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002257 SPCC = SPCC::ICC_NE;
2258 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2259 }
2260 case SPCC::FCC_UG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002261 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002262 SPCC = SPCC::ICC_G;
2263 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2264 }
2265 case SPCC::FCC_UGE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002266 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002267 SPCC = SPCC::ICC_NE;
2268 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2269 }
2270
2271 case SPCC::FCC_U : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002272 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002273 SPCC = SPCC::ICC_E;
2274 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2275 }
2276 case SPCC::FCC_O : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002277 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002278 SPCC = SPCC::ICC_NE;
2279 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2280 }
2281 case SPCC::FCC_LG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002282 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002283 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002284 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002285 SPCC = SPCC::ICC_NE;
2286 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2287 }
2288 case SPCC::FCC_UE : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002289 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002290 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002291 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002292 SPCC = SPCC::ICC_E;
2293 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2294 }
2295 }
2296}
2297
2298static SDValue
2299LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2300 const SparcTargetLowering &TLI) {
2301
2302 if (Op.getOperand(0).getValueType() == MVT::f64)
2303 return TLI.LowerF128Op(Op, DAG,
2304 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2305
2306 if (Op.getOperand(0).getValueType() == MVT::f32)
2307 return TLI.LowerF128Op(Op, DAG,
2308 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2309
2310 llvm_unreachable("fpextend with non-float operand!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002311 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002312}
2313
2314static SDValue
2315LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2316 const SparcTargetLowering &TLI) {
2317 // FP_ROUND on f64 and f32 are legal.
2318 if (Op.getOperand(0).getValueType() != MVT::f128)
2319 return Op;
2320
2321 if (Op.getValueType() == MVT::f64)
2322 return TLI.LowerF128Op(Op, DAG,
2323 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2324 if (Op.getValueType() == MVT::f32)
2325 return TLI.LowerF128Op(Op, DAG,
2326 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2327
2328 llvm_unreachable("fpround to non-float!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002329 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002330}
2331
2332static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2333 const SparcTargetLowering &TLI,
2334 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002335 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002336 EVT VT = Op.getValueType();
2337 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002338
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002339 // Expand f128 operations to fp128 abi calls.
2340 if (Op.getOperand(0).getValueType() == MVT::f128
2341 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2342 const char *libName = TLI.getLibcallName(VT == MVT::i32
2343 ? RTLIB::FPTOSINT_F128_I32
2344 : RTLIB::FPTOSINT_F128_I64);
2345 return TLI.LowerF128Op(Op, DAG, libName, 1);
2346 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002347
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002348 // Expand if the resulting type is illegal.
2349 if (!TLI.isTypeLegal(VT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002350 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002351
2352 // Otherwise, Convert the fp value to integer in an FP register.
2353 if (VT == MVT::i32)
2354 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2355 else
2356 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2357
2358 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002359}
2360
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002361static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2362 const SparcTargetLowering &TLI,
2363 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002364 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002365 EVT OpVT = Op.getOperand(0).getValueType();
2366 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2367
2368 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2369
2370 // Expand f128 operations to fp128 ABI calls.
2371 if (Op.getValueType() == MVT::f128
2372 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2373 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2374 ? RTLIB::SINTTOFP_I32_F128
2375 : RTLIB::SINTTOFP_I64_F128);
2376 return TLI.LowerF128Op(Op, DAG, libName, 1);
2377 }
2378
2379 // Expand if the operand type is illegal.
2380 if (!TLI.isTypeLegal(OpVT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002381 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002382
2383 // Otherwise, Convert the int value to FP in an FP register.
2384 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2385 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2386 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002387}
2388
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002389static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2390 const SparcTargetLowering &TLI,
2391 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002392 SDLoc dl(Op);
2393 EVT VT = Op.getValueType();
2394
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002395 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002396 // quad floating point instructions and the resulting type is legal.
2397 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2398 (hasHardQuad && TLI.isTypeLegal(VT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002399 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002400
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002401 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002402
2403 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002404 TLI.getLibcallName(VT == MVT::i32
2405 ? RTLIB::FPTOUINT_F128_I32
2406 : RTLIB::FPTOUINT_F128_I64),
2407 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002408}
2409
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002410static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2411 const SparcTargetLowering &TLI,
2412 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002413 SDLoc dl(Op);
2414 EVT OpVT = Op.getOperand(0).getValueType();
2415 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2416
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002417 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002418 // quad floating point instructions and the operand type is legal.
2419 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002420 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002421
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002422 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002423 TLI.getLibcallName(OpVT == MVT::i32
2424 ? RTLIB::UINTTOFP_I32_F128
2425 : RTLIB::UINTTOFP_I64_F128),
2426 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002427}
2428
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002429static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2430 const SparcTargetLowering &TLI,
2431 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002432 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002433 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002434 SDValue LHS = Op.getOperand(2);
2435 SDValue RHS = Op.getOperand(3);
2436 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002437 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002438 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002439
Chris Lattner0a1762e2008-03-17 03:21:36 +00002440 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2441 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2442 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002443
Chris Lattner0a1762e2008-03-17 03:21:36 +00002444 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002445 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002446 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002447 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002448 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002449 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2450 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002451 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002452 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2453 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2454 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2455 Opc = SPISD::BRICC;
2456 } else {
2457 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2458 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2459 Opc = SPISD::BRFCC;
2460 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002461 }
Owen Anderson9f944592009-08-11 20:47:22 +00002462 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002463 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002464}
2465
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002466static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2467 const SparcTargetLowering &TLI,
2468 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002469 SDValue LHS = Op.getOperand(0);
2470 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002471 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002472 SDValue TrueVal = Op.getOperand(2);
2473 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002474 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002475 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002476
Chris Lattner0a1762e2008-03-17 03:21:36 +00002477 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2478 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2479 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002480
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002481 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002482 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002483 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002484 Opc = LHS.getValueType() == MVT::i32 ?
2485 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002486 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2487 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002488 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2489 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2490 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2491 Opc = SPISD::SELECT_ICC;
2492 } else {
2493 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2494 Opc = SPISD::SELECT_FCC;
2495 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2496 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002497 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002498 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002499 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002500}
2501
Chris Dewhurst69fa1922016-05-04 09:33:30 +00002502SDValue SparcTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG,
2503 const SparcTargetLowering &TLI) const {
2504 SDLoc DL(Op);
2505 return DAG.getNode(SPISD::EH_SJLJ_SETJMP, DL,
2506 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), Op.getOperand(1));
2507
2508}
2509
2510SDValue SparcTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG,
2511 const SparcTargetLowering &TLI) const {
2512 SDLoc DL(Op);
2513 return DAG.getNode(SPISD::EH_SJLJ_LONGJMP, DL, MVT::Other, Op.getOperand(0), Op.getOperand(1));
2514}
2515
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002516static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002517 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002518 MachineFunction &MF = DAG.getMachineFunction();
2519 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002520 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002521
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002522 // Need frame address to find the address of VarArgsFrameIndex.
Matthias Braun941a7052016-07-28 18:40:00 +00002523 MF.getFrameInfo().setFrameAddressIsTaken(true);
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002524
Chris Lattner0a1762e2008-03-17 03:21:36 +00002525 // vastart just stores the address of the VarArgsFrameIndex slot into the
2526 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002527 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002528 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00002529 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2530 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002531 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002532 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00002533 MachinePointerInfo(SV));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002534}
2535
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002536static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002537 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002538 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002539 SDValue InChain = Node->getOperand(0);
2540 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002541 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002542 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002543 SDLoc DL(Node);
Justin Lebar9c375812016-07-15 18:27:10 +00002544 SDValue VAList =
2545 DAG.getLoad(PtrVT, DL, InChain, VAListPtr, MachinePointerInfo(SV));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002546 // Increment the pointer, VAList, to the next vaarg.
2547 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002548 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2549 DL));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002550 // Store the incremented VAList to the legalized pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00002551 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr, VAListPtr,
2552 MachinePointerInfo(SV));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002553 // Load the actual argument out of the pointer VAList.
2554 // We can't count on greater alignment than the word size.
2555 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002556 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits()) / 8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002557}
2558
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002559static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002560 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002561 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2562 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002563 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002564 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002565
Chris Lattner0a1762e2008-03-17 03:21:36 +00002566 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002567 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2568 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002569 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002570
Chris Lattner0a1762e2008-03-17 03:21:36 +00002571 // The resultant pointer is actually 16 words from the bottom of the stack,
2572 // to provide a register spill area.
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002573 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2574 regSpillArea += Subtarget->getStackPointerBias();
2575
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002576 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002577 DAG.getConstant(regSpillArea, dl, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002578 SDValue Ops[2] = { NewVal, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002579 return DAG.getMergeValues(Ops, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002580}
2581
Chris Lattner0a1762e2008-03-17 03:21:36 +00002582
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002583static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002584 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002585 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002586 dl, MVT::Other, DAG.getEntryNode());
2587 return Chain;
2588}
2589
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002590static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2591 const SparcSubtarget *Subtarget) {
Matthias Braun941a7052016-07-28 18:40:00 +00002592 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2593 MFI.setFrameAddressIsTaken(true);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002594
2595 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002596 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002597 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002598 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002599
2600 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002601
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002602 if (depth == 0) {
2603 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2604 if (Subtarget->is64Bit())
2605 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002606 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002607 return FrameAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002608 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002609
2610 // flush first to make sure the windowed registers' values are in stack
2611 SDValue Chain = getFLUSHW(Op, DAG);
2612 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2613
2614 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2615
2616 while (depth--) {
2617 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002618 DAG.getIntPtrConstant(Offset, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002619 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo());
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002620 }
2621 if (Subtarget->is64Bit())
2622 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002623 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002624 return FrameAddr;
2625}
2626
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002627
2628static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2629 const SparcSubtarget *Subtarget) {
2630
2631 uint64_t depth = Op.getConstantOperandVal(0);
2632
2633 return getFRAMEADDR(depth, Op, DAG, Subtarget);
James Y Knight2cc9da92016-08-12 14:48:09 +00002634
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002635}
2636
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002637static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002638 const SparcTargetLowering &TLI,
2639 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002640 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002641 MachineFrameInfo &MFI = MF.getFrameInfo();
2642 MFI.setReturnAddressIsTaken(true);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002643
Bill Wendling908bf812014-01-06 00:43:20 +00002644 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002645 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002646
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002647 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002648 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002649 uint64_t depth = Op.getConstantOperandVal(0);
2650
2651 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002652 if (depth == 0) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002653 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2654 unsigned RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002655 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002656 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002657 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002658
2659 // Need frame address to find return address of the caller.
2660 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2661
2662 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2663 SDValue Ptr = DAG.getNode(ISD::ADD,
2664 dl, VT,
2665 FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002666 DAG.getIntPtrConstant(Offset, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002667 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002668
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002669 return RetAddr;
2670}
2671
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002672static SDValue LowerF64Op(SDValue SrcReg64, const SDLoc &dl, SelectionDAG &DAG,
2673 unsigned opcode) {
James Y Knight51208ea2016-04-25 22:54:09 +00002674 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002675 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002676
2677 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2678 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2679 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2680
James Y Knight51208ea2016-04-25 22:54:09 +00002681 // Note: in little-endian, the floating-point value is stored in the
2682 // registers are in the opposite order, so the subreg with the sign
2683 // bit is the highest-numbered (odd), rather than the
2684 // lowest-numbered (even).
2685
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002686 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2687 SrcReg64);
2688 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2689 SrcReg64);
2690
James Y Knight51208ea2016-04-25 22:54:09 +00002691 if (DAG.getDataLayout().isLittleEndian())
2692 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2693 else
2694 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002695
2696 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2697 dl, MVT::f64), 0);
2698 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2699 DstReg64, Hi32);
2700 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2701 DstReg64, Lo32);
2702 return DstReg64;
2703}
2704
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002705// Lower a f128 load into two f64 loads.
2706static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2707{
2708 SDLoc dl(Op);
2709 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002710 assert(LdNode && LdNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002711 && "Unexpected node type");
2712
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002713 unsigned alignment = LdNode->getAlignment();
2714 if (alignment > 8)
2715 alignment = 8;
2716
Justin Lebar9c375812016-07-15 18:27:10 +00002717 SDValue Hi64 =
2718 DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LdNode->getBasePtr(),
2719 LdNode->getPointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002720 EVT addrVT = LdNode->getBasePtr().getValueType();
2721 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2722 LdNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002723 DAG.getConstant(8, dl, addrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00002724 SDValue Lo64 = DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LoPtr,
2725 LdNode->getPointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002726
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002727 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2728 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002729
2730 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2731 dl, MVT::f128);
2732 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2733 MVT::f128,
2734 SDValue(InFP128, 0),
2735 Hi64,
2736 SubRegEven);
2737 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2738 MVT::f128,
2739 SDValue(InFP128, 0),
2740 Lo64,
2741 SubRegOdd);
2742 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2743 SDValue(Lo64.getNode(), 1) };
Craig Topper48d114b2014-04-26 18:35:24 +00002744 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002745 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
Craig Topper64941d92014-04-27 19:20:57 +00002746 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002747}
2748
James Y Knight3994be82015-08-10 19:11:39 +00002749static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2750{
2751 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2752
2753 EVT MemVT = LdNode->getMemoryVT();
2754 if (MemVT == MVT::f128)
2755 return LowerF128Load(Op, DAG);
2756
2757 return Op;
2758}
2759
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002760// Lower a f128 store into two f64 stores.
2761static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2762 SDLoc dl(Op);
2763 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002764 assert(StNode && StNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002765 && "Unexpected node type");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002766 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2767 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002768
2769 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2770 dl,
2771 MVT::f64,
2772 StNode->getValue(),
2773 SubRegEven);
2774 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2775 dl,
2776 MVT::f64,
2777 StNode->getValue(),
2778 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002779
2780 unsigned alignment = StNode->getAlignment();
2781 if (alignment > 8)
2782 alignment = 8;
2783
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002784 SDValue OutChains[2];
Justin Lebar9c375812016-07-15 18:27:10 +00002785 OutChains[0] =
2786 DAG.getStore(StNode->getChain(), dl, SDValue(Hi64, 0),
2787 StNode->getBasePtr(), MachinePointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002788 EVT addrVT = StNode->getBasePtr().getValueType();
2789 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2790 StNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002791 DAG.getConstant(8, dl, addrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00002792 OutChains[1] = DAG.getStore(StNode->getChain(), dl, SDValue(Lo64, 0), LoPtr,
2793 MachinePointerInfo(), alignment);
Craig Topper48d114b2014-04-26 18:35:24 +00002794 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002795}
2796
James Y Knight3994be82015-08-10 19:11:39 +00002797static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
2798{
2799 SDLoc dl(Op);
2800 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
2801
2802 EVT MemVT = St->getMemoryVT();
2803 if (MemVT == MVT::f128)
2804 return LowerF128Store(Op, DAG);
2805
2806 if (MemVT == MVT::i64) {
2807 // Custom handling for i64 stores: turn it into a bitcast and a
2808 // v2i32 store.
2809 SDValue Val = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, St->getValue());
2810 SDValue Chain = DAG.getStore(
2811 St->getChain(), dl, Val, St->getBasePtr(), St->getPointerInfo(),
Douglas Katzman26cfb6a2016-07-21 23:28:54 +00002812 St->getAlignment(), St->getMemOperand()->getFlags(), St->getAAInfo());
James Y Knight3994be82015-08-10 19:11:39 +00002813 return Chain;
2814 }
2815
2816 return SDValue();
2817}
2818
Roman Divacky7a9c6542014-02-27 19:26:29 +00002819static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
Venkatraman Govindaraju3b6b0e42014-03-01 02:28:34 +00002820 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
2821 && "invalid opcode");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002822
James Y Knight51208ea2016-04-25 22:54:09 +00002823 SDLoc dl(Op);
2824
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002825 if (Op.getValueType() == MVT::f64)
James Y Knight51208ea2016-04-25 22:54:09 +00002826 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002827 if (Op.getValueType() != MVT::f128)
2828 return Op;
2829
Roman Divacky7a9c6542014-02-27 19:26:29 +00002830 // Lower fabs/fneg on f128 to fabs/fneg on f64
2831 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
James Y Knight51208ea2016-04-25 22:54:09 +00002832 // (As with LowerF64Op, on little-endian, we need to negate the odd
2833 // subreg)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002834
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002835 SDValue SrcReg128 = Op.getOperand(0);
2836 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2837 SrcReg128);
2838 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2839 SrcReg128);
James Y Knight51208ea2016-04-25 22:54:09 +00002840
2841 if (DAG.getDataLayout().isLittleEndian()) {
2842 if (isV9)
2843 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
2844 else
2845 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
2846 } else {
2847 if (isV9)
2848 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2849 else
2850 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
2851 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002852
2853 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2854 dl, MVT::f128), 0);
2855 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2856 DstReg128, Hi64);
2857 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2858 DstReg128, Lo64);
2859 return DstReg128;
2860}
2861
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002862static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002863
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002864 if (Op.getValueType() != MVT::i64)
2865 return Op;
2866
2867 SDLoc dl(Op);
2868 SDValue Src1 = Op.getOperand(0);
2869 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2870 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002871 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002872 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2873
2874 SDValue Src2 = Op.getOperand(1);
2875 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2876 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002877 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002878 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2879
2880
2881 bool hasChain = false;
2882 unsigned hiOpc = Op.getOpcode();
2883 switch (Op.getOpcode()) {
2884 default: llvm_unreachable("Invalid opcode");
2885 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2886 case ISD::ADDE: hasChain = true; break;
2887 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2888 case ISD::SUBE: hasChain = true; break;
2889 }
2890 SDValue Lo;
2891 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2892 if (hasChain) {
2893 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2894 Op.getOperand(2));
2895 } else {
2896 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2897 }
2898 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2899 SDValue Carry = Hi.getValue(1);
2900
2901 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2902 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2903 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002904 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002905
2906 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2907 SDValue Ops[2] = { Dst, Carry };
Craig Topper64941d92014-04-27 19:20:57 +00002908 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002909}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002910
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002911// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2912// in LegalizeDAG.cpp except the order of arguments to the library function.
2913static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2914 const SparcTargetLowering &TLI)
2915{
2916 unsigned opcode = Op.getOpcode();
2917 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2918
2919 bool isSigned = (opcode == ISD::SMULO);
2920 EVT VT = MVT::i64;
2921 EVT WideVT = MVT::i128;
2922 SDLoc dl(Op);
2923 SDValue LHS = Op.getOperand(0);
2924
2925 if (LHS.getValueType() != VT)
2926 return Op;
2927
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002928 SDValue ShiftAmt = DAG.getConstant(63, dl, VT);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002929
2930 SDValue RHS = Op.getOperand(1);
2931 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2932 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2933 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2934
2935 SDValue MulResult = TLI.makeLibCall(DAG,
2936 RTLIB::MUL_I128, WideVT,
Craig Topper8fe40e02015-10-22 17:05:00 +00002937 Args, isSigned, dl).first;
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002938 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002939 MulResult, DAG.getIntPtrConstant(0, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002940 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002941 MulResult, DAG.getIntPtrConstant(1, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002942 if (isSigned) {
2943 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2944 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2945 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002946 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, dl, VT),
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002947 ISD::SETNE);
2948 }
2949 // MulResult is a node with an illegal type. Because such things are not
Chandler Carruthee1a1fc2014-08-02 00:24:54 +00002950 // generally permitted during this phase of legalization, ensure that
2951 // nothing is left using the node. The above EXTRACT_ELEMENT nodes should have
2952 // been folded.
2953 assert(MulResult->use_empty() && "Illegally typed node still in use!");
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002954
2955 SDValue Ops[2] = { BottomHalf, TopHalf } ;
Craig Topper64941d92014-04-27 19:20:57 +00002956 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002957}
2958
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002959static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00002960 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
2961 // Expand with a fence.
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002962 return SDValue();
JF Bastien800f87a2016-04-06 21:19:33 +00002963
2964 // Monotonic load/stores are legal.
2965 return Op;
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002966}
2967
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00002968SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2969 SelectionDAG &DAG) const {
2970 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2971 SDLoc dl(Op);
2972 switch (IntNo) {
2973 default: return SDValue(); // Don't custom lower most intrinsics.
2974 case Intrinsic::thread_pointer: {
2975 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2976 return DAG.getRegister(SP::G7, PtrVT);
2977 }
2978 }
2979}
2980
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002981SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00002982LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002983
2984 bool hasHardQuad = Subtarget->hasHardQuad();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002985 bool isV9 = Subtarget->isV9();
2986
Chris Lattner0a1762e2008-03-17 03:21:36 +00002987 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002988 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002989
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002990 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
2991 Subtarget);
2992 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
2993 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002994 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002995 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002996 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002997 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002998 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
2999 hasHardQuad);
3000 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
3001 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00003002 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
3003 hasHardQuad);
3004 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3005 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003006 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
3007 hasHardQuad);
3008 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
3009 hasHardQuad);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003010 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG, *this);
3011 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG, *this);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003012 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3013 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00003014 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00003015 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00003016
James Y Knight3994be82015-08-10 19:11:39 +00003017 case ISD::LOAD: return LowerLOAD(Op, DAG);
3018 case ISD::STORE: return LowerSTORE(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003019 case ISD::FADD: return LowerF128Op(Op, DAG,
3020 getLibcallName(RTLIB::ADD_F128), 2);
3021 case ISD::FSUB: return LowerF128Op(Op, DAG,
3022 getLibcallName(RTLIB::SUB_F128), 2);
3023 case ISD::FMUL: return LowerF128Op(Op, DAG,
3024 getLibcallName(RTLIB::MUL_F128), 2);
3025 case ISD::FDIV: return LowerF128Op(Op, DAG,
3026 getLibcallName(RTLIB::DIV_F128), 2);
3027 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3028 getLibcallName(RTLIB::SQRT_F128),1);
Roman Divacky7a9c6542014-02-27 19:26:29 +00003029 case ISD::FABS:
3030 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003031 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3032 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00003033 case ISD::ADDC:
3034 case ISD::ADDE:
3035 case ISD::SUBC:
3036 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003037 case ISD::UMULO:
3038 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003039 case ISD::ATOMIC_LOAD:
3040 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003041 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003042 }
3043}
3044
3045MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003046SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00003047 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003048 switch (MI.getOpcode()) {
James Y Knight2cc9da92016-08-12 14:48:09 +00003049 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00003050 case SP::SELECT_CC_Int_ICC:
3051 case SP::SELECT_CC_FP_ICC:
3052 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003053 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003054 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003055 case SP::SELECT_CC_Int_FCC:
3056 case SP::SELECT_CC_FP_FCC:
3057 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003058 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003059 return expandSelectCC(MI, BB, SP::FBCOND);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003060 case SP::EH_SJLJ_SETJMP32ri:
3061 case SP::EH_SJLJ_SETJMP32rr:
3062 return emitEHSjLjSetJmp(MI, BB);
3063 case SP::EH_SJLJ_LONGJMP32rr:
3064 case SP::EH_SJLJ_LONGJMP32ri:
3065 return emitEHSjLjLongJmp(MI, BB);
James Y Knight2cc9da92016-08-12 14:48:09 +00003066
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003067 }
3068}
3069
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003070MachineBasicBlock *
3071SparcTargetLowering::expandSelectCC(MachineInstr &MI, MachineBasicBlock *BB,
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003072 unsigned BROpcode) const {
Eric Christopherf5e94062015-01-30 23:46:43 +00003073 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003074 DebugLoc dl = MI.getDebugLoc();
3075 unsigned CC = (SPCC::CondCodes)MI.getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003076
Chris Lattner0a1762e2008-03-17 03:21:36 +00003077 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3078 // control-flow pattern. The incoming instruction knows the destination vreg
3079 // to set, the condition code register to branch on, the true/false values to
3080 // select between, and a branch opcode to use.
3081 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc3f79882015-10-20 00:59:43 +00003082 MachineFunction::iterator It = ++BB->getIterator();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003083
Chris Lattner0a1762e2008-03-17 03:21:36 +00003084 // thisMBB:
3085 // ...
3086 // TrueVal = ...
3087 // [f]bCC copy1MBB
3088 // fallthrough --> copy0MBB
3089 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003090 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00003091 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3092 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00003093 F->insert(It, copy0MBB);
3094 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003095
3096 // Transfer the remainder of BB and its successor edges to sinkMBB.
3097 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003098 std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00003099 BB->end());
3100 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3101
3102 // Add the true and fallthrough blocks as its successors.
3103 BB->addSuccessor(copy0MBB);
3104 BB->addSuccessor(sinkMBB);
3105
Dale Johannesen215a9252009-02-13 02:31:35 +00003106 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003107
Chris Lattner0a1762e2008-03-17 03:21:36 +00003108 // copy0MBB:
3109 // %FalseValue = ...
3110 // # fallthrough to sinkMBB
3111 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003112
Chris Lattner0a1762e2008-03-17 03:21:36 +00003113 // Update machine-CFG edges
3114 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003115
Chris Lattner0a1762e2008-03-17 03:21:36 +00003116 // sinkMBB:
3117 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3118 // ...
3119 BB = sinkMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003120 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI.getOperand(0).getReg())
3121 .addReg(MI.getOperand(2).getReg())
3122 .addMBB(copy0MBB)
3123 .addReg(MI.getOperand(1).getReg())
3124 .addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003125
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003126 MI.eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00003127 return BB;
3128}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003129
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003130MachineBasicBlock *
3131SparcTargetLowering::emitEHSjLjLongJmp(MachineInstr &MI,
3132 MachineBasicBlock *MBB) const {
3133 DebugLoc DL = MI.getDebugLoc();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003134 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3135
3136 MachineFunction *MF = MBB->getParent();
3137 MachineRegisterInfo &MRI = MF->getRegInfo();
3138 MachineInstrBuilder MIB;
3139
3140 MVT PVT = getPointerTy(MF->getDataLayout());
3141 unsigned RegSize = PVT.getStoreSize();
3142 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3143
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003144 unsigned Buf = MI.getOperand(0).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003145 unsigned JmpLoc = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3146
3147 // TO DO: If we do 64-bit handling, this perhaps should be FLUSHW, not TA 3
3148 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::TRAPri), SP::G0).addImm(3).addImm(SPCC::ICC_A);
3149
3150 // Instruction to restore FP
3151 const unsigned FP = SP::I6;
3152 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3153 .addReg(FP)
3154 .addReg(Buf)
3155 .addImm(0);
3156
3157 // Instruction to load jmp location
3158 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3159 .addReg(JmpLoc, RegState::Define)
3160 .addReg(Buf)
3161 .addImm(RegSize);
3162
3163 // Instruction to restore SP
3164 const unsigned SP = SP::O6;
3165 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3166 .addReg(SP)
3167 .addReg(Buf)
3168 .addImm(2 * RegSize);
3169
3170 // Instruction to restore I7
3171 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3172 .addReg(SP::I7)
3173 .addReg(Buf, RegState::Kill)
3174 .addImm(3 * RegSize);
3175
3176 // Jump to JmpLoc
3177 BuildMI(*MBB, MI, DL, TII->get(SP::JMPLrr)).addReg(SP::G0).addReg(JmpLoc, RegState::Kill).addReg(SP::G0);
3178
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003179 MI.eraseFromParent();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003180 return MBB;
3181}
3182
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003183MachineBasicBlock *
3184SparcTargetLowering::emitEHSjLjSetJmp(MachineInstr &MI,
3185 MachineBasicBlock *MBB) const {
3186 DebugLoc DL = MI.getDebugLoc();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003187 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
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003197 unsigned DstReg = MI.getOperand(0).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003198 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
3199 assert(RC->hasType(MVT::i32) && "Invalid destination!");
3200 unsigned mainDstReg = MRI.createVirtualRegister(RC);
3201 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
3202
3203 // For v = setjmp(buf), we generate
3204 //
3205 // thisMBB:
3206 // buf[0] = FP
3207 // buf[RegSize] = restoreMBB <-- takes address of restoreMBB
3208 // buf[RegSize * 2] = O6
3209 // buf[RegSize * 3] = I7
3210 // Ensure restoreMBB remains in the relocations list (done using a bn instruction)
3211 // b mainMBB
3212 //
3213 // mainMBB:
3214 // v_main = 0
3215 // b sinkMBB
3216 //
3217 // restoreMBB:
3218 // v_restore = 1
3219 // --fall through--
3220 //
3221 // sinkMBB:
3222 // v = phi(main, restore)
3223
3224 const BasicBlock *BB = MBB->getBasicBlock();
3225 MachineFunction::iterator It = ++MBB->getIterator();
3226 MachineBasicBlock *thisMBB = MBB;
3227 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
3228 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
3229 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
3230
3231 MF->insert(It, mainMBB);
3232 MF->insert(It, restoreMBB);
3233 MF->insert(It, sinkMBB);
3234 restoreMBB->setHasAddressTaken();
3235
3236 // Transfer the remainder of BB and its successor edges to sinkMBB.
3237 sinkMBB->splice(sinkMBB->begin(), MBB,
3238 std::next(MachineBasicBlock::iterator(MI)),
3239 MBB->end());
3240 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
3241
3242 unsigned LabelReg = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3243 unsigned LabelReg2 = MRI.createVirtualRegister(&SP::IntRegsRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003244 unsigned BufReg = MI.getOperand(1).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003245
3246 // Instruction to store FP
3247 const unsigned FP = SP::I6;
3248 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3249 .addReg(BufReg)
3250 .addImm(0)
3251 .addReg(FP);
3252
3253 // Instructions to store jmp location
3254 MIB = BuildMI(thisMBB, DL, TII->get(SP::SETHIi))
3255 .addReg(LabelReg, RegState::Define)
3256 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_HI);
3257
3258 MIB = BuildMI(thisMBB, DL, TII->get(SP::ORri))
3259 .addReg(LabelReg2, RegState::Define)
3260 .addReg(LabelReg, RegState::Kill)
3261 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_LO);
3262
3263 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3264 .addReg(BufReg)
3265 .addImm(RegSize)
3266 .addReg(LabelReg2, RegState::Kill);
3267
3268 // Instruction to store SP
3269 const unsigned SP = SP::O6;
3270 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3271 .addReg(BufReg)
3272 .addImm(2 * RegSize)
3273 .addReg(SP);
3274
3275 // Instruction to store I7
3276 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3277 .addReg(BufReg)
3278 .addImm(3 * RegSize)
3279 .addReg(SP::I7);
3280
3281
3282 // FIX ME: This next instruction ensures that the restoreMBB block address remains
3283 // valid through optimization passes and serves no other purpose. The ICC_N ensures
3284 // that the branch is never taken. This commented-out code here was an alternative
3285 // attempt to achieve this which brought myriad problems.
3286 //MIB = BuildMI(thisMBB, DL, TII->get(SP::EH_SjLj_Setup)).addMBB(restoreMBB, SparcMCExpr::VK_Sparc_None);
3287 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3288 .addMBB(restoreMBB)
3289 .addImm(SPCC::ICC_N);
3290
3291 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3292 .addMBB(mainMBB)
3293 .addImm(SPCC::ICC_A);
3294
3295 thisMBB->addSuccessor(mainMBB);
3296 thisMBB->addSuccessor(restoreMBB);
3297
3298
3299 // mainMBB:
3300 MIB = BuildMI(mainMBB, DL, TII->get(SP::ORrr))
3301 .addReg(mainDstReg, RegState::Define)
3302 .addReg(SP::G0)
3303 .addReg(SP::G0);
3304 MIB = BuildMI(mainMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3305
3306 mainMBB->addSuccessor(sinkMBB);
3307
3308
3309 // restoreMBB:
3310 MIB = BuildMI(restoreMBB, DL, TII->get(SP::ORri))
3311 .addReg(restoreDstReg, RegState::Define)
3312 .addReg(SP::G0)
3313 .addImm(1);
3314 //MIB = BuildMI(restoreMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3315 restoreMBB->addSuccessor(sinkMBB);
3316
3317 // sinkMBB:
3318 MIB = BuildMI(*sinkMBB, sinkMBB->begin(), DL,
3319 TII->get(SP::PHI), DstReg)
3320 .addReg(mainDstReg).addMBB(mainMBB)
3321 .addReg(restoreDstReg).addMBB(restoreMBB);
3322
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003323 MI.eraseFromParent();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003324 return sinkMBB;
3325}
3326
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003327//===----------------------------------------------------------------------===//
3328// Sparc Inline Assembly Support
3329//===----------------------------------------------------------------------===//
3330
3331/// getConstraintType - Given a constraint letter, return the type of
3332/// constraint it is for this target.
3333SparcTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003334SparcTargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003335 if (Constraint.size() == 1) {
3336 switch (Constraint[0]) {
James Y Knight2cc9da92016-08-12 14:48:09 +00003337 default: break;
3338 case 'r': return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003339 case 'I': // SIMM13
3340 return C_Other;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003341 }
3342 }
3343
3344 return TargetLowering::getConstraintType(Constraint);
3345}
3346
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003347TargetLowering::ConstraintWeight SparcTargetLowering::
3348getSingleConstraintMatchWeight(AsmOperandInfo &info,
3349 const char *constraint) const {
3350 ConstraintWeight weight = CW_Invalid;
3351 Value *CallOperandVal = info.CallOperandVal;
3352 // If we don't have a value, we can't do a match,
3353 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003354 if (!CallOperandVal)
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003355 return CW_Default;
3356
3357 // Look at the constraint type.
3358 switch (*constraint) {
3359 default:
3360 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3361 break;
3362 case 'I': // SIMM13
3363 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3364 if (isInt<13>(C->getSExtValue()))
3365 weight = CW_Constant;
3366 }
3367 break;
3368 }
3369 return weight;
3370}
3371
3372/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3373/// vector. If it is invalid, don't add anything to Ops.
3374void SparcTargetLowering::
3375LowerAsmOperandForConstraint(SDValue Op,
3376 std::string &Constraint,
3377 std::vector<SDValue> &Ops,
3378 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003379 SDValue Result(nullptr, 0);
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003380
3381 // Only support length 1 constraints for now.
3382 if (Constraint.length() > 1)
3383 return;
3384
3385 char ConstraintLetter = Constraint[0];
3386 switch (ConstraintLetter) {
3387 default: break;
3388 case 'I':
3389 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3390 if (isInt<13>(C->getSExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003391 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
3392 Op.getValueType());
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003393 break;
3394 }
3395 return;
3396 }
3397 }
3398
3399 if (Result.getNode()) {
3400 Ops.push_back(Result);
3401 return;
3402 }
3403 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3404}
3405
Eric Christopher11e4df72015-02-26 22:38:43 +00003406std::pair<unsigned, const TargetRegisterClass *>
3407SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003408 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003409 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003410 if (Constraint.size() == 1) {
3411 switch (Constraint[0]) {
3412 case 'r':
James Y Knight3994be82015-08-10 19:11:39 +00003413 if (VT == MVT::v2i32)
3414 return std::make_pair(0U, &SP::IntPairRegClass);
3415 else
3416 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003417 }
James Y Knight3994be82015-08-10 19:11:39 +00003418 } else if (!Constraint.empty() && Constraint.size() <= 5
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003419 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3420 // constraint = '{r<d>}'
3421 // Remove the braces from around the name.
3422 StringRef name(Constraint.data()+1, Constraint.size()-2);
3423 // Handle register aliases:
3424 // r0-r7 -> g0-g7
3425 // r8-r15 -> o0-o7
3426 // r16-r23 -> l0-l7
3427 // r24-r31 -> i0-i7
3428 uint64_t intVal = 0;
3429 if (name.substr(0, 1).equals("r")
3430 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3431 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3432 char regType = regTypes[intVal/8];
3433 char regIdx = '0' + (intVal % 8);
3434 char tmp[] = { '{', regType, regIdx, '}', 0 };
3435 std::string newConstraint = std::string(tmp);
Eric Christopher11e4df72015-02-26 22:38:43 +00003436 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3437 VT);
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003438 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003439 }
3440
Eric Christopher11e4df72015-02-26 22:38:43 +00003441 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003442}
3443
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003444bool
3445SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3446 // The Sparc target isn't yet aware of offsets.
3447 return false;
3448}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003449
3450void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3451 SmallVectorImpl<SDValue>& Results,
3452 SelectionDAG &DAG) const {
3453
3454 SDLoc dl(N);
3455
3456 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3457
3458 switch (N->getOpcode()) {
3459 default:
3460 llvm_unreachable("Do not know how to custom type legalize this operation!");
3461
3462 case ISD::FP_TO_SINT:
3463 case ISD::FP_TO_UINT:
3464 // Custom lower only if it involves f128 or i64.
3465 if (N->getOperand(0).getValueType() != MVT::f128
3466 || N->getValueType(0) != MVT::i64)
3467 return;
3468 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3469 ? RTLIB::FPTOSINT_F128_I64
3470 : RTLIB::FPTOUINT_F128_I64);
3471
3472 Results.push_back(LowerF128Op(SDValue(N, 0),
3473 DAG,
3474 getLibcallName(libCall),
3475 1));
3476 return;
3477
3478 case ISD::SINT_TO_FP:
3479 case ISD::UINT_TO_FP:
3480 // Custom lower only if it involves f128 or i64.
3481 if (N->getValueType(0) != MVT::f128
3482 || N->getOperand(0).getValueType() != MVT::i64)
3483 return;
3484
3485 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3486 ? RTLIB::SINTTOFP_I64_F128
3487 : RTLIB::UINTTOFP_I64_F128);
3488
3489 Results.push_back(LowerF128Op(SDValue(N, 0),
3490 DAG,
3491 getLibcallName(libCall),
3492 1));
3493 return;
James Y Knight3994be82015-08-10 19:11:39 +00003494 case ISD::LOAD: {
3495 LoadSDNode *Ld = cast<LoadSDNode>(N);
3496 // Custom handling only for i64: turn i64 load into a v2i32 load,
3497 // and a bitcast.
3498 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3499 return;
3500
3501 SDLoc dl(N);
3502 SDValue LoadRes = DAG.getExtLoad(
Justin Lebar9c375812016-07-15 18:27:10 +00003503 Ld->getExtensionType(), dl, MVT::v2i32, Ld->getChain(),
3504 Ld->getBasePtr(), Ld->getPointerInfo(), MVT::v2i32, Ld->getAlignment(),
3505 Ld->getMemOperand()->getFlags(), Ld->getAAInfo());
James Y Knight3994be82015-08-10 19:11:39 +00003506
3507 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3508 Results.push_back(Res);
3509 Results.push_back(LoadRes.getValue(1));
3510 return;
3511 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003512 }
3513}
Marcin Koscielnicki33571e22016-04-26 10:37:14 +00003514
3515// Override to enable LOAD_STACK_GUARD lowering on Linux.
3516bool SparcTargetLowering::useLoadStackGuardNode() const {
3517 if (!Subtarget->isTargetLinux())
3518 return TargetLowering::useLoadStackGuardNode();
3519 return true;
3520}
3521
3522// Override to disable global variable loading on Linux.
3523void SparcTargetLowering::insertSSPDeclarations(Module &M) const {
3524 if (!Subtarget->isTargetLinux())
3525 return TargetLowering::insertSSPDeclarations(M);
3526}