xref: /aosp_15_r20/external/llvm/lib/Target/PowerPC/PPCSubtarget.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- PowerPCSubtarget.cpp - PPC Subtarget Information ------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the PPC specific subclass of TargetSubtargetInfo.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "PPCSubtarget.h"
15*9880d681SAndroid Build Coastguard Worker #include "PPC.h"
16*9880d681SAndroid Build Coastguard Worker #include "PPCRegisterInfo.h"
17*9880d681SAndroid Build Coastguard Worker #include "PPCTargetMachine.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineScheduler.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Attributes.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GlobalValue.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
26*9880d681SAndroid Build Coastguard Worker #include <cstdlib>
27*9880d681SAndroid Build Coastguard Worker 
28*9880d681SAndroid Build Coastguard Worker using namespace llvm;
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "ppc-subtarget"
31*9880d681SAndroid Build Coastguard Worker 
32*9880d681SAndroid Build Coastguard Worker #define GET_SUBTARGETINFO_TARGET_DESC
33*9880d681SAndroid Build Coastguard Worker #define GET_SUBTARGETINFO_CTOR
34*9880d681SAndroid Build Coastguard Worker #include "PPCGenSubtargetInfo.inc"
35*9880d681SAndroid Build Coastguard Worker 
36*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> UseSubRegLiveness("ppc-track-subreg-liveness",
37*9880d681SAndroid Build Coastguard Worker cl::desc("Enable subregister liveness tracking for PPC"), cl::Hidden);
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> QPXStackUnaligned("qpx-stack-unaligned",
40*9880d681SAndroid Build Coastguard Worker   cl::desc("Even when QPX is enabled the stack is not 32-byte aligned"),
41*9880d681SAndroid Build Coastguard Worker   cl::Hidden);
42*9880d681SAndroid Build Coastguard Worker 
initializeSubtargetDependencies(StringRef CPU,StringRef FS)43*9880d681SAndroid Build Coastguard Worker PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
44*9880d681SAndroid Build Coastguard Worker                                                             StringRef FS) {
45*9880d681SAndroid Build Coastguard Worker   initializeEnvironment();
46*9880d681SAndroid Build Coastguard Worker   initSubtargetFeatures(CPU, FS);
47*9880d681SAndroid Build Coastguard Worker   return *this;
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker 
PPCSubtarget(const Triple & TT,const std::string & CPU,const std::string & FS,const PPCTargetMachine & TM)50*9880d681SAndroid Build Coastguard Worker PPCSubtarget::PPCSubtarget(const Triple &TT, const std::string &CPU,
51*9880d681SAndroid Build Coastguard Worker                            const std::string &FS, const PPCTargetMachine &TM)
52*9880d681SAndroid Build Coastguard Worker     : PPCGenSubtargetInfo(TT, CPU, FS), TargetTriple(TT),
53*9880d681SAndroid Build Coastguard Worker       IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
54*9880d681SAndroid Build Coastguard Worker               TargetTriple.getArch() == Triple::ppc64le),
55*9880d681SAndroid Build Coastguard Worker       TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, FS)),
56*9880d681SAndroid Build Coastguard Worker       InstrInfo(*this), TLInfo(TM, *this) {}
57*9880d681SAndroid Build Coastguard Worker 
initializeEnvironment()58*9880d681SAndroid Build Coastguard Worker void PPCSubtarget::initializeEnvironment() {
59*9880d681SAndroid Build Coastguard Worker   StackAlignment = 16;
60*9880d681SAndroid Build Coastguard Worker   DarwinDirective = PPC::DIR_NONE;
61*9880d681SAndroid Build Coastguard Worker   HasMFOCRF = false;
62*9880d681SAndroid Build Coastguard Worker   Has64BitSupport = false;
63*9880d681SAndroid Build Coastguard Worker   Use64BitRegs = false;
64*9880d681SAndroid Build Coastguard Worker   UseCRBits = false;
65*9880d681SAndroid Build Coastguard Worker   UseSoftFloat = false;
66*9880d681SAndroid Build Coastguard Worker   HasAltivec = false;
67*9880d681SAndroid Build Coastguard Worker   HasSPE = false;
68*9880d681SAndroid Build Coastguard Worker   HasQPX = false;
69*9880d681SAndroid Build Coastguard Worker   HasVSX = false;
70*9880d681SAndroid Build Coastguard Worker   HasP8Vector = false;
71*9880d681SAndroid Build Coastguard Worker   HasP8Altivec = false;
72*9880d681SAndroid Build Coastguard Worker   HasP8Crypto = false;
73*9880d681SAndroid Build Coastguard Worker   HasP9Vector = false;
74*9880d681SAndroid Build Coastguard Worker   HasP9Altivec = false;
75*9880d681SAndroid Build Coastguard Worker   HasFCPSGN = false;
76*9880d681SAndroid Build Coastguard Worker   HasFSQRT = false;
77*9880d681SAndroid Build Coastguard Worker   HasFRE = false;
78*9880d681SAndroid Build Coastguard Worker   HasFRES = false;
79*9880d681SAndroid Build Coastguard Worker   HasFRSQRTE = false;
80*9880d681SAndroid Build Coastguard Worker   HasFRSQRTES = false;
81*9880d681SAndroid Build Coastguard Worker   HasRecipPrec = false;
82*9880d681SAndroid Build Coastguard Worker   HasSTFIWX = false;
83*9880d681SAndroid Build Coastguard Worker   HasLFIWAX = false;
84*9880d681SAndroid Build Coastguard Worker   HasFPRND = false;
85*9880d681SAndroid Build Coastguard Worker   HasFPCVT = false;
86*9880d681SAndroid Build Coastguard Worker   HasISEL = false;
87*9880d681SAndroid Build Coastguard Worker   HasBPERMD = false;
88*9880d681SAndroid Build Coastguard Worker   HasExtDiv = false;
89*9880d681SAndroid Build Coastguard Worker   HasCMPB = false;
90*9880d681SAndroid Build Coastguard Worker   HasLDBRX = false;
91*9880d681SAndroid Build Coastguard Worker   IsBookE = false;
92*9880d681SAndroid Build Coastguard Worker   HasOnlyMSYNC = false;
93*9880d681SAndroid Build Coastguard Worker   IsPPC4xx = false;
94*9880d681SAndroid Build Coastguard Worker   IsPPC6xx = false;
95*9880d681SAndroid Build Coastguard Worker   IsE500 = false;
96*9880d681SAndroid Build Coastguard Worker   FeatureMFTB = false;
97*9880d681SAndroid Build Coastguard Worker   DeprecatedDST = false;
98*9880d681SAndroid Build Coastguard Worker   HasLazyResolverStubs = false;
99*9880d681SAndroid Build Coastguard Worker   HasICBT = false;
100*9880d681SAndroid Build Coastguard Worker   HasInvariantFunctionDescriptors = false;
101*9880d681SAndroid Build Coastguard Worker   HasPartwordAtomics = false;
102*9880d681SAndroid Build Coastguard Worker   HasDirectMove = false;
103*9880d681SAndroid Build Coastguard Worker   IsQPXStackUnaligned = false;
104*9880d681SAndroid Build Coastguard Worker   HasHTM = false;
105*9880d681SAndroid Build Coastguard Worker   HasFusion = false;
106*9880d681SAndroid Build Coastguard Worker   HasFloat128 = false;
107*9880d681SAndroid Build Coastguard Worker   IsISA3_0 = false;
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker   HasPOPCNTD = POPCNTD_Unavailable;
110*9880d681SAndroid Build Coastguard Worker }
111*9880d681SAndroid Build Coastguard Worker 
initSubtargetFeatures(StringRef CPU,StringRef FS)112*9880d681SAndroid Build Coastguard Worker void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
113*9880d681SAndroid Build Coastguard Worker   // Determine default and user specified characteristics
114*9880d681SAndroid Build Coastguard Worker   std::string CPUName = CPU;
115*9880d681SAndroid Build Coastguard Worker   if (CPUName.empty() || CPU == "generic") {
116*9880d681SAndroid Build Coastguard Worker     // If cross-compiling with -march=ppc64le without -mcpu
117*9880d681SAndroid Build Coastguard Worker     if (TargetTriple.getArch() == Triple::ppc64le)
118*9880d681SAndroid Build Coastguard Worker       CPUName = "ppc64le";
119*9880d681SAndroid Build Coastguard Worker     else
120*9880d681SAndroid Build Coastguard Worker       CPUName = "generic";
121*9880d681SAndroid Build Coastguard Worker   }
122*9880d681SAndroid Build Coastguard Worker 
123*9880d681SAndroid Build Coastguard Worker   // Initialize scheduling itinerary for the specified CPU.
124*9880d681SAndroid Build Coastguard Worker   InstrItins = getInstrItineraryForCPU(CPUName);
125*9880d681SAndroid Build Coastguard Worker 
126*9880d681SAndroid Build Coastguard Worker   // Parse features string.
127*9880d681SAndroid Build Coastguard Worker   ParseSubtargetFeatures(CPUName, FS);
128*9880d681SAndroid Build Coastguard Worker 
129*9880d681SAndroid Build Coastguard Worker   // If the user requested use of 64-bit regs, but the cpu selected doesn't
130*9880d681SAndroid Build Coastguard Worker   // support it, ignore.
131*9880d681SAndroid Build Coastguard Worker   if (IsPPC64 && has64BitSupport())
132*9880d681SAndroid Build Coastguard Worker     Use64BitRegs = true;
133*9880d681SAndroid Build Coastguard Worker 
134*9880d681SAndroid Build Coastguard Worker   // Set up darwin-specific properties.
135*9880d681SAndroid Build Coastguard Worker   if (isDarwin())
136*9880d681SAndroid Build Coastguard Worker     HasLazyResolverStubs = true;
137*9880d681SAndroid Build Coastguard Worker 
138*9880d681SAndroid Build Coastguard Worker   // QPX requires a 32-byte aligned stack. Note that we need to do this if
139*9880d681SAndroid Build Coastguard Worker   // we're compiling for a BG/Q system regardless of whether or not QPX
140*9880d681SAndroid Build Coastguard Worker   // is enabled because external functions will assume this alignment.
141*9880d681SAndroid Build Coastguard Worker   IsQPXStackUnaligned = QPXStackUnaligned;
142*9880d681SAndroid Build Coastguard Worker   StackAlignment = getPlatformStackAlignment();
143*9880d681SAndroid Build Coastguard Worker 
144*9880d681SAndroid Build Coastguard Worker   // Determine endianness.
145*9880d681SAndroid Build Coastguard Worker   // FIXME: Part of the TargetMachine.
146*9880d681SAndroid Build Coastguard Worker   IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
147*9880d681SAndroid Build Coastguard Worker }
148*9880d681SAndroid Build Coastguard Worker 
149*9880d681SAndroid Build Coastguard Worker /// Return true if accesses to the specified global have to go through a dyld
150*9880d681SAndroid Build Coastguard Worker /// lazy resolution stub.  This means that an extra load is required to get the
151*9880d681SAndroid Build Coastguard Worker /// address of the global.
hasLazyResolverStub(const GlobalValue * GV) const152*9880d681SAndroid Build Coastguard Worker bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV) const {
153*9880d681SAndroid Build Coastguard Worker   if (!HasLazyResolverStubs)
154*9880d681SAndroid Build Coastguard Worker     return false;
155*9880d681SAndroid Build Coastguard Worker   if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
156*9880d681SAndroid Build Coastguard Worker     return true;
157*9880d681SAndroid Build Coastguard Worker   // 32 bit macho has no relocation for a-b if a is undefined, even if b is in
158*9880d681SAndroid Build Coastguard Worker   // the section that is being relocated. This means we have to use o load even
159*9880d681SAndroid Build Coastguard Worker   // for GVs that are known to be local to the dso.
160*9880d681SAndroid Build Coastguard Worker   if (GV->isDeclarationForLinker() || GV->hasCommonLinkage())
161*9880d681SAndroid Build Coastguard Worker     return true;
162*9880d681SAndroid Build Coastguard Worker   return false;
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker 
165*9880d681SAndroid Build Coastguard Worker // Embedded cores need aggressive scheduling (and some others also benefit).
needsAggressiveScheduling(unsigned Directive)166*9880d681SAndroid Build Coastguard Worker static bool needsAggressiveScheduling(unsigned Directive) {
167*9880d681SAndroid Build Coastguard Worker   switch (Directive) {
168*9880d681SAndroid Build Coastguard Worker   default: return false;
169*9880d681SAndroid Build Coastguard Worker   case PPC::DIR_440:
170*9880d681SAndroid Build Coastguard Worker   case PPC::DIR_A2:
171*9880d681SAndroid Build Coastguard Worker   case PPC::DIR_E500mc:
172*9880d681SAndroid Build Coastguard Worker   case PPC::DIR_E5500:
173*9880d681SAndroid Build Coastguard Worker   case PPC::DIR_PWR7:
174*9880d681SAndroid Build Coastguard Worker   case PPC::DIR_PWR8:
175*9880d681SAndroid Build Coastguard Worker   // FIXME: Same as P8 until POWER9 scheduling info is available
176*9880d681SAndroid Build Coastguard Worker   case PPC::DIR_PWR9:
177*9880d681SAndroid Build Coastguard Worker     return true;
178*9880d681SAndroid Build Coastguard Worker   }
179*9880d681SAndroid Build Coastguard Worker }
180*9880d681SAndroid Build Coastguard Worker 
enableMachineScheduler() const181*9880d681SAndroid Build Coastguard Worker bool PPCSubtarget::enableMachineScheduler() const {
182*9880d681SAndroid Build Coastguard Worker   // Enable MI scheduling for the embedded cores.
183*9880d681SAndroid Build Coastguard Worker   // FIXME: Enable this for all cores (some additional modeling
184*9880d681SAndroid Build Coastguard Worker   // may be necessary).
185*9880d681SAndroid Build Coastguard Worker   return needsAggressiveScheduling(DarwinDirective);
186*9880d681SAndroid Build Coastguard Worker }
187*9880d681SAndroid Build Coastguard Worker 
188*9880d681SAndroid Build Coastguard Worker // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
enablePostRAScheduler() const189*9880d681SAndroid Build Coastguard Worker bool PPCSubtarget::enablePostRAScheduler() const { return true; }
190*9880d681SAndroid Build Coastguard Worker 
getAntiDepBreakMode() const191*9880d681SAndroid Build Coastguard Worker PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
192*9880d681SAndroid Build Coastguard Worker   return TargetSubtargetInfo::ANTIDEP_ALL;
193*9880d681SAndroid Build Coastguard Worker }
194*9880d681SAndroid Build Coastguard Worker 
getCriticalPathRCs(RegClassVector & CriticalPathRCs) const195*9880d681SAndroid Build Coastguard Worker void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
196*9880d681SAndroid Build Coastguard Worker   CriticalPathRCs.clear();
197*9880d681SAndroid Build Coastguard Worker   CriticalPathRCs.push_back(isPPC64() ?
198*9880d681SAndroid Build Coastguard Worker                             &PPC::G8RCRegClass : &PPC::GPRCRegClass);
199*9880d681SAndroid Build Coastguard Worker }
200*9880d681SAndroid Build Coastguard Worker 
overrideSchedPolicy(MachineSchedPolicy & Policy,unsigned NumRegionInstrs) const201*9880d681SAndroid Build Coastguard Worker void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
202*9880d681SAndroid Build Coastguard Worker                                        unsigned NumRegionInstrs) const {
203*9880d681SAndroid Build Coastguard Worker   if (needsAggressiveScheduling(DarwinDirective)) {
204*9880d681SAndroid Build Coastguard Worker     Policy.OnlyTopDown = false;
205*9880d681SAndroid Build Coastguard Worker     Policy.OnlyBottomUp = false;
206*9880d681SAndroid Build Coastguard Worker   }
207*9880d681SAndroid Build Coastguard Worker 
208*9880d681SAndroid Build Coastguard Worker   // Spilling is generally expensive on all PPC cores, so always enable
209*9880d681SAndroid Build Coastguard Worker   // register-pressure tracking.
210*9880d681SAndroid Build Coastguard Worker   Policy.ShouldTrackPressure = true;
211*9880d681SAndroid Build Coastguard Worker }
212*9880d681SAndroid Build Coastguard Worker 
useAA() const213*9880d681SAndroid Build Coastguard Worker bool PPCSubtarget::useAA() const {
214*9880d681SAndroid Build Coastguard Worker   // Use AA during code generation for the embedded cores.
215*9880d681SAndroid Build Coastguard Worker   return needsAggressiveScheduling(DarwinDirective);
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker 
enableSubRegLiveness() const218*9880d681SAndroid Build Coastguard Worker bool PPCSubtarget::enableSubRegLiveness() const {
219*9880d681SAndroid Build Coastguard Worker   return UseSubRegLiveness;
220*9880d681SAndroid Build Coastguard Worker }
221*9880d681SAndroid Build Coastguard Worker 
classifyGlobalReference(const GlobalValue * GV) const222*9880d681SAndroid Build Coastguard Worker unsigned char PPCSubtarget::classifyGlobalReference(
223*9880d681SAndroid Build Coastguard Worker     const GlobalValue *GV) const {
224*9880d681SAndroid Build Coastguard Worker   // Note that currently we don't generate non-pic references.
225*9880d681SAndroid Build Coastguard Worker   // If a caller wants that, this will have to be updated.
226*9880d681SAndroid Build Coastguard Worker 
227*9880d681SAndroid Build Coastguard Worker   // Large code model always uses the TOC even for local symbols.
228*9880d681SAndroid Build Coastguard Worker   if (TM.getCodeModel() == CodeModel::Large)
229*9880d681SAndroid Build Coastguard Worker     return PPCII::MO_PIC_FLAG | PPCII::MO_NLP_FLAG;
230*9880d681SAndroid Build Coastguard Worker 
231*9880d681SAndroid Build Coastguard Worker   unsigned char flags = PPCII::MO_PIC_FLAG;
232*9880d681SAndroid Build Coastguard Worker 
233*9880d681SAndroid Build Coastguard Worker   // Only if the relocation mode is PIC do we have to worry about
234*9880d681SAndroid Build Coastguard Worker   // interposition. In all other cases we can use a slightly looser standard to
235*9880d681SAndroid Build Coastguard Worker   // decide how to access the symbol.
236*9880d681SAndroid Build Coastguard Worker   if (TM.getRelocationModel() == Reloc::PIC_) {
237*9880d681SAndroid Build Coastguard Worker     // If it's local, or it's non-default, it can't be interposed.
238*9880d681SAndroid Build Coastguard Worker     if (!GV->hasLocalLinkage() &&
239*9880d681SAndroid Build Coastguard Worker         GV->hasDefaultVisibility()) {
240*9880d681SAndroid Build Coastguard Worker       flags |= PPCII::MO_NLP_FLAG;
241*9880d681SAndroid Build Coastguard Worker     }
242*9880d681SAndroid Build Coastguard Worker     return flags;
243*9880d681SAndroid Build Coastguard Worker   }
244*9880d681SAndroid Build Coastguard Worker 
245*9880d681SAndroid Build Coastguard Worker   if (GV->isStrongDefinitionForLinker())
246*9880d681SAndroid Build Coastguard Worker     return flags;
247*9880d681SAndroid Build Coastguard Worker   return flags | PPCII::MO_NLP_FLAG;
248*9880d681SAndroid Build Coastguard Worker }
249*9880d681SAndroid Build Coastguard Worker 
isELFv2ABI() const250*9880d681SAndroid Build Coastguard Worker bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
isPPC64() const251*9880d681SAndroid Build Coastguard Worker bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }
252