blob: 7834fb279a06fd0fe426752d9a079a48718dfa67 [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 Espindola8c34dd82016-05-18 22:04:49 +000056static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
57 if (!RM.hasValue())
58 return Reloc::Static;
59 return *RM;
60}
61
Rafael Espindola38af4d62016-05-18 16:00:24 +000062/// Create an ILP32 architecture model
Chris Lattner158e1f52006-02-05 05:50:24 +000063///
Daniel Sanders3e5de882015-06-11 19:41:26 +000064SparcTargetMachine::SparcTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +000065 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000066 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +000067 Optional<Reloc::Model> RM,
68 CodeModel::Model CM,
Mehdi Amini93e1ea12015-03-12 00:07:24 +000069 CodeGenOpt::Level OL, bool is64bit)
Daniel Sanders3e5de882015-06-11 19:41:26 +000070 : LLVMTargetMachine(T, computeDataLayout(TT, is64bit), TT, CPU, FS, Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +000071 getEffectiveRelocModel(RM), CM, OL),
Chris Dewhurst68388a02016-05-18 09:14:13 +000072 TLOF(make_unique<SparcELFTargetObjectFile>()) {
Rafael Espindola227144c2013-05-13 01:16:13 +000073 initAsmInfo();
Chris Dewhurst68388a02016-05-18 09:14:13 +000074 this->is64Bit = is64bit;
Chris Lattner158e1f52006-02-05 05:50:24 +000075}
76
Reid Kleckner357600e2014-11-20 23:37:18 +000077SparcTargetMachine::~SparcTargetMachine() {}
78
Chris Dewhurst68388a02016-05-18 09:14:13 +000079const SparcSubtarget *
80SparcTargetMachine::getSubtargetImpl(const Function &F) const {
81 Attribute CPUAttr = F.getFnAttribute("target-cpu");
82 Attribute FSAttr = F.getFnAttribute("target-features");
83
84 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
85 ? CPUAttr.getValueAsString().str()
86 : TargetCPU;
87 std::string FS = !FSAttr.hasAttribute(Attribute::None)
88 ? FSAttr.getValueAsString().str()
89 : TargetFS;
90
91 // FIXME: This is related to the code below to reset the target options,
92 // we need to know whether or not the soft float flag is set on the
93 // function, so we can enable it as a subtarget feature.
94 bool softFloat =
95 F.hasFnAttribute("use-soft-float") &&
96 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
97
98 if (softFloat)
99 FS += FS.empty() ? "+soft-float" : ",+soft-float";
100
101 auto &I = SubtargetMap[CPU + FS];
102 if (!I) {
103 // This needs to be done before we create a new subtarget since any
104 // creation will depend on the TM and the code generation flags on the
105 // function that reside in TargetOptions.
106 resetTargetOptions(F);
107 I = llvm::make_unique<SparcSubtarget>(TargetTriple, CPU, FS, *this,
108 this->is64Bit);
109 }
110 return I.get();
111}
112
Andrew Trickccb67362012-02-03 05:12:41 +0000113namespace {
114/// Sparc Code Generator Pass Configuration Options.
115class SparcPassConfig : public TargetPassConfig {
116public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000117 SparcPassConfig(SparcTargetMachine *TM, PassManagerBase &PM)
118 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000119
120 SparcTargetMachine &getSparcTargetMachine() const {
121 return getTM<SparcTargetMachine>();
122 }
123
Robin Morissete2de06b2014-10-16 20:34:57 +0000124 void addIRPasses() override;
Craig Topperb0c941b2014-04-29 07:57:13 +0000125 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000126 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000127};
128} // namespace
129
Andrew Trickf8ea1082012-02-04 02:56:59 +0000130TargetPassConfig *SparcTargetMachine::createPassConfig(PassManagerBase &PM) {
131 return new SparcPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000132}
133
Robin Morissete2de06b2014-10-16 20:34:57 +0000134void SparcPassConfig::addIRPasses() {
135 addPass(createAtomicExpandPass(&getSparcTargetMachine()));
136
137 TargetPassConfig::addIRPasses();
138}
139
Andrew Trickccb67362012-02-03 05:12:41 +0000140bool SparcPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000141 addPass(createSparcISelDag(getSparcTargetMachine()));
Chris Lattner158e1f52006-02-05 05:50:24 +0000142 return false;
143}
144
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000145void SparcPassConfig::addPreEmitPass(){
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000146 addPass(createSparcDelaySlotFillerPass(getSparcTargetMachine()));
Chris Lattner12e97302006-09-04 04:14:57 +0000147}
Chris Lattner8228b112010-02-04 06:34:01 +0000148
David Blaikiea379b1812011-12-20 02:50:00 +0000149void SparcV8TargetMachine::anchor() { }
150
Daniel Sanders3e5de882015-06-11 19:41:26 +0000151SparcV8TargetMachine::SparcV8TargetMachine(const Target &T, const Triple &TT,
152 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000153 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000154 Optional<Reloc::Model> RM,
155 CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000156 CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000157 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Chris Lattner8228b112010-02-04 06:34:01 +0000158
David Blaikiea379b1812011-12-20 02:50:00 +0000159void SparcV9TargetMachine::anchor() { }
160
Daniel Sanders3e5de882015-06-11 19:41:26 +0000161SparcV9TargetMachine::SparcV9TargetMachine(const Target &T, const Triple &TT,
Douglas Katzman9160e782015-04-29 20:30:57 +0000162 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000163 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000164 Optional<Reloc::Model> RM,
165 CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000166 CodeGenOpt::Level OL)
Douglas Katzman9160e782015-04-29 20:30:57 +0000167 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
168
169void SparcelTargetMachine::anchor() {}
170
Daniel Sanders3e5de882015-06-11 19:41:26 +0000171SparcelTargetMachine::SparcelTargetMachine(const Target &T, const Triple &TT,
Douglas Katzman9160e782015-04-29 20:30:57 +0000172 StringRef CPU, StringRef FS,
173 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000174 Optional<Reloc::Model> RM,
175 CodeModel::Model CM,
Douglas Katzman9160e782015-04-29 20:30:57 +0000176 CodeGenOpt::Level OL)
177 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}