blob: 91c9d691081921a3b68b6d1db3016e1d1a994886 [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
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 Espindola8afbb282013-12-16 17:15:29 +000057 // Pointers are 32 bit on some ABIs.
58 if (!ST.isABI_N64())
Rafael Espindolabccb9d42013-12-16 18:01:51 +000059 Ret += "-p:32:32";
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000060
61 // 8 and 16 bit integers only need no have natural alignment, but try to
62 // align them to 32 bits. 64 bit integers have natural alignment.
Rafael Espindolabccb9d42013-12-16 18:01:51 +000063 Ret += "-i8:8:32-i16:16:32-i64:64";
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000064
65 // 32 bit registers are always available and the stack is at least 64 bit
66 // aligned. On N64 64 bit registers are also available and the stack is
67 // 128 bit aligned.
Rafael Espindolafebb8d22013-12-17 23:15:58 +000068 if (ST.isABI_N64() || ST.isABI_N32())
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000069 Ret += "-n32:64-S128";
70 else
71 Ret += "-n32-S64";
72
73 return Ret;
74}
75
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +000076// On function prologue, the stack is created by decrementing
77// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000078// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000079// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000080// Using CodeModel::Large enables different CALL behavior.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000081MipsTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +000082MipsTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000083 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000084 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +000085 CodeGenOpt::Level OL,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000086 bool isLittle)
87 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Reed Kotler1595f362013-04-09 19:46:01 +000088 Subtarget(TT, CPU, FS, isLittle, RM, this),
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000089 DL(computeDataLayout(Subtarget)),
Akira Hatanakafab89292012-08-02 18:21:47 +000090 InstrInfo(MipsInstrInfo::create(*this)),
91 FrameLowering(MipsFrameLowering::create(*this, Subtarget)),
Akira Hatanaka66bc4192013-07-12 23:33:22 +000092 TLInfo(MipsTargetLowering::create(*this)), TSInfo(*this),
93 InstrItins(Subtarget.getInstrItineraryData()), JITInfo() {
Rafael Espindola227144c2013-05-13 01:16:13 +000094 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +000095}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000096
Reed Kotler1595f362013-04-09 19:46:01 +000097
98void MipsTargetMachine::setHelperClassesMips16() {
99 InstrInfoSE.swap(InstrInfo);
100 FrameLoweringSE.swap(FrameLowering);
101 TLInfoSE.swap(TLInfo);
102 if (!InstrInfo16) {
103 InstrInfo.reset(MipsInstrInfo::create(*this));
104 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
105 TLInfo.reset(MipsTargetLowering::create(*this));
106 } else {
107 InstrInfo16.swap(InstrInfo);
108 FrameLowering16.swap(FrameLowering);
109 TLInfo16.swap(TLInfo);
110 }
111 assert(TLInfo && "null target lowering 16");
112 assert(InstrInfo && "null instr info 16");
113 assert(FrameLowering && "null frame lowering 16");
114}
115
116void MipsTargetMachine::setHelperClassesMipsSE() {
117 InstrInfo16.swap(InstrInfo);
118 FrameLowering16.swap(FrameLowering);
119 TLInfo16.swap(TLInfo);
120 if (!InstrInfoSE) {
121 InstrInfo.reset(MipsInstrInfo::create(*this));
122 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
123 TLInfo.reset(MipsTargetLowering::create(*this));
124 } else {
125 InstrInfoSE.swap(InstrInfo);
126 FrameLoweringSE.swap(FrameLowering);
127 TLInfoSE.swap(TLInfo);
128 }
129 assert(TLInfo && "null target lowering in SE");
130 assert(InstrInfo && "null instr info SE");
131 assert(FrameLowering && "null frame lowering SE");
132}
David Blaikiea379b1812011-12-20 02:50:00 +0000133void MipsebTargetMachine::anchor() { }
134
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000135MipsebTargetMachine::
136MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000137 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000138 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000139 CodeGenOpt::Level OL)
140 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000141
David Blaikiea379b1812011-12-20 02:50:00 +0000142void MipselTargetMachine::anchor() { }
143
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000144MipselTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +0000145MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000146 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000147 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000148 CodeGenOpt::Level OL)
149 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000150
Andrew Trickccb67362012-02-03 05:12:41 +0000151namespace {
152/// Mips Code Generator Pass Configuration Options.
153class MipsPassConfig : public TargetPassConfig {
154public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000155 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000156 : TargetPassConfig(TM, PM) {
157 // The current implementation of long branch pass requires a scratch
158 // register ($at) to be available before branch instructions. Tail merging
159 // can break this requirement, so disable it when long branch pass is
160 // enabled.
161 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
162 }
Andrew Trickccb67362012-02-03 05:12:41 +0000163
164 MipsTargetMachine &getMipsTargetMachine() const {
165 return getTM<MipsTargetMachine>();
166 }
167
168 const MipsSubtarget &getMipsSubtarget() const {
169 return *getMipsTargetMachine().getSubtargetImpl();
170 }
171
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000172 virtual void addIRPasses();
Andrew Trickccb67362012-02-03 05:12:41 +0000173 virtual bool addInstSelector();
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000174 virtual void addMachineSSAOptimization();
Andrew Trickccb67362012-02-03 05:12:41 +0000175 virtual bool addPreEmitPass();
176};
177} // namespace
178
Andrew Trickf8ea1082012-02-04 02:56:59 +0000179TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
180 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000181}
182
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000183void MipsPassConfig::addIRPasses() {
184 TargetPassConfig::addIRPasses();
185 if (getMipsSubtarget().os16())
186 addPass(createMipsOs16(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000187 if (getMipsSubtarget().inMips16HardFloat())
188 addPass(createMips16HardFloat(getMipsTargetMachine()));
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000189 addPass(createPartiallyInlineLibCallsPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000190}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000191// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000192// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000193bool MipsPassConfig::addInstSelector() {
Reed Kotler1595f362013-04-09 19:46:01 +0000194 if (getMipsSubtarget().allowMixed16_32()) {
195 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
196 addPass(createMips16ISelDag(getMipsTargetMachine()));
197 addPass(createMipsSEISelDag(getMipsTargetMachine()));
198 } else {
199 addPass(createMipsISelDag(getMipsTargetMachine()));
200 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000201 return false;
202}
203
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000204void MipsPassConfig::addMachineSSAOptimization() {
205 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
206 TargetPassConfig::addMachineSSAOptimization();
207}
208
Reed Kotler1595f362013-04-09 19:46:01 +0000209void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
210 if (Subtarget.allowMixed16_32()) {
211 DEBUG(errs() << "No ");
212 //FIXME: The Basic Target Transform Info
213 // pass needs to become a function pass instead of
214 // being an immutable pass and then this method as it exists now
215 // would be unnecessary.
216 PM.add(createNoTargetTransformInfoPass());
217 } else
218 LLVMTargetMachine::addAnalysisPasses(PM);
219 DEBUG(errs() << "Target Transform Info Pass Added\n");
220}
221
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000222// Implemented by targets that want to run passes immediately before
223// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000224// print out the code after the passes.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000225bool MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000226 MipsTargetMachine &TM = getMipsTargetMachine();
Reed Kotler1595f362013-04-09 19:46:01 +0000227 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000228 addPass(createMipsDelaySlotFillerPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000229
Akira Hatanakaa8a05be2013-10-07 19:06:57 +0000230 if (Subtarget.enableLongBranchPass())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000231 addPass(createMipsLongBranchPass(TM));
Reed Kotler1595f362013-04-09 19:46:01 +0000232 if (Subtarget.inMips16Mode() ||
233 Subtarget.allowMixed16_32())
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000234 addPass(createMipsConstantIslandPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000235
Bruno Cardoso Lopesa7465122007-08-18 01:58:15 +0000236 return true;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000237}
Akira Hatanaka27916972011-04-15 19:52:08 +0000238
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000239bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengecb29082011-11-16 08:38:26 +0000240 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000241 // Machine code emitter pass for Mips.
242 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
243 return false;
244}