RISCVTargetMachine.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //===-- RISCVTargetMachine.cpp - Define TargetMachine for RISCV -----------===//
  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. // Implements the info about RISCV target spec.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "RISCVTargetMachine.h"
  13. #include "MCTargetDesc/RISCVBaseInfo.h"
  14. #include "RISCV.h"
  15. #include "RISCVMachineFunctionInfo.h"
  16. #include "RISCVMacroFusion.h"
  17. #include "RISCVTargetObjectFile.h"
  18. #include "RISCVTargetTransformInfo.h"
  19. #include "TargetInfo/RISCVTargetInfo.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/Analysis/TargetTransformInfo.h"
  22. #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
  23. #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
  24. #include "llvm/CodeGen/GlobalISel/Legalizer.h"
  25. #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
  26. #include "llvm/CodeGen/MIRParser/MIParser.h"
  27. #include "llvm/CodeGen/MIRYamlMapping.h"
  28. #include "llvm/CodeGen/Passes.h"
  29. #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
  30. #include "llvm/CodeGen/TargetPassConfig.h"
  31. #include "llvm/IR/LegacyPassManager.h"
  32. #include "llvm/InitializePasses.h"
  33. #include "llvm/MC/TargetRegistry.h"
  34. #include "llvm/Support/FormattedStream.h"
  35. #include "llvm/Target/TargetOptions.h"
  36. #include "llvm/Transforms/IPO.h"
  37. #include <optional>
  38. using namespace llvm;
  39. static cl::opt<bool> EnableRedundantCopyElimination(
  40. "riscv-enable-copyelim",
  41. cl::desc("Enable the redundant copy elimination pass"), cl::init(true),
  42. cl::Hidden);
  43. // FIXME: Unify control over GlobalMerge.
  44. static cl::opt<cl::boolOrDefault>
  45. EnableGlobalMerge("riscv-enable-global-merge", cl::Hidden,
  46. cl::desc("Enable the global merge pass"));
  47. static cl::opt<bool>
  48. EnableMachineCombiner("riscv-enable-machine-combiner",
  49. cl::desc("Enable the machine combiner pass"),
  50. cl::init(true), cl::Hidden);
  51. static cl::opt<unsigned> RVVVectorBitsMaxOpt(
  52. "riscv-v-vector-bits-max",
  53. cl::desc("Assume V extension vector registers are at most this big, "
  54. "with zero meaning no maximum size is assumed."),
  55. cl::init(0), cl::Hidden);
  56. static cl::opt<int> RVVVectorBitsMinOpt(
  57. "riscv-v-vector-bits-min",
  58. cl::desc("Assume V extension vector registers are at least this big, "
  59. "with zero meaning no minimum size is assumed. A value of -1 "
  60. "means use Zvl*b extension. This is primarily used to enable "
  61. "autovectorization with fixed width vectors."),
  62. cl::init(-1), cl::Hidden);
  63. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
  64. RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target());
  65. RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target());
  66. auto *PR = PassRegistry::getPassRegistry();
  67. initializeGlobalISel(*PR);
  68. initializeRISCVMakeCompressibleOptPass(*PR);
  69. initializeRISCVGatherScatterLoweringPass(*PR);
  70. initializeRISCVCodeGenPreparePass(*PR);
  71. initializeRISCVMergeBaseOffsetOptPass(*PR);
  72. initializeRISCVSExtWRemovalPass(*PR);
  73. initializeRISCVStripWSuffixPass(*PR);
  74. initializeRISCVPreRAExpandPseudoPass(*PR);
  75. initializeRISCVExpandPseudoPass(*PR);
  76. initializeRISCVInsertVSETVLIPass(*PR);
  77. initializeRISCVDAGToDAGISelPass(*PR);
  78. }
  79. static StringRef computeDataLayout(const Triple &TT) {
  80. if (TT.isArch64Bit())
  81. return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
  82. assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
  83. return "e-m:e-p:32:32-i64:64-n32-S128";
  84. }
  85. static Reloc::Model getEffectiveRelocModel(const Triple &TT,
  86. std::optional<Reloc::Model> RM) {
  87. return RM.value_or(Reloc::Static);
  88. }
  89. RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
  90. StringRef CPU, StringRef FS,
  91. const TargetOptions &Options,
  92. std::optional<Reloc::Model> RM,
  93. std::optional<CodeModel::Model> CM,
  94. CodeGenOpt::Level OL, bool JIT)
  95. : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
  96. getEffectiveRelocModel(TT, RM),
  97. getEffectiveCodeModel(CM, CodeModel::Small), OL),
  98. TLOF(std::make_unique<RISCVELFTargetObjectFile>()) {
  99. initAsmInfo();
  100. // RISC-V supports the MachineOutliner.
  101. setMachineOutliner(true);
  102. setSupportsDefaultOutlining(true);
  103. }
  104. const RISCVSubtarget *
  105. RISCVTargetMachine::getSubtargetImpl(const Function &F) const {
  106. Attribute CPUAttr = F.getFnAttribute("target-cpu");
  107. Attribute TuneAttr = F.getFnAttribute("tune-cpu");
  108. Attribute FSAttr = F.getFnAttribute("target-features");
  109. std::string CPU =
  110. CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
  111. std::string TuneCPU =
  112. TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
  113. std::string FS =
  114. FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
  115. unsigned RVVBitsMin = RVVVectorBitsMinOpt;
  116. unsigned RVVBitsMax = RVVVectorBitsMaxOpt;
  117. Attribute VScaleRangeAttr = F.getFnAttribute(Attribute::VScaleRange);
  118. if (VScaleRangeAttr.isValid()) {
  119. if (!RVVVectorBitsMinOpt.getNumOccurrences())
  120. RVVBitsMin = VScaleRangeAttr.getVScaleRangeMin() * RISCV::RVVBitsPerBlock;
  121. std::optional<unsigned> VScaleMax = VScaleRangeAttr.getVScaleRangeMax();
  122. if (VScaleMax.has_value() && !RVVVectorBitsMaxOpt.getNumOccurrences())
  123. RVVBitsMax = *VScaleMax * RISCV::RVVBitsPerBlock;
  124. }
  125. if (RVVBitsMin != -1U) {
  126. // FIXME: Change to >= 32 when VLEN = 32 is supported.
  127. assert((RVVBitsMin == 0 || (RVVBitsMin >= 64 && RVVBitsMin <= 65536 &&
  128. isPowerOf2_32(RVVBitsMin))) &&
  129. "V or Zve* extension requires vector length to be in the range of "
  130. "64 to 65536 and a power 2!");
  131. assert((RVVBitsMax >= RVVBitsMin || RVVBitsMax == 0) &&
  132. "Minimum V extension vector length should not be larger than its "
  133. "maximum!");
  134. }
  135. assert((RVVBitsMax == 0 || (RVVBitsMax >= 64 && RVVBitsMax <= 65536 &&
  136. isPowerOf2_32(RVVBitsMax))) &&
  137. "V or Zve* extension requires vector length to be in the range of "
  138. "64 to 65536 and a power 2!");
  139. if (RVVBitsMin != -1U) {
  140. if (RVVBitsMax != 0) {
  141. RVVBitsMin = std::min(RVVBitsMin, RVVBitsMax);
  142. RVVBitsMax = std::max(RVVBitsMin, RVVBitsMax);
  143. }
  144. RVVBitsMin =
  145. PowerOf2Floor((RVVBitsMin < 64 || RVVBitsMin > 65536) ? 0 : RVVBitsMin);
  146. }
  147. RVVBitsMax =
  148. PowerOf2Floor((RVVBitsMax < 64 || RVVBitsMax > 65536) ? 0 : RVVBitsMax);
  149. SmallString<512> Key;
  150. Key += "RVVMin";
  151. Key += std::to_string(RVVBitsMin);
  152. Key += "RVVMax";
  153. Key += std::to_string(RVVBitsMax);
  154. Key += CPU;
  155. Key += TuneCPU;
  156. Key += FS;
  157. auto &I = SubtargetMap[Key];
  158. if (!I) {
  159. // This needs to be done before we create a new subtarget since any
  160. // creation will depend on the TM and the code generation flags on the
  161. // function that reside in TargetOptions.
  162. resetTargetOptions(F);
  163. auto ABIName = Options.MCOptions.getABIName();
  164. if (const MDString *ModuleTargetABI = dyn_cast_or_null<MDString>(
  165. F.getParent()->getModuleFlag("target-abi"))) {
  166. auto TargetABI = RISCVABI::getTargetABI(ABIName);
  167. if (TargetABI != RISCVABI::ABI_Unknown &&
  168. ModuleTargetABI->getString() != ABIName) {
  169. report_fatal_error("-target-abi option != target-abi module flag");
  170. }
  171. ABIName = ModuleTargetABI->getString();
  172. }
  173. I = std::make_unique<RISCVSubtarget>(
  174. TargetTriple, CPU, TuneCPU, FS, ABIName, RVVBitsMin, RVVBitsMax, *this);
  175. }
  176. return I.get();
  177. }
  178. MachineFunctionInfo *RISCVTargetMachine::createMachineFunctionInfo(
  179. BumpPtrAllocator &Allocator, const Function &F,
  180. const TargetSubtargetInfo *STI) const {
  181. return RISCVMachineFunctionInfo::create<RISCVMachineFunctionInfo>(Allocator,
  182. F, STI);
  183. }
  184. TargetTransformInfo
  185. RISCVTargetMachine::getTargetTransformInfo(const Function &F) const {
  186. return TargetTransformInfo(RISCVTTIImpl(this, F));
  187. }
  188. // A RISC-V hart has a single byte-addressable address space of 2^XLEN bytes
  189. // for all memory accesses, so it is reasonable to assume that an
  190. // implementation has no-op address space casts. If an implementation makes a
  191. // change to this, they can override it here.
  192. bool RISCVTargetMachine::isNoopAddrSpaceCast(unsigned SrcAS,
  193. unsigned DstAS) const {
  194. return true;
  195. }
  196. namespace {
  197. class RISCVPassConfig : public TargetPassConfig {
  198. public:
  199. RISCVPassConfig(RISCVTargetMachine &TM, PassManagerBase &PM)
  200. : TargetPassConfig(TM, PM) {}
  201. RISCVTargetMachine &getRISCVTargetMachine() const {
  202. return getTM<RISCVTargetMachine>();
  203. }
  204. ScheduleDAGInstrs *
  205. createMachineScheduler(MachineSchedContext *C) const override {
  206. const RISCVSubtarget &ST = C->MF->getSubtarget<RISCVSubtarget>();
  207. if (ST.hasMacroFusion()) {
  208. ScheduleDAGMILive *DAG = createGenericSchedLive(C);
  209. DAG->addMutation(createRISCVMacroFusionDAGMutation());
  210. return DAG;
  211. }
  212. return nullptr;
  213. }
  214. ScheduleDAGInstrs *
  215. createPostMachineScheduler(MachineSchedContext *C) const override {
  216. const RISCVSubtarget &ST = C->MF->getSubtarget<RISCVSubtarget>();
  217. if (ST.hasMacroFusion()) {
  218. ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
  219. DAG->addMutation(createRISCVMacroFusionDAGMutation());
  220. return DAG;
  221. }
  222. return nullptr;
  223. }
  224. void addIRPasses() override;
  225. bool addPreISel() override;
  226. bool addInstSelector() override;
  227. bool addIRTranslator() override;
  228. bool addLegalizeMachineIR() override;
  229. bool addRegBankSelect() override;
  230. bool addGlobalInstructionSelect() override;
  231. void addPreEmitPass() override;
  232. void addPreEmitPass2() override;
  233. void addPreSched2() override;
  234. void addMachineSSAOptimization() override;
  235. void addPreRegAlloc() override;
  236. void addPostRegAlloc() override;
  237. };
  238. } // namespace
  239. TargetPassConfig *RISCVTargetMachine::createPassConfig(PassManagerBase &PM) {
  240. return new RISCVPassConfig(*this, PM);
  241. }
  242. void RISCVPassConfig::addIRPasses() {
  243. addPass(createAtomicExpandPass());
  244. if (getOptLevel() != CodeGenOpt::None)
  245. addPass(createRISCVGatherScatterLoweringPass());
  246. if (getOptLevel() != CodeGenOpt::None)
  247. addPass(createRISCVCodeGenPreparePass());
  248. TargetPassConfig::addIRPasses();
  249. }
  250. bool RISCVPassConfig::addPreISel() {
  251. if (TM->getOptLevel() != CodeGenOpt::None) {
  252. // Add a barrier before instruction selection so that we will not get
  253. // deleted block address after enabling default outlining. See D99707 for
  254. // more details.
  255. addPass(createBarrierNoopPass());
  256. }
  257. if (EnableGlobalMerge == cl::BOU_TRUE) {
  258. addPass(createGlobalMergePass(TM, /* MaxOffset */ 2047,
  259. /* OnlyOptimizeForSize */ false,
  260. /* MergeExternalByDefault */ true));
  261. }
  262. return false;
  263. }
  264. bool RISCVPassConfig::addInstSelector() {
  265. addPass(createRISCVISelDag(getRISCVTargetMachine(), getOptLevel()));
  266. return false;
  267. }
  268. bool RISCVPassConfig::addIRTranslator() {
  269. addPass(new IRTranslator(getOptLevel()));
  270. return false;
  271. }
  272. bool RISCVPassConfig::addLegalizeMachineIR() {
  273. addPass(new Legalizer());
  274. return false;
  275. }
  276. bool RISCVPassConfig::addRegBankSelect() {
  277. addPass(new RegBankSelect());
  278. return false;
  279. }
  280. bool RISCVPassConfig::addGlobalInstructionSelect() {
  281. addPass(new InstructionSelect(getOptLevel()));
  282. return false;
  283. }
  284. void RISCVPassConfig::addPreSched2() {}
  285. void RISCVPassConfig::addPreEmitPass() {
  286. addPass(&BranchRelaxationPassID);
  287. addPass(createRISCVMakeCompressibleOptPass());
  288. }
  289. void RISCVPassConfig::addPreEmitPass2() {
  290. addPass(createRISCVExpandPseudoPass());
  291. // Schedule the expansion of AMOs at the last possible moment, avoiding the
  292. // possibility for other passes to break the requirements for forward
  293. // progress in the LR/SC block.
  294. addPass(createRISCVExpandAtomicPseudoPass());
  295. }
  296. void RISCVPassConfig::addMachineSSAOptimization() {
  297. TargetPassConfig::addMachineSSAOptimization();
  298. if (EnableMachineCombiner)
  299. addPass(&MachineCombinerID);
  300. if (TM->getTargetTriple().getArch() == Triple::riscv64) {
  301. addPass(createRISCVSExtWRemovalPass());
  302. addPass(createRISCVStripWSuffixPass());
  303. }
  304. }
  305. void RISCVPassConfig::addPreRegAlloc() {
  306. addPass(createRISCVPreRAExpandPseudoPass());
  307. if (TM->getOptLevel() != CodeGenOpt::None)
  308. addPass(createRISCVMergeBaseOffsetOptPass());
  309. addPass(createRISCVInsertVSETVLIPass());
  310. }
  311. void RISCVPassConfig::addPostRegAlloc() {
  312. if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
  313. addPass(createRISCVRedundantCopyEliminationPass());
  314. }
  315. yaml::MachineFunctionInfo *
  316. RISCVTargetMachine::createDefaultFuncInfoYAML() const {
  317. return new yaml::RISCVMachineFunctionInfo();
  318. }
  319. yaml::MachineFunctionInfo *
  320. RISCVTargetMachine::convertFuncInfoToYAML(const MachineFunction &MF) const {
  321. const auto *MFI = MF.getInfo<RISCVMachineFunctionInfo>();
  322. return new yaml::RISCVMachineFunctionInfo(*MFI);
  323. }
  324. bool RISCVTargetMachine::parseMachineFunctionInfo(
  325. const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
  326. SMDiagnostic &Error, SMRange &SourceRange) const {
  327. const auto &YamlMFI =
  328. static_cast<const yaml::RISCVMachineFunctionInfo &>(MFI);
  329. PFS.MF.getInfo<RISCVMachineFunctionInfo>()->initializeBaseYamlFields(YamlMFI);
  330. return false;
  331. }