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, const PPCTargetMachine &TM)
78 : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT),
79 DL(getDataLayoutString(TargetTriple)),
80 IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
81 TargetTriple.getArch() == Triple::ppc64le),
82 TargetABI(PPC_ABI_UNKNOWN),
83 FrameLowering(initializeSubtargetDependencies(CPU, FS)), InstrInfo(*this),
84 TLInfo(TM), TSInfo(&DL) {}
86 void PPCSubtarget::initializeEnvironment() {
88 DarwinDirective = PPC::DIR_NONE;
90 Has64BitSupport = false;
103 HasRecipPrec = false;
112 HasOnlyMSYNC = 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 // Parse features string.
136 ParseSubtargetFeatures(CPUName, FS);
138 // If the user requested use of 64-bit regs, but the cpu selected doesn't
139 // support it, ignore.
140 if (IsPPC64 && has64BitSupport())
143 // Set up darwin-specific properties.
145 HasLazyResolverStubs = true;
147 // QPX requires a 32-byte aligned stack. Note that we need to do this if
148 // we're compiling for a BG/Q system regardless of whether or not QPX
149 // is enabled because external functions will assume this alignment.
150 if (hasQPX() || isBGQ())
153 // Determine endianness.
154 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
156 // FIXME: For now, we disable VSX in little-endian mode until endian
157 // issues in those instructions can be addressed.
161 // Determine default ABI.
162 if (TargetABI == PPC_ABI_UNKNOWN) {
163 if (!isDarwin() && IsPPC64) {
165 TargetABI = PPC_ABI_ELFv2;
167 TargetABI = PPC_ABI_ELFv1;
172 /// hasLazyResolverStub - Return true if accesses to the specified global have
173 /// to go through a dyld lazy resolution stub. This means that an extra load
174 /// is required to get the address of the global.
175 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
176 const TargetMachine &TM) const {
177 // We never have stubs if HasLazyResolverStubs=false or if in static mode.
178 if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
180 // If symbol visibility is hidden, the extra load is not needed if
181 // the symbol is definitely defined in the current translation unit.
182 bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
183 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
185 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
186 GV->hasCommonLinkage() || isDecl;
189 // Embedded cores need aggressive scheduling (and some others also benefit).
190 static bool needsAggressiveScheduling(unsigned Directive) {
192 default: return false;
195 case PPC::DIR_E500mc:
203 bool PPCSubtarget::enableMachineScheduler() const {
204 // Enable MI scheduling for the embedded cores.
205 // FIXME: Enable this for all cores (some additional modeling
206 // may be necessary).
207 return needsAggressiveScheduling(DarwinDirective);
210 // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
211 bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
213 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
214 return TargetSubtargetInfo::ANTIDEP_ALL;
217 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
218 CriticalPathRCs.clear();
219 CriticalPathRCs.push_back(isPPC64() ?
220 &PPC::G8RCRegClass : &PPC::GPRCRegClass);
223 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
226 unsigned NumRegionInstrs) const {
227 if (needsAggressiveScheduling(DarwinDirective)) {
228 Policy.OnlyTopDown = false;
229 Policy.OnlyBottomUp = false;
232 // Spilling is generally expensive on all PPC cores, so always enable
233 // register-pressure tracking.
234 Policy.ShouldTrackPressure = true;
237 bool PPCSubtarget::useAA() const {
238 // Use AA during code generation for the embedded cores.
239 return needsAggressiveScheduling(DarwinDirective);