Jia Liu | 31d157a | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===// |
Nate Begeman | 8c00f8c | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | 8c00f8c | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 10 | // This file implements the PPC specific subclass of TargetSubtargetInfo. |
Nate Begeman | 8c00f8c | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 2668959 | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 14 | #include "PPCSubtarget.h" |
| 15 | #include "PPC.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "PPCRegisterInfo.h" |
Hal Finkel | a44c37f | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/IR/Attributes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/GlobalValue.h" |
Hal Finkel | a44c37f | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Function.h" |
Hal Finkel | 4db738a | 2012-06-12 03:03:13 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Host.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | d68a076 | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 24 | #include <cstdlib> |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 25 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 26 | #define GET_SUBTARGETINFO_TARGET_DESC |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 27 | #define GET_SUBTARGETINFO_CTOR |
Evan Cheng | 385e930 | 2011-07-01 22:36:09 +0000 | [diff] [blame] | 28 | #include "PPCGenSubtargetInfo.inc" |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 31 | |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 32 | PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU, |
| 33 | const std::string &FS, bool is64Bit) |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 34 | : PPCGenSubtargetInfo(TT, CPU, FS) |
Chris Lattner | 7c1fb5f | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 35 | , IsPPC64(is64Bit) |
Daniel Dunbar | 869eca1 | 2011-04-19 20:54:28 +0000 | [diff] [blame] | 36 | , TargetTriple(TT) { |
Hal Finkel | a44c37f | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 37 | initializeEnvironment(); |
| 38 | resetSubtargetFeatures(CPU, FS); |
| 39 | } |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 40 | |
Hal Finkel | a44c37f | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 41 | /// SetJITMode - This is called to inform the subtarget info that we are |
| 42 | /// producing code for the JIT. |
| 43 | void PPCSubtarget::SetJITMode() { |
| 44 | // JIT mode doesn't want lazy resolver stubs, it knows exactly where |
| 45 | // everything is. This matters for PPC64, which codegens in PIC mode without |
| 46 | // stubs. |
| 47 | HasLazyResolverStubs = false; |
| 48 | |
| 49 | // Calls to external functions need to use indirect calls |
| 50 | IsJITCodeModel = true; |
| 51 | } |
| 52 | |
| 53 | void PPCSubtarget::resetSubtargetFeatures(const MachineFunction *MF) { |
| 54 | AttributeSet FnAttrs = MF->getFunction()->getAttributes(); |
| 55 | Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex, |
| 56 | "target-cpu"); |
| 57 | Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex, |
| 58 | "target-features"); |
| 59 | std::string CPU = |
| 60 | !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : ""; |
| 61 | std::string FS = |
| 62 | !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : ""; |
| 63 | if (!FS.empty()) { |
| 64 | initializeEnvironment(); |
| 65 | resetSubtargetFeatures(CPU, FS); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void PPCSubtarget::initializeEnvironment() { |
| 70 | StackAlignment = 16; |
| 71 | DarwinDirective = PPC::DIR_NONE; |
| 72 | HasMFOCRF = false; |
| 73 | Has64BitSupport = false; |
| 74 | Use64BitRegs = false; |
| 75 | HasAltivec = false; |
| 76 | HasQPX = false; |
| 77 | HasFSQRT = false; |
| 78 | HasFRE = false; |
| 79 | HasFRES = false; |
| 80 | HasFRSQRTE = false; |
| 81 | HasFRSQRTES = false; |
| 82 | HasRecipPrec = false; |
| 83 | HasSTFIWX = false; |
| 84 | HasLFIWAX = false; |
| 85 | HasFPRND = false; |
| 86 | HasFPCVT = false; |
| 87 | HasISEL = false; |
| 88 | HasPOPCNTD = false; |
| 89 | HasLDBRX = false; |
| 90 | IsBookE = false; |
| 91 | HasLazyResolverStubs = false; |
| 92 | IsJITCodeModel = false; |
| 93 | } |
| 94 | |
| 95 | void PPCSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) { |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 96 | // Determine default and user specified characteristics |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 97 | std::string CPUName = CPU; |
| 98 | if (CPUName.empty()) |
| 99 | CPUName = "generic"; |
Hal Finkel | 4db738a | 2012-06-12 03:03:13 +0000 | [diff] [blame] | 100 | #if (defined(__APPLE__) || defined(__linux__)) && \ |
| 101 | (defined(__ppc__) || defined(__powerpc__)) |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 102 | if (CPUName == "generic") |
Hal Finkel | 4db738a | 2012-06-12 03:03:13 +0000 | [diff] [blame] | 103 | CPUName = sys::getHostCPUName(); |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 104 | #endif |
Jim Laskey | 581a8f7 | 2005-10-26 17:30:34 +0000 | [diff] [blame] | 105 | |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 106 | // Initialize scheduling itinerary for the specified CPU. |
| 107 | InstrItins = getInstrItineraryForCPU(CPUName); |
| 108 | |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 109 | // Make sure 64-bit features are available when CPUname is generic |
| 110 | std::string FullFS = FS; |
| 111 | |
Chris Lattner | 7c1fb5f | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 112 | // If we are generating code for ppc64, verify that options make sense. |
Hal Finkel | a44c37f | 2013-07-15 22:29:40 +0000 | [diff] [blame] | 113 | if (IsPPC64) { |
Dale Johannesen | 3b40744 | 2008-02-15 18:40:53 +0000 | [diff] [blame] | 114 | Has64BitSupport = true; |
Chris Lattner | 8fa05da | 2006-06-16 20:05:06 +0000 | [diff] [blame] | 115 | // Silently force 64-bit register use on ppc64. |
| 116 | Use64BitRegs = true; |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 117 | if (!FullFS.empty()) |
| 118 | FullFS = "+64bit," + FullFS; |
| 119 | else |
| 120 | FullFS = "+64bit"; |
Chris Lattner | 7c1fb5f | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 121 | } |
Will Schmidt | e370919 | 2012-10-04 16:20:24 +0000 | [diff] [blame] | 122 | |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 123 | // Parse features string. |
| 124 | ParseSubtargetFeatures(CPUName, FullFS); |
| 125 | |
Chris Lattner | 7c1fb5f | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 126 | // If the user requested use of 64-bit regs, but the cpu selected doesn't |
Dale Johannesen | 3b40744 | 2008-02-15 18:40:53 +0000 | [diff] [blame] | 127 | // support it, ignore. |
| 128 | if (use64BitRegs() && !has64BitSupport()) |
Chris Lattner | 7c1fb5f | 2006-06-16 17:50:12 +0000 | [diff] [blame] | 129 | Use64BitRegs = false; |
Chris Lattner | 57fc62c | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 130 | |
| 131 | // Set up darwin-specific properties. |
Chris Lattner | 74da671 | 2009-08-11 22:49:34 +0000 | [diff] [blame] | 132 | if (isDarwin()) |
Chris Lattner | 57fc62c | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 133 | HasLazyResolverStubs = true; |
Hal Finkel | 9a79b32 | 2013-01-30 23:43:27 +0000 | [diff] [blame] | 134 | |
| 135 | // QPX requires a 32-byte aligned stack. Note that we need to do this if |
| 136 | // we're compiling for a BG/Q system regardless of whether or not QPX |
| 137 | // is enabled because external functions will assume this alignment. |
| 138 | if (hasQPX() || isBGQ()) |
| 139 | StackAlignment = 32; |
Bill Schmidt | f38cc38 | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 140 | |
| 141 | // Determine endianness. |
| 142 | IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le); |
Chris Lattner | 57fc62c | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | 57fc62c | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 145 | /// hasLazyResolverStub - Return true if accesses to the specified global have |
| 146 | /// to go through a dyld lazy resolution stub. This means that an extra load |
| 147 | /// is required to get the address of the global. |
Daniel Dunbar | 3be0340 | 2009-08-02 22:11:08 +0000 | [diff] [blame] | 148 | bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV, |
| 149 | const TargetMachine &TM) const { |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 150 | // We never have stubs if HasLazyResolverStubs=false or if in static mode. |
Chris Lattner | 57fc62c | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 151 | if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static) |
| 152 | return false; |
Evan Cheng | ae94e59 | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 153 | // If symbol visibility is hidden, the extra load is not needed if |
| 154 | // the symbol is definitely defined in the current translation unit. |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 155 | bool isDecl = GV->isDeclaration() && !GV->isMaterializable(); |
Evan Cheng | ae94e59 | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 156 | if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage()) |
| 157 | return false; |
Chris Lattner | 57fc62c | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 158 | return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() || |
Evan Cheng | ae94e59 | 2008-12-05 01:06:39 +0000 | [diff] [blame] | 159 | GV->hasCommonLinkage() || isDecl; |
Nate Begeman | 8c00f8c | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 160 | } |
Hal Finkel | 64c34e2 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 161 | |
| 162 | bool PPCSubtarget::enablePostRAScheduler( |
| 163 | CodeGenOpt::Level OptLevel, |
| 164 | TargetSubtargetInfo::AntiDepBreakMode& Mode, |
| 165 | RegClassVector& CriticalPathRCs) const { |
Hal Finkel | 01a90f4 | 2012-06-10 11:15:36 +0000 | [diff] [blame] | 166 | // FIXME: It would be best to use TargetSubtargetInfo::ANTIDEP_ALL here, |
| 167 | // but we can't because we can't reassign the cr registers. There is a |
| 168 | // dependence between the cr register and the RLWINM instruction used |
| 169 | // to extract its value which the anti-dependency breaker can't currently |
| 170 | // see. Maybe we should make a late-expanded pseudo to encode this dependency. |
| 171 | // (the relevant code is in PPCDAGToDAGISel::SelectSETCC) |
| 172 | |
| 173 | Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL; |
Hal Finkel | 64c34e2 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 174 | |
Hal Finkel | 64c34e2 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 175 | CriticalPathRCs.clear(); |
| 176 | |
| 177 | if (isPPC64()) |
| 178 | CriticalPathRCs.push_back(&PPC::G8RCRegClass); |
| 179 | else |
| 180 | CriticalPathRCs.push_back(&PPC::GPRCRegClass); |
Hal Finkel | 01a90f4 | 2012-06-10 11:15:36 +0000 | [diff] [blame] | 181 | |
| 182 | CriticalPathRCs.push_back(&PPC::F8RCRegClass); |
| 183 | CriticalPathRCs.push_back(&PPC::VRRCRegClass); |
Hal Finkel | 64c34e2 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 184 | |
| 185 | return OptLevel >= CodeGenOpt::Default; |
| 186 | } |
| 187 | |