1 //===--- PPC.cpp - Implement PPC target feature support -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements PPC TargetInfo objects.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "PPC.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/MacroBuilder.h"
16 #include "clang/Basic/TargetBuiltins.h"
17 
18 using namespace clang;
19 using namespace clang::targets;
20 
21 const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
22 #define BUILTIN(ID, TYPE, ATTRS)                                               \
23   {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
24 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER)                                    \
25   {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
26 #include "clang/Basic/BuiltinsPPC.def"
27 };
28 
29 /// handleTargetFeatures - Perform initialization based on the user
30 /// configured set of features.
31 bool PPCTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
32                                          DiagnosticsEngine &Diags) {
33   FloatABI = HardFloat;
34   for (const auto &Feature : Features) {
35     if (Feature == "+altivec") {
36       HasAltivec = true;
37     } else if (Feature == "+vsx") {
38       HasVSX = true;
39     } else if (Feature == "+bpermd") {
40       HasBPERMD = true;
41     } else if (Feature == "+extdiv") {
42       HasExtDiv = true;
43     } else if (Feature == "+power8-vector") {
44       HasP8Vector = true;
45     } else if (Feature == "+crypto") {
46       HasP8Crypto = true;
47     } else if (Feature == "+direct-move") {
48       HasDirectMove = true;
49     } else if (Feature == "+htm") {
50       HasHTM = true;
51     } else if (Feature == "+float128") {
52       HasFloat128 = true;
53     } else if (Feature == "+power9-vector") {
54       HasP9Vector = true;
55     } else if (Feature == "+power10-vector") {
56       HasP10Vector = true;
57     } else if (Feature == "+pcrelative-memops") {
58       HasPCRelativeMemops = true;
59     } else if (Feature == "+prefix-instrs") {
60       HasPrefixInstrs = true;
61     } else if (Feature == "+spe" || Feature == "+efpu2") {
62       HasStrictFP = false;
63       HasSPE = true;
64       LongDoubleWidth = LongDoubleAlign = 64;
65       LongDoubleFormat = &llvm::APFloat::IEEEdouble();
66     } else if (Feature == "-hard-float") {
67       FloatABI = SoftFloat;
68     } else if (Feature == "+paired-vector-memops") {
69       PairedVectorMemops = true;
70     } else if (Feature == "+mma") {
71       HasMMA = true;
72     } else if (Feature == "+rop-protect") {
73       HasROPProtect = true;
74     } else if (Feature == "+privileged") {
75       HasPrivileged = true;
76     } else if (Feature == "+isa-v207-instructions") {
77       IsISA2_07 = true;
78     } else if (Feature == "+isa-v30-instructions") {
79       IsISA3_0 = true;
80     } else if (Feature == "+isa-v31-instructions") {
81       IsISA3_1 = true;
82     }
83     // TODO: Finish this list and add an assert that we've handled them
84     // all.
85   }
86 
87   return true;
88 }
89 
90 static void defineXLCompatMacros(MacroBuilder &Builder) {
91   Builder.defineMacro("__popcntb", "__builtin_ppc_popcntb");
92   Builder.defineMacro("__eieio", "__builtin_ppc_eieio");
93   Builder.defineMacro("__iospace_eieio", "__builtin_ppc_iospace_eieio");
94   Builder.defineMacro("__isync", "__builtin_ppc_isync");
95   Builder.defineMacro("__lwsync", "__builtin_ppc_lwsync");
96   Builder.defineMacro("__iospace_lwsync", "__builtin_ppc_iospace_lwsync");
97   Builder.defineMacro("__sync", "__builtin_ppc_sync");
98   Builder.defineMacro("__iospace_sync", "__builtin_ppc_iospace_sync");
99   Builder.defineMacro("__dcbfl", "__builtin_ppc_dcbfl");
100   Builder.defineMacro("__dcbflp", "__builtin_ppc_dcbflp");
101   Builder.defineMacro("__dcbst", "__builtin_ppc_dcbst");
102   Builder.defineMacro("__dcbt", "__builtin_ppc_dcbt");
103   Builder.defineMacro("__dcbtst", "__builtin_ppc_dcbtst");
104   Builder.defineMacro("__dcbz", "__builtin_ppc_dcbz");
105   Builder.defineMacro("__icbt", "__builtin_ppc_icbt");
106   Builder.defineMacro("__compare_and_swap", "__builtin_ppc_compare_and_swap");
107   Builder.defineMacro("__compare_and_swaplp",
108                       "__builtin_ppc_compare_and_swaplp");
109   Builder.defineMacro("__fetch_and_add", "__builtin_ppc_fetch_and_add");
110   Builder.defineMacro("__fetch_and_addlp", "__builtin_ppc_fetch_and_addlp");
111   Builder.defineMacro("__fetch_and_and", "__builtin_ppc_fetch_and_and");
112   Builder.defineMacro("__fetch_and_andlp", "__builtin_ppc_fetch_and_andlp");
113   Builder.defineMacro("__fetch_and_or", "__builtin_ppc_fetch_and_or");
114   Builder.defineMacro("__fetch_and_orlp", "__builtin_ppc_fetch_and_orlp");
115   Builder.defineMacro("__fetch_and_swap", "__builtin_ppc_fetch_and_swap");
116   Builder.defineMacro("__fetch_and_swaplp", "__builtin_ppc_fetch_and_swaplp");
117   Builder.defineMacro("__ldarx", "__builtin_ppc_ldarx");
118   Builder.defineMacro("__lwarx", "__builtin_ppc_lwarx");
119   Builder.defineMacro("__stdcx", "__builtin_ppc_stdcx");
120   Builder.defineMacro("__stwcx", "__builtin_ppc_stwcx");
121   Builder.defineMacro("__tdw", "__builtin_ppc_tdw");
122   Builder.defineMacro("__tw", "__builtin_ppc_tw");
123   Builder.defineMacro("__trap", "__builtin_ppc_trap");
124   Builder.defineMacro("__trapd", "__builtin_ppc_trapd");
125   Builder.defineMacro("__fcfid", "__builtin_ppc_fcfid");
126   Builder.defineMacro("__fcfud", "__builtin_ppc_fcfud");
127   Builder.defineMacro("__fctid", "__builtin_ppc_fctid");
128   Builder.defineMacro("__fctidz", "__builtin_ppc_fctidz");
129   Builder.defineMacro("__fctiw", "__builtin_ppc_fctiw");
130   Builder.defineMacro("__fctiwz", "__builtin_ppc_fctiwz");
131   Builder.defineMacro("__fctudz", "__builtin_ppc_fctudz");
132   Builder.defineMacro("__fctuwz", "__builtin_ppc_fctuwz");
133   Builder.defineMacro("__cmpeqb", "__builtin_ppc_cmpeqb");
134   Builder.defineMacro("__cmprb", "__builtin_ppc_cmprb");
135   Builder.defineMacro("__setb", "__builtin_ppc_setb");
136   Builder.defineMacro("__mulhd", "__builtin_ppc_mulhd");
137   Builder.defineMacro("__mulhdu", "__builtin_ppc_mulhdu");
138   Builder.defineMacro("__mulhw", "__builtin_ppc_mulhw");
139   Builder.defineMacro("__mulhwu", "__builtin_ppc_mulhwu");
140   Builder.defineMacro("__maddhd", "__builtin_ppc_maddhd");
141   Builder.defineMacro("__maddhdu", "__builtin_ppc_maddhdu");
142   Builder.defineMacro("__maddld", "__builtin_ppc_maddld");
143 }
144 
145 /// PPCTargetInfo::getTargetDefines - Return a set of the PowerPC-specific
146 /// #defines that are not tied to a specific subtarget.
147 void PPCTargetInfo::getTargetDefines(const LangOptions &Opts,
148                                      MacroBuilder &Builder) const {
149 
150   defineXLCompatMacros(Builder);
151 
152   // Target identification.
153   Builder.defineMacro("__ppc__");
154   Builder.defineMacro("__PPC__");
155   Builder.defineMacro("_ARCH_PPC");
156   Builder.defineMacro("__powerpc__");
157   Builder.defineMacro("__POWERPC__");
158   if (PointerWidth == 64) {
159     Builder.defineMacro("_ARCH_PPC64");
160     Builder.defineMacro("__powerpc64__");
161     Builder.defineMacro("__ppc64__");
162     Builder.defineMacro("__PPC64__");
163   }
164 
165   // Target properties.
166   if (getTriple().getArch() == llvm::Triple::ppc64le ||
167       getTriple().getArch() == llvm::Triple::ppcle) {
168     Builder.defineMacro("_LITTLE_ENDIAN");
169   } else {
170     if (!getTriple().isOSNetBSD() &&
171         !getTriple().isOSOpenBSD())
172       Builder.defineMacro("_BIG_ENDIAN");
173   }
174 
175   // ABI options.
176   if (ABI == "elfv1")
177     Builder.defineMacro("_CALL_ELF", "1");
178   if (ABI == "elfv2")
179     Builder.defineMacro("_CALL_ELF", "2");
180 
181   // This typically is only for a new enough linker (bfd >= 2.16.2 or gold), but
182   // our support post-dates this and it should work on all 64-bit ppc linux
183   // platforms. It is guaranteed to work on all elfv2 platforms.
184   if (getTriple().getOS() == llvm::Triple::Linux && PointerWidth == 64)
185     Builder.defineMacro("_CALL_LINUX", "1");
186 
187   // Subtarget options.
188   if (!getTriple().isOSAIX()){
189     Builder.defineMacro("__NATURAL_ALIGNMENT__");
190   }
191   Builder.defineMacro("__REGISTER_PREFIX__", "");
192 
193   // FIXME: Should be controlled by command line option.
194   if (LongDoubleWidth == 128) {
195     Builder.defineMacro("__LONG_DOUBLE_128__");
196     Builder.defineMacro("__LONGDOUBLE128");
197     if (Opts.PPCIEEELongDouble)
198       Builder.defineMacro("__LONG_DOUBLE_IEEE128__");
199     else
200       Builder.defineMacro("__LONG_DOUBLE_IBM128__");
201   }
202 
203   // Define this for elfv2 (64-bit only) or 64-bit darwin.
204   if (ABI == "elfv2" ||
205       (getTriple().getOS() == llvm::Triple::Darwin && PointerWidth == 64))
206     Builder.defineMacro("__STRUCT_PARM_ALIGN__", "16");
207 
208   if (ArchDefs & ArchDefineName)
209     Builder.defineMacro(Twine("_ARCH_", StringRef(CPU).upper()));
210   if (ArchDefs & ArchDefinePpcgr)
211     Builder.defineMacro("_ARCH_PPCGR");
212   if (ArchDefs & ArchDefinePpcsq)
213     Builder.defineMacro("_ARCH_PPCSQ");
214   if (ArchDefs & ArchDefine440)
215     Builder.defineMacro("_ARCH_440");
216   if (ArchDefs & ArchDefine603)
217     Builder.defineMacro("_ARCH_603");
218   if (ArchDefs & ArchDefine604)
219     Builder.defineMacro("_ARCH_604");
220   if (ArchDefs & ArchDefinePwr4)
221     Builder.defineMacro("_ARCH_PWR4");
222   if (ArchDefs & ArchDefinePwr5)
223     Builder.defineMacro("_ARCH_PWR5");
224   if (ArchDefs & ArchDefinePwr5x)
225     Builder.defineMacro("_ARCH_PWR5X");
226   if (ArchDefs & ArchDefinePwr6)
227     Builder.defineMacro("_ARCH_PWR6");
228   if (ArchDefs & ArchDefinePwr6x)
229     Builder.defineMacro("_ARCH_PWR6X");
230   if (ArchDefs & ArchDefinePwr7)
231     Builder.defineMacro("_ARCH_PWR7");
232   if (ArchDefs & ArchDefinePwr8)
233     Builder.defineMacro("_ARCH_PWR8");
234   if (ArchDefs & ArchDefinePwr9)
235     Builder.defineMacro("_ARCH_PWR9");
236   if (ArchDefs & ArchDefinePwr10)
237     Builder.defineMacro("_ARCH_PWR10");
238   if (ArchDefs & ArchDefineA2)
239     Builder.defineMacro("_ARCH_A2");
240   if (ArchDefs & ArchDefineE500)
241     Builder.defineMacro("__NO_LWSYNC__");
242   if (ArchDefs & ArchDefineFuture)
243     Builder.defineMacro("_ARCH_PWR_FUTURE");
244 
245   if (HasAltivec) {
246     Builder.defineMacro("__VEC__", "10206");
247     Builder.defineMacro("__ALTIVEC__");
248   }
249   if (HasSPE) {
250     Builder.defineMacro("__SPE__");
251     Builder.defineMacro("__NO_FPRS__");
252   }
253   if (HasVSX)
254     Builder.defineMacro("__VSX__");
255   if (HasP8Vector)
256     Builder.defineMacro("__POWER8_VECTOR__");
257   if (HasP8Crypto)
258     Builder.defineMacro("__CRYPTO__");
259   if (HasHTM)
260     Builder.defineMacro("__HTM__");
261   if (HasFloat128)
262     Builder.defineMacro("__FLOAT128__");
263   if (HasP9Vector)
264     Builder.defineMacro("__POWER9_VECTOR__");
265   if (HasMMA)
266     Builder.defineMacro("__MMA__");
267   if (HasROPProtect)
268     Builder.defineMacro("__ROP_PROTECT__");
269   if (HasPrivileged)
270     Builder.defineMacro("__PRIVILEGED__");
271   if (HasP10Vector)
272     Builder.defineMacro("__POWER10_VECTOR__");
273   if (HasPCRelativeMemops)
274     Builder.defineMacro("__PCREL__");
275 
276   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
277   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
278   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
279   if (PointerWidth == 64)
280     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
281 
282   // We have support for the bswap intrinsics so we can define this.
283   Builder.defineMacro("__HAVE_BSWAP__", "1");
284 
285   // FIXME: The following are not yet generated here by Clang, but are
286   //        generated by GCC:
287   //
288   //   _SOFT_FLOAT_
289   //   __RECIP_PRECISION__
290   //   __APPLE_ALTIVEC__
291   //   __RECIP__
292   //   __RECIPF__
293   //   __RSQRTE__
294   //   __RSQRTEF__
295   //   _SOFT_DOUBLE_
296   //   __NO_LWSYNC__
297   //   __CMODEL_MEDIUM__
298   //   __CMODEL_LARGE__
299   //   _CALL_SYSV
300   //   _CALL_DARWIN
301 }
302 
303 // Handle explicit options being passed to the compiler here: if we've
304 // explicitly turned off vsx and turned on any of:
305 // - power8-vector
306 // - direct-move
307 // - float128
308 // - power9-vector
309 // - paired-vector-memops
310 // - mma
311 // - power10-vector
312 // then go ahead and error since the customer has expressed an incompatible
313 // set of options.
314 static bool ppcUserFeaturesCheck(DiagnosticsEngine &Diags,
315                                  const std::vector<std::string> &FeaturesVec) {
316 
317   // vsx was not explicitly turned off.
318   if (llvm::find(FeaturesVec, "-vsx") == FeaturesVec.end())
319     return true;
320 
321   auto FindVSXSubfeature = [&](StringRef Feature, StringRef Option) {
322     if (llvm::find(FeaturesVec, Feature) != FeaturesVec.end()) {
323       Diags.Report(diag::err_opt_not_valid_with_opt) << Option << "-mno-vsx";
324       return true;
325     }
326     return false;
327   };
328 
329   bool Found = FindVSXSubfeature("+power8-vector", "-mpower8-vector");
330   Found |= FindVSXSubfeature("+direct-move", "-mdirect-move");
331   Found |= FindVSXSubfeature("+float128", "-mfloat128");
332   Found |= FindVSXSubfeature("+power9-vector", "-mpower9-vector");
333   Found |= FindVSXSubfeature("+paired-vector-memops", "-mpaired-vector-memops");
334   Found |= FindVSXSubfeature("+mma", "-mmma");
335   Found |= FindVSXSubfeature("+power10-vector", "-mpower10-vector");
336 
337   // Return false if any vsx subfeatures was found.
338   return !Found;
339 }
340 
341 bool PPCTargetInfo::initFeatureMap(
342     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
343     const std::vector<std::string> &FeaturesVec) const {
344   Features["altivec"] = llvm::StringSwitch<bool>(CPU)
345                             .Case("7400", true)
346                             .Case("g4", true)
347                             .Case("7450", true)
348                             .Case("g4+", true)
349                             .Case("970", true)
350                             .Case("g5", true)
351                             .Case("pwr6", true)
352                             .Case("pwr7", true)
353                             .Case("pwr8", true)
354                             .Case("pwr9", true)
355                             .Case("ppc64", true)
356                             .Case("ppc64le", true)
357                             .Default(false);
358 
359   Features["power9-vector"] = (CPU == "pwr9");
360   Features["crypto"] = llvm::StringSwitch<bool>(CPU)
361                            .Case("ppc64le", true)
362                            .Case("pwr9", true)
363                            .Case("pwr8", true)
364                            .Default(false);
365   Features["power8-vector"] = llvm::StringSwitch<bool>(CPU)
366                                   .Case("ppc64le", true)
367                                   .Case("pwr9", true)
368                                   .Case("pwr8", true)
369                                   .Default(false);
370   Features["bpermd"] = llvm::StringSwitch<bool>(CPU)
371                            .Case("ppc64le", true)
372                            .Case("pwr9", true)
373                            .Case("pwr8", true)
374                            .Case("pwr7", true)
375                            .Default(false);
376   Features["extdiv"] = llvm::StringSwitch<bool>(CPU)
377                            .Case("ppc64le", true)
378                            .Case("pwr9", true)
379                            .Case("pwr8", true)
380                            .Case("pwr7", true)
381                            .Default(false);
382   Features["direct-move"] = llvm::StringSwitch<bool>(CPU)
383                                 .Case("ppc64le", true)
384                                 .Case("pwr9", true)
385                                 .Case("pwr8", true)
386                                 .Default(false);
387   Features["vsx"] = llvm::StringSwitch<bool>(CPU)
388                         .Case("ppc64le", true)
389                         .Case("pwr9", true)
390                         .Case("pwr8", true)
391                         .Case("pwr7", true)
392                         .Default(false);
393   Features["htm"] = llvm::StringSwitch<bool>(CPU)
394                         .Case("ppc64le", true)
395                         .Case("pwr9", true)
396                         .Case("pwr8", true)
397                         .Default(false);
398 
399   // ROP Protect is off by default.
400   Features["rop-protect"] = false;
401   // Privileged instructions are off by default.
402   Features["privileged"] = false;
403 
404   Features["spe"] = llvm::StringSwitch<bool>(CPU)
405                         .Case("8548", true)
406                         .Case("e500", true)
407                         .Default(false);
408 
409   Features["isa-v207-instructions"] = llvm::StringSwitch<bool>(CPU)
410                                           .Case("ppc64le", true)
411                                           .Case("pwr9", true)
412                                           .Case("pwr8", true)
413                                           .Default(false);
414 
415   Features["isa-v30-instructions"] =
416       llvm::StringSwitch<bool>(CPU).Case("pwr9", true).Default(false);
417 
418   // Power10 includes all the same features as Power9 plus any features specific
419   // to the Power10 core.
420   if (CPU == "pwr10" || CPU == "power10") {
421     initFeatureMap(Features, Diags, "pwr9", FeaturesVec);
422     addP10SpecificFeatures(Features);
423   }
424 
425   // Future CPU should include all of the features of Power 10 as well as any
426   // additional features (yet to be determined) specific to it.
427   if (CPU == "future") {
428     initFeatureMap(Features, Diags, "pwr10", FeaturesVec);
429     addFutureSpecificFeatures(Features);
430   }
431 
432   if (!ppcUserFeaturesCheck(Diags, FeaturesVec))
433     return false;
434 
435   if (!(ArchDefs & ArchDefinePwr9) && (ArchDefs & ArchDefinePpcgr) &&
436       llvm::find(FeaturesVec, "+float128") != FeaturesVec.end()) {
437     // We have __float128 on PPC but not power 9 and above.
438     Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfloat128" << CPU;
439     return false;
440   }
441 
442   if (!(ArchDefs & ArchDefinePwr10) &&
443       llvm::find(FeaturesVec, "+mma") != FeaturesVec.end()) {
444     // We have MMA on PPC but not power 10 and above.
445     Diags.Report(diag::err_opt_not_valid_with_opt) << "-mmma" << CPU;
446     return false;
447   }
448 
449   if (!(ArchDefs & ArchDefinePwr8) &&
450       llvm::find(FeaturesVec, "+rop-protect") != FeaturesVec.end()) {
451     // We can turn on ROP Protect on Power 8 and above.
452     Diags.Report(diag::err_opt_not_valid_with_opt) << "-mrop-protect" << CPU;
453     return false;
454   }
455 
456   if (!(ArchDefs & ArchDefinePwr8) &&
457       llvm::find(FeaturesVec, "+privileged") != FeaturesVec.end()) {
458     Diags.Report(diag::err_opt_not_valid_with_opt) << "-mprivileged" << CPU;
459     return false;
460   }
461 
462   return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
463 }
464 
465 // Add any Power10 specific features.
466 void PPCTargetInfo::addP10SpecificFeatures(
467     llvm::StringMap<bool> &Features) const {
468   Features["htm"] = false; // HTM was removed for P10.
469   Features["paired-vector-memops"] = true;
470   Features["mma"] = true;
471   Features["power10-vector"] = true;
472   Features["pcrelative-memops"] = true;
473   Features["prefix-instrs"] = true;
474   Features["isa-v31-instructions"] = true;
475   return;
476 }
477 
478 // Add features specific to the "Future" CPU.
479 void PPCTargetInfo::addFutureSpecificFeatures(
480     llvm::StringMap<bool> &Features) const {
481   return;
482 }
483 
484 bool PPCTargetInfo::hasFeature(StringRef Feature) const {
485   return llvm::StringSwitch<bool>(Feature)
486       .Case("powerpc", true)
487       .Case("altivec", HasAltivec)
488       .Case("vsx", HasVSX)
489       .Case("power8-vector", HasP8Vector)
490       .Case("crypto", HasP8Crypto)
491       .Case("direct-move", HasDirectMove)
492       .Case("htm", HasHTM)
493       .Case("bpermd", HasBPERMD)
494       .Case("extdiv", HasExtDiv)
495       .Case("float128", HasFloat128)
496       .Case("power9-vector", HasP9Vector)
497       .Case("paired-vector-memops", PairedVectorMemops)
498       .Case("power10-vector", HasP10Vector)
499       .Case("pcrelative-memops", HasPCRelativeMemops)
500       .Case("prefix-instrs", HasPrefixInstrs)
501       .Case("spe", HasSPE)
502       .Case("mma", HasMMA)
503       .Case("rop-protect", HasROPProtect)
504       .Case("privileged", HasPrivileged)
505       .Case("isa-v207-instructions", IsISA2_07)
506       .Case("isa-v30-instructions", IsISA3_0)
507       .Case("isa-v31-instructions", IsISA3_1)
508       .Default(false);
509 }
510 
511 void PPCTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
512                                       StringRef Name, bool Enabled) const {
513   if (Enabled) {
514     if (Name == "efpu2")
515       Features["spe"] = true;
516     // If we're enabling any of the vsx based features then enable vsx and
517     // altivec. We'll diagnose any problems later.
518     bool FeatureHasVSX = llvm::StringSwitch<bool>(Name)
519                              .Case("vsx", true)
520                              .Case("direct-move", true)
521                              .Case("power8-vector", true)
522                              .Case("power9-vector", true)
523                              .Case("paired-vector-memops", true)
524                              .Case("power10-vector", true)
525                              .Case("float128", true)
526                              .Case("mma", true)
527                              .Default(false);
528     if (FeatureHasVSX)
529       Features["vsx"] = Features["altivec"] = true;
530     if (Name == "power9-vector")
531       Features["power8-vector"] = true;
532     else if (Name == "power10-vector")
533       Features["power8-vector"] = Features["power9-vector"] = true;
534     if (Name == "pcrel")
535       Features["pcrelative-memops"] = true;
536     else if (Name == "prefixed")
537       Features["prefix-instrs"] = true;
538     else
539       Features[Name] = true;
540   } else {
541     if (Name == "spe")
542       Features["efpu2"] = false;
543     // If we're disabling altivec or vsx go ahead and disable all of the vsx
544     // features.
545     if ((Name == "altivec") || (Name == "vsx"))
546       Features["vsx"] = Features["direct-move"] = Features["power8-vector"] =
547           Features["float128"] = Features["power9-vector"] =
548               Features["paired-vector-memops"] = Features["mma"] =
549                   Features["power10-vector"] = false;
550     if (Name == "power8-vector")
551       Features["power9-vector"] = Features["paired-vector-memops"] =
552           Features["mma"] = Features["power10-vector"] = false;
553     else if (Name == "power9-vector")
554       Features["paired-vector-memops"] = Features["mma"] =
555           Features["power10-vector"] = false;
556     if (Name == "pcrel")
557       Features["pcrelative-memops"] = false;
558     else if (Name == "prefixed")
559       Features["prefix-instrs"] = false;
560     else
561       Features[Name] = false;
562   }
563 }
564 
565 const char *const PPCTargetInfo::GCCRegNames[] = {
566     "r0",  "r1",     "r2",   "r3",      "r4",      "r5",  "r6",  "r7",  "r8",
567     "r9",  "r10",    "r11",  "r12",     "r13",     "r14", "r15", "r16", "r17",
568     "r18", "r19",    "r20",  "r21",     "r22",     "r23", "r24", "r25", "r26",
569     "r27", "r28",    "r29",  "r30",     "r31",     "f0",  "f1",  "f2",  "f3",
570     "f4",  "f5",     "f6",   "f7",      "f8",      "f9",  "f10", "f11", "f12",
571     "f13", "f14",    "f15",  "f16",     "f17",     "f18", "f19", "f20", "f21",
572     "f22", "f23",    "f24",  "f25",     "f26",     "f27", "f28", "f29", "f30",
573     "f31", "mq",     "lr",   "ctr",     "ap",      "cr0", "cr1", "cr2", "cr3",
574     "cr4", "cr5",    "cr6",  "cr7",     "xer",     "v0",  "v1",  "v2",  "v3",
575     "v4",  "v5",     "v6",   "v7",      "v8",      "v9",  "v10", "v11", "v12",
576     "v13", "v14",    "v15",  "v16",     "v17",     "v18", "v19", "v20", "v21",
577     "v22", "v23",    "v24",  "v25",     "v26",     "v27", "v28", "v29", "v30",
578     "v31", "vrsave", "vscr", "spe_acc", "spefscr", "sfp"
579 };
580 
581 ArrayRef<const char *> PPCTargetInfo::getGCCRegNames() const {
582   return llvm::makeArrayRef(GCCRegNames);
583 }
584 
585 const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
586     // While some of these aliases do map to different registers
587     // they still share the same register name.
588     {{"0"}, "r0"},     {{"1"}, "r1"},     {{"2"}, "r2"},     {{"3"}, "r3"},
589     {{"4"}, "r4"},     {{"5"}, "r5"},     {{"6"}, "r6"},     {{"7"}, "r7"},
590     {{"8"}, "r8"},     {{"9"}, "r9"},     {{"10"}, "r10"},   {{"11"}, "r11"},
591     {{"12"}, "r12"},   {{"13"}, "r13"},   {{"14"}, "r14"},   {{"15"}, "r15"},
592     {{"16"}, "r16"},   {{"17"}, "r17"},   {{"18"}, "r18"},   {{"19"}, "r19"},
593     {{"20"}, "r20"},   {{"21"}, "r21"},   {{"22"}, "r22"},   {{"23"}, "r23"},
594     {{"24"}, "r24"},   {{"25"}, "r25"},   {{"26"}, "r26"},   {{"27"}, "r27"},
595     {{"28"}, "r28"},   {{"29"}, "r29"},   {{"30"}, "r30"},   {{"31"}, "r31"},
596     {{"fr0"}, "f0"},   {{"fr1"}, "f1"},   {{"fr2"}, "f2"},   {{"fr3"}, "f3"},
597     {{"fr4"}, "f4"},   {{"fr5"}, "f5"},   {{"fr6"}, "f6"},   {{"fr7"}, "f7"},
598     {{"fr8"}, "f8"},   {{"fr9"}, "f9"},   {{"fr10"}, "f10"}, {{"fr11"}, "f11"},
599     {{"fr12"}, "f12"}, {{"fr13"}, "f13"}, {{"fr14"}, "f14"}, {{"fr15"}, "f15"},
600     {{"fr16"}, "f16"}, {{"fr17"}, "f17"}, {{"fr18"}, "f18"}, {{"fr19"}, "f19"},
601     {{"fr20"}, "f20"}, {{"fr21"}, "f21"}, {{"fr22"}, "f22"}, {{"fr23"}, "f23"},
602     {{"fr24"}, "f24"}, {{"fr25"}, "f25"}, {{"fr26"}, "f26"}, {{"fr27"}, "f27"},
603     {{"fr28"}, "f28"}, {{"fr29"}, "f29"}, {{"fr30"}, "f30"}, {{"fr31"}, "f31"},
604     {{"cc"}, "cr0"},
605 };
606 
607 ArrayRef<TargetInfo::GCCRegAlias> PPCTargetInfo::getGCCRegAliases() const {
608   return llvm::makeArrayRef(GCCRegAliases);
609 }
610 
611 // PPC ELFABIv2 DWARF Definitoin "Table 2.26. Mappings of Common Registers".
612 // vs0 ~ vs31 is mapping to 32 - 63,
613 // vs32 ~ vs63 is mapping to 77 - 108.
614 const TargetInfo::AddlRegName GCCAddlRegNames[] = {
615     // Table of additional register names to use in user input.
616     {{"vs0"}, 32},   {{"vs1"}, 33},   {{"vs2"}, 34},   {{"vs3"}, 35},
617     {{"vs4"}, 36},   {{"vs5"}, 37},   {{"vs6"}, 38},   {{"vs7"}, 39},
618     {{"vs8"}, 40},   {{"vs9"}, 41},   {{"vs10"}, 42},  {{"vs11"}, 43},
619     {{"vs12"}, 44},  {{"vs13"}, 45},  {{"vs14"}, 46},  {{"vs15"}, 47},
620     {{"vs16"}, 48},  {{"vs17"}, 49},  {{"vs18"}, 50},  {{"vs19"}, 51},
621     {{"vs20"}, 52},  {{"vs21"}, 53},  {{"vs22"}, 54},  {{"vs23"}, 55},
622     {{"vs24"}, 56},  {{"vs25"}, 57},  {{"vs26"}, 58},  {{"vs27"}, 59},
623     {{"vs28"}, 60},  {{"vs29"}, 61},  {{"vs30"}, 62},  {{"vs31"}, 63},
624     {{"vs32"}, 77},  {{"vs33"}, 78},  {{"vs34"}, 79},  {{"vs35"}, 80},
625     {{"vs36"}, 81},  {{"vs37"}, 82},  {{"vs38"}, 83},  {{"vs39"}, 84},
626     {{"vs40"}, 85},  {{"vs41"}, 86},  {{"vs42"}, 87},  {{"vs43"}, 88},
627     {{"vs44"}, 89},  {{"vs45"}, 90},  {{"vs46"}, 91},  {{"vs47"}, 92},
628     {{"vs48"}, 93},  {{"vs49"}, 94},  {{"vs50"}, 95},  {{"vs51"}, 96},
629     {{"vs52"}, 97},  {{"vs53"}, 98},  {{"vs54"}, 99},  {{"vs55"}, 100},
630     {{"vs56"}, 101}, {{"vs57"}, 102}, {{"vs58"}, 103}, {{"vs59"}, 104},
631     {{"vs60"}, 105}, {{"vs61"}, 106}, {{"vs62"}, 107}, {{"vs63"}, 108},
632 };
633 
634 ArrayRef<TargetInfo::AddlRegName> PPCTargetInfo::getGCCAddlRegNames() const {
635   if (ABI == "elfv2")
636     return llvm::makeArrayRef(GCCAddlRegNames);
637   else
638     return TargetInfo::getGCCAddlRegNames();
639 }
640 
641 static constexpr llvm::StringLiteral ValidCPUNames[] = {
642     {"generic"},     {"440"},     {"450"},    {"601"},       {"602"},
643     {"603"},         {"603e"},    {"603ev"},  {"604"},       {"604e"},
644     {"620"},         {"630"},     {"g3"},     {"7400"},      {"g4"},
645     {"7450"},        {"g4+"},     {"750"},    {"8548"},      {"970"},
646     {"g5"},          {"a2"},      {"e500"},   {"e500mc"},    {"e5500"},
647     {"power3"},      {"pwr3"},    {"power4"}, {"pwr4"},      {"power5"},
648     {"pwr5"},        {"power5x"}, {"pwr5x"},  {"power6"},    {"pwr6"},
649     {"power6x"},     {"pwr6x"},   {"power7"}, {"pwr7"},      {"power8"},
650     {"pwr8"},        {"power9"},  {"pwr9"},   {"power10"},   {"pwr10"},
651     {"powerpc"},     {"ppc"},     {"ppc32"},  {"powerpc64"}, {"ppc64"},
652     {"powerpc64le"}, {"ppc64le"}, {"future"}};
653 
654 bool PPCTargetInfo::isValidCPUName(StringRef Name) const {
655   return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
656 }
657 
658 void PPCTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
659   Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
660 }
661 
662 void PPCTargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
663   if (HasAltivec)
664     Opts.AltiVec = 1;
665   TargetInfo::adjust(Diags, Opts);
666   if (LongDoubleFormat != &llvm::APFloat::IEEEdouble())
667     LongDoubleFormat = Opts.PPCIEEELongDouble
668                            ? &llvm::APFloat::IEEEquad()
669                            : &llvm::APFloat::PPCDoubleDouble();
670   Opts.IEEE128 = 1;
671 }
672 
673 ArrayRef<Builtin::Info> PPCTargetInfo::getTargetBuiltins() const {
674   return llvm::makeArrayRef(BuiltinInfo, clang::PPC::LastTSBuiltin -
675                                              Builtin::FirstTSBuiltin);
676 }
677