| //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
| // |
| // The LLVM Compiler Infrastructure |
| // |
| // This file is distributed under the University of Illinois Open Source |
| // License. See LICENSE.TXT for details. |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // These classes wrap the information about a call or function |
| // definition used to handle ABI compliancy. |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "TargetInfo.h" |
| #include "ABIInfo.h" |
| #include "CGCXXABI.h" |
| #include "CGValue.h" |
| #include "CodeGenFunction.h" |
| #include "clang/AST/RecordLayout.h" |
| #include "clang/CodeGen/CGFunctionInfo.h" |
| #include "clang/Frontend/CodeGenOptions.h" |
| #include "llvm/ADT/StringExtras.h" |
| #include "llvm/ADT/Triple.h" |
| #include "llvm/IR/DataLayout.h" |
| #include "llvm/IR/Type.h" |
| #include "llvm/Support/raw_ostream.h" |
| #include <algorithm> // std::sort |
| |
| using namespace clang; |
| using namespace CodeGen; |
| |
| static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| llvm::Value *Array, |
| llvm::Value *Value, |
| unsigned FirstIndex, |
| unsigned LastIndex) { |
| // Alternatively, we could emit this as a loop in the source. |
| for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
| llvm::Value *Cell = |
| Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
| Builder.CreateStore(Value, Cell); |
| } |
| } |
| |
| static bool isAggregateTypeForABI(QualType T) { |
| return !CodeGenFunction::hasScalarEvaluationKind(T) || |
| T->isMemberFunctionPointerType(); |
| } |
| |
| ABIInfo::~ABIInfo() {} |
| |
| static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
| CGCXXABI &CXXABI) { |
| const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| if (!RD) |
| return CGCXXABI::RAA_Default; |
| return CXXABI.getRecordArgABI(RD); |
| } |
| |
| static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
| CGCXXABI &CXXABI) { |
| const RecordType *RT = T->getAs<RecordType>(); |
| if (!RT) |
| return CGCXXABI::RAA_Default; |
| return getRecordArgABI(RT, CXXABI); |
| } |
| |
| /// Pass transparent unions as if they were the type of the first element. Sema |
| /// should ensure that all elements of the union have the same "machine type". |
| static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| if (const RecordType *UT = Ty->getAsUnionType()) { |
| const RecordDecl *UD = UT->getDecl(); |
| if (UD->hasAttr<TransparentUnionAttr>()) { |
| assert(!UD->field_empty() && "sema created an empty transparent union"); |
| return UD->field_begin()->getType(); |
| } |
| } |
| return Ty; |
| } |
| |
| CGCXXABI &ABIInfo::getCXXABI() const { |
| return CGT.getCXXABI(); |
| } |
| |
| ASTContext &ABIInfo::getContext() const { |
| return CGT.getContext(); |
| } |
| |
| llvm::LLVMContext &ABIInfo::getVMContext() const { |
| return CGT.getLLVMContext(); |
| } |
| |
| const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| return CGT.getDataLayout(); |
| } |
| |
| const TargetInfo &ABIInfo::getTarget() const { |
| return CGT.getTarget(); |
| } |
| |
| bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| return false; |
| } |
| |
| bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| uint64_t Members) const { |
| return false; |
| } |
| |
| bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| return false; |
| } |
| |
| void ABIArgInfo::dump() const { |
| raw_ostream &OS = llvm::errs(); |
| OS << "(ABIArgInfo Kind="; |
| switch (TheKind) { |
| case Direct: |
| OS << "Direct Type="; |
| if (llvm::Type *Ty = getCoerceToType()) |
| Ty->print(OS); |
| else |
| OS << "null"; |
| break; |
| case Extend: |
| OS << "Extend"; |
| break; |
| case Ignore: |
| OS << "Ignore"; |
| break; |
| case InAlloca: |
| OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| break; |
| case Indirect: |
| OS << "Indirect Align=" << getIndirectAlign() |
| << " ByVal=" << getIndirectByVal() |
| << " Realign=" << getIndirectRealign(); |
| break; |
| case Expand: |
| OS << "Expand"; |
| break; |
| } |
| OS << ")\n"; |
| } |
| |
| TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| |
| // If someone can figure out a general rule for this, that would be great. |
| // It's probably just doomed to be platform-dependent, though. |
| unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| // Verified for: |
| // x86-64 FreeBSD, Linux, Darwin |
| // x86-32 FreeBSD, Linux, Darwin |
| // PowerPC Linux, Darwin |
| // ARM Darwin (*not* EABI) |
| // AArch64 Linux |
| return 32; |
| } |
| |
| bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| const FunctionNoProtoType *fnType) const { |
| // The following conventions are known to require this to be false: |
| // x86_stdcall |
| // MIPS |
| // For everything else, we just prefer false unless we opt out. |
| return false; |
| } |
| |
| void |
| TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| llvm::SmallString<24> &Opt) const { |
| // This assumes the user is passing a library name like "rt" instead of a |
| // filename like "librt.a/so", and that they don't care whether it's static or |
| // dynamic. |
| Opt = "-l"; |
| Opt += Lib; |
| } |
| |
| static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
| |
| /// isEmptyField - Return true iff a the field is "empty", that is it |
| /// is an unnamed bit-field or an (array of) empty record(s). |
| static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| bool AllowArrays) { |
| if (FD->isUnnamedBitfield()) |
| return true; |
| |
| QualType FT = FD->getType(); |
| |
| // Constant arrays of empty records count as empty, strip them off. |
| // Constant arrays of zero length always count as empty. |
| if (AllowArrays) |
| while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| if (AT->getSize() == 0) |
| return true; |
| FT = AT->getElementType(); |
| } |
| |
| const RecordType *RT = FT->getAs<RecordType>(); |
| if (!RT) |
| return false; |
| |
| // C++ record fields are never empty, at least in the Itanium ABI. |
| // |
| // FIXME: We should use a predicate for whether this behavior is true in the |
| // current ABI. |
| if (isa<CXXRecordDecl>(RT->getDecl())) |
| return false; |
| |
| return isEmptyRecord(Context, FT, AllowArrays); |
| } |
| |
| /// isEmptyRecord - Return true iff a structure contains only empty |
| /// fields. Note that a structure with a flexible array member is not |
| /// considered empty. |
| static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
| const RecordType *RT = T->getAs<RecordType>(); |
| if (!RT) |
| return 0; |
| const RecordDecl *RD = RT->getDecl(); |
| if (RD->hasFlexibleArrayMember()) |
| return false; |
| |
| // If this is a C++ record, check the bases first. |
| if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| for (const auto &I : CXXRD->bases()) |
| if (!isEmptyRecord(Context, I.getType(), true)) |
| return false; |
| |
| for (const auto *I : RD->fields()) |
| if (!isEmptyField(Context, I, AllowArrays)) |
| return false; |
| return true; |
| } |
| |
| /// isSingleElementStruct - Determine if a structure is a "single |
| /// element struct", i.e. it has exactly one non-empty field or |
| /// exactly one field which is itself a single element |
| /// struct. Structures with flexible array members are never |
| /// considered single element structs. |
| /// |
| /// \return The field declaration for the single non-empty field, if |
| /// it exists. |
| static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| const RecordType *RT = T->getAs<RecordType>(); |
| if (!RT) |
| return nullptr; |
| |
| const RecordDecl *RD = RT->getDecl(); |
| if (RD->hasFlexibleArrayMember()) |
| return nullptr; |
| |
| const Type *Found = nullptr; |
| |
| // If this is a C++ record, check the bases first. |
| if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| for (const auto &I : CXXRD->bases()) { |
| // Ignore empty records. |
| if (isEmptyRecord(Context, I.getType(), true)) |
| continue; |
| |
| // If we already found an element then this isn't a single-element struct. |
| if (Found) |
| return nullptr; |
| |
| // If this is non-empty and not a single element struct, the composite |
| // cannot be a single element struct. |
| Found = isSingleElementStruct(I.getType(), Context); |
| if (!Found) |
| return nullptr; |
| } |
| } |
| |
| // Check for single element. |
| for (const auto *FD : RD->fields()) { |
| QualType FT = FD->getType(); |
| |
| // Ignore empty fields. |
| if (isEmptyField(Context, FD, true)) |
| continue; |
| |
| // If we already found an element then this isn't a single-element |
| // struct. |
| if (Found) |
| return nullptr; |
| |
| // Treat single element arrays as the element. |
| while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| if (AT->getSize().getZExtValue() != 1) |
| break; |
| FT = AT->getElementType(); |
| } |
| |
| if (!isAggregateTypeForABI(FT)) { |
| Found = FT.getTypePtr(); |
| } else { |
| Found = isSingleElementStruct(FT, Context); |
| if (!Found) |
| return nullptr; |
| } |
| } |
| |
| // We don't consider a struct a single-element struct if it has |
| // padding beyond the element type. |
| if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
| return nullptr; |
| |
| return Found; |
| } |
| |
| static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| // Treat complex types as the element type. |
| if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| Ty = CTy->getElementType(); |
| |
| // Check for a type which we know has a simple scalar argument-passing |
| // convention without any padding. (We're specifically looking for 32 |
| // and 64-bit integer and integer-equivalents, float, and double.) |
| if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| return false; |
| |
| uint64_t Size = Context.getTypeSize(Ty); |
| return Size == 32 || Size == 64; |
| } |
| |
| /// canExpandIndirectArgument - Test whether an argument type which is to be |
| /// passed indirectly (on the stack) would have the equivalent layout if it was |
| /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| /// inhibiting optimizations. |
| /// |
| // FIXME: This predicate is missing many cases, currently it just follows |
| // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| // should probably make this smarter, or better yet make the LLVM backend |
| // capable of handling it. |
| static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| // We can only expand structure types. |
| const RecordType *RT = Ty->getAs<RecordType>(); |
| if (!RT) |
| return false; |
| |
| // We can only expand (C) structures. |
| // |
| // FIXME: This needs to be generalized to handle classes as well. |
| const RecordDecl *RD = RT->getDecl(); |
| if (!RD->isStruct()) |
| return false; |
| |
| // We try to expand CLike CXXRecordDecl. |
| if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| if (!CXXRD->isCLike()) |
| return false; |
| } |
| |
| uint64_t Size = 0; |
| |
| for (const auto *FD : RD->fields()) { |
| if (!is32Or64BitBasicType(FD->getType(), Context)) |
| return false; |
| |
| // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| // how to expand them yet, and the predicate for telling if a bitfield still |
| // counts as "basic" is more complicated than what we were doing previously. |
| if (FD->isBitField()) |
| return false; |
| |
| Size += Context.getTypeSize(FD->getType()); |
| } |
| |
| // Make sure there are not any holes in the struct. |
| if (Size != Context.getTypeSize(Ty)) |
| return false; |
| |
| return true; |
| } |
| |
| namespace { |
| /// DefaultABIInfo - The default implementation for ABI specific |
| /// details. This implementation provides information which results in |
| /// self-consistent and sensible LLVM IR generation, but does not |
| /// conform to any particular ABI. |
| class DefaultABIInfo : public ABIInfo { |
| public: |
| DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| |
| ABIArgInfo classifyReturnType(QualType RetTy) const; |
| ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| |
| void computeInfo(CGFunctionInfo &FI) const override { |
| if (!getCXXABI().classifyReturnType(FI)) |
| FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| for (auto &I : FI.arguments()) |
| I.info = classifyArgumentType(I.type); |
| } |
| |
| llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| CodeGenFunction &CGF) const override; |
| }; |
| |
| class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| public: |
| DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
| }; |
| |
| llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| CodeGenFunction &CGF) const { |
| return nullptr; |
| } |
| |
| ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
| Ty = useFirstFieldIfTransparentUnion(Ty); |
| |
| if (isAggregateTypeForABI(Ty)) { |
| // Records with non-trivial destructors/copy-constructors should not be |
| // passed by value. |
| if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| |
| return ABIArgInfo::getIndirect(0); |
| } |
| |
| // Treat an enum type as its underlying type. |
| if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| Ty = EnumTy->getDecl()->getIntegerType(); |
| |
| return (Ty->isPromotableIntegerType() ? |
| ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| } |
| |
| ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| if (RetTy->isVoidType()) |
| return ABIArgInfo::getIgnore(); |
| |
| if (isAggregateTypeForABI(RetTy)) |
| return ABIArgInfo::getIndirect(0); |
| |
| // Treat an enum type as its underlying type. |
| if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| RetTy = EnumTy->getDecl()->getIntegerType(); |
| |
| return (RetTy->isPromotableIntegerType() ? |
| ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| } |
| |
| // @LOCALMOD-START Emscripten |
| //===----------------------------------------------------------------------===// |
| // Emscripten ABI Implementation |
| // |
| // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| //===----------------------------------------------------------------------===// |
| |
| class EmscriptenABIInfo : public DefaultABIInfo { |
| public: |
| explicit EmscriptenABIInfo(CodeGen::CodeGenTypes &CGT) |
| : DefaultABIInfo(CGT) {} |
| |
| ABIArgInfo classifyReturnType(QualType RetTy) const; |
| ABIArgInfo classifyArgumentType(QualType Ty) const; |
| |
| // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| // non-virtual, but computeInfo is virtual, so we overload that. |
| void computeInfo(CGFunctionInfo &FI) const override { |
| FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| for (auto &Arg : FI.arguments()) |
| Arg.info = classifyArgumentType(Arg.type); |
| } |
| }; |
| |
| class EmscriptenTargetCodeGenInfo : public TargetCodeGenInfo { |
| public: |
| explicit EmscriptenTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| : TargetCodeGenInfo(new EmscriptenABIInfo(CGT)) {} |
| }; |
| |
| /// \brief Classify argument of given type \p Ty. |
| ABIArgInfo EmscriptenABIInfo::classifyArgumentType(QualType Ty) const { |
| if (isAggregateTypeForABI(Ty)) { |
| unsigned TypeAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| return ABIArgInfo::getIndirect(TypeAlign, |
| RAA == CGCXXABI::RAA_DirectInMemory); |
| return ABIArgInfo::getIndirect(TypeAlign); |
| } |
| |
| // Otherwise just do the default thing. |
| return DefaultABIInfo::classifyArgumentType(Ty); |
| } |
| |
| ABIArgInfo EmscriptenABIInfo::classifyReturnType(QualType RetTy) const { |
| if (isAggregateTypeForABI(RetTy)) { |
| // As an optimization, lower single-element structs to just return a regular |
| // value. |
| if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| } |
| |
| // Otherwise just do the default thing. |
| return DefaultABIInfo::classifyReturnType(RetTy); |
| } |
| // @LOCALMOD-END Emscripten |
| |
| //===----------------------------------------------------------------------===// |
| // le32/PNaCl bitcode ABI Implementation |
| // |
| // This is a simplified version of the x86_32 ABI. Arguments and return values |
| // are always passed on the stack. |
| //===----------------------------------------------------------------------===// |
| |
| class PNaClABIInfo : public ABIInfo { |
| public: |
| PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| |
| ABIArgInfo classifyReturnType(QualType RetTy) const; |
| ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| |
| void computeInfo(CGFunctionInfo &FI) const override; |
| llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| CodeGenFunction &CGF) const override; |
| }; |
| |
| class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| public: |
| PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| }; |
| |
| void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| if (!getCXXABI().classifyReturnType(FI)) |
| FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| |
| for (auto &I : FI.arguments()) |
| I.info = classifyArgumentType(I.type); |
| } |
| |
| llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| CodeGenFunction &CGF) const { |
| return nullptr; |
| } |
| |
| /// \brief Classify argument of given type \p Ty. |
| ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
| if (isAggregateTypeForABI(Ty)) { |
| if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| return ABIArgInfo::getIndirect(0); |
| } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| // Treat an enum type as its underlying type. |
| Ty = EnumTy->getDecl()->getIntegerType(); |
| } else if (Ty->isFloatingType()) { |
| // Floating-point types don't go inreg. |
| return ABIArgInfo::getDirect(); |
| } |
| |
| return (Ty->isPromotableIntegerType() ? |
| ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| } |
| |
| ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| if (RetTy->isVoidType()) |
| return ABIArgInfo::getIgnore(); |
| |
| // In the PNaCl ABI we always return records/structures on the stack. |
| if (isAggregateTypeForABI(RetTy)) |
| return ABIArgInfo::getIndirect(0); |
| |
| // Treat an enum type as its underlying type. |
| if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| RetTy = EnumTy->getDecl()->getIntegerType(); |
| |
| return (RetTy->isPromotableIntegerType() ? |
| ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| } |
| |
| /// IsX86_MMXType - Return true if this is an MMX type. |
| bool IsX86_MMXType(llvm::Type *IRType) { |
| // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
| return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| IRType->getScalarSizeInBits() != 64; |
| } |
| |
| static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
| StringRef Constraint, |
| llvm::Type* Ty) { |
| if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| // Invalid MMX constraint |
| return nullptr; |
| } |
| |
| return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
| } |
| |
| // No operation needed |
| return Ty; |
| } |
| |
| /// Returns true if this type can be passed in SSE registers with the |
| /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| return true; |
| } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| // registers specially. |
| unsigned VecSize = Context.getTypeSize(VT); |
| if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| return true; |
| } |
| return false; |
| } |
| |
| /// Returns true if this aggregate is small enough to be passed in SSE registers |
| /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| return NumMembers <= 4; |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // X86-32 ABI Implementation |
| //===----------------------------------------------------------------------===// |
| |
| /// \brief Similar to llvm::CCState, but for Clang. |
| struct CCState { |
| CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
| |
| unsigned CC; |
| unsigned FreeRegs; |
| unsigned FreeSSERegs; |
| }; |
| |
| /// X86_32ABIInfo - The X86-32 ABI information. |
| class X86_32ABIInfo : public ABIInfo { |
| enum Class { |
| Integer, |
| Float |
| }; |
| |
| static const unsigned MinABIStackAlignInBytes = 4; |
| |
| bool IsDarwinVectorABI; |
| bool IsSmallStructInRegABI; |
| bool IsWin32StructABI; |
| unsigned DefaultNumRegisterParameters; |
| |
| static bool isRegisterSize(unsigned Size) { |
| return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| } |
| |
| bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| // FIXME: Assumes vectorcall is in use. |
| return isX86VectorTypeForVectorCall(getContext(), Ty); |
| } |
| |
| bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| uint64_t NumMembers) const override { |
| // FIXME: Assumes vectorcall is in use. |
| return isX86VectorCallAggregateSmallEnough(NumMembers); |
| } |
| |
| bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
| |
| /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| /// such that the argument will be passed in memory. |
| ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| |
| ABIArgInfo getIndirectReturnResult(CCState &State) const; |
| |
| /// \brief Return the alignment to use for the given type on the stack. |
| unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
| |
| Class classify(QualType Ty) const; |
| ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
| ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
| |
| /// \brief Rewrite the function info so that all memory arguments use |
| /// inalloca. |
| void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| |
| void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| unsigned &StackOffset, ABIArgInfo &Info, |
| QualType Type) const; |
| |
| public: |
| |
| void computeInfo(CGFunctionInfo &FI) const override; |
| llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| CodeGenFunction &CGF) const override; |
| |
| X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
| unsigned r) |
| : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
| IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
| }; |
| |
| class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| public: |
| X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| bool d, bool p, bool w, unsigned r) |
| :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
| |
| static bool isStructReturnInRegABI( |
| const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| |
| void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| CodeGen::CodeGenModule &CGM) const override; |
| |
| int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
| // Darwin uses different dwarf register numbers for EH. |
| if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
| return 4; |
| } |
| |
| bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| llvm::Value *Address) const override; |
| |
| llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
| StringRef Constraint, |
| llvm::Type* Ty) const override { |
| return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| } |
| |
| void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| std::string &Constraints, |
| std::vector<llvm::Type *> &ResultRegTypes, |
| std::vector<llvm::Type *> &ResultTruncRegTypes, |
| std::vector<LValue> &ResultRegDests, |
| std::string &AsmString, |
| unsigned NumOutputs) const override; |
| |
| llvm::Constant * |
| getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
| unsigned Sig = (0xeb << 0) | // jmp rel8 |
| (0x06 << 8) | // .+0x08 |
| ('F' << 16) | |
| ('T' << 24); |
| return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| } |
| }; |
| |
| } |
| |
| /// Rewrite input constraint references after adding some output constraints. |
| /// In the case where there is one output and one input and we add one output, |
| /// we need to replace all operand references greater than or equal to 1: |
| /// mov $0, $1 |
| /// mov eax, $1 |
| /// The result will be: |
| /// mov $0, $2 |
| /// mov eax, $2 |
| static void rewriteInputConstraintReferences(unsigned FirstIn, |
| unsigned NumNewOuts, |
| std::string &AsmString) { |
| std::string Buf; |
| llvm::raw_string_ostream OS(Buf); |
| size_t Pos = 0; |
| while (Pos < AsmString.size()) { |
| size_t DollarStart = AsmString.find('$', Pos); |
| if (DollarStart == std::string::npos) |
| DollarStart = AsmString.size(); |
| size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| if (DollarEnd == std::string::npos) |
| DollarEnd = AsmString.size(); |
| OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| Pos = DollarEnd; |
| size_t NumDollars = DollarEnd - DollarStart; |
| if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| // We have an operand reference. |
| size_t DigitStart = Pos; |
| size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| if (DigitEnd == std::string::npos) |
| DigitEnd = AsmString.size(); |
| StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| unsigned OperandIndex; |
| if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| if (OperandIndex >= FirstIn) |
| OperandIndex += NumNewOuts; |
| OS << OperandIndex; |
| } else { |
| OS << OperandStr; |
| } |
| Pos = DigitEnd; |
| } |
| } |
| AsmString = std::move(OS.str()); |
| } |
| |
| /// Add output constraints for EAX:EDX because they are return registers. |
| void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| std::vector<llvm::Type *> &ResultRegTypes, |
| std::vector<llvm::Type *> &ResultTruncRegTypes, |
| std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| unsigned NumOutputs) const { |
| uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| |
| // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| // larger. |
| if (!Constraints.empty()) |
| Constraints += ','; |
| if (RetWidth <= 32) { |
| Constraints += "={eax}"; |
| ResultRegTypes.push_back(CGF.Int32Ty); |
| } else { |
| // Use the 'A' constraint for EAX:EDX. |
| Constraints += "=A"; |
| ResultRegTypes.push_back(CGF.Int64Ty); |
| } |
| |
| // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| ResultTruncRegTypes.push_back(CoerceTy); |
| |
| // Coerce the integer by bitcasting the return slot pointer. |
| ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| CoerceTy->getPointerTo())); |
| ResultRegDests.push_back(ReturnSlot); |
| |
| rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| } |
| |
| /// shouldReturnTypeInRegister - Determine if the given type should be |
| /// passed in a register (for the Darwin ABI). |
| bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| ASTContext &Context) const { |
| uint64_t Size = Context.getTypeSize(Ty); |
| |
| // Type must be register sized. |
| if (!isRegisterSize(Size)) |
| return false; |
| |
| if (Ty->isVectorType()) { |
| // 64- and 128- bit vectors inside structures are not returned in |
| // registers. |
| if (Size == 64 || Size == 128) |
| return false; |
| |
| return true; |
| } |
| |
| // If this is a builtin, pointer, enum, complex type, member pointer, or |
| // member function pointer it is ok. |
| if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
| Ty->isAnyComplexType() || Ty->isEnumeralType() || |
| Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
| return true; |
| |
| // Arrays are treated like records. |
| if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
| return shouldReturnTypeInRegister(AT->getElementType(), Context); |
| |
| // Otherwise, it must be a record type. |
| const RecordType *RT = Ty->getAs<RecordType>(); |
| if (!RT) return false; |
| |
| // FIXME: Traverse bases here too. |
| |
| // Structure types are passed in register if all fields would be |
| // passed in a register. |
| for (const auto *FD : RT->getDecl()->fields()) { |
| // Empty fields are ignored. |
| if (isEmptyField(Context, FD, true)) |
| continue; |
| |
| // Check fields recursively. |
| if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
| return false; |
| } |
| return true; |
| } |
| |
| ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| // If the return value is indirect, then the hidden argument is consuming one |
| // integer register. |
| if (State.FreeRegs) { |
| --State.FreeRegs; |
| return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| } |
| return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| } |
| |
| ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| CCState &State) const { |
| if (RetTy->isVoidType()) |
| return ABIArgInfo::getIgnore(); |
| |
| const Type *Base = nullptr; |
| uint64_t NumElts = 0; |
| if (State.CC == llvm::CallingConv::X86_VectorCall && |
| isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| // The LLVM struct type for such an aggregate should lower properly. |
| return ABIArgInfo::getDirect(); |
| } |
| |
| if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
| // On Darwin, some vectors are returned in registers. |
| if (IsDarwinVectorABI) { |
| uint64_t Size = getContext().getTypeSize(RetTy); |
| |
| // 128-bit vectors are a special case; they are returned in |
| // registers and we need to make sure to pick a type the LLVM |
| // backend will like. |
| if (Size == 128) |
| return ABIArgInfo::getDirect(llvm::VectorType::get( |
| llvm::Type::getInt64Ty(getVMContext()), 2)); |
| |
| // Always return in register if it fits in a general purpose |
| // register, or if it is 64 bits and has a single element. |
| if ((Size == 8 || Size == 16 || Size == 32) || |
| (Size == 64 && VT->getNumElements() == 1)) |
| return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| Size)); |
| |
| return getIndirectReturnResult(State); |
| } |
| |
| return ABIArgInfo::getDirect(); |
| } |
| |
| if (isAggregateTypeForABI(RetTy)) { |
| if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| // Structures with flexible arrays are always indirect. |
| if (RT->getDecl()->hasFlexibleArrayMember()) |
| return getIndirectReturnResult(State); |
| } |
| |
| // If specified, structs and unions are always indirect. |
| if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
| return getIndirectReturnResult(State); |
| |
| // Small structures which are register sized are generally returned |
| // in a register. |
| if (shouldReturnTypeInRegister(RetTy, getContext())) { |
| uint64_t Size = getContext().getTypeSize(RetTy); |
| |
| // As a special-case, if the struct is a "single-element" struct, and |
| // the field is of type "float" or "double", return it in a |
| // floating-point register. (MSVC does not apply this special case.) |
| // We apply a similar transformation for pointer types to improve the |
| // quality of the generated IR. |
| if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
| || SeltTy->hasPointerRepresentation()) |
| return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| |
| // FIXME: We should be able to narrow this integer in cases with dead |
| // padding. |
| return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
| } |
| |
| return getIndirectReturnResult(State); |
| } |
| |
| // Treat an enum type as its underlying type. |
| if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| RetTy = EnumTy->getDecl()->getIntegerType(); |
| |
| return (RetTy->isPromotableIntegerType() ? |
| ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| } |
| |
| static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| } |
| |
| static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| const RecordType *RT = Ty->getAs<RecordType>(); |
| if (!RT) |
| return 0; |
| const RecordDecl *RD = RT->getDecl(); |
| |
| // If this is a C++ record, check the bases first. |
| if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| for (const auto &I : CXXRD->bases()) |
| if (!isRecordWithSSEVectorType(Context, I.getType())) |
| return false; |
| |
| for (const auto *i : RD->fields()) { |
| QualType FT = i->getType(); |
| |
| if (isSSEVectorType(Context, FT)) |
| return true; |
| |
| if (isRecordWithSSEVectorType(Context, FT)) |
| return true; |
| } |
| |
| return false; |
| } |
| |
| unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| unsigned Align) const { |
| // Otherwise, if the alignment is less than or equal to the minimum ABI |
| // alignment, just use the default; the backend will handle this. |
| if (Align <= MinABIStackAlignInBytes) |
| return 0; // Use default alignment. |
| |
| // On non-Darwin, the stack type alignment is always 4. |
| if (!IsDarwinVectorABI) { |
| // Set explicit alignment, since we may need to realign the top. |
| return MinABIStackAlignInBytes; |
| } |
| |
| // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
| if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| isRecordWithSSEVectorType(getContext(), Ty))) |
| return 16; |
| |
| return MinABIStackAlignInBytes; |
| } |
| |
| ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| CCState &State) const { |
| if (!ByVal) { |
| if (State.FreeRegs) { |
| --State.FreeRegs; // Non-byval indirects just use one pointer. |
| return ABIArgInfo::getIndirectInReg(0, false); |
| } |
| return ABIArgInfo::getIndirect(0, false); |
| } |
| |
| // Compute the byval alignment. |
| unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| if (StackAlign == 0) |
| return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
| |
| // If the stack alignment is less than the type alignment, realign the |
| // argument. |
| bool Realign = TypeAlign > StackAlign; |
| return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
| } |
| |
| X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| const Type *T = isSingleElementStruct(Ty, getContext()); |
| if (!T) |
| T = Ty.getTypePtr(); |
| |
| if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| BuiltinType::Kind K = BT->getKind(); |
| if (K == BuiltinType::Float || K == BuiltinType::Double) |
| return Float; |
| } |
| return Integer; |
| } |
| |
| bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| bool &NeedsPadding) const { |
| NeedsPadding = false; |
| Class C = classify(Ty); |
| if (C == Float) |
| return false; |
| |
| unsigned Size = getContext().getTypeSize(Ty); |
| unsigned SizeInRegs = (Size + 31) / 32; |
| |
| if (SizeInRegs == 0) |
| return false; |
| |
| if (SizeInRegs > State.FreeRegs) { |
| State.FreeRegs = 0; |
| return false; |
| } |
| |
| State.FreeRegs -= SizeInRegs; |
| |
| if (State.CC == llvm::CallingConv::X86_FastCall || |
| State.CC == llvm::CallingConv::X86_VectorCall) { |
| if (Size > 32) |
| return false; |
| |
| if (Ty->isIntegralOrEnumerationType()) |
| return true; |
| |
| if (Ty->isPointerType()) |
| return true; |
| |
| if (Ty->isReferenceType()) |
| return true; |
| |
| if (State.FreeRegs) |
| NeedsPadding = true; |
| |
| return false; |
| } |
| |
| return true; |
| } |
| |
| ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| CCState &State) const { |
| // FIXME: Set alignment on indirect arguments. |
| |
| Ty = useFirstFieldIfTransparentUnion(Ty); |
| |
| // Check with the C++ ABI first. |
| const RecordType *RT = Ty->getAs<RecordType>(); |
| if (RT) { |
| CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| if (RAA == CGCXXABI::RAA_Indirect) { |
| return getIndirectResult(Ty, false, State); |
| } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| // The field index doesn't matter, we'll fix it up later. |
| return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| } |
| } |
| |
| // vectorcall adds the concept of a homogenous vector aggregate, similar |
| // to other targets. |
| const Type *Base = nullptr; |
| uint64_t NumElts = 0; |
| if (State.CC == llvm::CallingConv::X86_VectorCall && |
| isHomogeneousAggregate(Ty, Base, NumElts)) { |
| if (State.FreeSSERegs >= NumElts) { |
| State.FreeSSERegs -= NumElts; |
| if (Ty->isBuiltinType() || Ty->isVectorType()) |
| return ABIArgInfo::getDirect(); |
| return ABIArgInfo::getExpand(); |
| } |
| return getIndirectResult(Ty, /*ByVal=*/false, State); |
| } |
| |
| if (isAggregateTypeForABI(Ty)) { |
| if (RT) { |
| // Structs are always byval on win32, regardless of what they contain. |
| if (IsWin32StructABI) |
| return getIndirectResult(Ty, true, State); |
| |
| // Structures with flexible arrays are always indirect. |
| if (RT->getDecl()->hasFlexibleArrayMember()) |
| return getIndirectResult(Ty, true, State); |
| } |
| |
| // Ignore empty structs/unions. |
| if (isEmptyRecord(getContext(), Ty, true)) |
| return ABIArgInfo::getIgnore(); |
| |
| llvm::LLVMContext &LLVMContext = getVMContext(); |
| llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| bool NeedsPadding; |
| if (shouldUseInReg(Ty, State, NeedsPadding)) { |
| unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
| llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| return ABIArgInfo::getDirectInReg(Result); |
| } |
| llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
| |
| // Expand small (<= 128-bit) record types when we know that the stack layout |
| // of those arguments will match the struct. This is important because the |
| // LLVM backend isn't smart enough to remove byval, which inhibits many |
| // optimizations. |
| if (getContext().getTypeSize(Ty) <= 4*32 && |
| canExpandIndirectArgument(Ty, getContext())) |
| return ABIArgInfo::getExpandWithPadding( |
| State.CC == llvm::CallingConv::X86_FastCall || |
| State.CC == llvm::CallingConv::X86_VectorCall, |
| PaddingType); |
| |
| return getIndirectResult(Ty, true, State); |
| } |
| |
| if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| // On Darwin, some vectors are passed in memory, we handle this by passing |
| // it as an i8/i16/i32/i64. |
| if (IsDarwinVectorABI) { |
| uint64_t Size = getContext().getTypeSize(Ty); |
| if ((Size == 8 || Size == 16 || Size == 32) || |
| (Size == 64 && VT->getNumElements() == 1)) |
| return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| Size)); |
| } |
| |
| if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
| |
| return ABIArgInfo::getDirect(); |
| } |
| |
| |
| if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| Ty = EnumTy->getDecl()->getIntegerType(); |
| |
| bool NeedsPadding; |
| bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
| |
| if (Ty->isPromotableIntegerType()) { |
| if (InReg) |
| return ABIArgInfo::getExtendInReg(); |
| return ABIArgInfo::getExtend(); |
| } |
| if (InReg) |
| return ABIArgInfo::getDirectInReg(); |
| return ABIArgInfo::getDirect(); |
| } |
| |
| void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| CCState State(FI.getCallingConvention()); |
| if (State.CC == llvm::CallingConv::X86_FastCall) |
| State.FreeRegs = 2; |
| else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| State.FreeRegs = 2; |
| State.FreeSSERegs = 6; |
| } else if (FI.getHasRegParm()) |
| State.FreeRegs = FI.getRegParm(); |
| else |
| State.FreeRegs = DefaultNumRegisterParameters; |
| |
| if (!getCXXABI().classifyReturnType(FI)) { |
| FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
| } else if (FI.getReturnInfo().isIndirect()) { |
| // The C++ ABI is not aware of register usage, so we have to check if the |
| // return value was sret and put it in a register ourselves if appropriate. |
| if (State.FreeRegs) { |
| --State.FreeRegs; // The sret parameter consumes a register. |
| FI.getReturnInfo().setInReg(true); |
| } |
| } |
| |
| // The chain argument effectively gives us another free register. |
| if (FI.isChainCall()) |
| ++State.FreeRegs; |
| |
| bool UsedInAlloca = false; |
| for (auto &I : FI.arguments()) { |
| I.info = classifyArgumentType(I.type, State); |
| UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| } |
| |
| // If we needed to use inalloca for any argument, do a second pass and rewrite |
| // all the memory arguments to use inalloca. |
| if (UsedInAlloca) |
| rewriteWithInAlloca(FI); |
| } |
| |
| void |
| X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| unsigned &StackOffset, |
| ABIArgInfo &Info, QualType Type) const { |
| assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| |
| // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| // byte aligned. |
| if (StackOffset % 4U) { |
| unsigned OldOffset = StackOffset; |
| StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
| unsigned NumBytes = StackOffset - OldOffset; |
| assert(NumBytes); |
| llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| Ty = llvm::ArrayType::get(Ty, NumBytes); |
| FrameFields.push_back(Ty); |
| } |
| } |
| |
| static bool isArgInAlloca(const ABIArgInfo &Info) { |
| // Leave ignored and inreg arguments alone. |
| switch (Info.getKind()) { |
| case ABIArgInfo::InAlloca: |
| return true; |
| case ABIArgInfo::Indirect: |
| assert(Info.getIndirectByVal()); |
| return true; |
| case ABIArgInfo::Ignore: |
| return false; |
| case ABIArgInfo::Direct: |
| case ABIArgInfo::Extend: |
| case ABIArgInfo::Expand: |
| if (Info.getInReg()) |
| return false; |
| return true; |
| } |
| llvm_unreachable("invalid enum"); |
| } |
| |
| void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| assert(IsWin32StructABI && "inalloca only supported on win32"); |
| |
| // Build a packed struct type for all of the arguments in memory. |
| SmallVector<llvm::Type *, 6> FrameFields; |
| |
| unsigned StackOffset = 0; |
| CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| |
| // Put 'this' into the struct before 'sret', if necessary. |
| bool IsThisCall = |
| FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| ABIArgInfo &Ret = FI.getReturnInfo(); |
| if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| isArgInAlloca(I->info)) { |
| addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| ++I; |
| } |
| |
| // Put the sret parameter into the inalloca struct if it's in memory. |
| if (Ret.isIndirect() && !Ret.getInReg()) { |
| CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
| // On Windows, the hidden sret parameter is always returned in eax. |
| Ret.setInAllocaSRet(IsWin32StructABI); |
| } |
| |
| // Skip the 'this' parameter in ecx. |
| if (IsThisCall) |
| ++I; |
| |
| // Put arguments passed in memory into the struct. |
| for (; I != E; ++I) { |
| if (isArgInAlloca(I->info)) |
| addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| } |
| |
| FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| /*isPacked=*/true)); |
| } |
| |
| llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| CodeGenFunction &CGF) const { |
| llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| |
| CGBuilderTy &Builder = CGF.Builder; |
| llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| "ap"); |
| llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| |
| // Compute if the address needs to be aligned |
| unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| Align = getTypeStackAlignInBytes(Ty, Align); |
| Align = std::max(Align, 4U); |
| if (Align > 4) { |
| // addr = (addr + align - 1) & -align; |
| llvm::Value *Offset = |
| llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| CGF.Int32Ty); |
| llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| Addr->getType(), |
| "ap.cur.aligned"); |
| } |
| |
| llvm::Type *PTy = |
| llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| |
| uint64_t Offset = |
| llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
| llvm::Value *NextAddr = |
| Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| "ap.next"); |
| Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| |
| return AddrTyped; |
| } |
| |
| bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| assert(Triple.getArch() == llvm::Triple::x86); |
| |
| switch (Opts.getStructReturnConvention()) { |
| case CodeGenOptions::SRCK_Default: |
| break; |
| case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| return false; |
| case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| return true; |
| } |
| |
| if (Triple.isOSDarwin()) |
| return true; |
| |
| switch (Triple.getOS()) { |
| case llvm::Triple::DragonFly: |
| case llvm::Triple::FreeBSD: |
| case llvm::Triple::OpenBSD: |
| case llvm::Triple::Bitrig: |
| case llvm::Triple::Win32: |
| return true; |
| default: |
| return false; |
| } |
| } |
| |
| void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
| llvm::GlobalValue *GV, |
| CodeGen::CodeGenModule &CGM) const { |
| if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| // Get the LLVM function. |
| llvm::Function *Fn = cast<llvm::Function>(GV); |
| |
| // Now add the 'alignstack' attribute with a value of 16. |
| llvm::AttrBuilder B; |
| B.addStackAlignmentAttr(16); |
| Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| llvm::AttributeSet::get(CGM.getLLVMContext(), |
| llvm::AttributeSet::FunctionIndex, |
| B)); |
| } |
| } |
| } |
| |
| bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| CodeGen::CodeGenFunction &CGF, |
| llvm::Value *Address) const { |
| CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| |
| llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
| |
| // 0-7 are the eight integer registers; the order is different |
| // on Darwin (for EH), but the range is the same. |
| // 8 is %eip. |
| AssignToArrayRange(Builder, Address, Four8, 0, 8); |
| |
| if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
| // 12-16 are st(0..4). Not sure why we stop at 4. |
| // These have size 16, which is sizeof(long double) on |
| // platforms with 8-byte alignment for that type. |
| llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
| AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
| |
| } else { |
| // 9 is %eflags, which doesn't get a size on Darwin for some |
| // reason. |
| Builder.CreateStore( |
| Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9)); |
| |
| // 11-16 are st(0..5). Not sure why we stop at 5. |
| // These have size 12, which is sizeof(long double) on |
| // platforms with 4-byte alignment for that type. |
| llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
| AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| } |
| |
| return false; |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // X86-64 ABI Implementation |
| //===----------------------------------------------------------------------===// |
| |
| |
| namespace { |
| /// The AVX ABI level for X86 targets. |
| enum class X86AVXABILevel { |
| None, |
| AVX, |
| AVX512 |
| }; |
| |
| /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| switch (AVXLevel) { |
| case X86AVXABILevel::AVX512: |
| return 512; |
| case X86AVXABILevel::AVX: |
| return 256; |
| case X86AVXABILevel::None: |
| return 128; |
| } |
| llvm_unreachable("Unknown AVXLevel"); |
| } |
| |
| /// X86_64ABIInfo - The X86_64 ABI information. |
| class X86_64ABIInfo : public ABIInfo { |
| enum Class { |
| Integer = 0, |
| SSE, |
| SSEUp, |
| X87, |
| X87Up, |
| ComplexX87, |
| NoClass, |
| Memory |
| }; |
| |
| /// merge - Implement the X86_64 ABI merging algorithm. |
| /// |
| /// Merge an accumulating classification \arg Accum with a field |
| /// classification \arg Field. |
| /// |
| /// \param Accum - The accumulating classification. This should |
| /// always be either NoClass or the result of a previous merge |
| /// call. In addition, this should never be Memory (the caller |
| /// should just return Memory for the aggregate). |
| static Class merge(Class Accum, Class Field); |
| |
| /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| /// |
| /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| /// final MEMORY or SSE classes when necessary. |
| /// |
| /// \param AggregateSize - The size of the current aggregate in |
| /// the classification process. |
| /// |
| /// \param Lo - The classification for the parts of the type |
| /// residing in the low word of the containing object. |
| /// |
| /// \param Hi - The classification for the parts of the type |
| /// residing in the higher words of the containing object. |
| /// |
| void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| |
| /// classify - Determine the x86_64 register classes in which the |
| /// given type T should be passed. |
| /// |
| /// \param Lo - The classification for the parts of the type |
| /// residing in the low word of the containing object. |
| /// |
| /// \param Hi - The classification for the parts of the type |
| /// residing in the high word of the containing object. |
| /// |
| /// \param OffsetBase - The bit offset of this type in the |
| /// containing object. Some parameters are classified different |
| /// depending on whether they straddle an eightbyte boundary. |
| /// |
| /// \param isNamedArg - Whether the argument in question is a "named" |
| /// argument, as used in AMD64-ABI 3.5.7. |
| /// |
| /// If a word is unused its result will be NoClass; if a type should |
| /// be passed in Memory then at least the classification of \arg Lo |
| /// will be Memory. |
| /// |
| /// The \arg Lo class will be NoClass iff the argument is ignored. |
| /// |
| /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| /// also be ComplexX87. |
| void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| bool isNamedArg) const; |
| |
| llvm::Type *GetByteVectorType(QualType Ty) const; |
| llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| unsigned IROffset, QualType SourceTy, |
| unsigned SourceOffset) const; |
| llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| unsigned IROffset, QualType SourceTy, |
| unsigned SourceOffset) const; |
| |
| /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| /// such that the argument will be returned in memory. |
| ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
| |
| /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| /// such that the argument will be passed in memory. |
| /// |
| /// \param freeIntRegs - The number of free integer registers remaining |
| /// available. |
| ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
| |
| ABIArgInfo classifyReturnType(QualType RetTy) const; |
| |
| ABIArgInfo classifyArgumentType(QualType Ty, |
| unsigned freeIntRegs, |
| unsigned &neededInt, |
| unsigned &neededSSE, |
| bool isNamedArg) const; |
| |
| bool IsIllegalVectorType(QualType Ty) const; |
| |
| /// The 0.98 ABI revision clarified a lot of ambiguities, |
| /// unfortunately in ways that were not always consistent with |
| /// certain previous compilers. In particular, platforms which |
| /// required strict binary compatibility with older versions of GCC |
| /// may need to exempt themselves. |
| bool honorsRevision0_98() const { |
| return !getTarget().getTriple().isOSDarwin(); |
| } |
| |
| X86AVXABILevel AVXLevel; |
| // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| // 64-bit hardware. |
| bool Has64BitPointers; |
| |
| public: |
| X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
| ABIInfo(CGT), AVXLevel(AVXLevel), |
| Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
| } |
| |
| bool isPassedUsingAVXType(QualType type) const { |
| unsigned neededInt, neededSSE; |
| // The freeIntRegs argument doesn't matter here. |
| ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| /*isNamedArg*/true); |
| if (info.isDirect()) { |
| llvm::Type *ty = info.getCoerceToType(); |
| if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| return (vectorTy->getBitWidth() > 128); |
| } |
| return false; |
| } |
| |
| void computeInfo(CGFunctionInfo &FI) const override; |
| |
| llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| CodeGenFunction &CGF) const override; |
| |
| bool has64BitPointers() const { |
| return Has64BitPointers; |
| } |
| }; |
| |
| /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
| class WinX86_64ABIInfo : public ABIInfo { |
| |
| ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| bool IsReturnType) const; |
| |
| public: |
| WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| |
| void computeInfo(CGFunctionInfo &FI) const override; |
| |
| llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| CodeGenFunction &CGF) const override; |
| |
| bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| // FIXME: Assumes vectorcall is in use. |
| return isX86VectorTypeForVectorCall(getContext(), Ty); |
| } |
| |
| bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| uint64_t NumMembers) const override { |
| // FIXME: Assumes vectorcall is in use. |
| return isX86VectorCallAggregateSmallEnough(NumMembers); |
| } |
| }; |
| |
| class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| public: |
| X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
| |
| const X86_64ABIInfo &getABIInfo() const { |
| return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| } |
| |
| int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
| return 7; |
| } |
| |
| bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| llvm::Value *Address) const override { |
| llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
| |
| // 0-15 are the 16 integer registers. |
| // 16 is %rip. |
| AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
| return false; |
| } |
| |
| llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
| StringRef Constraint, |
| llvm::Type* Ty) const override { |
| return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| } |
| |
| bool isNoProtoCallVariadic(const CallArgList &args, |
| const FunctionNoProtoType *fnType) const override { |
| // The default CC on x86-64 sets %al to the number of SSA |
| // registers used, and GCC sets this when calling an unprototyped |
| // function, so we override the default behavior. However, don't do |
| // that when AVX types are involved: the ABI explicitly states it is |
| // undefined, and it doesn't work in practice because of how the ABI |
| // defines varargs anyway. |
| if (fnType->getCallConv() == CC_C) { |
| bool HasAVXType = false; |
| for (CallArgList::const_iterator |
| it = args.begin(), ie = args.end(); it != ie; ++it) { |
| if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| HasAVXType = true; |
| break; |
| } |
| } |
| |
| if (!HasAVXType) |
| return true; |
| } |
| |
| return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
| } |
| |
| llvm::Constant * |
| getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
| unsigned Sig; |
| if (getABIInfo().has64BitPointers()) |
| Sig = (0xeb << 0) | // jmp rel8 |
| (0x0a << 8) | // .+0x0c |
| ('F' << 16) | |
| ('T' << 24); |
| else |
| Sig = (0xeb << 0) | // jmp rel8 |
| (0x06 << 8) | // .+0x08 |
| ('F' << 16) | |
| ('T' << 24); |
| return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| } |
| }; |
| |
| class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| public: |
| PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
| |
| void getDependentLibraryOption(llvm::StringRef Lib, |
| llvm::SmallString<24> &Opt) const override { |
| Opt = "\01"; |
| // If the argument contains a space, enclose it in quotes. |
| if (Lib.find(" ") != StringRef::npos) |
| Opt += "\"" + Lib.str() + "\""; |
| else |
| Opt += Lib; |
| } |
| }; |
| |
| static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
| // If the argument does not end in .lib, automatically add the suffix. |
| // If the argument contains a space, enclose it in quotes. |
| // This matches the behavior of MSVC. |
| bool Quote = (Lib.find(" ") != StringRef::npos); |
| std::string ArgStr = Quote ? "\"" : ""; |
| ArgStr += Lib; |
| if (!Lib.endswith_lower(".lib")) |
| ArgStr += ".lib"; |
| ArgStr += Quote ? "\"" : ""; |
| return ArgStr; |
| } |
| |
| class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| public: |
| WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| bool d, bool p, bool w, unsigned RegParms) |
| : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
| |
| void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| CodeGen::CodeGenModule &CGM) const override; |
| |
| void getDependentLibraryOption(llvm::StringRef Lib, |
| llvm::SmallString<24> &Opt) const override { |
| Opt = "/DEFAULTLIB:"; |
| Opt += qualifyWindowsLibrary(Lib); |
| } |
| |
| void getDetectMismatchOption(llvm::StringRef Name, |
| llvm::StringRef Value, |
| llvm::SmallString<32> &Opt) const override { |
| Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| } |
| }; |
| |
| static void addStackProbeSizeTargetAttribute(const Decl *D, |
| llvm::GlobalValue *GV, |
| CodeGen::CodeGenModule &CGM) { |
| if (isa<FunctionDecl>(D)) { |
| if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| llvm::Function *Fn = cast<llvm::Function>(GV); |
| |
| Fn->addFnAttr("stack-probe-size", |
| llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| } |
| } |
| } |
| |
| void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
| llvm::GlobalValue *GV, |
| CodeGen::CodeGenModule &CGM) const { |
| X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| |
| addStackProbeSizeTargetAttribute(D, GV, CGM); |
| } |
| |
| class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| public: |
| WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| X86AVXABILevel AVXLevel) |
| : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| |
| void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| CodeGen::CodeGenModule &CGM) const override; |
| |
| int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
| return 7; |
| } |
| |
| bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| llvm::Value *Address) const override { |
| llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
| |
| // 0-15 are the 16 integer registers. |
| // 16 is %rip. |
| AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
| return false; |
| } |
| |
| void getDependentLibraryOption(llvm::StringRef Lib, |
| llvm::SmallString<24> &Opt) const override { |
| Opt = "/DEFAULTLIB:"; |
| Opt += qualifyWindowsLibrary(Lib); |
| } |
| |
| void getDetectMismatchOption(llvm::StringRef Name, |
| llvm::StringRef Value, |
| llvm::SmallString<32> &Opt) const override { |
| Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| } |
| }; |
| |
| void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
| llvm::GlobalValue *GV, |
| CodeGen::CodeGenModule &CGM) const { |
| TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| |
| addStackProbeSizeTargetAttribute(D, GV, CGM); |
| } |
| } |
| |
| void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| Class &Hi) const { |
| // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| // |
| // (a) If one of the classes is Memory, the whole argument is passed in |
| // memory. |
| // |
| // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| // memory. |
| // |
| // (c) If the size of the aggregate exceeds two eightbytes and the first |
| // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| // argument is passed in memory. NOTE: This is necessary to keep the |
| // ABI working for processors that don't support the __m256 type. |
| // |
| // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| // |
| // Some of these are enforced by the merging logic. Others can arise |
| // only with unions; for example: |
| // union { _Complex double; unsigned; } |
| // |
| // Note that clauses (b) and (c) were added in 0.98. |
| // |
| if (Hi == Memory) |
| Lo = Memory; |
| if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| Lo = Memory; |
| if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| Lo = Memory; |
| if (Hi == SSEUp && Lo != SSE) |
| Hi = SSE; |
| } |
| |
| X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
| // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| // classified recursively so that always two fields are |
| // considered. The resulting class is calculated according to |
| // the classes of the fields in the eightbyte: |
| // |
| // (a) If both classes are equal, this is the resulting class. |
| // |
| // (b) If one of the classes is NO_CLASS, the resulting class is |
| // the other class. |
| // |
| // (c) If one of the classes is MEMORY, the result is the MEMORY |
| // class. |
| // |
| // (d) If one of the classes is INTEGER, the result is the |
| // INTEGER. |
| // |
| // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| // MEMORY is used as class. |
| // |
| // (f) Otherwise class SSE is used. |
| |
| // Accum should never be memory (we should have returned) or |
| // ComplexX87 (because this cannot be passed in a structure). |
| assert((Accum != Memory && Accum != ComplexX87) && |
| "Invalid accumulated classification during merge."); |
| if (Accum == Field || Field == NoClass) |
| return Accum; |
| if (Field == Memory) |
| return Memory; |
| if (Accum == NoClass) |
| return Field; |
| if (Accum == Integer || Field == Integer) |
| return Integer; |
| if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| Accum == X87 || Accum == X87Up) |
| return Memory; |
| return SSE; |
| } |
| |
| void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
| Class &Lo, Class &Hi, bool isNamedArg) const { |
| // FIXME: This code can be simplified by introducing a simple value class for |
| // Class pairs with appropriate constructor methods for the various |
| // situations. |
| |
| // FIXME: Some of the split computations are wrong; unaligned vectors |
| // shouldn't be passed in registers for example, so there is no chance they |
| // can straddle an eightbyte. Verify & simplify. |
| |
| Lo = Hi = NoClass; |
| |
| Class &Current = OffsetBase < 64 ? Lo : Hi; |
| Current = Memory; |
| |
| if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| BuiltinType::Kind k = BT->getKind(); |
| |
| if (k == BuiltinType::Void) { |
| Current = NoClass; |
| } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| Lo = Integer; |
| Hi = Integer; |
| } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| Current = Integer; |
| } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| (k == BuiltinType::LongDouble && |
| getTarget().getTriple().isOSNaCl())) { |
| Current = SSE; |
| } else if (k == BuiltinType::LongDouble) { |
| Lo = X87; |
| Hi = X87Up; |
| } |
| // FIXME: _Decimal32 and _Decimal64 are SSE. |
| // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
| return; |
| } |
| |
| if (const EnumType *ET = Ty->getAs<EnumType>()) { |
| // Classify the underlying integer type. |
| classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
| return; |
| } |
| |
| if (Ty->hasPointerRepresentation()) { |
| Current = Integer; |
| return; |
| } |
| |
| if (Ty->isMemberPointerType()) { |
| if (Ty->isMemberFunctionPointerType()) { |
| if (Has64BitPointers) { |
| // If Has64BitPointers, this is an {i64, i64}, so classify both |
| // Lo and Hi now. |
| Lo = Hi = Integer; |
| } else { |
| // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| // straddles an eightbyte boundary, Hi should be classified as well. |
| uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| if (EB_FuncPtr != EB_ThisAdj) { |
| Lo = Hi = Integer; |
| } else { |
| Current = Integer; |
| } |
| } |
| } else { |
| Current = Integer; |
| } |
| return; |
| } |
| |
| if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| uint64_t Size = getContext().getTypeSize(VT); |
| if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| // gcc passes the following as integer: |
| // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| // 2 bytes - <2 x char>, <1 x short> |
| // 1 byte - <1 x char> |
| Current = Integer; |
| |
| // If this type crosses an eightbyte boundary, it should be |
| // split. |
| uint64_t EB_Lo = (OffsetBase) / 64; |
| uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| if (EB_Lo != EB_Hi) |
| Hi = Lo; |
| } else if (Size == 64) { |
| // gcc passes <1 x double> in memory. :( |
| if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| return; |
| |
| // gcc passes <1 x long long> as INTEGER. |
| if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
| VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
| Current = Integer; |
| else |
| Current = SSE; |
| |
| // If this type crosses an eightbyte boundary, it should be |
| // split. |
| if (OffsetBase && OffsetBase != 64) |
| Hi = Lo; |
| } else if (Size == 128 || |
| (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
| // Arguments of 256-bits are split into four eightbyte chunks. The |
| // least significant one belongs to class SSE and all the others to class |
| // SSEUP. The original Lo and Hi design considers that types can't be |
| // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| // This design isn't correct for 256-bits, but since there're no cases |
| // where the upper parts would need to be inspected, avoid adding |
| // complexity and just consider Hi to match the 64-256 part. |
| // |
| // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| // registers if they are "named", i.e. not part of the "..." of a |
| // variadic function. |
| // |
| // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| // split into eight eightbyte chunks, one SSE and seven SSEUP. |
| Lo = SSE; |
| Hi = SSEUp; |
| } |
| return; |
| } |
| |
| if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| QualType ET = getContext().getCanonicalType(CT->getElementType()); |
| |
| uint64_t Size = getContext().getTypeSize(Ty); |
| if (ET->isIntegralOrEnumerationType()) { |
| if (Size <= 64) |
| Current = Integer; |
| else if (Size <= 128) |
| Lo = Hi = Integer; |
| } else if (ET == getContext().FloatTy) |
| Current = SSE; |
| else if (ET == getContext().DoubleTy || |
| (ET == getContext().LongDoubleTy && |
| getTarget().getTriple().isOSNaCl())) |
| Lo = Hi = SSE; |
| else if (ET == getContext().LongDoubleTy) |
| Current = ComplexX87; |
| |
| // If this complex type crosses an eightbyte boundary then it |
| // should be split. |
| uint64_t EB_Real = (OffsetBase) / 64; |
| uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
| if (Hi == NoClass && EB_Real != EB_Imag) |
| Hi = Lo; |
| |
| return; |
| } |
| |
| if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| // Arrays are treated like structures. |
| |
| uint64_t Size = getContext().getTypeSize(Ty); |
| |
| // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| // than four eightbytes, ..., it has class MEMORY. |
| if (Size > 256) |
| return; |
| |
| // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| // fields, it has class MEMORY. |
| // |
| // Only need to check alignment of array base. |
| if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
| return; |
| |
| // Otherwise implement simplified merge. We could be smarter about |
| // this, but it isn't worth it and would be harder to verify. |
| Current = NoClass; |
| uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
| uint64_t ArraySize = AT->getSize().getZExtValue(); |
| |
| // The only case a 256-bit wide vector could be used is when the array |
| // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| // to work for sizes wider than 128, early check and fallback to memory. |
| if (Size > 128 && EltSize != 256) |
| return; |
| |
| for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| Class FieldLo, FieldHi; |
| classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
| Lo = merge(Lo, FieldLo); |
| Hi = merge(Hi, FieldHi); |
| if (Lo == Memory || Hi == Memory) |
| break; |
| } |
| |
| postMerge(Size, Lo, Hi); |
| assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
| return; |
| } |
| |
| if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| uint64_t Size = getContext().getTypeSize(Ty); |
| |
| // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| // than four eightbytes, ..., it has class MEMORY. |
| if (Size > 256) |
| return; |
| |
| // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| // copy constructor or a non-trivial destructor, it is passed by invisible |
| // reference. |
| if (getRecordArgABI(RT, getCXXABI())) |
| return; |
| |
| const RecordDecl *RD = RT->getDecl(); |
| |
| // Assume variable sized types are passed in memory. |
| if (RD->hasFlexibleArrayMember()) |
| return; |
| |
| const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| |
| // Reset Lo class, this will be recomputed. |
| Current = NoClass; |
| |
| // If this is a C++ record, classify the bases first. |
| if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| for (const auto &I : CXXRD->bases()) { |
| assert(!I.isVirtual() && !I.getType()->isDependentType() && |
| "Unexpected base class!"); |
| const CXXRecordDecl *Base = |
| cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
| |
| // Classify this field. |
| // |
| // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| // single eightbyte, each is classified separately. Each eightbyte gets |
| // initialized to class NO_CLASS. |
| Class FieldLo, FieldHi; |
| uint64_t Offset = |
| OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
| classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
| Lo = merge(Lo, FieldLo); |
| Hi = merge(Hi, FieldHi); |
| if (Lo == Memory || Hi == Memory) { |
| postMerge(Size, Lo, Hi); |
| return; |
| } |
| } |
| } |
| |
| // Classify the fields one at a time, merging the results. |
| unsigned idx = 0; |
| for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| i != e; ++i, ++idx) { |
| uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| bool BitField = i->isBitField(); |
| |
| // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
| // |
| // The only case a 256-bit wide vector could be used is when the struct |
| // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| // to work for sizes wider than 128, early check and fallback to memory. |
| // |
| if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| Lo = Memory; |
| postMerge(Size, Lo, Hi); |
| return; |
| } |
| // Note, skip this test for bit-fields, see below. |
| if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
| Lo = Memory; |
| postMerge(Size, Lo, Hi); |
| return; |
| } |
| |
| // Classify this field. |
| // |
| // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| // exceeds a single eightbyte, each is classified |
| // separately. Each eightbyte gets initialized to class |
| // NO_CLASS. |
| Class FieldLo, FieldHi; |
| |
| // Bit-fields require special handling, they do not force the |
| // structure to be passed in memory even if unaligned, and |
| // therefore they can straddle an eightbyte. |
| if (BitField) { |
| // Ignore padding bit-fields. |
| if (i->isUnnamedBitfield()) |
| continue; |
| |
| uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| uint64_t Size = i->getBitWidthValue(getContext()); |
| |
| uint64_t EB_Lo = Offset / 64; |
| uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| |
| if (EB_Lo) { |
| assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| FieldLo = NoClass; |
| FieldHi = Integer; |
| } else { |
| FieldLo = Integer; |
| FieldHi = EB_Hi ? Integer : NoClass; |
| } |
| } else |
| classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
| Lo = merge(Lo, FieldLo); |
| Hi = merge(Hi, FieldHi); |
| if (Lo == Memory || Hi == Memory) |
| break; |
| } |
| |
| postMerge(Size, Lo, Hi); |
| } |
| } |
| |
| ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
| // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| // place naturally. |
| if (!isAggregateTypeForABI(Ty)) { |
| // Treat an enum type as its underlying type. |
| if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| Ty = EnumTy->getDecl()->getIntegerType(); |
| |
| return (Ty->isPromotableIntegerType() ? |
| ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| } |
| |
| return ABIArgInfo::getIndirect(0); |
| } |
| |
| bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| uint64_t Size = getContext().getTypeSize(VecTy); |
| unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
| if (Size <= 64 || Size > LargestVector) |
| return true; |
| } |
| |
| return false; |
| } |
| |
| ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| unsigned freeIntRegs) const { |
| // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| // place naturally. |
| // |
| // This assumption is optimistic, as there could be free registers available |
| // when we need to pass this argument in memory, and LLVM could try to pass |
| // the argument in the free register. This does not seem to happen currently, |
| // but this code would be much safer if we could mark the argument with |
| // 'onstack'. See PR12193. |
| if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
| // Treat an enum type as its underlying type. |
| if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| Ty = EnumTy->getDecl()->getIntegerType(); |
| |
| return (Ty->isPromotableIntegerType() ? |
| ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| } |
| |
| if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| |
| // Compute the byval alignment. We specify the alignment of the byval in all |
| // cases so that the mid-level optimizer knows the alignment of the byval. |
| unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
| |
| // Attempt to avoid passing indirect results using byval when possible. This |
| // is important for good codegen. |
| // |
| // We do this by coercing the value into a scalar type which the backend can |
| // handle naturally (i.e., without using byval). |
| // |
| // For simplicity, we currently only do this when we have exhausted all of the |
| // free integer registers. Doing this when there are free integer registers |
| // would require more care, as we would have to ensure that the coerced value |
| // did not claim the unused register. That would require either reording the |
| // arguments to the function (so that any subsequent inreg values came first), |
| // or only doing this optimization when there were no following arguments that |
| // might be inreg. |
| // |
| // We currently expect it to be rare (particularly in well written code) for |
| // arguments to be passed on the stack when there are still free integer |
| // registers available (this would typically imply large structs being passed |
| // by value), so this seems like a fair tradeoff for now. |
| // |
| // We can revisit this if the backend grows support for 'onstack' parameter |
| // attributes. See PR12193. |
| if (freeIntRegs == 0) { |
| uint64_t Size = getContext().getTypeSize(Ty); |
| |
| // If this type fits in an eightbyte, coerce it into the matching integral |
| // type, which will end up on the stack (with alignment 8). |
| if (Align == 8 && Size <= 64) |
| return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| Size)); |
| } |
| |
| return ABIArgInfo::getIndirect(Align); |
| } |
| |
| /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| /// register. Pick an LLVM IR type that will be passed as a vector register. |
| llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
| // Wrapper structs/arrays that only contain vectors are passed just like |
| // vectors; strip them off if present. |
| if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| Ty = QualType(InnerTy, 0); |
| |
| llvm::Type *IRType = CGT.ConvertType(Ty); |
| if(isa<llvm::VectorType>(IRType)) |
| return IRType; |
| |
| // We couldn't find the preferred IR vector type for 'Ty'. |
| uint64_t Size = getContext().getTypeSize(Ty); |
| assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| |
| // Return a LLVM IR vector type based on the size of 'Ty'. |
| return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| Size / 64); |
| } |
| |
| /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| /// is known to either be off the end of the specified type or being in |
| /// alignment padding. The user type specified is known to be at most 128 bits |
| /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| /// classification that put one of the two halves in the INTEGER class. |
| /// |
| /// It is conservatively correct to return false. |
| static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| unsigned EndBit, ASTContext &Context) { |
| // If the bytes being queried are off the end of the type, there is no user |
| // data hiding here. This handles analysis of builtins, vectors and other |
| // types that don't contain interesting padding. |
| unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| if (TySize <= StartBit) |
| return true; |
| |
| if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| |
| // Check each element to see if the element overlaps with the queried range. |
| for (unsigned i = 0; i != NumElts; ++i) { |
| // If the element is after the span we care about, then we're done.. |
| unsigned EltOffset = i*EltSize; |
| if (EltOffset >= EndBit) break; |
| |
| unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| EndBit-EltOffset, Context)) |
| return false; |
| } |
| // If it overlaps no elements, then it is safe to process as padding. |
| return true; |
| } |
| |
| if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| const RecordDecl *RD = RT->getDecl(); |
| const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| |
| // If this is a C++ record, check the bases first. |
| if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| for (const auto &I : CXXRD->bases()) { |
| assert(!I.isVirtual() && !I.getType()->isDependentType() && |
| "Unexpected base class!"); |
| const CXXRecordDecl *Base = |
| cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
| |
| // If the base is after the span we care about, ignore it. |
| unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
| if (BaseOffset >= EndBit) continue; |
| |
| unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
| if (!BitsContainNoUserData(I.getType(), BaseStart, |
| EndBit-BaseOffset, Context)) |
| return false; |
| } |
| } |
| |
| // Verify that no field has data that overlaps the region of interest. Yes |
| // this could be sped up a lot by being smarter about queried fields, |
| // however we're only looking at structs up to 16 bytes, so we don't care |
| // much. |
| unsigned idx = 0; |
| for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| i != e; ++i, ++idx) { |
| unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
| |
| // If we found a field after the region we care about, then we're done. |
| if (FieldOffset >= EndBit) break; |
| |
| unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| Context)) |
| return false; |
| } |
| |
| // If nothing in this record overlapped the area of interest, then we're |
| // clean. |
| return true; |
| } |
| |
| return false; |
| } |
| |
| /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| /// float member at the specified offset. For example, {int,{float}} has a |
| /// float at offset 4. It is conservatively correct for this routine to return |
| /// false. |
| static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
| const llvm::DataLayout &TD) { |
| // Base case if we find a float. |
| if (IROffset == 0 && IRType->isFloatTy()) |
| return true; |
| |
| // If this is a struct, recurse into the field at the specified offset. |
| if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
| const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| unsigned Elt = SL->getElementContainingOffset(IROffset); |
| IROffset -= SL->getElementOffset(Elt); |
| return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| } |
| |
| // If this is an array, recurse into the field at the specified offset. |
| if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| llvm::Type *EltTy = ATy->getElementType(); |
| unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| IROffset -= IROffset/EltSize*EltSize; |
| return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| } |
| |
| return false; |
| } |
| |
| |
| /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| /// low 8 bytes of an XMM register, corresponding to the SSE class. |
| llvm::Type *X86_64ABIInfo:: |
| GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
| QualType SourceTy, unsigned SourceOffset) const { |
| // The only three choices we have are either double, <2 x float>, or float. We |
| // pass as float if the last 4 bytes is just padding. This happens for |
| // structs that contain 3 floats. |
| if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| SourceOffset*8+64, getContext())) |
| return llvm::Type::getFloatTy(getVMContext()); |
| |
| // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| // case. |
| if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
| return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
| |
| return llvm::Type::getDoubleTy(getVMContext()); |
| } |
| |
| |
| /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| /// about the high or low part of an up-to-16-byte struct. This routine picks |
| /// the best LLVM IR type to represent this, which may be i64 or may be anything |
| /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| /// etc). |
| /// |
| /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| /// the 8-byte value references. PrefType may be null. |
| /// |
| /// SourceTy is the source-level type for the entire argument. SourceOffset is |
| /// an offset into this that we're processing (which is always either 0 or 8). |
| /// |
| llvm::Type *X86_64ABIInfo:: |
| GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
| QualType SourceTy, unsigned SourceOffset) const { |
| // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| // returning an 8-byte unit starting with it. See if we can safely use it. |
| if (IROffset == 0) { |
| // Pointers and int64's always fill the 8-byte unit. |
| if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| IRType->isIntegerTy(64)) |
| return IRType; |
| |
| // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| // goodness in the source type is just tail padding. This is allowed to |
| // kick in for struct {double,int} on the int, but not on |
| // struct{double,int,int} because we wouldn't return the second int. We |
| // have to do this analysis on the source type because we can't depend on |
| // unions being lowered a specific way etc. |
| if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
| IRType->isIntegerTy(32) || |
| (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| cast<llvm::IntegerType>(IRType)->getBitWidth(); |
| |
| if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| SourceOffset*8+64, getContext())) |
| return IRType; |
| } |
| } |
| |
| if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
| // If this is a struct, recurse into the field at the specified offset. |
| const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
| if (IROffset < SL->getSizeInBytes()) { |
| unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| IROffset -= SL->getElementOffset(FieldIdx); |
| |
| return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| SourceTy, SourceOffset); |
| } |
| } |
| |
| if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| llvm::Type *EltTy = ATy->getElementType(); |
| unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
| unsigned EltOffset = IROffset/EltSize*EltSize; |
| return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| SourceOffset); |
| } |
| |
| // Okay, we don't have any better idea of what to pass, so we pass this in an |
| // integer register that isn't too big to fit the rest of the struct. |
| unsigned TySizeInBytes = |
| (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
| |
| assert(TySizeInBytes != SourceOffset && "Empty field?"); |
| |
| // It is always safe to classify this as an integer type up to i64 that |
| // isn't larger than the structure. |
| return llvm::IntegerType::get(getVMContext(), |
| std::min(TySizeInBytes-SourceOffset, 8U)*8); |
| } |
| |
| |
| /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| /// be used as elements of a two register pair to pass or return, return a |
| /// first class aggregate to represent them. For example, if the low part of |
| /// a by-value argument should be passed as i32* and the high part as float, |
| /// return {i32*, float}. |
| static llvm::Type * |
| GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
| const llvm::DataLayout &TD) { |
| // In order to correctly satisfy the ABI, we need to the high part to start |
| // at offset 8. If the high and low parts we inferred are both 4-byte types |
| // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| // the second element at offset 8. Check for this: |
| unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| unsigned HiAlign = TD.getABITypeAlignment(Hi); |
| unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
| assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
| |
| // To handle this, we have to increase the size of the low part so that the |
| // second element will start at an 8 byte offset. We can't increase the size |
| // of the second element because it might make us access off the end of the |
| // struct. |
| if (HiStart != 8) { |
| // There are usually two sorts of types the ABI generation code can produce |
| // for the low part of a pair that aren't 8 bytes in size: float or |
| // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| // NaCl). |
| // Promote these to a larger type. |
| if (Lo->isFloatTy()) |
| Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| else { |
| assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| && "Invalid/unknown lo type"); |
| Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| } |
| } |
| |
| llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
| |
| |
| // Verify that the second element is at an 8-byte offset. |
| assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| "Invalid x86-64 argument pair!"); |
| return Result; |
| } |
| |
| ABIArgInfo X86_64ABIInfo:: |
| classifyReturnType(QualType RetTy) const { |
| // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| // classification algorithm. |
| X86_64ABIInfo::Class Lo, Hi; |
| classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
| |
| // Check some invariants. |
| assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| |
| llvm::Type *ResType = nullptr; |
| switch (Lo) { |
| case NoClass: |
| if (Hi == NoClass) |
| return ABIArgInfo::getIgnore(); |
| // If the low part is just padding, it takes no register, leave ResType |
| // null. |
| assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| "Unknown missing lo part"); |
| break; |
| |
| case SSEUp: |
| case X87Up: |
| llvm_unreachable("Invalid classification for lo word."); |
| |
| // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| // hidden argument. |
| case Memory: |
| return getIndirectReturnResult(RetTy); |
| |
| // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| // available register of the sequence %rax, %rdx is used. |
| case Integer: |
| ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
| |
| // If we have a sign or zero extended integer, make sure to return Extend |
| // so that the parameter gets the right LLVM IR attributes. |
| if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| // Treat an enum type as its underlying type. |
| if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| RetTy = EnumTy->getDecl()->getIntegerType(); |
| |
| if (RetTy->isIntegralOrEnumerationType() && |
| RetTy->isPromotableIntegerType()) |
| return ABIArgInfo::getExtend(); |
| } |
| break; |
| |
| // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| // available SSE register of the sequence %xmm0, %xmm1 is used. |
| case SSE: |
| ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
| break; |
| |
| // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| // returned on the X87 stack in %st0 as 80-bit x87 number. |
| case X87: |
| ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
| break; |
| |
| // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| // part of the value is returned in %st0 and the imaginary part in |
| // %st1. |
| case ComplexX87: |
| assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
| ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
| llvm::Type::getX86_FP80Ty(getVMContext()), |
| nullptr); |
| break; |
| } |
| |
| llvm::Type *HighPart = nullptr; |
| switch (Hi) { |
| // Memory was handled previously and X87 should |
| // never occur as a hi class. |
| case Memory: |
| case X87: |
| llvm_unreachable("Invalid classification for hi word."); |
| |
| case ComplexX87: // Previously handled. |
| case NoClass: |
| break; |
| |
| case Integer: |
| HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
| if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| return ABIArgInfo::getDirect(HighPart, 8); |
| break; |
| case SSE: |
| HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
| if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| return ABIArgInfo::getDirect(HighPart, 8); |
| break; |
| |
| // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
| // is passed in the next available eightbyte chunk if the last used |
| // vector register. |
| // |
| // SSEUP should always be preceded by SSE, just widen. |
| case SSEUp: |
| assert(Lo == SSE && "Unexpected SSEUp classification."); |
| ResType = GetByteVectorType(RetTy); |
| break; |
| |
| // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| // returned together with the previous X87 value in %st0. |
| case X87Up: |
| // If X87Up is preceded by X87, we don't need to do |
| // anything. However, in some cases with unions it may not be |
| // preceded by X87. In such situations we follow gcc and pass the |
| // extra bits in an SSE reg. |
| if (Lo != X87) { |
| HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
| if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| return ABIArgInfo::getDirect(HighPart, 8); |
| } |
| break; |
| } |
| |
| // If a high part was specified, merge it together with the low part. It is |
| // known to pass in the high eightbyte of the result. We do this by forming a |
| // first class struct aggregate with the high and low part: {low, high} |
| if (HighPart) |
| ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
| |
| return ABIArgInfo::getDirect(ResType); |
| } |
| |
| ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
| QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| bool isNamedArg) |
| const |
| { |
| Ty = useFirstFieldIfTransparentUnion(Ty); |
| |
| X86_64ABIInfo::Class Lo, Hi; |
| classify(Ty, 0, Lo, Hi, isNamedArg); |
| |
| // Check some invariants. |
| // FIXME: Enforce these by construction. |
| assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| |
| neededInt = 0; |
| neededSSE = 0; |
| llvm::Type *ResType = nullptr; |
| switch (Lo) { |
| case NoClass: |
| if (Hi == NoClass) |
| return ABIArgInfo::getIgnore(); |
| // If the low part is just padding, it takes no register, leave ResType |
| // null. |
| assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| "Unknown missing lo part"); |
| break; |
| |
| // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| // on the stack. |
| case Memory: |
| |
| // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| // COMPLEX_X87, it is passed in memory. |
| case X87: |
| case ComplexX87: |
| if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
| ++neededInt; |
| return |