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