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