blob: 18c1ccedfd58d2404cf48766c65761f09659ad33 [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"
19#include "MipsSEFrameLowering.h"
20#include "MipsSEInstrInfo.h"
21#include "MipsSEISelLowering.h"
22#include "MipsSEISelDAGToDAG.h"
23#include "Mips16FrameLowering.h"
24#include "Mips16InstrInfo.h"
25#include "Mips16ISelDAGToDAG.h"
26#include "Mips16ISelLowering.h"
27#include "llvm/Analysis/TargetTransformInfo.h"
Andrew Trick843ee2e2012-02-03 05:12:41 +000028#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/PassManager.h"
Reed Kotlera430cb62013-04-09 19:46:01 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033using namespace llvm;
34
Reed Kotlera430cb62013-04-09 19:46:01 +000035
36
Daniel Dunbar0c795d62009-07-25 06:49:55 +000037extern "C" void LLVMInitializeMipsTarget() {
38 // Register the target.
Akira Hatanaka24648102011-09-21 03:00:58 +000039 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
Eli Friedmane2c74082009-08-03 02:22:28 +000040 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
Akira Hatanakab4f921b2012-07-31 21:39:17 +000041 RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
42 RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043}
44
45// DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
Bruno Cardoso Lopes51195af2007-08-28 05:13:42 +000046// The stack is always 8 byte aligned
47// On function prologue, the stack is created by decrementing
48// its pointer. Once decremented, all references are done with positive
Anton Korobeynikov33464912010-11-15 00:06:54 +000049// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000050// an easier handling.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000051// Using CodeModel::Large enables different CALL behavior.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000052MipsTargetMachine::
Evan Cheng43966132011-07-19 06:37:02 +000053MipsTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000054 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Cheng34ad6db2011-07-20 07:51:56 +000055 Reloc::Model RM, CodeModel::Model CM,
Evan Chengb95fc312011-11-16 08:38:26 +000056 CodeGenOpt::Level OL,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000057 bool isLittle)
58 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Reed Kotlera430cb62013-04-09 19:46:01 +000059 Subtarget(TT, CPU, FS, isLittle, RM, this),
Micah Villmow3574eca2012-10-08 16:38:25 +000060 DL(isLittle ?
Nick Lewycky8a8d4792011-12-02 22:16:29 +000061 (Subtarget.isABI_N64() ?
Akira Hatanaka3f0ef852013-01-05 02:00:56 +000062 "e-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-"
63 "n32:64-S128" :
64 "e-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32-S64") :
Nick Lewycky8a8d4792011-12-02 22:16:29 +000065 (Subtarget.isABI_N64() ?
Akira Hatanaka3f0ef852013-01-05 02:00:56 +000066 "E-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-"
67 "n32:64-S128" :
68 "E-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32-S64")),
Akira Hatanakaaf266262012-08-02 18:21:47 +000069 InstrInfo(MipsInstrInfo::create(*this)),
70 FrameLowering(MipsFrameLowering::create(*this, Subtarget)),
Reed Kotlera430cb62013-04-09 19:46:01 +000071 TLInfo(MipsTargetLowering::create(*this)),
72 TSInfo(*this), JITInfo() {
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000073}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000074
Reed Kotlera430cb62013-04-09 19:46:01 +000075
76void MipsTargetMachine::setHelperClassesMips16() {
77 InstrInfoSE.swap(InstrInfo);
78 FrameLoweringSE.swap(FrameLowering);
79 TLInfoSE.swap(TLInfo);
80 if (!InstrInfo16) {
81 InstrInfo.reset(MipsInstrInfo::create(*this));
82 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
83 TLInfo.reset(MipsTargetLowering::create(*this));
84 } else {
85 InstrInfo16.swap(InstrInfo);
86 FrameLowering16.swap(FrameLowering);
87 TLInfo16.swap(TLInfo);
88 }
89 assert(TLInfo && "null target lowering 16");
90 assert(InstrInfo && "null instr info 16");
91 assert(FrameLowering && "null frame lowering 16");
92}
93
94void MipsTargetMachine::setHelperClassesMipsSE() {
95 InstrInfo16.swap(InstrInfo);
96 FrameLowering16.swap(FrameLowering);
97 TLInfo16.swap(TLInfo);
98 if (!InstrInfoSE) {
99 InstrInfo.reset(MipsInstrInfo::create(*this));
100 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
101 TLInfo.reset(MipsTargetLowering::create(*this));
102 } else {
103 InstrInfoSE.swap(InstrInfo);
104 FrameLoweringSE.swap(FrameLowering);
105 TLInfoSE.swap(TLInfo);
106 }
107 assert(TLInfo && "null target lowering in SE");
108 assert(InstrInfo && "null instr info SE");
109 assert(FrameLowering && "null frame lowering SE");
110}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000111void MipsebTargetMachine::anchor() { }
112
Akira Hatanaka24648102011-09-21 03:00:58 +0000113MipsebTargetMachine::
114MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000115 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +0000116 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000117 CodeGenOpt::Level OL)
118 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka24648102011-09-21 03:00:58 +0000119
David Blaikie2d24e2a2011-12-20 02:50:00 +0000120void MipselTargetMachine::anchor() { }
121
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000122MipselTargetMachine::
Evan Cheng43966132011-07-19 06:37:02 +0000123MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000124 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +0000125 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000126 CodeGenOpt::Level OL)
127 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000128
Andrew Trick843ee2e2012-02-03 05:12:41 +0000129namespace {
130/// Mips Code Generator Pass Configuration Options.
131class MipsPassConfig : public TargetPassConfig {
132public:
Andrew Trick061efcf2012-02-04 02:56:59 +0000133 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
134 : TargetPassConfig(TM, PM) {}
Andrew Trick843ee2e2012-02-03 05:12:41 +0000135
136 MipsTargetMachine &getMipsTargetMachine() const {
137 return getTM<MipsTargetMachine>();
138 }
139
140 const MipsSubtarget &getMipsSubtarget() const {
141 return *getMipsTargetMachine().getSubtargetImpl();
142 }
143
144 virtual bool addInstSelector();
Andrew Trick843ee2e2012-02-03 05:12:41 +0000145 virtual bool addPreEmitPass();
146};
147} // namespace
148
Andrew Trick061efcf2012-02-04 02:56:59 +0000149TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
150 return new MipsPassConfig(this, PM);
Andrew Trick843ee2e2012-02-03 05:12:41 +0000151}
152
Anton Korobeynikov33464912010-11-15 00:06:54 +0000153// Install an instruction selector pass using
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000154// the ISelDag to gen Mips code.
Bill Wendling7c4ce302012-05-01 08:27:43 +0000155bool MipsPassConfig::addInstSelector() {
Reed Kotlera430cb62013-04-09 19:46:01 +0000156 if (getMipsSubtarget().allowMixed16_32()) {
157 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
158 addPass(createMips16ISelDag(getMipsTargetMachine()));
159 addPass(createMipsSEISelDag(getMipsTargetMachine()));
160 } else {
161 addPass(createMipsISelDag(getMipsTargetMachine()));
162 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000163 return false;
164}
165
Reed Kotlera430cb62013-04-09 19:46:01 +0000166void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
167 if (Subtarget.allowMixed16_32()) {
168 DEBUG(errs() << "No ");
169 //FIXME: The Basic Target Transform Info
170 // pass needs to become a function pass instead of
171 // being an immutable pass and then this method as it exists now
172 // would be unnecessary.
173 PM.add(createNoTargetTransformInfoPass());
174 } else
175 LLVMTargetMachine::addAnalysisPasses(PM);
176 DEBUG(errs() << "Target Transform Info Pass Added\n");
177}
178
Anton Korobeynikov33464912010-11-15 00:06:54 +0000179// Implemented by targets that want to run passes immediately before
180// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000181// print out the code after the passes.
Bill Wendling7c4ce302012-05-01 08:27:43 +0000182bool MipsPassConfig::addPreEmitPass() {
Akira Hatanaka9e975872012-06-14 01:19:35 +0000183 MipsTargetMachine &TM = getMipsTargetMachine();
Reed Kotlera430cb62013-04-09 19:46:01 +0000184 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Bob Wilson564fbf62012-07-02 19:48:31 +0000185 addPass(createMipsDelaySlotFillerPass(TM));
Akira Hatanaka9e975872012-06-14 01:19:35 +0000186
Reed Kotlera430cb62013-04-09 19:46:01 +0000187 if (Subtarget.hasStandardEncoding() ||
188 Subtarget.allowMixed16_32())
Bob Wilson564fbf62012-07-02 19:48:31 +0000189 addPass(createMipsLongBranchPass(TM));
Reed Kotlera430cb62013-04-09 19:46:01 +0000190 if (Subtarget.inMips16Mode() ||
191 Subtarget.allowMixed16_32())
Reed Kotlerd056dc02013-02-27 03:33:58 +0000192 addPass(createMipsConstantIslandPass(TM));
Akira Hatanaka9e975872012-06-14 01:19:35 +0000193
Bruno Cardoso Lopesaff42dc2007-08-18 01:58:15 +0000194 return true;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000195}
Akira Hatanaka99a2e982011-04-15 19:52:08 +0000196
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000197bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengb95fc312011-11-16 08:38:26 +0000198 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000199 // Machine code emitter pass for Mips.
200 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
201 return false;
202}