PPC.h 17 KB

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