blob: 8806aafe21829b402feab7d274d0d9c1bb003629 [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
14#include "Mips.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsTargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "llvm/PassManager.h"
Andrew Trick843ee2e2012-02-03 05:12:41 +000017#include "llvm/CodeGen/Passes.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000018#include "llvm/Support/TargetRegistry.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019using namespace llvm;
20
Daniel Dunbar0c795d62009-07-25 06:49:55 +000021extern "C" void LLVMInitializeMipsTarget() {
22 // Register the target.
Akira Hatanaka24648102011-09-21 03:00:58 +000023 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
Eli Friedmane2c74082009-08-03 02:22:28 +000024 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
Akira Hatanaka24648102011-09-21 03:00:58 +000025 RegisterTargetMachine<Mips64ebTargetMachine> A(TheMips64Target);
26 RegisterTargetMachine<Mips64elTargetMachine> B(TheMips64elTarget);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027}
28
29// DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
Bruno Cardoso Lopes51195af2007-08-28 05:13:42 +000030// The stack is always 8 byte aligned
31// On function prologue, the stack is created by decrementing
32// its pointer. Once decremented, all references are done with positive
Anton Korobeynikov33464912010-11-15 00:06:54 +000033// offset from the stack/frame pointer, using StackGrowsUp enables
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000034// an easier handling.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000035// Using CodeModel::Large enables different CALL behavior.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036MipsTargetMachine::
Evan Cheng43966132011-07-19 06:37:02 +000037MipsTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000038 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Cheng34ad6db2011-07-20 07:51:56 +000039 Reloc::Model RM, CodeModel::Model CM,
Evan Chengb95fc312011-11-16 08:38:26 +000040 CodeGenOpt::Level OL,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000041 bool isLittle)
42 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
43 Subtarget(TT, CPU, FS, isLittle),
44 DataLayout(isLittle ?
45 (Subtarget.isABI_N64() ?
46 "e-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-n32" :
47 "e-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32") :
48 (Subtarget.isABI_N64() ?
49 "E-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-n32" :
50 "E-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32")),
51 InstrInfo(*this),
52 FrameLowering(Subtarget),
53 TLInfo(*this), TSInfo(*this), JITInfo() {
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000054}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000055
David Blaikie2d24e2a2011-12-20 02:50:00 +000056void MipsebTargetMachine::anchor() { }
57
Akira Hatanaka24648102011-09-21 03:00:58 +000058MipsebTargetMachine::
59MipsebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000060 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +000061 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000062 CodeGenOpt::Level OL)
63 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka24648102011-09-21 03:00:58 +000064
David Blaikie2d24e2a2011-12-20 02:50:00 +000065void MipselTargetMachine::anchor() { }
66
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000067MipselTargetMachine::
Evan Cheng43966132011-07-19 06:37:02 +000068MipselTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000069 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +000070 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000071 CodeGenOpt::Level OL)
72 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000073
David Blaikie2d24e2a2011-12-20 02:50:00 +000074void Mips64ebTargetMachine::anchor() { }
75
Akira Hatanaka24648102011-09-21 03:00:58 +000076Mips64ebTargetMachine::
77Mips64ebTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000078 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +000079 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000080 CodeGenOpt::Level OL)
81 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Akira Hatanaka24648102011-09-21 03:00:58 +000082
David Blaikie2d24e2a2011-12-20 02:50:00 +000083void Mips64elTargetMachine::anchor() { }
84
Akira Hatanaka24648102011-09-21 03:00:58 +000085Mips64elTargetMachine::
86Mips64elTargetMachine(const Target &T, StringRef TT,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000087 StringRef CPU, StringRef FS, const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +000088 Reloc::Model RM, CodeModel::Model CM,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000089 CodeGenOpt::Level OL)
90 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Akira Hatanaka24648102011-09-21 03:00:58 +000091
Andrew Trick843ee2e2012-02-03 05:12:41 +000092namespace {
93/// Mips Code Generator Pass Configuration Options.
94class MipsPassConfig : public TargetPassConfig {
95public:
Andrew Trick061efcf2012-02-04 02:56:59 +000096 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
97 : TargetPassConfig(TM, PM) {}
Andrew Trick843ee2e2012-02-03 05:12:41 +000098
99 MipsTargetMachine &getMipsTargetMachine() const {
100 return getTM<MipsTargetMachine>();
101 }
102
103 const MipsSubtarget &getMipsSubtarget() const {
104 return *getMipsTargetMachine().getSubtargetImpl();
105 }
106
107 virtual bool addInstSelector();
108 virtual bool addPreRegAlloc();
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000109 virtual bool addPreSched2();
Andrew Trick843ee2e2012-02-03 05:12:41 +0000110 virtual bool addPreEmitPass();
111};
112} // namespace
113
Andrew Trick061efcf2012-02-04 02:56:59 +0000114TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
115 return new MipsPassConfig(this, PM);
Andrew Trick843ee2e2012-02-03 05:12:41 +0000116}
117
Anton Korobeynikov33464912010-11-15 00:06:54 +0000118// Install an instruction selector pass using
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000119// the ISelDag to gen Mips code.
Andrew Trick843ee2e2012-02-03 05:12:41 +0000120bool MipsPassConfig::addInstSelector()
Chris Lattner8eeba352010-01-20 06:34:14 +0000121{
Andrew Trick843ee2e2012-02-03 05:12:41 +0000122 PM.add(createMipsISelDag(getMipsTargetMachine()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000123 return false;
124}
125
Anton Korobeynikov33464912010-11-15 00:06:54 +0000126// Implemented by targets that want to run passes immediately before
127// machine code is emitted. return true if -print-machineinstrs should
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000128// print out the code after the passes.
Andrew Trick843ee2e2012-02-03 05:12:41 +0000129bool MipsPassConfig::addPreEmitPass()
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000130{
Andrew Trick843ee2e2012-02-03 05:12:41 +0000131 PM.add(createMipsDelaySlotFillerPass(getMipsTargetMachine()));
Bruno Cardoso Lopesaff42dc2007-08-18 01:58:15 +0000132 return true;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000133}
Akira Hatanaka99a2e982011-04-15 19:52:08 +0000134
Andrew Trick843ee2e2012-02-03 05:12:41 +0000135bool MipsPassConfig::addPreRegAlloc() {
Akira Hatanaka78fec582011-09-27 16:58:43 +0000136 // Do not restore $gp if target is Mips64.
137 // In N32/64, $gp is a callee-saved register.
Andrew Trick843ee2e2012-02-03 05:12:41 +0000138 if (!getMipsSubtarget().hasMips64())
139 PM.add(createMipsEmitGPRestorePass(getMipsTargetMachine()));
Akira Hatanaka6b7588e2011-05-04 17:54:27 +0000140 return true;
141}
142
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000143bool MipsPassConfig::addPreSched2() {
Andrew Trick843ee2e2012-02-03 05:12:41 +0000144 PM.add(createMipsExpandPseudoPass(getMipsTargetMachine()));
Akira Hatanaka99a2e982011-04-15 19:52:08 +0000145 return true;
146}
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000147
148bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
Evan Chengb95fc312011-11-16 08:38:26 +0000149 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000150 // Machine code emitter pass for Mips.
151 PM.add(createMipsJITCodeEmitterPass(*this, JCE));
152 return false;
153}