Remove the mblaze backend from llvm.
[oota-llvm.git] / lib / Support / Triple.cpp
1 //===--- Triple.cpp - Target triple helper class --------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include <cstring>
16 using namespace llvm;
17
18 const char *Triple::getArchTypeName(ArchType Kind) {
19   switch (Kind) {
20   case UnknownArch: return "unknown";
21
22   case aarch64: return "aarch64";
23   case arm:     return "arm";
24   case hexagon: return "hexagon";
25   case mips:    return "mips";
26   case mipsel:  return "mipsel";
27   case mips64:  return "mips64";
28   case mips64el:return "mips64el";
29   case msp430:  return "msp430";
30   case ppc64:   return "powerpc64";
31   case ppc:     return "powerpc";
32   case r600:    return "r600";
33   case sparc:   return "sparc";
34   case sparcv9: return "sparcv9";
35   case systemz: return "s390x";
36   case tce:     return "tce";
37   case thumb:   return "thumb";
38   case x86:     return "i386";
39   case x86_64:  return "x86_64";
40   case xcore:   return "xcore";
41   case nvptx:   return "nvptx";
42   case nvptx64: return "nvptx64";
43   case le32:    return "le32";
44   case amdil:   return "amdil";
45   case spir:    return "spir";
46   case spir64:  return "spir64";
47   }
48
49   llvm_unreachable("Invalid ArchType!");
50 }
51
52 const char *Triple::getArchTypePrefix(ArchType Kind) {
53   switch (Kind) {
54   default:
55     return 0;
56
57   case aarch64: return "aarch64";
58
59   case arm:
60   case thumb:   return "arm";
61
62   case ppc64:
63   case ppc:     return "ppc";
64
65   case mips:
66   case mipsel:
67   case mips64:
68   case mips64el:return "mips";
69
70   case hexagon: return "hexagon";
71
72   case r600:    return "r600";
73
74   case sparcv9:
75   case sparc:   return "sparc";
76
77   case systemz: return "systemz";
78
79   case x86:
80   case x86_64:  return "x86";
81
82   case xcore:   return "xcore";
83
84   case nvptx:   return "nvptx";
85   case nvptx64: return "nvptx";
86   case le32:    return "le32";
87   case amdil:   return "amdil";
88   case spir:    return "spir";
89   case spir64:  return "spir";
90   }
91 }
92
93 const char *Triple::getVendorTypeName(VendorType Kind) {
94   switch (Kind) {
95   case UnknownVendor: return "unknown";
96
97   case Apple: return "apple";
98   case PC: return "pc";
99   case SCEI: return "scei";
100   case BGP: return "bgp";
101   case BGQ: return "bgq";
102   case Freescale: return "fsl";
103   case IBM: return "ibm";
104   case NVIDIA: return "nvidia";
105   }
106
107   llvm_unreachable("Invalid VendorType!");
108 }
109
110 const char *Triple::getOSTypeName(OSType Kind) {
111   switch (Kind) {
112   case UnknownOS: return "unknown";
113
114   case AuroraUX: return "auroraux";
115   case Cygwin: return "cygwin";
116   case Darwin: return "darwin";
117   case DragonFly: return "dragonfly";
118   case FreeBSD: return "freebsd";
119   case IOS: return "ios";
120   case KFreeBSD: return "kfreebsd";
121   case Linux: return "linux";
122   case Lv2: return "lv2";
123   case MacOSX: return "macosx";
124   case MinGW32: return "mingw32";
125   case NetBSD: return "netbsd";
126   case OpenBSD: return "openbsd";
127   case Solaris: return "solaris";
128   case Win32: return "win32";
129   case Haiku: return "haiku";
130   case Minix: return "minix";
131   case RTEMS: return "rtems";
132   case NaCl: return "nacl";
133   case CNK: return "cnk";
134   case Bitrig: return "bitrig";
135   case AIX: return "aix";
136   case CUDA: return "cuda";
137   case NVCL: return "nvcl";
138   }
139
140   llvm_unreachable("Invalid OSType");
141 }
142
143 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
144   switch (Kind) {
145   case UnknownEnvironment: return "unknown";
146   case GNU: return "gnu";
147   case GNUEABIHF: return "gnueabihf";
148   case GNUEABI: return "gnueabi";
149   case GNUX32: return "gnux32";
150   case EABI: return "eabi";
151   case MachO: return "macho";
152   case Android: return "android";
153   case ELF: return "elf";
154   }
155
156   llvm_unreachable("Invalid EnvironmentType!");
157 }
158
159 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
160   return StringSwitch<Triple::ArchType>(Name)
161     .Case("aarch64", aarch64)
162     .Case("arm", arm)
163     .Case("mips", mips)
164     .Case("mipsel", mipsel)
165     .Case("mips64", mips64)
166     .Case("mips64el", mips64el)
167     .Case("msp430", msp430)
168     .Case("ppc64", ppc64)
169     .Case("ppc32", ppc)
170     .Case("ppc", ppc)
171     .Case("r600", r600)
172     .Case("hexagon", hexagon)
173     .Case("sparc", sparc)
174     .Case("sparcv9", sparcv9)
175     .Case("systemz", systemz)
176     .Case("tce", tce)
177     .Case("thumb", thumb)
178     .Case("x86", x86)
179     .Case("x86-64", x86_64)
180     .Case("xcore", xcore)
181     .Case("nvptx", nvptx)
182     .Case("nvptx64", nvptx64)
183     .Case("le32", le32)
184     .Case("amdil", amdil)
185     .Case("spir", spir)
186     .Case("spir64", spir64)
187     .Default(UnknownArch);
188 }
189
190 // Returns architecture name that is understood by the target assembler.
191 const char *Triple::getArchNameForAssembler() {
192   if (!isOSDarwin() && getVendor() != Triple::Apple)
193     return NULL;
194
195   return StringSwitch<const char*>(getArchName())
196     .Case("i386", "i386")
197     .Case("x86_64", "x86_64")
198     .Case("powerpc", "ppc")
199     .Case("powerpc64", "ppc64")
200     .Case("arm", "arm")
201     .Cases("armv4t", "thumbv4t", "armv4t")
202     .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
203     .Cases("armv6", "thumbv6", "armv6")
204     .Cases("armv7", "thumbv7", "armv7")
205     .Case("r600", "r600")
206     .Case("nvptx", "nvptx")
207     .Case("nvptx64", "nvptx64")
208     .Case("le32", "le32")
209     .Case("amdil", "amdil")
210     .Case("spir", "spir")
211     .Case("spir64", "spir64")
212     .Default(NULL);
213 }
214
215 static Triple::ArchType parseArch(StringRef ArchName) {
216   return StringSwitch<Triple::ArchType>(ArchName)
217     .Cases("i386", "i486", "i586", "i686", Triple::x86)
218     // FIXME: Do we need to support these?
219     .Cases("i786", "i886", "i986", Triple::x86)
220     .Cases("amd64", "x86_64", Triple::x86_64)
221     .Case("powerpc", Triple::ppc)
222     .Cases("powerpc64", "ppu", Triple::ppc64)
223     .Case("aarch64", Triple::aarch64)
224     .Cases("arm", "xscale", Triple::arm)
225     // FIXME: It would be good to replace these with explicit names for all the
226     // various suffixes supported.
227     .StartsWith("armv", Triple::arm)
228     .Case("thumb", Triple::thumb)
229     .StartsWith("thumbv", Triple::thumb)
230     .Case("msp430", Triple::msp430)
231     .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
232     .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
233     .Cases("mips64", "mips64eb", Triple::mips64)
234     .Case("mips64el", Triple::mips64el)
235     .Case("r600", Triple::r600)
236     .Case("hexagon", Triple::hexagon)
237     .Case("s390x", Triple::systemz)
238     .Case("sparc", Triple::sparc)
239     .Cases("sparcv9", "sparc64", Triple::sparcv9)
240     .Case("tce", Triple::tce)
241     .Case("xcore", Triple::xcore)
242     .Case("nvptx", Triple::nvptx)
243     .Case("nvptx64", Triple::nvptx64)
244     .Case("le32", Triple::le32)
245     .Case("amdil", Triple::amdil)
246     .Case("spir", Triple::spir)
247     .Case("spir64", Triple::spir64)
248     .Default(Triple::UnknownArch);
249 }
250
251 static Triple::VendorType parseVendor(StringRef VendorName) {
252   return StringSwitch<Triple::VendorType>(VendorName)
253     .Case("apple", Triple::Apple)
254     .Case("pc", Triple::PC)
255     .Case("scei", Triple::SCEI)
256     .Case("bgp", Triple::BGP)
257     .Case("bgq", Triple::BGQ)
258     .Case("fsl", Triple::Freescale)
259     .Case("ibm", Triple::IBM)
260     .Case("nvidia", Triple::NVIDIA)
261     .Default(Triple::UnknownVendor);
262 }
263
264 static Triple::OSType parseOS(StringRef OSName) {
265   return StringSwitch<Triple::OSType>(OSName)
266     .StartsWith("auroraux", Triple::AuroraUX)
267     .StartsWith("cygwin", Triple::Cygwin)
268     .StartsWith("darwin", Triple::Darwin)
269     .StartsWith("dragonfly", Triple::DragonFly)
270     .StartsWith("freebsd", Triple::FreeBSD)
271     .StartsWith("ios", Triple::IOS)
272     .StartsWith("kfreebsd", Triple::KFreeBSD)
273     .StartsWith("linux", Triple::Linux)
274     .StartsWith("lv2", Triple::Lv2)
275     .StartsWith("macosx", Triple::MacOSX)
276     .StartsWith("mingw32", Triple::MinGW32)
277     .StartsWith("netbsd", Triple::NetBSD)
278     .StartsWith("openbsd", Triple::OpenBSD)
279     .StartsWith("solaris", Triple::Solaris)
280     .StartsWith("win32", Triple::Win32)
281     .StartsWith("haiku", Triple::Haiku)
282     .StartsWith("minix", Triple::Minix)
283     .StartsWith("rtems", Triple::RTEMS)
284     .StartsWith("nacl", Triple::NaCl)
285     .StartsWith("cnk", Triple::CNK)
286     .StartsWith("bitrig", Triple::Bitrig)
287     .StartsWith("aix", Triple::AIX)
288     .StartsWith("cuda", Triple::CUDA)
289     .StartsWith("nvcl", Triple::NVCL)
290     .Default(Triple::UnknownOS);
291 }
292
293 static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
294   return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
295     .StartsWith("eabi", Triple::EABI)
296     .StartsWith("gnueabihf", Triple::GNUEABIHF)
297     .StartsWith("gnueabi", Triple::GNUEABI)
298     .StartsWith("gnux32", Triple::GNUX32)
299     .StartsWith("gnu", Triple::GNU)
300     .StartsWith("macho", Triple::MachO)
301     .StartsWith("android", Triple::Android)
302     .StartsWith("elf", Triple::ELF)
303     .Default(Triple::UnknownEnvironment);
304 }
305
306 /// \brief Construct a triple from the string representation provided.
307 ///
308 /// This stores the string representation and parses the various pieces into
309 /// enum members.
310 Triple::Triple(const Twine &Str)
311     : Data(Str.str()),
312       Arch(parseArch(getArchName())),
313       Vendor(parseVendor(getVendorName())),
314       OS(parseOS(getOSName())),
315       Environment(parseEnvironment(getEnvironmentName())) {
316 }
317
318 /// \brief Construct a triple from string representations of the architecture,
319 /// vendor, and OS.
320 ///
321 /// This joins each argument into a canonical string representation and parses
322 /// them into enum members. It leaves the environment unknown and omits it from
323 /// the string representation.
324 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
325     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
326       Arch(parseArch(ArchStr.str())),
327       Vendor(parseVendor(VendorStr.str())),
328       OS(parseOS(OSStr.str())),
329       Environment() {
330 }
331
332 /// \brief Construct a triple from string representations of the architecture,
333 /// vendor, OS, and environment.
334 ///
335 /// This joins each argument into a canonical string representation and parses
336 /// them into enum members.
337 Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
338                const Twine &EnvironmentStr)
339     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
340             EnvironmentStr).str()),
341       Arch(parseArch(ArchStr.str())),
342       Vendor(parseVendor(VendorStr.str())),
343       OS(parseOS(OSStr.str())),
344       Environment(parseEnvironment(EnvironmentStr.str())) {
345 }
346
347 std::string Triple::normalize(StringRef Str) {
348   // Parse into components.
349   SmallVector<StringRef, 4> Components;
350   Str.split(Components, "-");
351
352   // If the first component corresponds to a known architecture, preferentially
353   // use it for the architecture.  If the second component corresponds to a
354   // known vendor, preferentially use it for the vendor, etc.  This avoids silly
355   // component movement when a component parses as (eg) both a valid arch and a
356   // valid os.
357   ArchType Arch = UnknownArch;
358   if (Components.size() > 0)
359     Arch = parseArch(Components[0]);
360   VendorType Vendor = UnknownVendor;
361   if (Components.size() > 1)
362     Vendor = parseVendor(Components[1]);
363   OSType OS = UnknownOS;
364   if (Components.size() > 2)
365     OS = parseOS(Components[2]);
366   EnvironmentType Environment = UnknownEnvironment;
367   if (Components.size() > 3)
368     Environment = parseEnvironment(Components[3]);
369
370   // Note which components are already in their final position.  These will not
371   // be moved.
372   bool Found[4];
373   Found[0] = Arch != UnknownArch;
374   Found[1] = Vendor != UnknownVendor;
375   Found[2] = OS != UnknownOS;
376   Found[3] = Environment != UnknownEnvironment;
377
378   // If they are not there already, permute the components into their canonical
379   // positions by seeing if they parse as a valid architecture, and if so moving
380   // the component to the architecture position etc.
381   for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
382     if (Found[Pos])
383       continue; // Already in the canonical position.
384
385     for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
386       // Do not reparse any components that already matched.
387       if (Idx < array_lengthof(Found) && Found[Idx])
388         continue;
389
390       // Does this component parse as valid for the target position?
391       bool Valid = false;
392       StringRef Comp = Components[Idx];
393       switch (Pos) {
394       default: llvm_unreachable("unexpected component type!");
395       case 0:
396         Arch = parseArch(Comp);
397         Valid = Arch != UnknownArch;
398         break;
399       case 1:
400         Vendor = parseVendor(Comp);
401         Valid = Vendor != UnknownVendor;
402         break;
403       case 2:
404         OS = parseOS(Comp);
405         Valid = OS != UnknownOS;
406         break;
407       case 3:
408         Environment = parseEnvironment(Comp);
409         Valid = Environment != UnknownEnvironment;
410         break;
411       }
412       if (!Valid)
413         continue; // Nope, try the next component.
414
415       // Move the component to the target position, pushing any non-fixed
416       // components that are in the way to the right.  This tends to give
417       // good results in the common cases of a forgotten vendor component
418       // or a wrongly positioned environment.
419       if (Pos < Idx) {
420         // Insert left, pushing the existing components to the right.  For
421         // example, a-b-i386 -> i386-a-b when moving i386 to the front.
422         StringRef CurrentComponent(""); // The empty component.
423         // Replace the component we are moving with an empty component.
424         std::swap(CurrentComponent, Components[Idx]);
425         // Insert the component being moved at Pos, displacing any existing
426         // components to the right.
427         for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
428           // Skip over any fixed components.
429           while (i < array_lengthof(Found) && Found[i])
430             ++i;
431           // Place the component at the new position, getting the component
432           // that was at this position - it will be moved right.
433           std::swap(CurrentComponent, Components[i]);
434         }
435       } else if (Pos > Idx) {
436         // Push right by inserting empty components until the component at Idx
437         // reaches the target position Pos.  For example, pc-a -> -pc-a when
438         // moving pc to the second position.
439         do {
440           // Insert one empty component at Idx.
441           StringRef CurrentComponent(""); // The empty component.
442           for (unsigned i = Idx; i < Components.size();) {
443             // Place the component at the new position, getting the component
444             // that was at this position - it will be moved right.
445             std::swap(CurrentComponent, Components[i]);
446             // If it was placed on top of an empty component then we are done.
447             if (CurrentComponent.empty())
448               break;
449             // Advance to the next component, skipping any fixed components.
450             while (++i < array_lengthof(Found) && Found[i])
451               ;
452           }
453           // The last component was pushed off the end - append it.
454           if (!CurrentComponent.empty())
455             Components.push_back(CurrentComponent);
456
457           // Advance Idx to the component's new position.
458           while (++Idx < array_lengthof(Found) && Found[Idx])
459             ;
460         } while (Idx < Pos); // Add more until the final position is reached.
461       }
462       assert(Pos < Components.size() && Components[Pos] == Comp &&
463              "Component moved wrong!");
464       Found[Pos] = true;
465       break;
466     }
467   }
468
469   // Special case logic goes here.  At this point Arch, Vendor and OS have the
470   // correct values for the computed components.
471
472   // Stick the corrected components back together to form the normalized string.
473   std::string Normalized;
474   for (unsigned i = 0, e = Components.size(); i != e; ++i) {
475     if (i) Normalized += '-';
476     Normalized += Components[i];
477   }
478   return Normalized;
479 }
480
481 StringRef Triple::getArchName() const {
482   return StringRef(Data).split('-').first;           // Isolate first component
483 }
484
485 StringRef Triple::getVendorName() const {
486   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
487   return Tmp.split('-').first;                       // Isolate second component
488 }
489
490 StringRef Triple::getOSName() const {
491   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
492   Tmp = Tmp.split('-').second;                       // Strip second component
493   return Tmp.split('-').first;                       // Isolate third component
494 }
495
496 StringRef Triple::getEnvironmentName() const {
497   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
498   Tmp = Tmp.split('-').second;                       // Strip second component
499   return Tmp.split('-').second;                      // Strip third component
500 }
501
502 StringRef Triple::getOSAndEnvironmentName() const {
503   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
504   return Tmp.split('-').second;                      // Strip second component
505 }
506
507 static unsigned EatNumber(StringRef &Str) {
508   assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
509   unsigned Result = 0;
510
511   do {
512     // Consume the leading digit.
513     Result = Result*10 + (Str[0] - '0');
514
515     // Eat the digit.
516     Str = Str.substr(1);
517   } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
518
519   return Result;
520 }
521
522 void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
523                           unsigned &Micro) const {
524   StringRef OSName = getOSName();
525
526   // Assume that the OS portion of the triple starts with the canonical name.
527   StringRef OSTypeName = getOSTypeName(getOS());
528   if (OSName.startswith(OSTypeName))
529     OSName = OSName.substr(OSTypeName.size());
530
531   // Any unset version defaults to 0.
532   Major = Minor = Micro = 0;
533
534   // Parse up to three components.
535   unsigned *Components[3] = { &Major, &Minor, &Micro };
536   for (unsigned i = 0; i != 3; ++i) {
537     if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
538       break;
539
540     // Consume the leading number.
541     *Components[i] = EatNumber(OSName);
542
543     // Consume the separator, if present.
544     if (OSName.startswith("."))
545       OSName = OSName.substr(1);
546   }
547 }
548
549 bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
550                               unsigned &Micro) const {
551   getOSVersion(Major, Minor, Micro);
552
553   switch (getOS()) {
554   default: llvm_unreachable("unexpected OS for Darwin triple");
555   case Darwin:
556     // Default to darwin8, i.e., MacOSX 10.4.
557     if (Major == 0)
558       Major = 8;
559     // Darwin version numbers are skewed from OS X versions.
560     if (Major < 4)
561       return false;
562     Micro = 0;
563     Minor = Major - 4;
564     Major = 10;
565     break;
566   case MacOSX:
567     // Default to 10.4.
568     if (Major == 0) {
569       Major = 10;
570       Minor = 4;
571     }
572     if (Major != 10)
573       return false;
574     break;
575   case IOS:
576     // Ignore the version from the triple.  This is only handled because the
577     // the clang driver combines OS X and IOS support into a common Darwin
578     // toolchain that wants to know the OS X version number even when targeting
579     // IOS.
580     Major = 10;
581     Minor = 4;
582     Micro = 0;
583     break;
584   }
585   return true;
586 }
587
588 void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
589                            unsigned &Micro) const {
590   switch (getOS()) {
591   default: llvm_unreachable("unexpected OS for Darwin triple");
592   case Darwin:
593   case MacOSX:
594     // Ignore the version from the triple.  This is only handled because the
595     // the clang driver combines OS X and IOS support into a common Darwin
596     // toolchain that wants to know the iOS version number even when targeting
597     // OS X.
598     Major = 3;
599     Minor = 0;
600     Micro = 0;
601     break;
602   case IOS:
603     getOSVersion(Major, Minor, Micro);
604     // Default to 3.0.
605     if (Major == 0)
606       Major = 3;
607     break;
608   }
609 }
610
611 void Triple::setTriple(const Twine &Str) {
612   *this = Triple(Str);
613 }
614
615 void Triple::setArch(ArchType Kind) {
616   setArchName(getArchTypeName(Kind));
617 }
618
619 void Triple::setVendor(VendorType Kind) {
620   setVendorName(getVendorTypeName(Kind));
621 }
622
623 void Triple::setOS(OSType Kind) {
624   setOSName(getOSTypeName(Kind));
625 }
626
627 void Triple::setEnvironment(EnvironmentType Kind) {
628   setEnvironmentName(getEnvironmentTypeName(Kind));
629 }
630
631 void Triple::setArchName(StringRef Str) {
632   // Work around a miscompilation bug for Twines in gcc 4.0.3.
633   SmallString<64> Triple;
634   Triple += Str;
635   Triple += "-";
636   Triple += getVendorName();
637   Triple += "-";
638   Triple += getOSAndEnvironmentName();
639   setTriple(Triple.str());
640 }
641
642 void Triple::setVendorName(StringRef Str) {
643   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
644 }
645
646 void Triple::setOSName(StringRef Str) {
647   if (hasEnvironment())
648     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
649               "-" + getEnvironmentName());
650   else
651     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
652 }
653
654 void Triple::setEnvironmentName(StringRef Str) {
655   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
656             "-" + Str);
657 }
658
659 void Triple::setOSAndEnvironmentName(StringRef Str) {
660   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
661 }
662
663 static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
664   switch (Arch) {
665   case llvm::Triple::UnknownArch:
666     return 0;
667
668   case llvm::Triple::msp430:
669     return 16;
670
671   case llvm::Triple::amdil:
672   case llvm::Triple::arm:
673   case llvm::Triple::hexagon:
674   case llvm::Triple::le32:
675   case llvm::Triple::mips:
676   case llvm::Triple::mipsel:
677   case llvm::Triple::nvptx:
678   case llvm::Triple::ppc:
679   case llvm::Triple::r600:
680   case llvm::Triple::sparc:
681   case llvm::Triple::tce:
682   case llvm::Triple::thumb:
683   case llvm::Triple::x86:
684   case llvm::Triple::xcore:
685   case llvm::Triple::spir:
686     return 32;
687
688   case llvm::Triple::aarch64:
689   case llvm::Triple::mips64:
690   case llvm::Triple::mips64el:
691   case llvm::Triple::nvptx64:
692   case llvm::Triple::ppc64:
693   case llvm::Triple::sparcv9:
694   case llvm::Triple::systemz:
695   case llvm::Triple::x86_64:
696   case llvm::Triple::spir64:
697     return 64;
698   }
699   llvm_unreachable("Invalid architecture value");
700 }
701
702 bool Triple::isArch64Bit() const {
703   return getArchPointerBitWidth(getArch()) == 64;
704 }
705
706 bool Triple::isArch32Bit() const {
707   return getArchPointerBitWidth(getArch()) == 32;
708 }
709
710 bool Triple::isArch16Bit() const {
711   return getArchPointerBitWidth(getArch()) == 16;
712 }
713
714 Triple Triple::get32BitArchVariant() const {
715   Triple T(*this);
716   switch (getArch()) {
717   case Triple::UnknownArch:
718   case Triple::aarch64:
719   case Triple::msp430:
720   case Triple::systemz:
721     T.setArch(UnknownArch);
722     break;
723
724   case Triple::amdil:
725   case Triple::spir:
726   case Triple::arm:
727   case Triple::hexagon:
728   case Triple::le32:
729   case Triple::mips:
730   case Triple::mipsel:
731   case Triple::nvptx:
732   case Triple::ppc:
733   case Triple::r600:
734   case Triple::sparc:
735   case Triple::tce:
736   case Triple::thumb:
737   case Triple::x86:
738   case Triple::xcore:
739     // Already 32-bit.
740     break;
741
742   case Triple::mips64:    T.setArch(Triple::mips);    break;
743   case Triple::mips64el:  T.setArch(Triple::mipsel);  break;
744   case Triple::nvptx64:   T.setArch(Triple::nvptx);   break;
745   case Triple::ppc64:     T.setArch(Triple::ppc);   break;
746   case Triple::sparcv9:   T.setArch(Triple::sparc);   break;
747   case Triple::x86_64:    T.setArch(Triple::x86);     break;
748   case Triple::spir64:    T.setArch(Triple::spir);    break;
749   }
750   return T;
751 }
752
753 Triple Triple::get64BitArchVariant() const {
754   Triple T(*this);
755   switch (getArch()) {
756   case Triple::UnknownArch:
757   case Triple::amdil:
758   case Triple::arm:
759   case Triple::hexagon:
760   case Triple::le32:
761   case Triple::msp430:
762   case Triple::r600:
763   case Triple::tce:
764   case Triple::thumb:
765   case Triple::xcore:
766     T.setArch(UnknownArch);
767     break;
768
769   case Triple::aarch64:
770   case Triple::spir64:
771   case Triple::mips64:
772   case Triple::mips64el:
773   case Triple::nvptx64:
774   case Triple::ppc64:
775   case Triple::sparcv9:
776   case Triple::systemz:
777   case Triple::x86_64:
778     // Already 64-bit.
779     break;
780
781   case Triple::mips:    T.setArch(Triple::mips64);    break;
782   case Triple::mipsel:  T.setArch(Triple::mips64el);  break;
783   case Triple::nvptx:   T.setArch(Triple::nvptx64);   break;
784   case Triple::ppc:     T.setArch(Triple::ppc64);     break;
785   case Triple::sparc:   T.setArch(Triple::sparcv9);   break;
786   case Triple::x86:     T.setArch(Triple::x86_64);    break;
787   case Triple::spir:    T.setArch(Triple::spir64);    break;
788   }
789   return T;
790 }