blob: 79f6617fcabf651ca51564acf9bdfe296d502645 [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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "Mips16ISelDAGToDAG.h"
18#include "Mips16ISelLowering.h"
19#include "Mips16InstrInfo.h"
Akira Hatanakafab89292012-08-02 18:21:47 +000020#include "MipsFrameLowering.h"
21#include "MipsInstrInfo.h"
Reed Kotler1595f362013-04-09 19:46:01 +000022#include "MipsSEFrameLowering.h"
Reed Kotler1595f362013-04-09 19:46:01 +000023#include "MipsSEISelDAGToDAG.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "MipsSEISelLowering.h"
25#include "MipsSEInstrInfo.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000026#include "MipsTargetObjectFile.h"
Reed Kotler1595f362013-04-09 19:46:01 +000027#include "llvm/Analysis/TargetTransformInfo.h"
Andrew Trickccb67362012-02-03 05:12:41 +000028#include "llvm/CodeGen/Passes.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Reed Kotler1595f362013-04-09 19:46:01 +000030#include "llvm/Support/Debug.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000031#include "llvm/Support/TargetRegistry.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000032#include "llvm/Support/raw_ostream.h"
Richard Sandiford37cd6cf2013-08-23 10:27:02 +000033#include "llvm/Transforms/Scalar.h"
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000034
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000035using namespace llvm;
36
Chandler Carruthe96dd892014-04-21 22:55:11 +000037#define DEBUG_TYPE "mips"
38
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000039extern "C" void LLVMInitializeMipsTarget() {
40 // Register the target.
Akira Hatanaka3d673cc2011-09-21 03:00:58 +000041 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
Eli Friedman57c11da2009-08-03 02:22:28 +000042 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
Akira Hatanaka30651802012-07-31 21:39:17 +000043 RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
44 RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000045}
46
Mehdi Amini93e1ea12015-03-12 00:07:24 +000047static std::string computeDataLayout(StringRef TT, StringRef CPU,
48 const TargetOptions &Options,
49 bool isLittle) {
Eric Christopher8b770652015-01-26 19:03:15 +000050 std::string Ret = "";
Mehdi Amini93e1ea12015-03-12 00:07:24 +000051 MipsABIInfo ABI =
52 MipsABIInfo::computeTargetABI(Triple(TT), CPU, Options.MCOptions);
Eric Christopher8b770652015-01-26 19:03:15 +000053
54 // There are both little and big endian mips.
55 if (isLittle)
56 Ret += "e";
57 else
58 Ret += "E";
59
60 Ret += "-m:m";
61
62 // Pointers are 32 bit on some ABIs.
63 if (!ABI.IsN64())
64 Ret += "-p:32:32";
65
66 // 8 and 16 bit integers only need no have natural alignment, but try to
67 // align them to 32 bits. 64 bit integers have natural alignment.
68 Ret += "-i8:8:32-i16:16:32-i64:64";
69
70 // 32 bit registers are always available and the stack is at least 64 bit
71 // aligned. On N64 64 bit registers are also available and the stack is
72 // 128 bit aligned.
73 if (ABI.IsN64() || ABI.IsN32())
74 Ret += "-n32:64-S128";
75 else
76 Ret += "-n32-S64";
77
78 return Ret;
79}
80
Bruno Cardoso Lopes43318832007-08-28 05:13:42 +000081// On function prologue, the stack is created by decrementing
82// its pointer. Once decremented, all references are done with positive
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000083// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +000084// an easier handling.
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +000085// Using CodeModel::Large enables different CALL behavior.
Eric Christopher4407dde2014-07-02 00:54:07 +000086MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
87 StringRef CPU, StringRef FS,
88 const TargetOptions &Options,
89 Reloc::Model RM, CodeModel::Model CM,
90 CodeGenOpt::Level OL, bool isLittle)
Mehdi Amini93e1ea12015-03-12 00:07:24 +000091 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
92 CPU, FS, Options, RM, CM, OL),
Eric Christophera5762812015-01-26 17:33:46 +000093 isLittle(isLittle), TLOF(make_unique<MipsTargetObjectFile>()),
94 ABI(MipsABIInfo::computeTargetABI(Triple(TT), CPU, Options.MCOptions)),
Mehdi Amini93e1ea12015-03-12 00:07:24 +000095 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this),
Eric Christopher4e7d1e72014-07-18 23:41:32 +000096 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
Eric Christopher90724282015-01-08 18:18:57 +000097 isLittle, *this),
Eric Christopher4e7d1e72014-07-18 23:41:32 +000098 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
Eric Christopher90724282015-01-08 18:18:57 +000099 isLittle, *this) {
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000100 Subtarget = &DefaultSubtarget;
Rafael Espindola227144c2013-05-13 01:16:13 +0000101 initAsmInfo();
Bruno Cardoso Lopes35d86e62007-10-09 03:01:19 +0000102}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000103
Reid Kleckner357600e2014-11-20 23:37:18 +0000104MipsTargetMachine::~MipsTargetMachine() {}
105
David Blaikiea379b1812011-12-20 02:50:00 +0000106void MipsebTargetMachine::anchor() { }
107
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000108MipsebTargetMachine::
109MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000110 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +0000111 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000112 CodeGenOpt::Level OL)
113 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000114
David Blaikiea379b1812011-12-20 02:50:00 +0000115void MipselTargetMachine::anchor() { }
116
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000117MipselTargetMachine::
Evan Cheng2129f592011-07-19 06:37:02 +0000118MipselTargetMachine(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, true) {}
Bruno Cardoso Lopes326a0372008-06-04 01:45:25 +0000123
Eric Christophera9353d12014-09-26 01:44:08 +0000124const MipsSubtarget *
David Majnemerde360752014-09-26 02:57:05 +0000125MipsTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000126 Attribute CPUAttr = F.getFnAttribute("target-cpu");
127 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christophera9353d12014-09-26 01:44:08 +0000128
129 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
130 ? CPUAttr.getValueAsString().str()
131 : TargetCPU;
132 std::string FS = !FSAttr.hasAttribute(Attribute::None)
133 ? FSAttr.getValueAsString().str()
134 : TargetFS;
135 bool hasMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000136 !F.getFnAttribute("mips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000137 bool hasNoMips16Attr =
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000138 !F.getFnAttribute("nomips16").hasAttribute(Attribute::None);
Eric Christophera9353d12014-09-26 01:44:08 +0000139
Eric Christopher6a0551e2014-09-29 21:57:54 +0000140 // FIXME: This is related to the code below to reset the target options,
141 // we need to know whether or not the soft float flag is set on the
142 // function before we can generate a subtarget. We also need to use
143 // it as a key for the subtarget since that can be the only difference
144 // between two functions.
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000145 Attribute SFAttr = F.getFnAttribute("use-soft-float");
Eric Christopher6a0551e2014-09-29 21:57:54 +0000146 bool softFloat = !SFAttr.hasAttribute(Attribute::None)
Eric Christophera2db9222014-09-29 23:31:13 +0000147 ? SFAttr.getValueAsString() == "true"
Eric Christopher6a0551e2014-09-29 21:57:54 +0000148 : Options.UseSoftFloat;
149
Eric Christophera9353d12014-09-26 01:44:08 +0000150 if (hasMips16Attr)
151 FS += FS.empty() ? "+mips16" : ",+mips16";
152 else if (hasNoMips16Attr)
153 FS += FS.empty() ? "-mips16" : ",-mips16";
154
Eric Christopher6a0551e2014-09-29 21:57:54 +0000155 auto &I = SubtargetMap[CPU + FS + (softFloat ? "use-soft-float=true"
156 : "use-soft-float=false")];
Eric Christophera9353d12014-09-26 01:44:08 +0000157 if (!I) {
158 // This needs to be done before we create a new subtarget since any
159 // creation will depend on the TM and the code generation flags on the
160 // function that reside in TargetOptions.
161 resetTargetOptions(F);
Eric Christopher90724282015-01-08 18:18:57 +0000162 I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, *this);
Eric Christophera9353d12014-09-26 01:44:08 +0000163 }
164 return I.get();
165}
166
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000167void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
168 DEBUG(dbgs() << "resetSubtarget\n");
Eric Christophera9353d12014-09-26 01:44:08 +0000169
David Majnemerde360752014-09-26 02:57:05 +0000170 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
Eric Christopherfc6de422014-08-05 02:39:49 +0000171 MF->setSubtarget(Subtarget);
Eric Christopher4e7d1e72014-07-18 23:41:32 +0000172 return;
173}
174
Andrew Trickccb67362012-02-03 05:12:41 +0000175namespace {
176/// Mips Code Generator Pass Configuration Options.
177class MipsPassConfig : public TargetPassConfig {
178public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000179 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
Akira Hatanaka3c0d6af2013-10-07 19:13:53 +0000180 : TargetPassConfig(TM, PM) {
181 // The current implementation of long branch pass requires a scratch
182 // register ($at) to be available before branch instructions. Tail merging
183 // can break this requirement, so disable it when long branch pass is
184 // enabled.
185 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
186 }
Andrew Trickccb67362012-02-03 05:12:41 +0000187
188 MipsTargetMachine &getMipsTargetMachine() const {
189 return getTM<MipsTargetMachine>();
190 }
191
192 const MipsSubtarget &getMipsSubtarget() const {
193 return *getMipsTargetMachine().getSubtargetImpl();
194 }
195
Craig Topper56c590a2014-04-29 07:58:02 +0000196 void addIRPasses() override;
197 bool addInstSelector() override;
198 void addMachineSSAOptimization() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000199 void addPreEmitPass() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000200
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000201 void addPreRegAlloc() override;
Reed Kotler96b74022014-03-10 16:31:25 +0000202
Andrew Trickccb67362012-02-03 05:12:41 +0000203};
204} // namespace
205
Andrew Trickf8ea1082012-02-04 02:56:59 +0000206TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
207 return new MipsPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000208}
209
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000210void MipsPassConfig::addIRPasses() {
211 TargetPassConfig::addIRPasses();
Robin Morissete2de06b2014-10-16 20:34:57 +0000212 addPass(createAtomicExpandPass(&getMipsTargetMachine()));
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000213 if (getMipsSubtarget().os16())
Vasileios Kalintiris6312f512015-03-14 08:34:25 +0000214 addPass(createMipsOs16Pass(getMipsTargetMachine()));
Reed Kotler783c7942013-05-10 22:25:39 +0000215 if (getMipsSubtarget().inMips16HardFloat())
Vasileios Kalintiris6611eb32015-03-14 09:02:23 +0000216 addPass(createMips16HardFloatPass(getMipsTargetMachine()));
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000217}
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000218// Install an instruction selector pass using
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000219// the ISelDag to gen Mips code.
Bill Wendlingb12f16e2012-05-01 08:27:43 +0000220bool MipsPassConfig::addInstSelector() {
Vasileios Kalintiris46fa9b72015-03-14 09:20:52 +0000221 addPass(createMipsModuleISelDagPass(getMipsTargetMachine()));
Eric Christophera08db01b2014-07-18 20:29:02 +0000222 addPass(createMips16ISelDag(getMipsTargetMachine()));
223 addPass(createMipsSEISelDag(getMipsTargetMachine()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000224 return false;
225}
226
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000227void MipsPassConfig::addMachineSSAOptimization() {
228 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
229 TargetPassConfig::addMachineSSAOptimization();
230}
231
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000232void MipsPassConfig::addPreRegAlloc() {
233 if (getOptLevel() == CodeGenOpt::None)
Reed Kotler96b74022014-03-10 16:31:25 +0000234 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
Reed Kotler96b74022014-03-10 16:31:25 +0000235}
236
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000237TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() {
238 return TargetIRAnalysis([this](Function &F) {
239 if (Subtarget->allowMixed16_32()) {
240 DEBUG(errs() << "No Target Transform Info Pass Added\n");
241 // FIXME: This is no longer necessary as the TTI returned is per-function.
242 return TargetTransformInfo(getDataLayout());
243 }
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000244
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000245 DEBUG(errs() << "Target Transform Info Pass Added\n");
Chandler Carruthc956ab662015-02-01 14:22:17 +0000246 return TargetTransformInfo(BasicTTIImpl(this, F));
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000247 });
Reed Kotler1595f362013-04-09 19:46:01 +0000248}
249
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000250// Implemented by targets that want to run passes immediately before
251// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000252// print out the code after the passes.
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000253void MipsPassConfig::addPreEmitPass() {
Akira Hatanakaeb365222012-06-14 01:19:35 +0000254 MipsTargetMachine &TM = getMipsTargetMachine();
Matthias Braunb2f23882014-12-11 23:18:03 +0000255 addPass(createMipsDelaySlotFillerPass(TM));
256 addPass(createMipsLongBranchPass(TM));
Eric Christophera08db01b2014-07-18 20:29:02 +0000257 addPass(createMipsConstantIslandPass(TM));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000258}