blob: 420bec89f2668f868909628f1e72641373cc7e77 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- R600InstrInfo.h - R600 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 R600InstrInfo
12//
13//===----------------------------------------------------------------------===//
14
Matt Arsenault6b6a2c32016-03-11 08:00:27 +000015#ifndef LLVM_LIB_TARGET_AMDGPU_R600INSTRINFO_H
16#define LLVM_LIB_TARGET_AMDGPU_R600INSTRINFO_H
Tom Stellard75aadc22012-12-11 21:25:42 +000017
Tom Stellard75aadc22012-12-11 21:25:42 +000018#include "AMDGPUInstrInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "R600RegisterInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000020
21namespace llvm {
Matt Arsenault43e92fe2016-06-24 06:30:11 +000022class AMDGPUTargetMachine;
23class DFAPacketizer;
24class MachineFunction;
25class MachineInstr;
26class MachineInstrBuilder;
27class R600Subtarget;
Tom Stellard75aadc22012-12-11 21:25:42 +000028
Matt Arsenault43e92fe2016-06-24 06:30:11 +000029class R600InstrInfo final : public AMDGPUInstrInfo {
30private:
Tom Stellard75aadc22012-12-11 21:25:42 +000031 const R600RegisterInfo RI;
Matt Arsenault43e92fe2016-06-24 06:30:11 +000032 const R600Subtarget &ST;
Tom Stellard75aadc22012-12-11 21:25:42 +000033
Matt Arsenault43e92fe2016-06-24 06:30:11 +000034 std::vector<std::pair<int, unsigned>>
35 ExtractSrcs(MachineInstr *MI,
36 const DenseMap<unsigned, unsigned> &PV,
37 unsigned &ConstCount) const;
Tom Stellard880a80a2014-06-17 16:53:14 +000038
39 MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
40 MachineBasicBlock::iterator I,
41 unsigned ValueReg, unsigned Address,
42 unsigned OffsetReg,
43 unsigned AddrChan) const;
44
45 MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
Matt Arsenault43e92fe2016-06-24 06:30:11 +000046 MachineBasicBlock::iterator I,
47 unsigned ValueReg, unsigned Address,
48 unsigned OffsetReg,
49 unsigned AddrChan) const;
50public:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +000051 enum BankSwizzle {
Vincent Lejeunebb8a87212013-06-29 19:32:29 +000052 ALU_VEC_012_SCL_210 = 0,
53 ALU_VEC_021_SCL_122,
54 ALU_VEC_120_SCL_212,
55 ALU_VEC_102_SCL_221,
Vincent Lejeune0fca91d2013-05-17 16:50:02 +000056 ALU_VEC_201,
57 ALU_VEC_210
58 };
59
Matt Arsenault43e92fe2016-06-24 06:30:11 +000060 explicit R600InstrInfo(const R600Subtarget &);
Tom Stellard75aadc22012-12-11 21:25:42 +000061
Matt Arsenault43e92fe2016-06-24 06:30:11 +000062 const R600RegisterInfo &getRegisterInfo() const {
63 return RI;
64 }
65
Benjamin Kramerbdc49562016-06-12 15:39:02 +000066 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
67 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
Craig Topper5656db42014-04-29 07:57:24 +000068 bool KillSrc) const override;
Tom Stellardcd6b0a62013-11-22 00:41:08 +000069 bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
Craig Topper5656db42014-04-29 07:57:24 +000070 MachineBasicBlock::iterator MBBI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000071
72 bool isTrig(const MachineInstr &MI) const;
73 bool isPlaceHolderOpcode(unsigned opcode) const;
74 bool isReductionOp(unsigned opcode) const;
75 bool isCubeOp(unsigned opcode) const;
76
77 /// \returns true if this \p Opcode represents an ALU instruction.
78 bool isALUInstr(unsigned Opcode) const;
Tom Stellardc026e8b2013-06-28 15:47:08 +000079 bool hasInstrModifiers(unsigned Opcode) const;
80 bool isLDSInstr(unsigned Opcode) const;
Tom Stellard8f9fc202013-11-15 00:12:45 +000081 bool isLDSNoRetInstr(unsigned Opcode) const;
82 bool isLDSRetInstr(unsigned Opcode) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000083
Vincent Lejeunea4da6fb2013-10-01 19:32:58 +000084 /// \returns true if this \p Opcode represents an ALU instruction or an
85 /// instruction that will be lowered in ExpandSpecialInstrs Pass.
86 bool canBeConsideredALU(const MachineInstr *MI) const;
87
Vincent Lejeune076c0b22013-04-30 00:14:17 +000088 bool isTransOnly(unsigned Opcode) const;
89 bool isTransOnly(const MachineInstr *MI) const;
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +000090 bool isVectorOnly(unsigned Opcode) const;
91 bool isVectorOnly(const MachineInstr *MI) const;
Tom Stellard676c16d2013-08-16 01:11:51 +000092 bool isExport(unsigned Opcode) const;
Vincent Lejeune076c0b22013-04-30 00:14:17 +000093
Vincent Lejeunec2991642013-04-30 00:13:39 +000094 bool usesVertexCache(unsigned Opcode) const;
95 bool usesVertexCache(const MachineInstr *MI) const;
96 bool usesTextureCache(unsigned Opcode) const;
97 bool usesTextureCache(const MachineInstr *MI) const;
98
Tom Stellardce540332013-06-28 15:46:59 +000099 bool mustBeLastInClause(unsigned Opcode) const;
Tom Stellard26a3b672013-10-22 18:19:10 +0000100 bool usesAddressRegister(MachineInstr *MI) const;
101 bool definesAddressRegister(MachineInstr *MI) const;
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000102 bool readsLDSSrcReg(const MachineInstr *MI) const;
Tom Stellardce540332013-06-28 15:46:59 +0000103
Tom Stellard84021442013-07-23 01:48:24 +0000104 /// \returns The operand index for the given source number. Legal values
105 /// for SrcNum are 0, 1, and 2.
106 int getSrcIdx(unsigned Opcode, unsigned SrcNum) const;
107 /// \returns The operand Index for the Sel operand given an index to one
108 /// of the instruction's src operands.
109 int getSelIdx(unsigned Opcode, unsigned SrcIdx) const;
110
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000111 /// \returns a pair for each src of an ALU instructions.
112 /// The first member of a pair is the register id.
113 /// If register is ALU_CONST, second member is SEL.
114 /// If register is ALU_LITERAL, second member is IMM.
115 /// Otherwise, second member value is undefined.
116 SmallVector<std::pair<MachineOperand *, int64_t>, 3>
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000117 getSrcs(MachineInstr *MI) const;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000118
Vincent Lejeune77a83522013-06-29 19:32:43 +0000119 unsigned isLegalUpTo(
120 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
121 const std::vector<R600InstrInfo::BankSwizzle> &Swz,
122 const std::vector<std::pair<int, unsigned> > &TransSrcs,
123 R600InstrInfo::BankSwizzle TransSwz) const;
124
125 bool FindSwizzleForVectorSlot(
126 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
127 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
128 const std::vector<std::pair<int, unsigned> > &TransSrcs,
129 R600InstrInfo::BankSwizzle TransSwz) const;
Tom Stellardc026e8b2013-06-28 15:47:08 +0000130
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000131 /// Given the order VEC_012 < VEC_021 < VEC_120 < VEC_102 < VEC_201 < VEC_210
132 /// returns true and the first (in lexical order) BankSwizzle affectation
133 /// starting from the one already provided in the Instruction Group MIs that
134 /// fits Read Port limitations in BS if available. Otherwise returns false
135 /// and undefined content in BS.
Vincent Lejeune77a83522013-06-29 19:32:43 +0000136 /// isLastAluTrans should be set if the last Alu of MIs will be executed on
137 /// Trans ALU. In this case, ValidTSwizzle returns the BankSwizzle value to
138 /// apply to the last instruction.
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000139 /// PV holds GPR to PV registers in the Instruction Group MIs.
140 bool fitsReadPortLimitations(const std::vector<MachineInstr *> &MIs,
141 const DenseMap<unsigned, unsigned> &PV,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000142 std::vector<BankSwizzle> &BS,
143 bool isLastAluTrans) const;
144
145 /// An instruction group can only access 2 channel pair (either [XY] or [ZW])
146 /// from KCache bank on R700+. This function check if MI set in input meet
147 /// this limitations
148 bool fitsConstReadLimitations(const std::vector<MachineInstr *> &) const;
149 /// Same but using const index set instead of MI set.
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000150 bool fitsConstReadLimitations(const std::vector<unsigned>&) const;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000151
Alp Tokercb402912014-01-24 17:20:08 +0000152 /// \brief Vector instructions are instructions that must fill all
Tom Stellard75aadc22012-12-11 21:25:42 +0000153 /// instruction slots within an instruction group.
154 bool isVector(const MachineInstr &MI) const;
155
Tom Stellard2ff72622016-01-28 16:04:37 +0000156 bool isMov(unsigned Opcode) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000157
Eric Christopher143f02c2014-10-09 01:59:35 +0000158 DFAPacketizer *
159 CreateTargetScheduleState(const TargetSubtargetInfo &) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000160
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000161 bool ReverseBranchCondition(
162 SmallVectorImpl<MachineOperand> &Cond) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000163
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000164 bool AnalyzeBranch(MachineBasicBlock &MBB,
165 MachineBasicBlock *&TBB,
166 MachineBasicBlock *&FBB,
167 SmallVectorImpl<MachineOperand> &Cond,
168 bool AllowModify) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000169
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000170 unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
171 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000172 const DebugLoc &DL) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000173
Craig Topper5656db42014-04-29 07:57:24 +0000174 unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000175
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000176 bool isPredicated(const MachineInstr &MI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000177
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000178 bool isPredicable(MachineInstr &MI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000179
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000180 bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCyles,
181 BranchProbability Probability) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000182
183 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCyles,
184 unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000185 BranchProbability Probability) const override ;
Tom Stellard75aadc22012-12-11 21:25:42 +0000186
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000187 bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
188 unsigned NumTCycles, unsigned ExtraTCycles,
189 MachineBasicBlock &FMBB,
190 unsigned NumFCycles, unsigned ExtraFCycles,
191 BranchProbability Probability) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000192
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000193 bool DefinesPredicate(MachineInstr &MI,
194 std::vector<MachineOperand> &Pred) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000195
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000196 bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
197 ArrayRef<MachineOperand> Pred2) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000198
199 bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000200 MachineBasicBlock &FMBB) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000201
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000202 bool PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000203 ArrayRef<MachineOperand> Pred) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000204
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000205 unsigned int getPredicationCost(const MachineInstr &) const override;
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +0000206
Tom Stellard75aadc22012-12-11 21:25:42 +0000207 unsigned int getInstrLatency(const InstrItineraryData *ItinData,
208 const MachineInstr *MI,
Craig Topper5656db42014-04-29 07:57:24 +0000209 unsigned *PredCost = nullptr) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000210
Craig Topper5656db42014-04-29 07:57:24 +0000211 int getInstrLatency(const InstrItineraryData *ItinData,
212 SDNode *Node) const override { return 1;}
Tom Stellard75aadc22012-12-11 21:25:42 +0000213
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000214 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
Tom Stellard880a80a2014-06-17 16:53:14 +0000215
Tom Stellard81d871d2013-11-13 23:36:50 +0000216 /// \brief Reserve the registers that may be accesed using indirect addressing.
217 void reserveIndirectRegisters(BitVector &Reserved,
218 const MachineFunction &MF) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000219
Tom Stellard1242ce92016-02-05 18:44:57 +0000220 /// Calculate the "Indirect Address" for the given \p RegIndex and
221 /// \p Channel
222 ///
223 /// We model indirect addressing using a virtual address space that can be
224 /// accesed with loads and stores. The "Indirect Address" is the memory
225 /// address in this virtual address space that maps to the given \p RegIndex
226 /// and \p Channel.
227 unsigned calculateIndirectAddress(unsigned RegIndex, unsigned Channel) const;
228
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000229
Craig Topper5656db42014-04-29 07:57:24 +0000230 const TargetRegisterClass *getIndirectAddrRegClass() const override;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000231
Tom Stellard1242ce92016-02-05 18:44:57 +0000232 /// \brief Build instruction(s) for an indirect register write.
233 ///
234 /// \returns The instruction that performs the indirect register write
Craig Topper5656db42014-04-29 07:57:24 +0000235 MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
Tom Stellard1242ce92016-02-05 18:44:57 +0000236 MachineBasicBlock::iterator I,
237 unsigned ValueReg, unsigned Address,
238 unsigned OffsetReg) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000239
Tom Stellard1242ce92016-02-05 18:44:57 +0000240 /// \brief Build instruction(s) for an indirect register read.
241 ///
242 /// \returns The instruction that performs the indirect register read
Craig Topper5656db42014-04-29 07:57:24 +0000243 MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
244 MachineBasicBlock::iterator I,
245 unsigned ValueReg, unsigned Address,
Tom Stellard1242ce92016-02-05 18:44:57 +0000246 unsigned OffsetReg) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000247
Vincent Lejeune80031d9f2013-04-03 16:49:34 +0000248 unsigned getMaxAlusPerClause() const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000249
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000250 /// buildDefaultInstruction - This function returns a MachineInstr with all
251 /// the instruction modifiers initialized to their default values. You can
252 /// use this function to avoid manually specifying each instruction modifier
253 /// operand when building a new instruction.
Tom Stellard75aadc22012-12-11 21:25:42 +0000254 ///
255 /// \returns a MachineInstr with all the instruction modifiers initialized
256 /// to their default values.
257 MachineInstrBuilder buildDefaultInstruction(MachineBasicBlock &MBB,
258 MachineBasicBlock::iterator I,
259 unsigned Opcode,
260 unsigned DstReg,
261 unsigned Src0Reg,
262 unsigned Src1Reg = 0) const;
263
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000264 MachineInstr *buildSlotOfVectorInstruction(MachineBasicBlock &MBB,
265 MachineInstr *MI,
266 unsigned Slot,
267 unsigned DstReg) const;
268
Tom Stellard75aadc22012-12-11 21:25:42 +0000269 MachineInstr *buildMovImm(MachineBasicBlock &BB,
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000270 MachineBasicBlock::iterator I,
271 unsigned DstReg,
272 uint64_t Imm) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000273
Tom Stellard26a3b672013-10-22 18:19:10 +0000274 MachineInstr *buildMovInstr(MachineBasicBlock *MBB,
275 MachineBasicBlock::iterator I,
Tom Stellard2ff72622016-01-28 16:04:37 +0000276 unsigned DstReg, unsigned SrcReg) const;
Tom Stellard26a3b672013-10-22 18:19:10 +0000277
Tom Stellard75aadc22012-12-11 21:25:42 +0000278 /// \brief Get the index of Op in the MachineInstr.
279 ///
280 /// \returns -1 if the Instruction does not contain the specified \p Op.
Tom Stellard02661d92013-06-25 21:22:18 +0000281 int getOperandIdx(const MachineInstr &MI, unsigned Op) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000282
283 /// \brief Get the index of \p Op for the given Opcode.
284 ///
285 /// \returns -1 if the Instruction does not contain the specified \p Op.
Tom Stellard02661d92013-06-25 21:22:18 +0000286 int getOperandIdx(unsigned Opcode, unsigned Op) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000287
288 /// \brief Helper function for setting instruction flag values.
Tom Stellard02661d92013-06-25 21:22:18 +0000289 void setImmOperand(MachineInstr *MI, unsigned Op, int64_t Imm) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000290
291 /// \returns true if this instruction has an operand for storing target flags.
292 bool hasFlagOperand(const MachineInstr &MI) const;
293
294 ///\brief Add one of the MO_FLAG* flags to the specified \p Operand.
295 void addFlag(MachineInstr *MI, unsigned Operand, unsigned Flag) const;
296
297 ///\brief Determine if the specified \p Flag is set on this \p Operand.
298 bool isFlagSet(const MachineInstr &MI, unsigned Operand, unsigned Flag) const;
299
300 /// \param SrcIdx The register source to set the flag on (e.g src0, src1, src2)
301 /// \param Flag The flag being set.
302 ///
303 /// \returns the operand containing the flags for this instruction.
304 MachineOperand &getFlagOp(MachineInstr *MI, unsigned SrcIdx = 0,
305 unsigned Flag = 0) const;
306
307 /// \brief Clear the specified flag on the instruction.
308 void clearFlag(MachineInstr *MI, unsigned Operand, unsigned Flag) const;
Tom Stellard2ff72622016-01-28 16:04:37 +0000309
310 // Helper functions that check the opcode for status information
311 bool isRegisterStore(const MachineInstr &MI) const;
312 bool isRegisterLoad(const MachineInstr &MI) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000313};
314
Tom Stellard13c68ef2013-09-05 18:38:09 +0000315namespace AMDGPU {
316
317int getLDSNoRetOp(uint16_t Opcode);
318
319} //End namespace AMDGPU
320
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000321} // End llvm namespace
Tom Stellard75aadc22012-12-11 21:25:42 +0000322
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000323#endif