1 //===-- LanaiTargetMachine.cpp - Define TargetMachine for Lanai ---------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implements the info about Lanai target spec.
11 //===----------------------------------------------------------------------===//
13 #include "LanaiTargetMachine.h"
16 #include "LanaiMachineFunctionInfo.h"
17 #include "LanaiTargetObjectFile.h"
18 #include "LanaiTargetTransformInfo.h"
19 #include "TargetInfo/LanaiTargetInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
23 #include "llvm/CodeGen/TargetPassConfig.h"
24 #include "llvm/MC/TargetRegistry.h"
25 #include "llvm/Support/FormattedStream.h"
26 #include "llvm/Target/TargetOptions.h"
32 void initializeLanaiMemAluCombinerPass(PassRegistry
&);
35 extern "C" LLVM_EXTERNAL_VISIBILITY
void LLVMInitializeLanaiTarget() {
36 // Register the target.
37 RegisterTargetMachine
<LanaiTargetMachine
> registered_target(
39 PassRegistry
&PR
= *PassRegistry::getPassRegistry();
40 initializeLanaiDAGToDAGISelLegacyPass(PR
);
43 static std::string
computeDataLayout() {
44 // Data layout (keep in sync with clang/lib/Basic/Targets.cpp)
45 return "E" // Big endian
46 "-m:e" // ELF name manging
47 "-p:32:32" // 32-bit pointers, 32 bit aligned
48 "-i64:64" // 64 bit integers, 64 bit aligned
49 "-a:0:32" // 32 bit alignment of objects of aggregate type
50 "-n32" // 32 bit native integer width
51 "-S64"; // 64 bit natural stack alignment
54 static Reloc::Model
getEffectiveRelocModel(std::optional
<Reloc::Model
> RM
) {
55 return RM
.value_or(Reloc::PIC_
);
58 LanaiTargetMachine::LanaiTargetMachine(
59 const Target
&T
, const Triple
&TT
, StringRef Cpu
, StringRef FeatureString
,
60 const TargetOptions
&Options
, std::optional
<Reloc::Model
> RM
,
61 std::optional
<CodeModel::Model
> CodeModel
, CodeGenOptLevel OptLevel
,
63 : LLVMTargetMachine(T
, computeDataLayout(), TT
, Cpu
, FeatureString
, Options
,
64 getEffectiveRelocModel(RM
),
65 getEffectiveCodeModel(CodeModel
, CodeModel::Medium
),
67 Subtarget(TT
, Cpu
, FeatureString
, *this, Options
, getCodeModel(),
69 TLOF(new LanaiTargetObjectFile()) {
74 LanaiTargetMachine::getTargetTransformInfo(const Function
&F
) const {
75 return TargetTransformInfo(LanaiTTIImpl(this, F
));
78 MachineFunctionInfo
*LanaiTargetMachine::createMachineFunctionInfo(
79 BumpPtrAllocator
&Allocator
, const Function
&F
,
80 const TargetSubtargetInfo
*STI
) const {
81 return LanaiMachineFunctionInfo::create
<LanaiMachineFunctionInfo
>(Allocator
,
86 // Lanai Code Generator Pass Configuration Options.
87 class LanaiPassConfig
: public TargetPassConfig
{
89 LanaiPassConfig(LanaiTargetMachine
&TM
, PassManagerBase
*PassManager
)
90 : TargetPassConfig(TM
, *PassManager
) {}
92 LanaiTargetMachine
&getLanaiTargetMachine() const {
93 return getTM
<LanaiTargetMachine
>();
96 void addIRPasses() override
;
97 bool addInstSelector() override
;
98 void addPreSched2() override
;
99 void addPreEmitPass() override
;
104 LanaiTargetMachine::createPassConfig(PassManagerBase
&PassManager
) {
105 return new LanaiPassConfig(*this, &PassManager
);
108 void LanaiPassConfig::addIRPasses() {
109 addPass(createAtomicExpandLegacyPass());
111 TargetPassConfig::addIRPasses();
114 // Install an instruction selector pass.
115 bool LanaiPassConfig::addInstSelector() {
116 addPass(createLanaiISelDag(getLanaiTargetMachine()));
120 // Implemented by targets that want to run passes immediately before
121 // machine code is emitted.
122 void LanaiPassConfig::addPreEmitPass() {
123 addPass(createLanaiDelaySlotFillerPass(getLanaiTargetMachine()));
126 // Run passes after prolog-epilog insertion and before the second instruction
128 void LanaiPassConfig::addPreSched2() {
129 addPass(createLanaiMemAluCombinerPass());