PPC.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. //===--- PPC.cpp - Implement PPC target feature support -------------------===//
  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 implements PPC TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "PPC.h"
  13. #include "clang/Basic/Diagnostic.h"
  14. #include "clang/Basic/MacroBuilder.h"
  15. #include "clang/Basic/TargetBuiltins.h"
  16. using namespace clang;
  17. using namespace clang::targets;
  18. const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
  19. #define BUILTIN(ID, TYPE, ATTRS) \
  20. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
  21. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
  22. {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
  23. #include "clang/Basic/BuiltinsPPC.def"
  24. };
  25. /// handleTargetFeatures - Perform initialization based on the user
  26. /// configured set of features.
  27. bool PPCTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
  28. DiagnosticsEngine &Diags) {
  29. FloatABI = HardFloat;
  30. for (const auto &Feature : Features) {
  31. if (Feature == "+altivec") {
  32. HasAltivec = true;
  33. } else if (Feature == "+vsx") {
  34. HasVSX = true;
  35. } else if (Feature == "+bpermd") {
  36. HasBPERMD = true;
  37. } else if (Feature == "+extdiv") {
  38. HasExtDiv = true;
  39. } else if (Feature == "+power8-vector") {
  40. HasP8Vector = true;
  41. } else if (Feature == "+crypto") {
  42. HasP8Crypto = true;
  43. } else if (Feature == "+direct-move") {
  44. HasDirectMove = true;
  45. } else if (Feature == "+htm") {
  46. HasHTM = true;
  47. } else if (Feature == "+float128") {
  48. HasFloat128 = true;
  49. } else if (Feature == "+power9-vector") {
  50. HasP9Vector = true;
  51. } else if (Feature == "+power10-vector") {
  52. HasP10Vector = true;
  53. } else if (Feature == "+pcrelative-memops") {
  54. HasPCRelativeMemops = true;
  55. } else if (Feature == "+prefix-instrs") {
  56. HasPrefixInstrs = true;
  57. } else if (Feature == "+spe" || Feature == "+efpu2") {
  58. HasStrictFP = false;
  59. HasSPE = true;
  60. LongDoubleWidth = LongDoubleAlign = 64;
  61. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  62. } else if (Feature == "-hard-float") {
  63. FloatABI = SoftFloat;
  64. } else if (Feature == "+paired-vector-memops") {
  65. PairedVectorMemops = true;
  66. } else if (Feature == "+mma") {
  67. HasMMA = true;
  68. } else if (Feature == "+rop-protect") {
  69. HasROPProtect = true;
  70. } else if (Feature == "+privileged") {
  71. HasPrivileged = true;
  72. } else if (Feature == "+isa-v206-instructions") {
  73. IsISA2_06 = true;
  74. } else if (Feature == "+isa-v207-instructions") {
  75. IsISA2_07 = true;
  76. } else if (Feature == "+isa-v30-instructions") {
  77. IsISA3_0 = true;
  78. } else if (Feature == "+isa-v31-instructions") {
  79. IsISA3_1 = true;
  80. }
  81. // TODO: Finish this list and add an assert that we've handled them
  82. // all.
  83. }
  84. return true;
  85. }
  86. static void defineXLCompatMacros(MacroBuilder &Builder) {
  87. Builder.defineMacro("__popcntb", "__builtin_ppc_popcntb");
  88. Builder.defineMacro("__poppar4", "__builtin_ppc_poppar4");
  89. Builder.defineMacro("__poppar8", "__builtin_ppc_poppar8");
  90. Builder.defineMacro("__eieio", "__builtin_ppc_eieio");
  91. Builder.defineMacro("__iospace_eieio", "__builtin_ppc_iospace_eieio");
  92. Builder.defineMacro("__isync", "__builtin_ppc_isync");
  93. Builder.defineMacro("__lwsync", "__builtin_ppc_lwsync");
  94. Builder.defineMacro("__iospace_lwsync", "__builtin_ppc_iospace_lwsync");
  95. Builder.defineMacro("__sync", "__builtin_ppc_sync");
  96. Builder.defineMacro("__iospace_sync", "__builtin_ppc_iospace_sync");
  97. Builder.defineMacro("__dcbfl", "__builtin_ppc_dcbfl");
  98. Builder.defineMacro("__dcbflp", "__builtin_ppc_dcbflp");
  99. Builder.defineMacro("__dcbst", "__builtin_ppc_dcbst");
  100. Builder.defineMacro("__dcbt", "__builtin_ppc_dcbt");
  101. Builder.defineMacro("__dcbtst", "__builtin_ppc_dcbtst");
  102. Builder.defineMacro("__dcbz", "__builtin_ppc_dcbz");
  103. Builder.defineMacro("__icbt", "__builtin_ppc_icbt");
  104. Builder.defineMacro("__compare_and_swap", "__builtin_ppc_compare_and_swap");
  105. Builder.defineMacro("__compare_and_swaplp",
  106. "__builtin_ppc_compare_and_swaplp");
  107. Builder.defineMacro("__fetch_and_add", "__builtin_ppc_fetch_and_add");
  108. Builder.defineMacro("__fetch_and_addlp", "__builtin_ppc_fetch_and_addlp");
  109. Builder.defineMacro("__fetch_and_and", "__builtin_ppc_fetch_and_and");
  110. Builder.defineMacro("__fetch_and_andlp", "__builtin_ppc_fetch_and_andlp");
  111. Builder.defineMacro("__fetch_and_or", "__builtin_ppc_fetch_and_or");
  112. Builder.defineMacro("__fetch_and_orlp", "__builtin_ppc_fetch_and_orlp");
  113. Builder.defineMacro("__fetch_and_swap", "__builtin_ppc_fetch_and_swap");
  114. Builder.defineMacro("__fetch_and_swaplp", "__builtin_ppc_fetch_and_swaplp");
  115. Builder.defineMacro("__ldarx", "__builtin_ppc_ldarx");
  116. Builder.defineMacro("__lwarx", "__builtin_ppc_lwarx");
  117. Builder.defineMacro("__lharx", "__builtin_ppc_lharx");
  118. Builder.defineMacro("__lbarx", "__builtin_ppc_lbarx");
  119. Builder.defineMacro("__stfiw", "__builtin_ppc_stfiw");
  120. Builder.defineMacro("__stdcx", "__builtin_ppc_stdcx");
  121. Builder.defineMacro("__stwcx", "__builtin_ppc_stwcx");
  122. Builder.defineMacro("__sthcx", "__builtin_ppc_sthcx");
  123. Builder.defineMacro("__stbcx", "__builtin_ppc_stbcx");
  124. Builder.defineMacro("__tdw", "__builtin_ppc_tdw");
  125. Builder.defineMacro("__tw", "__builtin_ppc_tw");
  126. Builder.defineMacro("__trap", "__builtin_ppc_trap");
  127. Builder.defineMacro("__trapd", "__builtin_ppc_trapd");
  128. Builder.defineMacro("__fcfid", "__builtin_ppc_fcfid");
  129. Builder.defineMacro("__fcfud", "__builtin_ppc_fcfud");
  130. Builder.defineMacro("__fctid", "__builtin_ppc_fctid");
  131. Builder.defineMacro("__fctidz", "__builtin_ppc_fctidz");
  132. Builder.defineMacro("__fctiw", "__builtin_ppc_fctiw");
  133. Builder.defineMacro("__fctiwz", "__builtin_ppc_fctiwz");
  134. Builder.defineMacro("__fctudz", "__builtin_ppc_fctudz");
  135. Builder.defineMacro("__fctuwz", "__builtin_ppc_fctuwz");
  136. Builder.defineMacro("__cmpeqb", "__builtin_ppc_cmpeqb");
  137. Builder.defineMacro("__cmprb", "__builtin_ppc_cmprb");
  138. Builder.defineMacro("__setb", "__builtin_ppc_setb");
  139. Builder.defineMacro("__cmpb", "__builtin_ppc_cmpb");
  140. Builder.defineMacro("__mulhd", "__builtin_ppc_mulhd");
  141. Builder.defineMacro("__mulhdu", "__builtin_ppc_mulhdu");
  142. Builder.defineMacro("__mulhw", "__builtin_ppc_mulhw");
  143. Builder.defineMacro("__mulhwu", "__builtin_ppc_mulhwu");
  144. Builder.defineMacro("__maddhd", "__builtin_ppc_maddhd");
  145. Builder.defineMacro("__maddhdu", "__builtin_ppc_maddhdu");
  146. Builder.defineMacro("__maddld", "__builtin_ppc_maddld");
  147. Builder.defineMacro("__rlwnm", "__builtin_ppc_rlwnm");
  148. Builder.defineMacro("__rlwimi", "__builtin_ppc_rlwimi");
  149. Builder.defineMacro("__rldimi", "__builtin_ppc_rldimi");
  150. Builder.defineMacro("__load2r", "__builtin_ppc_load2r");
  151. Builder.defineMacro("__load4r", "__builtin_ppc_load4r");
  152. Builder.defineMacro("__load8r", "__builtin_ppc_load8r");
  153. Builder.defineMacro("__store2r", "__builtin_ppc_store2r");
  154. Builder.defineMacro("__store4r", "__builtin_ppc_store4r");
  155. Builder.defineMacro("__store8r", "__builtin_ppc_store8r");
  156. Builder.defineMacro("__extract_exp", "__builtin_ppc_extract_exp");
  157. Builder.defineMacro("__extract_sig", "__builtin_ppc_extract_sig");
  158. Builder.defineMacro("__mtfsb0", "__builtin_ppc_mtfsb0");
  159. Builder.defineMacro("__mtfsb1", "__builtin_ppc_mtfsb1");
  160. Builder.defineMacro("__mtfsf", "__builtin_ppc_mtfsf");
  161. Builder.defineMacro("__mtfsfi", "__builtin_ppc_mtfsfi");
  162. Builder.defineMacro("__insert_exp", "__builtin_ppc_insert_exp");
  163. Builder.defineMacro("__fmsub", "__builtin_ppc_fmsub");
  164. Builder.defineMacro("__fmsubs", "__builtin_ppc_fmsubs");
  165. Builder.defineMacro("__fnmadd", "__builtin_ppc_fnmadd");
  166. Builder.defineMacro("__fnmadds", "__builtin_ppc_fnmadds");
  167. Builder.defineMacro("__fnmsub", "__builtin_ppc_fnmsub");
  168. Builder.defineMacro("__fnmsubs", "__builtin_ppc_fnmsubs");
  169. Builder.defineMacro("__fre", "__builtin_ppc_fre");
  170. Builder.defineMacro("__fres", "__builtin_ppc_fres");
  171. Builder.defineMacro("__swdiv_nochk", "__builtin_ppc_swdiv_nochk");
  172. Builder.defineMacro("__swdivs_nochk", "__builtin_ppc_swdivs_nochk");
  173. Builder.defineMacro("__alloca", "__builtin_alloca");
  174. Builder.defineMacro("__vcipher", "__builtin_altivec_crypto_vcipher");
  175. Builder.defineMacro("__vcipherlast", "__builtin_altivec_crypto_vcipherlast");
  176. Builder.defineMacro("__vncipher", "__builtin_altivec_crypto_vncipher");
  177. Builder.defineMacro("__vncipherlast",
  178. "__builtin_altivec_crypto_vncipherlast");
  179. Builder.defineMacro("__vpermxor", "__builtin_altivec_crypto_vpermxor");
  180. Builder.defineMacro("__vpmsumb", "__builtin_altivec_crypto_vpmsumb");
  181. Builder.defineMacro("__vpmsumd", "__builtin_altivec_crypto_vpmsumd");
  182. Builder.defineMacro("__vpmsumh", "__builtin_altivec_crypto_vpmsumh");
  183. Builder.defineMacro("__vpmsumw", "__builtin_altivec_crypto_vpmsumw");
  184. Builder.defineMacro("__divde", "__builtin_divde");
  185. Builder.defineMacro("__divwe", "__builtin_divwe");
  186. Builder.defineMacro("__divdeu", "__builtin_divdeu");
  187. Builder.defineMacro("__divweu", "__builtin_divweu");
  188. Builder.defineMacro("__alignx", "__builtin_ppc_alignx");
  189. Builder.defineMacro("__bcopy", "bcopy");
  190. Builder.defineMacro("__bpermd", "__builtin_bpermd");
  191. Builder.defineMacro("__cntlz4", "__builtin_clz");
  192. Builder.defineMacro("__cntlz8", "__builtin_clzll");
  193. Builder.defineMacro("__cmplx", "__builtin_complex");
  194. Builder.defineMacro("__cmplxf", "__builtin_complex");
  195. Builder.defineMacro("__cnttz4", "__builtin_ctz");
  196. Builder.defineMacro("__cnttz8", "__builtin_ctzll");
  197. Builder.defineMacro("__darn", "__builtin_darn");
  198. Builder.defineMacro("__darn_32", "__builtin_darn_32");
  199. Builder.defineMacro("__darn_raw", "__builtin_darn_raw");
  200. Builder.defineMacro("__dcbf", "__builtin_dcbf");
  201. Builder.defineMacro("__fmadd", "__builtin_fma");
  202. Builder.defineMacro("__fmadds", "__builtin_fmaf");
  203. Builder.defineMacro("__labs", "__builtin_labs");
  204. Builder.defineMacro("__llabs", "__builtin_llabs");
  205. Builder.defineMacro("__popcnt4", "__builtin_popcount");
  206. Builder.defineMacro("__popcnt8", "__builtin_popcountll");
  207. Builder.defineMacro("__readflm", "__builtin_readflm");
  208. Builder.defineMacro("__rotatel4", "__builtin_rotateleft32");
  209. Builder.defineMacro("__rotatel8", "__builtin_rotateleft64");
  210. Builder.defineMacro("__rdlam", "__builtin_ppc_rdlam");
  211. Builder.defineMacro("__setflm", "__builtin_setflm");
  212. Builder.defineMacro("__setrnd", "__builtin_setrnd");
  213. Builder.defineMacro("__dcbtstt", "__builtin_ppc_dcbtstt");
  214. Builder.defineMacro("__dcbtt", "__builtin_ppc_dcbtt");
  215. Builder.defineMacro("__mftbu", "__builtin_ppc_mftbu");
  216. Builder.defineMacro("__mfmsr", "__builtin_ppc_mfmsr");
  217. Builder.defineMacro("__mtmsr", "__builtin_ppc_mtmsr");
  218. Builder.defineMacro("__mfspr", "__builtin_ppc_mfspr");
  219. Builder.defineMacro("__mtspr", "__builtin_ppc_mtspr");
  220. Builder.defineMacro("__fric", "__builtin_ppc_fric");
  221. Builder.defineMacro("__frim", "__builtin_ppc_frim");
  222. Builder.defineMacro("__frims", "__builtin_ppc_frims");
  223. Builder.defineMacro("__frin", "__builtin_ppc_frin");
  224. Builder.defineMacro("__frins", "__builtin_ppc_frins");
  225. Builder.defineMacro("__frip", "__builtin_ppc_frip");
  226. Builder.defineMacro("__frips", "__builtin_ppc_frips");
  227. Builder.defineMacro("__friz", "__builtin_ppc_friz");
  228. Builder.defineMacro("__frizs", "__builtin_ppc_frizs");
  229. Builder.defineMacro("__fsel", "__builtin_ppc_fsel");
  230. Builder.defineMacro("__fsels", "__builtin_ppc_fsels");
  231. Builder.defineMacro("__frsqrte", "__builtin_ppc_frsqrte");
  232. Builder.defineMacro("__frsqrtes", "__builtin_ppc_frsqrtes");
  233. Builder.defineMacro("__fsqrt", "__builtin_ppc_fsqrt");
  234. Builder.defineMacro("__fsqrts", "__builtin_ppc_fsqrts");
  235. Builder.defineMacro("__addex", "__builtin_ppc_addex");
  236. Builder.defineMacro("__cmplxl", "__builtin_complex");
  237. Builder.defineMacro("__compare_exp_uo", "__builtin_ppc_compare_exp_uo");
  238. Builder.defineMacro("__compare_exp_lt", "__builtin_ppc_compare_exp_lt");
  239. Builder.defineMacro("__compare_exp_gt", "__builtin_ppc_compare_exp_gt");
  240. Builder.defineMacro("__compare_exp_eq", "__builtin_ppc_compare_exp_eq");
  241. Builder.defineMacro("__test_data_class", "__builtin_ppc_test_data_class");
  242. Builder.defineMacro("__swdiv", "__builtin_ppc_swdiv");
  243. Builder.defineMacro("__swdivs", "__builtin_ppc_swdivs");
  244. }
  245. /// PPCTargetInfo::getTargetDefines - Return a set of the PowerPC-specific
  246. /// #defines that are not tied to a specific subtarget.
  247. void PPCTargetInfo::getTargetDefines(const LangOptions &Opts,
  248. MacroBuilder &Builder) const {
  249. // We define the XLC compatibility macros only on AIX and Linux since XLC
  250. // was never available on any other platforms.
  251. if (getTriple().isOSAIX() || getTriple().isOSLinux())
  252. defineXLCompatMacros(Builder);
  253. // Target identification.
  254. Builder.defineMacro("__ppc__");
  255. Builder.defineMacro("__PPC__");
  256. Builder.defineMacro("_ARCH_PPC");
  257. Builder.defineMacro("__powerpc__");
  258. Builder.defineMacro("__POWERPC__");
  259. if (PointerWidth == 64) {
  260. Builder.defineMacro("_ARCH_PPC64");
  261. Builder.defineMacro("__powerpc64__");
  262. Builder.defineMacro("__ppc64__");
  263. Builder.defineMacro("__PPC64__");
  264. } else if (getTriple().isOSAIX()) {
  265. // The XL compilers on AIX define _ARCH_PPC64 for both 32 and 64-bit modes.
  266. Builder.defineMacro("_ARCH_PPC64");
  267. }
  268. if (getTriple().isOSAIX()) {
  269. Builder.defineMacro("__THW_PPC__");
  270. // Define __PPC and __powerpc for AIX XL C/C++ compatibility
  271. Builder.defineMacro("__PPC");
  272. Builder.defineMacro("__powerpc");
  273. }
  274. // Target properties.
  275. if (getTriple().getArch() == llvm::Triple::ppc64le ||
  276. getTriple().getArch() == llvm::Triple::ppcle) {
  277. Builder.defineMacro("_LITTLE_ENDIAN");
  278. } else {
  279. if (!getTriple().isOSNetBSD() &&
  280. !getTriple().isOSOpenBSD())
  281. Builder.defineMacro("_BIG_ENDIAN");
  282. }
  283. // ABI options.
  284. if (ABI == "elfv1")
  285. Builder.defineMacro("_CALL_ELF", "1");
  286. if (ABI == "elfv2")
  287. Builder.defineMacro("_CALL_ELF", "2");
  288. // This typically is only for a new enough linker (bfd >= 2.16.2 or gold), but
  289. // our support post-dates this and it should work on all 64-bit ppc linux
  290. // platforms. It is guaranteed to work on all elfv2 platforms.
  291. if (getTriple().getOS() == llvm::Triple::Linux && PointerWidth == 64)
  292. Builder.defineMacro("_CALL_LINUX", "1");
  293. // Subtarget options.
  294. if (!getTriple().isOSAIX()){
  295. Builder.defineMacro("__NATURAL_ALIGNMENT__");
  296. }
  297. Builder.defineMacro("__REGISTER_PREFIX__", "");
  298. // FIXME: Should be controlled by command line option.
  299. if (LongDoubleWidth == 128) {
  300. Builder.defineMacro("__LONG_DOUBLE_128__");
  301. Builder.defineMacro("__LONGDOUBLE128");
  302. if (Opts.PPCIEEELongDouble)
  303. Builder.defineMacro("__LONG_DOUBLE_IEEE128__");
  304. else
  305. Builder.defineMacro("__LONG_DOUBLE_IBM128__");
  306. }
  307. if (getTriple().isOSAIX() && Opts.LongDoubleSize == 64) {
  308. assert(LongDoubleWidth == 64);
  309. Builder.defineMacro("__LONGDOUBLE64");
  310. }
  311. // Define this for elfv2 (64-bit only) or 64-bit darwin.
  312. if (ABI == "elfv2" ||
  313. (getTriple().getOS() == llvm::Triple::Darwin && PointerWidth == 64))
  314. Builder.defineMacro("__STRUCT_PARM_ALIGN__", "16");
  315. if (ArchDefs & ArchDefineName)
  316. Builder.defineMacro(Twine("_ARCH_", StringRef(CPU).upper()));
  317. if (ArchDefs & ArchDefinePpcgr)
  318. Builder.defineMacro("_ARCH_PPCGR");
  319. if (ArchDefs & ArchDefinePpcsq)
  320. Builder.defineMacro("_ARCH_PPCSQ");
  321. if (ArchDefs & ArchDefine440)
  322. Builder.defineMacro("_ARCH_440");
  323. if (ArchDefs & ArchDefine603)
  324. Builder.defineMacro("_ARCH_603");
  325. if (ArchDefs & ArchDefine604)
  326. Builder.defineMacro("_ARCH_604");
  327. if (ArchDefs & ArchDefinePwr4)
  328. Builder.defineMacro("_ARCH_PWR4");
  329. if (ArchDefs & ArchDefinePwr5)
  330. Builder.defineMacro("_ARCH_PWR5");
  331. if (ArchDefs & ArchDefinePwr5x)
  332. Builder.defineMacro("_ARCH_PWR5X");
  333. if (ArchDefs & ArchDefinePwr6)
  334. Builder.defineMacro("_ARCH_PWR6");
  335. if (ArchDefs & ArchDefinePwr6x)
  336. Builder.defineMacro("_ARCH_PWR6X");
  337. if (ArchDefs & ArchDefinePwr7)
  338. Builder.defineMacro("_ARCH_PWR7");
  339. if (ArchDefs & ArchDefinePwr8)
  340. Builder.defineMacro("_ARCH_PWR8");
  341. if (ArchDefs & ArchDefinePwr9)
  342. Builder.defineMacro("_ARCH_PWR9");
  343. if (ArchDefs & ArchDefinePwr10)
  344. Builder.defineMacro("_ARCH_PWR10");
  345. if (ArchDefs & ArchDefineA2)
  346. Builder.defineMacro("_ARCH_A2");
  347. if (ArchDefs & ArchDefineE500)
  348. Builder.defineMacro("__NO_LWSYNC__");
  349. if (ArchDefs & ArchDefineFuture)
  350. Builder.defineMacro("_ARCH_PWR_FUTURE");
  351. if (HasAltivec) {
  352. Builder.defineMacro("__VEC__", "10206");
  353. Builder.defineMacro("__ALTIVEC__");
  354. }
  355. if (HasSPE) {
  356. Builder.defineMacro("__SPE__");
  357. Builder.defineMacro("__NO_FPRS__");
  358. }
  359. if (HasVSX)
  360. Builder.defineMacro("__VSX__");
  361. if (HasP8Vector)
  362. Builder.defineMacro("__POWER8_VECTOR__");
  363. if (HasP8Crypto)
  364. Builder.defineMacro("__CRYPTO__");
  365. if (HasHTM)
  366. Builder.defineMacro("__HTM__");
  367. if (HasFloat128)
  368. Builder.defineMacro("__FLOAT128__");
  369. if (HasP9Vector)
  370. Builder.defineMacro("__POWER9_VECTOR__");
  371. if (HasMMA)
  372. Builder.defineMacro("__MMA__");
  373. if (HasROPProtect)
  374. Builder.defineMacro("__ROP_PROTECT__");
  375. if (HasP10Vector)
  376. Builder.defineMacro("__POWER10_VECTOR__");
  377. if (HasPCRelativeMemops)
  378. Builder.defineMacro("__PCREL__");
  379. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
  380. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
  381. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
  382. if (PointerWidth == 64)
  383. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
  384. // We have support for the bswap intrinsics so we can define this.
  385. Builder.defineMacro("__HAVE_BSWAP__", "1");
  386. // FIXME: The following are not yet generated here by Clang, but are
  387. // generated by GCC:
  388. //
  389. // _SOFT_FLOAT_
  390. // __RECIP_PRECISION__
  391. // __APPLE_ALTIVEC__
  392. // __RECIP__
  393. // __RECIPF__
  394. // __RSQRTE__
  395. // __RSQRTEF__
  396. // _SOFT_DOUBLE_
  397. // __NO_LWSYNC__
  398. // __CMODEL_MEDIUM__
  399. // __CMODEL_LARGE__
  400. // _CALL_SYSV
  401. // _CALL_DARWIN
  402. }
  403. // Handle explicit options being passed to the compiler here: if we've
  404. // explicitly turned off vsx and turned on any of:
  405. // - power8-vector
  406. // - direct-move
  407. // - float128
  408. // - power9-vector
  409. // - paired-vector-memops
  410. // - mma
  411. // - power10-vector
  412. // then go ahead and error since the customer has expressed an incompatible
  413. // set of options.
  414. static bool ppcUserFeaturesCheck(DiagnosticsEngine &Diags,
  415. const std::vector<std::string> &FeaturesVec) {
  416. // vsx was not explicitly turned off.
  417. if (!llvm::is_contained(FeaturesVec, "-vsx"))
  418. return true;
  419. auto FindVSXSubfeature = [&](StringRef Feature, StringRef Option) {
  420. if (llvm::is_contained(FeaturesVec, Feature)) {
  421. Diags.Report(diag::err_opt_not_valid_with_opt) << Option << "-mno-vsx";
  422. return true;
  423. }
  424. return false;
  425. };
  426. bool Found = FindVSXSubfeature("+power8-vector", "-mpower8-vector");
  427. Found |= FindVSXSubfeature("+direct-move", "-mdirect-move");
  428. Found |= FindVSXSubfeature("+float128", "-mfloat128");
  429. Found |= FindVSXSubfeature("+power9-vector", "-mpower9-vector");
  430. Found |= FindVSXSubfeature("+paired-vector-memops", "-mpaired-vector-memops");
  431. Found |= FindVSXSubfeature("+mma", "-mmma");
  432. Found |= FindVSXSubfeature("+power10-vector", "-mpower10-vector");
  433. // Return false if any vsx subfeatures was found.
  434. return !Found;
  435. }
  436. bool PPCTargetInfo::initFeatureMap(
  437. llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
  438. const std::vector<std::string> &FeaturesVec) const {
  439. Features["altivec"] = llvm::StringSwitch<bool>(CPU)
  440. .Case("7400", true)
  441. .Case("g4", true)
  442. .Case("7450", true)
  443. .Case("g4+", true)
  444. .Case("970", true)
  445. .Case("g5", true)
  446. .Case("pwr6", true)
  447. .Case("pwr7", true)
  448. .Case("pwr8", true)
  449. .Case("pwr9", true)
  450. .Case("ppc64", true)
  451. .Case("ppc64le", true)
  452. .Default(false);
  453. Features["power9-vector"] = (CPU == "pwr9");
  454. Features["crypto"] = llvm::StringSwitch<bool>(CPU)
  455. .Case("ppc64le", true)
  456. .Case("pwr9", true)
  457. .Case("pwr8", true)
  458. .Default(false);
  459. Features["power8-vector"] = llvm::StringSwitch<bool>(CPU)
  460. .Case("ppc64le", true)
  461. .Case("pwr9", true)
  462. .Case("pwr8", true)
  463. .Default(false);
  464. Features["bpermd"] = llvm::StringSwitch<bool>(CPU)
  465. .Case("ppc64le", true)
  466. .Case("pwr9", true)
  467. .Case("pwr8", true)
  468. .Case("pwr7", true)
  469. .Default(false);
  470. Features["extdiv"] = llvm::StringSwitch<bool>(CPU)
  471. .Case("ppc64le", true)
  472. .Case("pwr9", true)
  473. .Case("pwr8", true)
  474. .Case("pwr7", true)
  475. .Default(false);
  476. Features["direct-move"] = llvm::StringSwitch<bool>(CPU)
  477. .Case("ppc64le", true)
  478. .Case("pwr9", true)
  479. .Case("pwr8", true)
  480. .Default(false);
  481. Features["vsx"] = llvm::StringSwitch<bool>(CPU)
  482. .Case("ppc64le", true)
  483. .Case("pwr9", true)
  484. .Case("pwr8", true)
  485. .Case("pwr7", true)
  486. .Default(false);
  487. Features["htm"] = llvm::StringSwitch<bool>(CPU)
  488. .Case("ppc64le", true)
  489. .Case("pwr9", true)
  490. .Case("pwr8", true)
  491. .Default(false);
  492. // ROP Protect is off by default.
  493. Features["rop-protect"] = false;
  494. // Privileged instructions are off by default.
  495. Features["privileged"] = false;
  496. Features["spe"] = llvm::StringSwitch<bool>(CPU)
  497. .Case("8548", true)
  498. .Case("e500", true)
  499. .Default(false);
  500. Features["isa-v206-instructions"] = llvm::StringSwitch<bool>(CPU)
  501. .Case("ppc64le", true)
  502. .Case("pwr9", true)
  503. .Case("pwr8", true)
  504. .Case("pwr7", true)
  505. .Default(false);
  506. Features["isa-v207-instructions"] = llvm::StringSwitch<bool>(CPU)
  507. .Case("ppc64le", true)
  508. .Case("pwr9", true)
  509. .Case("pwr8", true)
  510. .Default(false);
  511. Features["isa-v30-instructions"] =
  512. llvm::StringSwitch<bool>(CPU).Case("pwr9", true).Default(false);
  513. // Power10 includes all the same features as Power9 plus any features specific
  514. // to the Power10 core.
  515. if (CPU == "pwr10" || CPU == "power10") {
  516. initFeatureMap(Features, Diags, "pwr9", FeaturesVec);
  517. addP10SpecificFeatures(Features);
  518. }
  519. // Future CPU should include all of the features of Power 10 as well as any
  520. // additional features (yet to be determined) specific to it.
  521. if (CPU == "future") {
  522. initFeatureMap(Features, Diags, "pwr10", FeaturesVec);
  523. addFutureSpecificFeatures(Features);
  524. }
  525. if (!ppcUserFeaturesCheck(Diags, FeaturesVec))
  526. return false;
  527. if (!(ArchDefs & ArchDefinePwr7) && (ArchDefs & ArchDefinePpcgr) &&
  528. llvm::is_contained(FeaturesVec, "+float128")) {
  529. // We have __float128 on PPC but not pre-VSX targets.
  530. Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfloat128" << CPU;
  531. return false;
  532. }
  533. if (!(ArchDefs & ArchDefinePwr10)) {
  534. if (llvm::find(FeaturesVec, "+mma") != FeaturesVec.end()) {
  535. // MMA operations are not available pre-Power10.
  536. Diags.Report(diag::err_opt_not_valid_with_opt) << "-mmma" << CPU;
  537. return false;
  538. }
  539. if (llvm::find(FeaturesVec, "+pcrel") != FeaturesVec.end()) {
  540. // PC-Relative instructions are not available pre-Power10,
  541. // and these instructions also require prefixed instructions support.
  542. Diags.Report(diag::err_opt_not_valid_without_opt)
  543. << "-mpcrel"
  544. << "-mcpu=pwr10 -mprefixed";
  545. return false;
  546. }
  547. if (llvm::find(FeaturesVec, "+prefixed") != FeaturesVec.end()) {
  548. // Prefixed instructions are not available pre-Power10.
  549. Diags.Report(diag::err_opt_not_valid_without_opt) << "-mprefixed"
  550. << "-mcpu=pwr10";
  551. return false;
  552. }
  553. if (llvm::find(FeaturesVec, "+paired-vector-memops") != FeaturesVec.end()) {
  554. // Paired vector memops are not available pre-Power10.
  555. Diags.Report(diag::err_opt_not_valid_without_opt)
  556. << "-mpaired-vector-memops"
  557. << "-mcpu=pwr10";
  558. return false;
  559. }
  560. }
  561. if (!(ArchDefs & ArchDefinePwr8) &&
  562. llvm::is_contained(FeaturesVec, "+rop-protect")) {
  563. // We can turn on ROP Protect on Power 8 and above.
  564. Diags.Report(diag::err_opt_not_valid_with_opt) << "-mrop-protect" << CPU;
  565. return false;
  566. }
  567. if (!(ArchDefs & ArchDefinePwr8) &&
  568. llvm::is_contained(FeaturesVec, "+privileged")) {
  569. Diags.Report(diag::err_opt_not_valid_with_opt) << "-mprivileged" << CPU;
  570. return false;
  571. }
  572. return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
  573. }
  574. // Add any Power10 specific features.
  575. void PPCTargetInfo::addP10SpecificFeatures(
  576. llvm::StringMap<bool> &Features) const {
  577. Features["htm"] = false; // HTM was removed for P10.
  578. Features["paired-vector-memops"] = true;
  579. Features["mma"] = true;
  580. Features["power10-vector"] = true;
  581. Features["pcrelative-memops"] = true;
  582. Features["prefix-instrs"] = true;
  583. Features["isa-v31-instructions"] = true;
  584. }
  585. // Add features specific to the "Future" CPU.
  586. void PPCTargetInfo::addFutureSpecificFeatures(
  587. llvm::StringMap<bool> &Features) const {}
  588. bool PPCTargetInfo::hasFeature(StringRef Feature) const {
  589. return llvm::StringSwitch<bool>(Feature)
  590. .Case("powerpc", true)
  591. .Case("altivec", HasAltivec)
  592. .Case("vsx", HasVSX)
  593. .Case("power8-vector", HasP8Vector)
  594. .Case("crypto", HasP8Crypto)
  595. .Case("direct-move", HasDirectMove)
  596. .Case("htm", HasHTM)
  597. .Case("bpermd", HasBPERMD)
  598. .Case("extdiv", HasExtDiv)
  599. .Case("float128", HasFloat128)
  600. .Case("power9-vector", HasP9Vector)
  601. .Case("paired-vector-memops", PairedVectorMemops)
  602. .Case("power10-vector", HasP10Vector)
  603. .Case("pcrelative-memops", HasPCRelativeMemops)
  604. .Case("prefix-instrs", HasPrefixInstrs)
  605. .Case("spe", HasSPE)
  606. .Case("mma", HasMMA)
  607. .Case("rop-protect", HasROPProtect)
  608. .Case("privileged", HasPrivileged)
  609. .Case("isa-v206-instructions", IsISA2_06)
  610. .Case("isa-v207-instructions", IsISA2_07)
  611. .Case("isa-v30-instructions", IsISA3_0)
  612. .Case("isa-v31-instructions", IsISA3_1)
  613. .Default(false);
  614. }
  615. void PPCTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
  616. StringRef Name, bool Enabled) const {
  617. if (Enabled) {
  618. if (Name == "efpu2")
  619. Features["spe"] = true;
  620. // If we're enabling any of the vsx based features then enable vsx and
  621. // altivec. We'll diagnose any problems later.
  622. bool FeatureHasVSX = llvm::StringSwitch<bool>(Name)
  623. .Case("vsx", true)
  624. .Case("direct-move", true)
  625. .Case("power8-vector", true)
  626. .Case("power9-vector", true)
  627. .Case("paired-vector-memops", true)
  628. .Case("power10-vector", true)
  629. .Case("float128", true)
  630. .Case("mma", true)
  631. .Default(false);
  632. if (FeatureHasVSX)
  633. Features["vsx"] = Features["altivec"] = true;
  634. if (Name == "power9-vector")
  635. Features["power8-vector"] = true;
  636. else if (Name == "power10-vector")
  637. Features["power8-vector"] = Features["power9-vector"] = true;
  638. if (Name == "pcrel")
  639. Features["pcrelative-memops"] = true;
  640. else if (Name == "prefixed")
  641. Features["prefix-instrs"] = true;
  642. else
  643. Features[Name] = true;
  644. } else {
  645. if (Name == "spe")
  646. Features["efpu2"] = false;
  647. // If we're disabling altivec or vsx go ahead and disable all of the vsx
  648. // features.
  649. if ((Name == "altivec") || (Name == "vsx"))
  650. Features["vsx"] = Features["direct-move"] = Features["power8-vector"] =
  651. Features["float128"] = Features["power9-vector"] =
  652. Features["paired-vector-memops"] = Features["mma"] =
  653. Features["power10-vector"] = false;
  654. if (Name == "power8-vector")
  655. Features["power9-vector"] = Features["paired-vector-memops"] =
  656. Features["mma"] = Features["power10-vector"] = false;
  657. else if (Name == "power9-vector")
  658. Features["paired-vector-memops"] = Features["mma"] =
  659. Features["power10-vector"] = false;
  660. if (Name == "pcrel")
  661. Features["pcrelative-memops"] = false;
  662. else if (Name == "prefixed")
  663. Features["prefix-instrs"] = false;
  664. else
  665. Features[Name] = false;
  666. }
  667. }
  668. const char *const PPCTargetInfo::GCCRegNames[] = {
  669. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8",
  670. "r9", "r10", "r11", "r12", "r13", "r14", "r15", "r16", "r17",
  671. "r18", "r19", "r20", "r21", "r22", "r23", "r24", "r25", "r26",
  672. "r27", "r28", "r29", "r30", "r31", "f0", "f1", "f2", "f3",
  673. "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
  674. "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20", "f21",
  675. "f22", "f23", "f24", "f25", "f26", "f27", "f28", "f29", "f30",
  676. "f31", "mq", "lr", "ctr", "ap", "cr0", "cr1", "cr2", "cr3",
  677. "cr4", "cr5", "cr6", "cr7", "xer", "v0", "v1", "v2", "v3",
  678. "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11", "v12",
  679. "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21",
  680. "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30",
  681. "v31", "vrsave", "vscr", "spe_acc", "spefscr", "sfp"
  682. };
  683. ArrayRef<const char *> PPCTargetInfo::getGCCRegNames() const {
  684. return llvm::makeArrayRef(GCCRegNames);
  685. }
  686. const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
  687. // While some of these aliases do map to different registers
  688. // they still share the same register name.
  689. {{"0"}, "r0"}, {{"1", "sp"}, "r1"}, {{"2"}, "r2"},
  690. {{"3"}, "r3"}, {{"4"}, "r4"}, {{"5"}, "r5"},
  691. {{"6"}, "r6"}, {{"7"}, "r7"}, {{"8"}, "r8"},
  692. {{"9"}, "r9"}, {{"10"}, "r10"}, {{"11"}, "r11"},
  693. {{"12"}, "r12"}, {{"13"}, "r13"}, {{"14"}, "r14"},
  694. {{"15"}, "r15"}, {{"16"}, "r16"}, {{"17"}, "r17"},
  695. {{"18"}, "r18"}, {{"19"}, "r19"}, {{"20"}, "r20"},
  696. {{"21"}, "r21"}, {{"22"}, "r22"}, {{"23"}, "r23"},
  697. {{"24"}, "r24"}, {{"25"}, "r25"}, {{"26"}, "r26"},
  698. {{"27"}, "r27"}, {{"28"}, "r28"}, {{"29"}, "r29"},
  699. {{"30"}, "r30"}, {{"31"}, "r31"}, {{"fr0"}, "f0"},
  700. {{"fr1"}, "f1"}, {{"fr2"}, "f2"}, {{"fr3"}, "f3"},
  701. {{"fr4"}, "f4"}, {{"fr5"}, "f5"}, {{"fr6"}, "f6"},
  702. {{"fr7"}, "f7"}, {{"fr8"}, "f8"}, {{"fr9"}, "f9"},
  703. {{"fr10"}, "f10"}, {{"fr11"}, "f11"}, {{"fr12"}, "f12"},
  704. {{"fr13"}, "f13"}, {{"fr14"}, "f14"}, {{"fr15"}, "f15"},
  705. {{"fr16"}, "f16"}, {{"fr17"}, "f17"}, {{"fr18"}, "f18"},
  706. {{"fr19"}, "f19"}, {{"fr20"}, "f20"}, {{"fr21"}, "f21"},
  707. {{"fr22"}, "f22"}, {{"fr23"}, "f23"}, {{"fr24"}, "f24"},
  708. {{"fr25"}, "f25"}, {{"fr26"}, "f26"}, {{"fr27"}, "f27"},
  709. {{"fr28"}, "f28"}, {{"fr29"}, "f29"}, {{"fr30"}, "f30"},
  710. {{"fr31"}, "f31"}, {{"cc"}, "cr0"},
  711. };
  712. ArrayRef<TargetInfo::GCCRegAlias> PPCTargetInfo::getGCCRegAliases() const {
  713. return llvm::makeArrayRef(GCCRegAliases);
  714. }
  715. // PPC ELFABIv2 DWARF Definitoin "Table 2.26. Mappings of Common Registers".
  716. // vs0 ~ vs31 is mapping to 32 - 63,
  717. // vs32 ~ vs63 is mapping to 77 - 108.
  718. const TargetInfo::AddlRegName GCCAddlRegNames[] = {
  719. // Table of additional register names to use in user input.
  720. {{"vs0"}, 32}, {{"vs1"}, 33}, {{"vs2"}, 34}, {{"vs3"}, 35},
  721. {{"vs4"}, 36}, {{"vs5"}, 37}, {{"vs6"}, 38}, {{"vs7"}, 39},
  722. {{"vs8"}, 40}, {{"vs9"}, 41}, {{"vs10"}, 42}, {{"vs11"}, 43},
  723. {{"vs12"}, 44}, {{"vs13"}, 45}, {{"vs14"}, 46}, {{"vs15"}, 47},
  724. {{"vs16"}, 48}, {{"vs17"}, 49}, {{"vs18"}, 50}, {{"vs19"}, 51},
  725. {{"vs20"}, 52}, {{"vs21"}, 53}, {{"vs22"}, 54}, {{"vs23"}, 55},
  726. {{"vs24"}, 56}, {{"vs25"}, 57}, {{"vs26"}, 58}, {{"vs27"}, 59},
  727. {{"vs28"}, 60}, {{"vs29"}, 61}, {{"vs30"}, 62}, {{"vs31"}, 63},
  728. {{"vs32"}, 77}, {{"vs33"}, 78}, {{"vs34"}, 79}, {{"vs35"}, 80},
  729. {{"vs36"}, 81}, {{"vs37"}, 82}, {{"vs38"}, 83}, {{"vs39"}, 84},
  730. {{"vs40"}, 85}, {{"vs41"}, 86}, {{"vs42"}, 87}, {{"vs43"}, 88},
  731. {{"vs44"}, 89}, {{"vs45"}, 90}, {{"vs46"}, 91}, {{"vs47"}, 92},
  732. {{"vs48"}, 93}, {{"vs49"}, 94}, {{"vs50"}, 95}, {{"vs51"}, 96},
  733. {{"vs52"}, 97}, {{"vs53"}, 98}, {{"vs54"}, 99}, {{"vs55"}, 100},
  734. {{"vs56"}, 101}, {{"vs57"}, 102}, {{"vs58"}, 103}, {{"vs59"}, 104},
  735. {{"vs60"}, 105}, {{"vs61"}, 106}, {{"vs62"}, 107}, {{"vs63"}, 108},
  736. };
  737. ArrayRef<TargetInfo::AddlRegName> PPCTargetInfo::getGCCAddlRegNames() const {
  738. if (ABI == "elfv2")
  739. return llvm::makeArrayRef(GCCAddlRegNames);
  740. else
  741. return TargetInfo::getGCCAddlRegNames();
  742. }
  743. static constexpr llvm::StringLiteral ValidCPUNames[] = {
  744. {"generic"}, {"440"}, {"450"}, {"601"}, {"602"},
  745. {"603"}, {"603e"}, {"603ev"}, {"604"}, {"604e"},
  746. {"620"}, {"630"}, {"g3"}, {"7400"}, {"g4"},
  747. {"7450"}, {"g4+"}, {"750"}, {"8548"}, {"970"},
  748. {"g5"}, {"a2"}, {"e500"}, {"e500mc"}, {"e5500"},
  749. {"power3"}, {"pwr3"}, {"power4"}, {"pwr4"}, {"power5"},
  750. {"pwr5"}, {"power5x"}, {"pwr5x"}, {"power6"}, {"pwr6"},
  751. {"power6x"}, {"pwr6x"}, {"power7"}, {"pwr7"}, {"power8"},
  752. {"pwr8"}, {"power9"}, {"pwr9"}, {"power10"}, {"pwr10"},
  753. {"powerpc"}, {"ppc"}, {"ppc32"}, {"powerpc64"}, {"ppc64"},
  754. {"powerpc64le"}, {"ppc64le"}, {"future"}};
  755. bool PPCTargetInfo::isValidCPUName(StringRef Name) const {
  756. return llvm::is_contained(ValidCPUNames, Name);
  757. }
  758. void PPCTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
  759. Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
  760. }
  761. void PPCTargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
  762. if (HasAltivec)
  763. Opts.AltiVec = 1;
  764. TargetInfo::adjust(Diags, Opts);
  765. if (LongDoubleFormat != &llvm::APFloat::IEEEdouble())
  766. LongDoubleFormat = Opts.PPCIEEELongDouble
  767. ? &llvm::APFloat::IEEEquad()
  768. : &llvm::APFloat::PPCDoubleDouble();
  769. Opts.IEEE128 = 1;
  770. }
  771. ArrayRef<Builtin::Info> PPCTargetInfo::getTargetBuiltins() const {
  772. return llvm::makeArrayRef(BuiltinInfo, clang::PPC::LastTSBuiltin -
  773. Builtin::FirstTSBuiltin);
  774. }