Path: blob/main/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZCallingConv.h
35267 views
//===-- SystemZCallingConv.h - Calling conventions for SystemZ --*- 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//===----------------------------------------------------------------------===//78#ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZCALLINGCONV_H9#define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZCALLINGCONV_H1011#include "SystemZSubtarget.h"12#include "llvm/ADT/SmallVector.h"13#include "llvm/CodeGen/CallingConvLower.h"14#include "llvm/MC/MCRegisterInfo.h"1516namespace llvm {17namespace SystemZ {18const unsigned ELFNumArgGPRs = 5;19extern const MCPhysReg ELFArgGPRs[ELFNumArgGPRs];2021const unsigned ELFNumArgFPRs = 4;22extern const MCPhysReg ELFArgFPRs[ELFNumArgFPRs];2324const unsigned XPLINK64NumArgGPRs = 3;25extern const MCPhysReg XPLINK64ArgGPRs[XPLINK64NumArgGPRs];2627const unsigned XPLINK64NumArgFPRs = 4;28extern const MCPhysReg XPLINK64ArgFPRs[XPLINK64NumArgFPRs];29} // end namespace SystemZ3031class SystemZCCState : public CCState {32private:33/// Records whether the value was a fixed argument.34/// See ISD::OutputArg::IsFixed.35SmallVector<bool, 4> ArgIsFixed;3637/// Records whether the value was widened from a short vector type.38SmallVector<bool, 4> ArgIsShortVector;3940// Check whether ArgVT is a short vector type.41bool IsShortVectorType(EVT ArgVT) {42return ArgVT.isVector() && ArgVT.getStoreSize() <= 8;43}4445public:46SystemZCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,47SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)48: CCState(CC, isVarArg, MF, locs, C) {}4950void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,51CCAssignFn Fn) {52// Formal arguments are always fixed.53ArgIsFixed.clear();54for (unsigned i = 0; i < Ins.size(); ++i)55ArgIsFixed.push_back(true);56// Record whether the call operand was a short vector.57ArgIsShortVector.clear();58for (unsigned i = 0; i < Ins.size(); ++i)59ArgIsShortVector.push_back(IsShortVectorType(Ins[i].ArgVT));6061CCState::AnalyzeFormalArguments(Ins, Fn);62}6364void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,65CCAssignFn Fn) {66// Record whether the call operand was a fixed argument.67ArgIsFixed.clear();68for (unsigned i = 0; i < Outs.size(); ++i)69ArgIsFixed.push_back(Outs[i].IsFixed);70// Record whether the call operand was a short vector.71ArgIsShortVector.clear();72for (unsigned i = 0; i < Outs.size(); ++i)73ArgIsShortVector.push_back(IsShortVectorType(Outs[i].ArgVT));7475CCState::AnalyzeCallOperands(Outs, Fn);76}7778// This version of AnalyzeCallOperands in the base class is not usable79// since we must provide a means of accessing ISD::OutputArg::IsFixed.80void AnalyzeCallOperands(const SmallVectorImpl<MVT> &Outs,81SmallVectorImpl<ISD::ArgFlagsTy> &Flags,82CCAssignFn Fn) = delete;8384bool IsFixed(unsigned ValNo) { return ArgIsFixed[ValNo]; }85bool IsShortVector(unsigned ValNo) { return ArgIsShortVector[ValNo]; }86};8788// Handle i128 argument types. These need to be passed by implicit89// reference. This could be as simple as the following .td line:90// CCIfType<[i128], CCPassIndirect<i64>>,91// except that i128 is not a legal type, and therefore gets split by92// common code into a pair of i64 arguments.93inline bool CC_SystemZ_I128Indirect(unsigned &ValNo, MVT &ValVT,94MVT &LocVT,95CCValAssign::LocInfo &LocInfo,96ISD::ArgFlagsTy &ArgFlags,97CCState &State) {98SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();99100// ArgFlags.isSplit() is true on the first part of a i128 argument;101// PendingMembers.empty() is false on all subsequent parts.102if (!ArgFlags.isSplit() && PendingMembers.empty())103return false;104105// Push a pending Indirect value location for each part.106LocVT = MVT::i64;107LocInfo = CCValAssign::Indirect;108PendingMembers.push_back(CCValAssign::getPending(ValNo, ValVT,109LocVT, LocInfo));110if (!ArgFlags.isSplitEnd())111return true;112113// OK, we've collected all parts in the pending list. Allocate114// the location (register or stack slot) for the indirect pointer.115// (This duplicates the usual i64 calling convention rules.)116unsigned Reg;117const SystemZSubtarget &Subtarget =118State.getMachineFunction().getSubtarget<SystemZSubtarget>();119if (Subtarget.isTargetELF())120Reg = State.AllocateReg(SystemZ::ELFArgGPRs);121else if (Subtarget.isTargetXPLINK64())122Reg = State.AllocateReg(SystemZ::XPLINK64ArgGPRs);123else124llvm_unreachable("Unknown Calling Convention!");125126unsigned Offset = Reg && !Subtarget.isTargetXPLINK64()127? 0128: State.AllocateStack(8, Align(8));129130// Use that same location for all the pending parts.131for (auto &It : PendingMembers) {132if (Reg)133It.convertToReg(Reg);134else135It.convertToMem(Offset);136State.addLoc(It);137}138139PendingMembers.clear();140141return true;142}143144inline bool CC_XPLINK64_Shadow_Reg(unsigned &ValNo, MVT &ValVT, MVT &LocVT,145CCValAssign::LocInfo &LocInfo,146ISD::ArgFlagsTy &ArgFlags, CCState &State) {147if (LocVT == MVT::f32 || LocVT == MVT::f64) {148State.AllocateReg(SystemZ::XPLINK64ArgGPRs);149}150if (LocVT == MVT::f128 || LocVT.is128BitVector()) {151// Shadow next two GPRs, if available.152State.AllocateReg(SystemZ::XPLINK64ArgGPRs);153State.AllocateReg(SystemZ::XPLINK64ArgGPRs);154155// Quad precision floating point needs to156// go inside pre-defined FPR pair.157if (LocVT == MVT::f128) {158for (unsigned I = 0; I < SystemZ::XPLINK64NumArgFPRs; I += 2)159if (State.isAllocated(SystemZ::XPLINK64ArgFPRs[I]))160State.AllocateReg(SystemZ::XPLINK64ArgFPRs[I + 1]);161}162}163return false;164}165166inline bool CC_XPLINK64_Allocate128BitVararg(unsigned &ValNo, MVT &ValVT,167MVT &LocVT,168CCValAssign::LocInfo &LocInfo,169ISD::ArgFlagsTy &ArgFlags,170CCState &State) {171// For any C or C++ program, this should always be172// false, since it is illegal to have a function173// where the first argument is variadic. Therefore174// the first fixed argument should already have175// allocated GPR1 either through shadowing it or176// using it for parameter passing.177State.AllocateReg(SystemZ::R1D);178179bool AllocGPR2 = State.AllocateReg(SystemZ::R2D);180bool AllocGPR3 = State.AllocateReg(SystemZ::R3D);181182// If GPR2 and GPR3 are available, then we may pass vararg in R2Q.183// If only GPR3 is available, we need to set custom handling to copy184// hi bits into GPR3.185// Either way, we allocate on the stack.186if (AllocGPR3) {187// For f128 and vector var arg case, set the bitcast flag to bitcast to188// i128.189LocVT = MVT::i128;190LocInfo = CCValAssign::BCvt;191auto Offset = State.AllocateStack(16, Align(8));192if (AllocGPR2)193State.addLoc(194CCValAssign::getReg(ValNo, ValVT, SystemZ::R2Q, LocVT, LocInfo));195else196State.addLoc(197CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));198return true;199}200201return false;202}203204inline bool RetCC_SystemZ_Error(unsigned &, MVT &, MVT &,205CCValAssign::LocInfo &, ISD::ArgFlagsTy &,206CCState &) {207llvm_unreachable("Return value calling convention currently unsupported.");208}209210inline bool CC_SystemZ_Error(unsigned &, MVT &, MVT &, CCValAssign::LocInfo &,211ISD::ArgFlagsTy &, CCState &) {212llvm_unreachable("Argument calling convention currently unsupported.");213}214215inline bool CC_SystemZ_GHC_Error(unsigned &, MVT &, MVT &,216CCValAssign::LocInfo &, ISD::ArgFlagsTy &,217CCState &) {218report_fatal_error("No registers left in GHC calling convention");219return false;220}221222} // end namespace llvm223224#endif225226227