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