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