blob: b168439c120e899ac14dad7028d0dcb2c07d6af1 [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;
56
57 // Target cpu features.
Benjamin Kramerffe60e02018-06-13 16:45:12 +000058 bool HasAltivec = false;
59 bool HasVSX = false;
60 bool HasP8Vector = false;
61 bool HasP8Crypto = false;
62 bool HasDirectMove = false;
63 bool HasQPX = false;
64 bool HasHTM = false;
65 bool HasBPERMD = false;
66 bool HasExtDiv = false;
67 bool HasP9Vector = false;
Erich Keaneebba5922017-07-21 22:37:03 +000068
69protected:
70 std::string ABI;
71
72public:
73 PPCTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
Benjamin Kramerffe60e02018-06-13 16:45:12 +000074 : TargetInfo(Triple) {
Erich Keaneebba5922017-07-21 22:37:03 +000075 SuitableAlign = 128;
76 SimdDefaultAlign = 128;
77 LongDoubleWidth = LongDoubleAlign = 128;
78 LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble();
79 }
80
Erich Keaneebba5922017-07-21 22:37:03 +000081 // Set the language option for altivec based on our value.
82 void adjust(LangOptions &Opts) override;
83
84 // Note: GCC recognizes the following additional cpus:
85 // 401, 403, 405, 405fp, 440fp, 464, 464fp, 476, 476fp, 505, 740, 801,
86 // 821, 823, 8540, 8548, e300c2, e300c3, e500mc64, e6500, 860, cell,
87 // titan, rs64.
88 bool isValidCPUName(StringRef Name) const override;
Erich Keanee44bdb32018-02-08 23:16:55 +000089 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
Erich Keaneebba5922017-07-21 22:37:03 +000090
91 bool setCPU(const std::string &Name) override {
92 bool CPUKnown = isValidCPUName(Name);
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +000093 if (CPUKnown) {
Erich Keaneebba5922017-07-21 22:37:03 +000094 CPU = Name;
Stefan Pintiliea6ce3fe2018-06-13 16:05:05 +000095
96 // CPU identification.
97 ArchDefs =
98 (ArchDefineTypes)llvm::StringSwitch<int>(CPU)
99 .Case("440", ArchDefineName)
100 .Case("450", ArchDefineName | ArchDefine440)
101 .Case("601", ArchDefineName)
102 .Case("602", ArchDefineName | ArchDefinePpcgr)
103 .Case("603", ArchDefineName | ArchDefinePpcgr)
104 .Case("603e", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
105 .Case("603ev", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
106 .Case("604", ArchDefineName | ArchDefinePpcgr)
107 .Case("604e", ArchDefineName | ArchDefine604 | ArchDefinePpcgr)
108 .Case("620", ArchDefineName | ArchDefinePpcgr)
109 .Case("630", ArchDefineName | ArchDefinePpcgr)
110 .Case("7400", ArchDefineName | ArchDefinePpcgr)
111 .Case("7450", ArchDefineName | ArchDefinePpcgr)
112 .Case("750", ArchDefineName | ArchDefinePpcgr)
113 .Case("970", ArchDefineName | ArchDefinePwr4 | ArchDefinePpcgr |
114 ArchDefinePpcsq)
115 .Case("a2", ArchDefineA2)
116 .Case("a2q", ArchDefineName | ArchDefineA2 | ArchDefineA2q)
117 .Cases("power3", "pwr3", ArchDefinePpcgr)
118 .Cases("power4", "pwr4",
119 ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
120 .Cases("power5", "pwr5",
121 ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
122 ArchDefinePpcsq)
123 .Cases("power5x", "pwr5x",
124 ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |
125 ArchDefinePpcgr | ArchDefinePpcsq)
126 .Cases("power6", "pwr6",
127 ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |
128 ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
129 .Cases("power6x", "pwr6x",
130 ArchDefinePwr6x | ArchDefinePwr6 | ArchDefinePwr5x |
131 ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
132 ArchDefinePpcsq)
133 .Cases("power7", "pwr7",
134 ArchDefinePwr7 | ArchDefinePwr6x | ArchDefinePwr6 |
135 ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |
136 ArchDefinePpcgr | ArchDefinePpcsq)
137 // powerpc64le automatically defaults to at least power8.
138 .Cases("power8", "pwr8", "ppc64le",
139 ArchDefinePwr8 | ArchDefinePwr7 | ArchDefinePwr6x |
140 ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |
141 ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
142 .Cases("power9", "pwr9",
143 ArchDefinePwr9 | ArchDefinePwr8 | ArchDefinePwr7 |
144 ArchDefinePwr6x | ArchDefinePwr6 | ArchDefinePwr5x |
145 ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
146 ArchDefinePpcsq)
147 .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;
187 case 'b': // Base register
188 case 'f': // Floating point register
189 Info.setAllowsRegister();
190 break;
191 // FIXME: The following are added to allow parsing.
192 // I just took a guess at what the actions should be.
193 // Also, is more specific checking needed? I.e. specific registers?
194 case 'd': // Floating point register (containing 64-bit value)
195 case 'v': // Altivec vector register
196 Info.setAllowsRegister();
197 break;
198 case 'w':
199 switch (Name[1]) {
200 case 'd': // VSX vector register to hold vector double data
201 case 'f': // VSX vector register to hold vector float data
202 case 's': // VSX vector register to hold scalar float data
203 case 'a': // Any VSX register
204 case 'c': // An individual CR bit
Li Jia Hebbaedf22018-11-01 02:32:49 +0000205 case 'i': // FP or VSX register to hold 64-bit integers data
Erich Keaneebba5922017-07-21 22:37:03 +0000206 break;
207 default:
208 return false;
209 }
210 Info.setAllowsRegister();
211 Name++; // Skip over 'w'.
212 break;
213 case 'h': // `MQ', `CTR', or `LINK' register
214 case 'q': // `MQ' register
215 case 'c': // `CTR' register
216 case 'l': // `LINK' register
217 case 'x': // `CR' register (condition register) number 0
218 case 'y': // `CR' register (condition register)
219 case 'z': // `XER[CA]' carry bit (part of the XER register)
220 Info.setAllowsRegister();
221 break;
222 case 'I': // Signed 16-bit constant
223 case 'J': // Unsigned 16-bit constant shifted left 16 bits
224 // (use `L' instead for SImode constants)
225 case 'K': // Unsigned 16-bit constant
226 case 'L': // Signed 16-bit constant shifted left 16 bits
227 case 'M': // Constant larger than 31
228 case 'N': // Exact power of 2
229 case 'P': // Constant whose negation is a signed 16-bit constant
230 case 'G': // Floating point constant that can be loaded into a
231 // register with one instruction per word
232 case 'H': // Integer/Floating point constant that can be loaded
233 // into a register using three instructions
234 break;
235 case 'm': // Memory operand. Note that on PowerPC targets, m can
236 // include addresses that update the base register. It
237 // is therefore only safe to use `m' in an asm statement
238 // if that asm statement accesses the operand exactly once.
239 // The asm statement must also use `%U<opno>' as a
240 // placeholder for the "update" flag in the corresponding
241 // load or store instruction. For example:
242 // asm ("st%U0 %1,%0" : "=m" (mem) : "r" (val));
243 // is correct but:
244 // asm ("st %1,%0" : "=m" (mem) : "r" (val));
245 // is not. Use es rather than m if you don't want the base
246 // register to be updated.
247 case 'e':
248 if (Name[1] != 's')
249 return false;
250 // es: A "stable" memory operand; that is, one which does not
251 // include any automodification of the base register. Unlike
252 // `m', this constraint can be used in asm statements that
253 // might access the operand several times, or that might not
254 // access it at all.
255 Info.setAllowsMemory();
256 Name++; // Skip over 'e'.
257 break;
258 case 'Q': // Memory operand that is an offset from a register (it is
259 // usually better to use `m' or `es' in asm statements)
260 case 'Z': // Memory operand that is an indexed or indirect from a
261 // register (it is usually better to use `m' or `es' in
262 // asm statements)
263 Info.setAllowsMemory();
264 Info.setAllowsRegister();
265 break;
266 case 'R': // AIX TOC entry
267 case 'a': // Address operand that is an indexed or indirect from a
268 // register (`p' is preferable for asm statements)
269 case 'S': // Constant suitable as a 64-bit mask operand
270 case 'T': // Constant suitable as a 32-bit mask operand
271 case 'U': // System V Release 4 small data area reference
272 case 't': // AND masks that can be performed by two rldic{l, r}
273 // instructions
274 case 'W': // Vector constant that does not require memory
275 case 'j': // Vector constant that is all zeros.
276 break;
277 // End FIXME.
278 }
279 return true;
280 }
281
282 std::string convertConstraint(const char *&Constraint) const override {
283 std::string R;
284 switch (*Constraint) {
285 case 'e':
286 case 'w':
287 // Two-character constraint; add "^" hint for later parsing.
288 R = std::string("^") + std::string(Constraint, 2);
289 Constraint++;
290 break;
291 default:
292 return TargetInfo::convertConstraint(Constraint);
293 }
294 return R;
295 }
296
297 const char *getClobbers() const override { return ""; }
298 int getEHDataRegisterNumber(unsigned RegNo) const override {
299 if (RegNo == 0)
300 return 3;
301 if (RegNo == 1)
302 return 4;
303 return -1;
304 }
305
306 bool hasSjLjLowering() const override { return true; }
307
308 bool useFloat128ManglingForLongDouble() const override {
309 return LongDoubleWidth == 128 &&
310 LongDoubleFormat == &llvm::APFloat::PPCDoubleDouble() &&
311 getTriple().isOSBinFormatELF();
312 }
313};
314
315class LLVM_LIBRARY_VISIBILITY PPC32TargetInfo : public PPCTargetInfo {
316public:
317 PPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
318 : PPCTargetInfo(Triple, Opts) {
319 resetDataLayout("E-m:e-p:32:32-i64:64-n32");
320
321 switch (getTriple().getOS()) {
322 case llvm::Triple::Linux:
323 case llvm::Triple::FreeBSD:
324 case llvm::Triple::NetBSD:
325 SizeType = UnsignedInt;
326 PtrDiffType = SignedInt;
327 IntPtrType = SignedInt;
328 break;
329 default:
330 break;
331 }
332
Brad Smith09699a72019-02-11 02:53:16 +0000333 switch (getTriple().getOS()) {
334 case llvm::Triple::FreeBSD:
335 case llvm::Triple::NetBSD:
336 case llvm::Triple::OpenBSD:
Erich Keaneebba5922017-07-21 22:37:03 +0000337 LongDoubleWidth = LongDoubleAlign = 64;
338 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
Brad Smith09699a72019-02-11 02:53:16 +0000339 break;
340 default:
341 break;
Erich Keaneebba5922017-07-21 22:37:03 +0000342 }
343
344 // PPC32 supports atomics up to 4 bytes.
345 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
346 }
347
348 BuiltinVaListKind getBuiltinVaListKind() const override {
349 // This is the ELF definition, and is overridden by the Darwin sub-target
350 return TargetInfo::PowerABIBuiltinVaList;
351 }
352};
353
354// Note: ABI differences may eventually require us to have a separate
355// TargetInfo for little endian.
356class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
357public:
358 PPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
359 : PPCTargetInfo(Triple, Opts) {
360 LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
361 IntMaxType = SignedLong;
362 Int64Type = SignedLong;
363
364 if ((Triple.getArch() == llvm::Triple::ppc64le)) {
365 resetDataLayout("e-m:e-i64:64-n32:64");
366 ABI = "elfv2";
367 } else {
368 resetDataLayout("E-m:e-i64:64-n32:64");
369 ABI = "elfv1";
370 }
371
372 switch (getTriple().getOS()) {
373 case llvm::Triple::FreeBSD:
374 LongDoubleWidth = LongDoubleAlign = 64;
375 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
376 break;
Erich Keaneebba5922017-07-21 22:37:03 +0000377 default:
378 break;
379 }
380
381 // PPC64 supports atomics up to 8 bytes.
382 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
383 }
384
385 BuiltinVaListKind getBuiltinVaListKind() const override {
386 return TargetInfo::CharPtrBuiltinVaList;
387 }
388
389 // PPC64 Linux-specific ABI options.
390 bool setABI(const std::string &Name) override {
391 if (Name == "elfv1" || Name == "elfv1-qpx" || Name == "elfv2") {
392 ABI = Name;
393 return true;
394 }
395 return false;
396 }
Bob Wilsonfa84fc92018-05-25 21:26:03 +0000397
398 CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
399 switch (CC) {
400 case CC_Swift:
401 return CCCR_OK;
402 default:
403 return CCCR_Warning;
404 }
405 }
Erich Keaneebba5922017-07-21 22:37:03 +0000406};
407
408class LLVM_LIBRARY_VISIBILITY DarwinPPC32TargetInfo
409 : public DarwinTargetInfo<PPC32TargetInfo> {
410public:
411 DarwinPPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
412 : DarwinTargetInfo<PPC32TargetInfo>(Triple, Opts) {
413 HasAlignMac68kSupport = true;
414 BoolWidth = BoolAlign = 32; // XXX support -mone-byte-bool?
415 PtrDiffType = SignedInt; // for http://llvm.org/bugs/show_bug.cgi?id=15726
416 LongLongAlign = 32;
417 resetDataLayout("E-m:o-p:32:32-f64:32:64-n32");
418 }
419
420 BuiltinVaListKind getBuiltinVaListKind() const override {
421 return TargetInfo::CharPtrBuiltinVaList;
422 }
423};
424
425class LLVM_LIBRARY_VISIBILITY DarwinPPC64TargetInfo
426 : public DarwinTargetInfo<PPC64TargetInfo> {
427public:
428 DarwinPPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
429 : DarwinTargetInfo<PPC64TargetInfo>(Triple, Opts) {
430 HasAlignMac68kSupport = true;
431 resetDataLayout("E-m:o-i64:64-n32:64");
432 }
433};
434
435} // namespace targets
436} // namespace clang
437#endif // LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H