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