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 13 #include "RISCVTargetMachine.h" 14 #include "RISCV.h" 15 #include "RISCVTargetObjectFile.h" 16 #include "RISCVTargetTransformInfo.h" 17 #include "TargetInfo/RISCVTargetInfo.h" 18 #include "Utils/RISCVBaseInfo.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/Analysis/TargetTransformInfo.h" 21 #include "llvm/CodeGen/GlobalISel/IRTranslator.h" 22 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" 23 #include "llvm/CodeGen/GlobalISel/Legalizer.h" 24 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" 25 #include "llvm/CodeGen/Passes.h" 26 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 27 #include "llvm/CodeGen/TargetPassConfig.h" 28 #include "llvm/IR/LegacyPassManager.h" 29 #include "llvm/InitializePasses.h" 30 #include "llvm/Support/FormattedStream.h" 31 #include "llvm/Support/TargetRegistry.h" 32 #include "llvm/Target/TargetOptions.h" 33 using namespace llvm; 34 35 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() { 36 RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target()); 37 RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target()); 38 auto PR = PassRegistry::getPassRegistry(); 39 initializeGlobalISel(*PR); 40 initializeRISCVExpandPseudoPass(*PR); 41 } 42 43 static StringRef computeDataLayout(const Triple &TT) { 44 if (TT.isArch64Bit()) { 45 return "e-m:e-p:64:64-i64:64-i128:128-n64-S128"; 46 } else { 47 assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported"); 48 return "e-m:e-p:32:32-i64:64-n32-S128"; 49 } 50 } 51 52 static Reloc::Model getEffectiveRelocModel(const Triple &TT, 53 Optional<Reloc::Model> RM) { 54 if (!RM.hasValue()) 55 return Reloc::Static; 56 return *RM; 57 } 58 59 RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT, 60 StringRef CPU, StringRef FS, 61 const TargetOptions &Options, 62 Optional<Reloc::Model> RM, 63 Optional<CodeModel::Model> CM, 64 CodeGenOpt::Level OL, bool JIT) 65 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, 66 getEffectiveRelocModel(TT, RM), 67 getEffectiveCodeModel(CM, CodeModel::Small), OL), 68 TLOF(std::make_unique<RISCVELFTargetObjectFile>()) { 69 initAsmInfo(); 70 71 // RISC-V supports the MachineOutliner. 72 setMachineOutliner(true); 73 } 74 75 const RISCVSubtarget * 76 RISCVTargetMachine::getSubtargetImpl(const Function &F) const { 77 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 78 Attribute TuneAttr = F.getFnAttribute("tune-cpu"); 79 Attribute FSAttr = F.getFnAttribute("target-features"); 80 81 std::string CPU = 82 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU; 83 std::string TuneCPU = 84 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU; 85 std::string FS = 86 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS; 87 std::string Key = CPU + TuneCPU + FS; 88 auto &I = SubtargetMap[Key]; 89 if (!I) { 90 // This needs to be done before we create a new subtarget since any 91 // creation will depend on the TM and the code generation flags on the 92 // function that reside in TargetOptions. 93 resetTargetOptions(F); 94 auto ABIName = Options.MCOptions.getABIName(); 95 if (const MDString *ModuleTargetABI = dyn_cast_or_null<MDString>( 96 F.getParent()->getModuleFlag("target-abi"))) { 97 auto TargetABI = RISCVABI::getTargetABI(ABIName); 98 if (TargetABI != RISCVABI::ABI_Unknown && 99 ModuleTargetABI->getString() != ABIName) { 100 report_fatal_error("-target-abi option != target-abi module flag"); 101 } 102 ABIName = ModuleTargetABI->getString(); 103 } 104 I = std::make_unique<RISCVSubtarget>(TargetTriple, CPU, TuneCPU, FS, ABIName, *this); 105 } 106 return I.get(); 107 } 108 109 TargetTransformInfo 110 RISCVTargetMachine::getTargetTransformInfo(const Function &F) { 111 return TargetTransformInfo(RISCVTTIImpl(this, F)); 112 } 113 114 namespace { 115 class RISCVPassConfig : public TargetPassConfig { 116 public: 117 RISCVPassConfig(RISCVTargetMachine &TM, PassManagerBase &PM) 118 : TargetPassConfig(TM, PM) {} 119 120 RISCVTargetMachine &getRISCVTargetMachine() const { 121 return getTM<RISCVTargetMachine>(); 122 } 123 124 void addIRPasses() override; 125 bool addInstSelector() override; 126 bool addIRTranslator() override; 127 bool addLegalizeMachineIR() override; 128 bool addRegBankSelect() override; 129 bool addGlobalInstructionSelect() override; 130 void addPreEmitPass() override; 131 void addPreEmitPass2() override; 132 void addPreSched2() override; 133 void addPreRegAlloc() override; 134 }; 135 } 136 137 TargetPassConfig *RISCVTargetMachine::createPassConfig(PassManagerBase &PM) { 138 return new RISCVPassConfig(*this, PM); 139 } 140 141 void RISCVPassConfig::addIRPasses() { 142 addPass(createAtomicExpandPass()); 143 TargetPassConfig::addIRPasses(); 144 } 145 146 bool RISCVPassConfig::addInstSelector() { 147 addPass(createRISCVISelDag(getRISCVTargetMachine())); 148 149 return false; 150 } 151 152 bool RISCVPassConfig::addIRTranslator() { 153 addPass(new IRTranslator(getOptLevel())); 154 return false; 155 } 156 157 bool RISCVPassConfig::addLegalizeMachineIR() { 158 addPass(new Legalizer()); 159 return false; 160 } 161 162 bool RISCVPassConfig::addRegBankSelect() { 163 addPass(new RegBankSelect()); 164 return false; 165 } 166 167 bool RISCVPassConfig::addGlobalInstructionSelect() { 168 addPass(new InstructionSelect()); 169 return false; 170 } 171 172 void RISCVPassConfig::addPreSched2() {} 173 174 void RISCVPassConfig::addPreEmitPass() { addPass(&BranchRelaxationPassID); } 175 176 void RISCVPassConfig::addPreEmitPass2() { 177 addPass(createRISCVExpandPseudoPass()); 178 // Schedule the expansion of AMOs at the last possible moment, avoiding the 179 // possibility for other passes to break the requirements for forward 180 // progress in the LR/SC block. 181 addPass(createRISCVExpandAtomicPseudoPass()); 182 } 183 184 void RISCVPassConfig::addPreRegAlloc() { 185 addPass(createRISCVMergeBaseOffsetOptPass()); 186 } 187