blob: 4f423fb370aa462d2cc0ded23e466773fb748184 [file] [log] [blame]
Chris Lattner0a1762e2008-03-17 03:21:36 +00001//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Sparc uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SparcISelLowering.h"
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +000016#include "MCTargetDesc/SparcMCExpr.h"
Dan Gohman31ae5862010-04-17 14:41:14 +000017#include "SparcMachineFunctionInfo.h"
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +000018#include "SparcRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "SparcTargetMachine.h"
Venkatraman Govindarajufd5c1f92014-01-29 04:51:35 +000020#include "SparcTargetObjectFile.h"
NAKAMURA Takumi0e57b132016-05-20 10:53:56 +000021#include "llvm/ADT/StringSwitch.h"
Chris Lattner49b269d2008-03-17 05:41:48 +000022#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000028#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/Module.h"
Torok Edwin56d06592009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000033using namespace llvm;
34
Chris Lattner49b269d2008-03-17 05:41:48 +000035//===----------------------------------------------------------------------===//
36// Calling Convention Implementation
37//===----------------------------------------------------------------------===//
38
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000039static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
40 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
41 ISD::ArgFlagsTy &ArgFlags, CCState &State)
42{
43 assert (ArgFlags.isSRet());
44
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000045 // Assign SRet argument.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000046 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
47 0,
48 LocVT, LocInfo));
49 return true;
50}
51
James Y Knight3994be82015-08-10 19:11:39 +000052static bool CC_Sparc_Assign_Split_64(unsigned &ValNo, MVT &ValVT,
53 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
54 ISD::ArgFlagsTy &ArgFlags, CCState &State)
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000055{
Craig Topper840beec2014-04-04 05:16:06 +000056 static const MCPhysReg RegList[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000057 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
58 };
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000059 // Try to get first reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000060 if (unsigned Reg = State.AllocateReg(RegList)) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000061 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
62 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000063 // Assign whole thing in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000064 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
65 State.AllocateStack(8,4),
66 LocVT, LocInfo));
67 return true;
68 }
69
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000070 // Try to get second reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000071 if (unsigned Reg = State.AllocateReg(RegList))
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000072 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
73 else
74 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
75 State.AllocateStack(4,4),
76 LocVT, LocInfo));
77 return true;
78}
79
James Y Knight3994be82015-08-10 19:11:39 +000080static bool CC_Sparc_Assign_Ret_Split_64(unsigned &ValNo, MVT &ValVT,
81 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
82 ISD::ArgFlagsTy &ArgFlags, CCState &State)
83{
84 static const MCPhysReg RegList[] = {
85 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
86 };
87
88 // Try to get first reg.
89 if (unsigned Reg = State.AllocateReg(RegList))
90 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
91 else
92 return false;
93
94 // Try to get second reg.
95 if (unsigned Reg = State.AllocateReg(RegList))
96 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
97 else
98 return false;
99
100 return true;
101}
102
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000103// Allocate a full-sized argument for the 64-bit ABI.
104static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
105 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
106 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000107 assert((LocVT == MVT::f32 || LocVT == MVT::f128
108 || LocVT.getSizeInBits() == 64) &&
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000109 "Can't handle non-64 bits locations");
110
111 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000112 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
113 unsigned alignment = (LocVT == MVT::f128) ? 16 : 8;
114 unsigned Offset = State.AllocateStack(size, alignment);
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000115 unsigned Reg = 0;
116
117 if (LocVT == MVT::i64 && Offset < 6*8)
118 // Promote integers to %i0-%i5.
119 Reg = SP::I0 + Offset/8;
120 else if (LocVT == MVT::f64 && Offset < 16*8)
121 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
122 Reg = SP::D0 + Offset/8;
123 else if (LocVT == MVT::f32 && Offset < 16*8)
124 // Promote floats to %f1, %f3, ...
125 Reg = SP::F1 + Offset/4;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000126 else if (LocVT == MVT::f128 && Offset < 16*8)
127 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
128 Reg = SP::Q0 + Offset/16;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000129
130 // Promote to register when possible, otherwise use the stack slot.
131 if (Reg) {
132 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
133 return true;
134 }
135
136 // This argument goes on the stack in an 8-byte slot.
137 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
138 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
139 if (LocVT == MVT::f32)
140 Offset += 4;
141
142 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
143 return true;
144}
145
146// Allocate a half-sized argument for the 64-bit ABI.
147//
148// This is used when passing { float, int } structs by value in registers.
149static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
150 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
151 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
152 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
153 unsigned Offset = State.AllocateStack(4, 4);
154
155 if (LocVT == MVT::f32 && Offset < 16*8) {
156 // Promote floats to %f0-%f31.
157 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
158 LocVT, LocInfo));
159 return true;
160 }
161
162 if (LocVT == MVT::i32 && Offset < 6*8) {
163 // Promote integers to %i0-%i5, using half the register.
164 unsigned Reg = SP::I0 + Offset/8;
165 LocVT = MVT::i64;
166 LocInfo = CCValAssign::AExt;
167
168 // Set the Custom bit if this i32 goes in the high bits of a register.
169 if (Offset % 8 == 0)
170 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
171 LocVT, LocInfo));
172 else
173 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
174 return true;
175 }
176
177 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
178 return true;
179}
180
Chris Lattner49b269d2008-03-17 05:41:48 +0000181#include "SparcGenCallingConv.inc"
182
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000183// The calling conventions in SparcCallingConv.td are described in terms of the
184// callee's register window. This function translates registers to the
185// corresponding caller window %o register.
186static unsigned toCallerWindow(unsigned Reg) {
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +0000187 static_assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7,
188 "Unexpected enum");
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000189 if (Reg >= SP::I0 && Reg <= SP::I7)
190 return Reg - SP::I0 + SP::O0;
191 return Reg;
192}
193
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000194SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000195SparcTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
196 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000197 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000198 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000199 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000200 if (Subtarget->is64Bit())
201 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
202 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
203}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000204
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000205SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000206SparcTargetLowering::LowerReturn_32(SDValue Chain, CallingConv::ID CallConv,
207 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000208 const SmallVectorImpl<ISD::OutputArg> &Outs,
209 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000210 const SDLoc &DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000211 MachineFunction &MF = DAG.getMachineFunction();
212
Chris Lattner49b269d2008-03-17 05:41:48 +0000213 // CCValAssign - represent the assignment of the return value to locations.
214 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000215
Chris Lattner49b269d2008-03-17 05:41:48 +0000216 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000217 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
218 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000219
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000220 // Analyze return values.
221 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000222
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000223 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000224 SmallVector<SDValue, 4> RetOps(1, Chain);
225 // Make room for the return address offset.
226 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000227
228 // Copy the result values into the output registers.
James Y Knight3994be82015-08-10 19:11:39 +0000229 for (unsigned i = 0, realRVLocIdx = 0;
230 i != RVLocs.size();
231 ++i, ++realRVLocIdx) {
Chris Lattner49b269d2008-03-17 05:41:48 +0000232 CCValAssign &VA = RVLocs[i];
233 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000234
James Y Knight3994be82015-08-10 19:11:39 +0000235 SDValue Arg = OutVals[realRVLocIdx];
236
237 if (VA.needsCustom()) {
238 assert(VA.getLocVT() == MVT::v2i32);
239 // Legalize ret v2i32 -> ret 2 x i32 (Basically: do what would
240 // happen by default if this wasn't a legal type)
241
242 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
243 Arg,
244 DAG.getConstant(0, DL, getVectorIdxTy(DAG.getDataLayout())));
245 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
246 Arg,
247 DAG.getConstant(1, DL, getVectorIdxTy(DAG.getDataLayout())));
248
249 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part0, Flag);
250 Flag = Chain.getValue(1);
251 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
252 VA = RVLocs[++i]; // skip ahead to next loc
253 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part1,
254 Flag);
255 } else
256 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000257
Chris Lattner49b269d2008-03-17 05:41:48 +0000258 // Guarantee that all emitted copies are stuck together with flags.
259 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000260 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000261 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000262
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000263 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000264 // If the function returns a struct, copy the SRetReturnReg to I0
265 if (MF.getFunction()->hasStructRetAttr()) {
266 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
267 unsigned Reg = SFI->getSRetReturnReg();
268 if (!Reg)
269 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +0000270 auto PtrVT = getPointerTy(DAG.getDataLayout());
271 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, PtrVT);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000272 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000273 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +0000274 RetOps.push_back(DAG.getRegister(SP::I0, PtrVT));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000275 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000276 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000277
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000278 RetOps[0] = Chain; // Update chain.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000279 RetOps[1] = DAG.getConstant(RetAddrOffset, DL, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000280
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000281 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000282 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000283 RetOps.push_back(Flag);
284
Craig Topper48d114b2014-04-26 18:35:24 +0000285 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000286}
287
288// Lower return values for the 64-bit ABI.
289// Return values are passed the exactly the same way as function arguments.
290SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000291SparcTargetLowering::LowerReturn_64(SDValue Chain, CallingConv::ID CallConv,
292 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000293 const SmallVectorImpl<ISD::OutputArg> &Outs,
294 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000295 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000296 // CCValAssign - represent the assignment of the return value to locations.
297 SmallVector<CCValAssign, 16> RVLocs;
298
299 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000300 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
301 *DAG.getContext());
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000302
303 // Analyze return values.
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +0000304 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000305
306 SDValue Flag;
307 SmallVector<SDValue, 4> RetOps(1, Chain);
308
309 // The second operand on the return instruction is the return address offset.
310 // The return address is always %i7+8 with the 64-bit ABI.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000311 RetOps.push_back(DAG.getConstant(8, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000312
313 // Copy the result values into the output registers.
314 for (unsigned i = 0; i != RVLocs.size(); ++i) {
315 CCValAssign &VA = RVLocs[i];
316 assert(VA.isRegLoc() && "Can only return in registers!");
317 SDValue OutVal = OutVals[i];
318
319 // Integer return values must be sign or zero extended by the callee.
320 switch (VA.getLocInfo()) {
Lang Hames06234ec2014-01-14 19:56:36 +0000321 case CCValAssign::Full: break;
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000322 case CCValAssign::SExt:
323 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
324 break;
325 case CCValAssign::ZExt:
326 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
327 break;
328 case CCValAssign::AExt:
329 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000330 break;
Lang Hames06234ec2014-01-14 19:56:36 +0000331 default:
332 llvm_unreachable("Unknown loc info!");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000333 }
334
335 // The custom bit on an i32 return value indicates that it should be passed
336 // in the high bits of the register.
337 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
338 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000339 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000340
341 // The next value may go in the low bits of the same register.
342 // Handle both at once.
343 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
344 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
345 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
346 // Skip the next value, it's already done.
347 ++i;
348 }
349 }
350
351 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
352
353 // Guarantee that all emitted copies are stuck together with flags.
354 Flag = Chain.getValue(1);
355 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
356 }
357
358 RetOps[0] = Chain; // Update chain.
359
360 // Add the flag if we have it.
361 if (Flag.getNode())
362 RetOps.push_back(Flag);
363
Craig Topper48d114b2014-04-26 18:35:24 +0000364 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Chris Lattner49b269d2008-03-17 05:41:48 +0000365}
366
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000367SDValue SparcTargetLowering::LowerFormalArguments(
368 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
369 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
370 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000371 if (Subtarget->is64Bit())
372 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
373 DL, DAG, InVals);
374 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
375 DL, DAG, InVals);
376}
377
378/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000379/// passed in either one or two GPRs, including FP values. TODO: we should
380/// pass FP values in FP registers for fastcc functions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000381SDValue SparcTargetLowering::LowerFormalArguments_32(
382 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
383 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
384 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000385 MachineFunction &MF = DAG.getMachineFunction();
386 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000387 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000388
389 // Assign locations to all of the incoming arguments.
390 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000391 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
392 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000393 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000394
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000395 const unsigned StackOffset = 92;
James Y Knight33beb242015-12-15 19:23:12 +0000396 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000397
Reid Kleckner79418562014-05-09 22:32:13 +0000398 unsigned InIdx = 0;
399 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000400 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000401
Reid Kleckner79418562014-05-09 22:32:13 +0000402 if (Ins[InIdx].Flags.isSRet()) {
403 if (InIdx != 0)
404 report_fatal_error("sparc only supports sret on the first parameter");
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000405 // Get SRet from [%fp+64].
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000406 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
407 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
408 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
409 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000410 false, false, false, 0);
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()) {
428 int FrameIdx = MF.getFrameInfo()->
429 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000430 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000431 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
432 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000433 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000434 } else {
435 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000436 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000437 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000438 }
James Y Knight33beb242015-12-15 19:23:12 +0000439
440 if (IsLittleEndian)
441 std::swap(LoVal, HiVal);
442
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000443 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000444 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000445 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000446 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000447 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000448 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000449 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
450 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
451 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
452 if (VA.getLocVT() == MVT::f32)
453 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
454 else if (VA.getLocVT() != MVT::i32) {
455 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
456 DAG.getValueType(VA.getLocVT()));
457 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
458 }
459 InVals.push_back(Arg);
460 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000461 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000462
463 assert(VA.isMemLoc());
464
465 unsigned Offset = VA.getLocMemOffset()+StackOffset;
Mehdi Amini44ede332015-07-09 02:09:04 +0000466 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000467
468 if (VA.needsCustom()) {
Hans Wennborg1f094852016-04-11 20:35:41 +0000469 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000470 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000471 if (Offset % 8 == 0) {
472 int FI = MF.getFrameInfo()->CreateFixedObject(8,
473 Offset,
474 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000475 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000476 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
477 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000478 false,false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000479 InVals.push_back(Load);
480 continue;
481 }
482
483 int FI = MF.getFrameInfo()->CreateFixedObject(4,
484 Offset,
485 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000486 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000487 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
488 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000489 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000490 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
491 Offset+4,
492 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000493 SDValue FIPtr2 = DAG.getFrameIndex(FI2, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000494
495 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
496 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000497 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000498
James Y Knight33beb242015-12-15 19:23:12 +0000499 if (IsLittleEndian)
500 std::swap(LoVal, HiVal);
501
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000502 SDValue WholeValue =
503 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000504 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000505 InVals.push_back(WholeValue);
506 continue;
507 }
508
509 int FI = MF.getFrameInfo()->CreateFixedObject(4,
510 Offset,
511 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000512 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000513 SDValue Load ;
514 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
515 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
516 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000517 false, false, false, 0);
James Y Knight33beb242015-12-15 19:23:12 +0000518 } else if (VA.getValVT() == MVT::f128) {
519 report_fatal_error("SPARCv8 does not handle f128 in calls; "
520 "pass indirectly");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000521 } else {
James Y Knight33beb242015-12-15 19:23:12 +0000522 // We shouldn't see any other value types here.
James Y Knight99fcb722015-12-15 23:07:16 +0000523 llvm_unreachable("Unexpected ValVT encountered in frame lowering.");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000524 }
525 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000526 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000527
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000528 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000529 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000530 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
531 unsigned Reg = SFI->getSRetReturnReg();
532 if (!Reg) {
533 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
534 SFI->setSRetReturnReg(Reg);
535 }
536 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
537 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
538 }
539
Chris Lattner49b269d2008-03-17 05:41:48 +0000540 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000541 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +0000542 static const MCPhysReg ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000543 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
544 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000545 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs);
Craig Topper840beec2014-04-04 05:16:06 +0000546 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000547 unsigned ArgOffset = CCInfo.getNextStackOffset();
548 if (NumAllocated == 6)
549 ArgOffset += StackOffset;
550 else {
551 assert(!ArgOffset);
552 ArgOffset = 68+4*NumAllocated;
553 }
554
Chris Lattner49b269d2008-03-17 05:41:48 +0000555 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000556 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000557
Eli Friedmanbe853b72009-07-19 19:53:46 +0000558 std::vector<SDValue> OutChains;
559
Chris Lattner49b269d2008-03-17 05:41:48 +0000560 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
561 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
562 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000563 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000564
David Greene1fbe0542009-11-12 20:49:22 +0000565 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Cheng0664a672010-07-03 00:40:23 +0000566 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000567 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000568
Chris Lattner676c61d2010-09-21 18:41:36 +0000569 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
570 MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000571 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000572 ArgOffset += 4;
573 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000574
575 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000576 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +0000577 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Eli Friedmanbe853b72009-07-19 19:53:46 +0000578 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000579 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000580
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000581 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000582}
583
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000584// Lower formal arguments for the 64 bit ABI.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000585SDValue SparcTargetLowering::LowerFormalArguments_64(
586 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
587 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
588 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000589 MachineFunction &MF = DAG.getMachineFunction();
590
591 // Analyze arguments according to CC_Sparc64.
592 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000593 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
594 *DAG.getContext());
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000595 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
596
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000597 // The argument array begins at %fp+BIAS+128, after the register save area.
598 const unsigned ArgArea = 128;
599
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000600 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
601 CCValAssign &VA = ArgLocs[i];
602 if (VA.isRegLoc()) {
603 // This argument is passed in a register.
604 // All integer register arguments are promoted by the caller to i64.
605
606 // Create a virtual register for the promoted live-in value.
607 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
608 getRegClassFor(VA.getLocVT()));
609 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
610
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000611 // Get the high bits for i32 struct elements.
612 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
613 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000614 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000615
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000616 // The caller promoted the argument, so insert an Assert?ext SDNode so we
617 // won't promote the value again in this function.
618 switch (VA.getLocInfo()) {
619 case CCValAssign::SExt:
620 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
621 DAG.getValueType(VA.getValVT()));
622 break;
623 case CCValAssign::ZExt:
624 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
625 DAG.getValueType(VA.getValVT()));
626 break;
627 default:
628 break;
629 }
630
631 // Truncate the register down to the argument type.
632 if (VA.isExtInLoc())
633 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
634
635 InVals.push_back(Arg);
636 continue;
637 }
638
639 // The registers are exhausted. This argument was passed on the stack.
640 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000641 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
642 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000643 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000644 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
645 // Adjust offset for extended arguments, SPARC is big-endian.
646 // The caller will have written the full slot with extended bytes, but we
647 // prefer our own extending loads.
648 if (VA.isExtInLoc())
649 Offset += 8 - ValSize;
650 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000651 InVals.push_back(DAG.getLoad(
652 VA.getValVT(), DL, Chain,
653 DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
Alex Lorenze40c8a22015-08-11 23:09:45 +0000654 MachinePointerInfo::getFixedStack(MF, FI), false, false, false, 0));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000655 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000656
657 if (!IsVarArg)
658 return Chain;
659
660 // This function takes variable arguments, some of which may have been passed
661 // in registers %i0-%i5. Variable floating point arguments are never passed
662 // in floating point registers. They go on %i0-%i5 or on the stack like
663 // integer arguments.
664 //
665 // The va_start intrinsic needs to know the offset to the first variable
666 // argument.
667 unsigned ArgOffset = CCInfo.getNextStackOffset();
668 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
669 // Skip the 128 bytes of register save area.
670 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
671 Subtarget->getStackPointerBias());
672
673 // Save the variable arguments that were passed in registers.
674 // The caller is required to reserve stack space for 6 arguments regardless
675 // of how many arguments were actually passed.
676 SmallVector<SDValue, 8> OutChains;
677 for (; ArgOffset < 6*8; ArgOffset += 8) {
678 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
679 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
680 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000681 auto PtrVT = getPointerTy(MF.getDataLayout());
Alex Lorenze40c8a22015-08-11 23:09:45 +0000682 OutChains.push_back(DAG.getStore(
683 Chain, DL, VArg, DAG.getFrameIndex(FI, PtrVT),
684 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000685 }
686
687 if (!OutChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000688 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000689
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000690 return Chain;
691}
692
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000693SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000694SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000695 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000696 if (Subtarget->is64Bit())
697 return LowerCall_64(CLI, InVals);
698 return LowerCall_32(CLI, InVals);
699}
700
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000701static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
702 ImmutableCallSite *CS) {
703 if (CS)
704 return CS->hasFnAttr(Attribute::ReturnsTwice);
705
Craig Topper062a2ba2014-04-25 05:30:21 +0000706 const Function *CalleeFn = nullptr;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000707 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
708 CalleeFn = dyn_cast<Function>(G->getGlobal());
709 } else if (ExternalSymbolSDNode *E =
710 dyn_cast<ExternalSymbolSDNode>(Callee)) {
711 const Function *Fn = DAG.getMachineFunction().getFunction();
712 const Module *M = Fn->getParent();
713 const char *CalleeName = E->getSymbol();
714 CalleeFn = M->getFunction(CalleeName);
715 }
716
717 if (!CalleeFn)
718 return false;
719 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
720}
721
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000722// Lower a call for the 32-bit ABI.
723SDValue
724SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
725 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000726 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000727 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000728 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
729 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
730 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000731 SDValue Chain = CLI.Chain;
732 SDValue Callee = CLI.Callee;
733 bool &isTailCall = CLI.IsTailCall;
734 CallingConv::ID CallConv = CLI.CallConv;
735 bool isVarArg = CLI.IsVarArg;
736
Evan Cheng67a69dd2010-01-27 00:07:07 +0000737 // Sparc target does not yet support tail call optimization.
738 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000739
Chris Lattner7d4152b2008-03-17 06:58:37 +0000740 // Analyze operands of the call, assigning locations to each operand.
741 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000742 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
743 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000744 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000745
Chris Lattner7d4152b2008-03-17 06:58:37 +0000746 // Get the size of the outgoing arguments stack space requirement.
747 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000748
Chris Lattner49b269d2008-03-17 05:41:48 +0000749 // Keep stack frames 8-byte aligned.
750 ArgsSize = (ArgsSize+7) & ~7;
751
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000752 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
753
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000754 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000755 SmallVector<SDValue, 8> ByValArgs;
756 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
757 ISD::ArgFlagsTy Flags = Outs[i].Flags;
758 if (!Flags.isByVal())
759 continue;
760
761 SDValue Arg = OutVals[i];
762 unsigned Size = Flags.getByValSize();
763 unsigned Align = Flags.getByValAlign();
764
Chris Dewhurst53bde952016-06-01 08:48:56 +0000765 if (Size > 0U) {
766 int FI = MFI->CreateStackObject(Size, Align, false);
767 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
768 SDValue SizeNode = DAG.getConstant(Size, dl, MVT::i32);
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000769
Chris Dewhurst53bde952016-06-01 08:48:56 +0000770 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
771 false, // isVolatile,
772 (Size <= 32), // AlwaysInline if size <= 32,
773 false, // isTailCall
774 MachinePointerInfo(), MachinePointerInfo());
775 ByValArgs.push_back(FIPtr);
776 }
777 else {
778 SDValue nullVal;
779 ByValArgs.push_back(nullVal);
780 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000781 }
782
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000783 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000784 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000785
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000786 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
787 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000788
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000789 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000790 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000791 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000792 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000793 i != e;
794 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000795 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000796 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000797
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000798 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
799
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000800 // Use local copy if it is a byval arg.
Chris Dewhurst53bde952016-06-01 08:48:56 +0000801 if (Flags.isByVal()) {
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000802 Arg = ByValArgs[byvalArgIdx++];
Chris Dewhurst53bde952016-06-01 08:48:56 +0000803 if (!Arg) {
804 continue;
805 }
806 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000807
Chris Lattner7d4152b2008-03-17 06:58:37 +0000808 // Promote the value if needed.
809 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000810 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000811 case CCValAssign::Full: break;
812 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000813 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000814 break;
815 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000816 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000817 break;
818 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000819 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
820 break;
821 case CCValAssign::BCvt:
822 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000823 break;
824 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000825
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000826 if (Flags.isSRet()) {
827 assert(VA.needsCustom());
828 // store SRet argument in %sp+64
829 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000830 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000831 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
832 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
833 MachinePointerInfo(),
834 false, false, 0));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000835 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000836 continue;
837 }
838
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000839 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000840 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000841
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000842 if (VA.isMemLoc()) {
843 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000844 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000845 if (Offset % 8 == 0) {
846 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000847 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000848 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
849 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
850 MachinePointerInfo(),
851 false, false, 0));
852 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000853 }
854 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000855
James Y Knight3994be82015-08-10 19:11:39 +0000856 if (VA.getLocVT() == MVT::f64) {
857 // Move from the float value from float registers into the
858 // integer registers.
859
James Y Knight692e0372015-10-09 21:36:19 +0000860 // TODO: The f64 -> v2i32 conversion is super-inefficient for
861 // constants: it sticks them in the constant pool, then loads
862 // to a fp register, then stores to temp memory, then loads to
863 // integer registers.
James Y Knight3994be82015-08-10 19:11:39 +0000864 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
865 }
866
867 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
868 Arg,
869 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
870 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
871 Arg,
872 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000873
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000874 if (VA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000875 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000876 assert(i+1 != e);
877 CCValAssign &NextVA = ArgLocs[++i];
878 if (NextVA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000879 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000880 } else {
James Y Knight3994be82015-08-10 19:11:39 +0000881 // Store the second part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000882 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
883 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000884 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000885 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000886 MemOpChains.push_back(DAG.getStore(Chain, dl, Part1, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000887 MachinePointerInfo(),
888 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000889 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000890 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000891 unsigned Offset = VA.getLocMemOffset() + StackOffset;
James Y Knight3994be82015-08-10 19:11:39 +0000892 // Store the first part.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000893 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000894 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000895 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000896 MemOpChains.push_back(DAG.getStore(Chain, dl, Part0, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000897 MachinePointerInfo(),
898 false, false, 0));
James Y Knight3994be82015-08-10 19:11:39 +0000899 // Store the second part.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000900 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000901 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
James Y Knight3994be82015-08-10 19:11:39 +0000902 MemOpChains.push_back(DAG.getStore(Chain, dl, Part1, PtrOff,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000903 MachinePointerInfo(),
904 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000905 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000906 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000907 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000908
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000909 // Arguments that can be passed on register must be kept at
910 // RegsToPass vector
911 if (VA.isRegLoc()) {
912 if (VA.getLocVT() != MVT::f32) {
913 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
914 continue;
915 }
916 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
917 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
918 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000919 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000920
921 assert(VA.isMemLoc());
922
923 // Create a store off the stack pointer for this argument.
924 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000925 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
926 dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000927 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
928 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
929 MachinePointerInfo(),
930 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000931 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000932
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000933
Chris Lattner49b269d2008-03-17 05:41:48 +0000934 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000935 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000936 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000937
938 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000939 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000940 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000941 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000942 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000943 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000944 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000945 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000946 InFlag = Chain.getValue(1);
947 }
948
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000949 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000950 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000951
Chris Lattner49b269d2008-03-17 05:41:48 +0000952 // If the callee is a GlobalAddress node (quite common, every direct call is)
953 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000954 // Likewise ExternalSymbol -> TargetExternalSymbol.
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +0000955 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Chris Lattner49b269d2008-03-17 05:41:48 +0000956 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000957 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000958 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000959 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000960
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000961 // Returns a chain & a flag for retval copy to use
962 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
963 SmallVector<SDValue, 8> Ops;
964 Ops.push_back(Chain);
965 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000966 if (hasStructRetAttr)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000967 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000968 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
969 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
970 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000971
972 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000973 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000974 const uint32_t *Mask =
975 ((hasReturnsTwice)
976 ? TRI->getRTCallPreservedMask(CallConv)
977 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000978 assert(Mask && "Missing call preserved mask for calling convention");
979 Ops.push_back(DAG.getRegisterMask(Mask));
980
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000981 if (InFlag.getNode())
982 Ops.push_back(InFlag);
983
Craig Topper48d114b2014-04-26 18:35:24 +0000984 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
Chris Lattner49b269d2008-03-17 05:41:48 +0000985 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000986
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000987 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
988 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000989 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000990
Chris Lattnerdb26db22008-03-17 06:01:07 +0000991 // Assign locations to each value returned by this call.
992 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000993 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
994 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000995
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000996 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000997
Chris Lattnerdb26db22008-03-17 06:01:07 +0000998 // Copy all of the result registers out of their specified physreg.
999 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Nirav Dave29938542016-02-26 18:55:22 +00001000 if (RVLocs[i].getLocVT() == MVT::v2i32) {
1001 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
1002 SDValue Lo = DAG.getCopyFromReg(
1003 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InFlag);
1004 Chain = Lo.getValue(1);
1005 InFlag = Lo.getValue(2);
1006 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
1007 DAG.getConstant(0, dl, MVT::i32));
1008 SDValue Hi = DAG.getCopyFromReg(
1009 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InFlag);
1010 Chain = Hi.getValue(1);
1011 InFlag = Hi.getValue(2);
1012 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
1013 DAG.getConstant(1, dl, MVT::i32));
1014 InVals.push_back(Vec);
1015 } else {
1016 Chain =
1017 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1018 RVLocs[i].getValVT(), InFlag)
1019 .getValue(1);
1020 InFlag = Chain.getValue(2);
1021 InVals.push_back(Chain.getValue(0));
1022 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001023 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001024
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001025 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +00001026}
1027
Chris Dewhurstad741172016-05-20 10:21:01 +00001028// FIXME? Maybe this could be a TableGen attribute on some registers and
1029// this table could be generated automatically from RegInfo.
1030unsigned SparcTargetLowering::getRegisterByName(const char* RegName, EVT VT,
1031 SelectionDAG &DAG) const {
1032 unsigned Reg = StringSwitch<unsigned>(RegName)
1033 .Case("i0", SP::I0).Case("i1", SP::I1).Case("i2", SP::I2).Case("i3", SP::I3)
1034 .Case("i4", SP::I4).Case("i5", SP::I5).Case("i6", SP::I6).Case("i7", SP::I7)
1035 .Case("o0", SP::O0).Case("o1", SP::O1).Case("o2", SP::O2).Case("o3", SP::O3)
1036 .Case("o4", SP::O4).Case("o5", SP::O5).Case("o6", SP::O6).Case("o7", SP::O7)
1037 .Case("l0", SP::L0).Case("l1", SP::L1).Case("l2", SP::L2).Case("l3", SP::L3)
1038 .Case("l4", SP::L4).Case("l5", SP::L5).Case("l6", SP::L6).Case("l7", SP::L7)
1039 .Case("g0", SP::G0).Case("g1", SP::G1).Case("g2", SP::G2).Case("g3", SP::G3)
1040 .Case("g4", SP::G4).Case("g5", SP::G5).Case("g6", SP::G6).Case("g7", SP::G7)
1041 .Default(0);
1042
1043 if (Reg)
1044 return Reg;
1045
1046 report_fatal_error("Invalid register name global variable");
1047}
1048
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001049// This functions returns true if CalleeName is a ABI function that returns
1050// a long double (fp128).
1051static bool isFP128ABICall(const char *CalleeName)
1052{
1053 static const char *const ABICalls[] =
1054 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
1055 "_Q_sqrt", "_Q_neg",
1056 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001057 "_Q_lltoq", "_Q_ulltoq",
Craig Topper062a2ba2014-04-25 05:30:21 +00001058 nullptr
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001059 };
Craig Topper062a2ba2014-04-25 05:30:21 +00001060 for (const char * const *I = ABICalls; *I != nullptr; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001061 if (strcmp(CalleeName, *I) == 0)
1062 return true;
1063 return false;
1064}
1065
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001066unsigned
1067SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
1068{
Craig Topper062a2ba2014-04-25 05:30:21 +00001069 const Function *CalleeFn = nullptr;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001070 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1071 CalleeFn = dyn_cast<Function>(G->getGlobal());
1072 } else if (ExternalSymbolSDNode *E =
1073 dyn_cast<ExternalSymbolSDNode>(Callee)) {
1074 const Function *Fn = DAG.getMachineFunction().getFunction();
1075 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001076 const char *CalleeName = E->getSymbol();
1077 CalleeFn = M->getFunction(CalleeName);
1078 if (!CalleeFn && isFP128ABICall(CalleeName))
1079 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001080 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001081
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001082 if (!CalleeFn)
1083 return 0;
1084
Joerg Sonnenberger72128092015-10-21 20:05:01 +00001085 // It would be nice to check for the sret attribute on CalleeFn here,
1086 // but since it is not part of the function type, any check will misfire.
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001087
Chris Lattner229907c2011-07-18 04:54:35 +00001088 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
1089 Type *ElementTy = Ty->getElementType();
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001090 return DAG.getDataLayout().getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001091}
Chris Lattner49b269d2008-03-17 05:41:48 +00001092
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001093
1094// Fixup floating point arguments in the ... part of a varargs call.
1095//
1096// The SPARC v9 ABI requires that floating point arguments are treated the same
1097// as integers when calling a varargs function. This does not apply to the
1098// fixed arguments that are part of the function's prototype.
1099//
1100// This function post-processes a CCValAssign array created by
1101// AnalyzeCallOperands().
1102static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1103 ArrayRef<ISD::OutputArg> Outs) {
1104 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1105 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001106 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001107 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1108 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001109 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001110 continue;
1111 // The fixed arguments to a varargs function still go in FP registers.
1112 if (Outs[VA.getValNo()].IsFixed)
1113 continue;
1114
1115 // This floating point argument should be reassigned.
1116 CCValAssign NewVA;
1117
1118 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001119 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1120 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1121 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001122 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1123
1124 if (Offset < 6*8) {
1125 // This argument should go in %i0-%i5.
1126 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001127 if (ValTy == MVT::f64)
1128 // Full register, just bitconvert into i64.
1129 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1130 IReg, MVT::i64, CCValAssign::BCvt);
1131 else {
1132 assert(ValTy == MVT::f128 && "Unexpected type!");
1133 // Full register, just bitconvert into i128 -- We will lower this into
1134 // two i64s in LowerCall_64.
1135 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1136 IReg, MVT::i128, CCValAssign::BCvt);
1137 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001138 } else {
1139 // This needs to go to memory, we're out of integer registers.
1140 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1141 Offset, VA.getLocVT(), VA.getLocInfo());
1142 }
1143 ArgLocs[i] = NewVA;
1144 }
1145}
1146
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001147// Lower a call for the 64-bit ABI.
1148SDValue
1149SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1150 SmallVectorImpl<SDValue> &InVals) const {
1151 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001152 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001153 SDValue Chain = CLI.Chain;
Mehdi Amini44ede332015-07-09 02:09:04 +00001154 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001155
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001156 // Sparc target does not yet support tail call optimization.
1157 CLI.IsTailCall = false;
1158
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001159 // Analyze operands of the call, assigning locations to each operand.
1160 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001161 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1162 *DAG.getContext());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001163 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1164
1165 // Get the size of the outgoing arguments stack space requirement.
1166 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001167 // Called functions expect 6 argument words to exist in the stack frame, used
1168 // or not.
1169 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001170
1171 // Keep stack frames 16-byte aligned.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001172 ArgsSize = alignTo(ArgsSize, 16);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001173
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001174 // Varargs calls require special treatment.
1175 if (CLI.IsVarArg)
1176 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1177
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001178 // Adjust the stack pointer to make room for the arguments.
1179 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1180 // with more than 6 arguments.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001181 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001182 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001183
1184 // Collect the set of registers to pass to the function and their values.
1185 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1186 // instruction.
1187 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1188
1189 // Collect chains from all the memory opeations that copy arguments to the
1190 // stack. They must follow the stack pointer adjustment above and precede the
1191 // call instruction itself.
1192 SmallVector<SDValue, 8> MemOpChains;
1193
1194 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1195 const CCValAssign &VA = ArgLocs[i];
1196 SDValue Arg = CLI.OutVals[i];
1197
1198 // Promote the value if needed.
1199 switch (VA.getLocInfo()) {
1200 default:
1201 llvm_unreachable("Unknown location info!");
1202 case CCValAssign::Full:
1203 break;
1204 case CCValAssign::SExt:
1205 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1206 break;
1207 case CCValAssign::ZExt:
1208 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1209 break;
1210 case CCValAssign::AExt:
1211 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1212 break;
1213 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001214 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1215 // SPARC does not support i128 natively. Lower it into two i64, see below.
1216 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1217 || VA.getLocVT() != MVT::i128)
1218 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001219 break;
1220 }
1221
1222 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001223 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1224 && VA.getLocVT() == MVT::i128) {
1225 // Store and reload into the interger register reg and reg+1.
1226 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1227 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
Mehdi Amini44ede332015-07-09 02:09:04 +00001228 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001229 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001230 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001231 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001232 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001233
1234 // Store to %sp+BIAS+128+Offset
1235 SDValue Store = DAG.getStore(Chain, DL, Arg, HiPtrOff,
1236 MachinePointerInfo(),
1237 false, false, 0);
1238 // Load into Reg and Reg+1
1239 SDValue Hi64 = DAG.getLoad(MVT::i64, DL, Store, HiPtrOff,
1240 MachinePointerInfo(),
1241 false, false, false, 0);
1242 SDValue Lo64 = DAG.getLoad(MVT::i64, DL, Store, LoPtrOff,
1243 MachinePointerInfo(),
1244 false, false, false, 0);
1245 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1246 Hi64));
1247 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1248 Lo64));
1249 continue;
1250 }
1251
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001252 // The custom bit on an i32 return value indicates that it should be
1253 // passed in the high bits of the register.
1254 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1255 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001256 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001257
1258 // The next value may go in the low bits of the same register.
1259 // Handle both at once.
1260 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1261 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1262 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1263 CLI.OutVals[i+1]);
1264 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1265 // Skip the next value, it's already done.
1266 ++i;
1267 }
1268 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001269 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001270 continue;
1271 }
1272
1273 assert(VA.isMemLoc());
1274
1275 // Create a store off the stack pointer for this argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00001276 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001277 // The argument area starts at %fp+BIAS+128 in the callee frame,
1278 // %sp+BIAS+128 in ours.
1279 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1280 Subtarget->getStackPointerBias() +
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001281 128, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001282 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001283 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1284 MachinePointerInfo(),
1285 false, false, 0));
1286 }
1287
1288 // Emit all stores, make sure they occur before the call.
1289 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001290 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001291
1292 // Build a sequence of CopyToReg nodes glued together with token chain and
1293 // glue operands which copy the outgoing args into registers. The InGlue is
1294 // necessary since all emitted instructions must be stuck together in order
1295 // to pass the live physical registers.
1296 SDValue InGlue;
1297 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1298 Chain = DAG.getCopyToReg(Chain, DL,
1299 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1300 InGlue = Chain.getValue(1);
1301 }
1302
1303 // If the callee is a GlobalAddress node (quite common, every direct call is)
1304 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1305 // Likewise ExternalSymbol -> TargetExternalSymbol.
1306 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001307 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +00001308 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001309 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001310 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001311 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001312 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001313
1314 // Build the operands for the call instruction itself.
1315 SmallVector<SDValue, 8> Ops;
1316 Ops.push_back(Chain);
1317 Ops.push_back(Callee);
1318 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1319 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1320 RegsToPass[i].second.getValueType()));
1321
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001322 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +00001323 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001324 const uint32_t *Mask =
1325 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
Eric Christopher9deb75d2015-03-11 22:42:13 +00001326 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1327 CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001328 assert(Mask && "Missing call preserved mask for calling convention");
1329 Ops.push_back(DAG.getRegisterMask(Mask));
1330
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001331 // Make sure the CopyToReg nodes are glued to the call instruction which
1332 // consumes the registers.
1333 if (InGlue.getNode())
1334 Ops.push_back(InGlue);
1335
1336 // Now the call itself.
1337 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001338 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001339 InGlue = Chain.getValue(1);
1340
1341 // Revert the stack pointer immediately after the call.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001342 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
1343 DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001344 InGlue = Chain.getValue(1);
1345
1346 // Now extract the return values. This is more or less the same as
1347 // LowerFormalArguments_64.
1348
1349 // Assign locations to each value returned by this call.
1350 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001351 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1352 *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001353
1354 // Set inreg flag manually for codegen generated library calls that
1355 // return float.
Craig Topper062a2ba2014-04-25 05:30:21 +00001356 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == nullptr)
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001357 CLI.Ins[0].Flags.setInReg();
1358
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001359 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001360
1361 // Copy all of the result registers out of their specified physreg.
1362 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1363 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001364 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001365
1366 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1367 // reside in the same register in the high and low bits. Reuse the
1368 // CopyFromReg previous node to avoid duplicate copies.
1369 SDValue RV;
1370 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1371 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1372 RV = Chain.getValue(0);
1373
1374 // But usually we'll create a new CopyFromReg for a different register.
1375 if (!RV.getNode()) {
1376 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1377 Chain = RV.getValue(1);
1378 InGlue = Chain.getValue(2);
1379 }
1380
1381 // Get the high bits for i32 struct elements.
1382 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1383 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001384 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001385
1386 // The callee promoted the return value, so insert an Assert?ext SDNode so
1387 // we won't promote the value again in this function.
1388 switch (VA.getLocInfo()) {
1389 case CCValAssign::SExt:
1390 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1391 DAG.getValueType(VA.getValVT()));
1392 break;
1393 case CCValAssign::ZExt:
1394 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1395 DAG.getValueType(VA.getValVT()));
1396 break;
1397 default:
1398 break;
1399 }
1400
1401 // Truncate the register down to the return value type.
1402 if (VA.isExtInLoc())
1403 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1404
1405 InVals.push_back(RV);
1406 }
1407
1408 return Chain;
1409}
1410
Chris Lattner0a1762e2008-03-17 03:21:36 +00001411//===----------------------------------------------------------------------===//
1412// TargetLowering Implementation
1413//===----------------------------------------------------------------------===//
1414
James Y Knight7306cd42016-03-29 19:09:54 +00001415TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1416 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1417 AI->getType()->getPrimitiveSizeInBits() == 32)
1418 return AtomicExpansionKind::None; // Uses xchg instruction
1419
1420 return AtomicExpansionKind::CmpXChg;
1421}
1422
Chris Lattner0a1762e2008-03-17 03:21:36 +00001423/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1424/// condition.
1425static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1426 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001427 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001428 case ISD::SETEQ: return SPCC::ICC_E;
1429 case ISD::SETNE: return SPCC::ICC_NE;
1430 case ISD::SETLT: return SPCC::ICC_L;
1431 case ISD::SETGT: return SPCC::ICC_G;
1432 case ISD::SETLE: return SPCC::ICC_LE;
1433 case ISD::SETGE: return SPCC::ICC_GE;
1434 case ISD::SETULT: return SPCC::ICC_CS;
1435 case ISD::SETULE: return SPCC::ICC_LEU;
1436 case ISD::SETUGT: return SPCC::ICC_GU;
1437 case ISD::SETUGE: return SPCC::ICC_CC;
1438 }
1439}
1440
1441/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1442/// FCC condition.
1443static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1444 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001445 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001446 case ISD::SETEQ:
1447 case ISD::SETOEQ: return SPCC::FCC_E;
1448 case ISD::SETNE:
1449 case ISD::SETUNE: return SPCC::FCC_NE;
1450 case ISD::SETLT:
1451 case ISD::SETOLT: return SPCC::FCC_L;
1452 case ISD::SETGT:
1453 case ISD::SETOGT: return SPCC::FCC_G;
1454 case ISD::SETLE:
1455 case ISD::SETOLE: return SPCC::FCC_LE;
1456 case ISD::SETGE:
1457 case ISD::SETOGE: return SPCC::FCC_GE;
1458 case ISD::SETULT: return SPCC::FCC_UL;
1459 case ISD::SETULE: return SPCC::FCC_ULE;
1460 case ISD::SETUGT: return SPCC::FCC_UG;
1461 case ISD::SETUGE: return SPCC::FCC_UGE;
1462 case ISD::SETUO: return SPCC::FCC_U;
1463 case ISD::SETO: return SPCC::FCC_O;
1464 case ISD::SETONE: return SPCC::FCC_LG;
1465 case ISD::SETUEQ: return SPCC::FCC_UE;
1466 }
1467}
1468
James Y Knightef31eaf2016-05-03 14:57:18 +00001469SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
Eric Christopherf5e94062015-01-30 23:46:43 +00001470 const SparcSubtarget &STI)
1471 : TargetLowering(TM), Subtarget(&STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +00001472 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00001473
James Y Knightd966fb62015-08-19 14:47:04 +00001474 // Instructions which use registers as conditionals examine all the
1475 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1476 // matters much whether it's ZeroOrOneBooleanContent, or
1477 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1478 // former.
1479 setBooleanContents(ZeroOrOneBooleanContent);
1480 setBooleanVectorContents(ZeroOrOneBooleanContent);
1481
Chris Lattner0a1762e2008-03-17 03:21:36 +00001482 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001483 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
Chris Dewhurst68388a02016-05-18 09:14:13 +00001484 if (!Subtarget->useSoftFloat()) {
1485 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1486 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1487 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1488 }
James Y Knight3994be82015-08-10 19:11:39 +00001489 if (Subtarget->is64Bit()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001490 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001491 } else {
1492 // On 32bit sparc, we define a double-register 32bit register
1493 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1494 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1495
1496 // ...but almost all operations must be expanded, so set that as
1497 // the default.
1498 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1499 setOperationAction(Op, MVT::v2i32, Expand);
1500 }
1501 // Truncating/extending stores/loads are also not supported.
1502 for (MVT VT : MVT::integer_vector_valuetypes()) {
1503 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1504 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1505 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1506
1507 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1508 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1509 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1510
1511 setTruncStoreAction(VT, MVT::v2i32, Expand);
1512 setTruncStoreAction(MVT::v2i32, VT, Expand);
1513 }
1514 // However, load and store *are* legal.
1515 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1516 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1517 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1518 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1519
1520 // And we need to promote i64 loads/stores into vector load/store
1521 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1522 setOperationAction(ISD::STORE, MVT::i64, Custom);
1523
1524 // Sadly, this doesn't work:
1525 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1526 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1527 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001528
1529 // Turn FP extload into load/fextend
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001530 for (MVT VT : MVT::fp_valuetypes()) {
1531 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1532 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1533 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001534
Chris Lattner0a1762e2008-03-17 03:21:36 +00001535 // Sparc doesn't have i1 sign extending load
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001536 for (MVT VT : MVT::integer_valuetypes())
1537 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001538
Chris Lattner0a1762e2008-03-17 03:21:36 +00001539 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001540 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001541 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1542 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001543
1544 // Custom legalize GlobalAddress nodes into LO/HI parts.
Mehdi Amini26d48132015-07-24 16:04:22 +00001545 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1546 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1547 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1548 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001549
Chris Lattner0a1762e2008-03-17 03:21:36 +00001550 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001551 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1552 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1553 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001554
1555 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001556 setOperationAction(ISD::UREM, MVT::i32, Expand);
1557 setOperationAction(ISD::SREM, MVT::i32, Expand);
1558 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1559 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001560
Roman Divacky2262cfa2013-10-31 19:22:33 +00001561 // ... nor does SparcV9.
1562 if (Subtarget->is64Bit()) {
1563 setOperationAction(ISD::UREM, MVT::i64, Expand);
1564 setOperationAction(ISD::SREM, MVT::i64, Expand);
1565 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1566 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1567 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001568
1569 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001570 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1571 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001572 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1573 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001574
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001575 // Custom Expand fp<->uint
1576 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1577 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001578 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1579 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001580
Wesley Peck527da1b2010-11-23 03:31:01 +00001581 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1582 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001583
Chris Lattner0a1762e2008-03-17 03:21:36 +00001584 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001585 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1586 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1587 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001588 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1589
Owen Anderson9f944592009-08-11 20:47:22 +00001590 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1591 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1592 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001593 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001594
Chris Lattner0a1762e2008-03-17 03:21:36 +00001595 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001596 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1597 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1598 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1599 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1600 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1601 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001602 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001603
Owen Anderson9f944592009-08-11 20:47:22 +00001604 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1605 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1606 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001607 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001608
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001609 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1610 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1611
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001612 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001613 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1614 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1615 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1616 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001617 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1618 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001619 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1620 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001621 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001622 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001623
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001624 setOperationAction(ISD::CTPOP, MVT::i64,
1625 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001626 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001627 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001628 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001629 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1630 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001631 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001632 }
1633
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001634 // ATOMICs.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001635 // Atomics are supported on SparcV9. 32-bit atomics are also
1636 // supported by some Leon SparcV8 variants. Otherwise, atomics
1637 // are unsupported.
Chris Dewhurst3202f062016-07-08 15:33:56 +00001638 if (Subtarget->isV9() || Subtarget->hasLeonCasa())
Chris Dewhurstd534d3a2016-06-27 22:11:09 +00001639 setMaxAtomicSizeInBitsSupported(64);
James Y Knight19f6cce2016-04-12 20:18:48 +00001640 else
1641 setMaxAtomicSizeInBitsSupported(0);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001642
James Y Knight148a6462016-06-17 18:11:48 +00001643 setMinCmpXchgSizeInBits(32);
1644
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001645 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001646
1647 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1648
1649 // Custom Lower Atomic LOAD/STORE
1650 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1651 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1652
1653 if (Subtarget->is64Bit()) {
1654 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001655 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001656 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1657 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1658 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001659
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001660 if (!Subtarget->isV9()) {
1661 // SparcV8 does not have FNEGD and FABSD.
1662 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1663 setOperationAction(ISD::FABS, MVT::f64, Custom);
1664 }
1665
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001666 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1667 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1668 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1669 setOperationAction(ISD::FREM , MVT::f128, Expand);
1670 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001671 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1672 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001673 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001674 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001675 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001676 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1677 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001678 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001679 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001680 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001681 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1682 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1683 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1684 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1685 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001686 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001687 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1688 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001689 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001690 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1691 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001692
Owen Anderson9f944592009-08-11 20:47:22 +00001693 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1694 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1695 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001696
1697 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001698 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1699 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001700
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001701 if (Subtarget->is64Bit()) {
1702 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1703 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1704 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1705 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001706
1707 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1708 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001709
1710 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1711 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1712 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001713 }
1714
Chris Lattner0a1762e2008-03-17 03:21:36 +00001715 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001716 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001717 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001718 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001719
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001720 setOperationAction(ISD::TRAP , MVT::Other, Legal);
1721
Chris Lattner0a1762e2008-03-17 03:21:36 +00001722 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001723 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1724 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1725 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1726 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1727 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001728
Chris Lattner0a1762e2008-03-17 03:21:36 +00001729 setStackPointerRegisterToSaveRestore(SP::O6);
1730
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001731 setOperationAction(ISD::CTPOP, MVT::i32,
1732 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001733
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001734 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1735 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1736 setOperationAction(ISD::STORE, MVT::f128, Legal);
1737 } else {
1738 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1739 setOperationAction(ISD::STORE, MVT::f128, Custom);
1740 }
1741
1742 if (Subtarget->hasHardQuad()) {
1743 setOperationAction(ISD::FADD, MVT::f128, Legal);
1744 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1745 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1746 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1747 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1748 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1749 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1750 if (Subtarget->isV9()) {
1751 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1752 setOperationAction(ISD::FABS, MVT::f128, Legal);
1753 } else {
1754 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1755 setOperationAction(ISD::FABS, MVT::f128, Custom);
1756 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001757
1758 if (!Subtarget->is64Bit()) {
1759 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1760 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1761 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1762 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1763 }
1764
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001765 } else {
1766 // Custom legalize f128 operations.
1767
1768 setOperationAction(ISD::FADD, MVT::f128, Custom);
1769 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1770 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1771 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1772 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1773 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1774 setOperationAction(ISD::FABS, MVT::f128, Custom);
1775
1776 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1777 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1778 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1779
1780 // Setup Runtime library names.
Chris Dewhurst68388a02016-05-18 09:14:13 +00001781 if (Subtarget->is64Bit() && !Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001782 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1783 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1784 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1785 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1786 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1787 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001788 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001789 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001790 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001791 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1792 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1793 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1794 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001795 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1796 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1797 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1798 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
Chris Dewhurst68388a02016-05-18 09:14:13 +00001799 } else if (!Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001800 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1801 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1802 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1803 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1804 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1805 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001806 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001807 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001808 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001809 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1810 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1811 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1812 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001813 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1814 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1815 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1816 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1817 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001818 }
1819
Chris Dewhurst0c1e0022016-06-19 11:03:28 +00001820 if (Subtarget->fixAllFDIVSQRT()) {
1821 // Promote FDIVS and FSQRTS to FDIVD and FSQRTD instructions instead as
1822 // the former instructions generate errata on LEON processors.
1823 setOperationAction(ISD::FDIV, MVT::f32, Promote);
1824 setOperationAction(ISD::FSQRT, MVT::f32, Promote);
1825 }
1826
1827 if (Subtarget->replaceFMULS()) {
1828 // Promote FMULS to FMULD instructions instead as
1829 // the former instructions generate errata on LEON processors.
1830 setOperationAction(ISD::FMUL, MVT::f32, Promote);
1831 }
1832
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00001833 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1834
Eli Friedman2518f832011-05-06 20:34:06 +00001835 setMinFunctionAlignment(2);
1836
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001837 computeRegisterProperties(Subtarget->getRegisterInfo());
Chris Lattner0a1762e2008-03-17 03:21:36 +00001838}
1839
Chris Dewhurst68388a02016-05-18 09:14:13 +00001840bool SparcTargetLowering::useSoftFloat() const {
1841 return Subtarget->useSoftFloat();
1842}
1843
Chris Lattner0a1762e2008-03-17 03:21:36 +00001844const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001845 switch ((SPISD::NodeType)Opcode) {
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001846 case SPISD::FIRST_NUMBER: break;
1847 case SPISD::CMPICC: return "SPISD::CMPICC";
1848 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1849 case SPISD::BRICC: return "SPISD::BRICC";
1850 case SPISD::BRXCC: return "SPISD::BRXCC";
1851 case SPISD::BRFCC: return "SPISD::BRFCC";
1852 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1853 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1854 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1855 case SPISD::EH_SJLJ_SETJMP: return "SPISD::EH_SJLJ_SETJMP";
1856 case SPISD::EH_SJLJ_LONGJMP: return "SPISD::EH_SJLJ_LONGJMP";
1857 case SPISD::Hi: return "SPISD::Hi";
1858 case SPISD::Lo: return "SPISD::Lo";
1859 case SPISD::FTOI: return "SPISD::FTOI";
1860 case SPISD::ITOF: return "SPISD::ITOF";
1861 case SPISD::FTOX: return "SPISD::FTOX";
1862 case SPISD::XTOF: return "SPISD::XTOF";
1863 case SPISD::CALL: return "SPISD::CALL";
1864 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001865 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001866 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1867 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1868 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1869 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001870 }
Matthias Braund04893f2015-05-07 21:33:59 +00001871 return nullptr;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001872}
1873
Mehdi Amini44ede332015-07-09 02:09:04 +00001874EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
1875 EVT VT) const {
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001876 if (!VT.isVector())
1877 return MVT::i32;
1878 return VT.changeVectorElementTypeToInteger();
1879}
1880
Chris Lattner0a1762e2008-03-17 03:21:36 +00001881/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1882/// be zero. Op is expected to be a target specific node. Used by DAG
1883/// combiner.
Jay Foada0653a32014-05-14 21:14:37 +00001884void SparcTargetLowering::computeKnownBitsForTargetNode
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001885 (const SDValue Op,
1886 APInt &KnownZero,
1887 APInt &KnownOne,
1888 const SelectionDAG &DAG,
1889 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001890 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001891 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001892
Chris Lattner0a1762e2008-03-17 03:21:36 +00001893 switch (Op.getOpcode()) {
1894 default: break;
1895 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001896 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001897 case SPISD::SELECT_FCC:
Jay Foada0653a32014-05-14 21:14:37 +00001898 DAG.computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1899 DAG.computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001900
Chris Lattner0a1762e2008-03-17 03:21:36 +00001901 // Only known if known in both the LHS and RHS.
1902 KnownOne &= KnownOne2;
1903 KnownZero &= KnownZero2;
1904 break;
1905 }
1906}
1907
Chris Lattner0a1762e2008-03-17 03:21:36 +00001908// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1909// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001910static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001911 ISD::CondCode CC, unsigned &SPCC) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00001912 if (isNullConstant(RHS) &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001913 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001914 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1915 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001916 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1917 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1918 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00001919 isOneConstant(LHS.getOperand(0)) &&
1920 isNullConstant(LHS.getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001921 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001922 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001923 LHS = CMPCC.getOperand(0);
1924 RHS = CMPCC.getOperand(1);
1925 }
1926}
1927
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001928// Convert to a target node and set target flags.
1929SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1930 SelectionDAG &DAG) const {
1931 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1932 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001933 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001934 GA->getValueType(0),
1935 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001936
1937 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1938 return DAG.getTargetConstantPool(CP->getConstVal(),
1939 CP->getValueType(0),
1940 CP->getAlignment(),
1941 CP->getOffset(), TF);
1942
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001943 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1944 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1945 Op.getValueType(),
1946 0,
1947 TF);
1948
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001949 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1950 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1951 ES->getValueType(0), TF);
1952
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001953 llvm_unreachable("Unhandled address SDNode");
1954}
1955
1956// Split Op into high and low parts according to HiTF and LoTF.
1957// Return an ADD node combining the parts.
1958SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1959 unsigned HiTF, unsigned LoTF,
1960 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001961 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001962 EVT VT = Op.getValueType();
1963 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1964 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1965 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1966}
1967
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001968// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1969// or ExternalSymbol SDNode.
1970SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001971 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001972 EVT VT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001973
Rafael Espindola428b3e62016-06-27 19:15:08 +00001974 // Handle PIC mode first. SPARC needs a got load for every variable!
1975 if (isPositionIndependent()) {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001976 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001977 SDValue HiLo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1978 SparcMCExpr::VK_Sparc_GOT10, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001979 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1980 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001981 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1982 // function has calls.
1983 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1984 MFI->setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001985 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001986 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1987 false, false, false, 0);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001988 }
1989
1990 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001991 switch(getTargetMachine().getCodeModel()) {
1992 default:
1993 llvm_unreachable("Unsupported absolute code model");
1994 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001995 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001996 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1997 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001998 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001999 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002000 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
2001 SparcMCExpr::VK_Sparc_M44, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002002 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002003 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00002004 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
2005 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
2006 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00002007 case CodeModel::Large: {
2008 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002009 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
2010 SparcMCExpr::VK_Sparc_HM, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002011 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002012 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
2013 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00002014 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
2015 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00002016 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002017}
2018
Wesley Peck527da1b2010-11-23 03:31:01 +00002019SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002020 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002021 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002022}
2023
Chris Lattner840c7002009-09-15 17:46:24 +00002024SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002025 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002026 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002027}
2028
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002029SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2030 SelectionDAG &DAG) const {
2031 return makeAddress(Op, DAG);
2032}
2033
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002034SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2035 SelectionDAG &DAG) const {
2036
2037 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002038 if (DAG.getTarget().Options.EmulatedTLS)
2039 return LowerToTLSEmulatedModel(GA, DAG);
2040
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002041 SDLoc DL(GA);
2042 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002043 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002044
2045 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2046
2047 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002048 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
2049 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
2050 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
2051 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
2052 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
2053 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
2054 unsigned addTF = ((model == TLSModel::GeneralDynamic)
2055 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
2056 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
2057 unsigned callTF = ((model == TLSModel::GeneralDynamic)
2058 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
2059 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002060
2061 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2062 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2063 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2064 withTargetFlags(Op, addTF, DAG));
2065
2066 SDValue Chain = DAG.getEntryNode();
2067 SDValue InFlag;
2068
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002069 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, DL, true), DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002070 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
2071 InFlag = Chain.getValue(1);
2072 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2073 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2074
2075 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopher9deb75d2015-03-11 22:42:13 +00002076 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2077 DAG.getMachineFunction(), CallingConv::C);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002078 assert(Mask && "Missing call preserved mask for calling convention");
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +00002079 SDValue Ops[] = {Chain,
2080 Callee,
2081 Symbol,
2082 DAG.getRegister(SP::O0, PtrVT),
2083 DAG.getRegisterMask(Mask),
2084 InFlag};
Craig Topper48d114b2014-04-26 18:35:24 +00002085 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002086 InFlag = Chain.getValue(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002087 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, DL, true),
2088 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002089 InFlag = Chain.getValue(1);
2090 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
2091
2092 if (model != TLSModel::LocalDynamic)
2093 return Ret;
2094
2095 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002096 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002097 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002098 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002099 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2100 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002101 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002102 }
2103
2104 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002105 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
2106 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002107
2108 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2109
2110 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2111 // function has calls.
2112 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2113 MFI->setHasCalls(true);
2114
2115 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002116 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
2117 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002118 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2119 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2120 DL, PtrVT, Ptr,
2121 withTargetFlags(Op, ldTF, DAG));
2122 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2123 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002124 withTargetFlags(Op,
2125 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002126 }
2127
2128 assert(model == TLSModel::LocalExec);
2129 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002130 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002131 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002132 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002133 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2134
2135 return DAG.getNode(ISD::ADD, DL, PtrVT,
2136 DAG.getRegister(SP::G7, PtrVT), Offset);
2137}
2138
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002139SDValue SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain,
2140 ArgListTy &Args, SDValue Arg,
2141 const SDLoc &DL,
2142 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002143 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2144 EVT ArgVT = Arg.getValueType();
2145 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2146
2147 ArgListEntry Entry;
2148 Entry.Node = Arg;
2149 Entry.Ty = ArgTy;
2150
2151 if (ArgTy->isFP128Ty()) {
2152 // Create a stack object and pass the pointer to the library function.
2153 int FI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002154 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002155 Chain = DAG.getStore(Chain,
2156 DL,
2157 Entry.Node,
2158 FIPtr,
2159 MachinePointerInfo(),
2160 false,
2161 false,
2162 8);
2163
2164 Entry.Node = FIPtr;
2165 Entry.Ty = PointerType::getUnqual(ArgTy);
2166 }
2167 Args.push_back(Entry);
2168 return Chain;
2169}
2170
2171SDValue
2172SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2173 const char *LibFuncName,
2174 unsigned numArgs) const {
2175
2176 ArgListTy Args;
2177
2178 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002179 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002180
Mehdi Amini44ede332015-07-09 02:09:04 +00002181 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002182 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2183 Type *RetTyABI = RetTy;
2184 SDValue Chain = DAG.getEntryNode();
2185 SDValue RetPtr;
2186
2187 if (RetTy->isFP128Ty()) {
2188 // Create a Stack Object to receive the return value of type f128.
2189 ArgListEntry Entry;
2190 int RetFI = MFI->CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002191 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002192 Entry.Node = RetPtr;
2193 Entry.Ty = PointerType::getUnqual(RetTy);
2194 if (!Subtarget->is64Bit())
2195 Entry.isSRet = true;
2196 Entry.isReturned = false;
2197 Args.push_back(Entry);
2198 RetTyABI = Type::getVoidTy(*DAG.getContext());
2199 }
2200
2201 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2202 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2203 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2204 }
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002205 TargetLowering::CallLoweringInfo CLI(DAG);
2206 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002207 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002208
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002209 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2210
2211 // chain is in second result.
2212 if (RetTyABI == RetTy)
2213 return CallInfo.first;
2214
2215 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2216
2217 Chain = CallInfo.second;
2218
2219 // Load RetPtr to get the return value.
2220 return DAG.getLoad(Op.getValueType(),
2221 SDLoc(Op),
2222 Chain,
2223 RetPtr,
2224 MachinePointerInfo(),
2225 false, false, false, 8);
2226}
2227
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002228SDValue SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2229 unsigned &SPCC, const SDLoc &DL,
2230 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002231
Craig Topper062a2ba2014-04-25 05:30:21 +00002232 const char *LibCall = nullptr;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002233 bool is64Bit = Subtarget->is64Bit();
2234 switch(SPCC) {
2235 default: llvm_unreachable("Unhandled conditional code!");
2236 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2237 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2238 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2239 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2240 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2241 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2242 case SPCC::FCC_UL :
2243 case SPCC::FCC_ULE:
2244 case SPCC::FCC_UG :
2245 case SPCC::FCC_UGE:
2246 case SPCC::FCC_U :
2247 case SPCC::FCC_O :
2248 case SPCC::FCC_LG :
2249 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2250 }
2251
Mehdi Amini44ede332015-07-09 02:09:04 +00002252 auto PtrVT = getPointerTy(DAG.getDataLayout());
2253 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002254 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2255 ArgListTy Args;
2256 SDValue Chain = DAG.getEntryNode();
2257 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2258 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2259
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002260 TargetLowering::CallLoweringInfo CLI(DAG);
2261 CLI.setDebugLoc(DL).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002262 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002263
2264 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2265
2266 // result is in first, and chain is in second result.
2267 SDValue Result = CallInfo.first;
2268
2269 switch(SPCC) {
2270 default: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002271 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002272 SPCC = SPCC::ICC_NE;
2273 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2274 }
2275 case SPCC::FCC_UL : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002276 SDValue Mask = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002277 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002278 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002279 SPCC = SPCC::ICC_NE;
2280 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2281 }
2282 case SPCC::FCC_ULE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002283 SDValue RHS = DAG.getTargetConstant(2, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002284 SPCC = SPCC::ICC_NE;
2285 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2286 }
2287 case SPCC::FCC_UG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002288 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002289 SPCC = SPCC::ICC_G;
2290 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2291 }
2292 case SPCC::FCC_UGE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002293 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002294 SPCC = SPCC::ICC_NE;
2295 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2296 }
2297
2298 case SPCC::FCC_U : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002299 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002300 SPCC = SPCC::ICC_E;
2301 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2302 }
2303 case SPCC::FCC_O : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002304 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002305 SPCC = SPCC::ICC_NE;
2306 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2307 }
2308 case SPCC::FCC_LG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002309 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002310 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002311 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002312 SPCC = SPCC::ICC_NE;
2313 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2314 }
2315 case SPCC::FCC_UE : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002316 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002317 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002318 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002319 SPCC = SPCC::ICC_E;
2320 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2321 }
2322 }
2323}
2324
2325static SDValue
2326LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2327 const SparcTargetLowering &TLI) {
2328
2329 if (Op.getOperand(0).getValueType() == MVT::f64)
2330 return TLI.LowerF128Op(Op, DAG,
2331 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2332
2333 if (Op.getOperand(0).getValueType() == MVT::f32)
2334 return TLI.LowerF128Op(Op, DAG,
2335 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2336
2337 llvm_unreachable("fpextend with non-float operand!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002338 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002339}
2340
2341static SDValue
2342LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2343 const SparcTargetLowering &TLI) {
2344 // FP_ROUND on f64 and f32 are legal.
2345 if (Op.getOperand(0).getValueType() != MVT::f128)
2346 return Op;
2347
2348 if (Op.getValueType() == MVT::f64)
2349 return TLI.LowerF128Op(Op, DAG,
2350 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2351 if (Op.getValueType() == MVT::f32)
2352 return TLI.LowerF128Op(Op, DAG,
2353 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2354
2355 llvm_unreachable("fpround to non-float!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002356 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002357}
2358
2359static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2360 const SparcTargetLowering &TLI,
2361 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002362 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002363 EVT VT = Op.getValueType();
2364 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002365
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002366 // Expand f128 operations to fp128 abi calls.
2367 if (Op.getOperand(0).getValueType() == MVT::f128
2368 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2369 const char *libName = TLI.getLibcallName(VT == MVT::i32
2370 ? RTLIB::FPTOSINT_F128_I32
2371 : RTLIB::FPTOSINT_F128_I64);
2372 return TLI.LowerF128Op(Op, DAG, libName, 1);
2373 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002374
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002375 // Expand if the resulting type is illegal.
2376 if (!TLI.isTypeLegal(VT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002377 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002378
2379 // Otherwise, Convert the fp value to integer in an FP register.
2380 if (VT == MVT::i32)
2381 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2382 else
2383 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2384
2385 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002386}
2387
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002388static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2389 const SparcTargetLowering &TLI,
2390 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002391 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002392 EVT OpVT = Op.getOperand(0).getValueType();
2393 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2394
2395 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2396
2397 // Expand f128 operations to fp128 ABI calls.
2398 if (Op.getValueType() == MVT::f128
2399 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2400 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2401 ? RTLIB::SINTTOFP_I32_F128
2402 : RTLIB::SINTTOFP_I64_F128);
2403 return TLI.LowerF128Op(Op, DAG, libName, 1);
2404 }
2405
2406 // Expand if the operand type is illegal.
2407 if (!TLI.isTypeLegal(OpVT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002408 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002409
2410 // Otherwise, Convert the int value to FP in an FP register.
2411 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2412 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2413 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002414}
2415
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002416static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2417 const SparcTargetLowering &TLI,
2418 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002419 SDLoc dl(Op);
2420 EVT VT = Op.getValueType();
2421
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002422 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002423 // quad floating point instructions and the resulting type is legal.
2424 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2425 (hasHardQuad && TLI.isTypeLegal(VT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002426 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002427
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002428 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002429
2430 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002431 TLI.getLibcallName(VT == MVT::i32
2432 ? RTLIB::FPTOUINT_F128_I32
2433 : RTLIB::FPTOUINT_F128_I64),
2434 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002435}
2436
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002437static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2438 const SparcTargetLowering &TLI,
2439 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002440 SDLoc dl(Op);
2441 EVT OpVT = Op.getOperand(0).getValueType();
2442 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2443
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002444 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002445 // quad floating point instructions and the operand type is legal.
2446 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002447 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002448
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002449 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002450 TLI.getLibcallName(OpVT == MVT::i32
2451 ? RTLIB::UINTTOFP_I32_F128
2452 : RTLIB::UINTTOFP_I64_F128),
2453 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002454}
2455
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002456static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2457 const SparcTargetLowering &TLI,
2458 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002459 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002460 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002461 SDValue LHS = Op.getOperand(2);
2462 SDValue RHS = Op.getOperand(3);
2463 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002464 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002465 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002466
Chris Lattner0a1762e2008-03-17 03:21:36 +00002467 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2468 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2469 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002470
Chris Lattner0a1762e2008-03-17 03:21:36 +00002471 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002472 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002473 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002474 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002475 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002476 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2477 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002478 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002479 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2480 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2481 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2482 Opc = SPISD::BRICC;
2483 } else {
2484 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2485 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2486 Opc = SPISD::BRFCC;
2487 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002488 }
Owen Anderson9f944592009-08-11 20:47:22 +00002489 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002490 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002491}
2492
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002493static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2494 const SparcTargetLowering &TLI,
2495 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002496 SDValue LHS = Op.getOperand(0);
2497 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002498 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002499 SDValue TrueVal = Op.getOperand(2);
2500 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002501 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002502 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002503
Chris Lattner0a1762e2008-03-17 03:21:36 +00002504 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2505 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2506 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002507
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002508 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002509 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002510 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002511 Opc = LHS.getValueType() == MVT::i32 ?
2512 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002513 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2514 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002515 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2516 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2517 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2518 Opc = SPISD::SELECT_ICC;
2519 } else {
2520 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2521 Opc = SPISD::SELECT_FCC;
2522 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2523 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002524 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002525 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002526 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002527}
2528
Chris Dewhurst69fa1922016-05-04 09:33:30 +00002529SDValue SparcTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG,
2530 const SparcTargetLowering &TLI) const {
2531 SDLoc DL(Op);
2532 return DAG.getNode(SPISD::EH_SJLJ_SETJMP, DL,
2533 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), Op.getOperand(1));
2534
2535}
2536
2537SDValue SparcTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG,
2538 const SparcTargetLowering &TLI) const {
2539 SDLoc DL(Op);
2540 return DAG.getNode(SPISD::EH_SJLJ_LONGJMP, DL, MVT::Other, Op.getOperand(0), Op.getOperand(1));
2541}
2542
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002543static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002544 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002545 MachineFunction &MF = DAG.getMachineFunction();
2546 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002547 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002548
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002549 // Need frame address to find the address of VarArgsFrameIndex.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002550 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2551
Chris Lattner0a1762e2008-03-17 03:21:36 +00002552 // vastart just stores the address of the VarArgsFrameIndex slot into the
2553 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002554 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002555 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00002556 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2557 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002558 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002559 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Chris Lattner676c61d2010-09-21 18:41:36 +00002560 MachinePointerInfo(SV), false, false, 0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002561}
2562
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002563static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002564 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002565 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002566 SDValue InChain = Node->getOperand(0);
2567 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002568 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002569 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002570 SDLoc DL(Node);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002571 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002572 MachinePointerInfo(SV), false, false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002573 // Increment the pointer, VAList, to the next vaarg.
2574 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002575 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2576 DL));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002577 // Store the incremented VAList to the legalized pointer.
2578 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
Chris Lattner676c61d2010-09-21 18:41:36 +00002579 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002580 // Load the actual argument out of the pointer VAList.
2581 // We can't count on greater alignment than the word size.
2582 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2583 false, false, false,
2584 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002585}
2586
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002587static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002588 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002589 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2590 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002591 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002592 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002593
Chris Lattner0a1762e2008-03-17 03:21:36 +00002594 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002595 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2596 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002597 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002598
Chris Lattner0a1762e2008-03-17 03:21:36 +00002599 // The resultant pointer is actually 16 words from the bottom of the stack,
2600 // to provide a register spill area.
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002601 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2602 regSpillArea += Subtarget->getStackPointerBias();
2603
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002604 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002605 DAG.getConstant(regSpillArea, dl, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002606 SDValue Ops[2] = { NewVal, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002607 return DAG.getMergeValues(Ops, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002608}
2609
Chris Lattner0a1762e2008-03-17 03:21:36 +00002610
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002611static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002612 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002613 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002614 dl, MVT::Other, DAG.getEntryNode());
2615 return Chain;
2616}
2617
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002618static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2619 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002620 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2621 MFI->setFrameAddressIsTaken(true);
2622
2623 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002624 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002625 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002626 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002627
2628 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002629
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002630 if (depth == 0) {
2631 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2632 if (Subtarget->is64Bit())
2633 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002634 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002635 return FrameAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002636 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002637
2638 // flush first to make sure the windowed registers' values are in stack
2639 SDValue Chain = getFLUSHW(Op, DAG);
2640 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2641
2642 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2643
2644 while (depth--) {
2645 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002646 DAG.getIntPtrConstant(Offset, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002647 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo(),
2648 false, false, false, 0);
2649 }
2650 if (Subtarget->is64Bit())
2651 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002652 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002653 return FrameAddr;
2654}
2655
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002656
2657static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2658 const SparcSubtarget *Subtarget) {
2659
2660 uint64_t depth = Op.getConstantOperandVal(0);
2661
2662 return getFRAMEADDR(depth, Op, DAG, Subtarget);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002663}
2664
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002665static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002666 const SparcTargetLowering &TLI,
2667 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002668 MachineFunction &MF = DAG.getMachineFunction();
2669 MachineFrameInfo *MFI = MF.getFrameInfo();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002670 MFI->setReturnAddressIsTaken(true);
2671
Bill Wendling908bf812014-01-06 00:43:20 +00002672 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002673 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002674
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002675 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002676 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002677 uint64_t depth = Op.getConstantOperandVal(0);
2678
2679 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002680 if (depth == 0) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002681 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2682 unsigned RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002683 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002684 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002685 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002686
2687 // Need frame address to find return address of the caller.
2688 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2689
2690 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2691 SDValue Ptr = DAG.getNode(ISD::ADD,
2692 dl, VT,
2693 FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002694 DAG.getIntPtrConstant(Offset, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002695 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr,
2696 MachinePointerInfo(), false, false, false, 0);
2697
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002698 return RetAddr;
2699}
2700
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002701static SDValue LowerF64Op(SDValue SrcReg64, const SDLoc &dl, SelectionDAG &DAG,
2702 unsigned opcode) {
James Y Knight51208ea2016-04-25 22:54:09 +00002703 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002704 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002705
2706 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2707 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2708 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2709
James Y Knight51208ea2016-04-25 22:54:09 +00002710 // Note: in little-endian, the floating-point value is stored in the
2711 // registers are in the opposite order, so the subreg with the sign
2712 // bit is the highest-numbered (odd), rather than the
2713 // lowest-numbered (even).
2714
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002715 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2716 SrcReg64);
2717 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2718 SrcReg64);
2719
James Y Knight51208ea2016-04-25 22:54:09 +00002720 if (DAG.getDataLayout().isLittleEndian())
2721 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2722 else
2723 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002724
2725 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2726 dl, MVT::f64), 0);
2727 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2728 DstReg64, Hi32);
2729 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2730 DstReg64, Lo32);
2731 return DstReg64;
2732}
2733
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002734// Lower a f128 load into two f64 loads.
2735static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2736{
2737 SDLoc dl(Op);
2738 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002739 assert(LdNode && LdNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002740 && "Unexpected node type");
2741
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002742 unsigned alignment = LdNode->getAlignment();
2743 if (alignment > 8)
2744 alignment = 8;
2745
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002746 SDValue Hi64 = DAG.getLoad(MVT::f64,
2747 dl,
2748 LdNode->getChain(),
2749 LdNode->getBasePtr(),
2750 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002751 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002752 EVT addrVT = LdNode->getBasePtr().getValueType();
2753 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2754 LdNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002755 DAG.getConstant(8, dl, addrVT));
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002756 SDValue Lo64 = DAG.getLoad(MVT::f64,
2757 dl,
2758 LdNode->getChain(),
2759 LoPtr,
2760 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002761 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002762
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002763 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2764 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002765
2766 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2767 dl, MVT::f128);
2768 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2769 MVT::f128,
2770 SDValue(InFP128, 0),
2771 Hi64,
2772 SubRegEven);
2773 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2774 MVT::f128,
2775 SDValue(InFP128, 0),
2776 Lo64,
2777 SubRegOdd);
2778 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2779 SDValue(Lo64.getNode(), 1) };
Craig Topper48d114b2014-04-26 18:35:24 +00002780 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002781 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
Craig Topper64941d92014-04-27 19:20:57 +00002782 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002783}
2784
James Y Knight3994be82015-08-10 19:11:39 +00002785static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2786{
2787 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2788
2789 EVT MemVT = LdNode->getMemoryVT();
2790 if (MemVT == MVT::f128)
2791 return LowerF128Load(Op, DAG);
2792
2793 return Op;
2794}
2795
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002796// Lower a f128 store into two f64 stores.
2797static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2798 SDLoc dl(Op);
2799 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002800 assert(StNode && StNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002801 && "Unexpected node type");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002802 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2803 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002804
2805 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2806 dl,
2807 MVT::f64,
2808 StNode->getValue(),
2809 SubRegEven);
2810 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2811 dl,
2812 MVT::f64,
2813 StNode->getValue(),
2814 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002815
2816 unsigned alignment = StNode->getAlignment();
2817 if (alignment > 8)
2818 alignment = 8;
2819
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002820 SDValue OutChains[2];
2821 OutChains[0] = DAG.getStore(StNode->getChain(),
2822 dl,
2823 SDValue(Hi64, 0),
2824 StNode->getBasePtr(),
2825 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002826 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002827 EVT addrVT = StNode->getBasePtr().getValueType();
2828 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2829 StNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002830 DAG.getConstant(8, dl, addrVT));
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002831 OutChains[1] = DAG.getStore(StNode->getChain(),
2832 dl,
2833 SDValue(Lo64, 0),
2834 LoPtr,
2835 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002836 false, false, alignment);
Craig Topper48d114b2014-04-26 18:35:24 +00002837 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002838}
2839
James Y Knight3994be82015-08-10 19:11:39 +00002840static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
2841{
2842 SDLoc dl(Op);
2843 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
2844
2845 EVT MemVT = St->getMemoryVT();
2846 if (MemVT == MVT::f128)
2847 return LowerF128Store(Op, DAG);
2848
2849 if (MemVT == MVT::i64) {
2850 // Custom handling for i64 stores: turn it into a bitcast and a
2851 // v2i32 store.
2852 SDValue Val = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, St->getValue());
2853 SDValue Chain = DAG.getStore(
2854 St->getChain(), dl, Val, St->getBasePtr(), St->getPointerInfo(),
2855 St->isVolatile(), St->isNonTemporal(), St->getAlignment(),
2856 St->getAAInfo());
2857 return Chain;
2858 }
2859
2860 return SDValue();
2861}
2862
Roman Divacky7a9c6542014-02-27 19:26:29 +00002863static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
Venkatraman Govindaraju3b6b0e42014-03-01 02:28:34 +00002864 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
2865 && "invalid opcode");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002866
James Y Knight51208ea2016-04-25 22:54:09 +00002867 SDLoc dl(Op);
2868
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002869 if (Op.getValueType() == MVT::f64)
James Y Knight51208ea2016-04-25 22:54:09 +00002870 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002871 if (Op.getValueType() != MVT::f128)
2872 return Op;
2873
Roman Divacky7a9c6542014-02-27 19:26:29 +00002874 // Lower fabs/fneg on f128 to fabs/fneg on f64
2875 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
James Y Knight51208ea2016-04-25 22:54:09 +00002876 // (As with LowerF64Op, on little-endian, we need to negate the odd
2877 // subreg)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002878
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002879 SDValue SrcReg128 = Op.getOperand(0);
2880 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2881 SrcReg128);
2882 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2883 SrcReg128);
James Y Knight51208ea2016-04-25 22:54:09 +00002884
2885 if (DAG.getDataLayout().isLittleEndian()) {
2886 if (isV9)
2887 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
2888 else
2889 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
2890 } else {
2891 if (isV9)
2892 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2893 else
2894 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
2895 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002896
2897 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2898 dl, MVT::f128), 0);
2899 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2900 DstReg128, Hi64);
2901 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2902 DstReg128, Lo64);
2903 return DstReg128;
2904}
2905
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002906static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002907
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002908 if (Op.getValueType() != MVT::i64)
2909 return Op;
2910
2911 SDLoc dl(Op);
2912 SDValue Src1 = Op.getOperand(0);
2913 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2914 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002915 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002916 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2917
2918 SDValue Src2 = Op.getOperand(1);
2919 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2920 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002921 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002922 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2923
2924
2925 bool hasChain = false;
2926 unsigned hiOpc = Op.getOpcode();
2927 switch (Op.getOpcode()) {
2928 default: llvm_unreachable("Invalid opcode");
2929 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2930 case ISD::ADDE: hasChain = true; break;
2931 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2932 case ISD::SUBE: hasChain = true; break;
2933 }
2934 SDValue Lo;
2935 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2936 if (hasChain) {
2937 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2938 Op.getOperand(2));
2939 } else {
2940 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2941 }
2942 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2943 SDValue Carry = Hi.getValue(1);
2944
2945 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2946 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2947 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002948 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002949
2950 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2951 SDValue Ops[2] = { Dst, Carry };
Craig Topper64941d92014-04-27 19:20:57 +00002952 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002953}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002954
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002955// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2956// in LegalizeDAG.cpp except the order of arguments to the library function.
2957static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2958 const SparcTargetLowering &TLI)
2959{
2960 unsigned opcode = Op.getOpcode();
2961 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2962
2963 bool isSigned = (opcode == ISD::SMULO);
2964 EVT VT = MVT::i64;
2965 EVT WideVT = MVT::i128;
2966 SDLoc dl(Op);
2967 SDValue LHS = Op.getOperand(0);
2968
2969 if (LHS.getValueType() != VT)
2970 return Op;
2971
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002972 SDValue ShiftAmt = DAG.getConstant(63, dl, VT);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002973
2974 SDValue RHS = Op.getOperand(1);
2975 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2976 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2977 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2978
2979 SDValue MulResult = TLI.makeLibCall(DAG,
2980 RTLIB::MUL_I128, WideVT,
Craig Topper8fe40e02015-10-22 17:05:00 +00002981 Args, isSigned, dl).first;
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002982 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002983 MulResult, DAG.getIntPtrConstant(0, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002984 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002985 MulResult, DAG.getIntPtrConstant(1, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002986 if (isSigned) {
2987 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2988 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2989 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002990 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, dl, VT),
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002991 ISD::SETNE);
2992 }
2993 // MulResult is a node with an illegal type. Because such things are not
Chandler Carruthee1a1fc2014-08-02 00:24:54 +00002994 // generally permitted during this phase of legalization, ensure that
2995 // nothing is left using the node. The above EXTRACT_ELEMENT nodes should have
2996 // been folded.
2997 assert(MulResult->use_empty() && "Illegally typed node still in use!");
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002998
2999 SDValue Ops[2] = { BottomHalf, TopHalf } ;
Craig Topper64941d92014-04-27 19:20:57 +00003000 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003001}
3002
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003003static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00003004 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
3005 // Expand with a fence.
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003006 return SDValue();
JF Bastien800f87a2016-04-06 21:19:33 +00003007
3008 // Monotonic load/stores are legal.
3009 return Op;
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003010}
3011
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003012SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
3013 SelectionDAG &DAG) const {
3014 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3015 SDLoc dl(Op);
3016 switch (IntNo) {
3017 default: return SDValue(); // Don't custom lower most intrinsics.
3018 case Intrinsic::thread_pointer: {
3019 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3020 return DAG.getRegister(SP::G7, PtrVT);
3021 }
3022 }
3023}
3024
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003025SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00003026LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003027
3028 bool hasHardQuad = Subtarget->hasHardQuad();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003029 bool isV9 = Subtarget->isV9();
3030
Chris Lattner0a1762e2008-03-17 03:21:36 +00003031 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003032 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00003033
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00003034 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
3035 Subtarget);
3036 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
3037 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00003038 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003039 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00003040 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003041 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003042 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
3043 hasHardQuad);
3044 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
3045 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00003046 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
3047 hasHardQuad);
3048 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3049 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003050 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
3051 hasHardQuad);
3052 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
3053 hasHardQuad);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003054 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG, *this);
3055 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG, *this);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003056 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3057 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00003058 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00003059 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00003060
James Y Knight3994be82015-08-10 19:11:39 +00003061 case ISD::LOAD: return LowerLOAD(Op, DAG);
3062 case ISD::STORE: return LowerSTORE(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003063 case ISD::FADD: return LowerF128Op(Op, DAG,
3064 getLibcallName(RTLIB::ADD_F128), 2);
3065 case ISD::FSUB: return LowerF128Op(Op, DAG,
3066 getLibcallName(RTLIB::SUB_F128), 2);
3067 case ISD::FMUL: return LowerF128Op(Op, DAG,
3068 getLibcallName(RTLIB::MUL_F128), 2);
3069 case ISD::FDIV: return LowerF128Op(Op, DAG,
3070 getLibcallName(RTLIB::DIV_F128), 2);
3071 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3072 getLibcallName(RTLIB::SQRT_F128),1);
Roman Divacky7a9c6542014-02-27 19:26:29 +00003073 case ISD::FABS:
3074 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003075 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3076 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00003077 case ISD::ADDC:
3078 case ISD::ADDE:
3079 case ISD::SUBC:
3080 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003081 case ISD::UMULO:
3082 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003083 case ISD::ATOMIC_LOAD:
3084 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003085 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003086 }
3087}
3088
3089MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003090SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00003091 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003092 switch (MI.getOpcode()) {
Chris Dewhurst3202f062016-07-08 15:33:56 +00003093 default: llvm_unreachable("Unknown Custom Instruction!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00003094 case SP::SELECT_CC_Int_ICC:
3095 case SP::SELECT_CC_FP_ICC:
3096 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003097 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003098 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003099 case SP::SELECT_CC_Int_FCC:
3100 case SP::SELECT_CC_FP_FCC:
3101 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003102 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003103 return expandSelectCC(MI, BB, SP::FBCOND);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003104 case SP::EH_SJLJ_SETJMP32ri:
3105 case SP::EH_SJLJ_SETJMP32rr:
3106 return emitEHSjLjSetJmp(MI, BB);
3107 case SP::EH_SJLJ_LONGJMP32rr:
3108 case SP::EH_SJLJ_LONGJMP32ri:
3109 return emitEHSjLjLongJmp(MI, BB);
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003110 }
3111}
3112
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003113MachineBasicBlock *
3114SparcTargetLowering::expandSelectCC(MachineInstr &MI, MachineBasicBlock *BB,
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003115 unsigned BROpcode) const {
Eric Christopherf5e94062015-01-30 23:46:43 +00003116 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003117 DebugLoc dl = MI.getDebugLoc();
3118 unsigned CC = (SPCC::CondCodes)MI.getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003119
Chris Lattner0a1762e2008-03-17 03:21:36 +00003120 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3121 // control-flow pattern. The incoming instruction knows the destination vreg
3122 // to set, the condition code register to branch on, the true/false values to
3123 // select between, and a branch opcode to use.
3124 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc3f79882015-10-20 00:59:43 +00003125 MachineFunction::iterator It = ++BB->getIterator();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003126
Chris Lattner0a1762e2008-03-17 03:21:36 +00003127 // thisMBB:
3128 // ...
3129 // TrueVal = ...
3130 // [f]bCC copy1MBB
3131 // fallthrough --> copy0MBB
3132 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003133 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00003134 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3135 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00003136 F->insert(It, copy0MBB);
3137 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003138
3139 // Transfer the remainder of BB and its successor edges to sinkMBB.
3140 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003141 std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00003142 BB->end());
3143 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3144
3145 // Add the true and fallthrough blocks as its successors.
3146 BB->addSuccessor(copy0MBB);
3147 BB->addSuccessor(sinkMBB);
3148
Dale Johannesen215a9252009-02-13 02:31:35 +00003149 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003150
Chris Lattner0a1762e2008-03-17 03:21:36 +00003151 // copy0MBB:
3152 // %FalseValue = ...
3153 // # fallthrough to sinkMBB
3154 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003155
Chris Lattner0a1762e2008-03-17 03:21:36 +00003156 // Update machine-CFG edges
3157 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003158
Chris Lattner0a1762e2008-03-17 03:21:36 +00003159 // sinkMBB:
3160 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3161 // ...
3162 BB = sinkMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003163 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI.getOperand(0).getReg())
3164 .addReg(MI.getOperand(2).getReg())
3165 .addMBB(copy0MBB)
3166 .addReg(MI.getOperand(1).getReg())
3167 .addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003168
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003169 MI.eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00003170 return BB;
3171}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003172
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003173MachineBasicBlock *
3174SparcTargetLowering::emitEHSjLjLongJmp(MachineInstr &MI,
3175 MachineBasicBlock *MBB) const {
3176 DebugLoc DL = MI.getDebugLoc();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003177 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3178
3179 MachineFunction *MF = MBB->getParent();
3180 MachineRegisterInfo &MRI = MF->getRegInfo();
3181 MachineInstrBuilder MIB;
3182
3183 MVT PVT = getPointerTy(MF->getDataLayout());
3184 unsigned RegSize = PVT.getStoreSize();
3185 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3186
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003187 unsigned Buf = MI.getOperand(0).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003188 unsigned JmpLoc = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3189
3190 // TO DO: If we do 64-bit handling, this perhaps should be FLUSHW, not TA 3
3191 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::TRAPri), SP::G0).addImm(3).addImm(SPCC::ICC_A);
3192
3193 // Instruction to restore FP
3194 const unsigned FP = SP::I6;
3195 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3196 .addReg(FP)
3197 .addReg(Buf)
3198 .addImm(0);
3199
3200 // Instruction to load jmp location
3201 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3202 .addReg(JmpLoc, RegState::Define)
3203 .addReg(Buf)
3204 .addImm(RegSize);
3205
3206 // Instruction to restore SP
3207 const unsigned SP = SP::O6;
3208 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3209 .addReg(SP)
3210 .addReg(Buf)
3211 .addImm(2 * RegSize);
3212
3213 // Instruction to restore I7
3214 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3215 .addReg(SP::I7)
3216 .addReg(Buf, RegState::Kill)
3217 .addImm(3 * RegSize);
3218
3219 // Jump to JmpLoc
3220 BuildMI(*MBB, MI, DL, TII->get(SP::JMPLrr)).addReg(SP::G0).addReg(JmpLoc, RegState::Kill).addReg(SP::G0);
3221
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003222 MI.eraseFromParent();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003223 return MBB;
3224}
3225
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003226MachineBasicBlock *
3227SparcTargetLowering::emitEHSjLjSetJmp(MachineInstr &MI,
3228 MachineBasicBlock *MBB) const {
3229 DebugLoc DL = MI.getDebugLoc();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003230 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3231
3232 MachineFunction *MF = MBB->getParent();
3233 MachineRegisterInfo &MRI = MF->getRegInfo();
3234 MachineInstrBuilder MIB;
3235
3236 MVT PVT = getPointerTy(MF->getDataLayout());
3237 unsigned RegSize = PVT.getStoreSize();
3238 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3239
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003240 unsigned DstReg = MI.getOperand(0).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003241 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
3242 assert(RC->hasType(MVT::i32) && "Invalid destination!");
3243 unsigned mainDstReg = MRI.createVirtualRegister(RC);
3244 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
3245
3246 // For v = setjmp(buf), we generate
3247 //
3248 // thisMBB:
3249 // buf[0] = FP
3250 // buf[RegSize] = restoreMBB <-- takes address of restoreMBB
3251 // buf[RegSize * 2] = O6
3252 // buf[RegSize * 3] = I7
3253 // Ensure restoreMBB remains in the relocations list (done using a bn instruction)
3254 // b mainMBB
3255 //
3256 // mainMBB:
3257 // v_main = 0
3258 // b sinkMBB
3259 //
3260 // restoreMBB:
3261 // v_restore = 1
3262 // --fall through--
3263 //
3264 // sinkMBB:
3265 // v = phi(main, restore)
3266
3267 const BasicBlock *BB = MBB->getBasicBlock();
3268 MachineFunction::iterator It = ++MBB->getIterator();
3269 MachineBasicBlock *thisMBB = MBB;
3270 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
3271 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
3272 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
3273
3274 MF->insert(It, mainMBB);
3275 MF->insert(It, restoreMBB);
3276 MF->insert(It, sinkMBB);
3277 restoreMBB->setHasAddressTaken();
3278
3279 // Transfer the remainder of BB and its successor edges to sinkMBB.
3280 sinkMBB->splice(sinkMBB->begin(), MBB,
3281 std::next(MachineBasicBlock::iterator(MI)),
3282 MBB->end());
3283 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
3284
3285 unsigned LabelReg = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3286 unsigned LabelReg2 = MRI.createVirtualRegister(&SP::IntRegsRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003287 unsigned BufReg = MI.getOperand(1).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003288
3289 // Instruction to store FP
3290 const unsigned FP = SP::I6;
3291 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3292 .addReg(BufReg)
3293 .addImm(0)
3294 .addReg(FP);
3295
3296 // Instructions to store jmp location
3297 MIB = BuildMI(thisMBB, DL, TII->get(SP::SETHIi))
3298 .addReg(LabelReg, RegState::Define)
3299 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_HI);
3300
3301 MIB = BuildMI(thisMBB, DL, TII->get(SP::ORri))
3302 .addReg(LabelReg2, RegState::Define)
3303 .addReg(LabelReg, RegState::Kill)
3304 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_LO);
3305
3306 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3307 .addReg(BufReg)
3308 .addImm(RegSize)
3309 .addReg(LabelReg2, RegState::Kill);
3310
3311 // Instruction to store SP
3312 const unsigned SP = SP::O6;
3313 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3314 .addReg(BufReg)
3315 .addImm(2 * RegSize)
3316 .addReg(SP);
3317
3318 // Instruction to store I7
3319 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3320 .addReg(BufReg)
3321 .addImm(3 * RegSize)
3322 .addReg(SP::I7);
3323
3324
3325 // FIX ME: This next instruction ensures that the restoreMBB block address remains
3326 // valid through optimization passes and serves no other purpose. The ICC_N ensures
3327 // that the branch is never taken. This commented-out code here was an alternative
3328 // attempt to achieve this which brought myriad problems.
3329 //MIB = BuildMI(thisMBB, DL, TII->get(SP::EH_SjLj_Setup)).addMBB(restoreMBB, SparcMCExpr::VK_Sparc_None);
3330 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3331 .addMBB(restoreMBB)
3332 .addImm(SPCC::ICC_N);
3333
3334 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3335 .addMBB(mainMBB)
3336 .addImm(SPCC::ICC_A);
3337
3338 thisMBB->addSuccessor(mainMBB);
3339 thisMBB->addSuccessor(restoreMBB);
3340
3341
3342 // mainMBB:
3343 MIB = BuildMI(mainMBB, DL, TII->get(SP::ORrr))
3344 .addReg(mainDstReg, RegState::Define)
3345 .addReg(SP::G0)
3346 .addReg(SP::G0);
3347 MIB = BuildMI(mainMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3348
3349 mainMBB->addSuccessor(sinkMBB);
3350
3351
3352 // restoreMBB:
3353 MIB = BuildMI(restoreMBB, DL, TII->get(SP::ORri))
3354 .addReg(restoreDstReg, RegState::Define)
3355 .addReg(SP::G0)
3356 .addImm(1);
3357 //MIB = BuildMI(restoreMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3358 restoreMBB->addSuccessor(sinkMBB);
3359
3360 // sinkMBB:
3361 MIB = BuildMI(*sinkMBB, sinkMBB->begin(), DL,
3362 TII->get(SP::PHI), DstReg)
3363 .addReg(mainDstReg).addMBB(mainMBB)
3364 .addReg(restoreDstReg).addMBB(restoreMBB);
3365
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003366 MI.eraseFromParent();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003367 return sinkMBB;
3368}
3369
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003370//===----------------------------------------------------------------------===//
3371// Sparc Inline Assembly Support
3372//===----------------------------------------------------------------------===//
3373
3374/// getConstraintType - Given a constraint letter, return the type of
3375/// constraint it is for this target.
3376SparcTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003377SparcTargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003378 if (Constraint.size() == 1) {
3379 switch (Constraint[0]) {
Chris Dewhurst3202f062016-07-08 15:33:56 +00003380 default:
3381 break;
3382 case 'f':
3383 case 'r':
3384 return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003385 case 'I': // SIMM13
3386 return C_Other;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003387 }
3388 }
3389
3390 return TargetLowering::getConstraintType(Constraint);
3391}
3392
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003393TargetLowering::ConstraintWeight SparcTargetLowering::
3394getSingleConstraintMatchWeight(AsmOperandInfo &info,
3395 const char *constraint) const {
3396 ConstraintWeight weight = CW_Invalid;
3397 Value *CallOperandVal = info.CallOperandVal;
3398 // If we don't have a value, we can't do a match,
3399 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003400 if (!CallOperandVal)
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003401 return CW_Default;
3402
3403 // Look at the constraint type.
3404 switch (*constraint) {
3405 default:
3406 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3407 break;
3408 case 'I': // SIMM13
3409 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3410 if (isInt<13>(C->getSExtValue()))
3411 weight = CW_Constant;
3412 }
3413 break;
3414 }
3415 return weight;
3416}
3417
3418/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3419/// vector. If it is invalid, don't add anything to Ops.
3420void SparcTargetLowering::
3421LowerAsmOperandForConstraint(SDValue Op,
3422 std::string &Constraint,
3423 std::vector<SDValue> &Ops,
3424 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003425 SDValue Result(nullptr, 0);
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003426
3427 // Only support length 1 constraints for now.
3428 if (Constraint.length() > 1)
3429 return;
3430
3431 char ConstraintLetter = Constraint[0];
3432 switch (ConstraintLetter) {
3433 default: break;
3434 case 'I':
3435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3436 if (isInt<13>(C->getSExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003437 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
3438 Op.getValueType());
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003439 break;
3440 }
3441 return;
3442 }
3443 }
3444
3445 if (Result.getNode()) {
3446 Ops.push_back(Result);
3447 return;
3448 }
3449 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3450}
3451
Eric Christopher11e4df72015-02-26 22:38:43 +00003452std::pair<unsigned, const TargetRegisterClass *>
3453SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003454 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003455 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003456 if (Constraint.size() == 1) {
3457 switch (Constraint[0]) {
Chris Dewhurst3202f062016-07-08 15:33:56 +00003458 case 'f':
3459 return std::make_pair(0U, &SP::FPRegsRegClass);
3460
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003461 case 'r':
James Y Knight3994be82015-08-10 19:11:39 +00003462 if (VT == MVT::v2i32)
3463 return std::make_pair(0U, &SP::IntPairRegClass);
3464 else
3465 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003466 }
James Y Knight3994be82015-08-10 19:11:39 +00003467 } else if (!Constraint.empty() && Constraint.size() <= 5
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003468 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3469 // constraint = '{r<d>}'
3470 // Remove the braces from around the name.
3471 StringRef name(Constraint.data()+1, Constraint.size()-2);
3472 // Handle register aliases:
3473 // r0-r7 -> g0-g7
3474 // r8-r15 -> o0-o7
3475 // r16-r23 -> l0-l7
3476 // r24-r31 -> i0-i7
3477 uint64_t intVal = 0;
3478 if (name.substr(0, 1).equals("r")
3479 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3480 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3481 char regType = regTypes[intVal/8];
3482 char regIdx = '0' + (intVal % 8);
3483 char tmp[] = { '{', regType, regIdx, '}', 0 };
3484 std::string newConstraint = std::string(tmp);
Eric Christopher11e4df72015-02-26 22:38:43 +00003485 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3486 VT);
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003487 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003488 }
3489
Eric Christopher11e4df72015-02-26 22:38:43 +00003490 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003491}
3492
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003493bool
3494SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3495 // The Sparc target isn't yet aware of offsets.
3496 return false;
3497}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003498
3499void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3500 SmallVectorImpl<SDValue>& Results,
3501 SelectionDAG &DAG) const {
3502
3503 SDLoc dl(N);
3504
3505 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3506
3507 switch (N->getOpcode()) {
3508 default:
3509 llvm_unreachable("Do not know how to custom type legalize this operation!");
3510
3511 case ISD::FP_TO_SINT:
3512 case ISD::FP_TO_UINT:
3513 // Custom lower only if it involves f128 or i64.
3514 if (N->getOperand(0).getValueType() != MVT::f128
3515 || N->getValueType(0) != MVT::i64)
3516 return;
3517 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3518 ? RTLIB::FPTOSINT_F128_I64
3519 : RTLIB::FPTOUINT_F128_I64);
3520
3521 Results.push_back(LowerF128Op(SDValue(N, 0),
3522 DAG,
3523 getLibcallName(libCall),
3524 1));
3525 return;
3526
3527 case ISD::SINT_TO_FP:
3528 case ISD::UINT_TO_FP:
3529 // Custom lower only if it involves f128 or i64.
3530 if (N->getValueType(0) != MVT::f128
3531 || N->getOperand(0).getValueType() != MVT::i64)
3532 return;
3533
3534 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3535 ? RTLIB::SINTTOFP_I64_F128
3536 : RTLIB::UINTTOFP_I64_F128);
3537
3538 Results.push_back(LowerF128Op(SDValue(N, 0),
3539 DAG,
3540 getLibcallName(libCall),
3541 1));
3542 return;
James Y Knight3994be82015-08-10 19:11:39 +00003543 case ISD::LOAD: {
3544 LoadSDNode *Ld = cast<LoadSDNode>(N);
3545 // Custom handling only for i64: turn i64 load into a v2i32 load,
3546 // and a bitcast.
3547 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3548 return;
3549
3550 SDLoc dl(N);
3551 SDValue LoadRes = DAG.getExtLoad(
3552 Ld->getExtensionType(), dl, MVT::v2i32,
3553 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
3554 MVT::v2i32, Ld->isVolatile(), Ld->isNonTemporal(),
3555 Ld->isInvariant(), Ld->getAlignment(), Ld->getAAInfo());
3556
3557 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3558 Results.push_back(Res);
3559 Results.push_back(LoadRes.getValue(1));
3560 return;
3561 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003562 }
3563}
Marcin Koscielnicki33571e22016-04-26 10:37:14 +00003564
3565// Override to enable LOAD_STACK_GUARD lowering on Linux.
3566bool SparcTargetLowering::useLoadStackGuardNode() const {
3567 if (!Subtarget->isTargetLinux())
3568 return TargetLowering::useLoadStackGuardNode();
3569 return true;
3570}
3571
3572// Override to disable global variable loading on Linux.
3573void SparcTargetLowering::insertSSPDeclarations(Module &M) const {
3574 if (!Subtarget->isTargetLinux())
3575 return TargetLowering::insertSSPDeclarations(M);
3576}