1 //===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===//
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 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #include "SparcTargetMachine.h"
13 #include "LeonPasses.h"
15 #include "SparcTargetObjectFile.h"
16 #include "TargetInfo/SparcTargetInfo.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/Support/TargetRegistry.h"
23 extern "C" LLVM_EXTERNAL_VISIBILITY
void LLVMInitializeSparcTarget() {
24 // Register the target.
25 RegisterTargetMachine
<SparcV8TargetMachine
> X(getTheSparcTarget());
26 RegisterTargetMachine
<SparcV9TargetMachine
> Y(getTheSparcV9Target());
27 RegisterTargetMachine
<SparcelTargetMachine
> Z(getTheSparcelTarget());
30 static std::string
computeDataLayout(const Triple
&T
, bool is64Bit
) {
31 // Sparc is typically big endian, but some are little.
32 std::string Ret
= T
.getArch() == Triple::sparcel
? "e" : "E";
35 // Some ABIs have 32bit pointers.
39 // Alignments for 64 bit integers.
42 // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
43 // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
47 Ret
+= "-f128:64-n32";
57 static Reloc::Model
getEffectiveRelocModel(Optional
<Reloc::Model
> RM
) {
58 return RM
.getValueOr(Reloc::Static
);
61 // Code models. Some only make sense for 64-bit code.
63 // SunCC Reloc CodeModel Constraints
64 // abs32 Static Small text+data+bss linked below 2^32 bytes
65 // abs44 Static Medium text+data+bss linked below 2^44 bytes
66 // abs64 Static Large text smaller than 2^31 bytes
67 // pic13 PIC_ Small GOT < 2^13 bytes
68 // pic32 PIC_ Medium GOT < 2^32 bytes
70 // All code models require that the text segment is smaller than 2GB.
71 static CodeModel::Model
72 getEffectiveSparcCodeModel(Optional
<CodeModel::Model
> CM
, Reloc::Model RM
,
73 bool Is64Bit
, bool JIT
) {
75 if (*CM
== CodeModel::Tiny
)
76 report_fatal_error("Target does not support the tiny CodeModel", false);
77 if (*CM
== CodeModel::Kernel
)
78 report_fatal_error("Target does not support the kernel CodeModel", false);
83 return CodeModel::Large
;
84 return RM
== Reloc::PIC_
? CodeModel::Small
: CodeModel::Medium
;
86 return CodeModel::Small
;
89 /// Create an ILP32 architecture model
90 SparcTargetMachine::SparcTargetMachine(
91 const Target
&T
, const Triple
&TT
, StringRef CPU
, StringRef FS
,
92 const TargetOptions
&Options
, Optional
<Reloc::Model
> RM
,
93 Optional
<CodeModel::Model
> CM
, CodeGenOpt::Level OL
, bool JIT
, bool is64bit
)
94 : LLVMTargetMachine(T
, computeDataLayout(TT
, is64bit
), TT
, CPU
, FS
, Options
,
95 getEffectiveRelocModel(RM
),
96 getEffectiveSparcCodeModel(
97 CM
, getEffectiveRelocModel(RM
), is64bit
, JIT
),
99 TLOF(std::make_unique
<SparcELFTargetObjectFile
>()),
100 Subtarget(TT
, std::string(CPU
), std::string(FS
), *this, is64bit
),
105 SparcTargetMachine::~SparcTargetMachine() {}
107 const SparcSubtarget
*
108 SparcTargetMachine::getSubtargetImpl(const Function
&F
) const {
109 Attribute CPUAttr
= F
.getFnAttribute("target-cpu");
110 Attribute FSAttr
= F
.getFnAttribute("target-features");
113 CPUAttr
.isValid() ? CPUAttr
.getValueAsString().str() : TargetCPU
;
115 FSAttr
.isValid() ? FSAttr
.getValueAsString().str() : TargetFS
;
117 // FIXME: This is related to the code below to reset the target options,
118 // we need to know whether or not the soft float flag is set on the
119 // function, so we can enable it as a subtarget feature.
120 bool softFloat
= F
.getFnAttribute("use-soft-float").getValueAsBool();
123 FS
+= FS
.empty() ? "+soft-float" : ",+soft-float";
125 auto &I
= SubtargetMap
[CPU
+ FS
];
127 // This needs to be done before we create a new subtarget since any
128 // creation will depend on the TM and the code generation flags on the
129 // function that reside in TargetOptions.
130 resetTargetOptions(F
);
131 I
= std::make_unique
<SparcSubtarget
>(TargetTriple
, CPU
, FS
, *this,
138 /// Sparc Code Generator Pass Configuration Options.
139 class SparcPassConfig
: public TargetPassConfig
{
141 SparcPassConfig(SparcTargetMachine
&TM
, PassManagerBase
&PM
)
142 : TargetPassConfig(TM
, PM
) {}
144 SparcTargetMachine
&getSparcTargetMachine() const {
145 return getTM
<SparcTargetMachine
>();
148 void addIRPasses() override
;
149 bool addInstSelector() override
;
150 void addPreEmitPass() override
;
154 TargetPassConfig
*SparcTargetMachine::createPassConfig(PassManagerBase
&PM
) {
155 return new SparcPassConfig(*this, PM
);
158 void SparcPassConfig::addIRPasses() {
159 addPass(createAtomicExpandPass());
161 TargetPassConfig::addIRPasses();
164 bool SparcPassConfig::addInstSelector() {
165 addPass(createSparcISelDag(getSparcTargetMachine()));
169 void SparcPassConfig::addPreEmitPass(){
170 addPass(createSparcDelaySlotFillerPass());
172 if (this->getSparcTargetMachine().getSubtargetImpl()->insertNOPLoad())
174 addPass(new InsertNOPLoad());
176 if (this->getSparcTargetMachine().getSubtargetImpl()->detectRoundChange()) {
177 addPass(new DetectRoundChange());
179 if (this->getSparcTargetMachine().getSubtargetImpl()->fixAllFDIVSQRT())
181 addPass(new FixAllFDIVSQRT());
185 void SparcV8TargetMachine::anchor() { }
187 SparcV8TargetMachine::SparcV8TargetMachine(const Target
&T
, const Triple
&TT
,
188 StringRef CPU
, StringRef FS
,
189 const TargetOptions
&Options
,
190 Optional
<Reloc::Model
> RM
,
191 Optional
<CodeModel::Model
> CM
,
192 CodeGenOpt::Level OL
, bool JIT
)
193 : SparcTargetMachine(T
, TT
, CPU
, FS
, Options
, RM
, CM
, OL
, JIT
, false) {}
195 void SparcV9TargetMachine::anchor() { }
197 SparcV9TargetMachine::SparcV9TargetMachine(const Target
&T
, const Triple
&TT
,
198 StringRef CPU
, StringRef FS
,
199 const TargetOptions
&Options
,
200 Optional
<Reloc::Model
> RM
,
201 Optional
<CodeModel::Model
> CM
,
202 CodeGenOpt::Level OL
, bool JIT
)
203 : SparcTargetMachine(T
, TT
, CPU
, FS
, Options
, RM
, CM
, OL
, JIT
, true) {}
205 void SparcelTargetMachine::anchor() {}
207 SparcelTargetMachine::SparcelTargetMachine(const Target
&T
, const Triple
&TT
,
208 StringRef CPU
, StringRef FS
,
209 const TargetOptions
&Options
,
210 Optional
<Reloc::Model
> RM
,
211 Optional
<CodeModel::Model
> CM
,
212 CodeGenOpt::Level OL
, bool JIT
)
213 : SparcTargetMachine(T
, TT
, CPU
, FS
, Options
, RM
, CM
, OL
, JIT
, false) {}