blob: a45a9c4b41c37b62911fa64f1146884460f0820d [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
Eugene Zelenko926883e2017-02-01 01:22:51 +000014#include "MCTargetDesc/MipsABIInfo.h"
15#include "MCTargetDesc/MipsMCTargetDesc.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "Mips.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "Mips16ISelDAGToDAG.h"
Reed Kotler1595f362013-04-09 19:46:01 +000018#include "MipsSEISelDAGToDAG.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000019#include "MipsSubtarget.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000020#include "MipsTargetObjectFile.h"
Eugene Zelenko926883e2017-02-01 01:22:51 +000021#include "MipsTargetMachine.h"
22#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 Espindola8c34dd82016-05-18 22:04:49 +000087static Reloc::Model getEffectiveRelocModel(CodeModel::Model CM,
88 Optional<Reloc::Model> RM) {
89 if (!RM.hasValue() || CM == CodeModel::JITDefault)
90 return Reloc::Static;
91 return *RM;
92}
93
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +000094// On function prologue, the stack is created by decrementing
95// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000096// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000097// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000098// Using CodeModel::Large enables different CALL behavior.
Daniel Sanders3e5de882015-06-11 19:41:26 +000099MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT,
Eric Christopher4407dde2014-07-02 00:54:07 +0000100 StringRef CPU, StringRef FS,
101 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000102 Optional<Reloc::Model> RM,
103 CodeModel::Model CM, CodeGenOpt::Level OL,
104 bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000105 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000106 CPU, FS, Options, getEffectiveRelocModel(CM, RM), CM,
107 OL),
Eugene Zelenko926883e2017-02-01 01:22:51 +0000108 isLittle(isLittle), TLOF(llvm::make_unique<MipsTargetObjectFile>()),
Daniel Sanders50f17232015-09-15 16:17:27 +0000109 ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000110 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this),
111 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
Eric Christopher90724282015-01-08 18:18:57 +0000112 isLittle, *this),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000113 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
114 isLittle, *this) {
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000115 Subtarget = &DefaultSubtarget;
Rafael Espindola227144c2013-05-13 01:16:13 +0000116 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +0000117}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000118
Eugene Zelenko926883e2017-02-01 01:22:51 +0000119MipsTargetMachine::~MipsTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000120
Eugene Zelenko926883e2017-02-01 01:22:51 +0000121void MipsebTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000122
Daniel Sanders3e5de882015-06-11 19:41:26 +0000123MipsebTargetMachine::MipsebTargetMachine(const Target &T, const Triple &TT,
124 StringRef CPU, StringRef FS,
125 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000126 Optional<Reloc::Model> RM,
127 CodeModel::Model CM,
Daniel Sanders3e5de882015-06-11 19:41:26 +0000128 CodeGenOpt::Level OL)
129 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000130
Eugene Zelenko926883e2017-02-01 01:22:51 +0000131void MipselTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000132
Daniel Sanders3e5de882015-06-11 19:41:26 +0000133MipselTargetMachine::MipselTargetMachine(const Target &T, const Triple &TT,
134 StringRef CPU, StringRef FS,
135 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000136 Optional<Reloc::Model> RM,
137 CodeModel::Model CM,
Daniel Sanders3e5de882015-06-11 19:41:26 +0000138 CodeGenOpt::Level OL)
139 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000140
Eric Christophera9353d12014-09-26 01:44:08 +0000141const MipsSubtarget *
David Majnemerde360752014-09-26 02:57:05 +0000142MipsTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000143 Attribute CPUAttr = F.getFnAttribute("target-cpu");
144 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christophera9353d12014-09-26 01:44:08 +0000145
146 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
147 ? CPUAttr.getValueAsString().str()
148 : TargetCPU;
149 std::string FS = !FSAttr.hasAttribute(Attribute::None)
150 ? FSAttr.getValueAsString().str()
151 : TargetFS;
152 bool hasMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000153 !F.getFnAttribute("mips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000154 bool hasNoMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000155 !F.getFnAttribute("nomips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000156
Eric Christopher6a0551e2014-09-29 21:57:54 +0000157 // FIXME: This is related to the code below to reset the target options,
158 // we need to know whether or not the soft float flag is set on the
Toma Tabacu506cfd02015-05-07 10:29:52 +0000159 // function, so we can enable it as a subtarget feature.
Eric Christopher824f42f2015-05-12 01:26:05 +0000160 bool softFloat =
161 F.hasFnAttribute("use-soft-float") &&
162 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
Eric Christopher6a0551e2014-09-29 21:57:54 +0000163
Eric Christophera9353d12014-09-26 01:44:08 +0000164 if (hasMips16Attr)
165 FS += FS.empty() ? "+mips16" : ",+mips16";
166 else if (hasNoMips16Attr)
167 FS += FS.empty() ? "-mips16" : ",-mips16";
Toma Tabacu506cfd02015-05-07 10:29:52 +0000168 if (softFloat)
169 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christophera9353d12014-09-26 01:44:08 +0000170
Toma Tabacu506cfd02015-05-07 10:29:52 +0000171 auto &I = SubtargetMap[CPU + FS];
Eric Christophera9353d12014-09-26 01:44:08 +0000172 if (!I) {
173 // This needs to be done before we create a new subtarget since any
174 // creation will depend on the TM and the code generation flags on the
175 // function that reside in TargetOptions.
176 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000177 I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle,
178 *this);
Eric Christophera9353d12014-09-26 01:44:08 +0000179 }
180 return I.get();
181}
182
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000183void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
184 DEBUG(dbgs() << "resetSubtarget\n");
Eric Christophera9353d12014-09-26 01:44:08 +0000185
David Majnemerde360752014-09-26 02:57:05 +0000186 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
Eric Christopherfc6de422014-08-05 02:39:49 +0000187 MF->setSubtarget(Subtarget);
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000188}
189
Andrew Trickccb67362012-02-03 05:12:41 +0000190namespace {
Eugene Zelenko926883e2017-02-01 01:22:51 +0000191
Andrew Trickccb67362012-02-03 05:12:41 +0000192/// Mips Code Generator Pass Configuration Options.
193class MipsPassConfig : public TargetPassConfig {
194public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000195 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000196 : TargetPassConfig(TM, PM) {
197 // The current implementation of long branch pass requires a scratch
198 // register ($at) to be available before branch instructions. Tail merging
199 // can break this requirement, so disable it when long branch pass is
200 // enabled.
201 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
202 }
Andrew Trickccb67362012-02-03 05:12:41 +0000203
204 MipsTargetMachine &getMipsTargetMachine() const {
205 return getTM<MipsTargetMachine>();
206 }
207
208 const MipsSubtarget &getMipsSubtarget() const {
209 return *getMipsTargetMachine().getSubtargetImpl();
210 }
211
Craig Topper56c590a2014-04-29 07:58:02 +0000212 void addIRPasses() override;
213 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000214 void addPreEmitPass() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000215 void addPreRegAlloc() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000216};
Eugene Zelenko926883e2017-02-01 01:22:51 +0000217
218} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000219
Andrew Trickf8ea1082012-02-04 02:56:59 +0000220TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
221 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000222}
223
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000224void MipsPassConfig::addIRPasses() {
225 TargetPassConfig::addIRPasses();
Robin Morissete2de06b2014-10-16 20:34:57 +0000226 addPass(createAtomicExpandPass(&getMipsTargetMachine()));
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000227 if (getMipsSubtarget().os16())
Vasileios Kalintiris6312f512015-03-14 08:34:25 +0000228 addPass(createMipsOs16Pass(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000229 if (getMipsSubtarget().inMips16HardFloat())
Vasileios Kalintiris6611eb32015-03-14 09:02:23 +0000230 addPass(createMips16HardFloatPass(getMipsTargetMachine()));
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000231}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000232// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000233// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000234bool MipsPassConfig::addInstSelector() {
Vasileios Kalintiris46fa9b72015-03-14 09:20:52 +0000235 addPass(createMipsModuleISelDagPass(getMipsTargetMachine()));
Daniel Sanders46fe6552016-07-14 13:25:22 +0000236 addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
237 addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000238 return false;
239}
240
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000241void MipsPassConfig::addPreRegAlloc() {
Vasileios Kalintirise3bb72e2016-11-02 15:11:27 +0000242 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
Reed Kotler96b74022014-03-10 16:31:25 +0000243}
244
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000245TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000246 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000247 if (Subtarget->allowMixed16_32()) {
248 DEBUG(errs() << "No Target Transform Info Pass Added\n");
249 // FIXME: This is no longer necessary as the TTI returned is per-function.
Mehdi Amini5010ebf2015-07-09 02:08:42 +0000250 return TargetTransformInfo(F.getParent()->getDataLayout());
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000251 }
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000252
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000253 DEBUG(errs() << "Target Transform Info Pass Added\n");
Chandler Carruthc956ab662015-02-01 14:22:17 +0000254 return TargetTransformInfo(BasicTTIImpl(this, F));
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000255 });
Reed Kotler1595f362013-04-09 19:46:01 +0000256}
257
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000258// Implemented by targets that want to run passes immediately before
259// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000260// print out the code after the passes.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000261void MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000262 MipsTargetMachine &TM = getMipsTargetMachine();
Daniel Sanderse8efff32016-03-14 16:24:05 +0000263
264 // The delay slot filler pass can potientially create forbidden slot (FS)
265 // hazards for MIPSR6 which the hazard schedule pass (HSP) will fix. Any
266 // (new) pass that creates compact branches after the HSP must handle FS
267 // hazards itself or be pipelined before the HSP.
Matthias Braunb2f23882014-12-11 23:18:03 +0000268 addPass(createMipsDelaySlotFillerPass(TM));
Chad Rosier7a21bb12016-03-14 18:10:20 +0000269 addPass(createMipsHazardSchedule());
Matthias Braunb2f23882014-12-11 23:18:03 +0000270 addPass(createMipsLongBranchPass(TM));
Rafael Espindola6f7c2802016-06-28 14:26:39 +0000271 addPass(createMipsConstantIslandPass());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000272}