blob: 5046c1b782f66d11abf843e36110f10395ed59e2 [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"
Akira Hatanakafab89292012-08-02 18:21:47 +000016#include "MipsFrameLowering.h"
17#include "MipsInstrInfo.h"
Reed Kotler1595f362013-04-09 19:46:01 +000018#include "MipsModuleISelDAGToDAG.h"
Reed Kotlerfe94cc32013-04-10 16:58:04 +000019#include "MipsOs16.h"
Reed Kotler1595f362013-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 Kotler783c7942013-05-10 22:25:39 +000025#include "Mips16HardFloat.h"
Reed Kotler1595f362013-04-09 19:46:01 +000026#include "Mips16InstrInfo.h"
27#include "Mips16ISelDAGToDAG.h"
28#include "Mips16ISelLowering.h"
29#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"
33#include "llvm/Support/raw_ostream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.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
Reed Kotler1595f362013-04-09 19:46:01 +000038
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
48// DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
Bruno Cardoso Lopes43318832007-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 Korobeynikovf7183ed2010-11-15 00:06:54 +000052// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000053// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000054// Using CodeModel::Large enables different CALL behavior.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000055MipsTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +000056MipsTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000057 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000058 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +000059 CodeGenOpt::Level OL,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000060 bool isLittle)
61 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Reed Kotler1595f362013-04-09 19:46:01 +000062 Subtarget(TT, CPU, FS, isLittle, RM, this),
Micah Villmowcdfe20b2012-10-08 16:38:25 +000063 DL(isLittle ?
Nick Lewycky50f02cb2011-12-02 22:16:29 +000064 (Subtarget.isABI_N64() ?
Akira Hatanakad35a26302013-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 Lewycky50f02cb2011-12-02 22:16:29 +000068 (Subtarget.isABI_N64() ?
Akira Hatanakad35a26302013-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 Hatanakafab89292012-08-02 18:21:47 +000072 InstrInfo(MipsInstrInfo::create(*this)),
73 FrameLowering(MipsFrameLowering::create(*this, Subtarget)),
Akira Hatanaka66bc4192013-07-12 23:33:22 +000074 TLInfo(MipsTargetLowering::create(*this)), TSInfo(*this),
75 InstrItins(Subtarget.getInstrItineraryData()), JITInfo() {
Rafael Espindola227144c2013-05-13 01:16:13 +000076 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +000077}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000078
Reed Kotler1595f362013-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 Blaikiea379b1812011-12-20 02:50:00 +0000115void MipsebTargetMachine::anchor() { }
116
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000117MipsebTargetMachine::
118MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000119 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000120 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000121 CodeGenOpt::Level OL)
122 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000123
David Blaikiea379b1812011-12-20 02:50:00 +0000124void MipselTargetMachine::anchor() { }
125
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000126MipselTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +0000127MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000128 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000129 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000130 CodeGenOpt::Level OL)
131 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000132
Andrew Trickccb67362012-02-03 05:12:41 +0000133namespace {
134/// Mips Code Generator Pass Configuration Options.
135class MipsPassConfig : public TargetPassConfig {
136public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000137 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000138 : TargetPassConfig(TM, PM) {
139 // The current implementation of long branch pass requires a scratch
140 // register ($at) to be available before branch instructions. Tail merging
141 // can break this requirement, so disable it when long branch pass is
142 // enabled.
143 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
144 }
Andrew Trickccb67362012-02-03 05:12:41 +0000145
146 MipsTargetMachine &getMipsTargetMachine() const {
147 return getTM<MipsTargetMachine>();
148 }
149
150 const MipsSubtarget &getMipsSubtarget() const {
151 return *getMipsTargetMachine().getSubtargetImpl();
152 }
153
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000154 virtual void addIRPasses();
Andrew Trickccb67362012-02-03 05:12:41 +0000155 virtual bool addInstSelector();
Andrew Trickccb67362012-02-03 05:12:41 +0000156 virtual bool addPreEmitPass();
157};
158} // namespace
159
Andrew Trickf8ea1082012-02-04 02:56:59 +0000160TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
161 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000162}
163
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000164void MipsPassConfig::addIRPasses() {
165 TargetPassConfig::addIRPasses();
166 if (getMipsSubtarget().os16())
167 addPass(createMipsOs16(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000168 if (getMipsSubtarget().inMips16HardFloat())
169 addPass(createMips16HardFloat(getMipsTargetMachine()));
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000170 addPass(createPartiallyInlineLibCallsPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000171}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000172// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000173// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000174bool MipsPassConfig::addInstSelector() {
Reed Kotler1595f362013-04-09 19:46:01 +0000175 if (getMipsSubtarget().allowMixed16_32()) {
176 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
177 addPass(createMips16ISelDag(getMipsTargetMachine()));
178 addPass(createMipsSEISelDag(getMipsTargetMachine()));
179 } else {
180 addPass(createMipsISelDag(getMipsTargetMachine()));
181 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000182 return false;
183}
184
Reed Kotler1595f362013-04-09 19:46:01 +0000185void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
186 if (Subtarget.allowMixed16_32()) {
187 DEBUG(errs() << "No ");
188 //FIXME: The Basic Target Transform Info
189 // pass needs to become a function pass instead of
190 // being an immutable pass and then this method as it exists now
191 // would be unnecessary.
192 PM.add(createNoTargetTransformInfoPass());
193 } else
194 LLVMTargetMachine::addAnalysisPasses(PM);
195 DEBUG(errs() << "Target Transform Info Pass Added\n");
196}
197
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000198// Implemented by targets that want to run passes immediately before
199// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000200// print out the code after the passes.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000201bool MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000202 MipsTargetMachine &TM = getMipsTargetMachine();
Reed Kotler1595f362013-04-09 19:46:01 +0000203 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000204 addPass(createMipsDelaySlotFillerPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000205
Akira Hatanakaa8a05be2013-10-07 19:06:57 +0000206 if (Subtarget.enableLongBranchPass())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000207 addPass(createMipsLongBranchPass(TM));
Reed Kotler1595f362013-04-09 19:46:01 +0000208 if (Subtarget.inMips16Mode() ||
209 Subtarget.allowMixed16_32())
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000210 addPass(createMipsConstantIslandPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000211
Bruno Cardoso Lopesa7465122007-08-18 01:58:15 +0000212 return true;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000213}
Akira Hatanaka27916972011-04-15 19:52:08 +0000214
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000215bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengecb29082011-11-16 08:38:26 +0000216 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000217 // Machine code emitter pass for Mips.
218 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
219 return false;
220}