xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64CallingConvention.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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};
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric static bool finishStackBlock(SmallVectorImpl<CCValAssign> &PendingMembers,
400b57cec5SDimitry Andric                              MVT LocVT, ISD::ArgFlagsTy &ArgFlags,
41*5ffd83dbSDimitry Andric                              CCState &State, Align SlotAlign) {
420b57cec5SDimitry Andric   unsigned Size = LocVT.getSizeInBits() / 8;
438bcb0991SDimitry Andric   const Align StackAlign =
440b57cec5SDimitry Andric       State.getMachineFunction().getDataLayout().getStackAlignment();
45*5ffd83dbSDimitry Andric   const Align OrigAlign = ArgFlags.getNonZeroOrigAlign();
46*5ffd83dbSDimitry Andric   const Align Alignment = std::min(OrigAlign, StackAlign);
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   for (auto &It : PendingMembers) {
49*5ffd83dbSDimitry Andric     It.convertToMem(State.AllocateStack(Size, std::max(Alignment, SlotAlign)));
500b57cec5SDimitry Andric     State.addLoc(It);
51*5ffd83dbSDimitry Andric     SlotAlign = Align(1);
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   // All pending members have now been allocated
550b57cec5SDimitry Andric   PendingMembers.clear();
560b57cec5SDimitry Andric   return true;
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric /// The Darwin variadic PCS places anonymous arguments in 8-byte stack slots. An
600b57cec5SDimitry Andric /// [N x Ty] type must still be contiguous in memory though.
610b57cec5SDimitry Andric static bool CC_AArch64_Custom_Stack_Block(
620b57cec5SDimitry Andric       unsigned &ValNo, MVT &ValVT, MVT &LocVT, CCValAssign::LocInfo &LocInfo,
630b57cec5SDimitry Andric       ISD::ArgFlagsTy &ArgFlags, CCState &State) {
640b57cec5SDimitry Andric   SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   // Add the argument to the list to be allocated once we know the size of the
670b57cec5SDimitry Andric   // block.
680b57cec5SDimitry Andric   PendingMembers.push_back(
690b57cec5SDimitry Andric       CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   if (!ArgFlags.isInConsecutiveRegsLast())
720b57cec5SDimitry Andric     return true;
730b57cec5SDimitry Andric 
74*5ffd83dbSDimitry Andric   return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, Align(8));
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric /// Given an [N x Ty] block, it should be passed in a consecutive sequence of
780b57cec5SDimitry Andric /// registers. If no such sequence is available, mark the rest of the registers
790b57cec5SDimitry Andric /// of that type as used and place the argument on the stack.
800b57cec5SDimitry Andric static bool CC_AArch64_Custom_Block(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
810b57cec5SDimitry Andric                                     CCValAssign::LocInfo &LocInfo,
820b57cec5SDimitry Andric                                     ISD::ArgFlagsTy &ArgFlags, CCState &State) {
838bcb0991SDimitry Andric   const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>(
848bcb0991SDimitry Andric       State.getMachineFunction().getSubtarget());
858bcb0991SDimitry Andric   bool IsDarwinILP32 = Subtarget.isTargetILP32() && Subtarget.isTargetMachO();
868bcb0991SDimitry Andric 
870b57cec5SDimitry Andric   // Try to allocate a contiguous block of registers, each of the correct
880b57cec5SDimitry Andric   // size to hold one member.
890b57cec5SDimitry Andric   ArrayRef<MCPhysReg> RegList;
908bcb0991SDimitry Andric   if (LocVT.SimpleTy == MVT::i64 || (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32))
910b57cec5SDimitry Andric     RegList = XRegList;
920b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f16)
930b57cec5SDimitry Andric     RegList = HRegList;
940b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f32 || LocVT.is32BitVector())
950b57cec5SDimitry Andric     RegList = SRegList;
960b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f64 || LocVT.is64BitVector())
970b57cec5SDimitry Andric     RegList = DRegList;
980b57cec5SDimitry Andric   else if (LocVT.SimpleTy == MVT::f128 || LocVT.is128BitVector())
990b57cec5SDimitry Andric     RegList = QRegList;
1000b57cec5SDimitry Andric   else {
1010b57cec5SDimitry Andric     // Not an array we want to split up after all.
1020b57cec5SDimitry Andric     return false;
1030b57cec5SDimitry Andric   }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   // Add the argument to the list to be allocated once we know the size of the
1080b57cec5SDimitry Andric   // block.
1090b57cec5SDimitry Andric   PendingMembers.push_back(
1100b57cec5SDimitry Andric       CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   if (!ArgFlags.isInConsecutiveRegsLast())
1130b57cec5SDimitry Andric     return true;
1140b57cec5SDimitry Andric 
1158bcb0991SDimitry Andric   // [N x i32] arguments get packed into x-registers on Darwin's arm64_32
1168bcb0991SDimitry Andric   // because that's how the armv7k Clang front-end emits small structs.
1178bcb0991SDimitry Andric   unsigned EltsPerReg = (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32) ? 2 : 1;
1188bcb0991SDimitry Andric   unsigned RegResult = State.AllocateRegBlock(
1198bcb0991SDimitry Andric       RegList, alignTo(PendingMembers.size(), EltsPerReg) / EltsPerReg);
1208bcb0991SDimitry Andric   if (RegResult && EltsPerReg == 1) {
1210b57cec5SDimitry Andric     for (auto &It : PendingMembers) {
1220b57cec5SDimitry Andric       It.convertToReg(RegResult);
1230b57cec5SDimitry Andric       State.addLoc(It);
1240b57cec5SDimitry Andric       ++RegResult;
1250b57cec5SDimitry Andric     }
1260b57cec5SDimitry Andric     PendingMembers.clear();
1270b57cec5SDimitry Andric     return true;
1288bcb0991SDimitry Andric   } else if (RegResult) {
1298bcb0991SDimitry Andric     assert(EltsPerReg == 2 && "unexpected ABI");
1308bcb0991SDimitry Andric     bool UseHigh = false;
1318bcb0991SDimitry Andric     CCValAssign::LocInfo Info;
1328bcb0991SDimitry Andric     for (auto &It : PendingMembers) {
1338bcb0991SDimitry Andric       Info = UseHigh ? CCValAssign::AExtUpper : CCValAssign::ZExt;
1348bcb0991SDimitry Andric       State.addLoc(CCValAssign::getReg(It.getValNo(), MVT::i32, RegResult,
1358bcb0991SDimitry Andric                                        MVT::i64, Info));
1368bcb0991SDimitry Andric       UseHigh = !UseHigh;
1378bcb0991SDimitry Andric       if (!UseHigh)
1388bcb0991SDimitry Andric         ++RegResult;
1398bcb0991SDimitry Andric     }
1408bcb0991SDimitry Andric     PendingMembers.clear();
1418bcb0991SDimitry Andric     return true;
1420b57cec5SDimitry Andric   }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   // Mark all regs in the class as unavailable
1450b57cec5SDimitry Andric   for (auto Reg : RegList)
1460b57cec5SDimitry Andric     State.AllocateReg(Reg);
1470b57cec5SDimitry Andric 
148*5ffd83dbSDimitry Andric   const Align SlotAlign = Subtarget.isTargetDarwin() ? Align(1) : Align(8);
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, SlotAlign);
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric // TableGen provides definitions of the calling convention analysis entry
1540b57cec5SDimitry Andric // points.
1550b57cec5SDimitry Andric #include "AArch64GenCallingConv.inc"
156