xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64CallingConvention.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //=== AArch64CallingConvention.cpp - AArch64 CC impl ------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the table-generated and custom routines for the AArch64
100b57cec5SDimitry Andric // Calling Convention.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "AArch64CallingConvention.h"
150b57cec5SDimitry Andric #include "AArch64.h"
160b57cec5SDimitry Andric #include "AArch64InstrInfo.h"
170b57cec5SDimitry Andric #include "AArch64Subtarget.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/CallingConvLower.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
200b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric static const MCPhysReg XRegList[] = {AArch64::X0, AArch64::X1, AArch64::X2,
240b57cec5SDimitry Andric                                      AArch64::X3, AArch64::X4, AArch64::X5,
250b57cec5SDimitry Andric                                      AArch64::X6, AArch64::X7};
260b57cec5SDimitry Andric static const MCPhysReg HRegList[] = {AArch64::H0, AArch64::H1, AArch64::H2,
270b57cec5SDimitry Andric                                      AArch64::H3, AArch64::H4, AArch64::H5,
280b57cec5SDimitry Andric                                      AArch64::H6, AArch64::H7};
290b57cec5SDimitry Andric static const MCPhysReg SRegList[] = {AArch64::S0, AArch64::S1, AArch64::S2,
300b57cec5SDimitry Andric                                      AArch64::S3, AArch64::S4, AArch64::S5,
310b57cec5SDimitry Andric                                      AArch64::S6, AArch64::S7};
320b57cec5SDimitry Andric static const MCPhysReg DRegList[] = {AArch64::D0, AArch64::D1, AArch64::D2,
330b57cec5SDimitry Andric                                      AArch64::D3, AArch64::D4, AArch64::D5,
340b57cec5SDimitry Andric                                      AArch64::D6, AArch64::D7};
350b57cec5SDimitry Andric static const MCPhysReg QRegList[] = {AArch64::Q0, AArch64::Q1, AArch64::Q2,
360b57cec5SDimitry Andric                                      AArch64::Q3, AArch64::Q4, AArch64::Q5,
370b57cec5SDimitry Andric                                      AArch64::Q6, AArch64::Q7};
38eaeb601bSDimitry Andric static const MCPhysReg ZRegList[] = {AArch64::Z0, AArch64::Z1, AArch64::Z2,
39eaeb601bSDimitry Andric                                      AArch64::Z3, AArch64::Z4, AArch64::Z5,
40eaeb601bSDimitry Andric                                      AArch64::Z6, AArch64::Z7};
41*0fca6ea1SDimitry Andric static const MCPhysReg PRegList[] = {AArch64::P0, AArch64::P1, AArch64::P2,
42*0fca6ea1SDimitry Andric                                      AArch64::P3};
430b57cec5SDimitry Andric 
finishStackBlock(SmallVectorImpl<CCValAssign> & PendingMembers,MVT LocVT,ISD::ArgFlagsTy & ArgFlags,CCState & State,Align SlotAlign)440b57cec5SDimitry Andric static bool finishStackBlock(SmallVectorImpl<CCValAssign> &PendingMembers,
450b57cec5SDimitry Andric                              MVT LocVT, ISD::ArgFlagsTy &ArgFlags,
465ffd83dbSDimitry Andric                              CCState &State, Align SlotAlign) {
47e8d8bef9SDimitry Andric   if (LocVT.isScalableVector()) {
48e8d8bef9SDimitry Andric     const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>(
49e8d8bef9SDimitry Andric         State.getMachineFunction().getSubtarget());
50e8d8bef9SDimitry Andric     const AArch64TargetLowering *TLI = Subtarget.getTargetLowering();
51e8d8bef9SDimitry Andric 
52e8d8bef9SDimitry Andric     // We are about to reinvoke the CCAssignFn auto-generated handler. If we
53e8d8bef9SDimitry Andric     // don't unset these flags we will get stuck in an infinite loop forever
54e8d8bef9SDimitry Andric     // invoking the custom handler.
55e8d8bef9SDimitry Andric     ArgFlags.setInConsecutiveRegs(false);
56e8d8bef9SDimitry Andric     ArgFlags.setInConsecutiveRegsLast(false);
57e8d8bef9SDimitry Andric 
58e8d8bef9SDimitry Andric     // The calling convention for passing SVE tuples states that in the event
59e8d8bef9SDimitry Andric     // we cannot allocate enough registers for the tuple we should still leave
60e8d8bef9SDimitry Andric     // any remaining registers unallocated. However, when we call the
61e8d8bef9SDimitry Andric     // CCAssignFn again we want it to behave as if all remaining registers are
62e8d8bef9SDimitry Andric     // allocated. This will force the code to pass the tuple indirectly in
63e8d8bef9SDimitry Andric     // accordance with the PCS.
64*0fca6ea1SDimitry Andric     bool ZRegsAllocated[8];
65e8d8bef9SDimitry Andric     for (int I = 0; I < 8; I++) {
66*0fca6ea1SDimitry Andric       ZRegsAllocated[I] = State.isAllocated(ZRegList[I]);
67e8d8bef9SDimitry Andric       State.AllocateReg(ZRegList[I]);
68e8d8bef9SDimitry Andric     }
69*0fca6ea1SDimitry Andric     // The same applies to P registers.
70*0fca6ea1SDimitry Andric     bool PRegsAllocated[4];
71*0fca6ea1SDimitry Andric     for (int I = 0; I < 4; I++) {
72*0fca6ea1SDimitry Andric       PRegsAllocated[I] = State.isAllocated(PRegList[I]);
73*0fca6ea1SDimitry Andric       State.AllocateReg(PRegList[I]);
74*0fca6ea1SDimitry Andric     }
75e8d8bef9SDimitry Andric 
76e8d8bef9SDimitry Andric     auto &It = PendingMembers[0];
77e8d8bef9SDimitry Andric     CCAssignFn *AssignFn =
78e8d8bef9SDimitry Andric         TLI->CCAssignFnForCall(State.getCallingConv(), /*IsVarArg=*/false);
79e8d8bef9SDimitry Andric     if (AssignFn(It.getValNo(), It.getValVT(), It.getValVT(), CCValAssign::Full,
80e8d8bef9SDimitry Andric                  ArgFlags, State))
81e8d8bef9SDimitry Andric       llvm_unreachable("Call operand has unhandled type");
82e8d8bef9SDimitry Andric 
83e8d8bef9SDimitry Andric     // Return the flags to how they were before.
84e8d8bef9SDimitry Andric     ArgFlags.setInConsecutiveRegs(true);
85e8d8bef9SDimitry Andric     ArgFlags.setInConsecutiveRegsLast(true);
86e8d8bef9SDimitry Andric 
87e8d8bef9SDimitry Andric     // Return the register state back to how it was before, leaving any
88e8d8bef9SDimitry Andric     // unallocated registers available for other smaller types.
89e8d8bef9SDimitry Andric     for (int I = 0; I < 8; I++)
90*0fca6ea1SDimitry Andric       if (!ZRegsAllocated[I])
91e8d8bef9SDimitry Andric         State.DeallocateReg(ZRegList[I]);
92*0fca6ea1SDimitry Andric     for (int I = 0; I < 4; I++)
93*0fca6ea1SDimitry Andric       if (!PRegsAllocated[I])
94*0fca6ea1SDimitry Andric         State.DeallocateReg(PRegList[I]);
95e8d8bef9SDimitry Andric 
96e8d8bef9SDimitry Andric     // All pending members have now been allocated
97e8d8bef9SDimitry Andric     PendingMembers.clear();
98e8d8bef9SDimitry Andric     return true;
99e8d8bef9SDimitry Andric   }
100e8d8bef9SDimitry Andric 
1010b57cec5SDimitry Andric   unsigned Size = LocVT.getSizeInBits() / 8;
1020b57cec5SDimitry Andric   for (auto &It : PendingMembers) {
103fe6060f1SDimitry Andric     It.convertToMem(State.AllocateStack(Size, SlotAlign));
1040b57cec5SDimitry Andric     State.addLoc(It);
1055ffd83dbSDimitry Andric     SlotAlign = Align(1);
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   // All pending members have now been allocated
1090b57cec5SDimitry Andric   PendingMembers.clear();
1100b57cec5SDimitry Andric   return true;
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric /// The Darwin variadic PCS places anonymous arguments in 8-byte stack slots. An
1140b57cec5SDimitry Andric /// [N x Ty] type must still be contiguous in memory though.
CC_AArch64_Custom_Stack_Block(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)1150b57cec5SDimitry Andric static bool CC_AArch64_Custom_Stack_Block(
1160b57cec5SDimitry Andric       unsigned &ValNo, MVT &ValVT, MVT &LocVT, CCValAssign::LocInfo &LocInfo,
1170b57cec5SDimitry Andric       ISD::ArgFlagsTy &ArgFlags, CCState &State) {
1180b57cec5SDimitry Andric   SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   // Add the argument to the list to be allocated once we know the size of the
1210b57cec5SDimitry Andric   // block.
1220b57cec5SDimitry Andric   PendingMembers.push_back(
1230b57cec5SDimitry Andric       CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   if (!ArgFlags.isInConsecutiveRegsLast())
1260b57cec5SDimitry Andric     return true;
1270b57cec5SDimitry Andric 
1285ffd83dbSDimitry Andric   return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, Align(8));
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric /// Given an [N x Ty] block, it should be passed in a consecutive sequence of
1320b57cec5SDimitry Andric /// registers. If no such sequence is available, mark the rest of the registers
1330b57cec5SDimitry Andric /// of that type as used and place the argument on the stack.
CC_AArch64_Custom_Block(unsigned & ValNo,MVT & ValVT,MVT & LocVT,CCValAssign::LocInfo & LocInfo,ISD::ArgFlagsTy & ArgFlags,CCState & State)1340b57cec5SDimitry Andric static bool CC_AArch64_Custom_Block(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1350b57cec5SDimitry Andric                                     CCValAssign::LocInfo &LocInfo,
1360b57cec5SDimitry Andric                                     ISD::ArgFlagsTy &ArgFlags, CCState &State) {
1378bcb0991SDimitry Andric   const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>(
1388bcb0991SDimitry Andric       State.getMachineFunction().getSubtarget());
1398bcb0991SDimitry Andric   bool IsDarwinILP32 = Subtarget.isTargetILP32() && Subtarget.isTargetMachO();
1408bcb0991SDimitry Andric 
1410b57cec5SDimitry Andric   // Try to allocate a contiguous block of registers, each of the correct
1420b57cec5SDimitry Andric   // size to hold one member.
1430b57cec5SDimitry Andric   ArrayRef<MCPhysReg> RegList;
1448bcb0991SDimitry Andric   if (LocVT.SimpleTy == MVT::i64 || (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32))
1450b57cec5SDimitry Andric     RegList = XRegList;
1460b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f16)
1470b57cec5SDimitry Andric     RegList = HRegList;
1480b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f32 || LocVT.is32BitVector())
1490b57cec5SDimitry Andric     RegList = SRegList;
1500b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f64 || LocVT.is64BitVector())
1510b57cec5SDimitry Andric     RegList = DRegList;
1520b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f128 || LocVT.is128BitVector())
1530b57cec5SDimitry Andric     RegList = QRegList;
154*0fca6ea1SDimitry Andric   else if (LocVT.isScalableVector()) {
155*0fca6ea1SDimitry Andric     // Scalable masks should be pass by Predicate registers.
156*0fca6ea1SDimitry Andric     if (LocVT == MVT::nxv1i1 || LocVT == MVT::nxv2i1 || LocVT == MVT::nxv4i1 ||
157*0fca6ea1SDimitry Andric         LocVT == MVT::nxv8i1 || LocVT == MVT::nxv16i1 ||
158*0fca6ea1SDimitry Andric         LocVT == MVT::aarch64svcount)
159*0fca6ea1SDimitry Andric       RegList = PRegList;
160*0fca6ea1SDimitry Andric     else
161eaeb601bSDimitry Andric       RegList = ZRegList;
162*0fca6ea1SDimitry Andric   } else {
1630b57cec5SDimitry Andric     // Not an array we want to split up after all.
1640b57cec5SDimitry Andric     return false;
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   // Add the argument to the list to be allocated once we know the size of the
1700b57cec5SDimitry Andric   // block.
1710b57cec5SDimitry Andric   PendingMembers.push_back(
1720b57cec5SDimitry Andric       CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   if (!ArgFlags.isInConsecutiveRegsLast())
1750b57cec5SDimitry Andric     return true;
1760b57cec5SDimitry Andric 
1778bcb0991SDimitry Andric   // [N x i32] arguments get packed into x-registers on Darwin's arm64_32
1788bcb0991SDimitry Andric   // because that's how the armv7k Clang front-end emits small structs.
1798bcb0991SDimitry Andric   unsigned EltsPerReg = (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32) ? 2 : 1;
1808bcb0991SDimitry Andric   unsigned RegResult = State.AllocateRegBlock(
1818bcb0991SDimitry Andric       RegList, alignTo(PendingMembers.size(), EltsPerReg) / EltsPerReg);
1828bcb0991SDimitry Andric   if (RegResult && EltsPerReg == 1) {
1830b57cec5SDimitry Andric     for (auto &It : PendingMembers) {
1840b57cec5SDimitry Andric       It.convertToReg(RegResult);
1850b57cec5SDimitry Andric       State.addLoc(It);
1860b57cec5SDimitry Andric       ++RegResult;
1870b57cec5SDimitry Andric     }
1880b57cec5SDimitry Andric     PendingMembers.clear();
1890b57cec5SDimitry Andric     return true;
1908bcb0991SDimitry Andric   } else if (RegResult) {
1918bcb0991SDimitry Andric     assert(EltsPerReg == 2 && "unexpected ABI");
1928bcb0991SDimitry Andric     bool UseHigh = false;
1938bcb0991SDimitry Andric     CCValAssign::LocInfo Info;
1948bcb0991SDimitry Andric     for (auto &It : PendingMembers) {
1958bcb0991SDimitry Andric       Info = UseHigh ? CCValAssign::AExtUpper : CCValAssign::ZExt;
1968bcb0991SDimitry Andric       State.addLoc(CCValAssign::getReg(It.getValNo(), MVT::i32, RegResult,
1978bcb0991SDimitry Andric                                        MVT::i64, Info));
1988bcb0991SDimitry Andric       UseHigh = !UseHigh;
1998bcb0991SDimitry Andric       if (!UseHigh)
2008bcb0991SDimitry Andric         ++RegResult;
2018bcb0991SDimitry Andric     }
2028bcb0991SDimitry Andric     PendingMembers.clear();
2038bcb0991SDimitry Andric     return true;
2040b57cec5SDimitry Andric   }
2050b57cec5SDimitry Andric 
206e8d8bef9SDimitry Andric   if (!LocVT.isScalableVector()) {
2070b57cec5SDimitry Andric     // Mark all regs in the class as unavailable
2080b57cec5SDimitry Andric     for (auto Reg : RegList)
2090b57cec5SDimitry Andric       State.AllocateReg(Reg);
210e8d8bef9SDimitry Andric   }
2110b57cec5SDimitry Andric 
212fe6060f1SDimitry Andric   const Align StackAlign =
213fe6060f1SDimitry Andric       State.getMachineFunction().getDataLayout().getStackAlignment();
214fe6060f1SDimitry Andric   const Align MemAlign = ArgFlags.getNonZeroMemAlign();
215fe6060f1SDimitry Andric   Align SlotAlign = std::min(MemAlign, StackAlign);
216fe6060f1SDimitry Andric   if (!Subtarget.isTargetDarwin())
217fe6060f1SDimitry Andric     SlotAlign = std::max(SlotAlign, Align(8));
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, SlotAlign);
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric // TableGen provides definitions of the calling convention analysis entry
2230b57cec5SDimitry Andric // points.
2240b57cec5SDimitry Andric #include "AArch64GenCallingConv.inc"
225