xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64CallingConvention.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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};
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric static bool finishStackBlock(SmallVectorImpl<CCValAssign> &PendingMembers,
430b57cec5SDimitry Andric                              MVT LocVT, ISD::ArgFlagsTy &ArgFlags,
445ffd83dbSDimitry Andric                              CCState &State, Align SlotAlign) {
45*e8d8bef9SDimitry Andric   if (LocVT.isScalableVector()) {
46*e8d8bef9SDimitry Andric     const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>(
47*e8d8bef9SDimitry Andric         State.getMachineFunction().getSubtarget());
48*e8d8bef9SDimitry Andric     const AArch64TargetLowering *TLI = Subtarget.getTargetLowering();
49*e8d8bef9SDimitry Andric 
50*e8d8bef9SDimitry Andric     // We are about to reinvoke the CCAssignFn auto-generated handler. If we
51*e8d8bef9SDimitry Andric     // don't unset these flags we will get stuck in an infinite loop forever
52*e8d8bef9SDimitry Andric     // invoking the custom handler.
53*e8d8bef9SDimitry Andric     ArgFlags.setInConsecutiveRegs(false);
54*e8d8bef9SDimitry Andric     ArgFlags.setInConsecutiveRegsLast(false);
55*e8d8bef9SDimitry Andric 
56*e8d8bef9SDimitry Andric     // The calling convention for passing SVE tuples states that in the event
57*e8d8bef9SDimitry Andric     // we cannot allocate enough registers for the tuple we should still leave
58*e8d8bef9SDimitry Andric     // any remaining registers unallocated. However, when we call the
59*e8d8bef9SDimitry Andric     // CCAssignFn again we want it to behave as if all remaining registers are
60*e8d8bef9SDimitry Andric     // allocated. This will force the code to pass the tuple indirectly in
61*e8d8bef9SDimitry Andric     // accordance with the PCS.
62*e8d8bef9SDimitry Andric     bool RegsAllocated[8];
63*e8d8bef9SDimitry Andric     for (int I = 0; I < 8; I++) {
64*e8d8bef9SDimitry Andric       RegsAllocated[I] = State.isAllocated(ZRegList[I]);
65*e8d8bef9SDimitry Andric       State.AllocateReg(ZRegList[I]);
66*e8d8bef9SDimitry Andric     }
67*e8d8bef9SDimitry Andric 
68*e8d8bef9SDimitry Andric     auto &It = PendingMembers[0];
69*e8d8bef9SDimitry Andric     CCAssignFn *AssignFn =
70*e8d8bef9SDimitry Andric         TLI->CCAssignFnForCall(State.getCallingConv(), /*IsVarArg=*/false);
71*e8d8bef9SDimitry Andric     if (AssignFn(It.getValNo(), It.getValVT(), It.getValVT(), CCValAssign::Full,
72*e8d8bef9SDimitry Andric                  ArgFlags, State))
73*e8d8bef9SDimitry Andric       llvm_unreachable("Call operand has unhandled type");
74*e8d8bef9SDimitry Andric 
75*e8d8bef9SDimitry Andric     // Return the flags to how they were before.
76*e8d8bef9SDimitry Andric     ArgFlags.setInConsecutiveRegs(true);
77*e8d8bef9SDimitry Andric     ArgFlags.setInConsecutiveRegsLast(true);
78*e8d8bef9SDimitry Andric 
79*e8d8bef9SDimitry Andric     // Return the register state back to how it was before, leaving any
80*e8d8bef9SDimitry Andric     // unallocated registers available for other smaller types.
81*e8d8bef9SDimitry Andric     for (int I = 0; I < 8; I++)
82*e8d8bef9SDimitry Andric       if (!RegsAllocated[I])
83*e8d8bef9SDimitry Andric         State.DeallocateReg(ZRegList[I]);
84*e8d8bef9SDimitry Andric 
85*e8d8bef9SDimitry Andric     // All pending members have now been allocated
86*e8d8bef9SDimitry Andric     PendingMembers.clear();
87*e8d8bef9SDimitry Andric     return true;
88*e8d8bef9SDimitry Andric   }
89*e8d8bef9SDimitry Andric 
900b57cec5SDimitry Andric   unsigned Size = LocVT.getSizeInBits() / 8;
918bcb0991SDimitry Andric   const Align StackAlign =
920b57cec5SDimitry Andric       State.getMachineFunction().getDataLayout().getStackAlignment();
935ffd83dbSDimitry Andric   const Align OrigAlign = ArgFlags.getNonZeroOrigAlign();
945ffd83dbSDimitry Andric   const Align Alignment = std::min(OrigAlign, StackAlign);
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   for (auto &It : PendingMembers) {
975ffd83dbSDimitry Andric     It.convertToMem(State.AllocateStack(Size, std::max(Alignment, SlotAlign)));
980b57cec5SDimitry Andric     State.addLoc(It);
995ffd83dbSDimitry Andric     SlotAlign = Align(1);
1000b57cec5SDimitry Andric   }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   // All pending members have now been allocated
1030b57cec5SDimitry Andric   PendingMembers.clear();
1040b57cec5SDimitry Andric   return true;
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric /// The Darwin variadic PCS places anonymous arguments in 8-byte stack slots. An
1080b57cec5SDimitry Andric /// [N x Ty] type must still be contiguous in memory though.
1090b57cec5SDimitry Andric static bool CC_AArch64_Custom_Stack_Block(
1100b57cec5SDimitry Andric       unsigned &ValNo, MVT &ValVT, MVT &LocVT, CCValAssign::LocInfo &LocInfo,
1110b57cec5SDimitry Andric       ISD::ArgFlagsTy &ArgFlags, CCState &State) {
1120b57cec5SDimitry Andric   SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   // Add the argument to the list to be allocated once we know the size of the
1150b57cec5SDimitry Andric   // block.
1160b57cec5SDimitry Andric   PendingMembers.push_back(
1170b57cec5SDimitry Andric       CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   if (!ArgFlags.isInConsecutiveRegsLast())
1200b57cec5SDimitry Andric     return true;
1210b57cec5SDimitry Andric 
1225ffd83dbSDimitry Andric   return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, Align(8));
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric /// Given an [N x Ty] block, it should be passed in a consecutive sequence of
1260b57cec5SDimitry Andric /// registers. If no such sequence is available, mark the rest of the registers
1270b57cec5SDimitry Andric /// of that type as used and place the argument on the stack.
1280b57cec5SDimitry Andric static bool CC_AArch64_Custom_Block(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1290b57cec5SDimitry Andric                                     CCValAssign::LocInfo &LocInfo,
1300b57cec5SDimitry Andric                                     ISD::ArgFlagsTy &ArgFlags, CCState &State) {
1318bcb0991SDimitry Andric   const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>(
1328bcb0991SDimitry Andric       State.getMachineFunction().getSubtarget());
1338bcb0991SDimitry Andric   bool IsDarwinILP32 = Subtarget.isTargetILP32() && Subtarget.isTargetMachO();
1348bcb0991SDimitry Andric 
1350b57cec5SDimitry Andric   // Try to allocate a contiguous block of registers, each of the correct
1360b57cec5SDimitry Andric   // size to hold one member.
1370b57cec5SDimitry Andric   ArrayRef<MCPhysReg> RegList;
1388bcb0991SDimitry Andric   if (LocVT.SimpleTy == MVT::i64 || (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32))
1390b57cec5SDimitry Andric     RegList = XRegList;
1400b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f16)
1410b57cec5SDimitry Andric     RegList = HRegList;
1420b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f32 || LocVT.is32BitVector())
1430b57cec5SDimitry Andric     RegList = SRegList;
1440b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f64 || LocVT.is64BitVector())
1450b57cec5SDimitry Andric     RegList = DRegList;
1460b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f128 || LocVT.is128BitVector())
1470b57cec5SDimitry Andric     RegList = QRegList;
148eaeb601bSDimitry Andric   else if (LocVT.isScalableVector())
149eaeb601bSDimitry Andric     RegList = ZRegList;
1500b57cec5SDimitry Andric   else {
1510b57cec5SDimitry Andric     // Not an array we want to split up after all.
1520b57cec5SDimitry Andric     return false;
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric   SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   // Add the argument to the list to be allocated once we know the size of the
1580b57cec5SDimitry Andric   // block.
1590b57cec5SDimitry Andric   PendingMembers.push_back(
1600b57cec5SDimitry Andric       CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   if (!ArgFlags.isInConsecutiveRegsLast())
1630b57cec5SDimitry Andric     return true;
1640b57cec5SDimitry Andric 
1658bcb0991SDimitry Andric   // [N x i32] arguments get packed into x-registers on Darwin's arm64_32
1668bcb0991SDimitry Andric   // because that's how the armv7k Clang front-end emits small structs.
1678bcb0991SDimitry Andric   unsigned EltsPerReg = (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32) ? 2 : 1;
1688bcb0991SDimitry Andric   unsigned RegResult = State.AllocateRegBlock(
1698bcb0991SDimitry Andric       RegList, alignTo(PendingMembers.size(), EltsPerReg) / EltsPerReg);
1708bcb0991SDimitry Andric   if (RegResult && EltsPerReg == 1) {
1710b57cec5SDimitry Andric     for (auto &It : PendingMembers) {
1720b57cec5SDimitry Andric       It.convertToReg(RegResult);
1730b57cec5SDimitry Andric       State.addLoc(It);
1740b57cec5SDimitry Andric       ++RegResult;
1750b57cec5SDimitry Andric     }
1760b57cec5SDimitry Andric     PendingMembers.clear();
1770b57cec5SDimitry Andric     return true;
1788bcb0991SDimitry Andric   } else if (RegResult) {
1798bcb0991SDimitry Andric     assert(EltsPerReg == 2 && "unexpected ABI");
1808bcb0991SDimitry Andric     bool UseHigh = false;
1818bcb0991SDimitry Andric     CCValAssign::LocInfo Info;
1828bcb0991SDimitry Andric     for (auto &It : PendingMembers) {
1838bcb0991SDimitry Andric       Info = UseHigh ? CCValAssign::AExtUpper : CCValAssign::ZExt;
1848bcb0991SDimitry Andric       State.addLoc(CCValAssign::getReg(It.getValNo(), MVT::i32, RegResult,
1858bcb0991SDimitry Andric                                        MVT::i64, Info));
1868bcb0991SDimitry Andric       UseHigh = !UseHigh;
1878bcb0991SDimitry Andric       if (!UseHigh)
1888bcb0991SDimitry Andric         ++RegResult;
1898bcb0991SDimitry Andric     }
1908bcb0991SDimitry Andric     PendingMembers.clear();
1918bcb0991SDimitry Andric     return true;
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric 
194*e8d8bef9SDimitry Andric   if (!LocVT.isScalableVector()) {
1950b57cec5SDimitry Andric     // Mark all regs in the class as unavailable
1960b57cec5SDimitry Andric     for (auto Reg : RegList)
1970b57cec5SDimitry Andric       State.AllocateReg(Reg);
198*e8d8bef9SDimitry Andric   }
1990b57cec5SDimitry Andric 
2005ffd83dbSDimitry Andric   const Align SlotAlign = Subtarget.isTargetDarwin() ? Align(1) : Align(8);
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, SlotAlign);
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric // TableGen provides definitions of the calling convention analysis entry
2060b57cec5SDimitry Andric // points.
2070b57cec5SDimitry Andric #include "AArch64GenCallingConv.inc"
208