blob: f25afe33ff9cfa960666bef504e9c6d5f797a064 [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// Implements the info about Mips target spec.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#include "MipsTargetMachine.h"
Craig Topper79aa3412012-03-17 18:46:09 +000015#include "Mips.h"
Akira Hatanakaaf266262012-08-02 18:21:47 +000016#include "MipsFrameLowering.h"
17#include "MipsInstrInfo.h"
Reed Kotlera430cb62013-04-09 19:46:01 +000018#include "MipsModuleISelDAGToDAG.h"
Reed Kotler74adad62013-04-10 16:58:04 +000019#include "MipsOs16.h"
Reed Kotlera430cb62013-04-09 19:46:01 +000020#include "MipsSEFrameLowering.h"
21#include "MipsSEInstrInfo.h"
22#include "MipsSEISelLowering.h"
23#include "MipsSEISelDAGToDAG.h"
24#include "Mips16FrameLowering.h"
Reed Kotler46090912013-05-10 22:25:39 +000025#include "Mips16HardFloat.h"
Reed Kotlera430cb62013-04-09 19:46:01 +000026#include "Mips16InstrInfo.h"
27#include "Mips16ISelDAGToDAG.h"
28#include "Mips16ISelLowering.h"
29#include "llvm/Analysis/TargetTransformInfo.h"
Andrew Trick843ee2e2012-02-03 05:12:41 +000030#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/PassManager.h"
Reed Kotlera430cb62013-04-09 19:46:01 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Richard Sandiforda8a70992013-08-23 10:27:02 +000035#include "llvm/Transforms/Scalar.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Reed Kotlera430cb62013-04-09 19:46:01 +000038
39
Daniel Dunbar0c795d62009-07-25 06:49:55 +000040extern "C" void LLVMInitializeMipsTarget() {
41 // Register the target.
Akira Hatanaka24648102011-09-21 03:00:58 +000042 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
Eli Friedmane2c74082009-08-03 02:22:28 +000043 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
Akira Hatanakab4f921b2012-07-31 21:39:17 +000044 RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
45 RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000046}
47
48// DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
Bruno Cardoso Lopes51195af2007-08-28 05:13:42 +000049// The stack is always 8 byte aligned
50// On function prologue, the stack is created by decrementing
51// its pointer. Once decremented, all references are done with positive
Anton Korobeynikov33464912010-11-15 00:06:54 +000052// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000053// an easier handling.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000054// Using CodeModel::Large enables different CALL behavior.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000055MipsTargetMachine::
Evan Cheng43966132011-07-19 06:37:02 +000056MipsTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000057 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Cheng34ad6db2011-07-20 07:51:56 +000058 Reloc::Model RM, CodeModel::Model CM,
Evan Chengb95fc312011-11-16 08:38:26 +000059 CodeGenOpt::Level OL,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000060 bool isLittle)
61 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Reed Kotlera430cb62013-04-09 19:46:01 +000062 Subtarget(TT, CPU, FS, isLittle, RM, this),
Micah Villmow3574eca2012-10-08 16:38:25 +000063 DL(isLittle ?
Nick Lewycky8a8d4792011-12-02 22:16:29 +000064 (Subtarget.isABI_N64() ?
Akira Hatanaka3f0ef852013-01-05 02:00:56 +000065 "e-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-"
66 "n32:64-S128" :
67 "e-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32-S64") :
Nick Lewycky8a8d4792011-12-02 22:16:29 +000068 (Subtarget.isABI_N64() ?
Akira Hatanaka3f0ef852013-01-05 02:00:56 +000069 "E-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-"
70 "n32:64-S128" :
71 "E-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32-S64")),
Akira Hatanakaaf266262012-08-02 18:21:47 +000072 InstrInfo(MipsInstrInfo::create(*this)),
73 FrameLowering(MipsFrameLowering::create(*this, Subtarget)),
Akira Hatanaka434c0bd2013-07-12 23:33:22 +000074 TLInfo(MipsTargetLowering::create(*this)), TSInfo(*this),
75 InstrItins(Subtarget.getInstrItineraryData()), JITInfo() {
Rafael Espindola4a971702013-05-13 01:16:13 +000076 initAsmInfo();
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000077}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000078
Reed Kotlera430cb62013-04-09 19:46:01 +000079
80void MipsTargetMachine::setHelperClassesMips16() {
81 InstrInfoSE.swap(InstrInfo);
82 FrameLoweringSE.swap(FrameLowering);
83 TLInfoSE.swap(TLInfo);
84 if (!InstrInfo16) {
85 InstrInfo.reset(MipsInstrInfo::create(*this));
86 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
87 TLInfo.reset(MipsTargetLowering::create(*this));
88 } else {
89 InstrInfo16.swap(InstrInfo);
90 FrameLowering16.swap(FrameLowering);
91 TLInfo16.swap(TLInfo);
92 }
93 assert(TLInfo && "null target lowering 16");
94 assert(InstrInfo && "null instr info 16");
95 assert(FrameLowering && "null frame lowering 16");
96}
97
98void MipsTargetMachine::setHelperClassesMipsSE() {
99 InstrInfo16.swap(InstrInfo);
100 FrameLowering16.swap(FrameLowering);
101 TLInfo16.swap(TLInfo);
102 if (!InstrInfoSE) {
103 InstrInfo.reset(MipsInstrInfo::create(*this));
104 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
105 TLInfo.reset(MipsTargetLowering::create(*this));
106 } else {
107 InstrInfoSE.swap(InstrInfo);
108 FrameLoweringSE.swap(FrameLowering);
109 TLInfoSE.swap(TLInfo);
110 }
111 assert(TLInfo && "null target lowering in SE");
112 assert(InstrInfo && "null instr info SE");
113 assert(FrameLowering && "null frame lowering SE");
114}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000115void MipsebTargetMachine::anchor() { }
116
Akira Hatanaka24648102011-09-21 03:00:58 +0000117MipsebTargetMachine::
118MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000119 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +0000120 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000121 CodeGenOpt::Level OL)
122 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka24648102011-09-21 03:00:58 +0000123
David Blaikie2d24e2a2011-12-20 02:50:00 +0000124void MipselTargetMachine::anchor() { }
125
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000126MipselTargetMachine::
Evan Cheng43966132011-07-19 06:37:02 +0000127MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000128 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +0000129 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000130 CodeGenOpt::Level OL)
131 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000132
Andrew Trick843ee2e2012-02-03 05:12:41 +0000133namespace {
134/// Mips Code Generator Pass Configuration Options.
135class MipsPassConfig : public TargetPassConfig {
136public:
Andrew Trick061efcf2012-02-04 02:56:59 +0000137 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
138 : TargetPassConfig(TM, PM) {}
Andrew Trick843ee2e2012-02-03 05:12:41 +0000139
140 MipsTargetMachine &getMipsTargetMachine() const {
141 return getTM<MipsTargetMachine>();
142 }
143
144 const MipsSubtarget &getMipsSubtarget() const {
145 return *getMipsTargetMachine().getSubtargetImpl();
146 }
147
Reed Kotler74adad62013-04-10 16:58:04 +0000148 virtual void addIRPasses();
Andrew Trick843ee2e2012-02-03 05:12:41 +0000149 virtual bool addInstSelector();
Andrew Trick843ee2e2012-02-03 05:12:41 +0000150 virtual bool addPreEmitPass();
151};
152} // namespace
153
Andrew Trick061efcf2012-02-04 02:56:59 +0000154TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
155 return new MipsPassConfig(this, PM);
Andrew Trick843ee2e2012-02-03 05:12:41 +0000156}
157
Reed Kotler74adad62013-04-10 16:58:04 +0000158void MipsPassConfig::addIRPasses() {
159 TargetPassConfig::addIRPasses();
160 if (getMipsSubtarget().os16())
161 addPass(createMipsOs16(getMipsTargetMachine()));
Reed Kotler46090912013-05-10 22:25:39 +0000162 if (getMipsSubtarget().inMips16HardFloat())
163 addPass(createMips16HardFloat(getMipsTargetMachine()));
Richard Sandiforda8a70992013-08-23 10:27:02 +0000164 addPass(createPartiallyInlineLibCallsPass());
Reed Kotler74adad62013-04-10 16:58:04 +0000165}
Anton Korobeynikov33464912010-11-15 00:06:54 +0000166// Install an instruction selector pass using
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000167// the ISelDag to gen Mips code.
Bill Wendling7c4ce302012-05-01 08:27:43 +0000168bool MipsPassConfig::addInstSelector() {
Reed Kotlera430cb62013-04-09 19:46:01 +0000169 if (getMipsSubtarget().allowMixed16_32()) {
170 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
171 addPass(createMips16ISelDag(getMipsTargetMachine()));
172 addPass(createMipsSEISelDag(getMipsTargetMachine()));
173 } else {
174 addPass(createMipsISelDag(getMipsTargetMachine()));
175 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000176 return false;
177}
178
Reed Kotlera430cb62013-04-09 19:46:01 +0000179void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
180 if (Subtarget.allowMixed16_32()) {
181 DEBUG(errs() << "No ");
182 //FIXME: The Basic Target Transform Info
183 // pass needs to become a function pass instead of
184 // being an immutable pass and then this method as it exists now
185 // would be unnecessary.
186 PM.add(createNoTargetTransformInfoPass());
187 } else
188 LLVMTargetMachine::addAnalysisPasses(PM);
189 DEBUG(errs() << "Target Transform Info Pass Added\n");
190}
191
Anton Korobeynikov33464912010-11-15 00:06:54 +0000192// Implemented by targets that want to run passes immediately before
193// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000194// print out the code after the passes.
Bill Wendling7c4ce302012-05-01 08:27:43 +0000195bool MipsPassConfig::addPreEmitPass() {
Akira Hatanaka9e975872012-06-14 01:19:35 +0000196 MipsTargetMachine &TM = getMipsTargetMachine();
Reed Kotlera430cb62013-04-09 19:46:01 +0000197 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Bob Wilson564fbf62012-07-02 19:48:31 +0000198 addPass(createMipsDelaySlotFillerPass(TM));
Akira Hatanaka9e975872012-06-14 01:19:35 +0000199
Reed Kotlera430cb62013-04-09 19:46:01 +0000200 if (Subtarget.hasStandardEncoding() ||
201 Subtarget.allowMixed16_32())
Bob Wilson564fbf62012-07-02 19:48:31 +0000202 addPass(createMipsLongBranchPass(TM));
Reed Kotlera430cb62013-04-09 19:46:01 +0000203 if (Subtarget.inMips16Mode() ||
204 Subtarget.allowMixed16_32())
Reed Kotlerd056dc02013-02-27 03:33:58 +0000205 addPass(createMipsConstantIslandPass(TM));
Akira Hatanaka9e975872012-06-14 01:19:35 +0000206
Bruno Cardoso Lopesaff42dc2007-08-18 01:58:15 +0000207 return true;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000208}
Akira Hatanaka99a2e982011-04-15 19:52:08 +0000209
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000210bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengb95fc312011-11-16 08:38:26 +0000211 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000212 // Machine code emitter pass for Mips.
213 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
214 return false;
215}