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) { 45e8d8bef9SDimitry Andric if (LocVT.isScalableVector()) { 46e8d8bef9SDimitry Andric const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>( 47e8d8bef9SDimitry Andric State.getMachineFunction().getSubtarget()); 48e8d8bef9SDimitry Andric const AArch64TargetLowering *TLI = Subtarget.getTargetLowering(); 49e8d8bef9SDimitry Andric 50e8d8bef9SDimitry Andric // We are about to reinvoke the CCAssignFn auto-generated handler. If we 51e8d8bef9SDimitry Andric // don't unset these flags we will get stuck in an infinite loop forever 52e8d8bef9SDimitry Andric // invoking the custom handler. 53e8d8bef9SDimitry Andric ArgFlags.setInConsecutiveRegs(false); 54e8d8bef9SDimitry Andric ArgFlags.setInConsecutiveRegsLast(false); 55e8d8bef9SDimitry Andric 56e8d8bef9SDimitry Andric // The calling convention for passing SVE tuples states that in the event 57e8d8bef9SDimitry Andric // we cannot allocate enough registers for the tuple we should still leave 58e8d8bef9SDimitry Andric // any remaining registers unallocated. However, when we call the 59e8d8bef9SDimitry Andric // CCAssignFn again we want it to behave as if all remaining registers are 60e8d8bef9SDimitry Andric // allocated. This will force the code to pass the tuple indirectly in 61e8d8bef9SDimitry Andric // accordance with the PCS. 62e8d8bef9SDimitry Andric bool RegsAllocated[8]; 63e8d8bef9SDimitry Andric for (int I = 0; I < 8; I++) { 64e8d8bef9SDimitry Andric RegsAllocated[I] = State.isAllocated(ZRegList[I]); 65e8d8bef9SDimitry Andric State.AllocateReg(ZRegList[I]); 66e8d8bef9SDimitry Andric } 67e8d8bef9SDimitry Andric 68e8d8bef9SDimitry Andric auto &It = PendingMembers[0]; 69e8d8bef9SDimitry Andric CCAssignFn *AssignFn = 70e8d8bef9SDimitry Andric TLI->CCAssignFnForCall(State.getCallingConv(), /*IsVarArg=*/false); 71e8d8bef9SDimitry Andric if (AssignFn(It.getValNo(), It.getValVT(), It.getValVT(), CCValAssign::Full, 72e8d8bef9SDimitry Andric ArgFlags, State)) 73e8d8bef9SDimitry Andric llvm_unreachable("Call operand has unhandled type"); 74e8d8bef9SDimitry Andric 75e8d8bef9SDimitry Andric // Return the flags to how they were before. 76e8d8bef9SDimitry Andric ArgFlags.setInConsecutiveRegs(true); 77e8d8bef9SDimitry Andric ArgFlags.setInConsecutiveRegsLast(true); 78e8d8bef9SDimitry Andric 79e8d8bef9SDimitry Andric // Return the register state back to how it was before, leaving any 80e8d8bef9SDimitry Andric // unallocated registers available for other smaller types. 81e8d8bef9SDimitry Andric for (int I = 0; I < 8; I++) 82e8d8bef9SDimitry Andric if (!RegsAllocated[I]) 83e8d8bef9SDimitry Andric State.DeallocateReg(ZRegList[I]); 84e8d8bef9SDimitry Andric 85e8d8bef9SDimitry Andric // All pending members have now been allocated 86e8d8bef9SDimitry Andric PendingMembers.clear(); 87e8d8bef9SDimitry Andric return true; 88e8d8bef9SDimitry Andric } 89e8d8bef9SDimitry Andric 900b57cec5SDimitry Andric unsigned Size = LocVT.getSizeInBits() / 8; 910b57cec5SDimitry Andric for (auto &It : PendingMembers) { 92*fe6060f1SDimitry Andric It.convertToMem(State.AllocateStack(Size, SlotAlign)); 930b57cec5SDimitry Andric State.addLoc(It); 945ffd83dbSDimitry Andric SlotAlign = Align(1); 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric // All pending members have now been allocated 980b57cec5SDimitry Andric PendingMembers.clear(); 990b57cec5SDimitry Andric return true; 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric /// The Darwin variadic PCS places anonymous arguments in 8-byte stack slots. An 1030b57cec5SDimitry Andric /// [N x Ty] type must still be contiguous in memory though. 1040b57cec5SDimitry Andric static bool CC_AArch64_Custom_Stack_Block( 1050b57cec5SDimitry Andric unsigned &ValNo, MVT &ValVT, MVT &LocVT, CCValAssign::LocInfo &LocInfo, 1060b57cec5SDimitry Andric ISD::ArgFlagsTy &ArgFlags, CCState &State) { 1070b57cec5SDimitry Andric SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs(); 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric // Add the argument to the list to be allocated once we know the size of the 1100b57cec5SDimitry Andric // block. 1110b57cec5SDimitry Andric PendingMembers.push_back( 1120b57cec5SDimitry Andric CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo)); 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric if (!ArgFlags.isInConsecutiveRegsLast()) 1150b57cec5SDimitry Andric return true; 1160b57cec5SDimitry Andric 1175ffd83dbSDimitry Andric return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, Align(8)); 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric /// Given an [N x Ty] block, it should be passed in a consecutive sequence of 1210b57cec5SDimitry Andric /// registers. If no such sequence is available, mark the rest of the registers 1220b57cec5SDimitry Andric /// of that type as used and place the argument on the stack. 1230b57cec5SDimitry Andric static bool CC_AArch64_Custom_Block(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 1240b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, 1250b57cec5SDimitry Andric ISD::ArgFlagsTy &ArgFlags, CCState &State) { 1268bcb0991SDimitry Andric const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>( 1278bcb0991SDimitry Andric State.getMachineFunction().getSubtarget()); 1288bcb0991SDimitry Andric bool IsDarwinILP32 = Subtarget.isTargetILP32() && Subtarget.isTargetMachO(); 1298bcb0991SDimitry Andric 1300b57cec5SDimitry Andric // Try to allocate a contiguous block of registers, each of the correct 1310b57cec5SDimitry Andric // size to hold one member. 1320b57cec5SDimitry Andric ArrayRef<MCPhysReg> RegList; 1338bcb0991SDimitry Andric if (LocVT.SimpleTy == MVT::i64 || (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32)) 1340b57cec5SDimitry Andric RegList = XRegList; 1350b57cec5SDimitry Andric else if (LocVT.SimpleTy == MVT::f16) 1360b57cec5SDimitry Andric RegList = HRegList; 1370b57cec5SDimitry Andric else if (LocVT.SimpleTy == MVT::f32 || LocVT.is32BitVector()) 1380b57cec5SDimitry Andric RegList = SRegList; 1390b57cec5SDimitry Andric else if (LocVT.SimpleTy == MVT::f64 || LocVT.is64BitVector()) 1400b57cec5SDimitry Andric RegList = DRegList; 1410b57cec5SDimitry Andric else if (LocVT.SimpleTy == MVT::f128 || LocVT.is128BitVector()) 1420b57cec5SDimitry Andric RegList = QRegList; 143eaeb601bSDimitry Andric else if (LocVT.isScalableVector()) 144eaeb601bSDimitry Andric RegList = ZRegList; 1450b57cec5SDimitry Andric else { 1460b57cec5SDimitry Andric // Not an array we want to split up after all. 1470b57cec5SDimitry Andric return false; 1480b57cec5SDimitry Andric } 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs(); 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric // Add the argument to the list to be allocated once we know the size of the 1530b57cec5SDimitry Andric // block. 1540b57cec5SDimitry Andric PendingMembers.push_back( 1550b57cec5SDimitry Andric CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo)); 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric if (!ArgFlags.isInConsecutiveRegsLast()) 1580b57cec5SDimitry Andric return true; 1590b57cec5SDimitry Andric 1608bcb0991SDimitry Andric // [N x i32] arguments get packed into x-registers on Darwin's arm64_32 1618bcb0991SDimitry Andric // because that's how the armv7k Clang front-end emits small structs. 1628bcb0991SDimitry Andric unsigned EltsPerReg = (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32) ? 2 : 1; 1638bcb0991SDimitry Andric unsigned RegResult = State.AllocateRegBlock( 1648bcb0991SDimitry Andric RegList, alignTo(PendingMembers.size(), EltsPerReg) / EltsPerReg); 1658bcb0991SDimitry Andric if (RegResult && EltsPerReg == 1) { 1660b57cec5SDimitry Andric for (auto &It : PendingMembers) { 1670b57cec5SDimitry Andric It.convertToReg(RegResult); 1680b57cec5SDimitry Andric State.addLoc(It); 1690b57cec5SDimitry Andric ++RegResult; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric PendingMembers.clear(); 1720b57cec5SDimitry Andric return true; 1738bcb0991SDimitry Andric } else if (RegResult) { 1748bcb0991SDimitry Andric assert(EltsPerReg == 2 && "unexpected ABI"); 1758bcb0991SDimitry Andric bool UseHigh = false; 1768bcb0991SDimitry Andric CCValAssign::LocInfo Info; 1778bcb0991SDimitry Andric for (auto &It : PendingMembers) { 1788bcb0991SDimitry Andric Info = UseHigh ? CCValAssign::AExtUpper : CCValAssign::ZExt; 1798bcb0991SDimitry Andric State.addLoc(CCValAssign::getReg(It.getValNo(), MVT::i32, RegResult, 1808bcb0991SDimitry Andric MVT::i64, Info)); 1818bcb0991SDimitry Andric UseHigh = !UseHigh; 1828bcb0991SDimitry Andric if (!UseHigh) 1838bcb0991SDimitry Andric ++RegResult; 1848bcb0991SDimitry Andric } 1858bcb0991SDimitry Andric PendingMembers.clear(); 1868bcb0991SDimitry Andric return true; 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric 189e8d8bef9SDimitry Andric if (!LocVT.isScalableVector()) { 1900b57cec5SDimitry Andric // Mark all regs in the class as unavailable 1910b57cec5SDimitry Andric for (auto Reg : RegList) 1920b57cec5SDimitry Andric State.AllocateReg(Reg); 193e8d8bef9SDimitry Andric } 1940b57cec5SDimitry Andric 195*fe6060f1SDimitry Andric const Align StackAlign = 196*fe6060f1SDimitry Andric State.getMachineFunction().getDataLayout().getStackAlignment(); 197*fe6060f1SDimitry Andric const Align MemAlign = ArgFlags.getNonZeroMemAlign(); 198*fe6060f1SDimitry Andric Align SlotAlign = std::min(MemAlign, StackAlign); 199*fe6060f1SDimitry Andric if (!Subtarget.isTargetDarwin()) 200*fe6060f1SDimitry Andric SlotAlign = std::max(SlotAlign, 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