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 resetSubtargetFeatures(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 JITInfo(*this), TLInfo(TM), TSInfo(&DL) {}
87 /// SetJITMode - This is called to inform the subtarget info that we are
88 /// producing code for the JIT.
89 void PPCSubtarget::SetJITMode() {
90 // JIT mode doesn't want lazy resolver stubs, it knows exactly where
91 // everything is. This matters for PPC64, which codegens in PIC mode without
93 HasLazyResolverStubs = false;
95 // Calls to external functions need to use indirect calls
96 IsJITCodeModel = true;
99 void PPCSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
100 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
101 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
103 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
106 !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
108 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
110 initializeEnvironment();
111 resetSubtargetFeatures(CPU, FS);
115 void PPCSubtarget::initializeEnvironment() {
117 DarwinDirective = PPC::DIR_NONE;
119 Has64BitSupport = false;
120 Use64BitRegs = false;
132 HasRecipPrec = false;
144 DeprecatedMFTB = false;
145 DeprecatedDST = false;
146 HasLazyResolverStubs = false;
147 IsJITCodeModel = false;
150 void PPCSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
151 // Determine default and user specified characteristics
152 std::string CPUName = CPU;
155 #if (defined(__APPLE__) || defined(__linux__)) && \
156 (defined(__ppc__) || defined(__powerpc__))
157 if (CPUName == "generic")
158 CPUName = sys::getHostCPUName();
161 // Initialize scheduling itinerary for the specified CPU.
162 InstrItins = getInstrItineraryForCPU(CPUName);
164 // Make sure 64-bit features are available when CPUname is generic
165 std::string FullFS = FS;
167 // If we are generating code for ppc64, verify that options make sense.
169 Has64BitSupport = true;
170 // Silently force 64-bit register use on ppc64.
173 FullFS = "+64bit," + FullFS;
178 // At -O2 and above, track CR bits as individual registers.
179 if (OptLevel >= CodeGenOpt::Default) {
181 FullFS = "+crbits," + FullFS;
186 // Parse features string.
187 ParseSubtargetFeatures(CPUName, FullFS);
189 // If the user requested use of 64-bit regs, but the cpu selected doesn't
190 // support it, ignore.
191 if (use64BitRegs() && !has64BitSupport())
192 Use64BitRegs = false;
194 // Set up darwin-specific properties.
196 HasLazyResolverStubs = true;
198 // QPX requires a 32-byte aligned stack. Note that we need to do this if
199 // we're compiling for a BG/Q system regardless of whether or not QPX
200 // is enabled because external functions will assume this alignment.
201 if (hasQPX() || isBGQ())
204 // Determine endianness.
205 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
207 // FIXME: For now, we disable VSX in little-endian mode until endian
208 // issues in those instructions can be addressed.
212 // Determine default ABI.
213 if (TargetABI == PPC_ABI_UNKNOWN) {
214 if (!isDarwin() && IsPPC64) {
216 TargetABI = PPC_ABI_ELFv2;
218 TargetABI = PPC_ABI_ELFv1;
223 /// hasLazyResolverStub - Return true if accesses to the specified global have
224 /// to go through a dyld lazy resolution stub. This means that an extra load
225 /// is required to get the address of the global.
226 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
227 const TargetMachine &TM) const {
228 // We never have stubs if HasLazyResolverStubs=false or if in static mode.
229 if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
231 // If symbol visibility is hidden, the extra load is not needed if
232 // the symbol is definitely defined in the current translation unit.
233 bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
234 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
236 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
237 GV->hasCommonLinkage() || isDecl;
240 // Embedded cores need aggressive scheduling (and some others also benefit).
241 static bool needsAggressiveScheduling(unsigned Directive) {
243 default: return false;
246 case PPC::DIR_E500mc:
254 bool PPCSubtarget::enableMachineScheduler() const {
255 // Enable MI scheduling for the embedded cores.
256 // FIXME: Enable this for all cores (some additional modeling
257 // may be necessary).
258 return needsAggressiveScheduling(DarwinDirective);
261 // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
262 bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
264 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
265 return TargetSubtargetInfo::ANTIDEP_ALL;
268 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
269 CriticalPathRCs.clear();
270 CriticalPathRCs.push_back(isPPC64() ?
271 &PPC::G8RCRegClass : &PPC::GPRCRegClass);
274 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
277 unsigned NumRegionInstrs) const {
278 if (needsAggressiveScheduling(DarwinDirective)) {
279 Policy.OnlyTopDown = false;
280 Policy.OnlyBottomUp = false;
283 // Spilling is generally expensive on all PPC cores, so always enable
284 // register-pressure tracking.
285 Policy.ShouldTrackPressure = true;
288 bool PPCSubtarget::useAA() const {
289 // Use AA during code generation for the embedded cores.
290 return needsAggressiveScheduling(DarwinDirective);