blob: de586e74eaf892b42937a5135f0a90d179e691e8 [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
22namespace llvm {
Tom Stellard75aadc22012-12-11 21:25:42 +000023 class AMDGPUTargetMachine;
24 class DFAPacketizer;
Tom Stellard75aadc22012-12-11 21:25:42 +000025 class MachineFunction;
26 class MachineInstr;
27 class MachineInstrBuilder;
28
Matt Arsenault6b6a2c32016-03-11 08:00:27 +000029 class R600InstrInfo final : public AMDGPUInstrInfo {
Tom Stellard75aadc22012-12-11 21:25:42 +000030 private:
31 const R600RegisterInfo RI;
32
Vincent Lejeune0fca91d2013-05-17 16:50:02 +000033 std::vector<std::pair<int, unsigned> >
Vincent Lejeunebb8a87212013-06-29 19:32:29 +000034 ExtractSrcs(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PV, unsigned &ConstCount) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000035
Tom Stellard880a80a2014-06-17 16:53:14 +000036
37 MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
38 MachineBasicBlock::iterator I,
39 unsigned ValueReg, unsigned Address,
40 unsigned OffsetReg,
41 unsigned AddrChan) const;
42
43 MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
44 MachineBasicBlock::iterator I,
45 unsigned ValueReg, unsigned Address,
46 unsigned OffsetReg,
47 unsigned AddrChan) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000048 public:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +000049 enum BankSwizzle {
Vincent Lejeunebb8a87212013-06-29 19:32:29 +000050 ALU_VEC_012_SCL_210 = 0,
51 ALU_VEC_021_SCL_122,
52 ALU_VEC_120_SCL_212,
53 ALU_VEC_102_SCL_221,
Vincent Lejeune0fca91d2013-05-17 16:50:02 +000054 ALU_VEC_201,
55 ALU_VEC_210
56 };
57
Tom Stellard2e59a452014-06-13 01:32:00 +000058 explicit R600InstrInfo(const AMDGPUSubtarget &st);
Tom Stellard75aadc22012-12-11 21:25:42 +000059
Craig Topper5656db42014-04-29 07:57:24 +000060 const R600RegisterInfo &getRegisterInfo() const override;
Benjamin Kramerbdc49562016-06-12 15:39:02 +000061 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
62 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
Craig Topper5656db42014-04-29 07:57:24 +000063 bool KillSrc) const override;
Tom Stellardcd6b0a62013-11-22 00:41:08 +000064 bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
Craig Topper5656db42014-04-29 07:57:24 +000065 MachineBasicBlock::iterator MBBI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000066
67 bool isTrig(const MachineInstr &MI) const;
68 bool isPlaceHolderOpcode(unsigned opcode) const;
69 bool isReductionOp(unsigned opcode) const;
70 bool isCubeOp(unsigned opcode) const;
71
72 /// \returns true if this \p Opcode represents an ALU instruction.
73 bool isALUInstr(unsigned Opcode) const;
Tom Stellardc026e8b2013-06-28 15:47:08 +000074 bool hasInstrModifiers(unsigned Opcode) const;
75 bool isLDSInstr(unsigned Opcode) const;
Tom Stellard8f9fc202013-11-15 00:12:45 +000076 bool isLDSNoRetInstr(unsigned Opcode) const;
77 bool isLDSRetInstr(unsigned Opcode) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000078
Vincent Lejeunea4da6fb2013-10-01 19:32:58 +000079 /// \returns true if this \p Opcode represents an ALU instruction or an
80 /// instruction that will be lowered in ExpandSpecialInstrs Pass.
81 bool canBeConsideredALU(const MachineInstr *MI) const;
82
Vincent Lejeune076c0b22013-04-30 00:14:17 +000083 bool isTransOnly(unsigned Opcode) const;
84 bool isTransOnly(const MachineInstr *MI) const;
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +000085 bool isVectorOnly(unsigned Opcode) const;
86 bool isVectorOnly(const MachineInstr *MI) const;
Tom Stellard676c16d2013-08-16 01:11:51 +000087 bool isExport(unsigned Opcode) const;
Vincent Lejeune076c0b22013-04-30 00:14:17 +000088
Vincent Lejeunec2991642013-04-30 00:13:39 +000089 bool usesVertexCache(unsigned Opcode) const;
90 bool usesVertexCache(const MachineInstr *MI) const;
91 bool usesTextureCache(unsigned Opcode) const;
92 bool usesTextureCache(const MachineInstr *MI) const;
93
Tom Stellardce540332013-06-28 15:46:59 +000094 bool mustBeLastInClause(unsigned Opcode) const;
Tom Stellard26a3b672013-10-22 18:19:10 +000095 bool usesAddressRegister(MachineInstr *MI) const;
96 bool definesAddressRegister(MachineInstr *MI) const;
Tom Stellard7f6fa4c2013-09-12 02:55:06 +000097 bool readsLDSSrcReg(const MachineInstr *MI) const;
Tom Stellardce540332013-06-28 15:46:59 +000098
Tom Stellard84021442013-07-23 01:48:24 +000099 /// \returns The operand index for the given source number. Legal values
100 /// for SrcNum are 0, 1, and 2.
101 int getSrcIdx(unsigned Opcode, unsigned SrcNum) const;
102 /// \returns The operand Index for the Sel operand given an index to one
103 /// of the instruction's src operands.
104 int getSelIdx(unsigned Opcode, unsigned SrcIdx) const;
105
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000106 /// \returns a pair for each src of an ALU instructions.
107 /// The first member of a pair is the register id.
108 /// If register is ALU_CONST, second member is SEL.
109 /// If register is ALU_LITERAL, second member is IMM.
110 /// Otherwise, second member value is undefined.
111 SmallVector<std::pair<MachineOperand *, int64_t>, 3>
112 getSrcs(MachineInstr *MI) const;
113
Vincent Lejeune77a83522013-06-29 19:32:43 +0000114 unsigned isLegalUpTo(
115 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
116 const std::vector<R600InstrInfo::BankSwizzle> &Swz,
117 const std::vector<std::pair<int, unsigned> > &TransSrcs,
118 R600InstrInfo::BankSwizzle TransSwz) const;
119
120 bool FindSwizzleForVectorSlot(
121 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
122 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
123 const std::vector<std::pair<int, unsigned> > &TransSrcs,
124 R600InstrInfo::BankSwizzle TransSwz) const;
Tom Stellardc026e8b2013-06-28 15:47:08 +0000125
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000126 /// Given the order VEC_012 < VEC_021 < VEC_120 < VEC_102 < VEC_201 < VEC_210
127 /// returns true and the first (in lexical order) BankSwizzle affectation
128 /// starting from the one already provided in the Instruction Group MIs that
129 /// fits Read Port limitations in BS if available. Otherwise returns false
130 /// and undefined content in BS.
Vincent Lejeune77a83522013-06-29 19:32:43 +0000131 /// isLastAluTrans should be set if the last Alu of MIs will be executed on
132 /// Trans ALU. In this case, ValidTSwizzle returns the BankSwizzle value to
133 /// apply to the last instruction.
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000134 /// PV holds GPR to PV registers in the Instruction Group MIs.
135 bool fitsReadPortLimitations(const std::vector<MachineInstr *> &MIs,
136 const DenseMap<unsigned, unsigned> &PV,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000137 std::vector<BankSwizzle> &BS,
138 bool isLastAluTrans) const;
139
140 /// An instruction group can only access 2 channel pair (either [XY] or [ZW])
141 /// from KCache bank on R700+. This function check if MI set in input meet
142 /// this limitations
143 bool fitsConstReadLimitations(const std::vector<MachineInstr *> &) const;
144 /// Same but using const index set instead of MI set.
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000145 bool fitsConstReadLimitations(const std::vector<unsigned>&) const;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000146
Alp Tokercb402912014-01-24 17:20:08 +0000147 /// \brief Vector instructions are instructions that must fill all
Tom Stellard75aadc22012-12-11 21:25:42 +0000148 /// instruction slots within an instruction group.
149 bool isVector(const MachineInstr &MI) const;
150
Tom Stellard2ff72622016-01-28 16:04:37 +0000151 bool isMov(unsigned Opcode) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000152
Eric Christopher143f02c2014-10-09 01:59:35 +0000153 DFAPacketizer *
154 CreateTargetScheduleState(const TargetSubtargetInfo &) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000155
Craig Topper5656db42014-04-29 07:57:24 +0000156 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000157
158 bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
Craig Topper5656db42014-04-29 07:57:24 +0000159 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000160
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000161 unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
162 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000163 const DebugLoc &DL) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000164
Craig Topper5656db42014-04-29 07:57:24 +0000165 unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000166
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000167 bool isPredicated(const MachineInstr &MI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000168
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000169 bool isPredicable(MachineInstr &MI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000170
171 bool
172 isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCyles,
Cong Houc536bd92015-09-10 23:10:42 +0000173 BranchProbability Probability) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000174
175 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCyles,
176 unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000177 BranchProbability Probability) const override ;
Tom Stellard75aadc22012-12-11 21:25:42 +0000178
179 bool
180 isProfitableToIfCvt(MachineBasicBlock &TMBB,
181 unsigned NumTCycles, unsigned ExtraTCycles,
182 MachineBasicBlock &FMBB,
183 unsigned NumFCycles, unsigned ExtraFCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000184 BranchProbability Probability) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000185
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000186 bool DefinesPredicate(MachineInstr &MI,
187 std::vector<MachineOperand> &Pred) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000188
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000189 bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
190 ArrayRef<MachineOperand> Pred2) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000191
192 bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
Craig Topper5656db42014-04-29 07:57:24 +0000193 MachineBasicBlock &FMBB) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000194
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000195 bool PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000196 ArrayRef<MachineOperand> Pred) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000197
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000198 unsigned int getPredicationCost(const MachineInstr &) const override;
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +0000199
Tom Stellard75aadc22012-12-11 21:25:42 +0000200 unsigned int getInstrLatency(const InstrItineraryData *ItinData,
201 const MachineInstr *MI,
Craig Topper5656db42014-04-29 07:57:24 +0000202 unsigned *PredCost = nullptr) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000203
Craig Topper5656db42014-04-29 07:57:24 +0000204 int getInstrLatency(const InstrItineraryData *ItinData,
205 SDNode *Node) const override { return 1;}
Tom Stellard75aadc22012-12-11 21:25:42 +0000206
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000207 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
Tom Stellard880a80a2014-06-17 16:53:14 +0000208
Tom Stellard81d871d2013-11-13 23:36:50 +0000209 /// \brief Reserve the registers that may be accesed using indirect addressing.
210 void reserveIndirectRegisters(BitVector &Reserved,
211 const MachineFunction &MF) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000212
Tom Stellard1242ce92016-02-05 18:44:57 +0000213 /// Calculate the "Indirect Address" for the given \p RegIndex and
214 /// \p Channel
215 ///
216 /// We model indirect addressing using a virtual address space that can be
217 /// accesed with loads and stores. The "Indirect Address" is the memory
218 /// address in this virtual address space that maps to the given \p RegIndex
219 /// and \p Channel.
220 unsigned calculateIndirectAddress(unsigned RegIndex, unsigned Channel) const;
221
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000222
Craig Topper5656db42014-04-29 07:57:24 +0000223 const TargetRegisterClass *getIndirectAddrRegClass() const override;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000224
Tom Stellard1242ce92016-02-05 18:44:57 +0000225 /// \brief Build instruction(s) for an indirect register write.
226 ///
227 /// \returns The instruction that performs the indirect register write
Craig Topper5656db42014-04-29 07:57:24 +0000228 MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
Tom Stellard1242ce92016-02-05 18:44:57 +0000229 MachineBasicBlock::iterator I,
230 unsigned ValueReg, unsigned Address,
231 unsigned OffsetReg) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000232
Tom Stellard1242ce92016-02-05 18:44:57 +0000233 /// \brief Build instruction(s) for an indirect register read.
234 ///
235 /// \returns The instruction that performs the indirect register read
Craig Topper5656db42014-04-29 07:57:24 +0000236 MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
237 MachineBasicBlock::iterator I,
238 unsigned ValueReg, unsigned Address,
Tom Stellard1242ce92016-02-05 18:44:57 +0000239 unsigned OffsetReg) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000240
Vincent Lejeune80031d9f2013-04-03 16:49:34 +0000241 unsigned getMaxAlusPerClause() const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000242
243 ///buildDefaultInstruction - This function returns a MachineInstr with
244 /// all the instruction modifiers initialized to their default values.
Tom Stellard75aadc22012-12-11 21:25:42 +0000245 /// You can use this function to avoid manually specifying each instruction
246 /// modifier operand when building a new instruction.
247 ///
248 /// \returns a MachineInstr with all the instruction modifiers initialized
249 /// to their default values.
250 MachineInstrBuilder buildDefaultInstruction(MachineBasicBlock &MBB,
251 MachineBasicBlock::iterator I,
252 unsigned Opcode,
253 unsigned DstReg,
254 unsigned Src0Reg,
255 unsigned Src1Reg = 0) const;
256
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000257 MachineInstr *buildSlotOfVectorInstruction(MachineBasicBlock &MBB,
258 MachineInstr *MI,
259 unsigned Slot,
260 unsigned DstReg) const;
261
Tom Stellard75aadc22012-12-11 21:25:42 +0000262 MachineInstr *buildMovImm(MachineBasicBlock &BB,
263 MachineBasicBlock::iterator I,
264 unsigned DstReg,
265 uint64_t Imm) const;
266
Tom Stellard26a3b672013-10-22 18:19:10 +0000267 MachineInstr *buildMovInstr(MachineBasicBlock *MBB,
268 MachineBasicBlock::iterator I,
Tom Stellard2ff72622016-01-28 16:04:37 +0000269 unsigned DstReg, unsigned SrcReg) const;
Tom Stellard26a3b672013-10-22 18:19:10 +0000270
Tom Stellard75aadc22012-12-11 21:25:42 +0000271 /// \brief Get the index of Op in the MachineInstr.
272 ///
273 /// \returns -1 if the Instruction does not contain the specified \p Op.
Tom Stellard02661d92013-06-25 21:22:18 +0000274 int getOperandIdx(const MachineInstr &MI, unsigned Op) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000275
276 /// \brief Get the index of \p Op for the given Opcode.
277 ///
278 /// \returns -1 if the Instruction does not contain the specified \p Op.
Tom Stellard02661d92013-06-25 21:22:18 +0000279 int getOperandIdx(unsigned Opcode, unsigned Op) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000280
281 /// \brief Helper function for setting instruction flag values.
Tom Stellard02661d92013-06-25 21:22:18 +0000282 void setImmOperand(MachineInstr *MI, unsigned Op, int64_t Imm) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000283
284 /// \returns true if this instruction has an operand for storing target flags.
285 bool hasFlagOperand(const MachineInstr &MI) const;
286
287 ///\brief Add one of the MO_FLAG* flags to the specified \p Operand.
288 void addFlag(MachineInstr *MI, unsigned Operand, unsigned Flag) const;
289
290 ///\brief Determine if the specified \p Flag is set on this \p Operand.
291 bool isFlagSet(const MachineInstr &MI, unsigned Operand, unsigned Flag) const;
292
293 /// \param SrcIdx The register source to set the flag on (e.g src0, src1, src2)
294 /// \param Flag The flag being set.
295 ///
296 /// \returns the operand containing the flags for this instruction.
297 MachineOperand &getFlagOp(MachineInstr *MI, unsigned SrcIdx = 0,
298 unsigned Flag = 0) const;
299
300 /// \brief Clear the specified flag on the instruction.
301 void clearFlag(MachineInstr *MI, unsigned Operand, unsigned Flag) const;
Tom Stellard2ff72622016-01-28 16:04:37 +0000302
303 // Helper functions that check the opcode for status information
304 bool isRegisterStore(const MachineInstr &MI) const;
305 bool isRegisterLoad(const MachineInstr &MI) const;
306
Tom Stellard75aadc22012-12-11 21:25:42 +0000307};
308
Tom Stellard13c68ef2013-09-05 18:38:09 +0000309namespace AMDGPU {
310
311int getLDSNoRetOp(uint16_t Opcode);
312
313} //End namespace AMDGPU
314
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000315} // End llvm namespace
Tom Stellard75aadc22012-12-11 21:25:42 +0000316
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000317#endif