LLVM  15.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 
13 #include "LoongArchTargetMachine.h"
14 #include "LoongArch.h"
17 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/MC/TargetRegistry.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "loongarch"
25 
27  // Register the target.
30 }
31 
32 static std::string computeDataLayout(const Triple &TT) {
33  if (TT.isArch64Bit())
34  return "e-m:e-p:64:64-i64:64-i128:128-n64-S128";
35  assert(TT.isArch32Bit() && "only LA32 and LA64 are currently supported");
36  return "e-m:e-p:32:32-i64:64-n32-S128";
37 }
38 
41  if (!RM.hasValue())
42  return Reloc::Static;
43  return *RM;
44 }
45 
47  const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
53  TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
54  initAsmInfo();
55 }
56 
58 
59 const LoongArchSubtarget *
61  Attribute CPUAttr = F.getFnAttribute("target-cpu");
62  Attribute TuneAttr = F.getFnAttribute("tune-cpu");
63  Attribute FSAttr = F.getFnAttribute("target-features");
64 
65  std::string CPU =
66  CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
67  std::string TuneCPU =
68  TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
69  std::string FS =
70  FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
71 
72  std::string Key = CPU + TuneCPU + FS;
73  auto &I = SubtargetMap[Key];
74  if (!I) {
75  // This needs to be done before we create a new subtarget since any
76  // creation will depend on the TM and the code generation flags on the
77  // function that reside in TargetOptions.
79  auto ABIName = Options.MCOptions.getABIName();
80  if (const MDString *ModuleTargetABI = dyn_cast_or_null<MDString>(
81  F.getParent()->getModuleFlag("target-abi"))) {
82  auto TargetABI = LoongArchABI::getTargetABI(ABIName);
83  if (TargetABI != LoongArchABI::ABI_Unknown &&
84  ModuleTargetABI->getString() != ABIName) {
85  report_fatal_error("-target-abi option != target-abi module flag");
86  }
87  ABIName = ModuleTargetABI->getString();
88  }
89  I = std::make_unique<LoongArchSubtarget>(TargetTriple, CPU, TuneCPU, FS,
90  ABIName, *this);
91  }
92  return I.get();
93 }
94 
95 namespace {
96 class LoongArchPassConfig : public TargetPassConfig {
97 public:
98  LoongArchPassConfig(LoongArchTargetMachine &TM, PassManagerBase &PM)
99  : TargetPassConfig(TM, PM) {}
100 
101  LoongArchTargetMachine &getLoongArchTargetMachine() const {
102  return getTM<LoongArchTargetMachine>();
103  }
104 
105  bool addInstSelector() override;
106 };
107 } // namespace
108 
111  return new LoongArchPassConfig(*this, PM);
112 }
113 
114 bool LoongArchPassConfig::addInstSelector() {
115  addPass(createLoongArchISelDag(getLoongArchTargetMachine()));
116 
117  return false;
118 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::Attribute::isValid
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:182
computeDataLayout
static std::string computeDataLayout(const Triple &TT)
Definition: LoongArchTargetMachine.cpp:32
llvm::TargetOptions
Definition: TargetOptions.h:124
llvm::getTheLoongArch32Target
Target & getTheLoongArch32Target()
Definition: LoongArchTargetInfo.cpp:13
T
llvm::Function
Definition: Function.h:60
llvm::Attribute
Definition: Attributes.h:65
llvm::LoongArchTargetMachine::LoongArchTargetMachine
LoongArchTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
Definition: LoongArchTargetMachine.cpp:46
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:145
llvm::MCTargetOptions::getABIName
StringRef getABIName() const
getABIName - If this returns a non-empty string this represents the textual name of the ABI that we w...
Definition: MCTargetOptions.cpp:23
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::TargetLoweringObjectFileELF
Definition: TargetLoweringObjectFileImpl.h:34
llvm::Optional< Reloc::Model >
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::Reloc::Model
Model
Definition: CodeGen.h:22
llvm::LoongArchTargetMachine::getSubtargetImpl
const LoongArchSubtarget * getSubtargetImpl() const =delete
llvm::MSP430Attrs::CodeModel
CodeModel
Definition: MSP430Attributes.h:37
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
llvm::TargetOptions::MCOptions
MCTargetOptions MCOptions
Machine level options.
Definition: TargetOptions.h:442
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
llvm::RegisterTargetMachine
RegisterTargetMachine - Helper template for registering a target machine implementation,...
Definition: TargetRegistry.h:1318
llvm::LoongArchABI::getTargetABI
ABI getTargetABI(StringRef ABIName)
Definition: LoongArchBaseInfo.cpp:23
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::TargetMachine::TargetFS
std::string TargetFS
Definition: TargetMachine.h:98
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:249
llvm::Attribute::getValueAsString
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:304
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::TargetMachine::resetTargetOptions
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
Definition: TargetMachine.cpp:53
Passes.h
llvm::TargetPassConfig
Target-Independent Code Generator Pass Configuration Options.
Definition: TargetPassConfig.h:84
llvm::TargetMachine::TargetTriple
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
Definition: TargetMachine.h:96
llvm::LoongArchABI::ABI_Unknown
@ ABI_Unknown
Definition: LoongArchBaseInfo.h:33
llvm::EngineKind::JIT
@ JIT
Definition: ExecutionEngine.h:524
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:126
llvm::LoongArchSubtarget
Definition: LoongArchSubtarget.h:32
I
#define I(x, y, z)
Definition: MD5.cpp:58
TargetPassConfig.h
llvm::LoongArchTargetMachine
Definition: LoongArchTargetMachine.h:21
llvm::TargetMachine::Options
TargetOptions Options
Definition: TargetMachine.h:118
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::X86AS::FS
@ FS
Definition: X86.h:192
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:471
LoongArch.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
LLVMInitializeLoongArchTarget
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTarget()
Definition: LoongArchTargetMachine.cpp:26
llvm::Reloc::Static
@ Static
Definition: CodeGen.h:22
llvm::CodeGenOpt::Level
Level
Definition: CodeGen.h:52
llvm::getEffectiveRelocModel
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
Definition: AVRTargetMachine.cpp:40
llvm::getEffectiveCodeModel
CodeModel::Model getEffectiveCodeModel(Optional< CodeModel::Model > CM, CodeModel::Model Default)
Helper method for getting the code model, returning Default if CM does not have a value.
Definition: TargetMachine.h:506
llvm::LLVMTargetMachine::initAsmInfo
void initAsmInfo()
Definition: LLVMTargetMachine.cpp:40
std
Definition: BitVector.h:851
LoongArchTargetInfo.h
llvm::LoongArchTargetMachine::createPassConfig
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
Definition: LoongArchTargetMachine.cpp:110
LoongArchBaseInfo.h
llvm::LLVMTargetMachine
This class describes a target machine that is implemented with the LLVM target-independent code gener...
Definition: TargetMachine.h:414
llvm::LoongArchTargetMachine::~LoongArchTargetMachine
~LoongArchTargetMachine() override
LoongArchTargetMachine.h
llvm::legacy::PassManagerBase
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
Definition: LegacyPassManager.h:39
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::createLoongArchISelDag
FunctionPass * createLoongArchISelDag(LoongArchTargetMachine &TM)
Definition: LoongArchISelDAGToDAG.cpp:130
llvm::MDString
A single uniqued string.
Definition: Metadata.h:612
TargetRegistry.h
llvm::TargetMachine::TargetCPU
std::string TargetCPU
Definition: TargetMachine.h:97
llvm::getTheLoongArch64Target
Target & getTheLoongArch64Target()
Definition: LoongArchTargetInfo.cpp:18
TargetLoweringObjectFileImpl.h