1*0b57cec5SDimitry Andric //===-- PPCCallingConv.h - --------------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "PPCRegisterInfo.h"
10*0b57cec5SDimitry Andric #include "PPCCallingConv.h"
11*0b57cec5SDimitry Andric #include "PPCSubtarget.h"
12*0b57cec5SDimitry Andric #include "PPCCCState.h"
13*0b57cec5SDimitry Andric using namespace llvm;
14*0b57cec5SDimitry Andric 
CC_PPC_AnyReg_Error(unsigned &,MVT &,MVT &,CCValAssign::LocInfo &,ISD::ArgFlagsTy &,CCState &)15*0b57cec5SDimitry Andric inline bool CC_PPC_AnyReg_Error(unsigned &, MVT &, MVT &,
16*0b57cec5SDimitry Andric                                 CCValAssign::LocInfo &, ISD::ArgFlagsTy &,
17*0b57cec5SDimitry Andric                                 CCState &) {
18*0b57cec5SDimitry Andric   llvm_unreachable("The AnyReg calling convention is only supported by the " \
19*0b57cec5SDimitry Andric                    "stackmap and patchpoint intrinsics.");
20*0b57cec5SDimitry Andric   // gracefully fallback to PPC C calling convention on Release builds.
21*0b57cec5SDimitry Andric   return false;
22*0b57cec5SDimitry Andric }
23*0b57cec5SDimitry Andric 
CC_PPC32_SVR4_Custom_Dummy(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)24*0b57cec5SDimitry Andric static bool CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
25*0b57cec5SDimitry Andric                                        CCValAssign::LocInfo &LocInfo,
26*0b57cec5SDimitry Andric                                        ISD::ArgFlagsTy &ArgFlags,
27*0b57cec5SDimitry Andric                                        CCState &State) {
28*0b57cec5SDimitry Andric   return true;
29*0b57cec5SDimitry Andric }
30*0b57cec5SDimitry Andric 
CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)31*0b57cec5SDimitry Andric static bool CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
32*0b57cec5SDimitry Andric                                               MVT &LocVT,
33*0b57cec5SDimitry Andric                                               CCValAssign::LocInfo &LocInfo,
34*0b57cec5SDimitry Andric                                               ISD::ArgFlagsTy &ArgFlags,
35*0b57cec5SDimitry Andric                                               CCState &State) {
36*0b57cec5SDimitry Andric   static const MCPhysReg ArgRegs[] = {
37*0b57cec5SDimitry Andric     PPC::R3, PPC::R4, PPC::R5, PPC::R6,
38*0b57cec5SDimitry Andric     PPC::R7, PPC::R8, PPC::R9, PPC::R10,
39*0b57cec5SDimitry Andric   };
40*0b57cec5SDimitry Andric   const unsigned NumArgRegs = array_lengthof(ArgRegs);
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric   unsigned RegNum = State.getFirstUnallocated(ArgRegs);
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric   // Skip one register if the first unallocated register has an even register
45*0b57cec5SDimitry Andric   // number and there are still argument registers available which have not been
46*0b57cec5SDimitry Andric   // allocated yet. RegNum is actually an index into ArgRegs, which means we
47*0b57cec5SDimitry Andric   // need to skip a register if RegNum is odd.
48*0b57cec5SDimitry Andric   if (RegNum != NumArgRegs && RegNum % 2 == 1) {
49*0b57cec5SDimitry Andric     State.AllocateReg(ArgRegs[RegNum]);
50*0b57cec5SDimitry Andric   }
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   // Always return false here, as this function only makes sure that the first
53*0b57cec5SDimitry Andric   // unallocated register has an odd register number and does not actually
54*0b57cec5SDimitry Andric   // allocate a register for the current argument.
55*0b57cec5SDimitry Andric   return false;
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric 
CC_PPC32_SVR4_Custom_SkipLastArgRegsPPCF128(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)58*0b57cec5SDimitry Andric static bool CC_PPC32_SVR4_Custom_SkipLastArgRegsPPCF128(
59*0b57cec5SDimitry Andric     unsigned &ValNo, MVT &ValVT, MVT &LocVT, CCValAssign::LocInfo &LocInfo,
60*0b57cec5SDimitry Andric     ISD::ArgFlagsTy &ArgFlags, CCState &State) {
61*0b57cec5SDimitry Andric   static const MCPhysReg ArgRegs[] = {
62*0b57cec5SDimitry Andric     PPC::R3, PPC::R4, PPC::R5, PPC::R6,
63*0b57cec5SDimitry Andric     PPC::R7, PPC::R8, PPC::R9, PPC::R10,
64*0b57cec5SDimitry Andric   };
65*0b57cec5SDimitry Andric   const unsigned NumArgRegs = array_lengthof(ArgRegs);
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric   unsigned RegNum = State.getFirstUnallocated(ArgRegs);
68*0b57cec5SDimitry Andric   int RegsLeft = NumArgRegs - RegNum;
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric   // Skip if there is not enough registers left for long double type (4 gpr regs
71*0b57cec5SDimitry Andric   // in soft float mode) and put long double argument on the stack.
72*0b57cec5SDimitry Andric   if (RegNum != NumArgRegs && RegsLeft < 4) {
73*0b57cec5SDimitry Andric     for (int i = 0; i < RegsLeft; i++) {
74*0b57cec5SDimitry Andric       State.AllocateReg(ArgRegs[RegNum + i]);
75*0b57cec5SDimitry Andric     }
76*0b57cec5SDimitry Andric   }
77*0b57cec5SDimitry Andric 
78*0b57cec5SDimitry Andric   return false;
79*0b57cec5SDimitry Andric }
80*0b57cec5SDimitry Andric 
CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)81*0b57cec5SDimitry Andric static bool CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
82*0b57cec5SDimitry Andric                                                 MVT &LocVT,
83*0b57cec5SDimitry Andric                                                 CCValAssign::LocInfo &LocInfo,
84*0b57cec5SDimitry Andric                                                 ISD::ArgFlagsTy &ArgFlags,
85*0b57cec5SDimitry Andric                                                 CCState &State) {
86*0b57cec5SDimitry Andric   static const MCPhysReg ArgRegs[] = {
87*0b57cec5SDimitry Andric     PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
88*0b57cec5SDimitry Andric     PPC::F8
89*0b57cec5SDimitry Andric   };
90*0b57cec5SDimitry Andric 
91*0b57cec5SDimitry Andric   const unsigned NumArgRegs = array_lengthof(ArgRegs);
92*0b57cec5SDimitry Andric 
93*0b57cec5SDimitry Andric   unsigned RegNum = State.getFirstUnallocated(ArgRegs);
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   // If there is only one Floating-point register left we need to put both f64
96*0b57cec5SDimitry Andric   // values of a split ppc_fp128 value on the stack.
97*0b57cec5SDimitry Andric   if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
98*0b57cec5SDimitry Andric     State.AllocateReg(ArgRegs[RegNum]);
99*0b57cec5SDimitry Andric   }
100*0b57cec5SDimitry Andric 
101*0b57cec5SDimitry Andric   // Always return false here, as this function only makes sure that the two f64
102*0b57cec5SDimitry Andric   // values a ppc_fp128 value is split into are both passed in registers or both
103*0b57cec5SDimitry Andric   // passed on the stack and does not actually allocate a register for the
104*0b57cec5SDimitry Andric   // current argument.
105*0b57cec5SDimitry Andric   return false;
106*0b57cec5SDimitry Andric }
107*0b57cec5SDimitry Andric 
108*0b57cec5SDimitry Andric // Split F64 arguments into two 32-bit consecutive registers.
CC_PPC32_SPE_CustomSplitFP64(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)109*0b57cec5SDimitry Andric static bool CC_PPC32_SPE_CustomSplitFP64(unsigned &ValNo, MVT &ValVT,
110*0b57cec5SDimitry Andric                                         MVT &LocVT,
111*0b57cec5SDimitry Andric                                         CCValAssign::LocInfo &LocInfo,
112*0b57cec5SDimitry Andric                                         ISD::ArgFlagsTy &ArgFlags,
113*0b57cec5SDimitry Andric                                         CCState &State) {
114*0b57cec5SDimitry Andric   static const MCPhysReg HiRegList[] = { PPC::R3, PPC::R5, PPC::R7, PPC::R9 };
115*0b57cec5SDimitry Andric   static const MCPhysReg LoRegList[] = { PPC::R4, PPC::R6, PPC::R8, PPC::R10 };
116*0b57cec5SDimitry Andric 
117*0b57cec5SDimitry Andric   // Try to get the first register.
118*0b57cec5SDimitry Andric   unsigned Reg = State.AllocateReg(HiRegList);
119*0b57cec5SDimitry Andric   if (!Reg)
120*0b57cec5SDimitry Andric     return false;
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric   unsigned i;
123*0b57cec5SDimitry Andric   for (i = 0; i < sizeof(HiRegList) / sizeof(HiRegList[0]); ++i)
124*0b57cec5SDimitry Andric     if (HiRegList[i] == Reg)
125*0b57cec5SDimitry Andric       break;
126*0b57cec5SDimitry Andric 
127*0b57cec5SDimitry Andric   unsigned T = State.AllocateReg(LoRegList[i]);
128*0b57cec5SDimitry Andric   (void)T;
129*0b57cec5SDimitry Andric   assert(T == LoRegList[i] && "Could not allocate register");
130*0b57cec5SDimitry Andric 
131*0b57cec5SDimitry Andric   State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
132*0b57cec5SDimitry Andric   State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
133*0b57cec5SDimitry Andric                                          LocVT, LocInfo));
134*0b57cec5SDimitry Andric   return true;
135*0b57cec5SDimitry Andric }
136*0b57cec5SDimitry Andric 
137*0b57cec5SDimitry Andric // Same as above, but for return values, so only allocate for R3 and R4
CC_PPC32_SPE_RetF64(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)138*0b57cec5SDimitry Andric static bool CC_PPC32_SPE_RetF64(unsigned &ValNo, MVT &ValVT,
139*0b57cec5SDimitry Andric                                MVT &LocVT,
140*0b57cec5SDimitry Andric                                CCValAssign::LocInfo &LocInfo,
141*0b57cec5SDimitry Andric                                ISD::ArgFlagsTy &ArgFlags,
142*0b57cec5SDimitry Andric                                CCState &State) {
143*0b57cec5SDimitry Andric   static const MCPhysReg HiRegList[] = { PPC::R3 };
144*0b57cec5SDimitry Andric   static const MCPhysReg LoRegList[] = { PPC::R4 };
145*0b57cec5SDimitry Andric 
146*0b57cec5SDimitry Andric   // Try to get the first register.
147*0b57cec5SDimitry Andric   unsigned Reg = State.AllocateReg(HiRegList, LoRegList);
148*0b57cec5SDimitry Andric   if (!Reg)
149*0b57cec5SDimitry Andric     return false;
150*0b57cec5SDimitry Andric 
151*0b57cec5SDimitry Andric   unsigned i;
152*0b57cec5SDimitry Andric   for (i = 0; i < sizeof(HiRegList) / sizeof(HiRegList[0]); ++i)
153*0b57cec5SDimitry Andric     if (HiRegList[i] == Reg)
154*0b57cec5SDimitry Andric       break;
155*0b57cec5SDimitry Andric 
156*0b57cec5SDimitry Andric   State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
157*0b57cec5SDimitry Andric   State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
158*0b57cec5SDimitry Andric                                          LocVT, LocInfo));
159*0b57cec5SDimitry Andric   return true;
160*0b57cec5SDimitry Andric }
161*0b57cec5SDimitry Andric 
162*0b57cec5SDimitry Andric #include "PPCGenCallingConv.inc"
163