blob: b5b33a60b9e423b2e7a861452c2e1abf4a6e24e2 [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"
19#include "R600Defines.h"
20#include "R600RegisterInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include <map>
22
23namespace llvm {
Tom Stellard75aadc22012-12-11 21:25:42 +000024 class AMDGPUTargetMachine;
25 class DFAPacketizer;
Tom Stellard75aadc22012-12-11 21:25:42 +000026 class MachineFunction;
27 class MachineInstr;
28 class MachineInstrBuilder;
29
Matt Arsenault6b6a2c32016-03-11 08:00:27 +000030 class R600InstrInfo final : public AMDGPUInstrInfo {
Tom Stellard75aadc22012-12-11 21:25:42 +000031 private:
32 const R600RegisterInfo RI;
33
Vincent Lejeune0fca91d2013-05-17 16:50:02 +000034 std::vector<std::pair<int, unsigned> >
Vincent Lejeunebb8a87212013-06-29 19:32:29 +000035 ExtractSrcs(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PV, unsigned &ConstCount) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000036
Tom Stellard880a80a2014-06-17 16:53:14 +000037
38 MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
39 MachineBasicBlock::iterator I,
40 unsigned ValueReg, unsigned Address,
41 unsigned OffsetReg,
42 unsigned AddrChan) const;
43
44 MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
45 MachineBasicBlock::iterator I,
46 unsigned ValueReg, unsigned Address,
47 unsigned OffsetReg,
48 unsigned AddrChan) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000049 public:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +000050 enum BankSwizzle {
Vincent Lejeunebb8a87212013-06-29 19:32:29 +000051 ALU_VEC_012_SCL_210 = 0,
52 ALU_VEC_021_SCL_122,
53 ALU_VEC_120_SCL_212,
54 ALU_VEC_102_SCL_221,
Vincent Lejeune0fca91d2013-05-17 16:50:02 +000055 ALU_VEC_201,
56 ALU_VEC_210
57 };
58
Tom Stellard2e59a452014-06-13 01:32:00 +000059 explicit R600InstrInfo(const AMDGPUSubtarget &st);
Tom Stellard75aadc22012-12-11 21:25:42 +000060
Craig Topper5656db42014-04-29 07:57:24 +000061 const R600RegisterInfo &getRegisterInfo() const override;
62 void copyPhysReg(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator MI, DebugLoc DL,
64 unsigned DestReg, unsigned SrcReg,
65 bool KillSrc) const override;
Tom Stellardcd6b0a62013-11-22 00:41:08 +000066 bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
Craig Topper5656db42014-04-29 07:57:24 +000067 MachineBasicBlock::iterator MBBI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000068
69 bool isTrig(const MachineInstr &MI) const;
70 bool isPlaceHolderOpcode(unsigned opcode) const;
71 bool isReductionOp(unsigned opcode) const;
72 bool isCubeOp(unsigned opcode) const;
73
74 /// \returns true if this \p Opcode represents an ALU instruction.
75 bool isALUInstr(unsigned Opcode) const;
Tom Stellardc026e8b2013-06-28 15:47:08 +000076 bool hasInstrModifiers(unsigned Opcode) const;
77 bool isLDSInstr(unsigned Opcode) const;
Tom Stellard8f9fc202013-11-15 00:12:45 +000078 bool isLDSNoRetInstr(unsigned Opcode) const;
79 bool isLDSRetInstr(unsigned Opcode) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000080
Vincent Lejeunea4da6fb2013-10-01 19:32:58 +000081 /// \returns true if this \p Opcode represents an ALU instruction or an
82 /// instruction that will be lowered in ExpandSpecialInstrs Pass.
83 bool canBeConsideredALU(const MachineInstr *MI) const;
84
Vincent Lejeune076c0b22013-04-30 00:14:17 +000085 bool isTransOnly(unsigned Opcode) const;
86 bool isTransOnly(const MachineInstr *MI) const;
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +000087 bool isVectorOnly(unsigned Opcode) const;
88 bool isVectorOnly(const MachineInstr *MI) const;
Tom Stellard676c16d2013-08-16 01:11:51 +000089 bool isExport(unsigned Opcode) const;
Vincent Lejeune076c0b22013-04-30 00:14:17 +000090
Vincent Lejeunec2991642013-04-30 00:13:39 +000091 bool usesVertexCache(unsigned Opcode) const;
92 bool usesVertexCache(const MachineInstr *MI) const;
93 bool usesTextureCache(unsigned Opcode) const;
94 bool usesTextureCache(const MachineInstr *MI) const;
95
Tom Stellardce540332013-06-28 15:46:59 +000096 bool mustBeLastInClause(unsigned Opcode) const;
Tom Stellard26a3b672013-10-22 18:19:10 +000097 bool usesAddressRegister(MachineInstr *MI) const;
98 bool definesAddressRegister(MachineInstr *MI) const;
Tom Stellard7f6fa4c2013-09-12 02:55:06 +000099 bool readsLDSSrcReg(const MachineInstr *MI) const;
Tom Stellardce540332013-06-28 15:46:59 +0000100
Tom Stellard84021442013-07-23 01:48:24 +0000101 /// \returns The operand index for the given source number. Legal values
102 /// for SrcNum are 0, 1, and 2.
103 int getSrcIdx(unsigned Opcode, unsigned SrcNum) const;
104 /// \returns The operand Index for the Sel operand given an index to one
105 /// of the instruction's src operands.
106 int getSelIdx(unsigned Opcode, unsigned SrcIdx) const;
107
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000108 /// \returns a pair for each src of an ALU instructions.
109 /// The first member of a pair is the register id.
110 /// If register is ALU_CONST, second member is SEL.
111 /// If register is ALU_LITERAL, second member is IMM.
112 /// Otherwise, second member value is undefined.
113 SmallVector<std::pair<MachineOperand *, int64_t>, 3>
114 getSrcs(MachineInstr *MI) const;
115
Vincent Lejeune77a83522013-06-29 19:32:43 +0000116 unsigned isLegalUpTo(
117 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
118 const std::vector<R600InstrInfo::BankSwizzle> &Swz,
119 const std::vector<std::pair<int, unsigned> > &TransSrcs,
120 R600InstrInfo::BankSwizzle TransSwz) const;
121
122 bool FindSwizzleForVectorSlot(
123 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
124 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
125 const std::vector<std::pair<int, unsigned> > &TransSrcs,
126 R600InstrInfo::BankSwizzle TransSwz) const;
Tom Stellardc026e8b2013-06-28 15:47:08 +0000127
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000128 /// Given the order VEC_012 < VEC_021 < VEC_120 < VEC_102 < VEC_201 < VEC_210
129 /// returns true and the first (in lexical order) BankSwizzle affectation
130 /// starting from the one already provided in the Instruction Group MIs that
131 /// fits Read Port limitations in BS if available. Otherwise returns false
132 /// and undefined content in BS.
Vincent Lejeune77a83522013-06-29 19:32:43 +0000133 /// isLastAluTrans should be set if the last Alu of MIs will be executed on
134 /// Trans ALU. In this case, ValidTSwizzle returns the BankSwizzle value to
135 /// apply to the last instruction.
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000136 /// PV holds GPR to PV registers in the Instruction Group MIs.
137 bool fitsReadPortLimitations(const std::vector<MachineInstr *> &MIs,
138 const DenseMap<unsigned, unsigned> &PV,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000139 std::vector<BankSwizzle> &BS,
140 bool isLastAluTrans) const;
141
142 /// An instruction group can only access 2 channel pair (either [XY] or [ZW])
143 /// from KCache bank on R700+. This function check if MI set in input meet
144 /// this limitations
145 bool fitsConstReadLimitations(const std::vector<MachineInstr *> &) const;
146 /// Same but using const index set instead of MI set.
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000147 bool fitsConstReadLimitations(const std::vector<unsigned>&) const;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000148
Alp Tokercb402912014-01-24 17:20:08 +0000149 /// \brief Vector instructions are instructions that must fill all
Tom Stellard75aadc22012-12-11 21:25:42 +0000150 /// instruction slots within an instruction group.
151 bool isVector(const MachineInstr &MI) const;
152
Tom Stellard2ff72622016-01-28 16:04:37 +0000153 bool isMov(unsigned Opcode) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000154
Eric Christopher143f02c2014-10-09 01:59:35 +0000155 DFAPacketizer *
156 CreateTargetScheduleState(const TargetSubtargetInfo &) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000157
Craig Topper5656db42014-04-29 07:57:24 +0000158 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000159
160 bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
Craig Topper5656db42014-04-29 07:57:24 +0000161 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000162
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000163 unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
164 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
165 DebugLoc DL) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000166
Craig Topper5656db42014-04-29 07:57:24 +0000167 unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000168
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000169 bool isPredicated(const MachineInstr &MI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000170
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000171 bool isPredicable(MachineInstr &MI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000172
173 bool
174 isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCyles,
Cong Houc536bd92015-09-10 23:10:42 +0000175 BranchProbability Probability) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000176
177 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCyles,
178 unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000179 BranchProbability Probability) const override ;
Tom Stellard75aadc22012-12-11 21:25:42 +0000180
181 bool
182 isProfitableToIfCvt(MachineBasicBlock &TMBB,
183 unsigned NumTCycles, unsigned ExtraTCycles,
184 MachineBasicBlock &FMBB,
185 unsigned NumFCycles, unsigned ExtraFCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000186 BranchProbability Probability) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000187
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000188 bool DefinesPredicate(MachineInstr &MI,
189 std::vector<MachineOperand> &Pred) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000190
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000191 bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
192 ArrayRef<MachineOperand> Pred2) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000193
194 bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
Craig Topper5656db42014-04-29 07:57:24 +0000195 MachineBasicBlock &FMBB) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000196
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000197 bool PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000198 ArrayRef<MachineOperand> Pred) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000199
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000200 unsigned int getPredicationCost(const MachineInstr &) const override;
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +0000201
Tom Stellard75aadc22012-12-11 21:25:42 +0000202 unsigned int getInstrLatency(const InstrItineraryData *ItinData,
203 const MachineInstr *MI,
Craig Topper5656db42014-04-29 07:57:24 +0000204 unsigned *PredCost = nullptr) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000205
Craig Topper5656db42014-04-29 07:57:24 +0000206 int getInstrLatency(const InstrItineraryData *ItinData,
207 SDNode *Node) const override { return 1;}
Tom Stellard75aadc22012-12-11 21:25:42 +0000208
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000209 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
Tom Stellard880a80a2014-06-17 16:53:14 +0000210
Tom Stellard81d871d2013-11-13 23:36:50 +0000211 /// \brief Reserve the registers that may be accesed using indirect addressing.
212 void reserveIndirectRegisters(BitVector &Reserved,
213 const MachineFunction &MF) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000214
Tom Stellard1242ce92016-02-05 18:44:57 +0000215 /// Calculate the "Indirect Address" for the given \p RegIndex and
216 /// \p Channel
217 ///
218 /// We model indirect addressing using a virtual address space that can be
219 /// accesed with loads and stores. The "Indirect Address" is the memory
220 /// address in this virtual address space that maps to the given \p RegIndex
221 /// and \p Channel.
222 unsigned calculateIndirectAddress(unsigned RegIndex, unsigned Channel) const;
223
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000224
Craig Topper5656db42014-04-29 07:57:24 +0000225 const TargetRegisterClass *getIndirectAddrRegClass() const override;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000226
Tom Stellard1242ce92016-02-05 18:44:57 +0000227 /// \brief Build instruction(s) for an indirect register write.
228 ///
229 /// \returns The instruction that performs the indirect register write
Craig Topper5656db42014-04-29 07:57:24 +0000230 MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
Tom Stellard1242ce92016-02-05 18:44:57 +0000231 MachineBasicBlock::iterator I,
232 unsigned ValueReg, unsigned Address,
233 unsigned OffsetReg) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000234
Tom Stellard1242ce92016-02-05 18:44:57 +0000235 /// \brief Build instruction(s) for an indirect register read.
236 ///
237 /// \returns The instruction that performs the indirect register read
Craig Topper5656db42014-04-29 07:57:24 +0000238 MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
239 MachineBasicBlock::iterator I,
240 unsigned ValueReg, unsigned Address,
Tom Stellard1242ce92016-02-05 18:44:57 +0000241 unsigned OffsetReg) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000242
Vincent Lejeune80031d9f2013-04-03 16:49:34 +0000243 unsigned getMaxAlusPerClause() const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000244
245 ///buildDefaultInstruction - This function returns a MachineInstr with
246 /// all the instruction modifiers initialized to their default values.
Tom Stellard75aadc22012-12-11 21:25:42 +0000247 /// You can use this function to avoid manually specifying each instruction
248 /// modifier operand when building a new instruction.
249 ///
250 /// \returns a MachineInstr with all the instruction modifiers initialized
251 /// to their default values.
252 MachineInstrBuilder buildDefaultInstruction(MachineBasicBlock &MBB,
253 MachineBasicBlock::iterator I,
254 unsigned Opcode,
255 unsigned DstReg,
256 unsigned Src0Reg,
257 unsigned Src1Reg = 0) const;
258
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000259 MachineInstr *buildSlotOfVectorInstruction(MachineBasicBlock &MBB,
260 MachineInstr *MI,
261 unsigned Slot,
262 unsigned DstReg) const;
263
Tom Stellard75aadc22012-12-11 21:25:42 +0000264 MachineInstr *buildMovImm(MachineBasicBlock &BB,
265 MachineBasicBlock::iterator I,
266 unsigned DstReg,
267 uint64_t Imm) const;
268
Tom Stellard26a3b672013-10-22 18:19:10 +0000269 MachineInstr *buildMovInstr(MachineBasicBlock *MBB,
270 MachineBasicBlock::iterator I,
Tom Stellard2ff72622016-01-28 16:04:37 +0000271 unsigned DstReg, unsigned SrcReg) const;
Tom Stellard26a3b672013-10-22 18:19:10 +0000272
Tom Stellard75aadc22012-12-11 21:25:42 +0000273 /// \brief Get the index of Op in the MachineInstr.
274 ///
275 /// \returns -1 if the Instruction does not contain the specified \p Op.
Tom Stellard02661d92013-06-25 21:22:18 +0000276 int getOperandIdx(const MachineInstr &MI, unsigned Op) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000277
278 /// \brief Get the index of \p Op for the given Opcode.
279 ///
280 /// \returns -1 if the Instruction does not contain the specified \p Op.
Tom Stellard02661d92013-06-25 21:22:18 +0000281 int getOperandIdx(unsigned Opcode, unsigned Op) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000282
283 /// \brief Helper function for setting instruction flag values.
Tom Stellard02661d92013-06-25 21:22:18 +0000284 void setImmOperand(MachineInstr *MI, unsigned Op, int64_t Imm) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000285
286 /// \returns true if this instruction has an operand for storing target flags.
287 bool hasFlagOperand(const MachineInstr &MI) const;
288
289 ///\brief Add one of the MO_FLAG* flags to the specified \p Operand.
290 void addFlag(MachineInstr *MI, unsigned Operand, unsigned Flag) const;
291
292 ///\brief Determine if the specified \p Flag is set on this \p Operand.
293 bool isFlagSet(const MachineInstr &MI, unsigned Operand, unsigned Flag) const;
294
295 /// \param SrcIdx The register source to set the flag on (e.g src0, src1, src2)
296 /// \param Flag The flag being set.
297 ///
298 /// \returns the operand containing the flags for this instruction.
299 MachineOperand &getFlagOp(MachineInstr *MI, unsigned SrcIdx = 0,
300 unsigned Flag = 0) const;
301
302 /// \brief Clear the specified flag on the instruction.
303 void clearFlag(MachineInstr *MI, unsigned Operand, unsigned Flag) const;
Tom Stellard2ff72622016-01-28 16:04:37 +0000304
305 // Helper functions that check the opcode for status information
306 bool isRegisterStore(const MachineInstr &MI) const;
307 bool isRegisterLoad(const MachineInstr &MI) const;
308
Tom Stellard75aadc22012-12-11 21:25:42 +0000309};
310
Tom Stellard13c68ef2013-09-05 18:38:09 +0000311namespace AMDGPU {
312
313int getLDSNoRetOp(uint16_t Opcode);
314
315} //End namespace AMDGPU
316
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000317} // End llvm namespace
Tom Stellard75aadc22012-12-11 21:25:42 +0000318
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000319#endif