blob: b07abe4461894f4266e742f15ef3fc3c7f696057 [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
Evan Cheng54b68e32011-07-01 20:45:01 +000027#define GET_SUBTARGETINFO_TARGET_DESC
Evan Cheng4d1ca962011-07-08 01:53:10 +000028#define GET_SUBTARGETINFO_CTOR
Evan Chengc9c090d2011-07-01 22:36:09 +000029#include "PPCGenSubtargetInfo.inc"
Evan Cheng54b68e32011-07-01 20:45:01 +000030
Chris Lattner983a4152005-08-05 22:05:03 +000031using namespace llvm;
Chris Lattner983a4152005-08-05 22:05:03 +000032
Evan Chengfe6e4052011-06-30 01:53:36 +000033PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
Hal Finkel940ab932014-02-28 00:27:01 +000034 const std::string &FS, bool is64Bit,
35 CodeGenOpt::Level OptLevel)
Evan Cheng1a72add62011-07-07 07:07:08 +000036 : PPCGenSubtargetInfo(TT, CPU, FS)
Chris Lattner16682ff2006-06-16 17:50:12 +000037 , IsPPC64(is64Bit)
Daniel Dunbara37aab22011-04-19 20:54:28 +000038 , TargetTriple(TT) {
Hal Finkela0014a52013-07-15 22:29:40 +000039 initializeEnvironment();
Hal Finkel940ab932014-02-28 00:27:01 +000040
41 std::string FullFS = FS;
42
43 // At -O2 and above, track CR bits as individual registers.
44 if (OptLevel >= CodeGenOpt::Default) {
45 if (!FullFS.empty())
46 FullFS = "+crbits," + FullFS;
47 else
48 FullFS = "+crbits";
49 }
50
51 resetSubtargetFeatures(CPU, FullFS);
Hal Finkela0014a52013-07-15 22:29:40 +000052}
Chris Lattner983a4152005-08-05 22:05:03 +000053
Hal Finkela0014a52013-07-15 22:29:40 +000054/// SetJITMode - This is called to inform the subtarget info that we are
55/// producing code for the JIT.
56void PPCSubtarget::SetJITMode() {
57 // JIT mode doesn't want lazy resolver stubs, it knows exactly where
58 // everything is. This matters for PPC64, which codegens in PIC mode without
59 // stubs.
60 HasLazyResolverStubs = false;
61
62 // Calls to external functions need to use indirect calls
63 IsJITCodeModel = true;
64}
65
66void PPCSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
67 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
68 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
69 "target-cpu");
70 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
71 "target-features");
72 std::string CPU =
73 !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
74 std::string FS =
75 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
76 if (!FS.empty()) {
77 initializeEnvironment();
78 resetSubtargetFeatures(CPU, FS);
79 }
80}
81
82void PPCSubtarget::initializeEnvironment() {
83 StackAlignment = 16;
84 DarwinDirective = PPC::DIR_NONE;
85 HasMFOCRF = false;
86 Has64BitSupport = false;
87 Use64BitRegs = false;
Hal Finkel940ab932014-02-28 00:27:01 +000088 UseCRBits = false;
Hal Finkela0014a52013-07-15 22:29:40 +000089 HasAltivec = false;
90 HasQPX = false;
Hal Finkel27774d92014-03-13 07:58:58 +000091 HasVSX = false;
Hal Finkeldbc78e12013-08-19 05:01:02 +000092 HasFCPSGN = false;
Hal Finkela0014a52013-07-15 22:29:40 +000093 HasFSQRT = false;
94 HasFRE = false;
95 HasFRES = false;
96 HasFRSQRTE = false;
97 HasFRSQRTES = false;
98 HasRecipPrec = false;
99 HasSTFIWX = false;
100 HasLFIWAX = false;
101 HasFPRND = false;
102 HasFPCVT = false;
103 HasISEL = false;
104 HasPOPCNTD = false;
105 HasLDBRX = false;
106 IsBookE = false;
Hal Finkel0096dbd2013-09-12 14:40:06 +0000107 DeprecatedMFTB = false;
108 DeprecatedDST = false;
Hal Finkela0014a52013-07-15 22:29:40 +0000109 HasLazyResolverStubs = false;
110 IsJITCodeModel = false;
111}
112
113void PPCSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
Jim Laskey19058c32005-09-01 21:38:21 +0000114 // Determine default and user specified characteristics
Evan Chengfe6e4052011-06-30 01:53:36 +0000115 std::string CPUName = CPU;
116 if (CPUName.empty())
117 CPUName = "generic";
Hal Finkel59b0ee82012-06-12 03:03:13 +0000118#if (defined(__APPLE__) || defined(__linux__)) && \
119 (defined(__ppc__) || defined(__powerpc__))
Evan Chengfe6e4052011-06-30 01:53:36 +0000120 if (CPUName == "generic")
Hal Finkel59b0ee82012-06-12 03:03:13 +0000121 CPUName = sys::getHostCPUName();
Jim Laskey19058c32005-09-01 21:38:21 +0000122#endif
Jim Laskeya2b52352005-10-26 17:30:34 +0000123
Evan Cheng54b68e32011-07-01 20:45:01 +0000124 // Initialize scheduling itinerary for the specified CPU.
125 InstrItins = getInstrItineraryForCPU(CPUName);
126
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000127 // Make sure 64-bit features are available when CPUname is generic
128 std::string FullFS = FS;
129
Chris Lattner16682ff2006-06-16 17:50:12 +0000130 // If we are generating code for ppc64, verify that options make sense.
Hal Finkela0014a52013-07-15 22:29:40 +0000131 if (IsPPC64) {
Dale Johannesen2e019122008-02-15 18:40:53 +0000132 Has64BitSupport = true;
Chris Lattner61d70312006-06-16 20:05:06 +0000133 // Silently force 64-bit register use on ppc64.
134 Use64BitRegs = true;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000135 if (!FullFS.empty())
136 FullFS = "+64bit," + FullFS;
137 else
138 FullFS = "+64bit";
Chris Lattner16682ff2006-06-16 17:50:12 +0000139 }
Will Schmidt2247f8a2012-10-04 16:20:24 +0000140
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000141 // Parse features string.
142 ParseSubtargetFeatures(CPUName, FullFS);
143
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.
146 if (use64BitRegs() && !has64BitSupport())
Chris Lattner16682ff2006-06-16 17:50:12 +0000147 Use64BitRegs = false;
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);
Chris Lattnerf4646a72006-12-11 23:22:45 +0000161}
162
Chris Lattnerf4646a72006-12-11 23:22:45 +0000163/// hasLazyResolverStub - Return true if accesses to the specified global have
164/// to go through a dyld lazy resolution stub. This means that an extra load
165/// is required to get the address of the global.
Daniel Dunbar31b44e82009-08-02 22:11:08 +0000166bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
167 const TargetMachine &TM) const {
Chris Lattneredb9d842010-11-15 02:46:57 +0000168 // We never have stubs if HasLazyResolverStubs=false or if in static mode.
Chris Lattnerf4646a72006-12-11 23:22:45 +0000169 if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
170 return false;
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000171 // If symbol visibility is hidden, the extra load is not needed if
172 // the symbol is definitely defined in the current translation unit.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000173 bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000174 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
175 return false;
Chris Lattnerf4646a72006-12-11 23:22:45 +0000176 return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000177 GV->hasCommonLinkage() || isDecl;
Nate Begeman3bcfcd92005-08-04 07:12:09 +0000178}
Hal Finkel58ca3602011-12-02 04:58:02 +0000179
180bool PPCSubtarget::enablePostRAScheduler(
181 CodeGenOpt::Level OptLevel,
182 TargetSubtargetInfo::AntiDepBreakMode& Mode,
183 RegClassVector& CriticalPathRCs) const {
Hal Finkel7fe6a532013-09-12 05:24:49 +0000184 Mode = TargetSubtargetInfo::ANTIDEP_ALL;
Hal Finkel58ca3602011-12-02 04:58:02 +0000185
Hal Finkel58ca3602011-12-02 04:58:02 +0000186 CriticalPathRCs.clear();
187
188 if (isPPC64())
189 CriticalPathRCs.push_back(&PPC::G8RCRegClass);
190 else
191 CriticalPathRCs.push_back(&PPC::GPRCRegClass);
Hal Finkela8100282012-06-10 11:15:36 +0000192
Hal Finkel58ca3602011-12-02 04:58:02 +0000193 return OptLevel >= CodeGenOpt::Default;
194}
195
Hal Finkel42daeae2013-11-30 20:55:12 +0000196// Embedded cores need aggressive scheduling (and some others also benefit).
Hal Finkel21442b22013-09-11 23:05:25 +0000197static bool needsAggressiveScheduling(unsigned Directive) {
198 switch (Directive) {
199 default: return false;
200 case PPC::DIR_440:
201 case PPC::DIR_A2:
202 case PPC::DIR_E500mc:
203 case PPC::DIR_E5500:
Hal Finkel42daeae2013-11-30 20:55:12 +0000204 case PPC::DIR_PWR7:
Hal Finkel21442b22013-09-11 23:05:25 +0000205 return true;
206 }
207}
208
209bool PPCSubtarget::enableMachineScheduler() const {
210 // Enable MI scheduling for the embedded cores.
211 // FIXME: Enable this for all cores (some additional modeling
212 // may be necessary).
213 return needsAggressiveScheduling(DarwinDirective);
214}
215
216void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
217 MachineInstr *begin,
218 MachineInstr *end,
219 unsigned NumRegionInstrs) const {
220 if (needsAggressiveScheduling(DarwinDirective)) {
221 Policy.OnlyTopDown = false;
222 Policy.OnlyBottomUp = false;
223 }
224
225 // Spilling is generally expensive on all PPC cores, so always enable
226 // register-pressure tracking.
227 Policy.ShouldTrackPressure = true;
228}
229
230bool PPCSubtarget::useAA() const {
231 // Use AA during code generation for the embedded cores.
232 return needsAggressiveScheduling(DarwinDirective);
233}
234