| 1 | //===----- ABIInfo.h - ABI information access & encapsulation ---*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_CLANG_LIB_CODEGEN_ABIINFO_H |
| 10 | #define LLVM_CLANG_LIB_CODEGEN_ABIINFO_H |
| 11 | |
| 12 | #include "clang/AST/Attr.h" |
| 13 | #include "clang/AST/CharUnits.h" |
| 14 | #include "clang/AST/Type.h" |
| 15 | #include "llvm/IR/CallingConv.h" |
| 16 | #include "llvm/IR/Type.h" |
| 17 | |
| 18 | namespace llvm { |
| 19 | class Value; |
| 20 | class LLVMContext; |
| 21 | class DataLayout; |
| 22 | class Type; |
| 23 | class FixedVectorType; |
| 24 | } // namespace llvm |
| 25 | |
| 26 | namespace clang { |
| 27 | class ASTContext; |
| 28 | class CodeGenOptions; |
| 29 | class FunctionDecl; |
| 30 | class TargetInfo; |
| 31 | |
| 32 | namespace CodeGen { |
| 33 | class ABIArgInfo; |
| 34 | class Address; |
| 35 | class CGCXXABI; |
| 36 | class CGFunctionInfo; |
| 37 | class CodeGenFunction; |
| 38 | class CodeGenTypes; |
| 39 | class RValue; |
| 40 | class AggValueSlot; |
| 41 | |
| 42 | // FIXME: All of this stuff should be part of the target interface |
| 43 | // somehow. It is currently here because it is not clear how to factor |
| 44 | // the targets to support this, since the Targets currently live in a |
| 45 | // layer below types n'stuff. |
| 46 | |
| 47 | /// ABIInfo - Target specific hooks for defining how a type should be |
| 48 | /// passed or returned from functions. |
| 49 | class ABIInfo { |
| 50 | protected: |
| 51 | CodeGen::CodeGenTypes &CGT; |
| 52 | llvm::CallingConv::ID RuntimeCC; |
| 53 | |
| 54 | public: |
| 55 | ABIInfo(CodeGen::CodeGenTypes &cgt) |
| 56 | : CGT(cgt), RuntimeCC(llvm::CallingConv::C) {} |
| 57 | |
| 58 | virtual ~ABIInfo(); |
| 59 | |
| 60 | virtual bool allowBFloatArgsAndRet() const { return false; } |
| 61 | |
| 62 | CodeGen::CGCXXABI &getCXXABI() const; |
| 63 | ASTContext &getContext() const; |
| 64 | llvm::LLVMContext &getVMContext() const; |
| 65 | const llvm::DataLayout &getDataLayout() const; |
| 66 | const TargetInfo &getTarget() const; |
| 67 | const CodeGenOptions &getCodeGenOpts() const; |
| 68 | |
| 69 | /// Return the calling convention to use for system runtime |
| 70 | /// functions. |
| 71 | llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; } |
| 72 | |
| 73 | // Get X86ABIAVXLevel for the given FunctionDecl and ExtInfo. |
| 74 | // This can be different than the global / module level X86ABIAVXLevel |
| 75 | // due to function attributes. |
| 76 | virtual unsigned getX86ABIAVXLevel(const FunctionDecl *, |
| 77 | const FunctionType::ExtInfo &) const { |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0; |
| 82 | |
| 83 | /// EmitVAArg - Emit the target dependent code to load a value of |
| 84 | /// \arg Ty from the va_list pointed to by \arg VAListAddr. |
| 85 | |
| 86 | // FIXME: This is a gaping layering violation if we wanted to drop |
| 87 | // the ABI information any lower than CodeGen. Of course, for |
| 88 | // VAArg handling it has to be at this level; there is no way to |
| 89 | // abstract this out. |
| 90 | virtual RValue EmitVAArg(CodeGen::CodeGenFunction &CGF, |
| 91 | CodeGen::Address VAListAddr, QualType Ty, |
| 92 | AggValueSlot Slot) const = 0; |
| 93 | |
| 94 | bool isAndroid() const; |
| 95 | bool isOHOSFamily() const; |
| 96 | |
| 97 | /// Emit the target dependent code to load a value of |
| 98 | /// \arg Ty from the \c __builtin_ms_va_list pointed to by \arg VAListAddr. |
| 99 | virtual RValue EmitMSVAArg(CodeGen::CodeGenFunction &CGF, |
| 100 | CodeGen::Address VAListAddr, QualType Ty, |
| 101 | AggValueSlot Slot) const; |
| 102 | |
| 103 | /// Emit the target dependent code to load a value of |
| 104 | /// \arg Ty from the \c __builtin_zos_va_list pointed to by \arg VAListAddr. |
| 105 | virtual RValue EmitZOSVAArg(CodeGen::CodeGenFunction &CGF, |
| 106 | CodeGen::Address VAListAddr, QualType Ty, |
| 107 | AggValueSlot Slot) const; |
| 108 | |
| 109 | virtual bool isHomogeneousAggregateBaseType(QualType Ty) const; |
| 110 | |
| 111 | virtual bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 112 | uint64_t Members) const; |
| 113 | virtual bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const; |
| 114 | |
| 115 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 116 | /// aggregate. Base is set to the base element type, and Members is set |
| 117 | /// to the number of base elements. |
| 118 | bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 119 | uint64_t &Members) const; |
| 120 | |
| 121 | // Implement the Type::IsPromotableIntegerType for ABI specific needs. The |
| 122 | // only difference is that this considers bit-precise integer types as well. |
| 123 | bool isPromotableIntegerTypeForABI(QualType Ty) const; |
| 124 | |
| 125 | /// A convenience method to return an indirect ABIArgInfo with an |
| 126 | /// expected alignment equal to the ABI alignment of the given type. |
| 127 | CodeGen::ABIArgInfo |
| 128 | getNaturalAlignIndirect(QualType Ty, unsigned AddrSpace, bool ByVal = true, |
| 129 | bool Realign = false, |
| 130 | llvm::Type *Padding = nullptr) const; |
| 131 | |
| 132 | CodeGen::ABIArgInfo getNaturalAlignIndirectInReg(QualType Ty, |
| 133 | bool Realign = false) const; |
| 134 | |
| 135 | virtual void appendAttributeMangling(TargetAttr *Attr, |
| 136 | raw_ostream &Out) const; |
| 137 | virtual void appendAttributeMangling(TargetVersionAttr *Attr, |
| 138 | raw_ostream &Out) const; |
| 139 | virtual void appendAttributeMangling(TargetClonesAttr *Attr, unsigned Index, |
| 140 | raw_ostream &Out) const; |
| 141 | virtual void appendAttributeMangling(StringRef AttrStr, |
| 142 | raw_ostream &Out) const; |
| 143 | |
| 144 | /// Returns the optimal vector memory type based on the given vector type. For |
| 145 | /// example, on certain targets, a vector with 3 elements might be promoted to |
| 146 | /// one with 4 elements to improve performance. |
| 147 | virtual llvm::FixedVectorType * |
| 148 | getOptimalVectorMemoryType(llvm::FixedVectorType *T, |
| 149 | const LangOptions &Opt) const; |
| 150 | |
| 151 | virtual llvm::Value *createCoercedLoad(Address SrcAddr, const ABIArgInfo &AI, |
| 152 | CodeGenFunction &CGF) const; |
| 153 | virtual void createCoercedStore(llvm::Value *Val, Address DstAddr, |
| 154 | const ABIArgInfo &AI, bool DestIsVolatile, |
| 155 | CodeGenFunction &CGF) const; |
| 156 | |
| 157 | /// Used by Arm64EC calling convention code to call into x86 calling |
| 158 | /// convention code for varargs function. |
| 159 | virtual ABIArgInfo classifyArgForArm64ECVarArg(QualType Ty) const; |
| 160 | }; |
| 161 | |
| 162 | /// Target specific hooks for defining how a type should be passed or returned |
| 163 | /// from functions with one of the Swift calling conventions. |
| 164 | class SwiftABIInfo { |
| 165 | protected: |
| 166 | CodeGenTypes &CGT; |
| 167 | bool SwiftErrorInRegister; |
| 168 | |
| 169 | bool occupiesMoreThan(ArrayRef<llvm::Type *> scalarTypes, |
| 170 | unsigned maxAllRegisters) const; |
| 171 | |
| 172 | public: |
| 173 | SwiftABIInfo(CodeGen::CodeGenTypes &CGT, bool SwiftErrorInRegister) |
| 174 | : CGT(CGT), SwiftErrorInRegister(SwiftErrorInRegister) {} |
| 175 | |
| 176 | virtual ~SwiftABIInfo(); |
| 177 | |
| 178 | /// Returns true if an aggregate which expands to the given type sequence |
| 179 | /// should be passed / returned indirectly. |
| 180 | virtual bool shouldPassIndirectly(ArrayRef<llvm::Type *> ComponentTys, |
| 181 | bool AsReturnValue) const; |
| 182 | |
| 183 | /// Returns true if the given vector type is legal from Swift's calling |
| 184 | /// convention perspective. |
| 185 | virtual bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy, |
| 186 | unsigned NumElts) const; |
| 187 | |
| 188 | /// Returns true if swifterror is lowered to a register by the target ABI. |
| 189 | bool isSwiftErrorInRegister() const { return SwiftErrorInRegister; }; |
| 190 | }; |
| 191 | } // end namespace CodeGen |
| 192 | } // end namespace clang |
| 193 | |
| 194 | #endif |
| 195 | |