PPC.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H
  13. #define LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H
  14. #include "OSTargets.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "clang/Basic/TargetOptions.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/ADT/StringSwitch.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace clang {
  21. namespace targets {
  22. // PPC abstract base class
  23. class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {
  24. /// Flags for architecture specific defines.
  25. typedef enum {
  26. ArchDefineNone = 0,
  27. ArchDefineName = 1 << 0, // <name> is substituted for arch name.
  28. ArchDefinePpcgr = 1 << 1,
  29. ArchDefinePpcsq = 1 << 2,
  30. ArchDefine440 = 1 << 3,
  31. ArchDefine603 = 1 << 4,
  32. ArchDefine604 = 1 << 5,
  33. ArchDefinePwr4 = 1 << 6,
  34. ArchDefinePwr5 = 1 << 7,
  35. ArchDefinePwr5x = 1 << 8,
  36. ArchDefinePwr6 = 1 << 9,
  37. ArchDefinePwr6x = 1 << 10,
  38. ArchDefinePwr7 = 1 << 11,
  39. ArchDefinePwr8 = 1 << 12,
  40. ArchDefinePwr9 = 1 << 13,
  41. ArchDefinePwr10 = 1 << 14,
  42. ArchDefineFuture = 1 << 15,
  43. ArchDefineA2 = 1 << 16,
  44. ArchDefineE500 = 1 << 18
  45. } ArchDefineTypes;
  46. ArchDefineTypes ArchDefs = ArchDefineNone;
  47. static const Builtin::Info BuiltinInfo[];
  48. static const char *const GCCRegNames[];
  49. static const TargetInfo::GCCRegAlias GCCRegAliases[];
  50. std::string CPU;
  51. enum PPCFloatABI { HardFloat, SoftFloat } FloatABI;
  52. // Target cpu features.
  53. bool HasAltivec = false;
  54. bool HasMMA = false;
  55. bool HasROPProtect = false;
  56. bool HasPrivileged = false;
  57. bool HasVSX = false;
  58. bool HasP8Vector = false;
  59. bool HasP8Crypto = false;
  60. bool HasDirectMove = false;
  61. bool HasHTM = false;
  62. bool HasBPERMD = false;
  63. bool HasExtDiv = false;
  64. bool HasP9Vector = false;
  65. bool HasSPE = false;
  66. bool PairedVectorMemops = false;
  67. bool HasP10Vector = false;
  68. bool HasPCRelativeMemops = false;
  69. bool HasPrefixInstrs = false;
  70. bool IsISA2_06 = false;
  71. bool IsISA2_07 = false;
  72. bool IsISA3_0 = false;
  73. bool IsISA3_1 = false;
  74. protected:
  75. std::string ABI;
  76. public:
  77. PPCTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
  78. : TargetInfo(Triple) {
  79. SuitableAlign = 128;
  80. SimdDefaultAlign = 128;
  81. LongDoubleWidth = LongDoubleAlign = 128;
  82. LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble();
  83. HasStrictFP = true;
  84. HasIbm128 = true;
  85. }
  86. // Set the language option for altivec based on our value.
  87. void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override;
  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, e300c2, e300c3, e500mc64, e6500, 860, cell, titan, rs64.
  91. bool isValidCPUName(StringRef Name) const override;
  92. void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
  93. bool setCPU(const std::string &Name) override {
  94. bool CPUKnown = isValidCPUName(Name);
  95. if (CPUKnown) {
  96. CPU = Name;
  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. .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 | ArchDefinePwr6 | ArchDefinePwr5x |
  135. ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
  136. ArchDefinePpcsq)
  137. // powerpc64le automatically defaults to at least power8.
  138. .Cases("power8", "pwr8", "ppc64le",
  139. ArchDefinePwr8 | ArchDefinePwr7 | ArchDefinePwr6 |
  140. ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |
  141. ArchDefinePpcgr | ArchDefinePpcsq)
  142. .Cases("power9", "pwr9",
  143. ArchDefinePwr9 | ArchDefinePwr8 | ArchDefinePwr7 |
  144. ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5 |
  145. ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
  146. .Cases("power10", "pwr10",
  147. ArchDefinePwr10 | ArchDefinePwr9 | ArchDefinePwr8 |
  148. ArchDefinePwr7 | ArchDefinePwr6 | ArchDefinePwr5x |
  149. ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr |
  150. ArchDefinePpcsq)
  151. .Case("future",
  152. ArchDefineFuture | ArchDefinePwr10 | ArchDefinePwr9 |
  153. ArchDefinePwr8 | ArchDefinePwr7 | ArchDefinePwr6 |
  154. ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4 |
  155. ArchDefinePpcgr | ArchDefinePpcsq)
  156. .Cases("8548", "e500", ArchDefineE500)
  157. .Default(ArchDefineNone);
  158. }
  159. return CPUKnown;
  160. }
  161. StringRef getABI() const override { return ABI; }
  162. ArrayRef<Builtin::Info> getTargetBuiltins() const override;
  163. bool isCLZForZeroUndef() const override { return false; }
  164. void getTargetDefines(const LangOptions &Opts,
  165. MacroBuilder &Builder) const override;
  166. bool
  167. initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
  168. StringRef CPU,
  169. const std::vector<std::string> &FeaturesVec) const override;
  170. void addP10SpecificFeatures(llvm::StringMap<bool> &Features) const;
  171. void addFutureSpecificFeatures(llvm::StringMap<bool> &Features) const;
  172. bool handleTargetFeatures(std::vector<std::string> &Features,
  173. DiagnosticsEngine &Diags) override;
  174. bool hasFeature(StringRef Feature) const override;
  175. void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,
  176. bool Enabled) const override;
  177. ArrayRef<const char *> getGCCRegNames() const override;
  178. ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
  179. ArrayRef<TargetInfo::AddlRegName> getGCCAddlRegNames() const override;
  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. Info.setAllowsRegister();
  270. LLVM_FALLTHROUGH;
  271. case 'Z': // Memory operand that is an indexed or indirect from a
  272. // register (it is usually better to use `m' or `es' in
  273. // asm statements)
  274. Info.setAllowsMemory();
  275. break;
  276. case 'R': // AIX TOC entry
  277. case 'a': // Address operand that is an indexed or indirect from a
  278. // register (`p' is preferable for asm statements)
  279. case 'S': // Constant suitable as a 64-bit mask operand
  280. case 'T': // Constant suitable as a 32-bit mask operand
  281. case 'U': // System V Release 4 small data area reference
  282. case 't': // AND masks that can be performed by two rldic{l, r}
  283. // instructions
  284. case 'W': // Vector constant that does not require memory
  285. case 'j': // Vector constant that is all zeros.
  286. break;
  287. // End FIXME.
  288. }
  289. return true;
  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. const char *getClobbers() const override { return ""; }
  306. int getEHDataRegisterNumber(unsigned RegNo) const override {
  307. if (RegNo == 0)
  308. return 3;
  309. if (RegNo == 1)
  310. return 4;
  311. return -1;
  312. }
  313. bool hasSjLjLowering() const override { return true; }
  314. const char *getLongDoubleMangling() const override {
  315. if (LongDoubleWidth == 64)
  316. return "e";
  317. return LongDoubleFormat == &llvm::APFloat::PPCDoubleDouble()
  318. ? "g"
  319. : "u9__ieee128";
  320. }
  321. const char *getFloat128Mangling() const override { return "u9__ieee128"; }
  322. const char *getIbm128Mangling() const override { return "g"; }
  323. bool hasBitIntType() const override { return true; }
  324. bool isSPRegName(StringRef RegName) const override {
  325. return RegName.equals("r1") || RegName.equals("x1");
  326. }
  327. };
  328. class LLVM_LIBRARY_VISIBILITY PPC32TargetInfo : public PPCTargetInfo {
  329. public:
  330. PPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  331. : PPCTargetInfo(Triple, Opts) {
  332. if (Triple.isOSAIX())
  333. resetDataLayout("E-m:a-p:32:32-i64:64-n32");
  334. else if (Triple.getArch() == llvm::Triple::ppcle)
  335. resetDataLayout("e-m:e-p:32:32-i64:64-n32");
  336. else
  337. resetDataLayout("E-m:e-p:32:32-i64:64-n32");
  338. switch (getTriple().getOS()) {
  339. case llvm::Triple::Linux:
  340. case llvm::Triple::FreeBSD:
  341. case llvm::Triple::NetBSD:
  342. SizeType = UnsignedInt;
  343. PtrDiffType = SignedInt;
  344. IntPtrType = SignedInt;
  345. break;
  346. case llvm::Triple::AIX:
  347. SizeType = UnsignedLong;
  348. PtrDiffType = SignedLong;
  349. IntPtrType = SignedLong;
  350. LongDoubleWidth = 64;
  351. LongDoubleAlign = DoubleAlign = 32;
  352. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  353. break;
  354. default:
  355. break;
  356. }
  357. if (Triple.isOSFreeBSD() || Triple.isOSNetBSD() || Triple.isOSOpenBSD() ||
  358. Triple.isMusl()) {
  359. LongDoubleWidth = LongDoubleAlign = 64;
  360. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  361. }
  362. // PPC32 supports atomics up to 4 bytes.
  363. MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
  364. }
  365. BuiltinVaListKind getBuiltinVaListKind() const override {
  366. // This is the ELF definition, and is overridden by the Darwin sub-target
  367. return TargetInfo::PowerABIBuiltinVaList;
  368. }
  369. };
  370. // Note: ABI differences may eventually require us to have a separate
  371. // TargetInfo for little endian.
  372. class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
  373. public:
  374. PPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  375. : PPCTargetInfo(Triple, Opts) {
  376. LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
  377. IntMaxType = SignedLong;
  378. Int64Type = SignedLong;
  379. std::string DataLayout;
  380. if (Triple.isOSAIX()) {
  381. // TODO: Set appropriate ABI for AIX platform.
  382. DataLayout = "E-m:a-i64:64-n32:64";
  383. LongDoubleWidth = 64;
  384. LongDoubleAlign = DoubleAlign = 32;
  385. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  386. } else if ((Triple.getArch() == llvm::Triple::ppc64le)) {
  387. DataLayout = "e-m:e-i64:64-n32:64";
  388. ABI = "elfv2";
  389. } else {
  390. DataLayout = "E-m:e-i64:64-n32:64";
  391. ABI = "elfv1";
  392. }
  393. if (Triple.isOSFreeBSD() || Triple.isOSOpenBSD() || Triple.isMusl()) {
  394. LongDoubleWidth = LongDoubleAlign = 64;
  395. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  396. }
  397. if (Triple.isOSAIX() || Triple.isOSLinux())
  398. DataLayout += "-S128-v256:256:256-v512:512:512";
  399. resetDataLayout(DataLayout);
  400. // PPC64 supports atomics up to 8 bytes.
  401. MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
  402. }
  403. BuiltinVaListKind getBuiltinVaListKind() const override {
  404. return TargetInfo::CharPtrBuiltinVaList;
  405. }
  406. // PPC64 Linux-specific ABI options.
  407. bool setABI(const std::string &Name) override {
  408. if (Name == "elfv1" || Name == "elfv2") {
  409. ABI = Name;
  410. return true;
  411. }
  412. return false;
  413. }
  414. CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
  415. switch (CC) {
  416. case CC_Swift:
  417. return CCCR_OK;
  418. case CC_SwiftAsync:
  419. return CCCR_Error;
  420. default:
  421. return CCCR_Warning;
  422. }
  423. }
  424. };
  425. class LLVM_LIBRARY_VISIBILITY DarwinPPC32TargetInfo
  426. : public DarwinTargetInfo<PPC32TargetInfo> {
  427. public:
  428. DarwinPPC32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  429. : DarwinTargetInfo<PPC32TargetInfo>(Triple, Opts) {
  430. HasAlignMac68kSupport = true;
  431. BoolWidth = BoolAlign = 32; // XXX support -mone-byte-bool?
  432. PtrDiffType = SignedInt; // for http://llvm.org/bugs/show_bug.cgi?id=15726
  433. LongLongAlign = 32;
  434. resetDataLayout("E-m:o-p:32:32-f64:32:64-n32", "_");
  435. }
  436. BuiltinVaListKind getBuiltinVaListKind() const override {
  437. return TargetInfo::CharPtrBuiltinVaList;
  438. }
  439. };
  440. class LLVM_LIBRARY_VISIBILITY DarwinPPC64TargetInfo
  441. : public DarwinTargetInfo<PPC64TargetInfo> {
  442. public:
  443. DarwinPPC64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
  444. : DarwinTargetInfo<PPC64TargetInfo>(Triple, Opts) {
  445. HasAlignMac68kSupport = true;
  446. resetDataLayout("E-m:o-i64:64-n32:64", "_");
  447. }
  448. };
  449. class LLVM_LIBRARY_VISIBILITY AIXPPC32TargetInfo :
  450. public AIXTargetInfo<PPC32TargetInfo> {
  451. public:
  452. using AIXTargetInfo::AIXTargetInfo;
  453. BuiltinVaListKind getBuiltinVaListKind() const override {
  454. return TargetInfo::CharPtrBuiltinVaList;
  455. }
  456. };
  457. class LLVM_LIBRARY_VISIBILITY AIXPPC64TargetInfo :
  458. public AIXTargetInfo<PPC64TargetInfo> {
  459. public:
  460. using AIXTargetInfo::AIXTargetInfo;
  461. };
  462. } // namespace targets
  463. } // namespace clang
  464. #endif // LLVM_CLANG_LIB_BASIC_TARGETS_PPC_H