LLVM  9.0.0svn
SparcTargetMachine.cpp
Go to the documentation of this file.
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 "SparcTargetObjectFile.h"
16 #include "llvm/CodeGen/Passes.h"
20 using namespace llvm;
21 
22 extern "C" void LLVMInitializeSparcTarget() {
23  // Register the target.
27 }
28 
29 static std::string computeDataLayout(const Triple &T, bool is64Bit) {
30  // Sparc is typically big endian, but some are little.
31  std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
32  Ret += "-m:e";
33 
34  // Some ABIs have 32bit pointers.
35  if (!is64Bit)
36  Ret += "-p:32:32";
37 
38  // Alignments for 64 bit integers.
39  Ret += "-i64:64";
40 
41  // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
42  // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
43  if (is64Bit)
44  Ret += "-n32:64";
45  else
46  Ret += "-f128:64-n32";
47 
48  if (is64Bit)
49  Ret += "-S128";
50  else
51  Ret += "-S64";
52 
53  return Ret;
54 }
55 
57  if (!RM.hasValue())
58  return Reloc::Static;
59  return *RM;
60 }
61 
62 // Code models. Some only make sense for 64-bit code.
63 //
64 // SunCC Reloc CodeModel Constraints
65 // abs32 Static Small text+data+bss linked below 2^32 bytes
66 // abs44 Static Medium text+data+bss linked below 2^44 bytes
67 // abs64 Static Large text smaller than 2^31 bytes
68 // pic13 PIC_ Small GOT < 2^13 bytes
69 // pic32 PIC_ Medium GOT < 2^32 bytes
70 //
71 // All code models require that the text segment is smaller than 2GB.
72 static CodeModel::Model
74  bool Is64Bit, bool JIT) {
75  if (CM) {
76  if (*CM == CodeModel::Tiny)
77  report_fatal_error("Target does not support the tiny CodeModel");
78  if (*CM == CodeModel::Kernel)
79  report_fatal_error("Target does not support the kernel CodeModel");
80  return *CM;
81  }
82  if (Is64Bit) {
83  if (JIT)
84  return CodeModel::Large;
86  }
87  return CodeModel::Small;
88 }
89 
90 /// Create an ILP32 architecture model
92  const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
93  const TargetOptions &Options, Optional<Reloc::Model> RM,
94  Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT, bool is64bit)
95  : LLVMTargetMachine(T, computeDataLayout(TT, is64bit), TT, CPU, FS, Options,
98  CM, getEffectiveRelocModel(RM), is64bit, JIT),
99  OL),
101  Subtarget(TT, CPU, FS, *this, is64bit), is64Bit(is64bit) {
102  initAsmInfo();
103 }
104 
106 
107 const SparcSubtarget *
109  Attribute CPUAttr = F.getFnAttribute("target-cpu");
110  Attribute FSAttr = F.getFnAttribute("target-features");
111 
112  std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
113  ? CPUAttr.getValueAsString().str()
114  : TargetCPU;
115  std::string FS = !FSAttr.hasAttribute(Attribute::None)
116  ? FSAttr.getValueAsString().str()
117  : TargetFS;
118 
119  // FIXME: This is related to the code below to reset the target options,
120  // we need to know whether or not the soft float flag is set on the
121  // function, so we can enable it as a subtarget feature.
122  bool softFloat =
123  F.hasFnAttribute("use-soft-float") &&
124  F.getFnAttribute("use-soft-float").getValueAsString() == "true";
125 
126  if (softFloat)
127  FS += FS.empty() ? "+soft-float" : ",+soft-float";
128 
129  auto &I = SubtargetMap[CPU + FS];
130  if (!I) {
131  // This needs to be done before we create a new subtarget since any
132  // creation will depend on the TM and the code generation flags on the
133  // function that reside in TargetOptions.
135  I = llvm::make_unique<SparcSubtarget>(TargetTriple, CPU, FS, *this,
136  this->is64Bit);
137  }
138  return I.get();
139 }
140 
141 namespace {
142 /// Sparc Code Generator Pass Configuration Options.
143 class SparcPassConfig : public TargetPassConfig {
144 public:
145  SparcPassConfig(SparcTargetMachine &TM, PassManagerBase &PM)
146  : TargetPassConfig(TM, PM) {}
147 
148  SparcTargetMachine &getSparcTargetMachine() const {
149  return getTM<SparcTargetMachine>();
150  }
151 
152  void addIRPasses() override;
153  bool addInstSelector() override;
154  void addPreEmitPass() override;
155 };
156 } // namespace
157 
159  return new SparcPassConfig(*this, PM);
160 }
161 
162 void SparcPassConfig::addIRPasses() {
163  addPass(createAtomicExpandPass());
164 
166 }
167 
168 bool SparcPassConfig::addInstSelector() {
169  addPass(createSparcISelDag(getSparcTargetMachine()));
170  return false;
171 }
172 
173 void SparcPassConfig::addPreEmitPass(){
175 
176  if (this->getSparcTargetMachine().getSubtargetImpl()->insertNOPLoad())
177  {
178  addPass(new InsertNOPLoad());
179  }
180  if (this->getSparcTargetMachine().getSubtargetImpl()->detectRoundChange()) {
181  addPass(new DetectRoundChange());
182  }
183  if (this->getSparcTargetMachine().getSubtargetImpl()->fixAllFDIVSQRT())
184  {
185  addPass(new FixAllFDIVSQRT());
186  }
187 }
188 
189 void SparcV8TargetMachine::anchor() { }
190 
192  StringRef CPU, StringRef FS,
193  const TargetOptions &Options,
196  CodeGenOpt::Level OL, bool JIT)
197  : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
198 
199 void SparcV9TargetMachine::anchor() { }
200 
202  StringRef CPU, StringRef FS,
203  const TargetOptions &Options,
206  CodeGenOpt::Level OL, bool JIT)
207  : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
208 
209 void SparcelTargetMachine::anchor() {}
210 
212  StringRef CPU, StringRef FS,
213  const TargetOptions &Options,
216  CodeGenOpt::Level OL, bool JIT)
217  : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
FunctionPass * createSparcDelaySlotFillerPass()
createSparcDelaySlotFillerPass - Returns a pass that fills in delay slots in Sparc MachineFunctions ...
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with...
Definition: TargetMachine.h:77
SparcelTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
SparcV8TargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
const SparcSubtarget * getSubtargetImpl() const
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:320
F(f)
block Block Frequency true
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&... args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:1348
void LLVMInitializeSparcTarget()
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
void resetTargetOptions(const Function &F) const
Reset the target options based on the function&#39;s attributes.
No attributes have been set.
Definition: Attributes.h:71
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
Target-Independent Code Generator Pass Configuration Options.
RegisterTargetMachine - Helper template for registering a target machine implementation, for use in the target machine initialization function.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:289
Target & getTheSparcTarget()
static CodeModel::Model getEffectiveSparcCodeModel(Optional< CodeModel::Model > CM, Reloc::Model RM, bool Is64Bit, bool JIT)
bool hasAttribute(AttrKind Val) const
Return true if the attribute is present.
Definition: Attributes.cpp:201
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
static bool is64Bit(const char *name)
Target & getTheSparcelTarget()
This class describes a target machine that is implemented with the LLVM target-independent code gener...
FunctionPass * createSparcISelDag(SparcTargetMachine &TM)
createSparcISelDag - This pass converts a legalized DAG into a SPARC-specific DAG, ready for instruction scheduling.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
SparcTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT, bool is64bit)
Create an ILP32 architecture model.
Target - Wrapper for Target specific information.
std::string TargetCPU
Definition: TargetMachine.h:78
Target & getTheSparcV9Target()
static std::string computeDataLayout(const Triple &T, bool is64Bit)
bool hasValue() const
Definition: Optional.h:259
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:194
TargetOptions Options
Definition: TargetMachine.h:96
#define I(x, y, z)
Definition: MD5.cpp:58
std::string TargetFS
Definition: TargetMachine.h:79
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
SparcV9TargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:330
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
FunctionPass * createAtomicExpandPass()