Skip to content

Commit 7841cf4

Browse files
committed
Refactored LLVMTargetMachine into CodeGenCommonTMImpl, and moved its
interface under the CodeGen library. Made TargetMachine to be the only interface directly used everywhere and got rid of all the static_casts.
1 parent 9254b81 commit 7841cf4

File tree

111 files changed

+435
-419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+435
-419
lines changed

llvm/docs/WritingAnLLVMBackend.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ To make your target actually do something, you need to implement a subclass of
152152
``lib/Target/DummyTargetMachine.cpp``, but any file in the ``lib/Target``
153153
directory will be built and should work. To use LLVM's target independent code
154154
generator, you should do what all current machine backends do: create a
155-
subclass of ``LLVMTargetMachine``. (To create a target from scratch, create a
155+
subclass of ``CodeGenCommonTMImpl``. (To create a target from scratch, create a
156156
subclass of ``TargetMachine``.)
157157

158158
To get LLVM to actually build and link your target, you need to run ``cmake``
@@ -165,15 +165,15 @@ located in the main ``CMakeLists.txt``.
165165
Target Machine
166166
==============
167167

168-
``LLVMTargetMachine`` is designed as a base class for targets implemented with
169-
the LLVM target-independent code generator. The ``LLVMTargetMachine`` class
168+
``CodeGenCommonTMImpl`` is designed as a base class for targets implemented with
169+
the LLVM target-independent code generator. The ``CodeGenCommonTMImpl`` class
170170
should be specialized by a concrete target class that implements the various
171-
virtual methods. ``LLVMTargetMachine`` is defined as a subclass of
172-
``TargetMachine`` in ``include/llvm/Target/TargetMachine.h``. The
173-
``TargetMachine`` class implementation (``TargetMachine.cpp``) also processes
174-
numerous command-line options.
171+
virtual methods. ``CodeGenCommonTMImpl`` is defined as a subclass of
172+
``TargetMachine`` in ``include/llvm/CodeGen/CodeGenCommonTMImpl.h``. The
173+
``TargetMachine`` class implementation (``include/llvm/Target/TargetMachine.cpp``)
174+
also processes numerous command-line options.
175175

176-
To create a concrete target-specific subclass of ``LLVMTargetMachine``, start
176+
To create a concrete target-specific subclass of ``CodeGenCommonTMImpl``, start
177177
by copying an existing ``TargetMachine`` class and header. You should name the
178178
files that you create to reflect your specific target. For instance, for the
179179
SPARC target, name the files ``SparcTargetMachine.h`` and
@@ -197,7 +197,7 @@ simply return a class member.
197197

198198
class Module;
199199

200-
class SparcTargetMachine : public LLVMTargetMachine {
200+
class SparcTargetMachine : public CodeGenCommonTMImpl {
201201
const DataLayout DataLayout; // Calculates type size & alignment
202202
SparcSubtarget Subtarget;
203203
SparcInstrInfo InstrInfo;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//===-- CodeGenCommonTMImpl.h -----------------------------------*- 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 This file describes the CodeGenCommonTMImpl class, which
10+
/// implements a set of functionality used by \c TargetMachine classes in
11+
/// LLVM that make use of the target-independent code generator.
12+
//===----------------------------------------------------------------------===//
13+
#ifndef LLVM_CODEGEN_CODEGENCOMMONTMIMPL_H
14+
#define LLVM_CODEGEN_CODEGENCOMMONTMIMPL_H
15+
#include "llvm/Target/TargetMachine.h"
16+
17+
namespace llvm {
18+
19+
/// \brief implements a set of functionality in the \c TargetMachine class
20+
/// for targets that make use of the independent code generator (CodeGen)
21+
/// library. Must not be used directly in code unless to inherit its
22+
/// implementation.
23+
class CodeGenCommonTMImpl : public TargetMachine {
24+
protected: // Can only create subclasses.
25+
CodeGenCommonTMImpl(const Target &T, StringRef DataLayoutString,
26+
const Triple &TT, StringRef CPU, StringRef FS,
27+
const TargetOptions &Options, Reloc::Model RM,
28+
CodeModel::Model CM, CodeGenOptLevel OL);
29+
30+
void initAsmInfo();
31+
32+
public:
33+
/// Get a TargetTransformInfo implementation for the target.
34+
///
35+
/// The TTI returned uses the common code generator to answer queries about
36+
/// the IR.
37+
TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
38+
39+
/// Create a pass configuration object to be used by addPassToEmitX methods
40+
/// for generating a pipeline of CodeGen passes.
41+
virtual TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
42+
43+
/// Add passes to the specified pass manager to get the specified file
44+
/// emitted. Typically this will involve several steps of code generation.
45+
/// \p MMIWP is an optional parameter that, if set to non-nullptr,
46+
/// will be used to set the MachineModuloInfo for this PM.
47+
bool
48+
addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
49+
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
50+
bool DisableVerify = true,
51+
MachineModuleInfoWrapperPass *MMIWP = nullptr) override;
52+
53+
/// Add passes to the specified pass manager to get machine code emitted with
54+
/// the MCJIT. This method returns true if machine code is not supported. It
55+
/// fills the MCContext Ctx pointer which can be used to build custom
56+
/// MCStreamer.
57+
bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
58+
raw_pwrite_stream &Out,
59+
bool DisableVerify = true) override;
60+
61+
/// Adds an AsmPrinter pass to the pipeline that prints assembly or
62+
/// machine code from the MI representation.
63+
bool addAsmPrinter(PassManagerBase &PM, raw_pwrite_stream &Out,
64+
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
65+
MCContext &Context) override;
66+
67+
Expected<std::unique_ptr<MCStreamer>>
68+
createMCStreamer(raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
69+
CodeGenFileType FileType, MCContext &Ctx) override;
70+
};
71+
72+
/// Helper method for getting the code model, returning Default if
73+
/// CM does not have a value. The tiny and kernel models will produce
74+
/// an error, so targets that support them or require more complex codemodel
75+
/// selection logic should implement and call their own getEffectiveCodeModel.
76+
inline CodeModel::Model
77+
getEffectiveCodeModel(std::optional<CodeModel::Model> CM,
78+
CodeModel::Model Default) {
79+
if (CM) {
80+
// By default, targets do not support the tiny and kernel models.
81+
if (*CM == CodeModel::Tiny)
82+
report_fatal_error("Target does not support the tiny CodeModel", false);
83+
if (*CM == CodeModel::Kernel)
84+
report_fatal_error("Target does not support the kernel CodeModel", false);
85+
return *CM;
86+
}
87+
return Default;
88+
}
89+
90+
} // namespace llvm
91+
92+
#endif

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DILocation;
5454
class Function;
5555
class GISelChangeObserver;
5656
class GlobalValue;
57-
class LLVMTargetMachine;
57+
class TargetMachine;
5858
class MachineConstantPool;
5959
class MachineFrameInfo;
6060
class MachineFunction;
@@ -256,7 +256,7 @@ struct LandingPadInfo {
256256

257257
class LLVM_ABI MachineFunction {
258258
Function &F;
259-
const LLVMTargetMachine &Target;
259+
const TargetMachine &Target;
260260
const TargetSubtargetInfo *STI;
261261
MCContext &Ctx;
262262

@@ -634,7 +634,7 @@ class LLVM_ABI MachineFunction {
634634
/// for instructions that have a stack spill fused into them.
635635
const static unsigned int DebugOperandMemNumber;
636636

637-
MachineFunction(Function &F, const LLVMTargetMachine &Target,
637+
MachineFunction(Function &F, const TargetMachine &Target,
638638
const TargetSubtargetInfo &STI, MCContext &Ctx,
639639
unsigned FunctionNum);
640640
MachineFunction(const MachineFunction &) = delete;
@@ -707,7 +707,7 @@ class LLVM_ABI MachineFunction {
707707
void assignBeginEndSections();
708708

709709
/// getTarget - Return the target machine this machine code is compiled with
710-
const LLVMTargetMachine &getTarget() const { return Target; }
710+
const TargetMachine &getTarget() const { return Target; }
711711

712712
/// getSubtarget - Return the subtarget for which this machine code is being
713713
/// compiled.

llvm/include/llvm/CodeGen/MachineFunctionAnalysis.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace llvm {
1919

2020
class MachineFunction;
21-
class LLVMTargetMachine;
21+
class TargetMachine;
2222

2323
/// This analysis create MachineFunction for given Function.
2424
/// To release the MachineFunction, users should invalidate it explicitly.
@@ -28,7 +28,7 @@ class MachineFunctionAnalysis
2828

2929
static AnalysisKey Key;
3030

31-
const LLVMTargetMachine *TM;
31+
const TargetMachine *TM;
3232

3333
public:
3434
class Result {
@@ -41,7 +41,7 @@ class MachineFunctionAnalysis
4141
FunctionAnalysisManager::Invalidator &);
4242
};
4343

44-
MachineFunctionAnalysis(const LLVMTargetMachine *TM) : TM(TM){};
44+
MachineFunctionAnalysis(const TargetMachine *TM) : TM(TM) {};
4545
Result run(Function &F, FunctionAnalysisManager &FAM);
4646
};
4747

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
namespace llvm {
4444

4545
class Function;
46-
class LLVMTargetMachine;
46+
class TargetMachine;
4747
class MachineFunction;
4848
class Module;
4949

@@ -83,7 +83,7 @@ class MachineModuleInfo {
8383
friend class MachineModuleInfoWrapperPass;
8484
friend class MachineModuleAnalysis;
8585

86-
const LLVMTargetMachine &TM;
86+
const TargetMachine &TM;
8787

8888
/// This is the MCContext used for the entire code generator.
8989
MCContext Context;
@@ -109,10 +109,9 @@ class MachineModuleInfo {
109109
MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
110110

111111
public:
112-
explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
112+
explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
113113

114-
explicit MachineModuleInfo(const LLVMTargetMachine *TM,
115-
MCContext *ExtContext);
114+
explicit MachineModuleInfo(const TargetMachine *TM, MCContext *ExtContext);
116115

117116
MachineModuleInfo(MachineModuleInfo &&MMII);
118117

@@ -121,7 +120,7 @@ class MachineModuleInfo {
121120
void initialize();
122121
void finalize();
123122

124-
const LLVMTargetMachine &getTarget() const { return TM; }
123+
const TargetMachine &getTarget() const { return TM; }
125124

126125
const MCContext &getContext() const {
127126
return ExternalContext ? *ExternalContext : Context;
@@ -173,9 +172,9 @@ class MachineModuleInfoWrapperPass : public ImmutablePass {
173172

174173
public:
175174
static char ID; // Pass identification, replacement for typeid
176-
explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM = nullptr);
175+
explicit MachineModuleInfoWrapperPass(const TargetMachine *TM = nullptr);
177176

178-
explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
177+
explicit MachineModuleInfoWrapperPass(const TargetMachine *TM,
179178
MCContext *ExtContext);
180179

181180
// Initialization and Finalization

llvm/include/llvm/CodeGen/RegisterUsageInfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
namespace llvm {
3030

3131
class Function;
32-
class LLVMTargetMachine;
32+
class TargetMachine;
3333

3434
class PhysicalRegisterUsageInfo : public ImmutablePass {
3535
public:
@@ -41,7 +41,7 @@ class PhysicalRegisterUsageInfo : public ImmutablePass {
4141
}
4242

4343
/// Set TargetMachine which is used to print analysis.
44-
void setTargetMachine(const LLVMTargetMachine &TM);
44+
void setTargetMachine(const TargetMachine &TM);
4545

4646
bool doInitialization(Module &M) override;
4747

@@ -63,7 +63,7 @@ class PhysicalRegisterUsageInfo : public ImmutablePass {
6363
/// and 1 means content of register will be preserved around function call.
6464
DenseMap<const Function *, std::vector<uint32_t>> RegMasks;
6565

66-
const LLVMTargetMachine *TM = nullptr;
66+
const TargetMachine *TM = nullptr;
6767
};
6868

6969
} // end namespace llvm

llvm/include/llvm/CodeGen/ScheduleDAG.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace llvm {
3232

3333
template <class GraphType> struct GraphTraits;
3434
template<class Graph> class GraphWriter;
35-
class LLVMTargetMachine;
35+
class TargetMachine;
3636
class MachineFunction;
3737
class MachineRegisterInfo;
3838
class MCInstrDesc;
@@ -571,7 +571,7 @@ class TargetRegisterInfo;
571571

572572
class ScheduleDAG {
573573
public:
574-
const LLVMTargetMachine &TM; ///< Target processor
574+
const TargetMachine &TM; ///< Target processor
575575
const TargetInstrInfo *TII; ///< Target instruction information
576576
const TargetRegisterInfo *TRI; ///< Target processor register info
577577
MachineFunction &MF; ///< Machine function

llvm/include/llvm/CodeGen/TargetPassConfig.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace llvm {
2323

24-
class LLVMTargetMachine;
24+
class TargetMachine;
2525
struct MachineSchedContext;
2626
class PassConfigImpl;
2727
class ScheduleDAGInstrs;
@@ -120,7 +120,7 @@ class TargetPassConfig : public ImmutablePass {
120120
void setStartStopPasses();
121121

122122
protected:
123-
LLVMTargetMachine *TM;
123+
TargetMachine *TM;
124124
PassConfigImpl *Impl = nullptr; // Internal data structures
125125
bool Initialized = false; // Flagged after all passes are configured.
126126

@@ -148,7 +148,7 @@ class TargetPassConfig : public ImmutablePass {
148148
bool addCoreISelPasses();
149149

150150
public:
151-
TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm);
151+
TargetPassConfig(TargetMachine &TM, PassManagerBase &PM);
152152
// Dummy constructor.
153153
TargetPassConfig();
154154

@@ -413,7 +413,7 @@ class TargetPassConfig : public ImmutablePass {
413413
virtual void addFastRegAlloc();
414414

415415
/// addOptimizedRegAlloc - Add passes related to register allocation.
416-
/// LLVMTargetMachine provides standard regalloc passes for most targets.
416+
/// CodeGenCommonTMImpl provides standard regalloc passes for most targets.
417417
virtual void addOptimizedRegAlloc();
418418

419419
/// addPreRewrite - Add passes to the optimized register allocation pipeline
@@ -497,7 +497,7 @@ class TargetPassConfig : public ImmutablePass {
497497
};
498498

499499
void registerCodeGenCallback(PassInstrumentationCallbacks &PIC,
500-
LLVMTargetMachine &);
500+
TargetMachine &);
501501

502502
} // end namespace llvm
503503

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
442442
Error addFastRegAlloc(AddMachinePass &) const;
443443

444444
/// addOptimizedRegAlloc - Add passes related to register allocation.
445-
/// LLVMTargetMachine provides standard regalloc passes for most targets.
445+
/// CodeGenCommonTMImpl provides standard regalloc passes for most targets.
446446
void addOptimizedRegAlloc(AddMachinePass &) const;
447447

448448
/// Add passes that optimize machine instructions after register allocation.

0 commit comments

Comments
 (0)