Fix a subtle difference between running clang vs llc for mips16.
[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 #define DEBUG_TYPE "mips-subtarget"
15
16 #include "MipsMachineFunction.h"
17 #include "MipsSubtarget.h"
18 #include "MipsTargetMachine.h"
19 #include "Mips.h"
20 #include "MipsRegisterInfo.h"
21 #include "llvm/IR/Attributes.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Support/raw_ostream.h"
27
28 #define GET_SUBTARGETINFO_TARGET_DESC
29 #define GET_SUBTARGETINFO_CTOR
30 #include "MipsGenSubtargetInfo.inc"
31
32
33 using namespace llvm;
34
35 // FIXME: Maybe this should be on by default when Mips16 is specified
36 //
37 static cl::opt<bool> Mixed16_32(
38   "mips-mixed-16-32",
39   cl::init(false),
40   cl::desc("Allow for a mixture of Mips16 "
41            "and Mips32 code in a single source file"),
42   cl::Hidden);
43
44 static cl::opt<bool> Mips_Os16(
45   "mips-os16",
46   cl::init(false),
47   cl::desc("Compile all functions that don' use "
48            "floating point as Mips 16"),
49   cl::Hidden);
50
51 static cl::opt<bool>
52 Mips16HardFloat("mips16-hard-float", cl::NotHidden,
53                 cl::desc("MIPS: mips16 hard float enable."),
54                 cl::init(false));
55
56 void MipsSubtarget::anchor() { }
57
58 MipsSubtarget::MipsSubtarget(const std::string &TT, const std::string &CPU,
59                              const std::string &FS, bool little,
60                              Reloc::Model _RM, MipsTargetMachine *_TM) :
61   MipsGenSubtargetInfo(TT, CPU, FS),
62   MipsArchVersion(Mips32), MipsABI(UnknownABI), IsLittle(little),
63   IsSingleFloat(false), IsFP64bit(false), IsGP64bit(false), HasVFPU(false),
64   IsLinux(true), HasSEInReg(false), HasCondMov(false), HasSwap(false),
65   HasBitCount(false), HasFPIdx(false),
66   InMips16Mode(false), InMips16HardFloat(Mips16HardFloat),
67   InMicroMipsMode(false), HasDSP(false), HasDSPR2(false),
68   AllowMixed16_32(Mixed16_32 | Mips_Os16), Os16(Mips_Os16), HasMSA(false),
69   RM(_RM), OverrideMode(NoOverride), TM(_TM)
70 {
71   std::string CPUName = CPU;
72   if (CPUName.empty())
73     CPUName = "mips32";
74
75   // Parse features string.
76   ParseSubtargetFeatures(CPUName, FS);
77
78   PreviousInMips16Mode = InMips16Mode;
79
80   // Initialize scheduling itinerary for the specified CPU.
81   InstrItins = getInstrItineraryForCPU(CPUName);
82
83   // Set MipsABI if it hasn't been set yet.
84   if (MipsABI == UnknownABI)
85     MipsABI = hasMips64() ? N64 : O32;
86
87   // Check if Architecture and ABI are compatible.
88   assert(((!hasMips64() && (isABI_O32() || isABI_EABI())) ||
89           (hasMips64() && (isABI_N32() || isABI_N64()))) &&
90          "Invalid  Arch & ABI pair.");
91
92   // Is the target system Linux ?
93   if (TT.find("linux") == std::string::npos)
94     IsLinux = false;
95
96   // Set UseSmallSection.
97   UseSmallSection = !IsLinux && (RM == Reloc::Static);
98   // set some subtarget specific features
99   if (inMips16Mode())
100     HasBitCount=false;
101 }
102
103 bool
104 MipsSubtarget::enablePostRAScheduler(CodeGenOpt::Level OptLevel,
105                                     TargetSubtargetInfo::AntiDepBreakMode &Mode,
106                                      RegClassVector &CriticalPathRCs) const {
107   Mode = TargetSubtargetInfo::ANTIDEP_NONE;
108   CriticalPathRCs.clear();
109   CriticalPathRCs.push_back(hasMips64() ?
110                             &Mips::GPR64RegClass : &Mips::GPR32RegClass);
111   return OptLevel >= CodeGenOpt::Aggressive;
112 }
113
114 //FIXME: This logic for reseting the subtarget along with
115 // the helper classes can probably be simplified but there are a lot of
116 // cases so we will defer rewriting this to later.
117 //
118 void MipsSubtarget::resetSubtarget(MachineFunction *MF) {
119   bool ChangeToMips16 = false, ChangeToNoMips16 = false;
120   DEBUG(dbgs() << "resetSubtargetFeatures" << "\n");
121   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
122   ChangeToMips16 = FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
123                                         "mips16");
124   ChangeToNoMips16 = FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
125                                         "nomips16");
126   assert (!(ChangeToMips16 & ChangeToNoMips16) &&
127           "mips16 and nomips16 specified on the same function");
128   if (ChangeToMips16) {
129     if (PreviousInMips16Mode)
130       return;
131     OverrideMode = Mips16Override;
132     PreviousInMips16Mode = true;
133     TM->setHelperClassesMips16();
134     return;
135   } else if (ChangeToNoMips16) {
136     if (!PreviousInMips16Mode)
137       return;
138     OverrideMode = NoMips16Override;
139     PreviousInMips16Mode = false;
140     TM->setHelperClassesMipsSE();
141     return;
142   } else {
143     if (OverrideMode == NoOverride)
144       return;
145     OverrideMode = NoOverride;
146     DEBUG(dbgs() << "back to default" << "\n");
147     if (inMips16Mode() && !PreviousInMips16Mode) {
148       TM->setHelperClassesMips16();
149       PreviousInMips16Mode = true;
150     } else if (!inMips16Mode() && PreviousInMips16Mode) {
151       TM->setHelperClassesMipsSE();
152       PreviousInMips16Mode = false;
153     }
154     return;
155   }
156 }
157