LLVM 22.0.0git
M68kTargetMachine.cpp
Go to the documentation of this file.
1//===-- M68kTargetMachine.cpp - M68k Target Machine -------------*- C++ -*-===//
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/// \file
10/// This file contains implementation for M68k target machine.
11///
12//===----------------------------------------------------------------------===//
13
14#include "M68kTargetMachine.h"
15#include "M68k.h"
16#include "M68kMachineFunction.h"
17#include "M68kSubtarget.h"
24#include "llvm/CodeGen/Passes.h"
28#include "llvm/PassRegistry.h"
29#include <memory>
30#include <optional>
31
32using namespace llvm;
33
34#define DEBUG_TYPE "m68k"
35
45}
46
47namespace {
48
49std::string computeDataLayout(const Triple &TT, StringRef CPU,
50 const TargetOptions &Options) {
51 std::string Ret = "";
52 // M68k is Big Endian
53 Ret += "E";
54
55 // FIXME how to wire it with the used object format?
56 Ret += "-m:e";
57
58 // M68k pointers are always 32 bit wide even for 16-bit CPUs.
59 // The ABI only specifies 16-bit alignment.
60 // On at least the 68020+ with a 32-bit bus, there is a performance benefit
61 // to having 32-bit alignment.
62 Ret += "-p:32:16:32";
63
64 // Bytes do not require special alignment, words are word aligned and
65 // long words are word aligned at minimum.
66 Ret += "-i8:8:8-i16:16:16-i32:16:32";
67
68 // FIXME no floats at the moment
69
70 // The registers can hold 8, 16, 32 bits
71 Ret += "-n8:16:32";
72
73 Ret += "-a:0:16-S16";
74
75 return Ret;
76}
77
79 std::optional<Reloc::Model> RM) {
80 // If not defined we default to static
81 if (!RM.has_value())
82 return Reloc::Static;
83
84 return *RM;
85}
86
87CodeModel::Model getEffectiveCodeModel(std::optional<CodeModel::Model> CM,
88 bool JIT) {
89 if (!CM) {
90 return CodeModel::Small;
91 } else if (CM == CodeModel::Kernel) {
92 llvm_unreachable("Kernel code model is not implemented yet");
93 }
94 return CM.value();
95}
96} // end anonymous namespace
97
99 StringRef CPU, StringRef FS,
100 const TargetOptions &Options,
101 std::optional<Reloc::Model> RM,
102 std::optional<CodeModel::Model> CM,
103 CodeGenOptLevel OL, bool JIT)
105 FS, Options, getEffectiveRelocModel(TT, RM),
106 ::getEffectiveCodeModel(CM, JIT), OL),
107 TLOF(std::make_unique<M68kELFTargetObjectFile>()),
108 Subtarget(TT, CPU, FS, *this) {
109 initAsmInfo();
110}
111
113
114const M68kSubtarget *
116 Attribute CPUAttr = F.getFnAttribute("target-cpu");
117 Attribute FSAttr = F.getFnAttribute("target-features");
118
119 auto CPU = CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
120 auto FS = FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
121
122 auto &I = SubtargetMap[CPU + FS];
123 if (!I) {
124 // This needs to be done before we create a new subtarget since any
125 // creation will depend on the TM and the code generation flags on the
126 // function that reside in TargetOptions.
128 I = std::make_unique<M68kSubtarget>(TargetTriple, CPU, FS, *this);
129 }
130 return I.get();
131}
132
134 BumpPtrAllocator &Allocator, const Function &F,
135 const TargetSubtargetInfo *STI) const {
136 return M68kMachineFunctionInfo::create<M68kMachineFunctionInfo>(Allocator, F,
137 STI);
138}
139
140//===----------------------------------------------------------------------===//
141// Pass Pipeline Configuration
142//===----------------------------------------------------------------------===//
143
144namespace {
145class M68kPassConfig : public TargetPassConfig {
146public:
147 M68kPassConfig(M68kTargetMachine &TM, PassManagerBase &PM)
148 : TargetPassConfig(TM, PM) {}
149
150 M68kTargetMachine &getM68kTargetMachine() const {
151 return getTM<M68kTargetMachine>();
152 }
153
154 const M68kSubtarget &getM68kSubtarget() const {
155 return *getM68kTargetMachine().getSubtargetImpl();
156 }
157 void addIRPasses() override;
158 bool addIRTranslator() override;
159 bool addLegalizeMachineIR() override;
160 bool addRegBankSelect() override;
161 bool addGlobalInstructionSelect() override;
162 bool addInstSelector() override;
163 void addPreSched2() override;
164 void addPreEmitPass() override;
165};
166} // namespace
167
169 return new M68kPassConfig(*this, PM);
170}
171
172void M68kPassConfig::addIRPasses() {
175}
176
177bool M68kPassConfig::addInstSelector() {
178 // Install an instruction selector.
179 addPass(createM68kISelDag(getM68kTargetMachine()));
181 return false;
182}
183
184bool M68kPassConfig::addIRTranslator() {
185 addPass(new IRTranslator());
186 return false;
187}
188
189bool M68kPassConfig::addLegalizeMachineIR() {
190 addPass(new Legalizer());
191 return false;
192}
193
194bool M68kPassConfig::addRegBankSelect() {
195 addPass(new RegBankSelect());
196 return false;
197}
198
199bool M68kPassConfig::addGlobalInstructionSelect() {
200 addPass(new InstructionSelect());
201 return false;
202}
203
204void M68kPassConfig::addPreSched2() { addPass(createM68kExpandPseudoPass()); }
205
206void M68kPassConfig::addPreEmitPass() {
208}
static Reloc::Model getEffectiveRelocModel(const Triple &TT, std::optional< Reloc::Model > RM)
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file declares the IRTranslator pass.
static LVOptions Options
Definition: LVOptions.cpp:25
static std::string computeDataLayout()
This file declares the M68k specific subclass of MachineFunctionInfo.
This file declares the M68k specific subclass of TargetSubtargetInfo.
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeM68kTarget()
This file declares the M68k specific subclass of TargetMachine.
This file contains declarations for M68k ELF object file lowering.
This file contains the entry points for global functions defined in the M68k target library,...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Basic Register Allocator
This file describes the interface of the MachineFunctionPass responsible for assigning the generic vi...
Target-Independent Code Generator Pass Configuration Options pass.
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:400
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:223
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
implements a set of functionality in the TargetMachine class for targets that make use of the indepen...
This pass is responsible for selecting generic machine instructions to target-specific instructions.
const M68kSubtarget * getSubtargetImpl() const
MachineFunctionInfo * createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F, const TargetSubtargetInfo *STI) const override
Create the target's instance of MachineFunctionInfo.
M68kTargetMachine(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)
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This pass implements the reg bank selector pass used in the GlobalISel pipeline.
Definition: RegBankSelect.h:91
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:233
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
std::string TargetFS
std::string TargetCPU
std::unique_ptr< const MCSubtargetInfo > STI
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
Target-Independent Code Generator Pass Configuration Options.
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
TargetSubtargetInfo - Generic base class for all target subtargets.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeM68kGlobalBaseRegPass(PassRegistry &)
void initializeM68kExpandPseudoPass(PassRegistry &)
void initializeM68kCollapseMOVEMPass(PassRegistry &)
FunctionPass * createM68kGlobalBaseRegPass()
This pass initializes a global base register for PIC on M68k.
CodeModel::Model getEffectiveCodeModel(std::optional< CodeModel::Model > CM, CodeModel::Model Default)
Helper method for getting the code model, returning Default if CM does not have a value.
FunctionPass * createM68kCollapseMOVEMPass()
Finds sequential MOVEM instruction and collapse them into a single one.
FunctionPass * createM68kISelDag(M68kTargetMachine &TM)
This pass converts a legalized DAG into a M68k-specific DAG, ready for instruction scheduling.
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:82
void initializeM68kDAGToDAGISelLegacyPass(PassRegistry &)
LLVM_ABI void initializeGlobalISel(PassRegistry &)
Initialize all passes linked into the GlobalISel library.
Definition: GlobalISel.cpp:17
Target & getTheM68kTarget()
FunctionPass * createM68kExpandPseudoPass()
Return a Machine IR pass that expands M68k-specific pseudo instructions into a sequence of actual ins...
LLVM_ABI FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
void initializeM68kAsmPrinterPass(PassRegistry &)
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
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,...