blob: 4a2ba00ac6c21f83a85bccd18963c49fe3bd03fa [file] [log] [blame]
Chris Lattner0a1762e2008-03-17 03:21:36 +00001//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner0a1762e2008-03-17 03:21:36 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the interfaces that Sparc uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SparcISelLowering.h"
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +000015#include "MCTargetDesc/SparcMCExpr.h"
Dan Gohman31ae5862010-04-17 14:41:14 +000016#include "SparcMachineFunctionInfo.h"
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +000017#include "SparcRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "SparcTargetMachine.h"
Venkatraman Govindarajufd5c1f92014-01-29 04:51:35 +000019#include "SparcTargetObjectFile.h"
Eric Christopherb6c190d2019-04-12 06:16:33 +000020#include "llvm/ADT/StringExtras.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
Matthias Braunf1caa282017-12-15 22:22:58 +0000267 if (MF.getFunction().hasStructRetAttr()) {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000268 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
Daniel Sanders0c476112019-08-15 19:22:08 +0000420 Register VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000421 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 }
Daniel Sanders0c476112019-08-15 19:22:08 +0000448 Register VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000449 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
Matthias Braunf1caa282017-12-15 22:22:58 +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) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000555 Register VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
Chris Lattner49b269d2008-03-17 05:41:48 +0000556 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,
Peter Collingbourne081ffe22017-07-26 19:15:29 +0000695 ImmutableCallSite CS) {
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000696 if (CS)
Peter Collingbourne081ffe22017-07-26 19:15:29 +0000697 return CS.hasFnAttr(Attribute::ReturnsTwice);
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000698
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)) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000704 const Function &Fn = DAG.getMachineFunction().getFunction();
705 const Module *M = Fn.getParent();
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000706 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;
Daniel Cederman0c597ca2018-08-17 10:40:00 +0000783 unsigned SRetArgSize = 0;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000784 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000785 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000786 i != e;
787 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000788 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000789 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000790
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000791 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
792
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000793 // Use local copy if it is a byval arg.
Chris Dewhurst53bde952016-06-01 08:48:56 +0000794 if (Flags.isByVal()) {
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000795 Arg = ByValArgs[byvalArgIdx++];
Chris Dewhurst53bde952016-06-01 08:48:56 +0000796 if (!Arg) {
797 continue;
798 }
799 }
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000800
Chris Lattner7d4152b2008-03-17 06:58:37 +0000801 // Promote the value if needed.
802 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000803 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000804 case CCValAssign::Full: break;
805 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000806 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000807 break;
808 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000809 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000810 break;
811 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000812 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
813 break;
814 case CCValAssign::BCvt:
815 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000816 break;
817 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000818
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000819 if (Flags.isSRet()) {
820 assert(VA.needsCustom());
821 // store SRet argument in %sp+64
822 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000823 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000824 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000825 MemOpChains.push_back(
826 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000827 hasStructRetAttr = true;
Daniel Cederman0c597ca2018-08-17 10:40:00 +0000828 // sret only allowed on first argument
829 assert(Outs[realArgIdx].OrigArgIndex == 0);
830 PointerType *Ty = cast<PointerType>(CLI.getArgs()[0].Ty);
831 Type *ElementTy = Ty->getElementType();
832 SRetArgSize = DAG.getDataLayout().getTypeAllocSize(ElementTy);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000833 continue;
834 }
835
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000836 if (VA.needsCustom()) {
James Y Knight3994be82015-08-10 19:11:39 +0000837 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000838
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000839 if (VA.isMemLoc()) {
840 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000841 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000842 if (Offset % 8 == 0) {
843 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000844 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000845 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000846 MemOpChains.push_back(
847 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000848 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000849 }
850 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000851
James Y Knight3994be82015-08-10 19:11:39 +0000852 if (VA.getLocVT() == MVT::f64) {
853 // Move from the float value from float registers into the
854 // integer registers.
Daniel Cederman92dadc02018-08-27 07:14:53 +0000855 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Arg))
856 Arg = bitcastConstantFPToInt(C, dl, DAG);
857 else
858 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
James Y Knight3994be82015-08-10 19:11:39 +0000859 }
860
861 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
862 Arg,
863 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
864 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
865 Arg,
866 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000867
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000868 if (VA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000869 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000870 assert(i+1 != e);
871 CCValAssign &NextVA = ArgLocs[++i];
872 if (NextVA.isRegLoc()) {
James Y Knight3994be82015-08-10 19:11:39 +0000873 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000874 } else {
James Y Knight3994be82015-08-10 19:11:39 +0000875 // Store the second part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000876 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
877 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000878 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000879 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000880 MemOpChains.push_back(
881 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000882 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000883 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000884 unsigned Offset = VA.getLocMemOffset() + StackOffset;
James Y Knight3994be82015-08-10 19:11:39 +0000885 // Store the first part.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000886 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000887 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000888 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000889 MemOpChains.push_back(
890 DAG.getStore(Chain, dl, Part0, PtrOff, MachinePointerInfo()));
James Y Knight3994be82015-08-10 19:11:39 +0000891 // Store the second part.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000892 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000893 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000894 MemOpChains.push_back(
895 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000896 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000897 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000898 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000899
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000900 // Arguments that can be passed on register must be kept at
901 // RegsToPass vector
902 if (VA.isRegLoc()) {
903 if (VA.getLocVT() != MVT::f32) {
904 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
905 continue;
906 }
907 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
908 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
909 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000910 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000911
912 assert(VA.isMemLoc());
913
914 // Create a store off the stack pointer for this argument.
915 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000916 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
917 dl);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000918 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +0000919 MemOpChains.push_back(
920 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000921 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000922
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000923
Chris Lattner49b269d2008-03-17 05:41:48 +0000924 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000925 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000926 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000927
928 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000929 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000930 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000931 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000932 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000933 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000934 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000935 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000936 InFlag = Chain.getValue(1);
937 }
938
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000939 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000940
Chris Lattner49b269d2008-03-17 05:41:48 +0000941 // If the callee is a GlobalAddress node (quite common, every direct call is)
942 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000943 // Likewise ExternalSymbol -> TargetExternalSymbol.
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +0000944 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Chris Lattner49b269d2008-03-17 05:41:48 +0000945 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000946 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000947 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000948 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000949
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000950 // Returns a chain & a flag for retval copy to use
951 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
952 SmallVector<SDValue, 8> Ops;
953 Ops.push_back(Chain);
954 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000955 if (hasStructRetAttr)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000956 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000957 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
958 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
959 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000960
961 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +0000962 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000963 const uint32_t *Mask =
964 ((hasReturnsTwice)
965 ? TRI->getRTCallPreservedMask(CallConv)
966 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000967 assert(Mask && "Missing call preserved mask for calling convention");
968 Ops.push_back(DAG.getRegisterMask(Mask));
969
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000970 if (InFlag.getNode())
971 Ops.push_back(InFlag);
972
Craig Topper48d114b2014-04-26 18:35:24 +0000973 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
Chris Lattner49b269d2008-03-17 05:41:48 +0000974 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000975
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000976 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, dl, true),
977 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000978 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000979
Chris Lattnerdb26db22008-03-17 06:01:07 +0000980 // Assign locations to each value returned by this call.
981 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000982 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
983 *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000984
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000985 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000986
Chris Lattnerdb26db22008-03-17 06:01:07 +0000987 // Copy all of the result registers out of their specified physreg.
988 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Nirav Dave29938542016-02-26 18:55:22 +0000989 if (RVLocs[i].getLocVT() == MVT::v2i32) {
990 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
991 SDValue Lo = DAG.getCopyFromReg(
992 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InFlag);
993 Chain = Lo.getValue(1);
994 InFlag = Lo.getValue(2);
995 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
996 DAG.getConstant(0, dl, MVT::i32));
997 SDValue Hi = DAG.getCopyFromReg(
998 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InFlag);
999 Chain = Hi.getValue(1);
1000 InFlag = Hi.getValue(2);
1001 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
1002 DAG.getConstant(1, dl, MVT::i32));
1003 InVals.push_back(Vec);
1004 } else {
1005 Chain =
1006 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1007 RVLocs[i].getValVT(), InFlag)
1008 .getValue(1);
1009 InFlag = Chain.getValue(2);
1010 InVals.push_back(Chain.getValue(0));
1011 }
Chris Lattner49b269d2008-03-17 05:41:48 +00001012 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001013
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001014 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +00001015}
1016
Chris Dewhurstad741172016-05-20 10:21:01 +00001017// FIXME? Maybe this could be a TableGen attribute on some registers and
1018// this table could be generated automatically from RegInfo.
Matt Arsenaultf24ac132019-10-01 01:44:39 +00001019Register SparcTargetLowering::getRegisterByName(const char* RegName, EVT VT,
1020 const MachineFunction &MF) const {
1021 Register Reg = StringSwitch<unsigned>(RegName)
Chris Dewhurstad741172016-05-20 10:21:01 +00001022 .Case("i0", SP::I0).Case("i1", SP::I1).Case("i2", SP::I2).Case("i3", SP::I3)
1023 .Case("i4", SP::I4).Case("i5", SP::I5).Case("i6", SP::I6).Case("i7", SP::I7)
1024 .Case("o0", SP::O0).Case("o1", SP::O1).Case("o2", SP::O2).Case("o3", SP::O3)
1025 .Case("o4", SP::O4).Case("o5", SP::O5).Case("o6", SP::O6).Case("o7", SP::O7)
1026 .Case("l0", SP::L0).Case("l1", SP::L1).Case("l2", SP::L2).Case("l3", SP::L3)
1027 .Case("l4", SP::L4).Case("l5", SP::L5).Case("l6", SP::L6).Case("l7", SP::L7)
1028 .Case("g0", SP::G0).Case("g1", SP::G1).Case("g2", SP::G2).Case("g3", SP::G3)
1029 .Case("g4", SP::G4).Case("g5", SP::G5).Case("g6", SP::G6).Case("g7", SP::G7)
1030 .Default(0);
1031
1032 if (Reg)
1033 return Reg;
1034
1035 report_fatal_error("Invalid register name global variable");
1036}
1037
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001038// Fixup floating point arguments in the ... part of a varargs call.
1039//
1040// The SPARC v9 ABI requires that floating point arguments are treated the same
1041// as integers when calling a varargs function. This does not apply to the
1042// fixed arguments that are part of the function's prototype.
1043//
1044// This function post-processes a CCValAssign array created by
1045// AnalyzeCallOperands().
1046static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1047 ArrayRef<ISD::OutputArg> Outs) {
1048 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1049 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001050 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001051 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1052 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001053 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001054 continue;
1055 // The fixed arguments to a varargs function still go in FP registers.
1056 if (Outs[VA.getValNo()].IsFixed)
1057 continue;
1058
1059 // This floating point argument should be reassigned.
1060 CCValAssign NewVA;
1061
1062 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001063 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1064 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1065 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001066 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1067
1068 if (Offset < 6*8) {
1069 // This argument should go in %i0-%i5.
1070 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001071 if (ValTy == MVT::f64)
1072 // Full register, just bitconvert into i64.
1073 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1074 IReg, MVT::i64, CCValAssign::BCvt);
1075 else {
1076 assert(ValTy == MVT::f128 && "Unexpected type!");
1077 // Full register, just bitconvert into i128 -- We will lower this into
1078 // two i64s in LowerCall_64.
1079 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1080 IReg, MVT::i128, CCValAssign::BCvt);
1081 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001082 } else {
1083 // This needs to go to memory, we're out of integer registers.
1084 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1085 Offset, VA.getLocVT(), VA.getLocInfo());
1086 }
1087 ArgLocs[i] = NewVA;
1088 }
1089}
1090
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001091// Lower a call for the 64-bit ABI.
1092SDValue
1093SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1094 SmallVectorImpl<SDValue> &InVals) const {
1095 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001096 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001097 SDValue Chain = CLI.Chain;
Mehdi Amini44ede332015-07-09 02:09:04 +00001098 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001099
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001100 // Sparc target does not yet support tail call optimization.
1101 CLI.IsTailCall = false;
1102
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001103 // Analyze operands of the call, assigning locations to each operand.
1104 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001105 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1106 *DAG.getContext());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001107 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1108
1109 // Get the size of the outgoing arguments stack space requirement.
1110 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001111 // Called functions expect 6 argument words to exist in the stack frame, used
1112 // or not.
1113 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001114
1115 // Keep stack frames 16-byte aligned.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001116 ArgsSize = alignTo(ArgsSize, 16);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001117
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001118 // Varargs calls require special treatment.
1119 if (CLI.IsVarArg)
1120 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1121
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001122 // Adjust the stack pointer to make room for the arguments.
1123 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1124 // with more than 6 arguments.
Serge Pavlovd526b132017-05-09 13:35:13 +00001125 Chain = DAG.getCALLSEQ_START(Chain, ArgsSize, 0, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001126
1127 // Collect the set of registers to pass to the function and their values.
1128 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1129 // instruction.
1130 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1131
1132 // Collect chains from all the memory opeations that copy arguments to the
1133 // stack. They must follow the stack pointer adjustment above and precede the
1134 // call instruction itself.
1135 SmallVector<SDValue, 8> MemOpChains;
1136
1137 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1138 const CCValAssign &VA = ArgLocs[i];
1139 SDValue Arg = CLI.OutVals[i];
1140
1141 // Promote the value if needed.
1142 switch (VA.getLocInfo()) {
1143 default:
1144 llvm_unreachable("Unknown location info!");
1145 case CCValAssign::Full:
1146 break;
1147 case CCValAssign::SExt:
1148 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1149 break;
1150 case CCValAssign::ZExt:
1151 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1152 break;
1153 case CCValAssign::AExt:
1154 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1155 break;
1156 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001157 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1158 // SPARC does not support i128 natively. Lower it into two i64, see below.
1159 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1160 || VA.getLocVT() != MVT::i128)
1161 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001162 break;
1163 }
1164
1165 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001166 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1167 && VA.getLocVT() == MVT::i128) {
Simon Pilgrimfd8bf982016-11-18 10:52:12 +00001168 // Store and reload into the integer register reg and reg+1.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001169 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1170 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
Mehdi Amini44ede332015-07-09 02:09:04 +00001171 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001172 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001173 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001174 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001175 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001176
1177 // Store to %sp+BIAS+128+Offset
Justin Lebar9c375812016-07-15 18:27:10 +00001178 SDValue Store =
1179 DAG.getStore(Chain, DL, Arg, HiPtrOff, MachinePointerInfo());
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001180 // Load into Reg and Reg+1
Justin Lebar9c375812016-07-15 18:27:10 +00001181 SDValue Hi64 =
1182 DAG.getLoad(MVT::i64, DL, Store, HiPtrOff, MachinePointerInfo());
1183 SDValue Lo64 =
1184 DAG.getLoad(MVT::i64, DL, Store, LoPtrOff, MachinePointerInfo());
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001185 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1186 Hi64));
1187 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1188 Lo64));
1189 continue;
1190 }
1191
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001192 // The custom bit on an i32 return value indicates that it should be
1193 // passed in the high bits of the register.
1194 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1195 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001196 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001197
1198 // The next value may go in the low bits of the same register.
1199 // Handle both at once.
1200 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1201 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1202 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1203 CLI.OutVals[i+1]);
1204 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1205 // Skip the next value, it's already done.
1206 ++i;
1207 }
1208 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001209 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001210 continue;
1211 }
1212
1213 assert(VA.isMemLoc());
1214
1215 // Create a store off the stack pointer for this argument.
Mehdi Amini44ede332015-07-09 02:09:04 +00001216 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001217 // The argument area starts at %fp+BIAS+128 in the callee frame,
1218 // %sp+BIAS+128 in ours.
1219 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1220 Subtarget->getStackPointerBias() +
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001221 128, DL);
Mehdi Amini44ede332015-07-09 02:09:04 +00001222 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
Justin Lebar9c375812016-07-15 18:27:10 +00001223 MemOpChains.push_back(
1224 DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001225 }
1226
1227 // Emit all stores, make sure they occur before the call.
1228 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001229 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001230
1231 // Build a sequence of CopyToReg nodes glued together with token chain and
1232 // glue operands which copy the outgoing args into registers. The InGlue is
1233 // necessary since all emitted instructions must be stuck together in order
1234 // to pass the live physical registers.
1235 SDValue InGlue;
1236 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1237 Chain = DAG.getCopyToReg(Chain, DL,
1238 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1239 InGlue = Chain.getValue(1);
1240 }
1241
1242 // If the callee is a GlobalAddress node (quite common, every direct call is)
1243 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1244 // Likewise ExternalSymbol -> TargetExternalSymbol.
1245 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001246 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Rafael Espindolacbfeb9f2016-06-27 18:37:44 +00001247 unsigned TF = isPositionIndependent() ? SparcMCExpr::VK_Sparc_WPLT30 : 0;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001248 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001249 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001250 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Mehdi Amini44ede332015-07-09 02:09:04 +00001251 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001252
1253 // Build the operands for the call instruction itself.
1254 SmallVector<SDValue, 8> Ops;
1255 Ops.push_back(Chain);
1256 Ops.push_back(Callee);
1257 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1258 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1259 RegsToPass[i].second.getValueType()));
1260
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001261 // Add a register mask operand representing the call-preserved registers.
Eric Christopherf5e94062015-01-30 23:46:43 +00001262 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001263 const uint32_t *Mask =
1264 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
Eric Christopher9deb75d2015-03-11 22:42:13 +00001265 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1266 CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001267 assert(Mask && "Missing call preserved mask for calling convention");
1268 Ops.push_back(DAG.getRegisterMask(Mask));
1269
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001270 // Make sure the CopyToReg nodes are glued to the call instruction which
1271 // consumes the registers.
1272 if (InGlue.getNode())
1273 Ops.push_back(InGlue);
1274
1275 // Now the call itself.
1276 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001277 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001278 InGlue = Chain.getValue(1);
1279
1280 // Revert the stack pointer immediately after the call.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001281 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
1282 DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001283 InGlue = Chain.getValue(1);
1284
1285 // Now extract the return values. This is more or less the same as
1286 // LowerFormalArguments_64.
1287
1288 // Assign locations to each value returned by this call.
1289 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001290 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1291 *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001292
1293 // Set inreg flag manually for codegen generated library calls that
1294 // return float.
Peter Collingbourne081ffe22017-07-26 19:15:29 +00001295 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && !CLI.CS)
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001296 CLI.Ins[0].Flags.setInReg();
1297
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001298 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001299
1300 // Copy all of the result registers out of their specified physreg.
1301 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1302 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001303 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001304
1305 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1306 // reside in the same register in the high and low bits. Reuse the
1307 // CopyFromReg previous node to avoid duplicate copies.
1308 SDValue RV;
1309 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1310 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1311 RV = Chain.getValue(0);
1312
1313 // But usually we'll create a new CopyFromReg for a different register.
1314 if (!RV.getNode()) {
1315 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1316 Chain = RV.getValue(1);
1317 InGlue = Chain.getValue(2);
1318 }
1319
1320 // Get the high bits for i32 struct elements.
1321 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1322 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001323 DAG.getConstant(32, DL, MVT::i32));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001324
1325 // The callee promoted the return value, so insert an Assert?ext SDNode so
1326 // we won't promote the value again in this function.
1327 switch (VA.getLocInfo()) {
1328 case CCValAssign::SExt:
1329 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1330 DAG.getValueType(VA.getValVT()));
1331 break;
1332 case CCValAssign::ZExt:
1333 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1334 DAG.getValueType(VA.getValVT()));
1335 break;
1336 default:
1337 break;
1338 }
1339
1340 // Truncate the register down to the return value type.
1341 if (VA.isExtInLoc())
1342 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1343
1344 InVals.push_back(RV);
1345 }
1346
1347 return Chain;
1348}
1349
Chris Lattner0a1762e2008-03-17 03:21:36 +00001350//===----------------------------------------------------------------------===//
1351// TargetLowering Implementation
1352//===----------------------------------------------------------------------===//
1353
James Y Knight7306cd42016-03-29 19:09:54 +00001354TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1355 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1356 AI->getType()->getPrimitiveSizeInBits() == 32)
1357 return AtomicExpansionKind::None; // Uses xchg instruction
1358
1359 return AtomicExpansionKind::CmpXChg;
1360}
1361
Chris Lattner0a1762e2008-03-17 03:21:36 +00001362/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1363/// condition.
1364static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1365 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001366 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001367 case ISD::SETEQ: return SPCC::ICC_E;
1368 case ISD::SETNE: return SPCC::ICC_NE;
1369 case ISD::SETLT: return SPCC::ICC_L;
1370 case ISD::SETGT: return SPCC::ICC_G;
1371 case ISD::SETLE: return SPCC::ICC_LE;
1372 case ISD::SETGE: return SPCC::ICC_GE;
1373 case ISD::SETULT: return SPCC::ICC_CS;
1374 case ISD::SETULE: return SPCC::ICC_LEU;
1375 case ISD::SETUGT: return SPCC::ICC_GU;
1376 case ISD::SETUGE: return SPCC::ICC_CC;
1377 }
1378}
1379
1380/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1381/// FCC condition.
1382static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1383 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001384 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001385 case ISD::SETEQ:
1386 case ISD::SETOEQ: return SPCC::FCC_E;
1387 case ISD::SETNE:
1388 case ISD::SETUNE: return SPCC::FCC_NE;
1389 case ISD::SETLT:
1390 case ISD::SETOLT: return SPCC::FCC_L;
1391 case ISD::SETGT:
1392 case ISD::SETOGT: return SPCC::FCC_G;
1393 case ISD::SETLE:
1394 case ISD::SETOLE: return SPCC::FCC_LE;
1395 case ISD::SETGE:
1396 case ISD::SETOGE: return SPCC::FCC_GE;
1397 case ISD::SETULT: return SPCC::FCC_UL;
1398 case ISD::SETULE: return SPCC::FCC_ULE;
1399 case ISD::SETUGT: return SPCC::FCC_UG;
1400 case ISD::SETUGE: return SPCC::FCC_UGE;
1401 case ISD::SETUO: return SPCC::FCC_U;
1402 case ISD::SETO: return SPCC::FCC_O;
1403 case ISD::SETONE: return SPCC::FCC_LG;
1404 case ISD::SETUEQ: return SPCC::FCC_UE;
1405 }
1406}
1407
James Y Knightef31eaf2016-05-03 14:57:18 +00001408SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
Eric Christopherf5e94062015-01-30 23:46:43 +00001409 const SparcSubtarget &STI)
1410 : TargetLowering(TM), Subtarget(&STI) {
Matt Arsenault41e5ac42018-03-14 00:36:23 +00001411 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize(0));
Mehdi Amini44ede332015-07-09 02:09:04 +00001412
James Y Knightd966fb62015-08-19 14:47:04 +00001413 // Instructions which use registers as conditionals examine all the
1414 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1415 // matters much whether it's ZeroOrOneBooleanContent, or
1416 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1417 // former.
1418 setBooleanContents(ZeroOrOneBooleanContent);
1419 setBooleanVectorContents(ZeroOrOneBooleanContent);
1420
Chris Lattner0a1762e2008-03-17 03:21:36 +00001421 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001422 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
Chris Dewhurst68388a02016-05-18 09:14:13 +00001423 if (!Subtarget->useSoftFloat()) {
1424 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1425 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1426 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1427 }
James Y Knight3994be82015-08-10 19:11:39 +00001428 if (Subtarget->is64Bit()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001429 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00001430 } else {
1431 // On 32bit sparc, we define a double-register 32bit register
1432 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1433 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1434
1435 // ...but almost all operations must be expanded, so set that as
1436 // the default.
1437 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1438 setOperationAction(Op, MVT::v2i32, Expand);
1439 }
1440 // Truncating/extending stores/loads are also not supported.
Graham Hunter1a9195d2019-09-17 10:19:23 +00001441 for (MVT VT : MVT::integer_fixedlen_vector_valuetypes()) {
James Y Knight3994be82015-08-10 19:11:39 +00001442 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1443 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1444 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1445
1446 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1447 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1448 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1449
1450 setTruncStoreAction(VT, MVT::v2i32, Expand);
1451 setTruncStoreAction(MVT::v2i32, VT, Expand);
1452 }
1453 // However, load and store *are* legal.
1454 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1455 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1456 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1457 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1458
1459 // And we need to promote i64 loads/stores into vector load/store
1460 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1461 setOperationAction(ISD::STORE, MVT::i64, Custom);
1462
1463 // Sadly, this doesn't work:
1464 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1465 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1466 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001467
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001468 // Turn FP extload into load/fpextend
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001469 for (MVT VT : MVT::fp_valuetypes()) {
1470 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1471 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1472 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001473
Chris Lattner0a1762e2008-03-17 03:21:36 +00001474 // Sparc doesn't have i1 sign extending load
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001475 for (MVT VT : MVT::integer_valuetypes())
1476 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001477
Chris Lattner0a1762e2008-03-17 03:21:36 +00001478 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001479 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001480 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1481 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001482
1483 // Custom legalize GlobalAddress nodes into LO/HI parts.
Mehdi Amini26d48132015-07-24 16:04:22 +00001484 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1485 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1486 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1487 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001488
Chris Lattner0a1762e2008-03-17 03:21:36 +00001489 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001490 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1491 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1492 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001493
1494 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001495 setOperationAction(ISD::UREM, MVT::i32, Expand);
1496 setOperationAction(ISD::SREM, MVT::i32, Expand);
1497 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1498 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001499
Roman Divacky2262cfa2013-10-31 19:22:33 +00001500 // ... nor does SparcV9.
1501 if (Subtarget->is64Bit()) {
1502 setOperationAction(ISD::UREM, MVT::i64, Expand);
1503 setOperationAction(ISD::SREM, MVT::i64, Expand);
1504 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1505 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1506 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001507
1508 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001509 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1510 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001511 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1512 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001513
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001514 // Custom Expand fp<->uint
1515 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1516 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001517 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1518 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001519
Richard Trieuefef0322017-12-11 22:25:04 +00001520 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1521 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
1522
Chris Lattner0a1762e2008-03-17 03:21:36 +00001523 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001524 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1525 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1526 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001527 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1528
Owen Anderson9f944592009-08-11 20:47:22 +00001529 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1530 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1531 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001532 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001533
Chris Lattner0a1762e2008-03-17 03:21:36 +00001534 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001535 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1536 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1537 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1538 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1539 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1540 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001541 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001542
Owen Anderson9f944592009-08-11 20:47:22 +00001543 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1544 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1545 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001546 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001547
Amaury Sechet84674112018-06-01 13:21:33 +00001548 setOperationAction(ISD::ADDC, MVT::i32, Custom);
1549 setOperationAction(ISD::ADDE, MVT::i32, Custom);
1550 setOperationAction(ISD::SUBC, MVT::i32, Custom);
1551 setOperationAction(ISD::SUBE, MVT::i32, Custom);
1552
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001553 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001554 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1555 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1556 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1557 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Richard Trieuefef0322017-12-11 22:25:04 +00001558 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1559 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001560 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1561 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001562 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001563 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001564
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001565 setOperationAction(ISD::CTPOP, MVT::i64,
1566 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001567 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001568 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001569 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001570 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1571 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001572 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001573 }
1574
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001575 // ATOMICs.
Chris Dewhurst7d8412f2016-05-16 11:02:00 +00001576 // Atomics are supported on SparcV9. 32-bit atomics are also
1577 // supported by some Leon SparcV8 variants. Otherwise, atomics
1578 // are unsupported.
James Y Knight2cc9da92016-08-12 14:48:09 +00001579 if (Subtarget->isV9())
1580 setMaxAtomicSizeInBitsSupported(64);
1581 else if (Subtarget->hasLeonCasa())
Chris Dewhurst92cac932016-09-06 14:41:09 +00001582 setMaxAtomicSizeInBitsSupported(32);
James Y Knight19f6cce2016-04-12 20:18:48 +00001583 else
1584 setMaxAtomicSizeInBitsSupported(0);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001585
James Y Knight148a6462016-06-17 18:11:48 +00001586 setMinCmpXchgSizeInBits(32);
1587
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001588 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001589
1590 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1591
1592 // Custom Lower Atomic LOAD/STORE
1593 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1594 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1595
1596 if (Subtarget->is64Bit()) {
1597 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001598 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001599 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1600 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1601 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001602
James Y Knight6ef32bf2016-09-02 20:29:11 +00001603 if (!Subtarget->is64Bit()) {
1604 // These libcalls are not available in 32-bit.
1605 setLibcallName(RTLIB::SHL_I128, nullptr);
1606 setLibcallName(RTLIB::SRL_I128, nullptr);
1607 setLibcallName(RTLIB::SRA_I128, nullptr);
1608 }
1609
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001610 if (!Subtarget->isV9()) {
1611 // SparcV8 does not have FNEGD and FABSD.
1612 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1613 setOperationAction(ISD::FABS, MVT::f64, Custom);
1614 }
1615
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001616 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1617 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1618 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1619 setOperationAction(ISD::FREM , MVT::f128, Expand);
1620 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001621 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1622 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001623 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001624 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001625 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001626 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1627 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001628 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001629 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001630 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001631 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1632 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1633 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1634 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1635 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001636 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001637 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1638 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001639 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001640 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1641 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001642
Owen Anderson9f944592009-08-11 20:47:22 +00001643 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1644 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1645 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001646
James Y Knightb0a473a2016-10-05 20:54:17 +00001647 // Expands to [SU]MUL_LOHI.
1648 setOperationAction(ISD::MULHU, MVT::i32, Expand);
1649 setOperationAction(ISD::MULHS, MVT::i32, Expand);
1650 setOperationAction(ISD::MUL, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001651
James Y Knightdda87ca2017-07-18 19:08:38 +00001652 if (Subtarget->useSoftMulDiv()) {
1653 // .umul works for both signed and unsigned
1654 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
1655 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1656 setLibcallName(RTLIB::MUL_I32, ".umul");
1657
1658 setOperationAction(ISD::SDIV, MVT::i32, Expand);
1659 setLibcallName(RTLIB::SDIV_I32, ".div");
1660
1661 setOperationAction(ISD::UDIV, MVT::i32, Expand);
1662 setLibcallName(RTLIB::UDIV_I32, ".udiv");
Daniel Cedermanab09da72018-07-16 12:22:08 +00001663
1664 setLibcallName(RTLIB::SREM_I32, ".rem");
1665 setLibcallName(RTLIB::UREM_I32, ".urem");
James Y Knightdda87ca2017-07-18 19:08:38 +00001666 }
1667
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001668 if (Subtarget->is64Bit()) {
1669 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1670 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1671 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1672 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001673
1674 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1675 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001676
1677 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1678 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1679 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001680 }
1681
Chris Lattner0a1762e2008-03-17 03:21:36 +00001682 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001683 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001684 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001685 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001686
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001687 setOperationAction(ISD::TRAP , MVT::Other, Legal);
Daniel Cederman68765752018-07-16 12:16:53 +00001688 setOperationAction(ISD::DEBUGTRAP , MVT::Other, Legal);
Benjamin Kramerfacca1f2014-02-23 21:43:52 +00001689
Chris Lattner0a1762e2008-03-17 03:21:36 +00001690 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001691 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1692 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1693 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1694 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1695 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001696
Chris Lattner0a1762e2008-03-17 03:21:36 +00001697 setStackPointerRegisterToSaveRestore(SP::O6);
1698
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001699 setOperationAction(ISD::CTPOP, MVT::i32,
1700 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001701
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001702 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1703 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1704 setOperationAction(ISD::STORE, MVT::f128, Legal);
1705 } else {
1706 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1707 setOperationAction(ISD::STORE, MVT::f128, Custom);
1708 }
1709
1710 if (Subtarget->hasHardQuad()) {
1711 setOperationAction(ISD::FADD, MVT::f128, Legal);
1712 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1713 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1714 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1715 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1716 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1717 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1718 if (Subtarget->isV9()) {
1719 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1720 setOperationAction(ISD::FABS, MVT::f128, Legal);
1721 } else {
1722 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1723 setOperationAction(ISD::FABS, MVT::f128, Custom);
1724 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001725
1726 if (!Subtarget->is64Bit()) {
1727 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1728 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1729 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1730 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1731 }
1732
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001733 } else {
1734 // Custom legalize f128 operations.
1735
1736 setOperationAction(ISD::FADD, MVT::f128, Custom);
1737 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1738 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1739 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1740 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1741 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1742 setOperationAction(ISD::FABS, MVT::f128, Custom);
1743
1744 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1745 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1746 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1747
1748 // Setup Runtime library names.
Chris Dewhurst68388a02016-05-18 09:14:13 +00001749 if (Subtarget->is64Bit() && !Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001750 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1751 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1752 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1753 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1754 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1755 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001756 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001757 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001758 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001759 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1760 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1761 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1762 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001763 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1764 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1765 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1766 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
Chris Dewhurst68388a02016-05-18 09:14:13 +00001767 } else if (!Subtarget->useSoftFloat()) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001768 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1769 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1770 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1771 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1772 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1773 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001774 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001775 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001776 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001777 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1778 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1779 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1780 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001781 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1782 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1783 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1784 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1785 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001786 }
1787
Chris Dewhurst0c1e0022016-06-19 11:03:28 +00001788 if (Subtarget->fixAllFDIVSQRT()) {
1789 // Promote FDIVS and FSQRTS to FDIVD and FSQRTD instructions instead as
1790 // the former instructions generate errata on LEON processors.
1791 setOperationAction(ISD::FDIV, MVT::f32, Promote);
1792 setOperationAction(ISD::FSQRT, MVT::f32, Promote);
1793 }
1794
James Y Knightbb76d482017-07-20 20:09:11 +00001795 if (Subtarget->hasNoFMULS()) {
Chris Dewhurst0c1e0022016-06-19 11:03:28 +00001796 setOperationAction(ISD::FMUL, MVT::f32, Promote);
1797 }
1798
Daniel Cederman92dadc02018-08-27 07:14:53 +00001799 // Custom combine bitcast between f64 and v2i32
1800 if (!Subtarget->is64Bit())
1801 setTargetDAGCombine(ISD::BITCAST);
1802
Daniel Cederman27395962018-08-27 11:11:47 +00001803 if (Subtarget->hasLeonCycleCounter())
1804 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
1805
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00001806 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1807
Guillaume Chatelet18f805a2019-09-27 12:54:21 +00001808 setMinFunctionAlignment(Align(4));
Eli Friedman2518f832011-05-06 20:34:06 +00001809
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001810 computeRegisterProperties(Subtarget->getRegisterInfo());
Chris Lattner0a1762e2008-03-17 03:21:36 +00001811}
1812
Chris Dewhurst68388a02016-05-18 09:14:13 +00001813bool SparcTargetLowering::useSoftFloat() const {
1814 return Subtarget->useSoftFloat();
1815}
1816
Chris Lattner0a1762e2008-03-17 03:21:36 +00001817const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001818 switch ((SPISD::NodeType)Opcode) {
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001819 case SPISD::FIRST_NUMBER: break;
1820 case SPISD::CMPICC: return "SPISD::CMPICC";
1821 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1822 case SPISD::BRICC: return "SPISD::BRICC";
1823 case SPISD::BRXCC: return "SPISD::BRXCC";
1824 case SPISD::BRFCC: return "SPISD::BRFCC";
1825 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1826 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1827 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001828 case SPISD::Hi: return "SPISD::Hi";
1829 case SPISD::Lo: return "SPISD::Lo";
1830 case SPISD::FTOI: return "SPISD::FTOI";
1831 case SPISD::ITOF: return "SPISD::ITOF";
1832 case SPISD::FTOX: return "SPISD::FTOX";
1833 case SPISD::XTOF: return "SPISD::XTOF";
1834 case SPISD::CALL: return "SPISD::CALL";
1835 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001836 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Chris Dewhurst69fa1922016-05-04 09:33:30 +00001837 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1838 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1839 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1840 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001841 }
Matthias Braund04893f2015-05-07 21:33:59 +00001842 return nullptr;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001843}
1844
Mehdi Amini44ede332015-07-09 02:09:04 +00001845EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
1846 EVT VT) const {
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001847 if (!VT.isVector())
1848 return MVT::i32;
1849 return VT.changeVectorElementTypeToInteger();
1850}
1851
Chris Lattner0a1762e2008-03-17 03:21:36 +00001852/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1853/// be zero. Op is expected to be a target specific node. Used by DAG
1854/// combiner.
Jay Foada0653a32014-05-14 21:14:37 +00001855void SparcTargetLowering::computeKnownBitsForTargetNode
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001856 (const SDValue Op,
Craig Topperd0af7e82017-04-28 05:31:46 +00001857 KnownBits &Known,
Simon Pilgrim37b536e2017-03-31 11:24:16 +00001858 const APInt &DemandedElts,
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001859 const SelectionDAG &DAG,
1860 unsigned Depth) const {
Craig Topperd0af7e82017-04-28 05:31:46 +00001861 KnownBits Known2;
Craig Topperf0aeee02017-05-05 17:36:09 +00001862 Known.resetAll();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001863
Chris Lattner0a1762e2008-03-17 03:21:36 +00001864 switch (Op.getOpcode()) {
1865 default: break;
1866 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001867 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001868 case SPISD::SELECT_FCC:
Simon Pilgrim7787a022018-12-21 15:32:36 +00001869 Known = DAG.computeKnownBits(Op.getOperand(1), Depth + 1);
1870 Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001871
Chris Lattner0a1762e2008-03-17 03:21:36 +00001872 // Only known if known in both the LHS and RHS.
Craig Topperd0af7e82017-04-28 05:31:46 +00001873 Known.One &= Known2.One;
1874 Known.Zero &= Known2.Zero;
Chris Lattner0a1762e2008-03-17 03:21:36 +00001875 break;
1876 }
1877}
1878
Chris Lattner0a1762e2008-03-17 03:21:36 +00001879// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1880// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001881static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001882 ISD::CondCode CC, unsigned &SPCC) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00001883 if (isNullConstant(RHS) &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001884 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001885 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1886 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001887 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1888 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1889 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00001890 isOneConstant(LHS.getOperand(0)) &&
1891 isNullConstant(LHS.getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001892 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001893 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001894 LHS = CMPCC.getOperand(0);
1895 RHS = CMPCC.getOperand(1);
1896 }
1897}
1898
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001899// Convert to a target node and set target flags.
1900SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1901 SelectionDAG &DAG) const {
1902 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1903 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001904 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001905 GA->getValueType(0),
1906 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001907
1908 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1909 return DAG.getTargetConstantPool(CP->getConstVal(),
1910 CP->getValueType(0),
1911 CP->getAlignment(),
1912 CP->getOffset(), TF);
1913
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001914 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1915 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1916 Op.getValueType(),
1917 0,
1918 TF);
1919
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001920 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1921 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1922 ES->getValueType(0), TF);
1923
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001924 llvm_unreachable("Unhandled address SDNode");
1925}
1926
1927// Split Op into high and low parts according to HiTF and LoTF.
1928// Return an ADD node combining the parts.
1929SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1930 unsigned HiTF, unsigned LoTF,
1931 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001932 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001933 EVT VT = Op.getValueType();
1934 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1935 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1936 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1937}
1938
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001939// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1940// or ExternalSymbol SDNode.
1941SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001942 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001943 EVT VT = getPointerTy(DAG.getDataLayout());
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001944
Rafael Espindola428b3e62016-06-27 19:15:08 +00001945 // Handle PIC mode first. SPARC needs a got load for every variable!
1946 if (isPositionIndependent()) {
Daniel Cederman33f67a22018-06-11 05:50:08 +00001947 const Module *M = DAG.getMachineFunction().getFunction().getParent();
1948 PICLevel::Level picLevel = M->getPICLevel();
1949 SDValue Idx;
1950
1951 if (picLevel == PICLevel::SmallPIC) {
1952 // This is the pic13 code model, the GOT is known to be smaller than 8KiB.
1953 Idx = DAG.getNode(SPISD::Lo, DL, Op.getValueType(),
1954 withTargetFlags(Op, SparcMCExpr::VK_Sparc_GOT13, DAG));
1955 } else {
1956 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
1957 Idx = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1958 SparcMCExpr::VK_Sparc_GOT10, DAG);
1959 }
1960
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001961 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
Daniel Cederman33f67a22018-06-11 05:50:08 +00001962 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, Idx);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001963 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1964 // function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001965 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
1966 MFI.setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001967 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001968 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001969 }
1970
1971 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001972 switch(getTargetMachine().getCodeModel()) {
1973 default:
1974 llvm_unreachable("Unsupported absolute code model");
1975 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001976 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001977 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1978 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001979 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001980 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001981 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
1982 SparcMCExpr::VK_Sparc_M44, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001983 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001984 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001985 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1986 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1987 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001988 case CodeModel::Large: {
1989 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001990 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
1991 SparcMCExpr::VK_Sparc_HM, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001992 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001993 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1994 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001995 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1996 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001997 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001998}
1999
Wesley Peck527da1b2010-11-23 03:31:01 +00002000SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002001 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002002 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002003}
2004
Chris Lattner840c7002009-09-15 17:46:24 +00002005SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002006 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00002007 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002008}
2009
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002010SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2011 SelectionDAG &DAG) const {
2012 return makeAddress(Op, DAG);
2013}
2014
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002015SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2016 SelectionDAG &DAG) const {
2017
2018 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh9f9e4682018-02-28 17:48:55 +00002019 if (DAG.getTarget().useEmulatedTLS())
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002020 return LowerToTLSEmulatedModel(GA, DAG);
2021
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002022 SDLoc DL(GA);
2023 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002024 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002025
2026 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2027
2028 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002029 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
2030 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
2031 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
2032 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
2033 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
2034 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
2035 unsigned addTF = ((model == TLSModel::GeneralDynamic)
2036 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
2037 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
2038 unsigned callTF = ((model == TLSModel::GeneralDynamic)
2039 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
2040 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002041
2042 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2043 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2044 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2045 withTargetFlags(Op, addTF, DAG));
2046
2047 SDValue Chain = DAG.getEntryNode();
2048 SDValue InFlag;
2049
Serge Pavlovd526b132017-05-09 13:35:13 +00002050 Chain = DAG.getCALLSEQ_START(Chain, 1, 0, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002051 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
2052 InFlag = Chain.getValue(1);
2053 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2054 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2055
2056 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopher9deb75d2015-03-11 22:42:13 +00002057 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2058 DAG.getMachineFunction(), CallingConv::C);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002059 assert(Mask && "Missing call preserved mask for calling convention");
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +00002060 SDValue Ops[] = {Chain,
2061 Callee,
2062 Symbol,
2063 DAG.getRegister(SP::O0, PtrVT),
2064 DAG.getRegisterMask(Mask),
2065 InFlag};
Craig Topper48d114b2014-04-26 18:35:24 +00002066 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002067 InFlag = Chain.getValue(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002068 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, DL, true),
2069 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002070 InFlag = Chain.getValue(1);
2071 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
2072
2073 if (model != TLSModel::LocalDynamic)
2074 return Ret;
2075
2076 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002077 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002078 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002079 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002080 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2081 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002082 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002083 }
2084
2085 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002086 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
2087 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002088
2089 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2090
2091 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2092 // function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00002093 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2094 MFI.setHasCalls(true);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002095
2096 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002097 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
2098 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002099 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2100 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2101 DL, PtrVT, Ptr,
2102 withTargetFlags(Op, ldTF, DAG));
2103 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2104 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002105 withTargetFlags(Op,
2106 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002107 }
2108
2109 assert(model == TLSModel::LocalExec);
2110 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002111 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002112 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00002113 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002114 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2115
2116 return DAG.getNode(ISD::ADD, DL, PtrVT,
2117 DAG.getRegister(SP::G7, PtrVT), Offset);
2118}
2119
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002120SDValue SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain,
2121 ArgListTy &Args, SDValue Arg,
2122 const SDLoc &DL,
2123 SelectionDAG &DAG) const {
Matthias Braun941a7052016-07-28 18:40:00 +00002124 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002125 EVT ArgVT = Arg.getValueType();
2126 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2127
2128 ArgListEntry Entry;
2129 Entry.Node = Arg;
2130 Entry.Ty = ArgTy;
2131
2132 if (ArgTy->isFP128Ty()) {
2133 // Create a stack object and pass the pointer to the library function.
Matthias Braun941a7052016-07-28 18:40:00 +00002134 int FI = MFI.CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002135 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00002136 Chain = DAG.getStore(Chain, DL, Entry.Node, FIPtr, MachinePointerInfo(),
2137 /* Alignment = */ 8);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002138
2139 Entry.Node = FIPtr;
2140 Entry.Ty = PointerType::getUnqual(ArgTy);
2141 }
2142 Args.push_back(Entry);
2143 return Chain;
2144}
2145
2146SDValue
2147SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2148 const char *LibFuncName,
2149 unsigned numArgs) const {
2150
2151 ArgListTy Args;
2152
Matthias Braun941a7052016-07-28 18:40:00 +00002153 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +00002154 auto PtrVT = getPointerTy(DAG.getDataLayout());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002155
Mehdi Amini44ede332015-07-09 02:09:04 +00002156 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002157 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2158 Type *RetTyABI = RetTy;
2159 SDValue Chain = DAG.getEntryNode();
2160 SDValue RetPtr;
2161
2162 if (RetTy->isFP128Ty()) {
2163 // Create a Stack Object to receive the return value of type f128.
2164 ArgListEntry Entry;
Matthias Braun941a7052016-07-28 18:40:00 +00002165 int RetFI = MFI.CreateStackObject(16, 8, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002166 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002167 Entry.Node = RetPtr;
2168 Entry.Ty = PointerType::getUnqual(RetTy);
2169 if (!Subtarget->is64Bit())
Nirav Dave6de2c772017-03-18 00:43:57 +00002170 Entry.IsSRet = true;
2171 Entry.IsReturned = false;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002172 Args.push_back(Entry);
2173 RetTyABI = Type::getVoidTy(*DAG.getContext());
2174 }
2175
2176 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2177 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2178 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2179 }
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002180 TargetLowering::CallLoweringInfo CLI(DAG);
2181 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002182 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002183
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002184 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2185
2186 // chain is in second result.
2187 if (RetTyABI == RetTy)
2188 return CallInfo.first;
2189
2190 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2191
2192 Chain = CallInfo.second;
2193
2194 // Load RetPtr to get the return value.
Justin Lebar9c375812016-07-15 18:27:10 +00002195 return DAG.getLoad(Op.getValueType(), SDLoc(Op), Chain, RetPtr,
2196 MachinePointerInfo(), /* Alignment = */ 8);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002197}
2198
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002199SDValue SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2200 unsigned &SPCC, const SDLoc &DL,
2201 SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002202
Craig Topper062a2ba2014-04-25 05:30:21 +00002203 const char *LibCall = nullptr;
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002204 bool is64Bit = Subtarget->is64Bit();
2205 switch(SPCC) {
2206 default: llvm_unreachable("Unhandled conditional code!");
2207 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2208 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2209 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2210 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2211 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2212 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2213 case SPCC::FCC_UL :
2214 case SPCC::FCC_ULE:
2215 case SPCC::FCC_UG :
2216 case SPCC::FCC_UGE:
2217 case SPCC::FCC_U :
2218 case SPCC::FCC_O :
2219 case SPCC::FCC_LG :
2220 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2221 }
2222
Mehdi Amini44ede332015-07-09 02:09:04 +00002223 auto PtrVT = getPointerTy(DAG.getDataLayout());
2224 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002225 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2226 ArgListTy Args;
2227 SDValue Chain = DAG.getEntryNode();
2228 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2229 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2230
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002231 TargetLowering::CallLoweringInfo CLI(DAG);
2232 CLI.setDebugLoc(DL).setChain(Chain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002233 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args));
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002234
2235 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2236
2237 // result is in first, and chain is in second result.
2238 SDValue Result = CallInfo.first;
2239
2240 switch(SPCC) {
2241 default: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002242 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002243 SPCC = SPCC::ICC_NE;
2244 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2245 }
2246 case SPCC::FCC_UL : {
Craig Topper1b7b4b42019-09-20 16:49:51 +00002247 SDValue Mask = DAG.getConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002248 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002249 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002250 SPCC = SPCC::ICC_NE;
2251 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2252 }
2253 case SPCC::FCC_ULE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002254 SDValue RHS = DAG.getTargetConstant(2, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002255 SPCC = SPCC::ICC_NE;
2256 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2257 }
2258 case SPCC::FCC_UG : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002259 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002260 SPCC = SPCC::ICC_G;
2261 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2262 }
2263 case SPCC::FCC_UGE: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002264 SDValue RHS = DAG.getTargetConstant(1, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002265 SPCC = SPCC::ICC_NE;
2266 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2267 }
2268
2269 case SPCC::FCC_U : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002270 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002271 SPCC = SPCC::ICC_E;
2272 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2273 }
2274 case SPCC::FCC_O : {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002275 SDValue RHS = DAG.getTargetConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002276 SPCC = SPCC::ICC_NE;
2277 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2278 }
2279 case SPCC::FCC_LG : {
Craig Topper1b7b4b42019-09-20 16:49:51 +00002280 SDValue Mask = DAG.getConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002281 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002282 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002283 SPCC = SPCC::ICC_NE;
2284 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2285 }
2286 case SPCC::FCC_UE : {
Craig Topper1b7b4b42019-09-20 16:49:51 +00002287 SDValue Mask = DAG.getConstant(3, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002288 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002289 SDValue RHS = DAG.getTargetConstant(0, DL, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002290 SPCC = SPCC::ICC_E;
2291 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2292 }
2293 }
2294}
2295
2296static SDValue
2297LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2298 const SparcTargetLowering &TLI) {
2299
2300 if (Op.getOperand(0).getValueType() == MVT::f64)
2301 return TLI.LowerF128Op(Op, DAG,
2302 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2303
2304 if (Op.getOperand(0).getValueType() == MVT::f32)
2305 return TLI.LowerF128Op(Op, DAG,
2306 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2307
2308 llvm_unreachable("fpextend with non-float operand!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002309 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002310}
2311
2312static SDValue
2313LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2314 const SparcTargetLowering &TLI) {
2315 // FP_ROUND on f64 and f32 are legal.
2316 if (Op.getOperand(0).getValueType() != MVT::f128)
2317 return Op;
2318
2319 if (Op.getValueType() == MVT::f64)
2320 return TLI.LowerF128Op(Op, DAG,
2321 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2322 if (Op.getValueType() == MVT::f32)
2323 return TLI.LowerF128Op(Op, DAG,
2324 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2325
2326 llvm_unreachable("fpround to non-float!");
Craig Topper062a2ba2014-04-25 05:30:21 +00002327 return SDValue();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002328}
2329
2330static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2331 const SparcTargetLowering &TLI,
2332 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002333 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002334 EVT VT = Op.getValueType();
2335 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002336
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002337 // Expand f128 operations to fp128 abi calls.
2338 if (Op.getOperand(0).getValueType() == MVT::f128
2339 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2340 const char *libName = TLI.getLibcallName(VT == MVT::i32
2341 ? RTLIB::FPTOSINT_F128_I32
2342 : RTLIB::FPTOSINT_F128_I64);
2343 return TLI.LowerF128Op(Op, DAG, libName, 1);
2344 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002345
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002346 // Expand if the resulting type is illegal.
2347 if (!TLI.isTypeLegal(VT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002348 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002349
2350 // Otherwise, Convert the fp value to integer in an FP register.
2351 if (VT == MVT::i32)
2352 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2353 else
2354 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2355
2356 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002357}
2358
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002359static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2360 const SparcTargetLowering &TLI,
2361 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002362 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002363 EVT OpVT = Op.getOperand(0).getValueType();
2364 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2365
2366 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2367
2368 // Expand f128 operations to fp128 ABI calls.
2369 if (Op.getValueType() == MVT::f128
2370 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2371 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2372 ? RTLIB::SINTTOFP_I32_F128
2373 : RTLIB::SINTTOFP_I64_F128);
2374 return TLI.LowerF128Op(Op, DAG, libName, 1);
2375 }
2376
2377 // Expand if the operand type is illegal.
2378 if (!TLI.isTypeLegal(OpVT))
Craig Topper062a2ba2014-04-25 05:30:21 +00002379 return SDValue();
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002380
2381 // Otherwise, Convert the int value to FP in an FP register.
2382 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2383 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2384 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002385}
2386
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002387static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2388 const SparcTargetLowering &TLI,
2389 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002390 SDLoc dl(Op);
2391 EVT VT = Op.getValueType();
2392
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002393 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002394 // quad floating point instructions and the resulting type is legal.
2395 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2396 (hasHardQuad && TLI.isTypeLegal(VT)))
Craig Topper062a2ba2014-04-25 05:30:21 +00002397 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002398
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002399 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002400
2401 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002402 TLI.getLibcallName(VT == MVT::i32
2403 ? RTLIB::FPTOUINT_F128_I32
2404 : RTLIB::FPTOUINT_F128_I64),
2405 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002406}
2407
Richard Trieuefef0322017-12-11 22:25:04 +00002408static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2409 const SparcTargetLowering &TLI,
2410 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002411 SDLoc dl(Op);
2412 EVT OpVT = Op.getOperand(0).getValueType();
2413 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2414
Richard Trieuefef0322017-12-11 22:25:04 +00002415 // Expand if it does not involve f128 or the target has support for
2416 // quad floating point instructions and the operand type is legal.
2417 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
2418 return SDValue();
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002419
Richard Trieuefef0322017-12-11 22:25:04 +00002420 return TLI.LowerF128Op(Op, DAG,
2421 TLI.getLibcallName(OpVT == MVT::i32
2422 ? RTLIB::UINTTOFP_I32_F128
2423 : RTLIB::UINTTOFP_I64_F128),
2424 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002425}
2426
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002427static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2428 const SparcTargetLowering &TLI,
2429 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002430 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002431 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002432 SDValue LHS = Op.getOperand(2);
2433 SDValue RHS = Op.getOperand(3);
2434 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002435 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002436 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002437
Chris Lattner0a1762e2008-03-17 03:21:36 +00002438 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2439 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2440 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002441
Chris Lattner0a1762e2008-03-17 03:21:36 +00002442 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002443 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002444 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002445 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002446 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002447 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2448 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002449 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002450 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2451 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2452 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2453 Opc = SPISD::BRICC;
2454 } else {
2455 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2456 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2457 Opc = SPISD::BRFCC;
2458 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002459 }
Owen Anderson9f944592009-08-11 20:47:22 +00002460 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002461 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002462}
2463
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002464static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2465 const SparcTargetLowering &TLI,
2466 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002467 SDValue LHS = Op.getOperand(0);
2468 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002469 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002470 SDValue TrueVal = Op.getOperand(2);
2471 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002472 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002473 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002474
Chris Lattner0a1762e2008-03-17 03:21:36 +00002475 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2476 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2477 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002478
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002479 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002480 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002481 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002482 Opc = LHS.getValueType() == MVT::i32 ?
2483 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002484 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2485 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002486 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2487 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2488 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2489 Opc = SPISD::SELECT_ICC;
2490 } else {
2491 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2492 Opc = SPISD::SELECT_FCC;
2493 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2494 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002495 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002496 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002497 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002498}
2499
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002500static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002501 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002502 MachineFunction &MF = DAG.getMachineFunction();
2503 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002504 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002505
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002506 // Need frame address to find the address of VarArgsFrameIndex.
Matthias Braun941a7052016-07-28 18:40:00 +00002507 MF.getFrameInfo().setFrameAddressIsTaken(true);
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002508
Chris Lattner0a1762e2008-03-17 03:21:36 +00002509 // vastart just stores the address of the VarArgsFrameIndex slot into the
2510 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002511 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002512 SDValue Offset =
Mehdi Amini44ede332015-07-09 02:09:04 +00002513 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2514 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002515 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002516 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00002517 MachinePointerInfo(SV));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002518}
2519
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002520static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002521 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002522 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002523 SDValue InChain = Node->getOperand(0);
2524 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002525 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002526 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002527 SDLoc DL(Node);
Justin Lebar9c375812016-07-15 18:27:10 +00002528 SDValue VAList =
2529 DAG.getLoad(PtrVT, DL, InChain, VAListPtr, MachinePointerInfo(SV));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002530 // Increment the pointer, VAList, to the next vaarg.
2531 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002532 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2533 DL));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002534 // Store the incremented VAList to the legalized pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00002535 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr, VAListPtr,
2536 MachinePointerInfo(SV));
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002537 // Load the actual argument out of the pointer VAList.
2538 // We can't count on greater alignment than the word size.
2539 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002540 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits()) / 8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002541}
2542
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002543static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002544 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002545 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2546 SDValue Size = Op.getOperand(1); // Legalize the size.
James Y Knight2e64b8b2016-10-25 22:13:28 +00002547 unsigned Align = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2548 unsigned StackAlign = Subtarget->getFrameLowering()->getStackAlignment();
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002549 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002550 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002551
James Y Knight2e64b8b2016-10-25 22:13:28 +00002552 // TODO: implement over-aligned alloca. (Note: also implies
2553 // supporting support for overaligned function frames + dynamic
2554 // allocations, at all, which currently isn't supported)
2555 if (Align > StackAlign) {
2556 const MachineFunction &MF = DAG.getMachineFunction();
2557 report_fatal_error("Function \"" + Twine(MF.getName()) + "\": "
2558 "over-aligned dynamic alloca not supported.");
2559 }
2560
2561 // The resultant pointer needs to be above the register spill area
2562 // at the bottom of the stack.
2563 unsigned regSpillArea;
2564 if (Subtarget->is64Bit()) {
2565 regSpillArea = 128;
2566 } else {
2567 // On Sparc32, the size of the spill area is 92. Unfortunately,
2568 // that's only 4-byte aligned, not 8-byte aligned (the stack
2569 // pointer is 8-byte aligned). So, if the user asked for an 8-byte
2570 // aligned dynamic allocation, we actually need to add 96 to the
2571 // bottom of the stack, instead of 92, to ensure 8-byte alignment.
2572
2573 // That also means adding 4 to the size of the allocation --
2574 // before applying the 8-byte rounding. Unfortunately, we the
2575 // value we get here has already had rounding applied. So, we need
2576 // to add 8, instead, wasting a bit more memory.
2577
2578 // Further, this only actually needs to be done if the required
2579 // alignment is > 4, but, we've lost that info by this point, too,
2580 // so we always apply it.
2581
2582 // (An alternative approach would be to always reserve 96 bytes
2583 // instead of the required 92, but then we'd waste 4 extra bytes
2584 // in every frame, not just those with dynamic stack allocations)
2585
2586 // TODO: modify code in SelectionDAGBuilder to make this less sad.
2587
2588 Size = DAG.getNode(ISD::ADD, dl, VT, Size,
2589 DAG.getConstant(8, dl, VT));
2590 regSpillArea = 96;
2591 }
2592
Chris Lattner0a1762e2008-03-17 03:21:36 +00002593 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002594 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2595 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002596 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002597
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002598 regSpillArea += Subtarget->getStackPointerBias();
2599
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002600 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002601 DAG.getConstant(regSpillArea, dl, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002602 SDValue Ops[2] = { NewVal, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002603 return DAG.getMergeValues(Ops, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002604}
2605
Chris Lattner0a1762e2008-03-17 03:21:36 +00002606
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002607static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002608 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002609 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002610 dl, MVT::Other, DAG.getEntryNode());
2611 return Chain;
2612}
2613
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002614static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
Daniel Cederman7d3e08f2018-08-17 09:18:31 +00002615 const SparcSubtarget *Subtarget,
2616 bool AlwaysFlush = false) {
Matthias Braun941a7052016-07-28 18:40:00 +00002617 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2618 MFI.setFrameAddressIsTaken(true);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002619
2620 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002621 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002622 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002623 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002624
2625 SDValue FrameAddr;
Daniel Cederman7d3e08f2018-08-17 09:18:31 +00002626 SDValue Chain;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002627
2628 // flush first to make sure the windowed registers' values are in stack
Daniel Cederman7d3e08f2018-08-17 09:18:31 +00002629 Chain = (depth || AlwaysFlush) ? getFLUSHW(Op, DAG) : DAG.getEntryNode();
2630
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002631 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2632
2633 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2634
2635 while (depth--) {
2636 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002637 DAG.getIntPtrConstant(Offset, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002638 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo());
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002639 }
2640 if (Subtarget->is64Bit())
2641 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002642 DAG.getIntPtrConstant(stackBias, dl));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002643 return FrameAddr;
2644}
2645
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002646
2647static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2648 const SparcSubtarget *Subtarget) {
2649
2650 uint64_t depth = Op.getConstantOperandVal(0);
2651
2652 return getFRAMEADDR(depth, Op, DAG, Subtarget);
James Y Knight2cc9da92016-08-12 14:48:09 +00002653
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002654}
2655
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002656static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002657 const SparcTargetLowering &TLI,
2658 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002659 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002660 MachineFrameInfo &MFI = MF.getFrameInfo();
2661 MFI.setReturnAddressIsTaken(true);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002662
Bill Wendling908bf812014-01-06 00:43:20 +00002663 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002664 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002665
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002666 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002667 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002668 uint64_t depth = Op.getConstantOperandVal(0);
2669
2670 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002671 if (depth == 0) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002672 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2673 unsigned RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002674 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002675 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002676 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002677
2678 // Need frame address to find return address of the caller.
Daniel Cederman7d3e08f2018-08-17 09:18:31 +00002679 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget, true);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002680
2681 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2682 SDValue Ptr = DAG.getNode(ISD::ADD,
2683 dl, VT,
2684 FrameAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002685 DAG.getIntPtrConstant(Offset, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002686 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr, MachinePointerInfo());
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002687
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002688 return RetAddr;
2689}
2690
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002691static SDValue LowerF64Op(SDValue SrcReg64, const SDLoc &dl, SelectionDAG &DAG,
2692 unsigned opcode) {
James Y Knight51208ea2016-04-25 22:54:09 +00002693 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002694 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002695
2696 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2697 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2698 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2699
James Y Knight51208ea2016-04-25 22:54:09 +00002700 // Note: in little-endian, the floating-point value is stored in the
2701 // registers are in the opposite order, so the subreg with the sign
2702 // bit is the highest-numbered (odd), rather than the
2703 // lowest-numbered (even).
2704
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002705 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2706 SrcReg64);
2707 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2708 SrcReg64);
2709
James Y Knight51208ea2016-04-25 22:54:09 +00002710 if (DAG.getDataLayout().isLittleEndian())
2711 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2712 else
2713 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002714
2715 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2716 dl, MVT::f64), 0);
2717 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2718 DstReg64, Hi32);
2719 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2720 DstReg64, Lo32);
2721 return DstReg64;
2722}
2723
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002724// Lower a f128 load into two f64 loads.
2725static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2726{
2727 SDLoc dl(Op);
2728 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002729 assert(LdNode && LdNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002730 && "Unexpected node type");
2731
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002732 unsigned alignment = LdNode->getAlignment();
2733 if (alignment > 8)
2734 alignment = 8;
2735
Justin Lebar9c375812016-07-15 18:27:10 +00002736 SDValue Hi64 =
2737 DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LdNode->getBasePtr(),
2738 LdNode->getPointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002739 EVT addrVT = LdNode->getBasePtr().getValueType();
2740 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2741 LdNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002742 DAG.getConstant(8, dl, addrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00002743 SDValue Lo64 = DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LoPtr,
2744 LdNode->getPointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002745
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002746 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2747 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002748
2749 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2750 dl, MVT::f128);
2751 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2752 MVT::f128,
2753 SDValue(InFP128, 0),
2754 Hi64,
2755 SubRegEven);
2756 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2757 MVT::f128,
2758 SDValue(InFP128, 0),
2759 Lo64,
2760 SubRegOdd);
2761 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2762 SDValue(Lo64.getNode(), 1) };
Craig Topper48d114b2014-04-26 18:35:24 +00002763 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002764 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
Craig Topper64941d92014-04-27 19:20:57 +00002765 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002766}
2767
James Y Knight3994be82015-08-10 19:11:39 +00002768static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2769{
2770 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2771
2772 EVT MemVT = LdNode->getMemoryVT();
2773 if (MemVT == MVT::f128)
2774 return LowerF128Load(Op, DAG);
2775
2776 return Op;
2777}
2778
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002779// Lower a f128 store into two f64 stores.
2780static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2781 SDLoc dl(Op);
2782 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
Sanjay Patel57195842016-03-14 17:28:46 +00002783 assert(StNode && StNode->getOffset().isUndef()
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002784 && "Unexpected node type");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002785 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2786 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002787
2788 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2789 dl,
2790 MVT::f64,
2791 StNode->getValue(),
2792 SubRegEven);
2793 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2794 dl,
2795 MVT::f64,
2796 StNode->getValue(),
2797 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002798
2799 unsigned alignment = StNode->getAlignment();
2800 if (alignment > 8)
2801 alignment = 8;
2802
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002803 SDValue OutChains[2];
Justin Lebar9c375812016-07-15 18:27:10 +00002804 OutChains[0] =
2805 DAG.getStore(StNode->getChain(), dl, SDValue(Hi64, 0),
2806 StNode->getBasePtr(), MachinePointerInfo(), alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002807 EVT addrVT = StNode->getBasePtr().getValueType();
2808 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2809 StNode->getBasePtr(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002810 DAG.getConstant(8, dl, addrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00002811 OutChains[1] = DAG.getStore(StNode->getChain(), dl, SDValue(Lo64, 0), LoPtr,
2812 MachinePointerInfo(), alignment);
Craig Topper48d114b2014-04-26 18:35:24 +00002813 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002814}
2815
James Y Knight3994be82015-08-10 19:11:39 +00002816static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
2817{
2818 SDLoc dl(Op);
2819 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
2820
2821 EVT MemVT = St->getMemoryVT();
2822 if (MemVT == MVT::f128)
2823 return LowerF128Store(Op, DAG);
2824
2825 if (MemVT == MVT::i64) {
2826 // Custom handling for i64 stores: turn it into a bitcast and a
2827 // v2i32 store.
2828 SDValue Val = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, St->getValue());
2829 SDValue Chain = DAG.getStore(
2830 St->getChain(), dl, Val, St->getBasePtr(), St->getPointerInfo(),
Douglas Katzman26cfb6a2016-07-21 23:28:54 +00002831 St->getAlignment(), St->getMemOperand()->getFlags(), St->getAAInfo());
James Y Knight3994be82015-08-10 19:11:39 +00002832 return Chain;
2833 }
2834
2835 return SDValue();
2836}
2837
Roman Divacky7a9c6542014-02-27 19:26:29 +00002838static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
Venkatraman Govindaraju3b6b0e42014-03-01 02:28:34 +00002839 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
2840 && "invalid opcode");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002841
James Y Knight51208ea2016-04-25 22:54:09 +00002842 SDLoc dl(Op);
2843
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002844 if (Op.getValueType() == MVT::f64)
James Y Knight51208ea2016-04-25 22:54:09 +00002845 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002846 if (Op.getValueType() != MVT::f128)
2847 return Op;
2848
Roman Divacky7a9c6542014-02-27 19:26:29 +00002849 // Lower fabs/fneg on f128 to fabs/fneg on f64
2850 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
James Y Knight51208ea2016-04-25 22:54:09 +00002851 // (As with LowerF64Op, on little-endian, we need to negate the odd
2852 // subreg)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002853
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002854 SDValue SrcReg128 = Op.getOperand(0);
2855 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2856 SrcReg128);
2857 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2858 SrcReg128);
James Y Knight51208ea2016-04-25 22:54:09 +00002859
2860 if (DAG.getDataLayout().isLittleEndian()) {
2861 if (isV9)
2862 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
2863 else
2864 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
2865 } else {
2866 if (isV9)
2867 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2868 else
2869 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
2870 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002871
2872 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2873 dl, MVT::f128), 0);
2874 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2875 DstReg128, Hi64);
2876 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2877 DstReg128, Lo64);
2878 return DstReg128;
2879}
2880
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002881static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002882
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002883 if (Op.getValueType() != MVT::i64)
2884 return Op;
2885
2886 SDLoc dl(Op);
2887 SDValue Src1 = Op.getOperand(0);
2888 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2889 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002890 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002891 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2892
2893 SDValue Src2 = Op.getOperand(1);
2894 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2895 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002896 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002897 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2898
2899
2900 bool hasChain = false;
2901 unsigned hiOpc = Op.getOpcode();
2902 switch (Op.getOpcode()) {
2903 default: llvm_unreachable("Invalid opcode");
2904 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2905 case ISD::ADDE: hasChain = true; break;
2906 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2907 case ISD::SUBE: hasChain = true; break;
2908 }
2909 SDValue Lo;
2910 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2911 if (hasChain) {
2912 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2913 Op.getOperand(2));
2914 } else {
2915 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2916 }
2917 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2918 SDValue Carry = Hi.getValue(1);
2919
2920 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2921 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2922 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002923 DAG.getConstant(32, dl, MVT::i64));
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002924
2925 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2926 SDValue Ops[2] = { Dst, Carry };
Craig Topper64941d92014-04-27 19:20:57 +00002927 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002928}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002929
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002930// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2931// in LegalizeDAG.cpp except the order of arguments to the library function.
2932static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2933 const SparcTargetLowering &TLI)
2934{
2935 unsigned opcode = Op.getOpcode();
2936 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2937
2938 bool isSigned = (opcode == ISD::SMULO);
2939 EVT VT = MVT::i64;
2940 EVT WideVT = MVT::i128;
2941 SDLoc dl(Op);
2942 SDValue LHS = Op.getOperand(0);
2943
2944 if (LHS.getValueType() != VT)
2945 return Op;
2946
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002947 SDValue ShiftAmt = DAG.getConstant(63, dl, VT);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002948
2949 SDValue RHS = Op.getOperand(1);
2950 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2951 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2952 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2953
Shiva Chen72a41e72019-08-22 04:59:43 +00002954 TargetLowering::MakeLibCallOptions CallOptions;
2955 CallOptions.setSExt(isSigned);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002956 SDValue MulResult = TLI.makeLibCall(DAG,
2957 RTLIB::MUL_I128, WideVT,
Shiva Chen72a41e72019-08-22 04:59:43 +00002958 Args, CallOptions, dl).first;
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002959 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002960 MulResult, DAG.getIntPtrConstant(0, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002961 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002962 MulResult, DAG.getIntPtrConstant(1, dl));
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002963 if (isSigned) {
2964 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2965 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2966 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002967 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, dl, VT),
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002968 ISD::SETNE);
2969 }
2970 // MulResult is a node with an illegal type. Because such things are not
Chandler Carruthee1a1fc2014-08-02 00:24:54 +00002971 // generally permitted during this phase of legalization, ensure that
2972 // nothing is left using the node. The above EXTRACT_ELEMENT nodes should have
2973 // been folded.
2974 assert(MulResult->use_empty() && "Illegally typed node still in use!");
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002975
2976 SDValue Ops[2] = { BottomHalf, TopHalf } ;
Craig Topper64941d92014-04-27 19:20:57 +00002977 return DAG.getMergeValues(Ops, dl);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002978}
2979
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002980static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00002981 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
2982 // Expand with a fence.
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002983 return SDValue();
JF Bastien800f87a2016-04-06 21:19:33 +00002984
2985 // Monotonic load/stores are legal.
2986 return Op;
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002987}
2988
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00002989SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2990 SelectionDAG &DAG) const {
2991 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2992 SDLoc dl(Op);
2993 switch (IntNo) {
2994 default: return SDValue(); // Don't custom lower most intrinsics.
2995 case Intrinsic::thread_pointer: {
2996 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2997 return DAG.getRegister(SP::G7, PtrVT);
2998 }
2999 }
3000}
3001
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003002SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00003003LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003004
3005 bool hasHardQuad = Subtarget->hasHardQuad();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003006 bool isV9 = Subtarget->isV9();
3007
Chris Lattner0a1762e2008-03-17 03:21:36 +00003008 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003009 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00003010
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00003011 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
3012 Subtarget);
3013 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
3014 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00003015 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003016 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00003017 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00003018 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003019 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
3020 hasHardQuad);
3021 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
3022 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00003023 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
3024 hasHardQuad);
Richard Trieuefef0322017-12-11 22:25:04 +00003025 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3026 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003027 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
3028 hasHardQuad);
3029 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
3030 hasHardQuad);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003031 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3032 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00003033 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00003034 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00003035
James Y Knight3994be82015-08-10 19:11:39 +00003036 case ISD::LOAD: return LowerLOAD(Op, DAG);
3037 case ISD::STORE: return LowerSTORE(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003038 case ISD::FADD: return LowerF128Op(Op, DAG,
3039 getLibcallName(RTLIB::ADD_F128), 2);
3040 case ISD::FSUB: return LowerF128Op(Op, DAG,
3041 getLibcallName(RTLIB::SUB_F128), 2);
3042 case ISD::FMUL: return LowerF128Op(Op, DAG,
3043 getLibcallName(RTLIB::MUL_F128), 2);
3044 case ISD::FDIV: return LowerF128Op(Op, DAG,
3045 getLibcallName(RTLIB::DIV_F128), 2);
3046 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3047 getLibcallName(RTLIB::SQRT_F128),1);
Roman Divacky7a9c6542014-02-27 19:26:29 +00003048 case ISD::FABS:
3049 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003050 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3051 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00003052 case ISD::ADDC:
3053 case ISD::ADDE:
3054 case ISD::SUBC:
3055 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00003056 case ISD::UMULO:
3057 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00003058 case ISD::ATOMIC_LOAD:
3059 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Marcin Koscielnickifafb4492016-04-26 10:37:01 +00003060 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003061 }
3062}
3063
Daniel Cederman92dadc02018-08-27 07:14:53 +00003064SDValue SparcTargetLowering::bitcastConstantFPToInt(ConstantFPSDNode *C,
3065 const SDLoc &DL,
3066 SelectionDAG &DAG) const {
3067 APInt V = C->getValueAPF().bitcastToAPInt();
3068 SDValue Lo = DAG.getConstant(V.zextOrTrunc(32), DL, MVT::i32);
3069 SDValue Hi = DAG.getConstant(V.lshr(32).zextOrTrunc(32), DL, MVT::i32);
3070 if (DAG.getDataLayout().isLittleEndian())
3071 std::swap(Lo, Hi);
3072 return DAG.getBuildVector(MVT::v2i32, DL, {Hi, Lo});
3073}
3074
3075SDValue SparcTargetLowering::PerformBITCASTCombine(SDNode *N,
3076 DAGCombinerInfo &DCI) const {
3077 SDLoc dl(N);
3078 SDValue Src = N->getOperand(0);
3079
3080 if (isa<ConstantFPSDNode>(Src) && N->getSimpleValueType(0) == MVT::v2i32 &&
3081 Src.getSimpleValueType() == MVT::f64)
3082 return bitcastConstantFPToInt(cast<ConstantFPSDNode>(Src), dl, DCI.DAG);
3083
3084 return SDValue();
3085}
3086
3087SDValue SparcTargetLowering::PerformDAGCombine(SDNode *N,
3088 DAGCombinerInfo &DCI) const {
3089 switch (N->getOpcode()) {
3090 default:
3091 break;
3092 case ISD::BITCAST:
3093 return PerformBITCASTCombine(N, DCI);
3094 }
3095 return SDValue();
3096}
3097
Chris Lattner0a1762e2008-03-17 03:21:36 +00003098MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003099SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00003100 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003101 switch (MI.getOpcode()) {
James Y Knight2cc9da92016-08-12 14:48:09 +00003102 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00003103 case SP::SELECT_CC_Int_ICC:
3104 case SP::SELECT_CC_FP_ICC:
3105 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003106 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003107 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00003108 case SP::SELECT_CC_Int_FCC:
3109 case SP::SELECT_CC_FP_FCC:
3110 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00003111 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003112 return expandSelectCC(MI, BB, SP::FBCOND);
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003113 }
3114}
3115
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003116MachineBasicBlock *
3117SparcTargetLowering::expandSelectCC(MachineInstr &MI, MachineBasicBlock *BB,
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003118 unsigned BROpcode) const {
Eric Christopherf5e94062015-01-30 23:46:43 +00003119 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003120 DebugLoc dl = MI.getDebugLoc();
3121 unsigned CC = (SPCC::CondCodes)MI.getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003122
Alex Bradburyc09d5612017-09-07 11:30:55 +00003123 // To "insert" a SELECT_CC instruction, we actually have to insert the
3124 // triangle control-flow pattern. The incoming instruction knows the
3125 // destination vreg to set, the condition code register to branch on, the
3126 // true/false values to select between, and the condition code for the branch.
3127 //
3128 // We produce the following control flow:
3129 // ThisMBB
3130 // | \
3131 // | IfFalseMBB
3132 // | /
3133 // SinkMBB
Chris Lattner0a1762e2008-03-17 03:21:36 +00003134 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smithc3f79882015-10-20 00:59:43 +00003135 MachineFunction::iterator It = ++BB->getIterator();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003136
Alex Bradburyc09d5612017-09-07 11:30:55 +00003137 MachineBasicBlock *ThisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003138 MachineFunction *F = BB->getParent();
Alex Bradburyc09d5612017-09-07 11:30:55 +00003139 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
3140 MachineBasicBlock *SinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3141 F->insert(It, IfFalseMBB);
3142 F->insert(It, SinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003143
Alex Bradburyc09d5612017-09-07 11:30:55 +00003144 // Transfer the remainder of ThisMBB and its successor edges to SinkMBB.
3145 SinkMBB->splice(SinkMBB->begin(), ThisMBB,
3146 std::next(MachineBasicBlock::iterator(MI)), ThisMBB->end());
3147 SinkMBB->transferSuccessorsAndUpdatePHIs(ThisMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003148
Alex Bradburyc09d5612017-09-07 11:30:55 +00003149 // Set the new successors for ThisMBB.
3150 ThisMBB->addSuccessor(IfFalseMBB);
3151 ThisMBB->addSuccessor(SinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00003152
Alex Bradburyc09d5612017-09-07 11:30:55 +00003153 BuildMI(ThisMBB, dl, TII.get(BROpcode))
3154 .addMBB(SinkMBB)
3155 .addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003156
Alex Bradburyc09d5612017-09-07 11:30:55 +00003157 // IfFalseMBB just falls through to SinkMBB.
3158 IfFalseMBB->addSuccessor(SinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003159
Alex Bradburyc09d5612017-09-07 11:30:55 +00003160 // %Result = phi [ %TrueValue, ThisMBB ], [ %FalseValue, IfFalseMBB ]
3161 BuildMI(*SinkMBB, SinkMBB->begin(), dl, TII.get(SP::PHI),
3162 MI.getOperand(0).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003163 .addReg(MI.getOperand(1).getReg())
Alex Bradburyc09d5612017-09-07 11:30:55 +00003164 .addMBB(ThisMBB)
3165 .addReg(MI.getOperand(2).getReg())
3166 .addMBB(IfFalseMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00003167
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003168 MI.eraseFromParent(); // The pseudo instruction is gone now.
Alex Bradburyc09d5612017-09-07 11:30:55 +00003169 return SinkMBB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00003170}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003171
3172//===----------------------------------------------------------------------===//
3173// Sparc Inline Assembly Support
3174//===----------------------------------------------------------------------===//
3175
3176/// getConstraintType - Given a constraint letter, return the type of
3177/// constraint it is for this target.
3178SparcTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003179SparcTargetLowering::getConstraintType(StringRef Constraint) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003180 if (Constraint.size() == 1) {
3181 switch (Constraint[0]) {
James Y Knight2cc9da92016-08-12 14:48:09 +00003182 default: break;
James Y Knightd4e1b002017-05-12 15:59:10 +00003183 case 'r':
3184 case 'f':
3185 case 'e':
3186 return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003187 case 'I': // SIMM13
Bill Wendling41a28472019-08-03 05:52:47 +00003188 return C_Immediate;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003189 }
3190 }
3191
3192 return TargetLowering::getConstraintType(Constraint);
3193}
3194
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003195TargetLowering::ConstraintWeight SparcTargetLowering::
3196getSingleConstraintMatchWeight(AsmOperandInfo &info,
3197 const char *constraint) const {
3198 ConstraintWeight weight = CW_Invalid;
3199 Value *CallOperandVal = info.CallOperandVal;
3200 // If we don't have a value, we can't do a match,
3201 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003202 if (!CallOperandVal)
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003203 return CW_Default;
3204
3205 // Look at the constraint type.
3206 switch (*constraint) {
3207 default:
3208 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3209 break;
3210 case 'I': // SIMM13
3211 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3212 if (isInt<13>(C->getSExtValue()))
3213 weight = CW_Constant;
3214 }
3215 break;
3216 }
3217 return weight;
3218}
3219
3220/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3221/// vector. If it is invalid, don't add anything to Ops.
3222void SparcTargetLowering::
3223LowerAsmOperandForConstraint(SDValue Op,
3224 std::string &Constraint,
3225 std::vector<SDValue> &Ops,
3226 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003227 SDValue Result(nullptr, 0);
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003228
3229 // Only support length 1 constraints for now.
3230 if (Constraint.length() > 1)
3231 return;
3232
3233 char ConstraintLetter = Constraint[0];
3234 switch (ConstraintLetter) {
3235 default: break;
3236 case 'I':
3237 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3238 if (isInt<13>(C->getSExtValue())) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003239 Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
3240 Op.getValueType());
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003241 break;
3242 }
3243 return;
3244 }
3245 }
3246
3247 if (Result.getNode()) {
3248 Ops.push_back(Result);
3249 return;
3250 }
3251 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3252}
3253
Eric Christopher11e4df72015-02-26 22:38:43 +00003254std::pair<unsigned, const TargetRegisterClass *>
3255SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003256 StringRef Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003257 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003258 if (Constraint.size() == 1) {
3259 switch (Constraint[0]) {
3260 case 'r':
Daniel Cederman959c8bf2018-07-18 10:05:30 +00003261 if (VT == MVT::v2i32)
James Y Knight3994be82015-08-10 19:11:39 +00003262 return std::make_pair(0U, &SP::IntPairRegClass);
Joerg Sonnenberger6e7cc492019-04-23 15:15:33 +00003263 else if (Subtarget->is64Bit())
3264 return std::make_pair(0U, &SP::I64RegsRegClass);
James Y Knight3994be82015-08-10 19:11:39 +00003265 else
3266 return std::make_pair(0U, &SP::IntRegsRegClass);
James Y Knightd4e1b002017-05-12 15:59:10 +00003267 case 'f':
Daniel Cedermanb5d28442018-12-13 15:13:29 +00003268 if (VT == MVT::f32 || VT == MVT::i32)
James Y Knightd4e1b002017-05-12 15:59:10 +00003269 return std::make_pair(0U, &SP::FPRegsRegClass);
Daniel Cedermanb5d28442018-12-13 15:13:29 +00003270 else if (VT == MVT::f64 || VT == MVT::i64)
James Y Knightd4e1b002017-05-12 15:59:10 +00003271 return std::make_pair(0U, &SP::LowDFPRegsRegClass);
3272 else if (VT == MVT::f128)
3273 return std::make_pair(0U, &SP::LowQFPRegsRegClass);
Daniel Cedermanb5d28442018-12-13 15:13:29 +00003274 // This will generate an error message
3275 return std::make_pair(0U, nullptr);
James Y Knightd4e1b002017-05-12 15:59:10 +00003276 case 'e':
Daniel Cedermanb5d28442018-12-13 15:13:29 +00003277 if (VT == MVT::f32 || VT == MVT::i32)
James Y Knightd4e1b002017-05-12 15:59:10 +00003278 return std::make_pair(0U, &SP::FPRegsRegClass);
Daniel Cedermanb5d28442018-12-13 15:13:29 +00003279 else if (VT == MVT::f64 || VT == MVT::i64 )
James Y Knightd4e1b002017-05-12 15:59:10 +00003280 return std::make_pair(0U, &SP::DFPRegsRegClass);
3281 else if (VT == MVT::f128)
3282 return std::make_pair(0U, &SP::QFPRegsRegClass);
Daniel Cedermanb5d28442018-12-13 15:13:29 +00003283 // This will generate an error message
3284 return std::make_pair(0U, nullptr);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003285 }
James Y Knight3994be82015-08-10 19:11:39 +00003286 } else if (!Constraint.empty() && Constraint.size() <= 5
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003287 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3288 // constraint = '{r<d>}'
3289 // Remove the braces from around the name.
3290 StringRef name(Constraint.data()+1, Constraint.size()-2);
3291 // Handle register aliases:
3292 // r0-r7 -> g0-g7
3293 // r8-r15 -> o0-o7
3294 // r16-r23 -> l0-l7
3295 // r24-r31 -> i0-i7
3296 uint64_t intVal = 0;
3297 if (name.substr(0, 1).equals("r")
3298 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3299 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3300 char regType = regTypes[intVal/8];
3301 char regIdx = '0' + (intVal % 8);
3302 char tmp[] = { '{', regType, regIdx, '}', 0 };
3303 std::string newConstraint = std::string(tmp);
Eric Christopher11e4df72015-02-26 22:38:43 +00003304 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3305 VT);
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003306 }
Daniel Cederman60e6ce42018-05-30 06:07:55 +00003307 if (name.substr(0, 1).equals("f") &&
3308 !name.substr(1).getAsInteger(10, intVal) && intVal <= 63) {
3309 std::string newConstraint;
3310
Daniel Cederman248dae82018-05-30 09:52:18 +00003311 if (VT == MVT::f32 || VT == MVT::Other) {
Daniel Cederman60e6ce42018-05-30 06:07:55 +00003312 newConstraint = "{f" + utostr(intVal) + "}";
3313 } else if (VT == MVT::f64 && (intVal % 2 == 0)) {
3314 newConstraint = "{d" + utostr(intVal / 2) + "}";
3315 } else if (VT == MVT::f128 && (intVal % 4 == 0)) {
3316 newConstraint = "{q" + utostr(intVal / 4) + "}";
3317 } else {
3318 return std::make_pair(0U, nullptr);
3319 }
3320 return TargetLowering::getRegForInlineAsmConstraint(TRI, newConstraint,
3321 VT);
3322 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003323 }
3324
Eric Christopher11e4df72015-02-26 22:38:43 +00003325 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003326}
3327
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003328bool
3329SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3330 // The Sparc target isn't yet aware of offsets.
3331 return false;
3332}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003333
3334void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3335 SmallVectorImpl<SDValue>& Results,
3336 SelectionDAG &DAG) const {
3337
3338 SDLoc dl(N);
3339
3340 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3341
3342 switch (N->getOpcode()) {
3343 default:
3344 llvm_unreachable("Do not know how to custom type legalize this operation!");
3345
3346 case ISD::FP_TO_SINT:
3347 case ISD::FP_TO_UINT:
3348 // Custom lower only if it involves f128 or i64.
3349 if (N->getOperand(0).getValueType() != MVT::f128
3350 || N->getValueType(0) != MVT::i64)
3351 return;
3352 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3353 ? RTLIB::FPTOSINT_F128_I64
3354 : RTLIB::FPTOUINT_F128_I64);
3355
3356 Results.push_back(LowerF128Op(SDValue(N, 0),
3357 DAG,
3358 getLibcallName(libCall),
3359 1));
3360 return;
Daniel Cederman27395962018-08-27 11:11:47 +00003361 case ISD::READCYCLECOUNTER: {
3362 assert(Subtarget->hasLeonCycleCounter());
3363 SDValue Lo = DAG.getCopyFromReg(N->getOperand(0), dl, SP::ASR23, MVT::i32);
3364 SDValue Hi = DAG.getCopyFromReg(Lo, dl, SP::G0, MVT::i32);
3365 SDValue Ops[] = { Lo, Hi };
3366 SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops);
3367 Results.push_back(Pair);
3368 Results.push_back(N->getOperand(0));
3369 return;
3370 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003371 case ISD::SINT_TO_FP:
3372 case ISD::UINT_TO_FP:
3373 // Custom lower only if it involves f128 or i64.
3374 if (N->getValueType(0) != MVT::f128
3375 || N->getOperand(0).getValueType() != MVT::i64)
3376 return;
3377
3378 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3379 ? RTLIB::SINTTOFP_I64_F128
3380 : RTLIB::UINTTOFP_I64_F128);
3381
3382 Results.push_back(LowerF128Op(SDValue(N, 0),
3383 DAG,
3384 getLibcallName(libCall),
3385 1));
3386 return;
James Y Knight3994be82015-08-10 19:11:39 +00003387 case ISD::LOAD: {
3388 LoadSDNode *Ld = cast<LoadSDNode>(N);
3389 // Custom handling only for i64: turn i64 load into a v2i32 load,
3390 // and a bitcast.
3391 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3392 return;
3393
3394 SDLoc dl(N);
3395 SDValue LoadRes = DAG.getExtLoad(
Justin Lebar9c375812016-07-15 18:27:10 +00003396 Ld->getExtensionType(), dl, MVT::v2i32, Ld->getChain(),
3397 Ld->getBasePtr(), Ld->getPointerInfo(), MVT::v2i32, Ld->getAlignment(),
3398 Ld->getMemOperand()->getFlags(), Ld->getAAInfo());
James Y Knight3994be82015-08-10 19:11:39 +00003399
3400 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3401 Results.push_back(Res);
3402 Results.push_back(LoadRes.getValue(1));
3403 return;
3404 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003405 }
3406}
Marcin Koscielnicki33571e22016-04-26 10:37:14 +00003407
3408// Override to enable LOAD_STACK_GUARD lowering on Linux.
3409bool SparcTargetLowering::useLoadStackGuardNode() const {
3410 if (!Subtarget->isTargetLinux())
3411 return TargetLowering::useLoadStackGuardNode();
3412 return true;
3413}
3414
3415// Override to disable global variable loading on Linux.
3416void SparcTargetLowering::insertSSPDeclarations(Module &M) const {
3417 if (!Subtarget->isTargetLinux())
3418 return TargetLowering::insertSSPDeclarations(M);
3419}