blob: 8a6386ff600891ed1e0905c8233f3fb39b5a4a52 [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"
Reed Kotler1595f362013-04-09 19:46:01 +000029#include "llvm/Analysis/TargetTransformInfo.h"
Andrew Trickccb67362012-02-03 05:12:41 +000030#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/PassManager.h"
Reed Kotler1595f362013-04-09 19:46:01 +000032#include "llvm/Support/Debug.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000034#include "llvm/Support/raw_ostream.h"
Richard Sandiford37cd6cf2013-08-23 10:27:02 +000035#include "llvm/Transforms/Scalar.h"
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
Reed Kotler1595f362013-04-09 19:46:01 +000040
41
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000042extern "C" void LLVMInitializeMipsTarget() {
43 // Register the target.
Akira Hatanaka3d673cc2011-09-21 03:00:58 +000044 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
Eli Friedman57c11da2009-08-03 02:22:28 +000045 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
Akira Hatanaka30651802012-07-31 21:39:17 +000046 RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
47 RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000048}
49
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000050static std::string computeDataLayout(const MipsSubtarget &ST) {
51 std::string Ret = "";
52
53 // There are both little and big endian mips.
54 if (ST.isLittle())
55 Ret += "e";
56 else
57 Ret += "E";
58
Rafael Espindola58873562014-01-03 19:21:54 +000059 Ret += "-m:m";
60
Rafael Espindola8afbb282013-12-16 17:15:29 +000061 // Pointers are 32 bit on some ABIs.
62 if (!ST.isABI_N64())
Rafael Espindolabccb9d42013-12-16 18:01:51 +000063 Ret += "-p:32:32";
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000064
65 // 8 and 16 bit integers only need no have natural alignment, but try to
66 // align them to 32 bits. 64 bit integers have natural alignment.
Rafael Espindolabccb9d42013-12-16 18:01:51 +000067 Ret += "-i8:8:32-i16:16:32-i64:64";
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000068
69 // 32 bit registers are always available and the stack is at least 64 bit
70 // aligned. On N64 64 bit registers are also available and the stack is
71 // 128 bit aligned.
Rafael Espindolafebb8d22013-12-17 23:15:58 +000072 if (ST.isABI_N64() || ST.isABI_N32())
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000073 Ret += "-n32:64-S128";
74 else
75 Ret += "-n32-S64";
76
77 return Ret;
78}
79
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +000080// On function prologue, the stack is created by decrementing
81// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000082// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000083// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000084// Using CodeModel::Large enables different CALL behavior.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000085MipsTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +000086MipsTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000087 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000088 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +000089 CodeGenOpt::Level OL,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000090 bool isLittle)
91 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Reed Kotler1595f362013-04-09 19:46:01 +000092 Subtarget(TT, CPU, FS, isLittle, RM, this),
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000093 DL(computeDataLayout(Subtarget)),
Akira Hatanakafab89292012-08-02 18:21:47 +000094 InstrInfo(MipsInstrInfo::create(*this)),
95 FrameLowering(MipsFrameLowering::create(*this, Subtarget)),
Akira Hatanaka66bc4192013-07-12 23:33:22 +000096 TLInfo(MipsTargetLowering::create(*this)), TSInfo(*this),
97 InstrItins(Subtarget.getInstrItineraryData()), JITInfo() {
Rafael Espindola227144c2013-05-13 01:16:13 +000098 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +000099}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000100
Reed Kotler1595f362013-04-09 19:46:01 +0000101
102void MipsTargetMachine::setHelperClassesMips16() {
103 InstrInfoSE.swap(InstrInfo);
104 FrameLoweringSE.swap(FrameLowering);
105 TLInfoSE.swap(TLInfo);
106 if (!InstrInfo16) {
107 InstrInfo.reset(MipsInstrInfo::create(*this));
108 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
109 TLInfo.reset(MipsTargetLowering::create(*this));
110 } else {
111 InstrInfo16.swap(InstrInfo);
112 FrameLowering16.swap(FrameLowering);
113 TLInfo16.swap(TLInfo);
114 }
115 assert(TLInfo && "null target lowering 16");
116 assert(InstrInfo && "null instr info 16");
117 assert(FrameLowering && "null frame lowering 16");
118}
119
120void MipsTargetMachine::setHelperClassesMipsSE() {
121 InstrInfo16.swap(InstrInfo);
122 FrameLowering16.swap(FrameLowering);
123 TLInfo16.swap(TLInfo);
124 if (!InstrInfoSE) {
125 InstrInfo.reset(MipsInstrInfo::create(*this));
126 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
127 TLInfo.reset(MipsTargetLowering::create(*this));
128 } else {
129 InstrInfoSE.swap(InstrInfo);
130 FrameLoweringSE.swap(FrameLowering);
131 TLInfoSE.swap(TLInfo);
132 }
133 assert(TLInfo && "null target lowering in SE");
134 assert(InstrInfo && "null instr info SE");
135 assert(FrameLowering && "null frame lowering SE");
136}
David Blaikiea379b1812011-12-20 02:50:00 +0000137void MipsebTargetMachine::anchor() { }
138
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000139MipsebTargetMachine::
140MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000141 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000142 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000143 CodeGenOpt::Level OL)
144 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000145
David Blaikiea379b1812011-12-20 02:50:00 +0000146void MipselTargetMachine::anchor() { }
147
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000148MipselTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +0000149MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000150 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000151 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000152 CodeGenOpt::Level OL)
153 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000154
Andrew Trickccb67362012-02-03 05:12:41 +0000155namespace {
156/// Mips Code Generator Pass Configuration Options.
157class MipsPassConfig : public TargetPassConfig {
158public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000159 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000160 : TargetPassConfig(TM, PM) {
161 // The current implementation of long branch pass requires a scratch
162 // register ($at) to be available before branch instructions. Tail merging
163 // can break this requirement, so disable it when long branch pass is
164 // enabled.
165 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
166 }
Andrew Trickccb67362012-02-03 05:12:41 +0000167
168 MipsTargetMachine &getMipsTargetMachine() const {
169 return getTM<MipsTargetMachine>();
170 }
171
172 const MipsSubtarget &getMipsSubtarget() const {
173 return *getMipsTargetMachine().getSubtargetImpl();
174 }
175
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000176 virtual void addIRPasses();
Andrew Trickccb67362012-02-03 05:12:41 +0000177 virtual bool addInstSelector();
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000178 virtual void addMachineSSAOptimization();
Andrew Trickccb67362012-02-03 05:12:41 +0000179 virtual bool addPreEmitPass();
Reed Kotler96b74022014-03-10 16:31:25 +0000180
181 virtual bool addPreRegAlloc();
182
Andrew Trickccb67362012-02-03 05:12:41 +0000183};
184} // namespace
185
Andrew Trickf8ea1082012-02-04 02:56:59 +0000186TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
187 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000188}
189
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000190void MipsPassConfig::addIRPasses() {
191 TargetPassConfig::addIRPasses();
192 if (getMipsSubtarget().os16())
193 addPass(createMipsOs16(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000194 if (getMipsSubtarget().inMips16HardFloat())
195 addPass(createMips16HardFloat(getMipsTargetMachine()));
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000196 addPass(createPartiallyInlineLibCallsPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000197}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000198// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000199// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000200bool MipsPassConfig::addInstSelector() {
Reed Kotler1595f362013-04-09 19:46:01 +0000201 if (getMipsSubtarget().allowMixed16_32()) {
202 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
203 addPass(createMips16ISelDag(getMipsTargetMachine()));
204 addPass(createMipsSEISelDag(getMipsTargetMachine()));
205 } else {
206 addPass(createMipsISelDag(getMipsTargetMachine()));
207 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000208 return false;
209}
210
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000211void MipsPassConfig::addMachineSSAOptimization() {
212 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
213 TargetPassConfig::addMachineSSAOptimization();
214}
215
Reed Kotler96b74022014-03-10 16:31:25 +0000216bool MipsPassConfig::addPreRegAlloc() {
217 if (getOptLevel() == CodeGenOpt::None) {
218 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
219 return true;
220 }
221 else
222 return false;
223}
224
Reed Kotler1595f362013-04-09 19:46:01 +0000225void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
226 if (Subtarget.allowMixed16_32()) {
227 DEBUG(errs() << "No ");
228 //FIXME: The Basic Target Transform Info
229 // pass needs to become a function pass instead of
230 // being an immutable pass and then this method as it exists now
231 // would be unnecessary.
232 PM.add(createNoTargetTransformInfoPass());
233 } else
234 LLVMTargetMachine::addAnalysisPasses(PM);
235 DEBUG(errs() << "Target Transform Info Pass Added\n");
236}
237
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000238// Implemented by targets that want to run passes immediately before
239// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000240// print out the code after the passes.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000241bool MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000242 MipsTargetMachine &TM = getMipsTargetMachine();
Reed Kotler1595f362013-04-09 19:46:01 +0000243 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000244 addPass(createMipsDelaySlotFillerPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000245
Akira Hatanakaa8a05be2013-10-07 19:06:57 +0000246 if (Subtarget.enableLongBranchPass())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000247 addPass(createMipsLongBranchPass(TM));
Reed Kotler1595f362013-04-09 19:46:01 +0000248 if (Subtarget.inMips16Mode() ||
249 Subtarget.allowMixed16_32())
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000250 addPass(createMipsConstantIslandPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000251
Bruno Cardoso Lopesa7465122007-08-18 01:58:15 +0000252 return true;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000253}
Akira Hatanaka27916972011-04-15 19:52:08 +0000254
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000255bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengecb29082011-11-16 08:38:26 +0000256 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000257 // Machine code emitter pass for Mips.
258 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
259 return false;
260}