xref: /src/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCSubtarget.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
163faed5bSDimitry Andric //===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9411bd29eSDimitry Andric // This file implements the PPC specific subclass of TargetSubtargetInfo.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten 
13009b1c42SEd Schouten #include "PPCSubtarget.h"
14b60736ecSDimitry Andric #include "GISel/PPCCallLowering.h"
15b60736ecSDimitry Andric #include "GISel/PPCLegalizerInfo.h"
16b60736ecSDimitry Andric #include "GISel/PPCRegisterBankInfo.h"
17009b1c42SEd Schouten #include "PPC.h"
184a16efa3SDimitry Andric #include "PPCRegisterInfo.h"
195a5ac124SDimitry Andric #include "PPCTargetMachine.h"
20b60736ecSDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
21145449b1SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
22f8af5cf6SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
23f8af5cf6SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h"
24f8af5cf6SDimitry Andric #include "llvm/IR/Attributes.h"
25f8af5cf6SDimitry Andric #include "llvm/IR/Function.h"
26ac9a064cSDimitry Andric #include "llvm/IR/GlobalAlias.h"
275ca98fd9SDimitry Andric #include "llvm/IR/GlobalValue.h"
28ac9a064cSDimitry Andric #include "llvm/IR/GlobalVariable.h"
29c0981da4SDimitry Andric #include "llvm/MC/TargetRegistry.h"
3067c32a98SDimitry Andric #include "llvm/Support/CommandLine.h"
314a16efa3SDimitry Andric #include "llvm/Target/TargetMachine.h"
32009b1c42SEd Schouten #include <cstdlib>
33411bd29eSDimitry Andric 
345ca98fd9SDimitry Andric using namespace llvm;
355ca98fd9SDimitry Andric 
365ca98fd9SDimitry Andric #define DEBUG_TYPE "ppc-subtarget"
375ca98fd9SDimitry Andric 
38411bd29eSDimitry Andric #define GET_SUBTARGETINFO_TARGET_DESC
39411bd29eSDimitry Andric #define GET_SUBTARGETINFO_CTOR
40411bd29eSDimitry Andric #include "PPCGenSubtargetInfo.inc"
41411bd29eSDimitry Andric 
42e3b55780SDimitry Andric static cl::opt<bool>
43e6d15924SDimitry Andric     EnableMachinePipeliner("ppc-enable-pipeliner",
44e6d15924SDimitry Andric                            cl::desc("Enable Machine Pipeliner for PPC"),
45e6d15924SDimitry Andric                            cl::init(false), cl::Hidden);
46e6d15924SDimitry Andric 
initializeSubtargetDependencies(StringRef CPU,StringRef TuneCPU,StringRef FS)475ca98fd9SDimitry Andric PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
48e3b55780SDimitry Andric                                                             StringRef TuneCPU,
495ca98fd9SDimitry Andric                                                             StringRef FS) {
50f8af5cf6SDimitry Andric   initializeEnvironment();
51e3b55780SDimitry Andric   initSubtargetFeatures(CPU, TuneCPU, FS);
525ca98fd9SDimitry Andric   return *this;
53f8af5cf6SDimitry Andric }
54009b1c42SEd Schouten 
PPCSubtarget(const Triple & TT,const std::string & CPU,const std::string & TuneCPU,const std::string & FS,const PPCTargetMachine & TM)553a0822f0SDimitry Andric PPCSubtarget::PPCSubtarget(const Triple &TT, const std::string &CPU,
56e3b55780SDimitry Andric                            const std::string &TuneCPU, const std::string &FS,
57e3b55780SDimitry Andric                            const PPCTargetMachine &TM)
58e3b55780SDimitry Andric     : PPCGenSubtargetInfo(TT, CPU, TuneCPU, FS), TargetTriple(TT),
5967c32a98SDimitry Andric       IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
6067c32a98SDimitry Andric               TargetTriple.getArch() == Triple::ppc64le),
61e3b55780SDimitry Andric       TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, TuneCPU, FS)),
62b60736ecSDimitry Andric       InstrInfo(*this), TLInfo(TM, *this) {
63b60736ecSDimitry Andric   CallLoweringInfo.reset(new PPCCallLowering(*getTargetLowering()));
64b60736ecSDimitry Andric   Legalizer.reset(new PPCLegalizerInfo(*this));
65b60736ecSDimitry Andric   auto *RBI = new PPCRegisterBankInfo(*getRegisterInfo());
66b60736ecSDimitry Andric   RegBankInfo.reset(RBI);
67b60736ecSDimitry Andric 
68ac9a064cSDimitry Andric   InstSelector.reset(createPPCInstructionSelector(TM, *this, *RBI));
69b60736ecSDimitry Andric }
705ca98fd9SDimitry Andric 
initializeEnvironment()71f8af5cf6SDimitry Andric void PPCSubtarget::initializeEnvironment() {
721d5ae102SDimitry Andric   StackAlignment = Align(16);
73706b4fc4SDimitry Andric   CPUDirective = PPC::DIR_NONE;
7401095a5dSDimitry Andric   HasPOPCNTD = POPCNTD_Unavailable;
75f8af5cf6SDimitry Andric }
76f8af5cf6SDimitry Andric 
initSubtargetFeatures(StringRef CPU,StringRef TuneCPU,StringRef FS)77e3b55780SDimitry Andric void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef TuneCPU,
78e3b55780SDimitry Andric                                          StringRef FS) {
79009b1c42SEd Schouten   // Determine default and user specified characteristics
80cfca06d7SDimitry Andric   std::string CPUName = std::string(CPU);
8101095a5dSDimitry Andric   if (CPUName.empty() || CPU == "generic") {
825a5ac124SDimitry Andric     // If cross-compiling with -march=ppc64le without -mcpu
835a5ac124SDimitry Andric     if (TargetTriple.getArch() == Triple::ppc64le)
845a5ac124SDimitry Andric       CPUName = "ppc64le";
85706b4fc4SDimitry Andric     else if (TargetTriple.getSubArch() == Triple::PPCSubArch_spe)
86706b4fc4SDimitry Andric       CPUName = "e500";
875a5ac124SDimitry Andric     else
88411bd29eSDimitry Andric       CPUName = "generic";
895a5ac124SDimitry Andric   }
90009b1c42SEd Schouten 
91e3b55780SDimitry Andric   // Determine the CPU to schedule for.
92e3b55780SDimitry Andric   if (TuneCPU.empty()) TuneCPU = CPUName;
93e3b55780SDimitry Andric 
94411bd29eSDimitry Andric   // Initialize scheduling itinerary for the specified CPU.
95411bd29eSDimitry Andric   InstrItins = getInstrItineraryForCPU(CPUName);
96009b1c42SEd Schouten 
97522600a2SDimitry Andric   // Parse features string.
98e3b55780SDimitry Andric   ParseSubtargetFeatures(CPUName, TuneCPU, FS);
99522600a2SDimitry Andric 
100009b1c42SEd Schouten   // If the user requested use of 64-bit regs, but the cpu selected doesn't
101009b1c42SEd Schouten   // support it, ignore.
10267c32a98SDimitry Andric   if (IsPPC64 && has64BitSupport())
10367c32a98SDimitry Andric     Use64BitRegs = true;
104009b1c42SEd Schouten 
1057fa27ce4SDimitry Andric   if (TargetTriple.isPPC32SecurePlt())
106e3b55780SDimitry Andric     IsSecurePlt = true;
107e6d15924SDimitry Andric 
108eb11fae6SDimitry Andric   if (HasSPE && IsPPC64)
109eb11fae6SDimitry Andric     report_fatal_error( "SPE is only supported for 32-bit targets.\n", false);
110b60736ecSDimitry Andric   if (HasSPE && (HasAltivec || HasVSX || HasFPU))
111eb11fae6SDimitry Andric     report_fatal_error(
112eb11fae6SDimitry Andric         "SPE and traditional floating point cannot both be enabled.\n", false);
113eb11fae6SDimitry Andric 
114eb11fae6SDimitry Andric   // If not SPE, set standard FPU
115eb11fae6SDimitry Andric   if (!HasSPE)
116eb11fae6SDimitry Andric     HasFPU = true;
117eb11fae6SDimitry Andric 
1185a5ac124SDimitry Andric   StackAlignment = getPlatformStackAlignment();
119f8af5cf6SDimitry Andric 
120f8af5cf6SDimitry Andric   // Determine endianness.
121344a3780SDimitry Andric   IsLittleEndian = TM.isLittleEndian();
122b1c73532SDimitry Andric 
123ac9a064cSDimitry Andric   if (HasAIXSmallLocalExecTLS || HasAIXSmallLocalDynamicTLS) {
124ac9a064cSDimitry Andric     if (!TargetTriple.isOSAIX() || !IsPPC64)
125ac9a064cSDimitry Andric       report_fatal_error("The aix-small-local-[exec|dynamic]-tls attribute is "
126ac9a064cSDimitry Andric                          "only supported on AIX in "
127ac9a064cSDimitry Andric                          "64-bit mode.\n",
128ac9a064cSDimitry Andric                          false);
129ac9a064cSDimitry Andric     // The aix-small-local-[exec|dynamic]-tls attribute should only be used with
130ac9a064cSDimitry Andric     // -data-sections, as having data sections turned off with this option
131ac9a064cSDimitry Andric     // is not ideal for performance. Moreover, the
132ac9a064cSDimitry Andric     // small-local-[exec|dynamic]-tls region is a limited resource, and should
133ac9a064cSDimitry Andric     // not be used for variables that may be replaced.
134ac9a064cSDimitry Andric     if (!TM.getDataSections())
135ac9a064cSDimitry Andric       report_fatal_error("The aix-small-local-[exec|dynamic]-tls attribute can "
136ac9a064cSDimitry Andric                          "only be specified with "
137ac9a064cSDimitry Andric                          "-data-sections.\n",
138ac9a064cSDimitry Andric                          false);
139ac9a064cSDimitry Andric   }
140ac9a064cSDimitry Andric 
141ac9a064cSDimitry Andric   if (HasAIXShLibTLSModelOpt && (!TargetTriple.isOSAIX() || !IsPPC64))
142ac9a064cSDimitry Andric     report_fatal_error("The aix-shared-lib-tls-model-opt attribute "
143ac9a064cSDimitry Andric                        "is only supported on AIX in 64-bit mode.\n",
144ac9a064cSDimitry Andric                        false);
145009b1c42SEd Schouten }
146009b1c42SEd Schouten 
enableMachineScheduler() const147e6d15924SDimitry Andric bool PPCSubtarget::enableMachineScheduler() const { return true; }
148e6d15924SDimitry Andric 
enableMachinePipeliner() const149e6d15924SDimitry Andric bool PPCSubtarget::enableMachinePipeliner() const {
150cfca06d7SDimitry Andric   return getSchedModel().hasInstrSchedModel() && EnableMachinePipeliner;
151f8af5cf6SDimitry Andric }
152f8af5cf6SDimitry Andric 
useDFAforSMS() const153e6d15924SDimitry Andric bool PPCSubtarget::useDFAforSMS() const { return false; }
154e6d15924SDimitry Andric 
1555ca98fd9SDimitry Andric // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
enablePostRAScheduler() const1563a0822f0SDimitry Andric bool PPCSubtarget::enablePostRAScheduler() const { return true; }
1575ca98fd9SDimitry Andric 
getAntiDepBreakMode() const1585ca98fd9SDimitry Andric PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
1595ca98fd9SDimitry Andric   return TargetSubtargetInfo::ANTIDEP_ALL;
1605ca98fd9SDimitry Andric }
1615ca98fd9SDimitry Andric 
getCriticalPathRCs(RegClassVector & CriticalPathRCs) const1625ca98fd9SDimitry Andric void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
1635ca98fd9SDimitry Andric   CriticalPathRCs.clear();
1645ca98fd9SDimitry Andric   CriticalPathRCs.push_back(isPPC64() ?
1655ca98fd9SDimitry Andric                             &PPC::G8RCRegClass : &PPC::GPRCRegClass);
1665ca98fd9SDimitry Andric }
1675ca98fd9SDimitry Andric 
overrideSchedPolicy(MachineSchedPolicy & Policy,unsigned NumRegionInstrs) const168f8af5cf6SDimitry Andric void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
169f8af5cf6SDimitry Andric                                        unsigned NumRegionInstrs) const {
170eb11fae6SDimitry Andric   // The GenericScheduler that we use defaults to scheduling bottom up only.
171eb11fae6SDimitry Andric   // We want to schedule from both the top and the bottom and so we set
172eb11fae6SDimitry Andric   // OnlyBottomUp to false.
173eb11fae6SDimitry Andric   // We want to do bi-directional scheduling since it provides a more balanced
174eb11fae6SDimitry Andric   // schedule leading to better performance.
175f8af5cf6SDimitry Andric   Policy.OnlyBottomUp = false;
176f8af5cf6SDimitry Andric   // Spilling is generally expensive on all PPC cores, so always enable
177f8af5cf6SDimitry Andric   // register-pressure tracking.
178f8af5cf6SDimitry Andric   Policy.ShouldTrackPressure = true;
179f8af5cf6SDimitry Andric }
180f8af5cf6SDimitry Andric 
useAA() const181f8af5cf6SDimitry Andric bool PPCSubtarget::useAA() const {
182eb11fae6SDimitry Andric   return true;
183f8af5cf6SDimitry Andric }
184f8af5cf6SDimitry Andric 
enableSubRegLiveness() const185ac9a064cSDimitry Andric bool PPCSubtarget::enableSubRegLiveness() const { return true; }
18667c32a98SDimitry Andric 
isGVIndirectSymbol(const GlobalValue * GV) const1871d5ae102SDimitry Andric bool PPCSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
188ac9a064cSDimitry Andric   if (isAIXABI()) {
189ac9a064cSDimitry Andric     if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
190ac9a064cSDimitry Andric       // On AIX the only symbols that aren't indirect are toc-data.
191ac9a064cSDimitry Andric       return !GVar->hasAttribute("toc-data");
192ac9a064cSDimitry Andric 
193ac9a064cSDimitry Andric     return true;
194ac9a064cSDimitry Andric   }
195ac9a064cSDimitry Andric 
196dd58ef01SDimitry Andric   // Large code model always uses the TOC even for local symbols.
197dd58ef01SDimitry Andric   if (TM.getCodeModel() == CodeModel::Large)
1981d5ae102SDimitry Andric     return true;
199ac9a064cSDimitry Andric 
200ac9a064cSDimitry Andric   if (TM.shouldAssumeDSOLocal(GV))
2011d5ae102SDimitry Andric     return false;
2021d5ae102SDimitry Andric   return true;
203dd58ef01SDimitry Andric }
204dd58ef01SDimitry Andric 
getCodeModel(const TargetMachine & TM,const GlobalValue * GV) const205ac9a064cSDimitry Andric CodeModel::Model PPCSubtarget::getCodeModel(const TargetMachine &TM,
206ac9a064cSDimitry Andric                                             const GlobalValue *GV) const {
207ac9a064cSDimitry Andric   // If there isn't an attribute to override the module code model
208ac9a064cSDimitry Andric   // this will be the effective code model.
209ac9a064cSDimitry Andric   CodeModel::Model ModuleModel = TM.getCodeModel();
210ac9a064cSDimitry Andric 
211ac9a064cSDimitry Andric   // Initially support per global code model for AIX only.
212ac9a064cSDimitry Andric   if (!isAIXABI())
213ac9a064cSDimitry Andric     return ModuleModel;
214ac9a064cSDimitry Andric 
215ac9a064cSDimitry Andric   // Only GlobalVariables carry an attribute which can override the module code
216ac9a064cSDimitry Andric   // model.
217ac9a064cSDimitry Andric   assert(GV && "Unexpected NULL GlobalValue");
218ac9a064cSDimitry Andric   const GlobalVariable *GlobalVar =
219ac9a064cSDimitry Andric       [](const GlobalValue *GV) -> const GlobalVariable * {
220ac9a064cSDimitry Andric     const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV);
221ac9a064cSDimitry Andric     if (Var)
222ac9a064cSDimitry Andric       return Var;
223ac9a064cSDimitry Andric 
224ac9a064cSDimitry Andric     const GlobalAlias *Alias = dyn_cast<GlobalAlias>(GV);
225ac9a064cSDimitry Andric     if (Alias)
226ac9a064cSDimitry Andric       return dyn_cast<GlobalVariable>(Alias->getAliaseeObject());
227ac9a064cSDimitry Andric 
228ac9a064cSDimitry Andric     return nullptr;
229ac9a064cSDimitry Andric   }(GV);
230ac9a064cSDimitry Andric 
231ac9a064cSDimitry Andric   if (!GlobalVar)
232ac9a064cSDimitry Andric     return ModuleModel;
233ac9a064cSDimitry Andric 
234ac9a064cSDimitry Andric   std::optional<CodeModel::Model> MaybeCodeModel = GlobalVar->getCodeModel();
235ac9a064cSDimitry Andric   if (MaybeCodeModel) {
236ac9a064cSDimitry Andric     CodeModel::Model CM = *MaybeCodeModel;
237ac9a064cSDimitry Andric     assert((CM == CodeModel::Small || CM == CodeModel::Large) &&
238ac9a064cSDimitry Andric            "invalid code model for AIX");
239ac9a064cSDimitry Andric     return CM;
240ac9a064cSDimitry Andric   }
241ac9a064cSDimitry Andric 
242ac9a064cSDimitry Andric   return ModuleModel;
243ac9a064cSDimitry Andric }
244ac9a064cSDimitry Andric 
isELFv2ABI() const2455a5ac124SDimitry Andric bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
isPPC64() const2465a5ac124SDimitry Andric bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }
247cfca06d7SDimitry Andric 
isUsingPCRelativeCalls() const248cfca06d7SDimitry Andric bool PPCSubtarget::isUsingPCRelativeCalls() const {
249cfca06d7SDimitry Andric   return isPPC64() && hasPCRelativeMemops() && isELFv2ABI() &&
250cfca06d7SDimitry Andric          CodeModel::Medium == getTargetMachine().getCodeModel();
251cfca06d7SDimitry Andric }
252b60736ecSDimitry Andric 
253b60736ecSDimitry Andric // GlobalISEL
getCallLowering() const254b60736ecSDimitry Andric const CallLowering *PPCSubtarget::getCallLowering() const {
255b60736ecSDimitry Andric   return CallLoweringInfo.get();
256b60736ecSDimitry Andric }
257b60736ecSDimitry Andric 
getRegBankInfo() const258b60736ecSDimitry Andric const RegisterBankInfo *PPCSubtarget::getRegBankInfo() const {
259b60736ecSDimitry Andric   return RegBankInfo.get();
260b60736ecSDimitry Andric }
261b60736ecSDimitry Andric 
getLegalizerInfo() const262b60736ecSDimitry Andric const LegalizerInfo *PPCSubtarget::getLegalizerInfo() const {
263b60736ecSDimitry Andric   return Legalizer.get();
264b60736ecSDimitry Andric }
265b60736ecSDimitry Andric 
getInstructionSelector() const266b60736ecSDimitry Andric InstructionSelector *PPCSubtarget::getInstructionSelector() const {
267b60736ecSDimitry Andric   return InstSelector.get();
268b60736ecSDimitry Andric }
269