7e049dca7b142732f9a7827b5c008cb28d943f73
[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 "SIDefines.h"
21 #include "SIRegisterInfo.h"
22
23 namespace llvm {
24
25 class SIInstrInfo : public AMDGPUInstrInfo {
26 private:
27   const SIRegisterInfo RI;
28
29   unsigned buildExtractSubReg(MachineBasicBlock::iterator MI,
30                               MachineRegisterInfo &MRI,
31                               MachineOperand &SuperReg,
32                               const TargetRegisterClass *SuperRC,
33                               unsigned SubIdx,
34                               const TargetRegisterClass *SubRC) const;
35   MachineOperand buildExtractSubRegOrImm(MachineBasicBlock::iterator MI,
36                                          MachineRegisterInfo &MRI,
37                                          MachineOperand &SuperReg,
38                                          const TargetRegisterClass *SuperRC,
39                                          unsigned SubIdx,
40                                          const TargetRegisterClass *SubRC) const;
41
42   unsigned split64BitImm(SmallVectorImpl<MachineInstr *> &Worklist,
43                          MachineBasicBlock::iterator MI,
44                          MachineRegisterInfo &MRI,
45                          const TargetRegisterClass *RC,
46                          const MachineOperand &Op) const;
47
48   void swapOperands(MachineBasicBlock::iterator Inst) const;
49
50   void splitScalar64BitUnaryOp(SmallVectorImpl<MachineInstr *> &Worklist,
51                                MachineInstr *Inst, unsigned Opcode) const;
52
53   void splitScalar64BitBinaryOp(SmallVectorImpl<MachineInstr *> &Worklist,
54                                 MachineInstr *Inst, unsigned Opcode) const;
55
56   void splitScalar64BitBCNT(SmallVectorImpl<MachineInstr *> &Worklist,
57                             MachineInstr *Inst) const;
58   void splitScalar64BitBFE(SmallVectorImpl<MachineInstr *> &Worklist,
59                            MachineInstr *Inst) const;
60
61   void addDescImplicitUseDef(const MCInstrDesc &Desc, MachineInstr *MI) const;
62
63   bool checkInstOffsetsDoNotOverlap(MachineInstr *MIa,
64                                     MachineInstr *MIb) const;
65
66   unsigned findUsedSGPR(const MachineInstr *MI, int OpIndices[3]) const;
67
68 public:
69   explicit SIInstrInfo(const AMDGPUSubtarget &st);
70
71   const SIRegisterInfo &getRegisterInfo() const override {
72     return RI;
73   }
74
75   bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
76                                          AliasAnalysis *AA) const override;
77
78   bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
79                                int64_t &Offset1,
80                                int64_t &Offset2) const override;
81
82   bool getLdStBaseRegImmOfs(MachineInstr *LdSt,
83                             unsigned &BaseReg, unsigned &Offset,
84                             const TargetRegisterInfo *TRI) const final;
85
86   bool shouldClusterLoads(MachineInstr *FirstLdSt,
87                           MachineInstr *SecondLdSt,
88                           unsigned NumLoads) const final;
89
90   void copyPhysReg(MachineBasicBlock &MBB,
91                    MachineBasicBlock::iterator MI, DebugLoc DL,
92                    unsigned DestReg, unsigned SrcReg,
93                    bool KillSrc) const override;
94
95   unsigned calculateLDSSpillAddress(MachineBasicBlock &MBB,
96                                     MachineBasicBlock::iterator MI,
97                                     RegScavenger *RS,
98                                     unsigned TmpReg,
99                                     unsigned Offset,
100                                     unsigned Size) const;
101
102   void storeRegToStackSlot(MachineBasicBlock &MBB,
103                            MachineBasicBlock::iterator MI,
104                            unsigned SrcReg, bool isKill, int FrameIndex,
105                            const TargetRegisterClass *RC,
106                            const TargetRegisterInfo *TRI) const override;
107
108   void loadRegFromStackSlot(MachineBasicBlock &MBB,
109                             MachineBasicBlock::iterator MI,
110                             unsigned DestReg, int FrameIndex,
111                             const TargetRegisterClass *RC,
112                             const TargetRegisterInfo *TRI) const override;
113
114   bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
115
116   // \brief Returns an opcode that can be used to move a value to a \p DstRC
117   // register.  If there is no hardware instruction that can store to \p
118   // DstRC, then AMDGPU::COPY is returned.
119   unsigned getMovOpcode(const TargetRegisterClass *DstRC) const;
120   unsigned commuteOpcode(const MachineInstr &MI) const;
121
122   MachineInstr *commuteInstruction(MachineInstr *MI,
123                                    bool NewMI = false) const override;
124   bool findCommutedOpIndices(MachineInstr *MI,
125                              unsigned &SrcOpIdx1,
126                              unsigned &SrcOpIdx2) const override;
127
128   bool isTriviallyReMaterializable(const MachineInstr *MI,
129                                    AliasAnalysis *AA = nullptr) const;
130
131   bool areMemAccessesTriviallyDisjoint(
132     MachineInstr *MIa, MachineInstr *MIb,
133     AliasAnalysis *AA = nullptr) const override;
134
135   MachineInstr *buildMovInstr(MachineBasicBlock *MBB,
136                               MachineBasicBlock::iterator I,
137                               unsigned DstReg, unsigned SrcReg) const override;
138   bool isMov(unsigned Opcode) const override;
139
140   bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override;
141
142   bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
143                      unsigned Reg, MachineRegisterInfo *MRI) const final;
144
145   bool isSALU(uint16_t Opcode) const {
146     return get(Opcode).TSFlags & SIInstrFlags::SALU;
147   }
148
149   bool isVALU(uint16_t Opcode) const {
150     return get(Opcode).TSFlags & SIInstrFlags::VALU;
151   }
152
153   bool isSOP1(uint16_t Opcode) const {
154     return get(Opcode).TSFlags & SIInstrFlags::SOP1;
155   }
156
157   bool isSOP2(uint16_t Opcode) const {
158     return get(Opcode).TSFlags & SIInstrFlags::SOP2;
159   }
160
161   bool isSOPC(uint16_t Opcode) const {
162     return get(Opcode).TSFlags & SIInstrFlags::SOPC;
163   }
164
165   bool isSOPK(uint16_t Opcode) const {
166     return get(Opcode).TSFlags & SIInstrFlags::SOPK;
167   }
168
169   bool isSOPP(uint16_t Opcode) const {
170     return get(Opcode).TSFlags & SIInstrFlags::SOPP;
171   }
172
173   bool isVOP1(uint16_t Opcode) const {
174     return get(Opcode).TSFlags & SIInstrFlags::VOP1;
175   }
176
177   bool isVOP2(uint16_t Opcode) const {
178     return get(Opcode).TSFlags & SIInstrFlags::VOP2;
179   }
180
181   bool isVOP3(uint16_t Opcode) const {
182     return get(Opcode).TSFlags & SIInstrFlags::VOP3;
183   }
184
185   bool isVOPC(uint16_t Opcode) const {
186     return get(Opcode).TSFlags & SIInstrFlags::VOPC;
187   }
188
189   bool isMUBUF(uint16_t Opcode) const {
190     return get(Opcode).TSFlags & SIInstrFlags::MUBUF;
191   }
192
193   bool isMTBUF(uint16_t Opcode) const {
194     return get(Opcode).TSFlags & SIInstrFlags::MTBUF;
195   }
196
197   bool isSMRD(uint16_t Opcode) const {
198     return get(Opcode).TSFlags & SIInstrFlags::SMRD;
199   }
200
201   bool isDS(uint16_t Opcode) const {
202     return get(Opcode).TSFlags & SIInstrFlags::DS;
203   }
204
205   bool isMIMG(uint16_t Opcode) const {
206     return get(Opcode).TSFlags & SIInstrFlags::MIMG;
207   }
208
209   bool isFLAT(uint16_t Opcode) const {
210     return get(Opcode).TSFlags & SIInstrFlags::FLAT;
211   }
212
213   bool isWQM(uint16_t Opcode) const {
214     return get(Opcode).TSFlags & SIInstrFlags::WQM;
215   }
216
217   bool isInlineConstant(const APInt &Imm) const;
218   bool isInlineConstant(const MachineOperand &MO, unsigned OpSize) const;
219   bool isLiteralConstant(const MachineOperand &MO, unsigned OpSize) const;
220
221   bool isImmOperandLegal(const MachineInstr *MI, unsigned OpNo,
222                          const MachineOperand &MO) const;
223
224   /// \brief Return true if this 64-bit VALU instruction has a 32-bit encoding.
225   /// This function will return false if you pass it a 32-bit instruction.
226   bool hasVALU32BitEncoding(unsigned Opcode) const;
227
228   /// \brief Returns true if this operand uses the constant bus.
229   bool usesConstantBus(const MachineRegisterInfo &MRI,
230                        const MachineOperand &MO,
231                        unsigned OpSize) const;
232
233   /// \brief Return true if this instruction has any modifiers.
234   ///  e.g. src[012]_mod, omod, clamp.
235   bool hasModifiers(unsigned Opcode) const;
236
237   bool hasModifiersSet(const MachineInstr &MI,
238                        unsigned OpName) const;
239
240   bool verifyInstruction(const MachineInstr *MI,
241                          StringRef &ErrInfo) const override;
242
243   static unsigned getVALUOp(const MachineInstr &MI);
244
245   bool isSALUOpSupportedOnVALU(const MachineInstr &MI) const;
246
247   /// \brief Return the correct register class for \p OpNo.  For target-specific
248   /// instructions, this will return the register class that has been defined
249   /// in tablegen.  For generic instructions, like REG_SEQUENCE it will return
250   /// the register class of its machine operand.
251   /// to infer the correct register class base on the other operands.
252   const TargetRegisterClass *getOpRegClass(const MachineInstr &MI,
253                                            unsigned OpNo) const;
254
255   /// \brief Return the size in bytes of the operand OpNo on the given
256   // instruction opcode.
257   unsigned getOpSize(uint16_t Opcode, unsigned OpNo) const {
258     const MCOperandInfo &OpInfo = get(Opcode).OpInfo[OpNo];
259
260     if (OpInfo.RegClass == -1) {
261       // If this is an immediate operand, this must be a 32-bit literal.
262       assert(OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE);
263       return 4;
264     }
265
266     return RI.getRegClass(OpInfo.RegClass)->getSize();
267   }
268
269   /// \brief This form should usually be preferred since it handles operands
270   /// with unknown register classes.
271   unsigned getOpSize(const MachineInstr &MI, unsigned OpNo) const {
272     return getOpRegClass(MI, OpNo)->getSize();
273   }
274
275   /// \returns true if it is legal for the operand at index \p OpNo
276   /// to read a VGPR.
277   bool canReadVGPR(const MachineInstr &MI, unsigned OpNo) const;
278
279   /// \brief Legalize the \p OpIndex operand of this instruction by inserting
280   /// a MOV.  For example:
281   /// ADD_I32_e32 VGPR0, 15
282   /// to
283   /// MOV VGPR1, 15
284   /// ADD_I32_e32 VGPR0, VGPR1
285   ///
286   /// If the operand being legalized is a register, then a COPY will be used
287   /// instead of MOV.
288   void legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const;
289
290   /// \brief Check if \p MO is a legal operand if it was the \p OpIdx Operand
291   /// for \p MI.
292   bool isOperandLegal(const MachineInstr *MI, unsigned OpIdx,
293                       const MachineOperand *MO = nullptr) const;
294
295   /// \brief Legalize all operands in this instruction.  This function may
296   /// create new instruction and insert them before \p MI.
297   void legalizeOperands(MachineInstr *MI) const;
298
299   /// \brief Split an SMRD instruction into two smaller loads of half the
300   //  size storing the results in \p Lo and \p Hi.
301   void splitSMRD(MachineInstr *MI, const TargetRegisterClass *HalfRC,
302                  unsigned HalfImmOp, unsigned HalfSGPROp,
303                  MachineInstr *&Lo, MachineInstr *&Hi) const;
304
305   void moveSMRDToVALU(MachineInstr *MI, MachineRegisterInfo &MRI) const;
306
307   /// \brief Replace this instruction's opcode with the equivalent VALU
308   /// opcode.  This function will also move the users of \p MI to the
309   /// VALU if necessary.
310   void moveToVALU(MachineInstr &MI) const;
311
312   unsigned calculateIndirectAddress(unsigned RegIndex,
313                                     unsigned Channel) const override;
314
315   const TargetRegisterClass *getIndirectAddrRegClass() const override;
316
317   MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
318                                          MachineBasicBlock::iterator I,
319                                          unsigned ValueReg,
320                                          unsigned Address,
321                                          unsigned OffsetReg) const override;
322
323   MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
324                                         MachineBasicBlock::iterator I,
325                                         unsigned ValueReg,
326                                         unsigned Address,
327                                         unsigned OffsetReg) const override;
328   void reserveIndirectRegisters(BitVector &Reserved,
329                                 const MachineFunction &MF) const;
330
331   void LoadM0(MachineInstr *MoveRel, MachineBasicBlock::iterator I,
332               unsigned SavReg, unsigned IndexReg) const;
333
334   void insertNOPs(MachineBasicBlock::iterator MI, int Count) const;
335
336   /// \brief Returns the operand named \p Op.  If \p MI does not have an
337   /// operand named \c Op, this function returns nullptr.
338   MachineOperand *getNamedOperand(MachineInstr &MI, unsigned OperandName) const;
339
340   const MachineOperand *getNamedOperand(const MachineInstr &MI,
341                                         unsigned OpName) const {
342     return getNamedOperand(const_cast<MachineInstr &>(MI), OpName);
343   }
344
345   uint64_t getDefaultRsrcDataFormat() const;
346
347 };
348
349 namespace AMDGPU {
350
351   int getVOPe64(uint16_t Opcode);
352   int getVOPe32(uint16_t Opcode);
353   int getCommuteRev(uint16_t Opcode);
354   int getCommuteOrig(uint16_t Opcode);
355   int getAddr64Inst(uint16_t Opcode);
356   int getAtomicRetOp(uint16_t Opcode);
357   int getAtomicNoRetOp(uint16_t Opcode);
358
359   const uint64_t RSRC_DATA_FORMAT = 0xf00000000000LL;
360   const uint64_t RSRC_TID_ENABLE = 1LL << 55;
361
362 } // End namespace AMDGPU
363
364 namespace SI {
365 namespace KernelInputOffsets {
366
367 /// Offsets in bytes from the start of the input buffer
368 enum Offsets {
369   NGROUPS_X = 0,
370   NGROUPS_Y = 4,
371   NGROUPS_Z = 8,
372   GLOBAL_SIZE_X = 12,
373   GLOBAL_SIZE_Y = 16,
374   GLOBAL_SIZE_Z = 20,
375   LOCAL_SIZE_X = 24,
376   LOCAL_SIZE_Y = 28,
377   LOCAL_SIZE_Z = 32
378 };
379
380 } // End namespace KernelInputOffsets
381 } // End namespace SI
382
383 } // End namespace llvm
384
385 #endif