Remove superfluous .str() and replace std::string concatenation with Twine.
[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/Host.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include <cstdlib>
28
29 using namespace llvm;
30
31 #define DEBUG_TYPE "ppc-subtarget"
32
33 #define GET_SUBTARGETINFO_TARGET_DESC
34 #define GET_SUBTARGETINFO_CTOR
35 #include "PPCGenSubtargetInfo.inc"
36
37 static cl::opt<bool> UseSubRegLiveness("ppc-track-subreg-liveness",
38 cl::desc("Enable subregister liveness tracking for PPC"), cl::Hidden);
39
40 static cl::opt<bool> QPXStackUnaligned("qpx-stack-unaligned",
41   cl::desc("Even when QPX is enabled the stack is not 32-byte aligned"),
42   cl::Hidden);
43
44 PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
45                                                             StringRef FS) {
46   initializeEnvironment();
47   initSubtargetFeatures(CPU, FS);
48   return *this;
49 }
50
51 PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
52                            const std::string &FS, const PPCTargetMachine &TM)
53     : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT),
54       IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
55               TargetTriple.getArch() == Triple::ppc64le),
56       TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, FS)),
57       InstrInfo(*this), TLInfo(TM, *this), TSInfo(TM.getDataLayout()) {}
58
59 void PPCSubtarget::initializeEnvironment() {
60   StackAlignment = 16;
61   DarwinDirective = PPC::DIR_NONE;
62   HasMFOCRF = false;
63   Has64BitSupport = false;
64   Use64BitRegs = false;
65   UseCRBits = 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   HasCMPB = false;
87   HasLDBRX = false;
88   IsBookE = false;
89   HasOnlyMSYNC = false;
90   IsPPC4xx = false;
91   IsPPC6xx = false;
92   IsE500 = false;
93   DeprecatedMFTB = false;
94   DeprecatedDST = false;
95   HasLazyResolverStubs = false;
96   HasICBT = false;
97   HasInvariantFunctionDescriptors = false;
98   HasPartwordAtomics = false;
99   IsQPXStackUnaligned = false;
100   HasHTM = false;
101 }
102
103 void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
104   // Determine default and user specified characteristics
105   std::string CPUName = CPU;
106   if (CPUName.empty()) {
107     // If cross-compiling with -march=ppc64le without -mcpu
108     if (TargetTriple.getArch() == Triple::ppc64le)
109       CPUName = "ppc64le";
110     else
111       CPUName = "generic";
112   }
113 #if (defined(__APPLE__) || defined(__linux__)) && \
114     (defined(__ppc__) || defined(__powerpc__))
115   if (CPUName == "generic")
116     CPUName = sys::getHostCPUName();
117 #endif
118
119   // Initialize scheduling itinerary for the specified CPU.
120   InstrItins = getInstrItineraryForCPU(CPUName);
121
122   // Parse features string.
123   ParseSubtargetFeatures(CPUName, FS);
124
125   // If the user requested use of 64-bit regs, but the cpu selected doesn't
126   // support it, ignore.
127   if (IsPPC64 && has64BitSupport())
128     Use64BitRegs = true;
129
130   // Set up darwin-specific properties.
131   if (isDarwin())
132     HasLazyResolverStubs = true;
133
134   // QPX requires a 32-byte aligned stack. Note that we need to do this if
135   // we're compiling for a BG/Q system regardless of whether or not QPX
136   // is enabled because external functions will assume this alignment.
137   IsQPXStackUnaligned = QPXStackUnaligned;
138   StackAlignment = getPlatformStackAlignment();
139
140   // Determine endianness.
141   // FIXME: Part of the TargetMachine.
142   IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
143 }
144
145 /// hasLazyResolverStub - Return true if accesses to the specified global have
146 /// to go through a dyld lazy resolution stub.  This means that an extra load
147 /// is required to get the address of the global.
148 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV) const {
149   // We never have stubs if HasLazyResolverStubs=false or if in static mode.
150   if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
151     return false;
152   bool isDecl = GV->isDeclaration();
153   if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
154     return false;
155   return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
156          GV->hasCommonLinkage() || isDecl;
157 }
158
159 // Embedded cores need aggressive scheduling (and some others also benefit).
160 static bool needsAggressiveScheduling(unsigned Directive) {
161   switch (Directive) {
162   default: return false;
163   case PPC::DIR_440:
164   case PPC::DIR_A2:
165   case PPC::DIR_E500mc:
166   case PPC::DIR_E5500:
167   case PPC::DIR_PWR7:
168   case PPC::DIR_PWR8:
169     return true;
170   }
171 }
172
173 bool PPCSubtarget::enableMachineScheduler() const {
174   // Enable MI scheduling for the embedded cores.
175   // FIXME: Enable this for all cores (some additional modeling
176   // may be necessary).
177   return needsAggressiveScheduling(DarwinDirective);
178 }
179
180 // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
181 bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
182
183 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
184   return TargetSubtargetInfo::ANTIDEP_ALL;
185 }
186
187 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
188   CriticalPathRCs.clear();
189   CriticalPathRCs.push_back(isPPC64() ?
190                             &PPC::G8RCRegClass : &PPC::GPRCRegClass);
191 }
192
193 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
194                                        MachineInstr *begin,
195                                        MachineInstr *end,
196                                        unsigned NumRegionInstrs) const {
197   if (needsAggressiveScheduling(DarwinDirective)) {
198     Policy.OnlyTopDown = false;
199     Policy.OnlyBottomUp = false;
200   }
201
202   // Spilling is generally expensive on all PPC cores, so always enable
203   // register-pressure tracking.
204   Policy.ShouldTrackPressure = true;
205 }
206
207 bool PPCSubtarget::useAA() const {
208   // Use AA during code generation for the embedded cores.
209   return needsAggressiveScheduling(DarwinDirective);
210 }
211
212 bool PPCSubtarget::enableSubRegLiveness() const {
213   return UseSubRegLiveness;
214 }
215
216 bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
217 bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }