1 //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements the info about Mips target spec.
12 //===----------------------------------------------------------------------===//
14 #include "MipsTargetMachine.h"
16 #include "MipsFrameLowering.h"
17 #include "MipsInstrInfo.h"
18 #include "MipsModuleISelDAGToDAG.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"
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);
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.
55 MipsTargetMachine(const Target &T, StringRef TT,
56 StringRef CPU, StringRef FS, const TargetOptions &Options,
57 Reloc::Model RM, CodeModel::Model CM,
60 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
61 Subtarget(TT, CPU, FS, isLittle, RM, this),
63 (Subtarget.isABI_N64() ?
64 "e-p:64:64:64-i8:8:32-i16:16:32-i64:64:64-f128:128:128-"
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-"
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() {
78 void MipsTargetMachine::setHelperClassesMips16() {
79 InstrInfoSE.swap(InstrInfo);
80 FrameLoweringSE.swap(FrameLowering);
81 TLInfoSE.swap(TLInfo);
83 InstrInfo.reset(MipsInstrInfo::create(*this));
84 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
85 TLInfo.reset(MipsTargetLowering::create(*this));
87 InstrInfo16.swap(InstrInfo);
88 FrameLowering16.swap(FrameLowering);
89 TLInfo16.swap(TLInfo);
91 assert(TLInfo && "null target lowering 16");
92 assert(InstrInfo && "null instr info 16");
93 assert(FrameLowering && "null frame lowering 16");
96 void MipsTargetMachine::setHelperClassesMipsSE() {
97 InstrInfo16.swap(InstrInfo);
98 FrameLowering16.swap(FrameLowering);
99 TLInfo16.swap(TLInfo);
101 InstrInfo.reset(MipsInstrInfo::create(*this));
102 FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget));
103 TLInfo.reset(MipsTargetLowering::create(*this));
105 InstrInfoSE.swap(InstrInfo);
106 FrameLoweringSE.swap(FrameLowering);
107 TLInfoSE.swap(TLInfo);
109 assert(TLInfo && "null target lowering in SE");
110 assert(InstrInfo && "null instr info SE");
111 assert(FrameLowering && "null frame lowering SE");
113 void MipsebTargetMachine::anchor() { }
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) {}
122 void MipselTargetMachine::anchor() { }
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) {}
132 /// Mips Code Generator Pass Configuration Options.
133 class MipsPassConfig : public TargetPassConfig {
135 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
136 : TargetPassConfig(TM, PM) {}
138 MipsTargetMachine &getMipsTargetMachine() const {
139 return getTM<MipsTargetMachine>();
142 const MipsSubtarget &getMipsSubtarget() const {
143 return *getMipsTargetMachine().getSubtargetImpl();
146 virtual void addIRPasses();
147 virtual bool addInstSelector();
148 virtual bool addPreEmitPass();
152 TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
153 return new MipsPassConfig(this, PM);
156 void MipsPassConfig::addIRPasses() {
157 TargetPassConfig::addIRPasses();
158 if (getMipsSubtarget().os16())
159 addPass(createMipsOs16(getMipsTargetMachine()));
160 if (getMipsSubtarget().inMips16HardFloat())
161 addPass(createMips16HardFloat(getMipsTargetMachine()));
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()));
171 addPass(createMipsISelDag(getMipsTargetMachine()));
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());
185 LLVMTargetMachine::addAnalysisPasses(PM);
186 DEBUG(errs() << "Target Transform Info Pass Added\n");
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));
197 if (Subtarget.hasStandardEncoding() ||
198 Subtarget.allowMixed16_32())
199 addPass(createMipsLongBranchPass(TM));
200 if (Subtarget.inMips16Mode() ||
201 Subtarget.allowMixed16_32())
202 addPass(createMipsConstantIslandPass(TM));
207 bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
208 JITCodeEmitter &JCE) {
209 // Machine code emitter pass for Mips.
210 PM.add(createMipsJITCodeEmitterPass(*this, JCE));