Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===//
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
//
10
//===----------------------------------------------------------------------===//
11
12
#include "SparcTargetMachine.h"
13
#include "LeonPasses.h"
14
#include "Sparc.h"
15
#include "SparcMachineFunctionInfo.h"
16
#include "SparcTargetObjectFile.h"
17
#include "TargetInfo/SparcTargetInfo.h"
18
#include "llvm/CodeGen/Passes.h"
19
#include "llvm/CodeGen/TargetPassConfig.h"
20
#include "llvm/MC/TargetRegistry.h"
21
#include <optional>
22
using namespace llvm;
23
24
62
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSparcTarget() {
25
  // Register the target.
26
62
  RegisterTargetMachine<SparcV8TargetMachine> X(getTheSparcTarget());
27
62
  RegisterTargetMachine<SparcV9TargetMachine> Y(getTheSparcV9Target());
28
62
  RegisterTargetMachine<SparcelTargetMachine> Z(getTheSparcelTarget());
29
30
62
  PassRegistry &PR = *PassRegistry::getPassRegistry();
31
62
  initializeSparcDAGToDAGISelPass(PR);
32
62
}
33
34
static cl::opt<bool>
35
    BranchRelaxation("sparc-enable-branch-relax", cl::Hidden, cl::init(true),
36
                     cl::desc("Relax out of range conditional branches"));
37
38
0
static std::string computeDataLayout(const Triple &T, bool is64Bit) {
39
  // Sparc is typically big endian, but some are little.
40
0
  std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
41
0
  Ret += "-m:e";
42
43
  // Some ABIs have 32bit pointers.
44
0
  if (!is64Bit)
45
0
    Ret += "-p:32:32";
46
47
  // Alignments for 64 bit integers.
48
0
  Ret += "-i64:64";
49
50
  // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
51
  // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
52
0
  if (is64Bit)
53
0
    Ret += "-n32:64";
54
0
  else
55
0
    Ret += "-f128:64-n32";
56
57
0
  if (is64Bit)
58
0
    Ret += "-S128";
59
0
  else
60
0
    Ret += "-S64";
61
62
0
  return Ret;
63
0
}
64
65
0
static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
66
0
  return RM.value_or(Reloc::Static);
67
0
}
68
69
// Code models. Some only make sense for 64-bit code.
70
//
71
// SunCC  Reloc   CodeModel  Constraints
72
// abs32  Static  Small      text+data+bss linked below 2^32 bytes
73
// abs44  Static  Medium     text+data+bss linked below 2^44 bytes
74
// abs64  Static  Large      text smaller than 2^31 bytes
75
// pic13  PIC_    Small      GOT < 2^13 bytes
76
// pic32  PIC_    Medium     GOT < 2^32 bytes
77
//
78
// All code models require that the text segment is smaller than 2GB.
79
static CodeModel::Model
80
getEffectiveSparcCodeModel(std::optional<CodeModel::Model> CM, Reloc::Model RM,
81
0
                           bool Is64Bit, bool JIT) {
82
0
  if (CM) {
83
0
    if (*CM == CodeModel::Tiny)
84
0
      report_fatal_error("Target does not support the tiny CodeModel", false);
85
0
    if (*CM == CodeModel::Kernel)
86
0
      report_fatal_error("Target does not support the kernel CodeModel", false);
87
0
    return *CM;
88
0
  }
89
0
  if (Is64Bit) {
90
0
    if (JIT)
91
0
      return CodeModel::Large;
92
0
    return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium;
93
0
  }
94
0
  return CodeModel::Small;
95
0
}
96
97
/// Create an ILP32 architecture model
98
SparcTargetMachine::SparcTargetMachine(const Target &T, const Triple &TT,
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,
104
                                       bool is64bit)
105
    : LLVMTargetMachine(T, computeDataLayout(TT, is64bit), TT, CPU, FS, Options,
106
                        getEffectiveRelocModel(RM),
107
                        getEffectiveSparcCodeModel(
108
                            CM, getEffectiveRelocModel(RM), is64bit, JIT),
109
                        OL),
