LLVM 20.0.0git
LoongArchTargetMachine.cpp
Go to the documentation of this file.
1//===-- LoongArchTargetMachine.cpp - Define TargetMachine for LoongArch ---===//
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 LoongArch target spec.
10//
11//===----------------------------------------------------------------------===//
12
14#include "LoongArch.h"
20#include "llvm/CodeGen/Passes.h"
26#include <optional>
27
28using namespace llvm;
29
30#define DEBUG_TYPE "loongarch"
31
33 // Register the target.
43}
44
46 "loongarch-enable-dead-defs", cl::Hidden,
47 cl::desc("Enable the pass that removes dead"
48 " definitons and replaces stores to"
49 " them with stores to r0"),
50 cl::init(true));
51
52static cl::opt<bool>
53 EnableLoopDataPrefetch("loongarch-enable-loop-data-prefetch", cl::Hidden,
54 cl::desc("Enable the loop data prefetch pass"),
55 cl::init(false));
56
57static std::string computeDataLayout(const Triple &TT) {
58 if (TT.isArch64Bit())
59 return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
60 assert(TT.isArch32Bit() && "only LA32 and LA64 are currently supported");
61 return "e-m:e-p:32:32-i64:64-n32-S128";
62}
63
65 std::optional<Reloc::Model> RM) {
66 return RM.value_or(Reloc::Static);
67}
68
71 std::optional<CodeModel::Model> CM) {
72 if (!CM)
73 return CodeModel::Small;
74
75 switch (*CM) {
77 return *CM;
80 if (!TT.isArch64Bit())
81 report_fatal_error("Medium/Large code model requires LA64");
82 return *CM;
83 default:
85 "Only small, medium and large code models are allowed on LoongArch");
86 }
87}
88
90 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
91 const TargetOptions &Options, std::optional<Reloc::Model> RM,
92 std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT)
96 TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
98}
99
101
102const LoongArchSubtarget *
104 Attribute CPUAttr = F.getFnAttribute("target-cpu");
105 Attribute TuneAttr = F.getFnAttribute("tune-cpu");
106 Attribute FSAttr = F.getFnAttribute("target-features");
107
108 std::string CPU =
109 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
110 std::string TuneCPU =
111 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
112 std::string FS =
113 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
114
115 std::string Key = CPU + TuneCPU + FS;
116 auto &I = SubtargetMap[Key];
117 if (!I) {
118 // This needs to be done before we create a new subtarget since any
119 // creation will depend on the TM and the code generation flags on the
120 // function that reside in TargetOptions.
122 auto ABIName = Options.MCOptions.getABIName();
123 if (const MDString *ModuleTargetABI = dyn_cast_or_null<MDString>(
124 F.getParent()->getModuleFlag("target-abi"))) {
125 auto TargetABI = LoongArchABI::getTargetABI(ABIName);
126 if (TargetABI != LoongArchABI::ABI_Unknown &&
127 ModuleTargetABI->getString() != ABIName) {
128 report_fatal_error("-target-abi option != target-abi module flag");
129 }
130 ABIName = ModuleTargetABI->getString();
131 }
132 I = std::make_unique<LoongArchSubtarget>(TargetTriple, CPU, TuneCPU, FS,
133 ABIName, *this);
134 }
135 return I.get();
136}
137
139 BumpPtrAllocator &Allocator, const Function &F,
140 const TargetSubtargetInfo *STI) const {
141 return LoongArchMachineFunctionInfo::create<LoongArchMachineFunctionInfo>(
142 Allocator, F, STI);
143}
144
145namespace {
146class LoongArchPassConfig : public TargetPassConfig {
147public:
148 LoongArchPassConfig(LoongArchTargetMachine &TM, PassManagerBase &PM)
149 : TargetPassConfig(TM, PM) {}
150
151 LoongArchTargetMachine &getLoongArchTargetMachine() const {
152 return getTM<LoongArchTargetMachine>();
153 }
154
155 void addIRPasses() override;
156 void addCodeGenPrepare() override;
157 bool addInstSelector() override;
158 void addPreEmitPass() override;
159 void addPreEmitPass2() override;
160 void addMachineSSAOptimization() override;
161 void addPreRegAlloc() override;
162 bool addRegAssignAndRewriteFast() override;
163 bool addRegAssignAndRewriteOptimized() override;
164};
165} // end namespace
166
169 return new LoongArchPassConfig(*this, PM);
170}
171
172void LoongArchPassConfig::addIRPasses() {
173 // Run LoopDataPrefetch
174 //
175 // Run this before LSR to remove the multiplies involved in computing the
176 // pointer values N iterations ahead.
177 if (TM->getOptLevel() != CodeGenOptLevel::None && EnableLoopDataPrefetch)
180
182}
183
184void LoongArchPassConfig::addCodeGenPrepare() {
185 if (getOptLevel() != CodeGenOptLevel::None)
188}
189
190bool LoongArchPassConfig::addInstSelector() {
191 addPass(createLoongArchISelDag(getLoongArchTargetMachine()));
192
193 return false;
194}
195
199}
200
201void LoongArchPassConfig::addPreEmitPass() { addPass(&BranchRelaxationPassID); }
202
203void LoongArchPassConfig::addPreEmitPass2() {
205 // Schedule the expansion of AtomicPseudos at the last possible moment,
206 // avoiding the possibility for other passes to break the requirements for
207 // forward progress in the LL/SC block.
209}
210
211void LoongArchPassConfig::addMachineSSAOptimization() {
213
214 if (TM->getTargetTriple().isLoongArch64()) {
216 }
217}
218
219void LoongArchPassConfig::addPreRegAlloc() {
221 if (TM->getOptLevel() != CodeGenOptLevel::None)
223}
224
225bool LoongArchPassConfig::addRegAssignAndRewriteFast() {
226 if (TM->getOptLevel() != CodeGenOptLevel::None &&
230}
231
232bool LoongArchPassConfig::addRegAssignAndRewriteOptimized() {
233 if (TM->getOptLevel() != CodeGenOptLevel::None &&
237}
static cl::opt< bool > EnableLoopDataPrefetch("aarch64-enable-loop-data-prefetch", cl::Hidden, cl::desc("Enable the loop data prefetch pass"), cl::init(true))
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static LVOptions Options
Definition: LVOptions.cpp:25
static std::string computeDataLayout()
static cl::opt< bool > EnableLoongArchDeadRegisterElimination("loongarch-enable-dead-defs", cl::Hidden, cl::desc("Enable the pass that removes dead" " definitons and replaces stores to" " them with stores to r0"), cl::init(true))
static cl::opt< bool > EnableLoopDataPrefetch("loongarch-enable-loop-data-prefetch", cl::Hidden, cl::desc("Enable the loop data prefetch pass"), cl::init(false))
static CodeModel::Model getEffectiveLoongArchCodeModel(const Triple &TT, std::optional< CodeModel::Model > CM)
static Reloc::Model getEffectiveRelocModel(const Triple &TT, std::optional< Reloc::Model > RM)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTarget()
This file a TargetTransformInfo::Concept conforming object specific to the LoongArch target machine.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Target-Independent Code Generator Pass Configuration Options pass.
This pass exposes codegen information to IR-level passes.
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:392
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:208
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
implements a set of functionality in the TargetMachine class for targets that make use of the indepen...
LoongArchTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM, CodeGenOptLevel OL, bool JIT)
const LoongArchSubtarget * getSubtargetImpl() const =delete
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
TargetTransformInfo getTargetTransformInfo(const Function &F) const override
Get a TargetTransformInfo implementation for the target.
MachineFunctionInfo * createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F, const TargetSubtargetInfo *STI) const override
Create the target's instance of MachineFunctionInfo.
StringRef getABIName() const
getABIName - If this returns a non-empty string this represents the textual name of the ABI that we w...
A single uniqued string.
Definition: Metadata.h:724
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
Definition: TargetMachine.h:96
std::string TargetFS
Definition: TargetMachine.h:98
std::string TargetCPU
Definition: TargetMachine.h:97
std::unique_ptr< const MCSubtargetInfo > STI
TargetOptions Options
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
MCTargetOptions MCOptions
Machine level options.
Target-Independent Code Generator Pass Configuration Options.
virtual void addCodeGenPrepare()
Add pass to prepare the LLVM IR for code generation.
virtual bool addRegAssignAndRewriteFast()
Add core register allocator passes which do the actual register assignment and rewriting.
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
virtual void addMachineSSAOptimization()
addMachineSSAOptimization - Add standard passes that optimize machine instructions in SSA form.
virtual bool addRegAssignAndRewriteOptimized()
TargetSubtargetInfo - Generic base class for all target subtargets.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
ABI getTargetABI(StringRef ABIName)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Target & getTheLoongArch64Target()
FunctionPass * createLoongArchExpandAtomicPseudoPass()
FunctionPass * createLoongArchDeadRegisterDefinitionsPass()
void initializeLoongArchDAGToDAGISelLegacyPass(PassRegistry &)
FunctionPass * createTypePromotionLegacyPass()
Create IR Type Promotion pass.
void initializeLoongArchPreRAExpandPseudoPass(PassRegistry &)
FunctionPass * createLoongArchISelDag(LoongArchTargetMachine &TM)
void initializeLoongArchMergeBaseOffsetOptPass(PassRegistry &)
char & BranchRelaxationPassID
BranchRelaxation - This pass replaces branches that need to jump further than is supported by a branc...
void initializeLoongArchExpandPseudoPass(PassRegistry &)
FunctionPass * createLoongArchOptWInstrsPass()
FunctionPass * createLoopDataPrefetchPass()
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54
Target & getTheLoongArch32Target()
FunctionPass * createLoongArchPreRAExpandPseudoPass()
void initializeLoongArchOptWInstrsPass(PassRegistry &)
FunctionPass * createLoongArchExpandPseudoPass()
FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
void initializeLoongArchDeadRegisterDefinitionsPass(PassRegistry &)
FunctionPass * createLoongArchMergeBaseOffsetOptPass()
Returns an instance of the Merge Base Offset Optimization pass.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
RegisterTargetMachine - Helper template for registering a target machine implementation,...