blob: 02b721b433bbd382cce48af3d52b05ac8ad8acc4 [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"
Eric Christopher8b770652015-01-26 19:03:15 +000017#include "PPCTargetMachine.h"
Hal Finkela0014a52013-07-15 22:29:40 +000018#include "llvm/CodeGen/MachineFunction.h"
Hal Finkel21442b22013-09-11 23:05:25 +000019#include "llvm/CodeGen/MachineScheduler.h"
Hal Finkela0014a52013-07-15 22:29:40 +000020#include "llvm/IR/Attributes.h"
Hal Finkela0014a52013-07-15 22:29:40 +000021#include "llvm/IR/Function.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/IR/GlobalValue.h"
Hal Finkel5ff00b42015-01-09 02:03:11 +000023#include "llvm/Support/CommandLine.h"
Hal Finkel59b0ee82012-06-12 03:03:13 +000024#include "llvm/Support/Host.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetMachine.h"
Dan Gohman906152a2009-01-05 17:59:02 +000027#include <cstdlib>
Evan Cheng54b68e32011-07-01 20:45:01 +000028
Chandler Carruthd174b722014-04-22 02:03:14 +000029using namespace llvm;
30
Chandler Carruthe96dd892014-04-21 22:55:11 +000031#define DEBUG_TYPE "ppc-subtarget"
32
Evan Cheng54b68e32011-07-01 20:45:01 +000033#define GET_SUBTARGETINFO_TARGET_DESC
Evan Cheng4d1ca962011-07-08 01:53:10 +000034#define GET_SUBTARGETINFO_CTOR
Evan Chengc9c090d2011-07-01 22:36:09 +000035#include "PPCGenSubtargetInfo.inc"
Evan Cheng54b68e32011-07-01 20:45:01 +000036
Hal Finkel5ff00b42015-01-09 02:03:11 +000037static cl::opt<bool> UseSubRegLiveness("ppc-track-subreg-liveness",
38cl::desc("Enable subregister liveness tracking for PPC"), cl::Hidden);
39
Hal Finkelc93a9a22015-02-25 01:06:45 +000040static cl::opt<bool> QPXStackUnaligned("qpx-stack-unaligned",
41 cl::desc("Even when QPX is enabled the stack is not 32-byte aligned"),
42 cl::Hidden);
43
Eric Christopherd104c312014-06-12 20:54:11 +000044PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
45 StringRef FS) {
46 initializeEnvironment();
Eric Christopherb68e2532014-09-03 20:36:31 +000047 initSubtargetFeatures(CPU, FS);
Eric Christopherd104c312014-06-12 20:54:11 +000048 return *this;
49}
50
Evan Chengfe6e4052011-06-30 01:53:36 +000051PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
Eric Christopherf6ed33e2014-10-01 21:36:28 +000052 const std::string &FS, const PPCTargetMachine &TM)
Eric Christopher3770cf52014-08-09 04:38:56 +000053 : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT),
54 IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
55 TargetTriple.getArch() == Triple::ppc64le),
Eric Christopherfee6aaf2015-02-17 06:45:15 +000056 TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, FS)),
57 InstrInfo(*this), TLInfo(TM, *this), TSInfo(TM.getDataLayout()) {}
Eric Christopherb9fd9ed2014-08-07 22:02:54 +000058
Hal Finkela0014a52013-07-15 22:29:40 +000059void PPCSubtarget::initializeEnvironment() {
60 StackAlignment = 16;
61 DarwinDirective = PPC::DIR_NONE;
62 HasMFOCRF = false;
63 Has64BitSupport = false;
64 Use64BitRegs = false;
Hal Finkel940ab932014-02-28 00:27:01 +000065 UseCRBits = false;
Hal Finkela0014a52013-07-15 22:29:40 +000066 HasAltivec = false;
Joerg Sonnenberger39f095a2014-08-07 12:18:21 +000067 HasSPE = false;
Hal Finkela0014a52013-07-15 22:29:40 +000068 HasQPX = false;
Hal Finkel27774d92014-03-13 07:58:58 +000069 HasVSX = false;
Bill Schmidtdcce0232014-10-10 17:21:15 +000070 HasP8Vector = false;
Bill Schmidtfe88b182015-02-03 21:58:23 +000071 HasP8Altivec = false;
Nemanja Ivanovice8effe12015-03-04 20:44:33 +000072 HasP8Crypto = false;
Hal Finkeldbc78e12013-08-19 05:01:02 +000073 HasFCPSGN = false;
Hal Finkela0014a52013-07-15 22:29:40 +000074 HasFSQRT = false;
75 HasFRE = false;
76 HasFRES = false;
77 HasFRSQRTE = false;
78 HasFRSQRTES = false;
79 HasRecipPrec = false;
80 HasSTFIWX = false;
81 HasLFIWAX = false;
82 HasFPRND = false;
83 HasFPCVT = false;
84 HasISEL = false;
85 HasPOPCNTD = false;
Hal Finkel4edc66b2015-01-03 01:16:37 +000086 HasCMPB = false;
Hal Finkela0014a52013-07-15 22:29:40 +000087 HasLDBRX = false;
88 IsBookE = false;
Hal Finkelfe3368c2014-10-02 22:34:22 +000089 HasOnlyMSYNC = false;
Joerg Sonnenberger0b2ebcb2014-08-04 15:47:38 +000090 IsPPC4xx = false;
Joerg Sonnenberger74052102014-08-04 17:07:41 +000091 IsPPC6xx = false;
Joerg Sonnenberger0b2ebcb2014-08-04 15:47:38 +000092 IsE500 = false;
Hal Finkel0096dbd2013-09-12 14:40:06 +000093 DeprecatedMFTB = false;
94 DeprecatedDST = false;
Hal Finkela0014a52013-07-15 22:29:40 +000095 HasLazyResolverStubs = false;
Bill Schmidt082cfc02015-01-14 20:17:10 +000096 HasICBT = false;
Hal Finkele2ab0f12015-01-15 21:17:34 +000097 HasInvariantFunctionDescriptors = false;
Hal Finkelc93a9a22015-02-25 01:06:45 +000098 IsQPXStackUnaligned = false;
Hal Finkela0014a52013-07-15 22:29:40 +000099}
100
Eric Christopherb68e2532014-09-03 20:36:31 +0000101void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
Jim Laskey19058c32005-09-01 21:38:21 +0000102 // Determine default and user specified characteristics
Evan Chengfe6e4052011-06-30 01:53:36 +0000103 std::string CPUName = CPU;
Bill Schmidt8cf15ce2015-01-29 15:59:09 +0000104 if (CPUName.empty()) {
105 // If cross-compiling with -march=ppc64le without -mcpu
106 if (TargetTriple.getArch() == Triple::ppc64le)
107 CPUName = "ppc64le";
108 else
109 CPUName = "generic";
110 }
Hal Finkel59b0ee82012-06-12 03:03:13 +0000111#if (defined(__APPLE__) || defined(__linux__)) && \
112 (defined(__ppc__) || defined(__powerpc__))
Evan Chengfe6e4052011-06-30 01:53:36 +0000113 if (CPUName == "generic")
Hal Finkel59b0ee82012-06-12 03:03:13 +0000114 CPUName = sys::getHostCPUName();
Jim Laskey19058c32005-09-01 21:38:21 +0000115#endif
Jim Laskeya2b52352005-10-26 17:30:34 +0000116
Evan Cheng54b68e32011-07-01 20:45:01 +0000117 // Initialize scheduling itinerary for the specified CPU.
118 InstrItins = getInstrItineraryForCPU(CPUName);
119
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000120 // Parse features string.
Eric Christopher36448af2014-10-01 20:38:26 +0000121 ParseSubtargetFeatures(CPUName, FS);
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000122
Chris Lattner16682ff2006-06-16 17:50:12 +0000123 // If the user requested use of 64-bit regs, but the cpu selected doesn't
Dale Johannesen2e019122008-02-15 18:40:53 +0000124 // support it, ignore.
Eric Christopher36448af2014-10-01 20:38:26 +0000125 if (IsPPC64 && has64BitSupport())
126 Use64BitRegs = true;
Chris Lattnerf4646a72006-12-11 23:22:45 +0000127
128 // Set up darwin-specific properties.
Chris Lattnere6555212009-08-11 22:49:34 +0000129 if (isDarwin())
Chris Lattnerf4646a72006-12-11 23:22:45 +0000130 HasLazyResolverStubs = true;
Hal Finkele1df9092013-01-30 23:43:27 +0000131
132 // QPX requires a 32-byte aligned stack. Note that we need to do this if
133 // we're compiling for a BG/Q system regardless of whether or not QPX
134 // is enabled because external functions will assume this alignment.
Hal Finkelc93a9a22015-02-25 01:06:45 +0000135 IsQPXStackUnaligned = QPXStackUnaligned;
136 StackAlignment = getPlatformStackAlignment();
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000137
138 // Determine endianness.
Eric Christopher75dc3902015-02-17 06:45:17 +0000139 // FIXME: Part of the TargetMachine.
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000140 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
Chris Lattnerf4646a72006-12-11 23:22:45 +0000141}
142
Chris Lattnerf4646a72006-12-11 23:22:45 +0000143/// hasLazyResolverStub - Return true if accesses to the specified global have
144/// to go through a dyld lazy resolution stub. This means that an extra load
145/// is required to get the address of the global.
Eric Christophere8dbfe12015-02-13 22:23:04 +0000146bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV) const {
Chris Lattneredb9d842010-11-15 02:46:57 +0000147 // We never have stubs if HasLazyResolverStubs=false or if in static mode.
Chris Lattnerf4646a72006-12-11 23:22:45 +0000148 if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
149 return false;
Rafael Espindola246c4fb2014-11-01 16:46:18 +0000150 bool isDecl = GV->isDeclaration();
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000151 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
152 return false;
Chris Lattnerf4646a72006-12-11 23:22:45 +0000153 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000154 GV->hasCommonLinkage() || isDecl;
Nate Begeman3bcfcd92005-08-04 07:12:09 +0000155}
Hal Finkel58ca3602011-12-02 04:58:02 +0000156
Hal Finkel42daeae2013-11-30 20:55:12 +0000157// Embedded cores need aggressive scheduling (and some others also benefit).
Hal Finkel21442b22013-09-11 23:05:25 +0000158static bool needsAggressiveScheduling(unsigned Directive) {
159 switch (Directive) {
160 default: return false;
161 case PPC::DIR_440:
162 case PPC::DIR_A2:
163 case PPC::DIR_E500mc:
164 case PPC::DIR_E5500:
Hal Finkel42daeae2013-11-30 20:55:12 +0000165 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000166 case PPC::DIR_PWR8:
Hal Finkel21442b22013-09-11 23:05:25 +0000167 return true;
168 }
169}
170
171bool PPCSubtarget::enableMachineScheduler() const {
172 // Enable MI scheduling for the embedded cores.
173 // FIXME: Enable this for all cores (some additional modeling
174 // may be necessary).
175 return needsAggressiveScheduling(DarwinDirective);
176}
177
Sanjay Patela2f658d2014-07-15 22:39:58 +0000178// This overrides the PostRAScheduler bit in the SchedModel for each CPU.
179bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
180
181PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
182 return TargetSubtargetInfo::ANTIDEP_ALL;
183}
184
185void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
186 CriticalPathRCs.clear();
187 CriticalPathRCs.push_back(isPPC64() ?
188 &PPC::G8RCRegClass : &PPC::GPRCRegClass);
189}
190
Hal Finkel21442b22013-09-11 23:05:25 +0000191void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
192 MachineInstr *begin,
193 MachineInstr *end,
194 unsigned NumRegionInstrs) const {
195 if (needsAggressiveScheduling(DarwinDirective)) {
196 Policy.OnlyTopDown = false;
197 Policy.OnlyBottomUp = false;
198 }
199
200 // Spilling is generally expensive on all PPC cores, so always enable
201 // register-pressure tracking.
202 Policy.ShouldTrackPressure = true;
203}
204
205bool PPCSubtarget::useAA() const {
206 // Use AA during code generation for the embedded cores.
207 return needsAggressiveScheduling(DarwinDirective);
208}
209
Hal Finkel5ff00b42015-01-09 02:03:11 +0000210bool PPCSubtarget::enableSubRegLiveness() const {
211 return UseSubRegLiveness;
212}
213
Eric Christopherfee6aaf2015-02-17 06:45:15 +0000214bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
215bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }