blob: 6199ffd789c348503583c2ac74bed0b3b539db04 [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"
Eugene Zelenko926883e2017-02-01 01:22:51 +000026#include "llvm/CodeGen/BasicTTIImpl.h"
27#include "llvm/CodeGen/MachineFunction.h"
Andrew Trickccb67362012-02-03 05:12:41 +000028#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000029#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000030#include "llvm/IR/Attributes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/Support/CodeGen.h"
Reed Kotler1595f362013-04-09 19:46:01 +000033#include "llvm/Support/Debug.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000035#include "llvm/Support/raw_ostream.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000036#include "llvm/Target/TargetOptions.h"
37#include <string>
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000038
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000039using namespace llvm;
40
Chandler Carruthe96dd892014-04-21 22:55:11 +000041#define DEBUG_TYPE "mips"
42
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000043extern "C" void LLVMInitializeMipsTarget() {
44 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000045 RegisterTargetMachine<MipsebTargetMachine> X(getTheMipsTarget());
46 RegisterTargetMachine<MipselTargetMachine> Y(getTheMipselTarget());
47 RegisterTargetMachine<MipsebTargetMachine> A(getTheMips64Target());
48 RegisterTargetMachine<MipselTargetMachine> B(getTheMips64elTarget());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000049}
50
Daniel Sandersed64d622015-06-11 15:34:59 +000051static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +000052 const TargetOptions &Options,
53 bool isLittle) {
Eugene Zelenko926883e2017-02-01 01:22:51 +000054 std::string Ret;
Daniel Sanders50f17232015-09-15 16:17:27 +000055 MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions);
Eric Christopher8b770652015-01-26 19:03:15 +000056
57 // There are both little and big endian mips.
58 if (isLittle)
59 Ret += "e";
60 else
61 Ret += "E";
62
Daniel Sanders6a738832016-07-19 10:49:03 +000063 if (ABI.IsO32())
64 Ret += "-m:m";
65 else
66 Ret += "-m:e";
Eric Christopher8b770652015-01-26 19:03:15 +000067
68 // Pointers are 32 bit on some ABIs.
69 if (!ABI.IsN64())
70 Ret += "-p:32:32";
71
Sanjay Pateld4e1bb82015-07-07 21:31:54 +000072 // 8 and 16 bit integers only need to have natural alignment, but try to
Eric Christopher8b770652015-01-26 19:03:15 +000073 // align them to 32 bits. 64 bit integers have natural alignment.
74 Ret += "-i8:8:32-i16:16:32-i64:64";
75
76 // 32 bit registers are always available and the stack is at least 64 bit
77 // aligned. On N64 64 bit registers are also available and the stack is
78 // 128 bit aligned.
79 if (ABI.IsN64() || ABI.IsN32())
80 Ret += "-n32:64-S128";
81 else
82 Ret += "-n32-S64";
83
84 return Ret;
85}
86
Rafael Espindola79e238a2017-08-03 02:16:21 +000087static Reloc::Model getEffectiveRelocModel(bool JIT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +000088 Optional<Reloc::Model> RM) {
Rafael Espindola79e238a2017-08-03 02:16:21 +000089 if (!RM.hasValue() || JIT)
Rafael Espindola8c34dd82016-05-18 22:04:49 +000090 return Reloc::Static;
91 return *RM;
92}
93
Rafael Espindola79e238a2017-08-03 02:16:21 +000094static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
95 if (CM)
96 return *CM;
97 return CodeModel::Small;
98}
99
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +0000100// On function prologue, the stack is created by decrementing
101// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000102// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000103// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000104// Using CodeModel::Large enables different CALL behavior.
Daniel Sanders3e5de882015-06-11 19:41:26 +0000105MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT,
Eric Christopher4407dde2014-07-02 00:54:07 +0000106 StringRef CPU, StringRef FS,
107 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000108 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000109 Optional<CodeModel::Model> CM,
110 CodeGenOpt::Level OL, bool JIT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000111 bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000112 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000113 CPU, FS, Options, getEffectiveRelocModel(JIT, RM),
114 getEffectiveCodeModel(CM), OL),
Eugene Zelenko926883e2017-02-01 01:22:51 +0000115 isLittle(isLittle), TLOF(llvm::make_unique<MipsTargetObjectFile>()),
Daniel Sanders50f17232015-09-15 16:17:27 +0000116 ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)),
John Baldwin1255b162017-08-14 21:49:38 +0000117 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this,
118 Options.StackAlignmentOverride),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000119 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
John Baldwin1255b162017-08-14 21:49:38 +0000120 isLittle, *this, Options.StackAlignmentOverride),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000121 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
John Baldwin1255b162017-08-14 21:49:38 +0000122 isLittle, *this, Options.StackAlignmentOverride) {
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000123 Subtarget = &DefaultSubtarget;
Rafael Espindola227144c2013-05-13 01:16:13 +0000124 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +0000125}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000126
Eugene Zelenko926883e2017-02-01 01:22:51 +0000127MipsTargetMachine::~MipsTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000128
Eugene Zelenko926883e2017-02-01 01:22:51 +0000129void MipsebTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000130
Daniel Sanders3e5de882015-06-11 19:41:26 +0000131MipsebTargetMachine::MipsebTargetMachine(const Target &T, const Triple &TT,
132 StringRef CPU, StringRef FS,
133 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000134 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000135 Optional<CodeModel::Model> CM,
136 CodeGenOpt::Level OL, bool JIT)
137 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000138
Eugene Zelenko926883e2017-02-01 01:22:51 +0000139void MipselTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000140
Daniel Sanders3e5de882015-06-11 19:41:26 +0000141MipselTargetMachine::MipselTargetMachine(const Target &T, const Triple &TT,
142 StringRef CPU, StringRef FS,
143 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000144 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000145 Optional<CodeModel::Model> CM,
146 CodeGenOpt::Level OL, bool JIT)
147 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000148
Eric Christophera9353d12014-09-26 01:44:08 +0000149const MipsSubtarget *
David Majnemerde360752014-09-26 02:57:05 +0000150MipsTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000151 Attribute CPUAttr = F.getFnAttribute("target-cpu");
152 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christophera9353d12014-09-26 01:44:08 +0000153
154 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
155 ? CPUAttr.getValueAsString().str()
156 : TargetCPU;
157 std::string FS = !FSAttr.hasAttribute(Attribute::None)
158 ? FSAttr.getValueAsString().str()
159 : TargetFS;
160 bool hasMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000161 !F.getFnAttribute("mips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000162 bool hasNoMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000163 !F.getFnAttribute("nomips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000164
Simon Atanasyane0b726f2017-05-22 12:47:41 +0000165 bool HasMicroMipsAttr =
166 !F.getFnAttribute("micromips").hasAttribute(Attribute::None);
167 bool HasNoMicroMipsAttr =
168 !F.getFnAttribute("nomicromips").hasAttribute(Attribute::None);
169
Eric Christopher6a0551e2014-09-29 21:57:54 +0000170 // FIXME: This is related to the code below to reset the target options,
171 // we need to know whether or not the soft float flag is set on the
Toma Tabacu506cfd02015-05-07 10:29:52 +0000172 // function, so we can enable it as a subtarget feature.
Eric Christopher824f42f2015-05-12 01:26:05 +0000173 bool softFloat =
174 F.hasFnAttribute("use-soft-float") &&
175 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
Eric Christopher6a0551e2014-09-29 21:57:54 +0000176
Eric Christophera9353d12014-09-26 01:44:08 +0000177 if (hasMips16Attr)
178 FS += FS.empty() ? "+mips16" : ",+mips16";
179 else if (hasNoMips16Attr)
180 FS += FS.empty() ? "-mips16" : ",-mips16";
Simon Atanasyane0b726f2017-05-22 12:47:41 +0000181 if (HasMicroMipsAttr)
182 FS += FS.empty() ? "+micromips" : ",+micromips";
183 else if (HasNoMicroMipsAttr)
184 FS += FS.empty() ? "-micromips" : ",-micromips";
Toma Tabacu506cfd02015-05-07 10:29:52 +0000185 if (softFloat)
186 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christophera9353d12014-09-26 01:44:08 +0000187
Toma Tabacu506cfd02015-05-07 10:29:52 +0000188 auto &I = SubtargetMap[CPU + FS];
Eric Christophera9353d12014-09-26 01:44:08 +0000189 if (!I) {
190 // This needs to be done before we create a new subtarget since any
191 // creation will depend on the TM and the code generation flags on the
192 // function that reside in TargetOptions.
193 resetTargetOptions(F);
Alexander Richardsoneb5ce8b2017-09-22 08:52:03 +0000194 I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, *this,
195 Options.StackAlignmentOverride);
Eric Christophera9353d12014-09-26 01:44:08 +0000196 }
197 return I.get();
198}
199
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000200void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
201 DEBUG(dbgs() << "resetSubtarget\n");
Eric Christophera9353d12014-09-26 01:44:08 +0000202
David Majnemerde360752014-09-26 02:57:05 +0000203 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
Eric Christopherfc6de422014-08-05 02:39:49 +0000204 MF->setSubtarget(Subtarget);
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000205}
206
Andrew Trickccb67362012-02-03 05:12:41 +0000207namespace {
Eugene Zelenko926883e2017-02-01 01:22:51 +0000208
Andrew Trickccb67362012-02-03 05:12:41 +0000209/// Mips Code Generator Pass Configuration Options.
210class MipsPassConfig : public TargetPassConfig {
211public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000212 MipsPassConfig(MipsTargetMachine &TM, PassManagerBase &PM)
Alexander Richardsoneb5ce8b2017-09-22 08:52:03 +0000213 : TargetPassConfig(TM, PM) {
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000214 // The current implementation of long branch pass requires a scratch
215 // register ($at) to be available before branch instructions. Tail merging
216 // can break this requirement, so disable it when long branch pass is
217 // enabled.
218 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
219 }
Andrew Trickccb67362012-02-03 05:12:41 +0000220
221 MipsTargetMachine &getMipsTargetMachine() const {
222 return getTM<MipsTargetMachine>();
223 }
224
225 const MipsSubtarget &getMipsSubtarget() const {
226 return *getMipsTargetMachine().getSubtargetImpl();
227 }
228
Craig Topper56c590a2014-04-29 07:58:02 +0000229 void addIRPasses() override;
230 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000231 void addPreEmitPass() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000232 void addPreRegAlloc() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000233};
Eugene Zelenko926883e2017-02-01 01:22:51 +0000234
235} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000236
Andrew Trickf8ea1082012-02-04 02:56:59 +0000237TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000238 return new MipsPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000239}
240
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000241void MipsPassConfig::addIRPasses() {
242 TargetPassConfig::addIRPasses();
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000243 addPass(createAtomicExpandPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000244 if (getMipsSubtarget().os16())
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000245 addPass(createMipsOs16Pass());
Reed Kotler783c7942013-05-10 22:25:39 +0000246 if (getMipsSubtarget().inMips16HardFloat())
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000247 addPass(createMips16HardFloatPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000248}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000249// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000250// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000251bool MipsPassConfig::addInstSelector() {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000252 addPass(createMipsModuleISelDagPass());
Daniel Sanders46fe6552016-07-14 13:25:22 +0000253 addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
254 addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000255 return false;
256}
257
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000258void MipsPassConfig::addPreRegAlloc() {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000259 addPass(createMipsOptimizePICCallPass());
Reed Kotler96b74022014-03-10 16:31:25 +0000260}
261
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000262TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000263 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000264 if (Subtarget->allowMixed16_32()) {
265 DEBUG(errs() << "No Target Transform Info Pass Added\n");
266 // FIXME: This is no longer necessary as the TTI returned is per-function.
Mehdi Amini5010ebf2015-07-09 02:08:42 +0000267 return TargetTransformInfo(F.getParent()->getDataLayout());
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000268 }
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000269
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000270 DEBUG(errs() << "Target Transform Info Pass Added\n");
Chandler Carruthc956ab662015-02-01 14:22:17 +0000271 return TargetTransformInfo(BasicTTIImpl(this, F));
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000272 });
Reed Kotler1595f362013-04-09 19:46:01 +0000273}
274
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000275// Implemented by targets that want to run passes immediately before
276// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000277// print out the code after the passes.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000278void MipsPassConfig::addPreEmitPass() {
Zoran Jovanovicffef3e32017-04-27 13:10:48 +0000279 addPass(createMicroMipsSizeReductionPass());
Daniel Sanderse8efff32016-03-14 16:24:05 +0000280
281 // The delay slot filler pass can potientially create forbidden slot (FS)
282 // hazards for MIPSR6 which the hazard schedule pass (HSP) will fix. Any
283 // (new) pass that creates compact branches after the HSP must handle FS
284 // hazards itself or be pipelined before the HSP.
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000285 addPass(createMipsDelaySlotFillerPass());
Chad Rosier7a21bb12016-03-14 18:10:20 +0000286 addPass(createMipsHazardSchedule());
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000287 addPass(createMipsLongBranchPass());
Rafael Espindola6f7c2802016-06-28 14:26:39 +0000288 addPass(createMipsConstantIslandPass());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000289}