blob: bb48188e3b87bb7ebae30e05793f6c2b54536321 [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
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsTargetMachine.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "Mips.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "Mips16FrameLowering.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "Mips16ISelDAGToDAG.h"
18#include "Mips16ISelLowering.h"
19#include "Mips16InstrInfo.h"
Akira Hatanakafab89292012-08-02 18:21:47 +000020#include "MipsFrameLowering.h"
21#include "MipsInstrInfo.h"
Reed Kotler1595f362013-04-09 19:46:01 +000022#include "MipsSEFrameLowering.h"
Reed Kotler1595f362013-04-09 19:46:01 +000023#include "MipsSEISelDAGToDAG.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "MipsSEISelLowering.h"
25#include "MipsSEInstrInfo.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000026#include "MipsTargetObjectFile.h"
Reed Kotler1595f362013-04-09 19:46:01 +000027#include "llvm/Analysis/TargetTransformInfo.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"
Chandler Carruth30d69c22015-02-13 10:01:29 +000030#include "llvm/IR/LegacyPassManager.h"
Reed Kotler1595f362013-04-09 19:46:01 +000031#include "llvm/Support/Debug.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Support/raw_ostream.h"
Richard Sandiford37cd6cf2013-08-23 10:27:02 +000034#include "llvm/Transforms/Scalar.h"
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000035
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000036using namespace llvm;
37
Chandler Carruthe96dd892014-04-21 22:55:11 +000038#define DEBUG_TYPE "mips"
39
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000040extern "C" void LLVMInitializeMipsTarget() {
41 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000042 RegisterTargetMachine<MipsebTargetMachine> X(getTheMipsTarget());
43 RegisterTargetMachine<MipselTargetMachine> Y(getTheMipselTarget());
44 RegisterTargetMachine<MipsebTargetMachine> A(getTheMips64Target());
45 RegisterTargetMachine<MipselTargetMachine> B(getTheMips64elTarget());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000046}
47
Daniel Sandersed64d622015-06-11 15:34:59 +000048static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +000049 const TargetOptions &Options,
50 bool isLittle) {
Eric Christopher8b770652015-01-26 19:03:15 +000051 std::string Ret = "";
Daniel Sanders50f17232015-09-15 16:17:27 +000052 MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions);
Eric Christopher8b770652015-01-26 19:03:15 +000053
54 // There are both little and big endian mips.
55 if (isLittle)
56 Ret += "e";
57 else
58 Ret += "E";
59
Daniel Sanders6a738832016-07-19 10:49:03 +000060 if (ABI.IsO32())
61 Ret += "-m:m";
62 else
63 Ret += "-m:e";
Eric Christopher8b770652015-01-26 19:03:15 +000064
65 // Pointers are 32 bit on some ABIs.
66 if (!ABI.IsN64())
67 Ret += "-p:32:32";
68
Sanjay Pateld4e1bb82015-07-07 21:31:54 +000069 // 8 and 16 bit integers only need to have natural alignment, but try to
Eric Christopher8b770652015-01-26 19:03:15 +000070 // align them to 32 bits. 64 bit integers have natural alignment.
71 Ret += "-i8:8:32-i16:16:32-i64:64";
72
73 // 32 bit registers are always available and the stack is at least 64 bit
74 // aligned. On N64 64 bit registers are also available and the stack is
75 // 128 bit aligned.
76 if (ABI.IsN64() || ABI.IsN32())
77 Ret += "-n32:64-S128";
78 else
79 Ret += "-n32-S64";
80
81 return Ret;
82}
83
Rafael Espindola8c34dd82016-05-18 22:04:49 +000084static Reloc::Model getEffectiveRelocModel(CodeModel::Model CM,
85 Optional<Reloc::Model> RM) {
86 if (!RM.hasValue() || CM == CodeModel::JITDefault)
87 return Reloc::Static;
88 return *RM;
89}
90
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +000091// On function prologue, the stack is created by decrementing
92// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000093// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000094// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000095// Using CodeModel::Large enables different CALL behavior.
Daniel Sanders3e5de882015-06-11 19:41:26 +000096MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT,
Eric Christopher4407dde2014-07-02 00:54:07 +000097 StringRef CPU, StringRef FS,
98 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +000099 Optional<Reloc::Model> RM,
100 CodeModel::Model CM, CodeGenOpt::Level OL,
101 bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000102 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000103 CPU, FS, Options, getEffectiveRelocModel(CM, RM), CM,
104 OL),
Eric Christophera5762812015-01-26 17:33:46 +0000105 isLittle(isLittle), TLOF(make_unique<MipsTargetObjectFile>()),
Daniel Sanders50f17232015-09-15 16:17:27 +0000106 ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000107 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this),
108 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
Eric Christopher90724282015-01-08 18:18:57 +0000109 isLittle, *this),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000110 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
111 isLittle, *this) {
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000112 Subtarget = &DefaultSubtarget;
Rafael Espindola227144c2013-05-13 01:16:13 +0000113 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +0000114}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000115
Reid Kleckner357600e2014-11-20 23:37:18 +0000116MipsTargetMachine::~MipsTargetMachine() {}
117
David Blaikiea379b1812011-12-20 02:50:00 +0000118void MipsebTargetMachine::anchor() { }
119
Daniel Sanders3e5de882015-06-11 19:41:26 +0000120MipsebTargetMachine::MipsebTargetMachine(const Target &T, const Triple &TT,
121 StringRef CPU, StringRef FS,
122 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000123 Optional<Reloc::Model> RM,
124 CodeModel::Model CM,
Daniel Sanders3e5de882015-06-11 19:41:26 +0000125 CodeGenOpt::Level OL)
126 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000127
David Blaikiea379b1812011-12-20 02:50:00 +0000128void MipselTargetMachine::anchor() { }
129
Daniel Sanders3e5de882015-06-11 19:41:26 +0000130MipselTargetMachine::MipselTargetMachine(const Target &T, const Triple &TT,
131 StringRef CPU, StringRef FS,
132 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000133 Optional<Reloc::Model> RM,
134 CodeModel::Model CM,
Daniel Sanders3e5de882015-06-11 19:41:26 +0000135 CodeGenOpt::Level OL)
136 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000137
Eric Christophera9353d12014-09-26 01:44:08 +0000138const MipsSubtarget *
David Majnemerde360752014-09-26 02:57:05 +0000139MipsTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000140 Attribute CPUAttr = F.getFnAttribute("target-cpu");
141 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christophera9353d12014-09-26 01:44:08 +0000142
143 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
144 ? CPUAttr.getValueAsString().str()
145 : TargetCPU;
146 std::string FS = !FSAttr.hasAttribute(Attribute::None)
147 ? FSAttr.getValueAsString().str()
148 : TargetFS;
149 bool hasMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000150 !F.getFnAttribute("mips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000151 bool hasNoMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000152 !F.getFnAttribute("nomips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000153
Eric Christopher6a0551e2014-09-29 21:57:54 +0000154 // FIXME: This is related to the code below to reset the target options,
155 // we need to know whether or not the soft float flag is set on the
Toma Tabacu506cfd02015-05-07 10:29:52 +0000156 // function, so we can enable it as a subtarget feature.
Eric Christopher824f42f2015-05-12 01:26:05 +0000157 bool softFloat =
158 F.hasFnAttribute("use-soft-float") &&
159 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
Eric Christopher6a0551e2014-09-29 21:57:54 +0000160
Eric Christophera9353d12014-09-26 01:44:08 +0000161 if (hasMips16Attr)
162 FS += FS.empty() ? "+mips16" : ",+mips16";
163 else if (hasNoMips16Attr)
164 FS += FS.empty() ? "-mips16" : ",-mips16";
Toma Tabacu506cfd02015-05-07 10:29:52 +0000165 if (softFloat)
166 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christophera9353d12014-09-26 01:44:08 +0000167
Toma Tabacu506cfd02015-05-07 10:29:52 +0000168 auto &I = SubtargetMap[CPU + FS];
Eric Christophera9353d12014-09-26 01:44:08 +0000169 if (!I) {
170 // This needs to be done before we create a new subtarget since any
171 // creation will depend on the TM and the code generation flags on the
172 // function that reside in TargetOptions.
173 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000174 I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle,
175 *this);
Eric Christophera9353d12014-09-26 01:44:08 +0000176 }
177 return I.get();
178}
179
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000180void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
181 DEBUG(dbgs() << "resetSubtarget\n");
Eric Christophera9353d12014-09-26 01:44:08 +0000182
David Majnemerde360752014-09-26 02:57:05 +0000183 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
Eric Christopherfc6de422014-08-05 02:39:49 +0000184 MF->setSubtarget(Subtarget);
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000185 return;
186}
187
Andrew Trickccb67362012-02-03 05:12:41 +0000188namespace {
189/// Mips Code Generator Pass Configuration Options.
190class MipsPassConfig : public TargetPassConfig {
191public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000192 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000193 : TargetPassConfig(TM, PM) {
194 // The current implementation of long branch pass requires a scratch
195 // register ($at) to be available before branch instructions. Tail merging
196 // can break this requirement, so disable it when long branch pass is
197 // enabled.
198 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
199 }
Andrew Trickccb67362012-02-03 05:12:41 +0000200
201 MipsTargetMachine &getMipsTargetMachine() const {
202 return getTM<MipsTargetMachine>();
203 }
204
205 const MipsSubtarget &getMipsSubtarget() const {
206 return *getMipsTargetMachine().getSubtargetImpl();
207 }
208
Craig Topper56c590a2014-04-29 07:58:02 +0000209 void addIRPasses() override;
210 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000211 void addPreEmitPass() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000212
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000213 void addPreRegAlloc() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000214
Andrew Trickccb67362012-02-03 05:12:41 +0000215};
216} // namespace
217
Andrew Trickf8ea1082012-02-04 02:56:59 +0000218TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
219 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000220}
221
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000222void MipsPassConfig::addIRPasses() {
223 TargetPassConfig::addIRPasses();
Robin Morissete2de06b2014-10-16 20:34:57 +0000224 addPass(createAtomicExpandPass(&getMipsTargetMachine()));
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000225 if (getMipsSubtarget().os16())
Vasileios Kalintiris6312f512015-03-14 08:34:25 +0000226 addPass(createMipsOs16Pass(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000227 if (getMipsSubtarget().inMips16HardFloat())
Vasileios Kalintiris6611eb32015-03-14 09:02:23 +0000228 addPass(createMips16HardFloatPass(getMipsTargetMachine()));
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000229}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000230// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000231// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000232bool MipsPassConfig::addInstSelector() {
Vasileios Kalintiris46fa9b72015-03-14 09:20:52 +0000233 addPass(createMipsModuleISelDagPass(getMipsTargetMachine()));
Daniel Sanders46fe6552016-07-14 13:25:22 +0000234 addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
235 addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000236 return false;
237}
238
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000239void MipsPassConfig::addPreRegAlloc() {
Vasileios Kalintirise3bb72e2016-11-02 15:11:27 +0000240 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
Reed Kotler96b74022014-03-10 16:31:25 +0000241}
242
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000243TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000244 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000245 if (Subtarget->allowMixed16_32()) {
246 DEBUG(errs() << "No Target Transform Info Pass Added\n");
247 // FIXME: This is no longer necessary as the TTI returned is per-function.
Mehdi Amini5010ebf2015-07-09 02:08:42 +0000248 return TargetTransformInfo(F.getParent()->getDataLayout());
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000249 }
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000250
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000251 DEBUG(errs() << "Target Transform Info Pass Added\n");
Chandler Carruthc956ab662015-02-01 14:22:17 +0000252 return TargetTransformInfo(BasicTTIImpl(this, F));
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000253 });
Reed Kotler1595f362013-04-09 19:46:01 +0000254}
255
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000256// Implemented by targets that want to run passes immediately before
257// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000258// print out the code after the passes.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000259void MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000260 MipsTargetMachine &TM = getMipsTargetMachine();
Daniel Sanderse8efff32016-03-14 16:24:05 +0000261
262 // The delay slot filler pass can potientially create forbidden slot (FS)
263 // hazards for MIPSR6 which the hazard schedule pass (HSP) will fix. Any
264 // (new) pass that creates compact branches after the HSP must handle FS
265 // hazards itself or be pipelined before the HSP.
Matthias Braunb2f23882014-12-11 23:18:03 +0000266 addPass(createMipsDelaySlotFillerPass(TM));
Chad Rosier7a21bb12016-03-14 18:10:20 +0000267 addPass(createMipsHazardSchedule());
Matthias Braunb2f23882014-12-11 23:18:03 +0000268 addPass(createMipsLongBranchPass(TM));
Rafael Espindola6f7c2802016-06-28 14:26:39 +0000269 addPass(createMipsConstantIslandPass());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000270}