Move subtarget dependent features into the subtarget from the target
[oota-llvm.git] / lib / Target / Mips / MipsSubtarget.cpp
1 //===-- MipsSubtarget.cpp - Mips Subtarget Information --------------------===//
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 // This file implements the Mips specific subclass of TargetSubtargetInfo.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsMachineFunction.h"
15 #include "Mips.h"
16 #include "MipsRegisterInfo.h"
17 #include "MipsSubtarget.h"
18 #include "MipsTargetMachine.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 #define DEBUG_TYPE "mips-subtarget"
29
30 #define GET_SUBTARGETINFO_TARGET_DESC
31 #define GET_SUBTARGETINFO_CTOR
32 #include "MipsGenSubtargetInfo.inc"
33
34 // FIXME: Maybe this should be on by default when Mips16 is specified
35 //
36 static cl::opt<bool> Mixed16_32(
37   "mips-mixed-16-32",
38   cl::init(false),
39   cl::desc("Allow for a mixture of Mips16 "
40            "and Mips32 code in a single source file"),
41   cl::Hidden);
42
43 static cl::opt<bool> Mips_Os16(
44   "mips-os16",
45   cl::init(false),
46   cl::desc("Compile all functions that don' use "
47            "floating point as Mips 16"),
48   cl::Hidden);
49
50 static cl::opt<bool>
51 Mips16HardFloat("mips16-hard-float", cl::NotHidden,
52                 cl::desc("MIPS: mips16 hard float enable."),
53                 cl::init(false));
54
55 static cl::opt<bool>
56 Mips16ConstantIslands(
57   "mips16-constant-islands", cl::NotHidden,
58   cl::desc("MIPS: mips16 constant islands enable."),
59   cl::init(true));
60
61 /// Select the Mips CPU for the given triple and cpu name.
62 /// FIXME: Merge with the copy in MipsMCTargetDesc.cpp
63 static StringRef selectMipsCPU(Triple TT, StringRef CPU) {
64   if (CPU.empty() || CPU == "generic") {
65     if (TT.getArch() == Triple::mips || TT.getArch() == Triple::mipsel)
66       CPU = "mips32";
67     else
68       CPU = "mips64";
69   }
70   return CPU;
71 }
72
73 void MipsSubtarget::anchor() { }
74
75 static std::string computeDataLayout(const MipsSubtarget &ST) {
76   std::string Ret = "";
77
78   // There are both little and big endian mips.
79   if (ST.isLittle())
80     Ret += "e";
81   else
82     Ret += "E";
83
84   Ret += "-m:m";
85
86   // Pointers are 32 bit on some ABIs.
87   if (!ST.isABI_N64())
88     Ret += "-p:32:32";
89
90   // 8 and 16 bit integers only need no have natural alignment, but try to
91   // align them to 32 bits. 64 bit integers have natural alignment.
92   Ret += "-i8:8:32-i16:16:32-i64:64";
93
94   // 32 bit registers are always available and the stack is at least 64 bit
95   // aligned. On N64 64 bit registers are also available and the stack is
96   // 128 bit aligned.
97   if (ST.isABI_N64() || ST.isABI_N32())
98     Ret += "-n32:64-S128";
99   else
100     Ret += "-n32-S64";
101
102   return Ret;
103 }
104
105 MipsSubtarget::MipsSubtarget(const std::string &TT, const std::string &CPU,
106                              const std::string &FS, bool little,
107                              Reloc::Model _RM, MipsTargetMachine *_TM)
108     : MipsGenSubtargetInfo(TT, CPU, FS), MipsArchVersion(Mips32),
109       MipsABI(UnknownABI), IsLittle(little), IsSingleFloat(false),
110       IsFP64bit(false), IsNaN2008bit(false), IsGP64bit(false), HasVFPU(false),
111       HasCnMips(false), IsLinux(true), HasMips3_32(false), HasMips3_32r2(false),
112       HasMips4_32(false), HasMips4_32r2(false), HasMips5_32r2(false),
113       InMips16Mode(false), InMips16HardFloat(Mips16HardFloat),
114       InMicroMipsMode(false), HasDSP(false), HasDSPR2(false),
115       AllowMixed16_32(Mixed16_32 | Mips_Os16), Os16(Mips_Os16), HasMSA(false),
116       RM(_RM), OverrideMode(NoOverride), TM(_TM), TargetTriple(TT),
117       DL(computeDataLayout(initializeSubtargetDependencies(CPU, FS, TM))),
118       TSInfo(DL), JITInfo(), InstrInfo(MipsInstrInfo::create(*TM)),
119       FrameLowering(MipsFrameLowering::create(*TM, *this)),
120       TLInfo(MipsTargetLowering::create(*TM)) {
121
122   PreviousInMips16Mode = InMips16Mode;
123
124   // Don't even attempt to generate code for MIPS-I, MIPS-II, MIPS-III, and
125   // MIPS-V. They have not been tested and currently exist for the integrated
126   // assembler only.
127   if (MipsArchVersion == Mips1)
128     report_fatal_error("Code generation for MIPS-I is not implemented", false);
129   if (MipsArchVersion == Mips2)
130     report_fatal_error("Code generation for MIPS-II is not implemented", false);
131   if (MipsArchVersion == Mips3)
132     report_fatal_error("Code generation for MIPS-III is not implemented",
133                        false);
134   if (MipsArchVersion == Mips5)
135     report_fatal_error("Code generation for MIPS-V is not implemented", false);
136
137   // Assert exactly one ABI was chosen.
138   assert(MipsABI != UnknownABI);
139   assert((((getFeatureBits() & Mips::FeatureO32) != 0) +
140           ((getFeatureBits() & Mips::FeatureEABI) != 0) +
141           ((getFeatureBits() & Mips::FeatureN32) != 0) +
142           ((getFeatureBits() & Mips::FeatureN64) != 0)) == 1);
143
144   // Check if Architecture and ABI are compatible.
145   assert(((!isGP64bit() && (isABI_O32() || isABI_EABI())) ||
146           (isGP64bit() && (isABI_N32() || isABI_N64()))) &&
147          "Invalid  Arch & ABI pair.");
148
149   if (hasMSA() && !isFP64bit())
150     report_fatal_error("MSA requires a 64-bit FPU register file (FR=1 mode). "
151                        "See -mattr=+fp64.",
152                        false);
153
154   if (hasMips32r6()) {
155     StringRef ISA = hasMips64r6() ? "MIPS64r6" : "MIPS32r6";
156
157     assert(isFP64bit());
158     assert(isNaN2008());
159     if (hasDSP())
160       report_fatal_error(ISA + " is not compatible with the DSP ASE", false);
161   }
162
163   // Is the target system Linux ?
164   if (TT.find("linux") == std::string::npos)
165     IsLinux = false;
166
167   // Set UseSmallSection.
168   // TODO: Investigate the IsLinux check. I suspect it's really checking for
169   //       bare-metal.
170   UseSmallSection = !IsLinux && (RM == Reloc::Static);
171 }
172
173 bool
174 MipsSubtarget::enablePostRAScheduler(CodeGenOpt::Level OptLevel,
175                                     TargetSubtargetInfo::AntiDepBreakMode &Mode,
176                                      RegClassVector &CriticalPathRCs) const {
177   Mode = TargetSubtargetInfo::ANTIDEP_NONE;
178   CriticalPathRCs.clear();
179   CriticalPathRCs.push_back(isGP64bit() ? &Mips::GPR64RegClass
180                                         : &Mips::GPR32RegClass);
181   return OptLevel >= CodeGenOpt::Aggressive;
182 }
183
184 MipsSubtarget &
185 MipsSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS,
186                                                const TargetMachine *TM) {
187   std::string CPUName = selectMipsCPU(TargetTriple, CPU);
188   
189   // Parse features string.
190   ParseSubtargetFeatures(CPUName, FS);
191   // Initialize scheduling itinerary for the specified CPU.
192   InstrItins = getInstrItineraryForCPU(CPUName);
193
194   if (InMips16Mode && !TM->Options.UseSoftFloat) {
195     // Hard float for mips16 means essentially to compile as soft float
196     // but to use a runtime library for soft float that is written with
197     // native mips32 floating point instructions (those runtime routines
198     // run in mips32 hard float mode).
199     TM->Options.UseSoftFloat = true;
200     TM->Options.FloatABIType = FloatABI::Soft;
201     InMips16HardFloat = true;
202   }
203
204   return *this;
205 }
206
207 //FIXME: This logic for reseting the subtarget along with
208 // the helper classes can probably be simplified but there are a lot of
209 // cases so we will defer rewriting this to later.
210 //
211 void MipsSubtarget::resetSubtarget(MachineFunction *MF) {
212   bool ChangeToMips16 = false, ChangeToNoMips16 = false;
213   DEBUG(dbgs() << "resetSubtargetFeatures" << "\n");
214   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
215   ChangeToMips16 = FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
216                                         "mips16");
217   ChangeToNoMips16 = FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
218                                         "nomips16");
219   assert (!(ChangeToMips16 & ChangeToNoMips16) &&
220           "mips16 and nomips16 specified on the same function");
221   if (ChangeToMips16) {
222     if (PreviousInMips16Mode)
223       return;
224     OverrideMode = Mips16Override;
225     PreviousInMips16Mode = true;
226     setHelperClassesMips16();
227     return;
228   } else if (ChangeToNoMips16) {
229     if (!PreviousInMips16Mode)
230       return;
231     OverrideMode = NoMips16Override;
232     PreviousInMips16Mode = false;
233     setHelperClassesMipsSE();
234     return;
235   } else {
236     if (OverrideMode == NoOverride)
237       return;
238     OverrideMode = NoOverride;
239     DEBUG(dbgs() << "back to default" << "\n");
240     if (inMips16Mode() && !PreviousInMips16Mode) {
241       setHelperClassesMips16();
242       PreviousInMips16Mode = true;
243     } else if (!inMips16Mode() && PreviousInMips16Mode) {
244       setHelperClassesMipsSE();
245       PreviousInMips16Mode = false;
246     }
247     return;
248   }
249 }
250
251 void MipsSubtarget::setHelperClassesMips16() {
252   InstrInfoSE.swap(InstrInfo);
253   FrameLoweringSE.swap(FrameLowering);
254   TLInfoSE.swap(TLInfo);
255   if (!InstrInfo16) {
256     InstrInfo.reset(MipsInstrInfo::create(*TM));
257     FrameLowering.reset(MipsFrameLowering::create(*TM, *this));
258     TLInfo.reset(MipsTargetLowering::create(*TM));
259   } else {
260     InstrInfo16.swap(InstrInfo);
261     FrameLowering16.swap(FrameLowering);
262     TLInfo16.swap(TLInfo);
263   }
264   assert(TLInfo && "null target lowering 16");
265   assert(InstrInfo && "null instr info 16");
266   assert(FrameLowering && "null frame lowering 16");
267 }
268
269 void MipsSubtarget::setHelperClassesMipsSE() {
270   InstrInfo16.swap(InstrInfo);
271   FrameLowering16.swap(FrameLowering);
272   TLInfo16.swap(TLInfo);
273   if (!InstrInfoSE) {
274     InstrInfo.reset(MipsInstrInfo::create(*TM));
275     FrameLowering.reset(MipsFrameLowering::create(*TM, *this));
276     TLInfo.reset(MipsTargetLowering::create(*TM));
277   } else {
278     InstrInfoSE.swap(InstrInfo);
279     FrameLoweringSE.swap(FrameLowering);
280     TLInfoSE.swap(TLInfo);
281   }
282   assert(TLInfo && "null target lowering in SE");
283   assert(InstrInfo && "null instr info SE");
284   assert(FrameLowering && "null frame lowering SE");
285 }
286
287 bool MipsSubtarget::mipsSEUsesSoftFloat() const {
288   return TM->Options.UseSoftFloat && !InMips16HardFloat;
289 }
290
291 bool MipsSubtarget::useConstantIslands() {
292   DEBUG(dbgs() << "use constant islands " << Mips16ConstantIslands << "\n");
293   return Mips16ConstantIslands;
294 }