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