LLVM 24.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"
27#include <optional>
28
29using namespace llvm;
30
31#define DEBUG_TYPE "loongarch"
32
49
51 "loongarch-enable-dead-defs", cl::Hidden,
52 cl::desc("Enable the pass that removes dead"
53 " definitons and replaces stores to"
54 " them with stores to r0"),
55 cl::init(true));
56
58 EnableLoopDataPrefetch("loongarch-enable-loop-data-prefetch", cl::Hidden,
59 cl::desc("Enable the loop data prefetch pass"),
60 cl::init(true));
61
63 EnableMergeBaseOffset("loongarch-enable-merge-offset",
64 cl::desc("Enable the merge base offset pass"),
65 cl::init(true), cl::Hidden);
66
67static cl::opt<bool>
68 EnableSinkFold("loongarch-enable-sink-fold",
69 cl::desc("Enable sinking and folding of instruction copies"),
70 cl::init(true), cl::Hidden);
71
73 EnableMachineCombiner("loongarch-enable-machine-combiner",
74 cl::desc("Enable the machine combiner pass"),
75 cl::init(true), cl::Hidden);
76
77static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
78 return RM.value_or(Reloc::Static);
79}
80
83 std::optional<CodeModel::Model> CM) {
84 if (!CM)
85 return TT.isArch64Bit() ? CodeModel::Medium : CodeModel::Small;
86
87 switch (*CM) {
90 return *CM;
92 if (!TT.isArch64Bit())
93 report_fatal_error("Large code model requires LA64");
94 return *CM;
95 default:
97 "Only small, medium and large code models are allowed on LoongArch");
98 }
99}
100
102 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
103 const TargetOptions &Options, std::optional<Reloc::Model> RM,
104 std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT)
105 : CodeGenTargetMachineImpl(T, TT, CPU, FS, Options,
108 TLOF(std::make_unique<LoongArchELFTargetObjectFile>()) {
109 initAsmInfo();
110}
111
113
114const LoongArchSubtarget *
116 Attribute CPUAttr = F.getFnAttribute("target-cpu");
117 Attribute TuneAttr = F.getFnAttribute("tune-cpu");
118 Attribute FSAttr = F.getFnAttribute("target-features");
119
120 std::string CPU =
121 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
122 std::string TuneCPU =
123 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
124 std::string FS =
125 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
126
127 std::string Key = CPU + TuneCPU + FS;
128 auto &I = SubtargetMap[Key];
129 if (!I) {
130 StringRef ABIName = getTargetABIName(*F.getParent());
131 I = std::make_unique<LoongArchSubtarget>(TargetTriple, CPU, TuneCPU, FS,
132 ABIName, *this);
133 }
134 return I.get();
135}
136
143
144namespace {
145class LoongArchPassConfig : public TargetPassConfig {
146public:
147 LoongArchPassConfig(LoongArchTargetMachine &TM, PassManagerBase &PM)
148 : TargetPassConfig(TM, PM) {
149 setEnableSinkAndFold(EnableSinkFold);
150 }
151
152 LoongArchTargetMachine &getLoongArchTargetMachine() const {
154 }
155
156 void addIRPasses() override;
157 void addCodeGenPrepare() override;
158 bool addInstSelector() override;
159 void addPreEmitPass() override;
160 void addPreEmitPass2() override;
161 void addMachineSSAOptimization() override;
162 void addPreRegAlloc() override;
163 bool addILPOpts() override;
164 void addRegAssignAndRewriteFast() override;
165 void addRegAssignAndRewriteOptimized() override;
166 void addMachineLateOptimization() override;
167};
168} // end namespace
169
172 return new LoongArchPassConfig(*this, PM);
173}
174
175void LoongArchPassConfig::addIRPasses() {
176 // Run LoopDataPrefetch
177 //
178 // Run this before LSR to remove the multiplies involved in computing the
179 // pointer values N iterations ahead.
180 if (TM->getOptLevel() != CodeGenOptLevel::None && EnableLoopDataPrefetch)
183
185}
186
187void LoongArchPassConfig::addCodeGenPrepare() {
188 if (getOptLevel() != CodeGenOptLevel::None)
191}
192
193bool LoongArchPassConfig::addInstSelector() {
194 addPass(createLoongArchISelDagLegacyPass(getLoongArchTargetMachine(),
195 getOptLevel()));
196
197 return false;
198}
199
202 return TargetTransformInfo(std::make_unique<LoongArchTTIImpl>(this, F));
203}
204
205void LoongArchPassConfig::addPreEmitPass() { addPass(&BranchRelaxationPassID); }
206
207void LoongArchPassConfig::addPreEmitPass2() {
209 // Schedule the expansion of AtomicPseudos at the last possible moment,
210 // avoiding the possibility for other passes to break the requirements for
211 // forward progress in the LL/SC block.
213 if (TM->getOptLevel() != CodeGenOptLevel::None)
215}
216
217void LoongArchPassConfig::addMachineSSAOptimization() {
219
220 if (TM->getTargetTriple().isLoongArch64()) {
222 }
223}
224
225void LoongArchPassConfig::addPreRegAlloc() {
227 if (TM->getOptLevel() != CodeGenOptLevel::None && EnableMergeBaseOffset)
229}
230
231bool LoongArchPassConfig::addILPOpts() {
233 addPass(&MachineCombinerID);
234
235 return true;
236}
237
238void LoongArchPassConfig::addRegAssignAndRewriteFast() {
239 if (TM->getOptLevel() != CodeGenOptLevel::None &&
243}
244
245void LoongArchPassConfig::addRegAssignAndRewriteOptimized() {
246 if (TM->getOptLevel() != CodeGenOptLevel::None &&
250}
251
252void LoongArchPassConfig::addMachineLateOptimization() {
253 if (TM->getOptLevel() != CodeGenOptLevel::None)
256}
static Reloc::Model getEffectiveRelocModel()
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
cl::opt< bool > EnableMachineCombiner
cl::opt< bool > EnableMergeBaseOffset
cl::opt< bool > EnableLoopDataPrefetch
cl::opt< bool > EnableLoongArchDeadRegisterElimination
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))
cl::opt< bool > EnableMachineCombiner("loongarch-enable-machine-combiner", cl::desc("Enable the machine combiner pass"), cl::init(true), cl::Hidden)
cl::opt< bool > EnableMergeBaseOffset("loongarch-enable-merge-offset", cl::desc("Enable the merge base offset pass"), cl::init(true), cl::Hidden)
cl::opt< bool > EnableLoopDataPrefetch("loongarch-enable-loop-data-prefetch", cl::Hidden, cl::desc("Enable the loop data prefetch pass"), cl::init(true))
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTarget()
static CodeModel::Model getEffectiveLoongArchCodeModel(const Triple &TT, std::optional< CodeModel::Model > CM)
static cl::opt< bool > EnableSinkFold("loongarch-enable-sink-fold", cl::desc("Enable sinking and folding of instruction copies"), cl::init(true), cl::Hidden)
This file a TargetTransformInfoImplBase conforming object specific to the LoongArch target machine.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
const GCNTargetMachine & getTM(const GCNSubtarget *STI)
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Target-Independent Code Generator Pass Configuration Options pass.
This pass exposes codegen information to IR-level passes.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:260
CodeGenTargetMachineImpl(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, CodeGenOptLevel OL)
This implementation is used for LoongArch ELF targets.
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.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
StringRef getTargetABIName(const Module &M) const
Returns the effective target ABI name: the "target-abi" module flag if present, otherwise the -target...
std::unique_ptr< const MCSubtargetInfo > STI
TargetOptions Options
Target-Independent Code Generator Pass Configuration Options.
virtual void addCodeGenPrepare()
Add pass to prepare the LLVM IR for code generation.
virtual void addRegAssignAndRewriteOptimized()
virtual void addRegAssignAndRewriteFast()
Add core register allocator passes which do the actual register assignment and rewriting.
virtual void addMachineLateOptimization()
Add passes that optimize machine instructions after register allocation.
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.
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:48
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
Target & getTheLoongArch64Target()
FunctionPass * createLoongArchExpandPseudoLegacyPass()
void initializeLoongArchDAGToDAGISelLegacyPass(PassRegistry &)
void initializeLoongArchExpandAtomicPseudoLegacyPass(PassRegistry &)
void initializeLoongArchMemoryBarrierOptLegacyPass(PassRegistry &)
LLVM_ABI FunctionPass * createTypePromotionLegacyPass()
Create IR Type Promotion pass.
void initializeLoongArchLateBranchOptLegacyPass(PassRegistry &)
FunctionPass * createLoongArchMemoryBarrierOptLegacyPass()
void initializeLoongArchDeadRegisterDefinitionsLegacyPass(PassRegistry &)
void initializeLoongArchMergeBaseOffsetLegacyPass(PassRegistry &)
FunctionPass * createLoongArchPreRAExpandPseudoLegacyPass()
FunctionPass * createLoongArchMergeBaseOffsetLegacyPass()
Returns an instance of the Merge Base Offset Optimization pass.
void initializeLoongArchPreRAExpandPseudoLegacyPass(PassRegistry &)
LLVM_ABI char & MachineCombinerID
This pass performs instruction combining using trace metrics to estimate critical-path and resource d...
static Reloc::Model getEffectiveRelocModel(std::optional< Reloc::Model > RM)
LLVM_ABI char & BranchRelaxationPassID
BranchRelaxation - This pass replaces branches that need to jump further than is supported by a branc...
FunctionPass * createLoongArchLateBranchOptLegacyPass()
void initializeLoongArchExpandPseudoLegacyPass(PassRegistry &)
LLVM_ABI FunctionPass * createLoopDataPrefetchPass()
FunctionPass * createLoongArchISelDagLegacyPass(LoongArchTargetMachine &TM, CodeGenOptLevel OptLevel)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
FunctionPass * createLoongArchOptWInstrsLegacyPass()
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Target & getTheLoongArch32Target()
void initializeLoongArchOptWInstrsLegacyPass(PassRegistry &)
LLVM_ABI FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
FunctionPass * createLoongArchExpandAtomicPseudoLegacyPass()
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
FunctionPass * createLoongArchDeadRegisterDefinitionsLegacyPass()
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
static FuncInfoTy * create(BumpPtrAllocator &Allocator, const Function &F, const SubtargetTy *STI)
Factory function: default behavior is to call new using the supplied allocator.
RegisterTargetMachine - Helper template for registering a target machine implementation,...