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/IRBuilder.h"
37#include "llvm/IR/Module.h"
38#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/KnownBits.h"
40using namespace llvm;
41
42
43//===----------------------------------------------------------------------===//
44// Calling Convention Implementation
45//===----------------------------------------------------------------------===//
46
47static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
48 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
49 ISD::ArgFlagsTy &ArgFlags, CCState &State)
50{
51 assert (ArgFlags.isSRet());
52
53 // Assign SRet argument.
54 State.addLoc(V: CCValAssign::getCustomMem(ValNo, ValVT,
55 Offset: 0,
56 LocVT, HTP: LocInfo));
57 return true;
58}
59
60static bool CC_Sparc_Assign_Split_64(unsigned &ValNo, MVT &ValVT,
61 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
62 ISD::ArgFlagsTy &ArgFlags, CCState &State)
63{
64 static const MCPhysReg RegList[] = {
65 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
66 };
67 // Try to get first reg.
68 if (Register Reg = State.AllocateReg(Regs: RegList)) {
69 State.addLoc(V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
70 } else {
71 // Assign whole thing in stack.
72 State.addLoc(V: CCValAssign::getCustomMem(
73 ValNo, ValVT, Offset: State.AllocateStack(Size: 8, Alignment: Align(4)), LocVT, HTP: LocInfo));
74 return true;
75 }
76
77 // Try to get second reg.
78 if (Register Reg = State.AllocateReg(Regs: RegList))
79 State.addLoc(V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
80 else
81 State.addLoc(V: CCValAssign::getCustomMem(
82 ValNo, ValVT, Offset: State.AllocateStack(Size: 4, Alignment: Align(4)), LocVT, HTP: LocInfo));
83 return true;
84}
85
86static bool CC_Sparc_Assign_Ret_Split_64(unsigned &ValNo, MVT &ValVT,
87 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
88 ISD::ArgFlagsTy &ArgFlags, CCState &State)
89{
90 static const MCPhysReg RegList[] = {
91 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
92 };
93
94 // Try to get first reg.
95 if (Register Reg = State.AllocateReg(Regs: RegList))
96 State.addLoc(V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
97 else
98 return false;
99
100 // Try to get second reg.
101 if (Register Reg = State.AllocateReg(Regs: RegList))
102 State.addLoc(V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
103 else
104 return false;
105
106 return true;
107}
108
109// Allocate a full-sized argument for the 64-bit ABI.
110static bool Analyze_CC_Sparc64_Full(bool IsReturn, unsigned &ValNo, MVT &ValVT,
111 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
112 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
113 assert((LocVT == MVT::f32 || LocVT == MVT::f128
114 || LocVT.getSizeInBits() == 64) &&
115 "Can't handle non-64 bits locations");
116
117 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
118 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
119 Align alignment =
120 (LocVT == MVT::f128 || ArgFlags.isSplit()) ? Align(16) : Align(8);
121 unsigned Offset = State.AllocateStack(Size: size, Alignment: alignment);
122 unsigned Reg = 0;
123
124 if (LocVT == MVT::i64 && Offset < 6*8)
125 // Promote integers to %i0-%i5.
126 Reg = SP::I0 + Offset/8;
127 else if (LocVT == MVT::f64 && Offset < 16*8)
128 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
129 Reg = SP::D0 + Offset/8;
130 else if (LocVT == MVT::f32 && Offset < 16*8)
131 // Promote floats to %f1, %f3, ...
132 Reg = SP::F1 + Offset/4;
133 else if (LocVT == MVT::f128 && Offset < 16*8)
134 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
135 Reg = SP::Q0 + Offset/16;
136
137 // Promote to register when possible, otherwise use the stack slot.
138 if (Reg) {
139 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
140 return true;
141 }
142
143 // Bail out if this is a return CC and we run out of registers to place
144 // values into.
145 if (IsReturn)
146 return false;
147
148 // This argument goes on the stack in an 8-byte slot.
149 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
150 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
151 if (LocVT == MVT::f32)
152 Offset += 4;
153
154 State.addLoc(V: CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, HTP: LocInfo));
155 return true;
156}
157
158// Allocate a half-sized argument for the 64-bit ABI.
159//
160// This is used when passing { float, int } structs by value in registers.
161static bool Analyze_CC_Sparc64_Half(bool IsReturn, unsigned &ValNo, MVT &ValVT,
162 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
163 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
164 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
165 unsigned Offset = State.AllocateStack(Size: 4, Alignment: Align(4));
166
167 if (LocVT == MVT::f32 && Offset < 16*8) {
168 // Promote floats to %f0-%f31.
169 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg: SP::F0 + Offset/4,
170 LocVT, HTP: LocInfo));
171 return true;
172 }
173
174 if (LocVT == MVT::i32 && Offset < 6*8) {
175 // Promote integers to %i0-%i5, using half the register.
176 unsigned Reg = SP::I0 + Offset/8;
177 LocVT = MVT::i64;
178 LocInfo = CCValAssign::AExt;
179
180 // Set the Custom bit if this i32 goes in the high bits of a register.
181 if (Offset % 8 == 0)
182 State.addLoc(V: CCValAssign::getCustomReg(ValNo, ValVT, Reg,
183 LocVT, HTP: LocInfo));
184 else
185 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
186 return true;
187 }
188
189 // Bail out if this is a return CC and we run out of registers to place
190 // values into.
191 if (IsReturn)
192 return false;
193
194 State.addLoc(V: CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, HTP: LocInfo));
195 return true;
196}
197
198static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
199 CCValAssign::LocInfo &LocInfo,
200 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
201 return Analyze_CC_Sparc64_Full(IsReturn: false, ValNo, ValVT, LocVT, LocInfo, ArgFlags,
202 State);
203}
204
205static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
206 CCValAssign::LocInfo &LocInfo,
207 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
208 return Analyze_CC_Sparc64_Half(IsReturn: false, ValNo, ValVT, LocVT, LocInfo, ArgFlags,
209 State);
210}
211
212static bool RetCC_Sparc64_Full(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
213 CCValAssign::LocInfo &LocInfo,
214 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
215 return Analyze_CC_Sparc64_Full(IsReturn: true, ValNo, ValVT, LocVT, LocInfo, ArgFlags,
216 State);
217}
218
219static bool RetCC_Sparc64_Half(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
220 CCValAssign::LocInfo &LocInfo,
221 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
222 return Analyze_CC_Sparc64_Half(IsReturn: true, ValNo, ValVT, LocVT, LocInfo, ArgFlags,
223 State);
224}
225
226#define GET_CALLING_CONV_IMPL
227#include "SparcGenCallingConv.inc"
228
229// The calling conventions in SparcCallingConv.td are described in terms of the
230// callee's register window. This function translates registers to the
231// corresponding caller window %o register.
232static unsigned toCallerWindow(unsigned Reg) {
233 static_assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7,
234 "Unexpected enum");
235 if (Reg >= SP::I0 && Reg <= SP::I7)
236 return Reg - SP::I0 + SP::O0;
237 return Reg;
238}
239
240bool SparcTargetLowering::CanLowerReturn(
241 CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
242 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context,
243 const Type *RetTy) const {
244 SmallVector<CCValAssign, 16> RVLocs;
245 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
246 return CCInfo.CheckReturn(Outs, Fn: Subtarget->is64Bit() ? RetCC_Sparc64
247 : RetCC_Sparc32);
248}
249
250SDValue
251SparcTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
252 bool IsVarArg,
253 const SmallVectorImpl<ISD::OutputArg> &Outs,
254 const SmallVectorImpl<SDValue> &OutVals,
255 const SDLoc &DL, SelectionDAG &DAG) const {
256 if (Subtarget->is64Bit())
257 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
258 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
259}
260
261SDValue
262SparcTargetLowering::LowerReturn_32(SDValue Chain, CallingConv::ID CallConv,
263 bool IsVarArg,
264 const SmallVectorImpl<ISD::OutputArg> &Outs,
265 const SmallVectorImpl<SDValue> &OutVals,
266 const SDLoc &DL, SelectionDAG &DAG) const {
267 MachineFunction &MF = DAG.getMachineFunction();
268
269 // CCValAssign - represent the assignment of the return value to locations.
270 SmallVector<CCValAssign, 16> RVLocs;
271
272 // CCState - Info about the registers and stack slot.
273 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
274 *DAG.getContext());
275
276 // Analyze return values.
277 CCInfo.AnalyzeReturn(Outs, Fn: RetCC_Sparc32);
278
279 SDValue Glue;
280 SmallVector<SDValue, 4> RetOps(1, Chain);
281 // Make room for the return address offset.
282 RetOps.push_back(Elt: SDValue());
283
284 // Copy the result values into the output registers.
285 for (unsigned i = 0, realRVLocIdx = 0;
286 i != RVLocs.size();
287 ++i, ++realRVLocIdx) {
288 CCValAssign &VA = RVLocs[i];
289 assert(VA.isRegLoc() && "Can only return in registers!");
290
291 SDValue Arg = OutVals[realRVLocIdx];
292
293 if (VA.needsCustom()) {
294 assert(VA.getLocVT() == MVT::v2i32);
295 // Legalize ret v2i32 -> ret 2 x i32 (Basically: do what would
296 // happen by default if this wasn't a legal type)
297
298 SDValue Part0 = DAG.getNode(Opcode: ISD::EXTRACT_VECTOR_ELT, DL, VT: MVT::i32,
299 N1: Arg,
300 N2: DAG.getConstant(Val: 0, DL, VT: getVectorIdxTy(DL: DAG.getDataLayout())));
301 SDValue Part1 = DAG.getNode(Opcode: ISD::EXTRACT_VECTOR_ELT, DL, VT: MVT::i32,
302 N1: Arg,
303 N2: DAG.getConstant(Val: 1, DL, VT: getVectorIdxTy(DL: DAG.getDataLayout())));
304
305 Chain = DAG.getCopyToReg(Chain, dl: DL, Reg: VA.getLocReg(), N: Part0, Glue);
306 Glue = Chain.getValue(R: 1);
307 RetOps.push_back(Elt: DAG.getRegister(Reg: VA.getLocReg(), VT: VA.getLocVT()));
308 VA = RVLocs[++i]; // skip ahead to next loc
309 Chain = DAG.getCopyToReg(Chain, dl: DL, Reg: VA.getLocReg(), N: Part1,
310 Glue);
311 } else
312 Chain = DAG.getCopyToReg(Chain, dl: DL, Reg: VA.getLocReg(), N: Arg, Glue);
313
314 // Guarantee that all emitted copies are stuck together with flags.
315 Glue = Chain.getValue(R: 1);
316 RetOps.push_back(Elt: DAG.getRegister(Reg: VA.getLocReg(), VT: VA.getLocVT()));
317 }
318
319 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
320 // If the function returns a struct, copy the SRetReturnReg to I0. This
321 // covers both an explicit sret parameter and a return value that was
322 // demoted to sret because it does not fit in the return registers.
323 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
324 if (Register Reg = SFI->getSRetReturnReg()) {
325 auto PtrVT = getPointerTy(DL: DAG.getDataLayout());
326 SDValue Val = DAG.getCopyFromReg(Chain, dl: DL, Reg, VT: PtrVT);
327 Chain = DAG.getCopyToReg(Chain, dl: DL, Reg: SP::I0, N: Val, Glue);
328 Glue = Chain.getValue(R: 1);
329 RetOps.push_back(Elt: DAG.getRegister(Reg: SP::I0, VT: PtrVT));
330
331 // A zero-sized return value, e.g. an empty struct or union, is returned
332 // without an unimp instruction after the call, so there is nothing for the
333 // return to skip over.
334 const Function &F = MF.getFunction();
335 Type *RetType =
336 F.hasStructRetAttr() ? F.getParamStructRetType(ArgNo: 0) : F.getReturnType();
337 if (!RetType->isEmptyTy())
338 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
339 }
340
341 RetOps[0] = Chain; // Update chain.
342 RetOps[1] = DAG.getConstant(Val: RetAddrOffset, DL, VT: MVT::i32);
343
344 // Add the glue if we have it.
345 if (Glue.getNode())
346 RetOps.push_back(Elt: Glue);
347
348 return DAG.getNode(Opcode: SPISD::RET_GLUE, DL, VT: MVT::Other, Ops: RetOps);
349}
350
351// Lower return values for the 64-bit ABI.
352// Return values are passed the exactly the same way as function arguments.
353SDValue
354SparcTargetLowering::LowerReturn_64(SDValue Chain, CallingConv::ID CallConv,
355 bool IsVarArg,
356 const SmallVectorImpl<ISD::OutputArg> &Outs,
357 const SmallVectorImpl<SDValue> &OutVals,
358 const SDLoc &DL, SelectionDAG &DAG) const {
359 // CCValAssign - represent the assignment of the return value to locations.
360 SmallVector<CCValAssign, 16> RVLocs;
361
362 // CCState - Info about the registers and stack slot.
363 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
364 *DAG.getContext());
365
366 // Analyze return values.
367 CCInfo.AnalyzeReturn(Outs, Fn: RetCC_Sparc64);
368
369 SDValue Glue;
370 SmallVector<SDValue, 4> RetOps(1, Chain);
371
372 // The second operand on the return instruction is the return address offset.
373 // The return address is always %i7+8 with the 64-bit ABI.
374 RetOps.push_back(Elt: DAG.getConstant(Val: 8, DL, VT: MVT::i32));
375
376 // Copy the result values into the output registers.
377 for (unsigned i = 0; i != RVLocs.size(); ++i) {
378 CCValAssign &VA = RVLocs[i];
379 assert(VA.isRegLoc() && "Can only return in registers!");
380 SDValue OutVal = OutVals[i];
381
382 // Integer return values must be sign or zero extended by the callee.
383 switch (VA.getLocInfo()) {
384 case CCValAssign::Full: break;
385 case CCValAssign::SExt:
386 OutVal = DAG.getNode(Opcode: ISD::SIGN_EXTEND, DL, VT: VA.getLocVT(), Operand: OutVal);
387 break;
388 case CCValAssign::ZExt:
389 OutVal = DAG.getNode(Opcode: ISD::ZERO_EXTEND, DL, VT: VA.getLocVT(), Operand: OutVal);
390 break;
391 case CCValAssign::AExt:
392 OutVal = DAG.getNode(Opcode: ISD::ANY_EXTEND, DL, VT: VA.getLocVT(), Operand: OutVal);
393 break;
394 default:
395 llvm_unreachable("Unknown loc info!");
396 }
397
398 // The custom bit on an i32 return value indicates that it should be passed
399 // in the high bits of the register.
400 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
401 OutVal = DAG.getNode(Opcode: ISD::SHL, DL, VT: MVT::i64, N1: OutVal,
402 N2: DAG.getConstant(Val: 32, DL, VT: MVT::i32));
403
404 // The next value may go in the low bits of the same register.
405 // Handle both at once.
406 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
407 SDValue NV = DAG.getNode(Opcode: ISD::ZERO_EXTEND, DL, VT: MVT::i64, Operand: OutVals[i+1]);
408 OutVal = DAG.getNode(Opcode: ISD::OR, DL, VT: MVT::i64, N1: OutVal, N2: NV);
409 // Skip the next value, it's already done.
410 ++i;
411 }
412 }
413
414 Chain = DAG.getCopyToReg(Chain, dl: DL, Reg: VA.getLocReg(), N: OutVal, Glue);
415
416 // Guarantee that all emitted copies are stuck together with flags.
417 Glue = Chain.getValue(R: 1);
418 RetOps.push_back(Elt: DAG.getRegister(Reg: VA.getLocReg(), VT: VA.getLocVT()));
419 }
420
421 RetOps[0] = Chain; // Update chain.
422
423 // Add the flag if we have it.
424 if (Glue.getNode())
425 RetOps.push_back(Elt: Glue);
426
427 return DAG.getNode(Opcode: SPISD::RET_GLUE, DL, VT: MVT::Other, Ops: RetOps);
428}
429
430SDValue SparcTargetLowering::LowerFormalArguments(
431 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
432 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
433 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
434 if (Subtarget->is64Bit())
435 return LowerFormalArguments_64(Chain, CallConv, isVarArg: IsVarArg, Ins,
436 dl: DL, DAG, InVals);
437 return LowerFormalArguments_32(Chain, CallConv, isVarArg: IsVarArg, Ins,
438 dl: DL, DAG, InVals);
439}
440
441/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
442/// passed in either one or two GPRs, including FP values. TODO: we should
443/// pass FP values in FP registers for fastcc functions.
444SDValue SparcTargetLowering::LowerFormalArguments_32(
445 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
446 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
447 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
448 MachineFunction &MF = DAG.getMachineFunction();
449 MachineRegisterInfo &RegInfo = MF.getRegInfo();
450 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
451 EVT PtrVT = getPointerTy(DL: DAG.getDataLayout());
452
453 // Assign locations to all of the incoming arguments.
454 SmallVector<CCValAssign, 16> ArgLocs;
455 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
456 *DAG.getContext());
457 CCInfo.AnalyzeFormalArguments(Ins, Fn: CC_Sparc32);
458
459 const unsigned StackOffset = 92;
460 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
461
462 unsigned InIdx = 0;
463 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i, ++InIdx) {
464 CCValAssign &VA = ArgLocs[i];
465 EVT LocVT = VA.getLocVT();
466
467 if (Ins[InIdx].Flags.isSRet()) {
468 if (InIdx != 0)
469 report_fatal_error(reason: "sparc only supports sret on the first parameter");
470 // Get SRet from [%fp+64].
471 int FrameIdx = MF.getFrameInfo().CreateFixedObject(Size: 4, SPOffset: 64, IsImmutable: true);
472 SDValue FIPtr = DAG.getFrameIndex(FI: FrameIdx, VT: MVT::i32);
473 SDValue Arg =
474 DAG.getLoad(VT: MVT::i32, dl, Chain, Ptr: FIPtr, PtrInfo: MachinePointerInfo());
475 InVals.push_back(Elt: Arg);
476 continue;
477 }
478
479 SDValue Arg;
480 if (VA.isRegLoc()) {
481 if (VA.needsCustom()) {
482 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
483
484 Register VRegHi = RegInfo.createVirtualRegister(RegClass: &SP::IntRegsRegClass);
485 MF.getRegInfo().addLiveIn(Reg: VA.getLocReg(), vreg: VRegHi);
486 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, Reg: VRegHi, VT: MVT::i32);
487
488 assert(i+1 < e);
489 CCValAssign &NextVA = ArgLocs[++i];
490
491 SDValue LoVal;
492 if (NextVA.isMemLoc()) {
493 int FrameIdx = MF.getFrameInfo().
494 CreateFixedObject(Size: 4, SPOffset: StackOffset+NextVA.getLocMemOffset(),IsImmutable: true);
495 SDValue FIPtr = DAG.getFrameIndex(FI: FrameIdx, VT: MVT::i32);
496 LoVal = DAG.getLoad(VT: MVT::i32, dl, Chain, Ptr: FIPtr, PtrInfo: MachinePointerInfo());
497 } else {
498 Register loReg = MF.addLiveIn(PReg: NextVA.getLocReg(),
499 RC: &SP::IntRegsRegClass);
500 LoVal = DAG.getCopyFromReg(Chain, dl, Reg: loReg, VT: MVT::i32);
501 }
502
503 if (IsLittleEndian)
504 std::swap(a&: LoVal, b&: HiVal);
505
506 SDValue WholeValue =
507 DAG.getNode(Opcode: ISD::BUILD_PAIR, DL: dl, VT: MVT::i64, N1: LoVal, N2: HiVal);
508 WholeValue = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: VA.getLocVT(), Operand: WholeValue);
509 InVals.push_back(Elt: WholeValue);
510 continue;
511 }
512 Register VReg = RegInfo.createVirtualRegister(RegClass: &SP::IntRegsRegClass);
513 MF.getRegInfo().addLiveIn(Reg: VA.getLocReg(), vreg: VReg);
514 Arg = DAG.getCopyFromReg(Chain, dl, Reg: VReg, VT: MVT::i32);
515 if (VA.getLocInfo() != CCValAssign::Indirect) {
516 if (VA.getLocVT() == MVT::f32)
517 Arg = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: MVT::f32, Operand: Arg);
518 else if (VA.getLocVT() != MVT::i32) {
519 Arg = DAG.getNode(Opcode: ISD::AssertSext, DL: dl, VT: MVT::i32, N1: Arg,
520 N2: DAG.getValueType(VA.getLocVT()));
521 Arg = DAG.getNode(Opcode: ISD::TRUNCATE, DL: dl, VT: VA.getLocVT(), Operand: Arg);
522 }
523 InVals.push_back(Elt: Arg);
524 continue;
525 }
526 } else {
527 assert(VA.isMemLoc());
528
529 unsigned Offset = VA.getLocMemOffset() + StackOffset;
530
531 if (VA.needsCustom()) {
532 assert(VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::v2i32);
533 // If it is double-word aligned, just load.
534 if (Offset % 8 == 0) {
535 int FI = MF.getFrameInfo().CreateFixedObject(Size: 8, SPOffset: Offset, IsImmutable: true);
536 SDValue FIPtr = DAG.getFrameIndex(FI, VT: PtrVT);
537 SDValue Load = DAG.getLoad(VT: VA.getValVT(), dl, Chain, Ptr: FIPtr,
538 PtrInfo: MachinePointerInfo());
539 InVals.push_back(Elt: Load);
540 continue;
541 }
542
543 int FI = MF.getFrameInfo().CreateFixedObject(Size: 4, SPOffset: Offset, IsImmutable: true);
544 SDValue FIPtr = DAG.getFrameIndex(FI, VT: PtrVT);
545 SDValue HiVal =
546 DAG.getLoad(VT: MVT::i32, dl, Chain, Ptr: FIPtr, PtrInfo: MachinePointerInfo());
547 int FI2 = MF.getFrameInfo().CreateFixedObject(Size: 4, SPOffset: Offset + 4, IsImmutable: true);
548 SDValue FIPtr2 = DAG.getFrameIndex(FI: FI2, VT: PtrVT);
549
550 SDValue LoVal =
551 DAG.getLoad(VT: MVT::i32, dl, Chain, Ptr: FIPtr2, PtrInfo: MachinePointerInfo());
552
553 if (IsLittleEndian)
554 std::swap(a&: LoVal, b&: HiVal);
555
556 SDValue WholeValue =
557 DAG.getNode(Opcode: ISD::BUILD_PAIR, DL: dl, VT: MVT::i64, N1: LoVal, N2: HiVal);
558 WholeValue = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: VA.getValVT(), Operand: WholeValue);
559 InVals.push_back(Elt: WholeValue);
560 continue;
561 }
562
563 int FI = MF.getFrameInfo().CreateFixedObject(Size: LocVT.getSizeInBits() / 8,
564 SPOffset: Offset, IsImmutable: true);
565 SDValue FIPtr = DAG.getFrameIndex(FI, VT: PtrVT);
566 SDValue Load = DAG.getLoad(VT: LocVT, dl, Chain, Ptr: FIPtr,
567 PtrInfo: MachinePointerInfo::getFixedStack(MF, FI));
568 if (VA.getLocInfo() != CCValAssign::Indirect) {
569 InVals.push_back(Elt: Load);
570 continue;
571 }
572 Arg = Load;
573 }
574
575 assert(VA.getLocInfo() == CCValAssign::Indirect);
576
577 SDValue ArgValue =
578 DAG.getLoad(VT: VA.getValVT(), dl, Chain, Ptr: Arg, PtrInfo: MachinePointerInfo());
579 InVals.push_back(Elt: ArgValue);
580
581 unsigned ArgIndex = Ins[InIdx].OrigArgIndex;
582 assert(Ins[InIdx].PartOffset == 0);
583 while (i + 1 != e && Ins[InIdx + 1].OrigArgIndex == ArgIndex) {
584 CCValAssign &PartVA = ArgLocs[i + 1];
585 unsigned PartOffset = Ins[InIdx + 1].PartOffset;
586 SDValue Address = DAG.getMemBasePlusOffset(
587 Base: ArgValue, Offset: TypeSize::getFixed(ExactSize: PartOffset), DL: dl);
588 InVals.push_back(Elt: DAG.getLoad(VT: PartVA.getValVT(), dl, Chain, Ptr: Address,
589 PtrInfo: MachinePointerInfo()));
590 ++i;
591 ++InIdx;
592 }
593 }
594
595 // Ins[0] is an sret argument both for an explicit sret parameter and for a
596 // return value that was demoted to sret.
597 if (!Ins.empty() && Ins[0].Flags.isSRet()) {
598 // Copy the SRet Argument to SRetReturnReg.
599 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
600 Register Reg = SFI->getSRetReturnReg();
601 if (!Reg) {
602 Reg = MF.getRegInfo().createVirtualRegister(RegClass: &SP::IntRegsRegClass);
603 SFI->setSRetReturnReg(Reg);
604 }
605 SDValue Copy = DAG.getCopyToReg(Chain: DAG.getEntryNode(), dl, Reg, N: InVals[0]);
606 Chain = DAG.getNode(Opcode: ISD::TokenFactor, DL: dl, VT: MVT::Other, N1: Copy, N2: Chain);
607 }
608
609 // Store remaining ArgRegs to the stack if this is a varargs function.
610 if (isVarArg) {
611 static const MCPhysReg ArgRegs[] = {
612 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
613 };
614 unsigned NumAllocated = CCInfo.getFirstUnallocated(Regs: ArgRegs);
615 const MCPhysReg *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
616 unsigned ArgOffset = CCInfo.getStackSize();
617 if (NumAllocated == 6)
618 ArgOffset += StackOffset;
619 else {
620 assert(!ArgOffset);
621 ArgOffset = 68+4*NumAllocated;
622 }
623
624 // Remember the vararg offset for the va_start implementation.
625 FuncInfo->setVarArgsFrameOffset(ArgOffset);
626
627 std::vector<SDValue> OutChains;
628
629 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
630 Register VReg = RegInfo.createVirtualRegister(RegClass: &SP::IntRegsRegClass);
631 MF.getRegInfo().addLiveIn(Reg: *CurArgReg, vreg: VReg);
632 SDValue Arg = DAG.getCopyFromReg(Chain: DAG.getRoot(), dl, Reg: VReg, VT: MVT::i32);
633
634 int FrameIdx = MF.getFrameInfo().CreateFixedObject(Size: 4, SPOffset: ArgOffset,
635 IsImmutable: true);
636 SDValue FIPtr = DAG.getFrameIndex(FI: FrameIdx, VT: MVT::i32);
637
638 OutChains.push_back(
639 x: DAG.getStore(Chain: DAG.getRoot(), dl, Val: Arg, Ptr: FIPtr, PtrInfo: MachinePointerInfo()));
640 ArgOffset += 4;
641 }
642
643 if (!OutChains.empty()) {
644 OutChains.push_back(x: Chain);
645 Chain = DAG.getNode(Opcode: ISD::TokenFactor, DL: dl, VT: MVT::Other, Ops: OutChains);
646 }
647 }
648
649 return Chain;
650}
651
652// Lower formal arguments for the 64 bit ABI.
653SDValue SparcTargetLowering::LowerFormalArguments_64(
654 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
655 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
656 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
657 MachineFunction &MF = DAG.getMachineFunction();
658
659 // Analyze arguments according to CC_Sparc64.
660 SmallVector<CCValAssign, 16> ArgLocs;
661 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
662 *DAG.getContext());
663 CCInfo.AnalyzeFormalArguments(Ins, Fn: CC_Sparc64);
664
665 // The argument array begins at %fp+BIAS+128, after the register save area.
666 const unsigned ArgArea = 128;
667
668 for (const CCValAssign &VA : ArgLocs) {
669 if (VA.isRegLoc()) {
670 // This argument is passed in a register.
671 // All integer register arguments are promoted by the caller to i64.
672
673 // Create a virtual register for the promoted live-in value.
674 Register VReg = MF.addLiveIn(PReg: VA.getLocReg(),
675 RC: getRegClassFor(VT: VA.getLocVT()));
676 SDValue Arg = DAG.getCopyFromReg(Chain, dl: DL, Reg: VReg, VT: VA.getLocVT());
677
678 // Get the high bits for i32 struct elements.
679 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
680 Arg = DAG.getNode(Opcode: ISD::SRL, DL, VT: VA.getLocVT(), N1: Arg,
681 N2: DAG.getConstant(Val: 32, DL, VT: MVT::i32));
682
683 // The caller promoted the argument, so insert an Assert?ext SDNode so we
684 // won't promote the value again in this function.
685 switch (VA.getLocInfo()) {
686 case CCValAssign::SExt:
687 Arg = DAG.getNode(Opcode: ISD::AssertSext, DL, VT: VA.getLocVT(), N1: Arg,
688 N2: DAG.getValueType(VA.getValVT()));
689 break;
690 case CCValAssign::ZExt:
691 Arg = DAG.getNode(Opcode: ISD::AssertZext, DL, VT: VA.getLocVT(), N1: Arg,
692 N2: DAG.getValueType(VA.getValVT()));
693 break;
694 default:
695 break;
696 }
697
698 // Truncate the register down to the argument type.
699 if (VA.isExtInLoc())
700 Arg = DAG.getNode(Opcode: ISD::TRUNCATE, DL, VT: VA.getValVT(), Operand: Arg);
701
702 InVals.push_back(Elt: Arg);
703 continue;
704 }
705
706 // The registers are exhausted. This argument was passed on the stack.
707 assert(VA.isMemLoc());
708 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
709 // beginning of the arguments area at %fp+BIAS+128.
710 unsigned Offset = VA.getLocMemOffset() + ArgArea;
711 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
712 // Adjust offset for extended arguments, SPARC is big-endian.
713 // The caller will have written the full slot with extended bytes, but we
714 // prefer our own extending loads.
715 if (VA.isExtInLoc())
716 Offset += 8 - ValSize;
717 int FI = MF.getFrameInfo().CreateFixedObject(Size: ValSize, SPOffset: Offset, IsImmutable: true);
718 InVals.push_back(
719 Elt: DAG.getLoad(VT: VA.getValVT(), dl: DL, Chain,
720 Ptr: DAG.getFrameIndex(FI, VT: getPointerTy(DL: MF.getDataLayout())),
721 PtrInfo: MachinePointerInfo::getFixedStack(MF, FI)));
722 }
723
724 if (!IsVarArg)
725 return Chain;
726
727 // This function takes variable arguments, some of which may have been passed
728 // in registers %i0-%i5. Variable floating point arguments are never passed
729 // in floating point registers. They go on %i0-%i5 or on the stack like
730 // integer arguments.
731 //
732 // The va_start intrinsic needs to know the offset to the first variable
733 // argument.
734 unsigned ArgOffset = CCInfo.getStackSize();
735 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
736 // Skip the 128 bytes of register save area.
737 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
738 Subtarget->getStackPointerBias());
739
740 // Save the variable arguments that were passed in registers.
741 // The caller is required to reserve stack space for 6 arguments regardless
742 // of how many arguments were actually passed.
743 SmallVector<SDValue, 8> OutChains;
744 for (; ArgOffset < 6*8; ArgOffset += 8) {
745 Register VReg = MF.addLiveIn(PReg: SP::I0 + ArgOffset/8, RC: &SP::I64RegsRegClass);
746 SDValue VArg = DAG.getCopyFromReg(Chain, dl: DL, Reg: VReg, VT: MVT::i64);
747 int FI = MF.getFrameInfo().CreateFixedObject(Size: 8, SPOffset: ArgOffset + ArgArea, IsImmutable: true);
748 auto PtrVT = getPointerTy(DL: MF.getDataLayout());
749 OutChains.push_back(
750 Elt: DAG.getStore(Chain, dl: DL, Val: VArg, Ptr: DAG.getFrameIndex(FI, VT: PtrVT),
751 PtrInfo: MachinePointerInfo::getFixedStack(MF, FI)));
752 }
753
754 if (!OutChains.empty())
755 Chain = DAG.getNode(Opcode: ISD::TokenFactor, DL, VT: MVT::Other, Ops: OutChains);
756
757 return Chain;
758}
759
760// Check whether any of the argument registers are reserved
761static bool isAnyArgRegReserved(const SparcRegisterInfo *TRI,
762 const MachineFunction &MF) {
763 // The register window design means that outgoing parameters at O*
764 // will appear in the callee as I*.
765 // Be conservative and check both sides of the register names.
766 bool Outgoing =
767 llvm::any_of(Range: SP::GPROutgoingArgRegClass, P: [TRI, &MF](MCPhysReg r) {
768 return TRI->isReservedReg(MF, Reg: r);
769 });
770 bool Incoming =
771 llvm::any_of(Range: SP::GPRIncomingArgRegClass, P: [TRI, &MF](MCPhysReg r) {
772 return TRI->isReservedReg(MF, Reg: r);
773 });
774 return Outgoing || Incoming;
775}
776
777static void emitReservedArgRegCallError(const MachineFunction &MF) {
778 const Function &F = MF.getFunction();
779 F.getContext().diagnose(DI: DiagnosticInfoUnsupported{
780 F, ("SPARC doesn't support"
781 " function calls if any of the argument registers is reserved.")});
782}
783
784SDValue
785SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
786 SmallVectorImpl<SDValue> &InVals) const {
787 if (Subtarget->is64Bit())
788 return LowerCall_64(CLI, InVals);
789 return LowerCall_32(CLI, InVals);
790}
791
792static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
793 const CallBase *Call) {
794 if (Call)
795 return Call->hasFnAttr(Kind: Attribute::ReturnsTwice);
796
797 const Function *CalleeFn = nullptr;
798 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Val&: Callee)) {
799 CalleeFn = dyn_cast<Function>(Val: G->getGlobal());
800 } else if (ExternalSymbolSDNode *E =
801 dyn_cast<ExternalSymbolSDNode>(Val&: Callee)) {
802 const Function &Fn = DAG.getMachineFunction().getFunction();
803 const Module *M = Fn.getParent();
804 const char *CalleeName = E->getSymbol();
805 CalleeFn = M->getFunction(Name: CalleeName);
806 }
807
808 if (!CalleeFn)
809 return false;
810 return CalleeFn->hasFnAttribute(Kind: Attribute::ReturnsTwice);
811}
812
813/// IsEligibleForTailCallOptimization - Check whether the call is eligible
814/// for tail call optimization.
815bool SparcTargetLowering::IsEligibleForTailCallOptimization(
816 CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF) const {
817
818 auto &Outs = CLI.Outs;
819 auto &Caller = MF.getFunction();
820
821 // Do not tail call opt functions with "disable-tail-calls" attribute.
822 if (Caller.getFnAttribute(Kind: "disable-tail-calls").getValueAsString() == "true")
823 return false;
824
825 // Do not tail call opt if the stack is used to pass parameters.
826 // 64-bit targets have a slightly higher limit since the ABI requires
827 // to allocate some space even when all the parameters fit inside registers.
828 unsigned StackSizeLimit = Subtarget->is64Bit() ? 48 : 0;
829 if (CCInfo.getStackSize() > StackSizeLimit)
830 return false;
831
832 // Do not tail call opt if either the callee or caller returns
833 // a struct and the other does not. On 32-bit SPARC the caller's
834 // return value may have been demoted to sret, in which case it has
835 // an SRetReturnReg but no sret attribute.
836 const SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
837 bool CallerSRet = Caller.hasStructRetAttr() || SFI->getSRetReturnReg();
838 bool CalleeSRet = !Outs.empty() && Outs[0].Flags.isSRet();
839 if (CallerSRet != CalleeSRet)
840 return false;
841
842 // Byval parameters hand the function a pointer directly into the stack area
843 // we want to reuse during a tail call.
844 for (auto &Arg : Outs)
845 if (Arg.Flags.isByVal())
846 return false;
847
848 return true;
849}
850
851// Lower a call for the 32-bit ABI.
852SDValue
853SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
854 SmallVectorImpl<SDValue> &InVals) const {
855 SelectionDAG &DAG = CLI.DAG;
856 SDLoc &dl = CLI.DL;
857 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
858 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
859 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
860 SDValue Chain = CLI.Chain;
861 SDValue Callee = CLI.Callee;
862 bool &isTailCall = CLI.IsTailCall;
863 CallingConv::ID CallConv = CLI.CallConv;
864 bool isVarArg = CLI.IsVarArg;
865 MachineFunction &MF = DAG.getMachineFunction();
866 LLVMContext &Ctx = *DAG.getContext();
867 EVT PtrVT = getPointerTy(DL: MF.getDataLayout());
868
869 // Analyze operands of the call, assigning locations to each operand.
870 SmallVector<CCValAssign, 16> ArgLocs;
871 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
872 *DAG.getContext());
873 CCInfo.AnalyzeCallOperands(Outs, Fn: CC_Sparc32);
874
875 isTailCall = isTailCall && IsEligibleForTailCallOptimization(
876 CCInfo, CLI, MF&: DAG.getMachineFunction());
877 if (!isTailCall && CLI.CB && CLI.CB->isMustTailCall())
878 reportFatalInternalError(reason: "failed to perform tail call elimination on a "
879 "call site marked musttail");
880
881 // Get the size of the outgoing arguments stack space requirement.
882 unsigned ArgsSize = CCInfo.getStackSize();
883
884 // Keep stack frames 8-byte aligned.
885 ArgsSize = (ArgsSize+7) & ~7;
886
887 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
888
889 // Create local copies for byval args.
890 SmallVector<SDValue, 8> ByValArgs;
891 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
892 ISD::ArgFlagsTy Flags = Outs[i].Flags;
893 if (!Flags.isByVal())
894 continue;
895
896 SDValue Arg = OutVals[i];
897 unsigned Size = Flags.getByValSize();
898 Align Alignment = Flags.getNonZeroByValAlign();
899
900 if (Size > 0U) {
901 int FI = MFI.CreateStackObject(Size, Alignment, isSpillSlot: false);
902 SDValue FIPtr = DAG.getFrameIndex(FI, VT: getPointerTy(DL: DAG.getDataLayout()));
903 SDValue SizeNode = DAG.getConstant(Val: Size, DL: dl, VT: MVT::i32);
904
905 Chain =
906 DAG.getMemcpy(Chain, dl, Dst: FIPtr, Src: Arg, Size: SizeNode, DstAlign: Alignment, SrcAlign: Alignment,
907 isVol: false, // isVolatile,
908 AlwaysInline: (Size <= 32), // AlwaysInline if size <= 32,
909 /*CI=*/nullptr, OverrideTailCall: std::nullopt, DstPtrInfo: MachinePointerInfo(),
910 SrcPtrInfo: MachinePointerInfo());
911 ByValArgs.push_back(Elt: FIPtr);
912 }
913 else {
914 SDValue nullVal;
915 ByValArgs.push_back(Elt: nullVal);
916 }
917 }
918
919 assert(!isTailCall || ArgsSize == 0);
920
921 if (!isTailCall)
922 Chain = DAG.getCALLSEQ_START(Chain, InSize: ArgsSize, OutSize: 0, DL: dl);
923
924 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
925 SmallVector<SDValue, 8> MemOpChains;
926
927 const unsigned StackOffset = 92;
928 bool hasStructRetAttr = false;
929 unsigned SRetArgSize = 0;
930 // Walk the register/memloc assignments, inserting copies/loads.
931 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
932 i != e;
933 ++i, ++realArgIdx) {
934 CCValAssign &VA = ArgLocs[i];
935 SDValue Arg = OutVals[realArgIdx];
936
937 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
938
939 // Use local copy if it is a byval arg.
940 if (Flags.isByVal()) {
941 Arg = ByValArgs[byvalArgIdx++];
942 if (!Arg) {
943 continue;
944 }
945 }
946
947 // Promote the value if needed.
948 switch (VA.getLocInfo()) {
949 default: llvm_unreachable("Unknown loc info!");
950 case CCValAssign::Full:
951 case CCValAssign::Indirect:
952 break;
953 case CCValAssign::SExt:
954 Arg = DAG.getNode(Opcode: ISD::SIGN_EXTEND, DL: dl, VT: VA.getLocVT(), Operand: Arg);
955 break;
956 case CCValAssign::ZExt:
957 Arg = DAG.getNode(Opcode: ISD::ZERO_EXTEND, DL: dl, VT: VA.getLocVT(), Operand: Arg);
958 break;
959 case CCValAssign::AExt:
960 Arg = DAG.getNode(Opcode: ISD::ANY_EXTEND, DL: dl, VT: VA.getLocVT(), Operand: Arg);
961 break;
962 case CCValAssign::BCvt:
963 Arg = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: VA.getLocVT(), Operand: Arg);
964 break;
965 }
966
967 if (Flags.isSRet()) {
968 assert(VA.needsCustom());
969
970 if (isTailCall)
971 continue;
972
973 // store SRet argument in %sp+64
974 SDValue StackPtr = DAG.getRegister(Reg: SP::O6, VT: MVT::i32);
975 SDValue PtrOff = DAG.getIntPtrConstant(Val: 64, DL: dl);
976 PtrOff = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT: MVT::i32, N1: StackPtr, N2: PtrOff);
977 MemOpChains.push_back(
978 Elt: DAG.getStore(Chain, dl, Val: Arg, Ptr: PtrOff, PtrInfo: MachinePointerInfo()));
979 hasStructRetAttr = true;
980 // sret only allowed on first argument
981 assert(Outs[realArgIdx].OrigArgIndex == 0);
982 SRetArgSize =
983 DAG.getDataLayout().getTypeAllocSize(Ty: CLI.getArgs()[0].IndirectType);
984 continue;
985 }
986
987 if (VA.needsCustom()) {
988 assert(VA.getLocVT() == MVT::f64 || VA.getLocVT() == MVT::v2i32);
989
990 if (VA.isMemLoc()) {
991 unsigned Offset = VA.getLocMemOffset() + StackOffset;
992 // if it is double-word aligned, just store.
993 if (Offset % 8 == 0) {
994 SDValue StackPtr = DAG.getRegister(Reg: SP::O6, VT: MVT::i32);
995 SDValue PtrOff = DAG.getIntPtrConstant(Val: Offset, DL: dl);
996 PtrOff = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT: MVT::i32, N1: StackPtr, N2: PtrOff);
997 MemOpChains.push_back(
998 Elt: DAG.getStore(Chain, dl, Val: Arg, Ptr: PtrOff, PtrInfo: MachinePointerInfo()));
999 continue;
1000 }
1001 }
1002
1003 if (VA.getLocVT() == MVT::f64) {
1004 // Move from the float value from float registers into the
1005 // integer registers.
1006 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val&: Arg))
1007 Arg = bitcastConstantFPToInt(C, DL: dl, DAG);
1008 else
1009 Arg = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: MVT::v2i32, Operand: Arg);
1010 }
1011
1012 SDValue Part0 = DAG.getNode(Opcode: ISD::EXTRACT_VECTOR_ELT, DL: dl, VT: MVT::i32,
1013 N1: Arg,
1014 N2: DAG.getConstant(Val: 0, DL: dl, VT: getVectorIdxTy(DL: DAG.getDataLayout())));
1015 SDValue Part1 = DAG.getNode(Opcode: ISD::EXTRACT_VECTOR_ELT, DL: dl, VT: MVT::i32,
1016 N1: Arg,
1017 N2: DAG.getConstant(Val: 1, DL: dl, VT: getVectorIdxTy(DL: DAG.getDataLayout())));
1018
1019 if (VA.isRegLoc()) {
1020 RegsToPass.push_back(Elt: std::make_pair(x: VA.getLocReg(), y&: Part0));
1021 assert(i+1 != e);
1022 CCValAssign &NextVA = ArgLocs[++i];
1023 if (NextVA.isRegLoc()) {
1024 RegsToPass.push_back(Elt: std::make_pair(x: NextVA.getLocReg(), y&: Part1));
1025 } else {
1026 // Store the second part in stack.
1027 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
1028 SDValue StackPtr = DAG.getRegister(Reg: SP::O6, VT: MVT::i32);
1029 SDValue PtrOff = DAG.getIntPtrConstant(Val: Offset, DL: dl);
1030 PtrOff = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT: MVT::i32, N1: StackPtr, N2: PtrOff);
1031 MemOpChains.push_back(
1032 Elt: DAG.getStore(Chain, dl, Val: Part1, Ptr: PtrOff, PtrInfo: MachinePointerInfo()));
1033 }
1034 } else {
1035 unsigned Offset = VA.getLocMemOffset() + StackOffset;
1036 // Store the first part.
1037 SDValue StackPtr = DAG.getRegister(Reg: SP::O6, VT: MVT::i32);
1038 SDValue PtrOff = DAG.getIntPtrConstant(Val: Offset, DL: dl);
1039 PtrOff = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT: MVT::i32, N1: StackPtr, N2: PtrOff);
1040 MemOpChains.push_back(
1041 Elt: DAG.getStore(Chain, dl, Val: Part0, Ptr: PtrOff, PtrInfo: MachinePointerInfo()));
1042 // Store the second part.
1043 PtrOff = DAG.getIntPtrConstant(Val: Offset + 4, DL: dl);
1044 PtrOff = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT: MVT::i32, N1: StackPtr, N2: PtrOff);
1045 MemOpChains.push_back(
1046 Elt: DAG.getStore(Chain, dl, Val: Part1, Ptr: PtrOff, PtrInfo: MachinePointerInfo()));
1047 }
1048 continue;
1049 }
1050
1051 if (VA.getLocInfo() == CCValAssign::Indirect) {
1052 // Store the argument in a stack slot and pass its address.
1053 unsigned ArgIndex = Outs[realArgIdx].OrigArgIndex;
1054 assert(Outs[realArgIdx].PartOffset == 0);
1055
1056 EVT SlotVT;
1057 if (i + 1 != e && Outs[realArgIdx + 1].OrigArgIndex == ArgIndex) {
1058 Type *OrigArgType = CLI.Args[ArgIndex].Ty;
1059 EVT OrigArgVT = getValueType(DL: MF.getDataLayout(), Ty: OrigArgType);
1060 MVT PartVT =
1061 getRegisterTypeForCallingConv(Context&: Ctx, CC: CLI.CallConv, VT: OrigArgVT);
1062 unsigned N =
1063 getNumRegistersForCallingConv(Context&: Ctx, CC: CLI.CallConv, VT: OrigArgVT);
1064 SlotVT = EVT::getIntegerVT(Context&: Ctx, BitWidth: PartVT.getSizeInBits() * N);
1065 } else {
1066 SlotVT = Outs[realArgIdx].VT;
1067 }
1068
1069 SDValue SpillSlot = DAG.CreateStackTemporary(VT: SlotVT);
1070 int FI = cast<FrameIndexSDNode>(Val&: SpillSlot)->getIndex();
1071 MemOpChains.push_back(
1072 Elt: DAG.getStore(Chain, dl, Val: Arg, Ptr: SpillSlot,
1073 PtrInfo: MachinePointerInfo::getFixedStack(MF, FI)));
1074 // If the original argument was split (e.g. f128), we need
1075 // to store all parts of it here (and pass just one address).
1076 while (i + 1 != e && Outs[realArgIdx + 1].OrigArgIndex == ArgIndex) {
1077 SDValue PartValue = OutVals[realArgIdx + 1];
1078 unsigned PartOffset = Outs[realArgIdx + 1].PartOffset;
1079 SDValue Address = DAG.getMemBasePlusOffset(
1080 Base: DAG.getFrameIndex(FI, VT: PtrVT), Offset: TypeSize::getFixed(ExactSize: PartOffset), DL: dl);
1081 MemOpChains.push_back(
1082 Elt: DAG.getStore(Chain, dl, Val: PartValue, Ptr: Address,
1083 PtrInfo: MachinePointerInfo::getFixedStack(MF, FI)));
1084 assert((PartOffset + PartValue.getValueType().getStoreSize() <=
1085 SlotVT.getStoreSize()) &&
1086 "Not enough space for argument part!");
1087 ++i;
1088 ++realArgIdx;
1089 }
1090
1091 Arg = SpillSlot;
1092 }
1093
1094 // Arguments that can be passed on register must be kept at
1095 // RegsToPass vector
1096 if (VA.isRegLoc()) {
1097 if (VA.getLocVT() != MVT::f32) {
1098 RegsToPass.push_back(Elt: std::make_pair(x: VA.getLocReg(), y&: Arg));
1099 continue;
1100 }
1101 Arg = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: MVT::i32, Operand: Arg);
1102 RegsToPass.push_back(Elt: std::make_pair(x: VA.getLocReg(), y&: Arg));
1103 continue;
1104 }
1105
1106 assert(VA.isMemLoc());
1107
1108 // Create a store off the stack pointer for this argument.
1109 SDValue StackPtr = DAG.getRegister(Reg: SP::O6, VT: MVT::i32);
1110 SDValue PtrOff = DAG.getIntPtrConstant(Val: VA.getLocMemOffset() + StackOffset,
1111 DL: dl);
1112 PtrOff = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT: MVT::i32, N1: StackPtr, N2: PtrOff);
1113 MemOpChains.push_back(
1114 Elt: DAG.getStore(Chain, dl, Val: Arg, Ptr: PtrOff, PtrInfo: MachinePointerInfo()));
1115 }
1116
1117
1118 // Emit all stores, make sure the occur before any copies into physregs.
1119 if (!MemOpChains.empty())
1120 Chain = DAG.getNode(Opcode: ISD::TokenFactor, DL: dl, VT: MVT::Other, Ops: MemOpChains);
1121
1122 // Build a sequence of copy-to-reg nodes chained together with token
1123 // chain and flag operands which copy the outgoing args into registers.
1124 // The InGlue in necessary since all emitted instructions must be
1125 // stuck together.
1126 SDValue InGlue;
1127 for (const auto &[OrigReg, N] : RegsToPass) {
1128 Register Reg = isTailCall ? OrigReg : toCallerWindow(Reg: OrigReg);
1129 Chain = DAG.getCopyToReg(Chain, dl, Reg, N, Glue: InGlue);
1130 InGlue = Chain.getValue(R: 1);
1131 }
1132
1133 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, Call: CLI.CB);
1134
1135 // If the callee is a GlobalAddress node (quite common, every direct call is)
1136 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1137 // Likewise ExternalSymbol -> TargetExternalSymbol.
1138 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Val&: Callee))
1139 Callee = DAG.getTargetGlobalAddress(GV: G->getGlobal(), DL: dl, VT: MVT::i32, offset: 0);
1140 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Val&: Callee))
1141 Callee = DAG.getTargetExternalSymbol(Sym: E->getSymbol(), VT: MVT::i32);
1142
1143 // Returns a chain & a flag for retval copy to use
1144 SDVTList NodeTys = DAG.getVTList(VT1: MVT::Other, VT2: MVT::Glue);
1145 SmallVector<SDValue, 8> Ops;
1146 Ops.push_back(Elt: Chain);
1147 Ops.push_back(Elt: Callee);
1148 if (hasStructRetAttr)
1149 Ops.push_back(Elt: DAG.getTargetConstant(Val: SRetArgSize, DL: dl, VT: MVT::i32));
1150 for (const auto &[OrigReg, N] : RegsToPass) {
1151 Register Reg = isTailCall ? OrigReg : toCallerWindow(Reg: OrigReg);
1152 Ops.push_back(Elt: DAG.getRegister(Reg, VT: N.getValueType()));
1153 }
1154
1155 // Add a register mask operand representing the call-preserved registers.
1156 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
1157 const uint32_t *Mask =
1158 ((hasReturnsTwice)
1159 ? TRI->getRTCallPreservedMask(CC: CallConv)
1160 : TRI->getCallPreservedMask(MF: DAG.getMachineFunction(), CC: CallConv));
1161
1162 if (isAnyArgRegReserved(TRI, MF))
1163 emitReservedArgRegCallError(MF);
1164
1165 assert(Mask && "Missing call preserved mask for calling convention");
1166 Ops.push_back(Elt: DAG.getRegisterMask(RegMask: Mask));
1167
1168 if (InGlue.getNode())
1169 Ops.push_back(Elt: InGlue);
1170
1171 if (isTailCall) {
1172 DAG.getMachineFunction().getFrameInfo().setHasTailCall();
1173 return DAG.getNode(Opcode: SPISD::TAIL_CALL, DL: dl, VT: MVT::Other, Ops);
1174 }
1175
1176 Chain = DAG.getNode(Opcode: SPISD::CALL, DL: dl, VTList: NodeTys, Ops);
1177 InGlue = Chain.getValue(R: 1);
1178
1179 Chain = DAG.getCALLSEQ_END(Chain, Size1: ArgsSize, Size2: 0, Glue: InGlue, DL: dl);
1180 InGlue = Chain.getValue(R: 1);
1181
1182 // Assign locations to each value returned by this call.
1183 SmallVector<CCValAssign, 16> RVLocs;
1184 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1185 *DAG.getContext());
1186
1187 RVInfo.AnalyzeCallResult(Ins, Fn: RetCC_Sparc32);
1188
1189 // Copy all of the result registers out of their specified physreg.
1190 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1191 assert(RVLocs[i].isRegLoc() && "Can only return in registers!");
1192 if (RVLocs[i].getLocVT() == MVT::v2i32) {
1193 SDValue Vec = DAG.getNode(Opcode: ISD::UNDEF, DL: dl, VT: MVT::v2i32);
1194 SDValue Lo = DAG.getCopyFromReg(
1195 Chain, dl, Reg: toCallerWindow(Reg: RVLocs[i++].getLocReg()), VT: MVT::i32, Glue: InGlue);
1196 Chain = Lo.getValue(R: 1);
1197 InGlue = Lo.getValue(R: 2);
1198 Vec = DAG.getNode(Opcode: ISD::INSERT_VECTOR_ELT, DL: dl, VT: MVT::v2i32, N1: Vec, N2: Lo,
1199 N3: DAG.getConstant(Val: 0, DL: dl, VT: MVT::i32));
1200 SDValue Hi = DAG.getCopyFromReg(
1201 Chain, dl, Reg: toCallerWindow(Reg: RVLocs[i].getLocReg()), VT: MVT::i32, Glue: InGlue);
1202 Chain = Hi.getValue(R: 1);
1203 InGlue = Hi.getValue(R: 2);
1204 Vec = DAG.getNode(Opcode: ISD::INSERT_VECTOR_ELT, DL: dl, VT: MVT::v2i32, N1: Vec, N2: Hi,
1205 N3: DAG.getConstant(Val: 1, DL: dl, VT: MVT::i32));
1206 InVals.push_back(Elt: Vec);
1207 } else {
1208 Chain =
1209 DAG.getCopyFromReg(Chain, dl, Reg: toCallerWindow(Reg: RVLocs[i].getLocReg()),
1210 VT: RVLocs[i].getValVT(), Glue: InGlue)
1211 .getValue(R: 1);
1212 InGlue = Chain.getValue(R: 2);
1213 InVals.push_back(Elt: Chain.getValue(R: 0));
1214 }
1215 }
1216
1217 return Chain;
1218}
1219
1220// FIXME? Maybe this could be a TableGen attribute on some registers and
1221// this table could be generated automatically from RegInfo.
1222Register SparcTargetLowering::getRegisterByName(const char* RegName, LLT VT,
1223 const MachineFunction &MF) const {
1224 StringRef Name(RegName);
1225 Name.consume_front(Prefix: "%");
1226 Register Reg = StringSwitch<Register>(Name)
1227 .Cases(CaseStrings: {"r24", "i0"}, Value: SP::I0)
1228 .Cases(CaseStrings: {"r25", "i1"}, Value: SP::I1)
1229 .Cases(CaseStrings: {"r26", "i2"}, Value: SP::I2)
1230 .Cases(CaseStrings: {"r27", "i3"}, Value: SP::I3)
1231 .Cases(CaseStrings: {"r28", "i4"}, Value: SP::I4)
1232 .Cases(CaseStrings: {"r29", "i5"}, Value: SP::I5)
1233 .Cases(CaseStrings: {"r30", "i6", "fp"}, Value: SP::I6)
1234 .Cases(CaseStrings: {"r31", "i7"}, Value: SP::I7)
1235 .Cases(CaseStrings: {"r8", "o0"}, Value: SP::O0)
1236 .Cases(CaseStrings: {"r9", "o1"}, Value: SP::O1)
1237 .Cases(CaseStrings: {"r10", "o2"}, Value: SP::O2)
1238 .Cases(CaseStrings: {"r11", "o3"}, Value: SP::O3)
1239 .Cases(CaseStrings: {"r12", "o4"}, Value: SP::O4)
1240 .Cases(CaseStrings: {"r13", "o5"}, Value: SP::O5)
1241 .Cases(CaseStrings: {"r14", "o6", "sp"}, Value: SP::O6)
1242 .Cases(CaseStrings: {"r15", "o7"}, Value: SP::O7)
1243 .Cases(CaseStrings: {"r16", "l0"}, Value: SP::L0)
1244 .Cases(CaseStrings: {"r17", "l1"}, Value: SP::L1)
1245 .Cases(CaseStrings: {"r18", "l2"}, Value: SP::L2)
1246 .Cases(CaseStrings: {"r19", "l3"}, Value: SP::L3)
1247 .Cases(CaseStrings: {"r20", "l4"}, Value: SP::L4)
1248 .Cases(CaseStrings: {"r21", "l5"}, Value: SP::L5)
1249 .Cases(CaseStrings: {"r22", "l6"}, Value: SP::L6)
1250 .Cases(CaseStrings: {"r23", "l7"}, Value: SP::L7)
1251 .Cases(CaseStrings: {"r0", "g0"}, Value: SP::G0)
1252 .Cases(CaseStrings: {"r1", "g1"}, Value: SP::G1)
1253 .Cases(CaseStrings: {"r2", "g2"}, Value: SP::G2)
1254 .Cases(CaseStrings: {"r3", "g3"}, Value: SP::G3)
1255 .Cases(CaseStrings: {"r4", "g4"}, Value: SP::G4)
1256 .Cases(CaseStrings: {"r5", "g5"}, Value: SP::G5)
1257 .Cases(CaseStrings: {"r6", "g6"}, Value: SP::G6)
1258 .Cases(CaseStrings: {"r7", "g7"}, Value: SP::G7)
1259 .Default(Value: 0);
1260
1261 // If we're directly referencing register names
1262 // (e.g in GCC C extension `register int r asm("g1");`),
1263 // make sure that said register is in the reserve list.
1264 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
1265 if (!TRI->isReservedReg(MF, Reg))
1266 Reg = Register();
1267
1268 return Reg;
1269}
1270
1271// Fixup floating point arguments in the ... part of a varargs call.
1272//
1273// The SPARC v9 ABI requires that floating point arguments are treated the same
1274// as integers when calling a varargs function. This does not apply to the
1275// fixed arguments that are part of the function's prototype.
1276//
1277// This function post-processes a CCValAssign array created by
1278// AnalyzeCallOperands().
1279static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1280 ArrayRef<ISD::OutputArg> Outs) {
1281 for (CCValAssign &VA : ArgLocs) {
1282 MVT ValTy = VA.getLocVT();
1283 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1284 // varargs functions.
1285 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
1286 continue;
1287 // The fixed arguments to a varargs function still go in FP registers.
1288 if (!Outs[VA.getValNo()].Flags.isVarArg())
1289 continue;
1290
1291 // This floating point argument should be reassigned.
1292 // Determine the offset into the argument array.
1293 Register firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1294 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1295 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
1296 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1297
1298 if (Offset < 6*8) {
1299 // This argument should go in %i0-%i5.
1300 unsigned IReg = SP::I0 + Offset/8;
1301 if (ValTy == MVT::f64)
1302 // Full register, just bitconvert into i64.
1303 VA = CCValAssign::getReg(ValNo: VA.getValNo(), ValVT: VA.getValVT(), Reg: IReg, LocVT: MVT::i64,
1304 HTP: CCValAssign::BCvt);
1305 else {
1306 assert(ValTy == MVT::f128 && "Unexpected type!");
1307 // Full register, just bitconvert into i128 -- We will lower this into
1308 // two i64s in LowerCall_64.
1309 VA = CCValAssign::getCustomReg(ValNo: VA.getValNo(), ValVT: VA.getValVT(), Reg: IReg,
1310 LocVT: MVT::i128, HTP: CCValAssign::BCvt);
1311 }
1312 } else {
1313 // This needs to go to memory, we're out of integer registers.
1314 VA = CCValAssign::getMem(ValNo: VA.getValNo(), ValVT: VA.getValVT(), Offset,
1315 LocVT: VA.getLocVT(), HTP: VA.getLocInfo());
1316 }
1317 }
1318}
1319
1320// Lower a call for the 64-bit ABI.
1321SDValue
1322SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1323 SmallVectorImpl<SDValue> &InVals) const {
1324 SelectionDAG &DAG = CLI.DAG;
1325 SDLoc DL = CLI.DL;
1326 SDValue Chain = CLI.Chain;
1327 auto PtrVT = getPointerTy(DL: DAG.getDataLayout());
1328 MachineFunction &MF = DAG.getMachineFunction();
1329
1330 // Analyze operands of the call, assigning locations to each operand.
1331 SmallVector<CCValAssign, 16> ArgLocs;
1332 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
1333 *DAG.getContext());
1334 CCInfo.AnalyzeCallOperands(Outs: CLI.Outs, Fn: CC_Sparc64);
1335
1336 CLI.IsTailCall = CLI.IsTailCall && IsEligibleForTailCallOptimization(
1337 CCInfo, CLI, MF&: DAG.getMachineFunction());
1338 if (!CLI.IsTailCall && CLI.CB && CLI.CB->isMustTailCall())
1339 reportFatalInternalError(reason: "failed to perform tail call elimination on a "
1340 "call site marked musttail");
1341
1342 // Get the size of the outgoing arguments stack space requirement.
1343 // The stack offset computed by CC_Sparc64 includes all arguments.
1344 // Called functions expect 6 argument words to exist in the stack frame, used
1345 // or not.
1346 unsigned StackReserved = 6 * 8u;
1347 unsigned ArgsSize = std::max<unsigned>(a: StackReserved, b: CCInfo.getStackSize());
1348
1349 // Keep stack frames 16-byte aligned.
1350 ArgsSize = alignTo(Value: ArgsSize, Align: 16);
1351
1352 // Varargs calls require special treatment.
1353 if (CLI.IsVarArg)
1354 fixupVariableFloatArgs(ArgLocs, Outs: CLI.Outs);
1355
1356 assert(!CLI.IsTailCall || ArgsSize == StackReserved);
1357
1358 // Adjust the stack pointer to make room for the arguments.
1359 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1360 // with more than 6 arguments.
1361 if (!CLI.IsTailCall)
1362 Chain = DAG.getCALLSEQ_START(Chain, InSize: ArgsSize, OutSize: 0, DL);
1363
1364 // Collect the set of registers to pass to the function and their values.
1365 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1366 // instruction.
1367 SmallVector<std::pair<Register, SDValue>, 8> RegsToPass;
1368
1369 // Collect chains from all the memory opeations that copy arguments to the
1370 // stack. They must follow the stack pointer adjustment above and precede the
1371 // call instruction itself.
1372 SmallVector<SDValue, 8> MemOpChains;
1373
1374 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1375 const CCValAssign &VA = ArgLocs[i];
1376 SDValue Arg = CLI.OutVals[i];
1377
1378 // Promote the value if needed.
1379 switch (VA.getLocInfo()) {
1380 default:
1381 llvm_unreachable("Unknown location info!");
1382 case CCValAssign::Full:
1383 break;
1384 case CCValAssign::SExt:
1385 Arg = DAG.getNode(Opcode: ISD::SIGN_EXTEND, DL, VT: VA.getLocVT(), Operand: Arg);
1386 break;
1387 case CCValAssign::ZExt:
1388 Arg = DAG.getNode(Opcode: ISD::ZERO_EXTEND, DL, VT: VA.getLocVT(), Operand: Arg);
1389 break;
1390 case CCValAssign::AExt:
1391 Arg = DAG.getNode(Opcode: ISD::ANY_EXTEND, DL, VT: VA.getLocVT(), Operand: Arg);
1392 break;
1393 case CCValAssign::BCvt:
1394 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1395 // SPARC does not support i128 natively. Lower it into two i64, see below.
1396 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1397 || VA.getLocVT() != MVT::i128)
1398 Arg = DAG.getNode(Opcode: ISD::BITCAST, DL, VT: VA.getLocVT(), Operand: Arg);
1399 break;
1400 }
1401
1402 if (VA.isRegLoc()) {
1403 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1404 && VA.getLocVT() == MVT::i128) {
1405 // Store and reload into the integer register reg and reg+1.
1406 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1407 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
1408 SDValue StackPtr = DAG.getRegister(Reg: SP::O6, VT: PtrVT);
1409 SDValue HiPtrOff = DAG.getIntPtrConstant(Val: StackOffset, DL);
1410 HiPtrOff = DAG.getNode(Opcode: ISD::ADD, DL, VT: PtrVT, N1: StackPtr, N2: HiPtrOff);
1411 SDValue LoPtrOff = DAG.getIntPtrConstant(Val: StackOffset + 8, DL);
1412 LoPtrOff = DAG.getNode(Opcode: ISD::ADD, DL, VT: PtrVT, N1: StackPtr, N2: LoPtrOff);
1413
1414 // Store to %sp+BIAS+128+Offset
1415 SDValue Store =
1416 DAG.getStore(Chain, dl: DL, Val: Arg, Ptr: HiPtrOff, PtrInfo: MachinePointerInfo());
1417 // Load into Reg and Reg+1
1418 SDValue Hi64 =
1419 DAG.getLoad(VT: MVT::i64, dl: DL, Chain: Store, Ptr: HiPtrOff, PtrInfo: MachinePointerInfo());
1420 SDValue Lo64 =
1421 DAG.getLoad(VT: MVT::i64, dl: DL, Chain: Store, Ptr: LoPtrOff, PtrInfo: MachinePointerInfo());
1422
1423 Register HiReg = VA.getLocReg();
1424 Register LoReg = VA.getLocReg() + 1;
1425 if (!CLI.IsTailCall) {
1426 HiReg = toCallerWindow(Reg: HiReg);
1427 LoReg = toCallerWindow(Reg: LoReg);
1428 }
1429
1430 RegsToPass.push_back(Elt: std::make_pair(x&: HiReg, y&: Hi64));
1431 RegsToPass.push_back(Elt: std::make_pair(x&: LoReg, y&: Lo64));
1432 continue;
1433 }
1434
1435 // The custom bit on an i32 return value indicates that it should be
1436 // passed in the high bits of the register.
1437 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1438 Arg = DAG.getNode(Opcode: ISD::SHL, DL, VT: MVT::i64, N1: Arg,
1439 N2: DAG.getConstant(Val: 32, DL, VT: MVT::i32));
1440
1441 // The next value may go in the low bits of the same register.
1442 // Handle both at once.
1443 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1444 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1445 SDValue NV = DAG.getNode(Opcode: ISD::ZERO_EXTEND, DL, VT: MVT::i64,
1446 Operand: CLI.OutVals[i+1]);
1447 Arg = DAG.getNode(Opcode: ISD::OR, DL, VT: MVT::i64, N1: Arg, N2: NV);
1448 // Skip the next value, it's already done.
1449 ++i;
1450 }
1451 }
1452
1453 Register Reg = VA.getLocReg();
1454 if (!CLI.IsTailCall)
1455 Reg = toCallerWindow(Reg);
1456 RegsToPass.push_back(Elt: std::make_pair(x&: Reg, y&: Arg));
1457 continue;
1458 }
1459
1460 assert(VA.isMemLoc());
1461
1462 // Create a store off the stack pointer for this argument.
1463 SDValue StackPtr = DAG.getRegister(Reg: SP::O6, VT: PtrVT);
1464 // The argument area starts at %fp+BIAS+128 in the callee frame,
1465 // %sp+BIAS+128 in ours.
1466 SDValue PtrOff = DAG.getIntPtrConstant(Val: VA.getLocMemOffset() +
1467 Subtarget->getStackPointerBias() +
1468 128, DL);
1469 PtrOff = DAG.getNode(Opcode: ISD::ADD, DL, VT: PtrVT, N1: StackPtr, N2: PtrOff);
1470 MemOpChains.push_back(
1471 Elt: DAG.getStore(Chain, dl: DL, Val: Arg, Ptr: PtrOff, PtrInfo: MachinePointerInfo()));
1472 }
1473
1474 // Emit all stores, make sure they occur before the call.
1475 if (!MemOpChains.empty())
1476 Chain = DAG.getNode(Opcode: ISD::TokenFactor, DL, VT: MVT::Other, Ops: MemOpChains);
1477
1478 // Build a sequence of CopyToReg nodes glued together with token chain and
1479 // glue operands which copy the outgoing args into registers. The InGlue is
1480 // necessary since all emitted instructions must be stuck together in order
1481 // to pass the live physical registers.
1482 SDValue InGlue;
1483 for (const auto &[Reg, N] : RegsToPass) {
1484 Chain = DAG.getCopyToReg(Chain, dl: DL, Reg, N, Glue: InGlue);
1485 InGlue = Chain.getValue(R: 1);
1486 }
1487
1488 // If the callee is a GlobalAddress node (quite common, every direct call is)
1489 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1490 // Likewise ExternalSymbol -> TargetExternalSymbol.
1491 SDValue Callee = CLI.Callee;
1492 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, Call: CLI.CB);
1493 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Val&: Callee))
1494 Callee = DAG.getTargetGlobalAddress(GV: G->getGlobal(), DL, VT: PtrVT, offset: 0);
1495 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Val&: Callee))
1496 Callee = DAG.getTargetExternalSymbol(Sym: E->getSymbol(), VT: PtrVT);
1497
1498 // Build the operands for the call instruction itself.
1499 SmallVector<SDValue, 8> Ops;
1500 Ops.push_back(Elt: Chain);
1501 Ops.push_back(Elt: Callee);
1502 for (const auto &[Reg, N] : RegsToPass)
1503 Ops.push_back(Elt: DAG.getRegister(Reg, VT: N.getValueType()));
1504
1505 // Add a register mask operand representing the call-preserved registers.
1506 const SparcRegisterInfo *TRI = Subtarget->getRegisterInfo();
1507 const uint32_t *Mask =
1508 ((hasReturnsTwice) ? TRI->getRTCallPreservedMask(CC: CLI.CallConv)
1509 : TRI->getCallPreservedMask(MF: DAG.getMachineFunction(),
1510 CC: CLI.CallConv));
1511
1512 if (isAnyArgRegReserved(TRI, MF))
1513 emitReservedArgRegCallError(MF);
1514
1515 assert(Mask && "Missing call preserved mask for calling convention");
1516 Ops.push_back(Elt: DAG.getRegisterMask(RegMask: Mask));
1517
1518 // Make sure the CopyToReg nodes are glued to the call instruction which
1519 // consumes the registers.
1520 if (InGlue.getNode())
1521 Ops.push_back(Elt: InGlue);
1522
1523 // Now the call itself.
1524 if (CLI.IsTailCall) {
1525 DAG.getMachineFunction().getFrameInfo().setHasTailCall();
1526 return DAG.getNode(Opcode: SPISD::TAIL_CALL, DL, VT: MVT::Other, Ops);
1527 }
1528 SDVTList NodeTys = DAG.getVTList(VT1: MVT::Other, VT2: MVT::Glue);
1529 Chain = DAG.getNode(Opcode: SPISD::CALL, DL, VTList: NodeTys, Ops);
1530 InGlue = Chain.getValue(R: 1);
1531
1532 // Revert the stack pointer immediately after the call.
1533 Chain = DAG.getCALLSEQ_END(Chain, Size1: ArgsSize, Size2: 0, Glue: InGlue, DL);
1534 InGlue = Chain.getValue(R: 1);
1535
1536 // Now extract the return values. This is more or less the same as
1537 // LowerFormalArguments_64.
1538
1539 // Assign locations to each value returned by this call.
1540 SmallVector<CCValAssign, 16> RVLocs;
1541 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
1542 *DAG.getContext());
1543
1544 // Set inreg flag manually for codegen generated library calls that
1545 // return float.
1546 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && !CLI.CB)
1547 CLI.Ins[0].Flags.setInReg();
1548
1549 RVInfo.AnalyzeCallResult(Ins: CLI.Ins, Fn: RetCC_Sparc64);
1550
1551 // Copy all of the result registers out of their specified physreg.
1552 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1553 CCValAssign &VA = RVLocs[i];
1554 assert(VA.isRegLoc() && "Can only return in registers!");
1555 unsigned Reg = toCallerWindow(Reg: VA.getLocReg());
1556
1557 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1558 // reside in the same register in the high and low bits. Reuse the
1559 // CopyFromReg previous node to avoid duplicate copies.
1560 SDValue RV;
1561 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Val: Chain.getOperand(i: 1)))
1562 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1563 RV = Chain.getValue(R: 0);
1564
1565 // But usually we'll create a new CopyFromReg for a different register.
1566 if (!RV.getNode()) {
1567 RV = DAG.getCopyFromReg(Chain, dl: DL, Reg, VT: RVLocs[i].getLocVT(), Glue: InGlue);
1568 Chain = RV.getValue(R: 1);
1569 InGlue = Chain.getValue(R: 2);
1570 }
1571
1572 // Get the high bits for i32 struct elements.
1573 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1574 RV = DAG.getNode(Opcode: ISD::SRL, DL, VT: VA.getLocVT(), N1: RV,
1575 N2: DAG.getConstant(Val: 32, DL, VT: MVT::i32));
1576
1577 // The callee promoted the return value, so insert an Assert?ext SDNode so
1578 // we won't promote the value again in this function.
1579 switch (VA.getLocInfo()) {
1580 case CCValAssign::SExt:
1581 RV = DAG.getNode(Opcode: ISD::AssertSext, DL, VT: VA.getLocVT(), N1: RV,
1582 N2: DAG.getValueType(VA.getValVT()));
1583 break;
1584 case CCValAssign::ZExt:
1585 RV = DAG.getNode(Opcode: ISD::AssertZext, DL, VT: VA.getLocVT(), N1: RV,
1586 N2: DAG.getValueType(VA.getValVT()));
1587 break;
1588 default:
1589 break;
1590 }
1591
1592 // Truncate the register down to the return value type.
1593 if (VA.isExtInLoc())
1594 RV = DAG.getNode(Opcode: ISD::TRUNCATE, DL, VT: VA.getValVT(), Operand: RV);
1595
1596 InVals.push_back(Elt: RV);
1597 }
1598
1599 return Chain;
1600}
1601
1602//===----------------------------------------------------------------------===//
1603// TargetLowering Implementation
1604//===----------------------------------------------------------------------===//
1605
1606TargetLowering::AtomicExpansionKind
1607SparcTargetLowering::shouldExpandAtomicRMWInIR(const AtomicRMWInst *AI) const {
1608 if (AI->getOperation() == AtomicRMWInst::Xchg &&
1609 AI->getType()->getPrimitiveSizeInBits() == 32)
1610 return AtomicExpansionKind::None; // Uses xchg instruction
1611
1612 return AtomicExpansionKind::CmpXChg;
1613}
1614
1615/// intCondCCodeToRcond - Convert a DAG integer condition code to a SPARC
1616/// rcond condition.
1617static SPCC::CondCodes intCondCCodeToRcond(ISD::CondCode CC) {
1618 switch (CC) {
1619 default:
1620 llvm_unreachable("Unknown/unsigned integer condition code!");
1621 case ISD::SETEQ:
1622 return SPCC::REG_Z;
1623 case ISD::SETNE:
1624 return SPCC::REG_NZ;
1625 case ISD::SETLT:
1626 return SPCC::REG_LZ;
1627 case ISD::SETGT:
1628 return SPCC::REG_GZ;
1629 case ISD::SETLE:
1630 return SPCC::REG_LEZ;
1631 case ISD::SETGE:
1632 return SPCC::REG_GEZ;
1633 }
1634}
1635
1636/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1637/// condition.
1638static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1639 switch (CC) {
1640 default: llvm_unreachable("Unknown integer condition code!");
1641 case ISD::SETEQ: return SPCC::ICC_E;
1642 case ISD::SETNE: return SPCC::ICC_NE;
1643 case ISD::SETLT: return SPCC::ICC_L;
1644 case ISD::SETGT: return SPCC::ICC_G;
1645 case ISD::SETLE: return SPCC::ICC_LE;
1646 case ISD::SETGE: return SPCC::ICC_GE;
1647 case ISD::SETULT: return SPCC::ICC_CS;
1648 case ISD::SETULE: return SPCC::ICC_LEU;
1649 case ISD::SETUGT: return SPCC::ICC_GU;
1650 case ISD::SETUGE: return SPCC::ICC_CC;
1651 }
1652}
1653
1654/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1655/// FCC condition.
1656static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1657 switch (CC) {
1658 default: llvm_unreachable("Unknown fp condition code!");
1659 case ISD::SETEQ:
1660 case ISD::SETOEQ: return SPCC::FCC_E;
1661 case ISD::SETNE:
1662 case ISD::SETUNE: return SPCC::FCC_NE;
1663 case ISD::SETLT:
1664 case ISD::SETOLT: return SPCC::FCC_L;
1665 case ISD::SETGT:
1666 case ISD::SETOGT: return SPCC::FCC_G;
1667 case ISD::SETLE:
1668 case ISD::SETOLE: return SPCC::FCC_LE;
1669 case ISD::SETGE:
1670 case ISD::SETOGE: return SPCC::FCC_GE;
1671 case ISD::SETULT: return SPCC::FCC_UL;
1672 case ISD::SETULE: return SPCC::FCC_ULE;
1673 case ISD::SETUGT: return SPCC::FCC_UG;
1674 case ISD::SETUGE: return SPCC::FCC_UGE;
1675 case ISD::SETUO: return SPCC::FCC_U;
1676 case ISD::SETO: return SPCC::FCC_O;
1677 case ISD::SETONE: return SPCC::FCC_LG;
1678 case ISD::SETUEQ: return SPCC::FCC_UE;
1679 }
1680}
1681
1682SparcTargetLowering::SparcTargetLowering(const TargetMachine &TM,
1683 const SparcSubtarget &STI)
1684 : TargetLowering(TM, STI), Subtarget(&STI) {
1685 MVT PtrVT = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
1686
1687 // Instructions which use registers as conditionals examine all the
1688 // bits (as does the pseudo SELECT_CC expansion). I don't think it
1689 // matters much whether it's ZeroOrOneBooleanContent, or
1690 // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
1691 // former.
1692 setBooleanContents(ZeroOrOneBooleanContent);
1693 setBooleanVectorContents(ZeroOrOneBooleanContent);
1694
1695 // Set up the register classes.
1696 addRegisterClass(VT: MVT::i32, RC: &SP::IntRegsRegClass);
1697 if (!Subtarget->useSoftFloat()) {
1698 addRegisterClass(VT: MVT::f32, RC: &SP::FPRegsRegClass);
1699 addRegisterClass(VT: MVT::f64, RC: &SP::DFPRegsRegClass);
1700 addRegisterClass(VT: MVT::f128, RC: &SP::QFPRegsRegClass);
1701 }
1702 if (Subtarget->is64Bit()) {
1703 addRegisterClass(VT: MVT::i64, RC: &SP::I64RegsRegClass);
1704 } else {
1705 // On 32bit sparc, we define a double-register 32bit register
1706 // class, as well. This is modeled in LLVM as a 2-vector of i32.
1707 addRegisterClass(VT: MVT::v2i32, RC: &SP::IntPairRegClass);
1708
1709 // ...but almost all operations must be expanded, so set that as
1710 // the default.
1711 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
1712 setOperationAction(Op, VT: MVT::v2i32, Action: Expand);
1713 }
1714 // Truncating/extending stores/loads are also not supported.
1715 for (MVT VT : MVT::integer_fixedlen_vector_valuetypes()) {
1716 setLoadExtAction(ExtType: ISD::SEXTLOAD, ValVT: VT, MemVT: MVT::v2i32, Action: Expand);
1717 setLoadExtAction(ExtType: ISD::ZEXTLOAD, ValVT: VT, MemVT: MVT::v2i32, Action: Expand);
1718 setLoadExtAction(ExtType: ISD::EXTLOAD, ValVT: VT, MemVT: MVT::v2i32, Action: Expand);
1719
1720 setLoadExtAction(ExtType: ISD::SEXTLOAD, ValVT: MVT::v2i32, MemVT: VT, Action: Expand);
1721 setLoadExtAction(ExtType: ISD::ZEXTLOAD, ValVT: MVT::v2i32, MemVT: VT, Action: Expand);
1722 setLoadExtAction(ExtType: ISD::EXTLOAD, ValVT: MVT::v2i32, MemVT: VT, Action: Expand);
1723
1724 setTruncStoreAction(ValVT: VT, MemVT: MVT::v2i32, Action: Expand);
1725 setTruncStoreAction(ValVT: MVT::v2i32, MemVT: VT, Action: Expand);
1726 }
1727 // However, load and store *are* legal.
1728 setOperationAction(Op: ISD::LOAD, VT: MVT::v2i32, Action: Legal);
1729 setOperationAction(Op: ISD::STORE, VT: MVT::v2i32, Action: Legal);
1730 setOperationAction(Op: ISD::EXTRACT_VECTOR_ELT, VT: MVT::v2i32, Action: Custom);
1731 setOperationAction(Op: ISD::BUILD_VECTOR, VT: MVT::v2i32, Action: Legal);
1732 setOperationAction(Op: ISD::BITCAST, VT: MVT::f64, Action: Expand);
1733
1734 // And we need to promote i64 loads/stores into vector load/store
1735 setOperationAction(Op: ISD::LOAD, VT: MVT::i64, Action: Custom);
1736 setOperationAction(Op: ISD::STORE, VT: MVT::i64, Action: Custom);
1737
1738 // Sadly, this doesn't work:
1739 // AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
1740 // AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
1741 }
1742
1743 // Turn FP extload into load/fpextend
1744 for (MVT VT : MVT::fp_valuetypes()) {
1745 setLoadExtAction(ExtType: ISD::EXTLOAD, ValVT: VT, MemVT: MVT::f16, Action: Expand);
1746 setLoadExtAction(ExtType: ISD::EXTLOAD, ValVT: VT, MemVT: MVT::f32, Action: Expand);
1747 setLoadExtAction(ExtType: ISD::EXTLOAD, ValVT: VT, MemVT: MVT::f64, Action: Expand);
1748 }
1749
1750 // Sparc doesn't have i1 sign extending load
1751 for (MVT VT : MVT::integer_valuetypes())
1752 setLoadExtAction(ExtType: ISD::SEXTLOAD, ValVT: VT, MemVT: MVT::i1, Action: Promote);
1753
1754 // Turn FP truncstore into trunc + store.
1755 setTruncStoreAction(ValVT: MVT::f32, MemVT: MVT::f16, Action: Expand);
1756 setTruncStoreAction(ValVT: MVT::f64, MemVT: MVT::f16, Action: Expand);
1757 setTruncStoreAction(ValVT: MVT::f64, MemVT: MVT::f32, Action: Expand);
1758 setTruncStoreAction(ValVT: MVT::f128, MemVT: MVT::f16, Action: Expand);
1759 setTruncStoreAction(ValVT: MVT::f128, MemVT: MVT::f32, Action: Expand);
1760 setTruncStoreAction(ValVT: MVT::f128, MemVT: MVT::f64, Action: Expand);
1761
1762 // Custom legalize GlobalAddress nodes into LO/HI parts.
1763 setOperationAction(Op: ISD::GlobalAddress, VT: PtrVT, Action: Custom);
1764 setOperationAction(Op: ISD::GlobalTLSAddress, VT: PtrVT, Action: Custom);
1765 setOperationAction(Op: ISD::ConstantPool, VT: PtrVT, Action: Custom);
1766 setOperationAction(Op: ISD::BlockAddress, VT: PtrVT, Action: Custom);
1767
1768 // Sparc doesn't have sext_inreg, replace them with shl/sra
1769 setOperationAction(Op: ISD::SIGN_EXTEND_INREG, VT: MVT::i16, Action: Expand);
1770 setOperationAction(Op: ISD::SIGN_EXTEND_INREG, VT: MVT::i8 , Action: Expand);
1771 setOperationAction(Op: ISD::SIGN_EXTEND_INREG, VT: MVT::i1 , Action: Expand);
1772
1773 // Sparc has no REM or DIVREM operations.
1774 setOperationAction(Op: ISD::UREM, VT: MVT::i32, Action: Expand);
1775 setOperationAction(Op: ISD::SREM, VT: MVT::i32, Action: Expand);
1776 setOperationAction(Op: ISD::SDIVREM, VT: MVT::i32, Action: Expand);
1777 setOperationAction(Op: ISD::UDIVREM, VT: MVT::i32, Action: Expand);
1778
1779 // ... nor does SparcV9.
1780 if (Subtarget->is64Bit()) {
1781 setOperationAction(Op: ISD::UREM, VT: MVT::i64, Action: Expand);
1782 setOperationAction(Op: ISD::SREM, VT: MVT::i64, Action: Expand);
1783 setOperationAction(Op: ISD::SDIVREM, VT: MVT::i64, Action: Expand);
1784 setOperationAction(Op: ISD::UDIVREM, VT: MVT::i64, Action: Expand);
1785 }
1786
1787 // Custom expand fp<->sint
1788 setOperationAction(Op: ISD::FP_TO_SINT, VT: MVT::i32, Action: Custom);
1789 setOperationAction(Op: ISD::SINT_TO_FP, VT: MVT::i32, Action: Custom);
1790 setOperationAction(Op: ISD::FP_TO_SINT, VT: MVT::i64, Action: Custom);
1791 setOperationAction(Op: ISD::SINT_TO_FP, VT: MVT::i64, Action: Custom);
1792
1793 // Custom Expand fp<->uint
1794 setOperationAction(Op: ISD::FP_TO_UINT, VT: MVT::i32, Action: Custom);
1795 setOperationAction(Op: ISD::UINT_TO_FP, VT: MVT::i32, Action: Custom);
1796 setOperationAction(Op: ISD::FP_TO_UINT, VT: MVT::i64, Action: Custom);
1797 setOperationAction(Op: ISD::UINT_TO_FP, VT: MVT::i64, Action: Custom);
1798
1799 // Lower f16 conversion operations into library calls
1800 setOperationAction(Op: ISD::FP16_TO_FP, VT: MVT::f32, Action: Expand);
1801 setOperationAction(Op: ISD::FP_TO_FP16, VT: MVT::f32, Action: Expand);
1802 setOperationAction(Op: ISD::FP16_TO_FP, VT: MVT::f64, Action: Expand);
1803 setOperationAction(Op: ISD::FP_TO_FP16, VT: MVT::f64, Action: Expand);
1804 setOperationAction(Op: ISD::FP16_TO_FP, VT: MVT::f128, Action: Expand);
1805 setOperationAction(Op: ISD::FP_TO_FP16, VT: MVT::f128, Action: Expand);
1806
1807 setOperationAction(Op: ISD::BITCAST, VT: MVT::f32,
1808 Action: Subtarget->isVIS3() ? Legal : Expand);
1809 setOperationAction(Op: ISD::BITCAST, VT: MVT::i32,
1810 Action: Subtarget->isVIS3() ? Legal : Expand);
1811
1812 // Sparc has no select or setcc: expand to SELECT_CC.
1813 setOperationAction(Op: ISD::SELECT, VT: MVT::i32, Action: Expand);
1814 setOperationAction(Op: ISD::SELECT, VT: MVT::f32, Action: Expand);
1815 setOperationAction(Op: ISD::SELECT, VT: MVT::f64, Action: Expand);
1816 setOperationAction(Op: ISD::SELECT, VT: MVT::f128, Action: Expand);
1817
1818 setOperationAction(Op: ISD::SETCC, VT: MVT::i32, Action: Expand);
1819 setOperationAction(Op: ISD::SETCC, VT: MVT::f32, Action: Expand);
1820 setOperationAction(Op: ISD::SETCC, VT: MVT::f64, Action: Expand);
1821 setOperationAction(Op: ISD::SETCC, VT: MVT::f128, Action: Expand);
1822
1823 // Sparc doesn't have BRCOND either, it has BR_CC.
1824 setOperationAction(Op: ISD::BRCOND, VT: MVT::Other, Action: Expand);
1825 setOperationAction(Op: ISD::BRIND, VT: MVT::Other, Action: Expand);
1826 setOperationAction(Op: ISD::BR_JT, VT: MVT::Other, Action: Expand);
1827 setOperationAction(Op: ISD::BR_CC, VT: MVT::i32, Action: Custom);
1828 setOperationAction(Op: ISD::BR_CC, VT: MVT::f32, Action: Custom);
1829 setOperationAction(Op: ISD::BR_CC, VT: MVT::f64, Action: Custom);
1830 setOperationAction(Op: ISD::BR_CC, VT: MVT::f128, Action: Custom);
1831
1832 setOperationAction(Op: ISD::SELECT_CC, VT: MVT::i32, Action: Custom);
1833 setOperationAction(Op: ISD::SELECT_CC, VT: MVT::f32, Action: Custom);
1834 setOperationAction(Op: ISD::SELECT_CC, VT: MVT::f64, Action: Custom);
1835 setOperationAction(Op: ISD::SELECT_CC, VT: MVT::f128, Action: Custom);
1836
1837 setOperationAction(Op: ISD::ADDC, VT: MVT::i32, Action: Legal);
1838 setOperationAction(Op: ISD::ADDE, VT: MVT::i32, Action: Legal);
1839 setOperationAction(Op: ISD::SUBC, VT: MVT::i32, Action: Legal);
1840 setOperationAction(Op: ISD::SUBE, VT: MVT::i32, Action: Legal);
1841
1842 if (Subtarget->isVIS3()) {
1843 setOperationAction(Op: ISD::ADDC, VT: MVT::i64, Action: Legal);
1844 setOperationAction(Op: ISD::ADDE, VT: MVT::i64, Action: Legal);
1845 }
1846
1847 if (Subtarget->is64Bit()) {
1848 setOperationAction(Op: ISD::BITCAST, VT: MVT::f64,
1849 Action: Subtarget->isVIS3() ? Legal : Expand);
1850 setOperationAction(Op: ISD::BITCAST, VT: MVT::i64,
1851 Action: Subtarget->isVIS3() ? Legal : Expand);
1852 setOperationAction(Op: ISD::SELECT, VT: MVT::i64, Action: Expand);
1853 setOperationAction(Op: ISD::SETCC, VT: MVT::i64, Action: Expand);
1854 setOperationAction(Op: ISD::BR_CC, VT: MVT::i64, Action: Custom);
1855 setOperationAction(Op: ISD::SELECT_CC, VT: MVT::i64, Action: Custom);
1856
1857 setOperationAction(Op: ISD::CTPOP, VT: MVT::i64,
1858 Action: Subtarget->usePopc() ? Legal : Expand);
1859 setOperationAction(Op: ISD::BSWAP, VT: MVT::i64, Action: Custom);
1860 setOperationAction(Op: ISD::ROTL , VT: MVT::i64, Action: Expand);
1861 setOperationAction(Op: ISD::ROTR , VT: MVT::i64, Action: Expand);
1862 setOperationAction(Op: ISD::DYNAMIC_STACKALLOC, VT: MVT::i64, Action: Custom);
1863 }
1864
1865 // ATOMICs.
1866 // Atomics are supported on SparcV9. 32-bit atomics are also
1867 // supported by some Leon SparcV8 variants. Otherwise, atomics
1868 // are unsupported.
1869 if (Subtarget->isV9()) {
1870 // TODO: we _ought_ to be able to support 64-bit atomics on 32-bit sparcv9,
1871 // but it hasn't been implemented in the backend yet.
1872 if (Subtarget->is64Bit())
1873 setMaxAtomicSizeInBitsSupported(64);
1874 else
1875 setMaxAtomicSizeInBitsSupported(32);
1876 } else if (Subtarget->hasLeonCasa())
1877 setMaxAtomicSizeInBitsSupported(32);
1878 else
1879 setMaxAtomicSizeInBitsSupported(0);
1880
1881 setMinCmpXchgSizeInBits(32);
1882
1883 setOperationAction(Op: ISD::ATOMIC_SWAP, VT: MVT::i32, Action: Legal);
1884
1885 setOperationAction(Op: ISD::ATOMIC_FENCE, VT: MVT::Other, Action: Legal);
1886
1887 // Custom Lower Atomic LOAD/STORE
1888 setOperationAction(Op: ISD::ATOMIC_LOAD, VT: MVT::i32, Action: Custom);
1889 setOperationAction(Op: ISD::ATOMIC_STORE, VT: MVT::i32, Action: Custom);
1890
1891 if (Subtarget->is64Bit()) {
1892 setOperationAction(Op: ISD::ATOMIC_CMP_SWAP, VT: MVT::i64, Action: Legal);
1893 setOperationAction(Op: ISD::ATOMIC_SWAP, VT: MVT::i64, Action: Legal);
1894 setOperationAction(Op: ISD::ATOMIC_LOAD, VT: MVT::i64, Action: Custom);
1895 setOperationAction(Op: ISD::ATOMIC_STORE, VT: MVT::i64, Action: Custom);
1896 }
1897
1898 if (!Subtarget->isV9()) {
1899 // SparcV8 does not have FNEGD and FABSD.
1900 setOperationAction(Op: ISD::FNEG, VT: MVT::f64, Action: Custom);
1901 setOperationAction(Op: ISD::FABS, VT: MVT::f64, Action: Custom);
1902 }
1903
1904 setOperationAction(Op: ISD::FSIN , VT: MVT::f128, Action: Expand);
1905 setOperationAction(Op: ISD::FCOS , VT: MVT::f128, Action: Expand);
1906 setOperationAction(Op: ISD::FSINCOS, VT: MVT::f128, Action: Expand);
1907 setOperationAction(Op: ISD::FREM, VT: MVT::f128, Action: LibCall);
1908 setOperationAction(Op: ISD::FMA , VT: MVT::f128, Action: Expand);
1909 setOperationAction(Op: ISD::FSIN , VT: MVT::f64, Action: Expand);
1910 setOperationAction(Op: ISD::FCOS , VT: MVT::f64, Action: Expand);
1911 setOperationAction(Op: ISD::FSINCOS, VT: MVT::f64, Action: Expand);
1912 setOperationAction(Op: ISD::FREM, VT: MVT::f64, Action: LibCall);
1913 setOperationAction(Op: ISD::FMA, VT: MVT::f64,
1914 Action: Subtarget->isUA2007() ? Legal : Expand);
1915 setOperationAction(Op: ISD::FSIN , VT: MVT::f32, Action: Expand);
1916 setOperationAction(Op: ISD::FCOS , VT: MVT::f32, Action: Expand);
1917 setOperationAction(Op: ISD::FSINCOS, VT: MVT::f32, Action: Expand);
1918 setOperationAction(Op: ISD::FREM, VT: MVT::f32, Action: LibCall);
1919 setOperationAction(Op: ISD::FMA, VT: MVT::f32,
1920 Action: Subtarget->isUA2007() ? Legal : Expand);
1921 setOperationAction(Op: ISD::ROTL , VT: MVT::i32, Action: Expand);
1922 setOperationAction(Op: ISD::ROTR , VT: MVT::i32, Action: Expand);
1923 setOperationAction(Op: ISD::BSWAP, VT: MVT::i32, Action: Subtarget->isV9() ? Custom : Expand);
1924 setOperationAction(Op: ISD::FCOPYSIGN, VT: MVT::f128, Action: Expand);
1925 setOperationAction(Op: ISD::FCOPYSIGN, VT: MVT::f64, Action: Expand);
1926 setOperationAction(Op: ISD::FCOPYSIGN, VT: MVT::f32, Action: Expand);
1927 setOperationAction(Op: ISD::FPOW , VT: MVT::f128, Action: Expand);
1928 setOperationAction(Op: ISD::FPOW , VT: MVT::f64, Action: Expand);
1929 setOperationAction(Op: ISD::FPOW , VT: MVT::f32, Action: Expand);
1930
1931 setOperationAction(Op: ISD::SHL_PARTS, VT: MVT::i32, Action: Expand);
1932 setOperationAction(Op: ISD::SRA_PARTS, VT: MVT::i32, Action: Expand);
1933 setOperationAction(Op: ISD::SRL_PARTS, VT: MVT::i32, Action: Expand);
1934
1935 // Expands to [SU]MUL_LOHI.
1936 setOperationAction(Op: ISD::MULHU, VT: MVT::i32, Action: Expand);
1937 setOperationAction(Op: ISD::MULHS, VT: MVT::i32, Action: Expand);
1938 setOperationAction(Op: ISD::MUL, VT: MVT::i32, Action: Expand);
1939
1940 if (Subtarget->useSoftMulDiv()) {
1941 // .umul works for both signed and unsigned
1942 setOperationAction(Op: ISD::SMUL_LOHI, VT: MVT::i32, Action: Expand);
1943 setOperationAction(Op: ISD::UMUL_LOHI, VT: MVT::i32, Action: Expand);
1944 setOperationAction(Op: ISD::SDIV, VT: MVT::i32, Action: Expand);
1945 setOperationAction(Op: ISD::UDIV, VT: MVT::i32, Action: Expand);
1946 }
1947
1948 if (Subtarget->is64Bit()) {
1949 setOperationAction(Op: ISD::UMUL_LOHI, VT: MVT::i64, Action: Expand);
1950 setOperationAction(Op: ISD::SMUL_LOHI, VT: MVT::i64, Action: Expand);
1951 setOperationAction(Op: ISD::MULHU, VT: MVT::i64,
1952 Action: Subtarget->isVIS3() ? Legal : Expand);
1953 setOperationAction(Op: ISD::MULHS, VT: MVT::i64,
1954 Action: Subtarget->isVIS3() ? Legal : Expand);
1955
1956 setOperationAction(Op: ISD::SHL_PARTS, VT: MVT::i64, Action: Expand);
1957 setOperationAction(Op: ISD::SRA_PARTS, VT: MVT::i64, Action: Expand);
1958 setOperationAction(Op: ISD::SRL_PARTS, VT: MVT::i64, Action: Expand);
1959 }
1960
1961 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1962 setOperationAction(Op: ISD::VASTART , VT: MVT::Other, Action: Custom);
1963 // VAARG needs to be lowered to not do unaligned accesses for doubles.
1964 setOperationAction(Op: ISD::VAARG , VT: MVT::Other, Action: Custom);
1965
1966 setOperationAction(Op: ISD::TRAP , VT: MVT::Other, Action: Legal);
1967 setOperationAction(Op: ISD::DEBUGTRAP , VT: MVT::Other, Action: Legal);
1968
1969 // Use the default implementation.
1970 setOperationAction(Op: ISD::VACOPY , VT: MVT::Other, Action: Expand);
1971 setOperationAction(Op: ISD::VAEND , VT: MVT::Other, Action: Expand);
1972 setOperationAction(Op: ISD::STACKSAVE , VT: MVT::Other, Action: Expand);
1973 setOperationAction(Op: ISD::STACKRESTORE , VT: MVT::Other, Action: Expand);
1974 setOperationAction(Op: ISD::DYNAMIC_STACKALLOC, VT: MVT::i32 , Action: Custom);
1975 setOperationAction(Op: ISD::STACKADDRESS, VT: MVT::Other, Action: Custom);
1976
1977 setStackPointerRegisterToSaveRestore(SP::O6);
1978
1979 setOperationAction(Op: ISD::CTPOP, VT: MVT::i32,
1980 Action: Subtarget->usePopc() ? Legal : Expand);
1981
1982 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1983 setOperationAction(Op: ISD::LOAD, VT: MVT::f128, Action: Legal);
1984 setOperationAction(Op: ISD::STORE, VT: MVT::f128, Action: Legal);
1985 } else {
1986 setOperationAction(Op: ISD::LOAD, VT: MVT::f128, Action: Custom);
1987 setOperationAction(Op: ISD::STORE, VT: MVT::f128, Action: Custom);
1988 }
1989
1990 if (Subtarget->hasHardQuad()) {
1991 setOperationAction(Op: ISD::FADD, VT: MVT::f128, Action: Legal);
1992 setOperationAction(Op: ISD::FSUB, VT: MVT::f128, Action: Legal);
1993 setOperationAction(Op: ISD::FMUL, VT: MVT::f128, Action: Legal);
1994 setOperationAction(Op: ISD::FDIV, VT: MVT::f128, Action: Legal);
1995 setOperationAction(Op: ISD::FSQRT, VT: MVT::f128, Action: Legal);
1996 setOperationAction(Op: ISD::FP_EXTEND, VT: MVT::f128, Action: Legal);
1997 setOperationAction(Op: ISD::FP_ROUND, VT: MVT::f64, Action: Legal);
1998 if (Subtarget->isV9()) {
1999 setOperationAction(Op: ISD::FNEG, VT: MVT::f128, Action: Legal);
2000 setOperationAction(Op: ISD::FABS, VT: MVT::f128, Action: Legal);
2001 } else {
2002 setOperationAction(Op: ISD::FNEG, VT: MVT::f128, Action: Custom);
2003 setOperationAction(Op: ISD::FABS, VT: MVT::f128, Action: Custom);
2004 }
2005 } else {
2006 // Custom legalize f128 operations.
2007
2008 setOperationAction(Op: ISD::FADD, VT: MVT::f128, Action: Custom);
2009 setOperationAction(Op: ISD::FSUB, VT: MVT::f128, Action: Custom);
2010 setOperationAction(Op: ISD::FMUL, VT: MVT::f128, Action: Custom);
2011 setOperationAction(Op: ISD::FDIV, VT: MVT::f128, Action: Custom);
2012 setOperationAction(Op: ISD::FSQRT, VT: MVT::f128, Action: Custom);
2013 setOperationAction(Op: ISD::FNEG, VT: MVT::f128, Action: Custom);
2014 setOperationAction(Op: ISD::FABS, VT: MVT::f128, Action: Custom);
2015
2016 setOperationAction(Op: ISD::FP_EXTEND, VT: MVT::f128, Action: Custom);
2017 setOperationAction(Op: ISD::FP_ROUND, VT: MVT::f64, Action: Custom);
2018 setOperationAction(Op: ISD::FP_ROUND, VT: MVT::f32, Action: Custom);
2019 }
2020
2021 if (Subtarget->hasNoFMULS()) {
2022 setOperationAction(Op: ISD::FMUL, VT: MVT::f32, Action: Promote);
2023 }
2024
2025 // Constrained (strict) floating-point support. With soft-float all FP
2026 // operations become libcalls during type legalization instead.
2027 IsStrictFPEnabled = true;
2028 if (!Subtarget->useSoftFloat()) {
2029 for (MVT VT : {MVT::f32, MVT::f64}) {
2030 setOperationAction(Ops: {ISD::STRICT_FADD, ISD::STRICT_FSUB, ISD::STRICT_FMUL,
2031 ISD::STRICT_FDIV, ISD::STRICT_FSQRT},
2032 VT, Action: Legal);
2033 setOperationAction(Op: ISD::STRICT_FMA, VT,
2034 Action: Subtarget->isUA2007() ? Legal : Expand);
2035 }
2036 setOperationAction(Op: ISD::STRICT_FP_EXTEND, VT: MVT::f64, Action: Legal);
2037 setOperationAction(Op: ISD::STRICT_FP_ROUND, VT: MVT::f32, Action: Legal);
2038
2039 for (MVT VT : {MVT::f32, MVT::f64, MVT::f128})
2040 setOperationAction(Ops: {ISD::STRICT_FSETCC, ISD::STRICT_FSETCCS}, VT, Action: Custom);
2041
2042 for (MVT VT : {MVT::i32, MVT::i64})
2043 setOperationAction(Ops: {ISD::STRICT_FP_TO_SINT, ISD::STRICT_FP_TO_UINT,
2044 ISD::STRICT_SINT_TO_FP, ISD::STRICT_UINT_TO_FP},
2045 VT, Action: Custom);
2046
2047 if (Subtarget->hasHardQuad()) {
2048 setOperationAction(Ops: {ISD::STRICT_FADD, ISD::STRICT_FSUB, ISD::STRICT_FMUL,
2049 ISD::STRICT_FDIV, ISD::STRICT_FSQRT,
2050 ISD::STRICT_FP_EXTEND},
2051 VT: MVT::f128, Action: Legal);
2052 setOperationAction(Op: ISD::STRICT_FP_ROUND, VT: MVT::f64, Action: Legal);
2053 } else {
2054 setOperationAction(Ops: {ISD::STRICT_FADD, ISD::STRICT_FSUB, ISD::STRICT_FMUL,
2055 ISD::STRICT_FDIV, ISD::STRICT_FSQRT,
2056 ISD::STRICT_FP_EXTEND},
2057 VT: MVT::f128, Action: Custom);
2058 setOperationAction(Op: ISD::STRICT_FP_ROUND, VT: MVT::f64, Action: Custom);
2059 setOperationAction(Op: ISD::STRICT_FP_ROUND, VT: MVT::f32, Action: Custom);
2060 }
2061
2062 if (Subtarget->hasNoFMULS())
2063 setOperationAction(Op: ISD::STRICT_FMUL, VT: MVT::f32, Action: Promote);
2064 }
2065
2066 // Custom combine bitcast between f64 and v2i32
2067 if (!Subtarget->is64Bit())
2068 setTargetDAGCombine(ISD::BITCAST);
2069
2070 if (Subtarget->isV9())
2071 setTargetDAGCombine({ISD::BSWAP, ISD::STORE});
2072
2073 if (Subtarget->hasLeonCycleCounter())
2074 setOperationAction(Ops: {ISD::READCYCLECOUNTER, ISD::READSTEADYCOUNTER},
2075 VT: MVT::i64, Action: Custom);
2076
2077 if (Subtarget->isVIS3()) {
2078 setOperationAction(Op: ISD::CTLZ, VT: MVT::i32, Action: Legal);
2079 setOperationAction(Op: ISD::CTLZ, VT: MVT::i64, Action: Legal);
2080 setOperationAction(Op: ISD::CTLZ_ZERO_POISON, VT: MVT::i32, Action: Legal);
2081 setOperationAction(Op: ISD::CTLZ_ZERO_POISON, VT: MVT::i64, Action: Legal);
2082
2083 setOperationAction(Op: ISD::CTTZ, VT: MVT::i32,
2084 Action: Subtarget->is64Bit() ? Promote : Expand);
2085 setOperationAction(Op: ISD::CTTZ, VT: MVT::i64, Action: Expand);
2086 setOperationAction(Op: ISD::CTTZ_ZERO_POISON, VT: MVT::i32,
2087 Action: Subtarget->is64Bit() ? Promote : Expand);
2088 setOperationAction(Op: ISD::CTTZ_ZERO_POISON, VT: MVT::i64, Action: Expand);
2089 } else if (Subtarget->usePopc()) {
2090 setOperationAction(Op: ISD::CTLZ, VT: MVT::i32, Action: Expand);
2091 setOperationAction(Op: ISD::CTLZ, VT: MVT::i64, Action: Expand);
2092 setOperationAction(Op: ISD::CTLZ_ZERO_POISON, VT: MVT::i32, Action: Expand);
2093 setOperationAction(Op: ISD::CTLZ_ZERO_POISON, VT: MVT::i64, Action: Expand);
2094
2095 setOperationAction(Op: ISD::CTTZ, VT: MVT::i32, Action: Expand);
2096 setOperationAction(Op: ISD::CTTZ, VT: MVT::i64, Action: Expand);
2097 setOperationAction(Op: ISD::CTTZ_ZERO_POISON, VT: MVT::i32, Action: Expand);
2098 setOperationAction(Op: ISD::CTTZ_ZERO_POISON, VT: MVT::i64, Action: Expand);
2099 } else {
2100 setOperationAction(Op: ISD::CTLZ, VT: MVT::i32, Action: Expand);
2101 setOperationAction(Op: ISD::CTLZ, VT: MVT::i64, Action: Expand);
2102 setOperationAction(Op: ISD::CTLZ_ZERO_POISON, VT: MVT::i32,
2103 Action: Subtarget->is64Bit() ? Promote : LibCall);
2104 setOperationAction(Op: ISD::CTLZ_ZERO_POISON, VT: MVT::i64, Action: LibCall);
2105
2106 // FIXME here we don't have any ISA extensions that could help us, so to
2107 // prevent large expansions those should be made into LibCalls.
2108 setOperationAction(Op: ISD::CTTZ, VT: MVT::i32, Action: Expand);
2109 setOperationAction(Op: ISD::CTTZ, VT: MVT::i64, Action: Expand);
2110 setOperationAction(Op: ISD::CTTZ_ZERO_POISON, VT: MVT::i32, Action: Expand);
2111 setOperationAction(Op: ISD::CTTZ_ZERO_POISON, VT: MVT::i64, Action: Expand);
2112 }
2113
2114 setOperationAction(Op: ISD::INTRINSIC_WO_CHAIN, VT: MVT::Other, Action: Custom);
2115
2116 // Some processors have no branch predictor and have pipelines longer than
2117 // what can be covered by the delay slot. This results in a stall, so mark
2118 // branches to be expensive on those processors.
2119 setJumpIsExpensive(Subtarget->hasNoPredictor());
2120 // The high cost of branching means that using conditional moves will
2121 // still be profitable even if the condition is predictable.
2122 PredictableSelectIsExpensive = !isJumpExpensive();
2123
2124 setMinFunctionAlignment(Align(4));
2125
2126 computeRegisterProperties(TRI: Subtarget->getRegisterInfo());
2127}
2128
2129bool SparcTargetLowering::useSoftFloat() const {
2130 return Subtarget->useSoftFloat();
2131}
2132
2133EVT SparcTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
2134 EVT VT) const {
2135 if (!VT.isVector())
2136 return MVT::i32;
2137 return VT.changeVectorElementTypeToInteger();
2138}
2139
2140/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
2141/// be zero. Op is expected to be a target specific node. Used by DAG
2142/// combiner.
2143void SparcTargetLowering::computeKnownBitsForTargetNode
2144 (const SDValue Op,
2145 KnownBits &Known,
2146 const APInt &DemandedElts,
2147 const SelectionDAG &DAG,
2148 unsigned Depth) const {
2149 KnownBits Known2;
2150 Known.resetAll();
2151
2152 switch (Op.getOpcode()) {
2153 default: break;
2154 case SPISD::SELECT_ICC:
2155 case SPISD::SELECT_XCC:
2156 case SPISD::SELECT_FCC:
2157 Known = DAG.computeKnownBits(Op: Op.getOperand(i: 1), Depth: Depth + 1);
2158 Known2 = DAG.computeKnownBits(Op: Op.getOperand(i: 0), Depth: Depth + 1);
2159
2160 // Only known if known in both the LHS and RHS.
2161 Known = Known.intersectWith(RHS: Known2);
2162 break;
2163 }
2164}
2165
2166// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
2167// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
2168static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
2169 ISD::CondCode CC, unsigned &SPCC) {
2170 if (isNullConstant(V: RHS) && CC == ISD::SETNE &&
2171 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
2172 LHS.getOpcode() == SPISD::SELECT_XCC) &&
2173 LHS.getOperand(i: 3).getOpcode() == SPISD::CMPICC) ||
2174 (LHS.getOpcode() == SPISD::SELECT_FCC &&
2175 (LHS.getOperand(i: 3).getOpcode() == SPISD::CMPFCC ||
2176 LHS.getOperand(i: 3).getOpcode() == SPISD::CMPFCC_V9))) &&
2177 isOneConstant(V: LHS.getOperand(i: 0)) && isNullConstant(V: LHS.getOperand(i: 1))) {
2178 SDValue CMPCC = LHS.getOperand(i: 3);
2179 SPCC = LHS.getConstantOperandVal(i: 2);
2180 LHS = CMPCC.getOperand(i: 0);
2181 RHS = CMPCC.getOperand(i: 1);
2182 }
2183}
2184
2185// Convert to a target node and set target flags.
2186SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
2187 SelectionDAG &DAG) const {
2188 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Val&: Op))
2189 return DAG.getTargetGlobalAddress(GV: GA->getGlobal(),
2190 DL: SDLoc(GA),
2191 VT: GA->getValueType(ResNo: 0),
2192 offset: GA->getOffset(), TargetFlags: TF);
2193
2194 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Val&: Op))
2195 return DAG.getTargetConstantPool(C: CP->getConstVal(), VT: CP->getValueType(ResNo: 0),
2196 Align: CP->getAlign(), Offset: CP->getOffset(), TargetFlags: TF);
2197
2198 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Val&: Op))
2199 return DAG.getTargetBlockAddress(BA: BA->getBlockAddress(),
2200 VT: Op.getValueType(),
2201 Offset: 0,
2202 TargetFlags: TF);
2203
2204 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Val&: Op))
2205 return DAG.getTargetExternalSymbol(Sym: ES->getSymbol(),
2206 VT: ES->getValueType(ResNo: 0), TargetFlags: TF);
2207
2208 llvm_unreachable("Unhandled address SDNode");
2209}
2210
2211// Split Op into high and low parts according to HiTF and LoTF.
2212// Return an ADD node combining the parts.
2213SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
2214 unsigned HiTF, unsigned LoTF,
2215 SelectionDAG &DAG) const {
2216 SDLoc DL(Op);
2217 EVT VT = Op.getValueType();
2218 SDValue Hi = DAG.getNode(Opcode: SPISD::Hi, DL, VT, Operand: withTargetFlags(Op, TF: HiTF, DAG));
2219 SDValue Lo = DAG.getNode(Opcode: SPISD::Lo, DL, VT, Operand: withTargetFlags(Op, TF: LoTF, DAG));
2220 return DAG.getNode(Opcode: ISD::ADD, DL, VT, N1: Hi, N2: Lo);
2221}
2222
2223// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
2224// or ExternalSymbol SDNode.
2225SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
2226 SDLoc DL(Op);
2227 EVT VT = getPointerTy(DL: DAG.getDataLayout());
2228
2229 // Handle PIC mode first.
2230 if (isPositionIndependent()) {
2231 const Module *M = DAG.getMachineFunction().getFunction().getParent();
2232 PICLevel::Level picLevel = M->getPICLevel();
2233
2234 SDValue GlobalBase = DAG.getNode(Opcode: SPISD::GLOBAL_BASE_REG, DL, VT);
2235 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2236 // function has calls.
2237 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2238 MFI.setHasCalls(true);
2239
2240 if (picLevel == PICLevel::SmallPIC) {
2241 // This is the pic13 code model, the GOT is known to be smaller than 8KiB.
2242 // There is no 13-bit form of the GOTDATA_OP relocations, so always load
2243 // the address from the GOT.
2244 SDValue Idx = DAG.getNode(Opcode: SPISD::Lo, DL, VT: Op.getValueType(),
2245 Operand: withTargetFlags(Op, TF: ELF::R_SPARC_GOT13, DAG));
2246 SDValue AbsAddr = DAG.getNode(Opcode: ISD::ADD, DL, VT, N1: GlobalBase, N2: Idx);
2247 return DAG.getLoad(VT, dl: DL, Chain: DAG.getEntryNode(), Ptr: AbsAddr,
2248 PtrInfo: MachinePointerInfo::getGOT(MF&: DAG.getMachineFunction()));
2249 }
2250
2251 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
2252 // Use the GOTDATA_OP sequence:
2253 //
2254 // sethi %gdop_hix22(sym), %r1
2255 // xor %r1, %gdop_lox10(sym), %r1
2256 // ld[x] [%gotbase + %r1], %r2, %gdop(sym)
2257 //
2258 // The three relocations tell the linker this is a GOT load of sym's
2259 // address. If sym binds locally, the linker rewrites the sethi/xor to
2260 // produce sym's offset from the GOT and turns the load into an add, so
2261 // nothing is read from the GOT at run time. This matches GCC and matters
2262 // for code that runs before dynamic relocations are applied, such as the
2263 // dynamic linker's own startup code.
2264 SDValue Hi =
2265 DAG.getNode(Opcode: SPISD::Hi, DL, VT,
2266 Operand: withTargetFlags(Op, TF: ELF::R_SPARC_GOTDATA_OP_HIX22, DAG));
2267 SDValue Lo =
2268 DAG.getNode(Opcode: SPISD::Lo, DL, VT,
2269 Operand: withTargetFlags(Op, TF: ELF::R_SPARC_GOTDATA_OP_LOX10, DAG));
2270 SDValue Idx = DAG.getNode(Opcode: ISD::XOR, DL, VT, N1: Hi, N2: Lo);
2271 SDValue AbsAddr = DAG.getNode(Opcode: ISD::ADD, DL, VT, N1: GlobalBase, N2: Idx);
2272 // Attach a GOT memory operand, as for the plain GOT load above, so that
2273 // the load is still known to be invariant and can be hoisted, CSE'd and
2274 // rematerialized.
2275 SDValue Ops[] = {DAG.getEntryNode(), AbsAddr,
2276 withTargetFlags(Op, TF: ELF::R_SPARC_GOTDATA_OP, DAG)};
2277 return DAG.getMemIntrinsicNode(
2278 Opcode: SPISD::LOAD_GDOP, dl: DL, VTList: DAG.getVTList(VT1: VT, VT2: MVT::Other), Ops, MemVT: VT,
2279 PtrInfo: MachinePointerInfo::getGOT(MF&: DAG.getMachineFunction()),
2280 /*Alignment=*/std::nullopt, Flags: MachineMemOperand::MOLoad);
2281 }
2282
2283 // This is one of the absolute code models.
2284 switch(getTargetMachine().getCodeModel()) {
2285 default:
2286 llvm_unreachable("Unsupported absolute code model");
2287 case CodeModel::Small:
2288 // abs32.
2289 return makeHiLoPair(Op, HiTF: ELF::R_SPARC_HI22, LoTF: ELF::R_SPARC_LO10, DAG);
2290 case CodeModel::Medium: {
2291 // abs44.
2292 SDValue H44 = makeHiLoPair(Op, HiTF: ELF::R_SPARC_H44, LoTF: ELF::R_SPARC_M44, DAG);
2293 H44 = DAG.getNode(Opcode: ISD::SHL, DL, VT, N1: H44, N2: DAG.getConstant(Val: 12, DL, VT: MVT::i32));
2294 SDValue L44 = withTargetFlags(Op, TF: ELF::R_SPARC_L44, DAG);
2295 L44 = DAG.getNode(Opcode: SPISD::Lo, DL, VT, Operand: L44);
2296 return DAG.getNode(Opcode: ISD::ADD, DL, VT, N1: H44, N2: L44);
2297 }
2298 case CodeModel::Large: {
2299 // abs64.
2300 SDValue Hi = makeHiLoPair(Op, HiTF: ELF::R_SPARC_HH22, LoTF: ELF::R_SPARC_HM10, DAG);
2301 Hi = DAG.getNode(Opcode: ISD::SHL, DL, VT, N1: Hi, N2: DAG.getConstant(Val: 32, DL, VT: MVT::i32));
2302 SDValue Lo = makeHiLoPair(Op, HiTF: ELF::R_SPARC_HI22, LoTF: ELF::R_SPARC_LO10, DAG);
2303 return DAG.getNode(Opcode: ISD::ADD, DL, VT, N1: Hi, N2: Lo);
2304 }
2305 }
2306}
2307
2308SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
2309 SelectionDAG &DAG) const {
2310 return makeAddress(Op, DAG);
2311}
2312
2313SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
2314 SelectionDAG &DAG) const {
2315 return makeAddress(Op, DAG);
2316}
2317
2318SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
2319 SelectionDAG &DAG) const {
2320 return makeAddress(Op, DAG);
2321}
2322
2323SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
2324 SelectionDAG &DAG) const {
2325
2326 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Val&: Op);
2327 if (DAG.getTarget().useEmulatedTLS())
2328 return LowerToTLSEmulatedModel(GA, DAG);
2329
2330 SDLoc DL(GA);
2331 const GlobalValue *GV = GA->getGlobal();
2332 EVT PtrVT = getPointerTy(DL: DAG.getDataLayout());
2333
2334 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2335
2336 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
2337 unsigned HiTF =
2338 ((model == TLSModel::GeneralDynamic) ? ELF::R_SPARC_TLS_GD_HI22
2339 : ELF::R_SPARC_TLS_LDM_HI22);
2340 unsigned LoTF =
2341 ((model == TLSModel::GeneralDynamic) ? ELF::R_SPARC_TLS_GD_LO10
2342 : ELF::R_SPARC_TLS_LDM_LO10);
2343 unsigned addTF =
2344 ((model == TLSModel::GeneralDynamic) ? ELF::R_SPARC_TLS_GD_ADD
2345 : ELF::R_SPARC_TLS_LDM_ADD);
2346 unsigned callTF =
2347 ((model == TLSModel::GeneralDynamic) ? ELF::R_SPARC_TLS_GD_CALL
2348 : ELF::R_SPARC_TLS_LDM_CALL);
2349
2350 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
2351 SDValue Base = DAG.getNode(Opcode: SPISD::GLOBAL_BASE_REG, DL, VT: PtrVT);
2352 SDValue Argument = DAG.getNode(Opcode: SPISD::TLS_ADD, DL, VT: PtrVT, N1: Base, N2: HiLo,
2353 N3: withTargetFlags(Op, TF: addTF, DAG));
2354
2355 SDValue Chain = DAG.getEntryNode();
2356 SDValue InGlue;
2357
2358 Chain = DAG.getCALLSEQ_START(Chain, InSize: 0, OutSize: 0, DL);
2359 Chain = DAG.getCopyToReg(Chain, dl: DL, Reg: SP::O0, N: Argument, Glue: InGlue);
2360 InGlue = Chain.getValue(R: 1);
2361 SDValue Callee = DAG.getTargetExternalSymbol(Sym: "__tls_get_addr", VT: PtrVT);
2362 SDValue Symbol = withTargetFlags(Op, TF: callTF, DAG);
2363
2364 SDVTList NodeTys = DAG.getVTList(VT1: MVT::Other, VT2: MVT::Glue);
2365 const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
2366 MF: DAG.getMachineFunction(), CC: CallingConv::C);
2367 assert(Mask && "Missing call preserved mask for calling convention");
2368 SDValue Ops[] = {Chain,
2369 Callee,
2370 Symbol,
2371 DAG.getRegister(Reg: SP::O0, VT: PtrVT),
2372 DAG.getRegisterMask(RegMask: Mask),
2373 InGlue};
2374 Chain = DAG.getNode(Opcode: SPISD::TLS_CALL, DL, VTList: NodeTys, Ops);
2375 InGlue = Chain.getValue(R: 1);
2376 Chain = DAG.getCALLSEQ_END(Chain, Size1: 0, Size2: 0, Glue: InGlue, DL);
2377 InGlue = Chain.getValue(R: 1);
2378 SDValue Ret = DAG.getCopyFromReg(Chain, dl: DL, Reg: SP::O0, VT: PtrVT, Glue: InGlue);
2379
2380 if (model != TLSModel::LocalDynamic)
2381 return Ret;
2382
2383 SDValue Hi =
2384 DAG.getNode(Opcode: SPISD::Hi, DL, VT: PtrVT,
2385 Operand: withTargetFlags(Op, TF: ELF::R_SPARC_TLS_LDO_HIX22, DAG));
2386 SDValue Lo =
2387 DAG.getNode(Opcode: SPISD::Lo, DL, VT: PtrVT,
2388 Operand: withTargetFlags(Op, TF: ELF::R_SPARC_TLS_LDO_LOX10, DAG));
2389 HiLo = DAG.getNode(Opcode: ISD::XOR, DL, VT: PtrVT, N1: Hi, N2: Lo);
2390 return DAG.getNode(Opcode: SPISD::TLS_ADD, DL, VT: PtrVT, N1: Ret, N2: HiLo,
2391 N3: withTargetFlags(Op, TF: ELF::R_SPARC_TLS_LDO_ADD, DAG));
2392 }
2393
2394 if (model == TLSModel::InitialExec) {
2395 unsigned ldTF = ((PtrVT == MVT::i64) ? ELF::R_SPARC_TLS_IE_LDX
2396 : ELF::R_SPARC_TLS_IE_LD);
2397
2398 SDValue Base = DAG.getNode(Opcode: SPISD::GLOBAL_BASE_REG, DL, VT: PtrVT);
2399
2400 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
2401 // function has calls.
2402 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2403 MFI.setHasCalls(true);
2404
2405 SDValue TGA = makeHiLoPair(Op, HiTF: ELF::R_SPARC_TLS_IE_HI22,
2406 LoTF: ELF::R_SPARC_TLS_IE_LO10, DAG);
2407 SDValue Ptr = DAG.getNode(Opcode: ISD::ADD, DL, VT: PtrVT, N1: Base, N2: TGA);
2408 SDValue Ops[] = {DAG.getEntryNode(), Ptr, withTargetFlags(Op, TF: ldTF, DAG)};
2409 SDValue Offset = DAG.getMemIntrinsicNode(
2410 Opcode: SPISD::TLS_LD, dl: DL, VTList: DAG.getVTList(VT1: PtrVT, VT2: MVT::Other), Ops, MemVT: PtrVT,
2411 PtrInfo: MachinePointerInfo::getGOT(MF&: DAG.getMachineFunction()),
2412 /*Alignment=*/std::nullopt, Flags: MachineMemOperand::MOLoad);
2413 return DAG.getNode(Opcode: SPISD::TLS_ADD, DL, VT: PtrVT,
2414 N1: DAG.getRegister(Reg: SP::G7, VT: PtrVT), N2: Offset,
2415 N3: withTargetFlags(Op, TF: ELF::R_SPARC_TLS_IE_ADD, DAG));
2416 }
2417
2418 assert(model == TLSModel::LocalExec);
2419 SDValue Hi = DAG.getNode(Opcode: SPISD::Hi, DL, VT: PtrVT,
2420 Operand: withTargetFlags(Op, TF: ELF::R_SPARC_TLS_LE_HIX22, DAG));
2421 SDValue Lo = DAG.getNode(Opcode: SPISD::Lo, DL, VT: PtrVT,
2422 Operand: withTargetFlags(Op, TF: ELF::R_SPARC_TLS_LE_LOX10, DAG));
2423 SDValue Offset = DAG.getNode(Opcode: ISD::XOR, DL, VT: PtrVT, N1: Hi, N2: Lo);
2424
2425 return DAG.getNode(Opcode: ISD::ADD, DL, VT: PtrVT,
2426 N1: DAG.getRegister(Reg: SP::G7, VT: PtrVT), N2: Offset);
2427}
2428
2429SDValue SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain,
2430 ArgListTy &Args, SDValue Arg,
2431 const SDLoc &DL,
2432 SelectionDAG &DAG) const {
2433 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2434 EVT ArgVT = Arg.getValueType();
2435 Type *ArgTy = ArgVT.getTypeForEVT(Context&: *DAG.getContext());
2436
2437 if (ArgTy->isFP128Ty()) {
2438 // Create a stack object and pass the pointer to the library function.
2439 int FI = MFI.CreateStackObject(Size: 16, Alignment: Align(8), isSpillSlot: false);
2440 SDValue FIPtr = DAG.getFrameIndex(FI, VT: getPointerTy(DL: DAG.getDataLayout()));
2441 Chain = DAG.getStore(Chain, dl: DL, Val: Arg, Ptr: FIPtr, PtrInfo: MachinePointerInfo(), Alignment: Align(8));
2442 Args.emplace_back(args&: FIPtr, args: PointerType::getUnqual(C&: ArgTy->getContext()));
2443 } else {
2444 Args.emplace_back(args&: Arg, args&: ArgTy);
2445 }
2446 return Chain;
2447}
2448
2449SDValue SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2450 RTLIB::Libcall LibFunc,
2451 unsigned numArgs) const {
2452 RTLIB::LibcallImpl LibFuncImpl = DAG.getLibcalls().getLibcallImpl(Call: LibFunc);
2453 if (LibFuncImpl == RTLIB::Unsupported)
2454 return SDValue();
2455
2456 ArgListTy Args;
2457
2458 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2459 auto PtrVT = getPointerTy(DL: DAG.getDataLayout());
2460
2461 SDValue Callee = DAG.getExternalSymbol(LCImpl: LibFuncImpl, VT: PtrVT);
2462 Type *RetTy = Op.getValueType().getTypeForEVT(Context&: *DAG.getContext());
2463 Type *RetTyABI = RetTy;
2464 // Constrained operations carry their chain as operand 0 and must produce
2465 // an output chain, so the libcall is ordered with respect to other
2466 // FP-environment accesses.
2467 bool IsStrict = Op->isStrictFPOpcode();
2468 SDValue Chain = IsStrict ? Op.getOperand(i: 0) : DAG.getEntryNode();
2469 unsigned FirstArg = IsStrict ? 1 : 0;
2470 SDLoc DL(Op);
2471 SDValue RetPtr;
2472
2473 if (RetTy->isFP128Ty()) {
2474 // Create a Stack Object to receive the return value of type f128.
2475 int RetFI = MFI.CreateStackObject(Size: 16, Alignment: Align(8), isSpillSlot: false);
2476 RetPtr = DAG.getFrameIndex(FI: RetFI, VT: PtrVT);
2477 ArgListEntry Entry(RetPtr, PointerType::getUnqual(C&: RetTy->getContext()));
2478 if (!Subtarget->is64Bit()) {
2479 Entry.IsSRet = true;
2480 Entry.IndirectType = RetTy;
2481 }
2482 Entry.IsReturned = false;
2483 Args.push_back(x: Entry);
2484 RetTyABI = Type::getVoidTy(C&: *DAG.getContext());
2485 }
2486
2487 assert(Op->getNumOperands() >= numArgs + FirstArg && "Not enough operands!");
2488 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2489 Chain =
2490 LowerF128_LibCallArg(Chain, Args, Arg: Op.getOperand(i: i + FirstArg), DL, DAG);
2491 }
2492
2493 CallingConv::ID CC = DAG.getLibcalls().getLibcallImplCallingConv(Call: LibFuncImpl);
2494 TargetLowering::CallLoweringInfo CLI(DAG);
2495 CLI.setDebugLoc(DL).setChain(Chain).setCallee(CC, ResultType: RetTyABI, Target: Callee,
2496 ArgsList: std::move(Args));
2497
2498 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2499
2500 // chain is in second result.
2501 if (RetTyABI == RetTy) {
2502 if (IsStrict)
2503 return DAG.getMergeValues(Ops: {CallInfo.first, CallInfo.second}, dl: DL);
2504 return CallInfo.first;
2505 }
2506
2507 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2508
2509 Chain = CallInfo.second;
2510
2511 // Load RetPtr to get the return value.
2512 SDValue Result = DAG.getLoad(VT: Op.getValueType(), dl: DL, Chain, Ptr: RetPtr,
2513 PtrInfo: MachinePointerInfo(), Alignment: Align(8));
2514 if (IsStrict)
2515 return DAG.getMergeValues(Ops: {Result, Result.getValue(R: 1)}, dl: DL);
2516 return Result;
2517}
2518
2519SDValue SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2520 unsigned &SPCC, const SDLoc &DL,
2521 SelectionDAG &DAG) const {
2522
2523 const char *LibCall = nullptr;
2524 bool is64Bit = Subtarget->is64Bit();
2525 switch(SPCC) {
2526 default: llvm_unreachable("Unhandled conditional code!");
2527 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2528 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2529 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2530 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2531 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2532 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2533 case SPCC::FCC_UL :
2534 case SPCC::FCC_ULE:
2535 case SPCC::FCC_UG :
2536 case SPCC::FCC_UGE:
2537 case SPCC::FCC_U :
2538 case SPCC::FCC_O :
2539 case SPCC::FCC_LG :
2540 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2541 }
2542
2543 auto PtrVT = getPointerTy(DL: DAG.getDataLayout());
2544 SDValue Callee = DAG.getExternalSymbol(Sym: LibCall, VT: PtrVT);
2545 Type *RetTy = Type::getInt32Ty(C&: *DAG.getContext());
2546 ArgListTy Args;
2547 SDValue Chain = DAG.getEntryNode();
2548 Chain = LowerF128_LibCallArg(Chain, Args, Arg: LHS, DL, DAG);
2549 Chain = LowerF128_LibCallArg(Chain, Args, Arg: RHS, DL, DAG);
2550
2551 TargetLowering::CallLoweringInfo CLI(DAG);
2552 CLI.setDebugLoc(DL).setChain(Chain)
2553 .setCallee(CC: CallingConv::C, ResultType: RetTy, Target: Callee, ArgsList: std::move(Args));
2554
2555 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2556
2557 // result is in first, and chain is in second result.
2558 SDValue Result = CallInfo.first;
2559
2560 switch(SPCC) {
2561 default: {
2562 SDValue RHS = DAG.getConstant(Val: 0, DL, VT: Result.getValueType());
2563 SPCC = SPCC::ICC_NE;
2564 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2565 }
2566 case SPCC::FCC_UL : {
2567 SDValue Mask = DAG.getConstant(Val: 1, DL, VT: Result.getValueType());
2568 Result = DAG.getNode(Opcode: ISD::AND, DL, VT: Result.getValueType(), N1: Result, N2: Mask);
2569 SDValue RHS = DAG.getConstant(Val: 0, DL, VT: Result.getValueType());
2570 SPCC = SPCC::ICC_NE;
2571 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2572 }
2573 case SPCC::FCC_ULE: {
2574 SDValue RHS = DAG.getConstant(Val: 2, DL, VT: Result.getValueType());
2575 SPCC = SPCC::ICC_NE;
2576 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2577 }
2578 case SPCC::FCC_UG : {
2579 SDValue RHS = DAG.getConstant(Val: 1, DL, VT: Result.getValueType());
2580 SPCC = SPCC::ICC_G;
2581 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2582 }
2583 case SPCC::FCC_UGE: {
2584 SDValue RHS = DAG.getConstant(Val: 1, DL, VT: Result.getValueType());
2585 SPCC = SPCC::ICC_NE;
2586 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2587 }
2588
2589 case SPCC::FCC_U : {
2590 SDValue RHS = DAG.getConstant(Val: 3, DL, VT: Result.getValueType());
2591 SPCC = SPCC::ICC_E;
2592 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2593 }
2594 case SPCC::FCC_O : {
2595 SDValue RHS = DAG.getConstant(Val: 3, DL, VT: Result.getValueType());
2596 SPCC = SPCC::ICC_NE;
2597 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2598 }
2599 case SPCC::FCC_LG : {
2600 SDValue One = DAG.getConstant(Val: 1, DL, VT: Result.getValueType());
2601 SDValue Two = DAG.getConstant(Val: 2, DL, VT: Result.getValueType());
2602 Result = DAG.getNode(Opcode: ISD::ADD, DL, VT: Result.getValueType(), N1: Result, N2: One);
2603 Result = DAG.getNode(Opcode: ISD::AND, DL, VT: Result.getValueType(), N1: Result, N2: Two);
2604 SDValue RHS = DAG.getConstant(Val: 0, DL, VT: Result.getValueType());
2605 SPCC = SPCC::ICC_NE;
2606 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2607 }
2608 case SPCC::FCC_UE : {
2609 SDValue One = DAG.getConstant(Val: 1, DL, VT: Result.getValueType());
2610 SDValue Two = DAG.getConstant(Val: 2, DL, VT: Result.getValueType());
2611 Result = DAG.getNode(Opcode: ISD::ADD, DL, VT: Result.getValueType(), N1: Result, N2: One);
2612 Result = DAG.getNode(Opcode: ISD::AND, DL, VT: Result.getValueType(), N1: Result, N2: Two);
2613 SDValue RHS = DAG.getConstant(Val: 0, DL, VT: Result.getValueType());
2614 SPCC = SPCC::ICC_E;
2615 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: Result, N2: RHS);
2616 }
2617 }
2618}
2619
2620// Lower a constrained f128 compare to a call to _Q_cmp/_Qp_cmp (quiet) or
2621// _Q_cmpe/_Qp_cmpe (signaling), and decode the result into an integer
2622// compare. Unlike the predicate-specific helpers (_Q_flt, _Q_feq, ...), these
2623// two let us pick the exception behaviour independently of the predicate.
2624SDValue SparcTargetLowering::LowerF128StrictCompare(SDValue &Chain, SDValue LHS,
2625 SDValue RHS, unsigned &SPCC,
2626 bool IsSignaling,
2627 const SDLoc &DL,
2628 SelectionDAG &DAG) const {
2629 bool is64Bit = Subtarget->is64Bit();
2630 const char *LibCall;
2631 if (IsSignaling)
2632 LibCall = is64Bit ? "_Qp_cmpe" : "_Q_cmpe";
2633 else
2634 LibCall = is64Bit ? "_Qp_cmp" : "_Q_cmp";
2635
2636 auto PtrVT = getPointerTy(DL: DAG.getDataLayout());
2637 SDValue Callee = DAG.getExternalSymbol(Sym: LibCall, VT: PtrVT);
2638 Type *RetTy = Type::getInt32Ty(C&: *DAG.getContext());
2639 ArgListTy Args;
2640 Chain = LowerF128_LibCallArg(Chain, Args, Arg: LHS, DL, DAG);
2641 Chain = LowerF128_LibCallArg(Chain, Args, Arg: RHS, DL, DAG);
2642
2643 TargetLowering::CallLoweringInfo CLI(DAG);
2644 CLI.setDebugLoc(DL).setChain(Chain).setCallee(CC: CallingConv::C, ResultType: RetTy, Target: Callee,
2645 ArgsList: std::move(Args));
2646
2647 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2648 Chain = CallInfo.second;
2649 SDValue Result = CallInfo.first;
2650 EVT ResVT = Result.getValueType();
2651
2652 auto Compare = [&](SDValue V, uint64_t C, unsigned NewSPCC) {
2653 SPCC = NewSPCC;
2654 return DAG.getNode(Opcode: SPISD::CMPICC, DL, VT: MVT::Glue, N1: V,
2655 N2: DAG.getConstant(Val: C, DL, VT: ResVT));
2656 };
2657 auto And = [&](SDValue V, uint64_t C) {
2658 return DAG.getNode(Opcode: ISD::AND, DL, VT: ResVT, N1: V, N2: DAG.getConstant(Val: C, DL, VT: ResVT));
2659 };
2660 auto Add = [&](SDValue V, int64_t C) {
2661 return DAG.getNode(Opcode: ISD::ADD, DL, VT: ResVT, N1: V,
2662 N2: DAG.getSignedConstant(Val: C, DL, VT: ResVT));
2663 };
2664
2665 switch (SPCC) {
2666 default:
2667 llvm_unreachable("Unhandled conditional code!");
2668 case SPCC::FCC_E: // 0
2669 return Compare(Result, 0, SPCC::ICC_E);
2670 case SPCC::FCC_NE: // 1, 2, 3
2671 return Compare(Result, 0, SPCC::ICC_NE);
2672 case SPCC::FCC_L: // 1
2673 return Compare(Result, 1, SPCC::ICC_E);
2674 case SPCC::FCC_G: // 2
2675 return Compare(Result, 2, SPCC::ICC_E);
2676 case SPCC::FCC_LE: // 0, 1
2677 return Compare(Result, 2, SPCC::ICC_CS);
2678 case SPCC::FCC_GE: // 0, 2
2679 return Compare(And(Result, 1), 0, SPCC::ICC_E);
2680 case SPCC::FCC_UL: // 1, 3
2681 return Compare(And(Result, 1), 0, SPCC::ICC_NE);
2682 case SPCC::FCC_ULE: // 0, 1, 3
2683 return Compare(Result, 2, SPCC::ICC_NE);
2684 case SPCC::FCC_UG: // 2, 3
2685 return Compare(Result, 1, SPCC::ICC_GU);
2686 case SPCC::FCC_UGE: // 0, 2, 3
2687 return Compare(Result, 1, SPCC::ICC_NE);
2688 case SPCC::FCC_U: // 3
2689 return Compare(Result, 3, SPCC::ICC_E);
2690 case SPCC::FCC_O: // 0, 1, 2
2691 return Compare(Result, 3, SPCC::ICC_NE);
2692 case SPCC::FCC_LG: // 1, 2
2693 return Compare(Add(Result, -1), 2, SPCC::ICC_CS);
2694 case SPCC::FCC_UE: // 0, 3
2695 return Compare(And(Add(Result, 1), 2), 0, SPCC::ICC_E);
2696 }
2697}
2698
2699static SDValue
2700LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2701 const SparcTargetLowering &TLI) {
2702 EVT SrcVT = Op.getOperand(i: Op->isStrictFPOpcode() ? 1 : 0).getValueType();
2703
2704 if (SrcVT == MVT::f64)
2705 return TLI.LowerF128Op(Op, DAG, LibFunc: RTLIB::FPEXT_F64_F128, numArgs: 1);
2706
2707 if (SrcVT == MVT::f32)
2708 return TLI.LowerF128Op(Op, DAG, LibFunc: RTLIB::FPEXT_F32_F128, numArgs: 1);
2709
2710 llvm_unreachable("fpextend with non-float operand!");
2711 return SDValue();
2712}
2713
2714static SDValue
2715LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2716 const SparcTargetLowering &TLI) {
2717 // FP_ROUND on f64 and f32 are legal.
2718 if (Op.getOperand(i: Op->isStrictFPOpcode() ? 1 : 0).getValueType() != MVT::f128)
2719 return Op;
2720
2721 if (Op.getValueType() == MVT::f64)
2722 return TLI.LowerF128Op(Op, DAG, LibFunc: RTLIB::FPROUND_F128_F64, numArgs: 1);
2723 if (Op.getValueType() == MVT::f32)
2724 return TLI.LowerF128Op(Op, DAG, LibFunc: RTLIB::FPROUND_F128_F32, numArgs: 1);
2725
2726 llvm_unreachable("fpround to non-float!");
2727 return SDValue();
2728}
2729
2730static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2731 const SparcTargetLowering &TLI,
2732 bool hasHardQuad) {
2733 SDLoc dl(Op);
2734 EVT VT = Op.getValueType();
2735 assert(VT == MVT::i32 || VT == MVT::i64);
2736 bool IsStrict = Op->isStrictFPOpcode();
2737 SDValue Src = Op.getOperand(i: IsStrict ? 1 : 0);
2738
2739 // Expand f128 operations to fp128 abi calls.
2740 if (Src.getValueType() == MVT::f128 &&
2741 (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2742 RTLIB::Libcall LibFunc =
2743 VT == MVT::i32 ? RTLIB::FPTOSINT_F128_I32 : RTLIB::FPTOSINT_F128_I64;
2744 return TLI.LowerF128Op(Op, DAG, LibFunc, numArgs: 1);
2745 }
2746
2747 // Expand if the resulting type is illegal.
2748 if (!TLI.isTypeLegal(VT))
2749 return SDValue();
2750
2751 // Otherwise, Convert the fp value to integer in an FP register.
2752 MVT FloatVT = VT == MVT::i32 ? MVT::f32 : MVT::f64;
2753 if (IsStrict) {
2754 unsigned Opc = VT == MVT::i32 ? SPISD::STRICT_FTOI : SPISD::STRICT_FTOX;
2755 SDValue Conv =
2756 DAG.getNode(Opcode: Opc, DL: dl, ResultTys: {FloatVT, MVT::Other}, Ops: {Op.getOperand(i: 0), Src});
2757 SDValue Res = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT, Operand: Conv);
2758 return DAG.getMergeValues(Ops: {Res, Conv.getValue(R: 1)}, dl);
2759 }
2760
2761 unsigned Opc = VT == MVT::i32 ? SPISD::FTOI : SPISD::FTOX;
2762 Op = DAG.getNode(Opcode: Opc, DL: dl, VT: FloatVT, Operand: Src);
2763 return DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT, Operand: Op);
2764}
2765
2766static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2767 const SparcTargetLowering &TLI,
2768 bool hasHardQuad) {
2769 SDLoc dl(Op);
2770 bool IsStrict = Op->isStrictFPOpcode();
2771 SDValue Src = Op.getOperand(i: IsStrict ? 1 : 0);
2772 EVT OpVT = Src.getValueType();
2773 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2774
2775 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2776
2777 // Expand f128 operations to fp128 ABI calls.
2778 if (Op.getValueType() == MVT::f128
2779 && (!hasHardQuad || !TLI.isTypeLegal(VT: OpVT))) {
2780 RTLIB::Libcall LibFunc =
2781 OpVT == MVT::i32 ? RTLIB::SINTTOFP_I32_F128 : RTLIB::SINTTOFP_I64_F128;
2782 return TLI.LowerF128Op(Op, DAG, LibFunc, numArgs: 1);
2783 }
2784
2785 // Expand if the operand type is illegal.
2786 if (!TLI.isTypeLegal(VT: OpVT))
2787 return SDValue();
2788
2789 // Otherwise, Convert the int value to FP in an FP register.
2790 SDValue Tmp;
2791 auto *LD = dyn_cast<LoadSDNode>(Val&: Src);
2792 if (LD && Src.hasOneUse() && ISD::isNormalLoad(N: LD) && LD->isSimple()) {
2793 // Load the integer directly into an FP register instead of moving it
2794 // there from an integer register through the stack.
2795 LD->getMemOperand()->clearRanges();
2796 Tmp = DAG.getLoad(VT: floatVT, dl, Chain: LD->getChain(), Ptr: LD->getBasePtr(),
2797 MMO: LD->getMemOperand());
2798 DAG.ReplaceAllUsesOfValueWith(From: SDValue(LD, 1), To: Tmp.getValue(R: 1));
2799 } else {
2800 Tmp = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: floatVT, Operand: Src);
2801 }
2802 if (IsStrict) {
2803 unsigned Opc = OpVT == MVT::i32 ? SPISD::STRICT_ITOF : SPISD::STRICT_XTOF;
2804 return DAG.getNode(Opcode: Opc, DL: dl, ResultTys: {Op.getValueType(), MVT::Other},
2805 Ops: {Op.getOperand(i: 0), Tmp});
2806 }
2807 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2808 return DAG.getNode(Opcode: opcode, DL: dl, VT: Op.getValueType(), Operand: Tmp);
2809}
2810
2811static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2812 const SparcTargetLowering &TLI,
2813 bool hasHardQuad) {
2814 EVT VT = Op.getValueType();
2815 SDValue Src = Op.getOperand(i: Op->isStrictFPOpcode() ? 1 : 0);
2816
2817 // Expand if it does not involve f128 or the target has support for
2818 // quad floating point instructions and the resulting type is legal.
2819 if (Src.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(VT)))
2820 return SDValue();
2821
2822 assert(VT == MVT::i32 || VT == MVT::i64);
2823
2824 return TLI.LowerF128Op(
2825 Op, DAG,
2826 LibFunc: VT == MVT::i32 ? RTLIB::FPTOUINT_F128_I32 : RTLIB::FPTOUINT_F128_I64, numArgs: 1);
2827}
2828
2829static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2830 const SparcTargetLowering &TLI,
2831 bool hasHardQuad) {
2832 SDLoc dl(Op);
2833 EVT VT = Op.getValueType();
2834 bool IsStrict = Op->isStrictFPOpcode();
2835 SDValue Src = Op.getOperand(i: IsStrict ? 1 : 0);
2836 EVT OpVT = Src.getValueType();
2837 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2838
2839 // Expand f128 operations to fp128 ABI calls if there is no hardware quad
2840 // support or the operand type is illegal.
2841 if (VT == MVT::f128 && (!hasHardQuad || !TLI.isTypeLegal(VT: OpVT)))
2842 return TLI.LowerF128Op(Op, DAG,
2843 LibFunc: OpVT == MVT::i32 ? RTLIB::UINTTOFP_I32_F128
2844 : RTLIB::UINTTOFP_I64_F128,
2845 numArgs: 1);
2846
2847 // When i64 is legal, zero-extend an i32 operand and use the signed 64-bit
2848 // conversion (fxtos/fxtod/fxtoq). The generic i32 expansion builds the
2849 // double 0x43300000:x and subtracts 2^52, which yields -0.0 for an input of
2850 // 0 when rounding toward negative infinity.
2851 if (OpVT == MVT::i32 && TLI.isTypeLegal(VT: MVT::i64)) {
2852 SDValue Ext = DAG.getNode(Opcode: ISD::ZERO_EXTEND, DL: dl, VT: MVT::i64, Operand: Src);
2853 if (IsStrict)
2854 return DAG.getNode(Opcode: ISD::STRICT_SINT_TO_FP, DL: dl, ResultTys: {VT, MVT::Other},
2855 Ops: {Op.getOperand(i: 0), Ext});
2856 return DAG.getNode(Opcode: ISD::SINT_TO_FP, DL: dl, VT, Operand: Ext);
2857 }
2858
2859 // For i64 to f64, the generic non-strict expansion splits the operand into
2860 // 2^84 and 2^52 biased halves, which also yields -0.0 for an input of 0 when
2861 // rounding toward negative infinity. Use the same sequence as the strict
2862 // expansion instead: if the sign bit is set, halve the operand keeping the
2863 // low bit sticky, convert with fxtod and double the result. This is correct
2864 // in all rounding modes and avoids the constant pool load.
2865 if (!IsStrict && OpVT == MVT::i64 && VT == MVT::f64 &&
2866 TLI.isTypeLegal(VT: MVT::i64)) {
2867 if (Op->getFlags().hasNonNeg())
2868 return DAG.getNode(Opcode: ISD::SINT_TO_FP, DL: dl, VT, Operand: Src);
2869
2870 EVT SetCCVT = TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
2871 VT: MVT::i64);
2872 SDValue SignBitTest = DAG.getSetCC(
2873 DL: dl, VT: SetCCVT, LHS: Src, RHS: DAG.getConstant(Val: 0, DL: dl, VT: MVT::i64), Cond: ISD::SETLT);
2874 SDValue Shr = DAG.getNode(Opcode: ISD::SRL, DL: dl, VT: MVT::i64, N1: Src,
2875 N2: DAG.getShiftAmountConstant(Val: 1, VT: MVT::i64, DL: dl));
2876 SDValue And = DAG.getNode(Opcode: ISD::AND, DL: dl, VT: MVT::i64, N1: Src,
2877 N2: DAG.getConstant(Val: 1, DL: dl, VT: MVT::i64));
2878 SDValue Or = DAG.getNode(Opcode: ISD::OR, DL: dl, VT: MVT::i64, N1: And, N2: Shr);
2879 SDValue InCvt = DAG.getSelect(DL: dl, VT: MVT::i64, Cond: SignBitTest, LHS: Or, RHS: Src);
2880 SDValue Cvt = DAG.getNode(Opcode: ISD::SINT_TO_FP, DL: dl, VT: MVT::f64, Operand: InCvt);
2881 SDValue Dbl = DAG.getNode(Opcode: ISD::FADD, DL: dl, VT: MVT::f64, N1: Cvt, N2: Cvt);
2882 return DAG.getSelect(DL: dl, VT: MVT::f64, Cond: SignBitTest, LHS: Dbl, RHS: Cvt);
2883 }
2884
2885 // Otherwise, use the generic expansion.
2886 return SDValue();
2887}
2888
2889// Lower a constrained FP compare. The quiet form uses fcmp and the signaling
2890// form uses fcmpe (or the corresponding f128 libcall), and the result is
2891// materialized with a conditional select.
2892static SDValue LowerSTRICT_FSETCC(SDValue Op, SelectionDAG &DAG,
2893 const SparcTargetLowering &TLI,
2894 bool hasHardQuad, bool isV9) {
2895 SDLoc dl(Op);
2896 bool IsSignaling = Op.getOpcode() == ISD::STRICT_FSETCCS;
2897 SDValue Chain = Op.getOperand(i: 0);
2898 SDValue LHS = Op.getOperand(i: 1);
2899 SDValue RHS = Op.getOperand(i: 2);
2900 ISD::CondCode CC = cast<CondCodeSDNode>(Val: Op.getOperand(i: 3))->get();
2901 EVT VT = Op.getValueType();
2902 unsigned SPCC = FPCondCCodeToFCC(CC);
2903
2904 SDValue CompareFlag;
2905 unsigned SelectOpc;
2906 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2907 CompareFlag =
2908 TLI.LowerF128StrictCompare(Chain, LHS, RHS, SPCC, IsSignaling, DL: dl, DAG);
2909 SelectOpc = SPISD::SELECT_ICC;
2910 } else {
2911 unsigned CmpOpc;
2912 if (isV9)
2913 CmpOpc = IsSignaling ? SPISD::STRICT_CMPFCCS_V9 : SPISD::STRICT_CMPFCC_V9;
2914 else
2915 CmpOpc = IsSignaling ? SPISD::STRICT_CMPFCCS : SPISD::STRICT_CMPFCC;
2916 SDValue Cmp =
2917 DAG.getNode(Opcode: CmpOpc, DL: dl, ResultTys: {MVT::Other, MVT::Glue}, Ops: {Chain, LHS, RHS});
2918 Chain = Cmp.getValue(R: 0);
2919 CompareFlag = Cmp.getValue(R: 1);
2920 SelectOpc = SPISD::SELECT_FCC;
2921 }
2922
2923 SDValue Res = DAG.getNode(Opcode: SelectOpc, DL: dl, VT, N1: DAG.getConstant(Val: 1, DL: dl, VT),
2924 N2: DAG.getConstant(Val: 0, DL: dl, VT),
2925 N3: DAG.getConstant(Val: SPCC, DL: dl, VT: MVT::i32), N4: CompareFlag);
2926 return DAG.getMergeValues(Ops: {Res, Chain}, dl);
2927}
2928
2929// If this is a br_cc on the result of a constrained FP compare that was
2930// lowered into a STRICT_CMPFCC*/SELECT_FCC pair, set Chain and Glue so that
2931// the branch can test the FP condition codes directly instead of
2932// materializing the result and comparing it against zero.
2933//
2934// The compare is glued to the branch, so nothing may be scheduled between
2935// them, and the branch's chain must come straight from the compare. That is
2936// the case if the compare's output chain feeds only the branch. If it instead
2937// reaches the branch through a token factor used only by the branch, emit a
2938// new compare chained after the token factor's other operands and branch on
2939// that. Those operands were not ordered with respect to the compare, and the
2940// original compare and its select become dead.
2941static bool LookThroughStrictSetCC(SDValue &Chain, SDValue LHS, SDValue RHS,
2942 ISD::CondCode CC, unsigned &SPCC,
2943 SDValue &Glue, const SDLoc &DL,
2944 SelectionDAG &DAG) {
2945 if (CC != ISD::SETNE || !isNullConstant(V: RHS) ||
2946 LHS.getOpcode() != SPISD::SELECT_FCC || !LHS.hasOneUse() ||
2947 !isOneConstant(V: LHS.getOperand(i: 0)) || !isNullConstant(V: LHS.getOperand(i: 1)))
2948 return false;
2949
2950 SDNode *Cmp = LHS.getOperand(i: 3).getNode();
2951 switch (Cmp->getOpcode()) {
2952 case SPISD::STRICT_CMPFCC:
2953 case SPISD::STRICT_CMPFCCS:
2954 case SPISD::STRICT_CMPFCC_V9:
2955 case SPISD::STRICT_CMPFCCS_V9:
2956 break;
2957 default:
2958 return false;
2959 }
2960
2961 SDValue CmpChain(Cmp, 0);
2962 if (!CmpChain.hasOneUse())
2963 return false;
2964
2965 if (Chain == CmpChain) {
2966 Glue = SDValue(Cmp, 1);
2967 } else if (Chain.getOpcode() == ISD::TokenFactor && Chain.hasOneUse() &&
2968 is_contained(Range: Chain->op_values(), Element: CmpChain)) {
2969 SmallVector<SDValue, 8> Ops;
2970 for (SDValue Op : Chain->op_values())
2971 if (Op != CmpChain)
2972 Ops.push_back(Elt: Op);
2973 Ops.push_back(Elt: Cmp->getOperand(Num: 0));
2974 SDValue InChain = DAG.getNode(Opcode: ISD::TokenFactor, DL, VT: MVT::Other, Ops);
2975 SDValue NewCmp =
2976 DAG.getNode(Opcode: Cmp->getOpcode(), DL, VTList: Cmp->getVTList(),
2977 Ops: {InChain, Cmp->getOperand(Num: 1), Cmp->getOperand(Num: 2)});
2978 Chain = NewCmp.getValue(R: 0);
2979 Glue = NewCmp.getValue(R: 1);
2980 } else {
2981 return false;
2982 }
2983
2984 SPCC = LHS.getConstantOperandVal(i: 2);
2985 return true;
2986}
2987
2988static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2989 const SparcTargetLowering &TLI, bool hasHardQuad,
2990 bool isV9, bool is64Bit) {
2991 SDValue Chain = Op.getOperand(i: 0);
2992 ISD::CondCode CC = Op.getOperandCondCode(i: 1);
2993 SDValue LHS = Op.getOperand(i: 2);
2994 SDValue RHS = Op.getOperand(i: 3);
2995 SDValue Dest = Op.getOperand(i: 4);
2996 SDLoc dl(Op);
2997 unsigned Opc, SPCC = ~0U;
2998
2999 // A brcond on the result of a lowered setcc is expanded into
3000 // br_cc (and X, 1), 0, setne. When X is a SELECT_[IF]CC of 1 and 0 the and
3001 // is a no-op, so look through it.
3002 if (CC == ISD::SETNE && isNullConstant(V: RHS) && LHS.getOpcode() == ISD::AND &&
3003 isOneConstant(V: LHS.getOperand(i: 1)) && LHS.hasOneUse()) {
3004 SDValue Sel = LHS.getOperand(i: 0);
3005 if ((Sel.getOpcode() == SPISD::SELECT_ICC ||
3006 Sel.getOpcode() == SPISD::SELECT_XCC ||
3007 Sel.getOpcode() == SPISD::SELECT_FCC) &&
3008 isOneConstant(V: Sel.getOperand(i: 0)) && isNullConstant(V: Sel.getOperand(i: 1)))
3009 LHS = Sel;
3010 }
3011
3012 // If this is a br_cc of a "setcc", and if the setcc got lowered into
3013 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
3014 LookThroughSetCC(LHS, RHS, CC, SPCC);
3015 assert(LHS.getValueType() == RHS.getValueType());
3016
3017 // Likewise for a constrained FP compare, branch on the FP condition codes.
3018 SDValue StrictCmpFlag;
3019 if (LookThroughStrictSetCC(Chain, LHS, RHS, CC, SPCC, Glue&: StrictCmpFlag, DL: dl, DAG))
3020 return DAG.getNode(Opcode: isV9 ? SPISD::BRFCC_V9 : SPISD::BRFCC, DL: dl, VT: MVT::Other,
3021 N1: Chain, N2: Dest, N3: DAG.getConstant(Val: SPCC, DL: dl, VT: MVT::i32),
3022 N4: StrictCmpFlag);
3023
3024 // Get the condition flag.
3025 SDValue CompareFlag;
3026 if (LHS.getValueType().isInteger()) {
3027 // On V9 processors running in 64-bit mode, if CC compares two `i64`s
3028 // and the RHS is zero we might be able to use a specialized branch.
3029 if (is64Bit && isV9 && LHS.getValueType() == MVT::i64 &&
3030 isNullConstant(V: RHS) && !ISD::isUnsignedIntSetCC(Code: CC))
3031 return DAG.getNode(Opcode: SPISD::BR_REG, DL: dl, VT: MVT::Other, N1: Chain, N2: Dest,
3032 N3: DAG.getConstant(Val: intCondCCodeToRcond(CC), DL: dl, VT: MVT::i32),
3033 N4: LHS);
3034
3035 CompareFlag = DAG.getNode(Opcode: SPISD::CMPICC, DL: dl, VT: MVT::Glue, N1: LHS, N2: RHS);
3036 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
3037 if (isV9)
3038 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
3039 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BPICC : SPISD::BPXCC;
3040 else
3041 // Non-v9 targets don't have xcc.
3042 Opc = SPISD::BRICC;
3043 } else {
3044 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
3045 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
3046 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, DL: dl, DAG);
3047 Opc = isV9 ? SPISD::BPICC : SPISD::BRICC;
3048 } else {
3049 unsigned CmpOpc = isV9 ? SPISD::CMPFCC_V9 : SPISD::CMPFCC;
3050 CompareFlag = DAG.getNode(Opcode: CmpOpc, DL: dl, VT: MVT::Glue, N1: LHS, N2: RHS);
3051 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
3052 Opc = isV9 ? SPISD::BRFCC_V9 : SPISD::BRFCC;
3053 }
3054 }
3055 return DAG.getNode(Opcode: Opc, DL: dl, VT: MVT::Other, N1: Chain, N2: Dest,
3056 N3: DAG.getConstant(Val: SPCC, DL: dl, VT: MVT::i32), N4: CompareFlag);
3057}
3058
3059static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
3060 const SparcTargetLowering &TLI, bool hasHardQuad,
3061 bool isV9, bool is64Bit) {
3062 SDValue LHS = Op.getOperand(i: 0);
3063 SDValue RHS = Op.getOperand(i: 1);
3064 ISD::CondCode CC = Op.getOperandCondCode(i: 4);
3065 SDValue TrueVal = Op.getOperand(i: 2);
3066 SDValue FalseVal = Op.getOperand(i: 3);
3067 SDLoc dl(Op);
3068 unsigned Opc, SPCC = ~0U;
3069
3070 // If this is a select_cc of a "setcc", and if the setcc got lowered into
3071 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
3072 LookThroughSetCC(LHS, RHS, CC, SPCC);
3073 assert(LHS.getValueType() == RHS.getValueType());
3074
3075 SDValue CompareFlag;
3076 if (LHS.getValueType().isInteger()) {
3077 // On V9 processors running in 64-bit mode, if CC compares two `i64`s
3078 // and the RHS is zero we might be able to use a specialized select.
3079 // All SELECT_CC between any two scalar integer types are eligible for
3080 // lowering to specialized instructions. Additionally, f32 and f64 types
3081 // are also eligible, but for f128 we can only use the specialized
3082 // instruction when we have hardquad.
3083 EVT ValType = TrueVal.getValueType();
3084 bool IsEligibleType = ValType.isScalarInteger() || ValType == MVT::f32 ||
3085 ValType == MVT::f64 ||
3086 (ValType == MVT::f128 && hasHardQuad);
3087 if (is64Bit && isV9 && LHS.getValueType() == MVT::i64 &&
3088 isNullConstant(V: RHS) && !ISD::isUnsignedIntSetCC(Code: CC) && IsEligibleType)
3089 return DAG.getNode(
3090 Opcode: SPISD::SELECT_REG, DL: dl, VT: TrueVal.getValueType(), N1: TrueVal, N2: FalseVal,
3091 N3: DAG.getConstant(Val: intCondCCodeToRcond(CC), DL: dl, VT: MVT::i32), N4: LHS);
3092
3093 CompareFlag = DAG.getNode(Opcode: SPISD::CMPICC, DL: dl, VT: MVT::Glue, N1: LHS, N2: RHS);
3094 Opc = LHS.getValueType() == MVT::i32 ?
3095 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
3096 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
3097 } else {
3098 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
3099 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
3100 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, DL: dl, DAG);
3101 Opc = SPISD::SELECT_ICC;
3102 } else {
3103 unsigned CmpOpc = isV9 ? SPISD::CMPFCC_V9 : SPISD::CMPFCC;
3104 CompareFlag = DAG.getNode(Opcode: CmpOpc, DL: dl, VT: MVT::Glue, N1: LHS, N2: RHS);
3105 Opc = SPISD::SELECT_FCC;
3106 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
3107 }
3108 }
3109 return DAG.getNode(Opcode: Opc, DL: dl, VT: TrueVal.getValueType(), N1: TrueVal, N2: FalseVal,
3110 N3: DAG.getConstant(Val: SPCC, DL: dl, VT: MVT::i32), N4: CompareFlag);
3111}
3112
3113static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
3114 const SparcTargetLowering &TLI) {
3115 MachineFunction &MF = DAG.getMachineFunction();
3116 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
3117 auto PtrVT = TLI.getPointerTy(DL: DAG.getDataLayout());
3118
3119 // Need frame address to find the address of VarArgsFrameIndex.
3120 MF.getFrameInfo().setFrameAddressIsTaken(true);
3121
3122 // vastart just stores the address of the VarArgsFrameIndex slot into the
3123 // memory location argument.
3124 SDLoc DL(Op);
3125 SDValue Offset =
3126 DAG.getNode(Opcode: ISD::ADD, DL, VT: PtrVT, N1: DAG.getRegister(Reg: SP::I6, VT: PtrVT),
3127 N2: DAG.getIntPtrConstant(Val: FuncInfo->getVarArgsFrameOffset(), DL));
3128 const Value *SV = cast<SrcValueSDNode>(Val: Op.getOperand(i: 2))->getValue();
3129 return DAG.getStore(Chain: Op.getOperand(i: 0), dl: DL, Val: Offset, Ptr: Op.getOperand(i: 1),
3130 PtrInfo: MachinePointerInfo(SV));
3131}
3132
3133static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
3134 SDNode *Node = Op.getNode();
3135 EVT VT = Node->getValueType(ResNo: 0);
3136 SDValue InChain = Node->getOperand(Num: 0);
3137 SDValue VAListPtr = Node->getOperand(Num: 1);
3138 EVT PtrVT = VAListPtr.getValueType();
3139 const Value *SV = cast<SrcValueSDNode>(Val: Node->getOperand(Num: 2))->getValue();
3140 SDLoc DL(Node);
3141 SDValue VAList =
3142 DAG.getLoad(VT: PtrVT, dl: DL, Chain: InChain, Ptr: VAListPtr, PtrInfo: MachinePointerInfo(SV));
3143 // Increment the pointer, VAList, to the next vaarg.
3144 SDValue NextPtr = DAG.getNode(Opcode: ISD::ADD, DL, VT: PtrVT, N1: VAList,
3145 N2: DAG.getIntPtrConstant(Val: VT.getSizeInBits()/8,
3146 DL));
3147 // Store the incremented VAList to the legalized pointer.
3148 InChain = DAG.getStore(Chain: VAList.getValue(R: 1), dl: DL, Val: NextPtr, Ptr: VAListPtr,
3149 PtrInfo: MachinePointerInfo(SV));
3150 // Load the actual argument out of the pointer VAList.
3151 // We can't count on greater alignment than the word size.
3152 return DAG.getLoad(
3153 VT, dl: DL, Chain: InChain, Ptr: VAList, PtrInfo: MachinePointerInfo(),
3154 Alignment: Align(std::min(a: PtrVT.getFixedSizeInBits(), b: VT.getFixedSizeInBits()) / 8));
3155}
3156
3157static SDValue LowerSTACKADDRESS(SDValue Op, SelectionDAG &DAG,
3158 const SparcSubtarget &Subtarget) {
3159 SDValue Chain = Op.getOperand(i: 0);
3160 EVT VT = Op->getValueType(ResNo: 0);
3161 SDLoc DL(Op);
3162
3163 MCRegister SPReg = SP::O6;
3164 SDValue SP = DAG.getCopyFromReg(Chain, dl: DL, Reg: SPReg, VT);
3165
3166 // Unbias the stack pointer register.
3167 unsigned OffsetToStackStart = Subtarget.getStackPointerBias();
3168 // Move past the register save area: 8 in registers + 8 local registers.
3169 OffsetToStackStart += 16 * (Subtarget.is64Bit() ? 8 : 4);
3170 // Move past the struct return address slot (4 bytes) on SPARC 32-bit.
3171 if (!Subtarget.is64Bit())
3172 OffsetToStackStart += 4;
3173
3174 SDValue StackAddr = DAG.getNode(Opcode: ISD::ADD, DL, VT, N1: SP,
3175 N2: DAG.getConstant(Val: OffsetToStackStart, DL, VT));
3176 return DAG.getMergeValues(Ops: {StackAddr, Chain}, dl: DL);
3177}
3178
3179static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
3180 const SparcSubtarget *Subtarget) {
3181 SDValue Chain = Op.getOperand(i: 0);
3182 SDValue Size = Op.getOperand(i: 1);
3183 SDValue Alignment = Op.getOperand(i: 2);
3184 MaybeAlign MaybeAlignment =
3185 cast<ConstantSDNode>(Val&: Alignment)->getMaybeAlignValue();
3186 EVT VT = Size->getValueType(ResNo: 0);
3187 SDLoc dl(Op);
3188
3189 unsigned SPReg = SP::O6;
3190 SDValue SP = DAG.getCopyFromReg(Chain, dl, Reg: SPReg, VT);
3191
3192 // The resultant pointer needs to be above the register spill area
3193 // at the bottom of the stack.
3194 unsigned regSpillArea;
3195 if (Subtarget->is64Bit()) {
3196 regSpillArea = 128;
3197 } else {
3198 // On Sparc32, the size of the spill area is 92. Unfortunately,
3199 // that's only 4-byte aligned, not 8-byte aligned (the stack
3200 // pointer is 8-byte aligned). So, if the user asked for an 8-byte
3201 // aligned dynamic allocation, we actually need to add 96 to the
3202 // bottom of the stack, instead of 92, to ensure 8-byte alignment.
3203
3204 // That also means adding 4 to the size of the allocation --
3205 // before applying the 8-byte rounding. Unfortunately, we the
3206 // value we get here has already had rounding applied. So, we need
3207 // to add 8, instead, wasting a bit more memory.
3208
3209 // Further, this only actually needs to be done if the required
3210 // alignment is > 4, but, we've lost that info by this point, too,
3211 // so we always apply it.
3212
3213 // (An alternative approach would be to always reserve 96 bytes
3214 // instead of the required 92, but then we'd waste 4 extra bytes
3215 // in every frame, not just those with dynamic stack allocations)
3216
3217 // TODO: modify code in SelectionDAGBuilder to make this less sad.
3218
3219 Size = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT, N1: Size,
3220 N2: DAG.getConstant(Val: 8, DL: dl, VT));
3221 regSpillArea = 96;
3222 }
3223
3224 int64_t Bias = Subtarget->getStackPointerBias();
3225
3226 // Debias and increment SP past the reserved spill area.
3227 // We need the SP to point to the first usable region before calculating
3228 // anything to prevent any of the pointers from becoming out of alignment when
3229 // we rebias the SP later on.
3230 SDValue StartOfUsableStack = DAG.getNode(
3231 Opcode: ISD::ADD, DL: dl, VT, N1: SP, N2: DAG.getConstant(Val: regSpillArea + Bias, DL: dl, VT));
3232 SDValue AllocatedPtr =
3233 DAG.getNode(Opcode: ISD::SUB, DL: dl, VT, N1: StartOfUsableStack, N2: Size);
3234
3235 bool IsOveraligned = MaybeAlignment.has_value();
3236 SDValue AlignedPtr =
3237 IsOveraligned
3238 ? DAG.getNode(Opcode: ISD::AND, DL: dl, VT, N1: AllocatedPtr,
3239 N2: DAG.getSignedConstant(Val: -MaybeAlignment->value(), DL: dl, VT))
3240 : AllocatedPtr;
3241
3242 // Now that we are done, restore the bias and reserved spill area.
3243 SDValue NewSP = DAG.getNode(Opcode: ISD::SUB, DL: dl, VT, N1: AlignedPtr,
3244 N2: DAG.getConstant(Val: regSpillArea + Bias, DL: dl, VT));
3245 Chain = DAG.getCopyToReg(Chain: SP.getValue(R: 1), dl, Reg: SPReg, N: NewSP);
3246 SDValue Ops[2] = {AlignedPtr, Chain};
3247 return DAG.getMergeValues(Ops, dl);
3248}
3249
3250
3251static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
3252 SDLoc dl(Op);
3253 SDValue Chain = DAG.getNode(Opcode: SPISD::FLUSHW,
3254 DL: dl, VT: MVT::Other, Operand: DAG.getEntryNode());
3255 return Chain;
3256}
3257
3258static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
3259 const SparcSubtarget *Subtarget,
3260 bool AlwaysFlush = false) {
3261 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
3262 MFI.setFrameAddressIsTaken(true);
3263
3264 EVT VT = Op.getValueType();
3265 SDLoc dl(Op);
3266 unsigned FrameReg = SP::I6;
3267 unsigned stackBias = Subtarget->getStackPointerBias();
3268
3269 SDValue FrameAddr;
3270 SDValue Chain;
3271
3272 // flush first to make sure the windowed registers' values are in stack
3273 Chain = (depth || AlwaysFlush) ? getFLUSHW(Op, DAG) : DAG.getEntryNode();
3274
3275 FrameAddr = DAG.getCopyFromReg(Chain, dl, Reg: FrameReg, VT);
3276
3277 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
3278
3279 while (depth--) {
3280 SDValue Ptr = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT, N1: FrameAddr,
3281 N2: DAG.getIntPtrConstant(Val: Offset, DL: dl));
3282 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, PtrInfo: MachinePointerInfo());
3283 }
3284 if (Subtarget->is64Bit())
3285 FrameAddr = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT, N1: FrameAddr,
3286 N2: DAG.getIntPtrConstant(Val: stackBias, DL: dl));
3287 return FrameAddr;
3288}
3289
3290
3291static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
3292 const SparcSubtarget *Subtarget) {
3293
3294 uint64_t depth = Op.getConstantOperandVal(i: 0);
3295
3296 return getFRAMEADDR(depth, Op, DAG, Subtarget);
3297
3298}
3299
3300static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
3301 const SparcTargetLowering &TLI,
3302 const SparcSubtarget *Subtarget) {
3303 MachineFunction &MF = DAG.getMachineFunction();
3304 MachineFrameInfo &MFI = MF.getFrameInfo();
3305 MFI.setReturnAddressIsTaken(true);
3306
3307 EVT VT = Op.getValueType();
3308 SDLoc dl(Op);
3309 uint64_t depth = Op.getConstantOperandVal(i: 0);
3310
3311 SDValue RetAddr;
3312 if (depth == 0) {
3313 auto PtrVT = TLI.getPointerTy(DL: DAG.getDataLayout());
3314 Register RetReg = MF.addLiveIn(PReg: SP::I7, RC: TLI.getRegClassFor(VT: PtrVT));
3315 RetAddr = DAG.getCopyFromReg(Chain: DAG.getEntryNode(), dl, Reg: RetReg, VT);
3316 return RetAddr;
3317 }
3318
3319 // Need frame address to find return address of the caller.
3320 SDValue FrameAddr = getFRAMEADDR(depth: depth - 1, Op, DAG, Subtarget, AlwaysFlush: true);
3321
3322 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
3323 SDValue Ptr = DAG.getNode(Opcode: ISD::ADD,
3324 DL: dl, VT,
3325 N1: FrameAddr,
3326 N2: DAG.getIntPtrConstant(Val: Offset, DL: dl));
3327 RetAddr = DAG.getLoad(VT, dl, Chain: DAG.getEntryNode(), Ptr, PtrInfo: MachinePointerInfo());
3328
3329 return RetAddr;
3330}
3331
3332static SDValue LowerF64Op(SDValue SrcReg64, const SDLoc &dl, SelectionDAG &DAG,
3333 unsigned opcode) {
3334 assert(SrcReg64.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
3335 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
3336
3337 // Lower fneg/fabs on f64 to fneg/fabs on f32.
3338 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
3339 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
3340
3341 // Note: in little-endian, the floating-point value is stored in the
3342 // registers are in the opposite order, so the subreg with the sign
3343 // bit is the highest-numbered (odd), rather than the
3344 // lowest-numbered (even).
3345
3346 SDValue Hi32 = DAG.getTargetExtractSubreg(SRIdx: SP::sub_even, DL: dl, VT: MVT::f32,
3347 Operand: SrcReg64);
3348 SDValue Lo32 = DAG.getTargetExtractSubreg(SRIdx: SP::sub_odd, DL: dl, VT: MVT::f32,
3349 Operand: SrcReg64);
3350
3351 if (DAG.getDataLayout().isLittleEndian())
3352 Lo32 = DAG.getNode(Opcode: opcode, DL: dl, VT: MVT::f32, Operand: Lo32);
3353 else
3354 Hi32 = DAG.getNode(Opcode: opcode, DL: dl, VT: MVT::f32, Operand: Hi32);
3355
3356 SDValue DstReg64 = SDValue(DAG.getMachineNode(Opcode: TargetOpcode::IMPLICIT_DEF,
3357 dl, VT: MVT::f64), 0);
3358 DstReg64 = DAG.getTargetInsertSubreg(SRIdx: SP::sub_even, DL: dl, VT: MVT::f64,
3359 Operand: DstReg64, Subreg: Hi32);
3360 DstReg64 = DAG.getTargetInsertSubreg(SRIdx: SP::sub_odd, DL: dl, VT: MVT::f64,
3361 Operand: DstReg64, Subreg: Lo32);
3362 return DstReg64;
3363}
3364
3365// Lower a f128 load into two f64 loads.
3366static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
3367{
3368 SDLoc dl(Op);
3369 LoadSDNode *LdNode = cast<LoadSDNode>(Val: Op.getNode());
3370 assert(LdNode->getOffset().isUndef() && "Unexpected node type");
3371
3372 Align Alignment = commonAlignment(A: LdNode->getBaseAlign(), Offset: 8);
3373
3374 SDValue Hi64 =
3375 DAG.getLoad(VT: MVT::f64, dl, Chain: LdNode->getChain(), Ptr: LdNode->getBasePtr(),
3376 PtrInfo: LdNode->getPointerInfo(), Alignment);
3377 EVT addrVT = LdNode->getBasePtr().getValueType();
3378 SDValue LoPtr = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT: addrVT,
3379 N1: LdNode->getBasePtr(),
3380 N2: DAG.getConstant(Val: 8, DL: dl, VT: addrVT));
3381 SDValue Lo64 = DAG.getLoad(VT: MVT::f64, dl, Chain: LdNode->getChain(), Ptr: LoPtr,
3382 PtrInfo: LdNode->getPointerInfo().getWithOffset(O: 8),
3383 Alignment);
3384
3385 SDValue SubRegEven = DAG.getTargetConstant(Val: SP::sub_even64, DL: dl, VT: MVT::i32);
3386 SDValue SubRegOdd = DAG.getTargetConstant(Val: SP::sub_odd64, DL: dl, VT: MVT::i32);
3387
3388 SDNode *InFP128 = DAG.getMachineNode(Opcode: TargetOpcode::IMPLICIT_DEF,
3389 dl, VT: MVT::f128);
3390 InFP128 = DAG.getMachineNode(Opcode: TargetOpcode::INSERT_SUBREG, dl,
3391 VT: MVT::f128,
3392 Op1: SDValue(InFP128, 0),
3393 Op2: Hi64,
3394 Op3: SubRegEven);
3395 InFP128 = DAG.getMachineNode(Opcode: TargetOpcode::INSERT_SUBREG, dl,
3396 VT: MVT::f128,
3397 Op1: SDValue(InFP128, 0),
3398 Op2: Lo64,
3399 Op3: SubRegOdd);
3400 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
3401 SDValue(Lo64.getNode(), 1) };
3402 SDValue OutChain = DAG.getNode(Opcode: ISD::TokenFactor, DL: dl, VT: MVT::Other, Ops: OutChains);
3403 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
3404 return DAG.getMergeValues(Ops, dl);
3405}
3406
3407SDValue SparcTargetLowering::LowerBSWAP(SDValue Op, SelectionDAG &DAG) const {
3408 // We don't have an in-register bswap, so expand bswap(x) into
3409 // load(store-swapped(x)). The reason the swap is done during the store is
3410 // that on some implementations (mainly older ones) ASI-tagged memory
3411 // operations are not pipelined, and generally stores finish faster than
3412 // loads.
3413
3414 MachineFunction &MF = DAG.getMachineFunction();
3415 MachineFrameInfo &MFI = MF.getFrameInfo();
3416 MVT PtrVT = getPointerTy(DL: DAG.getDataLayout());
3417 SDValue Chain = DAG.getEntryNode();
3418 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
3419 SDLoc DL(Op);
3420
3421 SDValue BSwapOp = Op.getOperand(i: 0);
3422 EVT VT = BSwapOp.getValueType();
3423 Type *Ty = VT.getTypeForEVT(Context&: *DAG.getContext());
3424 Align Al = DAG.getDataLayout().getPrefTypeAlign(Ty);
3425
3426 // Create a stack object to serve as temporary storage.
3427 int TmpFI = MFI.CreateStackObject(Size: VT.getStoreSize(), Alignment: Al, isSpillSlot: false);
3428 SDValue TmpPtr = DAG.getFrameIndex(FI: TmpFI, VT: PtrVT);
3429
3430 // Store-swap the value, then load it back.
3431 SDValue Ops[] = {Chain, BSwapOp, TmpPtr, DAG.getValueType(VT)};
3432 SDValue ST = DAG.getMemIntrinsicNode(
3433 Opcode: IsLittleEndian ? SPISD::STORE_BIG : SPISD::STORE_LITTLE, dl: DL,
3434 VTList: DAG.getVTList(VT: MVT::Other), Ops, MemVT: VT,
3435 PtrInfo: MachinePointerInfo::getFixedStack(MF, FI: TmpFI), Alignment: std::nullopt,
3436 Flags: MachineMemOperand::MOStore);
3437 return DAG.getLoad(VT, dl: DL, Chain: ST, Ptr: TmpPtr,
3438 PtrInfo: MachinePointerInfo::getFixedStack(MF, FI: TmpFI));
3439}
3440
3441static SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG)
3442{
3443 LoadSDNode *LdNode = cast<LoadSDNode>(Val: Op.getNode());
3444
3445 EVT MemVT = LdNode->getMemoryVT();
3446 if (MemVT == MVT::f128)
3447 return LowerF128Load(Op, DAG);
3448
3449 return Op;
3450}
3451
3452// Lower a f128 store into two f64 stores.
3453static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
3454 SDLoc dl(Op);
3455 StoreSDNode *StNode = cast<StoreSDNode>(Val: Op.getNode());
3456 assert(StNode->getOffset().isUndef() && "Unexpected node type");
3457
3458 SDValue SubRegEven = DAG.getTargetConstant(Val: SP::sub_even64, DL: dl, VT: MVT::i32);
3459 SDValue SubRegOdd = DAG.getTargetConstant(Val: SP::sub_odd64, DL: dl, VT: MVT::i32);
3460
3461 SDNode *Hi64 = DAG.getMachineNode(Opcode: TargetOpcode::EXTRACT_SUBREG,
3462 dl,
3463 VT: MVT::f64,
3464 Op1: StNode->getValue(),
3465 Op2: SubRegEven);
3466 SDNode *Lo64 = DAG.getMachineNode(Opcode: TargetOpcode::EXTRACT_SUBREG,
3467 dl,
3468 VT: MVT::f64,
3469 Op1: StNode->getValue(),
3470 Op2: SubRegOdd);
3471
3472 Align Alignment = commonAlignment(A: StNode->getBaseAlign(), Offset: 8);
3473
3474 SDValue OutChains[2];
3475 OutChains[0] =
3476 DAG.getStore(Chain: StNode->getChain(), dl, Val: SDValue(Hi64, 0),
3477 Ptr: StNode->getBasePtr(), PtrInfo: StNode->getPointerInfo(),
3478 Alignment);
3479 EVT addrVT = StNode->getBasePtr().getValueType();
3480 SDValue LoPtr = DAG.getNode(Opcode: ISD::ADD, DL: dl, VT: addrVT,
3481 N1: StNode->getBasePtr(),
3482 N2: DAG.getConstant(Val: 8, DL: dl, VT: addrVT));
3483 OutChains[1] = DAG.getStore(Chain: StNode->getChain(), dl, Val: SDValue(Lo64, 0), Ptr: LoPtr,
3484 PtrInfo: StNode->getPointerInfo().getWithOffset(O: 8),
3485 Alignment);
3486 return DAG.getNode(Opcode: ISD::TokenFactor, DL: dl, VT: MVT::Other, Ops: OutChains);
3487}
3488
3489static SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG)
3490{
3491 SDLoc dl(Op);
3492 StoreSDNode *St = cast<StoreSDNode>(Val: Op.getNode());
3493
3494 EVT MemVT = St->getMemoryVT();
3495 if (MemVT == MVT::f128)
3496 return LowerF128Store(Op, DAG);
3497
3498 if (MemVT == MVT::i64 && St->getAlign() >= Align(8)) {
3499 // Custom handling for aligned i64 stores: turn it into a bitcast and a
3500 // v2i32 store.
3501 SDValue Val = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: MVT::v2i32, Operand: St->getValue());
3502 SDValue Chain = DAG.getStore(
3503 Chain: St->getChain(), dl, Val, Ptr: St->getBasePtr(), PtrInfo: St->getPointerInfo(),
3504 Alignment: St->getBaseAlign(), MMOFlags: St->getMemOperand()->getFlags(), Metadata: St->getAAInfo());
3505 return Chain;
3506 }
3507
3508 return SDValue();
3509}
3510
3511static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
3512 assert((Op.getOpcode() == ISD::FNEG || Op.getOpcode() == ISD::FABS)
3513 && "invalid opcode");
3514
3515 SDLoc dl(Op);
3516
3517 if (Op.getValueType() == MVT::f64)
3518 return LowerF64Op(SrcReg64: Op.getOperand(i: 0), dl, DAG, opcode: Op.getOpcode());
3519 if (Op.getValueType() != MVT::f128)
3520 return Op;
3521
3522 // Lower fabs/fneg on f128 to fabs/fneg on f64
3523 // fabs/fneg f128 => fabs/fneg f64:sub_even64, fmov f64:sub_odd64
3524 // (As with LowerF64Op, on little-endian, we need to negate the odd
3525 // subreg)
3526
3527 SDValue SrcReg128 = Op.getOperand(i: 0);
3528 SDValue Hi64 = DAG.getTargetExtractSubreg(SRIdx: SP::sub_even64, DL: dl, VT: MVT::f64,
3529 Operand: SrcReg128);
3530 SDValue Lo64 = DAG.getTargetExtractSubreg(SRIdx: SP::sub_odd64, DL: dl, VT: MVT::f64,
3531 Operand: SrcReg128);
3532
3533 if (DAG.getDataLayout().isLittleEndian()) {
3534 if (isV9)
3535 Lo64 = DAG.getNode(Opcode: Op.getOpcode(), DL: dl, VT: MVT::f64, Operand: Lo64);
3536 else
3537 Lo64 = LowerF64Op(SrcReg64: Lo64, dl, DAG, opcode: Op.getOpcode());
3538 } else {
3539 if (isV9)
3540 Hi64 = DAG.getNode(Opcode: Op.getOpcode(), DL: dl, VT: MVT::f64, Operand: Hi64);
3541 else
3542 Hi64 = LowerF64Op(SrcReg64: Hi64, dl, DAG, opcode: Op.getOpcode());
3543 }
3544
3545 SDValue DstReg128 = SDValue(DAG.getMachineNode(Opcode: TargetOpcode::IMPLICIT_DEF,
3546 dl, VT: MVT::f128), 0);
3547 DstReg128 = DAG.getTargetInsertSubreg(SRIdx: SP::sub_even64, DL: dl, VT: MVT::f128,
3548 Operand: DstReg128, Subreg: Hi64);
3549 DstReg128 = DAG.getTargetInsertSubreg(SRIdx: SP::sub_odd64, DL: dl, VT: MVT::f128,
3550 Operand: DstReg128, Subreg: Lo64);
3551 return DstReg128;
3552}
3553
3554static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
3555 if (isStrongerThanMonotonic(AO: cast<AtomicSDNode>(Val&: Op)->getSuccessOrdering())) {
3556 // Expand with a fence.
3557 return SDValue();
3558 }
3559
3560 // Monotonic load/stores are legal.
3561 return Op;
3562}
3563
3564static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
3565 SDLoc DL(Op);
3566 SDValue Vec = Op.getOperand(i: 0);
3567 SDValue Idx = Op.getOperand(i: 1);
3568 assert(Vec.getSimpleValueType() == MVT::v2i32 && "Unexpected vector type");
3569
3570 // Constant indices are handled by the TableGen patterns.
3571 if (isa<ConstantSDNode>(Val: Idx))
3572 return Op;
3573
3574 EVT VT = Op.getValueType();
3575 EVT IdxVT = Idx.getValueType();
3576 SDValue Elt0 = DAG.getNode(Opcode: ISD::EXTRACT_VECTOR_ELT, DL, VT, N1: Vec,
3577 N2: DAG.getConstant(Val: 0, DL, VT: IdxVT));
3578 SDValue Elt1 = DAG.getNode(Opcode: ISD::EXTRACT_VECTOR_ELT, DL, VT, N1: Vec,
3579 N2: DAG.getConstant(Val: 1, DL, VT: IdxVT));
3580 return DAG.getSelectCC(DL, LHS: Idx, RHS: DAG.getConstant(Val: 0, DL, VT: IdxVT), True: Elt0, False: Elt1,
3581 Cond: ISD::SETEQ);
3582}
3583
3584SDValue SparcTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
3585 SelectionDAG &DAG) const {
3586 unsigned IntNo = Op.getConstantOperandVal(i: 0);
3587 switch (IntNo) {
3588 default: return SDValue(); // Don't custom lower most intrinsics.
3589 case Intrinsic::thread_pointer: {
3590 EVT PtrVT = getPointerTy(DL: DAG.getDataLayout());
3591 return DAG.getRegister(Reg: SP::G7, VT: PtrVT);
3592 }
3593 }
3594}
3595
3596SDValue SparcTargetLowering::
3597LowerOperation(SDValue Op, SelectionDAG &DAG) const {
3598
3599 bool hasHardQuad = Subtarget->hasHardQuad();
3600 bool isV9 = Subtarget->isV9();
3601 bool is64Bit = Subtarget->is64Bit();
3602
3603 switch (Op.getOpcode()) {
3604 default: llvm_unreachable("Should not custom lower this!");
3605
3606 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, TLI: *this,
3607 Subtarget);
3608 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
3609 Subtarget);
3610 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
3611 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3612 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
3613 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3614 case ISD::FP_TO_SINT:
3615 case ISD::STRICT_FP_TO_SINT:
3616 return LowerFP_TO_SINT(Op, DAG, TLI: *this, hasHardQuad);
3617 case ISD::SINT_TO_FP:
3618 case ISD::STRICT_SINT_TO_FP:
3619 return LowerSINT_TO_FP(Op, DAG, TLI: *this, hasHardQuad);
3620 case ISD::FP_TO_UINT:
3621 case ISD::STRICT_FP_TO_UINT:
3622 return LowerFP_TO_UINT(Op, DAG, TLI: *this, hasHardQuad);
3623 case ISD::UINT_TO_FP:
3624 case ISD::STRICT_UINT_TO_FP:
3625 return LowerUINT_TO_FP(Op, DAG, TLI: *this, hasHardQuad);
3626 case ISD::STRICT_FSETCC:
3627 case ISD::STRICT_FSETCCS:
3628 return LowerSTRICT_FSETCC(Op, DAG, TLI: *this, hasHardQuad, isV9);
3629 case ISD::BR_CC:
3630 return LowerBR_CC(Op, DAG, TLI: *this, hasHardQuad, isV9, is64Bit);
3631 case ISD::SELECT_CC:
3632 return LowerSELECT_CC(Op, DAG, TLI: *this, hasHardQuad, isV9, is64Bit);
3633 case ISD::VASTART: return LowerVASTART(Op, DAG, TLI: *this);
3634 case ISD::VAARG: return LowerVAARG(Op, DAG);
3635 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
3636 Subtarget);
3637 case ISD::STACKADDRESS:
3638 return LowerSTACKADDRESS(Op, DAG, Subtarget: *Subtarget);
3639
3640 case ISD::BSWAP:
3641 return LowerBSWAP(Op, DAG);
3642
3643 case ISD::LOAD: return LowerLOAD(Op, DAG);
3644 case ISD::STORE: return LowerSTORE(Op, DAG);
3645 case ISD::FADD:
3646 case ISD::STRICT_FADD:
3647 return LowerF128Op(Op, DAG, LibFunc: RTLIB::ADD_F128, numArgs: 2);
3648 case ISD::FSUB:
3649 case ISD::STRICT_FSUB:
3650 return LowerF128Op(Op, DAG, LibFunc: RTLIB::SUB_F128, numArgs: 2);
3651 case ISD::FMUL:
3652 case ISD::STRICT_FMUL:
3653 return LowerF128Op(Op, DAG, LibFunc: RTLIB::MUL_F128, numArgs: 2);
3654 case ISD::FDIV:
3655 case ISD::STRICT_FDIV:
3656 return LowerF128Op(Op, DAG, LibFunc: RTLIB::DIV_F128, numArgs: 2);
3657 case ISD::FSQRT:
3658 case ISD::STRICT_FSQRT:
3659 return LowerF128Op(Op, DAG, LibFunc: RTLIB::SQRT_F128, numArgs: 1);
3660 case ISD::FABS:
3661 case ISD::FNEG: return LowerFNEGorFABS(Op, DAG, isV9);
3662 case ISD::FP_EXTEND:
3663 case ISD::STRICT_FP_EXTEND:
3664 return LowerF128_FPEXTEND(Op, DAG, TLI: *this);
3665 case ISD::FP_ROUND:
3666 case ISD::STRICT_FP_ROUND:
3667 return LowerF128_FPROUND(Op, DAG, TLI: *this);
3668 case ISD::ATOMIC_LOAD:
3669 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
3670 case ISD::EXTRACT_VECTOR_ELT:
3671 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3672 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3673 }
3674}
3675
3676SDValue SparcTargetLowering::bitcastConstantFPToInt(ConstantFPSDNode *C,
3677 const SDLoc &DL,
3678 SelectionDAG &DAG) const {
3679 APInt V = C->getValueAPF().bitcastToAPInt();
3680 SDValue Lo = DAG.getConstant(Val: V.zextOrTrunc(width: 32), DL, VT: MVT::i32);
3681 SDValue Hi = DAG.getConstant(Val: V.lshr(shiftAmt: 32).zextOrTrunc(width: 32), DL, VT: MVT::i32);
3682 if (DAG.getDataLayout().isLittleEndian())
3683 std::swap(a&: Lo, b&: Hi);
3684 return DAG.getBuildVector(VT: MVT::v2i32, DL, Ops: {Hi, Lo});
3685}
3686
3687SDValue SparcTargetLowering::PerformBITCASTCombine(SDNode *N,
3688 DAGCombinerInfo &DCI) const {
3689 SDLoc dl(N);
3690 SDValue Src = N->getOperand(Num: 0);
3691
3692 if (isa<ConstantFPSDNode>(Val: Src) && N->getSimpleValueType(ResNo: 0) == MVT::v2i32 &&
3693 Src.getSimpleValueType() == MVT::f64)
3694 return bitcastConstantFPToInt(C: cast<ConstantFPSDNode>(Val&: Src), DL: dl, DAG&: DCI.DAG);
3695
3696 return SDValue();
3697}
3698
3699SDValue SparcTargetLowering::PerformBSWAPCombine(SDNode *N,
3700 DAGCombinerInfo &DCI) const {
3701 SDLoc DL(N);
3702 SelectionDAG &DAG = DCI.DAG;
3703 SDValue Op = N->getOperand(Num: 0);
3704 EVT VT = N->getValueType(ResNo: 0);
3705 auto *LN = dyn_cast<LoadSDNode>(Val: Op.getNode());
3706
3707 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
3708 bool IsAlignedLoad = LN && ISD::isNormalLoad(N: Op.getNode()) &&
3709 LN->getAlign() >= VT.getScalarStoreSize();
3710
3711 // Turn BSWAP (aligned-LOAD) -> ld*a #ASI_P(_L) on V9.
3712 if (Subtarget->isV9() && IsAlignedLoad && Op.getNode()->hasOneUse() &&
3713 (VT == MVT::i16 || VT == MVT::i32 ||
3714 (Subtarget->is64Bit() && VT == MVT::i64))) {
3715 SDValue Load = Op;
3716 auto *LD = cast<LoadSDNode>(Val&: Load);
3717
3718 // Create the byte-swapping load.
3719 SDValue Ops[] = {LD->getChain(), LD->getBasePtr(), DAG.getValueType(VT)};
3720
3721 SDValue BSLoad = DAG.getMemIntrinsicNode(
3722 Opcode: IsLittleEndian ? SPISD::LOAD_BIG : SPISD::LOAD_LITTLE, dl: DL,
3723 VTList: DAG.getVTList(VT1: VT == MVT::i64 ? MVT::i64 : MVT::i32, VT2: MVT::Other), Ops,
3724 MemVT: LD->getMemoryVT(), MMO: LD->getMemOperand());
3725
3726 // If this is an i16 load, insert the truncate.
3727 SDValue ResVal = BSLoad;
3728 if (VT == MVT::i16)
3729 ResVal = DAG.getNode(Opcode: ISD::TRUNCATE, DL, VT: MVT::i16, Operand: BSLoad);
3730
3731 return DCI.CombineTo(N, Res: ResVal);
3732 }
3733
3734 return SDValue();
3735}
3736
3737SDValue SparcTargetLowering::PerformSTORECombine(SDNode *N,
3738 DAGCombinerInfo &DCI) const {
3739 SDLoc DL(N);
3740 SelectionDAG &DAG = DCI.DAG;
3741 SDValue Op = N->getOperand(Num: 1);
3742 EVT VT = Op.getValueType();
3743 EVT MemVT = cast<StoreSDNode>(Val: N)->getMemoryVT();
3744 unsigned Opcode = Op.getOpcode();
3745 auto *SN = dyn_cast<StoreSDNode>(Val: N);
3746
3747 bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
3748 bool IsAlignedStore = SN && SN->getAlign() >= MemVT.getScalarStoreSize();
3749
3750 // Turn aligned-STORE (BSWAP) -> st*a #ASI_P(_L) on V9.
3751 if (Subtarget->isV9() && Opcode == ISD::BSWAP && Op.getNode()->hasOneUse() &&
3752 IsAlignedStore &&
3753 (VT == MVT::i16 || VT == MVT::i32 ||
3754 (Subtarget->is64Bit() && VT == MVT::i64))) {
3755
3756 // st*a can only handle simple types and it makes no sense to store less
3757 // than two bytes in byte-reversed order.
3758 if (MemVT.getSizeInBits() < 16)
3759 return SDValue();
3760
3761 SDValue BSwapOp = Op.getOperand(i: 0);
3762 // Do an any-extend to 32-bits if this is a half-word input.
3763 if (BSwapOp.getValueType() == MVT::i16)
3764 BSwapOp = DAG.getNode(Opcode: ISD::ANY_EXTEND, DL, VT: MVT::i32, Operand: BSwapOp);
3765
3766 // If the type of BSWAP operand is wider than stored memory width
3767 // it needs to be shifted to the right side before st*a.
3768 if (VT.bitsGT(VT: MemVT)) {
3769 unsigned Shift = VT.getSizeInBits() - MemVT.getSizeInBits();
3770 BSwapOp = DAG.getNode(Opcode: ISD::SRL, DL, VT, N1: BSwapOp,
3771 N2: DAG.getShiftAmountConstant(Val: Shift, VT, DL));
3772 }
3773
3774 SDValue Ops[] = {N->getOperand(Num: 0), BSwapOp, N->getOperand(Num: 2),
3775 DAG.getValueType(MemVT)};
3776 return DAG.getMemIntrinsicNode(
3777 Opcode: IsLittleEndian ? SPISD::STORE_BIG : SPISD::STORE_LITTLE, dl: DL,
3778 VTList: DAG.getVTList(VT: MVT::Other), Ops, MemVT: cast<StoreSDNode>(Val: N)->getMemoryVT(),
3779 MMO: cast<StoreSDNode>(Val: N)->getMemOperand());
3780 }
3781
3782 return SDValue();
3783}
3784
3785SDValue SparcTargetLowering::PerformDAGCombine(SDNode *N,
3786 DAGCombinerInfo &DCI) const {
3787 switch (N->getOpcode()) {
3788 default:
3789 break;
3790 case ISD::BITCAST:
3791 return PerformBITCASTCombine(N, DCI);
3792 case ISD::BSWAP:
3793 return PerformBSWAPCombine(N, DCI);
3794 case ISD::STORE:
3795 return PerformSTORECombine(N, DCI);
3796 }
3797 return SDValue();
3798}
3799
3800static MachineBasicBlock *emitReadCycleCounter(MachineInstr &MI,
3801 MachineBasicBlock *BB) {
3802 MachineFunction &MF = *BB->getParent();
3803 const BasicBlock *LLVMBB = BB->getBasicBlock();
3804 MachineFunction::iterator It = ++BB->getIterator();
3805 MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(BB: LLVMBB);
3806 MachineBasicBlock *DoneMBB = MF.CreateMachineBasicBlock(BB: LLVMBB);
3807 MF.insert(MBBI: It, MBB: LoopMBB);
3808 MF.insert(MBBI: It, MBB: DoneMBB);
3809
3810 DoneMBB->splice(Where: DoneMBB->begin(), Other: BB,
3811 From: std::next(x: MachineBasicBlock::iterator(MI)), To: BB->end());
3812 DoneMBB->transferSuccessorsAndUpdatePHIs(FromMBB: BB);
3813 BB->addSuccessor(Succ: LoopMBB);
3814 LoopMBB->addSuccessor(Succ: LoopMBB);
3815 LoopMBB->addSuccessor(Succ: DoneMBB);
3816
3817 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
3818 MachineRegisterInfo &MRI = MF.getRegInfo();
3819 Register Lo = MI.getOperand(i: 0).getReg();
3820 Register Hi = MI.getOperand(i: 1).getReg();
3821 Register HiAgain = MRI.createVirtualRegister(RegClass: &SP::IntRegsRegClass);
3822 DebugLoc DL = MI.getDebugLoc();
3823
3824 BuildMI(BB: LoopMBB, MIMD: DL, MCID: TII.get(Opcode: SP::RDASR), DestReg: Hi).addReg(RegNo: SP::ASR22);
3825 BuildMI(BB: LoopMBB, MIMD: DL, MCID: TII.get(Opcode: SP::RDASR), DestReg: Lo).addReg(RegNo: SP::ASR23);
3826 BuildMI(BB: LoopMBB, MIMD: DL, MCID: TII.get(Opcode: SP::RDASR), DestReg: HiAgain).addReg(RegNo: SP::ASR22);
3827 BuildMI(BB: LoopMBB, MIMD: DL, MCID: TII.get(Opcode: SP::SUBCCrr), DestReg: SP::G0).addReg(RegNo: Hi).addReg(RegNo: HiAgain);
3828 BuildMI(BB: LoopMBB, MIMD: DL, MCID: TII.get(Opcode: SP::BCOND)).addMBB(MBB: LoopMBB).addImm(Val: SPCC::ICC_NE);
3829
3830 MI.eraseFromParent();
3831 return DoneMBB;
3832}
3833
3834MachineBasicBlock *
3835SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
3836 MachineBasicBlock *BB) const {
3837 switch (MI.getOpcode()) {
3838 default:
3839 llvm_unreachable("Unknown custom-inserter instruction");
3840 case SP::ReadCycleCounter:
3841 return emitReadCycleCounter(MI, BB);
3842 case SP::SELECT_CC_Int_ICC:
3843 case SP::SELECT_CC_FP_ICC:
3844 case SP::SELECT_CC_DFP_ICC:
3845 case SP::SELECT_CC_QFP_ICC:
3846 if (Subtarget->isV9())
3847 return expandSelectCC(MI, BB, BROpcode: SP::BPICC);
3848 return expandSelectCC(MI, BB, BROpcode: SP::BCOND);
3849 case SP::SELECT_CC_Int_XCC:
3850 case SP::SELECT_CC_FP_XCC:
3851 case SP::SELECT_CC_DFP_XCC:
3852 case SP::SELECT_CC_QFP_XCC:
3853 return expandSelectCC(MI, BB, BROpcode: SP::BPXCC);
3854 case SP::SELECT_CC_Int_FCC:
3855 case SP::SELECT_CC_FP_FCC:
3856 case SP::SELECT_CC_DFP_FCC:
3857 case SP::SELECT_CC_QFP_FCC:
3858 if (Subtarget->isV9())
3859 return expandSelectCC(MI, BB, BROpcode: SP::FBCOND_V9);
3860 return expandSelectCC(MI, BB, BROpcode: SP::FBCOND);
3861 }
3862}
3863
3864MachineBasicBlock *
3865SparcTargetLowering::expandSelectCC(MachineInstr &MI, MachineBasicBlock *BB,
3866 unsigned BROpcode) const {
3867 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
3868 DebugLoc dl = MI.getDebugLoc();
3869 unsigned CC = (SPCC::CondCodes)MI.getOperand(i: 3).getImm();
3870
3871 // To "insert" a SELECT_CC instruction, we actually have to insert the
3872 // triangle control-flow pattern. The incoming instruction knows the
3873 // destination vreg to set, the condition code register to branch on, the
3874 // true/false values to select between, and the condition code for the branch.
3875 //
3876 // We produce the following control flow:
3877 // ThisMBB
3878 // | \
3879 // | IfFalseMBB
3880 // | /
3881 // SinkMBB
3882 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3883 MachineFunction::iterator It = ++BB->getIterator();
3884
3885 MachineBasicBlock *ThisMBB = BB;
3886 MachineFunction *F = BB->getParent();
3887 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(BB: LLVM_BB);
3888 MachineBasicBlock *SinkMBB = F->CreateMachineBasicBlock(BB: LLVM_BB);
3889 F->insert(MBBI: It, MBB: IfFalseMBB);
3890 F->insert(MBBI: It, MBB: SinkMBB);
3891
3892 // Transfer the remainder of ThisMBB and its successor edges to SinkMBB.
3893 SinkMBB->splice(Where: SinkMBB->begin(), Other: ThisMBB,
3894 From: std::next(x: MachineBasicBlock::iterator(MI)), To: ThisMBB->end());
3895 SinkMBB->transferSuccessorsAndUpdatePHIs(FromMBB: ThisMBB);
3896
3897 // Set the new successors for ThisMBB.
3898 ThisMBB->addSuccessor(Succ: IfFalseMBB);
3899 ThisMBB->addSuccessor(Succ: SinkMBB);
3900
3901 BuildMI(BB: ThisMBB, MIMD: dl, MCID: TII.get(Opcode: BROpcode))
3902 .addMBB(MBB: SinkMBB)
3903 .addImm(Val: CC);
3904
3905 // IfFalseMBB just falls through to SinkMBB.
3906 IfFalseMBB->addSuccessor(Succ: SinkMBB);
3907
3908 // %Result = phi [ %TrueValue, ThisMBB ], [ %FalseValue, IfFalseMBB ]
3909 BuildMI(BB&: *SinkMBB, I: SinkMBB->begin(), MIMD: dl, MCID: TII.get(Opcode: SP::PHI),
3910 DestReg: MI.getOperand(i: 0).getReg())
3911 .addReg(RegNo: MI.getOperand(i: 1).getReg())
3912 .addMBB(MBB: ThisMBB)
3913 .addReg(RegNo: MI.getOperand(i: 2).getReg())
3914 .addMBB(MBB: IfFalseMBB);
3915
3916 MI.eraseFromParent(); // The pseudo instruction is gone now.
3917 return SinkMBB;
3918}
3919
3920//===----------------------------------------------------------------------===//
3921// Sparc Inline Assembly Support
3922//===----------------------------------------------------------------------===//
3923
3924/// getConstraintType - Given a constraint letter, return the type of
3925/// constraint it is for this target.
3926SparcTargetLowering::ConstraintType
3927SparcTargetLowering::getConstraintType(StringRef Constraint) const {
3928 if (Constraint.size() == 1) {
3929 switch (Constraint[0]) {
3930 default: break;
3931 case 'r':
3932 case 'f':
3933 case 'e':
3934 return C_RegisterClass;
3935 case 'I': // SIMM13
3936 case 'J': // Zero
3937 case 'K': // 32-bit constant with the low 10 bits clear (sethi)
3938 case 'L': // SIMM11
3939 case 'M': // SIMM10
3940 case 'N': // Unsigned 32-bit constant with the low 10 bits clear (sethi)
3941 case 'O': // The constant 4096
3942 return C_Immediate;
3943 }
3944 }
3945
3946 return TargetLowering::getConstraintType(Constraint);
3947}
3948
3949/// isValidImmForConstraint - Return true if constraint is satisfied
3950static bool isValidImmForConstraint(char Constraint, int64_t Val) {
3951 switch (Constraint) {
3952 case 'I':
3953 return isInt<13>(x: Val);
3954 case 'J':
3955 return Val == 0;
3956 case 'K':
3957 return (isInt<32>(x: Val) || isUInt<32>(x: Val)) && (Val & 0x3ff) == 0;
3958 case 'L':
3959 return isInt<11>(x: Val);
3960 case 'M':
3961 return isInt<10>(x: Val);
3962 case 'N':
3963 return isUInt<32>(x: Val) && (Val & 0x3ff) == 0;
3964 case 'O':
3965 return Val == 4096;
3966 default:
3967 return false;
3968 }
3969}
3970
3971TargetLowering::ConstraintWeight SparcTargetLowering::
3972getSingleConstraintMatchWeight(AsmOperandInfo &info,
3973 const char *constraint) const {
3974 ConstraintWeight weight = CW_Invalid;
3975 Value *CallOperandVal = info.CallOperandVal;
3976 // If we don't have a value, we can't do a match,
3977 // but allow it at the lowest weight.
3978 if (!CallOperandVal)
3979 return CW_Default;
3980
3981 // Look at the constraint type.
3982 switch (*constraint) {
3983 default:
3984 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3985 break;
3986 case 'I': // SIMM13
3987 case 'J':
3988 case 'K':
3989 case 'L':
3990 case 'M':
3991 case 'N':
3992 case 'O':
3993 if (ConstantInt *C = dyn_cast<ConstantInt>(Val: info.CallOperandVal)) {
3994 if (isValidImmForConstraint(Constraint: *constraint, Val: C->getSExtValue()))
3995 weight = CW_Constant;
3996 }
3997 break;
3998 }
3999 return weight;
4000}
4001
4002/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4003/// vector. If it is invalid, don't add anything to Ops.
4004void SparcTargetLowering::LowerAsmOperandForConstraint(
4005 SDValue Op, StringRef Constraint, std::vector<SDValue> &Ops,
4006 SelectionDAG &DAG) const {
4007 SDValue Result;
4008
4009 // Only support length 1 constraints for now.
4010 if (Constraint.size() > 1)
4011 return;
4012
4013 char ConstraintLetter = Constraint[0];
4014 switch (ConstraintLetter) {
4015 default: break;
4016 case 'I':
4017 case 'J':
4018 case 'K':
4019 case 'L':
4020 case 'M':
4021 case 'N':
4022 case 'O':
4023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val&: Op)) {
4024 if (isValidImmForConstraint(Constraint: ConstraintLetter, Val: C->getSExtValue())) {
4025 Result = DAG.getSignedTargetConstant(Val: C->getSExtValue(), DL: SDLoc(Op),
4026 VT: Op.getValueType());
4027 break;
4028 }
4029 return;
4030 }
4031 }
4032
4033 if (Result.getNode()) {
4034 Ops.push_back(x: Result);
4035 return;
4036 }
4037 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
4038}
4039
4040std::pair<unsigned, const TargetRegisterClass *>
4041SparcTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
4042 StringRef Constraint,
4043 MVT VT) const {
4044 if (Constraint.empty())
4045 return std::make_pair(x: 0U, y: nullptr);
4046
4047 if (Constraint.size() == 1) {
4048 switch (Constraint[0]) {
4049 case 'r':
4050 if (VT == MVT::v2i32)
4051 return std::make_pair(x: 0U, y: &SP::IntPairRegClass);
4052 else if (Subtarget->is64Bit())
4053 return std::make_pair(x: 0U, y: &SP::I64RegsRegClass);
4054 else
4055 return std::make_pair(x: 0U, y: &SP::IntRegsRegClass);
4056 case 'f':
4057 if (VT == MVT::f32 || VT == MVT::i32)
4058 return std::make_pair(x: 0U, y: &SP::FPRegsRegClass);
4059 else if (VT == MVT::f64 || VT == MVT::i64)
4060 return std::make_pair(x: 0U, y: &SP::LowDFPRegsRegClass);
4061 else if (VT == MVT::f128)
4062 return std::make_pair(x: 0U, y: &SP::LowQFPRegsRegClass);
4063 // This will generate an error message
4064 return std::make_pair(x: 0U, y: nullptr);
4065 case 'e':
4066 if (VT == MVT::f32 || VT == MVT::i32)
4067 return std::make_pair(x: 0U, y: &SP::FPRegsRegClass);
4068 else if (VT == MVT::f64 || VT == MVT::i64 )
4069 return std::make_pair(x: 0U, y: &SP::DFPRegsRegClass);
4070 else if (VT == MVT::f128)
4071 return std::make_pair(x: 0U, y: &SP::QFPRegsRegClass);
4072 // This will generate an error message
4073 return std::make_pair(x: 0U, y: nullptr);
4074 }
4075 }
4076
4077 if (Constraint.front() != '{')
4078 return std::make_pair(x: 0U, y: nullptr);
4079
4080 assert(Constraint.back() == '}' && "Not a brace enclosed constraint?");
4081 StringRef RegName(Constraint.data() + 1, Constraint.size() - 2);
4082 if (RegName.empty())
4083 return std::make_pair(x: 0U, y: nullptr);
4084
4085 unsigned CCReg = StringSwitch<unsigned>(RegName)
4086 .Cases(CaseStrings: {"cc", "icc"}, Value: SP::ICC)
4087 .Case(S: "fcc0", Value: SP::FCC0)
4088 .Case(S: "fcc1", Value: SP::FCC1)
4089 .Case(S: "fcc2", Value: SP::FCC2)
4090 .Case(S: "fcc3", Value: SP::FCC3)
4091 .Default(Value: 0);
4092 if (CCReg == SP::ICC)
4093 return std::make_pair(x&: CCReg, y: &SP::ICCRegsRegClass);
4094 if (CCReg)
4095 return std::make_pair(x&: CCReg, y: &SP::FCCRegsRegClass);
4096
4097 unsigned long long RegNo;
4098 // Handle numbered register aliases.
4099 if (RegName[0] == 'r' &&
4100 getAsUnsignedInteger(Str: RegName.begin() + 1, Radix: 10, Result&: RegNo)) {
4101 // r0-r7 -> g0-g7
4102 // r8-r15 -> o0-o7
4103 // r16-r23 -> l0-l7
4104 // r24-r31 -> i0-i7
4105 if (RegNo > 31)
4106 return std::make_pair(x: 0U, y: nullptr);
4107 const char RegTypes[] = {'g', 'o', 'l', 'i'};
4108 char RegType = RegTypes[RegNo / 8];
4109 char RegIndex = '0' + (RegNo % 8);
4110 char Tmp[] = {'{', RegType, RegIndex, '}', 0};
4111 return getRegForInlineAsmConstraint(TRI, Constraint: Tmp, VT);
4112 }
4113
4114 // Rewrite the fN constraint according to the value type if needed.
4115 if (VT != MVT::f32 && VT != MVT::Other && RegName[0] == 'f' &&
4116 getAsUnsignedInteger(Str: RegName.begin() + 1, Radix: 10, Result&: RegNo)) {
4117 if (VT == MVT::f64 && (RegNo % 2 == 0)) {
4118 return getRegForInlineAsmConstraint(
4119 TRI, Constraint: StringRef("{d" + utostr(X: RegNo / 2) + "}"), VT);
4120 } else if (VT == MVT::f128 && (RegNo % 4 == 0)) {
4121 return getRegForInlineAsmConstraint(
4122 TRI, Constraint: StringRef("{q" + utostr(X: RegNo / 4) + "}"), VT);
4123 } else {
4124 return std::make_pair(x: 0U, y: nullptr);
4125 }
4126 }
4127
4128 auto ResultPair =
4129 TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4130 if (!ResultPair.second)
4131 return std::make_pair(x: 0U, y: nullptr);
4132
4133 // Force the use of I64Regs over IntRegs for 64-bit values.
4134 if (Subtarget->is64Bit() && VT == MVT::i64) {
4135 assert(ResultPair.second == &SP::IntRegsRegClass &&
4136 "Unexpected register class");
4137 return std::make_pair(x&: ResultPair.first, y: &SP::I64RegsRegClass);
4138 }
4139
4140 return ResultPair;
4141}
4142
4143bool
4144SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4145 // The Sparc target isn't yet aware of offsets.
4146 return false;
4147}
4148
4149void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
4150 SmallVectorImpl<SDValue>& Results,
4151 SelectionDAG &DAG) const {
4152
4153 SDLoc dl(N);
4154
4155 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
4156
4157 switch (N->getOpcode()) {
4158 default:
4159 llvm_unreachable("Do not know how to custom type legalize this operation!");
4160
4161 case ISD::FP_TO_SINT:
4162 case ISD::FP_TO_UINT:
4163 case ISD::STRICT_FP_TO_SINT:
4164 case ISD::STRICT_FP_TO_UINT: {
4165 bool IsStrict = N->isStrictFPOpcode();
4166 // Custom lower only if it involves f128 or i64.
4167 if (N->getOperand(Num: IsStrict ? 1 : 0).getValueType() != MVT::f128 ||
4168 N->getValueType(ResNo: 0) != MVT::i64)
4169 return;
4170 libCall = ((N->getOpcode() == ISD::FP_TO_SINT ||
4171 N->getOpcode() == ISD::STRICT_FP_TO_SINT)
4172 ? RTLIB::FPTOSINT_F128_I64
4173 : RTLIB::FPTOUINT_F128_I64);
4174
4175 SDValue Res = LowerF128Op(Op: SDValue(N, 0), DAG, LibFunc: libCall, numArgs: 1);
4176 Results.push_back(Elt: Res);
4177 if (IsStrict)
4178 Results.push_back(Elt: Res.getValue(R: 1));
4179 return;
4180 }
4181 case ISD::READCYCLECOUNTER:
4182 case ISD::READSTEADYCOUNTER: {
4183 assert(Subtarget->hasLeonCycleCounter());
4184 SDVTList VTs = DAG.getVTList(VT1: MVT::i32, VT2: MVT::i32, VT3: MVT::Other);
4185 SDValue Counter =
4186 DAG.getNode(Opcode: SPISD::READ_CYCLE_COUNTER, DL: dl, VTList: VTs, N: N->getOperand(Num: 0));
4187 Results.push_back(Elt: DAG.getNode(Opcode: ISD::BUILD_PAIR, DL: dl, VT: MVT::i64, N1: Counter,
4188 N2: Counter.getValue(R: 1)));
4189 Results.push_back(Elt: Counter.getValue(R: 2));
4190 return;
4191 }
4192 case ISD::SINT_TO_FP:
4193 case ISD::UINT_TO_FP:
4194 case ISD::STRICT_SINT_TO_FP:
4195 case ISD::STRICT_UINT_TO_FP: {
4196 bool IsStrict = N->isStrictFPOpcode();
4197 // Custom lower only if it involves f128 or i64.
4198 if (N->getValueType(ResNo: 0) != MVT::f128 ||
4199 N->getOperand(Num: IsStrict ? 1 : 0).getValueType() != MVT::i64)
4200 return;
4201
4202 libCall = ((N->getOpcode() == ISD::SINT_TO_FP ||
4203 N->getOpcode() == ISD::STRICT_SINT_TO_FP)
4204 ? RTLIB::SINTTOFP_I64_F128
4205 : RTLIB::UINTTOFP_I64_F128);
4206
4207 SDValue Res = LowerF128Op(Op: SDValue(N, 0), DAG, LibFunc: libCall, numArgs: 1);
4208 Results.push_back(Elt: Res);
4209 if (IsStrict)
4210 Results.push_back(Elt: Res.getValue(R: 1));
4211 return;
4212 }
4213 case ISD::LOAD: {
4214 LoadSDNode *Ld = cast<LoadSDNode>(Val: N);
4215 // Custom handling only for aligned i64 loads: turn i64 load into a v2i32
4216 // load, and a bitcast.
4217 if (Ld->getValueType(ResNo: 0) != MVT::i64 || Ld->getMemoryVT() != MVT::i64 ||
4218 Ld->getAlign() < Align(8))
4219 return;
4220
4221 SDLoc dl(N);
4222 SDValue LoadRes = DAG.getExtLoad(
4223 ExtType: Ld->getExtensionType(), dl, VT: MVT::v2i32, Chain: Ld->getChain(),
4224 Ptr: Ld->getBasePtr(), PtrInfo: Ld->getPointerInfo(), MemVT: MVT::v2i32, Alignment: Ld->getBaseAlign(),
4225 MMOFlags: Ld->getMemOperand()->getFlags(), Metadata: Ld->getAAInfo());
4226
4227 SDValue Res = DAG.getNode(Opcode: ISD::BITCAST, DL: dl, VT: MVT::i64, Operand: LoadRes);
4228 Results.push_back(Elt: Res);
4229 Results.push_back(Elt: LoadRes.getValue(R: 1));
4230 return;
4231 }
4232 }
4233}
4234
4235// Override to enable LOAD_STACK_GUARD lowering on Linux.
4236bool SparcTargetLowering::useLoadStackGuardNode(const Module &M) const {
4237 if (!Subtarget->getTargetTriple().isOSLinux())
4238 return TargetLowering::useLoadStackGuardNode(M);
4239 return true;
4240}
4241
4242bool SparcTargetLowering::isFNegFree(EVT VT) const {
4243 if (Subtarget->isVIS3())
4244 return VT == MVT::f32 || VT == MVT::f64;
4245 return false;
4246}
4247
4248TargetLowering::FPImmAction
4249SparcTargetLowering::getFPImmAction(const APFloat &Imm, EVT VT,
4250 bool ForCodeSize) const {
4251 if (VT != MVT::f32 && VT != MVT::f64)
4252 return FPImmAction::ConstantPool;
4253 if (Subtarget->isVIS() && Imm.isZero())
4254 return FPImmAction::Legal;
4255 // Check for +/-0.5.
4256 if (Subtarget->isVIS3() && Imm.getExactLog2Abs() == -1)
4257 return FPImmAction::Legal;
4258 return FPImmAction::ConstantPool;
4259}
4260
4261bool SparcTargetLowering::isCtlzFast() const { return Subtarget->isVIS3(); }
4262
4263bool SparcTargetLowering::isCheapToSpeculateCttz(Type *Ty) const {
4264 // We lack native cttz, however,
4265 // On 64-bit targets it is cheap to implement it in terms of popc.
4266 if (Subtarget->is64Bit() && Subtarget->usePopc())
4267 return true;
4268 // Otherwise, implementing cttz in terms of ctlz is still cheap.
4269 return isCheapToSpeculateCtlz(Ty);
4270}
4271
4272bool SparcTargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
4273 EVT VT) const {
4274 return Subtarget->isUA2007() && !Subtarget->useSoftFloat();
4275}
4276
4277void SparcTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
4278 SDNode *Node) const {
4279 assert(MI.getOpcode() == SP::SUBCCrr || MI.getOpcode() == SP::SUBCCri);
4280 // If the result is dead, replace it with %g0.
4281 if (!Node->hasAnyUseOfValue(Value: 0))
4282 MI.getOperand(i: 0).setReg(SP::G0);
4283}
4284
4285Instruction *SparcTargetLowering::emitLeadingFence(IRBuilderBase &Builder,
4286 Instruction *Inst,
4287 AtomicOrdering Ord) const {
4288 bool HasStoreSemantics =
4289 isa<AtomicCmpXchgInst, AtomicRMWInst, StoreInst>(Val: Inst);
4290 if (HasStoreSemantics && isReleaseOrStronger(AO: Ord))
4291 return Builder.CreateFence(Ordering: AtomicOrdering::Release);
4292 return nullptr;
4293}
4294
4295Instruction *SparcTargetLowering::emitTrailingFence(IRBuilderBase &Builder,
4296 Instruction *Inst,
4297 AtomicOrdering Ord) const {
4298 // V8 loads already come with implicit acquire barrier so there's no need to
4299 // emit it again.
4300 bool HasLoadSemantics = isa<AtomicCmpXchgInst, AtomicRMWInst, LoadInst>(Val: Inst);
4301 if (Subtarget->isV9() && HasLoadSemantics && isAcquireOrStronger(AO: Ord))
4302 return Builder.CreateFence(Ordering: AtomicOrdering::Acquire);
4303
4304 // SC plain stores would need a trailing full barrier.
4305 if (isa<StoreInst>(Val: Inst) && Ord == AtomicOrdering::SequentiallyConsistent)
4306 return Builder.CreateFence(Ordering: Ord);
4307 return nullptr;
4308}
4309