xref: /freebsd/contrib/llvm-project/llvm/lib/TargetParser/Triple.cpp (revision d686ce931cab72612a9e1ada9fe99d65e11a32a3)
1 //===--- Triple.cpp - Target triple helper class --------------------------===//
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 #include "llvm/TargetParser/Triple.h"
10 #include "llvm/ADT/DenseMap.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/SwapByteOrder.h"
16 #include "llvm/Support/VersionTuple.h"
17 #include "llvm/TargetParser/ARMTargetParser.h"
18 #include "llvm/TargetParser/ARMTargetParserCommon.h"
19 #include "llvm/TargetParser/Host.h"
20 #include <cassert>
21 #include <cstring>
22 using namespace llvm;
23 
getArchTypeName(ArchType Kind)24 StringRef Triple::getArchTypeName(ArchType Kind) {
25   switch (Kind) {
26   case UnknownArch:    return "unknown";
27 
28   case aarch64:        return "aarch64";
29   case aarch64_32:     return "aarch64_32";
30   case aarch64_be:     return "aarch64_be";
31   case amdgcn:         return "amdgcn";
32   case amdil64:        return "amdil64";
33   case amdil:          return "amdil";
34   case arc:            return "arc";
35   case arm:            return "arm";
36   case armeb:          return "armeb";
37   case avr:            return "avr";
38   case bpfeb:          return "bpfeb";
39   case bpfel:          return "bpfel";
40   case csky:           return "csky";
41   case dxil:           return "dxil";
42   case hexagon:        return "hexagon";
43   case hsail64:        return "hsail64";
44   case hsail:          return "hsail";
45   case kalimba:        return "kalimba";
46   case lanai:          return "lanai";
47   case le32:           return "le32";
48   case le64:           return "le64";
49   case loongarch32:    return "loongarch32";
50   case loongarch64:    return "loongarch64";
51   case m68k:           return "m68k";
52   case mips64:         return "mips64";
53   case mips64el:       return "mips64el";
54   case mips:           return "mips";
55   case mipsel:         return "mipsel";
56   case msp430:         return "msp430";
57   case nvptx64:        return "nvptx64";
58   case nvptx:          return "nvptx";
59   case ppc64:          return "powerpc64";
60   case ppc64le:        return "powerpc64le";
61   case ppc:            return "powerpc";
62   case ppcle:          return "powerpcle";
63   case r600:           return "r600";
64   case renderscript32: return "renderscript32";
65   case renderscript64: return "renderscript64";
66   case riscv32:        return "riscv32";
67   case riscv64:        return "riscv64";
68   case shave:          return "shave";
69   case sparc:          return "sparc";
70   case sparcel:        return "sparcel";
71   case sparcv9:        return "sparcv9";
72   case spir64:         return "spir64";
73   case spir:           return "spir";
74   case spirv:          return "spirv";
75   case spirv32:        return "spirv32";
76   case spirv64:        return "spirv64";
77   case systemz:        return "s390x";
78   case tce:            return "tce";
79   case tcele:          return "tcele";
80   case thumb:          return "thumb";
81   case thumbeb:        return "thumbeb";
82   case ve:             return "ve";
83   case wasm32:         return "wasm32";
84   case wasm64:         return "wasm64";
85   case x86:            return "i386";
86   case x86_64:         return "x86_64";
87   case xcore:          return "xcore";
88   case xtensa:         return "xtensa";
89   }
90 
91   llvm_unreachable("Invalid ArchType!");
92 }
93 
getArchName(ArchType Kind,SubArchType SubArch)94 StringRef Triple::getArchName(ArchType Kind, SubArchType SubArch) {
95   switch (Kind) {
96   case Triple::mips:
97     if (SubArch == MipsSubArch_r6)
98       return "mipsisa32r6";
99     break;
100   case Triple::mipsel:
101     if (SubArch == MipsSubArch_r6)
102       return "mipsisa32r6el";
103     break;
104   case Triple::mips64:
105     if (SubArch == MipsSubArch_r6)
106       return "mipsisa64r6";
107     break;
108   case Triple::mips64el:
109     if (SubArch == MipsSubArch_r6)
110       return "mipsisa64r6el";
111     break;
112   case Triple::aarch64:
113     if (SubArch == AArch64SubArch_arm64ec)
114       return "arm64ec";
115     if (SubArch == AArch64SubArch_arm64e)
116       return "arm64e";
117     break;
118   case Triple::dxil:
119     switch (SubArch) {
120     case Triple::NoSubArch:
121     case Triple::DXILSubArch_v1_0:
122       return "dxilv1.0";
123     case Triple::DXILSubArch_v1_1:
124       return "dxilv1.1";
125     case Triple::DXILSubArch_v1_2:
126       return "dxilv1.2";
127     case Triple::DXILSubArch_v1_3:
128       return "dxilv1.3";
129     case Triple::DXILSubArch_v1_4:
130       return "dxilv1.4";
131     case Triple::DXILSubArch_v1_5:
132       return "dxilv1.5";
133     case Triple::DXILSubArch_v1_6:
134       return "dxilv1.6";
135     case Triple::DXILSubArch_v1_7:
136       return "dxilv1.7";
137     case Triple::DXILSubArch_v1_8:
138       return "dxilv1.8";
139     default:
140       break;
141     }
142     break;
143   default:
144     break;
145   }
146   return getArchTypeName(Kind);
147 }
148 
getArchTypePrefix(ArchType Kind)149 StringRef Triple::getArchTypePrefix(ArchType Kind) {
150   switch (Kind) {
151   default:
152     return StringRef();
153 
154   case aarch64:
155   case aarch64_be:
156   case aarch64_32:  return "aarch64";
157 
158   case arc:         return "arc";
159 
160   case arm:
161   case armeb:
162   case thumb:
163   case thumbeb:     return "arm";
164 
165   case avr:         return "avr";
166 
167   case ppc64:
168   case ppc64le:
169   case ppc:
170   case ppcle:       return "ppc";
171 
172   case m68k:        return "m68k";
173 
174   case mips:
175   case mipsel:
176   case mips64:
177   case mips64el:    return "mips";
178 
179   case hexagon:     return "hexagon";
180 
181   case amdgcn:      return "amdgcn";
182   case r600:        return "r600";
183 
184   case bpfel:
185   case bpfeb:       return "bpf";
186 
187   case sparcv9:
188   case sparcel:
189   case sparc:       return "sparc";
190 
191   case systemz:     return "s390";
192 
193   case x86:
194   case x86_64:      return "x86";
195 
196   case xcore:       return "xcore";
197 
198   // NVPTX intrinsics are namespaced under nvvm.
199   case nvptx:       return "nvvm";
200   case nvptx64:     return "nvvm";
201 
202   case le32:        return "le32";
203   case le64:        return "le64";
204 
205   case amdil:
206   case amdil64:     return "amdil";
207 
208   case hsail:
209   case hsail64:     return "hsail";
210 
211   case spir:
212   case spir64:      return "spir";
213 
214   case spirv:
215   case spirv32:
216   case spirv64:     return "spv";
217 
218   case kalimba:     return "kalimba";
219   case lanai:       return "lanai";
220   case shave:       return "shave";
221   case wasm32:
222   case wasm64:      return "wasm";
223 
224   case riscv32:
225   case riscv64:     return "riscv";
226 
227   case ve:          return "ve";
228   case csky:        return "csky";
229 
230   case loongarch32:
231   case loongarch64: return "loongarch";
232 
233   case dxil:        return "dx";
234 
235   case xtensa:      return "xtensa";
236   }
237 }
238 
getVendorTypeName(VendorType Kind)239 StringRef Triple::getVendorTypeName(VendorType Kind) {
240   switch (Kind) {
241   case UnknownVendor: return "unknown";
242 
243   case AMD: return "amd";
244   case Apple: return "apple";
245   case CSR: return "csr";
246   case Freescale: return "fsl";
247   case IBM: return "ibm";
248   case ImaginationTechnologies: return "img";
249   case Mesa: return "mesa";
250   case MipsTechnologies: return "mti";
251   case NVIDIA: return "nvidia";
252   case OpenEmbedded: return "oe";
253   case PC: return "pc";
254   case SCEI: return "scei";
255   case SUSE: return "suse";
256   }
257 
258   llvm_unreachable("Invalid VendorType!");
259 }
260 
getOSTypeName(OSType Kind)261 StringRef Triple::getOSTypeName(OSType Kind) {
262   switch (Kind) {
263   case UnknownOS: return "unknown";
264 
265   case AIX: return "aix";
266   case AMDHSA: return "amdhsa";
267   case AMDPAL: return "amdpal";
268   case BridgeOS: return "bridgeos";
269   case CUDA: return "cuda";
270   case Darwin: return "darwin";
271   case DragonFly: return "dragonfly";
272   case DriverKit: return "driverkit";
273   case ELFIAMCU: return "elfiamcu";
274   case Emscripten: return "emscripten";
275   case FreeBSD: return "freebsd";
276   case Fuchsia: return "fuchsia";
277   case Haiku: return "haiku";
278   case HermitCore: return "hermit";
279   case Hurd: return "hurd";
280   case IOS: return "ios";
281   case KFreeBSD: return "kfreebsd";
282   case Linux: return "linux";
283   case Lv2: return "lv2";
284   case MacOSX: return "macosx";
285   case Mesa3D: return "mesa3d";
286   case NVCL: return "nvcl";
287   case NaCl: return "nacl";
288   case NetBSD: return "netbsd";
289   case OpenBSD: return "openbsd";
290   case PS4: return "ps4";
291   case PS5: return "ps5";
292   case RTEMS: return "rtems";
293   case Solaris: return "solaris";
294   case Serenity: return "serenity";
295   case TvOS: return "tvos";
296   case UEFI: return "uefi";
297   case WASI: return "wasi";
298   case WatchOS: return "watchos";
299   case Win32: return "windows";
300   case ZOS: return "zos";
301   case ShaderModel: return "shadermodel";
302   case LiteOS: return "liteos";
303   case XROS: return "xros";
304   case Vulkan: return "vulkan";
305   }
306 
307   llvm_unreachable("Invalid OSType");
308 }
309 
getEnvironmentTypeName(EnvironmentType Kind)310 StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
311   switch (Kind) {
312   case UnknownEnvironment: return "unknown";
313   case Android: return "android";
314   case CODE16: return "code16";
315   case CoreCLR: return "coreclr";
316   case Cygnus: return "cygnus";
317   case EABI: return "eabi";
318   case EABIHF: return "eabihf";
319   case GNU: return "gnu";
320   case GNUT64: return "gnut64";
321   case GNUABI64: return "gnuabi64";
322   case GNUABIN32: return "gnuabin32";
323   case GNUEABI: return "gnueabi";
324   case GNUEABIT64: return "gnueabit64";
325   case GNUEABIHF: return "gnueabihf";
326   case GNUEABIHFT64: return "gnueabihft64";
327   case GNUF32: return "gnuf32";
328   case GNUF64: return "gnuf64";
329   case GNUSF: return "gnusf";
330   case GNUX32: return "gnux32";
331   case GNUILP32: return "gnu_ilp32";
332   case Itanium: return "itanium";
333   case MSVC: return "msvc";
334   case MacABI: return "macabi";
335   case Musl: return "musl";
336   case MuslEABI: return "musleabi";
337   case MuslEABIHF: return "musleabihf";
338   case MuslX32: return "muslx32";
339   case Simulator: return "simulator";
340   case Pixel: return "pixel";
341   case Vertex: return "vertex";
342   case Geometry: return "geometry";
343   case Hull: return "hull";
344   case Domain: return "domain";
345   case Compute: return "compute";
346   case Library: return "library";
347   case RayGeneration: return "raygeneration";
348   case Intersection: return "intersection";
349   case AnyHit: return "anyhit";
350   case ClosestHit: return "closesthit";
351   case Miss: return "miss";
352   case Callable: return "callable";
353   case Mesh: return "mesh";
354   case Amplification: return "amplification";
355   case OpenCL:
356     return "opencl";
357   case OpenHOS: return "ohos";
358   case PAuthTest:
359     return "pauthtest";
360   }
361 
362   llvm_unreachable("Invalid EnvironmentType!");
363 }
364 
getObjectFormatTypeName(ObjectFormatType Kind)365 StringRef Triple::getObjectFormatTypeName(ObjectFormatType Kind) {
366   switch (Kind) {
367   case UnknownObjectFormat: return "";
368   case COFF: return "coff";
369   case ELF: return "elf";
370   case GOFF: return "goff";
371   case MachO: return "macho";
372   case Wasm: return "wasm";
373   case XCOFF: return "xcoff";
374   case DXContainer: return "dxcontainer";
375   case SPIRV: return "spirv";
376   }
377   llvm_unreachable("unknown object format type");
378 }
379 
parseBPFArch(StringRef ArchName)380 static Triple::ArchType parseBPFArch(StringRef ArchName) {
381   if (ArchName == "bpf") {
382     if (sys::IsLittleEndianHost)
383       return Triple::bpfel;
384     else
385       return Triple::bpfeb;
386   } else if (ArchName == "bpf_be" || ArchName == "bpfeb") {
387     return Triple::bpfeb;
388   } else if (ArchName == "bpf_le" || ArchName == "bpfel") {
389     return Triple::bpfel;
390   } else {
391     return Triple::UnknownArch;
392   }
393 }
394 
getArchTypeForLLVMName(StringRef Name)395 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
396   Triple::ArchType BPFArch(parseBPFArch(Name));
397   return StringSwitch<Triple::ArchType>(Name)
398     .Case("aarch64", aarch64)
399     .Case("aarch64_be", aarch64_be)
400     .Case("aarch64_32", aarch64_32)
401     .Case("arc", arc)
402     .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
403     .Case("arm64_32", aarch64_32)
404     .Case("arm", arm)
405     .Case("armeb", armeb)
406     .Case("avr", avr)
407     .StartsWith("bpf", BPFArch)
408     .Case("m68k", m68k)
409     .Case("mips", mips)
410     .Case("mipsel", mipsel)
411     .Case("mips64", mips64)
412     .Case("mips64el", mips64el)
413     .Case("msp430", msp430)
414     .Case("ppc64", ppc64)
415     .Case("ppc32", ppc)
416     .Case("ppc", ppc)
417     .Case("ppc32le", ppcle)
418     .Case("ppcle", ppcle)
419     .Case("ppc64le", ppc64le)
420     .Case("r600", r600)
421     .Case("amdgcn", amdgcn)
422     .Case("riscv32", riscv32)
423     .Case("riscv64", riscv64)
424     .Case("hexagon", hexagon)
425     .Case("sparc", sparc)
426     .Case("sparcel", sparcel)
427     .Case("sparcv9", sparcv9)
428     .Case("s390x", systemz)
429     .Case("systemz", systemz)
430     .Case("tce", tce)
431     .Case("tcele", tcele)
432     .Case("thumb", thumb)
433     .Case("thumbeb", thumbeb)
434     .Case("x86", x86)
435     .Case("i386", x86)
436     .Case("x86-64", x86_64)
437     .Case("xcore", xcore)
438     .Case("nvptx", nvptx)
439     .Case("nvptx64", nvptx64)
440     .Case("le32", le32)
441     .Case("le64", le64)
442     .Case("amdil", amdil)
443     .Case("amdil64", amdil64)
444     .Case("hsail", hsail)
445     .Case("hsail64", hsail64)
446     .Case("spir", spir)
447     .Case("spir64", spir64)
448     .Case("spirv", spirv)
449     .Case("spirv32", spirv32)
450     .Case("spirv64", spirv64)
451     .Case("kalimba", kalimba)
452     .Case("lanai", lanai)
453     .Case("shave", shave)
454     .Case("wasm32", wasm32)
455     .Case("wasm64", wasm64)
456     .Case("renderscript32", renderscript32)
457     .Case("renderscript64", renderscript64)
458     .Case("ve", ve)
459     .Case("csky", csky)
460     .Case("loongarch32", loongarch32)
461     .Case("loongarch64", loongarch64)
462     .Case("dxil", dxil)
463     .Case("xtensa", xtensa)
464     .Default(UnknownArch);
465 }
466 
parseARMArch(StringRef ArchName)467 static Triple::ArchType parseARMArch(StringRef ArchName) {
468   ARM::ISAKind ISA = ARM::parseArchISA(ArchName);
469   ARM::EndianKind ENDIAN = ARM::parseArchEndian(ArchName);
470 
471   Triple::ArchType arch = Triple::UnknownArch;
472   switch (ENDIAN) {
473   case ARM::EndianKind::LITTLE: {
474     switch (ISA) {
475     case ARM::ISAKind::ARM:
476       arch = Triple::arm;
477       break;
478     case ARM::ISAKind::THUMB:
479       arch = Triple::thumb;
480       break;
481     case ARM::ISAKind::AARCH64:
482       arch = Triple::aarch64;
483       break;
484     case ARM::ISAKind::INVALID:
485       break;
486     }
487     break;
488   }
489   case ARM::EndianKind::BIG: {
490     switch (ISA) {
491     case ARM::ISAKind::ARM:
492       arch = Triple::armeb;
493       break;
494     case ARM::ISAKind::THUMB:
495       arch = Triple::thumbeb;
496       break;
497     case ARM::ISAKind::AARCH64:
498       arch = Triple::aarch64_be;
499       break;
500     case ARM::ISAKind::INVALID:
501       break;
502     }
503     break;
504   }
505   case ARM::EndianKind::INVALID: {
506     break;
507   }
508   }
509 
510   ArchName = ARM::getCanonicalArchName(ArchName);
511   if (ArchName.empty())
512     return Triple::UnknownArch;
513 
514   // Thumb only exists in v4+
515   if (ISA == ARM::ISAKind::THUMB &&
516       (ArchName.starts_with("v2") || ArchName.starts_with("v3")))
517     return Triple::UnknownArch;
518 
519   // Thumb only for v6m
520   ARM::ProfileKind Profile = ARM::parseArchProfile(ArchName);
521   unsigned Version = ARM::parseArchVersion(ArchName);
522   if (Profile == ARM::ProfileKind::M && Version == 6) {
523     if (ENDIAN == ARM::EndianKind::BIG)
524       return Triple::thumbeb;
525     else
526       return Triple::thumb;
527   }
528 
529   return arch;
530 }
531 
parseArch(StringRef ArchName)532 static Triple::ArchType parseArch(StringRef ArchName) {
533   auto AT =
534       StringSwitch<Triple::ArchType>(ArchName)
535           .Cases("i386", "i486", "i586", "i686", Triple::x86)
536           // FIXME: Do we need to support these?
537           .Cases("i786", "i886", "i986", Triple::x86)
538           .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
539           .Cases("powerpc", "powerpcspe", "ppc", "ppc32", Triple::ppc)
540           .Cases("powerpcle", "ppcle", "ppc32le", Triple::ppcle)
541           .Cases("powerpc64", "ppu", "ppc64", Triple::ppc64)
542           .Cases("powerpc64le", "ppc64le", Triple::ppc64le)
543           .Case("xscale", Triple::arm)
544           .Case("xscaleeb", Triple::armeb)
545           .Case("aarch64", Triple::aarch64)
546           .Case("aarch64_be", Triple::aarch64_be)
547           .Case("aarch64_32", Triple::aarch64_32)
548           .Case("arc", Triple::arc)
549           .Case("arm64", Triple::aarch64)
550           .Case("arm64_32", Triple::aarch64_32)
551           .Case("arm64e", Triple::aarch64)
552           .Case("arm64ec", Triple::aarch64)
553           .Case("arm", Triple::arm)
554           .Case("armeb", Triple::armeb)
555           .Case("thumb", Triple::thumb)
556           .Case("thumbeb", Triple::thumbeb)
557           .Case("avr", Triple::avr)
558           .Case("m68k", Triple::m68k)
559           .Case("msp430", Triple::msp430)
560           .Cases("mips", "mipseb", "mipsallegrex", "mipsisa32r6", "mipsr6",
561                  Triple::mips)
562           .Cases("mipsel", "mipsallegrexel", "mipsisa32r6el", "mipsr6el",
563                  Triple::mipsel)
564           .Cases("mips64", "mips64eb", "mipsn32", "mipsisa64r6", "mips64r6",
565                  "mipsn32r6", Triple::mips64)
566           .Cases("mips64el", "mipsn32el", "mipsisa64r6el", "mips64r6el",
567                  "mipsn32r6el", Triple::mips64el)
568           .Case("r600", Triple::r600)
569           .Case("amdgcn", Triple::amdgcn)
570           .Case("riscv32", Triple::riscv32)
571           .Case("riscv64", Triple::riscv64)
572           .Case("hexagon", Triple::hexagon)
573           .Cases("s390x", "systemz", Triple::systemz)
574           .Case("sparc", Triple::sparc)
575           .Case("sparcel", Triple::sparcel)
576           .Cases("sparcv9", "sparc64", Triple::sparcv9)
577           .Case("tce", Triple::tce)
578           .Case("tcele", Triple::tcele)
579           .Case("xcore", Triple::xcore)
580           .Case("nvptx", Triple::nvptx)
581           .Case("nvptx64", Triple::nvptx64)
582           .Case("le32", Triple::le32)
583           .Case("le64", Triple::le64)
584           .Case("amdil", Triple::amdil)
585           .Case("amdil64", Triple::amdil64)
586           .Case("hsail", Triple::hsail)
587           .Case("hsail64", Triple::hsail64)
588           .Case("spir", Triple::spir)
589           .Case("spir64", Triple::spir64)
590           .Cases("spirv", "spirv1.5", "spirv1.6", Triple::spirv)
591           .Cases("spirv32", "spirv32v1.0", "spirv32v1.1", "spirv32v1.2",
592             "spirv32v1.3", "spirv32v1.4", "spirv32v1.5",
593             "spirv32v1.6", Triple::spirv32)
594           .Cases("spirv64", "spirv64v1.0", "spirv64v1.1", "spirv64v1.2",
595             "spirv64v1.3", "spirv64v1.4", "spirv64v1.5",
596             "spirv64v1.6", Triple::spirv64)
597           .StartsWith("kalimba", Triple::kalimba)
598           .Case("lanai", Triple::lanai)
599           .Case("renderscript32", Triple::renderscript32)
600           .Case("renderscript64", Triple::renderscript64)
601           .Case("shave", Triple::shave)
602           .Case("ve", Triple::ve)
603           .Case("wasm32", Triple::wasm32)
604           .Case("wasm64", Triple::wasm64)
605           .Case("csky", Triple::csky)
606           .Case("loongarch32", Triple::loongarch32)
607           .Case("loongarch64", Triple::loongarch64)
608           .Cases("dxil", "dxilv1.0", "dxilv1.1", "dxilv1.2", "dxilv1.3",
609                  "dxilv1.4", "dxilv1.5", "dxilv1.6", "dxilv1.7", "dxilv1.8",
610                  Triple::dxil)
611           .Case("xtensa", Triple::xtensa)
612           .Default(Triple::UnknownArch);
613 
614   // Some architectures require special parsing logic just to compute the
615   // ArchType result.
616   if (AT == Triple::UnknownArch) {
617     if (ArchName.starts_with("arm") || ArchName.starts_with("thumb") ||
618         ArchName.starts_with("aarch64"))
619       return parseARMArch(ArchName);
620     if (ArchName.starts_with("bpf"))
621       return parseBPFArch(ArchName);
622   }
623 
624   return AT;
625 }
626 
parseVendor(StringRef VendorName)627 static Triple::VendorType parseVendor(StringRef VendorName) {
628   return StringSwitch<Triple::VendorType>(VendorName)
629     .Case("apple", Triple::Apple)
630     .Case("pc", Triple::PC)
631     .Case("scei", Triple::SCEI)
632     .Case("sie", Triple::SCEI)
633     .Case("fsl", Triple::Freescale)
634     .Case("ibm", Triple::IBM)
635     .Case("img", Triple::ImaginationTechnologies)
636     .Case("mti", Triple::MipsTechnologies)
637     .Case("nvidia", Triple::NVIDIA)
638     .Case("csr", Triple::CSR)
639     .Case("amd", Triple::AMD)
640     .Case("mesa", Triple::Mesa)
641     .Case("suse", Triple::SUSE)
642     .Case("oe", Triple::OpenEmbedded)
643     .Default(Triple::UnknownVendor);
644 }
645 
parseOS(StringRef OSName)646 static Triple::OSType parseOS(StringRef OSName) {
647   return StringSwitch<Triple::OSType>(OSName)
648     .StartsWith("darwin", Triple::Darwin)
649     .StartsWith("dragonfly", Triple::DragonFly)
650     .StartsWith("freebsd", Triple::FreeBSD)
651     .StartsWith("fuchsia", Triple::Fuchsia)
652     .StartsWith("ios", Triple::IOS)
653     .StartsWith("kfreebsd", Triple::KFreeBSD)
654     .StartsWith("linux", Triple::Linux)
655     .StartsWith("lv2", Triple::Lv2)
656     .StartsWith("macos", Triple::MacOSX)
657     .StartsWith("netbsd", Triple::NetBSD)
658     .StartsWith("openbsd", Triple::OpenBSD)
659     .StartsWith("solaris", Triple::Solaris)
660     .StartsWith("uefi", Triple::UEFI)
661     .StartsWith("win32", Triple::Win32)
662     .StartsWith("windows", Triple::Win32)
663     .StartsWith("zos", Triple::ZOS)
664     .StartsWith("haiku", Triple::Haiku)
665     .StartsWith("rtems", Triple::RTEMS)
666     .StartsWith("nacl", Triple::NaCl)
667     .StartsWith("aix", Triple::AIX)
668     .StartsWith("cuda", Triple::CUDA)
669     .StartsWith("nvcl", Triple::NVCL)
670     .StartsWith("amdhsa", Triple::AMDHSA)
671     .StartsWith("ps4", Triple::PS4)
672     .StartsWith("ps5", Triple::PS5)
673     .StartsWith("elfiamcu", Triple::ELFIAMCU)
674     .StartsWith("tvos", Triple::TvOS)
675     .StartsWith("watchos", Triple::WatchOS)
676     .StartsWith("bridgeos", Triple::BridgeOS)
677     .StartsWith("driverkit", Triple::DriverKit)
678     .StartsWith("xros", Triple::XROS)
679     .StartsWith("visionos", Triple::XROS)
680     .StartsWith("mesa3d", Triple::Mesa3D)
681     .StartsWith("amdpal", Triple::AMDPAL)
682     .StartsWith("hermit", Triple::HermitCore)
683     .StartsWith("hurd", Triple::Hurd)
684     .StartsWith("wasi", Triple::WASI)
685     .StartsWith("emscripten", Triple::Emscripten)
686     .StartsWith("shadermodel", Triple::ShaderModel)
687     .StartsWith("liteos", Triple::LiteOS)
688     .StartsWith("serenity", Triple::Serenity)
689     .StartsWith("vulkan", Triple::Vulkan)
690     .Default(Triple::UnknownOS);
691 }
692 
parseEnvironment(StringRef EnvironmentName)693 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
694   return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
695       .StartsWith("eabihf", Triple::EABIHF)
696       .StartsWith("eabi", Triple::EABI)
697       .StartsWith("gnuabin32", Triple::GNUABIN32)
698       .StartsWith("gnuabi64", Triple::GNUABI64)
699       .StartsWith("gnueabihft64", Triple::GNUEABIHFT64)
700       .StartsWith("gnueabihf", Triple::GNUEABIHF)
701       .StartsWith("gnueabit64", Triple::GNUEABIT64)
702       .StartsWith("gnueabi", Triple::GNUEABI)
703       .StartsWith("gnuf32", Triple::GNUF32)
704       .StartsWith("gnuf64", Triple::GNUF64)
705       .StartsWith("gnusf", Triple::GNUSF)
706       .StartsWith("gnux32", Triple::GNUX32)
707       .StartsWith("gnu_ilp32", Triple::GNUILP32)
708       .StartsWith("code16", Triple::CODE16)
709       .StartsWith("gnut64", Triple::GNUT64)
710       .StartsWith("gnu", Triple::GNU)
711       .StartsWith("android", Triple::Android)
712       .StartsWith("musleabihf", Triple::MuslEABIHF)
713       .StartsWith("musleabi", Triple::MuslEABI)
714       .StartsWith("muslx32", Triple::MuslX32)
715       .StartsWith("musl", Triple::Musl)
716       .StartsWith("msvc", Triple::MSVC)
717       .StartsWith("itanium", Triple::Itanium)
718       .StartsWith("cygnus", Triple::Cygnus)
719       .StartsWith("coreclr", Triple::CoreCLR)
720       .StartsWith("simulator", Triple::Simulator)
721       .StartsWith("macabi", Triple::MacABI)
722       .StartsWith("pixel", Triple::Pixel)
723       .StartsWith("vertex", Triple::Vertex)
724       .StartsWith("geometry", Triple::Geometry)
725       .StartsWith("hull", Triple::Hull)
726       .StartsWith("domain", Triple::Domain)
727       .StartsWith("compute", Triple::Compute)
728       .StartsWith("library", Triple::Library)
729       .StartsWith("raygeneration", Triple::RayGeneration)
730       .StartsWith("intersection", Triple::Intersection)
731       .StartsWith("anyhit", Triple::AnyHit)
732       .StartsWith("closesthit", Triple::ClosestHit)
733       .StartsWith("miss", Triple::Miss)
734       .StartsWith("callable", Triple::Callable)
735       .StartsWith("mesh", Triple::Mesh)
736       .StartsWith("amplification", Triple::Amplification)
737       .StartsWith("opencl", Triple::OpenCL)
738       .StartsWith("ohos", Triple::OpenHOS)
739       .StartsWith("pauthtest", Triple::PAuthTest)
740       .Default(Triple::UnknownEnvironment);
741 }
742 
parseFormat(StringRef EnvironmentName)743 static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
744   return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
745       // "xcoff" must come before "coff" because of the order-dependendent
746       // pattern matching.
747       .EndsWith("xcoff", Triple::XCOFF)
748       .EndsWith("coff", Triple::COFF)
749       .EndsWith("elf", Triple::ELF)
750       .EndsWith("goff", Triple::GOFF)
751       .EndsWith("macho", Triple::MachO)
752       .EndsWith("wasm", Triple::Wasm)
753       .EndsWith("spirv", Triple::SPIRV)
754       .Default(Triple::UnknownObjectFormat);
755 }
756 
parseSubArch(StringRef SubArchName)757 static Triple::SubArchType parseSubArch(StringRef SubArchName) {
758   if (SubArchName.starts_with("mips") &&
759       (SubArchName.ends_with("r6el") || SubArchName.ends_with("r6")))
760     return Triple::MipsSubArch_r6;
761 
762   if (SubArchName == "powerpcspe")
763     return Triple::PPCSubArch_spe;
764 
765   if (SubArchName == "arm64e")
766     return Triple::AArch64SubArch_arm64e;
767 
768   if (SubArchName == "arm64ec")
769     return Triple::AArch64SubArch_arm64ec;
770 
771   if (SubArchName.starts_with("spirv"))
772     return StringSwitch<Triple::SubArchType>(SubArchName)
773         .EndsWith("v1.0", Triple::SPIRVSubArch_v10)
774         .EndsWith("v1.1", Triple::SPIRVSubArch_v11)
775         .EndsWith("v1.2", Triple::SPIRVSubArch_v12)
776         .EndsWith("v1.3", Triple::SPIRVSubArch_v13)
777         .EndsWith("v1.4", Triple::SPIRVSubArch_v14)
778         .EndsWith("v1.5", Triple::SPIRVSubArch_v15)
779         .EndsWith("v1.6", Triple::SPIRVSubArch_v16)
780         .Default(Triple::NoSubArch);
781 
782   if (SubArchName.starts_with("dxil"))
783     return StringSwitch<Triple::SubArchType>(SubArchName)
784         .EndsWith("v1.0", Triple::DXILSubArch_v1_0)
785         .EndsWith("v1.1", Triple::DXILSubArch_v1_1)
786         .EndsWith("v1.2", Triple::DXILSubArch_v1_2)
787         .EndsWith("v1.3", Triple::DXILSubArch_v1_3)
788         .EndsWith("v1.4", Triple::DXILSubArch_v1_4)
789         .EndsWith("v1.5", Triple::DXILSubArch_v1_5)
790         .EndsWith("v1.6", Triple::DXILSubArch_v1_6)
791         .EndsWith("v1.7", Triple::DXILSubArch_v1_7)
792         .EndsWith("v1.8", Triple::DXILSubArch_v1_8)
793         .Default(Triple::NoSubArch);
794 
795   StringRef ARMSubArch = ARM::getCanonicalArchName(SubArchName);
796 
797   // For now, this is the small part. Early return.
798   if (ARMSubArch.empty())
799     return StringSwitch<Triple::SubArchType>(SubArchName)
800       .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
801       .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
802       .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
803       .Default(Triple::NoSubArch);
804 
805   // ARM sub arch.
806   switch(ARM::parseArch(ARMSubArch)) {
807   case ARM::ArchKind::ARMV4:
808     return Triple::NoSubArch;
809   case ARM::ArchKind::ARMV4T:
810     return Triple::ARMSubArch_v4t;
811   case ARM::ArchKind::ARMV5T:
812     return Triple::ARMSubArch_v5;
813   case ARM::ArchKind::ARMV5TE:
814   case ARM::ArchKind::IWMMXT:
815   case ARM::ArchKind::IWMMXT2:
816   case ARM::ArchKind::XSCALE:
817   case ARM::ArchKind::ARMV5TEJ:
818     return Triple::ARMSubArch_v5te;
819   case ARM::ArchKind::ARMV6:
820     return Triple::ARMSubArch_v6;
821   case ARM::ArchKind::ARMV6K:
822   case ARM::ArchKind::ARMV6KZ:
823     return Triple::ARMSubArch_v6k;
824   case ARM::ArchKind::ARMV6T2:
825     return Triple::ARMSubArch_v6t2;
826   case ARM::ArchKind::ARMV6M:
827     return Triple::ARMSubArch_v6m;
828   case ARM::ArchKind::ARMV7A:
829   case ARM::ArchKind::ARMV7R:
830     return Triple::ARMSubArch_v7;
831   case ARM::ArchKind::ARMV7VE:
832     return Triple::ARMSubArch_v7ve;
833   case ARM::ArchKind::ARMV7K:
834     return Triple::ARMSubArch_v7k;
835   case ARM::ArchKind::ARMV7M:
836     return Triple::ARMSubArch_v7m;
837   case ARM::ArchKind::ARMV7S:
838     return Triple::ARMSubArch_v7s;
839   case ARM::ArchKind::ARMV7EM:
840     return Triple::ARMSubArch_v7em;
841   case ARM::ArchKind::ARMV8A:
842     return Triple::ARMSubArch_v8;
843   case ARM::ArchKind::ARMV8_1A:
844     return Triple::ARMSubArch_v8_1a;
845   case ARM::ArchKind::ARMV8_2A:
846     return Triple::ARMSubArch_v8_2a;
847   case ARM::ArchKind::ARMV8_3A:
848     return Triple::ARMSubArch_v8_3a;
849   case ARM::ArchKind::ARMV8_4A:
850     return Triple::ARMSubArch_v8_4a;
851   case ARM::ArchKind::ARMV8_5A:
852     return Triple::ARMSubArch_v8_5a;
853   case ARM::ArchKind::ARMV8_6A:
854     return Triple::ARMSubArch_v8_6a;
855   case ARM::ArchKind::ARMV8_7A:
856     return Triple::ARMSubArch_v8_7a;
857   case ARM::ArchKind::ARMV8_8A:
858     return Triple::ARMSubArch_v8_8a;
859   case ARM::ArchKind::ARMV8_9A:
860     return Triple::ARMSubArch_v8_9a;
861   case ARM::ArchKind::ARMV9A:
862     return Triple::ARMSubArch_v9;
863   case ARM::ArchKind::ARMV9_1A:
864     return Triple::ARMSubArch_v9_1a;
865   case ARM::ArchKind::ARMV9_2A:
866     return Triple::ARMSubArch_v9_2a;
867   case ARM::ArchKind::ARMV9_3A:
868     return Triple::ARMSubArch_v9_3a;
869   case ARM::ArchKind::ARMV9_4A:
870     return Triple::ARMSubArch_v9_4a;
871   case ARM::ArchKind::ARMV9_5A:
872     return Triple::ARMSubArch_v9_5a;
873   case ARM::ArchKind::ARMV8R:
874     return Triple::ARMSubArch_v8r;
875   case ARM::ArchKind::ARMV8MBaseline:
876     return Triple::ARMSubArch_v8m_baseline;
877   case ARM::ArchKind::ARMV8MMainline:
878     return Triple::ARMSubArch_v8m_mainline;
879   case ARM::ArchKind::ARMV8_1MMainline:
880     return Triple::ARMSubArch_v8_1m_mainline;
881   default:
882     return Triple::NoSubArch;
883   }
884 }
885 
getDefaultFormat(const Triple & T)886 static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
887   switch (T.getArch()) {
888   case Triple::UnknownArch:
889   case Triple::aarch64:
890   case Triple::aarch64_32:
891   case Triple::arm:
892   case Triple::thumb:
893   case Triple::x86:
894   case Triple::x86_64:
895     switch (T.getOS()) {
896     case Triple::Win32:
897     case Triple::UEFI:
898       return Triple::COFF;
899     default:
900       return T.isOSDarwin() ? Triple::MachO : Triple::ELF;
901     }
902   case Triple::aarch64_be:
903   case Triple::amdgcn:
904   case Triple::amdil64:
905   case Triple::amdil:
906   case Triple::arc:
907   case Triple::armeb:
908   case Triple::avr:
909   case Triple::bpfeb:
910   case Triple::bpfel:
911   case Triple::csky:
912   case Triple::hexagon:
913   case Triple::hsail64:
914   case Triple::hsail:
915   case Triple::kalimba:
916   case Triple::lanai:
917   case Triple::le32:
918   case Triple::le64:
919   case Triple::loongarch32:
920   case Triple::loongarch64:
921   case Triple::m68k:
922   case Triple::mips64:
923   case Triple::mips64el:
924   case Triple::mips:
925   case Triple::mipsel:
926   case Triple::msp430:
927   case Triple::nvptx64:
928   case Triple::nvptx:
929   case Triple::ppc64le:
930   case Triple::ppcle:
931   case Triple::r600:
932   case Triple::renderscript32:
933   case Triple::renderscript64:
934   case Triple::riscv32:
935   case Triple::riscv64:
936   case Triple::shave:
937   case Triple::sparc:
938   case Triple::sparcel:
939   case Triple::sparcv9:
940   case Triple::spir64:
941   case Triple::spir:
942   case Triple::tce:
943   case Triple::tcele:
944   case Triple::thumbeb:
945   case Triple::ve:
946   case Triple::xcore:
947   case Triple::xtensa:
948     return Triple::ELF;
949 
950   case Triple::ppc64:
951   case Triple::ppc:
952     if (T.isOSAIX())
953       return Triple::XCOFF;
954     if (T.isOSDarwin())
955       return Triple::MachO;
956     return Triple::ELF;
957 
958   case Triple::systemz:
959     if (T.isOSzOS())
960       return Triple::GOFF;
961     return Triple::ELF;
962 
963   case Triple::wasm32:
964   case Triple::wasm64:
965     return Triple::Wasm;
966 
967   case Triple::spirv:
968   case Triple::spirv32:
969   case Triple::spirv64:
970     return Triple::SPIRV;
971 
972   case Triple::dxil:
973     return Triple::DXContainer;
974   }
975   llvm_unreachable("unknown architecture");
976 }
977 
978 /// Construct a triple from the string representation provided.
979 ///
980 /// This stores the string representation and parses the various pieces into
981 /// enum members.
Triple(const Twine & Str)982 Triple::Triple(const Twine &Str)
983     : Data(Str.str()), Arch(UnknownArch), SubArch(NoSubArch),
984       Vendor(UnknownVendor), OS(UnknownOS), Environment(UnknownEnvironment),
985       ObjectFormat(UnknownObjectFormat) {
986   // Do minimal parsing by hand here.
987   SmallVector<StringRef, 4> Components;
988   StringRef(Data).split(Components, '-', /*MaxSplit*/ 3);
989   if (Components.size() > 0) {
990     Arch = parseArch(Components[0]);
991     SubArch = parseSubArch(Components[0]);
992     if (Components.size() > 1) {
993       Vendor = parseVendor(Components[1]);
994       if (Components.size() > 2) {
995         OS = parseOS(Components[2]);
996         if (Components.size() > 3) {
997           Environment = parseEnvironment(Components[3]);
998           ObjectFormat = parseFormat(Components[3]);
999         }
1000       }
1001     } else {
1002       Environment =
1003           StringSwitch<Triple::EnvironmentType>(Components[0])
1004               .StartsWith("mipsn32", Triple::GNUABIN32)
1005               .StartsWith("mips64", Triple::GNUABI64)
1006               .StartsWith("mipsisa64", Triple::GNUABI64)
1007               .StartsWith("mipsisa32", Triple::GNU)
1008               .Cases("mips", "mipsel", "mipsr6", "mipsr6el", Triple::GNU)
1009               .Default(UnknownEnvironment);
1010     }
1011   }
1012   if (ObjectFormat == UnknownObjectFormat)
1013     ObjectFormat = getDefaultFormat(*this);
1014 }
1015 
1016 /// Construct a triple from string representations of the architecture,
1017 /// vendor, and OS.
1018 ///
1019 /// This joins each argument into a canonical string representation and parses
1020 /// them into enum members. It leaves the environment unknown and omits it from
1021 /// the string representation.
Triple(const Twine & ArchStr,const Twine & VendorStr,const Twine & OSStr)1022 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
1023     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
1024       Arch(parseArch(ArchStr.str())),
1025       SubArch(parseSubArch(ArchStr.str())),
1026       Vendor(parseVendor(VendorStr.str())),
1027       OS(parseOS(OSStr.str())),
1028       Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
1029   ObjectFormat = getDefaultFormat(*this);
1030 }
1031 
1032 /// Construct a triple from string representations of the architecture,
1033 /// vendor, OS, and environment.
1034 ///
1035 /// This joins each argument into a canonical string representation and parses
1036 /// them into enum members.
Triple(const Twine & ArchStr,const Twine & VendorStr,const Twine & OSStr,const Twine & EnvironmentStr)1037 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
1038                const Twine &EnvironmentStr)
1039     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
1040             EnvironmentStr).str()),
1041       Arch(parseArch(ArchStr.str())),
1042       SubArch(parseSubArch(ArchStr.str())),
1043       Vendor(parseVendor(VendorStr.str())),
1044       OS(parseOS(OSStr.str())),
1045       Environment(parseEnvironment(EnvironmentStr.str())),
1046       ObjectFormat(parseFormat(EnvironmentStr.str())) {
1047   if (ObjectFormat == Triple::UnknownObjectFormat)
1048     ObjectFormat = getDefaultFormat(*this);
1049 }
1050 
1051 static VersionTuple parseVersionFromName(StringRef Name);
1052 
getDXILArchNameFromShaderModel(StringRef ShaderModelStr)1053 static StringRef getDXILArchNameFromShaderModel(StringRef ShaderModelStr) {
1054   VersionTuple Ver =
1055       parseVersionFromName(ShaderModelStr.drop_front(strlen("shadermodel")));
1056   // Default DXIL minor version when Shader Model version is anything other
1057   // than 6.[0...8] or 6.x (which translates to latest current SM version)
1058   const unsigned SMMajor = 6;
1059   if (!Ver.empty()) {
1060     if (Ver.getMajor() == SMMajor) {
1061       if (std::optional<unsigned> SMMinor = Ver.getMinor()) {
1062         switch (*SMMinor) {
1063         case 0:
1064           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_0);
1065         case 1:
1066           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_1);
1067         case 2:
1068           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_2);
1069         case 3:
1070           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_3);
1071         case 4:
1072           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_4);
1073         case 5:
1074           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_5);
1075         case 6:
1076           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_6);
1077         case 7:
1078           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_7);
1079         case 8:
1080           return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_8);
1081         default:
1082           report_fatal_error("Unsupported Shader Model version", false);
1083         }
1084       }
1085     }
1086   } else {
1087     // Special case: DXIL minor version is set to LatestCurrentDXILMinor for
1088     // shadermodel6.x is
1089     if (ShaderModelStr == "shadermodel6.x") {
1090       return Triple::getArchName(Triple::dxil, Triple::LatestDXILSubArch);
1091     }
1092   }
1093   // DXIL version corresponding to Shader Model version other than 6.Minor
1094   // is 1.0
1095   return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_0);
1096 }
1097 
normalize(StringRef Str)1098 std::string Triple::normalize(StringRef Str) {
1099   bool IsMinGW32 = false;
1100   bool IsCygwin = false;
1101 
1102   // Parse into components.
1103   SmallVector<StringRef, 4> Components;
1104   Str.split(Components, '-');
1105 
1106   // If the first component corresponds to a known architecture, preferentially
1107   // use it for the architecture.  If the second component corresponds to a
1108   // known vendor, preferentially use it for the vendor, etc.  This avoids silly
1109   // component movement when a component parses as (eg) both a valid arch and a
1110   // valid os.
1111   ArchType Arch = UnknownArch;
1112   if (Components.size() > 0)
1113     Arch = parseArch(Components[0]);
1114   VendorType Vendor = UnknownVendor;
1115   if (Components.size() > 1)
1116     Vendor = parseVendor(Components[1]);
1117   OSType OS = UnknownOS;
1118   if (Components.size() > 2) {
1119     OS = parseOS(Components[2]);
1120     IsCygwin = Components[2].starts_with("cygwin");
1121     IsMinGW32 = Components[2].starts_with("mingw");
1122   }
1123   EnvironmentType Environment = UnknownEnvironment;
1124   if (Components.size() > 3)
1125     Environment = parseEnvironment(Components[3]);
1126   ObjectFormatType ObjectFormat = UnknownObjectFormat;
1127   if (Components.size() > 4)
1128     ObjectFormat = parseFormat(Components[4]);
1129 
1130   // Note which components are already in their final position.  These will not
1131   // be moved.
1132   bool Found[4];
1133   Found[0] = Arch != UnknownArch;
1134   Found[1] = Vendor != UnknownVendor;
1135   Found[2] = OS != UnknownOS;
1136   Found[3] = Environment != UnknownEnvironment;
1137 
1138   // If they are not there already, permute the components into their canonical
1139   // positions by seeing if they parse as a valid architecture, and if so moving
1140   // the component to the architecture position etc.
1141   for (unsigned Pos = 0; Pos != std::size(Found); ++Pos) {
1142     if (Found[Pos])
1143       continue; // Already in the canonical position.
1144 
1145     for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
1146       // Do not reparse any components that already matched.
1147       if (Idx < std::size(Found) && Found[Idx])
1148         continue;
1149 
1150       // Does this component parse as valid for the target position?
1151       bool Valid = false;
1152       StringRef Comp = Components[Idx];
1153       switch (Pos) {
1154       default: llvm_unreachable("unexpected component type!");
1155       case 0:
1156         Arch = parseArch(Comp);
1157         Valid = Arch != UnknownArch;
1158         break;
1159       case 1:
1160         Vendor = parseVendor(Comp);
1161         Valid = Vendor != UnknownVendor;
1162         break;
1163       case 2:
1164         OS = parseOS(Comp);
1165         IsCygwin = Comp.starts_with("cygwin");
1166         IsMinGW32 = Comp.starts_with("mingw");
1167         Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
1168         break;
1169       case 3:
1170         Environment = parseEnvironment(Comp);
1171         Valid = Environment != UnknownEnvironment;
1172         if (!Valid) {
1173           ObjectFormat = parseFormat(Comp);
1174           Valid = ObjectFormat != UnknownObjectFormat;
1175         }
1176         break;
1177       }
1178       if (!Valid)
1179         continue; // Nope, try the next component.
1180 
1181       // Move the component to the target position, pushing any non-fixed
1182       // components that are in the way to the right.  This tends to give
1183       // good results in the common cases of a forgotten vendor component
1184       // or a wrongly positioned environment.
1185       if (Pos < Idx) {
1186         // Insert left, pushing the existing components to the right.  For
1187         // example, a-b-i386 -> i386-a-b when moving i386 to the front.
1188         StringRef CurrentComponent(""); // The empty component.
1189         // Replace the component we are moving with an empty component.
1190         std::swap(CurrentComponent, Components[Idx]);
1191         // Insert the component being moved at Pos, displacing any existing
1192         // components to the right.
1193         for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
1194           // Skip over any fixed components.
1195           while (i < std::size(Found) && Found[i])
1196             ++i;
1197           // Place the component at the new position, getting the component
1198           // that was at this position - it will be moved right.
1199           std::swap(CurrentComponent, Components[i]);
1200         }
1201       } else if (Pos > Idx) {
1202         // Push right by inserting empty components until the component at Idx
1203         // reaches the target position Pos.  For example, pc-a -> -pc-a when
1204         // moving pc to the second position.
1205         do {
1206           // Insert one empty component at Idx.
1207           StringRef CurrentComponent(""); // The empty component.
1208           for (unsigned i = Idx; i < Components.size();) {
1209             // Place the component at the new position, getting the component
1210             // that was at this position - it will be moved right.
1211             std::swap(CurrentComponent, Components[i]);
1212             // If it was placed on top of an empty component then we are done.
1213             if (CurrentComponent.empty())
1214               break;
1215             // Advance to the next component, skipping any fixed components.
1216             while (++i < std::size(Found) && Found[i])
1217               ;
1218           }
1219           // The last component was pushed off the end - append it.
1220           if (!CurrentComponent.empty())
1221             Components.push_back(CurrentComponent);
1222 
1223           // Advance Idx to the component's new position.
1224           while (++Idx < std::size(Found) && Found[Idx])
1225             ;
1226         } while (Idx < Pos); // Add more until the final position is reached.
1227       }
1228       assert(Pos < Components.size() && Components[Pos] == Comp &&
1229              "Component moved wrong!");
1230       Found[Pos] = true;
1231       break;
1232     }
1233   }
1234 
1235   // If "none" is in the middle component in a three-component triple, treat it
1236   // as the OS (Components[2]) instead of the vendor (Components[1]).
1237   if (Found[0] && !Found[1] && !Found[2] && Found[3] &&
1238       Components[1] == "none" && Components[2].empty())
1239     std::swap(Components[1], Components[2]);
1240 
1241   // Replace empty components with "unknown" value.
1242   for (StringRef &C : Components)
1243     if (C.empty())
1244       C = "unknown";
1245 
1246   // Special case logic goes here.  At this point Arch, Vendor and OS have the
1247   // correct values for the computed components.
1248   std::string NormalizedEnvironment;
1249   if (Environment == Triple::Android &&
1250       Components[3].starts_with("androideabi")) {
1251     StringRef AndroidVersion = Components[3].drop_front(strlen("androideabi"));
1252     if (AndroidVersion.empty()) {
1253       Components[3] = "android";
1254     } else {
1255       NormalizedEnvironment = Twine("android", AndroidVersion).str();
1256       Components[3] = NormalizedEnvironment;
1257     }
1258   }
1259 
1260   // SUSE uses "gnueabi" to mean "gnueabihf"
1261   if (Vendor == Triple::SUSE && Environment == llvm::Triple::GNUEABI)
1262     Components[3] = "gnueabihf";
1263 
1264   if (OS == Triple::Win32) {
1265     Components.resize(4);
1266     Components[2] = "windows";
1267     if (Environment == UnknownEnvironment) {
1268       if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
1269         Components[3] = "msvc";
1270       else
1271         Components[3] = getObjectFormatTypeName(ObjectFormat);
1272     }
1273   } else if (IsMinGW32) {
1274     Components.resize(4);
1275     Components[2] = "windows";
1276     Components[3] = "gnu";
1277   } else if (IsCygwin) {
1278     Components.resize(4);
1279     Components[2] = "windows";
1280     Components[3] = "cygnus";
1281   }
1282   if (IsMinGW32 || IsCygwin ||
1283       (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
1284     if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
1285       Components.resize(5);
1286       Components[4] = getObjectFormatTypeName(ObjectFormat);
1287     }
1288   }
1289 
1290   // Normalize DXIL triple if it does not include DXIL version number.
1291   // Determine DXIL version number using the minor version number of Shader
1292   // Model version specified in target triple, if any. Prior to decoupling DXIL
1293   // version numbering from that of Shader Model DXIL version 1.Y corresponds to
1294   // SM 6.Y. E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
1295   if (Components[0] == "dxil") {
1296     if (Components.size() > 4) {
1297       Components.resize(4);
1298     }
1299     // Add DXIL version only if shadermodel is specified in the triple
1300     if (OS == Triple::ShaderModel) {
1301       Components[0] = getDXILArchNameFromShaderModel(Components[2]);
1302     }
1303   }
1304   // Stick the corrected components back together to form the normalized string.
1305   return join(Components, "-");
1306 }
1307 
getArchName() const1308 StringRef Triple::getArchName() const {
1309   return StringRef(Data).split('-').first;           // Isolate first component
1310 }
1311 
getVendorName() const1312 StringRef Triple::getVendorName() const {
1313   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
1314   return Tmp.split('-').first;                       // Isolate second component
1315 }
1316 
getOSName() const1317 StringRef Triple::getOSName() const {
1318   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
1319   Tmp = Tmp.split('-').second;                       // Strip second component
1320   return Tmp.split('-').first;                       // Isolate third component
1321 }
1322 
getEnvironmentName() const1323 StringRef Triple::getEnvironmentName() const {
1324   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
1325   Tmp = Tmp.split('-').second;                       // Strip second component
1326   return Tmp.split('-').second;                      // Strip third component
1327 }
1328 
getOSAndEnvironmentName() const1329 StringRef Triple::getOSAndEnvironmentName() const {
1330   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
1331   return Tmp.split('-').second;                      // Strip second component
1332 }
1333 
parseVersionFromName(StringRef Name)1334 static VersionTuple parseVersionFromName(StringRef Name) {
1335   VersionTuple Version;
1336   Version.tryParse(Name);
1337   return Version.withoutBuild();
1338 }
1339 
getEnvironmentVersion() const1340 VersionTuple Triple::getEnvironmentVersion() const {
1341   return parseVersionFromName(getEnvironmentVersionString());
1342 }
1343 
getEnvironmentVersionString() const1344 StringRef Triple::getEnvironmentVersionString() const {
1345   StringRef EnvironmentName = getEnvironmentName();
1346 
1347   // none is a valid environment type - it basically amounts to a freestanding
1348   // environment.
1349   if (EnvironmentName == "none")
1350     return "";
1351 
1352   StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
1353   EnvironmentName.consume_front(EnvironmentTypeName);
1354 
1355   if (EnvironmentName.contains("-")) {
1356     // -obj is the suffix
1357     if (getObjectFormat() != Triple::UnknownObjectFormat) {
1358       StringRef ObjectFormatTypeName =
1359           getObjectFormatTypeName(getObjectFormat());
1360       const std::string tmp = (Twine("-") + ObjectFormatTypeName).str();
1361       EnvironmentName.consume_back(tmp);
1362     }
1363   }
1364   return EnvironmentName;
1365 }
1366 
getOSVersion() const1367 VersionTuple Triple::getOSVersion() const {
1368   StringRef OSName = getOSName();
1369   // Assume that the OS portion of the triple starts with the canonical name.
1370   StringRef OSTypeName = getOSTypeName(getOS());
1371   if (OSName.starts_with(OSTypeName))
1372     OSName = OSName.substr(OSTypeName.size());
1373   else if (getOS() == MacOSX)
1374     OSName.consume_front("macos");
1375   else if (OSName.starts_with("visionos"))
1376     OSName.consume_front("visionos");
1377 
1378   return parseVersionFromName(OSName);
1379 }
1380 
getMacOSXVersion(VersionTuple & Version) const1381 bool Triple::getMacOSXVersion(VersionTuple &Version) const {
1382   Version = getOSVersion();
1383 
1384   switch (getOS()) {
1385   default: llvm_unreachable("unexpected OS for Darwin triple");
1386   case Darwin:
1387     // Default to darwin8, i.e., MacOSX 10.4.
1388     if (Version.getMajor() == 0)
1389       Version = VersionTuple(8);
1390     // Darwin version numbers are skewed from OS X versions.
1391     if (Version.getMajor() < 4) {
1392       return false;
1393     }
1394     if (Version.getMajor() <= 19) {
1395       Version = VersionTuple(10, Version.getMajor() - 4);
1396     } else {
1397       // darwin20+ corresponds to macOS 11+.
1398       Version = VersionTuple(11 + Version.getMajor() - 20);
1399     }
1400     break;
1401   case MacOSX:
1402     // Default to 10.4.
1403     if (Version.getMajor() == 0) {
1404       Version = VersionTuple(10, 4);
1405     } else if (Version.getMajor() < 10) {
1406       return false;
1407     }
1408     break;
1409   case IOS:
1410   case TvOS:
1411   case WatchOS:
1412     // Ignore the version from the triple.  This is only handled because the
1413     // the clang driver combines OS X and IOS support into a common Darwin
1414     // toolchain that wants to know the OS X version number even when targeting
1415     // IOS.
1416     Version = VersionTuple(10, 4);
1417     break;
1418   case XROS:
1419     llvm_unreachable("OSX version isn't relevant for xrOS");
1420   case DriverKit:
1421     llvm_unreachable("OSX version isn't relevant for DriverKit");
1422   }
1423   return true;
1424 }
1425 
getiOSVersion() const1426 VersionTuple Triple::getiOSVersion() const {
1427   switch (getOS()) {
1428   default: llvm_unreachable("unexpected OS for Darwin triple");
1429   case Darwin:
1430   case MacOSX:
1431     // Ignore the version from the triple.  This is only handled because the
1432     // the clang driver combines OS X and IOS support into a common Darwin
1433     // toolchain that wants to know the iOS version number even when targeting
1434     // OS X.
1435     return VersionTuple(5);
1436   case IOS:
1437   case TvOS: {
1438     VersionTuple Version = getOSVersion();
1439     // Default to 5.0 (or 7.0 for arm64).
1440     if (Version.getMajor() == 0)
1441       return (getArch() == aarch64) ? VersionTuple(7) : VersionTuple(5);
1442     return Version;
1443   }
1444   case XROS: {
1445     // xrOS 1 is aligned with iOS 17.
1446     VersionTuple Version = getOSVersion();
1447     return Version.withMajorReplaced(Version.getMajor() + 16);
1448   }
1449   case WatchOS:
1450     llvm_unreachable("conflicting triple info");
1451   case DriverKit:
1452     llvm_unreachable("DriverKit doesn't have an iOS version");
1453   }
1454 }
1455 
getWatchOSVersion() const1456 VersionTuple Triple::getWatchOSVersion() const {
1457   switch (getOS()) {
1458   default: llvm_unreachable("unexpected OS for Darwin triple");
1459   case Darwin:
1460   case MacOSX:
1461     // Ignore the version from the triple.  This is only handled because the
1462     // the clang driver combines OS X and IOS support into a common Darwin
1463     // toolchain that wants to know the iOS version number even when targeting
1464     // OS X.
1465     return VersionTuple(2);
1466   case WatchOS: {
1467     VersionTuple Version = getOSVersion();
1468     if (Version.getMajor() == 0)
1469       return VersionTuple(2);
1470     return Version;
1471   }
1472   case IOS:
1473     llvm_unreachable("conflicting triple info");
1474   case XROS:
1475     llvm_unreachable("watchOS version isn't relevant for xrOS");
1476   case DriverKit:
1477     llvm_unreachable("DriverKit doesn't have a WatchOS version");
1478   }
1479 }
1480 
getDriverKitVersion() const1481 VersionTuple Triple::getDriverKitVersion() const {
1482   switch (getOS()) {
1483   default:
1484     llvm_unreachable("unexpected OS for Darwin triple");
1485   case DriverKit:
1486     VersionTuple Version = getOSVersion();
1487     if (Version.getMajor() == 0)
1488       return Version.withMajorReplaced(19);
1489     return Version;
1490   }
1491 }
1492 
getVulkanVersion() const1493 VersionTuple Triple::getVulkanVersion() const {
1494   if (getArch() != spirv || getOS() != Vulkan)
1495     llvm_unreachable("invalid Vulkan SPIR-V triple");
1496 
1497   VersionTuple VulkanVersion = getOSVersion();
1498   SubArchType SpirvVersion = getSubArch();
1499 
1500   llvm::DenseMap<VersionTuple, SubArchType> ValidVersionMap = {
1501       // Vulkan 1.2 -> SPIR-V 1.5.
1502       {VersionTuple(1, 2), SPIRVSubArch_v15},
1503       // Vulkan 1.3 -> SPIR-V 1.6.
1504       {VersionTuple(1, 3), SPIRVSubArch_v16}};
1505 
1506   // If Vulkan version is unset, default to 1.2.
1507   if (VulkanVersion == VersionTuple(0))
1508     VulkanVersion = VersionTuple(1, 2);
1509 
1510   if (ValidVersionMap.contains(VulkanVersion) &&
1511       (ValidVersionMap.lookup(VulkanVersion) == SpirvVersion ||
1512        SpirvVersion == NoSubArch))
1513     return VulkanVersion;
1514 
1515   return VersionTuple(0);
1516 }
1517 
getDXILVersion() const1518 VersionTuple Triple::getDXILVersion() const {
1519   if (getArch() != dxil || getOS() != ShaderModel)
1520     llvm_unreachable("invalid DXIL triple");
1521   StringRef Arch = getArchName();
1522   if (getSubArch() == NoSubArch)
1523     Arch = getDXILArchNameFromShaderModel(getOSName());
1524   Arch.consume_front("dxilv");
1525   VersionTuple DXILVersion = parseVersionFromName(Arch);
1526   // FIXME: validate DXIL version against Shader Model version.
1527   // Tracked by https://github.com/llvm/llvm-project/issues/91388
1528   return DXILVersion;
1529 }
1530 
setTriple(const Twine & Str)1531 void Triple::setTriple(const Twine &Str) {
1532   *this = Triple(Str);
1533 }
1534 
setArch(ArchType Kind,SubArchType SubArch)1535 void Triple::setArch(ArchType Kind, SubArchType SubArch) {
1536   setArchName(getArchName(Kind, SubArch));
1537 }
1538 
setVendor(VendorType Kind)1539 void Triple::setVendor(VendorType Kind) {
1540   setVendorName(getVendorTypeName(Kind));
1541 }
1542 
setOS(OSType Kind)1543 void Triple::setOS(OSType Kind) {
1544   setOSName(getOSTypeName(Kind));
1545 }
1546 
setEnvironment(EnvironmentType Kind)1547 void Triple::setEnvironment(EnvironmentType Kind) {
1548   if (ObjectFormat == getDefaultFormat(*this))
1549     return setEnvironmentName(getEnvironmentTypeName(Kind));
1550 
1551   setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
1552                       getObjectFormatTypeName(ObjectFormat)).str());
1553 }
1554 
setObjectFormat(ObjectFormatType Kind)1555 void Triple::setObjectFormat(ObjectFormatType Kind) {
1556   if (Environment == UnknownEnvironment)
1557     return setEnvironmentName(getObjectFormatTypeName(Kind));
1558 
1559   setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
1560                       getObjectFormatTypeName(Kind)).str());
1561 }
1562 
setArchName(StringRef Str)1563 void Triple::setArchName(StringRef Str) {
1564   // Work around a miscompilation bug for Twines in gcc 4.0.3.
1565   SmallString<64> Triple;
1566   Triple += Str;
1567   Triple += "-";
1568   Triple += getVendorName();
1569   Triple += "-";
1570   Triple += getOSAndEnvironmentName();
1571   setTriple(Triple);
1572 }
1573 
setVendorName(StringRef Str)1574 void Triple::setVendorName(StringRef Str) {
1575   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
1576 }
1577 
setOSName(StringRef Str)1578 void Triple::setOSName(StringRef Str) {
1579   if (hasEnvironment())
1580     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
1581               "-" + getEnvironmentName());
1582   else
1583     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1584 }
1585 
setEnvironmentName(StringRef Str)1586 void Triple::setEnvironmentName(StringRef Str) {
1587   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
1588             "-" + Str);
1589 }
1590 
setOSAndEnvironmentName(StringRef Str)1591 void Triple::setOSAndEnvironmentName(StringRef Str) {
1592   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1593 }
1594 
getArchPointerBitWidth(llvm::Triple::ArchType Arch)1595 unsigned Triple::getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
1596   switch (Arch) {
1597   case llvm::Triple::UnknownArch:
1598     return 0;
1599 
1600   case llvm::Triple::avr:
1601   case llvm::Triple::msp430:
1602     return 16;
1603 
1604   case llvm::Triple::aarch64_32:
1605   case llvm::Triple::amdil:
1606   case llvm::Triple::arc:
1607   case llvm::Triple::arm:
1608   case llvm::Triple::armeb:
1609   case llvm::Triple::csky:
1610   case llvm::Triple::dxil:
1611   case llvm::Triple::hexagon:
1612   case llvm::Triple::hsail:
1613   case llvm::Triple::kalimba:
1614   case llvm::Triple::lanai:
1615   case llvm::Triple::le32:
1616   case llvm::Triple::loongarch32:
1617   case llvm::Triple::m68k:
1618   case llvm::Triple::mips:
1619   case llvm::Triple::mipsel:
1620   case llvm::Triple::nvptx:
1621   case llvm::Triple::ppc:
1622   case llvm::Triple::ppcle:
1623   case llvm::Triple::r600:
1624   case llvm::Triple::renderscript32:
1625   case llvm::Triple::riscv32:
1626   case llvm::Triple::shave:
1627   case llvm::Triple::sparc:
1628   case llvm::Triple::sparcel:
1629   case llvm::Triple::spir:
1630   case llvm::Triple::spirv32:
1631   case llvm::Triple::tce:
1632   case llvm::Triple::tcele:
1633   case llvm::Triple::thumb:
1634   case llvm::Triple::thumbeb:
1635   case llvm::Triple::wasm32:
1636   case llvm::Triple::x86:
1637   case llvm::Triple::xcore:
1638   case llvm::Triple::xtensa:
1639     return 32;
1640 
1641   case llvm::Triple::aarch64:
1642   case llvm::Triple::aarch64_be:
1643   case llvm::Triple::amdgcn:
1644   case llvm::Triple::amdil64:
1645   case llvm::Triple::bpfeb:
1646   case llvm::Triple::bpfel:
1647   case llvm::Triple::hsail64:
1648   case llvm::Triple::le64:
1649   case llvm::Triple::loongarch64:
1650   case llvm::Triple::mips64:
1651   case llvm::Triple::mips64el:
1652   case llvm::Triple::nvptx64:
1653   case llvm::Triple::ppc64:
1654   case llvm::Triple::ppc64le:
1655   case llvm::Triple::renderscript64:
1656   case llvm::Triple::riscv64:
1657   case llvm::Triple::sparcv9:
1658   case llvm::Triple::spirv:
1659   case llvm::Triple::spir64:
1660   case llvm::Triple::spirv64:
1661   case llvm::Triple::systemz:
1662   case llvm::Triple::ve:
1663   case llvm::Triple::wasm64:
1664   case llvm::Triple::x86_64:
1665     return 64;
1666   }
1667   llvm_unreachable("Invalid architecture value");
1668 }
1669 
isArch64Bit() const1670 bool Triple::isArch64Bit() const {
1671   return getArchPointerBitWidth(getArch()) == 64;
1672 }
1673 
isArch32Bit() const1674 bool Triple::isArch32Bit() const {
1675   return getArchPointerBitWidth(getArch()) == 32;
1676 }
1677 
isArch16Bit() const1678 bool Triple::isArch16Bit() const {
1679   return getArchPointerBitWidth(getArch()) == 16;
1680 }
1681 
get32BitArchVariant() const1682 Triple Triple::get32BitArchVariant() const {
1683   Triple T(*this);
1684   switch (getArch()) {
1685   case Triple::UnknownArch:
1686   case Triple::amdgcn:
1687   case Triple::avr:
1688   case Triple::bpfeb:
1689   case Triple::bpfel:
1690   case Triple::msp430:
1691   case Triple::systemz:
1692   case Triple::ve:
1693     T.setArch(UnknownArch);
1694     break;
1695 
1696   case Triple::aarch64_32:
1697   case Triple::amdil:
1698   case Triple::arc:
1699   case Triple::arm:
1700   case Triple::armeb:
1701   case Triple::csky:
1702   case Triple::dxil:
1703   case Triple::hexagon:
1704   case Triple::hsail:
1705   case Triple::kalimba:
1706   case Triple::lanai:
1707   case Triple::le32:
1708   case Triple::loongarch32:
1709   case Triple::m68k:
1710   case Triple::mips:
1711   case Triple::mipsel:
1712   case Triple::nvptx:
1713   case Triple::ppc:
1714   case Triple::ppcle:
1715   case Triple::r600:
1716   case Triple::renderscript32:
1717   case Triple::riscv32:
1718   case Triple::shave:
1719   case Triple::sparc:
1720   case Triple::sparcel:
1721   case Triple::spir:
1722   case Triple::spirv32:
1723   case Triple::tce:
1724   case Triple::tcele:
1725   case Triple::thumb:
1726   case Triple::thumbeb:
1727   case Triple::wasm32:
1728   case Triple::x86:
1729   case Triple::xcore:
1730   case Triple::xtensa:
1731     // Already 32-bit.
1732     break;
1733 
1734   case Triple::aarch64:        T.setArch(Triple::arm);     break;
1735   case Triple::aarch64_be:     T.setArch(Triple::armeb);   break;
1736   case Triple::amdil64:        T.setArch(Triple::amdil);   break;
1737   case Triple::hsail64:        T.setArch(Triple::hsail);   break;
1738   case Triple::le64:           T.setArch(Triple::le32);    break;
1739   case Triple::loongarch64:    T.setArch(Triple::loongarch32); break;
1740   case Triple::mips64:
1741     T.setArch(Triple::mips, getSubArch());
1742     break;
1743   case Triple::mips64el:
1744     T.setArch(Triple::mipsel, getSubArch());
1745     break;
1746   case Triple::nvptx64:        T.setArch(Triple::nvptx);   break;
1747   case Triple::ppc64:          T.setArch(Triple::ppc);     break;
1748   case Triple::ppc64le:        T.setArch(Triple::ppcle);   break;
1749   case Triple::renderscript64: T.setArch(Triple::renderscript32); break;
1750   case Triple::riscv64:        T.setArch(Triple::riscv32); break;
1751   case Triple::sparcv9:        T.setArch(Triple::sparc);   break;
1752   case Triple::spir64:         T.setArch(Triple::spir);    break;
1753   case Triple::spirv:
1754   case Triple::spirv64:
1755     T.setArch(Triple::spirv32, getSubArch());
1756     break;
1757   case Triple::wasm64:         T.setArch(Triple::wasm32);  break;
1758   case Triple::x86_64:         T.setArch(Triple::x86);     break;
1759   }
1760   return T;
1761 }
1762 
get64BitArchVariant() const1763 Triple Triple::get64BitArchVariant() const {
1764   Triple T(*this);
1765   switch (getArch()) {
1766   case Triple::UnknownArch:
1767   case Triple::arc:
1768   case Triple::avr:
1769   case Triple::csky:
1770   case Triple::dxil:
1771   case Triple::hexagon:
1772   case Triple::kalimba:
1773   case Triple::lanai:
1774   case Triple::m68k:
1775   case Triple::msp430:
1776   case Triple::r600:
1777   case Triple::shave:
1778   case Triple::sparcel:
1779   case Triple::tce:
1780   case Triple::tcele:
1781   case Triple::xcore:
1782   case Triple::xtensa:
1783     T.setArch(UnknownArch);
1784     break;
1785 
1786   case Triple::aarch64:
1787   case Triple::aarch64_be:
1788   case Triple::amdgcn:
1789   case Triple::amdil64:
1790   case Triple::bpfeb:
1791   case Triple::bpfel:
1792   case Triple::hsail64:
1793   case Triple::le64:
1794   case Triple::loongarch64:
1795   case Triple::mips64:
1796   case Triple::mips64el:
1797   case Triple::nvptx64:
1798   case Triple::ppc64:
1799   case Triple::ppc64le:
1800   case Triple::renderscript64:
1801   case Triple::riscv64:
1802   case Triple::sparcv9:
1803   case Triple::spir64:
1804   case Triple::spirv64:
1805   case Triple::systemz:
1806   case Triple::ve:
1807   case Triple::wasm64:
1808   case Triple::x86_64:
1809     // Already 64-bit.
1810     break;
1811 
1812   case Triple::aarch64_32:      T.setArch(Triple::aarch64);    break;
1813   case Triple::amdil:           T.setArch(Triple::amdil64);    break;
1814   case Triple::arm:             T.setArch(Triple::aarch64);    break;
1815   case Triple::armeb:           T.setArch(Triple::aarch64_be); break;
1816   case Triple::hsail:           T.setArch(Triple::hsail64);    break;
1817   case Triple::le32:            T.setArch(Triple::le64);       break;
1818   case Triple::loongarch32:     T.setArch(Triple::loongarch64);    break;
1819   case Triple::mips:
1820     T.setArch(Triple::mips64, getSubArch());
1821     break;
1822   case Triple::mipsel:
1823     T.setArch(Triple::mips64el, getSubArch());
1824     break;
1825   case Triple::nvptx:           T.setArch(Triple::nvptx64);    break;
1826   case Triple::ppc:             T.setArch(Triple::ppc64);      break;
1827   case Triple::ppcle:           T.setArch(Triple::ppc64le);    break;
1828   case Triple::renderscript32:  T.setArch(Triple::renderscript64);     break;
1829   case Triple::riscv32:         T.setArch(Triple::riscv64);    break;
1830   case Triple::sparc:           T.setArch(Triple::sparcv9);    break;
1831   case Triple::spir:            T.setArch(Triple::spir64);     break;
1832   case Triple::spirv:
1833   case Triple::spirv32:
1834     T.setArch(Triple::spirv64, getSubArch());
1835     break;
1836   case Triple::thumb:           T.setArch(Triple::aarch64);    break;
1837   case Triple::thumbeb:         T.setArch(Triple::aarch64_be); break;
1838   case Triple::wasm32:          T.setArch(Triple::wasm64);     break;
1839   case Triple::x86:             T.setArch(Triple::x86_64);     break;
1840   }
1841   return T;
1842 }
1843 
getBigEndianArchVariant() const1844 Triple Triple::getBigEndianArchVariant() const {
1845   Triple T(*this);
1846   // Already big endian.
1847   if (!isLittleEndian())
1848     return T;
1849   switch (getArch()) {
1850   case Triple::UnknownArch:
1851   case Triple::amdgcn:
1852   case Triple::amdil64:
1853   case Triple::amdil:
1854   case Triple::avr:
1855   case Triple::dxil:
1856   case Triple::hexagon:
1857   case Triple::hsail64:
1858   case Triple::hsail:
1859   case Triple::kalimba:
1860   case Triple::le32:
1861   case Triple::le64:
1862   case Triple::loongarch32:
1863   case Triple::loongarch64:
1864   case Triple::msp430:
1865   case Triple::nvptx64:
1866   case Triple::nvptx:
1867   case Triple::r600:
1868   case Triple::renderscript32:
1869   case Triple::renderscript64:
1870   case Triple::riscv32:
1871   case Triple::riscv64:
1872   case Triple::shave:
1873   case Triple::spir64:
1874   case Triple::spir:
1875   case Triple::spirv:
1876   case Triple::spirv32:
1877   case Triple::spirv64:
1878   case Triple::wasm32:
1879   case Triple::wasm64:
1880   case Triple::x86:
1881   case Triple::x86_64:
1882   case Triple::xcore:
1883   case Triple::ve:
1884   case Triple::csky:
1885   case Triple::xtensa:
1886 
1887   // ARM is intentionally unsupported here, changing the architecture would
1888   // drop any arch suffixes.
1889   case Triple::arm:
1890   case Triple::thumb:
1891     T.setArch(UnknownArch);
1892     break;
1893 
1894   case Triple::aarch64: T.setArch(Triple::aarch64_be); break;
1895   case Triple::bpfel:   T.setArch(Triple::bpfeb);      break;
1896   case Triple::mips64el:
1897     T.setArch(Triple::mips64, getSubArch());
1898     break;
1899   case Triple::mipsel:
1900     T.setArch(Triple::mips, getSubArch());
1901     break;
1902   case Triple::ppcle:   T.setArch(Triple::ppc);        break;
1903   case Triple::ppc64le: T.setArch(Triple::ppc64);      break;
1904   case Triple::sparcel: T.setArch(Triple::sparc);      break;
1905   case Triple::tcele:   T.setArch(Triple::tce);        break;
1906   default:
1907     llvm_unreachable("getBigEndianArchVariant: unknown triple.");
1908   }
1909   return T;
1910 }
1911 
getLittleEndianArchVariant() const1912 Triple Triple::getLittleEndianArchVariant() const {
1913   Triple T(*this);
1914   if (isLittleEndian())
1915     return T;
1916 
1917   switch (getArch()) {
1918   case Triple::UnknownArch:
1919   case Triple::lanai:
1920   case Triple::sparcv9:
1921   case Triple::systemz:
1922   case Triple::m68k:
1923 
1924   // ARM is intentionally unsupported here, changing the architecture would
1925   // drop any arch suffixes.
1926   case Triple::armeb:
1927   case Triple::thumbeb:
1928     T.setArch(UnknownArch);
1929     break;
1930 
1931   case Triple::aarch64_be: T.setArch(Triple::aarch64);  break;
1932   case Triple::bpfeb:      T.setArch(Triple::bpfel);    break;
1933   case Triple::mips64:
1934     T.setArch(Triple::mips64el, getSubArch());
1935     break;
1936   case Triple::mips:
1937     T.setArch(Triple::mipsel, getSubArch());
1938     break;
1939   case Triple::ppc:        T.setArch(Triple::ppcle);    break;
1940   case Triple::ppc64:      T.setArch(Triple::ppc64le);  break;
1941   case Triple::sparc:      T.setArch(Triple::sparcel);  break;
1942   case Triple::tce:        T.setArch(Triple::tcele);    break;
1943   default:
1944     llvm_unreachable("getLittleEndianArchVariant: unknown triple.");
1945   }
1946   return T;
1947 }
1948 
isLittleEndian() const1949 bool Triple::isLittleEndian() const {
1950   switch (getArch()) {
1951   case Triple::aarch64:
1952   case Triple::aarch64_32:
1953   case Triple::amdgcn:
1954   case Triple::amdil64:
1955   case Triple::amdil:
1956   case Triple::arm:
1957   case Triple::avr:
1958   case Triple::bpfel:
1959   case Triple::csky:
1960   case Triple::dxil:
1961   case Triple::hexagon:
1962   case Triple::hsail64:
1963   case Triple::hsail:
1964   case Triple::kalimba:
1965   case Triple::le32:
1966   case Triple::le64:
1967   case Triple::loongarch32:
1968   case Triple::loongarch64:
1969   case Triple::mips64el:
1970   case Triple::mipsel:
1971   case Triple::msp430:
1972   case Triple::nvptx64:
1973   case Triple::nvptx:
1974   case Triple::ppcle:
1975   case Triple::ppc64le:
1976   case Triple::r600:
1977   case Triple::renderscript32:
1978   case Triple::renderscript64:
1979   case Triple::riscv32:
1980   case Triple::riscv64:
1981   case Triple::shave:
1982   case Triple::sparcel:
1983   case Triple::spir64:
1984   case Triple::spir:
1985   case Triple::spirv:
1986   case Triple::spirv32:
1987   case Triple::spirv64:
1988   case Triple::tcele:
1989   case Triple::thumb:
1990   case Triple::ve:
1991   case Triple::wasm32:
1992   case Triple::wasm64:
1993   case Triple::x86:
1994   case Triple::x86_64:
1995   case Triple::xcore:
1996   case Triple::xtensa:
1997     return true;
1998   default:
1999     return false;
2000   }
2001 }
2002 
isCompatibleWith(const Triple & Other) const2003 bool Triple::isCompatibleWith(const Triple &Other) const {
2004   // ARM and Thumb triples are compatible, if subarch, vendor and OS match.
2005   if ((getArch() == Triple::thumb && Other.getArch() == Triple::arm) ||
2006       (getArch() == Triple::arm && Other.getArch() == Triple::thumb) ||
2007       (getArch() == Triple::thumbeb && Other.getArch() == Triple::armeb) ||
2008       (getArch() == Triple::armeb && Other.getArch() == Triple::thumbeb)) {
2009     if (getVendor() == Triple::Apple)
2010       return getSubArch() == Other.getSubArch() &&
2011              getVendor() == Other.getVendor() && getOS() == Other.getOS();
2012     else
2013       return getSubArch() == Other.getSubArch() &&
2014              getVendor() == Other.getVendor() && getOS() == Other.getOS() &&
2015              getEnvironment() == Other.getEnvironment() &&
2016              getObjectFormat() == Other.getObjectFormat();
2017   }
2018 
2019   // If vendor is apple, ignore the version number.
2020   if (getVendor() == Triple::Apple)
2021     return getArch() == Other.getArch() && getSubArch() == Other.getSubArch() &&
2022            getVendor() == Other.getVendor() && getOS() == Other.getOS();
2023 
2024   return *this == Other;
2025 }
2026 
merge(const Triple & Other) const2027 std::string Triple::merge(const Triple &Other) const {
2028   // If vendor is apple, pick the triple with the larger version number.
2029   if (getVendor() == Triple::Apple)
2030     if (Other.isOSVersionLT(*this))
2031       return str();
2032 
2033   return Other.str();
2034 }
2035 
isMacOSXVersionLT(unsigned Major,unsigned Minor,unsigned Micro) const2036 bool Triple::isMacOSXVersionLT(unsigned Major, unsigned Minor,
2037                                unsigned Micro) const {
2038   assert(isMacOSX() && "Not an OS X triple!");
2039 
2040   // If this is OS X, expect a sane version number.
2041   if (getOS() == Triple::MacOSX)
2042     return isOSVersionLT(Major, Minor, Micro);
2043 
2044   // Otherwise, compare to the "Darwin" number.
2045   if (Major == 10) {
2046     return isOSVersionLT(Minor + 4, Micro, 0);
2047   } else {
2048     assert(Major >= 11 && "Unexpected major version");
2049     return isOSVersionLT(Major - 11 + 20, Minor, Micro);
2050   }
2051 }
2052 
getMinimumSupportedOSVersion() const2053 VersionTuple Triple::getMinimumSupportedOSVersion() const {
2054   if (getVendor() != Triple::Apple || getArch() != Triple::aarch64)
2055     return VersionTuple();
2056   switch (getOS()) {
2057   case Triple::MacOSX:
2058     // ARM64 slice is supported starting from macOS 11.0+.
2059     return VersionTuple(11, 0, 0);
2060   case Triple::IOS:
2061     // ARM64 slice is supported starting from Mac Catalyst 14 (macOS 11).
2062     // ARM64 simulators are supported for iOS 14+.
2063     if (isMacCatalystEnvironment() || isSimulatorEnvironment())
2064       return VersionTuple(14, 0, 0);
2065     // ARM64e slice is supported starting from iOS 14.
2066     if (isArm64e())
2067       return VersionTuple(14, 0, 0);
2068     break;
2069   case Triple::TvOS:
2070     // ARM64 simulators are supported for tvOS 14+.
2071     if (isSimulatorEnvironment())
2072       return VersionTuple(14, 0, 0);
2073     break;
2074   case Triple::WatchOS:
2075     // ARM64 simulators are supported for watchOS 7+.
2076     if (isSimulatorEnvironment())
2077       return VersionTuple(7, 0, 0);
2078     break;
2079   case Triple::DriverKit:
2080     return VersionTuple(20, 0, 0);
2081   default:
2082     break;
2083   }
2084   return VersionTuple();
2085 }
2086 
getCanonicalVersionForOS(OSType OSKind,const VersionTuple & Version)2087 VersionTuple Triple::getCanonicalVersionForOS(OSType OSKind,
2088                                               const VersionTuple &Version) {
2089   switch (OSKind) {
2090   case MacOSX:
2091     // macOS 10.16 is canonicalized to macOS 11.
2092     if (Version == VersionTuple(10, 16))
2093       return VersionTuple(11, 0);
2094     [[fallthrough]];
2095   default:
2096     return Version;
2097   }
2098 }
2099 
2100 // HLSL triple environment orders are relied on in the front end
2101 static_assert(Triple::Vertex - Triple::Pixel == 1,
2102               "incorrect HLSL stage order");
2103 static_assert(Triple::Geometry - Triple::Pixel == 2,
2104               "incorrect HLSL stage order");
2105 static_assert(Triple::Hull - Triple::Pixel == 3,
2106               "incorrect HLSL stage order");
2107 static_assert(Triple::Domain - Triple::Pixel == 4,
2108               "incorrect HLSL stage order");
2109 static_assert(Triple::Compute - Triple::Pixel == 5,
2110               "incorrect HLSL stage order");
2111 static_assert(Triple::Library - Triple::Pixel == 6,
2112               "incorrect HLSL stage order");
2113 static_assert(Triple::RayGeneration - Triple::Pixel == 7,
2114               "incorrect HLSL stage order");
2115 static_assert(Triple::Intersection - Triple::Pixel == 8,
2116               "incorrect HLSL stage order");
2117 static_assert(Triple::AnyHit - Triple::Pixel == 9,
2118               "incorrect HLSL stage order");
2119 static_assert(Triple::ClosestHit - Triple::Pixel == 10,
2120               "incorrect HLSL stage order");
2121 static_assert(Triple::Miss - Triple::Pixel == 11,
2122               "incorrect HLSL stage order");
2123 static_assert(Triple::Callable - Triple::Pixel == 12,
2124               "incorrect HLSL stage order");
2125 static_assert(Triple::Mesh - Triple::Pixel == 13,
2126               "incorrect HLSL stage order");
2127 static_assert(Triple::Amplification - Triple::Pixel == 14,
2128               "incorrect HLSL stage order");
2129