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 | |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 35 | PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU, |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 36 | const std::string &FS, bool is64Bit, |
| 37 | CodeGenOpt::Level OptLevel) |
Evan Cheng | 1a72add6 | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 38 | : PPCGenSubtargetInfo(TT, CPU, FS) |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 39 | , IsPPC64(is64Bit) |
Daniel Dunbar | a37aab2 | 2011-04-19 20:54:28 +0000 | [diff] [blame] | 40 | , TargetTriple(TT) { |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 41 | initializeEnvironment(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 42 | |
| 43 | std::string FullFS = FS; |
| 44 | |
| 45 | // At -O2 and above, track CR bits as individual registers. |
| 46 | if (OptLevel >= CodeGenOpt::Default) { |
| 47 | if (!FullFS.empty()) |
| 48 | FullFS = "+crbits," + FullFS; |
| 49 | else |
| 50 | FullFS = "+crbits"; |
| 51 | } |
| 52 | |
| 53 | resetSubtargetFeatures(CPU, FullFS); |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 54 | } |
Chris Lattner | 983a415 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 55 | |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 56 | /// SetJITMode - This is called to inform the subtarget info that we are |
| 57 | /// producing code for the JIT. |
| 58 | void PPCSubtarget::SetJITMode() { |
| 59 | // JIT mode doesn't want lazy resolver stubs, it knows exactly where |
| 60 | // everything is. This matters for PPC64, which codegens in PIC mode without |
| 61 | // stubs. |
| 62 | HasLazyResolverStubs = false; |
| 63 | |
| 64 | // Calls to external functions need to use indirect calls |
| 65 | IsJITCodeModel = true; |
| 66 | } |
| 67 | |
| 68 | void PPCSubtarget::resetSubtargetFeatures(const MachineFunction *MF) { |
| 69 | AttributeSet FnAttrs = MF->getFunction()->getAttributes(); |
| 70 | Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex, |
| 71 | "target-cpu"); |
| 72 | Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex, |
| 73 | "target-features"); |
| 74 | std::string CPU = |
| 75 | !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : ""; |
| 76 | std::string FS = |
| 77 | !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : ""; |
| 78 | if (!FS.empty()) { |
| 79 | initializeEnvironment(); |
| 80 | resetSubtargetFeatures(CPU, FS); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void PPCSubtarget::initializeEnvironment() { |
| 85 | StackAlignment = 16; |
| 86 | DarwinDirective = PPC::DIR_NONE; |
| 87 | HasMFOCRF = false; |
| 88 | Has64BitSupport = false; |
| 89 | Use64BitRegs = false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 90 | UseCRBits = false; |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 91 | HasAltivec = false; |
| 92 | HasQPX = false; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 93 | HasVSX = false; |
Hal Finkel | dbc78e1 | 2013-08-19 05:01:02 +0000 | [diff] [blame] | 94 | HasFCPSGN = false; |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 95 | HasFSQRT = false; |
| 96 | HasFRE = false; |
| 97 | HasFRES = false; |
| 98 | HasFRSQRTE = false; |
| 99 | HasFRSQRTES = false; |
| 100 | HasRecipPrec = false; |
| 101 | HasSTFIWX = false; |
| 102 | HasLFIWAX = false; |
| 103 | HasFPRND = false; |
| 104 | HasFPCVT = false; |
| 105 | HasISEL = false; |
| 106 | HasPOPCNTD = false; |
| 107 | HasLDBRX = false; |
| 108 | IsBookE = false; |
Hal Finkel | 0096dbd | 2013-09-12 14:40:06 +0000 | [diff] [blame] | 109 | DeprecatedMFTB = false; |
| 110 | DeprecatedDST = false; |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 111 | HasLazyResolverStubs = false; |
| 112 | IsJITCodeModel = false; |
| 113 | } |
| 114 | |
| 115 | void PPCSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) { |
Jim Laskey | 19058c3 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 116 | // Determine default and user specified characteristics |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 117 | std::string CPUName = CPU; |
| 118 | if (CPUName.empty()) |
| 119 | CPUName = "generic"; |
Hal Finkel | 59b0ee8 | 2012-06-12 03:03:13 +0000 | [diff] [blame] | 120 | #if (defined(__APPLE__) || defined(__linux__)) && \ |
| 121 | (defined(__ppc__) || defined(__powerpc__)) |
Evan Cheng | fe6e405 | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 122 | if (CPUName == "generic") |
Hal Finkel | 59b0ee8 | 2012-06-12 03:03:13 +0000 | [diff] [blame] | 123 | CPUName = sys::getHostCPUName(); |
Jim Laskey | 19058c3 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 124 | #endif |
Jim Laskey | a2b5235 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 125 | |
Evan Cheng | 54b68e3 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 126 | // Initialize scheduling itinerary for the specified CPU. |
| 127 | InstrItins = getInstrItineraryForCPU(CPUName); |
| 128 | |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 129 | // Make sure 64-bit features are available when CPUname is generic |
| 130 | std::string FullFS = FS; |
| 131 | |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 132 | // If we are generating code for ppc64, verify that options make sense. |
Hal Finkel | a0014a5 | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 133 | if (IsPPC64) { |
Dale Johannesen | 2e01912 | 2008-02-15 18:40:53 +0000 | [diff] [blame] | 134 | Has64BitSupport = true; |
Chris Lattner | 61d7031 | 2006-06-16 20:05:06 +0000 | [diff] [blame] | 135 | // Silently force 64-bit register use on ppc64. |
| 136 | Use64BitRegs = true; |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 137 | if (!FullFS.empty()) |
| 138 | FullFS = "+64bit," + FullFS; |
| 139 | else |
| 140 | FullFS = "+64bit"; |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 141 | } |
Will Schmidt | 2247f8a | 2012-10-04 16:20:24 +0000 | [diff] [blame] | 142 | |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 143 | // Parse features string. |
| 144 | ParseSubtargetFeatures(CPUName, FullFS); |
| 145 | |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 146 | // 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] | 147 | // support it, ignore. |
| 148 | if (use64BitRegs() && !has64BitSupport()) |
Chris Lattner | 16682ff | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 149 | Use64BitRegs = false; |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 150 | |
| 151 | // Set up darwin-specific properties. |
Chris Lattner | e655521 | 2009-08-11 22:49:34 +0000 | [diff] [blame] | 152 | if (isDarwin()) |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 153 | HasLazyResolverStubs = true; |
Hal Finkel | e1df909 | 2013-01-30 23:43:27 +0000 | [diff] [blame] | 154 | |
| 155 | // QPX requires a 32-byte aligned stack. Note that we need to do this if |
| 156 | // we're compiling for a BG/Q system regardless of whether or not QPX |
| 157 | // is enabled because external functions will assume this alignment. |
| 158 | if (hasQPX() || isBGQ()) |
| 159 | StackAlignment = 32; |
Bill Schmidt | 0a9170d | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 160 | |
| 161 | // Determine endianness. |
| 162 | IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le); |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 165 | /// hasLazyResolverStub - Return true if accesses to the specified global have |
| 166 | /// to go through a dyld lazy resolution stub. This means that an extra load |
| 167 | /// is required to get the address of the global. |
Daniel Dunbar | 31b44e8 | 2009-08-02 22:11:08 +0000 | [diff] [blame] | 168 | bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV, |
| 169 | const TargetMachine &TM) const { |
Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 170 | // We never have stubs if HasLazyResolverStubs=false or if in static mode. |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 171 | if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static) |
| 172 | return false; |
Evan Cheng | 2a03c7e | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 173 | // If symbol visibility is hidden, the extra load is not needed if |
| 174 | // the symbol is definitely defined in the current translation unit. |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 175 | bool isDecl = GV->isDeclaration() && !GV->isMaterializable(); |
Evan Cheng | 2a03c7e | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 176 | if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage()) |
| 177 | return false; |
Chris Lattner | f4646a7 | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 178 | return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() || |
Evan Cheng | 2a03c7e | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 179 | GV->hasCommonLinkage() || isDecl; |
Nate Begeman | 3bcfcd9 | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 180 | } |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 181 | |
| 182 | bool PPCSubtarget::enablePostRAScheduler( |
| 183 | CodeGenOpt::Level OptLevel, |
| 184 | TargetSubtargetInfo::AntiDepBreakMode& Mode, |
| 185 | RegClassVector& CriticalPathRCs) const { |
Hal Finkel | 7fe6a53 | 2013-09-12 05:24:49 +0000 | [diff] [blame] | 186 | Mode = TargetSubtargetInfo::ANTIDEP_ALL; |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 187 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 188 | CriticalPathRCs.clear(); |
| 189 | |
| 190 | if (isPPC64()) |
| 191 | CriticalPathRCs.push_back(&PPC::G8RCRegClass); |
| 192 | else |
| 193 | CriticalPathRCs.push_back(&PPC::GPRCRegClass); |
Hal Finkel | a810028 | 2012-06-10 11:15:36 +0000 | [diff] [blame] | 194 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 195 | return OptLevel >= CodeGenOpt::Default; |
| 196 | } |
| 197 | |
Hal Finkel | 42daeae | 2013-11-30 20:55:12 +0000 | [diff] [blame] | 198 | // Embedded cores need aggressive scheduling (and some others also benefit). |
Hal Finkel | 21442b2 | 2013-09-11 23:05:25 +0000 | [diff] [blame] | 199 | static bool needsAggressiveScheduling(unsigned Directive) { |
| 200 | switch (Directive) { |
| 201 | default: return false; |
| 202 | case PPC::DIR_440: |
| 203 | case PPC::DIR_A2: |
| 204 | case PPC::DIR_E500mc: |
| 205 | case PPC::DIR_E5500: |
Hal Finkel | 42daeae | 2013-11-30 20:55:12 +0000 | [diff] [blame] | 206 | case PPC::DIR_PWR7: |
Hal Finkel | 21442b2 | 2013-09-11 23:05:25 +0000 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | bool PPCSubtarget::enableMachineScheduler() const { |
| 212 | // Enable MI scheduling for the embedded cores. |
| 213 | // FIXME: Enable this for all cores (some additional modeling |
| 214 | // may be necessary). |
| 215 | return needsAggressiveScheduling(DarwinDirective); |
| 216 | } |
| 217 | |
| 218 | void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy, |
| 219 | MachineInstr *begin, |
| 220 | MachineInstr *end, |
| 221 | unsigned NumRegionInstrs) const { |
| 222 | if (needsAggressiveScheduling(DarwinDirective)) { |
| 223 | Policy.OnlyTopDown = false; |
| 224 | Policy.OnlyBottomUp = false; |
| 225 | } |
| 226 | |
| 227 | // Spilling is generally expensive on all PPC cores, so always enable |
| 228 | // register-pressure tracking. |
| 229 | Policy.ShouldTrackPressure = true; |
| 230 | } |
| 231 | |
| 232 | bool PPCSubtarget::useAA() const { |
| 233 | // Use AA during code generation for the embedded cores. |
| 234 | return needsAggressiveScheduling(DarwinDirective); |
| 235 | } |
| 236 | |