OSDN Git Service

AMDGPU/SI: Avoid creating unnecessary copies in the SIFixSGPRCopies pass
[android-x86/external-llvm.git] / lib / Target / AMDGPU / SIRegisterInfo.h
1 //===-- SIRegisterInfo.h - SI Register Info Interface ----------*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// \brief Interface definition for SIRegisterInfo
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_TARGET_AMDGPU_SIREGISTERINFO_H
16 #define LLVM_LIB_TARGET_AMDGPU_SIREGISTERINFO_H
17
18 #include "AMDGPURegisterInfo.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20
21 namespace llvm {
22
23 class SISubtarget;
24 class MachineRegisterInfo;
25
26 class SIRegisterInfo final : public AMDGPURegisterInfo {
27 private:
28   unsigned SGPRSetID;
29   unsigned VGPRSetID;
30   BitVector SGPRPressureSets;
31   BitVector VGPRPressureSets;
32
33   void reserveRegisterTuples(BitVector &, unsigned Reg) const;
34   void classifyPressureSet(unsigned PSetID, unsigned Reg,
35                            BitVector &PressureSets) const;
36
37 public:
38   SIRegisterInfo();
39
40   /// Return the end register initially reserved for the scratch buffer in case
41   /// spilling is needed.
42   unsigned reservedPrivateSegmentBufferReg(const MachineFunction &MF) const;
43
44   /// Return the end register initially reserved for the scratch wave offset in
45   /// case spilling is needed.
46   unsigned reservedPrivateSegmentWaveByteOffsetReg(
47     const MachineFunction &MF) const;
48
49   BitVector getReservedRegs(const MachineFunction &MF) const override;
50
51   bool requiresRegisterScavenging(const MachineFunction &Fn) const override;
52
53
54   bool requiresFrameIndexScavenging(const MachineFunction &MF) const override;
55   bool requiresVirtualBaseRegisters(const MachineFunction &Fn) const override;
56   bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const override;
57
58   int64_t getFrameIndexInstrOffset(const MachineInstr *MI,
59                                    int Idx) const override;
60
61   bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const override;
62
63   void materializeFrameBaseRegister(MachineBasicBlock *MBB,
64                                     unsigned BaseReg, int FrameIdx,
65                                     int64_t Offset) const override;
66
67   void resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
68                          int64_t Offset) const override;
69
70   bool isFrameOffsetLegal(const MachineInstr *MI, unsigned BaseReg,
71                           int64_t Offset) const override;
72
73   const TargetRegisterClass *getPointerRegClass(
74     const MachineFunction &MF, unsigned Kind = 0) const override;
75
76   void spillSGPR(MachineBasicBlock::iterator MI,
77                  int FI, RegScavenger *RS) const;
78
79   void restoreSGPR(MachineBasicBlock::iterator MI,
80                    int FI, RegScavenger *RS) const;
81
82   void eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj,
83                            unsigned FIOperandNum,
84                            RegScavenger *RS) const override;
85
86   unsigned getHWRegIndex(unsigned Reg) const {
87     return getEncodingValue(Reg) & 0xff;
88   }
89
90   /// \brief Return the 'base' register class for this register.
91   /// e.g. SGPR0 => SReg_32, VGPR => VGPR_32 SGPR0_SGPR1 -> SReg_32, etc.
92   const TargetRegisterClass *getPhysRegClass(unsigned Reg) const;
93
94   /// \returns true if this class contains only SGPR registers
95   bool isSGPRClass(const TargetRegisterClass *RC) const {
96     return !hasVGPRs(RC);
97   }
98
99   /// \returns true if this class ID contains only SGPR registers
100   bool isSGPRClassID(unsigned RCID) const {
101     return isSGPRClass(getRegClass(RCID));
102   }
103
104   bool isSGPRReg(const MachineRegisterInfo &MRI, unsigned Reg) const {
105     const TargetRegisterClass *RC;
106     if (TargetRegisterInfo::isVirtualRegister(Reg))
107       RC = MRI.getRegClass(Reg);
108     else
109       RC = getPhysRegClass(Reg);
110     return isSGPRClass(RC);
111   }
112
113   /// \returns true if this class contains VGPR registers.
114   bool hasVGPRs(const TargetRegisterClass *RC) const;
115
116   /// \returns A VGPR reg class with the same width as \p SRC
117   const TargetRegisterClass *getEquivalentVGPRClass(
118                                           const TargetRegisterClass *SRC) const;
119
120   /// \returns A SGPR reg class with the same width as \p SRC
121   const TargetRegisterClass *getEquivalentSGPRClass(
122                                            const TargetRegisterClass *VRC) const;
123
124   /// \returns The register class that is used for a sub-register of \p RC for
125   /// the given \p SubIdx.  If \p SubIdx equals NoSubRegister, \p RC will
126   /// be returned.
127   const TargetRegisterClass *getSubRegClass(const TargetRegisterClass *RC,
128                                             unsigned SubIdx) const;
129
130   bool shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
131                             unsigned DefSubReg,
132                             const TargetRegisterClass *SrcRC,
133                             unsigned SrcSubReg) const override;
134
135   /// \returns True if operands defined with this operand type can accept
136   /// a literal constant (i.e. any 32-bit immediate).
137   bool opCanUseLiteralConstant(unsigned OpType) const;
138
139   /// \returns True if operands defined with this operand type can accept
140   /// an inline constant. i.e. An integer value in the range (-16, 64) or
141   /// -4.0f, -2.0f, -1.0f, -0.5f, 0.0f, 0.5f, 1.0f, 2.0f, 4.0f.
142   bool opCanUseInlineConstant(unsigned OpType) const;
143
144   enum PreloadedValue {
145     // SGPRS:
146     PRIVATE_SEGMENT_BUFFER = 0,
147     DISPATCH_PTR        =  1,
148     QUEUE_PTR           =  2,
149     KERNARG_SEGMENT_PTR =  3,
150     DISPATCH_ID         =  4,
151     FLAT_SCRATCH_INIT   =  5,
152     WORKGROUP_ID_X      = 10,
153     WORKGROUP_ID_Y      = 11,
154     WORKGROUP_ID_Z      = 12,
155     PRIVATE_SEGMENT_WAVE_BYTE_OFFSET = 14,
156
157     // VGPRS:
158     FIRST_VGPR_VALUE    = 15,
159     WORKITEM_ID_X       = FIRST_VGPR_VALUE,
160     WORKITEM_ID_Y       = 16,
161     WORKITEM_ID_Z       = 17
162   };
163
164   /// \brief Returns the physical register that \p Value is stored in.
165   unsigned getPreloadedValue(const MachineFunction &MF,
166                              enum PreloadedValue Value) const;
167
168   unsigned findUnusedRegister(const MachineRegisterInfo &MRI,
169                               const TargetRegisterClass *RC,
170                               const MachineFunction &MF) const;
171
172   unsigned getSGPRPressureSet() const { return SGPRSetID; };
173   unsigned getVGPRPressureSet() const { return VGPRSetID; };
174
175   const TargetRegisterClass *getRegClassForReg(const MachineRegisterInfo &MRI,
176                                                unsigned Reg) const;
177   bool isVGPR(const MachineRegisterInfo &MRI, unsigned Reg) const;
178
179   bool isSGPRPressureSet(unsigned SetID) const {
180     return SGPRPressureSets.test(SetID) && !VGPRPressureSets.test(SetID);
181   }
182   bool isVGPRPressureSet(unsigned SetID) const {
183     return VGPRPressureSets.test(SetID) && !SGPRPressureSets.test(SetID);
184   }
185
186   /// \returns SGPR allocation granularity supported by the subtarget.
187   unsigned getSGPRAllocGranule() const {
188     return 8;
189   }
190
191   /// \returns Total number of SGPRs supported by the subtarget.
192   unsigned getTotalNumSGPRs(const SISubtarget &ST) const;
193
194   /// \returns Number of addressable SGPRs supported by the subtarget.
195   unsigned getNumAddressableSGPRs(const SISubtarget &ST) const;
196
197   /// \returns Number of reserved SGPRs supported by the subtarget.
198   unsigned getNumReservedSGPRs(const SISubtarget &ST) const;
199
200   /// \returns Minimum number of SGPRs that meets given number of waves per
201   /// execution unit requirement for given subtarget.
202   unsigned getMinNumSGPRs(const SISubtarget &ST, unsigned WavesPerEU) const;
203
204   /// \returns Maximum number of SGPRs that meets given number of waves per
205   /// execution unit requirement for given subtarget.
206   unsigned getMaxNumSGPRs(const SISubtarget &ST, unsigned WavesPerEU) const;
207
208   /// \returns Maximum number of SGPRs that meets number of waves per execution
209   /// unit requirement for function \p MF, or number of SGPRs explicitly
210   /// requested using "amdgpu-num-sgpr" attribute attached to function \p MF.
211   ///
212   /// \returns Value that meets number of waves per execution unit requirement
213   /// if explicitly requested value cannot be converted to integer, violates
214   /// subtarget's specifications, or does not meet number of waves per execution
215   /// unit requirement.
216   unsigned getMaxNumSGPRs(const MachineFunction &MF) const;
217
218   /// \returns VGPR allocation granularity supported by the subtarget.
219   unsigned getVGPRAllocGranule() const {
220     return 4;
221   }
222
223   /// \returns Total number of VGPRs supported by the subtarget.
224   unsigned getTotalNumVGPRs() const {
225     return 256;
226   }
227
228   /// \returns Number of reserved VGPRs for debugger use supported by the
229   /// subtarget.
230   unsigned getNumDebuggerReservedVGPRs(const SISubtarget &ST) const;
231
232   /// \returns Minimum number of SGPRs that meets given number of waves per
233   /// execution unit requirement.
234   unsigned getMinNumVGPRs(unsigned WavesPerEU) const;
235
236   /// \returns Maximum number of VGPRs that meets given number of waves per
237   /// execution unit requirement.
238   unsigned getMaxNumVGPRs(unsigned WavesPerEU) const;
239
240   /// \returns Maximum number of VGPRs that meets number of waves per execution
241   /// unit requirement for function \p MF, or number of VGPRs explicitly
242   /// requested using "amdgpu-num-vgpr" attribute attached to function \p MF.
243   ///
244   /// \returns Value that meets number of waves per execution unit requirement
245   /// if explicitly requested value cannot be converted to integer, violates
246   /// subtarget's specifications, or does not meet number of waves per execution
247   /// unit requirement.
248   unsigned getMaxNumVGPRs(const MachineFunction &MF) const;
249
250   ArrayRef<int16_t> getRegSplitParts(const TargetRegisterClass *RC,
251                                      unsigned EltSize) const;
252
253 private:
254   void buildSpillLoadStore(MachineBasicBlock::iterator MI,
255                            unsigned LoadStoreOp, const MachineOperand *SrcDst,
256                            unsigned ScratchRsrcReg, unsigned ScratchOffset,
257                            int64_t Offset,
258                            RegScavenger *RS) const;
259 };
260
261 } // End namespace llvm
262
263 #endif