1//===-- llvm/TargetParser/Triple.h - Target triple helper class--*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_TARGETPARSER_TRIPLE_H
10#define LLVM_TARGETPARSER_TRIPLE_H
11
12#include "llvm/ADT/Twine.h"
13#include "llvm/Support/VersionTuple.h"
14
15// Some system headers or GCC predefined macros conflict with identifiers in
16// this file.  Undefine them here.
17#undef NetBSD
18#undef mips
19#undef sparc
20
21namespace llvm {
22
23/// Triple - Helper class for working with autoconf configuration names. For
24/// historical reasons, we also call these 'triples' (they used to contain
25/// exactly three fields).
26///
27/// Configuration names are strings in the canonical form:
28///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
29/// or
30///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
31///
32/// This class is used for clients which want to support arbitrary
33/// configuration names, but also want to implement certain special
34/// behavior for particular configurations. This class isolates the mapping
35/// from the components of the configuration name to well known IDs.
36///
37/// At its core the Triple class is designed to be a wrapper for a triple
38/// string; the constructor does not change or normalize the triple string.
39/// Clients that need to handle the non-canonical triples that users often
40/// specify should use the normalize method.
41///
42/// See autoconf/config.guess for a glimpse into what configuration names
43/// look like in practice.
44class Triple {
45public:
46  enum ArchType {
47    UnknownArch,
48
49    arm,            // ARM (little endian): arm, armv.*, xscale
50    armeb,          // ARM (big endian): armeb
51    aarch64,        // AArch64 (little endian): aarch64
52    aarch64_be,     // AArch64 (big endian): aarch64_be
53    aarch64_32,     // AArch64 (little endian) ILP32: aarch64_32
54    arc,            // ARC: Synopsys ARC
55    avr,            // AVR: Atmel AVR microcontroller
56    bpfel,          // eBPF or extended BPF or 64-bit BPF (little endian)
57    bpfeb,          // eBPF or extended BPF or 64-bit BPF (big endian)
58    csky,           // CSKY: csky
59    dxil,           // DXIL 32-bit DirectX bytecode
60    hexagon,        // Hexagon: hexagon
61    loongarch32,    // LoongArch (32-bit): loongarch32
62    loongarch64,    // LoongArch (64-bit): loongarch64
63    m68k,           // M68k: Motorola 680x0 family
64    mips,           // MIPS: mips, mipsallegrex, mipsr6
65    mipsel,         // MIPSEL: mipsel, mipsallegrexe, mipsr6el
66    mips64,         // MIPS64: mips64, mips64r6, mipsn32, mipsn32r6
67    mips64el,       // MIPS64EL: mips64el, mips64r6el, mipsn32el, mipsn32r6el
68    msp430,         // MSP430: msp430
69    ppc,            // PPC: powerpc
70    ppcle,          // PPCLE: powerpc (little endian)
71    ppc64,          // PPC64: powerpc64, ppu
72    ppc64le,        // PPC64LE: powerpc64le
73    r600,           // R600: AMD GPUs HD2XXX - HD6XXX
74    amdgcn,         // AMDGCN: AMD GCN GPUs
75    riscv32,        // RISC-V (32-bit): riscv32
76    riscv64,        // RISC-V (64-bit): riscv64
77    sparc,          // Sparc: sparc
78    sparcv9,        // Sparcv9: Sparcv9
79    sparcel,        // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
80    systemz,        // SystemZ: s390x
81    tce,            // TCE (http://tce.cs.tut.fi/): tce
82    tcele,          // TCE little endian (http://tce.cs.tut.fi/): tcele
83    thumb,          // Thumb (little endian): thumb, thumbv.*
84    thumbeb,        // Thumb (big endian): thumbeb
85    x86,            // X86: i[3-9]86
86    x86_64,         // X86-64: amd64, x86_64
87    xcore,          // XCore: xcore
88    xtensa,         // Tensilica: Xtensa
89    nvptx,          // NVPTX: 32-bit
90    nvptx64,        // NVPTX: 64-bit
91    le32,           // le32: generic little-endian 32-bit CPU (PNaCl)
92    le64,           // le64: generic little-endian 64-bit CPU (PNaCl)
93    amdil,          // AMDIL
94    amdil64,        // AMDIL with 64-bit pointers
95    hsail,          // AMD HSAIL
96    hsail64,        // AMD HSAIL with 64-bit pointers
97    spir,           // SPIR: standard portable IR for OpenCL 32-bit version
98    spir64,         // SPIR: standard portable IR for OpenCL 64-bit version
99    spirv32,        // SPIR-V with 32-bit pointers
100    spirv64,        // SPIR-V with 64-bit pointers
101    kalimba,        // Kalimba: generic kalimba
102    shave,          // SHAVE: Movidius vector VLIW processors
103    lanai,          // Lanai: Lanai 32-bit
104    wasm32,         // WebAssembly with 32-bit pointers
105    wasm64,         // WebAssembly with 64-bit pointers
106    renderscript32, // 32-bit RenderScript
107    renderscript64, // 64-bit RenderScript
108    ve,             // NEC SX-Aurora Vector Engine
109    LastArchType = ve
110  };
111  enum SubArchType {
112    NoSubArch,
113
114    ARMSubArch_v9_4a,
115    ARMSubArch_v9_3a,
116    ARMSubArch_v9_2a,
117    ARMSubArch_v9_1a,
118    ARMSubArch_v9,
119    ARMSubArch_v8_9a,
120    ARMSubArch_v8_8a,
121    ARMSubArch_v8_7a,
122    ARMSubArch_v8_6a,
123    ARMSubArch_v8_5a,
124    ARMSubArch_v8_4a,
125    ARMSubArch_v8_3a,
126    ARMSubArch_v8_2a,
127    ARMSubArch_v8_1a,
128    ARMSubArch_v8,
129    ARMSubArch_v8r,
130    ARMSubArch_v8m_baseline,
131    ARMSubArch_v8m_mainline,
132    ARMSubArch_v8_1m_mainline,
133    ARMSubArch_v7,
134    ARMSubArch_v7em,
135    ARMSubArch_v7m,
136    ARMSubArch_v7s,
137    ARMSubArch_v7k,
138    ARMSubArch_v7ve,
139    ARMSubArch_v6,
140    ARMSubArch_v6m,
141    ARMSubArch_v6k,
142    ARMSubArch_v6t2,
143    ARMSubArch_v5,
144    ARMSubArch_v5te,
145    ARMSubArch_v4t,
146
147    AArch64SubArch_arm64e,
148    AArch64SubArch_arm64ec,
149
150    KalimbaSubArch_v3,
151    KalimbaSubArch_v4,
152    KalimbaSubArch_v5,
153
154    MipsSubArch_r6,
155
156    PPCSubArch_spe,
157
158    // SPIR-V sub-arch corresponds to its version.
159    SPIRVSubArch_v10,
160    SPIRVSubArch_v11,
161    SPIRVSubArch_v12,
162    SPIRVSubArch_v13,
163    SPIRVSubArch_v14,
164    SPIRVSubArch_v15,
165  };
166  enum VendorType {
167    UnknownVendor,
168
169    Apple,
170    PC,
171    SCEI,
172    Freescale,
173    IBM,
174    ImaginationTechnologies,
175    MipsTechnologies,
176    NVIDIA,
177    CSR,
178    Myriad,
179    AMD,
180    Mesa,
181    SUSE,
182    OpenEmbedded,
183    LastVendorType = OpenEmbedded
184  };
185  enum OSType {
186    UnknownOS,
187
188    Ananas,
189    CloudABI,
190    Darwin,
191    DragonFly,
192    FreeBSD,
193    Fuchsia,
194    IOS,
195    KFreeBSD,
196    Linux,
197    Lv2,        // PS3
198    MacOSX,
199    NetBSD,
200    OpenBSD,
201    Solaris,
202    Win32,
203    ZOS,
204    Haiku,
205    Minix,
206    RTEMS,
207    NaCl,       // Native Client
208    AIX,
209    CUDA,       // NVIDIA CUDA
210    NVCL,       // NVIDIA OpenCL
211    AMDHSA,     // AMD HSA Runtime
212    PS4,
213    PS5,
214    ELFIAMCU,
215    TvOS,       // Apple tvOS
216    WatchOS,    // Apple watchOS
217    DriverKit,  // Apple DriverKit
218    Mesa3D,
219    Contiki,
220    AMDPAL,     // AMD PAL Runtime
221    HermitCore, // HermitCore Unikernel/Multikernel
222    Hurd,       // GNU/Hurd
223    WASI,       // Experimental WebAssembly OS
224    Emscripten,
225    ShaderModel, // DirectX ShaderModel
226    LastOSType = ShaderModel
227  };
228  enum EnvironmentType {
229    UnknownEnvironment,
230
231    GNU,
232    GNUABIN32,
233    GNUABI64,
234    GNUEABI,
235    GNUEABIHF,
236    GNUF32,
237    GNUF64,
238    GNUSF,
239    GNUX32,
240    GNUILP32,
241    CODE16,
242    EABI,
243    EABIHF,
244    Android,
245    Musl,
246    MuslEABI,
247    MuslEABIHF,
248    MuslX32,
249
250    MSVC,
251    Itanium,
252    Cygnus,
253    CoreCLR,
254    Simulator, // Simulator variants of other systems, e.g., Apple's iOS
255    MacABI, // Mac Catalyst variant of Apple's iOS deployment target.
256
257    // Shader Stages
258    // The order of these values matters, and must be kept in sync with the
259    // language options enum in Clang. The ordering is enforced in
260    // static_asserts in Triple.cpp and in Clang.
261    Pixel,
262    Vertex,
263    Geometry,
264    Hull,
265    Domain,
266    Compute,
267    Library,
268    RayGeneration,
269    Intersection,
270    AnyHit,
271    ClosestHit,
272    Miss,
273    Callable,
274    Mesh,
275    Amplification,
276
277    LastEnvironmentType = Amplification
278  };
279  enum ObjectFormatType {
280    UnknownObjectFormat,
281
282    COFF,
283    DXContainer,
284    ELF,
285    GOFF,
286    MachO,
287    SPIRV,
288    Wasm,
289    XCOFF,
290  };
291
292private:
293  std::string Data;
294
295  /// The parsed arch type.
296  ArchType Arch{};
297
298  /// The parsed subarchitecture type.
299  SubArchType SubArch{};
300
301  /// The parsed vendor type.
302  VendorType Vendor{};
303
304  /// The parsed OS type.
305  OSType OS{};
306
307  /// The parsed Environment type.
308  EnvironmentType Environment{};
309
310  /// The object format type.
311  ObjectFormatType ObjectFormat{};
312
313public:
314  /// @name Constructors
315  /// @{
316
317  /// Default constructor is the same as an empty string and leaves all
318  /// triple fields unknown.
319  Triple() = default;
320
321  explicit Triple(const Twine &Str);
322  Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
323  Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
324         const Twine &EnvironmentStr);
325
326  bool operator==(const Triple &Other) const {
327    return Arch == Other.Arch && SubArch == Other.SubArch &&
328           Vendor == Other.Vendor && OS == Other.OS &&
329           Environment == Other.Environment &&
330           ObjectFormat == Other.ObjectFormat;
331  }
332
333  bool operator!=(const Triple &Other) const {
334    return !(*this == Other);
335  }
336
337  /// @}
338  /// @name Normalization
339  /// @{
340
341  /// Turn an arbitrary machine specification into the canonical triple form (or
342  /// something sensible that the Triple class understands if nothing better can
343  /// reasonably be done).  In particular, it handles the common case in which
344  /// otherwise valid components are in the wrong order.
345  static std::string normalize(StringRef Str);
346
347  /// Return the normalized form of this triple's string.
348  std::string normalize() const { return normalize(Data); }
349
350  /// @}
351  /// @name Typed Component Access
352  /// @{
353
354  /// Get the parsed architecture type of this triple.
355  ArchType getArch() const { return Arch; }
356
357  /// get the parsed subarchitecture type for this triple.
358  SubArchType getSubArch() const { return SubArch; }
359
360  /// Get the parsed vendor type of this triple.
361  VendorType getVendor() const { return Vendor; }
362
363  /// Get the parsed operating system type of this triple.
364  OSType getOS() const { return OS; }
365
366  /// Does this triple have the optional environment (fourth) component?
367  bool hasEnvironment() const {
368    return getEnvironmentName() != "";
369  }
370
371  /// Get the parsed environment type of this triple.
372  EnvironmentType getEnvironment() const { return Environment; }
373
374  /// Parse the version number from the OS name component of the
375  /// triple, if present.
376  ///
377  /// For example, "fooos1.2.3" would return (1, 2, 3).
378  VersionTuple getEnvironmentVersion() const;
379
380  /// Get the object format for this triple.
381  ObjectFormatType getObjectFormat() const { return ObjectFormat; }
382
383  /// Parse the version number from the OS name component of the triple, if
384  /// present.
385  ///
386  /// For example, "fooos1.2.3" would return (1, 2, 3).
387  VersionTuple getOSVersion() const;
388
389  /// Return just the major version number, this is specialized because it is a
390  /// common query.
391  unsigned getOSMajorVersion() const { return getOSVersion().getMajor(); }
392
393  /// Parse the version number as with getOSVersion and then translate generic
394  /// "darwin" versions to the corresponding OS X versions.  This may also be
395  /// called with IOS triples but the OS X version number is just set to a
396  /// constant 10.4.0 in that case.  Returns true if successful.
397  bool getMacOSXVersion(VersionTuple &Version) const;
398
399  /// Parse the version number as with getOSVersion.  This should only be called
400  /// with IOS or generic triples.
401  VersionTuple getiOSVersion() const;
402
403  /// Parse the version number as with getOSVersion.  This should only be called
404  /// with WatchOS or generic triples.
405  VersionTuple getWatchOSVersion() const;
406
407  /// Parse the version number as with getOSVersion.
408  VersionTuple getDriverKitVersion() const;
409
410  /// @}
411  /// @name Direct Component Access
412  /// @{
413
414  const std::string &str() const { return Data; }
415
416  const std::string &getTriple() const { return Data; }
417
418  /// Get the architecture (first) component of the triple.
419  StringRef getArchName() const;
420
421  /// Get the architecture name based on Kind and SubArch.
422  StringRef getArchName(ArchType Kind, SubArchType SubArch = NoSubArch) const;
423
424  /// Get the vendor (second) component of the triple.
425  StringRef getVendorName() const;
426
427  /// Get the operating system (third) component of the triple.
428  StringRef getOSName() const;
429
430  /// Get the optional environment (fourth) component of the triple, or "" if
431  /// empty.
432  StringRef getEnvironmentName() const;
433
434  /// Get the operating system and optional environment components as a single
435  /// string (separated by a '-' if the environment component is present).
436  StringRef getOSAndEnvironmentName() const;
437
438  /// @}
439  /// @name Convenience Predicates
440  /// @{
441
442  /// Test whether the architecture is 64-bit
443  ///
444  /// Note that this tests for 64-bit pointer width, and nothing else. Note
445  /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
446  /// 16-bit. The inner details of pointer width for particular architectures
447  /// is not summed up in the triple, and so only a coarse grained predicate
448  /// system is provided.
449  bool isArch64Bit() const;
450
451  /// Test whether the architecture is 32-bit
452  ///
453  /// Note that this tests for 32-bit pointer width, and nothing else.
454  bool isArch32Bit() const;
455
456  /// Test whether the architecture is 16-bit
457  ///
458  /// Note that this tests for 16-bit pointer width, and nothing else.
459  bool isArch16Bit() const;
460
461  /// Helper function for doing comparisons against version numbers included in
462  /// the target triple.
463  bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
464                     unsigned Micro = 0) const {
465    if (Minor == 0) {
466      return getOSVersion() < VersionTuple(Major);
467    }
468    if (Micro == 0) {
469      return getOSVersion() < VersionTuple(Major, Minor);
470    }
471    return getOSVersion() < VersionTuple(Major, Minor, Micro);
472  }
473
474  bool isOSVersionLT(const Triple &Other) const {
475    return getOSVersion() < Other.getOSVersion();
476  }
477
478  /// Comparison function for checking OS X version compatibility, which handles
479  /// supporting skewed version numbering schemes used by the "darwin" triples.
480  bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
481                         unsigned Micro = 0) const;
482
483  /// Is this a Mac OS X triple. For legacy reasons, we support both "darwin"
484  /// and "osx" as OS X triples.
485  bool isMacOSX() const {
486    return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
487  }
488
489  /// Is this an iOS triple.
490  /// Note: This identifies tvOS as a variant of iOS. If that ever
491  /// changes, i.e., if the two operating systems diverge or their version
492  /// numbers get out of sync, that will need to be changed.
493  /// watchOS has completely different version numbers so it is not included.
494  bool isiOS() const {
495    return getOS() == Triple::IOS || isTvOS();
496  }
497
498  /// Is this an Apple tvOS triple.
499  bool isTvOS() const {
500    return getOS() == Triple::TvOS;
501  }
502
503  /// Is this an Apple watchOS triple.
504  bool isWatchOS() const {
505    return getOS() == Triple::WatchOS;
506  }
507
508  bool isWatchABI() const {
509    return getSubArch() == Triple::ARMSubArch_v7k;
510  }
511
512  /// Is this an Apple DriverKit triple.
513  bool isDriverKit() const { return getOS() == Triple::DriverKit; }
514
515  bool isOSzOS() const { return getOS() == Triple::ZOS; }
516
517  /// Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, or DriverKit).
518  bool isOSDarwin() const {
519    return isMacOSX() || isiOS() || isWatchOS() || isDriverKit();
520  }
521
522  bool isSimulatorEnvironment() const {
523    return getEnvironment() == Triple::Simulator;
524  }
525
526  bool isMacCatalystEnvironment() const {
527    return getEnvironment() == Triple::MacABI;
528  }
529
530  /// Returns true for targets that run on a macOS machine.
531  bool isTargetMachineMac() const {
532    return isMacOSX() || (isOSDarwin() && (isSimulatorEnvironment() ||
533                                           isMacCatalystEnvironment()));
534  }
535
536  bool isOSNetBSD() const {
537    return getOS() == Triple::NetBSD;
538  }
539
540  bool isOSOpenBSD() const {
541    return getOS() == Triple::OpenBSD;
542  }
543
544  bool isOSFreeBSD() const {
545    return getOS() == Triple::FreeBSD;
546  }
547
548  bool isOSFuchsia() const {
549    return getOS() == Triple::Fuchsia;
550  }
551
552  bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
553
554  bool isOSSolaris() const {
555    return getOS() == Triple::Solaris;
556  }
557
558  bool isOSIAMCU() const {
559    return getOS() == Triple::ELFIAMCU;
560  }
561
562  bool isOSUnknown() const { return getOS() == Triple::UnknownOS; }
563
564  bool isGNUEnvironment() const {
565    EnvironmentType Env = getEnvironment();
566    return Env == Triple::GNU || Env == Triple::GNUABIN32 ||
567           Env == Triple::GNUABI64 || Env == Triple::GNUEABI ||
568           Env == Triple::GNUEABIHF || Env == Triple::GNUF32 ||
569           Env == Triple::GNUF64 || Env == Triple::GNUSF ||
570           Env == Triple::GNUX32;
571  }
572
573  bool isOSContiki() const {
574    return getOS() == Triple::Contiki;
575  }
576
577  /// Tests whether the OS is Haiku.
578  bool isOSHaiku() const {
579    return getOS() == Triple::Haiku;
580  }
581
582  /// Tests whether the OS is Windows.
583  bool isOSWindows() const {
584    return getOS() == Triple::Win32;
585  }
586
587  /// Checks if the environment is MSVC.
588  bool isKnownWindowsMSVCEnvironment() const {
589    return isOSWindows() && getEnvironment() == Triple::MSVC;
590  }
591
592  /// Checks if the environment could be MSVC.
593  bool isWindowsMSVCEnvironment() const {
594    return isKnownWindowsMSVCEnvironment() ||
595           (isOSWindows() && getEnvironment() == Triple::UnknownEnvironment);
596  }
597
598  // Checks if we're using the Windows Arm64EC ABI.
599  bool isWindowsArm64EC() const {
600    return getArch() == Triple::aarch64 &&
601           getSubArch() == Triple::AArch64SubArch_arm64ec;
602  }
603
604  bool isWindowsCoreCLREnvironment() const {
605    return isOSWindows() && getEnvironment() == Triple::CoreCLR;
606  }
607
608  bool isWindowsItaniumEnvironment() const {
609    return isOSWindows() && getEnvironment() == Triple::Itanium;
610  }
611
612  bool isWindowsCygwinEnvironment() const {
613    return isOSWindows() && getEnvironment() == Triple::Cygnus;
614  }
615
616  bool isWindowsGNUEnvironment() const {
617    return isOSWindows() && getEnvironment() == Triple::GNU;
618  }
619
620  /// Tests for either Cygwin or MinGW OS
621  bool isOSCygMing() const {
622    return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
623  }
624
625  /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
626  bool isOSMSVCRT() const {
627    return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
628           isWindowsItaniumEnvironment();
629  }
630
631  /// Tests whether the OS is NaCl (Native Client)
632  bool isOSNaCl() const {
633    return getOS() == Triple::NaCl;
634  }
635
636  /// Tests whether the OS is Linux.
637  bool isOSLinux() const {
638    return getOS() == Triple::Linux;
639  }
640
641  /// Tests whether the OS is kFreeBSD.
642  bool isOSKFreeBSD() const {
643    return getOS() == Triple::KFreeBSD;
644  }
645
646  /// Tests whether the OS is Hurd.
647  bool isOSHurd() const {
648    return getOS() == Triple::Hurd;
649  }
650
651  /// Tests whether the OS is WASI.
652  bool isOSWASI() const {
653    return getOS() == Triple::WASI;
654  }
655
656  /// Tests whether the OS is Emscripten.
657  bool isOSEmscripten() const {
658    return getOS() == Triple::Emscripten;
659  }
660
661  /// Tests whether the OS uses glibc.
662  bool isOSGlibc() const {
663    return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD ||
664            getOS() == Triple::Hurd) &&
665           !isAndroid();
666  }
667
668  /// Tests whether the OS is AIX.
669  bool isOSAIX() const {
670    return getOS() == Triple::AIX;
671  }
672
673  /// Tests whether the OS uses the ELF binary format.
674  bool isOSBinFormatELF() const {
675    return getObjectFormat() == Triple::ELF;
676  }
677
678  /// Tests whether the OS uses the COFF binary format.
679  bool isOSBinFormatCOFF() const {
680    return getObjectFormat() == Triple::COFF;
681  }
682
683  /// Tests whether the OS uses the GOFF binary format.
684  bool isOSBinFormatGOFF() const { return getObjectFormat() == Triple::GOFF; }
685
686  /// Tests whether the environment is MachO.
687  bool isOSBinFormatMachO() const {
688    return getObjectFormat() == Triple::MachO;
689  }
690
691  /// Tests whether the OS uses the Wasm binary format.
692  bool isOSBinFormatWasm() const {
693    return getObjectFormat() == Triple::Wasm;
694  }
695
696  /// Tests whether the OS uses the XCOFF binary format.
697  bool isOSBinFormatXCOFF() const {
698    return getObjectFormat() == Triple::XCOFF;
699  }
700
701  /// Tests whether the OS uses the DXContainer binary format.
702  bool isOSBinFormatDXContainer() const {
703    return getObjectFormat() == Triple::DXContainer;
704  }
705
706  /// Tests whether the target is the PS4 platform.
707  bool isPS4() const {
708    return getArch() == Triple::x86_64 &&
709           getVendor() == Triple::SCEI &&
710           getOS() == Triple::PS4;
711  }
712
713  /// Tests whether the target is the PS5 platform.
714  bool isPS5() const {
715    return getArch() == Triple::x86_64 &&
716      getVendor() == Triple::SCEI &&
717      getOS() == Triple::PS5;
718  }
719
720  /// Tests whether the target is the PS4 or PS5 platform.
721  bool isPS() const { return isPS4() || isPS5(); }
722
723  /// Tests whether the target is Android
724  bool isAndroid() const { return getEnvironment() == Triple::Android; }
725
726  bool isAndroidVersionLT(unsigned Major) const {
727    assert(isAndroid() && "Not an Android triple!");
728
729    VersionTuple Version = getEnvironmentVersion();
730
731    // 64-bit targets did not exist before API level 21 (Lollipop).
732    if (isArch64Bit() && Version.getMajor() < 21)
733      return VersionTuple(21) < VersionTuple(Major);
734
735    return Version < VersionTuple(Major);
736  }
737
738  /// Tests whether the environment is musl-libc
739  bool isMusl() const {
740    return getEnvironment() == Triple::Musl ||
741           getEnvironment() == Triple::MuslEABI ||
742           getEnvironment() == Triple::MuslEABIHF ||
743           getEnvironment() == Triple::MuslX32;
744  }
745
746  /// Tests whether the target is DXIL.
747  bool isDXIL() const {
748    return getArch() == Triple::dxil;
749  }
750
751  /// Tests whether the target is SPIR (32- or 64-bit).
752  bool isSPIR() const {
753    return getArch() == Triple::spir || getArch() == Triple::spir64;
754  }
755
756  /// Tests whether the target is SPIR-V (32/64-bit).
757  bool isSPIRV() const {
758    return getArch() == Triple::spirv32 || getArch() == Triple::spirv64;
759  }
760
761  /// Tests whether the target is NVPTX (32- or 64-bit).
762  bool isNVPTX() const {
763    return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
764  }
765
766  /// Tests whether the target is AMDGCN
767  bool isAMDGCN() const { return getArch() == Triple::amdgcn; }
768
769  bool isAMDGPU() const {
770    return getArch() == Triple::r600 || getArch() == Triple::amdgcn;
771  }
772
773  /// Tests whether the target is Thumb (little and big endian).
774  bool isThumb() const {
775    return getArch() == Triple::thumb || getArch() == Triple::thumbeb;
776  }
777
778  /// Tests whether the target is ARM (little and big endian).
779  bool isARM() const {
780    return getArch() == Triple::arm || getArch() == Triple::armeb;
781  }
782
783  /// Tests whether the target supports the EHABI exception
784  /// handling standard.
785  bool isTargetEHABICompatible() const {
786    return (isARM() || isThumb()) &&
787           (getEnvironment() == Triple::EABI ||
788            getEnvironment() == Triple::GNUEABI ||
789            getEnvironment() == Triple::MuslEABI ||
790            getEnvironment() == Triple::EABIHF ||
791            getEnvironment() == Triple::GNUEABIHF ||
792            getEnvironment() == Triple::MuslEABIHF || isAndroid()) &&
793           isOSBinFormatELF();
794  }
795
796  /// Tests whether the target is T32.
797  bool isArmT32() const {
798    switch (getSubArch()) {
799    case Triple::ARMSubArch_v8m_baseline:
800    case Triple::ARMSubArch_v7s:
801    case Triple::ARMSubArch_v7k:
802    case Triple::ARMSubArch_v7ve:
803    case Triple::ARMSubArch_v6:
804    case Triple::ARMSubArch_v6m:
805    case Triple::ARMSubArch_v6k:
806    case Triple::ARMSubArch_v6t2:
807    case Triple::ARMSubArch_v5:
808    case Triple::ARMSubArch_v5te:
809    case Triple::ARMSubArch_v4t:
810      return false;
811    default:
812      return true;
813    }
814  }
815
816  /// Tests whether the target is an M-class.
817  bool isArmMClass() const {
818    switch (getSubArch()) {
819    case Triple::ARMSubArch_v6m:
820    case Triple::ARMSubArch_v7m:
821    case Triple::ARMSubArch_v7em:
822    case Triple::ARMSubArch_v8m_mainline:
823    case Triple::ARMSubArch_v8m_baseline:
824    case Triple::ARMSubArch_v8_1m_mainline:
825      return true;
826    default:
827      return false;
828    }
829  }
830
831  /// Tests whether the target is AArch64 (little and big endian).
832  bool isAArch64() const {
833    return getArch() == Triple::aarch64 || getArch() == Triple::aarch64_be ||
834           getArch() == Triple::aarch64_32;
835  }
836
837  /// Tests whether the target is AArch64 and pointers are the size specified by
838  /// \p PointerWidth.
839  bool isAArch64(int PointerWidth) const {
840    assert(PointerWidth == 64 || PointerWidth == 32);
841    if (!isAArch64())
842      return false;
843    return getArch() == Triple::aarch64_32 ||
844                   getEnvironment() == Triple::GNUILP32
845               ? PointerWidth == 32
846               : PointerWidth == 64;
847  }
848
849  /// Tests whether the target is LoongArch (32- and 64-bit).
850  bool isLoongArch() const {
851    return getArch() == Triple::loongarch32 || getArch() == Triple::loongarch64;
852  }
853
854  /// Tests whether the target is MIPS 32-bit (little and big endian).
855  bool isMIPS32() const {
856    return getArch() == Triple::mips || getArch() == Triple::mipsel;
857  }
858
859  /// Tests whether the target is MIPS 64-bit (little and big endian).
860  bool isMIPS64() const {
861    return getArch() == Triple::mips64 || getArch() == Triple::mips64el;
862  }
863
864  /// Tests whether the target is MIPS (little and big endian, 32- or 64-bit).
865  bool isMIPS() const {
866    return isMIPS32() || isMIPS64();
867  }
868
869  /// Tests whether the target is PowerPC (32- or 64-bit LE or BE).
870  bool isPPC() const {
871    return getArch() == Triple::ppc || getArch() == Triple::ppc64 ||
872           getArch() == Triple::ppcle || getArch() == Triple::ppc64le;
873  }
874
875  /// Tests whether the target is 32-bit PowerPC (little and big endian).
876  bool isPPC32() const {
877    return getArch() == Triple::ppc || getArch() == Triple::ppcle;
878  }
879
880  /// Tests whether the target is 64-bit PowerPC (little and big endian).
881  bool isPPC64() const {
882    return getArch() == Triple::ppc64 || getArch() == Triple::ppc64le;
883  }
884
885  /// Tests whether the target 64-bit PowerPC big endian ABI is ELFv2.
886  bool isPPC64ELFv2ABI() const {
887    return (getArch() == Triple::ppc64 &&
888            ((getOS() == Triple::FreeBSD &&
889            (getOSMajorVersion() >= 13 || getOSVersion().empty())) ||
890            getOS() == Triple::OpenBSD || isMusl()));
891  }
892
893  /// Tests whether the target is 32-bit RISC-V.
894  bool isRISCV32() const { return getArch() == Triple::riscv32; }
895
896  /// Tests whether the target is 64-bit RISC-V.
897  bool isRISCV64() const { return getArch() == Triple::riscv64; }
898
899  /// Tests whether the target is RISC-V (32- and 64-bit).
900  bool isRISCV() const { return isRISCV32() || isRISCV64(); }
901
902  /// Tests whether the target is 32-bit SPARC (little and big endian).
903  bool isSPARC32() const {
904    return getArch() == Triple::sparc || getArch() == Triple::sparcel;
905  }
906
907  /// Tests whether the target is 64-bit SPARC (big endian).
908  bool isSPARC64() const { return getArch() == Triple::sparcv9; }
909
910  /// Tests whether the target is SPARC.
911  bool isSPARC() const { return isSPARC32() || isSPARC64(); }
912
913  /// Tests whether the target is SystemZ.
914  bool isSystemZ() const {
915    return getArch() == Triple::systemz;
916  }
917
918  /// Tests whether the target is x86 (32- or 64-bit).
919  bool isX86() const {
920    return getArch() == Triple::x86 || getArch() == Triple::x86_64;
921  }
922
923  /// Tests whether the target is VE
924  bool isVE() const {
925    return getArch() == Triple::ve;
926  }
927
928  /// Tests whether the target is wasm (32- and 64-bit).
929  bool isWasm() const {
930    return getArch() == Triple::wasm32 || getArch() == Triple::wasm64;
931  }
932
933  // Tests whether the target is CSKY
934  bool isCSKY() const {
935    return getArch() == Triple::csky;
936  }
937
938  /// Tests whether the target is the Apple "arm64e" AArch64 subarch.
939  bool isArm64e() const {
940    return getArch() == Triple::aarch64 &&
941           getSubArch() == Triple::AArch64SubArch_arm64e;
942  }
943
944  /// Tests whether the target is X32.
945  bool isX32() const {
946    EnvironmentType Env = getEnvironment();
947    return Env == Triple::GNUX32 || Env == Triple::MuslX32;
948  }
949
950  /// Tests whether the target is eBPF.
951  bool isBPF() const {
952    return getArch() == Triple::bpfel || getArch() == Triple::bpfeb;
953  }
954
955  /// Tests whether the target supports comdat
956  bool supportsCOMDAT() const {
957    return !(isOSBinFormatMachO() || isOSBinFormatXCOFF() ||
958             isOSBinFormatDXContainer());
959  }
960
961  /// Tests whether the target uses emulated TLS as default.
962  bool hasDefaultEmulatedTLS() const {
963    return isAndroid() || isOSOpenBSD() || isWindowsCygwinEnvironment();
964  }
965
966  /// Tests whether the target uses -data-sections as default.
967  bool hasDefaultDataSections() const {
968    return isOSBinFormatXCOFF() || isWasm();
969  }
970
971  /// Tests if the environment supports dllimport/export annotations.
972  bool hasDLLImportExport() const { return isOSWindows() || isPS(); }
973
974  /// @}
975  /// @name Mutators
976  /// @{
977
978  /// Set the architecture (first) component of the triple to a known type.
979  void setArch(ArchType Kind, SubArchType SubArch = NoSubArch);
980
981  /// Set the vendor (second) component of the triple to a known type.
982  void setVendor(VendorType Kind);
983
984  /// Set the operating system (third) component of the triple to a known type.
985  void setOS(OSType Kind);
986
987  /// Set the environment (fourth) component of the triple to a known type.
988  void setEnvironment(EnvironmentType Kind);
989
990  /// Set the object file format.
991  void setObjectFormat(ObjectFormatType Kind);
992
993  /// Set all components to the new triple \p Str.
994  void setTriple(const Twine &Str);
995
996  /// Set the architecture (first) component of the triple by name.
997  void setArchName(StringRef Str);
998
999  /// Set the vendor (second) component of the triple by name.
1000  void setVendorName(StringRef Str);
1001
1002  /// Set the operating system (third) component of the triple by name.
1003  void setOSName(StringRef Str);
1004
1005  /// Set the optional environment (fourth) component of the triple by name.
1006  void setEnvironmentName(StringRef Str);
1007
1008  /// Set the operating system and optional environment components with a single
1009  /// string.
1010  void setOSAndEnvironmentName(StringRef Str);
1011
1012  /// @}
1013  /// @name Helpers to build variants of a particular triple.
1014  /// @{
1015
1016  /// Form a triple with a 32-bit variant of the current architecture.
1017  ///
1018  /// This can be used to move across "families" of architectures where useful.
1019  ///
1020  /// \returns A new triple with a 32-bit architecture or an unknown
1021  ///          architecture if no such variant can be found.
1022  llvm::Triple get32BitArchVariant() const;
1023
1024  /// Form a triple with a 64-bit variant of the current architecture.
1025  ///
1026  /// This can be used to move across "families" of architectures where useful.
1027  ///
1028  /// \returns A new triple with a 64-bit architecture or an unknown
1029  ///          architecture if no such variant can be found.
1030  llvm::Triple get64BitArchVariant() const;
1031
1032  /// Form a triple with a big endian variant of the current architecture.
1033  ///
1034  /// This can be used to move across "families" of architectures where useful.
1035  ///
1036  /// \returns A new triple with a big endian architecture or an unknown
1037  ///          architecture if no such variant can be found.
1038  llvm::Triple getBigEndianArchVariant() const;
1039
1040  /// Form a triple with a little endian variant of the current architecture.
1041  ///
1042  /// This can be used to move across "families" of architectures where useful.
1043  ///
1044  /// \returns A new triple with a little endian architecture or an unknown
1045  ///          architecture if no such variant can be found.
1046  llvm::Triple getLittleEndianArchVariant() const;
1047
1048  /// Tests whether the target triple is little endian.
1049  ///
1050  /// \returns true if the triple is little endian, false otherwise.
1051  bool isLittleEndian() const;
1052
1053  /// Test whether target triples are compatible.
1054  bool isCompatibleWith(const Triple &Other) const;
1055
1056  /// Merge target triples.
1057  std::string merge(const Triple &Other) const;
1058
1059  /// Some platforms have different minimum supported OS versions that
1060  /// varies by the architecture specified in the triple. This function
1061  /// returns the minimum supported OS version for this triple if one an exists,
1062  /// or an invalid version tuple if this triple doesn't have one.
1063  VersionTuple getMinimumSupportedOSVersion() const;
1064
1065  /// @}
1066  /// @name Static helpers for IDs.
1067  /// @{
1068
1069  /// Get the canonical name for the \p Kind architecture.
1070  static StringRef getArchTypeName(ArchType Kind);
1071
1072  /// Get the "prefix" canonical name for the \p Kind architecture. This is the
1073  /// prefix used by the architecture specific builtins, and is suitable for
1074  /// passing to \see Intrinsic::getIntrinsicForClangBuiltin().
1075  ///
1076  /// \return - The architecture prefix, or 0 if none is defined.
1077  static StringRef getArchTypePrefix(ArchType Kind);
1078
1079  /// Get the canonical name for the \p Kind vendor.
1080  static StringRef getVendorTypeName(VendorType Kind);
1081
1082  /// Get the canonical name for the \p Kind operating system.
1083  static StringRef getOSTypeName(OSType Kind);
1084
1085  /// Get the canonical name for the \p Kind environment.
1086  static StringRef getEnvironmentTypeName(EnvironmentType Kind);
1087
1088  /// @}
1089  /// @name Static helpers for converting alternate architecture names.
1090  /// @{
1091
1092  /// The canonical type for the given LLVM architecture name (e.g., "x86").
1093  static ArchType getArchTypeForLLVMName(StringRef Str);
1094
1095  /// @}
1096
1097  /// Returns a canonicalized OS version number for the specified OS.
1098  static VersionTuple getCanonicalVersionForOS(OSType OSKind,
1099                                               const VersionTuple &Version);
1100};
1101
1102} // End llvm namespace
1103
1104
1105#endif
1106