blob: d8a047a72ba8d0db35ab7624412e6ab45d264458 [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"
Chris Dewhurst4f7cac32016-05-23 10:56:36 +000016#include "LeonPasses.h"
Andrew Trickccb67362012-02-03 05:12:41 +000017#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000018#include "llvm/CodeGen/TargetPassConfig.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000019#include "llvm/IR/LegacyPassManager.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000020#include "llvm/Support/TargetRegistry.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000021using namespace llvm;
22
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000023extern "C" void LLVMInitializeSparcTarget() {
24 // Register the target.
Chris Lattner8228b112010-02-04 06:34:01 +000025 RegisterTargetMachine<SparcV8TargetMachine> X(TheSparcTarget);
26 RegisterTargetMachine<SparcV9TargetMachine> Y(TheSparcV9Target);
Douglas Katzman9160e782015-04-29 20:30:57 +000027 RegisterTargetMachine<SparcelTargetMachine> Z(TheSparcelTarget);
Jim Laskeyae92ce82006-09-07 23:39:26 +000028}
29
Douglas Katzman9160e782015-04-29 20:30:57 +000030static 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";
33 Ret += "-m:e";
Eric Christopher8b770652015-01-26 19:03:15 +000034
35 // Some ABIs have 32bit pointers.
36 if (!is64Bit)
37 Ret += "-p:32:32";
38
39 // Alignments for 64 bit integers.
40 Ret += "-i64:64";
41
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.
44 if (is64Bit)
45 Ret += "-n32:64";
46 else
47 Ret += "-f128:64-n32";
48
49 if (is64Bit)
50 Ret += "-S128";
51 else
52 Ret += "-S64";
53
54 return Ret;
55}
56
Rafael Espindola8c34dd82016-05-18 22:04:49 +000057static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
58 if (!RM.hasValue())
59 return Reloc::Static;
60 return *RM;
61}
62
Rafael Espindola38af4d62016-05-18 16:00:24 +000063/// Create an ILP32 architecture model
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 Dewhurst4f7cac32016-05-23 10:56:36 +000072 TLOF(make_unique<SparcELFTargetObjectFile>()),
73 Subtarget(TT, CPU, FS, *this, is64bit), is64Bit(is64bit) {
Rafael Espindola227144c2013-05-13 01:16:13 +000074 initAsmInfo();
Chris Lattner158e1f52006-02-05 05:50:24 +000075}
76
Reid Kleckner357600e2014-11-20 23:37:18 +000077SparcTargetMachine::~SparcTargetMachine() {}
78
James Y Knight2cc9da92016-08-12 14:48:09 +000079const SparcSubtarget *
Chris Dewhurst68388a02016-05-18 09:14:13 +000080SparcTargetMachine::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
James Y Knight2cc9da92016-08-12 14:48:09 +000098 if (softFloat)
Chris Dewhurst68388a02016-05-18 09:14:13 +000099 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)
James Y Knight2cc9da92016-08-12 14:48:09 +0000118 : 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
James Y Knight2cc9da92016-08-12 14:48:09 +0000145void SparcPassConfig::addPreEmitPass(){
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000146 addPass(createSparcDelaySlotFillerPass(getSparcTargetMachine()));
James Y Knight2cc9da92016-08-12 14:48:09 +0000147
148 if (this->getSparcTargetMachine().getSubtargetImpl()->insertNOPLoad())
149 {
Chris Dewhurst3202f062016-07-08 15:33:56 +0000150 addPass(new InsertNOPLoad(getSparcTargetMachine()));
151 }
James Y Knight2cc9da92016-08-12 14:48:09 +0000152 if (this->getSparcTargetMachine().getSubtargetImpl()->fixFSMULD())
153 {
154 addPass(new FixFSMULD(getSparcTargetMachine()));
Chris Dewhurst829f8ef2016-08-12 09:34:26 +0000155 }
James Y Knight2cc9da92016-08-12 14:48:09 +0000156 if (this->getSparcTargetMachine().getSubtargetImpl()->replaceFMULS())
157 {
158 addPass(new ReplaceFMULS(getSparcTargetMachine()));
Chris Dewhurst3202f062016-07-08 15:33:56 +0000159 }
James Y Knight2cc9da92016-08-12 14:48:09 +0000160 if (this->getSparcTargetMachine().getSubtargetImpl()->fixAllFDIVSQRT())
161 {
162 addPass(new FixAllFDIVSQRT(getSparcTargetMachine()));
Chris Dewhurst3202f062016-07-08 15:33:56 +0000163 }
Chris Lattner12e97302006-09-04 04:14:57 +0000164}
Chris Lattner8228b112010-02-04 06:34:01 +0000165
James Y Knight2cc9da92016-08-12 14:48:09 +0000166void SparcV8TargetMachine::anchor() { }
David Blaikiea379b1812011-12-20 02:50:00 +0000167
Daniel Sanders3e5de882015-06-11 19:41:26 +0000168SparcV8TargetMachine::SparcV8TargetMachine(const Target &T, const Triple &TT,
169 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000170 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000171 Optional<Reloc::Model> RM,
172 CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000173 CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000174 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Chris Lattner8228b112010-02-04 06:34:01 +0000175
James Y Knight2cc9da92016-08-12 14:48:09 +0000176void SparcV9TargetMachine::anchor() { }
David Blaikiea379b1812011-12-20 02:50:00 +0000177
Daniel Sanders3e5de882015-06-11 19:41:26 +0000178SparcV9TargetMachine::SparcV9TargetMachine(const Target &T, const Triple &TT,
Douglas Katzman9160e782015-04-29 20:30:57 +0000179 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000180 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000181 Optional<Reloc::Model> RM,
182 CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000183 CodeGenOpt::Level OL)
Douglas Katzman9160e782015-04-29 20:30:57 +0000184 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
185
186void SparcelTargetMachine::anchor() {}
187
Daniel Sanders3e5de882015-06-11 19:41:26 +0000188SparcelTargetMachine::SparcelTargetMachine(const Target &T, const Triple &TT,
Douglas Katzman9160e782015-04-29 20:30:57 +0000189 StringRef CPU, StringRef FS,
190 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000191 Optional<Reloc::Model> RM,
192 CodeModel::Model CM,
Douglas Katzman9160e782015-04-29 20:30:57 +0000193 CodeGenOpt::Level OL)
194 : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}