blob: d360a9ebe36131f4a822c9a706ef5d688a7a764a [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
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.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// Implements the info about Mips target spec.
11//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000013
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "MipsTargetMachine.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000015#include "MCTargetDesc/MipsABIInfo.h"
16#include "MCTargetDesc/MipsMCTargetDesc.h"
Craig Topperb25fda92012-03-17 18:46:09 +000017#include "Mips.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "Mips16ISelDAGToDAG.h"
Reed Kotler1595f362013-04-09 19:46:01 +000019#include "MipsSEISelDAGToDAG.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000020#include "MipsSubtarget.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000021#include "MipsTargetObjectFile.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000022#include "llvm/ADT/Optional.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/StringRef.h"
Reed Kotler1595f362013-04-09 19:46:01 +000025#include "llvm/Analysis/TargetTransformInfo.h"
Petar Jovanovicfac93e22018-02-23 11:06:40 +000026#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
27#include "llvm/CodeGen/GlobalISel/Legalizer.h"
28#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
29#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000030#include "llvm/CodeGen/BasicTTIImpl.h"
31#include "llvm/CodeGen/MachineFunction.h"
Andrew Trickccb67362012-02-03 05:12:41 +000032#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000033#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000034#include "llvm/IR/Attributes.h"
35#include "llvm/IR/Function.h"
36#include "llvm/Support/CodeGen.h"
Reed Kotler1595f362013-04-09 19:46:01 +000037#include "llvm/Support/Debug.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000038#include "llvm/Support/TargetRegistry.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000039#include "llvm/Support/raw_ostream.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000040#include "llvm/Target/TargetOptions.h"
41#include <string>
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000042
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000043using namespace llvm;
44
Chandler Carruthe96dd892014-04-21 22:55:11 +000045#define DEBUG_TYPE "mips"
46
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000047extern "C" void LLVMInitializeMipsTarget() {
48 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000049 RegisterTargetMachine<MipsebTargetMachine> X(getTheMipsTarget());
50 RegisterTargetMachine<MipselTargetMachine> Y(getTheMipselTarget());
51 RegisterTargetMachine<MipsebTargetMachine> A(getTheMips64Target());
52 RegisterTargetMachine<MipselTargetMachine> B(getTheMips64elTarget());
Petar Jovanovicfac93e22018-02-23 11:06:40 +000053
54 PassRegistry *PR = PassRegistry::getPassRegistry();
55 initializeGlobalISel(*PR);
Simon Dardisae201082018-05-11 16:13:53 +000056 initializeMipsDelaySlotFillerPass(*PR);
Simon Dardis65cc0cb2018-05-12 16:57:26 +000057 initializeMipsLongBranchPass(*PR);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000058}
59
Daniel Sandersed64d622015-06-11 15:34:59 +000060static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +000061 const TargetOptions &Options,
62 bool isLittle) {
Eugene Zelenko926883e2017-02-01 01:22:51 +000063 std::string Ret;
Daniel Sanders50f17232015-09-15 16:17:27 +000064 MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions);
Eric Christopher8b770652015-01-26 19:03:15 +000065
66 // There are both little and big endian mips.
67 if (isLittle)
68 Ret += "e";
69 else
70 Ret += "E";
71
Daniel Sanders6a738832016-07-19 10:49:03 +000072 if (ABI.IsO32())
73 Ret += "-m:m";
74 else
75 Ret += "-m:e";
Eric Christopher8b770652015-01-26 19:03:15 +000076
77 // Pointers are 32 bit on some ABIs.
78 if (!ABI.IsN64())
79 Ret += "-p:32:32";
80
Sanjay Pateld4e1bb82015-07-07 21:31:54 +000081 // 8 and 16 bit integers only need to have natural alignment, but try to
Eric Christopher8b770652015-01-26 19:03:15 +000082 // align them to 32 bits. 64 bit integers have natural alignment.
83 Ret += "-i8:8:32-i16:16:32-i64:64";
84
85 // 32 bit registers are always available and the stack is at least 64 bit
86 // aligned. On N64 64 bit registers are also available and the stack is
87 // 128 bit aligned.
88 if (ABI.IsN64() || ABI.IsN32())
89 Ret += "-n32:64-S128";
90 else
91 Ret += "-n32-S64";
92
93 return Ret;
94}
95
Rafael Espindola79e238a2017-08-03 02:16:21 +000096static Reloc::Model getEffectiveRelocModel(bool JIT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +000097 Optional<Reloc::Model> RM) {
Rafael Espindola79e238a2017-08-03 02:16:21 +000098 if (!RM.hasValue() || JIT)
Rafael Espindola8c34dd82016-05-18 22:04:49 +000099 return Reloc::Static;
100 return *RM;
101}
102
Rafael Espindola79e238a2017-08-03 02:16:21 +0000103static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
104 if (CM)
105 return *CM;
106 return CodeModel::Small;
107}
108
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +0000109// On function prologue, the stack is created by decrementing
110// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000111// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000112// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000113// Using CodeModel::Large enables different CALL behavior.
Daniel Sanders3e5de882015-06-11 19:41:26 +0000114MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT,
Eric Christopher4407dde2014-07-02 00:54:07 +0000115 StringRef CPU, StringRef FS,
116 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000117 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000118 Optional<CodeModel::Model> CM,
119 CodeGenOpt::Level OL, bool JIT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000120 bool isLittle)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000121 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
122 CPU, FS, Options, getEffectiveRelocModel(JIT, RM),
123 getEffectiveCodeModel(CM), OL),
Eugene Zelenko926883e2017-02-01 01:22:51 +0000124 isLittle(isLittle), TLOF(llvm::make_unique<MipsTargetObjectFile>()),
Daniel Sanders50f17232015-09-15 16:17:27 +0000125 ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)),
John Baldwin1255b162017-08-14 21:49:38 +0000126 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this,
127 Options.StackAlignmentOverride),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000128 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
John Baldwin1255b162017-08-14 21:49:38 +0000129 isLittle, *this, Options.StackAlignmentOverride),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000130 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
John Baldwin1255b162017-08-14 21:49:38 +0000131 isLittle, *this, Options.StackAlignmentOverride) {
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000132 Subtarget = &DefaultSubtarget;
Rafael Espindola227144c2013-05-13 01:16:13 +0000133 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +0000134}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000135
Eugene Zelenko926883e2017-02-01 01:22:51 +0000136MipsTargetMachine::~MipsTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000137
Eugene Zelenko926883e2017-02-01 01:22:51 +0000138void MipsebTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000139
Daniel Sanders3e5de882015-06-11 19:41:26 +0000140MipsebTargetMachine::MipsebTargetMachine(const Target &T, const Triple &TT,
141 StringRef CPU, StringRef FS,
142 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000143 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000144 Optional<CodeModel::Model> CM,
145 CodeGenOpt::Level OL, bool JIT)
146 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000147
Eugene Zelenko926883e2017-02-01 01:22:51 +0000148void MipselTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000149
Daniel Sanders3e5de882015-06-11 19:41:26 +0000150MipselTargetMachine::MipselTargetMachine(const Target &T, const Triple &TT,
151 StringRef CPU, StringRef FS,
152 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000153 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000154 Optional<CodeModel::Model> CM,
155 CodeGenOpt::Level OL, bool JIT)
156 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000157
Eric Christophera9353d12014-09-26 01:44:08 +0000158const MipsSubtarget *
David Majnemerde360752014-09-26 02:57:05 +0000159MipsTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000160 Attribute CPUAttr = F.getFnAttribute("target-cpu");
161 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christophera9353d12014-09-26 01:44:08 +0000162
163 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
164 ? CPUAttr.getValueAsString().str()
165 : TargetCPU;
166 std::string FS = !FSAttr.hasAttribute(Attribute::None)
167 ? FSAttr.getValueAsString().str()
168 : TargetFS;
169 bool hasMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000170 !F.getFnAttribute("mips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000171 bool hasNoMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000172 !F.getFnAttribute("nomips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000173
Simon Atanasyane0b726f2017-05-22 12:47:41 +0000174 bool HasMicroMipsAttr =
175 !F.getFnAttribute("micromips").hasAttribute(Attribute::None);
176 bool HasNoMicroMipsAttr =
177 !F.getFnAttribute("nomicromips").hasAttribute(Attribute::None);
178
Eric Christopher6a0551e2014-09-29 21:57:54 +0000179 // FIXME: This is related to the code below to reset the target options,
180 // we need to know whether or not the soft float flag is set on the
Toma Tabacu506cfd02015-05-07 10:29:52 +0000181 // function, so we can enable it as a subtarget feature.
Eric Christopher824f42f2015-05-12 01:26:05 +0000182 bool softFloat =
183 F.hasFnAttribute("use-soft-float") &&
184 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
Eric Christopher6a0551e2014-09-29 21:57:54 +0000185
Eric Christophera9353d12014-09-26 01:44:08 +0000186 if (hasMips16Attr)
187 FS += FS.empty() ? "+mips16" : ",+mips16";
188 else if (hasNoMips16Attr)
189 FS += FS.empty() ? "-mips16" : ",-mips16";
Simon Atanasyane0b726f2017-05-22 12:47:41 +0000190 if (HasMicroMipsAttr)
191 FS += FS.empty() ? "+micromips" : ",+micromips";
192 else if (HasNoMicroMipsAttr)
193 FS += FS.empty() ? "-micromips" : ",-micromips";
Toma Tabacu506cfd02015-05-07 10:29:52 +0000194 if (softFloat)
195 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christophera9353d12014-09-26 01:44:08 +0000196
Toma Tabacu506cfd02015-05-07 10:29:52 +0000197 auto &I = SubtargetMap[CPU + FS];
Eric Christophera9353d12014-09-26 01:44:08 +0000198 if (!I) {
199 // This needs to be done before we create a new subtarget since any
200 // creation will depend on the TM and the code generation flags on the
201 // function that reside in TargetOptions.
202 resetTargetOptions(F);
Alexander Richardsoneb5ce8b2017-09-22 08:52:03 +0000203 I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, *this,
204 Options.StackAlignmentOverride);
Eric Christophera9353d12014-09-26 01:44:08 +0000205 }
206 return I.get();
207}
208
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000209void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000210 LLVM_DEBUG(dbgs() << "resetSubtarget\n");
Eric Christophera9353d12014-09-26 01:44:08 +0000211
Matthias Braunf1caa282017-12-15 22:22:58 +0000212 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(MF->getFunction()));
Eric Christopherfc6de422014-08-05 02:39:49 +0000213 MF->setSubtarget(Subtarget);
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000214}
215
Andrew Trickccb67362012-02-03 05:12:41 +0000216namespace {
Eugene Zelenko926883e2017-02-01 01:22:51 +0000217
Andrew Trickccb67362012-02-03 05:12:41 +0000218/// Mips Code Generator Pass Configuration Options.
219class MipsPassConfig : public TargetPassConfig {
220public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000221 MipsPassConfig(MipsTargetMachine &TM, PassManagerBase &PM)
Alexander Richardsoneb5ce8b2017-09-22 08:52:03 +0000222 : TargetPassConfig(TM, PM) {
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000223 // The current implementation of long branch pass requires a scratch
224 // register ($at) to be available before branch instructions. Tail merging
225 // can break this requirement, so disable it when long branch pass is
226 // enabled.
227 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
228 }
Andrew Trickccb67362012-02-03 05:12:41 +0000229
230 MipsTargetMachine &getMipsTargetMachine() const {
231 return getTM<MipsTargetMachine>();
232 }
233
234 const MipsSubtarget &getMipsSubtarget() const {
235 return *getMipsTargetMachine().getSubtargetImpl();
236 }
237
Craig Topper56c590a2014-04-29 07:58:02 +0000238 void addIRPasses() override;
239 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000240 void addPreEmitPass() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000241 void addPreRegAlloc() override;
Petar Jovanovicfac93e22018-02-23 11:06:40 +0000242 bool addIRTranslator() override;
243 bool addLegalizeMachineIR() override;
244 bool addRegBankSelect() override;
245 bool addGlobalInstructionSelect() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000246};
Eugene Zelenko926883e2017-02-01 01:22:51 +0000247
248} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000249
Andrew Trickf8ea1082012-02-04 02:56:59 +0000250TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000251 return new MipsPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000252}
253
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000254void MipsPassConfig::addIRPasses() {
255 TargetPassConfig::addIRPasses();
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000256 addPass(createAtomicExpandPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000257 if (getMipsSubtarget().os16())
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000258 addPass(createMipsOs16Pass());
Reed Kotler783c7942013-05-10 22:25:39 +0000259 if (getMipsSubtarget().inMips16HardFloat())
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000260 addPass(createMips16HardFloatPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000261}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000262// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000263// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000264bool MipsPassConfig::addInstSelector() {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000265 addPass(createMipsModuleISelDagPass());
Daniel Sanders46fe6552016-07-14 13:25:22 +0000266 addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
267 addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000268 return false;
269}
270
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000271void MipsPassConfig::addPreRegAlloc() {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000272 addPass(createMipsOptimizePICCallPass());
Reed Kotler96b74022014-03-10 16:31:25 +0000273}
274
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000275TargetTransformInfo
276MipsTargetMachine::getTargetTransformInfo(const Function &F) {
277 if (Subtarget->allowMixed16_32()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000278 LLVM_DEBUG(errs() << "No Target Transform Info Pass Added\n");
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000279 // FIXME: This is no longer necessary as the TTI returned is per-function.
280 return TargetTransformInfo(F.getParent()->getDataLayout());
281 }
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000282
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000283 LLVM_DEBUG(errs() << "Target Transform Info Pass Added\n");
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000284 return TargetTransformInfo(BasicTTIImpl(this, F));
Reed Kotler1595f362013-04-09 19:46:01 +0000285}
286
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000287// Implemented by targets that want to run passes immediately before
288// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000289// print out the code after the passes.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000290void MipsPassConfig::addPreEmitPass() {
Zoran Jovanovicffef3e32017-04-27 13:10:48 +0000291 addPass(createMicroMipsSizeReductionPass());
Daniel Sanderse8efff32016-03-14 16:24:05 +0000292
Simon Dardis1631d6c2017-11-20 15:59:18 +0000293 // The delay slot filler and the long branch passes can potientially create
294 // forbidden slot/ hazards for MIPSR6 which the hazard schedule pass will
295 // fix. Any new pass must come before the hazard schedule pass.
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000296 addPass(createMipsDelaySlotFillerPass());
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000297 addPass(createMipsLongBranchPass());
Simon Dardis1631d6c2017-11-20 15:59:18 +0000298 addPass(createMipsHazardSchedule());
Rafael Espindola6f7c2802016-06-28 14:26:39 +0000299 addPass(createMipsConstantIslandPass());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000300}
Petar Jovanovicfac93e22018-02-23 11:06:40 +0000301
302bool MipsPassConfig::addIRTranslator() {
303 addPass(new IRTranslator());
304 return false;
305}
306
307bool MipsPassConfig::addLegalizeMachineIR() {
308 addPass(new Legalizer());
309 return false;
310}
311
312bool MipsPassConfig::addRegBankSelect() {
313 addPass(new RegBankSelect());
314 return false;
315}
316
317bool MipsPassConfig::addGlobalInstructionSelect() {
318 addPass(new InstructionSelect());
319 return false;
320}