blob: cf634d13dacfae44f3240efdf57603e377851a8d [file] [log] [blame]
Chris Lattner158e1f52006-02-05 05:50:24 +00001//===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner158e1f52006-02-05 05:50:24 +00007//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
13#include "SparcTargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000014#include "SparcTargetObjectFile.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "Sparc.h"
Andrew Trickccb67362012-02-03 05:12:41 +000016#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000017#include "llvm/CodeGen/TargetPassConfig.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000018#include "llvm/IR/LegacyPassManager.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000019#include "llvm/Support/TargetRegistry.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000020using namespace llvm;
21
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000022extern "C" void LLVMInitializeSparcTarget() {
23 // Register the target.
Chris Lattner8228b112010-02-04 06:34:01 +000024 RegisterTargetMachine<SparcV8TargetMachine> X(TheSparcTarget);
25 RegisterTargetMachine<SparcV9TargetMachine> Y(TheSparcV9Target);
Douglas Katzman9160e782015-04-29 20:30:57 +000026 RegisterTargetMachine<SparcelTargetMachine> Z(TheSparcelTarget);
Jim Laskeyae92ce82006-09-07 23:39:26 +000027}
28
Douglas Katzman9160e782015-04-29 20:30:57 +000029static 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";
Eric Christopher8b770652015-01-26 19:03:15 +000033
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
Rafael Espindola38af4d62016-05-18 16:00:24 +000056/// Create an ILP32 architecture model
Chris Lattner158e1f52006-02-05 05:50:24 +000057///
Daniel Sanders3e5de882015-06-11 19:41:26 +000058SparcTargetMachine::SparcTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +000059 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000060 const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000061 Reloc::Model RM, CodeModel::Model CM,
Mehdi Amini93e1ea12015-03-12 00:07:24 +000062 CodeGenOpt::Level OL, bool is64bit)
Daniel Sanders3e5de882015-06-11 19:41:26 +000063 : LLVMTargetMachine(T, computeDataLayout(TT, is64bit), TT, CPU, FS, Options,
64 RM, CM, OL),
Chris Dewhurst68388a02016-05-18 09:14:13 +000065 TLOF(make_unique<SparcELFTargetObjectFile>()) {
Rafael Espindola227144c2013-05-13 01:16:13 +000066 initAsmInfo();
Chris Dewhurst68388a02016-05-18 09:14:13 +000067 this->is64Bit = is64bit;
Chris Lattner158e1f52006-02-05 05:50:24 +000068}
69
Reid Kleckner357600e2014-11-20 23:37:18 +000070SparcTargetMachine::~SparcTargetMachine() {}
71
Chris Dewhurst68388a02016-05-18 09:14:13 +000072const SparcSubtarget *
73SparcTargetMachine::getSubtargetImpl(const Function &F) const {
74 Attribute CPUAttr = F.getFnAttribute("target-cpu");
75 Attribute FSAttr = F.getFnAttribute("target-features");
76
77 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
78 ? CPUAttr.getValueAsString().str()
79 : TargetCPU;
80 std::string FS = !FSAttr.hasAttribute(Attribute::None)
81 ? FSAttr.getValueAsString().str()
82 : TargetFS;
83
84 // FIXME: This is related to the code below to reset the target options,
85 // we need to know whether or not the soft float flag is set on the
86 // function, so we can enable it as a subtarget feature.
87 bool softFloat =
88 F.hasFnAttribute("use-soft-float") &&
89 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
90
91 if (softFloat)
92 FS += FS.empty() ? "+soft-float" : ",+soft-float";
93
94 auto &I = SubtargetMap[CPU + FS];
95 if (!I) {
96 // This needs to be done before we create a new subtarget since any
97 // creation will depend on the TM and the code generation flags on the
98 // function that reside in TargetOptions.
99 resetTargetOptions(F);
100 I = llvm::make_unique<SparcSubtarget>(TargetTriple, CPU, FS, *this,
101 this->is64Bit);
102 }
103 return I.get();
104}
105
Andrew Trickccb67362012-02-03 05:12:41 +0000106namespace {
107/// Sparc Code Generator Pass Configuration Options.
108class SparcPassConfig : public TargetPassConfig {
109public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000110 SparcPassConfig(SparcTargetMachine *TM, PassManagerBase &PM)
111 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000112
113 SparcTargetMachine &getSparcTargetMachine() const {
114 return getTM<SparcTargetMachine>();
115 }
116
Robin Morissete2de06b2014-10-16 20:34:57 +0000117 void addIRPasses() override;
Craig Topperb0c941b2014-04-29 07:57:13 +0000118 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000119 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000120};
121} // namespace
122
Andrew Trickf8ea1082012-02-04 02:56:59 +0000123TargetPassConfig *SparcTargetMachine::createPassConfig(PassManagerBase &PM) {
124 return new SparcPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000125}
126
Robin Morissete2de06b2014-10-16 20:34:57 +0000127void SparcPassConfig::addIRPasses() {
128 addPass(createAtomicExpandPass(&getSparcTargetMachine()));
129
130 TargetPassConfig::addIRPasses();
131}
132
Andrew Trickccb67362012-02-03 05:12:41 +0000133bool SparcPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000134 addPass(createSparcISelDag(getSparcTargetMachine()));
Chris Lattner158e1f52006-02-05 05:50:24 +0000135 return false;
136}
137
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000138void SparcPassConfig::addPreEmitPass(){
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000139 addPass(createSparcDelaySlotFillerPass(getSparcTargetMachine()));
Chris Lattner12e97302006-09-04 04:14:57 +0000140}
Chris Lattner8228b112010-02-04 06:34:01 +0000141
David Blaikiea379b1812011-12-20 02:50:00 +0000142void SparcV8TargetMachine::anchor() { }
143
Daniel Sanders3e5de882015-06-11 19:41:26 +0000144SparcV8TargetMachine::SparcV8TargetMachine(const Target &T, const Triple &TT,
145 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000146 const TargetOptions &Options,
Daniel Sanders3e5de882015-06-11 19:41:26 +0000147 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000148 CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000149 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Chris Lattner8228b112010-02-04 06:34:01 +0000150
David Blaikiea379b1812011-12-20 02:50:00 +0000151void SparcV9TargetMachine::anchor() { }
152
Daniel Sanders3e5de882015-06-11 19:41:26 +0000153SparcV9TargetMachine::SparcV9TargetMachine(const Target &T, const Triple &TT,
Douglas Katzman9160e782015-04-29 20:30:57 +0000154 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000155 const TargetOptions &Options,
Douglas Katzman9160e782015-04-29 20:30:57 +0000156 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000157 CodeGenOpt::Level OL)
Douglas Katzman9160e782015-04-29 20:30:57 +0000158 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
159
160void SparcelTargetMachine::anchor() {}
161
Daniel Sanders3e5de882015-06-11 19:41:26 +0000162SparcelTargetMachine::SparcelTargetMachine(const Target &T, const Triple &TT,
Douglas Katzman9160e782015-04-29 20:30:57 +0000163 StringRef CPU, StringRef FS,
164 const TargetOptions &Options,
165 Reloc::Model RM, CodeModel::Model CM,
166 CodeGenOpt::Level OL)
167 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}