Path: blob/main/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64CallingConvention.cpp
35267 views
//=== AArch64CallingConvention.cpp - AArch64 CC impl ------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the table-generated and custom routines for the AArch649// Calling Convention.10//11//===----------------------------------------------------------------------===//1213#include "AArch64CallingConvention.h"14#include "AArch64.h"15#include "AArch64InstrInfo.h"16#include "AArch64Subtarget.h"17#include "llvm/CodeGen/CallingConvLower.h"18#include "llvm/CodeGen/TargetInstrInfo.h"19#include "llvm/IR/CallingConv.h"20using namespace llvm;2122static const MCPhysReg XRegList[] = {AArch64::X0, AArch64::X1, AArch64::X2,23AArch64::X3, AArch64::X4, AArch64::X5,24AArch64::X6, AArch64::X7};25static const MCPhysReg HRegList[] = {AArch64::H0, AArch64::H1, AArch64::H2,26AArch64::H3, AArch64::H4, AArch64::H5,27AArch64::H6, AArch64::H7};28static const MCPhysReg SRegList[] = {AArch64::S0, AArch64::S1, AArch64::S2,29AArch64::S3, AArch64::S4, AArch64::S5,30AArch64::S6, AArch64::S7};31static const MCPhysReg DRegList[] = {AArch64::D0, AArch64::D1, AArch64::D2,32AArch64::D3, AArch64::D4, AArch64::D5,33AArch64::D6, AArch64::D7};34static const MCPhysReg QRegList[] = {AArch64::Q0, AArch64::Q1, AArch64::Q2,35AArch64::Q3, AArch64::Q4, AArch64::Q5,36AArch64::Q6, AArch64::Q7};37static const MCPhysReg ZRegList[] = {AArch64::Z0, AArch64::Z1, AArch64::Z2,38AArch64::Z3, AArch64::Z4, AArch64::Z5,39AArch64::Z6, AArch64::Z7};40static const MCPhysReg PRegList[] = {AArch64::P0, AArch64::P1, AArch64::P2,41AArch64::P3};4243static bool finishStackBlock(SmallVectorImpl<CCValAssign> &PendingMembers,44MVT LocVT, ISD::ArgFlagsTy &ArgFlags,45CCState &State, Align SlotAlign) {46if (LocVT.isScalableVector()) {47const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>(48State.getMachineFunction().getSubtarget());49const AArch64TargetLowering *TLI = Subtarget.getTargetLowering();5051// We are about to reinvoke the CCAssignFn auto-generated handler. If we52// don't unset these flags we will get stuck in an infinite loop forever53// invoking the custom handler.54ArgFlags.setInConsecutiveRegs(false);55ArgFlags.setInConsecutiveRegsLast(false);5657// The calling convention for passing SVE tuples states that in the event58// we cannot allocate enough registers for the tuple we should still leave59// any remaining registers unallocated. However, when we call the60// CCAssignFn again we want it to behave as if all remaining registers are61// allocated. This will force the code to pass the tuple indirectly in62// accordance with the PCS.63bool ZRegsAllocated[8];64for (int I = 0; I < 8; I++) {65ZRegsAllocated[I] = State.isAllocated(ZRegList[I]);66State.AllocateReg(ZRegList[I]);67}68// The same applies to P registers.69bool PRegsAllocated[4];70for (int I = 0; I < 4; I++) {71PRegsAllocated[I] = State.isAllocated(PRegList[I]);72State.AllocateReg(PRegList[I]);73}7475auto &It = PendingMembers[0];76CCAssignFn *AssignFn =77TLI->CCAssignFnForCall(State.getCallingConv(), /*IsVarArg=*/false);78if (AssignFn(It.getValNo(), It.getValVT(), It.getValVT(), CCValAssign::Full,79ArgFlags, State))80llvm_unreachable("Call operand has unhandled type");8182// Return the flags to how they were before.83ArgFlags.setInConsecutiveRegs(true);84ArgFlags.setInConsecutiveRegsLast(true);8586// Return the register state back to how it was before, leaving any87// unallocated registers available for other smaller types.88for (int I = 0; I < 8; I++)89if (!ZRegsAllocated[I])90State.DeallocateReg(ZRegList[I]);91for (int I = 0; I < 4; I++)92if (!PRegsAllocated[I])93State.DeallocateReg(PRegList[I]);9495// All pending members have now been allocated96PendingMembers.clear();97return true;98}99100unsigned Size = LocVT.getSizeInBits() / 8;101for (auto &It : PendingMembers) {102It.convertToMem(State.AllocateStack(Size, SlotAlign));103State.addLoc(It);104SlotAlign = Align(1);105}106107// All pending members have now been allocated108PendingMembers.clear();109return true;110}111112/// The Darwin variadic PCS places anonymous arguments in 8-byte stack slots. An113/// [N x Ty] type must still be contiguous in memory though.114static bool CC_AArch64_Custom_Stack_Block(115unsigned &ValNo, MVT &ValVT, MVT &LocVT, CCValAssign::LocInfo &LocInfo,116ISD::ArgFlagsTy &ArgFlags, CCState &State) {117SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();118119// Add the argument to the list to be allocated once we know the size of the120// block.121PendingMembers.push_back(122CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));123124if (!ArgFlags.isInConsecutiveRegsLast())125return true;126127return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, Align(8));128}129130/// Given an [N x Ty] block, it should be passed in a consecutive sequence of131/// registers. If no such sequence is available, mark the rest of the registers132/// of that type as used and place the argument on the stack.133static bool CC_AArch64_Custom_Block(unsigned &ValNo, MVT &ValVT, MVT &LocVT,134CCValAssign::LocInfo &LocInfo,135ISD::ArgFlagsTy &ArgFlags, CCState &State) {136const AArch64Subtarget &Subtarget = static_cast<const AArch64Subtarget &>(137State.getMachineFunction().getSubtarget());138bool IsDarwinILP32 = Subtarget.isTargetILP32() && Subtarget.isTargetMachO();139140// Try to allocate a contiguous block of registers, each of the correct141// size to hold one member.142ArrayRef<MCPhysReg> RegList;143if (LocVT.SimpleTy == MVT::i64 || (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32))144RegList = XRegList;145else if (LocVT.SimpleTy == MVT::f16)146RegList = HRegList;147else if (LocVT.SimpleTy == MVT::f32 || LocVT.is32BitVector())148RegList = SRegList;149else if (LocVT.SimpleTy == MVT::f64 || LocVT.is64BitVector())150RegList = DRegList;151else if (LocVT.SimpleTy == MVT::f128 || LocVT.is128BitVector())152RegList = QRegList;153else if (LocVT.isScalableVector()) {154// Scalable masks should be pass by Predicate registers.155if (LocVT == MVT::nxv1i1 || LocVT == MVT::nxv2i1 || LocVT == MVT::nxv4i1 ||156LocVT == MVT::nxv8i1 || LocVT == MVT::nxv16i1 ||157LocVT == MVT::aarch64svcount)158RegList = PRegList;159else160RegList = ZRegList;161} else {162// Not an array we want to split up after all.163return false;164}165166SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();167168// Add the argument to the list to be allocated once we know the size of the169// block.170PendingMembers.push_back(171CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));172173if (!ArgFlags.isInConsecutiveRegsLast())174return true;175176// [N x i32] arguments get packed into x-registers on Darwin's arm64_32177// because that's how the armv7k Clang front-end emits small structs.178unsigned EltsPerReg = (IsDarwinILP32 && LocVT.SimpleTy == MVT::i32) ? 2 : 1;179unsigned RegResult = State.AllocateRegBlock(180RegList, alignTo(PendingMembers.size(), EltsPerReg) / EltsPerReg);181if (RegResult && EltsPerReg == 1) {182for (auto &It : PendingMembers) {183It.convertToReg(RegResult);184State.addLoc(It);185++RegResult;186}187PendingMembers.clear();188return true;189} else if (RegResult) {190assert(EltsPerReg == 2 && "unexpected ABI");191bool UseHigh = false;192CCValAssign::LocInfo Info;193for (auto &It : PendingMembers) {194Info = UseHigh ? CCValAssign::AExtUpper : CCValAssign::ZExt;195State.addLoc(CCValAssign::getReg(It.getValNo(), MVT::i32, RegResult,196MVT::i64, Info));197UseHigh = !UseHigh;198if (!UseHigh)199++RegResult;200}201PendingMembers.clear();202return true;203}204205if (!LocVT.isScalableVector()) {206// Mark all regs in the class as unavailable207for (auto Reg : RegList)208State.AllocateReg(Reg);209}210211const Align StackAlign =212State.getMachineFunction().getDataLayout().getStackAlignment();213const Align MemAlign = ArgFlags.getNonZeroMemAlign();214Align SlotAlign = std::min(MemAlign, StackAlign);215if (!Subtarget.isTargetDarwin())216SlotAlign = std::max(SlotAlign, Align(8));217218return finishStackBlock(PendingMembers, LocVT, ArgFlags, State, SlotAlign);219}220221// TableGen provides definitions of the calling convention analysis entry222// points.223#include "AArch64GenCallingConv.inc"224225226