Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===// |
Nate Begeman | 3bcfcd9 | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Nate Begeman | 3bcfcd9 | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 10 | // This file implements the PPC specific subclass of TargetSubtargetInfo. |
Nate Begeman | 3bcfcd9 | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | bfca1ab | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 14 | #include "PPCSubtarget.h" |
| 15 | #include "PPC.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "PPCRegisterInfo.h" |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
Hal Finkel | 21442b2 | 2013-09-11 23:05:25 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineScheduler.h" |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Attributes.h" |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Function.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/IR/GlobalValue.h" |
Hal Finkel | 59b0ee8 | 2012-06-12 03:03:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Host.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 906152a | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 25 | #include <cstdlib> |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 26 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "ppc-subtarget" |
| 30 | |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 31 | #define GET_SUBTARGETINFO_TARGET_DESC |
Evan Cheng | 4d1ca96 | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 32 | #define GET_SUBTARGETINFO_CTOR |
Evan Cheng | c9c090d | 2011-07-01 22:36:09 +0000 | [diff] [blame] | 33 | #include "PPCGenSubtargetInfo.inc" |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 34 | |
Eric Christopher | 49628bc | 2014-06-12 21:08:06 +0000 | [diff] [blame] | 35 | /// Return the datalayout string of a subtarget. |
Eric Christopher | 0ead61c | 2014-08-09 04:53:17 +0000 | [diff] [blame] | 36 | static std::string getDataLayoutString(const Triple &T) { |
| 37 | bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le; |
Eric Christopher | 49628bc | 2014-06-12 21:08:06 +0000 | [diff] [blame] | 38 | std::string Ret; |
| 39 | |
| 40 | // Most PPC* platforms are big endian, PPC64LE is little endian. |
Eric Christopher | 0ead61c | 2014-08-09 04:53:17 +0000 | [diff] [blame] | 41 | if (T.getArch() == Triple::ppc64le) |
Eric Christopher | 49628bc | 2014-06-12 21:08:06 +0000 | [diff] [blame] | 42 | Ret = "e"; |
| 43 | else |
| 44 | Ret = "E"; |
| 45 | |
| 46 | Ret += DataLayout::getManglingComponent(T); |
| 47 | |
| 48 | // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit |
| 49 | // pointers. |
Eric Christopher | 0ead61c | 2014-08-09 04:53:17 +0000 | [diff] [blame] | 50 | if (!is64Bit || T.getOS() == Triple::Lv2) |
Eric Christopher | 49628bc | 2014-06-12 21:08:06 +0000 | [diff] [blame] | 51 | Ret += "-p:32:32"; |
| 52 | |
| 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"). |
Eric Christopher | 0ead61c | 2014-08-09 04:53:17 +0000 | [diff] [blame] | 55 | if (is64Bit || !T.isOSDarwin()) |
Eric Christopher | 49628bc | 2014-06-12 21:08:06 +0000 | [diff] [blame] | 56 | Ret += "-i64:64"; |
| 57 | else |
| 58 | Ret += "-f64:32:64"; |
| 59 | |
| 60 | // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones. |
Eric Christopher | 0ead61c | 2014-08-09 04:53:17 +0000 | [diff] [blame] | 61 | if (is64Bit) |
Eric Christopher | 49628bc | 2014-06-12 21:08:06 +0000 | [diff] [blame] | 62 | Ret += "-n32:64"; |
| 63 | else |
| 64 | Ret += "-n32"; |
| 65 | |
| 66 | return Ret; |
| 67 | } |
| 68 | |
Eric Christopher | d104c31 | 2014-06-12 20:54:11 +0000 | [diff] [blame] | 69 | PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU, |
| 70 | StringRef FS) { |
| 71 | initializeEnvironment(); |
Eric Christopher | b68e253 | 2014-09-03 20:36:31 +0000 | [diff] [blame] | 72 | initSubtargetFeatures(CPU, FS); |
Eric Christopher | d104c31 | 2014-06-12 20:54:11 +0000 | [diff] [blame] | 73 | return *this; |
| 74 | } |
| 75 | |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 76 | PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU, |
Eric Christopher | f8c031f | 2014-06-12 22:50:10 +0000 | [diff] [blame] | 77 | const std::string &FS, PPCTargetMachine &TM, |
Eric Christopher | 3770cf5 | 2014-08-09 04:38:56 +0000 | [diff] [blame] | 78 | CodeGenOpt::Level OptLevel) |
| 79 | : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT), |
Eric Christopher | 0ead61c | 2014-08-09 04:53:17 +0000 | [diff] [blame] | 80 | DL(getDataLayoutString(TargetTriple)), |
Eric Christopher | 3770cf5 | 2014-08-09 04:38:56 +0000 | [diff] [blame] | 81 | IsPPC64(TargetTriple.getArch() == Triple::ppc64 || |
| 82 | TargetTriple.getArch() == Triple::ppc64le), |
Ulrich Weigand | 90a5de8 | 2014-07-28 13:09:28 +0000 | [diff] [blame] | 83 | OptLevel(OptLevel), TargetABI(PPC_ABI_UNKNOWN), |
Eric Christopher | 0ead61c | 2014-08-09 04:53:17 +0000 | [diff] [blame] | 84 | FrameLowering(initializeSubtargetDependencies(CPU, FS)), InstrInfo(*this), |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 85 | TLInfo(TM), TSInfo(&DL) {} |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 86 | |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 87 | void PPCSubtarget::initializeEnvironment() { |
| 88 | StackAlignment = 16; |
| 89 | DarwinDirective = PPC::DIR_NONE; |
| 90 | HasMFOCRF = false; |
| 91 | Has64BitSupport = false; |
| 92 | Use64BitRegs = false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 93 | UseCRBits = false; |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 94 | HasAltivec = false; |
Joerg Sonnenberger | 39f095a | 2014-08-07 12:18:21 +0000 | [diff] [blame] | 95 | HasSPE = false; |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 96 | HasQPX = false; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 97 | HasVSX = false; |
Hal Finkel | dbc78e1 | 2013-08-19 05:01:02 +0000 | [diff] [blame] | 98 | HasFCPSGN = false; |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 99 | HasFSQRT = false; |
| 100 | HasFRE = false; |
| 101 | HasFRES = false; |
| 102 | HasFRSQRTE = false; |
| 103 | HasFRSQRTES = false; |
| 104 | HasRecipPrec = false; |
| 105 | HasSTFIWX = false; |
| 106 | HasLFIWAX = false; |
| 107 | HasFPRND = false; |
| 108 | HasFPCVT = false; |
| 109 | HasISEL = false; |
| 110 | HasPOPCNTD = false; |
| 111 | HasLDBRX = false; |
| 112 | IsBookE = false; |
Joerg Sonnenberger | 0b2ebcb | 2014-08-04 15:47:38 +0000 | [diff] [blame] | 113 | IsPPC4xx = false; |
Joerg Sonnenberger | 7405210 | 2014-08-04 17:07:41 +0000 | [diff] [blame] | 114 | IsPPC6xx = false; |
Joerg Sonnenberger | 0b2ebcb | 2014-08-04 15:47:38 +0000 | [diff] [blame] | 115 | IsE500 = false; |
Hal Finkel | 0096dbd | 2013-09-12 14:40:06 +0000 | [diff] [blame] | 116 | DeprecatedMFTB = false; |
| 117 | DeprecatedDST = false; |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 118 | HasLazyResolverStubs = false; |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Eric Christopher | b68e253 | 2014-09-03 20:36:31 +0000 | [diff] [blame] | 121 | void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { |
Jim Laskey | 19058c3 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 122 | // Determine default and user specified characteristics |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 123 | std::string CPUName = CPU; |
| 124 | if (CPUName.empty()) |
| 125 | CPUName = "generic"; |
Hal Finkel | 59b0ee8 | 2012-06-12 03:03:13 +0000 | [diff] [blame] | 126 | #if (defined(__APPLE__) || defined(__linux__)) && \ |
| 127 | (defined(__ppc__) || defined(__powerpc__)) |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 128 | if (CPUName == "generic") |
Hal Finkel | 59b0ee8 | 2012-06-12 03:03:13 +0000 | [diff] [blame] | 129 | CPUName = sys::getHostCPUName(); |
Jim Laskey | 19058c3 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 130 | #endif |
Jim Laskey | a2b5235 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 131 | |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 132 | // Initialize scheduling itinerary for the specified CPU. |
| 133 | InstrItins = getInstrItineraryForCPU(CPUName); |
| 134 | |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 135 | // Make sure 64-bit features are available when CPUname is generic |
| 136 | std::string FullFS = FS; |
| 137 | |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 138 | // If we are generating code for ppc64, verify that options make sense. |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 139 | if (IsPPC64) { |
Dale Johannesen | 2e01912 | 2008-02-15 18:40:53 +0000 | [diff] [blame] | 140 | Has64BitSupport = true; |
Chris Lattner | 61d7031 | 2006-06-16 20:05:06 +0000 | [diff] [blame] | 141 | // Silently force 64-bit register use on ppc64. |
| 142 | Use64BitRegs = true; |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 143 | if (!FullFS.empty()) |
| 144 | FullFS = "+64bit," + FullFS; |
| 145 | else |
| 146 | FullFS = "+64bit"; |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 147 | } |
Will Schmidt | 2247f8a | 2012-10-04 16:20:24 +0000 | [diff] [blame] | 148 | |
Eric Christopher | d1309ee | 2014-05-13 20:49:08 +0000 | [diff] [blame] | 149 | // At -O2 and above, track CR bits as individual registers. |
| 150 | if (OptLevel >= CodeGenOpt::Default) { |
| 151 | if (!FullFS.empty()) |
| 152 | FullFS = "+crbits," + FullFS; |
| 153 | else |
| 154 | FullFS = "+crbits"; |
| 155 | } |
| 156 | |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 157 | // Parse features string. |
| 158 | ParseSubtargetFeatures(CPUName, FullFS); |
| 159 | |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 160 | // If the user requested use of 64-bit regs, but the cpu selected doesn't |
Dale Johannesen | 2e01912 | 2008-02-15 18:40:53 +0000 | [diff] [blame] | 161 | // support it, ignore. |
| 162 | if (use64BitRegs() && !has64BitSupport()) |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 163 | Use64BitRegs = false; |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 164 | |
| 165 | // Set up darwin-specific properties. |
Chris Lattner | e655521 | 2009-08-11 22:49:34 +0000 | [diff] [blame] | 166 | if (isDarwin()) |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 167 | HasLazyResolverStubs = true; |
Hal Finkel | e1df909 | 2013-01-30 23:43:27 +0000 | [diff] [blame] | 168 | |
| 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()) |
| 173 | StackAlignment = 32; |
Bill Schmidt | 0a9170d | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 174 | |
| 175 | // Determine endianness. |
| 176 | IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le); |
Bill Schmidt | 82f9e8a | 2014-06-05 16:21:13 +0000 | [diff] [blame] | 177 | |
| 178 | // FIXME: For now, we disable VSX in little-endian mode until endian |
| 179 | // issues in those instructions can be addressed. |
| 180 | if (IsLittleEndian) |
| 181 | HasVSX = false; |
Ulrich Weigand | 90a5de8 | 2014-07-28 13:09:28 +0000 | [diff] [blame] | 182 | |
| 183 | // Determine default ABI. |
| 184 | if (TargetABI == PPC_ABI_UNKNOWN) { |
| 185 | if (!isDarwin() && IsPPC64) { |
| 186 | if (IsLittleEndian) |
| 187 | TargetABI = PPC_ABI_ELFv2; |
| 188 | else |
| 189 | TargetABI = PPC_ABI_ELFv1; |
| 190 | } |
| 191 | } |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 31b44e8 | 2009-08-02 22:11:08 +0000 | [diff] [blame] | 197 | bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV, |
| 198 | const TargetMachine &TM) const { |
Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 199 | // We never have stubs if HasLazyResolverStubs=false or if in static mode. |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 200 | if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static) |
| 201 | return false; |
Evan Cheng | 2a03c7e | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 202 | // If symbol visibility is hidden, the extra load is not needed if |
| 203 | // the symbol is definitely defined in the current translation unit. |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 204 | bool isDecl = GV->isDeclaration() && !GV->isMaterializable(); |
Evan Cheng | 2a03c7e | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 205 | if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage()) |
| 206 | return false; |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 207 | return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() || |
Evan Cheng | 2a03c7e | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 208 | GV->hasCommonLinkage() || isDecl; |
Nate Begeman | 3bcfcd9 | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 209 | } |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 210 | |
Hal Finkel | 42daeae | 2013-11-30 20:55:12 +0000 | [diff] [blame] | 211 | // Embedded cores need aggressive scheduling (and some others also benefit). |
Hal Finkel | 21442b2 | 2013-09-11 23:05:25 +0000 | [diff] [blame] | 212 | static bool needsAggressiveScheduling(unsigned Directive) { |
| 213 | switch (Directive) { |
| 214 | default: return false; |
| 215 | case PPC::DIR_440: |
| 216 | case PPC::DIR_A2: |
| 217 | case PPC::DIR_E500mc: |
| 218 | case PPC::DIR_E5500: |
Hal Finkel | 42daeae | 2013-11-30 20:55:12 +0000 | [diff] [blame] | 219 | case PPC::DIR_PWR7: |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 220 | case PPC::DIR_PWR8: |
Hal Finkel | 21442b2 | 2013-09-11 23:05:25 +0000 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | } |
| 224 | |
| 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); |
| 230 | } |
| 231 | |
Sanjay Patel | a2f658d | 2014-07-15 22:39:58 +0000 | [diff] [blame] | 232 | // This overrides the PostRAScheduler bit in the SchedModel for each CPU. |
| 233 | bool PPCSubtarget::enablePostMachineScheduler() const { return true; } |
| 234 | |
| 235 | PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const { |
| 236 | return TargetSubtargetInfo::ANTIDEP_ALL; |
| 237 | } |
| 238 | |
| 239 | void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const { |
| 240 | CriticalPathRCs.clear(); |
| 241 | CriticalPathRCs.push_back(isPPC64() ? |
| 242 | &PPC::G8RCRegClass : &PPC::GPRCRegClass); |
| 243 | } |
| 244 | |
Hal Finkel | 21442b2 | 2013-09-11 23:05:25 +0000 | [diff] [blame] | 245 | void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy, |
| 246 | MachineInstr *begin, |
| 247 | MachineInstr *end, |
| 248 | unsigned NumRegionInstrs) const { |
| 249 | if (needsAggressiveScheduling(DarwinDirective)) { |
| 250 | Policy.OnlyTopDown = false; |
| 251 | Policy.OnlyBottomUp = false; |
| 252 | } |
| 253 | |
| 254 | // Spilling is generally expensive on all PPC cores, so always enable |
| 255 | // register-pressure tracking. |
| 256 | Policy.ShouldTrackPressure = true; |
| 257 | } |
| 258 | |
| 259 | bool PPCSubtarget::useAA() const { |
| 260 | // Use AA during code generation for the embedded cores. |
| 261 | return needsAggressiveScheduling(DarwinDirective); |
| 262 | } |
| 263 | |