Remove resetSubtargetFeatures as it is unused.
[oota-llvm.git] / lib / Target / PowerPC / PPCSubtarget.cpp
1 //===-- PowerPCSubtarget.cpp - PPC 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 PPC specific subclass of TargetSubtargetInfo.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PPCSubtarget.h"
15 #include "PPC.h"
16 #include "PPCRegisterInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineScheduler.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include <cstdlib>
26
27 using namespace llvm;
28
29 #define DEBUG_TYPE "ppc-subtarget"
30
31 #define GET_SUBTARGETINFO_TARGET_DESC
32 #define GET_SUBTARGETINFO_CTOR
33 #include "PPCGenSubtargetInfo.inc"
34
35 /// Return the datalayout string of a subtarget.
36 static std::string getDataLayoutString(const Triple &T) {
37   bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
38   std::string Ret;
39
40   // Most PPC* platforms are big endian, PPC64LE is little endian.
41   if (T.getArch() == Triple::ppc64le)
42     Ret = "e";
43   else
44     Ret = "E";
45
46   Ret += DataLayout::getManglingComponent(T);
47
48   // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
49   // pointers.
50   if (!is64Bit || T.getOS() == Triple::Lv2)
51     Ret += "-p:32:32";
52
53   // Note, the alignment values for f64 and i64 on ppc64 in Darwin
54   // documentation are wrong; these are correct (i.e. "what gcc does").
55   if (is64Bit || !T.isOSDarwin())
56     Ret += "-i64:64";
57   else
58     Ret += "-f64:32:64";
59
60   // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
61   if (is64Bit)
62     Ret += "-n32:64";
63   else
64     Ret += "-n32";
65
66   return Ret;
67 }
68
69 PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
70                                                             StringRef FS) {
71   initializeEnvironment();
72   initSubtargetFeatures(CPU, FS);
73   return *this;
74 }
75
76 PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
77                            const std::string &FS, PPCTargetMachine &TM,
78                            CodeGenOpt::Level OptLevel)
79     : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT),
80       DL(getDataLayoutString(TargetTriple)),
81       IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
82               TargetTriple.getArch() == Triple::ppc64le),
83       OptLevel(OptLevel), TargetABI(PPC_ABI_UNKNOWN),
84       FrameLowering(initializeSubtargetDependencies(CPU, FS)), InstrInfo(*this),
85       TLInfo(TM), TSInfo(&DL) {}
86
87 void PPCSubtarget::initializeEnvironment() {
88   StackAlignment = 16;
89   DarwinDirective = PPC::DIR_NONE;
90   HasMFOCRF = false;
91   Has64BitSupport = false;
92   Use64BitRegs = false;
93   UseCRBits = false;
94   HasAltivec = false;
95   HasSPE = false;
96   HasQPX = false;
97   HasVSX = false;
98   HasFCPSGN = false;
99   HasFSQRT = false;
100   HasFRE = false;
101   HasFRES = false;
102   HasFRSQRTE = false;
103   HasFRSQRTES = false;
104   HasRecipPrec = false;
105   HasSTFIWX = false;
106   HasLFIWAX = false;
107   HasFPRND = false;
108   HasFPCVT = false;
109   HasISEL = false;
110   HasPOPCNTD = false;
111   HasLDBRX = false;
112   IsBookE = false;
113   IsPPC4xx = false;
114   IsPPC6xx = false;
115   IsE500 = false;
116   DeprecatedMFTB = false;
117   DeprecatedDST = false;
118   HasLazyResolverStubs = false;
119 }
120
121 void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
122   // Determine default and user specified characteristics
123   std::string CPUName = CPU;
124   if (CPUName.empty())
125     CPUName = "generic";
126 #if (defined(__APPLE__) || defined(__linux__)) && \
127     (defined(__ppc__) || defined(__powerpc__))
128   if (CPUName == "generic")
129     CPUName = sys::getHostCPUName();
130 #endif
131
132   // Initialize scheduling itinerary for the specified CPU.
133   InstrItins = getInstrItineraryForCPU(CPUName);
134
135   // Make sure 64-bit features are available when CPUname is generic
136   std::string FullFS = FS;
137
138   // If we are generating code for ppc64, verify that options make sense.
139   if (IsPPC64) {
140     Has64BitSupport = true;
141     // Silently force 64-bit register use on ppc64.
142     Use64BitRegs = true;
143     if (!FullFS.empty())
144       FullFS = "+64bit," + FullFS;
145     else
146       FullFS = "+64bit";
147   }
148
149   // At -O2 and above, track CR bits as individual registers.
150   if (OptLevel >= CodeGenOpt::Default) {
151     if (!FullFS.empty())
152       FullFS = "+crbits," + FullFS;
153     else
154       FullFS = "+crbits";
155   }
156
157   // Parse features string.
158   ParseSubtargetFeatures(CPUName, FullFS);
159
160   // If the user requested use of 64-bit regs, but the cpu selected doesn't
161   // support it, ignore.
162   if (use64BitRegs() && !has64BitSupport())
163     Use64BitRegs = false;
164
165   // Set up darwin-specific properties.
166   if (isDarwin())
167     HasLazyResolverStubs = true;
168
169   // QPX requires a 32-byte aligned stack. Note that we need to do this if
170   // we're compiling for a BG/Q system regardless of whether or not QPX
171   // is enabled because external functions will assume this alignment.
172   if (hasQPX() || isBGQ())
173     StackAlignment = 32;
174
175   // Determine endianness.
176   IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
177
178   // FIXME: For now, we disable VSX in little-endian mode until endian
179   // issues in those instructions can be addressed.
180   if (IsLittleEndian)
181     HasVSX = false;
182
183   // Determine default ABI.
184   if (TargetABI == PPC_ABI_UNKNOWN) {
185     if (!isDarwin() && IsPPC64) {
186       if (IsLittleEndian)
187         TargetABI = PPC_ABI_ELFv2;
188       else
189         TargetABI = PPC_ABI_ELFv1;
190     }
191   }
192 }
193
194 /// hasLazyResolverStub - Return true if accesses to the specified global have
195 /// to go through a dyld lazy resolution stub.  This means that an extra load
196 /// is required to get the address of the global.
197 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
198                                        const TargetMachine &TM) const {
199   // We never have stubs if HasLazyResolverStubs=false or if in static mode.
200   if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
201     return false;
202   // If symbol visibility is hidden, the extra load is not needed if
203   // the symbol is definitely defined in the current translation unit.
204   bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
205   if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
206     return false;
207   return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
208          GV->hasCommonLinkage() || isDecl;
209 }
210
211 // Embedded cores need aggressive scheduling (and some others also benefit).
212 static bool needsAggressiveScheduling(unsigned Directive) {
213   switch (Directive) {
214   default: return false;
215   case PPC::DIR_440:
216   case PPC::DIR_A2:
217   case PPC::DIR_E500mc:
218   case PPC::DIR_E5500:
219   case PPC::DIR_PWR7:
220   case PPC::DIR_PWR8:
221     return true;
222   }
223 }
224
225 bool PPCSubtarget::enableMachineScheduler() const {
226   // Enable MI scheduling for the embedded cores.
227   // FIXME: Enable this for all cores (some additional modeling
228   // may be necessary).
229   return needsAggressiveScheduling(DarwinDirective);
230 }
231
232 // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
233 bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
234
235 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
236   return TargetSubtargetInfo::ANTIDEP_ALL;
237 }
238
239 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
240   CriticalPathRCs.clear();
241   CriticalPathRCs.push_back(isPPC64() ?
242                             &PPC::G8RCRegClass : &PPC::GPRCRegClass);
243 }
244
245 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
246                                        MachineInstr *begin,
247                                        MachineInstr *end,
248                                        unsigned NumRegionInstrs) const {
249   if (needsAggressiveScheduling(DarwinDirective)) {
250     Policy.OnlyTopDown = false;
251     Policy.OnlyBottomUp = false;
252   }
253
254   // Spilling is generally expensive on all PPC cores, so always enable
255   // register-pressure tracking.
256   Policy.ShouldTrackPressure = true;
257 }
258
259 bool PPCSubtarget::useAA() const {
260   // Use AA during code generation for the embedded cores.
261   return needsAggressiveScheduling(DarwinDirective);
262 }
263