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