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