1//===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the PPC specific subclass of TargetSubtargetInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#include "PPCSubtarget.h"
14#include "GISel/PPCCallLowering.h"
15#include "GISel/PPCLegalizerInfo.h"
16#include "GISel/PPCRegisterBankInfo.h"
17#include "PPC.h"
18#include "PPCRegisterInfo.h"
19#include "PPCSelectionDAGInfo.h"
20#include "PPCTargetMachine.h"
21#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
22#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineScheduler.h"
25#include "llvm/IR/GlobalAlias.h"
26#include "llvm/IR/GlobalValue.h"
27#include "llvm/IR/GlobalVariable.h"
28#include "llvm/MC/TargetRegistry.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/TargetParser/PPCTargetParser.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "ppc-subtarget"
35
36#define GET_SUBTARGETINFO_TARGET_DESC
37#define GET_SUBTARGETINFO_CTOR
38#include "PPCGenSubtargetInfo.inc"
39
40PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
41 StringRef TuneCPU,
42 StringRef FS) {
43 initializeEnvironment();
44 initSubtargetFeatures(CPU, TuneCPU, FS);
45 return *this;
46}
47
48PPCSubtarget::PPCSubtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU,
49 StringRef FS, StringRef ABIName,
50 const PPCTargetMachine &TM)
51 : PPCGenSubtargetInfo(TT, CPU, TuneCPU, FS),
52 TargetABI(PPCTargetMachine::computeABI(TT, ABIName)), TM(TM),
53 FrameLowering(initializeSubtargetDependencies(CPU, TuneCPU, FS)),
54 InstrInfo(*this), TLInfo(TM, *this) {
55 TSInfo = std::make_unique<PPCSelectionDAGInfo>();
56
57 CallLoweringInfo.reset(p: new PPCCallLowering(*getTargetLowering()));
58 Legalizer.reset(p: new PPCLegalizerInfo(*this));
59 auto *RBI = new PPCRegisterBankInfo(*getRegisterInfo());
60 RegBankInfo.reset(p: RBI);
61
62 InstSelector.reset(p: createPPCInstructionSelector(TM, *this, *RBI));
63}
64
65PPCSubtarget::~PPCSubtarget() = default;
66
67const SelectionDAGTargetInfo *PPCSubtarget::getSelectionDAGInfo() const {
68 return TSInfo.get();
69}
70
71void PPCSubtarget::initializeEnvironment() {
72 StackAlignment = Align(16);
73 CPUDirective = PPC::DIR_NONE;
74 HasPOPCNTD = POPCNTD_Unavailable;
75}
76
77void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef TuneCPU,
78 StringRef FS) {
79 // Determine default and user specified characteristics
80 std::string CPUName = std::string(CPU);
81 if (CPUName.empty() || CPU == "generic") {
82 if (getTargetTriple().getSubArch() == Triple::PPCSubArch_spe)
83 CPUName = "e500";
84 else
85 CPUName = std::string(PPC::getNormalizedPPCTargetCPU(T: getTargetTriple()));
86 }
87
88 // Determine the CPU to schedule for.
89 if (TuneCPU.empty()) TuneCPU = CPUName;
90
91 // Initialize scheduling itinerary for the specified CPU.
92 InstrItins = getInstrItineraryForCPU(CPU: CPUName);
93
94 // Parse features string.
95 ParseSubtargetFeatures(CPU: CPUName, TuneCPU, FS);
96
97 // If the user requested use of 64-bit regs, but the cpu selected doesn't
98 // support it, ignore.
99 if (IsPPC64 && has64BitSupport())
100 Use64BitRegs = true;
101
102 if (getTargetTriple().isPPC32SecurePlt())
103 IsSecurePlt = true;
104
105 if (HasSPE && IsPPC64)
106 report_fatal_error( reason: "SPE is only supported for 32-bit targets.\n", gen_crash_diag: false);
107 if (HasSPE && (HasAltivec || HasVSX || HasFPU))
108 report_fatal_error(
109 reason: "SPE and traditional floating point cannot both be enabled.\n", gen_crash_diag: false);
110
111 // If not SPE, set standard FPU
112 if (!HasSPE)
113 HasFPU = true;
114
115 StackAlignment = getPlatformStackAlignment();
116
117 // Determine endianness.
118 IsLittleEndian = TM.isLittleEndian();
119
120 if (HasAIXSmallLocalExecTLS || HasAIXSmallLocalDynamicTLS) {
121 if (!getTargetTriple().isOSAIX() || !IsPPC64)
122 report_fatal_error(reason: "The aix-small-local-[exec|dynamic]-tls attribute is "
123 "only supported on AIX in "
124 "64-bit mode.\n",
125 gen_crash_diag: false);
126 // The aix-small-local-[exec|dynamic]-tls attribute should only be used with
127 // -data-sections, as having data sections turned off with this option
128 // is not ideal for performance. Moreover, the
129 // small-local-[exec|dynamic]-tls region is a limited resource, and should
130 // not be used for variables that may be replaced.
131 if (!TM.getDataSections())
132 report_fatal_error(reason: "The aix-small-local-[exec|dynamic]-tls attribute can "
133 "only be specified with "
134 "-data-sections.\n",
135 gen_crash_diag: false);
136 }
137
138 if (HasAIXShLibTLSModelOpt && (!getTargetTriple().isOSAIX() || !IsPPC64))
139 report_fatal_error(reason: "The aix-shared-lib-tls-model-opt attribute "
140 "is only supported on AIX in 64-bit mode.\n",
141 gen_crash_diag: false);
142
143 if (UsePointerGlueHelper && !getTargetTriple().isOSAIX())
144 report_fatal_error(reason: "use-ptrgl-helper feature is only supported on AIX\n",
145 gen_crash_diag: false);
146}
147
148bool PPCSubtarget::enableMachineScheduler() const { return true; }
149
150bool PPCSubtarget::enableMachinePipeliner() const {
151 return getSchedModel().hasInstrSchedModel();
152}
153
154bool PPCSubtarget::useDFAforSMS() const { return false; }
155
156// This overrides the PostRAScheduler bit in the SchedModel for each CPU.
157bool PPCSubtarget::enablePostRAScheduler() const { return true; }
158
159PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
160 return TargetSubtargetInfo::ANTIDEP_ALL;
161}
162
163void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
164 CriticalPathRCs.clear();
165 CriticalPathRCs.push_back(Elt: isPPC64() ?
166 &PPC::G8RCRegClass : &PPC::GPRCRegClass);
167}
168
169void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
170 const SchedRegion &Region) const {
171 // The GenericScheduler that we use defaults to scheduling bottom up only.
172 // We want to schedule from both the top and the bottom and so we set
173 // OnlyBottomUp to false.
174 // We want to do bi-directional scheduling since it provides a more balanced
175 // schedule leading to better performance.
176 Policy.OnlyBottomUp = false;
177 // Spilling is generally expensive on all PPC cores, so always enable
178 // register-pressure tracking.
179 Policy.ShouldTrackPressure = true;
180}
181
182bool PPCSubtarget::useAA() const {
183 return true;
184}
185
186bool PPCSubtarget::enableSubRegLiveness() const { return true; }
187
188bool PPCSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
189 if (isAIXABI()) {
190 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(Val: GV))
191 // On AIX the only symbols that aren't indirect are toc-data.
192 return !GVar->hasAttribute(Kind: "toc-data");
193
194 return true;
195 }
196
197 // Large code model always uses the TOC even for local symbols.
198 if (TM.getCodeModel() == CodeModel::Large)
199 return true;
200
201 if (TM.shouldAssumeDSOLocal(GV))
202 return false;
203 return true;
204}
205
206CodeModel::Model PPCSubtarget::getCodeModel(const TargetMachine &TM,
207 const GlobalValue *GV) const {
208 // If there isn't an attribute to override the module code model
209 // this will be the effective code model.
210 CodeModel::Model ModuleModel = TM.getCodeModel();
211
212 // Initially support per global code model for AIX only.
213 if (!isAIXABI())
214 return ModuleModel;
215
216 // Only GlobalVariables carry an attribute which can override the module code
217 // model.
218 assert(GV && "Unexpected NULL GlobalValue");
219 const GlobalVariable *GlobalVar =
220 [](const GlobalValue *GV) -> const GlobalVariable * {
221 const GlobalVariable *Var = dyn_cast<GlobalVariable>(Val: GV);
222 if (Var)
223 return Var;
224
225 const GlobalAlias *Alias = dyn_cast<GlobalAlias>(Val: GV);
226 if (Alias)
227 return dyn_cast<GlobalVariable>(Val: Alias->getAliaseeObject());
228
229 return nullptr;
230 }(GV);
231
232 if (!GlobalVar)
233 return ModuleModel;
234
235 std::optional<CodeModel::Model> MaybeCodeModel = GlobalVar->getCodeModel();
236 if (MaybeCodeModel) {
237 CodeModel::Model CM = *MaybeCodeModel;
238 assert((CM == CodeModel::Small || CM == CodeModel::Large) &&
239 "invalid code model for AIX");
240 return CM;
241 }
242
243 return ModuleModel;
244}
245
246bool PPCSubtarget::isELFv2ABI() const { return TargetABI == PPC_ABI_ELFv2; }
247
248bool PPCSubtarget::isUsingPCRelativeCalls() const {
249 return isPPC64() && hasPCRelativeMemops() && isELFv2ABI() &&
250 CodeModel::Medium == getTargetMachine().getCodeModel();
251}
252
253// GlobalISEL
254const CallLowering *PPCSubtarget::getCallLowering() const {
255 return CallLoweringInfo.get();
256}
257
258const RegisterBankInfo *PPCSubtarget::getRegBankInfo() const {
259 return RegBankInfo.get();
260}
261
262const LegalizerInfo *PPCSubtarget::getLegalizerInfo() const {
263 return Legalizer.get();
264}
265
266InstructionSelector *PPCSubtarget::getInstructionSelector() const {
267 return InstSelector.get();
268}
269