blob: d16632453bc9d6fe1e1fd91c30f40cad1a08d0ed [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// Implements the info about Mips target spec.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#include "MipsTargetMachine.h"
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000015#include "MipsSEInstrInfo.h"
16#include "Mips16InstrInfo.h"
Craig Topper79aa3412012-03-17 18:46:09 +000017#include "Mips.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "llvm/PassManager.h"
Andrew Trick843ee2e2012-02-03 05:12:41 +000019#include "llvm/CodeGen/Passes.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000020#include "llvm/Support/TargetRegistry.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021using namespace llvm;
22
Daniel Dunbar0c795d62009-07-25 06:49:55 +000023extern "C" void LLVMInitializeMipsTarget() {
24 // Register the target.
Akira Hatanaka24648102011-09-21 03:00:58 +000025 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
Eli Friedmane2c74082009-08-03 02:22:28 +000026 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
Akira Hatanakab4f921b2012-07-31 21:39:17 +000027 RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
28 RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029}
30
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000031static const MipsInstrInfo *genInstrInfo(MipsTargetMachine &TM) {
32 const MipsInstrInfo *II;
33
34 if (TM.getSubtargetImpl()->inMips16Mode())
35 II = new Mips16InstrInfo(TM);
36 else
37 II = new MipsSEInstrInfo(TM);
38
39 return II;
40}
41
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042// DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
Bruno Cardoso Lopes51195af2007-08-28 05:13:42 +000043// The stack is always 8 byte aligned
44// On function prologue, the stack is created by decrementing
45// its pointer. Once decremented, all references are done with positive
Anton Korobeynikov33464912010-11-15 00:06:54 +000046// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000047// an easier handling.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000048// Using CodeModel::Large enables different CALL behavior.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000049MipsTargetMachine::
Evan Cheng43966132011-07-19 06:37:02 +000050MipsTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000051 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Cheng34ad6db2011-07-20 07:51:56 +000052 Reloc::Model RM, CodeModel::Model CM,
Evan Chengb95fc312011-11-16 08:38:26 +000053 CodeGenOpt::Level OL,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000054 bool isLittle)
55 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
56 Subtarget(TT, CPU, FS, isLittle),
57 DataLayout(isLittle ?
58 (Subtarget.isABI_N64() ?
59 "e-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-n32" :
60 "e-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32") :
61 (Subtarget.isABI_N64() ?
62 "E-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-n32" :
63 "E-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32")),
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000064 InstrInfo(genInstrInfo(*this)),
Nick Lewycky8a8d4792011-12-02 22:16:29 +000065 FrameLowering(Subtarget),
66 TLInfo(*this), TSInfo(*this), JITInfo() {
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000067}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068
David Blaikie2d24e2a2011-12-20 02:50:00 +000069void MipsebTargetMachine::anchor() { }
70
Akira Hatanaka24648102011-09-21 03:00:58 +000071MipsebTargetMachine::
72MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000073 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +000074 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000075 CodeGenOpt::Level OL)
76 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka24648102011-09-21 03:00:58 +000077
David Blaikie2d24e2a2011-12-20 02:50:00 +000078void MipselTargetMachine::anchor() { }
79
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000080MipselTargetMachine::
Evan Cheng43966132011-07-19 06:37:02 +000081MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000082 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +000083 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000084 CodeGenOpt::Level OL)
85 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000086
Andrew Trick843ee2e2012-02-03 05:12:41 +000087namespace {
88/// Mips Code Generator Pass Configuration Options.
89class MipsPassConfig : public TargetPassConfig {
90public:
Andrew Trick061efcf2012-02-04 02:56:59 +000091 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
92 : TargetPassConfig(TM, PM) {}
Andrew Trick843ee2e2012-02-03 05:12:41 +000093
94 MipsTargetMachine &getMipsTargetMachine() const {
95 return getTM<MipsTargetMachine>();
96 }
97
98 const MipsSubtarget &getMipsSubtarget() const {
99 return *getMipsTargetMachine().getSubtargetImpl();
100 }
101
102 virtual bool addInstSelector();
Andrew Trick843ee2e2012-02-03 05:12:41 +0000103 virtual bool addPreEmitPass();
104};
105} // namespace
106
Andrew Trick061efcf2012-02-04 02:56:59 +0000107TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
108 return new MipsPassConfig(this, PM);
Andrew Trick843ee2e2012-02-03 05:12:41 +0000109}
110
Anton Korobeynikov33464912010-11-15 00:06:54 +0000111// Install an instruction selector pass using
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000112// the ISelDag to gen Mips code.
Bill Wendling7c4ce302012-05-01 08:27:43 +0000113bool MipsPassConfig::addInstSelector() {
Bob Wilson564fbf62012-07-02 19:48:31 +0000114 addPass(createMipsISelDag(getMipsTargetMachine()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000115 return false;
116}
117
Anton Korobeynikov33464912010-11-15 00:06:54 +0000118// Implemented by targets that want to run passes immediately before
119// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000120// print out the code after the passes.
Bill Wendling7c4ce302012-05-01 08:27:43 +0000121bool MipsPassConfig::addPreEmitPass() {
Akira Hatanaka9e975872012-06-14 01:19:35 +0000122 MipsTargetMachine &TM = getMipsTargetMachine();
Bob Wilson564fbf62012-07-02 19:48:31 +0000123 addPass(createMipsDelaySlotFillerPass(TM));
Akira Hatanaka9e975872012-06-14 01:19:35 +0000124
125 // NOTE: long branch has not been implemented for mips16.
126 if (TM.getSubtarget<MipsSubtarget>().hasStandardEncoding())
Bob Wilson564fbf62012-07-02 19:48:31 +0000127 addPass(createMipsLongBranchPass(TM));
Akira Hatanaka9e975872012-06-14 01:19:35 +0000128
Bruno Cardoso Lopesaff42dc2007-08-18 01:58:15 +0000129 return true;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000130}
Akira Hatanaka99a2e982011-04-15 19:52:08 +0000131
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000132bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengb95fc312011-11-16 08:38:26 +0000133 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000134 // Machine code emitter pass for Mips.
135 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
136 return false;
137}