R600/SI: Implement findCommutedOpIndices
[oota-llvm.git] / lib / Target / R600 / SIInstrInfo.h
1 //===-- SIInstrInfo.h - SI Instruction 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 SIInstrInfo.
12 //
13 //===----------------------------------------------------------------------===//
14
15
16 #ifndef LLVM_LIB_TARGET_R600_SIINSTRINFO_H
17 #define LLVM_LIB_TARGET_R600_SIINSTRINFO_H
18
19 #include "AMDGPUInstrInfo.h"
20 #include "SIRegisterInfo.h"
21
22 namespace llvm {
23
24 class SIInstrInfo : public AMDGPUInstrInfo {
25 private:
26   const SIRegisterInfo RI;
27
28   unsigned buildExtractSubReg(MachineBasicBlock::iterator MI,
29                               MachineRegisterInfo &MRI,
30                               MachineOperand &SuperReg,
31                               const TargetRegisterClass *SuperRC,
32                               unsigned SubIdx,
33                               const TargetRegisterClass *SubRC) const;
34   MachineOperand buildExtractSubRegOrImm(MachineBasicBlock::iterator MI,
35                                          MachineRegisterInfo &MRI,
36                                          MachineOperand &SuperReg,
37                                          const TargetRegisterClass *SuperRC,
38                                          unsigned SubIdx,
39                                          const TargetRegisterClass *SubRC) const;
40
41   unsigned split64BitImm(SmallVectorImpl<MachineInstr *> &Worklist,
42                          MachineBasicBlock::iterator MI,
43                          MachineRegisterInfo &MRI,
44                          const TargetRegisterClass *RC,
45                          const MachineOperand &Op) const;
46
47   void splitScalar64BitUnaryOp(SmallVectorImpl<MachineInstr *> &Worklist,
48                                MachineInstr *Inst, unsigned Opcode) const;
49
50   void splitScalar64BitBinaryOp(SmallVectorImpl<MachineInstr *> &Worklist,
51                                 MachineInstr *Inst, unsigned Opcode) const;
52
53   void splitScalar64BitBCNT(SmallVectorImpl<MachineInstr *> &Worklist,
54                             MachineInstr *Inst) const;
55
56   void addDescImplicitUseDef(const MCInstrDesc &Desc, MachineInstr *MI) const;
57
58 public:
59   explicit SIInstrInfo(const AMDGPUSubtarget &st);
60
61   const SIRegisterInfo &getRegisterInfo() const override {
62     return RI;
63   }
64
65   bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
66                                int64_t &Offset1,
67                                int64_t &Offset2) const override;
68
69   bool getLdStBaseRegImmOfs(MachineInstr *LdSt,
70                             unsigned &BaseReg, unsigned &Offset,
71                             const TargetRegisterInfo *TRI) const final;
72
73   bool shouldClusterLoads(MachineInstr *FirstLdSt,
74                           MachineInstr *SecondLdSt,
75                           unsigned NumLoads) const final;
76
77   void copyPhysReg(MachineBasicBlock &MBB,
78                    MachineBasicBlock::iterator MI, DebugLoc DL,
79                    unsigned DestReg, unsigned SrcReg,
80                    bool KillSrc) const override;
81
82   unsigned calculateLDSSpillAddress(MachineBasicBlock &MBB,
83                                     MachineBasicBlock::iterator MI,
84                                     RegScavenger *RS,
85                                     unsigned TmpReg,
86                                     unsigned Offset,
87                                     unsigned Size) const;
88
89   void storeRegToStackSlot(MachineBasicBlock &MBB,
90                            MachineBasicBlock::iterator MI,
91                            unsigned SrcReg, bool isKill, int FrameIndex,
92                            const TargetRegisterClass *RC,
93                            const TargetRegisterInfo *TRI) const override;
94
95   void loadRegFromStackSlot(MachineBasicBlock &MBB,
96                             MachineBasicBlock::iterator MI,
97                             unsigned DestReg, int FrameIndex,
98                             const TargetRegisterClass *RC,
99                             const TargetRegisterInfo *TRI) const override;
100
101   bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
102
103   unsigned commuteOpcode(unsigned Opcode) const;
104
105   MachineInstr *commuteInstruction(MachineInstr *MI,
106                                    bool NewMI = false) const override;
107   bool findCommutedOpIndices(MachineInstr *MI,
108                              unsigned &SrcOpIdx1,
109                              unsigned &SrcOpIdx2) const override;
110
111   bool isTriviallyReMaterializable(const MachineInstr *MI,
112                                    AliasAnalysis *AA = nullptr) const;
113
114   MachineInstr *buildMovInstr(MachineBasicBlock *MBB,
115                               MachineBasicBlock::iterator I,
116                               unsigned DstReg, unsigned SrcReg) const override;
117   bool isMov(unsigned Opcode) const override;
118
119   bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override;
120   bool isDS(uint16_t Opcode) const;
121   bool isMIMG(uint16_t Opcode) const;
122   bool isSMRD(uint16_t Opcode) const;
123   bool isMUBUF(uint16_t Opcode) const;
124   bool isMTBUF(uint16_t Opcode) const;
125   bool isFLAT(uint16_t Opcode) const;
126   bool isVOP1(uint16_t Opcode) const;
127   bool isVOP2(uint16_t Opcode) const;
128   bool isVOP3(uint16_t Opcode) const;
129   bool isVOPC(uint16_t Opcode) const;
130   bool isInlineConstant(const APInt &Imm) const;
131   bool isInlineConstant(const MachineOperand &MO) const;
132   bool isLiteralConstant(const MachineOperand &MO) const;
133
134   bool isImmOperandLegal(const MachineInstr *MI, unsigned OpNo,
135                          const MachineOperand &MO) const;
136
137   /// \brief Return true if the given offset Size in bytes can be folded into
138   /// the immediate offsets of a memory instruction for the given address space.
139   static bool canFoldOffset(unsigned OffsetSize, unsigned AS) LLVM_READNONE;
140
141   /// \brief Return true if this 64-bit VALU instruction has a 32-bit encoding.
142   /// This function will return false if you pass it a 32-bit instruction.
143   bool hasVALU32BitEncoding(unsigned Opcode) const;
144
145   /// \brief Returns true if this operand uses the constant bus.
146   bool usesConstantBus(const MachineRegisterInfo &MRI,
147                        const MachineOperand &MO) const;
148
149   /// \brief Return true if this instruction has any modifiers.
150   ///  e.g. src[012]_mod, omod, clamp.
151   bool hasModifiers(unsigned Opcode) const;
152   bool verifyInstruction(const MachineInstr *MI,
153                          StringRef &ErrInfo) const override;
154
155   bool isSALUInstr(const MachineInstr &MI) const;
156   static unsigned getVALUOp(const MachineInstr &MI);
157
158   bool isSALUOpSupportedOnVALU(const MachineInstr &MI) const;
159
160   /// \brief Return the correct register class for \p OpNo.  For target-specific
161   /// instructions, this will return the register class that has been defined
162   /// in tablegen.  For generic instructions, like REG_SEQUENCE it will return
163   /// the register class of its machine operand.
164   /// to infer the correct register class base on the other operands.
165   const TargetRegisterClass *getOpRegClass(const MachineInstr &MI,
166                                            unsigned OpNo) const;\
167
168   /// \returns true if it is legal for the operand at index \p OpNo
169   /// to read a VGPR.
170   bool canReadVGPR(const MachineInstr &MI, unsigned OpNo) const;
171
172   /// \brief Legalize the \p OpIndex operand of this instruction by inserting
173   /// a MOV.  For example:
174   /// ADD_I32_e32 VGPR0, 15
175   /// to
176   /// MOV VGPR1, 15
177   /// ADD_I32_e32 VGPR0, VGPR1
178   ///
179   /// If the operand being legalized is a register, then a COPY will be used
180   /// instead of MOV.
181   void legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const;
182
183   /// \brief Check if \p MO is a legal operand if it was the \p OpIdx Operand
184   /// for \p MI.
185   bool isOperandLegal(const MachineInstr *MI, unsigned OpIdx,
186                       const MachineOperand *MO = nullptr) const;
187
188   /// \brief Legalize all operands in this instruction.  This function may
189   /// create new instruction and insert them before \p MI.
190   void legalizeOperands(MachineInstr *MI) const;
191
192   /// \brief Split an SMRD instruction into two smaller loads of half the
193   //  size storing the results in \p Lo and \p Hi.
194   void splitSMRD(MachineInstr *MI, const TargetRegisterClass *HalfRC,
195                  unsigned HalfImmOp, unsigned HalfSGPROp,
196                  MachineInstr *&Lo, MachineInstr *&Hi) const;
197
198   void moveSMRDToVALU(MachineInstr *MI, MachineRegisterInfo &MRI) const;
199
200   /// \brief Replace this instruction's opcode with the equivalent VALU
201   /// opcode.  This function will also move the users of \p MI to the
202   /// VALU if necessary.
203   void moveToVALU(MachineInstr &MI) const;
204
205   unsigned calculateIndirectAddress(unsigned RegIndex,
206                                     unsigned Channel) const override;
207
208   const TargetRegisterClass *getIndirectAddrRegClass() const override;
209
210   MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
211                                          MachineBasicBlock::iterator I,
212                                          unsigned ValueReg,
213                                          unsigned Address,
214                                          unsigned OffsetReg) const override;
215
216   MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
217                                         MachineBasicBlock::iterator I,
218                                         unsigned ValueReg,
219                                         unsigned Address,
220                                         unsigned OffsetReg) const override;
221   void reserveIndirectRegisters(BitVector &Reserved,
222                                 const MachineFunction &MF) const;
223
224   void LoadM0(MachineInstr *MoveRel, MachineBasicBlock::iterator I,
225               unsigned SavReg, unsigned IndexReg) const;
226
227   void insertNOPs(MachineBasicBlock::iterator MI, int Count) const;
228
229   /// \brief Returns the operand named \p Op.  If \p MI does not have an
230   /// operand named \c Op, this function returns nullptr.
231   MachineOperand *getNamedOperand(MachineInstr &MI, unsigned OperandName) const;
232 };
233
234 namespace AMDGPU {
235
236   int getVOPe64(uint16_t Opcode);
237   int getVOPe32(uint16_t Opcode);
238   int getCommuteRev(uint16_t Opcode);
239   int getCommuteOrig(uint16_t Opcode);
240   int getMCOpcode(uint16_t Opcode, unsigned Gen);
241   int getAddr64Inst(uint16_t Opcode);
242   int getAtomicRetOp(uint16_t Opcode);
243   int getAtomicNoRetOp(uint16_t Opcode);
244
245   const uint64_t RSRC_DATA_FORMAT = 0xf00000000000LL;
246   const uint64_t RSRC_TID_ENABLE = 1LL << 55;
247
248 } // End namespace AMDGPU
249
250 namespace SI {
251 namespace KernelInputOffsets {
252
253 /// Offsets in bytes from the start of the input buffer
254 enum Offsets {
255   NGROUPS_X = 0,
256   NGROUPS_Y = 4,
257   NGROUPS_Z = 8,
258   GLOBAL_SIZE_X = 12,
259   GLOBAL_SIZE_Y = 16,
260   GLOBAL_SIZE_Z = 20,
261   LOCAL_SIZE_X = 24,
262   LOCAL_SIZE_Y = 28,
263   LOCAL_SIZE_Z = 32
264 };
265
266 } // End namespace KernelInputOffsets
267 } // End namespace SI
268
269 } // End namespace llvm
270
271 namespace SIInstrFlags {
272   enum Flags {
273     // First 4 bits are the instruction encoding
274     VM_CNT = 1 << 0,
275     EXP_CNT = 1 << 1,
276     LGKM_CNT = 1 << 2
277   };
278 }
279
280 namespace SISrcMods {
281   enum {
282    NEG = 1 << 0,
283    ABS = 1 << 1
284   };
285 }
286
287 #endif