xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 //===-- LanaiTargetMachine.cpp - Define TargetMachine for Lanai ---------===//
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 Lanai target spec.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "LanaiTargetMachine.h"
14 
15 #include "Lanai.h"
16 #include "LanaiTargetObjectFile.h"
17 #include "LanaiTargetTransformInfo.h"
18 #include "TargetInfo/LanaiTargetInfo.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
22 #include "llvm/CodeGen/TargetPassConfig.h"
23 #include "llvm/MC/TargetRegistry.h"
24 #include "llvm/Support/FormattedStream.h"
25 #include "llvm/Target/TargetOptions.h"
26 
27 using namespace llvm;
28 
29 namespace llvm {
30 void initializeLanaiMemAluCombinerPass(PassRegistry &);
31 } // namespace llvm
32 
33 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLanaiTarget() {
34   // Register the target.
35   RegisterTargetMachine<LanaiTargetMachine> registered_target(
36       getTheLanaiTarget());
37 }
38 
39 static std::string computeDataLayout() {
40   // Data layout (keep in sync with clang/lib/Basic/Targets.cpp)
41   return "E"        // Big endian
42          "-m:e"     // ELF name manging
43          "-p:32:32" // 32-bit pointers, 32 bit aligned
44          "-i64:64"  // 64 bit integers, 64 bit aligned
45          "-a:0:32"  // 32 bit alignment of objects of aggregate type
46          "-n32"     // 32 bit native integer width
47          "-S64";    // 64 bit natural stack alignment
48 }
49 
50 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
51   return RM.getValueOr(Reloc::PIC_);
52 }
53 
54 LanaiTargetMachine::LanaiTargetMachine(const Target &T, const Triple &TT,
55                                        StringRef Cpu, StringRef FeatureString,
56                                        const TargetOptions &Options,
57                                        Optional<Reloc::Model> RM,
58                                        Optional<CodeModel::Model> CodeModel,
59                                        CodeGenOpt::Level OptLevel, bool JIT)
60     : LLVMTargetMachine(T, computeDataLayout(), TT, Cpu, FeatureString, Options,
61                         getEffectiveRelocModel(RM),
62                         getEffectiveCodeModel(CodeModel, CodeModel::Medium),
63                         OptLevel),
64       Subtarget(TT, Cpu, FeatureString, *this, Options, getCodeModel(),
65                 OptLevel),
66       TLOF(new LanaiTargetObjectFile()) {
67   initAsmInfo();
68 }
69 
70 TargetTransformInfo
71 LanaiTargetMachine::getTargetTransformInfo(const Function &F) {
72   return TargetTransformInfo(LanaiTTIImpl(this, F));
73 }
74 
75 namespace {
76 // Lanai Code Generator Pass Configuration Options.
77 class LanaiPassConfig : public TargetPassConfig {
78 public:
79   LanaiPassConfig(LanaiTargetMachine &TM, PassManagerBase *PassManager)
80       : TargetPassConfig(TM, *PassManager) {}
81 
82   LanaiTargetMachine &getLanaiTargetMachine() const {
83     return getTM<LanaiTargetMachine>();
84   }
85 
86   bool addInstSelector() override;
87   void addPreSched2() override;
88   void addPreEmitPass() override;
89 };
90 } // namespace
91 
92 TargetPassConfig *
93 LanaiTargetMachine::createPassConfig(PassManagerBase &PassManager) {
94   return new LanaiPassConfig(*this, &PassManager);
95 }
96 
97 // Install an instruction selector pass.
98 bool LanaiPassConfig::addInstSelector() {
99   addPass(createLanaiISelDag(getLanaiTargetMachine()));
100   return false;
101 }
102 
103 // Implemented by targets that want to run passes immediately before
104 // machine code is emitted.
105 void LanaiPassConfig::addPreEmitPass() {
106   addPass(createLanaiDelaySlotFillerPass(getLanaiTargetMachine()));
107 }
108 
109 // Run passes after prolog-epilog insertion and before the second instruction
110 // scheduling pass.
111 void LanaiPassConfig::addPreSched2() {
112   addPass(createLanaiMemAluCombinerPass());
113 }
114