1 //===-- SIInstrInfo.h - SI Instruction Info Interface -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Interface definition for SIInstrInfo.
13 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_LIB_TARGET_R600_SIINSTRINFO_H
17 #define LLVM_LIB_TARGET_R600_SIINSTRINFO_H
19 #include "AMDGPUInstrInfo.h"
20 #include "SIDefines.h"
21 #include "SIRegisterInfo.h"
25 class SIInstrInfo : public AMDGPUInstrInfo {
27 const SIRegisterInfo RI;
29 unsigned buildExtractSubReg(MachineBasicBlock::iterator MI,
30 MachineRegisterInfo &MRI,
31 MachineOperand &SuperReg,
32 const TargetRegisterClass *SuperRC,
34 const TargetRegisterClass *SubRC) const;
35 MachineOperand buildExtractSubRegOrImm(MachineBasicBlock::iterator MI,
36 MachineRegisterInfo &MRI,
37 MachineOperand &SuperReg,
38 const TargetRegisterClass *SuperRC,
40 const TargetRegisterClass *SubRC) const;
42 void swapOperands(MachineBasicBlock::iterator Inst) const;
44 void lowerScalarAbs(SmallVectorImpl<MachineInstr *> &Worklist,
45 MachineInstr *Inst) const;
47 void splitScalar64BitUnaryOp(SmallVectorImpl<MachineInstr *> &Worklist,
48 MachineInstr *Inst, unsigned Opcode) const;
50 void splitScalar64BitBinaryOp(SmallVectorImpl<MachineInstr *> &Worklist,
51 MachineInstr *Inst, unsigned Opcode) const;
53 void splitScalar64BitBCNT(SmallVectorImpl<MachineInstr *> &Worklist,
54 MachineInstr *Inst) const;
55 void splitScalar64BitBFE(SmallVectorImpl<MachineInstr *> &Worklist,
56 MachineInstr *Inst) const;
58 void addUsersToMoveToVALUWorklist(
59 unsigned Reg, MachineRegisterInfo &MRI,
60 SmallVectorImpl<MachineInstr *> &Worklist) const;
62 const TargetRegisterClass *
63 getDestEquivalentVGPRClass(const MachineInstr &Inst) const;
65 bool checkInstOffsetsDoNotOverlap(MachineInstr *MIa,
66 MachineInstr *MIb) const;
68 unsigned findUsedSGPR(const MachineInstr *MI, int OpIndices[3]) const;
71 MachineInstr *commuteInstructionImpl(MachineInstr *MI,
74 unsigned OpIdx1) const override;
77 explicit SIInstrInfo(const AMDGPUSubtarget &st);
79 const SIRegisterInfo &getRegisterInfo() const override {
83 bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
84 AliasAnalysis *AA) const override;
86 bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
88 int64_t &Offset2) const override;
90 bool getMemOpBaseRegImmOfs(MachineInstr *LdSt, unsigned &BaseReg,
92 const TargetRegisterInfo *TRI) const final;
94 bool shouldClusterLoads(MachineInstr *FirstLdSt,
95 MachineInstr *SecondLdSt,
96 unsigned NumLoads) const final;
98 void copyPhysReg(MachineBasicBlock &MBB,
99 MachineBasicBlock::iterator MI, DebugLoc DL,
100 unsigned DestReg, unsigned SrcReg,
101 bool KillSrc) const override;
103 unsigned calculateLDSSpillAddress(MachineBasicBlock &MBB,
104 MachineBasicBlock::iterator MI,
108 unsigned Size) const;
110 void storeRegToStackSlot(MachineBasicBlock &MBB,
111 MachineBasicBlock::iterator MI,
112 unsigned SrcReg, bool isKill, int FrameIndex,
113 const TargetRegisterClass *RC,
114 const TargetRegisterInfo *TRI) const override;
116 void loadRegFromStackSlot(MachineBasicBlock &MBB,
117 MachineBasicBlock::iterator MI,
118 unsigned DestReg, int FrameIndex,
119 const TargetRegisterClass *RC,
120 const TargetRegisterInfo *TRI) const override;
122 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
124 // \brief Returns an opcode that can be used to move a value to a \p DstRC
125 // register. If there is no hardware instruction that can store to \p
126 // DstRC, then AMDGPU::COPY is returned.
127 unsigned getMovOpcode(const TargetRegisterClass *DstRC) const;
130 int commuteOpcode(const MachineInstr &MI) const;
132 bool findCommutedOpIndices(MachineInstr *MI,
134 unsigned &SrcOpIdx2) const override;
136 bool areMemAccessesTriviallyDisjoint(
137 MachineInstr *MIa, MachineInstr *MIb,
138 AliasAnalysis *AA = nullptr) const override;
140 MachineInstr *buildMovInstr(MachineBasicBlock *MBB,
141 MachineBasicBlock::iterator I,
142 unsigned DstReg, unsigned SrcReg) const override;
143 bool isMov(unsigned Opcode) const override;
145 bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
146 unsigned Reg, MachineRegisterInfo *MRI) const final;
148 unsigned getMachineCSELookAheadLimit() const override { return 500; }
150 MachineInstr *convertToThreeAddress(MachineFunction::iterator &MBB,
151 MachineBasicBlock::iterator &MI,
152 LiveVariables *LV) const override;
154 static bool isSALU(const MachineInstr &MI) {
155 return MI.getDesc().TSFlags & SIInstrFlags::SALU;
158 bool isSALU(uint16_t Opcode) const {
159 return get(Opcode).TSFlags & SIInstrFlags::SALU;
162 static bool isVALU(const MachineInstr &MI) {
163 return MI.getDesc().TSFlags & SIInstrFlags::VALU;
166 bool isVALU(uint16_t Opcode) const {
167 return get(Opcode).TSFlags & SIInstrFlags::VALU;
170 static bool isSOP1(const MachineInstr &MI) {
171 return MI.getDesc().TSFlags & SIInstrFlags::SOP1;
174 bool isSOP1(uint16_t Opcode) const {
175 return get(Opcode).TSFlags & SIInstrFlags::SOP1;
178 static bool isSOP2(const MachineInstr &MI) {
179 return MI.getDesc().TSFlags & SIInstrFlags::SOP2;
182 bool isSOP2(uint16_t Opcode) const {
183 return get(Opcode).TSFlags & SIInstrFlags::SOP2;
186 static bool isSOPC(const MachineInstr &MI) {
187 return MI.getDesc().TSFlags & SIInstrFlags::SOPC;
190 bool isSOPC(uint16_t Opcode) const {
191 return get(Opcode).TSFlags & SIInstrFlags::SOPC;
194 static bool isSOPK(const MachineInstr &MI) {
195 return MI.getDesc().TSFlags & SIInstrFlags::SOPK;
198 bool isSOPK(uint16_t Opcode) const {
199 return get(Opcode).TSFlags & SIInstrFlags::SOPK;
202 static bool isSOPP(const MachineInstr &MI) {
203 return MI.getDesc().TSFlags & SIInstrFlags::SOPP;
206 bool isSOPP(uint16_t Opcode) const {
207 return get(Opcode).TSFlags & SIInstrFlags::SOPP;
210 static bool isVOP1(const MachineInstr &MI) {
211 return MI.getDesc().TSFlags & SIInstrFlags::VOP1;
214 bool isVOP1(uint16_t Opcode) const {
215 return get(Opcode).TSFlags & SIInstrFlags::VOP1;
218 static bool isVOP2(const MachineInstr &MI) {
219 return MI.getDesc().TSFlags & SIInstrFlags::VOP2;
222 bool isVOP2(uint16_t Opcode) const {
223 return get(Opcode).TSFlags & SIInstrFlags::VOP2;
226 static bool isVOP3(const MachineInstr &MI) {
227 return MI.getDesc().TSFlags & SIInstrFlags::VOP3;
230 bool isVOP3(uint16_t Opcode) const {
231 return get(Opcode).TSFlags & SIInstrFlags::VOP3;
234 static bool isVOPC(const MachineInstr &MI) {
235 return MI.getDesc().TSFlags & SIInstrFlags::VOPC;
238 bool isVOPC(uint16_t Opcode) const {
239 return get(Opcode).TSFlags & SIInstrFlags::VOPC;
242 static bool isMUBUF(const MachineInstr &MI) {
243 return MI.getDesc().TSFlags & SIInstrFlags::MUBUF;
246 bool isMUBUF(uint16_t Opcode) const {
247 return get(Opcode).TSFlags & SIInstrFlags::MUBUF;
250 static bool isMTBUF(const MachineInstr &MI) {
251 return MI.getDesc().TSFlags & SIInstrFlags::MTBUF;
254 bool isMTBUF(uint16_t Opcode) const {
255 return get(Opcode).TSFlags & SIInstrFlags::MTBUF;
258 static bool isSMRD(const MachineInstr &MI) {
259 return MI.getDesc().TSFlags & SIInstrFlags::SMRD;
262 bool isSMRD(uint16_t Opcode) const {
263 return get(Opcode).TSFlags & SIInstrFlags::SMRD;
266 static bool isDS(const MachineInstr &MI) {
267 return MI.getDesc().TSFlags & SIInstrFlags::DS;
270 bool isDS(uint16_t Opcode) const {
271 return get(Opcode).TSFlags & SIInstrFlags::DS;
274 static bool isMIMG(const MachineInstr &MI) {
275 return MI.getDesc().TSFlags & SIInstrFlags::MIMG;
278 bool isMIMG(uint16_t Opcode) const {
279 return get(Opcode).TSFlags & SIInstrFlags::MIMG;
282 static bool isFLAT(const MachineInstr &MI) {
283 return MI.getDesc().TSFlags & SIInstrFlags::FLAT;
286 bool isFLAT(uint16_t Opcode) const {
287 return get(Opcode).TSFlags & SIInstrFlags::FLAT;
290 static bool isWQM(const MachineInstr &MI) {
291 return MI.getDesc().TSFlags & SIInstrFlags::WQM;
294 bool isWQM(uint16_t Opcode) const {
295 return get(Opcode).TSFlags & SIInstrFlags::WQM;
298 static bool isVGPRSpill(const MachineInstr &MI) {
299 return MI.getDesc().TSFlags & SIInstrFlags::VGPRSpill;
302 bool isVGPRSpill(uint16_t Opcode) const {
303 return get(Opcode).TSFlags & SIInstrFlags::VGPRSpill;
306 bool isInlineConstant(const APInt &Imm) const;
307 bool isInlineConstant(const MachineOperand &MO, unsigned OpSize) const;
308 bool isLiteralConstant(const MachineOperand &MO, unsigned OpSize) const;
310 bool isImmOperandLegal(const MachineInstr *MI, unsigned OpNo,
311 const MachineOperand &MO) const;
313 /// \brief Return true if this 64-bit VALU instruction has a 32-bit encoding.
314 /// This function will return false if you pass it a 32-bit instruction.
315 bool hasVALU32BitEncoding(unsigned Opcode) const;
317 /// \brief Returns true if this operand uses the constant bus.
318 bool usesConstantBus(const MachineRegisterInfo &MRI,
319 const MachineOperand &MO,
320 unsigned OpSize) const;
322 /// \brief Return true if this instruction has any modifiers.
323 /// e.g. src[012]_mod, omod, clamp.
324 bool hasModifiers(unsigned Opcode) const;
326 bool hasModifiersSet(const MachineInstr &MI,
327 unsigned OpName) const;
329 bool verifyInstruction(const MachineInstr *MI,
330 StringRef &ErrInfo) const override;
332 static unsigned getVALUOp(const MachineInstr &MI);
334 bool isSALUOpSupportedOnVALU(const MachineInstr &MI) const;
336 /// \brief Return the correct register class for \p OpNo. For target-specific
337 /// instructions, this will return the register class that has been defined
338 /// in tablegen. For generic instructions, like REG_SEQUENCE it will return
339 /// the register class of its machine operand.
340 /// to infer the correct register class base on the other operands.
341 const TargetRegisterClass *getOpRegClass(const MachineInstr &MI,
342 unsigned OpNo) const;
344 /// \brief Return the size in bytes of the operand OpNo on the given
345 // instruction opcode.
346 unsigned getOpSize(uint16_t Opcode, unsigned OpNo) const {
347 const MCOperandInfo &OpInfo = get(Opcode).OpInfo[OpNo];
349 if (OpInfo.RegClass == -1) {
350 // If this is an immediate operand, this must be a 32-bit literal.
351 assert(OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE);
355 return RI.getRegClass(OpInfo.RegClass)->getSize();
358 /// \brief This form should usually be preferred since it handles operands
359 /// with unknown register classes.
360 unsigned getOpSize(const MachineInstr &MI, unsigned OpNo) const {
361 return getOpRegClass(MI, OpNo)->getSize();
364 /// \returns true if it is legal for the operand at index \p OpNo
366 bool canReadVGPR(const MachineInstr &MI, unsigned OpNo) const;
368 /// \brief Legalize the \p OpIndex operand of this instruction by inserting
369 /// a MOV. For example:
370 /// ADD_I32_e32 VGPR0, 15
373 /// ADD_I32_e32 VGPR0, VGPR1
375 /// If the operand being legalized is a register, then a COPY will be used
377 void legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const;
379 /// \brief Check if \p MO is a legal operand if it was the \p OpIdx Operand
381 bool isOperandLegal(const MachineInstr *MI, unsigned OpIdx,
382 const MachineOperand *MO = nullptr) const;
384 /// \brief Check if \p MO would be a valid operand for the given operand
385 /// definition \p OpInfo. Note this does not attempt to validate constant bus
386 /// restrictions (e.g. literal constant usage).
387 bool isLegalVSrcOperand(const MachineRegisterInfo &MRI,
388 const MCOperandInfo &OpInfo,
389 const MachineOperand &MO) const;
391 /// \brief Check if \p MO (a register operand) is a legal register for the
392 /// given operand description.
393 bool isLegalRegOperand(const MachineRegisterInfo &MRI,
394 const MCOperandInfo &OpInfo,
395 const MachineOperand &MO) const;
397 /// \brief Legalize operands in \p MI by either commuting it or inserting a
399 void legalizeOperandsVOP2(MachineRegisterInfo &MRI, MachineInstr *MI) const;
401 /// \brief Fix operands in \p MI to satisfy constant bus requirements.
402 void legalizeOperandsVOP3(MachineRegisterInfo &MRI, MachineInstr *MI) const;
404 /// \brief Legalize all operands in this instruction. This function may
405 /// create new instruction and insert them before \p MI.
406 void legalizeOperands(MachineInstr *MI) const;
408 /// \brief Split an SMRD instruction into two smaller loads of half the
409 // size storing the results in \p Lo and \p Hi.
410 void splitSMRD(MachineInstr *MI, const TargetRegisterClass *HalfRC,
411 unsigned HalfImmOp, unsigned HalfSGPROp,
412 MachineInstr *&Lo, MachineInstr *&Hi) const;
414 void moveSMRDToVALU(MachineInstr *MI, MachineRegisterInfo &MRI,
415 SmallVectorImpl<MachineInstr *> &Worklist) const;
417 /// \brief Replace this instruction's opcode with the equivalent VALU
418 /// opcode. This function will also move the users of \p MI to the
419 /// VALU if necessary.
420 void moveToVALU(MachineInstr &MI) const;
422 unsigned calculateIndirectAddress(unsigned RegIndex,
423 unsigned Channel) const override;
425 const TargetRegisterClass *getIndirectAddrRegClass() const override;
427 MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
428 MachineBasicBlock::iterator I,
431 unsigned OffsetReg) const override;
433 MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
434 MachineBasicBlock::iterator I,
437 unsigned OffsetReg) const override;
438 void reserveIndirectRegisters(BitVector &Reserved,
439 const MachineFunction &MF) const;
441 void LoadM0(MachineInstr *MoveRel, MachineBasicBlock::iterator I,
442 unsigned SavReg, unsigned IndexReg) const;
444 void insertWaitStates(MachineBasicBlock::iterator MI, int Count) const;
446 /// \brief Returns the operand named \p Op. If \p MI does not have an
447 /// operand named \c Op, this function returns nullptr.
449 MachineOperand *getNamedOperand(MachineInstr &MI, unsigned OperandName) const;
452 const MachineOperand *getNamedOperand(const MachineInstr &MI,
453 unsigned OpName) const {
454 return getNamedOperand(const_cast<MachineInstr &>(MI), OpName);
457 /// Get required immediate operand
458 int64_t getNamedImmOperand(const MachineInstr &MI, unsigned OpName) const {
459 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OpName);
460 return MI.getOperand(Idx).getImm();
463 uint64_t getDefaultRsrcDataFormat() const;
464 uint64_t getScratchRsrcWords23() const;
469 int getVOPe64(uint16_t Opcode);
472 int getVOPe32(uint16_t Opcode);
475 int getCommuteRev(uint16_t Opcode);
478 int getCommuteOrig(uint16_t Opcode);
481 int getAddr64Inst(uint16_t Opcode);
484 int getAtomicRetOp(uint16_t Opcode);
487 int getAtomicNoRetOp(uint16_t Opcode);
489 const uint64_t RSRC_DATA_FORMAT = 0xf00000000000LL;
490 const uint64_t RSRC_TID_ENABLE = 1LL << 55;
492 } // End namespace AMDGPU
495 namespace KernelInputOffsets {
497 /// Offsets in bytes from the start of the input buffer
510 } // End namespace KernelInputOffsets
511 } // End namespace SI
513 } // End namespace llvm