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 std::string &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), TSInfo(TM.getDataLayout()) {} 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 HasAltivec = false; 66 HasSPE = false; 67 HasQPX = false; 68 HasVSX = false; 69 HasP8Vector = false; 70 HasP8Altivec = false; 71 HasP8Crypto = false; 72 HasFCPSGN = false; 73 HasFSQRT = false; 74 HasFRE = false; 75 HasFRES = false; 76 HasFRSQRTE = false; 77 HasFRSQRTES = false; 78 HasRecipPrec = false; 79 HasSTFIWX = false; 80 HasLFIWAX = false; 81 HasFPRND = false; 82 HasFPCVT = false; 83 HasISEL = false; 84 HasPOPCNTD = false; 85 HasCMPB = false; 86 HasLDBRX = false; 87 IsBookE = false; 88 HasOnlyMSYNC = false; 89 IsPPC4xx = false; 90 IsPPC6xx = false; 91 IsE500 = false; 92 DeprecatedMFTB = false; 93 DeprecatedDST = false; 94 HasLazyResolverStubs = false; 95 HasICBT = false; 96 HasInvariantFunctionDescriptors = false; 97 HasPartwordAtomics = false; 98 IsQPXStackUnaligned = false; 99 HasHTM = false; 100 } 101 102 void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { 103 // Determine default and user specified characteristics 104 std::string CPUName = CPU; 105 if (CPUName.empty()) { 106 // If cross-compiling with -march=ppc64le without -mcpu 107 if (TargetTriple.getArch() == Triple::ppc64le) 108 CPUName = "ppc64le"; 109 else 110 CPUName = "generic"; 111 } 112 113 // Initialize scheduling itinerary for the specified CPU. 114 InstrItins = getInstrItineraryForCPU(CPUName); 115 116 // Parse features string. 117 ParseSubtargetFeatures(CPUName, FS); 118 119 // If the user requested use of 64-bit regs, but the cpu selected doesn't 120 // support it, ignore. 121 if (IsPPC64 && has64BitSupport()) 122 Use64BitRegs = true; 123 124 // Set up darwin-specific properties. 125 if (isDarwin()) 126 HasLazyResolverStubs = true; 127 128 // QPX requires a 32-byte aligned stack. Note that we need to do this if 129 // we're compiling for a BG/Q system regardless of whether or not QPX 130 // is enabled because external functions will assume this alignment. 131 IsQPXStackUnaligned = QPXStackUnaligned; 132 StackAlignment = getPlatformStackAlignment(); 133 134 // Determine endianness. 135 // FIXME: Part of the TargetMachine. 136 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le); 137 } 138 139 /// hasLazyResolverStub - Return true if accesses to the specified global have 140 /// to go through a dyld lazy resolution stub. This means that an extra load 141 /// is required to get the address of the global. 142 bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV) const { 143 // We never have stubs if HasLazyResolverStubs=false or if in static mode. 144 if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static) 145 return false; 146 bool isDecl = GV->isDeclaration(); 147 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage()) 148 return false; 149 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() || 150 GV->hasCommonLinkage() || isDecl; 151 } 152 153 // Embedded cores need aggressive scheduling (and some others also benefit). 154 static bool needsAggressiveScheduling(unsigned Directive) { 155 switch (Directive) { 156 default: return false; 157 case PPC::DIR_440: 158 case PPC::DIR_A2: 159 case PPC::DIR_E500mc: 160 case PPC::DIR_E5500: 161 case PPC::DIR_PWR7: 162 case PPC::DIR_PWR8: 163 return true; 164 } 165 } 166 167 bool PPCSubtarget::enableMachineScheduler() const { 168 // Enable MI scheduling for the embedded cores. 169 // FIXME: Enable this for all cores (some additional modeling 170 // may be necessary). 171 return needsAggressiveScheduling(DarwinDirective); 172 } 173 174 // This overrides the PostRAScheduler bit in the SchedModel for each CPU. 175 bool PPCSubtarget::enablePostMachineScheduler() const { return true; } 176 177 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const { 178 return TargetSubtargetInfo::ANTIDEP_ALL; 179 } 180 181 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const { 182 CriticalPathRCs.clear(); 183 CriticalPathRCs.push_back(isPPC64() ? 184 &PPC::G8RCRegClass : &PPC::GPRCRegClass); 185 } 186 187 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy, 188 MachineInstr *begin, 189 MachineInstr *end, 190 unsigned NumRegionInstrs) const { 191 if (needsAggressiveScheduling(DarwinDirective)) { 192 Policy.OnlyTopDown = false; 193 Policy.OnlyBottomUp = false; 194 } 195 196 // Spilling is generally expensive on all PPC cores, so always enable 197 // register-pressure tracking. 198 Policy.ShouldTrackPressure = true; 199 } 200 201 bool PPCSubtarget::useAA() const { 202 // Use AA during code generation for the embedded cores. 203 return needsAggressiveScheduling(DarwinDirective); 204 } 205 206 bool PPCSubtarget::enableSubRegLiveness() const { 207 return UseSubRegLiveness; 208 } 209 210 bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); } 211 bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); } 212