blob: 984c58eb6c1e3a138818bac9ed964ef32af43625 [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.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000083MipsTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +000084MipsTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000085 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000086 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +000087 CodeGenOpt::Level OL,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000088 bool isLittle)
89 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Reed Kotler1595f362013-04-09 19:46:01 +000090 Subtarget(TT, CPU, FS, isLittle, RM, this),
Rafael Espindolab2fb78d2013-12-11 01:41:10 +000091 DL(computeDataLayout(Subtarget)),
Akira Hatanakafab89292012-08-02 18:21:47 +000092 InstrInfo(MipsInstrInfo::create(*this)),
93 FrameLowering(MipsFrameLowering::create(*this, Subtarget)),
Akira Hatanaka66bc4192013-07-12 23:33:22 +000094 TLInfo(MipsTargetLowering::create(*this)), TSInfo(*this),
95 InstrItins(Subtarget.getInstrItineraryData()), JITInfo() {
Rafael Espindola227144c2013-05-13 01:16:13 +000096 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +000097}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000098
Reed Kotler1595f362013-04-09 19:46:01 +000099
100void MipsTargetMachine::setHelperClassesMips16() {
101 InstrInfoSE.swap(InstrInfo);
102 FrameLoweringSE.swap(FrameLowering);
103 TLInfoSE.swap(TLInfo);
104 if (!InstrInfo16) {
105 InstrInfo.reset(MipsInstrInfo::create(*this));
106 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
107 TLInfo.reset(MipsTargetLowering::create(*this));
108 } else {
109 InstrInfo16.swap(InstrInfo);
110 FrameLowering16.swap(FrameLowering);
111 TLInfo16.swap(TLInfo);
112 }
113 assert(TLInfo && "null target lowering 16");
114 assert(InstrInfo && "null instr info 16");
115 assert(FrameLowering && "null frame lowering 16");
116}
117
118void MipsTargetMachine::setHelperClassesMipsSE() {
119 InstrInfo16.swap(InstrInfo);
120 FrameLowering16.swap(FrameLowering);
121 TLInfo16.swap(TLInfo);
122 if (!InstrInfoSE) {
123 InstrInfo.reset(MipsInstrInfo::create(*this));
124 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
125 TLInfo.reset(MipsTargetLowering::create(*this));
126 } else {
127 InstrInfoSE.swap(InstrInfo);
128 FrameLoweringSE.swap(FrameLowering);
129 TLInfoSE.swap(TLInfo);
130 }
131 assert(TLInfo && "null target lowering in SE");
132 assert(InstrInfo && "null instr info SE");
133 assert(FrameLowering && "null frame lowering SE");
134}
David Blaikiea379b1812011-12-20 02:50:00 +0000135void MipsebTargetMachine::anchor() { }
136
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000137MipsebTargetMachine::
138MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000139 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000140 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000141 CodeGenOpt::Level OL)
142 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000143
David Blaikiea379b1812011-12-20 02:50:00 +0000144void MipselTargetMachine::anchor() { }
145
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000146MipselTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +0000147MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000148 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000149 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000150 CodeGenOpt::Level OL)
151 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000152
Andrew Trickccb67362012-02-03 05:12:41 +0000153namespace {
154/// Mips Code Generator Pass Configuration Options.
155class MipsPassConfig : public TargetPassConfig {
156public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000157 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000158 : TargetPassConfig(TM, PM) {
159 // The current implementation of long branch pass requires a scratch
160 // register ($at) to be available before branch instructions. Tail merging
161 // can break this requirement, so disable it when long branch pass is
162 // enabled.
163 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
164 }
Andrew Trickccb67362012-02-03 05:12:41 +0000165
166 MipsTargetMachine &getMipsTargetMachine() const {
167 return getTM<MipsTargetMachine>();
168 }
169
170 const MipsSubtarget &getMipsSubtarget() const {
171 return *getMipsTargetMachine().getSubtargetImpl();
172 }
173
Craig Topper56c590a2014-04-29 07:58:02 +0000174 void addIRPasses() override;
175 bool addInstSelector() override;
176 void addMachineSSAOptimization() override;
177 bool addPreEmitPass() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000178
Craig Topper56c590a2014-04-29 07:58:02 +0000179 bool addPreRegAlloc() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000180
Andrew Trickccb67362012-02-03 05:12:41 +0000181};
182} // namespace
183
Andrew Trickf8ea1082012-02-04 02:56:59 +0000184TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
185 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000186}
187
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000188void MipsPassConfig::addIRPasses() {
189 TargetPassConfig::addIRPasses();
190 if (getMipsSubtarget().os16())
191 addPass(createMipsOs16(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000192 if (getMipsSubtarget().inMips16HardFloat())
193 addPass(createMips16HardFloat(getMipsTargetMachine()));
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000194 addPass(createPartiallyInlineLibCallsPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000195}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000196// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000197// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000198bool MipsPassConfig::addInstSelector() {
Reed Kotler1595f362013-04-09 19:46:01 +0000199 if (getMipsSubtarget().allowMixed16_32()) {
200 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
201 addPass(createMips16ISelDag(getMipsTargetMachine()));
202 addPass(createMipsSEISelDag(getMipsTargetMachine()));
203 } else {
204 addPass(createMipsISelDag(getMipsTargetMachine()));
205 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000206 return false;
207}
208
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000209void MipsPassConfig::addMachineSSAOptimization() {
210 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
211 TargetPassConfig::addMachineSSAOptimization();
212}
213
Reed Kotler96b74022014-03-10 16:31:25 +0000214bool MipsPassConfig::addPreRegAlloc() {
215 if (getOptLevel() == CodeGenOpt::None) {
216 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
217 return true;
218 }
219 else
220 return false;
221}
222
Reed Kotler1595f362013-04-09 19:46:01 +0000223void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
224 if (Subtarget.allowMixed16_32()) {
225 DEBUG(errs() << "No ");
226 //FIXME: The Basic Target Transform Info
227 // pass needs to become a function pass instead of
228 // being an immutable pass and then this method as it exists now
229 // would be unnecessary.
230 PM.add(createNoTargetTransformInfoPass());
231 } else
232 LLVMTargetMachine::addAnalysisPasses(PM);
233 DEBUG(errs() << "Target Transform Info Pass Added\n");
234}
235
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000236// Implemented by targets that want to run passes immediately before
237// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000238// print out the code after the passes.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000239bool MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000240 MipsTargetMachine &TM = getMipsTargetMachine();
Reed Kotler1595f362013-04-09 19:46:01 +0000241 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000242 addPass(createMipsDelaySlotFillerPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000243
Akira Hatanakaa8a05be2013-10-07 19:06:57 +0000244 if (Subtarget.enableLongBranchPass())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000245 addPass(createMipsLongBranchPass(TM));
Reed Kotler1595f362013-04-09 19:46:01 +0000246 if (Subtarget.inMips16Mode() ||
247 Subtarget.allowMixed16_32())
Reed Kotlerbb3094a2013-02-27 03:33:58 +0000248 addPass(createMipsConstantIslandPass(TM));
Akira Hatanakaeb365222012-06-14 01:19:35 +0000249
Bruno Cardoso Lopesa7465122007-08-18 01:58:15 +0000250 return true;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000251}
Akira Hatanaka27916972011-04-15 19:52:08 +0000252
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000253bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengecb29082011-11-16 08:38:26 +0000254 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000255 // Machine code emitter pass for Mips.
256 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
257 return false;
258}