10b57cec5SDimitry Andric //=== ARMCallingConv.cpp - ARM Custom CC Routines ---------------*- 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 custom routines for the ARM Calling Convention that 100b57cec5SDimitry Andric // aren't done by tablegen, and includes the table generated implementations. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "ARM.h" 150b57cec5SDimitry Andric #include "ARMCallingConv.h" 160b57cec5SDimitry Andric #include "ARMSubtarget.h" 170b57cec5SDimitry Andric #include "ARMRegisterInfo.h" 180b57cec5SDimitry Andric using namespace llvm; 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric // APCS f64 is in register pairs, possibly split to stack 210b57cec5SDimitry Andric static bool f64AssignAPCS(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 220b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, 230b57cec5SDimitry Andric CCState &State, bool CanFail) { 240b57cec5SDimitry Andric static const MCPhysReg RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 }; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric // Try to get the first register. 270b57cec5SDimitry Andric if (unsigned Reg = State.AllocateReg(RegList)) 280b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 290b57cec5SDimitry Andric else { 300b57cec5SDimitry Andric // For the 2nd half of a v2f64, do not fail. 310b57cec5SDimitry Andric if (CanFail) 320b57cec5SDimitry Andric return false; 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric // Put the whole thing on the stack. 350b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, 360b57cec5SDimitry Andric State.AllocateStack(8, 4), 370b57cec5SDimitry Andric LocVT, LocInfo)); 380b57cec5SDimitry Andric return true; 390b57cec5SDimitry Andric } 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric // Try to get the second register. 420b57cec5SDimitry Andric if (unsigned Reg = State.AllocateReg(RegList)) 430b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 440b57cec5SDimitry Andric else 450b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, 460b57cec5SDimitry Andric State.AllocateStack(4, 4), 470b57cec5SDimitry Andric LocVT, LocInfo)); 480b57cec5SDimitry Andric return true; 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 520b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, 530b57cec5SDimitry Andric ISD::ArgFlagsTy &ArgFlags, 540b57cec5SDimitry Andric CCState &State) { 550b57cec5SDimitry Andric if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true)) 560b57cec5SDimitry Andric return false; 570b57cec5SDimitry Andric if (LocVT == MVT::v2f64 && 580b57cec5SDimitry Andric !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false)) 590b57cec5SDimitry Andric return false; 600b57cec5SDimitry Andric return true; // we handled it 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric // AAPCS f64 is in aligned register pairs 640b57cec5SDimitry Andric static bool f64AssignAAPCS(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 650b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, 660b57cec5SDimitry Andric CCState &State, bool CanFail) { 670b57cec5SDimitry Andric static const MCPhysReg HiRegList[] = { ARM::R0, ARM::R2 }; 680b57cec5SDimitry Andric static const MCPhysReg LoRegList[] = { ARM::R1, ARM::R3 }; 690b57cec5SDimitry Andric static const MCPhysReg ShadowRegList[] = { ARM::R0, ARM::R1 }; 700b57cec5SDimitry Andric static const MCPhysReg GPRArgRegs[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 }; 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric unsigned Reg = State.AllocateReg(HiRegList, ShadowRegList); 730b57cec5SDimitry Andric if (Reg == 0) { 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric // If we had R3 unallocated only, now we still must to waste it. 760b57cec5SDimitry Andric Reg = State.AllocateReg(GPRArgRegs); 770b57cec5SDimitry Andric assert((!Reg || Reg == ARM::R3) && "Wrong GPRs usage for f64"); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric // For the 2nd half of a v2f64, do not just fail. 800b57cec5SDimitry Andric if (CanFail) 810b57cec5SDimitry Andric return false; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // Put the whole thing on the stack. 840b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, 850b57cec5SDimitry Andric State.AllocateStack(8, 8), 860b57cec5SDimitry Andric LocVT, LocInfo)); 870b57cec5SDimitry Andric return true; 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric unsigned i; 910b57cec5SDimitry Andric for (i = 0; i < 2; ++i) 920b57cec5SDimitry Andric if (HiRegList[i] == Reg) 930b57cec5SDimitry Andric break; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric unsigned T = State.AllocateReg(LoRegList[i]); 960b57cec5SDimitry Andric (void)T; 970b57cec5SDimitry Andric assert(T == LoRegList[i] && "Could not allocate register"); 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 1000b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i], 1010b57cec5SDimitry Andric LocVT, LocInfo)); 1020b57cec5SDimitry Andric return true; 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 1060b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, 1070b57cec5SDimitry Andric ISD::ArgFlagsTy &ArgFlags, 1080b57cec5SDimitry Andric CCState &State) { 1090b57cec5SDimitry Andric if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true)) 1100b57cec5SDimitry Andric return false; 1110b57cec5SDimitry Andric if (LocVT == MVT::v2f64 && 1120b57cec5SDimitry Andric !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false)) 1130b57cec5SDimitry Andric return false; 1140b57cec5SDimitry Andric return true; // we handled it 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric static bool f64RetAssign(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 1180b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, CCState &State) { 1190b57cec5SDimitry Andric static const MCPhysReg HiRegList[] = { ARM::R0, ARM::R2 }; 1200b57cec5SDimitry Andric static const MCPhysReg LoRegList[] = { ARM::R1, ARM::R3 }; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric unsigned Reg = State.AllocateReg(HiRegList, LoRegList); 1230b57cec5SDimitry Andric if (Reg == 0) 1240b57cec5SDimitry Andric return false; // we didn't handle it 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric unsigned i; 1270b57cec5SDimitry Andric for (i = 0; i < 2; ++i) 1280b57cec5SDimitry Andric if (HiRegList[i] == Reg) 1290b57cec5SDimitry Andric break; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 1320b57cec5SDimitry Andric State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i], 1330b57cec5SDimitry Andric LocVT, LocInfo)); 1340b57cec5SDimitry Andric return true; 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 1380b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, 1390b57cec5SDimitry Andric ISD::ArgFlagsTy &ArgFlags, 1400b57cec5SDimitry Andric CCState &State) { 1410b57cec5SDimitry Andric if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State)) 1420b57cec5SDimitry Andric return false; 1430b57cec5SDimitry Andric if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State)) 1440b57cec5SDimitry Andric return false; 1450b57cec5SDimitry Andric return true; // we handled it 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT, 1490b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, 1500b57cec5SDimitry Andric ISD::ArgFlagsTy &ArgFlags, 1510b57cec5SDimitry Andric CCState &State) { 1520b57cec5SDimitry Andric return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, 1530b57cec5SDimitry Andric State); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric static const MCPhysReg RRegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 }; 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric static const MCPhysReg SRegList[] = { ARM::S0, ARM::S1, ARM::S2, ARM::S3, 1590b57cec5SDimitry Andric ARM::S4, ARM::S5, ARM::S6, ARM::S7, 1600b57cec5SDimitry Andric ARM::S8, ARM::S9, ARM::S10, ARM::S11, 1610b57cec5SDimitry Andric ARM::S12, ARM::S13, ARM::S14, ARM::S15 }; 1620b57cec5SDimitry Andric static const MCPhysReg DRegList[] = { ARM::D0, ARM::D1, ARM::D2, ARM::D3, 1630b57cec5SDimitry Andric ARM::D4, ARM::D5, ARM::D6, ARM::D7 }; 1640b57cec5SDimitry Andric static const MCPhysReg QRegList[] = { ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3 }; 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric // Allocate part of an AAPCS HFA or HVA. We assume that each member of the HA 1680b57cec5SDimitry Andric // has InConsecutiveRegs set, and that the last member also has 1690b57cec5SDimitry Andric // InConsecutiveRegsLast set. We must process all members of the HA before 1700b57cec5SDimitry Andric // we can allocate it, as we need to know the total number of registers that 1710b57cec5SDimitry Andric // will be needed in order to (attempt to) allocate a contiguous block. 1720b57cec5SDimitry Andric static bool CC_ARM_AAPCS_Custom_Aggregate(unsigned &ValNo, MVT &ValVT, 1730b57cec5SDimitry Andric MVT &LocVT, 1740b57cec5SDimitry Andric CCValAssign::LocInfo &LocInfo, 1750b57cec5SDimitry Andric ISD::ArgFlagsTy &ArgFlags, 1760b57cec5SDimitry Andric CCState &State) { 1770b57cec5SDimitry Andric SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs(); 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric // AAPCS HFAs must have 1-4 elements, all of the same type 1800b57cec5SDimitry Andric if (PendingMembers.size() > 0) 1810b57cec5SDimitry Andric assert(PendingMembers[0].getLocVT() == LocVT); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric // Add the argument to the list to be allocated once we know the size of the 1840b57cec5SDimitry Andric // aggregate. Store the type's required alignmnent as extra info for later: in 1850b57cec5SDimitry Andric // the [N x i64] case all trace has been removed by the time we actually get 1860b57cec5SDimitry Andric // to do allocation. 1870b57cec5SDimitry Andric PendingMembers.push_back(CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo, 1880b57cec5SDimitry Andric ArgFlags.getOrigAlign())); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric if (!ArgFlags.isInConsecutiveRegsLast()) 1910b57cec5SDimitry Andric return true; 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric // Try to allocate a contiguous block of registers, each of the correct 1940b57cec5SDimitry Andric // size to hold one member. 1950b57cec5SDimitry Andric auto &DL = State.getMachineFunction().getDataLayout(); 196*8bcb0991SDimitry Andric unsigned StackAlign = DL.getStackAlignment().value(); 1970b57cec5SDimitry Andric unsigned Align = std::min(PendingMembers[0].getExtraInfo(), StackAlign); 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric ArrayRef<MCPhysReg> RegList; 2000b57cec5SDimitry Andric switch (LocVT.SimpleTy) { 2010b57cec5SDimitry Andric case MVT::i32: { 2020b57cec5SDimitry Andric RegList = RRegList; 2030b57cec5SDimitry Andric unsigned RegIdx = State.getFirstUnallocated(RegList); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // First consume all registers that would give an unaligned object. Whether 2060b57cec5SDimitry Andric // we go on stack or in regs, no-one will be using them in future. 2070b57cec5SDimitry Andric unsigned RegAlign = alignTo(Align, 4) / 4; 2080b57cec5SDimitry Andric while (RegIdx % RegAlign != 0 && RegIdx < RegList.size()) 2090b57cec5SDimitry Andric State.AllocateReg(RegList[RegIdx++]); 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric break; 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric case MVT::f16: 2140b57cec5SDimitry Andric case MVT::f32: 2150b57cec5SDimitry Andric RegList = SRegList; 2160b57cec5SDimitry Andric break; 2170b57cec5SDimitry Andric case MVT::v4f16: 2180b57cec5SDimitry Andric case MVT::f64: 2190b57cec5SDimitry Andric RegList = DRegList; 2200b57cec5SDimitry Andric break; 2210b57cec5SDimitry Andric case MVT::v8f16: 2220b57cec5SDimitry Andric case MVT::v2f64: 2230b57cec5SDimitry Andric RegList = QRegList; 2240b57cec5SDimitry Andric break; 2250b57cec5SDimitry Andric default: 2260b57cec5SDimitry Andric llvm_unreachable("Unexpected member type for block aggregate"); 2270b57cec5SDimitry Andric break; 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric unsigned RegResult = State.AllocateRegBlock(RegList, PendingMembers.size()); 2310b57cec5SDimitry Andric if (RegResult) { 2320b57cec5SDimitry Andric for (SmallVectorImpl<CCValAssign>::iterator It = PendingMembers.begin(); 2330b57cec5SDimitry Andric It != PendingMembers.end(); ++It) { 2340b57cec5SDimitry Andric It->convertToReg(RegResult); 2350b57cec5SDimitry Andric State.addLoc(*It); 2360b57cec5SDimitry Andric ++RegResult; 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric PendingMembers.clear(); 2390b57cec5SDimitry Andric return true; 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric // Register allocation failed, we'll be needing the stack 2430b57cec5SDimitry Andric unsigned Size = LocVT.getSizeInBits() / 8; 2440b57cec5SDimitry Andric if (LocVT == MVT::i32 && State.getNextStackOffset() == 0) { 2450b57cec5SDimitry Andric // If nothing else has used the stack until this point, a non-HFA aggregate 2460b57cec5SDimitry Andric // can be split between regs and stack. 2470b57cec5SDimitry Andric unsigned RegIdx = State.getFirstUnallocated(RegList); 2480b57cec5SDimitry Andric for (auto &It : PendingMembers) { 2490b57cec5SDimitry Andric if (RegIdx >= RegList.size()) 2500b57cec5SDimitry Andric It.convertToMem(State.AllocateStack(Size, Size)); 2510b57cec5SDimitry Andric else 2520b57cec5SDimitry Andric It.convertToReg(State.AllocateReg(RegList[RegIdx++])); 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric State.addLoc(It); 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric PendingMembers.clear(); 2570b57cec5SDimitry Andric return true; 2580b57cec5SDimitry Andric } else if (LocVT != MVT::i32) 2590b57cec5SDimitry Andric RegList = SRegList; 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric // Mark all regs as unavailable (AAPCS rule C.2.vfp for VFP, C.6 for core) 2620b57cec5SDimitry Andric for (auto Reg : RegList) 2630b57cec5SDimitry Andric State.AllocateReg(Reg); 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric // After the first item has been allocated, the rest are packed as tightly as 2660b57cec5SDimitry Andric // possible. (E.g. an incoming i64 would have starting Align of 8, but we'll 2670b57cec5SDimitry Andric // be allocating a bunch of i32 slots). 2680b57cec5SDimitry Andric unsigned RestAlign = std::min(Align, Size); 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric for (auto &It : PendingMembers) { 2710b57cec5SDimitry Andric It.convertToMem(State.AllocateStack(Size, Align)); 2720b57cec5SDimitry Andric State.addLoc(It); 2730b57cec5SDimitry Andric Align = RestAlign; 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric // All pending members have now been allocated 2770b57cec5SDimitry Andric PendingMembers.clear(); 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric // This will be allocated by the last member of the aggregate 2800b57cec5SDimitry Andric return true; 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric // Include the table generated calling convention implementations. 2840b57cec5SDimitry Andric #include "ARMGenCallingConv.inc" 285