Hexagon.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //===--- Hexagon.cpp - Implement Hexagon 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 Hexagon TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "Hexagon.h"
  13. #include "Targets.h"
  14. #include "clang/Basic/MacroBuilder.h"
  15. #include "clang/Basic/TargetBuiltins.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. using namespace clang;
  18. using namespace clang::targets;
  19. void HexagonTargetInfo::getTargetDefines(const LangOptions &Opts,
  20. MacroBuilder &Builder) const {
  21. Builder.defineMacro("__qdsp6__", "1");
  22. Builder.defineMacro("__hexagon__", "1");
  23. Builder.defineMacro("__ELF__");
  24. // The macro __HVXDBL__ is deprecated.
  25. bool DefineHvxDbl = false;
  26. if (CPU == "hexagonv5") {
  27. Builder.defineMacro("__HEXAGON_V5__");
  28. Builder.defineMacro("__HEXAGON_ARCH__", "5");
  29. if (Opts.HexagonQdsp6Compat) {
  30. Builder.defineMacro("__QDSP6_V5__");
  31. Builder.defineMacro("__QDSP6_ARCH__", "5");
  32. }
  33. } else if (CPU == "hexagonv55") {
  34. Builder.defineMacro("__HEXAGON_V55__");
  35. Builder.defineMacro("__HEXAGON_ARCH__", "55");
  36. Builder.defineMacro("__QDSP6_V55__");
  37. Builder.defineMacro("__QDSP6_ARCH__", "55");
  38. } else if (CPU == "hexagonv60") {
  39. DefineHvxDbl = true;
  40. Builder.defineMacro("__HEXAGON_V60__");
  41. Builder.defineMacro("__HEXAGON_ARCH__", "60");
  42. Builder.defineMacro("__QDSP6_V60__");
  43. Builder.defineMacro("__QDSP6_ARCH__", "60");
  44. } else if (CPU == "hexagonv62") {
  45. DefineHvxDbl = true;
  46. Builder.defineMacro("__HEXAGON_V62__");
  47. Builder.defineMacro("__HEXAGON_ARCH__", "62");
  48. } else if (CPU == "hexagonv65") {
  49. DefineHvxDbl = true;
  50. Builder.defineMacro("__HEXAGON_V65__");
  51. Builder.defineMacro("__HEXAGON_ARCH__", "65");
  52. } else if (CPU == "hexagonv66") {
  53. DefineHvxDbl = true;
  54. Builder.defineMacro("__HEXAGON_V66__");
  55. Builder.defineMacro("__HEXAGON_ARCH__", "66");
  56. } else if (CPU == "hexagonv67") {
  57. Builder.defineMacro("__HEXAGON_V67__");
  58. Builder.defineMacro("__HEXAGON_ARCH__", "67");
  59. } else if (CPU == "hexagonv67t") {
  60. Builder.defineMacro("__HEXAGON_V67T__");
  61. Builder.defineMacro("__HEXAGON_ARCH__", "67");
  62. } else if (CPU == "hexagonv68") {
  63. Builder.defineMacro("__HEXAGON_V68__");
  64. Builder.defineMacro("__HEXAGON_ARCH__", "68");
  65. } else if (CPU == "hexagonv69") {
  66. Builder.defineMacro("__HEXAGON_V69__");
  67. Builder.defineMacro("__HEXAGON_ARCH__", "69");
  68. }
  69. if (hasFeature("hvx-length64b")) {
  70. Builder.defineMacro("__HVX__");
  71. Builder.defineMacro("__HVX_ARCH__", HVXVersion);
  72. Builder.defineMacro("__HVX_LENGTH__", "64");
  73. }
  74. if (hasFeature("hvx-length128b")) {
  75. Builder.defineMacro("__HVX__");
  76. Builder.defineMacro("__HVX_ARCH__", HVXVersion);
  77. Builder.defineMacro("__HVX_LENGTH__", "128");
  78. if (DefineHvxDbl)
  79. Builder.defineMacro("__HVXDBL__");
  80. }
  81. if (hasFeature("audio")) {
  82. Builder.defineMacro("__HEXAGON_AUDIO__");
  83. }
  84. std::string NumPhySlots = isTinyCore() ? "3" : "4";
  85. Builder.defineMacro("__HEXAGON_PHYSICAL_SLOTS__", NumPhySlots);
  86. }
  87. bool HexagonTargetInfo::initFeatureMap(
  88. llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
  89. const std::vector<std::string> &FeaturesVec) const {
  90. if (isTinyCore())
  91. Features["audio"] = true;
  92. StringRef CPUFeature = CPU;
  93. CPUFeature.consume_front("hexagon");
  94. CPUFeature.consume_back("t");
  95. if (!CPUFeature.empty())
  96. Features[CPUFeature] = true;
  97. Features["long-calls"] = false;
  98. return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
  99. }
  100. bool HexagonTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
  101. DiagnosticsEngine &Diags) {
  102. for (auto &F : Features) {
  103. if (F == "+hvx-length64b")
  104. HasHVX = HasHVX64B = true;
  105. else if (F == "+hvx-length128b")
  106. HasHVX = HasHVX128B = true;
  107. else if (F.find("+hvxv") != std::string::npos) {
  108. HasHVX = true;
  109. HVXVersion = F.substr(std::string("+hvxv").length());
  110. } else if (F == "-hvx")
  111. HasHVX = HasHVX64B = HasHVX128B = false;
  112. else if (F == "+long-calls")
  113. UseLongCalls = true;
  114. else if (F == "-long-calls")
  115. UseLongCalls = false;
  116. else if (F == "+audio")
  117. HasAudio = true;
  118. }
  119. if (CPU.compare("hexagonv68") >= 0) {
  120. HasLegalHalfType = true;
  121. HasFloat16 = true;
  122. }
  123. return true;
  124. }
  125. const char *const HexagonTargetInfo::GCCRegNames[] = {
  126. // Scalar registers:
  127. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11",
  128. "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19", "r20", "r21",
  129. "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  130. "r1:0", "r3:2", "r5:4", "r7:6", "r9:8", "r11:10", "r13:12", "r15:14",
  131. "r17:16", "r19:18", "r21:20", "r23:22", "r25:24", "r27:26", "r29:28",
  132. "r31:30",
  133. // Predicate registers:
  134. "p0", "p1", "p2", "p3",
  135. // Control registers:
  136. "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11",
  137. "c12", "c13", "c14", "c15", "c16", "c17", "c18", "c19", "c20", "c21",
  138. "c22", "c23", "c24", "c25", "c26", "c27", "c28", "c29", "c30", "c31",
  139. "c1:0", "c3:2", "c5:4", "c7:6", "c9:8", "c11:10", "c13:12", "c15:14",
  140. "c17:16", "c19:18", "c21:20", "c23:22", "c25:24", "c27:26", "c29:28",
  141. "c31:30",
  142. // Control register aliases:
  143. "sa0", "lc0", "sa1", "lc1", "p3:0", "m0", "m1", "usr", "pc", "ugp",
  144. "gp", "cs0", "cs1", "upcyclelo", "upcyclehi", "framelimit", "framekey",
  145. "pktcountlo", "pktcounthi", "utimerlo", "utimerhi",
  146. "upcycle", "pktcount", "utimer",
  147. // HVX vector registers:
  148. "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11",
  149. "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21",
  150. "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
  151. "v1:0", "v3:2", "v5:4", "v7:6", "v9:8", "v11:10", "v13:12", "v15:14",
  152. "v17:16", "v19:18", "v21:20", "v23:22", "v25:24", "v27:26", "v29:28",
  153. "v31:30",
  154. "v3:0", "v7:4", "v11:8", "v15:12", "v19:16", "v23:20", "v27:24", "v31:28",
  155. // HVX vector predicates:
  156. "q0", "q1", "q2", "q3",
  157. };
  158. ArrayRef<const char *> HexagonTargetInfo::getGCCRegNames() const {
  159. return llvm::makeArrayRef(GCCRegNames);
  160. }
  161. const TargetInfo::GCCRegAlias HexagonTargetInfo::GCCRegAliases[] = {
  162. {{"sp"}, "r29"},
  163. {{"fp"}, "r30"},
  164. {{"lr"}, "r31"},
  165. };
  166. ArrayRef<TargetInfo::GCCRegAlias> HexagonTargetInfo::getGCCRegAliases() const {
  167. return llvm::makeArrayRef(GCCRegAliases);
  168. }
  169. const Builtin::Info HexagonTargetInfo::BuiltinInfo[] = {
  170. #define BUILTIN(ID, TYPE, ATTRS) \
  171. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
  172. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
  173. {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
  174. #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
  175. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
  176. #include "clang/Basic/BuiltinsHexagon.def"
  177. };
  178. bool HexagonTargetInfo::hasFeature(StringRef Feature) const {
  179. std::string VS = "hvxv" + HVXVersion;
  180. if (Feature == VS)
  181. return true;
  182. return llvm::StringSwitch<bool>(Feature)
  183. .Case("hexagon", true)
  184. .Case("hvx", HasHVX)
  185. .Case("hvx-length64b", HasHVX64B)
  186. .Case("hvx-length128b", HasHVX128B)
  187. .Case("long-calls", UseLongCalls)
  188. .Case("audio", HasAudio)
  189. .Default(false);
  190. }
  191. struct CPUSuffix {
  192. llvm::StringLiteral Name;
  193. llvm::StringLiteral Suffix;
  194. };
  195. static constexpr CPUSuffix Suffixes[] = {
  196. {{"hexagonv5"}, {"5"}}, {{"hexagonv55"}, {"55"}},
  197. {{"hexagonv60"}, {"60"}}, {{"hexagonv62"}, {"62"}},
  198. {{"hexagonv65"}, {"65"}}, {{"hexagonv66"}, {"66"}},
  199. {{"hexagonv67"}, {"67"}}, {{"hexagonv67t"}, {"67t"}},
  200. {{"hexagonv68"}, {"68"}}, {{"hexagonv69"}, {"69"}},
  201. };
  202. const char *HexagonTargetInfo::getHexagonCPUSuffix(StringRef Name) {
  203. const CPUSuffix *Item = llvm::find_if(
  204. Suffixes, [Name](const CPUSuffix &S) { return S.Name == Name; });
  205. if (Item == std::end(Suffixes))
  206. return nullptr;
  207. return Item->Suffix.data();
  208. }
  209. void HexagonTargetInfo::fillValidCPUList(
  210. SmallVectorImpl<StringRef> &Values) const {
  211. for (const CPUSuffix &Suffix : Suffixes)
  212. Values.push_back(Suffix.Name);
  213. }
  214. ArrayRef<Builtin::Info> HexagonTargetInfo::getTargetBuiltins() const {
  215. return llvm::makeArrayRef(BuiltinInfo, clang::Hexagon::LastTSBuiltin -
  216. Builtin::FirstTSBuiltin);
  217. }