1 //===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PPC specific subclass of TargetSubtargetInfo.
12 //===----------------------------------------------------------------------===//
14 #include "PPCSubtarget.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"
29 #define DEBUG_TYPE "ppc-subtarget"
31 #define GET_SUBTARGETINFO_TARGET_DESC
32 #define GET_SUBTARGETINFO_CTOR
33 #include "PPCGenSubtargetInfo.inc"
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;
40 // Most PPC* platforms are big endian, PPC64LE is little endian.
41 if (T.getArch() == Triple::ppc64le)
46 Ret += DataLayout::getManglingComponent(T);
48 // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
50 if (!is64Bit || T.getOS() == Triple::Lv2)
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())
60 // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
69 PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
71 initializeEnvironment();
72 initSubtargetFeatures(CPU, FS);
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) {}
87 void PPCSubtarget::initializeEnvironment() {
89 DarwinDirective = PPC::DIR_NONE;
91 Has64BitSupport = false;
104 HasRecipPrec = false;
116 DeprecatedMFTB = false;
117 DeprecatedDST = false;
118 HasLazyResolverStubs = false;
121 void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
122 // Determine default and user specified characteristics
123 std::string CPUName = CPU;
126 #if (defined(__APPLE__) || defined(__linux__)) && \
127 (defined(__ppc__) || defined(__powerpc__))
128 if (CPUName == "generic")
129 CPUName = sys::getHostCPUName();
132 // Initialize scheduling itinerary for the specified CPU.
133 InstrItins = getInstrItineraryForCPU(CPUName);
135 // Make sure 64-bit features are available when CPUname is generic
136 std::string FullFS = FS;
138 // If we are generating code for ppc64, verify that options make sense.
140 Has64BitSupport = true;
141 // Silently force 64-bit register use on ppc64.
144 FullFS = "+64bit," + FullFS;
149 // At -O2 and above, track CR bits as individual registers.
150 if (OptLevel >= CodeGenOpt::Default) {
152 FullFS = "+crbits," + FullFS;
157 // Parse features string.
158 ParseSubtargetFeatures(CPUName, FullFS);
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;
165 // Set up darwin-specific properties.
167 HasLazyResolverStubs = true;
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())
175 // Determine endianness.
176 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
178 // FIXME: For now, we disable VSX in little-endian mode until endian
179 // issues in those instructions can be addressed.
183 // Determine default ABI.
184 if (TargetABI == PPC_ABI_UNKNOWN) {
185 if (!isDarwin() && IsPPC64) {
187 TargetABI = PPC_ABI_ELFv2;
189 TargetABI = PPC_ABI_ELFv1;
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)
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())
207 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
208 GV->hasCommonLinkage() || isDecl;
211 // Embedded cores need aggressive scheduling (and some others also benefit).
212 static bool needsAggressiveScheduling(unsigned Directive) {
214 default: return false;
217 case PPC::DIR_E500mc:
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);
232 // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
233 bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
235 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
236 return TargetSubtargetInfo::ANTIDEP_ALL;
239 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
240 CriticalPathRCs.clear();
241 CriticalPathRCs.push_back(isPPC64() ?
242 &PPC::G8RCRegClass : &PPC::GPRCRegClass);
245 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
248 unsigned NumRegionInstrs) const {
249 if (needsAggressiveScheduling(DarwinDirective)) {
250 Policy.OnlyTopDown = false;
251 Policy.OnlyBottomUp = false;
254 // Spilling is generally expensive on all PPC cores, so always enable
255 // register-pressure tracking.
256 Policy.ShouldTrackPressure = true;
259 bool PPCSubtarget::useAA() const {
260 // Use AA during code generation for the embedded cores.
261 return needsAggressiveScheduling(DarwinDirective);