1 //===-- PPCSubtarget.h - Define Subtarget for the PPC ----------*- C++ -*--===//
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 declares the PowerPC specific subclass of TargetSubtargetInfo.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_POWERPC_PPCSUBTARGET_H
14 #define LLVM_LIB_TARGET_POWERPC_PPCSUBTARGET_H
15 
16 #include "PPCFrameLowering.h"
17 #include "PPCISelLowering.h"
18 #include "PPCInstrInfo.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/CodeGen/GlobalISel/CallLowering.h"
21 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
22 #include "llvm/CodeGen/RegisterBankInfo.h"
23 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
24 #include "llvm/CodeGen/TargetSubtargetInfo.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/MC/MCInstrItineraries.h"
27 #include <string>
28 
29 #define GET_SUBTARGETINFO_HEADER
30 #include "PPCGenSubtargetInfo.inc"
31 
32 // GCC #defines PPC on Linux but we use it as our namespace name
33 #undef PPC
34 
35 namespace llvm {
36 class StringRef;
37 
38 namespace PPC {
39   // -m directive values.
40 enum {
41   DIR_NONE,
42   DIR_32,
43   DIR_440,
44   DIR_601,
45   DIR_602,
46   DIR_603,
47   DIR_7400,
48   DIR_750,
49   DIR_970,
50   DIR_A2,
51   DIR_E500,
52   DIR_E500mc,
53   DIR_E5500,
54   DIR_PWR3,
55   DIR_PWR4,
56   DIR_PWR5,
57   DIR_PWR5X,
58   DIR_PWR6,
59   DIR_PWR6X,
60   DIR_PWR7,
61   DIR_PWR8,
62   DIR_PWR9,
63   DIR_PWR10,
64   DIR_PWR_FUTURE,
65   DIR_64
66 };
67 }
68 
69 class GlobalValue;
70 
71 class PPCSubtarget : public PPCGenSubtargetInfo {
72 public:
73   enum POPCNTDKind {
74     POPCNTD_Unavailable,
75     POPCNTD_Slow,
76     POPCNTD_Fast
77   };
78 
79 protected:
80   /// TargetTriple - What processor and OS we're targeting.
81   Triple TargetTriple;
82 
83   /// stackAlignment - The minimum alignment known to hold of the stack frame on
84   /// entry to the function and which must be maintained by every function.
85   Align StackAlignment;
86 
87   /// Selected instruction itineraries (one entry per itinerary class.)
88   InstrItineraryData InstrItins;
89 
90   /// Which cpu directive was used.
91   unsigned CPUDirective;
92 
93   /// Used by the ISel to turn in optimizations for POWER4-derived architectures
94   bool HasMFOCRF;
95   bool Has64BitSupport;
96   bool Use64BitRegs;
97   bool UseCRBits;
98   bool HasHardFloat;
99   bool IsPPC64;
100   bool HasAltivec;
101   bool HasFPU;
102   bool HasSPE;
103   bool HasEFPU2;
104   bool HasVSX;
105   bool NeedsTwoConstNR;
106   bool HasP8Vector;
107   bool HasP8Altivec;
108   bool HasP8Crypto;
109   bool HasP9Vector;
110   bool HasP9Altivec;
111   bool HasP10Vector;
112   bool HasPrefixInstrs;
113   bool HasPCRelativeMemops;
114   bool HasMMA;
115   bool HasROPProtect;
116   bool HasPrivileged;
117   bool HasFCPSGN;
118   bool HasFSQRT;
119   bool HasFRE, HasFRES, HasFRSQRTE, HasFRSQRTES;
120   bool HasRecipPrec;
121   bool HasSTFIWX;
122   bool HasLFIWAX;
123   bool HasFPRND;
124   bool HasFPCVT;
125   bool HasISEL;
126   bool HasBPERMD;
127   bool HasExtDiv;
128   bool HasCMPB;
129   bool HasLDBRX;
130   bool IsBookE;
131   bool HasOnlyMSYNC;
132   bool IsE500;
133   bool IsPPC4xx;
134   bool IsPPC6xx;
135   bool FeatureMFTB;
136   bool AllowsUnalignedFPAccess;
137   bool DeprecatedDST;
138   bool IsLittleEndian;
139   bool HasICBT;
140   bool HasInvariantFunctionDescriptors;
141   bool HasPartwordAtomics;
142   bool HasQuadwordAtomics;
143   bool HasDirectMove;
144   bool HasHTM;
145   bool HasFloat128;
146   bool HasFusion;
147   bool HasStoreFusion;
148   bool HasAddiLoadFusion;
149   bool HasAddisLoadFusion;
150   bool HasArithAddFusion;
151   bool HasAddLogicalFusion;
152   bool HasLogicalAddFusion;
153   bool HasLogicalFusion;
154   bool HasSha3Fusion;
155   bool HasCompareFusion;
156   bool HasWideImmFusion;
157   bool HasZeroMoveFusion;
158   bool HasBack2BackFusion;
159   bool IsISA2_06;
160   bool IsISA2_07;
161   bool IsISA3_0;
162   bool IsISA3_1;
163   bool IsISAFuture;
164   bool UseLongCalls;
165   bool SecurePlt;
166   bool VectorsUseTwoUnits;
167   bool UsePPCPreRASchedStrategy;
168   bool UsePPCPostRASchedStrategy;
169   bool PairedVectorMemops;
170   bool PredictableSelectIsExpensive;
171   bool HasModernAIXAs;
172   bool IsAIX;
173 
174   POPCNTDKind HasPOPCNTD;
175 
176   const PPCTargetMachine &TM;
177   PPCFrameLowering FrameLowering;
178   PPCInstrInfo InstrInfo;
179   PPCTargetLowering TLInfo;
180   SelectionDAGTargetInfo TSInfo;
181 
182   /// GlobalISel related APIs.
183   std::unique_ptr<CallLowering> CallLoweringInfo;
184   std::unique_ptr<LegalizerInfo> Legalizer;
185   std::unique_ptr<RegisterBankInfo> RegBankInfo;
186   std::unique_ptr<InstructionSelector> InstSelector;
187 
188 public:
189   /// This constructor initializes the data members to match that
190   /// of the specified triple.
191   ///
192   PPCSubtarget(const Triple &TT, const std::string &CPU, const std::string &FS,
193                const PPCTargetMachine &TM);
194 
195   /// ParseSubtargetFeatures - Parses features string setting specified
196   /// subtarget options.  Definition of function is auto generated by tblgen.
197   void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
198 
199   /// getStackAlignment - Returns the minimum alignment known to hold of the
200   /// stack frame on entry to the function and which must be maintained by every
201   /// function for this subtarget.
getStackAlignment()202   Align getStackAlignment() const { return StackAlignment; }
203 
204   /// getCPUDirective - Returns the -m directive specified for the cpu.
205   ///
getCPUDirective()206   unsigned getCPUDirective() const { return CPUDirective; }
207 
208   /// getInstrItins - Return the instruction itineraries based on subtarget
209   /// selection.
getInstrItineraryData()210   const InstrItineraryData *getInstrItineraryData() const override {
211     return &InstrItins;
212   }
213 
getFrameLowering()214   const PPCFrameLowering *getFrameLowering() const override {
215     return &FrameLowering;
216   }
getInstrInfo()217   const PPCInstrInfo *getInstrInfo() const override { return &InstrInfo; }
getTargetLowering()218   const PPCTargetLowering *getTargetLowering() const override {
219     return &TLInfo;
220   }
getSelectionDAGInfo()221   const SelectionDAGTargetInfo *getSelectionDAGInfo() const override {
222     return &TSInfo;
223   }
getRegisterInfo()224   const PPCRegisterInfo *getRegisterInfo() const override {
225     return &getInstrInfo()->getRegisterInfo();
226   }
getTargetMachine()227   const PPCTargetMachine &getTargetMachine() const { return TM; }
228 
229   /// initializeSubtargetDependencies - Initializes using a CPU and feature string
230   /// so that we can use initializer lists for subtarget initialization.
231   PPCSubtarget &initializeSubtargetDependencies(StringRef CPU, StringRef FS);
232 
233 private:
234   void initializeEnvironment();
235   void initSubtargetFeatures(StringRef CPU, StringRef FS);
236 
237 public:
238   /// isPPC64 - Return true if we are generating code for 64-bit pointer mode.
239   ///
240   bool isPPC64() const;
241 
242   /// has64BitSupport - Return true if the selected CPU supports 64-bit
243   /// instructions, regardless of whether we are in 32-bit or 64-bit mode.
has64BitSupport()244   bool has64BitSupport() const { return Has64BitSupport; }
245   // useSoftFloat - Return true if soft-float option is turned on.
useSoftFloat()246   bool useSoftFloat() const {
247     if (isAIXABI() && !HasHardFloat)
248       report_fatal_error("soft-float is not yet supported on AIX.");
249     return !HasHardFloat;
250   }
251 
252   /// use64BitRegs - Return true if in 64-bit mode or if we should use 64-bit
253   /// registers in 32-bit mode when possible.  This can only true if
254   /// has64BitSupport() returns true.
use64BitRegs()255   bool use64BitRegs() const { return Use64BitRegs; }
256 
257   /// useCRBits - Return true if we should store and manipulate i1 values in
258   /// the individual condition register bits.
useCRBits()259   bool useCRBits() const { return UseCRBits; }
260 
261   // isLittleEndian - True if generating little-endian code
isLittleEndian()262   bool isLittleEndian() const { return IsLittleEndian; }
263 
264   // Specific obvious features.
hasFCPSGN()265   bool hasFCPSGN() const { return HasFCPSGN; }
hasFSQRT()266   bool hasFSQRT() const { return HasFSQRT; }
hasFRE()267   bool hasFRE() const { return HasFRE; }
hasFRES()268   bool hasFRES() const { return HasFRES; }
hasFRSQRTE()269   bool hasFRSQRTE() const { return HasFRSQRTE; }
hasFRSQRTES()270   bool hasFRSQRTES() const { return HasFRSQRTES; }
hasRecipPrec()271   bool hasRecipPrec() const { return HasRecipPrec; }
hasSTFIWX()272   bool hasSTFIWX() const { return HasSTFIWX; }
hasLFIWAX()273   bool hasLFIWAX() const { return HasLFIWAX; }
hasFPRND()274   bool hasFPRND() const { return HasFPRND; }
hasFPCVT()275   bool hasFPCVT() const { return HasFPCVT; }
hasAltivec()276   bool hasAltivec() const { return HasAltivec; }
hasSPE()277   bool hasSPE() const { return HasSPE; }
hasEFPU2()278   bool hasEFPU2() const { return HasEFPU2; }
hasFPU()279   bool hasFPU() const { return HasFPU; }
hasVSX()280   bool hasVSX() const { return HasVSX; }
needsTwoConstNR()281   bool needsTwoConstNR() const { return NeedsTwoConstNR; }
hasP8Vector()282   bool hasP8Vector() const { return HasP8Vector; }
hasP8Altivec()283   bool hasP8Altivec() const { return HasP8Altivec; }
hasP8Crypto()284   bool hasP8Crypto() const { return HasP8Crypto; }
hasP9Vector()285   bool hasP9Vector() const { return HasP9Vector; }
hasP9Altivec()286   bool hasP9Altivec() const { return HasP9Altivec; }
hasP10Vector()287   bool hasP10Vector() const { return HasP10Vector; }
hasPrefixInstrs()288   bool hasPrefixInstrs() const { return HasPrefixInstrs; }
hasPCRelativeMemops()289   bool hasPCRelativeMemops() const { return HasPCRelativeMemops; }
hasMMA()290   bool hasMMA() const { return HasMMA; }
hasROPProtect()291   bool hasROPProtect() const { return HasROPProtect; }
hasPrivileged()292   bool hasPrivileged() const { return HasPrivileged; }
pairedVectorMemops()293   bool pairedVectorMemops() const { return PairedVectorMemops; }
hasMFOCRF()294   bool hasMFOCRF() const { return HasMFOCRF; }
hasISEL()295   bool hasISEL() const { return HasISEL; }
hasBPERMD()296   bool hasBPERMD() const { return HasBPERMD; }
hasExtDiv()297   bool hasExtDiv() const { return HasExtDiv; }
hasCMPB()298   bool hasCMPB() const { return HasCMPB; }
hasLDBRX()299   bool hasLDBRX() const { return HasLDBRX; }
isBookE()300   bool isBookE() const { return IsBookE; }
hasOnlyMSYNC()301   bool hasOnlyMSYNC() const { return HasOnlyMSYNC; }
isPPC4xx()302   bool isPPC4xx() const { return IsPPC4xx; }
isPPC6xx()303   bool isPPC6xx() const { return IsPPC6xx; }
isSecurePlt()304   bool isSecurePlt() const {return SecurePlt; }
vectorsUseTwoUnits()305   bool vectorsUseTwoUnits() const {return VectorsUseTwoUnits; }
isE500()306   bool isE500() const { return IsE500; }
isFeatureMFTB()307   bool isFeatureMFTB() const { return FeatureMFTB; }
allowsUnalignedFPAccess()308   bool allowsUnalignedFPAccess() const { return AllowsUnalignedFPAccess; }
isDeprecatedDST()309   bool isDeprecatedDST() const { return DeprecatedDST; }
hasICBT()310   bool hasICBT() const { return HasICBT; }
hasInvariantFunctionDescriptors()311   bool hasInvariantFunctionDescriptors() const {
312     return HasInvariantFunctionDescriptors;
313   }
usePPCPreRASchedStrategy()314   bool usePPCPreRASchedStrategy() const { return UsePPCPreRASchedStrategy; }
usePPCPostRASchedStrategy()315   bool usePPCPostRASchedStrategy() const { return UsePPCPostRASchedStrategy; }
hasPartwordAtomics()316   bool hasPartwordAtomics() const { return HasPartwordAtomics; }
hasQuadwordAtomics()317   bool hasQuadwordAtomics() const { return HasQuadwordAtomics; }
hasDirectMove()318   bool hasDirectMove() const { return HasDirectMove; }
319 
getPlatformStackAlignment()320   Align getPlatformStackAlignment() const {
321     return Align(16);
322   }
323 
getRedZoneSize()324   unsigned  getRedZoneSize() const {
325     if (isPPC64())
326       // 288 bytes = 18*8 (FPRs) + 18*8 (GPRs, GPR13 reserved)
327       return 288;
328 
329     // AIX PPC32: 220 bytes = 18*8 (FPRs) + 19*4 (GPRs);
330     // PPC32 SVR4ABI has no redzone.
331     return isAIXABI() ? 220 : 0;
332   }
333 
hasHTM()334   bool hasHTM() const { return HasHTM; }
hasFloat128()335   bool hasFloat128() const { return HasFloat128; }
isISA2_06()336   bool isISA2_06() const { return IsISA2_06; }
isISA2_07()337   bool isISA2_07() const { return IsISA2_07; }
isISA3_0()338   bool isISA3_0() const { return IsISA3_0; }
isISA3_1()339   bool isISA3_1() const { return IsISA3_1; }
isISAFuture()340   bool isISAFuture() const { return IsISAFuture; }
useLongCalls()341   bool useLongCalls() const { return UseLongCalls; }
hasFusion()342   bool hasFusion() const { return HasFusion; }
hasStoreFusion()343   bool hasStoreFusion() const { return HasStoreFusion; }
hasAddiLoadFusion()344   bool hasAddiLoadFusion() const { return HasAddiLoadFusion; }
hasAddisLoadFusion()345   bool hasAddisLoadFusion() const { return HasAddisLoadFusion; }
hasArithAddFusion()346   bool hasArithAddFusion() const { return HasArithAddFusion; }
hasAddLogicalFusion()347   bool hasAddLogicalFusion() const { return HasAddLogicalFusion; }
hasLogicalAddFusion()348   bool hasLogicalAddFusion() const { return HasLogicalAddFusion; }
hasLogicalFusion()349   bool hasLogicalFusion() const { return HasLogicalFusion; }
hasCompareFusion()350   bool hasCompareFusion() const { return HasCompareFusion; }
hasWideImmFusion()351   bool hasWideImmFusion() const { return HasWideImmFusion; }
hasSha3Fusion()352   bool hasSha3Fusion() const { return HasSha3Fusion; }
hasZeroMoveFusion()353   bool hasZeroMoveFusion() const { return HasZeroMoveFusion; }
hasBack2BackFusion()354   bool hasBack2BackFusion() const { return HasBack2BackFusion; }
needsSwapsForVSXMemOps()355   bool needsSwapsForVSXMemOps() const {
356     return hasVSX() && isLittleEndian() && !hasP9Vector();
357   }
358 
hasPOPCNTD()359   POPCNTDKind hasPOPCNTD() const { return HasPOPCNTD; }
360 
getTargetTriple()361   const Triple &getTargetTriple() const { return TargetTriple; }
362 
isTargetELF()363   bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
isTargetMachO()364   bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }
isTargetLinux()365   bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
366 
isAIXABI()367   bool isAIXABI() const { return TargetTriple.isOSAIX(); }
isSVR4ABI()368   bool isSVR4ABI() const { return !isAIXABI(); }
369   bool isELFv2ABI() const;
370 
is64BitELFABI()371   bool is64BitELFABI() const { return  isSVR4ABI() && isPPC64(); }
is32BitELFABI()372   bool is32BitELFABI() const { return  isSVR4ABI() && !isPPC64(); }
373   bool isUsingPCRelativeCalls() const;
374 
375   /// Originally, this function return hasISEL(). Now we always enable it,
376   /// but may expand the ISEL instruction later.
enableEarlyIfConversion()377   bool enableEarlyIfConversion() const override { return true; }
378 
379   /// Scheduling customization.
380   bool enableMachineScheduler() const override;
381   /// Pipeliner customization.
382   bool enableMachinePipeliner() const override;
383   /// Machine Pipeliner customization
384   bool useDFAforSMS() const override;
385   /// This overrides the PostRAScheduler bit in the SchedModel for each CPU.
386   bool enablePostRAScheduler() const override;
387   AntiDepBreakMode getAntiDepBreakMode() const override;
388   void getCriticalPathRCs(RegClassVector &CriticalPathRCs) const override;
389 
390   void overrideSchedPolicy(MachineSchedPolicy &Policy,
391                            unsigned NumRegionInstrs) const override;
392   bool useAA() const override;
393 
394   bool enableSubRegLiveness() const override;
395 
396   /// True if the GV will be accessed via an indirect symbol.
397   bool isGVIndirectSymbol(const GlobalValue *GV) const;
398 
399   /// True if the ABI is descriptor based.
usesFunctionDescriptors()400   bool usesFunctionDescriptors() const {
401     // Both 32-bit and 64-bit AIX are descriptor based. For ELF only the 64-bit
402     // v1 ABI uses descriptors.
403     return isAIXABI() || (is64BitELFABI() && !isELFv2ABI());
404   }
405 
descriptorTOCAnchorOffset()406   unsigned descriptorTOCAnchorOffset() const {
407     assert(usesFunctionDescriptors() &&
408            "Should only be called when the target uses descriptors.");
409     return IsPPC64 ? 8 : 4;
410   }
411 
descriptorEnvironmentPointerOffset()412   unsigned descriptorEnvironmentPointerOffset() const {
413     assert(usesFunctionDescriptors() &&
414            "Should only be called when the target uses descriptors.");
415     return IsPPC64 ? 16 : 8;
416   }
417 
getEnvironmentPointerRegister()418   MCRegister getEnvironmentPointerRegister() const {
419     assert(usesFunctionDescriptors() &&
420            "Should only be called when the target uses descriptors.");
421      return IsPPC64 ? PPC::X11 : PPC::R11;
422   }
423 
getTOCPointerRegister()424   MCRegister getTOCPointerRegister() const {
425     assert((is64BitELFABI() || isAIXABI()) &&
426            "Should only be called when the target is a TOC based ABI.");
427     return IsPPC64 ? PPC::X2 : PPC::R2;
428   }
429 
getStackPointerRegister()430   MCRegister getStackPointerRegister() const {
431     return IsPPC64 ? PPC::X1 : PPC::R1;
432   }
433 
isXRaySupported()434   bool isXRaySupported() const override { return IsPPC64 && IsLittleEndian; }
435 
isPredictableSelectIsExpensive()436   bool isPredictableSelectIsExpensive() const {
437     return PredictableSelectIsExpensive;
438   }
439 
440   // Select allocation orders of GPRC and G8RC. It should be strictly consistent
441   // with corresponding AltOrders in PPCRegisterInfo.td.
getGPRAllocationOrderIdx()442   unsigned getGPRAllocationOrderIdx() const {
443     if (is64BitELFABI())
444       return 1;
445     if (isAIXABI())
446       return 2;
447     return 0;
448   }
449 
450   // GlobalISEL
451   const CallLowering *getCallLowering() const override;
452   const RegisterBankInfo *getRegBankInfo() const override;
453   const LegalizerInfo *getLegalizerInfo() const override;
454   InstructionSelector *getInstructionSelector() const override;
455 };
456 } // End llvm namespace
457 
458 #endif
459