1 //===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the PPC specific subclass of TargetSubtargetInfo. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "PPCSubtarget.h" 14 #include "PPC.h" 15 #include "PPCRegisterInfo.h" 16 #include "PPCTargetMachine.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/CommandLine.h" 23 #include "llvm/Support/TargetRegistry.h" 24 #include "llvm/Target/TargetMachine.h" 25 #include <cstdlib> 26 27 using namespace llvm; 28 29 #define DEBUG_TYPE "ppc-subtarget" 30 31 #define GET_SUBTARGETINFO_TARGET_DESC 32 #define GET_SUBTARGETINFO_CTOR 33 #include "PPCGenSubtargetInfo.inc" 34 35 static cl::opt<bool> UseSubRegLiveness("ppc-track-subreg-liveness", 36 cl::desc("Enable subregister liveness tracking for PPC"), cl::Hidden); 37 38 static cl::opt<bool> QPXStackUnaligned("qpx-stack-unaligned", 39 cl::desc("Even when QPX is enabled the stack is not 32-byte aligned"), 40 cl::Hidden); 41 42 static cl::opt<bool> 43 EnableMachinePipeliner("ppc-enable-pipeliner", 44 cl::desc("Enable Machine Pipeliner for PPC"), 45 cl::init(false), cl::Hidden); 46 47 PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU, 48 StringRef FS) { 49 initializeEnvironment(); 50 initSubtargetFeatures(CPU, FS); 51 return *this; 52 } 53 54 PPCSubtarget::PPCSubtarget(const Triple &TT, const std::string &CPU, 55 const std::string &FS, const PPCTargetMachine &TM) 56 : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT), 57 IsPPC64(TargetTriple.getArch() == Triple::ppc64 || 58 TargetTriple.getArch() == Triple::ppc64le), 59 TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, FS)), 60 InstrInfo(*this), TLInfo(TM, *this) {} 61 62 void PPCSubtarget::initializeEnvironment() { 63 StackAlignment = Align(16); 64 CPUDirective = PPC::DIR_NONE; 65 HasMFOCRF = false; 66 Has64BitSupport = false; 67 Use64BitRegs = false; 68 UseCRBits = false; 69 HasHardFloat = false; 70 HasAltivec = false; 71 HasSPE = false; 72 HasFPU = false; 73 HasQPX = false; 74 HasVSX = false; 75 NeedsTwoConstNR = false; 76 HasP8Vector = false; 77 HasP8Altivec = false; 78 HasP8Crypto = false; 79 HasP9Vector = false; 80 HasP9Altivec = false; 81 HasPrefixInstrs = false; 82 HasPCRelativeMemops = false; 83 HasFCPSGN = false; 84 HasFSQRT = false; 85 HasFRE = false; 86 HasFRES = false; 87 HasFRSQRTE = false; 88 HasFRSQRTES = false; 89 HasRecipPrec = false; 90 HasSTFIWX = false; 91 HasLFIWAX = false; 92 HasFPRND = false; 93 HasFPCVT = false; 94 HasISEL = false; 95 HasBPERMD = false; 96 HasExtDiv = false; 97 HasCMPB = false; 98 HasLDBRX = false; 99 IsBookE = false; 100 HasOnlyMSYNC = false; 101 IsPPC4xx = false; 102 IsPPC6xx = false; 103 IsE500 = false; 104 FeatureMFTB = false; 105 AllowsUnalignedFPAccess = false; 106 DeprecatedDST = false; 107 HasICBT = false; 108 HasInvariantFunctionDescriptors = false; 109 HasPartwordAtomics = false; 110 HasDirectMove = false; 111 IsQPXStackUnaligned = false; 112 HasHTM = false; 113 HasFloat128 = false; 114 IsISA3_0 = false; 115 UseLongCalls = false; 116 SecurePlt = false; 117 VectorsUseTwoUnits = false; 118 UsePPCPreRASchedStrategy = false; 119 UsePPCPostRASchedStrategy = false; 120 121 HasPOPCNTD = POPCNTD_Unavailable; 122 } 123 124 void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { 125 // Determine default and user specified characteristics 126 std::string CPUName = std::string(CPU); 127 if (CPUName.empty() || CPU == "generic") { 128 // If cross-compiling with -march=ppc64le without -mcpu 129 if (TargetTriple.getArch() == Triple::ppc64le) 130 CPUName = "ppc64le"; 131 else if (TargetTriple.getSubArch() == Triple::PPCSubArch_spe) 132 CPUName = "e500"; 133 else 134 CPUName = "generic"; 135 } 136 137 // Initialize scheduling itinerary for the specified CPU. 138 InstrItins = getInstrItineraryForCPU(CPUName); 139 140 // Parse features string. 141 ParseSubtargetFeatures(CPUName, FS); 142 143 // If the user requested use of 64-bit regs, but the cpu selected doesn't 144 // support it, ignore. 145 if (IsPPC64 && has64BitSupport()) 146 Use64BitRegs = true; 147 148 if ((TargetTriple.isOSFreeBSD() && TargetTriple.getOSMajorVersion() >= 13) || 149 TargetTriple.isOSNetBSD() || TargetTriple.isOSOpenBSD() || 150 TargetTriple.isMusl()) 151 SecurePlt = true; 152 153 if (HasSPE && IsPPC64) 154 report_fatal_error( "SPE is only supported for 32-bit targets.\n", false); 155 if (HasSPE && (HasAltivec || HasQPX || HasVSX || HasFPU)) 156 report_fatal_error( 157 "SPE and traditional floating point cannot both be enabled.\n", false); 158 159 // If not SPE, set standard FPU 160 if (!HasSPE) 161 HasFPU = true; 162 163 // QPX requires a 32-byte aligned stack. Note that we need to do this if 164 // we're compiling for a BG/Q system regardless of whether or not QPX 165 // is enabled because external functions will assume this alignment. 166 IsQPXStackUnaligned = QPXStackUnaligned; 167 StackAlignment = getPlatformStackAlignment(); 168 169 // Determine endianness. 170 // FIXME: Part of the TargetMachine. 171 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le); 172 } 173 174 bool PPCSubtarget::enableMachineScheduler() const { return true; } 175 176 bool PPCSubtarget::enableMachinePipeliner() const { 177 return (CPUDirective == PPC::DIR_PWR9) && EnableMachinePipeliner; 178 } 179 180 bool PPCSubtarget::useDFAforSMS() const { return false; } 181 182 // This overrides the PostRAScheduler bit in the SchedModel for each CPU. 183 bool PPCSubtarget::enablePostRAScheduler() const { return true; } 184 185 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const { 186 return TargetSubtargetInfo::ANTIDEP_ALL; 187 } 188 189 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const { 190 CriticalPathRCs.clear(); 191 CriticalPathRCs.push_back(isPPC64() ? 192 &PPC::G8RCRegClass : &PPC::GPRCRegClass); 193 } 194 195 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy, 196 unsigned NumRegionInstrs) const { 197 // The GenericScheduler that we use defaults to scheduling bottom up only. 198 // We want to schedule from both the top and the bottom and so we set 199 // OnlyBottomUp to false. 200 // We want to do bi-directional scheduling since it provides a more balanced 201 // schedule leading to better performance. 202 Policy.OnlyBottomUp = false; 203 // Spilling is generally expensive on all PPC cores, so always enable 204 // register-pressure tracking. 205 Policy.ShouldTrackPressure = true; 206 } 207 208 bool PPCSubtarget::useAA() const { 209 return true; 210 } 211 212 bool PPCSubtarget::enableSubRegLiveness() const { 213 return UseSubRegLiveness; 214 } 215 216 bool PPCSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const { 217 // Large code model always uses the TOC even for local symbols. 218 if (TM.getCodeModel() == CodeModel::Large) 219 return true; 220 if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) 221 return false; 222 return true; 223 } 224 225 bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); } 226 bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); } 227