Path: blob/main/contrib/llvm-project/clang/lib/Basic/Targets/PPC.h
35266 views
//===--- PPC.h - Declare PPC target feature support -------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file declares PPC TargetInfo objects.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H13#define LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H1415#include "OSTargets.h"16#include "clang/Basic/TargetInfo.h"17#include "clang/Basic/TargetOptions.h"18#include "llvm/ADT/StringSwitch.h"19#include "llvm/Support/Compiler.h"20#include "llvm/TargetParser/Triple.h"2122namespace clang {23namespace targets {2425// PPC abstract base class26class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {2728/// Flags for architecture specific defines.29typedef enum {30ArchDefineNone = 0,31ArchDefineName = 1 << 0, // <name> is substituted for arch name.32ArchDefinePpcgr = 1 << 1,33ArchDefinePpcsq = 1 << 2,34ArchDefine440 = 1 << 3,35ArchDefine603 = 1 << 4,36ArchDefine604 = 1 << 5,37ArchDefinePwr4 = 1 << 6,38ArchDefinePwr5 = 1 << 7,39ArchDefinePwr5x = 1 << 8,40ArchDefinePwr6 = 1 << 9,41ArchDefinePwr6x = 1 << 10,42ArchDefinePwr7 = 1 << 11,43ArchDefinePwr8 = 1 << 12,44ArchDefinePwr9 = 1 << 13,45ArchDefinePwr10 = 1 << 14,46ArchDefinePwr11 = 1 << 15,47ArchDefineFuture = 1 << 16,48ArchDefineA2 = 1 << 17,49ArchDefineE500 = 1 << 1850} ArchDefineTypes;5152ArchDefineTypes ArchDefs = ArchDefineNone;53static const char *const GCCRegNames[];54static const TargetInfo::GCCRegAlias GCCRegAliases[];55std::string CPU;56enum PPCFloatABI { HardFloat, SoftFloat } FloatABI;5758// Target cpu features.59bool HasAltivec = false;60bool HasMMA = false;61bool HasROPProtect = false;62bool HasPrivileged = false;63bool HasAIXSmallLocalExecTLS = false;64bool HasAIXSmallLocalDynamicTLS = false;65bool HasVSX = false;66bool UseCRBits = false;67bool HasP8Vector = false;68bool HasP8Crypto = false;69bool HasDirectMove = false;70bool HasHTM = false;71bool HasBPERMD = false;72bool HasExtDiv = false;73bool HasP9Vector = false;74bool HasSPE = false;75bool PairedVectorMemops = false;76bool HasP10Vector = false;77bool HasPCRelativeMemops = false;78bool HasPrefixInstrs = false;79bool IsISA2_06 = false;80bool IsISA2_07 = false;81bool IsISA3_0 = false;82bool IsISA3_1 = false;83bool HasQuadwordAtomics = false;84bool HasAIXShLibTLSModelOpt = false;85bool UseLongCalls = false;8687protected:88std::string ABI;8990public:91PPCTargetInfo(const llvm::Triple &Triple, const TargetOptions &)92: TargetInfo(Triple) {93SuitableAlign = 128;94LongDoubleWidth = LongDoubleAlign = 128;95LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble();96HasStrictFP = true;97HasIbm128 = true;98HasUnalignedAccess = true;99}100101// Set the language option for altivec based on our value.102void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override;103104// Note: GCC recognizes the following additional cpus:105// 401, 403, 405, 405fp, 440fp, 464, 464fp, 476, 476fp, 505, 740, 801,106// 821, 823, 8540, e300c2, e300c3, e500mc64, e6500, 860, cell, titan, rs64.107bool isValidCPUName(StringRef Name) const override;108void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;109110bool setCPU(const std::string &Name) override {111bool CPUKnown = isValidCPUName(Name);112if (CPUKnown) {113CPU = Name;114115// CPU identification.116ArchDefs =117(ArchDefineTypes)llvm::StringSwitch<int>(CPU)118.Case("440", ArchDefineName)119.Case("450", ArchDefineName | ArchDefine440)120.Case("601", ArchDefineName)121.Case("602", ArchDefineName | ArchDefinePpcgr)122.Case("603", ArchDefineName | ArchDefinePpcgr)123.Case("603e", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)124.Case("603ev", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)125.Case("604", ArchDefineName | ArchDefinePpcgr)126.Case("604e", ArchDefineName | ArchDefine604 | ArchDefinePpcgr)127.Case("620", ArchDefineName | ArchDefinePpcgr)128.Case("630", ArchDefineName | ArchDefinePpcgr)129.Case("7400", ArchDefineName | ArchDefinePpcgr)130.Case("7450", ArchDefineName | ArchDefinePpcgr)131.Case("750", ArchDefineName | ArchDefinePpcgr)132.Case("970", ArchDefineName | ArchDefinePwr4 | ArchDefinePpcgr |133ArchDefinePpcsq)134.Case("a2", ArchDefineA2)135.Cases("power3", "pwr3", ArchDefinePpcgr)136.Cases("power4", "pwr4",137ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)138.Cases("power5", "pwr5",139ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |140ArchDefinePpcsq)141.Cases("power5x", "pwr5x",142ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |143ArchDefinePpcgr | ArchDefinePpcsq)144.Cases("power6", "pwr6",145ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |146ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)147.Cases("power6x", "pwr6x",148ArchDefinePwr6x | ArchDefinePwr6 | ArchDefinePwr5x |149ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |150ArchDefinePpcsq)151.Cases("power7", "pwr7",152ArchDefinePwr7 | ArchDefinePwr6 | ArchDefinePwr5x |153ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |154ArchDefinePpcsq)155// powerpc64le automatically defaults to at least power8.156.Cases("power8", "pwr8", "ppc64le",157ArchDefinePwr8 | ArchDefinePwr7 | ArchDefinePwr6 |158ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |159ArchDefinePpcgr | ArchDefinePpcsq)160.Cases("power9", "pwr9",161ArchDefinePwr9 | ArchDefinePwr8 | ArchDefinePwr7 |162ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |163ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)164.Cases("power10", "pwr10",165ArchDefinePwr10 | ArchDefinePwr9 | ArchDefinePwr8 |166ArchDefinePwr7 | ArchDefinePwr6 | ArchDefinePwr5x |167ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |168ArchDefinePpcsq)169.Cases("power11", "pwr11",170ArchDefinePwr11 | ArchDefinePwr10 | ArchDefinePwr9 |171ArchDefinePwr8 | ArchDefinePwr7 | ArchDefinePwr6 |172ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |173ArchDefinePpcgr | ArchDefinePpcsq)174.Case("future",175ArchDefineFuture | ArchDefinePwr11 | ArchDefinePwr10 |176ArchDefinePwr9 | ArchDefinePwr8 | ArchDefinePwr7 |177ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |178ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)179.Cases("8548", "e500", ArchDefineE500)180.Default(ArchDefineNone);181}182return CPUKnown;183}184185StringRef getABI() const override { return ABI; }186187ArrayRef<Builtin::Info> getTargetBuiltins() const override;188189bool isCLZForZeroUndef() const override { return false; }190191void getTargetDefines(const LangOptions &Opts,192MacroBuilder &Builder) const override;193194bool195initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,196StringRef CPU,197const std::vector<std::string> &FeaturesVec) const override;198199void addP10SpecificFeatures(llvm::StringMap<bool> &Features) const;200void addP11SpecificFeatures(llvm::StringMap<bool> &Features) const;201void addFutureSpecificFeatures(llvm::StringMap<bool> &Features) const;202203bool handleTargetFeatures(std::vector<std::string> &Features,204DiagnosticsEngine &Diags) override;205206bool hasFeature(StringRef Feature) const override;207208void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,209bool Enabled) const override;210211bool supportsTargetAttributeTune() const override { return true; }212213ArrayRef<const char *> getGCCRegNames() const override;214215ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;216217ArrayRef<TargetInfo::AddlRegName> getGCCAddlRegNames() const override;218219bool validateAsmConstraint(const char *&Name,220TargetInfo::ConstraintInfo &Info) const override {221switch (*Name) {222default:223return false;224case 'O': // Zero225break;226case 'f': // Floating point register227// Don't use floating point registers on soft float ABI.228if (FloatABI == SoftFloat)229return false;230[[fallthrough]];231case 'b': // Base register232Info.setAllowsRegister();233break;234// FIXME: The following are added to allow parsing.235// I just took a guess at what the actions should be.236// Also, is more specific checking needed? I.e. specific registers?237case 'd': // Floating point register (containing 64-bit value)238case 'v': // Altivec vector register239// Don't use floating point and altivec vector registers240// on soft float ABI241if (FloatABI == SoftFloat)242return false;243Info.setAllowsRegister();244break;245case 'w':246switch (Name[1]) {247case 'd': // VSX vector register to hold vector double data248case 'f': // VSX vector register to hold vector float data249case 's': // VSX vector register to hold scalar double data250case 'w': // VSX vector register to hold scalar double data251case 'a': // Any VSX register252case 'c': // An individual CR bit253case 'i': // FP or VSX register to hold 64-bit integers data254break;255default:256return false;257}258Info.setAllowsRegister();259Name++; // Skip over 'w'.260break;261case 'h': // `MQ', `CTR', or `LINK' register262case 'q': // `MQ' register263case 'c': // `CTR' register264case 'l': // `LINK' register265case 'x': // `CR' register (condition register) number 0266case 'y': // `CR' register (condition register)267case 'z': // `XER[CA]' carry bit (part of the XER register)268Info.setAllowsRegister();269break;270case 'I': // Signed 16-bit constant271case 'J': // Unsigned 16-bit constant shifted left 16 bits272// (use `L' instead for SImode constants)273case 'K': // Unsigned 16-bit constant274case 'L': // Signed 16-bit constant shifted left 16 bits275case 'M': // Constant larger than 31276case 'N': // Exact power of 2277case 'P': // Constant whose negation is a signed 16-bit constant278case 'G': // Floating point constant that can be loaded into a279// register with one instruction per word280case 'H': // Integer/Floating point constant that can be loaded281// into a register using three instructions282break;283case 'm': // Memory operand. Note that on PowerPC targets, m can284// include addresses that update the base register. It285// is therefore only safe to use `m' in an asm statement286// if that asm statement accesses the operand exactly once.287// The asm statement must also use `%U<opno>' as a288// placeholder for the "update" flag in the corresponding289// load or store instruction. For example:290// asm ("st%U0 %1,%0" : "=m" (mem) : "r" (val));291// is correct but:292// asm ("st %1,%0" : "=m" (mem) : "r" (val));293// is not. Use es rather than m if you don't want the base294// register to be updated.295case 'e':296if (Name[1] != 's')297return false;298// es: A "stable" memory operand; that is, one which does not299// include any automodification of the base register. Unlike300// `m', this constraint can be used in asm statements that301// might access the operand several times, or that might not302// access it at all.303Info.setAllowsMemory();304Name++; // Skip over 'e'.305break;306case 'Q': // Memory operand that is an offset from a register (it is307// usually better to use `m' or `es' in asm statements)308Info.setAllowsRegister();309[[fallthrough]];310case 'Z': // Memory operand that is an indexed or indirect from a311// register (it is usually better to use `m' or `es' in312// asm statements)313Info.setAllowsMemory();314break;315case 'a': // Address operand that is an indexed or indirect from a316// register (`p' is preferable for asm statements)317// TODO: Add full support for this constraint318return false;319case 'R': // AIX TOC entry320case 'S': // Constant suitable as a 64-bit mask operand321case 'T': // Constant suitable as a 32-bit mask operand322case 'U': // System V Release 4 small data area reference323case 't': // AND masks that can be performed by two rldic{l, r}324// instructions325case 'W': // Vector constant that does not require memory326case 'j': // Vector constant that is all zeros.327break;328// End FIXME.329}330return true;331}332333std::string convertConstraint(const char *&Constraint) const override {334std::string R;335switch (*Constraint) {336case 'e':337case 'w':338// Two-character constraint; add "^" hint for later parsing.339R = std::string("^") + std::string(Constraint, 2);340Constraint++;341break;342default:343return TargetInfo::convertConstraint(Constraint);344}345return R;346}347348std::string_view getClobbers() const override { return ""; }349int getEHDataRegisterNumber(unsigned RegNo) const override {350if (RegNo == 0)351return 3;352if (RegNo == 1)353return 4;354return -1;355}356357bool hasSjLjLowering() const override { return true; }358359const char *getLongDoubleMangling() const override {360if (LongDoubleWidth == 64)361return "e";362return LongDoubleFormat == &llvm::APFloat::PPCDoubleDouble()363? "g"364: "u9__ieee128";365}366const char *getFloat128Mangling() const override { return "u9__ieee128"; }367const char *getIbm128Mangling() const override { return "g"; }368369bool hasBitIntType() const override { return true; }370371bool isSPRegName(StringRef RegName) const override {372return RegName == "r1" || RegName == "x1";373}374375// We support __builtin_cpu_supports/__builtin_cpu_is on targets that376// have Glibc since it is Glibc that provides the HWCAP[2] in the auxv.377static constexpr int MINIMUM_AIX_OS_MAJOR = 7;378static constexpr int MINIMUM_AIX_OS_MINOR = 2;379bool supportsCpuSupports() const override {380llvm::Triple Triple = getTriple();381// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports().382return Triple.isOSGlibc() ||383(Triple.isOSAIX() &&384!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR));385}386387bool supportsCpuIs() const override {388llvm::Triple Triple = getTriple();389// AIX 7.2 is the minimum requirement to support __builtin_cpu_is().390return Triple.isOSGlibc() ||391(Triple.isOSAIX() &&392!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR));393}394bool validateCpuSupports(StringRef Feature) const override;395bool validateCpuIs(StringRef Name) const override;396};397398class LLVM_LIBRARY_VISIBILITY PPC32TargetInfo : public PPCTargetInfo {399public:400PPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)401: PPCTargetInfo(Triple, Opts) {402if (Triple.isOSAIX())403resetDataLayout("E-m:a-p:32:32-Fi32-i64:64-n32");404else if (Triple.getArch() == llvm::Triple::ppcle)405resetDataLayout("e-m:e-p:32:32-Fn32-i64:64-n32");406else407resetDataLayout("E-m:e-p:32:32-Fn32-i64:64-n32");408409switch (getTriple().getOS()) {410case llvm::Triple::Linux:411case llvm::Triple::FreeBSD:412case llvm::Triple::NetBSD:413SizeType = UnsignedInt;414PtrDiffType = SignedInt;415IntPtrType = SignedInt;416break;417case llvm::Triple::AIX:418SizeType = UnsignedLong;419PtrDiffType = SignedLong;420IntPtrType = SignedLong;421LongDoubleWidth = 64;422LongDoubleAlign = DoubleAlign = 32;423LongDoubleFormat = &llvm::APFloat::IEEEdouble();424break;425default:426break;427}428429if (Triple.isOSFreeBSD() || Triple.isOSNetBSD() || Triple.isOSOpenBSD() ||430Triple.isMusl()) {431LongDoubleWidth = LongDoubleAlign = 64;432LongDoubleFormat = &llvm::APFloat::IEEEdouble();433}434435// PPC32 supports atomics up to 4 bytes.436MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;437}438439BuiltinVaListKind getBuiltinVaListKind() const override {440// This is the ELF definition441return TargetInfo::PowerABIBuiltinVaList;442}443444std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {445return std::make_pair(32, 32);446}447};448449// Note: ABI differences may eventually require us to have a separate450// TargetInfo for little endian.451class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {452public:453PPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)454: PPCTargetInfo(Triple, Opts) {455LongWidth = LongAlign = PointerWidth = PointerAlign = 64;456IntMaxType = SignedLong;457Int64Type = SignedLong;458std::string DataLayout;459460if (Triple.isOSAIX()) {461// TODO: Set appropriate ABI for AIX platform.462DataLayout = "E-m:a-Fi64-i64:64-n32:64";463LongDoubleWidth = 64;464LongDoubleAlign = DoubleAlign = 32;465LongDoubleFormat = &llvm::APFloat::IEEEdouble();466} else if ((Triple.getArch() == llvm::Triple::ppc64le)) {467DataLayout = "e-m:e-Fn32-i64:64-n32:64";468ABI = "elfv2";469} else {470DataLayout = "E-m:e";471if (Triple.isPPC64ELFv2ABI()) {472ABI = "elfv2";473DataLayout += "-Fn32";474} else {475ABI = "elfv1";476DataLayout += "-Fi64";477}478DataLayout += "-i64:64-n32:64";479}480481if (Triple.isOSFreeBSD() || Triple.isOSOpenBSD() || Triple.isMusl()) {482LongDoubleWidth = LongDoubleAlign = 64;483LongDoubleFormat = &llvm::APFloat::IEEEdouble();484}485486if (Triple.isOSAIX() || Triple.isOSLinux())487DataLayout += "-S128-v256:256:256-v512:512:512";488resetDataLayout(DataLayout);489490// Newer PPC64 instruction sets support atomics up to 16 bytes.491MaxAtomicPromoteWidth = 128;492// Baseline PPC64 supports inlining atomics up to 8 bytes.493MaxAtomicInlineWidth = 64;494}495496void setMaxAtomicWidth() override {497// For power8 and up, backend is able to inline 16-byte atomic lock free498// code.499// TODO: We should allow AIX to inline quadword atomics in the future.500if (!getTriple().isOSAIX() && hasFeature("quadword-atomics"))501MaxAtomicInlineWidth = 128;502}503504BuiltinVaListKind getBuiltinVaListKind() const override {505return TargetInfo::CharPtrBuiltinVaList;506}507508// PPC64 Linux-specific ABI options.509bool setABI(const std::string &Name) override {510if (Name == "elfv1" || Name == "elfv2") {511ABI = Name;512return true;513}514return false;515}516517CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {518switch (CC) {519case CC_Swift:520return CCCR_OK;521case CC_SwiftAsync:522return CCCR_Error;523default:524return CCCR_Warning;525}526}527528std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {529return std::make_pair(128, 128);530}531};532533class LLVM_LIBRARY_VISIBILITY AIXPPC32TargetInfo :534public AIXTargetInfo<PPC32TargetInfo> {535public:536using AIXTargetInfo::AIXTargetInfo;537BuiltinVaListKind getBuiltinVaListKind() const override {538return TargetInfo::CharPtrBuiltinVaList;539}540};541542class LLVM_LIBRARY_VISIBILITY AIXPPC64TargetInfo :543public AIXTargetInfo<PPC64TargetInfo> {544public:545using AIXTargetInfo::AIXTargetInfo;546};547548} // namespace targets549} // namespace clang550#endif // LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H551552553