blob: 25234b902332051940398f2435eaa3f65a2941fc [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
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000040extern "C" void LLVMInitializeMipsTarget() {
41 // Register the target.
Akira Hatanaka3d673cc2011-09-21 03:00:58 +000042 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
Eli Friedman57c11da2009-08-03 02:22:28 +000043 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
Akira Hatanaka30651802012-07-31 21:39:17 +000044 RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
45 RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000046}
47
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000048static std::string computeDataLayout(const MipsSubtarget &ST) {
49 std::string Ret = "";
50
51 // There are both little and big endian mips.
52 if (ST.isLittle())
53 Ret += "e";
54 else
55 Ret += "E";
56
Rafael Espindola58873562014-01-03 19:21:54 +000057 Ret += "-m:m";
58
Rafael Espindola8afbb282013-12-16 17:15:29 +000059 // Pointers are 32 bit on some ABIs.
60 if (!ST.isABI_N64())
Rafael Espindolabccb9d42013-12-16 18:01:51 +000061 Ret += "-p:32:32";
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000062
63 // 8 and 16 bit integers only need no have natural alignment, but try to
64 // align them to 32 bits. 64 bit integers have natural alignment.
Rafael Espindolabccb9d42013-12-16 18:01:51 +000065 Ret += "-i8:8:32-i16:16:32-i64:64";
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000066
67 // 32 bit registers are always available and the stack is at least 64 bit
68 // aligned. On N64 64 bit registers are also available and the stack is
69 // 128 bit aligned.
Rafael Espindolafebb8d22013-12-17 23:15:58 +000070 if (ST.isABI_N64() || ST.isABI_N32())
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000071 Ret += "-n32:64-S128";
72 else
73 Ret += "-n32-S64";
74
75 return Ret;
76}
77
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +000078// On function prologue, the stack is created by decrementing
79// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000080// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000081// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000082// Using CodeModel::Large enables different CALL behavior.
Eric Christopher4407dde2014-07-02 00:54:07 +000083MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
84 StringRef CPU, StringRef FS,
85 const TargetOptions &Options,
86 Reloc::Model RM, CodeModel::Model CM,
87 CodeGenOpt::Level OL, bool isLittle)
88 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
89 Subtarget(TT, CPU, FS, isLittle, RM, this),
90 DL(computeDataLayout(Subtarget)), InstrInfo(MipsInstrInfo::create(*this)),
91 FrameLowering(MipsFrameLowering::create(*this, Subtarget)),
92 TLInfo(MipsTargetLowering::create(*this)), TSInfo(DL), JITInfo() {
Rafael Espindola227144c2013-05-13 01:16:13 +000093 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +000094}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000095
Reed Kotler1595f362013-04-09 19:46:01 +000096
97void MipsTargetMachine::setHelperClassesMips16() {
98 InstrInfoSE.swap(InstrInfo);
99 FrameLoweringSE.swap(FrameLowering);
100 TLInfoSE.swap(TLInfo);
101 if (!InstrInfo16) {
102 InstrInfo.reset(MipsInstrInfo::create(*this));
103 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
104 TLInfo.reset(MipsTargetLowering::create(*this));
105 } else {
106 InstrInfo16.swap(InstrInfo);
107 FrameLowering16.swap(FrameLowering);
108 TLInfo16.swap(TLInfo);
109 }
110 assert(TLInfo && "null target lowering 16");
111 assert(InstrInfo && "null instr info 16");
112 assert(FrameLowering && "null frame lowering 16");
113}
114
115void MipsTargetMachine::setHelperClassesMipsSE() {
116 InstrInfo16.swap(InstrInfo);
117 FrameLowering16.swap(FrameLowering);
118 TLInfo16.swap(TLInfo);
119 if (!InstrInfoSE) {
120 InstrInfo.reset(MipsInstrInfo::create(*this));
121 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
122 TLInfo.reset(MipsTargetLowering::create(*this));
123 } else {
124 InstrInfoSE.swap(InstrInfo);
125 FrameLoweringSE.swap(FrameLowering);
126 TLInfoSE.swap(TLInfo);
127 }
128 assert(TLInfo && "null target lowering in SE");
129 assert(InstrInfo && "null instr info SE");
130 assert(FrameLowering && "null frame lowering SE");
131}
David Blaikiea379b1812011-12-20 02:50:00 +0000132void MipsebTargetMachine::anchor() { }
133
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000134MipsebTargetMachine::
135MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000136 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000137 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000138 CodeGenOpt::Level OL)
139 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000140
David Blaikiea379b1812011-12-20 02:50:00 +0000141void MipselTargetMachine::anchor() { }
142
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000143MipselTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +0000144MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000145 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000146 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000147 CodeGenOpt::Level OL)
148 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000149
Andrew Trickccb67362012-02-03 05:12:41 +0000150namespace {
151/// Mips Code Generator Pass Configuration Options.
152class MipsPassConfig : public TargetPassConfig {
153public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000154 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000155 : TargetPassConfig(TM, PM) {
156 // The current implementation of long branch pass requires a scratch
157 // register ($at) to be available before branch instructions. Tail merging
158 // can break this requirement, so disable it when long branch pass is
159 // enabled.
160 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
161 }
Andrew Trickccb67362012-02-03 05:12:41 +0000162
163 MipsTargetMachine &getMipsTargetMachine() const {
164 return getTM<MipsTargetMachine>();
165 }
166
167 const MipsSubtarget &getMipsSubtarget() const {
168 return *getMipsTargetMachine().getSubtargetImpl();
169 }
170
Craig Topper56c590a2014-04-29 07:58:02 +0000171 void addIRPasses() override;
172 bool addInstSelector() override;
173 void addMachineSSAOptimization() override;
174 bool addPreEmitPass() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000175
Craig Topper56c590a2014-04-29 07:58:02 +0000176 bool addPreRegAlloc() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000177
Andrew Trickccb67362012-02-03 05:12:41 +0000178};
179} // namespace
180
Andrew Trickf8ea1082012-02-04 02:56:59 +0000181TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
182 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000183}
184
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000185void MipsPassConfig::addIRPasses() {
186 TargetPassConfig::addIRPasses();
187 if (getMipsSubtarget().os16())
188 addPass(createMipsOs16(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000189 if (getMipsSubtarget().inMips16HardFloat())
190 addPass(createMips16HardFloat(getMipsTargetMachine()));
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000191 addPass(createPartiallyInlineLibCallsPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000192}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000193// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000194// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000195bool MipsPassConfig::addInstSelector() {
Reed Kotler1595f362013-04-09 19:46:01 +0000196 if (getMipsSubtarget().allowMixed16_32()) {
197 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
198 addPass(createMips16ISelDag(getMipsTargetMachine()));
199 addPass(createMipsSEISelDag(getMipsTargetMachine()));
200 } else {
201 addPass(createMipsISelDag(getMipsTargetMachine()));
202 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000203 return false;
204}
205
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000206void MipsPassConfig::addMachineSSAOptimization() {
207 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
208 TargetPassConfig::addMachineSSAOptimization();
209}
210
Reed Kotler96b74022014-03-10 16:31:25 +0000211bool MipsPassConfig::addPreRegAlloc() {
212 if (getOptLevel() == CodeGenOpt::None) {
213 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
214 return true;
215 }
216 else
217 return false;
218}
219
Reed Kotler1595f362013-04-09 19:46:01 +0000220void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
221 if (Subtarget.allowMixed16_32()) {
222 DEBUG(errs() << "No ");
223 //FIXME: The Basic Target Transform Info
224 // pass needs to become a function pass instead of
225 // being an immutable pass and then this method as it exists now
226 // would be unnecessary.
227 PM.add(createNoTargetTransformInfoPass());
228 } else
229 LLVMTargetMachine::addAnalysisPasses(PM);
230 DEBUG(errs() << "Target Transform Info Pass Added\n");
231}
232
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000233// Implemented by targets that want to run passes immediately before
234// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000235// print out the code after the passes.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000236bool MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000237 MipsTargetMachine &TM = getMipsTargetMachine();
Reed Kotler1595f362013-04-09 19:46:01 +0000238 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000239 addPass(createMipsDelaySlotFillerPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000240
Akira Hatanakaa8a05be2013-10-07 19:06:57 +0000241 if (Subtarget.enableLongBranchPass())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000242 addPass(createMipsLongBranchPass(TM));
Reed Kotler1595f362013-04-09 19:46:01 +0000243 if (Subtarget.inMips16Mode() ||
244 Subtarget.allowMixed16_32())
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000245 addPass(createMipsConstantIslandPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000246
Bruno Cardoso Lopesa7465122007-08-18 01:58:15 +0000247 return true;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000248}
Akira Hatanaka27916972011-04-15 19:52:08 +0000249
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000250bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengecb29082011-11-16 08:38:26 +0000251 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000252 // Machine code emitter pass for Mips.
253 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
254 return false;
255}