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