LLVM  15.0.0git
CSKYTargetMachine.cpp
Go to the documentation of this file.
1 //===--- CSKYTargetMachine.cpp - Define TargetMachine for CSKY ------------===//
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 CSKY target spec.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CSKYTargetMachine.h"
14 #include "CSKY.h"
15 #include "CSKYSubtarget.h"
16 #include "CSKYTargetObjectFile.h"
22 #include "llvm/MC/TargetRegistry.h"
23 
24 using namespace llvm;
25 
28 
31 }
32 
33 static std::string computeDataLayout(const Triple &TT) {
34  std::string Ret;
35 
36  // Only support little endian for now.
37  // TODO: Add support for big endian.
38  Ret += "e";
39 
40  // CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
41  // It's a 4-byte aligned stack with ELF mangling only.
42  Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
43  "-v128:32:32-a:0:32-Fi32-n32";
44 
45  return Ret;
46 }
47 
49  StringRef CPU, StringRef FS,
50  const TargetOptions &Options,
53  CodeGenOpt::Level OL, bool JIT)
55  RM.value_or(Reloc::Static),
57  TLOF(std::make_unique<CSKYELFTargetObjectFile>()) {
58  initAsmInfo();
59 }
60 
61 const CSKYSubtarget *
63  Attribute CPUAttr = F.getFnAttribute("target-cpu");
64  Attribute TuneAttr = F.getFnAttribute("tune-cpu");
65  Attribute FSAttr = F.getFnAttribute("target-features");
66 
67  std::string CPU =
68  CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
69  std::string TuneCPU =
70  TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
71  std::string FS =
72  FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
73 
74  std::string Key = CPU + TuneCPU + FS;
75  auto &I = SubtargetMap[Key];
76  if (!I) {
77  // This needs to be done before we create a new subtarget since any
78  // creation will depend on the TM and the code generation flags on the
79  // function that reside in TargetOptions.
81  I = std::make_unique<CSKYSubtarget>(TargetTriple, CPU, TuneCPU, FS, *this);
82  if (I->useHardFloat() && !I->hasAnyFloatExt())
83  errs() << "Hard-float can't be used with current CPU,"
84  " set to Soft-float\n";
85  }
86  return I.get();
87 }
88 
89 namespace {
90 class CSKYPassConfig : public TargetPassConfig {
91 public:
92  CSKYPassConfig(CSKYTargetMachine &TM, PassManagerBase &PM)
93  : TargetPassConfig(TM, PM) {}
94 
95  CSKYTargetMachine &getCSKYTargetMachine() const {
96  return getTM<CSKYTargetMachine>();
97  }
98 
99  void addIRPasses() override;
100  bool addInstSelector() override;
101  void addPreEmitPass() override;
102 };
103 
104 } // namespace
105 
107  return new CSKYPassConfig(*this, PM);
108 }
109 
110 void CSKYPassConfig::addIRPasses() {
111  addPass(createAtomicExpandPass());
113 }
114 
115 bool CSKYPassConfig::addInstSelector() {
116  addPass(createCSKYISelDag(getCSKYTargetMachine()));
117 
118  return false;
119 }
120 
121 void CSKYPassConfig::addPreEmitPass() {
122  addPass(createCSKYConstantIslandPass());
123 }
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
llvm::TargetOptions
Definition: TargetOptions.h:124
T
llvm::Function
Definition: Function.h:60
llvm::Attribute
Definition: Attributes.h:65
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:145
CSKYTargetMachine.h
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
CSKY.h
LLVMInitializeCSKYTarget
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYTarget()
Definition: CSKYTargetMachine.cpp:26
llvm::Optional< Reloc::Model >
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:893
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
llvm::createCSKYISelDag
FunctionPass * createCSKYISelDag(CSKYTargetMachine &TM)
Definition: CSKYISelDAGToDAG.cpp:397
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::getTheCSKYTarget
Target & getTheCSKYTarget()
Definition: CSKYTargetInfo.cpp:13
llvm::MSP430Attrs::CodeModel
CodeModel
Definition: MSP430Attributes.h:37
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
CSKYSubtarget.h
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
computeDataLayout
static std::string computeDataLayout(const Triple &TT)
Definition: CSKYTargetMachine.cpp:33
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
llvm::createAtomicExpandPass
FunctionPass * createAtomicExpandPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
llvm::RegisterTargetMachine
RegisterTargetMachine - Helper template for registering a target machine implementation,...
Definition: TargetRegistry.h:1318
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
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
llvm::CSKYSubtarget
Definition: CSKYSubtarget.h:30
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
llvm::CSKYTargetMachine::createPassConfig
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
Definition: CSKYTargetMachine.cpp:106
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::EngineKind::JIT
@ JIT
Definition: ExecutionEngine.h:524
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:126
llvm::CSKYELFTargetObjectFile
Definition: CSKYTargetObjectFile.h:17
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::TargetPassConfig::addIRPasses
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
Definition: TargetPassConfig.cpp:853
llvm::createCSKYConstantIslandPass
FunctionPass * createCSKYConstantIslandPass()
Returns a pass that converts branches to long branches.
Definition: CSKYConstantIslandPass.cpp:1370
TargetPassConfig.h
llvm::initializeCSKYConstantIslandsPass
void initializeCSKYConstantIslandsPass(PassRegistry &)
CSKYTargetObjectFile.h
llvm::X86AS::FS
@ FS
Definition: X86.h:196
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:471
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
TargetSubtargetInfo.h
llvm::CSKYTargetMachine
Definition: CSKYTargetMachine.h:22
llvm::CodeGenOpt::Level
Level
Definition: CodeGen.h:52
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
MachineFrameInfo.h
llvm::LLVMTargetMachine
This class describes a target machine that is implemented with the LLVM target-independent code gener...
Definition: TargetMachine.h:414
llvm::CSKYTargetMachine::CSKYTargetMachine
CSKYTargetMachine(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: CSKYTargetMachine.cpp:48
llvm::Registry
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
CSKYTargetInfo.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
TargetRegistry.h
llvm::TargetMachine::TargetCPU
std::string TargetCPU
Definition: TargetMachine.h:97
llvm::CSKYTargetMachine::getSubtargetImpl
const CSKYSubtarget * getSubtargetImpl() const =delete
TargetLoweringObjectFileImpl.h