================ @@ -0,0 +1,1424 @@ +//===- X86.cpp ------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ABI/FunctionInfo.h" +#include "llvm/ABI/TargetInfo.h" +#include "llvm/ABI/Types.h" +#include "llvm/Support/Alignment.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MathExtras.h" +#include "llvm/Support/TypeSize.h" +#include <algorithm> +#include <cassert> +#include <cstdint> + +namespace llvm { +namespace abi { + +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"); +} + +class X86_64TargetInfo : public TargetInfo { +public: + enum Class { Integer, Sse, SseUp, X87, X87Up, ComplexX87, NoClass, Memory }; + +private: + TypeBuilder &TB; + X86AVXABILevel AVXLevel; + bool Has64BitPointers; + + static Class merge(Class Accum, Class Field); + + void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; + + void classify(const Type *T, uint64_t OffsetBase, Class &Lo, Class &Hi, + bool IsNamedArg, bool IsRegCall = false) const; + + const Type *getIntegerTypeAtOffset(const Type *IRType, unsigned IROffset, + const Type *SourceTy, + unsigned SourceOffset, + bool InMemory = false) const; + + const Type *getSSETypeAtOffset(const Type *ABIType, unsigned ABIOffset, + const Type *SourceTy, + unsigned SourceOffset) const; + bool isIllegalVectorType(const Type *Ty) const; + bool containsMatrixField(const RecordType *RT) const; + + void computeInfo(FunctionInfo &FI) const override; + ArgInfo getIndirectReturnResult(const Type *Ty) const; + const Type *getFPTypeAtOffset(const Type *Ty, unsigned Offset) const; + + const Type *isSingleElementStruct(const Type *Ty) const; + const Type *getByteVectorType(const Type *Ty) const; + + const Type *createPairType(const Type *Lo, const Type *Hi) const; + ArgInfo getIndirectResult(const Type *Ty, unsigned FreeIntRegs) const; + + ArgInfo classifyReturnType(const Type *RetTy) const; + + ArgInfo classifyArgumentType(const Type *Ty, unsigned FreeIntRegs, + unsigned &NeededInt, unsigned &NeededSse, + bool IsNamedArg, bool IsRegCall = false) const; + const Type *useFirstFieldIfTransparentUnion(const Type *Ty) const; + +public: + X86_64TargetInfo(TypeBuilder &TypeBuilder, X86AVXABILevel AVXABILevel, + bool Has64BitPtrs, const ABICompatInfo &Compat) + : TargetInfo(Compat), TB(TypeBuilder), AVXLevel(AVXABILevel), + Has64BitPointers(Has64BitPtrs) {} + + bool has64BitPointers() const { return Has64BitPointers; } +}; + +// Gets the "best" type to represent the union. +static const Type *reduceUnionForX8664(const RecordType *UnionType, + TypeBuilder &TB) { + assert(UnionType->isUnion() && "Expected union type"); + + ArrayRef<FieldInfo> Fields = UnionType->getFields(); + if (Fields.empty()) { + return nullptr; + } + + const Type *StorageType = nullptr; + + for (const auto &Field : Fields) { + if (Field.IsBitField && Field.IsUnnamedBitfield && + Field.BitFieldWidth == 0) { + continue; + } + + const Type *FieldType = Field.FieldType; + + if (UnionType->isTransparentUnion() && !StorageType) { + StorageType = FieldType; + break; + } + + if (!StorageType || + FieldType->getAlignment() > StorageType->getAlignment() || + (FieldType->getAlignment() == StorageType->getAlignment() && + TypeSize::isKnownGT(FieldType->getSizeInBits(), + StorageType->getSizeInBits()))) { + StorageType = FieldType; + } + } + return StorageType; +} + +void X86_64TargetInfo::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 && getABICompatInfo().HonorsRevision98) + Lo = Memory; + if (AggregateSize > 128 && (Lo != Sse || Hi != SseUp)) + Lo = Memory; + if (Hi == SseUp && Lo != Sse) + Hi = Sse; +} +X86_64TargetInfo::Class X86_64TargetInfo::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; +} + +bool X86_64TargetInfo::containsMatrixField(const RecordType *RT) const { + for (const auto &Field : RT->getFields()) { + const Type *FieldType = Field.FieldType; + + if (const auto *AT = dyn_cast<ArrayType>(FieldType)) { + if (AT->isMatrixType()) + return true; + continue; + } + + if (const auto *NestedRT = dyn_cast<RecordType>(FieldType)) + if (containsMatrixField(NestedRT)) + return true; + } + return false; +} + +void X86_64TargetInfo::classify(const Type *T, uint64_t OffsetBase, Class &Lo, + Class &Hi, bool IsNamedArg, + bool IsRegCall) const { + Lo = Hi = NoClass; + Class &Current = OffsetBase < 64 ? Lo : Hi; + Current = Memory; + + if (T->isVoid()) { + Current = NoClass; + return; + } + + if (const auto *IT = dyn_cast<IntegerType>(T)) { + auto BitWidth = IT->getSizeInBits().getFixedValue(); + + if (BitWidth == 128 || + (IT->isBitInt() && BitWidth > 64 && BitWidth <= 128)) { + Lo = Integer; + Hi = Integer; + } else if (BitWidth <= 64) + Current = Integer; + + return; + } + + if (const auto *FT = dyn_cast<FloatType>(T)) { + const auto *FltSem = FT->getSemantics(); + + if (FltSem == &llvm::APFloat::IEEEsingle() || + FltSem == &llvm::APFloat::IEEEdouble() || + FltSem == &llvm::APFloat::IEEEhalf() || + FltSem == &llvm::APFloat::BFloat()) { + Current = Sse; + } else if (FltSem == &llvm::APFloat::IEEEquad()) { + Lo = Sse; + Hi = SseUp; + } else if (FltSem == &llvm::APFloat::x87DoubleExtended()) { + Lo = X87; + Hi = X87Up; + } else + Current = Sse; + return; + } + if (T->isPointer()) { + Current = Integer; + return; + } + + if (const auto *MPT = dyn_cast<MemberPointerType>(T)) { ---------------- nikic wrote:
This question also came up one some previous PR. If member pointers can be correctly handled by lowering it to a different type before it gets to the ABI library, then it would be fine to drop it as a dedicated type. I think we should leave it for now to keep things more aligned with clang code, but we can drop it later (esp. after we also have MSVC ABI handling). https://github.com/llvm/llvm-project/pull/194718 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
