RISCV.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //===--- RISCV.cpp - Implement RISCV 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 RISCV TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "RISCV.h"
  13. #include "clang/Basic/Diagnostic.h"
  14. #include "clang/Basic/MacroBuilder.h"
  15. #include "clang/Basic/TargetBuiltins.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. #include "llvm/Support/TargetParser.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace clang;
  20. using namespace clang::targets;
  21. ArrayRef<const char *> RISCVTargetInfo::getGCCRegNames() const {
  22. static const char *const GCCRegNames[] = {
  23. // Integer registers
  24. "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
  25. "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
  26. "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
  27. "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
  28. // Floating point registers
  29. "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  30. "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
  31. "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  32. "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  33. // Vector registers
  34. "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
  35. "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
  36. "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
  37. "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31"};
  38. return llvm::makeArrayRef(GCCRegNames);
  39. }
  40. ArrayRef<TargetInfo::GCCRegAlias> RISCVTargetInfo::getGCCRegAliases() const {
  41. static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
  42. {{"zero"}, "x0"}, {{"ra"}, "x1"}, {{"sp"}, "x2"}, {{"gp"}, "x3"},
  43. {{"tp"}, "x4"}, {{"t0"}, "x5"}, {{"t1"}, "x6"}, {{"t2"}, "x7"},
  44. {{"s0"}, "x8"}, {{"s1"}, "x9"}, {{"a0"}, "x10"}, {{"a1"}, "x11"},
  45. {{"a2"}, "x12"}, {{"a3"}, "x13"}, {{"a4"}, "x14"}, {{"a5"}, "x15"},
  46. {{"a6"}, "x16"}, {{"a7"}, "x17"}, {{"s2"}, "x18"}, {{"s3"}, "x19"},
  47. {{"s4"}, "x20"}, {{"s5"}, "x21"}, {{"s6"}, "x22"}, {{"s7"}, "x23"},
  48. {{"s8"}, "x24"}, {{"s9"}, "x25"}, {{"s10"}, "x26"}, {{"s11"}, "x27"},
  49. {{"t3"}, "x28"}, {{"t4"}, "x29"}, {{"t5"}, "x30"}, {{"t6"}, "x31"},
  50. {{"ft0"}, "f0"}, {{"ft1"}, "f1"}, {{"ft2"}, "f2"}, {{"ft3"}, "f3"},
  51. {{"ft4"}, "f4"}, {{"ft5"}, "f5"}, {{"ft6"}, "f6"}, {{"ft7"}, "f7"},
  52. {{"fs0"}, "f8"}, {{"fs1"}, "f9"}, {{"fa0"}, "f10"}, {{"fa1"}, "f11"},
  53. {{"fa2"}, "f12"}, {{"fa3"}, "f13"}, {{"fa4"}, "f14"}, {{"fa5"}, "f15"},
  54. {{"fa6"}, "f16"}, {{"fa7"}, "f17"}, {{"fs2"}, "f18"}, {{"fs3"}, "f19"},
  55. {{"fs4"}, "f20"}, {{"fs5"}, "f21"}, {{"fs6"}, "f22"}, {{"fs7"}, "f23"},
  56. {{"fs8"}, "f24"}, {{"fs9"}, "f25"}, {{"fs10"}, "f26"}, {{"fs11"}, "f27"},
  57. {{"ft8"}, "f28"}, {{"ft9"}, "f29"}, {{"ft10"}, "f30"}, {{"ft11"}, "f31"}};
  58. return llvm::makeArrayRef(GCCRegAliases);
  59. }
  60. bool RISCVTargetInfo::validateAsmConstraint(
  61. const char *&Name, TargetInfo::ConstraintInfo &Info) const {
  62. switch (*Name) {
  63. default:
  64. return false;
  65. case 'I':
  66. // A 12-bit signed immediate.
  67. Info.setRequiresImmediate(-2048, 2047);
  68. return true;
  69. case 'J':
  70. // Integer zero.
  71. Info.setRequiresImmediate(0);
  72. return true;
  73. case 'K':
  74. // A 5-bit unsigned immediate for CSR access instructions.
  75. Info.setRequiresImmediate(0, 31);
  76. return true;
  77. case 'f':
  78. // A floating-point register.
  79. Info.setAllowsRegister();
  80. return true;
  81. case 'A':
  82. // An address that is held in a general-purpose register.
  83. Info.setAllowsMemory();
  84. return true;
  85. case 'S': // A symbolic address
  86. Info.setAllowsRegister();
  87. return true;
  88. case 'v':
  89. // A vector register.
  90. if (Name[1] == 'r' || Name[1] == 'm') {
  91. Info.setAllowsRegister();
  92. Name += 1;
  93. return true;
  94. }
  95. return false;
  96. }
  97. }
  98. std::string RISCVTargetInfo::convertConstraint(const char *&Constraint) const {
  99. std::string R;
  100. switch (*Constraint) {
  101. case 'v':
  102. R = std::string("^") + std::string(Constraint, 2);
  103. Constraint += 1;
  104. break;
  105. default:
  106. R = TargetInfo::convertConstraint(Constraint);
  107. break;
  108. }
  109. return R;
  110. }
  111. void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts,
  112. MacroBuilder &Builder) const {
  113. Builder.defineMacro("__ELF__");
  114. Builder.defineMacro("__riscv");
  115. bool Is64Bit = getTriple().getArch() == llvm::Triple::riscv64;
  116. Builder.defineMacro("__riscv_xlen", Is64Bit ? "64" : "32");
  117. StringRef CodeModel = getTargetOpts().CodeModel;
  118. unsigned FLen = ISAInfo->getFLen();
  119. unsigned MinVLen = ISAInfo->getMinVLen();
  120. unsigned MaxELen = ISAInfo->getMaxELen();
  121. unsigned MaxELenFp = ISAInfo->getMaxELenFp();
  122. if (CodeModel == "default")
  123. CodeModel = "small";
  124. if (CodeModel == "small")
  125. Builder.defineMacro("__riscv_cmodel_medlow");
  126. else if (CodeModel == "medium")
  127. Builder.defineMacro("__riscv_cmodel_medany");
  128. StringRef ABIName = getABI();
  129. if (ABIName == "ilp32f" || ABIName == "lp64f")
  130. Builder.defineMacro("__riscv_float_abi_single");
  131. else if (ABIName == "ilp32d" || ABIName == "lp64d")
  132. Builder.defineMacro("__riscv_float_abi_double");
  133. else
  134. Builder.defineMacro("__riscv_float_abi_soft");
  135. if (ABIName == "ilp32e")
  136. Builder.defineMacro("__riscv_abi_rve");
  137. Builder.defineMacro("__riscv_arch_test");
  138. for (auto &Extension : ISAInfo->getExtensions()) {
  139. auto ExtName = Extension.first;
  140. auto ExtInfo = Extension.second;
  141. unsigned Version =
  142. (ExtInfo.MajorVersion * 1000000) + (ExtInfo.MinorVersion * 1000);
  143. Builder.defineMacro(Twine("__riscv_", ExtName), Twine(Version));
  144. }
  145. if (ISAInfo->hasExtension("m")) {
  146. Builder.defineMacro("__riscv_mul");
  147. Builder.defineMacro("__riscv_div");
  148. Builder.defineMacro("__riscv_muldiv");
  149. }
  150. if (ISAInfo->hasExtension("a")) {
  151. Builder.defineMacro("__riscv_atomic");
  152. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
  153. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
  154. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
  155. if (Is64Bit)
  156. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
  157. }
  158. if (FLen) {
  159. Builder.defineMacro("__riscv_flen", Twine(FLen));
  160. Builder.defineMacro("__riscv_fdiv");
  161. Builder.defineMacro("__riscv_fsqrt");
  162. }
  163. if (MinVLen) {
  164. Builder.defineMacro("__riscv_v_min_vlen", Twine(MinVLen));
  165. Builder.defineMacro("__riscv_v_elen", Twine(MaxELen));
  166. Builder.defineMacro("__riscv_v_elen_fp", Twine(MaxELenFp));
  167. }
  168. if (ISAInfo->hasExtension("c"))
  169. Builder.defineMacro("__riscv_compressed");
  170. if (ISAInfo->hasExtension("zve32x") || ISAInfo->hasExtension("v"))
  171. Builder.defineMacro("__riscv_vector");
  172. }
  173. const Builtin::Info RISCVTargetInfo::BuiltinInfo[] = {
  174. #define BUILTIN(ID, TYPE, ATTRS) \
  175. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
  176. #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
  177. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
  178. #include "clang/Basic/BuiltinsRISCVVector.def"
  179. #define BUILTIN(ID, TYPE, ATTRS) \
  180. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
  181. #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
  182. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
  183. #include "clang/Basic/BuiltinsRISCV.def"
  184. };
  185. ArrayRef<Builtin::Info> RISCVTargetInfo::getTargetBuiltins() const {
  186. return llvm::makeArrayRef(BuiltinInfo, clang::RISCV::LastTSBuiltin -
  187. Builtin::FirstTSBuiltin);
  188. }
  189. bool RISCVTargetInfo::initFeatureMap(
  190. llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
  191. const std::vector<std::string> &FeaturesVec) const {
  192. unsigned XLen = 32;
  193. if (getTriple().getArch() == llvm::Triple::riscv64) {
  194. Features["64bit"] = true;
  195. XLen = 64;
  196. }
  197. auto ParseResult = llvm::RISCVISAInfo::parseFeatures(XLen, FeaturesVec);
  198. if (!ParseResult) {
  199. std::string Buffer;
  200. llvm::raw_string_ostream OutputErrMsg(Buffer);
  201. handleAllErrors(ParseResult.takeError(), [&](llvm::StringError &ErrMsg) {
  202. OutputErrMsg << ErrMsg.getMessage();
  203. });
  204. Diags.Report(diag::err_invalid_feature_combination) << OutputErrMsg.str();
  205. return false;
  206. }
  207. return TargetInfo::initFeatureMap(Features, Diags, CPU,
  208. (*ParseResult)->toFeatureVector());
  209. }
  210. /// Return true if has this feature, need to sync with handleTargetFeatures.
  211. bool RISCVTargetInfo::hasFeature(StringRef Feature) const {
  212. bool Is64Bit = getTriple().getArch() == llvm::Triple::riscv64;
  213. auto Result = llvm::StringSwitch<Optional<bool>>(Feature)
  214. .Case("riscv", true)
  215. .Case("riscv32", !Is64Bit)
  216. .Case("riscv64", Is64Bit)
  217. .Case("64bit", Is64Bit)
  218. .Default(None);
  219. if (Result.hasValue())
  220. return Result.getValue();
  221. if (ISAInfo->isSupportedExtensionFeature(Feature))
  222. return ISAInfo->hasExtension(Feature);
  223. return false;
  224. }
  225. /// Perform initialization based on the user configured set of features.
  226. bool RISCVTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
  227. DiagnosticsEngine &Diags) {
  228. unsigned XLen = getTriple().isArch64Bit() ? 64 : 32;
  229. auto ParseResult = llvm::RISCVISAInfo::parseFeatures(XLen, Features);
  230. if (!ParseResult) {
  231. std::string Buffer;
  232. llvm::raw_string_ostream OutputErrMsg(Buffer);
  233. handleAllErrors(ParseResult.takeError(), [&](llvm::StringError &ErrMsg) {
  234. OutputErrMsg << ErrMsg.getMessage();
  235. });
  236. Diags.Report(diag::err_invalid_feature_combination) << OutputErrMsg.str();
  237. return false;
  238. } else {
  239. ISAInfo = std::move(*ParseResult);
  240. }
  241. if (ABI.empty())
  242. ABI = llvm::RISCV::computeDefaultABIFromArch(*ISAInfo).str();
  243. return true;
  244. }
  245. bool RISCV32TargetInfo::isValidCPUName(StringRef Name) const {
  246. return llvm::RISCV::checkCPUKind(llvm::RISCV::parseCPUKind(Name),
  247. /*Is64Bit=*/false);
  248. }
  249. void RISCV32TargetInfo::fillValidCPUList(
  250. SmallVectorImpl<StringRef> &Values) const {
  251. llvm::RISCV::fillValidCPUArchList(Values, false);
  252. }
  253. bool RISCV32TargetInfo::isValidTuneCPUName(StringRef Name) const {
  254. return llvm::RISCV::checkTuneCPUKind(
  255. llvm::RISCV::parseTuneCPUKind(Name, false),
  256. /*Is64Bit=*/false);
  257. }
  258. void RISCV32TargetInfo::fillValidTuneCPUList(
  259. SmallVectorImpl<StringRef> &Values) const {
  260. llvm::RISCV::fillValidTuneCPUArchList(Values, false);
  261. }
  262. bool RISCV64TargetInfo::isValidCPUName(StringRef Name) const {
  263. return llvm::RISCV::checkCPUKind(llvm::RISCV::parseCPUKind(Name),
  264. /*Is64Bit=*/true);
  265. }
  266. void RISCV64TargetInfo::fillValidCPUList(
  267. SmallVectorImpl<StringRef> &Values) const {
  268. llvm::RISCV::fillValidCPUArchList(Values, true);
  269. }
  270. bool RISCV64TargetInfo::isValidTuneCPUName(StringRef Name) const {
  271. return llvm::RISCV::checkTuneCPUKind(
  272. llvm::RISCV::parseTuneCPUKind(Name, true),
  273. /*Is64Bit=*/true);
  274. }
  275. void RISCV64TargetInfo::fillValidTuneCPUList(
  276. SmallVectorImpl<StringRef> &Values) const {
  277. llvm::RISCV::fillValidTuneCPUArchList(Values, true);
  278. }