blob: 2eac0f54598804671dae0e060ab06abff0363df0 [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 Finkel59b0ee82012-06-12 03:03:13 +000022#include "llvm/Support/Host.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Target/TargetMachine.h"
Dan Gohman906152a2009-01-05 17:59:02 +000025#include <cstdlib>
Evan Cheng54b68e32011-07-01 20:45:01 +000026
Chandler Carruthd174b722014-04-22 02:03:14 +000027using namespace llvm;
28
Chandler Carruthe96dd892014-04-21 22:55:11 +000029#define DEBUG_TYPE "ppc-subtarget"
30
Evan Cheng54b68e32011-07-01 20:45:01 +000031#define GET_SUBTARGETINFO_TARGET_DESC
Evan Cheng4d1ca962011-07-08 01:53:10 +000032#define GET_SUBTARGETINFO_CTOR
Evan Chengc9c090d2011-07-01 22:36:09 +000033#include "PPCGenSubtargetInfo.inc"
Evan Cheng54b68e32011-07-01 20:45:01 +000034
Eric Christopher49628bc2014-06-12 21:08:06 +000035/// Return the datalayout string of a subtarget.
36static std::string getDataLayoutString(const PPCSubtarget &ST) {
37 const Triple &T = ST.getTargetTriple();
38
39 std::string Ret;
40
41 // Most PPC* platforms are big endian, PPC64LE is little endian.
42 if (ST.isLittleEndian())
43 Ret = "e";
44 else
45 Ret = "E";
46
47 Ret += DataLayout::getManglingComponent(T);
48
49 // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
50 // pointers.
51 if (!ST.isPPC64() || T.getOS() == Triple::Lv2)
52 Ret += "-p:32:32";
53
54 // Note, the alignment values for f64 and i64 on ppc64 in Darwin
55 // documentation are wrong; these are correct (i.e. "what gcc does").
56 if (ST.isPPC64() || ST.isSVR4ABI())
57 Ret += "-i64:64";
58 else
59 Ret += "-f64:32:64";
60
61 // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
62 if (ST.isPPC64())
63 Ret += "-n32:64";
64 else
65 Ret += "-n32";
66
67 return Ret;
68}
69
Eric Christopherd104c312014-06-12 20:54:11 +000070PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
71 StringRef FS) {
72 initializeEnvironment();
73 resetSubtargetFeatures(CPU, FS);
74 return *this;
75}
76
Evan Chengfe6e4052011-06-30 01:53:36 +000077PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
Eric Christopherf8c031f2014-06-12 22:50:10 +000078 const std::string &FS, PPCTargetMachine &TM,
79 bool is64Bit, CodeGenOpt::Level OptLevel)
Eric Christopherd1309ee2014-05-13 20:49:08 +000080 : PPCGenSubtargetInfo(TT, CPU, FS), IsPPC64(is64Bit), TargetTriple(TT),
Ulrich Weigand90a5de82014-07-28 13:09:28 +000081 OptLevel(OptLevel), TargetABI(PPC_ABI_UNKNOWN),
Eric Christopher49628bc2014-06-12 21:08:06 +000082 FrameLowering(initializeSubtargetDependencies(CPU, FS)),
Eric Christopherf8c031f2014-06-12 22:50:10 +000083 DL(getDataLayoutString(*this)), InstrInfo(*this), JITInfo(*this),
Eric Christopher02ae6902014-06-12 23:02:32 +000084 TLInfo(TM), TSInfo(&DL) {}
Chris Lattner983a4152005-08-05 22:05:03 +000085
Hal Finkela0014a52013-07-15 22:29:40 +000086/// SetJITMode - This is called to inform the subtarget info that we are
87/// producing code for the JIT.
88void PPCSubtarget::SetJITMode() {
89 // JIT mode doesn't want lazy resolver stubs, it knows exactly where
90 // everything is. This matters for PPC64, which codegens in PIC mode without
91 // stubs.
92 HasLazyResolverStubs = false;
93
94 // Calls to external functions need to use indirect calls
95 IsJITCodeModel = true;
96}
97
98void PPCSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
99 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
100 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
101 "target-cpu");
102 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
103 "target-features");
104 std::string CPU =
105 !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
106 std::string FS =
107 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
108 if (!FS.empty()) {
109 initializeEnvironment();
110 resetSubtargetFeatures(CPU, FS);
111 }
112}
113
114void PPCSubtarget::initializeEnvironment() {
115 StackAlignment = 16;
116 DarwinDirective = PPC::DIR_NONE;
117 HasMFOCRF = false;
118 Has64BitSupport = false;
119 Use64BitRegs = false;
Hal Finkel940ab932014-02-28 00:27:01 +0000120 UseCRBits = false;
Hal Finkela0014a52013-07-15 22:29:40 +0000121 HasAltivec = false;
122 HasQPX = false;
Hal Finkel27774d92014-03-13 07:58:58 +0000123 HasVSX = false;
Hal Finkeldbc78e12013-08-19 05:01:02 +0000124 HasFCPSGN = false;
Hal Finkela0014a52013-07-15 22:29:40 +0000125 HasFSQRT = false;
126 HasFRE = false;
127 HasFRES = false;
128 HasFRSQRTE = false;
129 HasFRSQRTES = false;
130 HasRecipPrec = false;
131 HasSTFIWX = false;
132 HasLFIWAX = false;
133 HasFPRND = false;
134 HasFPCVT = false;
135 HasISEL = false;
136 HasPOPCNTD = false;
137 HasLDBRX = false;
138 IsBookE = false;
Joerg Sonnenberger0b2ebcb2014-08-04 15:47:38 +0000139 IsPPC4xx = false;
Joerg Sonnenberger74052102014-08-04 17:07:41 +0000140 IsPPC6xx = false;
Joerg Sonnenberger0b2ebcb2014-08-04 15:47:38 +0000141 IsE500 = false;
Hal Finkel0096dbd2013-09-12 14:40:06 +0000142 DeprecatedMFTB = false;
143 DeprecatedDST = false;
Hal Finkela0014a52013-07-15 22:29:40 +0000144 HasLazyResolverStubs = false;
145 IsJITCodeModel = false;
146}
147
148void PPCSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
Jim Laskey19058c32005-09-01 21:38:21 +0000149 // Determine default and user specified characteristics
Evan Chengfe6e4052011-06-30 01:53:36 +0000150 std::string CPUName = CPU;
151 if (CPUName.empty())
152 CPUName = "generic";
Hal Finkel59b0ee82012-06-12 03:03:13 +0000153#if (defined(__APPLE__) || defined(__linux__)) && \
154 (defined(__ppc__) || defined(__powerpc__))
Evan Chengfe6e4052011-06-30 01:53:36 +0000155 if (CPUName == "generic")
Hal Finkel59b0ee82012-06-12 03:03:13 +0000156 CPUName = sys::getHostCPUName();
Jim Laskey19058c32005-09-01 21:38:21 +0000157#endif
Jim Laskeya2b52352005-10-26 17:30:34 +0000158
Evan Cheng54b68e32011-07-01 20:45:01 +0000159 // Initialize scheduling itinerary for the specified CPU.
160 InstrItins = getInstrItineraryForCPU(CPUName);
161
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000162 // Make sure 64-bit features are available when CPUname is generic
163 std::string FullFS = FS;
164
Chris Lattner16682ff2006-06-16 17:50:12 +0000165 // If we are generating code for ppc64, verify that options make sense.
Hal Finkela0014a52013-07-15 22:29:40 +0000166 if (IsPPC64) {
Dale Johannesen2e019122008-02-15 18:40:53 +0000167 Has64BitSupport = true;
Chris Lattner61d70312006-06-16 20:05:06 +0000168 // Silently force 64-bit register use on ppc64.
169 Use64BitRegs = true;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000170 if (!FullFS.empty())
171 FullFS = "+64bit," + FullFS;
172 else
173 FullFS = "+64bit";
Chris Lattner16682ff2006-06-16 17:50:12 +0000174 }
Will Schmidt2247f8a2012-10-04 16:20:24 +0000175
Eric Christopherd1309ee2014-05-13 20:49:08 +0000176 // At -O2 and above, track CR bits as individual registers.
177 if (OptLevel >= CodeGenOpt::Default) {
178 if (!FullFS.empty())
179 FullFS = "+crbits," + FullFS;
180 else
181 FullFS = "+crbits";
182 }
183
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000184 // Parse features string.
185 ParseSubtargetFeatures(CPUName, FullFS);
186
Chris Lattner16682ff2006-06-16 17:50:12 +0000187 // If the user requested use of 64-bit regs, but the cpu selected doesn't
Dale Johannesen2e019122008-02-15 18:40:53 +0000188 // support it, ignore.
189 if (use64BitRegs() && !has64BitSupport())
Chris Lattner16682ff2006-06-16 17:50:12 +0000190 Use64BitRegs = false;
Chris Lattnerf4646a72006-12-11 23:22:45 +0000191
192 // Set up darwin-specific properties.
Chris Lattnere6555212009-08-11 22:49:34 +0000193 if (isDarwin())
Chris Lattnerf4646a72006-12-11 23:22:45 +0000194 HasLazyResolverStubs = true;
Hal Finkele1df9092013-01-30 23:43:27 +0000195
196 // QPX requires a 32-byte aligned stack. Note that we need to do this if
197 // we're compiling for a BG/Q system regardless of whether or not QPX
198 // is enabled because external functions will assume this alignment.
199 if (hasQPX() || isBGQ())
200 StackAlignment = 32;
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000201
202 // Determine endianness.
203 IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
Bill Schmidt82f9e8a2014-06-05 16:21:13 +0000204
205 // FIXME: For now, we disable VSX in little-endian mode until endian
206 // issues in those instructions can be addressed.
207 if (IsLittleEndian)
208 HasVSX = false;
Ulrich Weigand90a5de82014-07-28 13:09:28 +0000209
210 // Determine default ABI.
211 if (TargetABI == PPC_ABI_UNKNOWN) {
212 if (!isDarwin() && IsPPC64) {
213 if (IsLittleEndian)
214 TargetABI = PPC_ABI_ELFv2;
215 else
216 TargetABI = PPC_ABI_ELFv1;
217 }
218 }
Chris Lattnerf4646a72006-12-11 23:22:45 +0000219}
220
Chris Lattnerf4646a72006-12-11 23:22:45 +0000221/// hasLazyResolverStub - Return true if accesses to the specified global have
222/// to go through a dyld lazy resolution stub. This means that an extra load
223/// is required to get the address of the global.
Daniel Dunbar31b44e82009-08-02 22:11:08 +0000224bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
225 const TargetMachine &TM) const {
Chris Lattneredb9d842010-11-15 02:46:57 +0000226 // We never have stubs if HasLazyResolverStubs=false or if in static mode.
Chris Lattnerf4646a72006-12-11 23:22:45 +0000227 if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
228 return false;
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000229 // If symbol visibility is hidden, the extra load is not needed if
230 // the symbol is definitely defined in the current translation unit.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000231 bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000232 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
233 return false;
Chris Lattnerf4646a72006-12-11 23:22:45 +0000234 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000235 GV->hasCommonLinkage() || isDecl;
Nate Begeman3bcfcd92005-08-04 07:12:09 +0000236}
Hal Finkel58ca3602011-12-02 04:58:02 +0000237
Hal Finkel42daeae2013-11-30 20:55:12 +0000238// Embedded cores need aggressive scheduling (and some others also benefit).
Hal Finkel21442b22013-09-11 23:05:25 +0000239static bool needsAggressiveScheduling(unsigned Directive) {
240 switch (Directive) {
241 default: return false;
242 case PPC::DIR_440:
243 case PPC::DIR_A2:
244 case PPC::DIR_E500mc:
245 case PPC::DIR_E5500:
Hal Finkel42daeae2013-11-30 20:55:12 +0000246 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000247 case PPC::DIR_PWR8:
Hal Finkel21442b22013-09-11 23:05:25 +0000248 return true;
249 }
250}
251
252bool PPCSubtarget::enableMachineScheduler() const {
253 // Enable MI scheduling for the embedded cores.
254 // FIXME: Enable this for all cores (some additional modeling
255 // may be necessary).
256 return needsAggressiveScheduling(DarwinDirective);
257}
258
Sanjay Patela2f658d2014-07-15 22:39:58 +0000259// This overrides the PostRAScheduler bit in the SchedModel for each CPU.
260bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
261
262PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
263 return TargetSubtargetInfo::ANTIDEP_ALL;
264}
265
266void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
267 CriticalPathRCs.clear();
268 CriticalPathRCs.push_back(isPPC64() ?
269 &PPC::G8RCRegClass : &PPC::GPRCRegClass);
270}
271
Hal Finkel21442b22013-09-11 23:05:25 +0000272void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
273 MachineInstr *begin,
274 MachineInstr *end,
275 unsigned NumRegionInstrs) const {
276 if (needsAggressiveScheduling(DarwinDirective)) {
277 Policy.OnlyTopDown = false;
278 Policy.OnlyBottomUp = false;
279 }
280
281 // Spilling is generally expensive on all PPC cores, so always enable
282 // register-pressure tracking.
283 Policy.ShouldTrackPressure = true;
284}
285
286bool PPCSubtarget::useAA() const {
287 // Use AA during code generation for the embedded cores.
288 return needsAggressiveScheduling(DarwinDirective);
289}
290