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