| 1 | //===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===// |
| 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 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include "SparcTargetMachine.h" |
| 13 | #include "LeonPasses.h" |
| 14 | #include "Sparc.h" |
| 15 | #include "SparcMachineFunctionInfo.h" |
| 16 | #include "SparcTargetObjectFile.h" |
| 17 | #include "SparcTargetTransformInfo.h" |
| 18 | #include "TargetInfo/SparcTargetInfo.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 22 | #include "llvm/MC/TargetRegistry.h" |
| 23 | #include "llvm/Option/LibraryOptions.h" |
| 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include <optional> |
| 26 | using namespace llvm; |
| 27 | |
| 28 | extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSparcTarget() { |
| 29 | // Register the target. |
| 30 | RegisterTargetMachine<SparcV8TargetMachine> X(getTheSparcTarget()); |
| 31 | RegisterTargetMachine<SparcV9TargetMachine> Y(getTheSparcV9Target()); |
| 32 | RegisterTargetMachine<SparcelTargetMachine> Z(getTheSparcelTarget()); |
| 33 | static opt::RegisterLibraryOptions<SparcOptions> O; |
| 34 | |
| 35 | PassRegistry &PR = *PassRegistry::getPassRegistry(); |
| 36 | initializeSparcAsmPrinterPass(PR); |
| 37 | initializeSparcDAGToDAGISelLegacyPass(PR); |
| 38 | initializeSparcErrataWorkaroundLegacyPass(PR); |
| 39 | } |
| 40 | |
| 41 | static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) { |
| 42 | return RM.value_or(u: Reloc::Static); |
| 43 | } |
| 44 | |
| 45 | // Code models. Some only make sense for 64-bit code. |
| 46 | // |
| 47 | // SunCC Reloc CodeModel Constraints |
| 48 | // abs32 Static Small text+data+bss linked below 2^32 bytes |
| 49 | // abs44 Static Medium text+data+bss linked below 2^44 bytes |
| 50 | // abs64 Static Large text smaller than 2^31 bytes |
| 51 | // pic13 PIC_ Small GOT < 2^13 bytes |
| 52 | // pic32 PIC_ Medium GOT < 2^32 bytes |
| 53 | // |
| 54 | // All code models require that the text segment is smaller than 2GB. |
| 55 | static CodeModel::Model |
| 56 | getEffectiveSparcCodeModel(std::optional<CodeModel::Model> CM, Reloc::Model RM, |
| 57 | bool Is64Bit, bool JIT) { |
| 58 | if (CM) { |
| 59 | if (*CM == CodeModel::Tiny) |
| 60 | report_fatal_error(reason: "Target does not support the tiny CodeModel" , gen_crash_diag: false); |
| 61 | if (*CM == CodeModel::Kernel) |
| 62 | report_fatal_error(reason: "Target does not support the kernel CodeModel" , gen_crash_diag: false); |
| 63 | return *CM; |
| 64 | } |
| 65 | if (Is64Bit) { |
| 66 | if (JIT) |
| 67 | return CodeModel::Large; |
| 68 | return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium; |
| 69 | } |
| 70 | return CodeModel::Small; |
| 71 | } |
| 72 | |
| 73 | /// Create an ILP32 architecture model |
| 74 | SparcTargetMachine::SparcTargetMachine(const Target &T, const Triple &TT, |
| 75 | StringRef CPU, StringRef FS, |
| 76 | const TargetOptions &Options, |
| 77 | std::optional<Reloc::Model> RM, |
| 78 | std::optional<CodeModel::Model> CM, |
| 79 | CodeGenOptLevel OL, bool JIT) |
| 80 | : CodeGenTargetMachineImpl( |
| 81 | T, TT, CPU, FS, Options, getEffectiveRelocModel(RM), |
| 82 | getEffectiveSparcCodeModel(CM, RM: getEffectiveRelocModel(RM), |
| 83 | Is64Bit: TT.isSPARC64(), JIT), |
| 84 | OL), |
| 85 | CLOpts(cl::LibraryOptionsContext::get<SparcOptions>( |
| 86 | C: Options.MCOptions.LibraryOptions)), |
| 87 | TLOF(std::make_unique<SparcELFTargetObjectFile>()) { |
| 88 | initAsmInfo(); |
| 89 | } |
| 90 | |
| 91 | SparcTargetMachine::~SparcTargetMachine() = default; |
| 92 | |
| 93 | const SparcSubtarget * |
| 94 | SparcTargetMachine::getSubtargetImpl(const Function &F) const { |
| 95 | AttributeSet FnAttrs = F.getAttributes().getFnAttrs(); |
| 96 | const Module *M = F.getParent(); |
| 97 | if (const SparcSubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M)) |
| 98 | return ST; |
| 99 | |
| 100 | SubtargetAttrs Attrs; |
| 101 | Attrs.CPU = FnAttrs.getAttribute(Kind: "target-cpu" ); |
| 102 | Attrs.TuneCPU = FnAttrs.getAttribute(Kind: "tune-cpu" ); |
| 103 | Attrs.FS = FnAttrs.getAttribute(Kind: "target-features" ); |
| 104 | Attrs.SoftFloat = FnAttrs.getAttribute(Kind: "use-soft-float" ); |
| 105 | |
| 106 | if (const SparcSubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs)) |
| 107 | return ST; |
| 108 | |
| 109 | StringRef CPU = |
| 110 | Attrs.CPU.isValid() ? Attrs.CPU.getValueAsString() : StringRef(TargetCPU); |
| 111 | StringRef TuneCPU = |
| 112 | Attrs.TuneCPU.isValid() ? Attrs.TuneCPU.getValueAsString() : CPU; |
| 113 | StringRef FS = |
| 114 | Attrs.FS.isValid() ? Attrs.FS.getValueAsString() : StringRef(TargetFS); |
| 115 | |
| 116 | SmallString<256> Key({CPU, TuneCPU}); |
| 117 | |
| 118 | // Keep track of the start of the feature portion of the string. |
| 119 | unsigned FSStart = Key.size(); |
| 120 | Key += FS; |
| 121 | |
| 122 | // FIXME: This is related to the code below to reset the target options, |
| 123 | // we need to know whether or not the soft float flag is set on the |
| 124 | // function, so we can enable it as a subtarget feature. |
| 125 | if (Attrs.SoftFloat.getValueAsBool()) |
| 126 | Key += FS.empty() ? "+soft-float" : ",+soft-float" ; |
| 127 | |
| 128 | auto &I = SubtargetMap[Key]; |
| 129 | if (!I) |
| 130 | I = std::make_unique<SparcSubtarget>(args&: CPU, args&: TuneCPU, args: Key.substr(Start: FSStart), |
| 131 | args: *this); |
| 132 | LastSubtarget.set(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs, NewSubtarget: I.get()); |
| 133 | return I.get(); |
| 134 | } |
| 135 | |
| 136 | MachineFunctionInfo *SparcTargetMachine::createMachineFunctionInfo( |
| 137 | BumpPtrAllocator &Allocator, const Function &F, |
| 138 | const TargetSubtargetInfo *STI) const { |
| 139 | return SparcMachineFunctionInfo::create<SparcMachineFunctionInfo>(Allocator, |
| 140 | F, STI); |
| 141 | } |
| 142 | |
| 143 | namespace { |
| 144 | /// Sparc Code Generator Pass Configuration Options. |
| 145 | class SparcPassConfig : public TargetPassConfig { |
| 146 | public: |
| 147 | SparcPassConfig(SparcTargetMachine &TM, PassManagerBase &PM) |
| 148 | : TargetPassConfig(TM, PM) {} |
| 149 | |
| 150 | SparcTargetMachine &getSparcTargetMachine() const { |
| 151 | return getTM<SparcTargetMachine>(); |
| 152 | } |
| 153 | |
| 154 | void addIRPasses() override; |
| 155 | bool addInstSelector() override; |
| 156 | void addPreEmitPass() override; |
| 157 | }; |
| 158 | } // namespace |
| 159 | |
| 160 | TargetPassConfig *SparcTargetMachine::createPassConfig(PassManagerBase &PM) { |
| 161 | return new SparcPassConfig(*this, PM); |
| 162 | } |
| 163 | |
| 164 | TargetTransformInfo |
| 165 | SparcTargetMachine::getTargetTransformInfo(const Function &F) const { |
| 166 | return TargetTransformInfo(std::make_unique<SparcTTIImpl>(args: this, args: F)); |
| 167 | } |
| 168 | |
| 169 | void SparcPassConfig::addIRPasses() { |
| 170 | addPass(P: createAtomicExpandLegacyPass()); |
| 171 | |
| 172 | TargetPassConfig::addIRPasses(); |
| 173 | } |
| 174 | |
| 175 | bool SparcPassConfig::addInstSelector() { |
| 176 | addPass(P: createSparcDAGToDAGISelLegacyPass(TM&: getSparcTargetMachine(), |
| 177 | OptLevel: getOptLevel())); |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | void SparcPassConfig::addPreEmitPass(){ |
| 182 | if (getSparcTargetMachine().getCLOpts().enable_branch_relax) |
| 183 | addPass(PassID: &BranchRelaxationPassID); |
| 184 | |
| 185 | addPass(P: createSparcDelaySlotFillerLegacyPass()); |
| 186 | addPass(P: new SparcErrataWorkaroundLegacy()); |
| 187 | } |
| 188 | |
| 189 | void SparcV8TargetMachine::anchor() { } |
| 190 | |
| 191 | SparcV8TargetMachine::SparcV8TargetMachine(const Target &T, const Triple &TT, |
| 192 | StringRef CPU, StringRef FS, |
| 193 | const TargetOptions &Options, |
| 194 | std::optional<Reloc::Model> RM, |
| 195 | std::optional<CodeModel::Model> CM, |
| 196 | CodeGenOptLevel OL, bool JIT) |
| 197 | : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT) {} |
| 198 | |
| 199 | void SparcV9TargetMachine::anchor() { } |
| 200 | |
| 201 | SparcV9TargetMachine::SparcV9TargetMachine(const Target &T, const Triple &TT, |
| 202 | StringRef CPU, StringRef FS, |
| 203 | const TargetOptions &Options, |
| 204 | std::optional<Reloc::Model> RM, |
| 205 | std::optional<CodeModel::Model> CM, |
| 206 | CodeGenOptLevel OL, bool JIT) |
| 207 | : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT) {} |
| 208 | |
| 209 | void SparcelTargetMachine::anchor() {} |
| 210 | |
| 211 | SparcelTargetMachine::SparcelTargetMachine(const Target &T, const Triple &TT, |
| 212 | StringRef CPU, StringRef FS, |
| 213 | const TargetOptions &Options, |
| 214 | std::optional<Reloc::Model> RM, |
| 215 | std::optional<CodeModel::Model> CM, |
| 216 | CodeGenOptLevel OL, bool JIT) |
| 217 | : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT) {} |
| 218 | |