Checkin in of first of several patches to finish implementation of
[oota-llvm.git] / lib / Target / Mips / MipsTargetMachine.cpp
1 //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements the info about Mips target spec.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsTargetMachine.h"
15 #include "Mips.h"
16 #include "MipsFrameLowering.h"
17 #include "MipsInstrInfo.h"
18 #include "MipsModuleISelDAGToDAG.h"
19 #include "MipsOs16.h"
20 #include "MipsSEFrameLowering.h"
21 #include "MipsSEInstrInfo.h"
22 #include "MipsSEISelLowering.h"
23 #include "MipsSEISelDAGToDAG.h"
24 #include "Mips16FrameLowering.h"
25 #include "Mips16HardFloat.h"
26 #include "Mips16InstrInfo.h"
27 #include "Mips16ISelDAGToDAG.h"
28 #include "Mips16ISelLowering.h"
29 #include "llvm/Analysis/TargetTransformInfo.h"
30 #include "llvm/CodeGen/Passes.h"
31 #include "llvm/PassManager.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Support/TargetRegistry.h"
35 using namespace llvm;
36
37
38
39 extern "C" void LLVMInitializeMipsTarget() {
40   // Register the target.
41   RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
42   RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
43   RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
44   RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
45 }
46
47 // DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
48 // The stack is always 8 byte aligned
49 // On function prologue, the stack is created by decrementing
50 // its pointer. Once decremented, all references are done with positive
51 // offset from the stack/frame pointer, using StackGrowsUp enables
52 // an easier handling.
53 // Using CodeModel::Large enables different CALL behavior.
54 MipsTargetMachine::
55 MipsTargetMachine(const Target &T, StringRef TT,
56                   StringRef CPU, StringRef FS, const TargetOptions &Options,
57                   Reloc::Model RM, CodeModel::Model CM,
58                   CodeGenOpt::Level OL,
59                   bool isLittle)
60   : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
61     Subtarget(TT, CPU, FS, isLittle, RM, this),
62     DL(isLittle ?
63                (Subtarget.isABI_N64() ?
64                 "e-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-"
65                 "n32:64-S128" :
66                 "e-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32-S64") :
67                (Subtarget.isABI_N64() ?
68                 "E-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-"
69                 "n32:64-S128" :
70                 "E-p:32:32:32-i8:8:32-i16:16:32-i64:64:64-n32-S64")),
71     InstrInfo(MipsInstrInfo::create(*this)),
72     FrameLowering(MipsFrameLowering::create(*this, Subtarget)),
73     TLInfo(MipsTargetLowering::create(*this)),
74     TSInfo(*this), JITInfo() {
75 }
76
77
78 void MipsTargetMachine::setHelperClassesMips16() {
79   InstrInfoSE.swap(InstrInfo);
80   FrameLoweringSE.swap(FrameLowering);
81   TLInfoSE.swap(TLInfo);
82   if (!InstrInfo16) {
83     InstrInfo.reset(MipsInstrInfo::create(*this));
84     FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
85     TLInfo.reset(MipsTargetLowering::create(*this));
86   } else {
87     InstrInfo16.swap(InstrInfo);
88     FrameLowering16.swap(FrameLowering);
89     TLInfo16.swap(TLInfo);
90   }
91   assert(TLInfo && "null target lowering 16");
92   assert(InstrInfo && "null instr info 16");
93   assert(FrameLowering && "null frame lowering 16");
94 }
95
96 void MipsTargetMachine::setHelperClassesMipsSE() {
97   InstrInfo16.swap(InstrInfo);
98   FrameLowering16.swap(FrameLowering);
99   TLInfo16.swap(TLInfo);
100   if (!InstrInfoSE) {
101     InstrInfo.reset(MipsInstrInfo::create(*this));
102     FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
103     TLInfo.reset(MipsTargetLowering::create(*this));
104   } else {
105     InstrInfoSE.swap(InstrInfo);
106     FrameLoweringSE.swap(FrameLowering);
107     TLInfoSE.swap(TLInfo);
108   }
109   assert(TLInfo && "null target lowering in SE");
110   assert(InstrInfo && "null instr info SE");
111   assert(FrameLowering && "null frame lowering SE");
112 }
113 void MipsebTargetMachine::anchor() { }
114
115 MipsebTargetMachine::
116 MipsebTargetMachine(const Target &T, StringRef TT,
117                     StringRef CPU, StringRef FS, const TargetOptions &Options,
118                     Reloc::Model RM, CodeModel::Model CM,
119                     CodeGenOpt::Level OL)
120   : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
121
122 void MipselTargetMachine::anchor() { }
123
124 MipselTargetMachine::
125 MipselTargetMachine(const Target &T, StringRef TT,
126                     StringRef CPU, StringRef FS, const TargetOptions &Options,
127                     Reloc::Model RM, CodeModel::Model CM,
128                     CodeGenOpt::Level OL)
129   : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
130
131 namespace {
132 /// Mips Code Generator Pass Configuration Options.
133 class MipsPassConfig : public TargetPassConfig {
134 public:
135   MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
136     : TargetPassConfig(TM, PM) {}
137
138   MipsTargetMachine &getMipsTargetMachine() const {
139     return getTM<MipsTargetMachine>();
140   }
141
142   const MipsSubtarget &getMipsSubtarget() const {
143     return *getMipsTargetMachine().getSubtargetImpl();
144   }
145
146   virtual void addIRPasses();
147   virtual bool addInstSelector();
148   virtual bool addPreEmitPass();
149 };
150 } // namespace
151
152 TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
153   return new MipsPassConfig(this, PM);
154 }
155
156 void MipsPassConfig::addIRPasses() {
157   TargetPassConfig::addIRPasses();
158   if (getMipsSubtarget().os16())
159     addPass(createMipsOs16(getMipsTargetMachine()));
160   if (getMipsSubtarget().inMips16HardFloat())
161     addPass(createMips16HardFloat(getMipsTargetMachine()));
162 }
163 // Install an instruction selector pass using
164 // the ISelDag to gen Mips code.
165 bool MipsPassConfig::addInstSelector() {
166   if (getMipsSubtarget().allowMixed16_32()) {
167     addPass(createMipsModuleISelDag(getMipsTargetMachine()));
168     addPass(createMips16ISelDag(getMipsTargetMachine()));
169     addPass(createMipsSEISelDag(getMipsTargetMachine()));
170   } else {
171     addPass(createMipsISelDag(getMipsTargetMachine()));
172   }
173   return false;
174 }
175
176 void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
177   if (Subtarget.allowMixed16_32()) {
178     DEBUG(errs() << "No ");
179     //FIXME: The Basic Target Transform Info
180     // pass needs to become a function pass instead of
181     // being an immutable pass and then this method as it exists now
182     // would be unnecessary.
183     PM.add(createNoTargetTransformInfoPass());
184   } else
185     LLVMTargetMachine::addAnalysisPasses(PM);
186   DEBUG(errs() << "Target Transform Info Pass Added\n");
187 }
188
189 // Implemented by targets that want to run passes immediately before
190 // machine code is emitted. return true if -print-machineinstrs should
191 // print out the code after the passes.
192 bool MipsPassConfig::addPreEmitPass() {
193   MipsTargetMachine &TM = getMipsTargetMachine();
194   const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
195   addPass(createMipsDelaySlotFillerPass(TM));
196
197   if (Subtarget.hasStandardEncoding() ||
198       Subtarget.allowMixed16_32())
199     addPass(createMipsLongBranchPass(TM));
200   if (Subtarget.inMips16Mode() ||
201       Subtarget.allowMixed16_32())
202     addPass(createMipsConstantIslandPass(TM));
203
204   return true;
205 }
206
207 bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
208                                        JITCodeEmitter &JCE) {
209   // Machine code emitter pass for Mips.
210   PM.add(createMipsJITCodeEmitterPass(*this, JCE));
211   return false;
212 }