xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCSubtarget.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
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 "PPCTargetMachine.h"
20 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineScheduler.h"
23 #include "llvm/IR/Attributes.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/MC/TargetRegistry.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include <cstdlib>
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "ppc-subtarget"
34 
35 #define GET_SUBTARGETINFO_TARGET_DESC
36 #define GET_SUBTARGETINFO_CTOR
37 #include "PPCGenSubtargetInfo.inc"
38 
39 static cl::opt<bool> UseSubRegLiveness("ppc-track-subreg-liveness",
40 cl::desc("Enable subregister liveness tracking for PPC"), cl::Hidden);
41 
42 static cl::opt<bool>
43     EnableMachinePipeliner("ppc-enable-pipeliner",
44                            cl::desc("Enable Machine Pipeliner for PPC"),
45                            cl::init(false), cl::Hidden);
46 
47 PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
48                                                             StringRef FS) {
49   initializeEnvironment();
50   initSubtargetFeatures(CPU, FS);
51   return *this;
52 }
53 
54 PPCSubtarget::PPCSubtarget(const Triple &TT, const std::string &CPU,
55                            const std::string &FS, const PPCTargetMachine &TM)
56     : PPCGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS), TargetTriple(TT),
57       IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
58               TargetTriple.getArch() == Triple::ppc64le),
59       TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, FS)),
60       InstrInfo(*this), TLInfo(TM, *this) {
61   CallLoweringInfo.reset(new PPCCallLowering(*getTargetLowering()));
62   Legalizer.reset(new PPCLegalizerInfo(*this));
63   auto *RBI = new PPCRegisterBankInfo(*getRegisterInfo());
64   RegBankInfo.reset(RBI);
65 
66   InstSelector.reset(createPPCInstructionSelector(
67       *static_cast<const PPCTargetMachine *>(&TM), *this, *RBI));
68 }
69 
70 void PPCSubtarget::initializeEnvironment() {
71   StackAlignment = Align(16);
72   CPUDirective = PPC::DIR_NONE;
73   HasMFOCRF = false;
74   Has64BitSupport = false;
75   Use64BitRegs = false;
76   UseCRBits = false;
77   HasHardFloat = false;
78   HasAltivec = false;
79   HasSPE = false;
80   HasEFPU2 = false;
81   HasFPU = false;
82   HasVSX = false;
83   NeedsTwoConstNR = false;
84   HasP8Vector = false;
85   HasP8Altivec = false;
86   HasP8Crypto = false;
87   HasP9Vector = false;
88   HasP9Altivec = false;
89   HasMMA = false;
90   HasROPProtect = false;
91   HasPrivileged = false;
92   HasP10Vector = false;
93   HasPrefixInstrs = false;
94   HasPCRelativeMemops = false;
95   HasFCPSGN = false;
96   HasFSQRT = false;
97   HasFRE = false;
98   HasFRES = false;
99   HasFRSQRTE = false;
100   HasFRSQRTES = false;
101   HasRecipPrec = false;
102   HasSTFIWX = false;
103   HasLFIWAX = false;
104   HasFPRND = false;
105   HasFPCVT = false;
106   HasISEL = false;
107   HasBPERMD = false;
108   HasExtDiv = false;
109   HasCMPB = false;
110   HasLDBRX = false;
111   IsBookE = false;
112   HasOnlyMSYNC = false;
113   IsPPC4xx = false;
114   IsPPC6xx = false;
115   IsE500 = false;
116   FeatureMFTB = false;
117   AllowsUnalignedFPAccess = false;
118   DeprecatedDST = false;
119   HasICBT = false;
120   HasInvariantFunctionDescriptors = false;
121   HasPartwordAtomics = false;
122   HasQuadwordAtomics = false;
123   HasDirectMove = false;
124   HasHTM = false;
125   HasFloat128 = false;
126   HasFusion = false;
127   HasStoreFusion = false;
128   HasAddiLoadFusion = false;
129   HasAddisLoadFusion = false;
130   HasArithAddFusion = false;
131   HasAddLogicalFusion = false;
132   HasLogicalAddFusion = false;
133   HasLogicalFusion = false;
134   IsISA2_06 = false;
135   IsISA2_07 = false;
136   IsISA3_0 = false;
137   IsISA3_1 = false;
138   UseLongCalls = false;
139   SecurePlt = false;
140   VectorsUseTwoUnits = false;
141   UsePPCPreRASchedStrategy = false;
142   UsePPCPostRASchedStrategy = false;
143   PairedVectorMemops = false;
144   PredictableSelectIsExpensive = false;
145   HasModernAIXAs = false;
146   IsAIX = false;
147 
148   HasPOPCNTD = POPCNTD_Unavailable;
149 }
150 
151 void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
152   // Determine default and user specified characteristics
153   std::string CPUName = std::string(CPU);
154   if (CPUName.empty() || CPU == "generic") {
155     // If cross-compiling with -march=ppc64le without -mcpu
156     if (TargetTriple.getArch() == Triple::ppc64le)
157       CPUName = "ppc64le";
158     else if (TargetTriple.getSubArch() == Triple::PPCSubArch_spe)
159       CPUName = "e500";
160     else
161       CPUName = "generic";
162   }
163 
164   // Initialize scheduling itinerary for the specified CPU.
165   InstrItins = getInstrItineraryForCPU(CPUName);
166 
167   // Parse features string.
168   ParseSubtargetFeatures(CPUName, /*TuneCPU*/ CPUName, FS);
169 
170   // If the user requested use of 64-bit regs, but the cpu selected doesn't
171   // support it, ignore.
172   if (IsPPC64 && has64BitSupport())
173     Use64BitRegs = true;
174 
175   if ((TargetTriple.isOSFreeBSD() && TargetTriple.getOSMajorVersion() >= 13) ||
176       TargetTriple.isOSNetBSD() || TargetTriple.isOSOpenBSD() ||
177       TargetTriple.isMusl())
178     SecurePlt = true;
179 
180   if (HasSPE && IsPPC64)
181     report_fatal_error( "SPE is only supported for 32-bit targets.\n", false);
182   if (HasSPE && (HasAltivec || HasVSX || HasFPU))
183     report_fatal_error(
184         "SPE and traditional floating point cannot both be enabled.\n", false);
185 
186   // If not SPE, set standard FPU
187   if (!HasSPE)
188     HasFPU = true;
189 
190   StackAlignment = getPlatformStackAlignment();
191 
192   // Determine endianness.
193   IsLittleEndian = TM.isLittleEndian();
194 }
195 
196 bool PPCSubtarget::enableMachineScheduler() const { return true; }
197 
198 bool PPCSubtarget::enableMachinePipeliner() const {
199   return getSchedModel().hasInstrSchedModel() && EnableMachinePipeliner;
200 }
201 
202 bool PPCSubtarget::useDFAforSMS() const { return false; }
203 
204 // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
205 bool PPCSubtarget::enablePostRAScheduler() const { return true; }
206 
207 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
208   return TargetSubtargetInfo::ANTIDEP_ALL;
209 }
210 
211 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
212   CriticalPathRCs.clear();
213   CriticalPathRCs.push_back(isPPC64() ?
214                             &PPC::G8RCRegClass : &PPC::GPRCRegClass);
215 }
216 
217 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
218                                        unsigned NumRegionInstrs) const {
219   // The GenericScheduler that we use defaults to scheduling bottom up only.
220   // We want to schedule from both the top and the bottom and so we set
221   // OnlyBottomUp to false.
222   // We want to do bi-directional scheduling since it provides a more balanced
223   // schedule leading to better performance.
224   Policy.OnlyBottomUp = false;
225   // Spilling is generally expensive on all PPC cores, so always enable
226   // register-pressure tracking.
227   Policy.ShouldTrackPressure = true;
228 }
229 
230 bool PPCSubtarget::useAA() const {
231   return true;
232 }
233 
234 bool PPCSubtarget::enableSubRegLiveness() const {
235   return UseSubRegLiveness;
236 }
237 
238 bool PPCSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
239   // Large code model always uses the TOC even for local symbols.
240   if (TM.getCodeModel() == CodeModel::Large)
241     return true;
242   if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
243     return false;
244   return true;
245 }
246 
247 bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
248 bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }
249 
250 bool PPCSubtarget::isUsingPCRelativeCalls() const {
251   return isPPC64() && hasPCRelativeMemops() && isELFv2ABI() &&
252          CodeModel::Medium == getTargetMachine().getCodeModel();
253 }
254 
255 // GlobalISEL
256 const CallLowering *PPCSubtarget::getCallLowering() const {
257   return CallLoweringInfo.get();
258 }
259 
260 const RegisterBankInfo *PPCSubtarget::getRegBankInfo() const {
261   return RegBankInfo.get();
262 }
263 
264 const LegalizerInfo *PPCSubtarget::getLegalizerInfo() const {
265   return Legalizer.get();
266 }
267 
268 InstructionSelector *PPCSubtarget::getInstructionSelector() const {
269   return InstSelector.get();
270 }
271