1 //===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2 //
3 // 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
6 //
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"
15 #include "MCTargetDesc/SparcMCTargetDesc.h"
16 #include "SparcMachineFunctionInfo.h"
17 #include "SparcRegisterInfo.h"
18 #include "SparcSelectionDAGInfo.h"
19 #include "SparcTargetMachine.h"
20 #include "SparcTargetObjectFile.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/BinaryFormat/ELF.h"
24 #include "llvm/CodeGen/CallingConvLower.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/SelectionDAG.h"
30 #include "llvm/CodeGen/SelectionDAGNodes.h"
31 #include "llvm/CodeGen/TargetLowering.h"
32 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/DiagnosticInfo.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/KnownBits.h"
39 using namespace llvm;
40
41
42 //===----------------------------------------------------------------------===//
43 // Calling Convention Implementation
44 //===----------------------------------------------------------------------===//
45
CC_Sparc_Assign_SRet(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)46 static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
47 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
48 ISD::ArgFlagsTy &ArgFlags, CCState &State)
49 {
50 assert (ArgFlags.isSRet());
51
52 // Assign SRet argument.
53 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
54 0,
55 LocVT, LocInfo));
56 return true;
57 }
58
CC_Sparc_Assign_Split_64(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)59 static bool CC_Sparc_Assign_Split_64(unsigned &ValNo, MVT &ValVT,
60 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags, CCState &State)
62 {
63 static const MCPhysReg RegList[] = {
64 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
65 };
66 // Try to get first reg.
67 if (Register Reg = State.AllocateReg(RegList)) {
68 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
69 } else {
70 // Assign whole thing in stack.
71 State.addLoc(CCValAssign::getCustomMem(
72 ValNo, ValVT, State.AllocateStack(8, Align(4)), LocVT, LocInfo));
73 return true;
74 }
75
76 // Try to get second reg.
77 if (Register Reg = State.AllocateReg(RegList))
78 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
79 else
80 State.addLoc(CCValAssign::getCustomMem(
81 ValNo, ValVT, State.AllocateStack(4, Align(4)), LocVT, LocInfo));
82 return true;
83 }
84
CC_Sparc_Assign_Ret_Split_64(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)85 static bool CC_Sparc_Assign_Ret_Split_64(unsigned &ValNo, MVT &ValVT,
86 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
87 ISD::ArgFlagsTy &ArgFlags, CCState &State)
88 {
89 static const MCPhysReg RegList[] = {
90 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
91 };
92
93 // Try to get first reg.
94 if (Register Reg = State.AllocateReg(RegList))
95 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
96 else
97 return false;
98
99 // Try to get second reg.
100 if (Register Reg = State.AllocateReg(RegList))
101 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
102 else
103 return false;
104
105 return true;
106 }
107
108 // Allocate a full-sized argument for the 64-bit ABI.
Analyze_CC_Sparc64_Full(bool IsReturn,unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)109 static bool Analyze_CC_Sparc64_Full(bool IsReturn, unsigned &ValNo, MVT &ValVT,
110 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
111 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
112 assert((LocVT == MVT::f32 || LocVT == MVT::f128
113 || LocVT.getSizeInBits() == 64) &&
114 "Can't handle non-64 bits locations");
115
116 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
117 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
118 Align alignment =
119 (LocVT == MVT::f128 || ArgFlags.isSplit()) ? Align(16) : Align(8);
120 unsigned Offset = State.AllocateStack(size, alignment);
121 unsigned Reg = 0;
122
123 if (LocVT == MVT::i64 && Offset < 6*8)
124 // Promote integers to %i0-%i5.
125 Reg = SP::I0 + Offset/8;
126 else if (LocVT == MVT::f64 && Offset < 16*8)
127 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
128 Reg = SP::D0 + Offset/8;
129 else if (LocVT == MVT::f32 && Offset < 16*8)
130 // Promote floats to %f1, %f3, ...
131 Reg = SP::F1 + Offset/4;
132 else if (LocVT == MVT::f128 && Offset < 16*8)
133 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
134 Reg = SP::Q0 + Offset/16;
135
136 // Promote to register when possible, otherwise use the stack slot.
137 if (Reg) {
138 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
139 return true;
140 }
141
142 // Bail out if this is a return CC and we run out of registers to place
143 // values into.
144 if (IsReturn)
145 return false;
146
147 // This argument goes on the stack in an 8-byte slot.
148 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
149 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
150 if (LocVT == MVT::f32)
151 Offset += 4;
152
153 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
154 return true;
155 }
156
157 // Allocate a half-sized argument for the 64-bit ABI.
158 //
159 // This is used when passing { float, int } structs by value in registers.
Analyze_CC_Sparc64_Half(bool IsReturn,unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)160 static bool Analyze_CC_Sparc64_Half(bool IsReturn, unsigned &ValNo, MVT &ValVT,
161 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
162 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
163 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
164 unsigned Offset = State.AllocateStack(4, Align(4));
165
166 if (LocVT == MVT::f32 && Offset < 16*8) {
167 // Promote floats to %f0-%f31.
168 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
169 LocVT, LocInfo));
170 return true;
171 }
172
173 if (LocVT == MVT::i32 && Offset < 6*8) {
174 // Promote integers to %i0-%i5, using half the register.
175 unsigned Reg = SP::I0 + Offset/8;
176 LocVT = MVT::i64;
177 LocInfo = CCValAssign::AExt;
178
179 // Set the Custom bit if this i32 goes in the high bits of a register.
180 if (Offset % 8 == 0)
181 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
182 LocVT, LocInfo));
183 else
184 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
185 return true;
186 }
187
188 // Bail out if this is a return CC and we run out of registers to place
189 // values into.
190 if (IsReturn)
191 return false;
192
193 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
194 return true;
195 }
196
CC_Sparc64_Full(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)197 static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
198 CCValAssign::LocInfo &LocInfo,
199 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
200 return Analyze_CC_Sparc64_Full(false, ValNo, ValVT, LocVT, LocInfo, ArgFlags,
201 State);
202 }
203
CC_Sparc64_Half(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)204 static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
205 CCValAssign::LocInfo &LocInfo,
206 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
207 return Analyze_CC_Sparc64_Half(false, ValNo, ValVT, LocVT, LocInfo, ArgFlags,
208 State);
209 }
210
RetCC_Sparc64_Full(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)211 static bool RetCC_Sparc64_Full(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
212 CCValAssign::LocInfo &LocInfo,
213 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
214 return Analyze_CC_Sparc64_Full(true, ValNo, ValVT, LocVT, LocInfo, ArgFlags,
215 State);
216 }
217
RetCC_Sparc64_Half(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)218 static bool RetCC_Sparc64_Half(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
219 CCValAssign::LocInfo &LocInfo,
220 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
221 return Analyze_CC_Sparc64_Half(true, ValNo, ValVT, LocVT, LocInfo, ArgFlags,
222 State);
223 }
224
225 #include "SparcGenCallingConv.inc"
226
227 // The calling conventions in SparcCallingConv.td are described in terms of the
228 // callee's register window. This function translates registers to the
229 // corresponding caller window %o register.
toCallerWindow(unsigned Reg)230 static unsigned toCallerWindow(unsigned Reg) {
231 static_assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7,
232 "Unexpected enum");
233 if (Reg >= SP::I0 && Reg <= SP::I7)
234 return Reg - SP::I0 + SP::O0;
235 return Reg;
236 }
237
CanLowerReturn(CallingConv::ID CallConv,MachineFunction & MF,bool isVarArg,const SmallVectorImpl<ISD::OutputArg> & Outs,LLVMContext & Context,const Type * RetTy) const238 bool SparcTargetLowering::CanLowerReturn(
239 CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
240 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context,
241 const Type *RetTy) const {
242 SmallVector<CCValAssign, 16> RVLocs;
243 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
244 return CCInfo.CheckReturn(Outs, Subtarget->is64Bit() ? RetCC_Sparc64
245 : RetCC_Sparc32);
246 }
247
248 SDValue
LowerReturn(SDValue Chain,CallingConv::ID CallConv,bool IsVarArg,const SmallVectorImpl<ISD::OutputArg> & Outs,const SmallVectorImpl<SDValue> & OutVals,const SDLoc & DL,SelectionDAG & DAG) const249 SparcTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
250 bool IsVarArg,
251 const SmallVectorImpl<ISD::OutputArg> &Outs,
252 const SmallVectorImpl<SDValue> &OutVals,
253 const SDLoc &DL, SelectionDAG &DAG) const {
254 if (Subtarget->is64Bit())
255 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
256 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
257 }
258
259 SDValue
LowerReturn_32(SDValue Chain,CallingConv::ID CallConv,bool IsVarArg,const SmallVectorImpl<ISD::OutputArg> & Outs,const SmallVectorImpl<SDValue> & OutVals,const SDLoc & DL,SelectionDAG & DAG) const260 SparcTargetLowering::LowerReturn_32(SDValue Chain, CallingConv::ID CallConv,
261 bool IsVarArg,
262 const SmallVectorImpl<ISD::OutputArg> &Outs,
263 const SmallVectorImpl<SDValue> &OutVals,
264 const SDLoc &DL, SelectionDAG &DAG) const {
265 MachineFunction &MF = DAG.getMachineFunction();
266
267 // CCValAssign - represent the assignment of the return value to locations.
268 SmallVector<CCValAssign, 16> RVLocs;
269
270 // CCState - Info about the registers and stack slot.
271 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
272 *DAG.getContext());
273
274 // Analyze return values.
275 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
276
277 SDValue Glue;
278 SmallVector<SDValue, 4> RetOps(1, Chain);
279 // Make room for the return address offset.
280 RetOps.push_back(SDValue());
281
282 // Copy the result values into the output registers.
283 for (unsigned i = 0, realRVLocIdx = 0;
284 i != RVLocs.size();
285 ++i, ++realRVLocIdx) {
286 CCValAssign &VA = RVLocs[i];
287 assert(VA.isRegLoc() && "Can only return in registers!");
288
289 SDValue Arg = OutVals[realRVLocIdx];
290
291 if (VA.needsCustom()) {
292 assert(VA.getLocVT() == MVT::v2i32);
293 // Legalize ret v2i32 -> ret 2 x i32 (Basically: do what would
294 // happen by default if this wasn't a legal type)
295
296 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
297 Arg,
298 DAG.getConstant(0, DL, getVectorIdxTy(DAG.getDataLayout())));
299 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32,
300 Arg,
301 DAG.getConstant(1, DL, getVectorIdxTy(DAG.getDataLayout())));
302
303 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part0, Glue);
304 Glue = Chain.getValue(1);
305 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
306 VA = RVLocs[++i]; // skip ahead to next loc
307 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Part1,
308 Glue);
309 } else
310 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Glue);
311
312 // Guarantee that all emitted copies are stuck together with flags.
313 Glue = Chain.getValue(1);
314 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
315 }
316
317 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
318 // If the function returns a struct, copy the SRetReturnReg to I0
319 if (MF.getFunction().hasStructRetAttr()) {
320 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
321 Register Reg = SFI->getSRetReturnReg();
322 if (!Reg)
323 llvm_unreachable("sret virtual register not created in the entry block");
324 auto PtrVT = getPointerTy(DAG.getDataLayout());
325 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, PtrVT);
326 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Glue);
327 Glue = Chain.getValue(1);
328 RetOps.push_back(DAG.getRegister(SP::I0, PtrVT));
329 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
330 }
331
332 RetOps[0] = Chain; // Update chain.
333 RetOps[1] = DAG.getConstant(RetAddrOffset, DL, MVT::i32);
334
335 // Add the glue if we have it.
336 if (Glue.getNode())
337 RetOps.push_back(Glue);
338
339 return DAG.getNode(SPISD::RET_GLUE, DL, MVT::Other, RetOps);
340 }
341
342 // Lower return values for the 64-bit ABI.
343 // Return values are passed the exactly the same way as function arguments.
344 SDValue
LowerReturn_64(SDValue Chain,CallingConv::ID CallConv,bool IsVarArg,const SmallVectorImpl<ISD::OutputArg> & Outs,const SmallVectorImpl<SDValue> & OutVals,const SDLoc & DL,SelectionDAG & DAG) const345 SparcTargetLowering::LowerReturn_64(SDValue Chain, CallingConv::ID CallConv,
346 bool IsVarArg,
347 const SmallVectorImpl<ISD::OutputArg> &Outs,
348 const SmallVectorImpl<SDValue> &OutVals,
349 const SDLoc &DL, SelectionDAG &DAG) const {
350 // CCValAssign - represent the assignment of the return value to locations.
351 SmallVector<CCValAssign, 16> RVLocs;
352
353 // CCState - Info about the registers and stack slot.
354 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
355 *DAG.getContext());
356
357 // Analyze return values.
358 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
359
360 SDValue Glue;
361 SmallVector<SDValue, 4> RetOps(1, Chain);
362
363 // The second operand on the return instruction is the return address offset.
364 // The return address is always %i7+8 with the 64-bit ABI.
365 RetOps.push_back(DAG.getConstant(8, DL, MVT::i32));
366
367 // Copy the result values into the output registers.
368 for (unsigned i = 0; i != RVLocs.size(); ++i) {
369 CCValAssign &VA = RVLocs[i];
370 assert(VA.isRegLoc() && "Can only return in registers!");
371 SDValue OutVal = OutVals[i];
372
373 // Integer return values must be sign or zero extended by the callee.
374 switch (VA.getLocInfo()) {
375 case CCValAssign::Full: break;
376 case CCValAssign::SExt:
377 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
378 break;
379 case CCValAssign::ZExt:
380 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
381 break;
382 case CCValAssign::AExt:
383 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
384 break;
385 default:
386 llvm_unreachable("Unknown loc info!");
387 }
388
389 // The custom bit on an i32 return value indicates that it should be passed
390 // in the high bits of the register.
391 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
392 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
393 DAG.getConstant(32, DL, MVT::i32));
394
395 // The next value may go in the low bits of the same register.
396 // Handle both at once.
397 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
398 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
399 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
400 // Skip the next value, it's already done.
401 ++i;
402 }
403 }
404
405 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Glue);
406
407 // Guarantee that all emitted copies are stuck together with flags.
408 Glue = Chain.getValue(1);
409 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
410 }
411
412 RetOps[0] = Chain; // Update chain.
413
414 // Add the flag if we have it.
415 if (Glue.getNode())
416 RetOps.push_back(Glue);
417
418 return DAG.getNode(SPISD::RET_GLUE, DL, MVT::Other, RetOps);
419 }
420
LowerFormalArguments(SDValue Chain,CallingConv::ID CallConv,bool IsVarArg,const SmallVectorImpl<ISD::InputArg> & Ins,const SDLoc & DL,SelectionDAG & DAG,SmallVectorImpl<SDValue> & InVals) const421 SDValue SparcTargetLowering::LowerFormalArguments(
422 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
423 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
424 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
425 if (Subtarget->is64Bit())
426 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
427 DL, DAG, InVals);
428 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
429 DL, DAG, InVals);
430 }
431
432 /// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
433 /// passed in either one or two GPRs, including FP values. TODO: we should
434 /// pass FP values in FP registers for fastcc functions.
LowerFormalArguments_32(SDValue Chain,CallingConv::ID CallConv,bool isVarArg,const SmallVectorImpl<ISD::InputArg> & Ins,const SDLoc & dl,SelectionDAG & DAG,SmallVectorImpl<SDValue> & InVals) const435 SDValue SparcTargetLowering::LowerFormalArguments_32(
436 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
437 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
438 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
439 MachineFunction &MF = DAG.getMachineFunction();
440 MachineRegisterInfo &RegInfo = MF.getRegInfo();
441 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
442
443 // Assign locations to all of the incoming arguments.
444 SmallVector<CCValAssign, 16> ArgLocs;
445 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
446 *DAG.getContext());
447 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
448
449 const unsigned StackOffset = 92;
450 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
451
452 unsigned InIdx = 0;
453 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
454 CCValAssign &VA = ArgLocs[i];
455
456 if (Ins[InIdx].Flags.isSRet()) {
457 if (InIdx != 0)
458 report_fatal_error("sparc only supports sret on the first parameter");
459 // Get SRet from [%fp+64].
460 int FrameIdx = MF.getFrameInfo().CreateFixedObject(4, 64, true);
461 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
462 SDValue Arg =
463 DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
464 InVals.push_back(Arg);
465 continue;
466 }
467
468 if (VA.isRegLoc()) {
469 if (VA.needsCustom()) {
470 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
471
472 Register VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
473 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
474 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
475
476 assert(i+1 < e);
477 CCValAssign &NextVA = ArgLocs[++i];
478
479 SDValue LoVal;
480 if (NextVA.isMemLoc()) {
481 int FrameIdx = MF.getFrameInfo().
482 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
483 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
484 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
485 } else {
486 Register loReg = MF.addLiveIn(NextVA.getLocReg(),
487 &SP::IntRegsRegClass);
488 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
489 }
490
491 if (IsLittleEndian)
492 std::swap(LoVal, HiVal);
493
494 SDValue WholeValue =
495 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
496 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), WholeValue);
497 InVals.push_back(WholeValue);
498 continue;
499 }
500 Register VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
501 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
502 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
503 if (VA.getLocVT() == MVT::f32)
504 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
505 else if (VA.getLocVT() != MVT::i32) {
506 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
507 DAG.getValueType(VA.getLocVT()));
508 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
509 }
510 InVals.push_back(Arg);
511 continue;
512 }
513
514 assert(VA.isMemLoc());
515
516 unsigned Offset = VA.getLocMemOffset()+StackOffset;
517 auto PtrVT = getPointerTy(DAG.getDataLayout());
518
519 if (VA.needsCustom()) {
520 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
521 // If it is double-word aligned, just load.
522 if (Offset % 8 == 0) {
523 int FI = MF.getFrameInfo().CreateFixedObject(8,
524 Offset,
525 true);
526 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
527 SDValue Load =
528 DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, MachinePointerInfo());
529 InVals.push_back(Load);
530 continue;
531 }
532
533 int FI = MF.getFrameInfo().CreateFixedObject(4,
534 Offset,
535 true);
536 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
537 SDValue HiVal =
538 DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo());
539 int FI2 = MF.getFrameInfo().CreateFixedObject(4,
540 Offset+4,
541 true);
542 SDValue FIPtr2 = DAG.getFrameIndex(FI2, PtrVT);
543
544 SDValue LoVal =
545 DAG.getLoad(MVT::i32, dl, Chain, FIPtr2, MachinePointerInfo());
546
547 if (IsLittleEndian)
548 std::swap(LoVal, HiVal);
549
550 SDValue WholeValue =
551 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
552 WholeValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), WholeValue);
553 InVals.push_back(WholeValue);
554 continue;
555 }
556
557 int FI = MF.getFrameInfo().CreateFixedObject(4,
558 Offset,
559 true);
560 SDValue FIPtr = DAG.getFrameIndex(FI, PtrVT);
561 SDValue Load ;
562 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
563 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, MachinePointerInfo());
564 } else if (VA.getValVT() == MVT::f128) {
565 report_fatal_error("SPARCv8 does not handle f128 in calls; "
566 "pass indirectly");
567 } else {
568 // We shouldn't see any other value types here.
569 llvm_unreachable("Unexpected ValVT encountered in frame lowering.");
570 }
571 InVals.push_back(Load);
572 }
573
574 if (MF.getFunction().hasStructRetAttr()) {
575 // Copy the SRet Argument to SRetReturnReg.
576 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
577 Register Reg = SFI->getSRetReturnReg();
578 if (!Reg) {
579 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
580 SFI->setSRetReturnReg(Reg);
581 }
582 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
583 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
584 }
585
586 // Store remaining ArgRegs to the stack if this is a varargs function.
587 if (isVarArg) {
588 static const MCPhysReg ArgRegs[] = {
589 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
590 };
591 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs);
592 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
593 unsigned ArgOffset = CCInfo.getStackSize();
594 if (NumAllocated == 6)
595 ArgOffset += StackOffset;
596 else {
597 assert(!ArgOffset);
598 ArgOffset = 68+4*NumAllocated;
599 }
600
601 // Remember the vararg offset for the va_start implementation.
602 FuncInfo->setVarArgsFrameOffset(ArgOffset);
603
604 std::vector<SDValue> OutChains;
605
606 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
607 Register VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
608 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
609 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
610
611 int FrameIdx = MF.getFrameInfo().CreateFixedObject(4, ArgOffset,
612 true);
613 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
614
615 OutChains.push_back(
616 DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, MachinePointerInfo()));
617 ArgOffset += 4;
618 }
619
620 if (!OutChains.empty()) {
621 OutChains.push_back(Chain);
622 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
623 }
624 }
625
626 return Chain;
627 }
628
629 // Lower formal arguments for the 64 bit ABI.
LowerFormalArguments_64(SDValue Chain,CallingConv::ID CallConv,bool IsVarArg,const SmallVectorImpl<ISD::InputArg> & Ins,const SDLoc & DL,SelectionDAG & DAG,SmallVectorImpl<SDValue> & InVals) const630 SDValue SparcTargetLowering::LowerFormalArguments_64(
631 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
632 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
633 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
634 MachineFunction &MF = DAG.getMachineFunction();
635
636 // Analyze arguments according to CC_Sparc64.
637 SmallVector<CCValAssign, 16> ArgLocs;
638 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
639 *DAG.getContext());
640 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
641
642 // The argument array begins at %fp+BIAS+128, after the register save area.
643 const unsigned ArgArea = 128;
644
645 for (const CCValAssign &VA : ArgLocs) {
646 if (VA.isRegLoc()) {
647 // This argument is passed in a register.
648 // All integer register arguments are promoted by the caller to i64.
649
650 // Create a virtual register for the promoted live-in value.
651 Register VReg = MF.addLiveIn(VA.getLocReg(),
652 getRegClassFor(VA.getLocVT()));
653 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
654
655 // Get the high bits for i32 struct elements.
656 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
657 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
658 DAG.getConstant(32, DL, MVT::i32));
659
660 // The caller promoted the argument, so insert an Assert?ext SDNode so we
661 // won't promote the value again in this function.
662 switch (VA.getLocInfo()) {
663 case CCValAssign::SExt:
664 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
665 DAG.getValueType(VA.getValVT()));
666 break;
667 case CCValAssign::ZExt:
668 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
669 DAG.getValueType(VA.getValVT()));
670 break;
671 default:
672 break;
673 }
674
675 // Truncate the register down to the argument type.
676 if (VA.isExtInLoc())
677 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
678
679 InVals.push_back(Arg);
680 continue;
681 }
682
683 // The registers are exhausted. This argument was passed on the stack.
684 assert(VA.isMemLoc());
685 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
686 // beginning of the arguments area at %fp+BIAS+128.
687 unsigned Offset = VA.getLocMemOffset() + ArgArea;
688 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
689 // Adjust offset for extended arguments, SPARC is big-endian.
690 // The caller will have written the full slot with extended bytes, but we
691 // prefer our own extending loads.
692 if (VA.isExtInLoc())
693 Offset += 8 - ValSize;
694 int FI = MF.getFrameInfo().CreateFixedObject(ValSize, Offset, true);
695 InVals.push_back(
696 DAG.getLoad(VA.getValVT(), DL, Chain,
697 DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
698 MachinePointerInfo::getFixedStack(MF, FI)));
699 }
700
701 if (!IsVarArg)
702 return Chain;
703
704 // This function takes variable arguments, some of which may have been passed
705 // in registers %i0-%i5. Variable floating point arguments are never passed
706 // in floating point registers. They go on %i0-%i5 or on the stack like
707 // integer arguments.
708 //
709 // The va_start intrinsic needs to know the offset to the first variable
710 // argument.
711 unsigned ArgOffset = CCInfo.getStackSize();
712 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
713 // Skip the 128 bytes of register save area.
714 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
715 Subtarget->getStackPointerBias());
716
717 // Save the variable arguments that were passed in registers.
718 // The caller is required to reserve stack space for 6 arguments regardless
719 // of how many arguments were actually passed.
720 SmallVector<SDValue, 8> OutChains;
721 for (; ArgOffset < 6*8; ArgOffset += 8) {
722 Register VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
723 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
724 int FI = MF.getFrameInfo().CreateFixedObject(8, ArgOffset + ArgArea, true);
725 auto PtrVT = getPointerTy(MF.getDataLayout());
726 OutChains.push_back(
727 DAG.getStore(Chain, DL, VArg, DAG.getFrameIndex(FI, PtrVT),
728 MachinePointerInfo::getFixedStack(MF, FI)));
729 }
730
731 if (!OutChains.empty())
732 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
733
734 return Chain;
735 }
736
737 // Check whether any of the argument registers are reserved
isAnyArgRegReserved(const SparcRegisterInfo * TRI,const MachineFunction & MF)738 static bool isAnyArgRegReserved(const SparcRegisterInfo *TRI,
739 const MachineFunction &MF) {
740 // The register window design means that outgoing parameters at O*
741 // will appear in the callee as I*.
742 // Be conservative and check both sides of the register names.
743 bool Outgoing =
744 llvm::any_of(SP::GPROutgoingArgRegClass, [TRI, &MF](MCPhysReg r) {
745 return TRI->isReservedReg(MF, r);
746 });
747 bool Incoming =
748 llvm::any_of(SP::GPRIncomingArgRegClass, [TRI, &MF](MCPhysReg r) {
749 return TRI->isReservedReg(MF, r);
750 });
751 return Outgoing || Incoming;
752 }
753
emitReservedArgRegCallError(const MachineFunction & MF)754 static void emitReservedArgRegCallError(const MachineFunction &MF) {
755 const Function &F = MF.getFunction();
756 F.getContext().diagnose(DiagnosticInfoUnsupported{
757 F, ("SPARC doesn't support"
758 " function calls if any of the argument registers is reserved.")});
759 }
760
761 SDValue
LowerCall(TargetLowering::CallLoweringInfo & CLI,SmallVectorImpl<SDValue> & InVals) const762 SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
763 SmallVectorImpl<SDValue> &InVals) const {
764 if (Subtarget->is64Bit())
765 return LowerCall_64(CLI, InVals);
766 return LowerCall_32(CLI, InVals);
767 }
768
hasReturnsTwiceAttr(SelectionDAG & DAG,SDValue Callee,const CallBase * Call)769 static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
770 const CallBase *Call) {
771 if (Call)
772 return Call->hasFnAttr(Attribute::ReturnsTwice);
773
774 const Function *CalleeFn = nullptr;
775 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
776 CalleeFn = dyn_cast<Function>(G->getGlobal());
777 } else if (ExternalSymbolSDNode *E =
778 dyn_cast<ExternalSymbolSDNode>(Callee)) {
779 const Function &Fn = DAG.getMachineFunction().getFunction();
780 const Module *M = Fn.getParent();
781 const char *CalleeName = E->getSymbol();
782 CalleeFn = M->getFunction(CalleeName);
783 }
784
785 if (!CalleeFn)
786 return false;
787 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
788 }
789
790 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
791 /// for tail call optimization.
IsEligibleForTailCallOptimization(CCState & CCInfo,CallLoweringInfo & CLI,MachineFunction & MF) const792 bool SparcTargetLowering::IsEligibleForTailCallOptimization(
793 CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF) const {
794
795 auto &Outs = CLI.Outs;
796 auto &Caller = MF.getFunction();
797
798 // Do not tail call opt functions with "disable-tail-calls" attribute.
799 if (Caller.getFnAttribute("disable-tail-calls").getValueAsString() == "true")
800 return false;
801
802 // Do not tail call opt if the stack is used to pass parameters.
803 // 64-bit targets have a slightly higher limit since the ABI requires
804 // to allocate some space even when all the parameters fit inside registers.
805 unsigned StackSizeLimit = Subtarget->is64Bit() ? 48 : 0;
806 if (CCInfo.getStackSize() > StackSizeLimit)
807 return false;
808
809 // Do not tail call opt if either the callee or caller returns
810 // a struct and the other does not.
811 if (!Outs.empty() && Caller.hasStructRetAttr() != Outs[0].Flags.isSRet())
812 return false;
813
814 // Byval parameters hand the function a pointer directly into the stack area
815 // we want to reuse during a tail call.
816 for (auto &Arg : Outs)
817 if (Arg.Flags.isByVal())
818 return false;
819
820 return true;
821 }
822
823 // Lower a call for the 32-bit ABI.
824 SDValue
LowerCall_32(TargetLowering::CallLoweringInfo & CLI,SmallVectorImpl<SDValue> & InVals) const825 SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
826 SmallVectorImpl<SDValue> &InVals) const {
827 SelectionDAG &DAG = CLI.DAG;
828 SDLoc &dl = CLI.DL;
829 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
830 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
831 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
832 SDValue Chain = CLI.Chain;
833 SDValue Callee = CLI.Callee;
834 bool &isTailCall = CLI.IsTailCall;
835 CallingConv::ID CallConv = CLI.CallConv;
836 bool isVarArg = CLI.IsVarArg;
837 MachineFunction &MF = DAG.getMachineFunction();
838
839 // Analyze operands of the call, assigning locations to each operand.
840 SmallVector<CCValAssign, 16> ArgLocs;
841 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
842 *DAG.getContext());
843 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
844
845 isTailCall = isTailCall && IsEligibleForTailCallOptimization(
846 CCInfo, CLI, DAG.getMachineFunction());
847
848 // Get the size of the outgoing arguments stack space requirement.
849 unsigned ArgsSize = CCInfo.getStackSize();
850
851 // Keep stack frames 8-byte aligned.
852 ArgsSize = (ArgsSize+7) & ~7;
853
854 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
855
856 // Create local copies for byval args.
857 SmallVector<SDValue, 8> ByValArgs;
858 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
859 ISD::ArgFlagsTy Flags = Outs[i].Flags;
860 if (!Flags.isByVal())
861 continue;
862
863 SDValue Arg = OutVals[i];
864 unsigned Size = Flags.getByValSize();
865 Align Alignment = Flags.getNonZeroByValAlign();
866
867 if (Size > 0U) {
868 int FI = MFI.CreateStackObject(Size, Alignment, false);
869 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
870 SDValue SizeNode = DAG.getConstant(Size, dl, MVT::i32);
871
872 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Alignment,
873 false, // isVolatile,
874 (Size <= 32), // AlwaysInline if size <= 32,
875 /*CI=*/nullptr, std::nullopt, MachinePointerInfo(),
876 MachinePointerInfo());
877 ByValArgs.push_back(FIPtr);
878 }
879 else {
880 SDValue nullVal;
881 ByValArgs.push_back(nullVal);
882 }
883 }
884
885 assert(!isTailCall || ArgsSize == 0);
886
887 if (!isTailCall)
888 Chain = DAG.getCALLSEQ_START(Chain, ArgsSize, 0, dl);
889
890 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
891 SmallVector<SDValue, 8> MemOpChains;
892
893 const unsigned StackOffset = 92;
894 bool hasStructRetAttr = false;
895 unsigned SRetArgSize = 0;
896 // Walk the register/memloc assignments, inserting copies/loads.
897 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
898 i != e;
899 ++i, ++realArgIdx) {
900 CCValAssign &VA = ArgLocs[i];
901 SDValue Arg = OutVals[realArgIdx];
902
903 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
904
905 // Use local copy if it is a byval arg.
906 if (Flags.isByVal()) {
907 Arg = ByValArgs[byvalArgIdx++];
908 if (!Arg) {
909 continue;
910 }
911 }
912
913 // Promote the value if needed.
914 switch (VA.getLocInfo()) {
915 default: llvm_unreachable("Unknown loc info!");
916 case CCValAssign::Full: break;
917 case CCValAssign::SExt:
918 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
919 break;
920 case CCValAssign::ZExt:
921 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
922 break;
923 case CCValAssign::AExt:
924 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
925 break;
926 case CCValAssign::BCvt:
927 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
928 break;
929 }
930
931 if (Flags.isSRet()) {
932 assert(VA.needsCustom());
933
934 if (isTailCall)
935 continue;
936
937 // store SRet argument in %sp+64
938 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
939 SDValue PtrOff = DAG.getIntPtrConstant(64, dl);
940 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
941 MemOpChains.push_back(
942 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
943 hasStructRetAttr = true;
944 // sret only allowed on first argument
945 assert(Outs[realArgIdx].OrigArgIndex == 0);
946 SRetArgSize =
947 DAG.getDataLayout().getTypeAllocSize(CLI.getArgs()[0].IndirectType);
948 continue;
949 }
950
951 if (VA.needsCustom()) {
952 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
953
954 if (VA.isMemLoc()) {
955 unsigned Offset = VA.getLocMemOffset() + StackOffset;
956 // if it is double-word aligned, just store.
957 if (Offset % 8 == 0) {
958 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
959 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
960 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
961 MemOpChains.push_back(
962 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
963 continue;
964 }
965 }
966
967 if (VA.getLocVT() == MVT::f64) {
968 // Move from the float value from float registers into the
969 // integer registers.
970 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Arg))
971 Arg = bitcastConstantFPToInt(C, dl, DAG);
972 else
973 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg);
974 }
975
976 SDValue Part0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
977 Arg,
978 DAG.getConstant(0, dl, getVectorIdxTy(DAG.getDataLayout())));
979 SDValue Part1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
980 Arg,
981 DAG.getConstant(1, dl, getVectorIdxTy(DAG.getDataLayout())));
982
983 if (VA.isRegLoc()) {
984 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Part0));
985 assert(i+1 != e);
986 CCValAssign &NextVA = ArgLocs[++i];
987 if (NextVA.isRegLoc()) {
988 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Part1));
989 } else {
990 // Store the second part in stack.
991 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
992 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
993 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
994 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
995 MemOpChains.push_back(
996 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
997 }
998 } else {
999 unsigned Offset = VA.getLocMemOffset() + StackOffset;
1000 // Store the first part.
1001 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
1002 SDValue PtrOff = DAG.getIntPtrConstant(Offset, dl);
1003 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
1004 MemOpChains.push_back(
1005 DAG.getStore(Chain, dl, Part0, PtrOff, MachinePointerInfo()));
1006 // Store the second part.
1007 PtrOff = DAG.getIntPtrConstant(Offset + 4, dl);
1008 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
1009 MemOpChains.push_back(
1010 DAG.getStore(Chain, dl, Part1, PtrOff, MachinePointerInfo()));
1011 }
1012 continue;
1013 }
1014
1015 // Arguments that can be passed on register must be kept at
1016 // RegsToPass vector
1017 if (VA.isRegLoc()) {
1018 if (VA.getLocVT() != MVT::f32) {
1019 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1020 continue;
1021 }
1022 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
1023 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1024 continue;
1025 }
1026
1027 assert(VA.isMemLoc());
1028
1029 // Create a store off the stack pointer for this argument.
1030 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
1031 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + StackOffset,
1032 dl);
1033 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
1034 MemOpChains.push_back(
1035 DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
1036 }
1037
1038
1039 // Emit all stores, make sure the occur before any copies into physregs.
1040 if (!MemOpChains.empty())
1041 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
1042
1043 // Build a sequence of copy-to-reg nodes chained together with token
1044 // chain and flag operands which copy the outgoing args into registers.
1045 // The InGlue in necessary since all emitted instructions must be
1046 // stuck together.
1047 SDValue InGlue;
1048 for (const auto &[OrigReg, N] : RegsToPass) {
1049 Register Reg = isTailCall ? OrigReg : toCallerWindow(OrigReg);
1050 Chain = DAG.getCopyToReg(Chain, dl, Reg, N, InGlue);
1051 InGlue = Chain.getValue(1);
1052 }
1053
1054 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CB);
1055
1056 // If the callee is a GlobalAddress node (quite common, every direct call is)
1057 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1058 // Likewise ExternalSymbol -> TargetExternalSymbol.
1059 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1060 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0);
1061 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1062 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
1063
1064 // Returns a chain & a flag for retval copy to use
1065 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1066 SmallVector<SDValue, 8> Ops;
1067 Ops.push_back(Chain);
1068 Ops.push_back(Callee);
1069 if (hasStructRetAttr)
1070 Ops.push_back(DAG.getTargetConstant(SRetArgSize, dl, MVT::i32));
1071 for (const auto &[OrigReg, N] : RegsToPass) {
1072 Register Reg = isTailCall ? OrigReg : toCallerWindow(OrigReg);
1073 Ops.push_back(DAG.getRegister(Reg, N.getValueType()));
1074 }
1075
1076 // Add a register mask operand representing the call-preserved registers.
1077 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
1078 const uint32_t *Mask =
1079 ((hasReturnsTwice)
1080 ? TRI->getRTCallPreservedMask(CallConv)
1081 : TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv));
1082
1083 if (isAnyArgRegReserved(TRI, MF))
1084 emitReservedArgRegCallError(MF);
1085
1086 assert(Mask && "Missing call preserved mask for calling convention");
1087 Ops.push_back(DAG.getRegisterMask(Mask));
1088
1089 if (InGlue.getNode())
1090 Ops.push_back(InGlue);
1091
1092 if (isTailCall) {
1093 DAG.getMachineFunction().getFrameInfo().setHasTailCall();
1094 return DAG.getNode(SPISD::TAIL_CALL, dl, MVT::Other, Ops);
1095 }
1096
1097 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops);
1098 InGlue = Chain.getValue(1);
1099
1100 Chain = DAG.getCALLSEQ_END(Chain, ArgsSize, 0, InGlue, dl);
1101 InGlue = Chain.getValue(1);
1102
1103 // Assign locations to each value returned by this call.
1104 SmallVector<CCValAssign, 16> RVLocs;
1105 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1106 *DAG.getContext());
1107
1108 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
1109
1110 // Copy all of the result registers out of their specified physreg.
1111 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1112 assert(RVLocs[i].isRegLoc() && "Can only return in registers!");
1113 if (RVLocs[i].getLocVT() == MVT::v2i32) {
1114 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2i32);
1115 SDValue Lo = DAG.getCopyFromReg(
1116 Chain, dl, toCallerWindow(RVLocs[i++].getLocReg()), MVT::i32, InGlue);
1117 Chain = Lo.getValue(1);
1118 InGlue = Lo.getValue(2);
1119 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Lo,
1120 DAG.getConstant(0, dl, MVT::i32));
1121 SDValue Hi = DAG.getCopyFromReg(
1122 Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), MVT::i32, InGlue);
1123 Chain = Hi.getValue(1);
1124 InGlue = Hi.getValue(2);
1125 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2i32, Vec, Hi,
1126 DAG.getConstant(1, dl, MVT::i32));
1127 InVals.push_back(Vec);
1128 } else {
1129 Chain =
1130 DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
1131 RVLocs[i].getValVT(), InGlue)
1132 .getValue(1);
1133 InGlue = Chain.getValue(2);
1134 InVals.push_back(Chain.getValue(0));
1135 }
1136 }
1137
1138 return Chain;
1139 }
1140
1141 // FIXME? Maybe this could be a TableGen attribute on some registers and
1142 // this table could be generated automatically from RegInfo.
getRegisterByName(const char * RegName,LLT VT,const MachineFunction & MF) const1143 Register SparcTargetLowering::getRegisterByName(const char* RegName, LLT VT,
1144 const MachineFunction &MF) const {
1145 Register Reg = StringSwitch<Register>(RegName)
1146 .Case("i0", SP::I0).Case("i1", SP::I1).Case("i2", SP::I2).Case("i3", SP::I3)
1147 .Case("i4", SP::I4).Case("i5", SP::I5).Case("i6", SP::I6).Case("i7", SP::I7)
1148 .Case("o0", SP::O0).Case("o1", SP::O1).Case("o2", SP::O2).Case("o3", SP::O3)
1149 .Case("o4", SP::O4).Case("o5", SP::O5).Case("o6", SP::O6).Case("o7", SP::O7)
1150 .Case("l0", SP::L0).Case("l1", SP::L1).Case("l2", SP::L2).Case("l3", SP::L3)
1151 .Case("l4", SP::L4).Case("l5", SP::L5).Case("l6", SP::L6).Case("l7", SP::L7)
1152 .Case("g0", SP::G0).Case("g1", SP::G1).Case("g2", SP::G2).Case("g3", SP::G3)
1153 .Case("g4", SP::G4).Case("g5", SP::G5).Case("g6", SP::G6).Case("g7", SP::G7)
1154 .Default(0);
1155
1156 // If we're directly referencing register names
1157 // (e.g in GCC C extension `register int r asm("g1");`),
1158 // make sure that said register is in the reserve list.
1159 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
1160 if (!TRI->isReservedReg(MF, Reg))
1161 Reg = Register();
1162
1163 return Reg;
1164 }
1165
1166 // Fixup floating point arguments in the ... part of a varargs call.
1167 //
1168 // The SPARC v9 ABI requires that floating point arguments are treated the same
1169 // as integers when calling a varargs function. This does not apply to the
1170 // fixed arguments that are part of the function's prototype.
1171 //
1172 // This function post-processes a CCValAssign array created by
1173 // AnalyzeCallOperands().
fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> & ArgLocs,ArrayRef<ISD::OutputArg> Outs)1174 static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1175 ArrayRef<ISD::OutputArg> Outs) {
1176 for (CCValAssign &VA : ArgLocs) {
1177 MVT ValTy = VA.getLocVT();
1178 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1179 // varargs functions.
1180 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
1181 continue;
1182 // The fixed arguments to a varargs function still go in FP registers.
1183 if (Outs[VA.getValNo()].IsFixed)
1184 continue;
1185
1186 // This floating point argument should be reassigned.
1187 // Determine the offset into the argument array.
1188 Register firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1189 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1190 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
1191 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1192
1193 if (Offset < 6*8) {
1194 // This argument should go in %i0-%i5.
1195 unsigned IReg = SP::I0 + Offset/8;
1196 if (ValTy == MVT::f64)
1197 // Full register, just bitconvert into i64.
1198 VA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(), IReg, MVT::i64,
1199 CCValAssign::BCvt);
1200 else {
1201 assert(ValTy == MVT::f128 && "Unexpected type!");
1202 // Full register, just bitconvert into i128 -- We will lower this into
1203 // two i64s in LowerCall_64.
1204 VA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(), IReg,
1205 MVT::i128, CCValAssign::BCvt);
1206 }
1207 } else {
1208 // This needs to go to memory, we're out of integer registers.
1209 VA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(), Offset,
1210 VA.getLocVT(), VA.getLocInfo());
1211 }
1212 }
1213 }
1214
1215 // Lower a call for the 64-bit ABI.
1216 SDValue
LowerCall_64(TargetLowering::CallLoweringInfo & CLI,SmallVectorImpl<SDValue> & InVals) const1217 SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1218 SmallVectorImpl<SDValue> &InVals) const {
1219 SelectionDAG &DAG = CLI.DAG;
1220 SDLoc DL = CLI.DL;
1221 SDValue Chain = CLI.Chain;
1222 auto PtrVT = getPointerTy(DAG.getDataLayout());
1223 MachineFunction &MF = DAG.getMachineFunction();
1224
1225 // Analyze operands of the call, assigning locations to each operand.
1226 SmallVector<CCValAssign, 16> ArgLocs;
1227 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1228 *DAG.getContext());
1229 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1230
1231 CLI.IsTailCall = CLI.IsTailCall && IsEligibleForTailCallOptimization(
1232 CCInfo, CLI, DAG.getMachineFunction());
1233
1234 // Get the size of the outgoing arguments stack space requirement.
1235 // The stack offset computed by CC_Sparc64 includes all arguments.
1236 // Called functions expect 6 argument words to exist in the stack frame, used
1237 // or not.
1238 unsigned StackReserved = 6 * 8u;
1239 unsigned ArgsSize = std::max<unsigned>(StackReserved, CCInfo.getStackSize());
1240
1241 // Keep stack frames 16-byte aligned.
1242 ArgsSize = alignTo(ArgsSize, 16);
1243
1244 // Varargs calls require special treatment.
1245 if (CLI.IsVarArg)
1246 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1247
1248 assert(!CLI.IsTailCall || ArgsSize == StackReserved);
1249
1250 // Adjust the stack pointer to make room for the arguments.
1251 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1252 // with more than 6 arguments.
1253 if (!CLI.IsTailCall)
1254 Chain = DAG.getCALLSEQ_START(Chain, ArgsSize, 0, DL);
1255
1256 // Collect the set of registers to pass to the function and their values.
1257 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1258 // instruction.
1259 SmallVector<std::pair<Register, SDValue>, 8> RegsToPass;
1260
1261 // Collect chains from all the memory opeations that copy arguments to the
1262 // stack. They must follow the stack pointer adjustment above and precede the
1263 // call instruction itself.
1264 SmallVector<SDValue, 8> MemOpChains;
1265
1266 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1267 const CCValAssign &VA = ArgLocs[i];
1268 SDValue Arg = CLI.OutVals[i];
1269
1270 // Promote the value if needed.
1271 switch (VA.getLocInfo()) {
1272 default:
1273 llvm_unreachable("Unknown location info!");
1274 case CCValAssign::Full:
1275 break;
1276 case CCValAssign::SExt:
1277 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1278 break;
1279 case CCValAssign::ZExt:
1280 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1281 break;
1282 case CCValAssign::AExt:
1283 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1284 break;
1285 case CCValAssign::BCvt:
1286 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1287 // SPARC does not support i128 natively. Lower it into two i64, see below.
1288 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1289 || VA.getLocVT() != MVT::i128)
1290 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1291 break;
1292 }
1293
1294 if (VA.isRegLoc()) {
1295 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1296 && VA.getLocVT() == MVT::i128) {
1297 // Store and reload into the integer register reg and reg+1.
1298 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1299 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
1300 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
1301 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset, DL);
1302 HiPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, HiPtrOff);
1303 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8, DL);
1304 LoPtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, LoPtrOff);
1305
1306 // Store to %sp+BIAS+128+Offset
1307 SDValue Store =
1308 DAG.getStore(Chain, DL, Arg, HiPtrOff, MachinePointerInfo());
1309 // Load into Reg and Reg+1
1310 SDValue Hi64 =
1311 DAG.getLoad(MVT::i64, DL, Store, HiPtrOff, MachinePointerInfo());
1312 SDValue Lo64 =
1313 DAG.getLoad(MVT::i64, DL, Store, LoPtrOff, MachinePointerInfo());
1314
1315 Register HiReg = VA.getLocReg();
1316 Register LoReg = VA.getLocReg() + 1;
1317 if (!CLI.IsTailCall) {
1318 HiReg = toCallerWindow(HiReg);
1319 LoReg = toCallerWindow(LoReg);
1320 }
1321
1322 RegsToPass.push_back(std::make_pair(HiReg, Hi64));
1323 RegsToPass.push_back(std::make_pair(LoReg, Lo64));
1324 continue;
1325 }
1326
1327 // The custom bit on an i32 return value indicates that it should be
1328 // passed in the high bits of the register.
1329 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1330 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
1331 DAG.getConstant(32, DL, MVT::i32));
1332
1333 // The next value may go in the low bits of the same register.
1334 // Handle both at once.
1335 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1336 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1337 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1338 CLI.OutVals[i+1]);
1339 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1340 // Skip the next value, it's already done.
1341 ++i;
1342 }
1343 }
1344
1345 Register Reg = VA.getLocReg();
1346 if (!CLI.IsTailCall)
1347 Reg = toCallerWindow(Reg);
1348 RegsToPass.push_back(std::make_pair(Reg, Arg));
1349 continue;
1350 }
1351
1352 assert(VA.isMemLoc());
1353
1354 // Create a store off the stack pointer for this argument.
1355 SDValue StackPtr = DAG.getRegister(SP::O6, PtrVT);
1356 // The argument area starts at %fp+BIAS+128 in the callee frame,
1357 // %sp+BIAS+128 in ours.
1358 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1359 Subtarget->getStackPointerBias() +
1360 128, DL);
1361 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
1362 MemOpChains.push_back(
1363 DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()));
1364 }
1365
1366 // Emit all stores, make sure they occur before the call.
1367 if (!MemOpChains.empty())
1368 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
1369
1370 // Build a sequence of CopyToReg nodes glued together with token chain and
1371 // glue operands which copy the outgoing args into registers. The InGlue is
1372 // necessary since all emitted instructions must be stuck together in order
1373 // to pass the live physical registers.
1374 SDValue InGlue;
1375 for (const auto &[Reg, N] : RegsToPass) {
1376 Chain = DAG.getCopyToReg(Chain, DL, Reg, N, InGlue);
1377 InGlue = Chain.getValue(1);
1378 }
1379
1380 // If the callee is a GlobalAddress node (quite common, every direct call is)
1381 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1382 // Likewise ExternalSymbol -> TargetExternalSymbol.
1383 SDValue Callee = CLI.Callee;
1384 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CB);
1385 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1386 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT, 0);
1387 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1388 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1389
1390 // Build the operands for the call instruction itself.
1391 SmallVector<SDValue, 8> Ops;
1392 Ops.push_back(Chain);
1393 Ops.push_back(Callee);
1394 for (const auto &[Reg, N] : RegsToPass)
1395 Ops.push_back(DAG.getRegister(Reg, N.getValueType()));
1396
1397 // Add a register mask operand representing the call-preserved registers.
1398 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
1399 const uint32_t *Mask =
1400 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CLI.CallConv)
1401 : TRI->getCallPreservedMask(DAG.getMachineFunction(),
1402 CLI.CallConv));
1403
1404 if (isAnyArgRegReserved(TRI, MF))
1405 emitReservedArgRegCallError(MF);
1406
1407 assert(Mask && "Missing call preserved mask for calling convention");
1408 Ops.push_back(DAG.getRegisterMask(Mask));
1409
1410 // Make sure the CopyToReg nodes are glued to the call instruction which
1411 // consumes the registers.
1412 if (InGlue.getNode())
1413 Ops.push_back(InGlue);
1414
1415 // Now the call itself.
1416 if (CLI.IsTailCall) {
1417 DAG.getMachineFunction().getFrameInfo().setHasTailCall();
1418 return DAG.getNode(SPISD::TAIL_CALL, DL, MVT::Other, Ops);
1419 }
1420 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1421 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, Ops);
1422 InGlue = Chain.getValue(1);
1423
1424 // Revert the stack pointer immediately after the call.
1425 Chain = DAG.getCALLSEQ_END(Chain, ArgsSize, 0, InGlue, DL);
1426 InGlue = Chain.getValue(1);
1427
1428 // Now extract the return values. This is more or less the same as
1429 // LowerFormalArguments_64.
1430
1431 // Assign locations to each value returned by this call.
1432 SmallVector<CCValAssign, 16> RVLocs;
1433 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1434 *DAG.getContext());
1435
1436 // Set inreg flag manually for codegen generated library calls that
1437 // return float.
1438 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && !CLI.CB)
1439 CLI.Ins[0].Flags.setInReg();
1440
1441 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
1442
1443 // Copy all of the result registers out of their specified physreg.
1444 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1445 CCValAssign &VA = RVLocs[i];
1446 assert(VA.isRegLoc() && "Can only return in registers!");
1447 unsigned Reg = toCallerWindow(VA.getLocReg());
1448
1449 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1450 // reside in the same register in the high and low bits. Reuse the
1451 // CopyFromReg previous node to avoid duplicate copies.
1452 SDValue RV;
1453 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1454 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1455 RV = Chain.getValue(0);
1456
1457 // But usually we'll create a new CopyFromReg for a different register.
1458 if (!RV.getNode()) {
1459 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1460 Chain = RV.getValue(1);
1461 InGlue = Chain.getValue(2);
1462 }
1463
1464 // Get the high bits for i32 struct elements.
1465 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1466 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
1467 DAG.getConstant(32, DL, MVT::i32));
1468
1469 // The callee promoted the return value, so insert an Assert?ext SDNode so
1470 // we won't promote the value again in this function.
1471 switch (VA.getLocInfo()) {
1472 case CCValAssign::SExt:
1473 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1474 DAG.getValueType(VA.getValVT()));
1475 break;
1476 case CCValAssign::ZExt:
1477 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1478 DAG.getValueType(VA.getValVT()));
1479 break;
1480 default:
1481 break;
1482 }
1483
1484 // Truncate the register down to the return value type.
1485 if (VA.isExtInLoc())
1486 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1487
1488 InVals.push_back(RV);
1489 }
1490
1491 return Chain;
1492 }
1493
1494 //===----------------------------------------------------------------------===//
1495 // TargetLowering Implementation
1496 //===----------------------------------------------------------------------===//
1497
shouldExpandAtomicRMWInIR(AtomicRMWInst * AI) const1498 TargetLowering::AtomicExpansionKind SparcTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1499 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1500 AI->getType()->getPrimitiveSizeInBits() == 32)
1501 return AtomicExpansionKind::None; // Uses xchg instruction
1502
1503 return AtomicExpansionKind::CmpXChg;
1504 }
1505
1506 /// intCondCCodeToRcond - Convert a DAG integer condition code to a SPARC
1507 /// rcond condition.
intCondCCodeToRcond(ISD::CondCode CC)1508 static SPCC::CondCodes intCondCCodeToRcond(ISD::CondCode CC) {
1509 switch (CC) {
1510 default:
1511 llvm_unreachable("Unknown/unsigned integer condition code!");
1512 case ISD::SETEQ:
1513 return SPCC::REG_Z;
1514 case ISD::SETNE:
1515 return SPCC::REG_NZ;
1516 case ISD::SETLT:
1517 return SPCC::REG_LZ;
1518 case ISD::SETGT:
1519 return SPCC::REG_GZ;
1520 case ISD::SETLE:
1521 return SPCC::REG_LEZ;
1522 case ISD::SETGE:
1523 return SPCC::REG_GEZ;
1524 }
1525 }
1526
1527 /// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1528 /// condition.
IntCondCCodeToICC(ISD::CondCode CC)1529 static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1530 switch (CC) {
1531 default: llvm_unreachable("Unknown integer condition code!");
1532 case ISD::SETEQ: return SPCC::ICC_E;
1533 case ISD::SETNE: return SPCC::ICC_NE;
1534 case ISD::SETLT: return SPCC::ICC_L;
1535 case ISD::SETGT: return SPCC::ICC_G;
1536 case ISD::SETLE: return SPCC::ICC_LE;
1537 case ISD::SETGE: return SPCC::ICC_GE;
1538 case ISD::SETULT: return SPCC::ICC_CS;
1539 case ISD::SETULE: return SPCC::ICC_LEU;
1540 case ISD::SETUGT: return SPCC::ICC_GU;
1541 case ISD::SETUGE: return SPCC::ICC_CC;
1542 }
1543 }
1544
1545 /// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1546 /// FCC condition.
FPCondCCodeToFCC(ISD::CondCode CC)1547 static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1548 switch (CC) {
1549 default: llvm_unreachable("Unknown fp condition code!");
1550 case ISD::SETEQ:
1551 case ISD::SETOEQ: return SPCC::FCC_E;
1552 case ISD::SETNE:
1553 case ISD::SETUNE: return SPCC::FCC_NE;
1554 case ISD::SETLT:
1555 case ISD::SETOLT: return SPCC::FCC_L;
1556 case ISD::SETGT:
1557 case ISD::SETOGT: return SPCC::FCC_G;
1558 case ISD::SETLE:
1559 case ISD::SETOLE: return SPCC::FCC_LE;
1560 case ISD::SETGE:
1561 case ISD::SETOGE: return SPCC::FCC_GE;
1562 case ISD::SETULT: return SPCC::FCC_UL;
1563 case ISD::SETULE: return SPCC::FCC_ULE;
1564 case ISD::SETUGT: return SPCC::FCC_UG;
1565 case ISD::SETUGE: return SPCC::FCC_UGE;
1566 case ISD::SETUO: return SPCC::FCC_U;
1567 case ISD::SETO: return SPCC::FCC_O;
1568 case ISD::SETONE: return SPCC::FCC_LG;
1569 case ISD::SETUEQ: return SPCC::FCC_UE;
1570 }
1571 }
1572
SparcTargetLowering(const TargetMachine & TM,const SparcSubtarget & STI)1573 SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
1574 const SparcSubtarget &STI)
1575 : TargetLowering(TM), Subtarget(&STI) {
1576 MVT PtrVT = MVT::getIntegerVT(TM.getPointerSizeInBits(0));
1577
1578 // Instructions which use registers as conditionals examine all the
1579 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1580 // matters much whether it's ZeroOrOneBooleanContent, or
1581 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1582 // former.
1583 setBooleanContents(ZeroOrOneBooleanContent);
1584 setBooleanVectorContents(ZeroOrOneBooleanContent);
1585
1586 // Set up the register classes.
1587 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1588 if (!Subtarget->useSoftFloat()) {
1589 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1590 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1591 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1592 }
1593 if (Subtarget->is64Bit()) {
1594 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
1595 } else {
1596 // On 32bit sparc, we define a double-register 32bit register
1597 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1598 addRegisterClass(MVT::v2i32, &SP::IntPairRegClass);
1599
1600 // ...but almost all operations must be expanded, so set that as
1601 // the default.
1602 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1603 setOperationAction(Op, MVT::v2i32, Expand);
1604 }
1605 // Truncating/extending stores/loads are also not supported.
1606 for (MVT VT : MVT::integer_fixedlen_vector_valuetypes()) {
1607 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Expand);
1608 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i32, Expand);
1609 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Expand);
1610
1611 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i32, VT, Expand);
1612 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, VT, Expand);
1613 setLoadExtAction(ISD::EXTLOAD, MVT::v2i32, VT, Expand);
1614
1615 setTruncStoreAction(VT, MVT::v2i32, Expand);
1616 setTruncStoreAction(MVT::v2i32, VT, Expand);
1617 }
1618 // However, load and store *are* legal.
1619 setOperationAction(ISD::LOAD, MVT::v2i32, Legal);
1620 setOperationAction(ISD::STORE, MVT::v2i32, Legal);
1621 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Legal);
1622 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Legal);
1623
1624 // And we need to promote i64 loads/stores into vector load/store
1625 setOperationAction(ISD::LOAD, MVT::i64, Custom);
1626 setOperationAction(ISD::STORE, MVT::i64, Custom);
1627
1628 // Sadly, this doesn't work:
1629 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1630 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1631 }
1632
1633 // Turn FP extload into load/fpextend
1634 for (MVT VT : MVT::fp_valuetypes()) {
1635 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
1636 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1637 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
1638 }
1639
1640 // Sparc doesn't have i1 sign extending load
1641 for (MVT VT : MVT::integer_valuetypes())
1642 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
1643
1644 // Turn FP truncstore into trunc + store.
1645 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
1646 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
1647 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1648 setTruncStoreAction(MVT::f128, MVT::f16, Expand);
1649 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1650 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
1651
1652 // Custom legalize GlobalAddress nodes into LO/HI parts.
1653 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
1654 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
1655 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
1656 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
1657
1658 // Sparc doesn't have sext_inreg, replace them with shl/sra
1659 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1660 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1661 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
1662
1663 // Sparc has no REM or DIVREM operations.
1664 setOperationAction(ISD::UREM, MVT::i32, Expand);
1665 setOperationAction(ISD::SREM, MVT::i32, Expand);
1666 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1667 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1668
1669 // ... nor does SparcV9.
1670 if (Subtarget->is64Bit()) {
1671 setOperationAction(ISD::UREM, MVT::i64, Expand);
1672 setOperationAction(ISD::SREM, MVT::i64, Expand);
1673 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1674 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1675 }
1676
1677 // Custom expand fp<->sint
1678 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1679 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
1680 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1681 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
1682
1683 // Custom Expand fp<->uint
1684 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1685 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
1686 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1687 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
1688
1689 // Lower f16 conversion operations into library calls
1690 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
1691 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
1692 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
1693 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
1694 setOperationAction(ISD::FP16_TO_FP, MVT::f128, Expand);
1695 setOperationAction(ISD::FP_TO_FP16, MVT::f128, Expand);
1696
1697 setOperationAction(ISD::BITCAST, MVT::f32,
1698 Subtarget->isVIS3() ? Legal : Expand);
1699 setOperationAction(ISD::BITCAST, MVT::i32,
1700 Subtarget->isVIS3() ? Legal : Expand);
1701
1702 // Sparc has no select or setcc: expand to SELECT_CC.
1703 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1704 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1705 setOperationAction(ISD::SELECT, MVT::f64, Expand);
1706 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1707
1708 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1709 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1710 setOperationAction(ISD::SETCC, MVT::f64, Expand);
1711 setOperationAction(ISD::SETCC, MVT::f128, Expand);
1712
1713 // Sparc doesn't have BRCOND either, it has BR_CC.
1714 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1715 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1716 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1717 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1718 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1719 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
1720 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
1721
1722 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1723 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1724 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
1725 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
1726
1727 setOperationAction(ISD::ADDC, MVT::i32, Legal);
1728 setOperationAction(ISD::ADDE, MVT::i32, Legal);
1729 setOperationAction(ISD::SUBC, MVT::i32, Legal);
1730 setOperationAction(ISD::SUBE, MVT::i32, Legal);
1731
1732 if (Subtarget->isVIS3()) {
1733 setOperationAction(ISD::ADDC, MVT::i64, Legal);
1734 setOperationAction(ISD::ADDE, MVT::i64, Legal);
1735 }
1736
1737 if (Subtarget->is64Bit()) {
1738 setOperationAction(ISD::BITCAST, MVT::f64,
1739 Subtarget->isVIS3() ? Legal : Expand);
1740 setOperationAction(ISD::BITCAST, MVT::i64,
1741 Subtarget->isVIS3() ? Legal : Expand);
1742 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1743 setOperationAction(ISD::SETCC, MVT::i64, Expand);
1744 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
1745 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
1746
1747 setOperationAction(ISD::CTPOP, MVT::i64,
1748 Subtarget->usePopc() ? Legal : Expand);
1749 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
1750 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1751 setOperationAction(ISD::ROTR , MVT::i64, Expand);
1752 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
1753 }
1754
1755 // ATOMICs.
1756 // Atomics are supported on SparcV9. 32-bit atomics are also
1757 // supported by some Leon SparcV8 variants. Otherwise, atomics
1758 // are unsupported.
1759 if (Subtarget->isV9()) {
1760 // TODO: we _ought_ to be able to support 64-bit atomics on 32-bit sparcv9,
1761 // but it hasn't been implemented in the backend yet.
1762 if (Subtarget->is64Bit())
1763 setMaxAtomicSizeInBitsSupported(64);
1764 else
1765 setMaxAtomicSizeInBitsSupported(32);
1766 } else if (Subtarget->hasLeonCasa())
1767 setMaxAtomicSizeInBitsSupported(32);
1768 else
1769 setMaxAtomicSizeInBitsSupported(0);
1770
1771 setMinCmpXchgSizeInBits(32);
1772
1773 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
1774
1775 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1776
1777 // Custom Lower Atomic LOAD/STORE
1778 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1779 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1780
1781 if (Subtarget->is64Bit()) {
1782 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
1783 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
1784 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1785 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1786 }
1787
1788 if (!Subtarget->isV9()) {
1789 // SparcV8 does not have FNEGD and FABSD.
1790 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1791 setOperationAction(ISD::FABS, MVT::f64, Custom);
1792 }
1793
1794 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1795 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1796 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1797 setOperationAction(ISD::FREM , MVT::f128, Expand);
1798 setOperationAction(ISD::FMA , MVT::f128, Expand);
1799 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1800 setOperationAction(ISD::FCOS , MVT::f64, Expand);
1801 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
1802 setOperationAction(ISD::FREM , MVT::f64, Expand);
1803 setOperationAction(ISD::FMA, MVT::f64,
1804 Subtarget->isUA2007() ? Legal : Expand);
1805 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1806 setOperationAction(ISD::FCOS , MVT::f32, Expand);
1807 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
1808 setOperationAction(ISD::FREM , MVT::f32, Expand);
1809 setOperationAction(ISD::FMA, MVT::f32,
1810 Subtarget->isUA2007() ? Legal : Expand);
1811 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1812 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1813 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1814 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
1815 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1816 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1817 setOperationAction(ISD::FPOW , MVT::f128, Expand);
1818 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1819 setOperationAction(ISD::FPOW , MVT::f32, Expand);
1820
1821 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1822 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1823 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
1824
1825 // Expands to [SU]MUL_LOHI.
1826 setOperationAction(ISD::MULHU, MVT::i32, Expand);
1827 setOperationAction(ISD::MULHS, MVT::i32, Expand);
1828 setOperationAction(ISD::MUL, MVT::i32, Expand);
1829
1830 if (Subtarget->useSoftMulDiv()) {
1831 // .umul works for both signed and unsigned
1832 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
1833 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1834 setOperationAction(ISD::SDIV, MVT::i32, Expand);
1835 setOperationAction(ISD::UDIV, MVT::i32, Expand);
1836 }
1837
1838 if (Subtarget->is64Bit()) {
1839 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1840 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1841 setOperationAction(ISD::MULHU, MVT::i64,
1842 Subtarget->isVIS3() ? Legal : Expand);
1843 setOperationAction(ISD::MULHS, MVT::i64,
1844 Subtarget->isVIS3() ? Legal : Expand);
1845
1846 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1847 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1848 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
1849 }
1850
1851 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1852 setOperationAction(ISD::VASTART , MVT::Other, Custom);
1853 // VAARG needs to be lowered to not do unaligned accesses for doubles.
1854 setOperationAction(ISD::VAARG , MVT::Other, Custom);
1855
1856 setOperationAction(ISD::TRAP , MVT::Other, Legal);
1857 setOperationAction(ISD::DEBUGTRAP , MVT::Other, Legal);
1858
1859 // Use the default implementation.
1860 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1861 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1862 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1863 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1864 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
1865
1866 setStackPointerRegisterToSaveRestore(SP::O6);
1867
1868 setOperationAction(ISD::CTPOP, MVT::i32,
1869 Subtarget->usePopc() ? Legal : Expand);
1870
1871 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1872 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1873 setOperationAction(ISD::STORE, MVT::f128, Legal);
1874 } else {
1875 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1876 setOperationAction(ISD::STORE, MVT::f128, Custom);
1877 }
1878
1879 if (Subtarget->hasHardQuad()) {
1880 setOperationAction(ISD::FADD, MVT::f128, Legal);
1881 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1882 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1883 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1884 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1885 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1886 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1887 if (Subtarget->isV9()) {
1888 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1889 setOperationAction(ISD::FABS, MVT::f128, Legal);
1890 } else {
1891 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1892 setOperationAction(ISD::FABS, MVT::f128, Custom);
1893 }
1894 } else {
1895 // Custom legalize f128 operations.
1896
1897 setOperationAction(ISD::FADD, MVT::f128, Custom);
1898 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1899 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1900 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1901 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1902 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1903 setOperationAction(ISD::FABS, MVT::f128, Custom);
1904
1905 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1906 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1907 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1908
1909 // Setup Runtime library names.
1910 if (Subtarget->is64Bit() && !Subtarget->useSoftFloat()) {
1911 setLibcallImpl(RTLIB::ADD_F128, RTLIB::_Qp_add);
1912 setLibcallImpl(RTLIB::SUB_F128, RTLIB::_Qp_sub);
1913 setLibcallImpl(RTLIB::MUL_F128, RTLIB::_Qp_mul);
1914 setLibcallImpl(RTLIB::DIV_F128, RTLIB::_Qp_div);
1915 setLibcallImpl(RTLIB::SQRT_F128, RTLIB::_Qp_sqrt);
1916 setLibcallImpl(RTLIB::FPTOSINT_F128_I32, RTLIB::_Qp_qtoi);
1917 setLibcallImpl(RTLIB::FPTOUINT_F128_I32, RTLIB::_Qp_qtoui);
1918 setLibcallImpl(RTLIB::SINTTOFP_I32_F128, RTLIB::_Qp_itoq);
1919 setLibcallImpl(RTLIB::UINTTOFP_I32_F128, RTLIB::_Qp_uitoq);
1920 setLibcallImpl(RTLIB::FPTOSINT_F128_I64, RTLIB::_Qp_qtox);
1921 setLibcallImpl(RTLIB::FPTOUINT_F128_I64, RTLIB::_Qp_qtoux);
1922 setLibcallImpl(RTLIB::SINTTOFP_I64_F128, RTLIB::_Qp_xtoq);
1923 setLibcallImpl(RTLIB::UINTTOFP_I64_F128, RTLIB::_Qp_uxtoq);
1924 setLibcallImpl(RTLIB::FPEXT_F32_F128, RTLIB::_Qp_stoq);
1925 setLibcallImpl(RTLIB::FPEXT_F64_F128, RTLIB::_Qp_dtoq);
1926 setLibcallImpl(RTLIB::FPROUND_F128_F32, RTLIB::_Qp_qtos);
1927 setLibcallImpl(RTLIB::FPROUND_F128_F64, RTLIB::_Qp_qtod);
1928 } else if (!Subtarget->useSoftFloat()) {
1929 setLibcallImpl(RTLIB::ADD_F128, RTLIB::_Q_add);
1930 setLibcallImpl(RTLIB::SUB_F128, RTLIB::_Q_sub);
1931 setLibcallImpl(RTLIB::MUL_F128, RTLIB::_Q_mul);
1932 setLibcallImpl(RTLIB::DIV_F128, RTLIB::_Q_div);
1933 setLibcallImpl(RTLIB::SQRT_F128, RTLIB::_Q_sqrt);
1934 setLibcallImpl(RTLIB::FPTOSINT_F128_I32, RTLIB::_Q_qtoi);
1935 setLibcallImpl(RTLIB::FPTOUINT_F128_I32, RTLIB::_Q_qtou);
1936 setLibcallImpl(RTLIB::SINTTOFP_I32_F128, RTLIB::_Q_itoq);
1937 setLibcallImpl(RTLIB::UINTTOFP_I32_F128, RTLIB::_Q_utoq);
1938 setLibcallImpl(RTLIB::FPEXT_F32_F128, RTLIB::_Q_stoq);
1939 setLibcallImpl(RTLIB::FPEXT_F64_F128, RTLIB::_Q_dtoq);
1940 setLibcallImpl(RTLIB::FPROUND_F128_F32, RTLIB::_Q_qtos);
1941 setLibcallImpl(RTLIB::FPROUND_F128_F64, RTLIB::_Q_qtod);
1942 }
1943 }
1944
1945 if (Subtarget->fixAllFDIVSQRT()) {
1946 // Promote FDIVS and FSQRTS to FDIVD and FSQRTD instructions instead as
1947 // the former instructions generate errata on LEON processors.
1948 setOperationAction(ISD::FDIV, MVT::f32, Promote);
1949 setOperationAction(ISD::FSQRT, MVT::f32, Promote);
1950 }
1951
1952 if (Subtarget->hasNoFMULS()) {
1953 setOperationAction(ISD::FMUL, MVT::f32, Promote);
1954 }
1955
1956 // Custom combine bitcast between f64 and v2i32
1957 if (!Subtarget->is64Bit())
1958 setTargetDAGCombine(ISD::BITCAST);
1959
1960 if (Subtarget->hasLeonCycleCounter())
1961 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
1962
1963 if (Subtarget->isVIS3()) {
1964 setOperationAction(ISD::CTLZ, MVT::i32, Legal);
1965 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
1966 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Legal);
1967 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Legal);
1968
1969 setOperationAction(ISD::CTTZ, MVT::i32,
1970 Subtarget->is64Bit() ? Promote : Expand);
1971 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
1972 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32,
1973 Subtarget->is64Bit() ? Promote : Expand);
1974 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1975 } else if (Subtarget->usePopc()) {
1976 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
1977 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
1978 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
1979 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1980
1981 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
1982 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
1983 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
1984 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1985 } else {
1986 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
1987 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
1988 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32,
1989 Subtarget->is64Bit() ? Promote : LibCall);
1990 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, LibCall);
1991
1992 // FIXME here we don't have any ISA extensions that could help us, so to
1993 // prevent large expansions those should be made into LibCalls.
1994 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
1995 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
1996 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
1997 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1998 }
1999
2000 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
2001
2002 setMinFunctionAlignment(Align(4));
2003
2004 computeRegisterProperties(Subtarget->getRegisterInfo());
2005 }
2006
useSoftFloat() const2007 bool SparcTargetLowering::useSoftFloat() const {
2008 return Subtarget->useSoftFloat();
2009 }
2010
getSetCCResultType(const DataLayout &,LLVMContext &,EVT VT) const2011 EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
2012 EVT VT) const {
2013 if (!VT.isVector())
2014 return MVT::i32;
2015 return VT.changeVectorElementTypeToInteger();
2016 }
2017
2018 /// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
2019 /// be zero. Op is expected to be a target specific node. Used by DAG
2020 /// combiner.
computeKnownBitsForTargetNode(const SDValue Op,KnownBits & Known,const APInt & DemandedElts,const SelectionDAG & DAG,unsigned Depth) const2021 void SparcTargetLowering::computeKnownBitsForTargetNode
2022 (const SDValue Op,
2023 KnownBits &Known,
2024 const APInt &DemandedElts,
2025 const SelectionDAG &DAG,
2026 unsigned Depth) const {
2027 KnownBits Known2;
2028 Known.resetAll();
2029
2030 switch (Op.getOpcode()) {
2031 default: break;
2032 case SPISD::SELECT_ICC:
2033 case SPISD::SELECT_XCC:
2034 case SPISD::SELECT_FCC:
2035 Known = DAG.computeKnownBits(Op.getOperand(1), Depth + 1);
2036 Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
2037
2038 // Only known if known in both the LHS and RHS.
2039 Known = Known.intersectWith(Known2);
2040 break;
2041 }
2042 }
2043
2044 // Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
2045 // set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
LookThroughSetCC(SDValue & LHS,SDValue & RHS,ISD::CondCode CC,unsigned & SPCC)2046 static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
2047 ISD::CondCode CC, unsigned &SPCC) {
2048 if (isNullConstant(RHS) && CC == ISD::SETNE &&
2049 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
2050 LHS.getOpcode() == SPISD::SELECT_XCC) &&
2051 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
2052 (LHS.getOpcode() == SPISD::SELECT_FCC &&
2053 (LHS.getOperand(3).getOpcode() == SPISD::CMPFCC ||
2054 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC_V9))) &&
2055 isOneConstant(LHS.getOperand(0)) && isNullConstant(LHS.getOperand(1))) {
2056 SDValue CMPCC = LHS.getOperand(3);
2057 SPCC = LHS.getConstantOperandVal(2);
2058 LHS = CMPCC.getOperand(0);
2059 RHS = CMPCC.getOperand(1);
2060 }
2061 }
2062
2063 // Convert to a target node and set target flags.
withTargetFlags(SDValue Op,unsigned TF,SelectionDAG & DAG) const2064 SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
2065 SelectionDAG &DAG) const {
2066 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
2067 return DAG.getTargetGlobalAddress(GA->getGlobal(),
2068 SDLoc(GA),
2069 GA->getValueType(0),
2070 GA->getOffset(), TF);
2071
2072 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
2073 return DAG.getTargetConstantPool(CP->getConstVal(), CP->getValueType(0),
2074 CP->getAlign(), CP->getOffset(), TF);
2075
2076 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
2077 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
2078 Op.getValueType(),
2079 0,
2080 TF);
2081
2082 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
2083 return DAG.getTargetExternalSymbol(ES->getSymbol(),
2084 ES->getValueType(0), TF);
2085
2086 llvm_unreachable("Unhandled address SDNode");
2087 }
2088
2089 // Split Op into high and low parts according to HiTF and LoTF.
2090 // Return an ADD node combining the parts.
makeHiLoPair(SDValue Op,unsigned HiTF,unsigned LoTF,SelectionDAG & DAG) const2091 SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
2092 unsigned HiTF, unsigned LoTF,
2093 SelectionDAG &DAG) const {
2094 SDLoc DL(Op);
2095 EVT VT = Op.getValueType();
2096 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
2097 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
2098 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
2099 }
2100
2101 // Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
2102 // or ExternalSymbol SDNode.
makeAddress(SDValue Op,SelectionDAG & DAG) const2103 SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
2104 SDLoc DL(Op);
2105 EVT VT = getPointerTy(DAG.getDataLayout());
2106
2107 // Handle PIC mode first. SPARC needs a got load for every variable!
2108 if (isPositionIndependent()) {
2109 const Module *M = DAG.getMachineFunction().getFunction().getParent();
2110 PICLevel::Level picLevel = M->getPICLevel();
2111 SDValue Idx;
2112
2113 if (picLevel == PICLevel::SmallPIC) {
2114 // This is the pic13 code model, the GOT is known to be smaller than 8KiB.
2115 Idx = DAG.getNode(SPISD::Lo, DL, Op.getValueType(),
2116 withTargetFlags(Op, ELF::R_SPARC_GOT13, DAG));
2117 } else {
2118 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
2119 Idx = makeHiLoPair(Op, ELF::R_SPARC_GOT22, ELF::R_SPARC_GOT10, DAG);
2120 }
2121
2122 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
2123 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, Idx);
2124 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2125 // function has calls.
2126 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2127 MFI.setHasCalls(true);
2128 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
2129 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
2130 }
2131
2132 // This is one of the absolute code models.
2133 switch(getTargetMachine().getCodeModel()) {
2134 default:
2135 llvm_unreachable("Unsupported absolute code model");
2136 case CodeModel::Small:
2137 // abs32.
2138 return makeHiLoPair(Op, ELF::R_SPARC_HI22, ELF::R_SPARC_LO10, DAG);
2139 case CodeModel::Medium: {
2140 // abs44.
2141 SDValue H44 = makeHiLoPair(Op, ELF::R_SPARC_H44, ELF::R_SPARC_M44, DAG);
2142 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, DL, MVT::i32));
2143 SDValue L44 = withTargetFlags(Op, ELF::R_SPARC_L44, DAG);
2144 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
2145 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
2146 }
2147 case CodeModel::Large: {
2148 // abs64.
2149 SDValue Hi = makeHiLoPair(Op, ELF::R_SPARC_HH22, ELF::R_SPARC_HM10, DAG);
2150 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, DL, MVT::i32));
2151 SDValue Lo = makeHiLoPair(Op, ELF::R_SPARC_HI22, ELF::R_SPARC_LO10, DAG);
2152 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
2153 }
2154 }
2155 }
2156
LowerGlobalAddress(SDValue Op,SelectionDAG & DAG) const2157 SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
2158 SelectionDAG &DAG) const {
2159 return makeAddress(Op, DAG);
2160 }
2161
LowerConstantPool(SDValue Op,SelectionDAG & DAG) const2162 SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
2163 SelectionDAG &DAG) const {
2164 return makeAddress(Op, DAG);
2165 }
2166
LowerBlockAddress(SDValue Op,SelectionDAG & DAG) const2167 SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2168 SelectionDAG &DAG) const {
2169 return makeAddress(Op, DAG);
2170 }
2171
LowerGlobalTLSAddress(SDValue Op,SelectionDAG & DAG) const2172 SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2173 SelectionDAG &DAG) const {
2174
2175 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2176 if (DAG.getTarget().useEmulatedTLS())
2177 return LowerToTLSEmulatedModel(GA, DAG);
2178
2179 SDLoc DL(GA);
2180 const GlobalValue *GV = GA->getGlobal();
2181 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2182
2183 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2184
2185 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
2186 unsigned HiTF =
2187 ((model == TLSModel::GeneralDynamic) ? ELF::R_SPARC_TLS_GD_HI22
2188 : ELF::R_SPARC_TLS_LDM_HI22);
2189 unsigned LoTF =
2190 ((model == TLSModel::GeneralDynamic) ? ELF::R_SPARC_TLS_GD_LO10
2191 : ELF::R_SPARC_TLS_LDM_LO10);
2192 unsigned addTF =
2193 ((model == TLSModel::GeneralDynamic) ? ELF::R_SPARC_TLS_GD_ADD
2194 : ELF::R_SPARC_TLS_LDM_ADD);
2195 unsigned callTF =
2196 ((model == TLSModel::GeneralDynamic) ? ELF::R_SPARC_TLS_GD_CALL
2197 : ELF::R_SPARC_TLS_LDM_CALL);
2198
2199 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2200 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2201 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
2202 withTargetFlags(Op, addTF, DAG));
2203
2204 SDValue Chain = DAG.getEntryNode();
2205 SDValue InGlue;
2206
2207 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL);
2208 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InGlue);
2209 InGlue = Chain.getValue(1);
2210 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
2211 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
2212
2213 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2214 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2215 DAG.getMachineFunction(), CallingConv::C);
2216 assert(Mask && "Missing call preserved mask for calling convention");
2217 SDValue Ops[] = {Chain,
2218 Callee,
2219 Symbol,
2220 DAG.getRegister(SP::O0, PtrVT),
2221 DAG.getRegisterMask(Mask),
2222 InGlue};
2223 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, Ops);
2224 InGlue = Chain.getValue(1);
2225 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
2226 InGlue = Chain.getValue(1);
2227 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InGlue);
2228
2229 if (model != TLSModel::LocalDynamic)
2230 return Ret;
2231
2232 SDValue Hi =
2233 DAG.getNode(SPISD::Hi, DL, PtrVT,
2234 withTargetFlags(Op, ELF::R_SPARC_TLS_LDO_HIX22, DAG));
2235 SDValue Lo =
2236 DAG.getNode(SPISD::Lo, DL, PtrVT,
2237 withTargetFlags(Op, ELF::R_SPARC_TLS_LDO_LOX10, DAG));
2238 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2239 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
2240 withTargetFlags(Op, ELF::R_SPARC_TLS_LDO_ADD, DAG));
2241 }
2242
2243 if (model == TLSModel::InitialExec) {
2244 unsigned ldTF = ((PtrVT == MVT::i64) ? ELF::R_SPARC_TLS_IE_LDX
2245 : ELF::R_SPARC_TLS_IE_LD);
2246
2247 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
2248
2249 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2250 // function has calls.
2251 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2252 MFI.setHasCalls(true);
2253
2254 SDValue TGA = makeHiLoPair(Op, ELF::R_SPARC_TLS_IE_HI22,
2255 ELF::R_SPARC_TLS_IE_LO10, DAG);
2256 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
2257 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
2258 DL, PtrVT, Ptr,
2259 withTargetFlags(Op, ldTF, DAG));
2260 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
2261 DAG.getRegister(SP::G7, PtrVT), Offset,
2262 withTargetFlags(Op, ELF::R_SPARC_TLS_IE_ADD, DAG));
2263 }
2264
2265 assert(model == TLSModel::LocalExec);
2266 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
2267 withTargetFlags(Op, ELF::R_SPARC_TLS_LE_HIX22, DAG));
2268 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
2269 withTargetFlags(Op, ELF::R_SPARC_TLS_LE_LOX10, DAG));
2270 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
2271
2272 return DAG.getNode(ISD::ADD, DL, PtrVT,
2273 DAG.getRegister(SP::G7, PtrVT), Offset);
2274 }
2275
LowerF128_LibCallArg(SDValue Chain,ArgListTy & Args,SDValue Arg,const SDLoc & DL,SelectionDAG & DAG) const2276 SDValue SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain,
2277 ArgListTy &Args, SDValue Arg,
2278 const SDLoc &DL,
2279 SelectionDAG &DAG) const {
2280 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2281 EVT ArgVT = Arg.getValueType();
2282 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2283
2284 ArgListEntry Entry;
2285 Entry.Node = Arg;
2286 Entry.Ty = ArgTy;
2287
2288 if (ArgTy->isFP128Ty()) {
2289 // Create a stack object and pass the pointer to the library function.
2290 int FI = MFI.CreateStackObject(16, Align(8), false);
2291 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2292 Chain = DAG.getStore(Chain, DL, Entry.Node, FIPtr, MachinePointerInfo(),
2293 Align(8));
2294
2295 Entry.Node = FIPtr;
2296 Entry.Ty = PointerType::getUnqual(ArgTy->getContext());
2297 }
2298 Args.push_back(Entry);
2299 return Chain;
2300 }
2301
2302 SDValue
LowerF128Op(SDValue Op,SelectionDAG & DAG,const char * LibFuncName,unsigned numArgs) const2303 SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2304 const char *LibFuncName,
2305 unsigned numArgs) const {
2306
2307 ArgListTy Args;
2308
2309 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2310 auto PtrVT = getPointerTy(DAG.getDataLayout());
2311
2312 SDValue Callee = DAG.getExternalSymbol(LibFuncName, PtrVT);
2313 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2314 Type *RetTyABI = RetTy;
2315 SDValue Chain = DAG.getEntryNode();
2316 SDValue RetPtr;
2317
2318 if (RetTy->isFP128Ty()) {
2319 // Create a Stack Object to receive the return value of type f128.
2320 ArgListEntry Entry;
2321 int RetFI = MFI.CreateStackObject(16, Align(8), false);
2322 RetPtr = DAG.getFrameIndex(RetFI, PtrVT);
2323 Entry.Node = RetPtr;
2324 Entry.Ty = PointerType::getUnqual(RetTy->getContext());
2325 if (!Subtarget->is64Bit()) {
2326 Entry.IsSRet = true;
2327 Entry.IndirectType = RetTy;
2328 }
2329 Entry.IsReturned = false;
2330 Args.push_back(Entry);
2331 RetTyABI = Type::getVoidTy(*DAG.getContext());
2332 }
2333
2334 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2335 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2336 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2337 }
2338 TargetLowering::CallLoweringInfo CLI(DAG);
2339 CLI.setDebugLoc(SDLoc(Op)).setChain(Chain)
2340 .setCallee(CallingConv::C, RetTyABI, Callee, std::move(Args));
2341
2342 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2343
2344 // chain is in second result.
2345 if (RetTyABI == RetTy)
2346 return CallInfo.first;
2347
2348 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2349
2350 Chain = CallInfo.second;
2351
2352 // Load RetPtr to get the return value.
2353 return DAG.getLoad(Op.getValueType(), SDLoc(Op), Chain, RetPtr,
2354 MachinePointerInfo(), Align(8));
2355 }
2356
LowerF128Compare(SDValue LHS,SDValue RHS,unsigned & SPCC,const SDLoc & DL,SelectionDAG & DAG) const2357 SDValue SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2358 unsigned &SPCC, const SDLoc &DL,
2359 SelectionDAG &DAG) const {
2360
2361 const char *LibCall = nullptr;
2362 bool is64Bit = Subtarget->is64Bit();
2363 switch(SPCC) {
2364 default: llvm_unreachable("Unhandled conditional code!");
2365 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2366 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2367 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2368 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2369 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2370 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2371 case SPCC::FCC_UL :
2372 case SPCC::FCC_ULE:
2373 case SPCC::FCC_UG :
2374 case SPCC::FCC_UGE:
2375 case SPCC::FCC_U :
2376 case SPCC::FCC_O :
2377 case SPCC::FCC_LG :
2378 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2379 }
2380
2381 auto PtrVT = getPointerTy(DAG.getDataLayout());
2382 SDValue Callee = DAG.getExternalSymbol(LibCall, PtrVT);
2383 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2384 ArgListTy Args;
2385 SDValue Chain = DAG.getEntryNode();
2386 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2387 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2388
2389 TargetLowering::CallLoweringInfo CLI(DAG);
2390 CLI.setDebugLoc(DL).setChain(Chain)
2391 .setCallee(CallingConv::C, RetTy, Callee, std::move(Args));
2392
2393 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2394
2395 // result is in first, and chain is in second result.
2396 SDValue Result = CallInfo.first;
2397
2398 switch(SPCC) {
2399 default: {
2400 SDValue RHS = DAG.getConstant(0, DL, Result.getValueType());
2401 SPCC = SPCC::ICC_NE;
2402 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2403 }
2404 case SPCC::FCC_UL : {
2405 SDValue Mask = DAG.getConstant(1, DL, Result.getValueType());
2406 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2407 SDValue RHS = DAG.getConstant(0, DL, Result.getValueType());
2408 SPCC = SPCC::ICC_NE;
2409 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2410 }
2411 case SPCC::FCC_ULE: {
2412 SDValue RHS = DAG.getConstant(2, DL, Result.getValueType());
2413 SPCC = SPCC::ICC_NE;
2414 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2415 }
2416 case SPCC::FCC_UG : {
2417 SDValue RHS = DAG.getConstant(1, DL, Result.getValueType());
2418 SPCC = SPCC::ICC_G;
2419 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2420 }
2421 case SPCC::FCC_UGE: {
2422 SDValue RHS = DAG.getConstant(1, DL, Result.getValueType());
2423 SPCC = SPCC::ICC_NE;
2424 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2425 }
2426
2427 case SPCC::FCC_U : {
2428 SDValue RHS = DAG.getConstant(3, DL, Result.getValueType());
2429 SPCC = SPCC::ICC_E;
2430 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2431 }
2432 case SPCC::FCC_O : {
2433 SDValue RHS = DAG.getConstant(3, DL, Result.getValueType());
2434 SPCC = SPCC::ICC_NE;
2435 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2436 }
2437 case SPCC::FCC_LG : {
2438 SDValue Mask = DAG.getConstant(3, DL, Result.getValueType());
2439 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2440 SDValue RHS = DAG.getConstant(0, DL, Result.getValueType());
2441 SPCC = SPCC::ICC_NE;
2442 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2443 }
2444 case SPCC::FCC_UE : {
2445 SDValue Mask = DAG.getConstant(3, DL, Result.getValueType());
2446 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2447 SDValue RHS = DAG.getConstant(0, DL, Result.getValueType());
2448 SPCC = SPCC::ICC_E;
2449 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2450 }
2451 }
2452 }
2453
2454 static SDValue
LowerF128_FPEXTEND(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI)2455 LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2456 const SparcTargetLowering &TLI) {
2457
2458 if (Op.getOperand(0).getValueType() == MVT::f64)
2459 return TLI.LowerF128Op(Op, DAG,
2460 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2461
2462 if (Op.getOperand(0).getValueType() == MVT::f32)
2463 return TLI.LowerF128Op(Op, DAG,
2464 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2465
2466 llvm_unreachable("fpextend with non-float operand!");
2467 return SDValue();
2468 }
2469
2470 static SDValue
LowerF128_FPROUND(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI)2471 LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2472 const SparcTargetLowering &TLI) {
2473 // FP_ROUND on f64 and f32 are legal.
2474 if (Op.getOperand(0).getValueType() != MVT::f128)
2475 return Op;
2476
2477 if (Op.getValueType() == MVT::f64)
2478 return TLI.LowerF128Op(Op, DAG,
2479 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2480 if (Op.getValueType() == MVT::f32)
2481 return TLI.LowerF128Op(Op, DAG,
2482 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2483
2484 llvm_unreachable("fpround to non-float!");
2485 return SDValue();
2486 }
2487
LowerFP_TO_SINT(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI,bool hasHardQuad)2488 static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2489 const SparcTargetLowering &TLI,
2490 bool hasHardQuad) {
2491 SDLoc dl(Op);
2492 EVT VT = Op.getValueType();
2493 assert(VT == MVT::i32 || VT == MVT::i64);
2494
2495 // Expand f128 operations to fp128 abi calls.
2496 if (Op.getOperand(0).getValueType() == MVT::f128
2497 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2498 const char *libName = TLI.getLibcallName(VT == MVT::i32
2499 ? RTLIB::FPTOSINT_F128_I32
2500 : RTLIB::FPTOSINT_F128_I64);
2501 return TLI.LowerF128Op(Op, DAG, libName, 1);
2502 }
2503
2504 // Expand if the resulting type is illegal.
2505 if (!TLI.isTypeLegal(VT))
2506 return SDValue();
2507
2508 // Otherwise, Convert the fp value to integer in an FP register.
2509 if (VT == MVT::i32)
2510 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2511 else
2512 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2513
2514 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
2515 }
2516
LowerSINT_TO_FP(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI,bool hasHardQuad)2517 static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2518 const SparcTargetLowering &TLI,
2519 bool hasHardQuad) {
2520 SDLoc dl(Op);
2521 EVT OpVT = Op.getOperand(0).getValueType();
2522 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2523
2524 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2525
2526 // Expand f128 operations to fp128 ABI calls.
2527 if (Op.getValueType() == MVT::f128
2528 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2529 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2530 ? RTLIB::SINTTOFP_I32_F128
2531 : RTLIB::SINTTOFP_I64_F128);
2532 return TLI.LowerF128Op(Op, DAG, libName, 1);
2533 }
2534
2535 // Expand if the operand type is illegal.
2536 if (!TLI.isTypeLegal(OpVT))
2537 return SDValue();
2538
2539 // Otherwise, Convert the int value to FP in an FP register.
2540 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2541 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2542 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
2543 }
2544
LowerFP_TO_UINT(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI,bool hasHardQuad)2545 static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2546 const SparcTargetLowering &TLI,
2547 bool hasHardQuad) {
2548 EVT VT = Op.getValueType();
2549
2550 // Expand if it does not involve f128 or the target has support for
2551 // quad floating point instructions and the resulting type is legal.
2552 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2553 (hasHardQuad && TLI.isTypeLegal(VT)))
2554 return SDValue();
2555
2556 assert(VT == MVT::i32 || VT == MVT::i64);
2557
2558 return TLI.LowerF128Op(Op, DAG,
2559 TLI.getLibcallName(VT == MVT::i32
2560 ? RTLIB::FPTOUINT_F128_I32
2561 : RTLIB::FPTOUINT_F128_I64),
2562 1);
2563 }
2564
LowerUINT_TO_FP(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI,bool hasHardQuad)2565 static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2566 const SparcTargetLowering &TLI,
2567 bool hasHardQuad) {
2568 EVT OpVT = Op.getOperand(0).getValueType();
2569 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2570
2571 // Expand if it does not involve f128 or the target has support for
2572 // quad floating point instructions and the operand type is legal.
2573 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
2574 return SDValue();
2575
2576 return TLI.LowerF128Op(Op, DAG,
2577 TLI.getLibcallName(OpVT == MVT::i32
2578 ? RTLIB::UINTTOFP_I32_F128
2579 : RTLIB::UINTTOFP_I64_F128),
2580 1);
2581 }
2582
LowerBR_CC(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI,bool hasHardQuad,bool isV9,bool is64Bit)2583 static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2584 const SparcTargetLowering &TLI, bool hasHardQuad,
2585 bool isV9, bool is64Bit) {
2586 SDValue Chain = Op.getOperand(0);
2587 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2588 SDValue LHS = Op.getOperand(2);
2589 SDValue RHS = Op.getOperand(3);
2590 SDValue Dest = Op.getOperand(4);
2591 SDLoc dl(Op);
2592 unsigned Opc, SPCC = ~0U;
2593
2594 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2595 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2596 LookThroughSetCC(LHS, RHS, CC, SPCC);
2597 assert(LHS.getValueType() == RHS.getValueType());
2598
2599 // Get the condition flag.
2600 SDValue CompareFlag;
2601 if (LHS.getValueType().isInteger()) {
2602 // On V9 processors running in 64-bit mode, if CC compares two `i64`s
2603 // and the RHS is zero we might be able to use a specialized branch.
2604 if (is64Bit && isV9 && LHS.getValueType() == MVT::i64 &&
2605 isNullConstant(RHS) && !ISD::isUnsignedIntSetCC(CC))
2606 return DAG.getNode(SPISD::BR_REG, dl, MVT::Other, Chain, Dest,
2607 DAG.getConstant(intCondCCodeToRcond(CC), dl, MVT::i32),
2608 LHS);
2609
2610 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
2611 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2612 if (isV9)
2613 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2614 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BPICC : SPISD::BPXCC;
2615 else
2616 // Non-v9 targets don't have xcc.
2617 Opc = SPISD::BRICC;
2618 } else {
2619 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2620 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2621 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2622 Opc = isV9 ? SPISD::BPICC : SPISD::BRICC;
2623 } else {
2624 unsigned CmpOpc = isV9 ? SPISD::CMPFCC_V9 : SPISD::CMPFCC;
2625 CompareFlag = DAG.getNode(CmpOpc, dl, MVT::Glue, LHS, RHS);
2626 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2627 Opc = isV9 ? SPISD::BRFCC_V9 : SPISD::BRFCC;
2628 }
2629 }
2630 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
2631 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
2632 }
2633
LowerSELECT_CC(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI,bool hasHardQuad,bool isV9,bool is64Bit)2634 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2635 const SparcTargetLowering &TLI, bool hasHardQuad,
2636 bool isV9, bool is64Bit) {
2637 SDValue LHS = Op.getOperand(0);
2638 SDValue RHS = Op.getOperand(1);
2639 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
2640 SDValue TrueVal = Op.getOperand(2);
2641 SDValue FalseVal = Op.getOperand(3);
2642 SDLoc dl(Op);
2643 unsigned Opc, SPCC = ~0U;
2644
2645 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2646 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2647 LookThroughSetCC(LHS, RHS, CC, SPCC);
2648 assert(LHS.getValueType() == RHS.getValueType());
2649
2650 SDValue CompareFlag;
2651 if (LHS.getValueType().isInteger()) {
2652 // On V9 processors running in 64-bit mode, if CC compares two `i64`s
2653 // and the RHS is zero we might be able to use a specialized select.
2654 // All SELECT_CC between any two scalar integer types are eligible for
2655 // lowering to specialized instructions. Additionally, f32 and f64 types
2656 // are also eligible, but for f128 we can only use the specialized
2657 // instruction when we have hardquad.
2658 EVT ValType = TrueVal.getValueType();
2659 bool IsEligibleType = ValType.isScalarInteger() || ValType == MVT::f32 ||
2660 ValType == MVT::f64 ||
2661 (ValType == MVT::f128 && hasHardQuad);
2662 if (is64Bit && isV9 && LHS.getValueType() == MVT::i64 &&
2663 isNullConstant(RHS) && !ISD::isUnsignedIntSetCC(CC) && IsEligibleType)
2664 return DAG.getNode(
2665 SPISD::SELECT_REG, dl, TrueVal.getValueType(), TrueVal, FalseVal,
2666 DAG.getConstant(intCondCCodeToRcond(CC), dl, MVT::i32), LHS);
2667
2668 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
2669 Opc = LHS.getValueType() == MVT::i32 ?
2670 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
2671 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2672 } else {
2673 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2674 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2675 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2676 Opc = SPISD::SELECT_ICC;
2677 } else {
2678 unsigned CmpOpc = isV9 ? SPISD::CMPFCC_V9 : SPISD::CMPFCC;
2679 CompareFlag = DAG.getNode(CmpOpc, dl, MVT::Glue, LHS, RHS);
2680 Opc = SPISD::SELECT_FCC;
2681 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2682 }
2683 }
2684 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
2685 DAG.getConstant(SPCC, dl, MVT::i32), CompareFlag);
2686 }
2687
LowerVASTART(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI)2688 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
2689 const SparcTargetLowering &TLI) {
2690 MachineFunction &MF = DAG.getMachineFunction();
2691 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
2692 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2693
2694 // Need frame address to find the address of VarArgsFrameIndex.
2695 MF.getFrameInfo().setFrameAddressIsTaken(true);
2696
2697 // vastart just stores the address of the VarArgsFrameIndex slot into the
2698 // memory location argument.
2699 SDLoc DL(Op);
2700 SDValue Offset =
2701 DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(SP::I6, PtrVT),
2702 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
2703 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2704 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
2705 MachinePointerInfo(SV));
2706 }
2707
LowerVAARG(SDValue Op,SelectionDAG & DAG)2708 static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
2709 SDNode *Node = Op.getNode();
2710 EVT VT = Node->getValueType(0);
2711 SDValue InChain = Node->getOperand(0);
2712 SDValue VAListPtr = Node->getOperand(1);
2713 EVT PtrVT = VAListPtr.getValueType();
2714 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2715 SDLoc DL(Node);
2716 SDValue VAList =
2717 DAG.getLoad(PtrVT, DL, InChain, VAListPtr, MachinePointerInfo(SV));
2718 // Increment the pointer, VAList, to the next vaarg.
2719 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
2720 DAG.getIntPtrConstant(VT.getSizeInBits()/8,
2721 DL));
2722 // Store the incremented VAList to the legalized pointer.
2723 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr, VAListPtr,
2724 MachinePointerInfo(SV));
2725 // Load the actual argument out of the pointer VAList.
2726 // We can't count on greater alignment than the word size.
2727 return DAG.getLoad(
2728 VT, DL, InChain, VAList, MachinePointerInfo(),
2729 Align(std::min(PtrVT.getFixedSizeInBits(), VT.getFixedSizeInBits()) / 8));
2730 }
2731
LowerDYNAMIC_STACKALLOC(SDValue Op,SelectionDAG & DAG,const SparcSubtarget * Subtarget)2732 static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
2733 const SparcSubtarget *Subtarget) {
2734 SDValue Chain = Op.getOperand(0);
2735 SDValue Size = Op.getOperand(1);
2736 SDValue Alignment = Op.getOperand(2);
2737 MaybeAlign MaybeAlignment =
2738 cast<ConstantSDNode>(Alignment)->getMaybeAlignValue();
2739 EVT VT = Size->getValueType(0);
2740 SDLoc dl(Op);
2741
2742 unsigned SPReg = SP::O6;
2743 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2744
2745 // The resultant pointer needs to be above the register spill area
2746 // at the bottom of the stack.
2747 unsigned regSpillArea;
2748 if (Subtarget->is64Bit()) {
2749 regSpillArea = 128;
2750 } else {
2751 // On Sparc32, the size of the spill area is 92. Unfortunately,
2752 // that's only 4-byte aligned, not 8-byte aligned (the stack
2753 // pointer is 8-byte aligned). So, if the user asked for an 8-byte
2754 // aligned dynamic allocation, we actually need to add 96 to the
2755 // bottom of the stack, instead of 92, to ensure 8-byte alignment.
2756
2757 // That also means adding 4 to the size of the allocation --
2758 // before applying the 8-byte rounding. Unfortunately, we the
2759 // value we get here has already had rounding applied. So, we need
2760 // to add 8, instead, wasting a bit more memory.
2761
2762 // Further, this only actually needs to be done if the required
2763 // alignment is > 4, but, we've lost that info by this point, too,
2764 // so we always apply it.
2765
2766 // (An alternative approach would be to always reserve 96 bytes
2767 // instead of the required 92, but then we'd waste 4 extra bytes
2768 // in every frame, not just those with dynamic stack allocations)
2769
2770 // TODO: modify code in SelectionDAGBuilder to make this less sad.
2771
2772 Size = DAG.getNode(ISD::ADD, dl, VT, Size,
2773 DAG.getConstant(8, dl, VT));
2774 regSpillArea = 96;
2775 }
2776
2777 int64_t Bias = Subtarget->getStackPointerBias();
2778
2779 // Debias and increment SP past the reserved spill area.
2780 // We need the SP to point to the first usable region before calculating
2781 // anything to prevent any of the pointers from becoming out of alignment when
2782 // we rebias the SP later on.
2783 SDValue StartOfUsableStack = DAG.getNode(
2784 ISD::ADD, dl, VT, SP, DAG.getConstant(regSpillArea + Bias, dl, VT));
2785 SDValue AllocatedPtr =
2786 DAG.getNode(ISD::SUB, dl, VT, StartOfUsableStack, Size);
2787
2788 bool IsOveraligned = MaybeAlignment.has_value();
2789 SDValue AlignedPtr =
2790 IsOveraligned
2791 ? DAG.getNode(ISD::AND, dl, VT, AllocatedPtr,
2792 DAG.getSignedConstant(-MaybeAlignment->value(), dl, VT))
2793 : AllocatedPtr;
2794
2795 // Now that we are done, restore the bias and reserved spill area.
2796 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, AlignedPtr,
2797 DAG.getConstant(regSpillArea + Bias, dl, VT));
2798 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP);
2799 SDValue Ops[2] = {AlignedPtr, Chain};
2800 return DAG.getMergeValues(Ops, dl);
2801 }
2802
2803
getFLUSHW(SDValue Op,SelectionDAG & DAG)2804 static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
2805 SDLoc dl(Op);
2806 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
2807 dl, MVT::Other, DAG.getEntryNode());
2808 return Chain;
2809 }
2810
getFRAMEADDR(uint64_t depth,SDValue Op,SelectionDAG & DAG,const SparcSubtarget * Subtarget,bool AlwaysFlush=false)2811 static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2812 const SparcSubtarget *Subtarget,
2813 bool AlwaysFlush = false) {
2814 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2815 MFI.setFrameAddressIsTaken(true);
2816
2817 EVT VT = Op.getValueType();
2818 SDLoc dl(Op);
2819 unsigned FrameReg = SP::I6;
2820 unsigned stackBias = Subtarget->getStackPointerBias();
2821
2822 SDValue FrameAddr;
2823 SDValue Chain;
2824
2825 // flush first to make sure the windowed registers' values are in stack
2826 Chain = (depth || AlwaysFlush) ? getFLUSHW(Op, DAG) : DAG.getEntryNode();
2827
2828 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2829
2830 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2831
2832 while (depth--) {
2833 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
2834 DAG.getIntPtrConstant(Offset, dl));
2835 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo());
2836 }
2837 if (Subtarget->is64Bit())
2838 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
2839 DAG.getIntPtrConstant(stackBias, dl));
2840 return FrameAddr;
2841 }
2842
2843
LowerFRAMEADDR(SDValue Op,SelectionDAG & DAG,const SparcSubtarget * Subtarget)2844 static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2845 const SparcSubtarget *Subtarget) {
2846
2847 uint64_t depth = Op.getConstantOperandVal(0);
2848
2849 return getFRAMEADDR(depth, Op, DAG, Subtarget);
2850
2851 }
2852
LowerRETURNADDR(SDValue Op,SelectionDAG & DAG,const SparcTargetLowering & TLI,const SparcSubtarget * Subtarget)2853 static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
2854 const SparcTargetLowering &TLI,
2855 const SparcSubtarget *Subtarget) {
2856 MachineFunction &MF = DAG.getMachineFunction();
2857 MachineFrameInfo &MFI = MF.getFrameInfo();
2858 MFI.setReturnAddressIsTaken(true);
2859
2860 EVT VT = Op.getValueType();
2861 SDLoc dl(Op);
2862 uint64_t depth = Op.getConstantOperandVal(0);
2863
2864 SDValue RetAddr;
2865 if (depth == 0) {
2866 auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
2867 Register RetReg = MF.addLiveIn(SP::I7, TLI.getRegClassFor(PtrVT));
2868 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
2869 return RetAddr;
2870 }
2871
2872 // Need frame address to find return address of the caller.
2873 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget, true);
2874
2875 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2876 SDValue Ptr = DAG.getNode(ISD::ADD,
2877 dl, VT,
2878 FrameAddr,
2879 DAG.getIntPtrConstant(Offset, dl));
2880 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr, MachinePointerInfo());
2881
2882 return RetAddr;
2883 }
2884
LowerF64Op(SDValue SrcReg64,const SDLoc & dl,SelectionDAG & DAG,unsigned opcode)2885 static SDValue LowerF64Op(SDValue SrcReg64, const SDLoc &dl, SelectionDAG &DAG,
2886 unsigned opcode) {
2887 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
2888 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
2889
2890 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2891 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2892 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2893
2894 // Note: in little-endian, the floating-point value is stored in the
2895 // registers are in the opposite order, so the subreg with the sign
2896 // bit is the highest-numbered (odd), rather than the
2897 // lowest-numbered (even).
2898
2899 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2900 SrcReg64);
2901 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2902 SrcReg64);
2903
2904 if (DAG.getDataLayout().isLittleEndian())
2905 Lo32 = DAG.getNode(opcode, dl, MVT::f32, Lo32);
2906 else
2907 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
2908
2909 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2910 dl, MVT::f64), 0);
2911 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2912 DstReg64, Hi32);
2913 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2914 DstReg64, Lo32);
2915 return DstReg64;
2916 }
2917
2918 // Lower a f128 load into two f64 loads.
LowerF128Load(SDValue Op,SelectionDAG & DAG)2919 static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2920 {
2921 SDLoc dl(Op);
2922 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2923 assert(LdNode->getOffset().isUndef() && "Unexpected node type");
2924
2925 Align Alignment = commonAlignment(LdNode->getBaseAlign(), 8);
2926
2927 SDValue Hi64 =
2928 DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LdNode->getBasePtr(),
2929 LdNode->getPointerInfo(), Alignment);
2930 EVT addrVT = LdNode->getBasePtr().getValueType();
2931 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2932 LdNode->getBasePtr(),
2933 DAG.getConstant(8, dl, addrVT));
2934 SDValue Lo64 = DAG.getLoad(MVT::f64, dl, LdNode->getChain(), LoPtr,
2935 LdNode->getPointerInfo().getWithOffset(8),
2936 Alignment);
2937
2938 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2939 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
2940
2941 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2942 dl, MVT::f128);
2943 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2944 MVT::f128,
2945 SDValue(InFP128, 0),
2946 Hi64,
2947 SubRegEven);
2948 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2949 MVT::f128,
2950 SDValue(InFP128, 0),
2951 Lo64,
2952 SubRegOdd);
2953 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2954 SDValue(Lo64.getNode(), 1) };
2955 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
2956 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
2957 return DAG.getMergeValues(Ops, dl);
2958 }
2959
LowerLOAD(SDValue Op,SelectionDAG & DAG)2960 static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
2961 {
2962 LoadSDNode *LdNode = cast<LoadSDNode>(Op.getNode());
2963
2964 EVT MemVT = LdNode->getMemoryVT();
2965 if (MemVT == MVT::f128)
2966 return LowerF128Load(Op, DAG);
2967
2968 return Op;
2969 }
2970
2971 // Lower a f128 store into two f64 stores.
LowerF128Store(SDValue Op,SelectionDAG & DAG)2972 static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2973 SDLoc dl(Op);
2974 StoreSDNode *StNode = cast<StoreSDNode>(Op.getNode());
2975 assert(StNode->getOffset().isUndef() && "Unexpected node type");
2976
2977 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, dl, MVT::i32);
2978 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, dl, MVT::i32);
2979
2980 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2981 dl,
2982 MVT::f64,
2983 StNode->getValue(),
2984 SubRegEven);
2985 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2986 dl,
2987 MVT::f64,
2988 StNode->getValue(),
2989 SubRegOdd);
2990
2991 Align Alignment = commonAlignment(StNode->getBaseAlign(), 8);
2992
2993 SDValue OutChains[2];
2994 OutChains[0] =
2995 DAG.getStore(StNode->getChain(), dl, SDValue(Hi64, 0),
2996 StNode->getBasePtr(), StNode->getPointerInfo(),
2997 Alignment);
2998 EVT addrVT = StNode->getBasePtr().getValueType();
2999 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
3000 StNode->getBasePtr(),
3001 DAG.getConstant(8, dl, addrVT));
3002 OutChains[1] = DAG.getStore(StNode->getChain(), dl, SDValue(Lo64, 0), LoPtr,
3003 StNode->getPointerInfo().getWithOffset(8),
3004 Alignment);
3005 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
3006 }
3007
LowerSTORE(SDValue Op,SelectionDAG & DAG)3008 static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
3009 {
3010 SDLoc dl(Op);
3011 StoreSDNode *St = cast<StoreSDNode>(Op.getNode());
3012
3013 EVT MemVT = St->getMemoryVT();
3014 if (MemVT == MVT::f128)
3015 return LowerF128Store(Op, DAG);
3016
3017 if (MemVT == MVT::i64) {
3018 // Custom handling for i64 stores: turn it into a bitcast and a
3019 // v2i32 store.
3020 SDValue Val = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, St->getValue());
3021 SDValue Chain = DAG.getStore(
3022 St->getChain(), dl, Val, St->getBasePtr(), St->getPointerInfo(),
3023 St->getBaseAlign(), St->getMemOperand()->getFlags(), St->getAAInfo());
3024 return Chain;
3025 }
3026
3027 return SDValue();
3028 }
3029
LowerFNEGorFABS(SDValue Op,SelectionDAG & DAG,bool isV9)3030 static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
3031 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
3032 && "invalid opcode");
3033
3034 SDLoc dl(Op);
3035
3036 if (Op.getValueType() == MVT::f64)
3037 return LowerF64Op(Op.getOperand(0), dl, DAG, Op.getOpcode());
3038 if (Op.getValueType() != MVT::f128)
3039 return Op;
3040
3041 // Lower fabs/fneg on f128 to fabs/fneg on f64
3042 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
3043 // (As with LowerF64Op, on little-endian, we need to negate the odd
3044 // subreg)
3045
3046 SDValue SrcReg128 = Op.getOperand(0);
3047 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
3048 SrcReg128);
3049 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
3050 SrcReg128);
3051
3052 if (DAG.getDataLayout().isLittleEndian()) {
3053 if (isV9)
3054 Lo64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Lo64);
3055 else
3056 Lo64 = LowerF64Op(Lo64, dl, DAG, Op.getOpcode());
3057 } else {
3058 if (isV9)
3059 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
3060 else
3061 Hi64 = LowerF64Op(Hi64, dl, DAG, Op.getOpcode());
3062 }
3063
3064 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
3065 dl, MVT::f128), 0);
3066 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
3067 DstReg128, Hi64);
3068 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
3069 DstReg128, Lo64);
3070 return DstReg128;
3071 }
3072
LowerATOMIC_LOAD_STORE(SDValue Op,SelectionDAG & DAG)3073 static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
3074 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getSuccessOrdering())) {
3075 // Expand with a fence.
3076 return SDValue();
3077 }
3078
3079 // Monotonic load/stores are legal.
3080 return Op;
3081 }
3082
LowerINTRINSIC_WO_CHAIN(SDValue Op,SelectionDAG & DAG) const3083 SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
3084 SelectionDAG &DAG) const {
3085 unsigned IntNo = Op.getConstantOperandVal(0);
3086 switch (IntNo) {
3087 default: return SDValue(); // Don't custom lower most intrinsics.
3088 case Intrinsic::thread_pointer: {
3089 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3090 return DAG.getRegister(SP::G7, PtrVT);
3091 }
3092 }
3093 }
3094
3095 SDValue SparcTargetLowering::
LowerOperation(SDValue Op,SelectionDAG & DAG) const3096 LowerOperation(SDValue Op, SelectionDAG &DAG) const {
3097
3098 bool hasHardQuad = Subtarget->hasHardQuad();
3099 bool isV9 = Subtarget->isV9();
3100 bool is64Bit = Subtarget->is64Bit();
3101
3102 switch (Op.getOpcode()) {
3103 default: llvm_unreachable("Should not custom lower this!");
3104
3105 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
3106 Subtarget);
3107 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
3108 Subtarget);
3109 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
3110 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3111 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
3112 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3113 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
3114 hasHardQuad);
3115 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
3116 hasHardQuad);
3117 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
3118 hasHardQuad);
3119 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
3120 hasHardQuad);
3121 case ISD::BR_CC:
3122 return LowerBR_CC(Op, DAG, *this, hasHardQuad, isV9, is64Bit);
3123 case ISD::SELECT_CC:
3124 return LowerSELECT_CC(Op, DAG, *this, hasHardQuad, isV9, is64Bit);
3125 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
3126 case ISD::VAARG: return LowerVAARG(Op, DAG);
3127 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
3128 Subtarget);
3129
3130 case ISD::LOAD: return LowerLOAD(Op, DAG);
3131 case ISD::STORE: return LowerSTORE(Op, DAG);
3132 case ISD::FADD: return LowerF128Op(Op, DAG,
3133 getLibcallName(RTLIB::ADD_F128), 2);
3134 case ISD::FSUB: return LowerF128Op(Op, DAG,
3135 getLibcallName(RTLIB::SUB_F128), 2);
3136 case ISD::FMUL: return LowerF128Op(Op, DAG,
3137 getLibcallName(RTLIB::MUL_F128), 2);
3138 case ISD::FDIV: return LowerF128Op(Op, DAG,
3139 getLibcallName(RTLIB::DIV_F128), 2);
3140 case ISD::FSQRT: return LowerF128Op(Op, DAG,
3141 getLibcallName(RTLIB::SQRT_F128),1);
3142 case ISD::FABS:
3143 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
3144 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
3145 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
3146 case ISD::ATOMIC_LOAD:
3147 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
3148 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3149 }
3150 }
3151
bitcastConstantFPToInt(ConstantFPSDNode * C,const SDLoc & DL,SelectionDAG & DAG) const3152 SDValue SparcTargetLowering::bitcastConstantFPToInt(ConstantFPSDNode *C,
3153 const SDLoc &DL,
3154 SelectionDAG &DAG) const {
3155 APInt V = C->getValueAPF().bitcastToAPInt();
3156 SDValue Lo = DAG.getConstant(V.zextOrTrunc(32), DL, MVT::i32);
3157 SDValue Hi = DAG.getConstant(V.lshr(32).zextOrTrunc(32), DL, MVT::i32);
3158 if (DAG.getDataLayout().isLittleEndian())
3159 std::swap(Lo, Hi);
3160 return DAG.getBuildVector(MVT::v2i32, DL, {Hi, Lo});
3161 }
3162
PerformBITCASTCombine(SDNode * N,DAGCombinerInfo & DCI) const3163 SDValue SparcTargetLowering::PerformBITCASTCombine(SDNode *N,
3164 DAGCombinerInfo &DCI) const {
3165 SDLoc dl(N);
3166 SDValue Src = N->getOperand(0);
3167
3168 if (isa<ConstantFPSDNode>(Src) && N->getSimpleValueType(0) == MVT::v2i32 &&
3169 Src.getSimpleValueType() == MVT::f64)
3170 return bitcastConstantFPToInt(cast<ConstantFPSDNode>(Src), dl, DCI.DAG);
3171
3172 return SDValue();
3173 }
3174
PerformDAGCombine(SDNode * N,DAGCombinerInfo & DCI) const3175 SDValue SparcTargetLowering::PerformDAGCombine(SDNode *N,
3176 DAGCombinerInfo &DCI) const {
3177 switch (N->getOpcode()) {
3178 default:
3179 break;
3180 case ISD::BITCAST:
3181 return PerformBITCASTCombine(N, DCI);
3182 }
3183 return SDValue();
3184 }
3185
3186 MachineBasicBlock *
EmitInstrWithCustomInserter(MachineInstr & MI,MachineBasicBlock * BB) const3187 SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
3188 MachineBasicBlock *BB) const {
3189 switch (MI.getOpcode()) {
3190 default: llvm_unreachable("Unknown SELECT_CC!");
3191 case SP::SELECT_CC_Int_ICC:
3192 case SP::SELECT_CC_FP_ICC:
3193 case SP::SELECT_CC_DFP_ICC:
3194 case SP::SELECT_CC_QFP_ICC:
3195 if (Subtarget->isV9())
3196 return expandSelectCC(MI, BB, SP::BPICC);
3197 return expandSelectCC(MI, BB, SP::BCOND);
3198 case SP::SELECT_CC_Int_XCC:
3199 case SP::SELECT_CC_FP_XCC:
3200 case SP::SELECT_CC_DFP_XCC:
3201 case SP::SELECT_CC_QFP_XCC:
3202 return expandSelectCC(MI, BB, SP::BPXCC);
3203 case SP::SELECT_CC_Int_FCC:
3204 case SP::SELECT_CC_FP_FCC:
3205 case SP::SELECT_CC_DFP_FCC:
3206 case SP::SELECT_CC_QFP_FCC:
3207 if (Subtarget->isV9())
3208 return expandSelectCC(MI, BB, SP::FBCOND_V9);
3209 return expandSelectCC(MI, BB, SP::FBCOND);
3210 }
3211 }
3212
3213 MachineBasicBlock *
expandSelectCC(MachineInstr & MI,MachineBasicBlock * BB,unsigned BROpcode) const3214 SparcTargetLowering::expandSelectCC(MachineInstr &MI, MachineBasicBlock *BB,
3215 unsigned BROpcode) const {
3216 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
3217 DebugLoc dl = MI.getDebugLoc();
3218 unsigned CC = (SPCC::CondCodes)MI.getOperand(3).getImm();
3219
3220 // To "insert" a SELECT_CC instruction, we actually have to insert the
3221 // triangle control-flow pattern. The incoming instruction knows the
3222 // destination vreg to set, the condition code register to branch on, the
3223 // true/false values to select between, and the condition code for the branch.
3224 //
3225 // We produce the following control flow:
3226 // ThisMBB
3227 // | \
3228 // | IfFalseMBB
3229 // | /
3230 // SinkMBB
3231 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3232 MachineFunction::iterator It = ++BB->getIterator();
3233
3234 MachineBasicBlock *ThisMBB = BB;
3235 MachineFunction *F = BB->getParent();
3236 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
3237 MachineBasicBlock *SinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3238 F->insert(It, IfFalseMBB);
3239 F->insert(It, SinkMBB);
3240
3241 // Transfer the remainder of ThisMBB and its successor edges to SinkMBB.
3242 SinkMBB->splice(SinkMBB->begin(), ThisMBB,
3243 std::next(MachineBasicBlock::iterator(MI)), ThisMBB->end());
3244 SinkMBB->transferSuccessorsAndUpdatePHIs(ThisMBB);
3245
3246 // Set the new successors for ThisMBB.
3247 ThisMBB->addSuccessor(IfFalseMBB);
3248 ThisMBB->addSuccessor(SinkMBB);
3249
3250 BuildMI(ThisMBB, dl, TII.get(BROpcode))
3251 .addMBB(SinkMBB)
3252 .addImm(CC);
3253
3254 // IfFalseMBB just falls through to SinkMBB.
3255 IfFalseMBB->addSuccessor(SinkMBB);
3256
3257 // %Result = phi [ %TrueValue, ThisMBB ], [ %FalseValue, IfFalseMBB ]
3258 BuildMI(*SinkMBB, SinkMBB->begin(), dl, TII.get(SP::PHI),
3259 MI.getOperand(0).getReg())
3260 .addReg(MI.getOperand(1).getReg())
3261 .addMBB(ThisMBB)
3262 .addReg(MI.getOperand(2).getReg())
3263 .addMBB(IfFalseMBB);
3264
3265 MI.eraseFromParent(); // The pseudo instruction is gone now.
3266 return SinkMBB;
3267 }
3268
3269 //===----------------------------------------------------------------------===//
3270 // Sparc Inline Assembly Support
3271 //===----------------------------------------------------------------------===//
3272
3273 /// getConstraintType - Given a constraint letter, return the type of
3274 /// constraint it is for this target.
3275 SparcTargetLowering::ConstraintType
getConstraintType(StringRef Constraint) const3276 SparcTargetLowering::getConstraintType(StringRef Constraint) const {
3277 if (Constraint.size() == 1) {
3278 switch (Constraint[0]) {
3279 default: break;
3280 case 'r':
3281 case 'f':
3282 case 'e':
3283 return C_RegisterClass;
3284 case 'I': // SIMM13
3285 return C_Immediate;
3286 }
3287 }
3288
3289 return TargetLowering::getConstraintType(Constraint);
3290 }
3291
3292 TargetLowering::ConstraintWeight SparcTargetLowering::
getSingleConstraintMatchWeight(AsmOperandInfo & info,const char * constraint) const3293 getSingleConstraintMatchWeight(AsmOperandInfo &info,
3294 const char *constraint) const {
3295 ConstraintWeight weight = CW_Invalid;
3296 Value *CallOperandVal = info.CallOperandVal;
3297 // If we don't have a value, we can't do a match,
3298 // but allow it at the lowest weight.
3299 if (!CallOperandVal)
3300 return CW_Default;
3301
3302 // Look at the constraint type.
3303 switch (*constraint) {
3304 default:
3305 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3306 break;
3307 case 'I': // SIMM13
3308 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3309 if (isInt<13>(C->getSExtValue()))
3310 weight = CW_Constant;
3311 }
3312 break;
3313 }
3314 return weight;
3315 }
3316
3317 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3318 /// vector. If it is invalid, don't add anything to Ops.
LowerAsmOperandForConstraint(SDValue Op,StringRef Constraint,std::vector<SDValue> & Ops,SelectionDAG & DAG) const3319 void SparcTargetLowering::LowerAsmOperandForConstraint(
3320 SDValue Op, StringRef Constraint, std::vector<SDValue> &Ops,
3321 SelectionDAG &DAG) const {
3322 SDValue Result;
3323
3324 // Only support length 1 constraints for now.
3325 if (Constraint.size() > 1)
3326 return;
3327
3328 char ConstraintLetter = Constraint[0];
3329 switch (ConstraintLetter) {
3330 default: break;
3331 case 'I':
3332 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3333 if (isInt<13>(C->getSExtValue())) {
3334 Result = DAG.getSignedTargetConstant(C->getSExtValue(), SDLoc(Op),
3335 Op.getValueType());
3336 break;
3337 }
3338 return;
3339 }
3340 }
3341
3342 if (Result.getNode()) {
3343 Ops.push_back(Result);
3344 return;
3345 }
3346 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3347 }
3348
3349 std::pair<unsigned, const TargetRegisterClass *>
getRegForInlineAsmConstraint(const TargetRegisterInfo * TRI,StringRef Constraint,MVT VT) const3350 SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
3351 StringRef Constraint,
3352 MVT VT) const {
3353 if (Constraint.empty())
3354 return std::make_pair(0U, nullptr);
3355
3356 if (Constraint.size() == 1) {
3357 switch (Constraint[0]) {
3358 case 'r':
3359 if (VT == MVT::v2i32)
3360 return std::make_pair(0U, &SP::IntPairRegClass);
3361 else if (Subtarget->is64Bit())
3362 return std::make_pair(0U, &SP::I64RegsRegClass);
3363 else
3364 return std::make_pair(0U, &SP::IntRegsRegClass);
3365 case 'f':
3366 if (VT == MVT::f32 || VT == MVT::i32)
3367 return std::make_pair(0U, &SP::FPRegsRegClass);
3368 else if (VT == MVT::f64 || VT == MVT::i64)
3369 return std::make_pair(0U, &SP::LowDFPRegsRegClass);
3370 else if (VT == MVT::f128)
3371 return std::make_pair(0U, &SP::LowQFPRegsRegClass);
3372 // This will generate an error message
3373 return std::make_pair(0U, nullptr);
3374 case 'e':
3375 if (VT == MVT::f32 || VT == MVT::i32)
3376 return std::make_pair(0U, &SP::FPRegsRegClass);
3377 else if (VT == MVT::f64 || VT == MVT::i64 )
3378 return std::make_pair(0U, &SP::DFPRegsRegClass);
3379 else if (VT == MVT::f128)
3380 return std::make_pair(0U, &SP::QFPRegsRegClass);
3381 // This will generate an error message
3382 return std::make_pair(0U, nullptr);
3383 }
3384 }
3385
3386 if (Constraint.front() != '{')
3387 return std::make_pair(0U, nullptr);
3388
3389 assert(Constraint.back() == '}' && "Not a brace enclosed constraint?");
3390 StringRef RegName(Constraint.data() + 1, Constraint.size() - 2);
3391 if (RegName.empty())
3392 return std::make_pair(0U, nullptr);
3393
3394 unsigned long long RegNo;
3395 // Handle numbered register aliases.
3396 if (RegName[0] == 'r' &&
3397 getAsUnsignedInteger(RegName.begin() + 1, 10, RegNo)) {
3398 // r0-r7 -> g0-g7
3399 // r8-r15 -> o0-o7
3400 // r16-r23 -> l0-l7
3401 // r24-r31 -> i0-i7
3402 if (RegNo > 31)
3403 return std::make_pair(0U, nullptr);
3404 const char RegTypes[] = {'g', 'o', 'l', 'i'};
3405 char RegType = RegTypes[RegNo / 8];
3406 char RegIndex = '0' + (RegNo % 8);
3407 char Tmp[] = {'{', RegType, RegIndex, '}', 0};
3408 return getRegForInlineAsmConstraint(TRI, Tmp, VT);
3409 }
3410
3411 // Rewrite the fN constraint according to the value type if needed.
3412 if (VT != MVT::f32 && VT != MVT::Other && RegName[0] == 'f' &&
3413 getAsUnsignedInteger(RegName.begin() + 1, 10, RegNo)) {
3414 if (VT == MVT::f64 && (RegNo % 2 == 0)) {
3415 return getRegForInlineAsmConstraint(
3416 TRI, StringRef("{d" + utostr(RegNo / 2) + "}"), VT);
3417 } else if (VT == MVT::f128 && (RegNo % 4 == 0)) {
3418 return getRegForInlineAsmConstraint(
3419 TRI, StringRef("{q" + utostr(RegNo / 4) + "}"), VT);
3420 } else {
3421 return std::make_pair(0U, nullptr);
3422 }
3423 }
3424
3425 auto ResultPair =
3426 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3427 if (!ResultPair.second)
3428 return std::make_pair(0U, nullptr);
3429
3430 // Force the use of I64Regs over IntRegs for 64-bit values.
3431 if (Subtarget->is64Bit() && VT == MVT::i64) {
3432 assert(ResultPair.second == &SP::IntRegsRegClass &&
3433 "Unexpected register class");
3434 return std::make_pair(ResultPair.first, &SP::I64RegsRegClass);
3435 }
3436
3437 return ResultPair;
3438 }
3439
3440 bool
isOffsetFoldingLegal(const GlobalAddressSDNode * GA) const3441 SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3442 // The Sparc target isn't yet aware of offsets.
3443 return false;
3444 }
3445
ReplaceNodeResults(SDNode * N,SmallVectorImpl<SDValue> & Results,SelectionDAG & DAG) const3446 void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3447 SmallVectorImpl<SDValue>& Results,
3448 SelectionDAG &DAG) const {
3449
3450 SDLoc dl(N);
3451
3452 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3453
3454 switch (N->getOpcode()) {
3455 default:
3456 llvm_unreachable("Do not know how to custom type legalize this operation!");
3457
3458 case ISD::FP_TO_SINT:
3459 case ISD::FP_TO_UINT:
3460 // Custom lower only if it involves f128 or i64.
3461 if (N->getOperand(0).getValueType() != MVT::f128
3462 || N->getValueType(0) != MVT::i64)
3463 return;
3464 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3465 ? RTLIB::FPTOSINT_F128_I64
3466 : RTLIB::FPTOUINT_F128_I64);
3467
3468 Results.push_back(LowerF128Op(SDValue(N, 0),
3469 DAG,
3470 getLibcallName(libCall),
3471 1));
3472 return;
3473 case ISD::READCYCLECOUNTER: {
3474 assert(Subtarget->hasLeonCycleCounter());
3475 SDValue Lo = DAG.getCopyFromReg(N->getOperand(0), dl, SP::ASR23, MVT::i32);
3476 SDValue Hi = DAG.getCopyFromReg(Lo, dl, SP::G0, MVT::i32);
3477 SDValue Ops[] = { Lo, Hi };
3478 SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops);
3479 Results.push_back(Pair);
3480 Results.push_back(N->getOperand(0));
3481 return;
3482 }
3483 case ISD::SINT_TO_FP:
3484 case ISD::UINT_TO_FP:
3485 // Custom lower only if it involves f128 or i64.
3486 if (N->getValueType(0) != MVT::f128
3487 || N->getOperand(0).getValueType() != MVT::i64)
3488 return;
3489
3490 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3491 ? RTLIB::SINTTOFP_I64_F128
3492 : RTLIB::UINTTOFP_I64_F128);
3493
3494 Results.push_back(LowerF128Op(SDValue(N, 0),
3495 DAG,
3496 getLibcallName(libCall),
3497 1));
3498 return;
3499 case ISD::LOAD: {
3500 LoadSDNode *Ld = cast<LoadSDNode>(N);
3501 // Custom handling only for i64: turn i64 load into a v2i32 load,
3502 // and a bitcast.
3503 if (Ld->getValueType(0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64)
3504 return;
3505
3506 SDLoc dl(N);
3507 SDValue LoadRes = DAG.getExtLoad(
3508 Ld->getExtensionType(), dl, MVT::v2i32, Ld->getChain(),
3509 Ld->getBasePtr(), Ld->getPointerInfo(), MVT::v2i32, Ld->getBaseAlign(),
3510 Ld->getMemOperand()->getFlags(), Ld->getAAInfo());
3511
3512 SDValue Res = DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes);
3513 Results.push_back(Res);
3514 Results.push_back(LoadRes.getValue(1));
3515 return;
3516 }
3517 }
3518 }
3519
3520 // Override to enable LOAD_STACK_GUARD lowering on Linux.
useLoadStackGuardNode(const Module & M) const3521 bool SparcTargetLowering::useLoadStackGuardNode(const Module &M) const {
3522 if (!Subtarget->isTargetLinux())
3523 return TargetLowering::useLoadStackGuardNode(M);
3524 return true;
3525 }
3526
isFNegFree(EVT VT) const3527 bool SparcTargetLowering::isFNegFree(EVT VT) const {
3528 if (Subtarget->isVIS3())
3529 return VT == MVT::f32 || VT == MVT::f64;
3530 return false;
3531 }
3532
isFPImmLegal(const APFloat & Imm,EVT VT,bool ForCodeSize) const3533 bool SparcTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
3534 bool ForCodeSize) const {
3535 if (VT != MVT::f32 && VT != MVT::f64)
3536 return false;
3537 if (Subtarget->isVIS() && Imm.isZero())
3538 return true;
3539 if (Subtarget->isVIS3())
3540 return Imm.isExactlyValue(+0.5) || Imm.isExactlyValue(-0.5) ||
3541 Imm.getExactLog2Abs() == -1;
3542 return false;
3543 }
3544
isCtlzFast() const3545 bool SparcTargetLowering::isCtlzFast() const { return Subtarget->isVIS3(); }
3546
isCheapToSpeculateCttz(Type * Ty) const3547 bool SparcTargetLowering::isCheapToSpeculateCttz(Type *Ty) const {
3548 // We lack native cttz, however,
3549 // On 64-bit targets it is cheap to implement it in terms of popc.
3550 if (Subtarget->is64Bit() && Subtarget->usePopc())
3551 return true;
3552 // Otherwise, implementing cttz in terms of ctlz is still cheap.
3553 return isCheapToSpeculateCtlz(Ty);
3554 }
3555
isFMAFasterThanFMulAndFAdd(const MachineFunction & MF,EVT VT) const3556 bool SparcTargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
3557 EVT VT) const {
3558 return Subtarget->isUA2007() && !Subtarget->useSoftFloat();
3559 }
3560
3561 // Override to disable global variable loading on Linux.
insertSSPDeclarations(Module & M) const3562 void SparcTargetLowering::insertSSPDeclarations(Module &M) const {
3563 if (!Subtarget->isTargetLinux())
3564 return TargetLowering::insertSSPDeclarations(M);
3565 }
3566
AdjustInstrPostInstrSelection(MachineInstr & MI,SDNode * Node) const3567 void SparcTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
3568 SDNode *Node) const {
3569 assert(MI.getOpcode() == SP::SUBCCrr || MI.getOpcode() == SP::SUBCCri);
3570 // If the result is dead, replace it with %g0.
3571 if (!Node->hasAnyUseOfValue(0))
3572 MI.getOperand(0).setReg(SP::G0);
3573 }
3574