blob: bb1870ebe6059567c93d188733a946b8d5d419d6 [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
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +000048// On function prologue, the stack is created by decrementing
49// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000050// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000051// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000052// Using CodeModel::Large enables different CALL behavior.
Eric Christopher4407dde2014-07-02 00:54:07 +000053MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
54 StringRef CPU, StringRef FS,
55 const TargetOptions &Options,
56 Reloc::Model RM, CodeModel::Model CM,
57 CodeGenOpt::Level OL, bool isLittle)
58 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Eric Christopher4e7d1e72014-07-18 23:41:32 +000059 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, this),
60 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
61 isLittle, this),
62 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
63 isLittle, this) {
64 Subtarget = &DefaultSubtarget;
Rafael Espindola227144c2013-05-13 01:16:13 +000065 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +000066}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000067
David Blaikiea379b1812011-12-20 02:50:00 +000068void MipsebTargetMachine::anchor() { }
69
Akira Hatanaka3d673cc2011-09-21 03:00:58 +000070MipsebTargetMachine::
71MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000072 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000073 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000074 CodeGenOpt::Level OL)
75 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +000076
David Blaikiea379b1812011-12-20 02:50:00 +000077void MipselTargetMachine::anchor() { }
78
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +000079MipselTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +000080MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000081 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000082 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000083 CodeGenOpt::Level OL)
84 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +000085
Eric Christopher4e7d1e72014-07-18 23:41:32 +000086void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
87 DEBUG(dbgs() << "resetSubtarget\n");
88 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
89 bool Mips16Attr = FnAttrs.hasAttribute(AttributeSet::FunctionIndex, "mips16");
90 bool NoMips16Attr =
91 FnAttrs.hasAttribute(AttributeSet::FunctionIndex, "nomips16");
92 assert(!(Mips16Attr && NoMips16Attr) &&
93 "mips16 and nomips16 specified on the same function");
94 if (Mips16Attr)
95 Subtarget = &Mips16Subtarget;
96 else if (NoMips16Attr)
97 Subtarget = &NoMips16Subtarget;
98 else
99 Subtarget = &DefaultSubtarget;
100 return;
101}
102
Andrew Trickccb67362012-02-03 05:12:41 +0000103namespace {
104/// Mips Code Generator Pass Configuration Options.
105class MipsPassConfig : public TargetPassConfig {
106public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000107 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000108 : TargetPassConfig(TM, PM) {
109 // The current implementation of long branch pass requires a scratch
110 // register ($at) to be available before branch instructions. Tail merging
111 // can break this requirement, so disable it when long branch pass is
112 // enabled.
113 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
114 }
Andrew Trickccb67362012-02-03 05:12:41 +0000115
116 MipsTargetMachine &getMipsTargetMachine() const {
117 return getTM<MipsTargetMachine>();
118 }
119
120 const MipsSubtarget &getMipsSubtarget() const {
121 return *getMipsTargetMachine().getSubtargetImpl();
122 }
123
Craig Topper56c590a2014-04-29 07:58:02 +0000124 void addIRPasses() override;
125 bool addInstSelector() override;
126 void addMachineSSAOptimization() override;
127 bool addPreEmitPass() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000128
Craig Topper56c590a2014-04-29 07:58:02 +0000129 bool addPreRegAlloc() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000130
Andrew Trickccb67362012-02-03 05:12:41 +0000131};
132} // namespace
133
Andrew Trickf8ea1082012-02-04 02:56:59 +0000134TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
135 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000136}
137
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000138void MipsPassConfig::addIRPasses() {
139 TargetPassConfig::addIRPasses();
140 if (getMipsSubtarget().os16())
141 addPass(createMipsOs16(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000142 if (getMipsSubtarget().inMips16HardFloat())
143 addPass(createMips16HardFloat(getMipsTargetMachine()));
Richard Sandiford37cd6cf2013-08-23 10:27:02 +0000144 addPass(createPartiallyInlineLibCallsPass());
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000145}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000146// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000147// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000148bool MipsPassConfig::addInstSelector() {
Eric Christophera08db01b2014-07-18 20:29:02 +0000149 addPass(createMipsModuleISelDag(getMipsTargetMachine()));
150 addPass(createMips16ISelDag(getMipsTargetMachine()));
151 addPass(createMipsSEISelDag(getMipsTargetMachine()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000152 return false;
153}
154
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000155void MipsPassConfig::addMachineSSAOptimization() {
156 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
157 TargetPassConfig::addMachineSSAOptimization();
158}
159
Reed Kotler96b74022014-03-10 16:31:25 +0000160bool MipsPassConfig::addPreRegAlloc() {
161 if (getOptLevel() == CodeGenOpt::None) {
162 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
163 return true;
164 }
165 else
166 return false;
167}
168
Reed Kotler1595f362013-04-09 19:46:01 +0000169void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000170 if (Subtarget->allowMixed16_32()) {
Reed Kotler1595f362013-04-09 19:46:01 +0000171 DEBUG(errs() << "No ");
172 //FIXME: The Basic Target Transform Info
173 // pass needs to become a function pass instead of
174 // being an immutable pass and then this method as it exists now
175 // would be unnecessary.
176 PM.add(createNoTargetTransformInfoPass());
177 } else
178 LLVMTargetMachine::addAnalysisPasses(PM);
179 DEBUG(errs() << "Target Transform Info Pass Added\n");
180}
181
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000182// Implemented by targets that want to run passes immediately before
183// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000184// print out the code after the passes.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000185bool MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000186 MipsTargetMachine &TM = getMipsTargetMachine();
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000187 addPass(createMipsDelaySlotFillerPass(TM));
Eric Christophera08db01b2014-07-18 20:29:02 +0000188 addPass(createMipsLongBranchPass(TM));
189 addPass(createMipsConstantIslandPass(TM));
Bruno Cardoso Lopesa7465122007-08-18 01:58:15 +0000190 return true;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000191}
Akira Hatanaka27916972011-04-15 19:52:08 +0000192
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000193bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengecb29082011-11-16 08:38:26 +0000194 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesd1d9c782011-07-21 16:28:51 +0000195 // Machine code emitter pass for Mips.
196 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
197 return false;
198}