89bbcf783463771358f7e9c2309d7199061a3c51
[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 "PPCTargetMachine.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineScheduler.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include <cstdlib>
27
28 using namespace llvm;
29
30 #define DEBUG_TYPE "ppc-subtarget"
31
32 #define GET_SUBTARGETINFO_TARGET_DESC
33 #define GET_SUBTARGETINFO_CTOR
34 #include "PPCGenSubtargetInfo.inc"
35
36 static cl::opt<bool> UseSubRegLiveness("ppc-track-subreg-liveness",
37 cl::desc("Enable subregister liveness tracking for PPC"), cl::Hidden);
38
39 static cl::opt<bool> QPXStackUnaligned("qpx-stack-unaligned",
40   cl::desc("Even when QPX is enabled the stack is not 32-byte aligned"),
41   cl::Hidden);
42
43 PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
44                                                             StringRef FS) {
45   initializeEnvironment();
46   initSubtargetFeatures(CPU, FS);
47   return *this;
48 }
49
50 PPCSubtarget::PPCSubtarget(const Triple &TT, const std::string &CPU,
51                            const std::string &FS, const PPCTargetMachine &TM)
52     : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT),
53       IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
54               TargetTriple.getArch() == Triple::ppc64le),
55       TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, FS)),
56       InstrInfo(*this), TLInfo(TM, *this) {}
57
58 void PPCSubtarget::initializeEnvironment() {
59   StackAlignment = 16;
60   DarwinDirective = PPC::DIR_NONE;
61   HasMFOCRF = false;
62   Has64BitSupport = false;
63   Use64BitRegs = false;
64   UseCRBits = false;
65   UseSoftFloat = false;
66   HasAltivec = false;
67   HasSPE = false;
68   HasQPX = false;
69   HasVSX = false;
70   HasP8Vector = false;
71   HasP8Altivec = false;
72   HasP8Crypto = false;
73   HasFCPSGN = false;
74   HasFSQRT = false;
75   HasFRE = false;
76   HasFRES = false;
77   HasFRSQRTE = false;
78   HasFRSQRTES = false;
79   HasRecipPrec = false;
80   HasSTFIWX = false;
81   HasLFIWAX = false;
82   HasFPRND = false;
83   HasFPCVT = false;
84   HasISEL = false;
85   HasPOPCNTD = false;
86   HasBPERMD = false;
87   HasExtDiv = false;
88   HasCMPB = false;
89   HasLDBRX = false;
90   IsBookE = false;
91   HasOnlyMSYNC = false;
92   IsPPC4xx = false;
93   IsPPC6xx = false;
94   IsE500 = false;
95   FeatureMFTB = false;
96   DeprecatedDST = false;
97   HasLazyResolverStubs = false;
98   HasICBT = false;
99   HasInvariantFunctionDescriptors = false;
100   HasPartwordAtomics = false;
101   HasDirectMove = false;
102   IsQPXStackUnaligned = false;
103   HasHTM = false;
104   HasFusion = false;
105 }
106
107 void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
108   // Determine default and user specified characteristics
109   std::string CPUName = CPU;
110   if (CPUName.empty()) {
111     // If cross-compiling with -march=ppc64le without -mcpu
112     if (TargetTriple.getArch() == Triple::ppc64le)
113       CPUName = "ppc64le";
114     else
115       CPUName = "generic";
116   }
117
118   // Initialize scheduling itinerary for the specified CPU.
119   InstrItins = getInstrItineraryForCPU(CPUName);
120
121   // Parse features string.
122   ParseSubtargetFeatures(CPUName, FS);
123
124   // If the user requested use of 64-bit regs, but the cpu selected doesn't
125   // support it, ignore.
126   if (IsPPC64 && has64BitSupport())
127     Use64BitRegs = true;
128
129   // Set up darwin-specific properties.
130   if (isDarwin())
131     HasLazyResolverStubs = true;
132
133   // QPX requires a 32-byte aligned stack. Note that we need to do this if
134   // we're compiling for a BG/Q system regardless of whether or not QPX
135   // is enabled because external functions will assume this alignment.
136   IsQPXStackUnaligned = QPXStackUnaligned;
137   StackAlignment = getPlatformStackAlignment();
138
139   // Determine endianness.
140   // FIXME: Part of the TargetMachine.
141   IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
142 }
143
144 /// hasLazyResolverStub - Return true if accesses to the specified global have
145 /// to go through a dyld lazy resolution stub.  This means that an extra load
146 /// is required to get the address of the global.
147 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV) const {
148   // We never have stubs if HasLazyResolverStubs=false or if in static mode.
149   if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
150     return false;
151   bool isDecl = GV->isDeclaration();
152   if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
153     return false;
154   return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
155          GV->hasCommonLinkage() || isDecl;
156 }
157
158 // Embedded cores need aggressive scheduling (and some others also benefit).
159 static bool needsAggressiveScheduling(unsigned Directive) {
160   switch (Directive) {
161   default: return false;
162   case PPC::DIR_440:
163   case PPC::DIR_A2:
164   case PPC::DIR_E500mc:
165   case PPC::DIR_E5500:
166   case PPC::DIR_PWR7:
167   case PPC::DIR_PWR8:
168     return true;
169   }
170 }
171
172 bool PPCSubtarget::enableMachineScheduler() const {
173   // Enable MI scheduling for the embedded cores.
174   // FIXME: Enable this for all cores (some additional modeling
175   // may be necessary).
176   return needsAggressiveScheduling(DarwinDirective);
177 }
178
179 // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
180 bool PPCSubtarget::enablePostRAScheduler() const { return true; }
181
182 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
183   return TargetSubtargetInfo::ANTIDEP_ALL;
184 }
185
186 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
187   CriticalPathRCs.clear();
188   CriticalPathRCs.push_back(isPPC64() ?
189                             &PPC::G8RCRegClass : &PPC::GPRCRegClass);
190 }
191
192 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
193                                        MachineInstr *begin,
194                                        MachineInstr *end,
195                                        unsigned NumRegionInstrs) const {
196   if (needsAggressiveScheduling(DarwinDirective)) {
197     Policy.OnlyTopDown = false;
198     Policy.OnlyBottomUp = false;
199   }
200
201   // Spilling is generally expensive on all PPC cores, so always enable
202   // register-pressure tracking.
203   Policy.ShouldTrackPressure = true;
204 }
205
206 bool PPCSubtarget::useAA() const {
207   // Use AA during code generation for the embedded cores.
208   return needsAggressiveScheduling(DarwinDirective);
209 }
210
211 bool PPCSubtarget::enableSubRegLiveness() const {
212   return UseSubRegLiveness;
213 }
214
215 unsigned char PPCSubtarget::classifyGlobalReference(
216     const GlobalValue *GV) const {
217   // Note that currently we don't generate non-pic references.
218   // If a caller wants that, this will have to be updated.
219
220   // Large code model always uses the TOC even for local symbols.
221   if (TM.getCodeModel() == CodeModel::Large)
222     return PPCII::MO_PIC_FLAG | PPCII::MO_NLP_FLAG;
223
224   unsigned char flags = PPCII::MO_PIC_FLAG;
225
226   // Only if the relocation mode is PIC do we have to worry about
227   // interposition. In all other cases we can use a slightly looser standard to
228   // decide how to access the symbol.
229   if (TM.getRelocationModel() == Reloc::PIC_) {
230     // If it's local, or it's non-default, it can't be interposed.
231     if (!GV->hasLocalLinkage() &&
232         GV->hasDefaultVisibility()) {
233       flags |= PPCII::MO_NLP_FLAG;
234     }
235     return flags;
236   }
237
238   if (GV->isStrongDefinitionForLinker())
239     return flags;
240   return flags | PPCII::MO_NLP_FLAG;
241 }
242
243 bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
244 bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }