blob: 9e7e3c6b705a9baeba87dc9725e384e86f3a2712 [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"
Craig Topperd0af7e82017-04-28 05:31:46 +000033#include "llvm/Support/KnownBits.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000034using namespace llvm;
35
James Y Knight2cc9da92016-08-12 14:48:09 +000036
Chris Lattner49b269d2008-03-17 05:41:48 +000037//===----------------------------------------------------------------------===//
38// Calling Convention Implementation
39//===----------------------------------------------------------------------===//
40
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000041static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
42 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
43 ISD::ArgFlagsTy &ArgFlags, CCState &State)
44{
45 assert (ArgFlags.isSRet());
46
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000047 // Assign SRet argument.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000048 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
49 0,
50 LocVT, LocInfo));
51 return true;
52}
53
James Y Knight3994be82015-08-10 19:11:39 +000054static bool CC_Sparc_Assign_Split_64(unsigned &ValNo, MVT &ValVT,
55 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
56 ISD::ArgFlagsTy &ArgFlags, CCState &State)
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000057{
Craig Topper840beec2014-04-04 05:16:06 +000058 static const MCPhysReg RegList[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000059 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
60 };
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000061 // Try to get first reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000062 if (unsigned Reg = State.AllocateReg(RegList)) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000063 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
64 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000065 // Assign whole thing in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000066 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
67 State.AllocateStack(8,4),
68 LocVT, LocInfo));
69 return true;
70 }
71
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000072 // Try to get second reg.
Tim Northover3b6b7ca2015-02-21 02:11:17 +000073 if (unsigned Reg = State.AllocateReg(RegList))
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000074 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
75 else
76 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
77 State.AllocateStack(4,4),
78 LocVT, LocInfo));
79 return true;
80}
81
James Y Knight3994be82015-08-10 19:11:39 +000082static bool CC_Sparc_Assign_Ret_Split_64(unsigned &ValNo, MVT &ValVT,
83 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
84 ISD::ArgFlagsTy &ArgFlags, CCState &State)
85{
86 static const MCPhysReg RegList[] = {
87 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
88 };
89
90 // Try to get first reg.
91 if (unsigned Reg = State.AllocateReg(RegList))
92 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
93 else
94 return false;
95
96 // Try to get second reg.
97 if (unsigned Reg = State.AllocateReg(RegList))
98 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
99 else
100 return false;
101
102 return true;
103}
104
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000105// Allocate a full-sized argument for the 64-bit ABI.
106static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
107 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
108 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000109 assert((LocVT == MVT::f32 || LocVT == MVT::f128
110 || LocVT.getSizeInBits() == 64) &&
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000111 "Can't handle non-64 bits locations");
112
113 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000114 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
115 unsigned alignment = (LocVT == MVT::f128) ? 16 : 8;
116 unsigned Offset = State.AllocateStack(size, alignment);
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000117 unsigned Reg = 0;
118
119 if (LocVT == MVT::i64 && Offset < 6*8)
120 // Promote integers to %i0-%i5.
121 Reg = SP::I0 + Offset/8;
122 else if (LocVT == MVT::f64 && Offset < 16*8)
123 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
124 Reg = SP::D0 + Offset/8;
125 else if (LocVT == MVT::f32 && Offset < 16*8)
126 // Promote floats to %f1, %f3, ...
127 Reg = SP::F1 + Offset/4;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000128 else if (LocVT == MVT::f128 && Offset < 16*8)
129 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
130 Reg = SP::Q0 + Offset/16;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000131
132 // Promote to register when possible, otherwise use the stack slot.
133 if (Reg) {
134 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
135 return true;
136 }
137
138 // This argument goes on the stack in an 8-byte slot.
139 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
140 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
141 if (LocVT == MVT::f32)
142 Offset += 4;
143
144 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
145 return true;
146}
147
148// Allocate a half-sized argument for the 64-bit ABI.
149//
150// This is used when passing { float, int } structs by value in registers.
151static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
152 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
153 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
154 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
155 unsigned Offset = State.AllocateStack(4, 4);
156
157 if (LocVT == MVT::f32 && Offset < 16*8) {
158 // Promote floats to %f0-%f31.
159 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
160 LocVT, LocInfo));
161 return true;
162 }
163
164 if (LocVT == MVT::i32 && Offset < 6*8) {
165 // Promote integers to %i0-%i5, using half the register.
166 unsigned Reg = SP::I0 + Offset/8;
167 LocVT = MVT::i64;
168 LocInfo = CCValAssign::AExt;
169
170 // Set the Custom bit if this i32 goes in the high bits of a register.
171 if (Offset % 8 == 0)
172 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
173 LocVT, LocInfo));
174 else
175 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
176 return true;
177 }
178
179 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
180 return true;
181}
182
Chris Lattner49b269d2008-03-17 05:41:48 +0000183#include "SparcGenCallingConv.inc"
184
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000185// The calling conventions in SparcCallingConv.td are described in terms of the
186// callee's register window. This function translates registers to the
187// corresponding caller window %o register.
188static unsigned toCallerWindow(unsigned Reg) {
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +0000189 static_assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7,
190 "Unexpected enum");
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000191 if (Reg >= SP::I0 && Reg <= SP::I7)
192 return Reg - SP::I0 + SP::O0;
193 return Reg;
194}
195
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000196SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000197SparcTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
198 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000199 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000200 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000201 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000202 if (Subtarget->is64Bit())
203 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
204 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
205}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000206
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000207SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000208SparcTargetLowering::LowerReturn_32(SDValue Chain, CallingConv::ID CallConv,
209 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000210 const SmallVectorImpl<ISD::OutputArg> &Outs,
211 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000212 const SDLoc &DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000213 MachineFunction &MF = DAG.getMachineFunction();
214
Chris Lattner49b269d2008-03-17 05:41:48 +0000215 // CCValAssign - represent the assignment of the return value to locations.
216 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000217
Chris Lattner49b269d2008-03-17 05:41:48 +0000218 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000219 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
220 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000221
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000222 // Analyze return values.
223 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000224
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000225 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000226 SmallVector<SDValue, 4> RetOps(1, Chain);
227 // Make room for the return address offset.
228 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000229
230 // Copy the result values into the output registers.
James Y Knight3994be82015-08-10 19:11:39 +0000231 for (unsigned i = 0, realRVLocIdx = 0;
232 i != RVLocs.size();
233 ++i, ++realRVLocIdx) {
Chris Lattner49b269d2008-03-17 05:41:48 +0000234 CCValAssign &VA = RVLocs[i];
235 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000236
James Y Knight3994be82015-08-10 19:11:39 +0000237 SDValue Arg = OutVals[realRVLocIdx];
238
239 if (VA.needsCustom()) {
240 assert(VA.getLocVT() == MVT::v2i32);
241 // Legalize ret v2i32 -> ret 2 x i32 (Basically: do what would
242 // happen by default if this wasn't a legal type)
243
244 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
245 Arg,
246 DAG.getConstant(0, DL, getVectorIdxTy(DAG.getDataLayout())));
247 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
248 Arg,
249 DAG.getConstant(1, DL, getVectorIdxTy(DAG.getDataLayout())));
250
251 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part0, Flag);
252 Flag = Chain.getValue(1);
253 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
254 VA = RVLocs[++i]; // skip ahead to next loc
255 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part1,
256 Flag);
257 } else
258 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000259
Chris Lattner49b269d2008-03-17 05:41:48 +0000260 // Guarantee that all emitted copies are stuck together with flags.
261 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000262 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000263 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000264
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000265 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000266 // If the function returns a struct, copy the SRetReturnReg to I0
267 if (MF.getFunction()->hasStructRetAttr()) {
268 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
269 unsigned Reg = SFI->getSRetReturnReg();
270 if (!Reg)
271 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +0000272 auto PtrVT = getPointerTy(DAG.getDataLayout());
273 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, PtrVT);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000274 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000275 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +0000276 RetOps.push_back(DAG.getRegister(SP::I0, PtrVT));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000277 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000278 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000279
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000280 RetOps[0] = Chain; // Update chain.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000281 RetOps[1] = DAG.getConstant(RetAddrOffset, DL, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000282
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000283 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000284 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000285 RetOps.push_back(Flag);
286
Craig Topper48d114b2014-04-26 18:35:24 +0000287 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000288}
289
290// Lower return values for the 64-bit ABI.
291// Return values are passed the exactly the same way as function arguments.
292SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000293SparcTargetLowering::LowerReturn_64(SDValue Chain, CallingConv::ID CallConv,
294 bool IsVarArg,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000295 const SmallVectorImpl<ISD::OutputArg> &Outs,
296 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000297 const SDLoc &DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000298 // CCValAssign - represent the assignment of the return value to locations.
299 SmallVector<CCValAssign, 16> RVLocs;
300
301 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000302 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
303 *DAG.getContext());
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000304
305 // Analyze return values.
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +0000306 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000307
308 SDValue Flag;
309 SmallVector<SDValue, 4> RetOps(1, Chain);
310
311 // The second operand on the return instruction is the return address offset.
312 // The return address is always %i7+8 with the 64-bit ABI.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000313 RetOps.push_back(DAG.getConstant(8, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000314
315 // Copy the result values into the output registers.
316 for (unsigned i = 0; i != RVLocs.size(); ++i) {
317 CCValAssign &VA = RVLocs[i];
318 assert(VA.isRegLoc() && "Can only return in registers!");
319 SDValue OutVal = OutVals[i];
320
321 // Integer return values must be sign or zero extended by the callee.
322 switch (VA.getLocInfo()) {
Lang Hames06234ec2014-01-14 19:56:36 +0000323 case CCValAssign::Full: break;
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000324 case CCValAssign::SExt:
325 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
326 break;
327 case CCValAssign::ZExt:
328 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
329 break;
330 case CCValAssign::AExt:
331 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000332 break;
Lang Hames06234ec2014-01-14 19:56:36 +0000333 default:
334 llvm_unreachable("Unknown loc info!");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000335 }
336
337 // The custom bit on an i32 return value indicates that it should be passed
338 // in the high bits of the register.
339 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
340 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000341 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000342
343 // The next value may go in the low bits of the same register.
344 // Handle both at once.
345 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
346 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
347 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
348 // Skip the next value, it's already done.
349 ++i;
350 }
351 }
352
353 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
354
355 // Guarantee that all emitted copies are stuck together with flags.
356 Flag = Chain.getValue(1);
357 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
358 }
359
360 RetOps[0] = Chain; // Update chain.
361
362 // Add the flag if we have it.
363 if (Flag.getNode())
364 RetOps.push_back(Flag);
365
Craig Topper48d114b2014-04-26 18:35:24 +0000366 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, RetOps);
Chris Lattner49b269d2008-03-17 05:41:48 +0000367}
368
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000369SDValue SparcTargetLowering::LowerFormalArguments(
370 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
371 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
372 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000373 if (Subtarget->is64Bit())
374 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
375 DL, DAG, InVals);
376 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
377 DL, DAG, InVals);
378}
379
380/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000381/// passed in either one or two GPRs, including FP values. TODO: we should
382/// pass FP values in FP registers for fastcc functions.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000383SDValue SparcTargetLowering::LowerFormalArguments_32(
384 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
385 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
386 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000387 MachineFunction &MF = DAG.getMachineFunction();
388 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000389 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000390
391 // Assign locations to all of the incoming arguments.
392 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000393 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
394 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000395 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000396
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000397 const unsigned StackOffset = 92;
James Y Knight33beb242015-12-15 19:23:12 +0000398 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000399
Reid Kleckner79418562014-05-09 22:32:13 +0000400 unsigned InIdx = 0;
401 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000402 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000403
Reid Kleckner79418562014-05-09 22:32:13 +0000404 if (Ins[InIdx].Flags.isSRet()) {
405 if (InIdx != 0)
406 report_fatal_error("sparc only supports sret on the first parameter");
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000407 // Get SRet from [%fp+64].
Matthias Braun941a7052016-07-28 18:40:00 +0000408 int FrameIdx = MF.getFrameInfo().CreateFixedObject(4, 64, true);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000409 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +0000410 SDValue Arg =
411 DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000412 InVals.push_back(Arg);
413 continue;
414 }
415
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000416 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000417 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000418 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
419
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000420 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
421 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
422 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000423
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000424 assert(i+1 < e);
425 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000426
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000427 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000428 if (NextVA.isMemLoc()) {
Matthias Braun941a7052016-07-28 18:40:00 +0000429 int FrameIdx = MF.getFrameInfo().
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000430 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000431 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +0000432 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000433 } else {
434 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000435 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000436 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000437 }
James Y Knight33beb242015-12-15 19:23:12 +0000438
439 if (IsLittleEndian)
440 std::swap(LoVal, HiVal);
441
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000442 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000443 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000444 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000445 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000446 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000447 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000448 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
449 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
450 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
451 if (VA.getLocVT() == MVT::f32)
452 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
453 else if (VA.getLocVT() != MVT::i32) {
454 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
455 DAG.getValueType(VA.getLocVT()));
456 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
457 }
458 InVals.push_back(Arg);
459 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000460 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000461
462 assert(VA.isMemLoc());
463
464 unsigned Offset = VA.getLocMemOffset()+StackOffset;
Mehdi Amini44ede332015-07-09 02:09:04 +0000465 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000466
467 if (VA.needsCustom()) {
Hans Wennborg1f094852016-04-11 20:35:41 +0000468 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000469 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000470 if (Offset % 8 == 0) {
Matthias Braun941a7052016-07-28 18:40:00 +0000471 int FI = MF.getFrameInfo().CreateFixedObject(8,
472 Offset,
473 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000474 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +0000475 SDValue Load =
476 DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000477 InVals.push_back(Load);
478 continue;
479 }
480
Matthias Braun941a7052016-07-28 18:40:00 +0000481 int FI = MF.getFrameInfo().CreateFixedObject(4,
482 Offset,
483 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000484 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +0000485 SDValue HiVal =
486 DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
Matthias Braun941a7052016-07-28 18:40:00 +0000487 int FI2 = MF.getFrameInfo().CreateFixedObject(4,
488 Offset+4,
489 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000490 SDValue FIPtr2 = DAG.getFrameIndex(FI2, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000491
Justin Lebar9c375812016-07-15 18:27:10 +0000492 SDValue LoVal =
493 DAG.getLoad(MVT::i32, dl, Chain, FIPtr2, MachinePointerInfo());
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000494
James Y Knight33beb242015-12-15 19:23:12 +0000495 if (IsLittleEndian)
496 std::swap(LoVal, HiVal);
497
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000498 SDValue WholeValue =
499 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
James Y Knight3994be82015-08-10 19:11:39 +0000500 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000501 InVals.push_back(WholeValue);
502 continue;
503 }
504
Matthias Braun941a7052016-07-28 18:40:00 +0000505 int FI = MF.getFrameInfo().CreateFixedObject(4,
506 Offset,
507 true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000508 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000509 SDValue Load ;
510 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
Justin Lebar9c375812016-07-15 18:27:10 +0000511 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, MachinePointerInfo());
James Y Knight33beb242015-12-15 19:23:12 +0000512 } else if (VA.getValVT() == MVT::f128) {
513 report_fatal_error("SPARCv8 does not handle f128 in calls; "
514 "pass indirectly");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000515 } else {
James Y Knight33beb242015-12-15 19:23:12 +0000516 // We shouldn't see any other value types here.
James Y Knight99fcb722015-12-15 23:07:16 +0000517 llvm_unreachable("Unexpected ValVT encountered in frame lowering.");
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000518 }
519 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000520 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000521
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000522 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000523 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000524 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
525 unsigned Reg = SFI->getSRetReturnReg();
526 if (!Reg) {
527 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
528 SFI->setSRetReturnReg(Reg);
529 }
530 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
531 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
532 }
533
Chris Lattner49b269d2008-03-17 05:41:48 +0000534 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000535 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +0000536 static const MCPhysReg ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000537 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
538 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000539 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs);
Craig Topper840beec2014-04-04 05:16:06 +0000540 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000541 unsigned ArgOffset = CCInfo.getNextStackOffset();
542 if (NumAllocated == 6)
543 ArgOffset += StackOffset;
544 else {
545 assert(!ArgOffset);
546 ArgOffset = 68+4*NumAllocated;
547 }
548
Chris Lattner49b269d2008-03-17 05:41:48 +0000549 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000550 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000551
Eli Friedmanbe853b72009-07-19 19:53:46 +0000552 std::vector<SDValue> OutChains;
553
Chris Lattner49b269d2008-03-17 05:41:48 +0000554 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
555 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
556 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000557 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000558
Matthias Braun941a7052016-07-28 18:40:00 +0000559 int FrameIdx = MF.getFrameInfo().CreateFixedObject(4, ArgOffset,
560 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000561 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000562
Justin Lebar9c375812016-07-15 18:27:10 +0000563 OutChains.push_back(
564 DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, MachinePointerInfo()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000565 ArgOffset += 4;
566 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000567
568 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000569 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +0000570 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Eli Friedmanbe853b72009-07-19 19:53:46 +0000571 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000572 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000573
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000574 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000575}
576
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000577// Lower formal arguments for the 64 bit ABI.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000578SDValue SparcTargetLowering::LowerFormalArguments_64(
579 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
580 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
581 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000582 MachineFunction &MF = DAG.getMachineFunction();
583
584 // Analyze arguments according to CC_Sparc64.
585 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000586 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
587 *DAG.getContext());
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000588 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
589
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000590 // The argument array begins at %fp+BIAS+128, after the register save area.
591 const unsigned ArgArea = 128;
592
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000593 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
594 CCValAssign &VA = ArgLocs[i];
595 if (VA.isRegLoc()) {
596 // This argument is passed in a register.
597 // All integer register arguments are promoted by the caller to i64.
598
599 // Create a virtual register for the promoted live-in value.
600 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
601 getRegClassFor(VA.getLocVT()));
602 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
603
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000604 // Get the high bits for i32 struct elements.
605 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
606 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000607 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000608
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000609 // The caller promoted the argument, so insert an Assert?ext SDNode so we
610 // won't promote the value again in this function.
611 switch (VA.getLocInfo()) {
612 case CCValAssign::SExt:
613 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
614 DAG.getValueType(VA.getValVT()));
615 break;
616 case CCValAssign::ZExt:
617 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
618 DAG.getValueType(VA.getValVT()));
619 break;
620 default:
621 break;
622 }
623
624 // Truncate the register down to the argument type.
625 if (VA.isExtInLoc())
626 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
627
628 InVals.push_back(Arg);
629 continue;
630 }
631
632 // The registers are exhausted. This argument was passed on the stack.
633 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000634 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
635 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000636 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000637 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
638 // Adjust offset for extended arguments, SPARC is big-endian.
639 // The caller will have written the full slot with extended bytes, but we
640 // prefer our own extending loads.
641 if (VA.isExtInLoc())
642 Offset += 8 - ValSize;
Matthias Braun941a7052016-07-28 18:40:00 +0000643 int FI = MF.getFrameInfo().CreateFixedObject(ValSize, Offset, true);
Justin Lebar9c375812016-07-15 18:27:10 +0000644 InVals.push_back(
645 DAG.getLoad(VA.getValVT(), DL, Chain,
646 DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
647 MachinePointerInfo::getFixedStack(MF, FI)));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000648 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000649
650 if (!IsVarArg)
651 return Chain;
652
653 // This function takes variable arguments, some of which may have been passed
654 // in registers %i0-%i5. Variable floating point arguments are never passed
655 // in floating point registers. They go on %i0-%i5 or on the stack like
656 // integer arguments.
657 //
658 // The va_start intrinsic needs to know the offset to the first variable
659 // argument.
660 unsigned ArgOffset = CCInfo.getNextStackOffset();
661 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
662 // Skip the 128 bytes of register save area.
663 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
664 Subtarget->getStackPointerBias());
665
666 // Save the variable arguments that were passed in registers.
667 // The caller is required to reserve stack space for 6 arguments regardless
668 // of how many arguments were actually passed.
669 SmallVector<SDValue, 8> OutChains;
670 for (; ArgOffset < 6*8; ArgOffset += 8) {
671 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
672 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
Matthias Braun941a7052016-07-28 18:40:00 +0000673 int FI = MF.getFrameInfo().CreateFixedObject(8, ArgOffset + ArgArea, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000674 auto PtrVT = getPointerTy(MF.getDataLayout());
Justin Lebar9c375812016-07-15 18:27:10 +0000675 OutChains.push_back(
676 DAG.getStore(Chain, DL, VArg, DAG.getFrameIndex(FI, PtrVT),
677 MachinePointerInfo::getFixedStack(MF, FI)));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000678 }
679
680 if (!OutChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000681 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000682
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000683 return Chain;
684}
685
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000686SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000687SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000688 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000689 if (Subtarget->is64Bit())
690 return LowerCall_64(CLI, InVals);
691 return LowerCall_32(CLI, InVals);
692}
693
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000694static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
695 ImmutableCallSite *CS) {
696 if (CS)
697 return CS->hasFnAttr(Attribute::ReturnsTwice);
698
Craig Topper062a2ba2014-04-25 05:30:21 +0000699 const Function *CalleeFn = nullptr;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000700 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
701 CalleeFn = dyn_cast<Function>(G->getGlobal());
702 } else if (ExternalSymbolSDNode *E =
703 dyn_cast<ExternalSymbolSDNode>(Callee)) {
704 const Function *Fn = DAG.getMachineFunction().getFunction();
705 const Module *M = Fn->getParent();
706 const char *CalleeName = E->getSymbol();
707 CalleeFn = M->getFunction(CalleeName);
708 }
709
710 if (!CalleeFn)
711 return false;
712 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
713}
714
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000715// Lower a call for the 32-bit ABI.
716SDValue
717SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
718 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000719 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000720 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000721 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
722 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
723 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000724 SDValue Chain = CLI.Chain;
725 SDValue Callee = CLI.Callee;
726 bool &isTailCall = CLI.IsTailCall;
727 CallingConv::ID CallConv = CLI.CallConv;
728 bool isVarArg = CLI.IsVarArg;
729
Evan Cheng67a69dd2010-01-27 00:07:07 +0000730 // Sparc target does not yet support tail call optimization.
731 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000732
Chris Lattner7d4152b2008-03-17 06:58:37 +0000733 // Analyze operands of the call, assigning locations to each operand.
734 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000735 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
736 *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000737 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000738
Chris Lattner7d4152b2008-03-17 06:58:37 +0000739 // Get the size of the outgoing arguments stack space requirement.
740 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000741
Chris Lattner49b269d2008-03-17 05:41:48 +0000742 // Keep stack frames 8-byte aligned.
743 ArgsSize = (ArgsSize+7) & ~7;
744
Matthias Braun941a7052016-07-28 18:40:00 +0000745 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000746
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000747 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000748 SmallVector<SDValue, 8> ByValArgs;
749 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
750 ISD::ArgFlagsTy Flags = Outs[i].Flags;
751 if (!Flags.isByVal())
752 continue;
753
754 SDValue Arg = OutVals[i];
755 unsigned Size = Flags.getByValSize();
756 unsigned Align = Flags.getByValAlign();
757
Chris Dewhurst53bde952016-06-01 08:48:56 +0000758 if (Size > 0U) {
Matthias Braun941a7052016-07-28 18:40:00 +0000759 int FI = MFI.CreateStackObject(Size, Align, false);
Chris Dewhurst53bde952016-06-01 08:48:56 +0000760 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
761 SDValue SizeNode = DAG.getConstant(Size, dl, MVT::i32);
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000762
Chris Dewhurst53bde952016-06-01 08:48:56 +0000763 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
764 false, // isVolatile,
765 (Size <= 32), // AlwaysInline if size <= 32,
766 false, // isTailCall
767 MachinePointerInfo(), MachinePointerInfo());
768 ByValArgs.push_back(FIPtr);
769 }
770 else {
771 SDValue nullVal;
772 ByValArgs.push_back(nullVal);
773 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000774 }
775
Serge Pavlovd526b132017-05-09 13:35:13 +0000776 Chain = DAG.getCALLSEQ_START(Chain, ArgsSize, 0, dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000777
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000778 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
779 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000780
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000781 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000782 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000783 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000784 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000785 i != e;
786 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000787 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000788 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000789
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000790 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
791
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000792 // Use local copy if it is a byval arg.
Chris Dewhurst53bde952016-06-01 08:48:56 +0000793 if (Flags.isByVal()) {
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000794 Arg = ByValArgs[byvalArgIdx++];
Chris Dewhurst53bde952016-06-01 08:48:56 +0000795 if (!Arg) {
796 continue;
797 }
798 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000799
Chris Lattner7d4152b2008-03-17 06:58:37 +0000800 // Promote the value if needed.
801 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000802 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000803 case CCValAssign::Full: break;
804 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000805 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000806 break;
807 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000808 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000809 break;
810 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000811 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
812 break;
813 case CCValAssign::BCvt:
814 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000815 break;
816 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000817
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000818 if (Flags.isSRet()) {
819 assert(VA.needsCustom());
820 // store SRet argument in %sp+64
821 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000822 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000823 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000824 MemOpChains.push_back(
825 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000826 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000827 continue;
828 }
829
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000830 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000831 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000832
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000833 if (VA.isMemLoc()) {
834 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000835 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000836 if (Offset % 8 == 0) {
837 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000838 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000839 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000840 MemOpChains.push_back(
841 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000842 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000843 }
844 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000845
James Y Knight3994be82015-08-10 19:11:39 +0000846 if (VA.getLocVT() == MVT::f64) {
847 // Move from the float value from float registers into the
848 // integer registers.
849
James Y Knight692e0372015-10-09 21:36:19 +0000850 // TODO: The f64 -> v2i32 conversion is super-inefficient for
851 // constants: it sticks them in the constant pool, then loads
852 // to a fp register, then stores to temp memory, then loads to
853 // integer registers.
James Y Knight3994be82015-08-10 19:11:39 +0000854 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
855 }
856
857 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
858 Arg,
859 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
860 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
861 Arg,
862 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000863
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000864 if (VA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000865 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000866 assert(i+1 != e);
867 CCValAssign &NextVA = ArgLocs[++i];
868 if (NextVA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000869 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000870 } else {
James Y Knight3994be82015-08-10 19:11:39 +0000871 // Store the second part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000872 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
873 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000874 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000875 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000876 MemOpChains.push_back(
877 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000878 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000879 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000880 unsigned Offset = VA.getLocMemOffset() + StackOffset;
James Y Knight3994be82015-08-10 19:11:39 +0000881 // Store the first part.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000882 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000883 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000884 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000885 MemOpChains.push_back(
886 DAG.getStore(Chain, dl, Part0, PtrOff, MachinePointerInfo()));
James Y Knight3994be82015-08-10 19:11:39 +0000887 // Store the second part.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000888 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000889 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000890 MemOpChains.push_back(
891 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000892 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000893 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000894 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000895
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000896 // Arguments that can be passed on register must be kept at
897 // RegsToPass vector
898 if (VA.isRegLoc()) {
899 if (VA.getLocVT() != MVT::f32) {
900 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
901 continue;
902 }
903 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
904 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
905 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000906 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000907
908 assert(VA.isMemLoc());
909
910 // Create a store off the stack pointer for this argument.
911 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000912 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
913 dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000914 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000915 MemOpChains.push_back(
916 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000917 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000918
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000919
Chris Lattner49b269d2008-03-17 05:41:48 +0000920 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000921 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000922 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000923
924 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000925 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000926 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000927 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000928 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000929 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000930 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000931 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000932 InFlag = Chain.getValue(1);
933 }
934
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000935 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000936 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000937
Chris Lattner49b269d2008-03-17 05:41:48 +0000938 // If the callee is a GlobalAddress node (quite common, every direct call is)
939 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000940 // Likewise ExternalSymbol -> TargetExternalSymbol.
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +0000941 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Chris Lattner49b269d2008-03-17 05:41:48 +0000942 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000943 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000944 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000945 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000946
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000947 // Returns a chain & a flag for retval copy to use
948 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
949 SmallVector<SDValue, 8> Ops;
950 Ops.push_back(Chain);
951 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000952 if (hasStructRetAttr)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000953 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000954 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
955 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
956 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000957
958 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000959 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000960 const uint32_t *Mask =
961 ((hasReturnsTwice)
962 ? TRI->getRTCallPreservedMask(CallConv)
963 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000964 assert(Mask && "Missing call preserved mask for calling convention");
965 Ops.push_back(DAG.getRegisterMask(Mask));
966
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000967 if (InFlag.getNode())
968 Ops.push_back(InFlag);
969
Craig Topper48d114b2014-04-26 18:35:24 +0000970 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
Chris Lattner49b269d2008-03-17 05:41:48 +0000971 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000972
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000973 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
974 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000975 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000976
Chris Lattnerdb26db22008-03-17 06:01:07 +0000977 // Assign locations to each value returned by this call.
978 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000979 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
980 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000981
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000982 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000983
Chris Lattnerdb26db22008-03-17 06:01:07 +0000984 // Copy all of the result registers out of their specified physreg.
985 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Nirav Dave29938542016-02-26 18:55:22 +0000986 if (RVLocs[i].getLocVT() == MVT::v2i32) {
987 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
988 SDValue Lo = DAG.getCopyFromReg(
989 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InFlag);
990 Chain = Lo.getValue(1);
991 InFlag = Lo.getValue(2);
992 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
993 DAG.getConstant(0, dl, MVT::i32));
994 SDValue Hi = DAG.getCopyFromReg(
995 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InFlag);
996 Chain = Hi.getValue(1);
997 InFlag = Hi.getValue(2);
998 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
999 DAG.getConstant(1, dl, MVT::i32));
1000 InVals.push_back(Vec);
1001 } else {
1002 Chain =
1003 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1004 RVLocs[i].getValVT(), InFlag)
1005 .getValue(1);
1006 InFlag = Chain.getValue(2);
1007 InVals.push_back(Chain.getValue(0));
1008 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001009 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001010
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001011 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +00001012}
1013
Chris Dewhurstad741172016-05-20 10:21:01 +00001014// FIXME? Maybe this could be a TableGen attribute on some registers and
1015// this table could be generated automatically from RegInfo.
1016unsigned SparcTargetLowering::getRegisterByName(const char* RegName, EVT VT,
1017 SelectionDAG &DAG) const {
1018 unsigned Reg = StringSwitch<unsigned>(RegName)
1019 .Case("i0", SP::I0).Case("i1", SP::I1).Case("i2", SP::I2).Case("i3", SP::I3)
1020 .Case("i4", SP::I4).Case("i5", SP::I5).Case("i6", SP::I6).Case("i7", SP::I7)
1021 .Case("o0", SP::O0).Case("o1", SP::O1).Case("o2", SP::O2).Case("o3", SP::O3)
1022 .Case("o4", SP::O4).Case("o5", SP::O5).Case("o6", SP::O6).Case("o7", SP::O7)
1023 .Case("l0", SP::L0).Case("l1", SP::L1).Case("l2", SP::L2).Case("l3", SP::L3)
1024 .Case("l4", SP::L4).Case("l5", SP::L5).Case("l6", SP::L6).Case("l7", SP::L7)
1025 .Case("g0", SP::G0).Case("g1", SP::G1).Case("g2", SP::G2).Case("g3", SP::G3)
1026 .Case("g4", SP::G4).Case("g5", SP::G5).Case("g6", SP::G6).Case("g7", SP::G7)
1027 .Default(0);
1028
1029 if (Reg)
1030 return Reg;
1031
1032 report_fatal_error("Invalid register name global variable");
1033}
1034
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001035// This functions returns true if CalleeName is a ABI function that returns
1036// a long double (fp128).
1037static bool isFP128ABICall(const char *CalleeName)
1038{
1039 static const char *const ABICalls[] =
1040 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
1041 "_Q_sqrt", "_Q_neg",
1042 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001043 "_Q_lltoq", "_Q_ulltoq",
Craig Topper062a2ba2014-04-25 05:30:21 +00001044 nullptr
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001045 };
Craig Topper062a2ba2014-04-25 05:30:21 +00001046 for (const char * const *I = ABICalls; *I != nullptr; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001047 if (strcmp(CalleeName, *I) == 0)
1048 return true;
1049 return false;
1050}
1051
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001052unsigned
1053SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
1054{
Craig Topper062a2ba2014-04-25 05:30:21 +00001055 const Function *CalleeFn = nullptr;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001056 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1057 CalleeFn = dyn_cast<Function>(G->getGlobal());
1058 } else if (ExternalSymbolSDNode *E =
1059 dyn_cast<ExternalSymbolSDNode>(Callee)) {
1060 const Function *Fn = DAG.getMachineFunction().getFunction();
1061 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001062 const char *CalleeName = E->getSymbol();
1063 CalleeFn = M->getFunction(CalleeName);
1064 if (!CalleeFn && isFP128ABICall(CalleeName))
1065 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001066 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001067
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001068 if (!CalleeFn)
1069 return 0;
1070
Joerg Sonnenberger72128092015-10-21 20:05:01 +00001071 // It would be nice to check for the sret attribute on CalleeFn here,
1072 // but since it is not part of the function type, any check will misfire.
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001073
Chris Lattner229907c2011-07-18 04:54:35 +00001074 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
1075 Type *ElementTy = Ty->getElementType();
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001076 return DAG.getDataLayout().getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +00001077}
Chris Lattner49b269d2008-03-17 05:41:48 +00001078
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001079
1080// Fixup floating point arguments in the ... part of a varargs call.
1081//
1082// The SPARC v9 ABI requires that floating point arguments are treated the same
1083// as integers when calling a varargs function. This does not apply to the
1084// fixed arguments that are part of the function's prototype.
1085//
1086// This function post-processes a CCValAssign array created by
1087// AnalyzeCallOperands().
1088static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1089 ArrayRef<ISD::OutputArg> Outs) {
1090 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1091 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001092 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001093 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1094 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001095 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001096 continue;
1097 // The fixed arguments to a varargs function still go in FP registers.
1098 if (Outs[VA.getValNo()].IsFixed)
1099 continue;
1100
1101 // This floating point argument should be reassigned.
1102 CCValAssign NewVA;
1103
1104 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001105 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1106 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1107 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001108 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1109
1110 if (Offset < 6*8) {
1111 // This argument should go in %i0-%i5.
1112 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001113 if (ValTy == MVT::f64)
1114 // Full register, just bitconvert into i64.
1115 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1116 IReg, MVT::i64, CCValAssign::BCvt);
1117 else {
1118 assert(ValTy == MVT::f128 && "Unexpected type!");
1119 // Full register, just bitconvert into i128 -- We will lower this into
1120 // two i64s in LowerCall_64.
1121 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1122 IReg, MVT::i128, CCValAssign::BCvt);
1123 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001124 } else {
1125 // This needs to go to memory, we're out of integer registers.
1126 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1127 Offset, VA.getLocVT(), VA.getLocInfo());
1128 }
1129 ArgLocs[i] = NewVA;
1130 }
1131}
1132
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001133// Lower a call for the 64-bit ABI.
1134SDValue
1135SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1136 SmallVectorImpl<SDValue> &InVals) const {
1137 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001138 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001139 SDValue Chain = CLI.Chain;
Mehdi Amini44ede332015-07-09 02:09:04 +00001140 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001141
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001142 // Sparc target does not yet support tail call optimization.
1143 CLI.IsTailCall = false;
1144
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001145 // Analyze operands of the call, assigning locations to each operand.
1146 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001147 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1148 *DAG.getContext());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001149 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1150
1151 // Get the size of the outgoing arguments stack space requirement.
1152 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001153 // Called functions expect 6 argument words to exist in the stack frame, used
1154 // or not.
1155 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001156
1157 // Keep stack frames 16-byte aligned.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001158 ArgsSize = alignTo(ArgsSize, 16);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001159
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001160 // Varargs calls require special treatment.
1161 if (CLI.IsVarArg)
1162 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1163
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001164 // Adjust the stack pointer to make room for the arguments.
1165 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1166 // with more than 6 arguments.
Serge Pavlovd526b132017-05-09 13:35:13 +00001167 Chain = DAG.getCALLSEQ_START(Chain, ArgsSize, 0, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001168
1169 // Collect the set of registers to pass to the function and their values.
1170 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1171 // instruction.
1172 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1173
1174 // Collect chains from all the memory opeations that copy arguments to the
1175 // stack. They must follow the stack pointer adjustment above and precede the
1176 // call instruction itself.
1177 SmallVector<SDValue, 8> MemOpChains;
1178
1179 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1180 const CCValAssign &VA = ArgLocs[i];
1181 SDValue Arg = CLI.OutVals[i];
1182
1183 // Promote the value if needed.
1184 switch (VA.getLocInfo()) {
1185 default:
1186 llvm_unreachable("Unknown location info!");
1187 case CCValAssign::Full:
1188 break;
1189 case CCValAssign::SExt:
1190 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1191 break;
1192 case CCValAssign::ZExt:
1193 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1194 break;
1195 case CCValAssign::AExt:
1196 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1197 break;
1198 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001199 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1200 // SPARC does not support i128 natively. Lower it into two i64, see below.
1201 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1202 || VA.getLocVT() != MVT::i128)
1203 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001204 break;
1205 }
1206
1207 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001208 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1209 && VA.getLocVT() == MVT::i128) {
Simon Pilgrimfd8bf982016-11-18 10:52:12 +00001210 // Store and reload into the integer register reg and reg+1.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001211 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1212 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
Mehdi Amini44ede332015-07-09 02:09:04 +00001213 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001214 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001215 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001216 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001217 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001218
1219 // Store to %sp+BIAS+128+Offset
Justin Lebar9c375812016-07-15 18:27:10 +00001220 SDValue Store =
1221 DAG.getStore(Chain, DL, Arg, HiPtrOff, MachinePointerInfo());
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001222 // Load into Reg and Reg+1
Justin Lebar9c375812016-07-15 18:27:10 +00001223 SDValue Hi64 =
1224 DAG.getLoad(MVT::i64, DL, Store, HiPtrOff, MachinePointerInfo());
1225 SDValue Lo64 =
1226 DAG.getLoad(MVT::i64, DL, Store, LoPtrOff, MachinePointerInfo());
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001227 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1228 Hi64));
1229 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1230 Lo64));
1231 continue;
1232 }
1233
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001234 // The custom bit on an i32 return value indicates that it should be
1235 // passed in the high bits of the register.
1236 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1237 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001238 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001239
1240 // The next value may go in the low bits of the same register.
1241 // Handle both at once.
1242 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1243 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1244 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1245 CLI.OutVals[i+1]);
1246 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1247 // Skip the next value, it's already done.
1248 ++i;
1249 }
1250 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001251 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001252 continue;
1253 }
1254
1255 assert(VA.isMemLoc());
1256
1257 // Create a store off the stack pointer for this argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00001258 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001259 // The argument area starts at %fp+BIAS+128 in the callee frame,
1260 // %sp+BIAS+128 in ours.
1261 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1262 Subtarget->getStackPointerBias() +
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001263 128, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001264 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +00001265 MemOpChains.push_back(
1266 DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001267 }
1268
1269 // Emit all stores, make sure they occur before the call.
1270 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001271 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001272
1273 // Build a sequence of CopyToReg nodes glued together with token chain and
1274 // glue operands which copy the outgoing args into registers. The InGlue is
1275 // necessary since all emitted instructions must be stuck together in order
1276 // to pass the live physical registers.
1277 SDValue InGlue;
1278 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1279 Chain = DAG.getCopyToReg(Chain, DL,
1280 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1281 InGlue = Chain.getValue(1);
1282 }
1283
1284 // If the callee is a GlobalAddress node (quite common, every direct call is)
1285 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1286 // Likewise ExternalSymbol -> TargetExternalSymbol.
1287 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001288 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +00001289 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001290 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001291 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001292 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001293 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001294
1295 // Build the operands for the call instruction itself.
1296 SmallVector<SDValue, 8> Ops;
1297 Ops.push_back(Chain);
1298 Ops.push_back(Callee);
1299 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1300 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1301 RegsToPass[i].second.getValueType()));
1302
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001303 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +00001304 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001305 const uint32_t *Mask =
1306 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
Eric Christopher9deb75d2015-03-11 22:42:13 +00001307 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1308 CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001309 assert(Mask && "Missing call preserved mask for calling convention");
1310 Ops.push_back(DAG.getRegisterMask(Mask));
1311
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001312 // Make sure the CopyToReg nodes are glued to the call instruction which
1313 // consumes the registers.
1314 if (InGlue.getNode())
1315 Ops.push_back(InGlue);
1316
1317 // Now the call itself.
1318 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001319 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001320 InGlue = Chain.getValue(1);
1321
1322 // Revert the stack pointer immediately after the call.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001323 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
1324 DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001325 InGlue = Chain.getValue(1);
1326
1327 // Now extract the return values. This is more or less the same as
1328 // LowerFormalArguments_64.
1329
1330 // Assign locations to each value returned by this call.
1331 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001332 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1333 *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001334
1335 // Set inreg flag manually for codegen generated library calls that
1336 // return float.
Craig Topper062a2ba2014-04-25 05:30:21 +00001337 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == nullptr)
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001338 CLI.Ins[0].Flags.setInReg();
1339
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001340 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001341
1342 // Copy all of the result registers out of their specified physreg.
1343 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1344 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001345 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001346
1347 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1348 // reside in the same register in the high and low bits. Reuse the
1349 // CopyFromReg previous node to avoid duplicate copies.
1350 SDValue RV;
1351 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1352 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1353 RV = Chain.getValue(0);
1354
1355 // But usually we'll create a new CopyFromReg for a different register.
1356 if (!RV.getNode()) {
1357 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1358 Chain = RV.getValue(1);
1359 InGlue = Chain.getValue(2);
1360 }
1361
1362 // Get the high bits for i32 struct elements.
1363 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1364 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001365 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001366
1367 // The callee promoted the return value, so insert an Assert?ext SDNode so
1368 // we won't promote the value again in this function.
1369 switch (VA.getLocInfo()) {
1370 case CCValAssign::SExt:
1371 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1372 DAG.getValueType(VA.getValVT()));
1373 break;
1374 case CCValAssign::ZExt:
1375 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1376 DAG.getValueType(VA.getValVT()));
1377 break;
1378 default:
1379 break;
1380 }
1381
1382 // Truncate the register down to the return value type.
1383 if (VA.isExtInLoc())
1384 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1385
1386 InVals.push_back(RV);
1387 }
1388
1389 return Chain;
1390}
1391
Chris Lattner0a1762e2008-03-17 03:21:36 +00001392//===----------------------------------------------------------------------===//
1393// TargetLowering Implementation
1394//===----------------------------------------------------------------------===//
1395
James Y Knight7306cd42016-03-29 19:09:54 +00001396TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1397 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1398 AI->getType()->getPrimitiveSizeInBits() == 32)
1399 return AtomicExpansionKind::None; // Uses xchg instruction
1400
1401 return AtomicExpansionKind::CmpXChg;
1402}
1403
Chris Lattner0a1762e2008-03-17 03:21:36 +00001404/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1405/// condition.
1406static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1407 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001408 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001409 case ISD::SETEQ: return SPCC::ICC_E;
1410 case ISD::SETNE: return SPCC::ICC_NE;
1411 case ISD::SETLT: return SPCC::ICC_L;
1412 case ISD::SETGT: return SPCC::ICC_G;
1413 case ISD::SETLE: return SPCC::ICC_LE;
1414 case ISD::SETGE: return SPCC::ICC_GE;
1415 case ISD::SETULT: return SPCC::ICC_CS;
1416 case ISD::SETULE: return SPCC::ICC_LEU;
1417 case ISD::SETUGT: return SPCC::ICC_GU;
1418 case ISD::SETUGE: return SPCC::ICC_CC;
1419 }
1420}
1421
1422/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1423/// FCC condition.
1424static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1425 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001426 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001427 case ISD::SETEQ:
1428 case ISD::SETOEQ: return SPCC::FCC_E;
1429 case ISD::SETNE:
1430 case ISD::SETUNE: return SPCC::FCC_NE;
1431 case ISD::SETLT:
1432 case ISD::SETOLT: return SPCC::FCC_L;
1433 case ISD::SETGT:
1434 case ISD::SETOGT: return SPCC::FCC_G;
1435 case ISD::SETLE:
1436 case ISD::SETOLE: return SPCC::FCC_LE;
1437 case ISD::SETGE:
1438 case ISD::SETOGE: return SPCC::FCC_GE;
1439 case ISD::SETULT: return SPCC::FCC_UL;
1440 case ISD::SETULE: return SPCC::FCC_ULE;
1441 case ISD::SETUGT: return SPCC::FCC_UG;
1442 case ISD::SETUGE: return SPCC::FCC_UGE;
1443 case ISD::SETUO: return SPCC::FCC_U;
1444 case ISD::SETO: return SPCC::FCC_O;
1445 case ISD::SETONE: return SPCC::FCC_LG;
1446 case ISD::SETUEQ: return SPCC::FCC_UE;
1447 }
1448}
1449
James Y Knightef31eaf2016-05-03 14:57:18 +00001450SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
Eric Christopherf5e94062015-01-30 23:46:43 +00001451 const SparcSubtarget &STI)
1452 : TargetLowering(TM), Subtarget(&STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +00001453 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00001454
James Y Knightd966fb62015-08-19 14:47:04 +00001455 // Instructions which use registers as conditionals examine all the
1456 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1457 // matters much whether it's ZeroOrOneBooleanContent, or
1458 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1459 // former.
1460 setBooleanContents(ZeroOrOneBooleanContent);
1461 setBooleanVectorContents(ZeroOrOneBooleanContent);
1462
Chris Lattner0a1762e2008-03-17 03:21:36 +00001463 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001464 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
Chris Dewhurst68388a02016-05-18 09:14:13 +00001465 if (!Subtarget->useSoftFloat()) {
1466 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1467 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1468 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1469 }
James Y Knight3994be82015-08-10 19:11:39 +00001470 if (Subtarget->is64Bit()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001471 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001472 } else {
1473 // On 32bit sparc, we define a double-register 32bit register
1474 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1475 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1476
1477 // ...but almost all operations must be expanded, so set that as
1478 // the default.
1479 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1480 setOperationAction(Op, MVT::v2i32, Expand);
1481 }
1482 // Truncating/extending stores/loads are also not supported.
1483 for (MVT VT : MVT::integer_vector_valuetypes()) {
1484 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1485 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1486 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1487
1488 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1489 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1490 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1491
1492 setTruncStoreAction(VT, MVT::v2i32, Expand);
1493 setTruncStoreAction(MVT::v2i32, VT, Expand);
1494 }
1495 // However, load and store *are* legal.
1496 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1497 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1498 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1499 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1500
1501 // And we need to promote i64 loads/stores into vector load/store
1502 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1503 setOperationAction(ISD::STORE, MVT::i64, Custom);
1504
1505 // Sadly, this doesn't work:
1506 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1507 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1508 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001509
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001510 // Turn FP extload into load/fpextend
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001511 for (MVT VT : MVT::fp_valuetypes()) {
1512 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1513 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1514 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001515
Chris Lattner0a1762e2008-03-17 03:21:36 +00001516 // Sparc doesn't have i1 sign extending load
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001517 for (MVT VT : MVT::integer_valuetypes())
1518 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001519
Chris Lattner0a1762e2008-03-17 03:21:36 +00001520 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001521 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001522 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1523 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001524
1525 // Custom legalize GlobalAddress nodes into LO/HI parts.
Mehdi Amini26d48132015-07-24 16:04:22 +00001526 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1527 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1528 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1529 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001530
Chris Lattner0a1762e2008-03-17 03:21:36 +00001531 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001532 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1533 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1534 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001535
1536 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001537 setOperationAction(ISD::UREM, MVT::i32, Expand);
1538 setOperationAction(ISD::SREM, MVT::i32, Expand);
1539 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1540 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001541
Roman Divacky2262cfa2013-10-31 19:22:33 +00001542 // ... nor does SparcV9.
1543 if (Subtarget->is64Bit()) {
1544 setOperationAction(ISD::UREM, MVT::i64, Expand);
1545 setOperationAction(ISD::SREM, MVT::i64, Expand);
1546 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1547 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1548 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001549
1550 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001551 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1552 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001553 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1554 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001555
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001556 // Custom Expand fp<->uint
1557 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1558 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001559 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1560 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001561
Wesley Peck527da1b2010-11-23 03:31:01 +00001562 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1563 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001564
Chris Lattner0a1762e2008-03-17 03:21:36 +00001565 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001566 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1567 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1568 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001569 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1570
Owen Anderson9f944592009-08-11 20:47:22 +00001571 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1572 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1573 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001574 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001575
Chris Lattner0a1762e2008-03-17 03:21:36 +00001576 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001577 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1578 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1579 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1580 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1581 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1582 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001583 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001584
Owen Anderson9f944592009-08-11 20:47:22 +00001585 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1586 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1587 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001588 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001589
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001590 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1591 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1592
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001593 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001594 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1595 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1596 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1597 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001598 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1599 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001600 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1601 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001602 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001603 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001604
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001605 setOperationAction(ISD::CTPOP, MVT::i64,
1606 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001607 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001608 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001609 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001610 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1611 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001612 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001613 }
1614
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001615 // ATOMICs.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001616 // Atomics are supported on SparcV9. 32-bit atomics are also
1617 // supported by some Leon SparcV8 variants. Otherwise, atomics
1618 // are unsupported.
James Y Knight2cc9da92016-08-12 14:48:09 +00001619 if (Subtarget->isV9())
1620 setMaxAtomicSizeInBitsSupported(64);
1621 else if (Subtarget->hasLeonCasa())
Chris Dewhurst92cac932016-09-06 14:41:09 +00001622 setMaxAtomicSizeInBitsSupported(32);
James Y Knight19f6cce2016-04-12 20:18:48 +00001623 else
1624 setMaxAtomicSizeInBitsSupported(0);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001625
James Y Knight148a6462016-06-17 18:11:48 +00001626 setMinCmpXchgSizeInBits(32);
1627
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001628 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001629
1630 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1631
1632 // Custom Lower Atomic LOAD/STORE
1633 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1634 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1635
1636 if (Subtarget->is64Bit()) {
1637 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001638 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001639 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1640 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1641 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001642
James Y Knight6ef32bf2016-09-02 20:29:11 +00001643 if (!Subtarget->is64Bit()) {
1644 // These libcalls are not available in 32-bit.
1645 setLibcallName(RTLIB::SHL_I128, nullptr);
1646 setLibcallName(RTLIB::SRL_I128, nullptr);
1647 setLibcallName(RTLIB::SRA_I128, nullptr);
1648 }
1649
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001650 if (!Subtarget->isV9()) {
1651 // SparcV8 does not have FNEGD and FABSD.
1652 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1653 setOperationAction(ISD::FABS, MVT::f64, Custom);
1654 }
1655
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001656 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1657 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1658 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1659 setOperationAction(ISD::FREM , MVT::f128, Expand);
1660 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001661 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1662 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001663 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001664 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001665 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001666 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1667 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001668 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001669 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001670 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001671 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1672 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1673 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1674 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1675 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001676 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001677 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1678 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001679 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001680 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1681 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001682
Owen Anderson9f944592009-08-11 20:47:22 +00001683 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1684 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1685 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001686
James Y Knightb0a473a2016-10-05 20:54:17 +00001687 // Expands to [SU]MUL_LOHI.
1688 setOperationAction(ISD::MULHU, MVT::i32, Expand);
1689 setOperationAction(ISD::MULHS, MVT::i32, Expand);
1690 setOperationAction(ISD::MUL, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001691
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001692 if (Subtarget->is64Bit()) {
1693 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1694 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1695 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1696 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001697
1698 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1699 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001700
1701 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1702 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1703 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001704 }
1705
Chris Lattner0a1762e2008-03-17 03:21:36 +00001706 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001707 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001708 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001709 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001710
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001711 setOperationAction(ISD::TRAP , MVT::Other, Legal);
1712
Chris Lattner0a1762e2008-03-17 03:21:36 +00001713 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001714 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1715 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1716 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1717 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1718 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001719
Chris Lattner0a1762e2008-03-17 03:21:36 +00001720 setStackPointerRegisterToSaveRestore(SP::O6);
1721
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001722 setOperationAction(ISD::CTPOP, MVT::i32,
1723 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001724
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001725 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1726 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1727 setOperationAction(ISD::STORE, MVT::f128, Legal);
1728 } else {
1729 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1730 setOperationAction(ISD::STORE, MVT::f128, Custom);
1731 }
1732
1733 if (Subtarget->hasHardQuad()) {
1734 setOperationAction(ISD::FADD, MVT::f128, Legal);
1735 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1736 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1737 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1738 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1739 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1740 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1741 if (Subtarget->isV9()) {
1742 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1743 setOperationAction(ISD::FABS, MVT::f128, Legal);
1744 } else {
1745 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1746 setOperationAction(ISD::FABS, MVT::f128, Custom);
1747 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001748
1749 if (!Subtarget->is64Bit()) {
1750 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1751 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1752 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1753 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1754 }
1755
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001756 } else {
1757 // Custom legalize f128 operations.
1758
1759 setOperationAction(ISD::FADD, MVT::f128, Custom);
1760 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1761 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1762 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1763 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1764 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1765 setOperationAction(ISD::FABS, MVT::f128, Custom);
1766
1767 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1768 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1769 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1770
1771 // Setup Runtime library names.
Chris Dewhurst68388a02016-05-18 09:14:13 +00001772 if (Subtarget->is64Bit() && !Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001773 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1774 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1775 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1776 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1777 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1778 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001779 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001780 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001781 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001782 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1783 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1784 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1785 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001786 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1787 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1788 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1789 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
Chris Dewhurst68388a02016-05-18 09:14:13 +00001790 } else if (!Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001791 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1792 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1793 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1794 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1795 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1796 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001797 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001798 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001799 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001800 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1801 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1802 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1803 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001804 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1805 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1806 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1807 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1808 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001809 }
1810
Chris Dewhurst0c1e0022016-06-19 11:03:28 +00001811 if (Subtarget->fixAllFDIVSQRT()) {
1812 // Promote FDIVS and FSQRTS to FDIVD and FSQRTD instructions instead as
1813 // the former instructions generate errata on LEON processors.
1814 setOperationAction(ISD::FDIV, MVT::f32, Promote);
1815 setOperationAction(ISD::FSQRT, MVT::f32, Promote);
1816 }
1817
1818 if (Subtarget->replaceFMULS()) {
1819 // Promote FMULS to FMULD instructions instead as
1820 // the former instructions generate errata on LEON processors.
1821 setOperationAction(ISD::FMUL, MVT::f32, Promote);
1822 }
1823
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00001824 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1825
Eli Friedman2518f832011-05-06 20:34:06 +00001826 setMinFunctionAlignment(2);
1827
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001828 computeRegisterProperties(Subtarget->getRegisterInfo());
Chris Lattner0a1762e2008-03-17 03:21:36 +00001829}
1830
Chris Dewhurst68388a02016-05-18 09:14:13 +00001831bool SparcTargetLowering::useSoftFloat() const {
1832 return Subtarget->useSoftFloat();
1833}
1834
Chris Lattner0a1762e2008-03-17 03:21:36 +00001835const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001836 switch ((SPISD::NodeType)Opcode) {
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001837 case SPISD::FIRST_NUMBER: break;
1838 case SPISD::CMPICC: return "SPISD::CMPICC";
1839 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1840 case SPISD::BRICC: return "SPISD::BRICC";
1841 case SPISD::BRXCC: return "SPISD::BRXCC";
1842 case SPISD::BRFCC: return "SPISD::BRFCC";
1843 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1844 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1845 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1846 case SPISD::EH_SJLJ_SETJMP: return "SPISD::EH_SJLJ_SETJMP";
1847 case SPISD::EH_SJLJ_LONGJMP: return "SPISD::EH_SJLJ_LONGJMP";
1848 case SPISD::Hi: return "SPISD::Hi";
1849 case SPISD::Lo: return "SPISD::Lo";
1850 case SPISD::FTOI: return "SPISD::FTOI";
1851 case SPISD::ITOF: return "SPISD::ITOF";
1852 case SPISD::FTOX: return "SPISD::FTOX";
1853 case SPISD::XTOF: return "SPISD::XTOF";
1854 case SPISD::CALL: return "SPISD::CALL";
1855 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001856 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001857 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1858 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1859 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1860 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001861 }
Matthias Braund04893f2015-05-07 21:33:59 +00001862 return nullptr;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001863}
1864
Mehdi Amini44ede332015-07-09 02:09:04 +00001865EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
1866 EVT VT) const {
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001867 if (!VT.isVector())
1868 return MVT::i32;
1869 return VT.changeVectorElementTypeToInteger();
1870}
1871
Chris Lattner0a1762e2008-03-17 03:21:36 +00001872/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1873/// be zero. Op is expected to be a target specific node. Used by DAG
1874/// combiner.
Jay Foada0653a32014-05-14 21:14:37 +00001875void SparcTargetLowering::computeKnownBitsForTargetNode
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001876 (const SDValue Op,
Craig Topperd0af7e82017-04-28 05:31:46 +00001877 KnownBits &Known,
Simon Pilgrim37b536e2017-03-31 11:24:16 +00001878 const APInt &DemandedElts,
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001879 const SelectionDAG &DAG,
1880 unsigned Depth) const {
Craig Topperd0af7e82017-04-28 05:31:46 +00001881 KnownBits Known2;
Craig Topperf0aeee02017-05-05 17:36:09 +00001882 Known.resetAll();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001883
Chris Lattner0a1762e2008-03-17 03:21:36 +00001884 switch (Op.getOpcode()) {
1885 default: break;
1886 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001887 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001888 case SPISD::SELECT_FCC:
Craig Topperd0af7e82017-04-28 05:31:46 +00001889 DAG.computeKnownBits(Op.getOperand(1), Known, Depth+1);
1890 DAG.computeKnownBits(Op.getOperand(0), Known2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001891
Chris Lattner0a1762e2008-03-17 03:21:36 +00001892 // Only known if known in both the LHS and RHS.
Craig Topperd0af7e82017-04-28 05:31:46 +00001893 Known.One &= Known2.One;
1894 Known.Zero &= Known2.Zero;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001895 break;
1896 }
1897}
1898
Chris Lattner0a1762e2008-03-17 03:21:36 +00001899// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1900// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001901static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001902 ISD::CondCode CC, unsigned &SPCC) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00001903 if (isNullConstant(RHS) &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001904 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001905 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1906 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001907 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1908 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1909 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00001910 isOneConstant(LHS.getOperand(0)) &&
1911 isNullConstant(LHS.getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001912 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001913 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001914 LHS = CMPCC.getOperand(0);
1915 RHS = CMPCC.getOperand(1);
1916 }
1917}
1918
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001919// Convert to a target node and set target flags.
1920SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1921 SelectionDAG &DAG) const {
1922 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1923 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001924 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001925 GA->getValueType(0),
1926 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001927
1928 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1929 return DAG.getTargetConstantPool(CP->getConstVal(),
1930 CP->getValueType(0),
1931 CP->getAlignment(),
1932 CP->getOffset(), TF);
1933
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001934 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1935 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1936 Op.getValueType(),
1937 0,
1938 TF);
1939
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001940 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1941 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1942 ES->getValueType(0), TF);
1943
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001944 llvm_unreachable("Unhandled address SDNode");
1945}
1946
1947// Split Op into high and low parts according to HiTF and LoTF.
1948// Return an ADD node combining the parts.
1949SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1950 unsigned HiTF, unsigned LoTF,
1951 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001952 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001953 EVT VT = Op.getValueType();
1954 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1955 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1956 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1957}
1958
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001959// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1960// or ExternalSymbol SDNode.
1961SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001962 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001963 EVT VT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001964
Rafael Espindola428b3e62016-06-27 19:15:08 +00001965 // Handle PIC mode first. SPARC needs a got load for every variable!
1966 if (isPositionIndependent()) {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001967 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001968 SDValue HiLo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1969 SparcMCExpr::VK_Sparc_GOT10, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001970 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1971 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001972 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1973 // function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001974 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
1975 MFI.setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001976 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001977 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001978 }
1979
1980 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001981 switch(getTargetMachine().getCodeModel()) {
1982 default:
1983 llvm_unreachable("Unsupported absolute code model");
1984 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001985 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001986 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1987 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001988 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001989 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001990 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
1991 SparcMCExpr::VK_Sparc_M44, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001992 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001993 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001994 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1995 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1996 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001997 case CodeModel::Large: {
1998 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001999 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
2000 SparcMCExpr::VK_Sparc_HM, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002001 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002002 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
2003 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00002004 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
2005 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00002006 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002007}
2008
Wesley Peck527da1b2010-11-23 03:31:01 +00002009SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002010 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002011 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002012}
2013
Chris Lattner840c7002009-09-15 17:46:24 +00002014SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002015 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002016 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002017}
2018
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002019SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2020 SelectionDAG &DAG) const {
2021 return makeAddress(Op, DAG);
2022}
2023
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002024SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2025 SelectionDAG &DAG) const {
2026
2027 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002028 if (DAG.getTarget().Options.EmulatedTLS)
2029 return LowerToTLSEmulatedModel(GA, DAG);
2030
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002031 SDLoc DL(GA);
2032 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002033 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002034
2035 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2036
2037 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002038 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
2039 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
2040 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
2041 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
2042 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
2043 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
2044 unsigned addTF = ((model == TLSModel::GeneralDynamic)
2045 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
2046 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
2047 unsigned callTF = ((model == TLSModel::GeneralDynamic)
2048 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
2049 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002050
2051 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2052 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2053 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2054 withTargetFlags(Op, addTF, DAG));
2055
2056 SDValue Chain = DAG.getEntryNode();
2057 SDValue InFlag;
2058
Serge Pavlovd526b132017-05-09 13:35:13 +00002059 Chain = DAG.getCALLSEQ_START(Chain, 1, 0, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002060 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
2061 InFlag = Chain.getValue(1);
2062 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2063 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2064
2065 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopher9deb75d2015-03-11 22:42:13 +00002066 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2067 DAG.getMachineFunction(), CallingConv::C);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002068 assert(Mask && "Missing call preserved mask for calling convention");
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +00002069 SDValue Ops[] = {Chain,
2070 Callee,
2071 Symbol,
2072 DAG.getRegister(SP::O0, PtrVT),
2073 DAG.getRegisterMask(Mask),
2074 InFlag};
Craig Topper48d114b2014-04-26 18:35:24 +00002075 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002076 InFlag = Chain.getValue(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002077 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, DL, true),
2078 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002079 InFlag = Chain.getValue(1);
2080 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
2081
2082 if (model != TLSModel::LocalDynamic)
2083 return Ret;
2084
2085 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002086 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002087 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002088 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002089 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2090 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002091 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002092 }
2093
2094 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002095 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
2096 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002097
2098 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2099
2100 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2101 // function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00002102 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2103 MFI.setHasCalls(true);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002104
2105 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002106 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
2107 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002108 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2109 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2110 DL, PtrVT, Ptr,
2111 withTargetFlags(Op, ldTF, DAG));
2112 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2113 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002114 withTargetFlags(Op,
2115 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002116 }
2117
2118 assert(model == TLSModel::LocalExec);
2119 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002120 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002121 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002122 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002123 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2124
2125 return DAG.getNode(ISD::ADD, DL, PtrVT,
2126 DAG.getRegister(SP::G7, PtrVT), Offset);
2127}
2128
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002129SDValue SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain,
2130 ArgListTy &Args, SDValue Arg,
2131 const SDLoc &DL,
2132 SelectionDAG &DAG) const {
Matthias Braun941a7052016-07-28 18:40:00 +00002133 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002134 EVT ArgVT = Arg.getValueType();
2135 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2136
2137 ArgListEntry Entry;
2138 Entry.Node = Arg;
2139 Entry.Ty = ArgTy;
2140
2141 if (ArgTy->isFP128Ty()) {
2142 // Create a stack object and pass the pointer to the library function.
Matthias Braun941a7052016-07-28 18:40:00 +00002143 int FI = MFI.CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002144 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00002145 Chain = DAG.getStore(Chain, DL, Entry.Node, FIPtr, MachinePointerInfo(),
2146 /* Alignment = */ 8);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002147
2148 Entry.Node = FIPtr;
2149 Entry.Ty = PointerType::getUnqual(ArgTy);
2150 }
2151 Args.push_back(Entry);
2152 return Chain;
2153}
2154
2155SDValue
2156SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2157 const char *LibFuncName,
2158 unsigned numArgs) const {
2159
2160 ArgListTy Args;
2161
Matthias Braun941a7052016-07-28 18:40:00 +00002162 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002163 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002164
Mehdi Amini44ede332015-07-09 02:09:04 +00002165 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002166 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2167 Type *RetTyABI = RetTy;
2168 SDValue Chain = DAG.getEntryNode();
2169 SDValue RetPtr;
2170
2171 if (RetTy->isFP128Ty()) {
2172 // Create a Stack Object to receive the return value of type f128.
2173 ArgListEntry Entry;
Matthias Braun941a7052016-07-28 18:40:00 +00002174 int RetFI = MFI.CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002175 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002176 Entry.Node = RetPtr;
2177 Entry.Ty = PointerType::getUnqual(RetTy);
2178 if (!Subtarget->is64Bit())
Nirav Dave6de2c772017-03-18 00:43:57 +00002179 Entry.IsSRet = true;
2180 Entry.IsReturned = false;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002181 Args.push_back(Entry);
2182 RetTyABI = Type::getVoidTy(*DAG.getContext());
2183 }
2184
2185 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2186 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2187 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2188 }
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002189 TargetLowering::CallLoweringInfo CLI(DAG);
2190 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002191 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002192
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002193 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2194
2195 // chain is in second result.
2196 if (RetTyABI == RetTy)
2197 return CallInfo.first;
2198
2199 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2200
2201 Chain = CallInfo.second;
2202
2203 // Load RetPtr to get the return value.
Justin Lebar9c375812016-07-15 18:27:10 +00002204 return DAG.getLoad(Op.getValueType(), SDLoc(Op), Chain, RetPtr,
2205 MachinePointerInfo(), /* Alignment = */ 8);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002206}
2207
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002208SDValue SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2209 unsigned &SPCC, const SDLoc &DL,
2210 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002211
Craig Topper062a2ba2014-04-25 05:30:21 +00002212 const char *LibCall = nullptr;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002213 bool is64Bit = Subtarget->is64Bit();
2214 switch(SPCC) {
2215 default: llvm_unreachable("Unhandled conditional code!");
2216 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2217 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2218 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2219 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2220 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2221 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2222 case SPCC::FCC_UL :
2223 case SPCC::FCC_ULE:
2224 case SPCC::FCC_UG :
2225 case SPCC::FCC_UGE:
2226 case SPCC::FCC_U :
2227 case SPCC::FCC_O :
2228 case SPCC::FCC_LG :
2229 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2230 }
2231
Mehdi Amini44ede332015-07-09 02:09:04 +00002232 auto PtrVT = getPointerTy(DAG.getDataLayout());
2233 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002234 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2235 ArgListTy Args;
2236 SDValue Chain = DAG.getEntryNode();
2237 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2238 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2239
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002240 TargetLowering::CallLoweringInfo CLI(DAG);
2241 CLI.setDebugLoc(DL).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002242 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002243
2244 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2245
2246 // result is in first, and chain is in second result.
2247 SDValue Result = CallInfo.first;
2248
2249 switch(SPCC) {
2250 default: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002251 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002252 SPCC = SPCC::ICC_NE;
2253 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2254 }
2255 case SPCC::FCC_UL : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002256 SDValue Mask = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002257 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002258 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002259 SPCC = SPCC::ICC_NE;
2260 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2261 }
2262 case SPCC::FCC_ULE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002263 SDValue RHS = DAG.getTargetConstant(2, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002264 SPCC = SPCC::ICC_NE;
2265 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2266 }
2267 case SPCC::FCC_UG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002268 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002269 SPCC = SPCC::ICC_G;
2270 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2271 }
2272 case SPCC::FCC_UGE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002273 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002274 SPCC = SPCC::ICC_NE;
2275 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2276 }
2277
2278 case SPCC::FCC_U : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002279 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002280 SPCC = SPCC::ICC_E;
2281 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2282 }
2283 case SPCC::FCC_O : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002284 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002285 SPCC = SPCC::ICC_NE;
2286 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2287 }
2288 case SPCC::FCC_LG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002289 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002290 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002291 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002292 SPCC = SPCC::ICC_NE;
2293 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2294 }
2295 case SPCC::FCC_UE : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002296 SDValue Mask = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002297 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002298 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002299 SPCC = SPCC::ICC_E;
2300 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2301 }
2302 }
2303}
2304
2305static SDValue
2306LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2307 const SparcTargetLowering &TLI) {
2308
2309 if (Op.getOperand(0).getValueType() == MVT::f64)
2310 return TLI.LowerF128Op(Op, DAG,
2311 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2312
2313 if (Op.getOperand(0).getValueType() == MVT::f32)
2314 return TLI.LowerF128Op(Op, DAG,
2315 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2316
2317 llvm_unreachable("fpextend with non-float operand!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002318 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002319}
2320
2321static SDValue
2322LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2323 const SparcTargetLowering &TLI) {
2324 // FP_ROUND on f64 and f32 are legal.
2325 if (Op.getOperand(0).getValueType() != MVT::f128)
2326 return Op;
2327
2328 if (Op.getValueType() == MVT::f64)
2329 return TLI.LowerF128Op(Op, DAG,
2330 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2331 if (Op.getValueType() == MVT::f32)
2332 return TLI.LowerF128Op(Op, DAG,
2333 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2334
2335 llvm_unreachable("fpround to non-float!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002336 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002337}
2338
2339static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2340 const SparcTargetLowering &TLI,
2341 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002342 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002343 EVT VT = Op.getValueType();
2344 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002345
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002346 // Expand f128 operations to fp128 abi calls.
2347 if (Op.getOperand(0).getValueType() == MVT::f128
2348 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2349 const char *libName = TLI.getLibcallName(VT == MVT::i32
2350 ? RTLIB::FPTOSINT_F128_I32
2351 : RTLIB::FPTOSINT_F128_I64);
2352 return TLI.LowerF128Op(Op, DAG, libName, 1);
2353 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002354
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002355 // Expand if the resulting type is illegal.
2356 if (!TLI.isTypeLegal(VT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002357 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002358
2359 // Otherwise, Convert the fp value to integer in an FP register.
2360 if (VT == MVT::i32)
2361 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2362 else
2363 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2364
2365 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002366}
2367
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002368static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2369 const SparcTargetLowering &TLI,
2370 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002371 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002372 EVT OpVT = Op.getOperand(0).getValueType();
2373 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2374
2375 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2376
2377 // Expand f128 operations to fp128 ABI calls.
2378 if (Op.getValueType() == MVT::f128
2379 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2380 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2381 ? RTLIB::SINTTOFP_I32_F128
2382 : RTLIB::SINTTOFP_I64_F128);
2383 return TLI.LowerF128Op(Op, DAG, libName, 1);
2384 }
2385
2386 // Expand if the operand type is illegal.
2387 if (!TLI.isTypeLegal(OpVT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002388 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002389
2390 // Otherwise, Convert the int value to FP in an FP register.
2391 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2392 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2393 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002394}
2395
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002396static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2397 const SparcTargetLowering &TLI,
2398 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002399 SDLoc dl(Op);
2400 EVT VT = Op.getValueType();
2401
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002402 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002403 // quad floating point instructions and the resulting type is legal.
2404 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2405 (hasHardQuad && TLI.isTypeLegal(VT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002406 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002407
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002408 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002409
2410 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002411 TLI.getLibcallName(VT == MVT::i32
2412 ? RTLIB::FPTOUINT_F128_I32
2413 : RTLIB::FPTOUINT_F128_I64),
2414 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002415}
2416
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002417static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2418 const SparcTargetLowering &TLI,
2419 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002420 SDLoc dl(Op);
2421 EVT OpVT = Op.getOperand(0).getValueType();
2422 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2423
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002424 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002425 // quad floating point instructions and the operand type is legal.
2426 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002427 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002428
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002429 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002430 TLI.getLibcallName(OpVT == MVT::i32
2431 ? RTLIB::UINTTOFP_I32_F128
2432 : RTLIB::UINTTOFP_I64_F128),
2433 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002434}
2435
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002436static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2437 const SparcTargetLowering &TLI,
2438 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002439 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002440 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002441 SDValue LHS = Op.getOperand(2);
2442 SDValue RHS = Op.getOperand(3);
2443 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002444 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002445 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002446
Chris Lattner0a1762e2008-03-17 03:21:36 +00002447 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2448 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2449 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002450
Chris Lattner0a1762e2008-03-17 03:21:36 +00002451 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002452 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002453 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002454 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002455 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002456 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2457 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002458 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002459 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2460 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2461 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2462 Opc = SPISD::BRICC;
2463 } else {
2464 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2465 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2466 Opc = SPISD::BRFCC;
2467 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002468 }
Owen Anderson9f944592009-08-11 20:47:22 +00002469 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002470 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002471}
2472
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002473static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2474 const SparcTargetLowering &TLI,
2475 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002476 SDValue LHS = Op.getOperand(0);
2477 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002478 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002479 SDValue TrueVal = Op.getOperand(2);
2480 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002481 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002482 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002483
Chris Lattner0a1762e2008-03-17 03:21:36 +00002484 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2485 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2486 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002487
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002488 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002489 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002490 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002491 Opc = LHS.getValueType() == MVT::i32 ?
2492 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002493 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2494 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002495 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2496 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2497 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2498 Opc = SPISD::SELECT_ICC;
2499 } else {
2500 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2501 Opc = SPISD::SELECT_FCC;
2502 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2503 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002504 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002505 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002506 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002507}
2508
Chris Dewhurst69fa1922016-05-04 09:33:30 +00002509SDValue SparcTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG,
2510 const SparcTargetLowering &TLI) const {
2511 SDLoc DL(Op);
2512 return DAG.getNode(SPISD::EH_SJLJ_SETJMP, DL,
2513 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), Op.getOperand(1));
2514
2515}
2516
2517SDValue SparcTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG,
2518 const SparcTargetLowering &TLI) const {
2519 SDLoc DL(Op);
2520 return DAG.getNode(SPISD::EH_SJLJ_LONGJMP, DL, MVT::Other, Op.getOperand(0), Op.getOperand(1));
2521}
2522
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002523static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002524 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002525 MachineFunction &MF = DAG.getMachineFunction();
2526 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002527 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002528
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002529 // Need frame address to find the address of VarArgsFrameIndex.
Matthias Braun941a7052016-07-28 18:40:00 +00002530 MF.getFrameInfo().setFrameAddressIsTaken(true);
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002531
Chris Lattner0a1762e2008-03-17 03:21:36 +00002532 // vastart just stores the address of the VarArgsFrameIndex slot into the
2533 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002534 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002535 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00002536 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2537 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002538 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002539 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00002540 MachinePointerInfo(SV));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002541}
2542
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002543static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002544 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002545 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002546 SDValue InChain = Node->getOperand(0);
2547 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002548 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002549 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002550 SDLoc DL(Node);
Justin Lebar9c375812016-07-15 18:27:10 +00002551 SDValue VAList =
2552 DAG.getLoad(PtrVT, DL, InChain, VAListPtr, MachinePointerInfo(SV));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002553 // Increment the pointer, VAList, to the next vaarg.
2554 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002555 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2556 DL));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002557 // Store the incremented VAList to the legalized pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00002558 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr, VAListPtr,
2559 MachinePointerInfo(SV));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002560 // Load the actual argument out of the pointer VAList.
2561 // We can't count on greater alignment than the word size.
2562 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002563 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits()) / 8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002564}
2565
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002566static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002567 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002568 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2569 SDValue Size = Op.getOperand(1); // Legalize the size.
James Y Knight2e64b8b2016-10-25 22:13:28 +00002570 unsigned Align = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2571 unsigned StackAlign = Subtarget->getFrameLowering()->getStackAlignment();
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002572 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002573 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002574
James Y Knight2e64b8b2016-10-25 22:13:28 +00002575 // TODO: implement over-aligned alloca. (Note: also implies
2576 // supporting support for overaligned function frames + dynamic
2577 // allocations, at all, which currently isn't supported)
2578 if (Align > StackAlign) {
2579 const MachineFunction &MF = DAG.getMachineFunction();
2580 report_fatal_error("Function \"" + Twine(MF.getName()) + "\": "
2581 "over-aligned dynamic alloca not supported.");
2582 }
2583
2584 // The resultant pointer needs to be above the register spill area
2585 // at the bottom of the stack.
2586 unsigned regSpillArea;
2587 if (Subtarget->is64Bit()) {
2588 regSpillArea = 128;
2589 } else {
2590 // On Sparc32, the size of the spill area is 92. Unfortunately,
2591 // that's only 4-byte aligned, not 8-byte aligned (the stack
2592 // pointer is 8-byte aligned). So, if the user asked for an 8-byte
2593 // aligned dynamic allocation, we actually need to add 96 to the
2594 // bottom of the stack, instead of 92, to ensure 8-byte alignment.
2595
2596 // That also means adding 4 to the size of the allocation --
2597 // before applying the 8-byte rounding. Unfortunately, we the
2598 // value we get here has already had rounding applied. So, we need
2599 // to add 8, instead, wasting a bit more memory.
2600
2601 // Further, this only actually needs to be done if the required
2602 // alignment is > 4, but, we've lost that info by this point, too,
2603 // so we always apply it.
2604
2605 // (An alternative approach would be to always reserve 96 bytes
2606 // instead of the required 92, but then we'd waste 4 extra bytes
2607 // in every frame, not just those with dynamic stack allocations)
2608
2609 // TODO: modify code in SelectionDAGBuilder to make this less sad.
2610
2611 Size = DAG.getNode(ISD::ADD, dl, VT, Size,
2612 DAG.getConstant(8, dl, VT));
2613 regSpillArea = 96;
2614 }
2615
Chris Lattner0a1762e2008-03-17 03:21:36 +00002616 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002617 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2618 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002619 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002620
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002621 regSpillArea += Subtarget->getStackPointerBias();
2622
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002623 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002624 DAG.getConstant(regSpillArea, dl, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002625 SDValue Ops[2] = { NewVal, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002626 return DAG.getMergeValues(Ops, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002627}
2628
Chris Lattner0a1762e2008-03-17 03:21:36 +00002629
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002630static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002631 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002632 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002633 dl, MVT::Other, DAG.getEntryNode());
2634 return Chain;
2635}
2636
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002637static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2638 const SparcSubtarget *Subtarget) {
Matthias Braun941a7052016-07-28 18:40:00 +00002639 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2640 MFI.setFrameAddressIsTaken(true);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002641
2642 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002643 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002644 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002645 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002646
2647 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002648
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002649 if (depth == 0) {
2650 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2651 if (Subtarget->is64Bit())
2652 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002653 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002654 return FrameAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002655 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002656
2657 // flush first to make sure the windowed registers' values are in stack
2658 SDValue Chain = getFLUSHW(Op, DAG);
2659 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2660
2661 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2662
2663 while (depth--) {
2664 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002665 DAG.getIntPtrConstant(Offset, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002666 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo());
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002667 }
2668 if (Subtarget->is64Bit())
2669 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002670 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002671 return FrameAddr;
2672}
2673
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002674
2675static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2676 const SparcSubtarget *Subtarget) {
2677
2678 uint64_t depth = Op.getConstantOperandVal(0);
2679
2680 return getFRAMEADDR(depth, Op, DAG, Subtarget);
James Y Knight2cc9da92016-08-12 14:48:09 +00002681
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002682}
2683
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002684static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002685 const SparcTargetLowering &TLI,
2686 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002687 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002688 MachineFrameInfo &MFI = MF.getFrameInfo();
2689 MFI.setReturnAddressIsTaken(true);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002690
Bill Wendling908bf812014-01-06 00:43:20 +00002691 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002692 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002693
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002694 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002695 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002696 uint64_t depth = Op.getConstantOperandVal(0);
2697
2698 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002699 if (depth == 0) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002700 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2701 unsigned RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002702 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002703 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002704 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002705
2706 // Need frame address to find return address of the caller.
2707 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2708
2709 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2710 SDValue Ptr = DAG.getNode(ISD::ADD,
2711 dl, VT,
2712 FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002713 DAG.getIntPtrConstant(Offset, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002714 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002715
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002716 return RetAddr;
2717}
2718
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002719static SDValue LowerF64Op(SDValue SrcReg64, const SDLoc &dl, SelectionDAG &DAG,
2720 unsigned opcode) {
James Y Knight51208ea2016-04-25 22:54:09 +00002721 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002722 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002723
2724 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2725 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2726 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2727
James Y Knight51208ea2016-04-25 22:54:09 +00002728 // Note: in little-endian, the floating-point value is stored in the
2729 // registers are in the opposite order, so the subreg with the sign
2730 // bit is the highest-numbered (odd), rather than the
2731 // lowest-numbered (even).
2732
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002733 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2734 SrcReg64);
2735 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2736 SrcReg64);
2737
James Y Knight51208ea2016-04-25 22:54:09 +00002738 if (DAG.getDataLayout().isLittleEndian())
2739 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2740 else
2741 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002742
2743 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2744 dl, MVT::f64), 0);
2745 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2746 DstReg64, Hi32);
2747 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2748 DstReg64, Lo32);
2749 return DstReg64;
2750}
2751
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002752// Lower a f128 load into two f64 loads.
2753static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2754{
2755 SDLoc dl(Op);
2756 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002757 assert(LdNode && LdNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002758 && "Unexpected node type");
2759
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002760 unsigned alignment = LdNode->getAlignment();
2761 if (alignment > 8)
2762 alignment = 8;
2763
Justin Lebar9c375812016-07-15 18:27:10 +00002764 SDValue Hi64 =
2765 DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LdNode->getBasePtr(),
2766 LdNode->getPointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002767 EVT addrVT = LdNode->getBasePtr().getValueType();
2768 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2769 LdNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002770 DAG.getConstant(8, dl, addrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00002771 SDValue Lo64 = DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LoPtr,
2772 LdNode->getPointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002773
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002774 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2775 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002776
2777 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2778 dl, MVT::f128);
2779 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2780 MVT::f128,
2781 SDValue(InFP128, 0),
2782 Hi64,
2783 SubRegEven);
2784 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2785 MVT::f128,
2786 SDValue(InFP128, 0),
2787 Lo64,
2788 SubRegOdd);
2789 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2790 SDValue(Lo64.getNode(), 1) };
Craig Topper48d114b2014-04-26 18:35:24 +00002791 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002792 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
Craig Topper64941d92014-04-27 19:20:57 +00002793 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002794}
2795
James Y Knight3994be82015-08-10 19:11:39 +00002796static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2797{
2798 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2799
2800 EVT MemVT = LdNode->getMemoryVT();
2801 if (MemVT == MVT::f128)
2802 return LowerF128Load(Op, DAG);
2803
2804 return Op;
2805}
2806
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002807// Lower a f128 store into two f64 stores.
2808static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2809 SDLoc dl(Op);
2810 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002811 assert(StNode && StNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002812 && "Unexpected node type");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002813 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2814 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002815
2816 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2817 dl,
2818 MVT::f64,
2819 StNode->getValue(),
2820 SubRegEven);
2821 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2822 dl,
2823 MVT::f64,
2824 StNode->getValue(),
2825 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002826
2827 unsigned alignment = StNode->getAlignment();
2828 if (alignment > 8)
2829 alignment = 8;
2830
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002831 SDValue OutChains[2];
Justin Lebar9c375812016-07-15 18:27:10 +00002832 OutChains[0] =
2833 DAG.getStore(StNode->getChain(), dl, SDValue(Hi64, 0),
2834 StNode->getBasePtr(), MachinePointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002835 EVT addrVT = StNode->getBasePtr().getValueType();
2836 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2837 StNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002838 DAG.getConstant(8, dl, addrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00002839 OutChains[1] = DAG.getStore(StNode->getChain(), dl, SDValue(Lo64, 0), LoPtr,
2840 MachinePointerInfo(), alignment);
Craig Topper48d114b2014-04-26 18:35:24 +00002841 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002842}
2843
James Y Knight3994be82015-08-10 19:11:39 +00002844static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
2845{
2846 SDLoc dl(Op);
2847 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
2848
2849 EVT MemVT = St->getMemoryVT();
2850 if (MemVT == MVT::f128)
2851 return LowerF128Store(Op, DAG);
2852
2853 if (MemVT == MVT::i64) {
2854 // Custom handling for i64 stores: turn it into a bitcast and a
2855 // v2i32 store.
2856 SDValue Val = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, St->getValue());
2857 SDValue Chain = DAG.getStore(
2858 St->getChain(), dl, Val, St->getBasePtr(), St->getPointerInfo(),
Douglas Katzman26cfb6a2016-07-21 23:28:54 +00002859 St->getAlignment(), St->getMemOperand()->getFlags(), St->getAAInfo());
James Y Knight3994be82015-08-10 19:11:39 +00002860 return Chain;
2861 }
2862
2863 return SDValue();
2864}
2865
Roman Divacky7a9c6542014-02-27 19:26:29 +00002866static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
Venkatraman Govindaraju3b6b0e42014-03-01 02:28:34 +00002867 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
2868 && "invalid opcode");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002869
James Y Knight51208ea2016-04-25 22:54:09 +00002870 SDLoc dl(Op);
2871
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002872 if (Op.getValueType() == MVT::f64)
James Y Knight51208ea2016-04-25 22:54:09 +00002873 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002874 if (Op.getValueType() != MVT::f128)
2875 return Op;
2876
Roman Divacky7a9c6542014-02-27 19:26:29 +00002877 // Lower fabs/fneg on f128 to fabs/fneg on f64
2878 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
James Y Knight51208ea2016-04-25 22:54:09 +00002879 // (As with LowerF64Op, on little-endian, we need to negate the odd
2880 // subreg)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002881
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002882 SDValue SrcReg128 = Op.getOperand(0);
2883 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2884 SrcReg128);
2885 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2886 SrcReg128);
James Y Knight51208ea2016-04-25 22:54:09 +00002887
2888 if (DAG.getDataLayout().isLittleEndian()) {
2889 if (isV9)
2890 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
2891 else
2892 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
2893 } else {
2894 if (isV9)
2895 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2896 else
2897 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
2898 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002899
2900 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2901 dl, MVT::f128), 0);
2902 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2903 DstReg128, Hi64);
2904 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2905 DstReg128, Lo64);
2906 return DstReg128;
2907}
2908
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002909static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002910
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002911 if (Op.getValueType() != MVT::i64)
2912 return Op;
2913
2914 SDLoc dl(Op);
2915 SDValue Src1 = Op.getOperand(0);
2916 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2917 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002918 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002919 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2920
2921 SDValue Src2 = Op.getOperand(1);
2922 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2923 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002924 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002925 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2926
2927
2928 bool hasChain = false;
2929 unsigned hiOpc = Op.getOpcode();
2930 switch (Op.getOpcode()) {
2931 default: llvm_unreachable("Invalid opcode");
2932 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2933 case ISD::ADDE: hasChain = true; break;
2934 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2935 case ISD::SUBE: hasChain = true; break;
2936 }
2937 SDValue Lo;
2938 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2939 if (hasChain) {
2940 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2941 Op.getOperand(2));
2942 } else {
2943 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2944 }
2945 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2946 SDValue Carry = Hi.getValue(1);
2947
2948 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2949 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2950 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002951 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002952
2953 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2954 SDValue Ops[2] = { Dst, Carry };
Craig Topper64941d92014-04-27 19:20:57 +00002955 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002956}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002957
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002958// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2959// in LegalizeDAG.cpp except the order of arguments to the library function.
2960static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2961 const SparcTargetLowering &TLI)
2962{
2963 unsigned opcode = Op.getOpcode();
2964 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2965
2966 bool isSigned = (opcode == ISD::SMULO);
2967 EVT VT = MVT::i64;
2968 EVT WideVT = MVT::i128;
2969 SDLoc dl(Op);
2970 SDValue LHS = Op.getOperand(0);
2971
2972 if (LHS.getValueType() != VT)
2973 return Op;
2974
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002975 SDValue ShiftAmt = DAG.getConstant(63, dl, VT);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002976
2977 SDValue RHS = Op.getOperand(1);
2978 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2979 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2980 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2981
2982 SDValue MulResult = TLI.makeLibCall(DAG,
2983 RTLIB::MUL_I128, WideVT,
Craig Topper8fe40e02015-10-22 17:05:00 +00002984 Args, isSigned, dl).first;
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002985 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002986 MulResult, DAG.getIntPtrConstant(0, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002987 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002988 MulResult, DAG.getIntPtrConstant(1, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002989 if (isSigned) {
2990 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2991 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2992 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002993 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, dl, VT),
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002994 ISD::SETNE);
2995 }
2996 // MulResult is a node with an illegal type. Because such things are not
Chandler Carruthee1a1fc2014-08-02 00:24:54 +00002997 // generally permitted during this phase of legalization, ensure that
2998 // nothing is left using the node. The above EXTRACT_ELEMENT nodes should have
2999 // been folded.
3000 assert(MulResult->use_empty() && "Illegally typed node still in use!");
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003001
3002 SDValue Ops[2] = { BottomHalf, TopHalf } ;
Craig Topper64941d92014-04-27 19:20:57 +00003003 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003004}
3005
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003006static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00003007 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
3008 // Expand with a fence.
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003009 return SDValue();
JF Bastien800f87a2016-04-06 21:19:33 +00003010
3011 // Monotonic load/stores are legal.
3012 return Op;
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003013}
3014
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003015SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
3016 SelectionDAG &DAG) const {
3017 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3018 SDLoc dl(Op);
3019 switch (IntNo) {
3020 default: return SDValue(); // Don't custom lower most intrinsics.
3021 case Intrinsic::thread_pointer: {
3022 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3023 return DAG.getRegister(SP::G7, PtrVT);
3024 }
3025 }
3026}
3027
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003028SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00003029LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003030
3031 bool hasHardQuad = Subtarget->hasHardQuad();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003032 bool isV9 = Subtarget->isV9();
3033
Chris Lattner0a1762e2008-03-17 03:21:36 +00003034 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003035 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00003036
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00003037 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
3038 Subtarget);
3039 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
3040 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00003041 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003042 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00003043 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003044 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003045 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
3046 hasHardQuad);
3047 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
3048 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00003049 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
3050 hasHardQuad);
3051 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3052 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003053 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
3054 hasHardQuad);
3055 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
3056 hasHardQuad);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003057 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG, *this);
3058 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG, *this);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003059 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3060 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00003061 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00003062 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00003063
James Y Knight3994be82015-08-10 19:11:39 +00003064 case ISD::LOAD: return LowerLOAD(Op, DAG);
3065 case ISD::STORE: return LowerSTORE(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003066 case ISD::FADD: return LowerF128Op(Op, DAG,
3067 getLibcallName(RTLIB::ADD_F128), 2);
3068 case ISD::FSUB: return LowerF128Op(Op, DAG,
3069 getLibcallName(RTLIB::SUB_F128), 2);
3070 case ISD::FMUL: return LowerF128Op(Op, DAG,
3071 getLibcallName(RTLIB::MUL_F128), 2);
3072 case ISD::FDIV: return LowerF128Op(Op, DAG,
3073 getLibcallName(RTLIB::DIV_F128), 2);
3074 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3075 getLibcallName(RTLIB::SQRT_F128),1);
Roman Divacky7a9c6542014-02-27 19:26:29 +00003076 case ISD::FABS:
3077 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003078 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3079 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00003080 case ISD::ADDC:
3081 case ISD::ADDE:
3082 case ISD::SUBC:
3083 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003084 case ISD::UMULO:
3085 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003086 case ISD::ATOMIC_LOAD:
3087 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003088 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003089 }
3090}
3091
3092MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003093SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00003094 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003095 switch (MI.getOpcode()) {
James Y Knight2cc9da92016-08-12 14:48:09 +00003096 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00003097 case SP::SELECT_CC_Int_ICC:
3098 case SP::SELECT_CC_FP_ICC:
3099 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003100 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003101 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003102 case SP::SELECT_CC_Int_FCC:
3103 case SP::SELECT_CC_FP_FCC:
3104 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003105 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003106 return expandSelectCC(MI, BB, SP::FBCOND);
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003107 case SP::EH_SJLJ_SETJMP32ri:
3108 case SP::EH_SJLJ_SETJMP32rr:
3109 return emitEHSjLjSetJmp(MI, BB);
3110 case SP::EH_SJLJ_LONGJMP32rr:
3111 case SP::EH_SJLJ_LONGJMP32ri:
3112 return emitEHSjLjLongJmp(MI, BB);
James Y Knight2cc9da92016-08-12 14:48:09 +00003113
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003114 }
3115}
3116
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003117MachineBasicBlock *
3118SparcTargetLowering::expandSelectCC(MachineInstr &MI, MachineBasicBlock *BB,
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003119 unsigned BROpcode) const {
Eric Christopherf5e94062015-01-30 23:46:43 +00003120 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003121 DebugLoc dl = MI.getDebugLoc();
3122 unsigned CC = (SPCC::CondCodes)MI.getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003123
Chris Lattner0a1762e2008-03-17 03:21:36 +00003124 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
3125 // control-flow pattern. The incoming instruction knows the destination vreg
3126 // to set, the condition code register to branch on, the true/false values to
3127 // select between, and a branch opcode to use.
3128 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc3f79882015-10-20 00:59:43 +00003129 MachineFunction::iterator It = ++BB->getIterator();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003130
Chris Lattner0a1762e2008-03-17 03:21:36 +00003131 // thisMBB:
3132 // ...
3133 // TrueVal = ...
3134 // [f]bCC copy1MBB
3135 // fallthrough --> copy0MBB
3136 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003137 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00003138 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3139 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00003140 F->insert(It, copy0MBB);
3141 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003142
3143 // Transfer the remainder of BB and its successor edges to sinkMBB.
3144 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003145 std::next(MachineBasicBlock::iterator(MI)),
Dan Gohman34396292010-07-06 20:24:04 +00003146 BB->end());
3147 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3148
3149 // Add the true and fallthrough blocks as its successors.
3150 BB->addSuccessor(copy0MBB);
3151 BB->addSuccessor(sinkMBB);
3152
Dale Johannesen215a9252009-02-13 02:31:35 +00003153 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003154
Chris Lattner0a1762e2008-03-17 03:21:36 +00003155 // copy0MBB:
3156 // %FalseValue = ...
3157 // # fallthrough to sinkMBB
3158 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003159
Chris Lattner0a1762e2008-03-17 03:21:36 +00003160 // Update machine-CFG edges
3161 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003162
Chris Lattner0a1762e2008-03-17 03:21:36 +00003163 // sinkMBB:
3164 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3165 // ...
3166 BB = sinkMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003167 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI.getOperand(0).getReg())
3168 .addReg(MI.getOperand(2).getReg())
3169 .addMBB(copy0MBB)
3170 .addReg(MI.getOperand(1).getReg())
3171 .addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003172
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003173 MI.eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00003174 return BB;
3175}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003176
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003177MachineBasicBlock *
3178SparcTargetLowering::emitEHSjLjLongJmp(MachineInstr &MI,
3179 MachineBasicBlock *MBB) const {
3180 DebugLoc DL = MI.getDebugLoc();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003181 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3182
3183 MachineFunction *MF = MBB->getParent();
3184 MachineRegisterInfo &MRI = MF->getRegInfo();
3185 MachineInstrBuilder MIB;
3186
3187 MVT PVT = getPointerTy(MF->getDataLayout());
3188 unsigned RegSize = PVT.getStoreSize();
3189 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3190
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003191 unsigned Buf = MI.getOperand(0).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003192 unsigned JmpLoc = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3193
3194 // TO DO: If we do 64-bit handling, this perhaps should be FLUSHW, not TA 3
3195 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::TRAPri), SP::G0).addImm(3).addImm(SPCC::ICC_A);
3196
3197 // Instruction to restore FP
3198 const unsigned FP = SP::I6;
3199 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3200 .addReg(FP)
3201 .addReg(Buf)
3202 .addImm(0);
3203
3204 // Instruction to load jmp location
3205 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3206 .addReg(JmpLoc, RegState::Define)
3207 .addReg(Buf)
3208 .addImm(RegSize);
3209
3210 // Instruction to restore SP
3211 const unsigned SP = SP::O6;
3212 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3213 .addReg(SP)
3214 .addReg(Buf)
3215 .addImm(2 * RegSize);
3216
3217 // Instruction to restore I7
3218 MIB = BuildMI(*MBB, MI, DL, TII->get(SP::LDri))
3219 .addReg(SP::I7)
3220 .addReg(Buf, RegState::Kill)
3221 .addImm(3 * RegSize);
3222
3223 // Jump to JmpLoc
3224 BuildMI(*MBB, MI, DL, TII->get(SP::JMPLrr)).addReg(SP::G0).addReg(JmpLoc, RegState::Kill).addReg(SP::G0);
3225
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003226 MI.eraseFromParent();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003227 return MBB;
3228}
3229
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003230MachineBasicBlock *
3231SparcTargetLowering::emitEHSjLjSetJmp(MachineInstr &MI,
3232 MachineBasicBlock *MBB) const {
3233 DebugLoc DL = MI.getDebugLoc();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003234 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +00003235 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003236
3237 MachineFunction *MF = MBB->getParent();
3238 MachineRegisterInfo &MRI = MF->getRegInfo();
3239 MachineInstrBuilder MIB;
3240
3241 MVT PVT = getPointerTy(MF->getDataLayout());
3242 unsigned RegSize = PVT.getStoreSize();
3243 assert(PVT == MVT::i32 && "Invalid Pointer Size!");
3244
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003245 unsigned DstReg = MI.getOperand(0).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003246 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +00003247 assert(TRI->isTypeLegalForClass(*RC, MVT::i32) && "Invalid destination!");
3248 (void)TRI;
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003249 unsigned mainDstReg = MRI.createVirtualRegister(RC);
3250 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
3251
3252 // For v = setjmp(buf), we generate
3253 //
3254 // thisMBB:
3255 // buf[0] = FP
3256 // buf[RegSize] = restoreMBB <-- takes address of restoreMBB
3257 // buf[RegSize * 2] = O6
3258 // buf[RegSize * 3] = I7
3259 // Ensure restoreMBB remains in the relocations list (done using a bn instruction)
3260 // b mainMBB
3261 //
3262 // mainMBB:
3263 // v_main = 0
3264 // b sinkMBB
3265 //
3266 // restoreMBB:
3267 // v_restore = 1
3268 // --fall through--
3269 //
3270 // sinkMBB:
3271 // v = phi(main, restore)
3272
3273 const BasicBlock *BB = MBB->getBasicBlock();
3274 MachineFunction::iterator It = ++MBB->getIterator();
3275 MachineBasicBlock *thisMBB = MBB;
3276 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
3277 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
3278 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
3279
3280 MF->insert(It, mainMBB);
3281 MF->insert(It, restoreMBB);
3282 MF->insert(It, sinkMBB);
3283 restoreMBB->setHasAddressTaken();
3284
3285 // Transfer the remainder of BB and its successor edges to sinkMBB.
3286 sinkMBB->splice(sinkMBB->begin(), MBB,
3287 std::next(MachineBasicBlock::iterator(MI)),
3288 MBB->end());
3289 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
3290
3291 unsigned LabelReg = MRI.createVirtualRegister(&SP::IntRegsRegClass);
3292 unsigned LabelReg2 = MRI.createVirtualRegister(&SP::IntRegsRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003293 unsigned BufReg = MI.getOperand(1).getReg();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003294
3295 // Instruction to store FP
3296 const unsigned FP = SP::I6;
3297 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3298 .addReg(BufReg)
3299 .addImm(0)
3300 .addReg(FP);
3301
3302 // Instructions to store jmp location
3303 MIB = BuildMI(thisMBB, DL, TII->get(SP::SETHIi))
3304 .addReg(LabelReg, RegState::Define)
3305 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_HI);
3306
3307 MIB = BuildMI(thisMBB, DL, TII->get(SP::ORri))
3308 .addReg(LabelReg2, RegState::Define)
3309 .addReg(LabelReg, RegState::Kill)
3310 .addMBB(restoreMBB, SparcMCExpr::VK_Sparc_LO);
3311
3312 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3313 .addReg(BufReg)
3314 .addImm(RegSize)
3315 .addReg(LabelReg2, RegState::Kill);
3316
3317 // Instruction to store SP
3318 const unsigned SP = SP::O6;
3319 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3320 .addReg(BufReg)
3321 .addImm(2 * RegSize)
3322 .addReg(SP);
3323
3324 // Instruction to store I7
3325 MIB = BuildMI(thisMBB, DL, TII->get(SP::STri))
3326 .addReg(BufReg)
3327 .addImm(3 * RegSize)
3328 .addReg(SP::I7);
3329
3330
3331 // FIX ME: This next instruction ensures that the restoreMBB block address remains
3332 // valid through optimization passes and serves no other purpose. The ICC_N ensures
3333 // that the branch is never taken. This commented-out code here was an alternative
3334 // attempt to achieve this which brought myriad problems.
3335 //MIB = BuildMI(thisMBB, DL, TII->get(SP::EH_SjLj_Setup)).addMBB(restoreMBB, SparcMCExpr::VK_Sparc_None);
3336 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3337 .addMBB(restoreMBB)
3338 .addImm(SPCC::ICC_N);
3339
3340 MIB = BuildMI(thisMBB, DL, TII->get(SP::BCOND))
3341 .addMBB(mainMBB)
3342 .addImm(SPCC::ICC_A);
3343
3344 thisMBB->addSuccessor(mainMBB);
3345 thisMBB->addSuccessor(restoreMBB);
3346
3347
3348 // mainMBB:
3349 MIB = BuildMI(mainMBB, DL, TII->get(SP::ORrr))
3350 .addReg(mainDstReg, RegState::Define)
3351 .addReg(SP::G0)
3352 .addReg(SP::G0);
3353 MIB = BuildMI(mainMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3354
3355 mainMBB->addSuccessor(sinkMBB);
3356
3357
3358 // restoreMBB:
3359 MIB = BuildMI(restoreMBB, DL, TII->get(SP::ORri))
3360 .addReg(restoreDstReg, RegState::Define)
3361 .addReg(SP::G0)
3362 .addImm(1);
3363 //MIB = BuildMI(restoreMBB, DL, TII->get(SP::BCOND)).addMBB(sinkMBB).addImm(SPCC::ICC_A);
3364 restoreMBB->addSuccessor(sinkMBB);
3365
3366 // sinkMBB:
3367 MIB = BuildMI(*sinkMBB, sinkMBB->begin(), DL,
3368 TII->get(SP::PHI), DstReg)
3369 .addReg(mainDstReg).addMBB(mainMBB)
3370 .addReg(restoreDstReg).addMBB(restoreMBB);
3371
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003372 MI.eraseFromParent();
Chris Dewhurst69fa1922016-05-04 09:33:30 +00003373 return sinkMBB;
3374}
3375
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003376//===----------------------------------------------------------------------===//
3377// Sparc Inline Assembly Support
3378//===----------------------------------------------------------------------===//
3379
3380/// getConstraintType - Given a constraint letter, return the type of
3381/// constraint it is for this target.
3382SparcTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003383SparcTargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003384 if (Constraint.size() == 1) {
3385 switch (Constraint[0]) {
James Y Knight2cc9da92016-08-12 14:48:09 +00003386 default: break;
James Y Knightd4e1b002017-05-12 15:59:10 +00003387 case 'r':
3388 case 'f':
3389 case 'e':
3390 return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003391 case 'I': // SIMM13
3392 return C_Other;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003393 }
3394 }
3395
3396 return TargetLowering::getConstraintType(Constraint);
3397}
3398
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003399TargetLowering::ConstraintWeight SparcTargetLowering::
3400getSingleConstraintMatchWeight(AsmOperandInfo &info,
3401 const char *constraint) const {
3402 ConstraintWeight weight = CW_Invalid;
3403 Value *CallOperandVal = info.CallOperandVal;
3404 // If we don't have a value, we can't do a match,
3405 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003406 if (!CallOperandVal)
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003407 return CW_Default;
3408
3409 // Look at the constraint type.
3410 switch (*constraint) {
3411 default:
3412 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3413 break;
3414 case 'I': // SIMM13
3415 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3416 if (isInt<13>(C->getSExtValue()))
3417 weight = CW_Constant;
3418 }
3419 break;
3420 }
3421 return weight;
3422}
3423
3424/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3425/// vector. If it is invalid, don't add anything to Ops.
3426void SparcTargetLowering::
3427LowerAsmOperandForConstraint(SDValue Op,
3428 std::string &Constraint,
3429 std::vector<SDValue> &Ops,
3430 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003431 SDValue Result(nullptr, 0);
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003432
3433 // Only support length 1 constraints for now.
3434 if (Constraint.length() > 1)
3435 return;
3436
3437 char ConstraintLetter = Constraint[0];
3438 switch (ConstraintLetter) {
3439 default: break;
3440 case 'I':
3441 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3442 if (isInt<13>(C->getSExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003443 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
3444 Op.getValueType());
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003445 break;
3446 }
3447 return;
3448 }
3449 }
3450
3451 if (Result.getNode()) {
3452 Ops.push_back(Result);
3453 return;
3454 }
3455 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3456}
3457
Eric Christopher11e4df72015-02-26 22:38:43 +00003458std::pair<unsigned, const TargetRegisterClass *>
3459SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003460 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003461 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003462 if (Constraint.size() == 1) {
3463 switch (Constraint[0]) {
3464 case 'r':
James Y Knight3994be82015-08-10 19:11:39 +00003465 if (VT == MVT::v2i32)
3466 return std::make_pair(0U, &SP::IntPairRegClass);
3467 else
3468 return std::make_pair(0U, &SP::IntRegsRegClass);
James Y Knightd4e1b002017-05-12 15:59:10 +00003469 case 'f':
3470 if (VT == MVT::f32)
3471 return std::make_pair(0U, &SP::FPRegsRegClass);
3472 else if (VT == MVT::f64)
3473 return std::make_pair(0U, &SP::LowDFPRegsRegClass);
3474 else if (VT == MVT::f128)
3475 return std::make_pair(0U, &SP::LowQFPRegsRegClass);
3476 llvm_unreachable("Unknown ValueType for f-register-type!");
3477 break;
3478 case 'e':
3479 if (VT == MVT::f32)
3480 return std::make_pair(0U, &SP::FPRegsRegClass);
3481 else if (VT == MVT::f64)
3482 return std::make_pair(0U, &SP::DFPRegsRegClass);
3483 else if (VT == MVT::f128)
3484 return std::make_pair(0U, &SP::QFPRegsRegClass);
3485 llvm_unreachable("Unknown ValueType for e-register-type!");
3486 break;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003487 }
James Y Knight3994be82015-08-10 19:11:39 +00003488 } else if (!Constraint.empty() && Constraint.size() <= 5
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003489 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3490 // constraint = '{r<d>}'
3491 // Remove the braces from around the name.
3492 StringRef name(Constraint.data()+1, Constraint.size()-2);
3493 // Handle register aliases:
3494 // r0-r7 -> g0-g7
3495 // r8-r15 -> o0-o7
3496 // r16-r23 -> l0-l7
3497 // r24-r31 -> i0-i7
3498 uint64_t intVal = 0;
3499 if (name.substr(0, 1).equals("r")
3500 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3501 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3502 char regType = regTypes[intVal/8];
3503 char regIdx = '0' + (intVal % 8);
3504 char tmp[] = { '{', regType, regIdx, '}', 0 };
3505 std::string newConstraint = std::string(tmp);
Eric Christopher11e4df72015-02-26 22:38:43 +00003506 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3507 VT);
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003508 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003509 }
3510
Eric Christopher11e4df72015-02-26 22:38:43 +00003511 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003512}
3513
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003514bool
3515SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3516 // The Sparc target isn't yet aware of offsets.
3517 return false;
3518}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003519
3520void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3521 SmallVectorImpl<SDValue>& Results,
3522 SelectionDAG &DAG) const {
3523
3524 SDLoc dl(N);
3525
3526 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3527
3528 switch (N->getOpcode()) {
3529 default:
3530 llvm_unreachable("Do not know how to custom type legalize this operation!");
3531
3532 case ISD::FP_TO_SINT:
3533 case ISD::FP_TO_UINT:
3534 // Custom lower only if it involves f128 or i64.
3535 if (N->getOperand(0).getValueType() != MVT::f128
3536 || N->getValueType(0) != MVT::i64)
3537 return;
3538 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3539 ? RTLIB::FPTOSINT_F128_I64
3540 : RTLIB::FPTOUINT_F128_I64);
3541
3542 Results.push_back(LowerF128Op(SDValue(N, 0),
3543 DAG,
3544 getLibcallName(libCall),
3545 1));
3546 return;
3547
3548 case ISD::SINT_TO_FP:
3549 case ISD::UINT_TO_FP:
3550 // Custom lower only if it involves f128 or i64.
3551 if (N->getValueType(0) != MVT::f128
3552 || N->getOperand(0).getValueType() != MVT::i64)
3553 return;
3554
3555 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3556 ? RTLIB::SINTTOFP_I64_F128
3557 : RTLIB::UINTTOFP_I64_F128);
3558
3559 Results.push_back(LowerF128Op(SDValue(N, 0),
3560 DAG,
3561 getLibcallName(libCall),
3562 1));
3563 return;
James Y Knight3994be82015-08-10 19:11:39 +00003564 case ISD::LOAD: {
3565 LoadSDNode *Ld = cast<LoadSDNode>(N);
3566 // Custom handling only for i64: turn i64 load into a v2i32 load,
3567 // and a bitcast.
3568 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3569 return;
3570
3571 SDLoc dl(N);
3572 SDValue LoadRes = DAG.getExtLoad(
Justin Lebar9c375812016-07-15 18:27:10 +00003573 Ld->getExtensionType(), dl, MVT::v2i32, Ld->getChain(),
3574 Ld->getBasePtr(), Ld->getPointerInfo(), MVT::v2i32, Ld->getAlignment(),
3575 Ld->getMemOperand()->getFlags(), Ld->getAAInfo());
James Y Knight3994be82015-08-10 19:11:39 +00003576
3577 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3578 Results.push_back(Res);
3579 Results.push_back(LoadRes.getValue(1));
3580 return;
3581 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003582 }
3583}
Marcin Koscielnicki33571e22016-04-26 10:37:14 +00003584
3585// Override to enable LOAD_STACK_GUARD lowering on Linux.
3586bool SparcTargetLowering::useLoadStackGuardNode() const {
3587 if (!Subtarget->isTargetLinux())
3588 return TargetLowering::useLoadStackGuardNode();
3589 return true;
3590}
3591
3592// Override to disable global variable loading on Linux.
3593void SparcTargetLowering::insertSSPDeclarations(Module &M) const {
3594 if (!Subtarget->isTargetLinux())
3595 return TargetLowering::insertSSPDeclarations(M);
3596}