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