blob: 339542ed2fd623c5d95f618b08ca106413ffcf94 [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"
17#include "Mips16HardFloat.h"
18#include "Mips16ISelDAGToDAG.h"
19#include "Mips16ISelLowering.h"
20#include "Mips16InstrInfo.h"
Akira Hatanakafab89292012-08-02 18:21:47 +000021#include "MipsFrameLowering.h"
22#include "MipsInstrInfo.h"
Reed Kotler1595f362013-04-09 19:46:01 +000023#include "MipsModuleISelDAGToDAG.h"
Reed Kotlerfe94cc32013-04-10 16:58:04 +000024#include "MipsOs16.h"
Reed Kotler1595f362013-04-09 19:46:01 +000025#include "MipsSEFrameLowering.h"
Reed Kotler1595f362013-04-09 19:46:01 +000026#include "MipsSEISelDAGToDAG.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000027#include "MipsSEISelLowering.h"
28#include "MipsSEInstrInfo.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000029#include "MipsTargetObjectFile.h"
Reed Kotler1595f362013-04-09 19:46:01 +000030#include "llvm/Analysis/TargetTransformInfo.h"
Andrew Trickccb67362012-02-03 05:12:41 +000031#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/PassManager.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"
Richard Sandiford37cd6cf2013-08-23 10:27:02 +000036#include "llvm/Transforms/Scalar.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000037using namespace llvm;
38
Chandler Carruthe96dd892014-04-21 22:55:11 +000039#define DEBUG_TYPE "mips"
40
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000041extern "C" void LLVMInitializeMipsTarget() {
42 // Register the target.
Akira Hatanaka3d673cc2011-09-21 03:00:58 +000043 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
Eli Friedman57c11da2009-08-03 02:22:28 +000044 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
Akira Hatanaka30651802012-07-31 21:39:17 +000045 RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
46 RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000047}
48
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +000049// On function prologue, the stack is created by decrementing
50// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000051// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000052// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000053// Using CodeModel::Large enables different CALL behavior.
Eric Christopher4407dde2014-07-02 00:54:07 +000054MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
55 StringRef CPU, StringRef FS,
56 const TargetOptions &Options,
57 Reloc::Model RM, CodeModel::Model CM,
58 CodeGenOpt::Level OL, bool isLittle)
59 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Eric Christophera5762812015-01-26 17:33:46 +000060 isLittle(isLittle), TLOF(make_unique<MipsTargetObjectFile>()),
61 ABI(MipsABIInfo::computeTargetABI(Triple(TT), CPU, Options.MCOptions)),
62 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this),
Eric Christopher4e7d1e72014-07-18 23:41:32 +000063 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
Eric Christopher90724282015-01-08 18:18:57 +000064 isLittle, *this),
Eric Christopher4e7d1e72014-07-18 23:41:32 +000065 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
Eric Christopher90724282015-01-08 18:18:57 +000066 isLittle, *this) {
Eric Christopher4e7d1e72014-07-18 23:41:32 +000067 Subtarget = &DefaultSubtarget;
Rafael Espindola227144c2013-05-13 01:16:13 +000068 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +000069}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000070
Reid Kleckner357600e2014-11-20 23:37:18 +000071MipsTargetMachine::~MipsTargetMachine() {}
72
David Blaikiea379b1812011-12-20 02:50:00 +000073void MipsebTargetMachine::anchor() { }
74
Akira Hatanaka3d673cc2011-09-21 03:00:58 +000075MipsebTargetMachine::
76MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000077 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000078 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000079 CodeGenOpt::Level OL)
80 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +000081
David Blaikiea379b1812011-12-20 02:50:00 +000082void MipselTargetMachine::anchor() { }
83
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +000084MipselTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +000085MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000086 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000087 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000088 CodeGenOpt::Level OL)
89 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +000090
Eric Christophera9353d12014-09-26 01:44:08 +000091const MipsSubtarget *
David Majnemerde360752014-09-26 02:57:05 +000092MipsTargetMachine::getSubtargetImpl(const Function &F) const {
Eric Christophera9353d12014-09-26 01:44:08 +000093 AttributeSet FnAttrs = F.getAttributes();
94 Attribute CPUAttr =
95 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
96 Attribute FSAttr =
97 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
98
99 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
100 ? CPUAttr.getValueAsString().str()
101 : TargetCPU;
102 std::string FS = !FSAttr.hasAttribute(Attribute::None)
103 ? FSAttr.getValueAsString().str()
104 : TargetFS;
105 bool hasMips16Attr =
106 !FnAttrs.getAttribute(AttributeSet::FunctionIndex, "mips16")
107 .hasAttribute(Attribute::None);
108 bool hasNoMips16Attr =
109 !FnAttrs.getAttribute(AttributeSet::FunctionIndex, "nomips16")
110 .hasAttribute(Attribute::None);
111
Eric Christopher6a0551e2014-09-29 21:57:54 +0000112 // FIXME: This is related to the code below to reset the target options,
113 // we need to know whether or not the soft float flag is set on the
114 // function before we can generate a subtarget. We also need to use
115 // it as a key for the subtarget since that can be the only difference
116 // between two functions.
117 Attribute SFAttr =
118 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "use-soft-float");
119 bool softFloat = !SFAttr.hasAttribute(Attribute::None)
Eric Christophera2db9222014-09-29 23:31:13 +0000120 ? SFAttr.getValueAsString() == "true"
Eric Christopher6a0551e2014-09-29 21:57:54 +0000121 : Options.UseSoftFloat;
122
Eric Christophera9353d12014-09-26 01:44:08 +0000123 if (hasMips16Attr)
124 FS += FS.empty() ? "+mips16" : ",+mips16";
125 else if (hasNoMips16Attr)
126 FS += FS.empty() ? "-mips16" : ",-mips16";
127
Eric Christopher6a0551e2014-09-29 21:57:54 +0000128 auto &I = SubtargetMap[CPU + FS + (softFloat ? "use-soft-float=true"
129 : "use-soft-float=false")];
Eric Christophera9353d12014-09-26 01:44:08 +0000130 if (!I) {
131 // This needs to be done before we create a new subtarget since any
132 // creation will depend on the TM and the code generation flags on the
133 // function that reside in TargetOptions.
134 resetTargetOptions(F);
Eric Christopher90724282015-01-08 18:18:57 +0000135 I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, *this);
Eric Christophera9353d12014-09-26 01:44:08 +0000136 }
137 return I.get();
138}
139
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000140void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
141 DEBUG(dbgs() << "resetSubtarget\n");
Eric Christophera9353d12014-09-26 01:44:08 +0000142
David Majnemerde360752014-09-26 02:57:05 +0000143 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
Eric Christopherfc6de422014-08-05 02:39:49 +0000144 MF->setSubtarget(Subtarget);
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000145 return;
146}
147
Andrew Trickccb67362012-02-03 05:12:41 +0000148namespace {
149/// Mips Code Generator Pass Configuration Options.
150class MipsPassConfig : public TargetPassConfig {
151public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000152 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000153 : TargetPassConfig(TM, PM) {
154 // The current implementation of long branch pass requires a scratch
155 // register ($at) to be available before branch instructions. Tail merging
156 // can break this requirement, so disable it when long branch pass is
157 // enabled.
158 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
159 }
Andrew Trickccb67362012-02-03 05:12:41 +0000160
161 MipsTargetMachine &getMipsTargetMachine() const {
162 return getTM<MipsTargetMachine>();
163 }
164
165 const MipsSubtarget &getMipsSubtarget() const {
166 return *getMipsTargetMachine().getSubtargetImpl();
167 }
168
Craig Topper56c590a2014-04-29 07:58:02 +0000169 void addIRPasses() override;
170 bool addInstSelector() override;
171 void addMachineSSAOptimization() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000172 void addPreEmitPass() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000173
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000174 void addPreRegAlloc() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000175
Andrew Trickccb67362012-02-03 05:12:41 +0000176};
177} // namespace
178
Andrew Trickf8ea1082012-02-04 02:56:59 +0000179TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
180 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000181}
182
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000183void MipsPassConfig::addIRPasses() {
184 TargetPassConfig::addIRPasses();
Robin Morissete2de06b2014-10-16 20:34:57 +0000185 addPass(createAtomicExpandPass(&getMipsTargetMachine()));
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000186 if (getMipsSubtarget().os16())
187 addPass(createMipsOs16(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000188 if (getMipsSubtarget().inMips16HardFloat())
189 addPass(createMips16HardFloat(getMipsTargetMachine()));
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000190}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000191// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000192// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000193bool MipsPassConfig::addInstSelector() {
Eric Christophera08db01b2014-07-18 20:29:02 +0000194 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
195 addPass(createMips16ISelDag(getMipsTargetMachine()));
196 addPass(createMipsSEISelDag(getMipsTargetMachine()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000197 return false;
198}
199
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000200void MipsPassConfig::addMachineSSAOptimization() {
201 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
202 TargetPassConfig::addMachineSSAOptimization();
203}
204
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000205void MipsPassConfig::addPreRegAlloc() {
206 if (getOptLevel() == CodeGenOpt::None)
Reed Kotler96b74022014-03-10 16:31:25 +0000207 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
Reed Kotler96b74022014-03-10 16:31:25 +0000208}
209
Reed Kotler1595f362013-04-09 19:46:01 +0000210void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000211 if (Subtarget->allowMixed16_32()) {
Reed Kotler1595f362013-04-09 19:46:01 +0000212 DEBUG(errs() << "No ");
213 //FIXME: The Basic Target Transform Info
214 // pass needs to become a function pass instead of
215 // being an immutable pass and then this method as it exists now
216 // would be unnecessary.
217 PM.add(createNoTargetTransformInfoPass());
218 } else
219 LLVMTargetMachine::addAnalysisPasses(PM);
220 DEBUG(errs() << "Target Transform Info Pass Added\n");
221}
222
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000223// Implemented by targets that want to run passes immediately before
224// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000225// print out the code after the passes.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000226void MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000227 MipsTargetMachine &TM = getMipsTargetMachine();
Matthias Braunb2f23882014-12-11 23:18:03 +0000228 addPass(createMipsDelaySlotFillerPass(TM));
229 addPass(createMipsLongBranchPass(TM));
Eric Christophera08db01b2014-07-18 20:29:02 +0000230 addPass(createMipsConstantIslandPass(TM));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000231}