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/GlobalISel/InstructionSelector.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineScheduler.h"
24 #include "llvm/IR/Attributes.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/GlobalValue.h"
27 #include "llvm/MC/TargetRegistry.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include <cstdlib>
31 
32 using 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 
40 static cl::opt<bool>
41     UseSubRegLiveness("ppc-track-subreg-liveness",
42                       cl::desc("Enable subregister liveness tracking for PPC"),
43                       cl::init(true), cl::Hidden);
44 
45 static cl::opt<bool>
46     EnableMachinePipeliner("ppc-enable-pipeliner",
47                            cl::desc("Enable Machine Pipeliner for PPC"),
48                            cl::init(false), cl::Hidden);
49 
50 PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
51                                                             StringRef TuneCPU,
52                                                             StringRef FS) {
53   initializeEnvironment();
54   initSubtargetFeatures(CPU, TuneCPU, FS);
55   return *this;
56 }
57 
58 PPCSubtarget::PPCSubtarget(const Triple &TT, const std::string &CPU,
59                            const std::string &TuneCPU, const std::string &FS,
60                            const PPCTargetMachine &TM)
61     : PPCGenSubtargetInfo(TT, CPU, TuneCPU, FS), TargetTriple(TT),
62       IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
63               TargetTriple.getArch() == Triple::ppc64le),
64       TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, TuneCPU, FS)),
65       InstrInfo(*this), TLInfo(TM, *this) {
66   CallLoweringInfo.reset(new PPCCallLowering(*getTargetLowering()));
67   Legalizer.reset(new PPCLegalizerInfo(*this));
68   auto *RBI = new PPCRegisterBankInfo(*getRegisterInfo());
69   RegBankInfo.reset(RBI);
70 
71   InstSelector.reset(createPPCInstructionSelector(
72       *static_cast<const PPCTargetMachine *>(&TM), *this, *RBI));
73 }
74 
75 void PPCSubtarget::initializeEnvironment() {
76   StackAlignment = Align(16);
77   CPUDirective = PPC::DIR_NONE;
78   HasPOPCNTD = POPCNTD_Unavailable;
79 }
80 
81 void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef TuneCPU,
82                                          StringRef FS) {
83   // Determine default and user specified characteristics
84   std::string CPUName = std::string(CPU);
85   if (CPUName.empty() || CPU == "generic") {
86     // If cross-compiling with -march=ppc64le without -mcpu
87     if (TargetTriple.getArch() == Triple::ppc64le)
88       CPUName = "ppc64le";
89     else if (TargetTriple.getSubArch() == Triple::PPCSubArch_spe)
90       CPUName = "e500";
91     else
92       CPUName = "generic";
93   }
94 
95   // Determine the CPU to schedule for.
96   if (TuneCPU.empty()) TuneCPU = CPUName;
97 
98   // Initialize scheduling itinerary for the specified CPU.
99   InstrItins = getInstrItineraryForCPU(CPUName);
100 
101   // Parse features string.
102   ParseSubtargetFeatures(CPUName, TuneCPU, FS);
103 
104   // If the user requested use of 64-bit regs, but the cpu selected doesn't
105   // support it, ignore.
106   if (IsPPC64 && has64BitSupport())
107     Use64BitRegs = true;
108 
109   if (TargetTriple.isPPC32SecurePlt())
110     IsSecurePlt = true;
111 
112   if (HasSPE && IsPPC64)
113     report_fatal_error( "SPE is only supported for 32-bit targets.\n", false);
114   if (HasSPE && (HasAltivec || HasVSX || HasFPU))
115     report_fatal_error(
116         "SPE and traditional floating point cannot both be enabled.\n", false);
117 
118   // If not SPE, set standard FPU
119   if (!HasSPE)
120     HasFPU = true;
121 
122   StackAlignment = getPlatformStackAlignment();
123 
124   // Determine endianness.
125   IsLittleEndian = TM.isLittleEndian();
126 }
127 
128 bool PPCSubtarget::enableMachineScheduler() const { return true; }
129 
130 bool PPCSubtarget::enableMachinePipeliner() const {
131   return getSchedModel().hasInstrSchedModel() && EnableMachinePipeliner;
132 }
133 
134 bool PPCSubtarget::useDFAforSMS() const { return false; }
135 
136 // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
137 bool PPCSubtarget::enablePostRAScheduler() const { return true; }
138 
139 PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
140   return TargetSubtargetInfo::ANTIDEP_ALL;
141 }
142 
143 void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
144   CriticalPathRCs.clear();
145   CriticalPathRCs.push_back(isPPC64() ?
146                             &PPC::G8RCRegClass : &PPC::GPRCRegClass);
147 }
148 
149 void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
150                                        unsigned NumRegionInstrs) const {
151   // The GenericScheduler that we use defaults to scheduling bottom up only.
152   // We want to schedule from both the top and the bottom and so we set
153   // OnlyBottomUp to false.
154   // We want to do bi-directional scheduling since it provides a more balanced
155   // schedule leading to better performance.
156   Policy.OnlyBottomUp = false;
157   // Spilling is generally expensive on all PPC cores, so always enable
158   // register-pressure tracking.
159   Policy.ShouldTrackPressure = true;
160 }
161 
162 bool PPCSubtarget::useAA() const {
163   return true;
164 }
165 
166 bool PPCSubtarget::enableSubRegLiveness() const {
167   return UseSubRegLiveness;
168 }
169 
170 bool PPCSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
171   // Large code model always uses the TOC even for local symbols.
172   if (TM.getCodeModel() == CodeModel::Large)
173     return true;
174   if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
175     return false;
176   return true;
177 }
178 
179 bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
180 bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }
181 
182 bool PPCSubtarget::isUsingPCRelativeCalls() const {
183   return isPPC64() && hasPCRelativeMemops() && isELFv2ABI() &&
184          CodeModel::Medium == getTargetMachine().getCodeModel();
185 }
186 
187 // GlobalISEL
188 const CallLowering *PPCSubtarget::getCallLowering() const {
189   return CallLoweringInfo.get();
190 }
191 
192 const RegisterBankInfo *PPCSubtarget::getRegBankInfo() const {
193   return RegBankInfo.get();
194 }
195 
196 const LegalizerInfo *PPCSubtarget::getLegalizerInfo() const {
197   return Legalizer.get();
198 }
199 
200 InstructionSelector *PPCSubtarget::getInstructionSelector() const {
201   return InstSelector.get();
202 }
203