blob: d8ba1c71a0f53a9c481e2ebb3c55b3574d18c88e [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===//
Nate Begeman3bcfcd92005-08-04 07:12:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman3bcfcd92005-08-04 07:12:09 +00007//
8//===----------------------------------------------------------------------===//
9//
Evan Cheng0d639a22011-07-01 21:01:15 +000010// This file implements the PPC specific subclass of TargetSubtargetInfo.
Nate Begeman3bcfcd92005-08-04 07:12:09 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000014#include "PPCSubtarget.h"
15#include "PPC.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "PPCRegisterInfo.h"
Hal Finkela0014a52013-07-15 22:29:40 +000017#include "llvm/CodeGen/MachineFunction.h"
Hal Finkel21442b22013-09-11 23:05:25 +000018#include "llvm/CodeGen/MachineScheduler.h"
Hal Finkela0014a52013-07-15 22:29:40 +000019#include "llvm/IR/Attributes.h"
Hal Finkela0014a52013-07-15 22:29:40 +000020#include "llvm/IR/Function.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/IR/GlobalValue.h"
Hal Finkel5ff00b42015-01-09 02:03:11 +000022#include "llvm/Support/CommandLine.h"
Hal Finkel59b0ee82012-06-12 03:03:13 +000023#include "llvm/Support/Host.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000024#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Target/TargetMachine.h"
Dan Gohman906152a2009-01-05 17:59:02 +000026#include <cstdlib>
Evan Cheng54b68e32011-07-01 20:45:01 +000027
Chandler Carruthd174b722014-04-22 02:03:14 +000028using namespace llvm;
29
Chandler Carruthe96dd892014-04-21 22:55:11 +000030#define DEBUG_TYPE "ppc-subtarget"
31
Evan Cheng54b68e32011-07-01 20:45:01 +000032#define GET_SUBTARGETINFO_TARGET_DESC
Evan Cheng4d1ca962011-07-08 01:53:10 +000033#define GET_SUBTARGETINFO_CTOR
Evan Chengc9c090d2011-07-01 22:36:09 +000034#include "PPCGenSubtargetInfo.inc"
Evan Cheng54b68e32011-07-01 20:45:01 +000035
Hal Finkel5ff00b42015-01-09 02:03:11 +000036static cl::opt<bool> UseSubRegLiveness("ppc-track-subreg-liveness",
37cl::desc("Enable subregister liveness tracking for PPC"), cl::Hidden);
38
Eric Christopher49628bc2014-06-12 21:08:06 +000039/// Return the datalayout string of a subtarget.
Eric Christopher0ead61c2014-08-09 04:53:17 +000040static std::string getDataLayoutString(const Triple &T) {
41 bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
Eric Christopher49628bc2014-06-12 21:08:06 +000042 std::string Ret;
43
44 // Most PPC* platforms are big endian, PPC64LE is little endian.
Eric Christopher0ead61c2014-08-09 04:53:17 +000045 if (T.getArch() == Triple::ppc64le)
Eric Christopher49628bc2014-06-12 21:08:06 +000046 Ret = "e";
47 else
48 Ret = "E";
49
50 Ret += DataLayout::getManglingComponent(T);
51
52 // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
53 // pointers.
Eric Christopher0ead61c2014-08-09 04:53:17 +000054 if (!is64Bit || T.getOS() == Triple::Lv2)
Eric Christopher49628bc2014-06-12 21:08:06 +000055 Ret += "-p:32:32";
56
57 // Note, the alignment values for f64 and i64 on ppc64 in Darwin
58 // documentation are wrong; these are correct (i.e. "what gcc does").
Eric Christopher0ead61c2014-08-09 04:53:17 +000059 if (is64Bit || !T.isOSDarwin())
Eric Christopher49628bc2014-06-12 21:08:06 +000060 Ret += "-i64:64";
61 else
62 Ret += "-f64:32:64";
63
64 // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
Eric Christopher0ead61c2014-08-09 04:53:17 +000065 if (is64Bit)
Eric Christopher49628bc2014-06-12 21:08:06 +000066 Ret += "-n32:64";
67 else
68 Ret += "-n32";
69
70 return Ret;
71}
72
Eric Christopherd104c312014-06-12 20:54:11 +000073PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
74 StringRef FS) {
75 initializeEnvironment();
Eric Christopherb68e2532014-09-03 20:36:31 +000076 initSubtargetFeatures(CPU, FS);
Eric Christopherd104c312014-06-12 20:54:11 +000077 return *this;
78}
79
Evan Chengfe6e4052011-06-30 01:53:36 +000080PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
Eric Christopherf6ed33e2014-10-01 21:36:28 +000081 const std::string &FS, const PPCTargetMachine &TM)
Eric Christopher3770cf52014-08-09 04:38:56 +000082 : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT),
Eric Christopher0ead61c2014-08-09 04:53:17 +000083 DL(getDataLayoutString(TargetTriple)),
Eric Christopher3770cf52014-08-09 04:38:56 +000084 IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
85 TargetTriple.getArch() == Triple::ppc64le),
Eric Christophereb6e3bb2014-10-01 21:05:35 +000086 TargetABI(PPC_ABI_UNKNOWN),
Eric Christopher0ead61c2014-08-09 04:53:17 +000087 FrameLowering(initializeSubtargetDependencies(CPU, FS)), InstrInfo(*this),
Eric Christopher79cc1e32014-09-02 22:28:02 +000088 TLInfo(TM), TSInfo(&DL) {}
Eric Christopherb9fd9ed2014-08-07 22:02:54 +000089
Hal Finkela0014a52013-07-15 22:29:40 +000090void PPCSubtarget::initializeEnvironment() {
91 StackAlignment = 16;
92 DarwinDirective = PPC::DIR_NONE;
93 HasMFOCRF = false;
94 Has64BitSupport = false;
95 Use64BitRegs = false;
Hal Finkel940ab932014-02-28 00:27:01 +000096 UseCRBits = false;
Hal Finkela0014a52013-07-15 22:29:40 +000097 HasAltivec = false;
Joerg Sonnenberger39f095a2014-08-07 12:18:21 +000098 HasSPE = false;
Hal Finkela0014a52013-07-15 22:29:40 +000099 HasQPX = false;
Hal Finkel27774d92014-03-13 07:58:58 +0000100 HasVSX = false;
Bill Schmidtdcce0232014-10-10 17:21:15 +0000101 HasP8Vector = false;
Hal Finkeldbc78e12013-08-19 05:01:02 +0000102 HasFCPSGN = false;
Hal Finkela0014a52013-07-15 22:29:40 +0000103 HasFSQRT = false;
104 HasFRE = false;
105 HasFRES = false;
106 HasFRSQRTE = false;
107 HasFRSQRTES = false;
108 HasRecipPrec = false;
109 HasSTFIWX = false;
110 HasLFIWAX = false;
111 HasFPRND = false;
112 HasFPCVT = false;
113 HasISEL = false;
114 HasPOPCNTD = false;
Hal Finkel4edc66b2015-01-03 01:16:37 +0000115 HasCMPB = false;
Hal Finkela0014a52013-07-15 22:29:40 +0000116 HasLDBRX = false;
117 IsBookE = false;
Hal Finkelfe3368c2014-10-02 22:34:22 +0000118 HasOnlyMSYNC = false;
Joerg Sonnenberger0b2ebcb2014-08-04 15:47:38 +0000119 IsPPC4xx = false;
Joerg Sonnenberger74052102014-08-04 17:07:41 +0000120 IsPPC6xx = false;
Joerg Sonnenberger0b2ebcb2014-08-04 15:47:38 +0000121 IsE500 = false;
Hal Finkel0096dbd2013-09-12 14:40:06 +0000122 DeprecatedMFTB = false;
123 DeprecatedDST = false;
Hal Finkela0014a52013-07-15 22:29:40 +0000124 HasLazyResolverStubs = false;
Hal Finkela0014a52013-07-15 22:29:40 +0000125}
126
Eric Christopherb68e2532014-09-03 20:36:31 +0000127void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
Jim Laskey19058c32005-09-01 21:38:21 +0000128 // Determine default and user specified characteristics
Evan Chengfe6e4052011-06-30 01:53:36 +0000129 std::string CPUName = CPU;
130 if (CPUName.empty())
131 CPUName = "generic";
Hal Finkel59b0ee82012-06-12 03:03:13 +0000132#if (defined(__APPLE__) || defined(__linux__)) && \
133 (defined(__ppc__) || defined(__powerpc__))
Evan Chengfe6e4052011-06-30 01:53:36 +0000134 if (CPUName == "generic")
Hal Finkel59b0ee82012-06-12 03:03:13 +0000135 CPUName = sys::getHostCPUName();
Jim Laskey19058c32005-09-01 21:38:21 +0000136#endif
Jim Laskeya2b52352005-10-26 17:30:34 +0000137
Evan Cheng54b68e32011-07-01 20:45:01 +0000138 // Initialize scheduling itinerary for the specified CPU.
139 InstrItins = getInstrItineraryForCPU(CPUName);
140
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000141 // Parse features string.
Eric Christopher36448af2014-10-01 20:38:26 +0000142 ParseSubtargetFeatures(CPUName, FS);
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000143
Chris Lattner16682ff2006-06-16 17:50:12 +0000144 // If the user requested use of 64-bit regs, but the cpu selected doesn't
Dale Johannesen2e019122008-02-15 18:40:53 +0000145 // support it, ignore.
Eric Christopher36448af2014-10-01 20:38:26 +0000146 if (IsPPC64 && has64BitSupport())
147 Use64BitRegs = true;
Chris Lattnerf4646a72006-12-11 23:22:45 +0000148
149 // Set up darwin-specific properties.
Chris Lattnere6555212009-08-11 22:49:34 +0000150 if (isDarwin())
Chris Lattnerf4646a72006-12-11 23:22:45 +0000151 HasLazyResolverStubs = true;
Hal Finkele1df9092013-01-30 23:43:27 +0000152
153 // QPX requires a 32-byte aligned stack. Note that we need to do this if
154 // we're compiling for a BG/Q system regardless of whether or not QPX
155 // is enabled because external functions will assume this alignment.
156 if (hasQPX() || isBGQ())
157 StackAlignment = 32;
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000158
159 // Determine endianness.
160 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
Bill Schmidt82f9e8a2014-06-05 16:21:13 +0000161
Ulrich Weigand90a5de82014-07-28 13:09:28 +0000162 // Determine default ABI.
163 if (TargetABI == PPC_ABI_UNKNOWN) {
164 if (!isDarwin() && IsPPC64) {
165 if (IsLittleEndian)
166 TargetABI = PPC_ABI_ELFv2;
167 else
168 TargetABI = PPC_ABI_ELFv1;
169 }
170 }
Chris Lattnerf4646a72006-12-11 23:22:45 +0000171}
172
Chris Lattnerf4646a72006-12-11 23:22:45 +0000173/// hasLazyResolverStub - Return true if accesses to the specified global have
174/// to go through a dyld lazy resolution stub. This means that an extra load
175/// is required to get the address of the global.
Daniel Dunbar31b44e82009-08-02 22:11:08 +0000176bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
177 const TargetMachine &TM) const {
Chris Lattneredb9d842010-11-15 02:46:57 +0000178 // We never have stubs if HasLazyResolverStubs=false or if in static mode.
Chris Lattnerf4646a72006-12-11 23:22:45 +0000179 if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
180 return false;
Rafael Espindola246c4fb2014-11-01 16:46:18 +0000181 bool isDecl = GV->isDeclaration();
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000182 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
183 return false;
Chris Lattnerf4646a72006-12-11 23:22:45 +0000184 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000185 GV->hasCommonLinkage() || isDecl;
Nate Begeman3bcfcd92005-08-04 07:12:09 +0000186}
Hal Finkel58ca3602011-12-02 04:58:02 +0000187
Hal Finkel42daeae2013-11-30 20:55:12 +0000188// Embedded cores need aggressive scheduling (and some others also benefit).
Hal Finkel21442b22013-09-11 23:05:25 +0000189static bool needsAggressiveScheduling(unsigned Directive) {
190 switch (Directive) {
191 default: return false;
192 case PPC::DIR_440:
193 case PPC::DIR_A2:
194 case PPC::DIR_E500mc:
195 case PPC::DIR_E5500:
Hal Finkel42daeae2013-11-30 20:55:12 +0000196 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000197 case PPC::DIR_PWR8:
Hal Finkel21442b22013-09-11 23:05:25 +0000198 return true;
199 }
200}
201
202bool PPCSubtarget::enableMachineScheduler() const {
203 // Enable MI scheduling for the embedded cores.
204 // FIXME: Enable this for all cores (some additional modeling
205 // may be necessary).
206 return needsAggressiveScheduling(DarwinDirective);
207}
208
Sanjay Patela2f658d2014-07-15 22:39:58 +0000209// This overrides the PostRAScheduler bit in the SchedModel for each CPU.
210bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
211
212PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
213 return TargetSubtargetInfo::ANTIDEP_ALL;
214}
215
216void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
217 CriticalPathRCs.clear();
218 CriticalPathRCs.push_back(isPPC64() ?
219 &PPC::G8RCRegClass : &PPC::GPRCRegClass);
220}
221
Hal Finkel21442b22013-09-11 23:05:25 +0000222void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
223 MachineInstr *begin,
224 MachineInstr *end,
225 unsigned NumRegionInstrs) const {
226 if (needsAggressiveScheduling(DarwinDirective)) {
227 Policy.OnlyTopDown = false;
228 Policy.OnlyBottomUp = false;
229 }
230
231 // Spilling is generally expensive on all PPC cores, so always enable
232 // register-pressure tracking.
233 Policy.ShouldTrackPressure = true;
234}
235
236bool PPCSubtarget::useAA() const {
237 // Use AA during code generation for the embedded cores.
238 return needsAggressiveScheduling(DarwinDirective);
239}
240
Hal Finkel5ff00b42015-01-09 02:03:11 +0000241bool PPCSubtarget::enableSubRegLiveness() const {
242 return UseSubRegLiveness;
243}
244