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