blob: 7049020a91139ad9fbca9886a9ff2331916ea406 [file] [log] [blame]
Erich Keaneebba5922017-07-21 22:37:03 +00001//===--- PPC.h - Declare PPC target feature support -------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Erich Keaneebba5922017-07-21 22:37:03 +00006//
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"
18#include "clang/Basic/TargetOptions.h"
19#include "llvm/ADT/Triple.h"
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +000020#include "llvm/ADT/StringSwitch.h"
Erich Keaneebba5922017-07-21 22:37:03 +000021#include "llvm/Support/Compiler.h"
22
23namespace clang {
24namespace targets {
25
26// PPC abstract base class
27class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +000028
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
Benjamin Kramerffe60e02018-06-13 16:45:12 +000051 ArchDefineTypes ArchDefs = ArchDefineNone;
Erich Keaneebba5922017-07-21 22:37:03 +000052 static const Builtin::Info BuiltinInfo[];
53 static const char *const GCCRegNames[];
54 static const TargetInfo::GCCRegAlias GCCRegAliases[];
55 std::string CPU;
Strahinja Petrovic4f839ac2019-04-02 11:00:09 +000056 enum PPCFloatABI { HardFloat, SoftFloat } FloatABI;
Erich Keaneebba5922017-07-21 22:37:03 +000057
58 // Target cpu features.
Benjamin Kramerffe60e02018-06-13 16:45:12 +000059 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;
Erich Keaneebba5922017-07-21 22:37:03 +000069
70protected:
71 std::string ABI;
72
73public:
74 PPCTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
Benjamin Kramerffe60e02018-06-13 16:45:12 +000075 : TargetInfo(Triple) {
Erich Keaneebba5922017-07-21 22:37:03 +000076 SuitableAlign = 128;
77 SimdDefaultAlign = 128;
78 LongDoubleWidth = LongDoubleAlign = 128;
79 LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble();
80 }
81
Erich Keaneebba5922017-07-21 22:37:03 +000082 // 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;
Erich Keanee44bdb32018-02-08 23:16:55 +000090 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
Erich Keaneebba5922017-07-21 22:37:03 +000091
92 bool setCPU(const std::string &Name) override {
93 bool CPUKnown = isValidCPUName(Name);
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +000094 if (CPUKnown) {
Erich Keaneebba5922017-07-21 22:37:03 +000095 CPU = Name;
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +000096
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",
Hubert Tong45195c82019-02-13 20:17:13 +0000135 ArchDefinePwr7 | ArchDefinePwr6 | ArchDefinePwr5x |
136 ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
137 ArchDefinePpcsq)
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +0000138 // powerpc64le automatically defaults to at least power8.
139 .Cases("power8", "pwr8", "ppc64le",
Hubert Tong45195c82019-02-13 20:17:13 +0000140 ArchDefinePwr8 | ArchDefinePwr7 | ArchDefinePwr6 |
141 ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |
142 ArchDefinePpcgr | ArchDefinePpcsq)
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +0000143 .Cases("power9", "pwr9",
Hubert Tong45195c82019-02-13 20:17:13 +0000144 ArchDefinePwr9 | ArchDefinePwr8 | ArchDefinePwr7 |
145 ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |
146 ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +0000147 .Default(ArchDefineNone);
148 }
Erich Keaneebba5922017-07-21 22:37:03 +0000149 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
Kang Zhang9606d582018-12-07 08:58:12 +0000178 ArrayRef<TargetInfo::AddlRegName> getGCCAddlRegNames() const override;
179
Erich Keaneebba5922017-07-21 22:37:03 +0000180 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;
Erich Keaneebba5922017-07-21 22:37:03 +0000187 case 'f': // Floating point register
Strahinja Petrovic4f839ac2019-04-02 11:00:09 +0000188 // Don't use floating point registers on soft float ABI.
189 if (FloatABI == SoftFloat)
190 return false;
191 case 'b': // Base register
Erich Keaneebba5922017-07-21 22:37:03 +0000192 Info.setAllowsRegister();
193 break;
194 // FIXME: The following are added to allow parsing.
195 // I just took a guess at what the actions should be.
196 // Also, is more specific checking needed? I.e. specific registers?
197 case 'd': // Floating point register (containing 64-bit value)
198 case 'v': // Altivec vector register
Strahinja Petrovic4f839ac2019-04-02 11:00:09 +0000199 // Don't use floating point and altivec vector registers
200 // on soft float ABI
201 if (FloatABI == SoftFloat)
202 return false;
Erich Keaneebba5922017-07-21 22:37:03 +0000203 Info.setAllowsRegister();
204 break;
205 case 'w':
206 switch (Name[1]) {
207 case 'd': // VSX vector register to hold vector double data
208 case 'f': // VSX vector register to hold vector float data
209 case 's': // VSX vector register to hold scalar float data
210 case 'a': // Any VSX register
211 case 'c': // An individual CR bit
Li Jia Hebbaedf22018-11-01 02:32:49 +0000212 case 'i': // FP or VSX register to hold 64-bit integers data
Erich Keaneebba5922017-07-21 22:37:03 +0000213 break;
214 default:
215 return false;
216 }
217 Info.setAllowsRegister();
218 Name++; // Skip over 'w'.
219 break;
220 case 'h': // `MQ', `CTR', or `LINK' register
221 case 'q': // `MQ' register
222 case 'c': // `CTR' register
223 case 'l': // `LINK' register
224 case 'x': // `CR' register (condition register) number 0
225 case 'y': // `CR' register (condition register)
226 case 'z': // `XER[CA]' carry bit (part of the XER register)
227 Info.setAllowsRegister();
228 break;
229 case 'I': // Signed 16-bit constant
230 case 'J': // Unsigned 16-bit constant shifted left 16 bits
231 // (use `L' instead for SImode constants)
232 case 'K': // Unsigned 16-bit constant
233 case 'L': // Signed 16-bit constant shifted left 16 bits
234 case 'M': // Constant larger than 31
235 case 'N': // Exact power of 2
236 case 'P': // Constant whose negation is a signed 16-bit constant
237 case 'G': // Floating point constant that can be loaded into a
238 // register with one instruction per word
239 case 'H': // Integer/Floating point constant that can be loaded
240 // into a register using three instructions
241 break;
242 case 'm': // Memory operand. Note that on PowerPC targets, m can
243 // include addresses that update the base register. It
244 // is therefore only safe to use `m' in an asm statement
245 // if that asm statement accesses the operand exactly once.
246 // The asm statement must also use `%U<opno>' as a
247 // placeholder for the "update" flag in the corresponding
248 // load or store instruction. For example:
249 // asm ("st%U0 %1,%0" : "=m" (mem) : "r" (val));
250 // is correct but:
251 // asm ("st %1,%0" : "=m" (mem) : "r" (val));
252 // is not. Use es rather than m if you don't want the base
253 // register to be updated.
254 case 'e':
255 if (Name[1] != 's')
256 return false;
257 // es: A "stable" memory operand; that is, one which does not
258 // include any automodification of the base register. Unlike
259 // `m', this constraint can be used in asm statements that
260 // might access the operand several times, or that might not
261 // access it at all.
262 Info.setAllowsMemory();
263 Name++; // Skip over 'e'.
264 break;
265 case 'Q': // Memory operand that is an offset from a register (it is
266 // usually better to use `m' or `es' in asm statements)
267 case 'Z': // Memory operand that is an indexed or indirect from a
268 // register (it is usually better to use `m' or `es' in
269 // asm statements)
270 Info.setAllowsMemory();
271 Info.setAllowsRegister();
272 break;
273 case 'R': // AIX TOC entry
274 case 'a': // Address operand that is an indexed or indirect from a
275 // register (`p' is preferable for asm statements)
276 case 'S': // Constant suitable as a 64-bit mask operand
277 case 'T': // Constant suitable as a 32-bit mask operand
278 case 'U': // System V Release 4 small data area reference
279 case 't': // AND masks that can be performed by two rldic{l, r}
280 // instructions
281 case 'W': // Vector constant that does not require memory
282 case 'j': // Vector constant that is all zeros.
283 break;
284 // End FIXME.
285 }
286 return true;
287 }
288
289 std::string convertConstraint(const char *&Constraint) const override {
290 std::string R;
291 switch (*Constraint) {
292 case 'e':
293 case 'w':
294 // Two-character constraint; add "^" hint for later parsing.
295 R = std::string("^") + std::string(Constraint, 2);
296 Constraint++;
297 break;
298 default:
299 return TargetInfo::convertConstraint(Constraint);
300 }
301 return R;
302 }
303
304 const char *getClobbers() const override { return ""; }
305 int getEHDataRegisterNumber(unsigned RegNo) const override {
306 if (RegNo == 0)
307 return 3;
308 if (RegNo == 1)
309 return 4;
310 return -1;
311 }
312
313 bool hasSjLjLowering() const override { return true; }
314
315 bool useFloat128ManglingForLongDouble() const override {
316 return LongDoubleWidth == 128 &&
317 LongDoubleFormat == &llvm::APFloat::PPCDoubleDouble() &&
318 getTriple().isOSBinFormatELF();
319 }
320};
321
322class LLVM_LIBRARY_VISIBILITY PPC32TargetInfo : public PPCTargetInfo {
323public:
324 PPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
325 : PPCTargetInfo(Triple, Opts) {
326 resetDataLayout("E-m:e-p:32:32-i64:64-n32");
327
328 switch (getTriple().getOS()) {
329 case llvm::Triple::Linux:
330 case llvm::Triple::FreeBSD:
331 case llvm::Triple::NetBSD:
332 SizeType = UnsignedInt;
333 PtrDiffType = SignedInt;
334 IntPtrType = SignedInt;
335 break;
Jason Liu7f7867b2019-03-14 21:54:30 +0000336 case llvm::Triple::AIX:
337 SizeType = UnsignedLong;
338 PtrDiffType = SignedLong;
339 IntPtrType = SignedLong;
340 SuitableAlign = 64;
341 break;
Erich Keaneebba5922017-07-21 22:37:03 +0000342 default:
343 break;
344 }
345
Brad Smith09699a72019-02-11 02:53:16 +0000346 switch (getTriple().getOS()) {
347 case llvm::Triple::FreeBSD:
348 case llvm::Triple::NetBSD:
349 case llvm::Triple::OpenBSD:
Jason Liu7f7867b2019-03-14 21:54:30 +0000350 // FIXME: -mlong-double-128 is not yet supported on AIX.
351 case llvm::Triple::AIX:
Erich Keaneebba5922017-07-21 22:37:03 +0000352 LongDoubleWidth = LongDoubleAlign = 64;
353 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
Brad Smith09699a72019-02-11 02:53:16 +0000354 break;
355 default:
356 break;
Erich Keaneebba5922017-07-21 22:37:03 +0000357 }
358
359 // PPC32 supports atomics up to 4 bytes.
360 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
361 }
362
363 BuiltinVaListKind getBuiltinVaListKind() const override {
364 // This is the ELF definition, and is overridden by the Darwin sub-target
365 return TargetInfo::PowerABIBuiltinVaList;
366 }
367};
368
369// Note: ABI differences may eventually require us to have a separate
370// TargetInfo for little endian.
371class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
372public:
373 PPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
374 : PPCTargetInfo(Triple, Opts) {
375 LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
376 IntMaxType = SignedLong;
377 Int64Type = SignedLong;
378
379 if ((Triple.getArch() == llvm::Triple::ppc64le)) {
380 resetDataLayout("e-m:e-i64:64-n32:64");
381 ABI = "elfv2";
382 } else {
383 resetDataLayout("E-m:e-i64:64-n32:64");
384 ABI = "elfv1";
385 }
386
387 switch (getTriple().getOS()) {
388 case llvm::Triple::FreeBSD:
389 LongDoubleWidth = LongDoubleAlign = 64;
390 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
391 break;
Jason Liu7f7867b2019-03-14 21:54:30 +0000392 case llvm::Triple::AIX:
393 // FIXME: -mlong-double-128 is not yet supported on AIX.
394 LongDoubleWidth = LongDoubleAlign = 64;
395 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
396 SuitableAlign = 64;
397 break;
Erich Keaneebba5922017-07-21 22:37:03 +0000398 default:
399 break;
400 }
401
402 // PPC64 supports atomics up to 8 bytes.
403 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
404 }
405
406 BuiltinVaListKind getBuiltinVaListKind() const override {
407 return TargetInfo::CharPtrBuiltinVaList;
408 }
409
410 // PPC64 Linux-specific ABI options.
411 bool setABI(const std::string &Name) override {
412 if (Name == "elfv1" || Name == "elfv1-qpx" || Name == "elfv2") {
413 ABI = Name;
414 return true;
415 }
416 return false;
417 }
Bob Wilsonfa84fc92018-05-25 21:26:03 +0000418
419 CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
420 switch (CC) {
421 case CC_Swift:
422 return CCCR_OK;
423 default:
424 return CCCR_Warning;
425 }
426 }
Erich Keaneebba5922017-07-21 22:37:03 +0000427};
428
429class LLVM_LIBRARY_VISIBILITY DarwinPPC32TargetInfo
430 : public DarwinTargetInfo<PPC32TargetInfo> {
431public:
432 DarwinPPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
433 : DarwinTargetInfo<PPC32TargetInfo>(Triple, Opts) {
434 HasAlignMac68kSupport = true;
435 BoolWidth = BoolAlign = 32; // XXX support -mone-byte-bool?
436 PtrDiffType = SignedInt; // for http://llvm.org/bugs/show_bug.cgi?id=15726
437 LongLongAlign = 32;
438 resetDataLayout("E-m:o-p:32:32-f64:32:64-n32");
439 }
440
441 BuiltinVaListKind getBuiltinVaListKind() const override {
442 return TargetInfo::CharPtrBuiltinVaList;
443 }
444};
445
446class LLVM_LIBRARY_VISIBILITY DarwinPPC64TargetInfo
447 : public DarwinTargetInfo<PPC64TargetInfo> {
448public:
449 DarwinPPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
450 : DarwinTargetInfo<PPC64TargetInfo>(Triple, Opts) {
451 HasAlignMac68kSupport = true;
452 resetDataLayout("E-m:o-i64:64-n32:64");
453 }
454};
455
Jason Liu7f7867b2019-03-14 21:54:30 +0000456class LLVM_LIBRARY_VISIBILITY AIXPPC32TargetInfo :
457 public AIXTargetInfo<PPC32TargetInfo> {
458public:
459 using AIXTargetInfo::AIXTargetInfo;
460 BuiltinVaListKind getBuiltinVaListKind() const override {
461 return TargetInfo::CharPtrBuiltinVaList;
462 }
463};
464
465class LLVM_LIBRARY_VISIBILITY AIXPPC64TargetInfo :
466 public AIXTargetInfo<PPC64TargetInfo> {
467public:
468 using AIXTargetInfo::AIXTargetInfo;
469};
470
Erich Keaneebba5922017-07-21 22:37:03 +0000471} // namespace targets
472} // namespace clang
473#endif // LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H