110
0
      TLOF(std::make_unique<SparcELFTargetObjectFile>()), is64Bit(is64bit) {
111
0
  initAsmInfo();
112
0
}
113
114
0
SparcTargetMachine::~SparcTargetMachine() = default;
115
116
const SparcSubtarget *
117
0
SparcTargetMachine::getSubtargetImpl(const Function &F) const {
118
0
  Attribute CPUAttr = F.getFnAttribute("target-cpu");
119
0
  Attribute TuneAttr = F.getFnAttribute("tune-cpu");
120
0
  Attribute FSAttr = F.getFnAttribute("target-features");
121
122
0
  std::string CPU =
123
0
      CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
124
0
  std::string TuneCPU =
125
0
      TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
126
0
  std::string FS =
127
0
      FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
128
129
  // FIXME: This is related to the code below to reset the target options,
130
  // we need to know whether or not the soft float flag is set on the
131
  // function, so we can enable it as a subtarget feature.
132
0
  bool softFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
133
134
0
  if (softFloat)
135
0
    FS += FS.empty() ? "+soft-float" : ",+soft-float";
136
137
0
  auto &I = SubtargetMap[CPU + FS];
138
0
  if (!I) {
139
    // This needs to be done before we create a new subtarget since any
140
    // creation will depend on the TM and the code generation flags on the
141
    // function that reside in TargetOptions.
142
0
    resetTargetOptions(F);
143
0
    I = std::make_unique<SparcSubtarget>(CPU, TuneCPU, FS, *this,
144
0
                                         this->is64Bit);
145
0
  }
146
0
  return I.get();
147
0
}
148
149
MachineFunctionInfo *SparcTargetMachine::createMachineFunctionInfo(
150
    BumpPtrAllocator &Allocator, const Function &F,
151
0
    const TargetSubtargetInfo *STI) const {
152
0
  return SparcMachineFunctionInfo::create<SparcMachineFunctionInfo>(Allocator,
153
0
                                                                    F, STI);
154
0
}
155
156
namespace {
157
/// Sparc Code Generator Pass Configuration Options.
158
class SparcPassConfig : public TargetPassConfig {
159
public:
160
  SparcPassConfig(SparcTargetMachine &TM, PassManagerBase &PM)
161
0
    : TargetPassConfig(TM, PM) {}
162
163
0
  SparcTargetMachine &getSparcTargetMachine() const {
164
0
    return getTM<SparcTargetMachine>();
165
0
  }
166
167
  void addIRPasses() override;
168
  bool addInstSelector() override;
169
  void addPreEmitPass() override;
170
};
171
} // namespace
172
173
0
TargetPassConfig *SparcTargetMachine::createPassConfig(PassManagerBase &PM) {
174
0
  return new SparcPassConfig(*this, PM);
175
0
}
176
177
0
void SparcPassConfig::addIRPasses() {
178
0
  addPass(createAtomicExpandPass());
179
180
0
  TargetPassConfig::addIRPasses();
181
0
}
182
183
0
bool SparcPassConfig::addInstSelector() {
184
0
  addPass(createSparcISelDag(getSparcTargetMachine()));
185
0
  return false;
186
0
}
187
188
0
void SparcPassConfig::addPreEmitPass(){
189
0
  if (BranchRelaxation)
190
0
    addPass(&BranchRelaxationPassID);
191
192
0
  addPass(createSparcDelaySlotFillerPass());
193
0
  addPass(new InsertNOPLoad());
194
0
  addPass(new DetectRoundChange());
195
0
  addPass(new FixAllFDIVSQRT());
196
0
}
197
198
0
void SparcV8TargetMachine::anchor() { }
199
200
SparcV8TargetMachine::SparcV8TargetMachine(const Target &T, const Triple &TT,
201
                                           StringRef CPU, StringRef FS,
202
                                           const TargetOptions &Options,
203
                                           std::optional<Reloc::Model> RM,
204
                                           std::optional<CodeModel::Model> CM,
205
                                           CodeGenOptLevel OL, bool JIT)
206
0
    : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
207
208
0
void SparcV9TargetMachine::anchor() { }
209
210
SparcV9TargetMachine::SparcV9TargetMachine(const Target &T, const Triple &TT,
211
                                           StringRef CPU, StringRef FS,
212
                                           const TargetOptions &Options,
213
                                           std::optional<Reloc::Model> RM,
214
                                           std::optional<CodeModel::Model> CM,
215
                                           CodeGenOptLevel OL, bool JIT)
216
0
    : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
217
218
0
void SparcelTargetMachine::anchor() {}
219
220
SparcelTargetMachine::SparcelTargetMachine(const Target &T, const Triple &TT,
221
                                           StringRef CPU, StringRef FS,
222
                                           const TargetOptions &Options,
223
                                           std::optional<Reloc::Model> RM,
224
                                           std::optional<CodeModel::Model> CM,
225
                                           CodeGenOptLevel OL, bool JIT)
226
0
    : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}