clang  9.0.0svn
PPC.h
Go to the documentation of this file.
1 //===--- PPC.h - Declare PPC target feature support -------------*- C++ -*-===//
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 declares PPC TargetInfo objects.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H
14 #define LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H
15 
16 #include "OSTargets.h"
17 #include "clang/Basic/TargetInfo.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Support/Compiler.h"
22 
23 namespace clang {
24 namespace targets {
25 
26 // PPC abstract base class
27 class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {
28 
29  /// Flags for architecture specific defines.
30  typedef enum {
31  ArchDefineNone = 0,
32  ArchDefineName = 1 << 0, // <name> is substituted for arch name.
33  ArchDefinePpcgr = 1 << 1,
34  ArchDefinePpcsq = 1 << 2,
35  ArchDefine440 = 1 << 3,
36  ArchDefine603 = 1 << 4,
37  ArchDefine604 = 1 << 5,
38  ArchDefinePwr4 = 1 << 6,
39  ArchDefinePwr5 = 1 << 7,
40  ArchDefinePwr5x = 1 << 8,
41  ArchDefinePwr6 = 1 << 9,
42  ArchDefinePwr6x = 1 << 10,
43  ArchDefinePwr7 = 1 << 11,
44  ArchDefinePwr8 = 1 << 12,
45  ArchDefinePwr9 = 1 << 13,
46  ArchDefineA2 = 1 << 14,
47  ArchDefineA2q = 1 << 15
48  } ArchDefineTypes;
49 
50 
51  ArchDefineTypes ArchDefs = ArchDefineNone;
52  static const Builtin::Info BuiltinInfo[];
53  static const char *const GCCRegNames[];
54  static const TargetInfo::GCCRegAlias GCCRegAliases[];
55  std::string CPU;
56  enum PPCFloatABI { HardFloat, SoftFloat } FloatABI;
57 
58  // Target cpu features.
59  bool HasAltivec = false;
60  bool HasVSX = false;
61  bool HasP8Vector = false;
62  bool HasP8Crypto = false;
63  bool HasDirectMove = false;
64  bool HasQPX = false;
65  bool HasHTM = false;
66  bool HasBPERMD = false;
67  bool HasExtDiv = false;
68  bool HasP9Vector = false;
69 
70 protected:
71  std::string ABI;
72 
73 public:
74  PPCTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
75  : TargetInfo(Triple) {
76  SuitableAlign = 128;
77  SimdDefaultAlign = 128;
78  LongDoubleWidth = LongDoubleAlign = 128;
79  LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble();
80  }
81 
82  // Set the language option for altivec based on our value.
83  void adjust(LangOptions &Opts) override;
84 
85  // Note: GCC recognizes the following additional cpus:
86  // 401, 403, 405, 405fp, 440fp, 464, 464fp, 476, 476fp, 505, 740, 801,
87  // 821, 823, 8540, 8548, e300c2, e300c3, e500mc64, e6500, 860, cell,
88  // titan, rs64.
89  bool isValidCPUName(StringRef Name) const override;
90  void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
91 
92  bool setCPU(const std::string &Name) override {
93  bool CPUKnown = isValidCPUName(Name);
94  if (CPUKnown) {
95  CPU = Name;
96 
97  // CPU identification.
98  ArchDefs =
99  (ArchDefineTypes)llvm::StringSwitch<int>(CPU)
100  .Case("440", ArchDefineName)
101  .Case("450", ArchDefineName | ArchDefine440)
102  .Case("601", ArchDefineName)
103  .Case("602", ArchDefineName | ArchDefinePpcgr)
104  .Case("603", ArchDefineName | ArchDefinePpcgr)
105  .Case("603e", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
106  .Case("603ev", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
107  .Case("604", ArchDefineName | ArchDefinePpcgr)
108  .Case("604e", ArchDefineName | ArchDefine604 | ArchDefinePpcgr)
109  .Case("620", ArchDefineName | ArchDefinePpcgr)
110  .Case("630", ArchDefineName | ArchDefinePpcgr)
111  .Case("7400", ArchDefineName | ArchDefinePpcgr)
112  .Case("7450", ArchDefineName | ArchDefinePpcgr)
113  .Case("750", ArchDefineName | ArchDefinePpcgr)
114  .Case("970", ArchDefineName | ArchDefinePwr4 | ArchDefinePpcgr |
115  ArchDefinePpcsq)
116  .Case("a2", ArchDefineA2)
117  .Case("a2q", ArchDefineName | ArchDefineA2 | ArchDefineA2q)
118  .Cases("power3", "pwr3", ArchDefinePpcgr)
119  .Cases("power4", "pwr4",
120  ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
121  .Cases("power5", "pwr5",
122  ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
123  ArchDefinePpcsq)
124  .Cases("power5x", "pwr5x",
125  ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |
126  ArchDefinePpcgr | ArchDefinePpcsq)
127  .Cases("power6", "pwr6",
128  ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |
129  ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
130  .Cases("power6x", "pwr6x",
131  ArchDefinePwr6x | ArchDefinePwr6 | ArchDefinePwr5x |
132  ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
133  ArchDefinePpcsq)
134  .Cases("power7", "pwr7",
135  ArchDefinePwr7 | ArchDefinePwr6 | ArchDefinePwr5x |
136  ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
137  ArchDefinePpcsq)
138  // powerpc64le automatically defaults to at least power8.
139  .Cases("power8", "pwr8", "ppc64le",
140  ArchDefinePwr8 | ArchDefinePwr7 | ArchDefinePwr6 |
141  ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |
142  ArchDefinePpcgr | ArchDefinePpcsq)
143  .Cases("power9", "pwr9",
144  ArchDefinePwr9 | ArchDefinePwr8 | ArchDefinePwr7 |
145  ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |
146  ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
147  .Default(ArchDefineNone);
148  }
149  return CPUKnown;
150  }
151 
152  StringRef getABI() const override { return ABI; }
153 
154  ArrayRef<Builtin::Info> getTargetBuiltins() const override;
155 
156  bool isCLZForZeroUndef() const override { return false; }
157 
158  void getTargetDefines(const LangOptions &Opts,
159  MacroBuilder &Builder) const override;
160 
161  bool
162  initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
163  StringRef CPU,
164  const std::vector<std::string> &FeaturesVec) const override;
165 
166  bool handleTargetFeatures(std::vector<std::string> &Features,
167  DiagnosticsEngine &Diags) override;
168 
169  bool hasFeature(StringRef Feature) const override;
170 
171  void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,
172  bool Enabled) const override;
173 
174  ArrayRef<const char *> getGCCRegNames() const override;
175 
176  ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
177 
178  ArrayRef<TargetInfo::AddlRegName> getGCCAddlRegNames() const override;
179 
180  bool validateAsmConstraint(const char *&Name,
181  TargetInfo::ConstraintInfo &Info) const override {
182  switch (*Name) {
183  default:
184  return false;
185  case 'O': // Zero
186  break;
187  case 'f': // Floating point register
188  // Don't use floating point registers on soft float ABI.
189  if (FloatABI == SoftFloat)
190  return false;
191  LLVM_FALLTHROUGH;
192  case 'b': // Base register
193  Info.setAllowsRegister();
194  break;
195  // FIXME: The following are added to allow parsing.
196  // I just took a guess at what the actions should be.
197  // Also, is more specific checking needed? I.e. specific registers?
198  case 'd': // Floating point register (containing 64-bit value)
199  case 'v': // Altivec vector register
200  // Don't use floating point and altivec vector registers
201  // on soft float ABI
202  if (FloatABI == SoftFloat)
203  return false;
204  Info.setAllowsRegister();
205  break;
206  case 'w':
207  switch (Name[1]) {
208  case 'd': // VSX vector register to hold vector double data
209  case 'f': // VSX vector register to hold vector float data
210  case 's': // VSX vector register to hold scalar double data
211  case 'w': // VSX vector register to hold scalar double data
212  case 'a': // Any VSX register
213  case 'c': // An individual CR bit
214  case 'i': // FP or VSX register to hold 64-bit integers data
215  break;
216  default:
217  return false;
218  }
219  Info.setAllowsRegister();
220  Name++; // Skip over 'w'.
221  break;
222  case 'h': // `MQ', `CTR', or `LINK' register
223  case 'q': // `MQ' register
224  case 'c': // `CTR' register
225  case 'l': // `LINK' register
226  case 'x': // `CR' register (condition register) number 0
227  case 'y': // `CR' register (condition register)
228  case 'z': // `XER[CA]' carry bit (part of the XER register)
229  Info.setAllowsRegister();
230  break;
231  case 'I': // Signed 16-bit constant
232  case 'J': // Unsigned 16-bit constant shifted left 16 bits
233  // (use `L' instead for SImode constants)
234  case 'K': // Unsigned 16-bit constant
235  case 'L': // Signed 16-bit constant shifted left 16 bits
236  case 'M': // Constant larger than 31
237  case 'N': // Exact power of 2
238  case 'P': // Constant whose negation is a signed 16-bit constant
239  case 'G': // Floating point constant that can be loaded into a
240  // register with one instruction per word
241  case 'H': // Integer/Floating point constant that can be loaded
242  // into a register using three instructions
243  break;
244  case 'm': // Memory operand. Note that on PowerPC targets, m can
245  // include addresses that update the base register. It
246  // is therefore only safe to use `m' in an asm statement
247  // if that asm statement accesses the operand exactly once.
248  // The asm statement must also use `%U<opno>' as a
249  // placeholder for the "update" flag in the corresponding
250  // load or store instruction. For example:
251  // asm ("st%U0 %1,%0" : "=m" (mem) : "r" (val));
252  // is correct but:
253  // asm ("st %1,%0" : "=m" (mem) : "r" (val));
254  // is not. Use es rather than m if you don't want the base
255  // register to be updated.
256  case 'e':
257  if (Name[1] != 's')
258  return false;
259  // es: A "stable" memory operand; that is, one which does not
260  // include any automodification of the base register. Unlike
261  // `m', this constraint can be used in asm statements that
262  // might access the operand several times, or that might not
263  // access it at all.
264  Info.setAllowsMemory();
265  Name++; // Skip over 'e'.
266  break;
267  case 'Q': // Memory operand that is an offset from a register (it is
268  // usually better to use `m' or `es' in asm statements)
269  case 'Z': // Memory operand that is an indexed or indirect from a
270  // register (it is usually better to use `m' or `es' in
271  // asm statements)
272  Info.setAllowsMemory();
273  Info.setAllowsRegister();
274  break;
275  case 'R': // AIX TOC entry
276  case 'a': // Address operand that is an indexed or indirect from a
277  // register (`p' is preferable for asm statements)
278  case 'S': // Constant suitable as a 64-bit mask operand
279  case 'T': // Constant suitable as a 32-bit mask operand
280  case 'U': // System V Release 4 small data area reference
281  case 't': // AND masks that can be performed by two rldic{l, r}
282  // instructions
283  case 'W': // Vector constant that does not require memory
284  case 'j': // Vector constant that is all zeros.
285  break;
286  // End FIXME.
287  }
288  return true;
289  }
290 
291  std::string convertConstraint(const char *&Constraint) const override {
292  std::string R;
293  switch (*Constraint) {
294  case 'e':
295  case 'w':
296  // Two-character constraint; add "^" hint for later parsing.
297  R = std::string("^") + std::string(Constraint, 2);
298  Constraint++;
299  break;
300  default:
301  return TargetInfo::convertConstraint(Constraint);
302  }
303  return R;
304  }
305 
306  const char *getClobbers() const override { return ""; }
307  int getEHDataRegisterNumber(unsigned RegNo) const override {
308  if (RegNo == 0)
309  return 3;
310  if (RegNo == 1)
311  return 4;
312  return -1;
313  }
314 
315  bool hasSjLjLowering() const override { return true; }
316 
317  const char *getLongDoubleMangling() const override {
318  if (LongDoubleWidth == 64)
319  return "e";
320  return LongDoubleFormat == &llvm::APFloat::PPCDoubleDouble()
321  ? "g"
322  : "u9__ieee128";
323  }
324  const char *getFloat128Mangling() const override { return "u9__ieee128"; }
325 };
326 
327 class LLVM_LIBRARY_VISIBILITY PPC32TargetInfo : public PPCTargetInfo {
328 public:
329  PPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
330  : PPCTargetInfo(Triple, Opts) {
331  resetDataLayout("E-m:e-p:32:32-i64:64-n32");
332 
333  switch (getTriple().getOS()) {
334  case llvm::Triple::Linux:
335  case llvm::Triple::FreeBSD:
336  case llvm::Triple::NetBSD:
337  SizeType = UnsignedInt;
338  PtrDiffType = SignedInt;
339  IntPtrType = SignedInt;
340  break;
341  case llvm::Triple::AIX:
342  SizeType = UnsignedLong;
343  PtrDiffType = SignedLong;
344  IntPtrType = SignedLong;
345  SuitableAlign = 64;
346  break;
347  default:
348  break;
349  }
350 
351  if (Triple.isOSFreeBSD() || Triple.isOSNetBSD() || Triple.isOSOpenBSD() ||
352  Triple.getOS() == llvm::Triple::AIX || Triple.isMusl()) {
353  LongDoubleWidth = LongDoubleAlign = 64;
354  LongDoubleFormat = &llvm::APFloat::IEEEdouble();
355  }
356 
357  // PPC32 supports atomics up to 4 bytes.
358  MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
359  }
360 
362  // This is the ELF definition, and is overridden by the Darwin sub-target
364  }
365 };
366 
367 // Note: ABI differences may eventually require us to have a separate
368 // TargetInfo for little endian.
369 class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
370 public:
371  PPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
372  : PPCTargetInfo(Triple, Opts) {
373  LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
374  IntMaxType = SignedLong;
375  Int64Type = SignedLong;
376 
377  if ((Triple.getArch() == llvm::Triple::ppc64le)) {
378  resetDataLayout("e-m:e-i64:64-n32:64");
379  ABI = "elfv2";
380  } else {
381  resetDataLayout("E-m:e-i64:64-n32:64");
382  ABI = Triple.getEnvironment() == llvm::Triple::ELFv2 ? "elfv2" : "elfv1";
383  }
384 
385  if (Triple.getOS() == llvm::Triple::AIX)
386  SuitableAlign = 64;
387 
388  if (Triple.isOSFreeBSD() || Triple.getOS() == llvm::Triple::AIX ||
389  Triple.isMusl()) {
390  LongDoubleWidth = LongDoubleAlign = 64;
391  LongDoubleFormat = &llvm::APFloat::IEEEdouble();
392  }
393 
394  // PPC64 supports atomics up to 8 bytes.
395  MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
396  }
397 
400  }
401 
402  // PPC64 Linux-specific ABI options.
403  bool setABI(const std::string &Name) override {
404  if (Name == "elfv1" || Name == "elfv1-qpx" || Name == "elfv2") {
405  ABI = Name;
406  return true;
407  }
408  return false;
409  }
410 
412  switch (CC) {
413  case CC_Swift:
414  return CCCR_OK;
415  default:
416  return CCCR_Warning;
417  }
418  }
419 };
420 
421 class LLVM_LIBRARY_VISIBILITY DarwinPPC32TargetInfo
422  : public DarwinTargetInfo<PPC32TargetInfo> {
423 public:
424  DarwinPPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
425  : DarwinTargetInfo<PPC32TargetInfo>(Triple, Opts) {
426  HasAlignMac68kSupport = true;
427  BoolWidth = BoolAlign = 32; // XXX support -mone-byte-bool?
428  PtrDiffType = SignedInt; // for http://llvm.org/bugs/show_bug.cgi?id=15726
429  LongLongAlign = 32;
430  resetDataLayout("E-m:o-p:32:32-f64:32:64-n32");
431  }
432 
435  }
436 };
437 
438 class LLVM_LIBRARY_VISIBILITY DarwinPPC64TargetInfo
439  : public DarwinTargetInfo<PPC64TargetInfo> {
440 public:
441  DarwinPPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
442  : DarwinTargetInfo<PPC64TargetInfo>(Triple, Opts) {
443  HasAlignMac68kSupport = true;
444  resetDataLayout("E-m:o-i64:64-n32:64");
445  }
446 };
447 
448 class LLVM_LIBRARY_VISIBILITY AIXPPC32TargetInfo :
449  public AIXTargetInfo<PPC32TargetInfo> {
450 public:
454  }
455 };
456 
457 class LLVM_LIBRARY_VISIBILITY AIXPPC64TargetInfo :
458  public AIXTargetInfo<PPC64TargetInfo> {
459 public:
461 };
462 
463 } // namespace targets
464 } // namespace clang
465 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H
PPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: PPC.h:329
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override
Determines whether a given calling convention is valid for the target.
Definition: PPC.h:411
DarwinPPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: PPC.h:441
static const Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:20
static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, const TargetInfo &Target)
Determine whether a translation unit built using the current language options has the given feature...
Definition: Module.cpp:106
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: PPC.h:361
Options for controlling the target.
Definition: TargetOptions.h:26
PPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: PPC.h:371
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
StringRef getABI() const override
Get the ABI currently in use.
Definition: PPC.h:152
static const char *const GCCRegNames[]
Definition: X86.cpp:43
AIXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: OSTargets.h:699
DarwinPPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: PPC.h:424
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: PPC.h:398
Exposes information about the current target.
Definition: TargetInfo.h:161
bool setABI(const std::string &Name) override
Use the specified ABI.
Definition: PPC.h:403
bool setCPU(const std::string &Name) override
Target the specified CPU.
Definition: PPC.h:92
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:264
int getEHDataRegisterNumber(unsigned RegNo) const override
Return the register number that __builtin_eh_return_regno would return with the specified argument...
Definition: PPC.h:307
Defines the clang::TargetOptions class.
__builtin_va_list as defined by the Power ABI: https://www.power.org /resources/downloads/Power-Arch-...
Definition: TargetInfo.h:241
const char * getFloat128Mangling() const override
Return the mangled code of __float128.
Definition: PPC.h:324
bool isCLZForZeroUndef() const override
The __builtin_clz* and __builtin_ctz* built-in functions are specified to have undefined results for ...
Definition: PPC.h:156
Dataflow Directional Tag Classes.
typedef char* __builtin_va_list;
Definition: TargetInfo.h:225
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: PPC.h:452
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition: TargetInfo.h:223
bool hasSjLjLowering() const override
Controls if __builtin_longjmp / __builtin_setjmp can be lowered to llvm.eh.sjlj.longjmp / llvm...
Definition: PPC.h:315
Defines the clang::TargetInfo interface.
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: PPC.h:433
PPCTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
Definition: PPC.h:74
std::string convertConstraint(const char *&Constraint) const override
Definition: PPC.h:291
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Definition: PPC.h:180
const char * getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition: PPC.h:306
virtual std::string convertConstraint(const char *&Constraint) const
Definition: TargetInfo.h:968
const char * getLongDoubleMangling() const override
Return the mangled code of long double.
Definition: PPC.h:317