blob: 31cdb0a2cfc6364beb2d8c281477ac22c8c2e602 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- AMDGPUInstrInfo.h - AMDGPU Instruction Information ------*- 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 Contains the definition of a TargetInstrInfo class that is common
12/// to all AMD GPUs.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef AMDGPUINSTRUCTIONINFO_H
17#define AMDGPUINSTRUCTIONINFO_H
18
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "AMDGPUInstrInfo.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000020#include "AMDGPURegisterInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "llvm/Target/TargetInstrInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000022#include <map>
23
24#define GET_INSTRINFO_HEADER
25#define GET_INSTRINFO_ENUM
Tom Stellard02661d92013-06-25 21:22:18 +000026#define GET_INSTRINFO_OPERAND_ENUM
Tom Stellard75aadc22012-12-11 21:25:42 +000027#include "AMDGPUGenInstrInfo.inc"
28
29#define OPCODE_IS_ZERO_INT AMDGPU::PRED_SETE_INT
30#define OPCODE_IS_NOT_ZERO_INT AMDGPU::PRED_SETNE_INT
31#define OPCODE_IS_ZERO AMDGPU::PRED_SETE
32#define OPCODE_IS_NOT_ZERO AMDGPU::PRED_SETNE
33
34namespace llvm {
35
Tom Stellard2e59a452014-06-13 01:32:00 +000036class AMDGPUSubtarget;
Tom Stellard75aadc22012-12-11 21:25:42 +000037class MachineFunction;
38class MachineInstr;
39class MachineInstrBuilder;
40
41class AMDGPUInstrInfo : public AMDGPUGenInstrInfo {
42private:
43 const AMDGPURegisterInfo RI;
Tom Stellard75aadc22012-12-11 21:25:42 +000044 bool getNextBranchInstr(MachineBasicBlock::iterator &iter,
45 MachineBasicBlock &MBB) const;
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000046 virtual void anchor();
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000047protected:
Tom Stellard2e59a452014-06-13 01:32:00 +000048 const AMDGPUSubtarget &ST;
Tom Stellard75aadc22012-12-11 21:25:42 +000049public:
Tom Stellard2e59a452014-06-13 01:32:00 +000050 explicit AMDGPUInstrInfo(const AMDGPUSubtarget &st);
Tom Stellard75aadc22012-12-11 21:25:42 +000051
52 virtual const AMDGPURegisterInfo &getRegisterInfo() const = 0;
53
54 bool isCoalescableExtInstr(const MachineInstr &MI, unsigned &SrcReg,
Craig Topper5656db42014-04-29 07:57:24 +000055 unsigned &DstReg, unsigned &SubIdx) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000056
Craig Topper5656db42014-04-29 07:57:24 +000057 unsigned isLoadFromStackSlot(const MachineInstr *MI,
58 int &FrameIndex) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000059 unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
Craig Topper5656db42014-04-29 07:57:24 +000060 int &FrameIndex) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000061 bool hasLoadFromStackSlot(const MachineInstr *MI,
62 const MachineMemOperand *&MMO,
Craig Topper5656db42014-04-29 07:57:24 +000063 int &FrameIndex) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000064 unsigned isStoreFromStackSlot(const MachineInstr *MI, int &FrameIndex) const;
65 unsigned isStoreFromStackSlotPostFE(const MachineInstr *MI,
66 int &FrameIndex) const;
67 bool hasStoreFromStackSlot(const MachineInstr *MI,
68 const MachineMemOperand *&MMO,
69 int &FrameIndex) const;
70
71 MachineInstr *
72 convertToThreeAddress(MachineFunction::iterator &MFI,
73 MachineBasicBlock::iterator &MBBI,
Craig Topper5656db42014-04-29 07:57:24 +000074 LiveVariables *LV) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000075
76
77 virtual void copyPhysReg(MachineBasicBlock &MBB,
78 MachineBasicBlock::iterator MI, DebugLoc DL,
79 unsigned DestReg, unsigned SrcReg,
80 bool KillSrc) const = 0;
81
Craig Topper5656db42014-04-29 07:57:24 +000082 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
Tom Stellard26a3b672013-10-22 18:19:10 +000083
Craig Topper5656db42014-04-29 07:57:24 +000084 void storeRegToStackSlot(MachineBasicBlock &MBB,
85 MachineBasicBlock::iterator MI,
86 unsigned SrcReg, bool isKill, int FrameIndex,
87 const TargetRegisterClass *RC,
88 const TargetRegisterInfo *TRI) const override;
89 void loadRegFromStackSlot(MachineBasicBlock &MBB,
90 MachineBasicBlock::iterator MI,
91 unsigned DestReg, int FrameIndex,
92 const TargetRegisterClass *RC,
93 const TargetRegisterInfo *TRI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +000094
95protected:
96 MachineInstr *foldMemoryOperandImpl(MachineFunction &MF,
97 MachineInstr *MI,
98 const SmallVectorImpl<unsigned> &Ops,
Craig Topper5656db42014-04-29 07:57:24 +000099 int FrameIndex) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000100 MachineInstr *foldMemoryOperandImpl(MachineFunction &MF,
101 MachineInstr *MI,
102 const SmallVectorImpl<unsigned> &Ops,
Craig Topper5656db42014-04-29 07:57:24 +0000103 MachineInstr *LoadMI) const override;
Tom Stellard81d871d2013-11-13 23:36:50 +0000104 /// \returns the smallest register index that will be accessed by an indirect
105 /// read or write or -1 if indirect addressing is not used by this program.
Craig Topperee7b0f32014-04-30 05:53:27 +0000106 int getIndirectIndexBegin(const MachineFunction &MF) const;
Tom Stellard81d871d2013-11-13 23:36:50 +0000107
108 /// \returns the largest register index that will be accessed by an indirect
109 /// read or write or -1 if indirect addressing is not used by this program.
Craig Topperee7b0f32014-04-30 05:53:27 +0000110 int getIndirectIndexEnd(const MachineFunction &MF) const;
Tom Stellard81d871d2013-11-13 23:36:50 +0000111
Tom Stellard75aadc22012-12-11 21:25:42 +0000112public:
113 bool canFoldMemoryOperand(const MachineInstr *MI,
Craig Topper5656db42014-04-29 07:57:24 +0000114 const SmallVectorImpl<unsigned> &Ops) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000115 bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
Craig Topper5656db42014-04-29 07:57:24 +0000116 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
117 SmallVectorImpl<MachineInstr *> &NewMIs) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000118 bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
Craig Topper5656db42014-04-29 07:57:24 +0000119 SmallVectorImpl<SDNode *> &NewNodes) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000120 unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
Craig Topper5656db42014-04-29 07:57:24 +0000121 bool UnfoldLoad, bool UnfoldStore,
122 unsigned *LoadRegIndex = nullptr) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000123 bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
124 int64_t Offset1, int64_t Offset2,
Craig Topper5656db42014-04-29 07:57:24 +0000125 unsigned NumLoads) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000126
Craig Topper5656db42014-04-29 07:57:24 +0000127 bool
128 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000129 void insertNoop(MachineBasicBlock &MBB,
Craig Topper5656db42014-04-29 07:57:24 +0000130 MachineBasicBlock::iterator MI) const override;
131 bool isPredicated(const MachineInstr *MI) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000132 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
Craig Topper5656db42014-04-29 07:57:24 +0000133 const SmallVectorImpl<MachineOperand> &Pred2) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000134 bool DefinesPredicate(MachineInstr *MI,
Craig Topper5656db42014-04-29 07:57:24 +0000135 std::vector<MachineOperand> &Pred) const override;
136 bool isPredicable(MachineInstr *MI) const override;
137 bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000138
139 // Helper functions that check the opcode for status information
140 bool isLoadInst(llvm::MachineInstr *MI) const;
141 bool isExtLoadInst(llvm::MachineInstr *MI) const;
142 bool isSWSExtLoadInst(llvm::MachineInstr *MI) const;
143 bool isSExtLoadInst(llvm::MachineInstr *MI) const;
144 bool isZExtLoadInst(llvm::MachineInstr *MI) const;
145 bool isAExtLoadInst(llvm::MachineInstr *MI) const;
146 bool isStoreInst(llvm::MachineInstr *MI) const;
147 bool isTruncStoreInst(llvm::MachineInstr *MI) const;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000148 bool isRegisterStore(const MachineInstr &MI) const;
149 bool isRegisterLoad(const MachineInstr &MI) const;
150
151//===---------------------------------------------------------------------===//
152// Pure virtual funtions to be implemented by sub-classes.
153//===---------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000154
Tom Stellard75aadc22012-12-11 21:25:42 +0000155 virtual unsigned getIEQOpcode() const = 0;
156 virtual bool isMov(unsigned opcode) const = 0;
157
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000158 /// \brief Calculate the "Indirect Address" for the given \p RegIndex and
159 /// \p Channel
160 ///
161 /// We model indirect addressing using a virtual address space that can be
162 /// accesed with loads and stores. The "Indirect Address" is the memory
163 /// address in this virtual address space that maps to the given \p RegIndex
164 /// and \p Channel.
165 virtual unsigned calculateIndirectAddress(unsigned RegIndex,
166 unsigned Channel) const = 0;
167
Tom Stellard26a3b672013-10-22 18:19:10 +0000168 /// \returns The register class to be used for loading and storing values
169 /// from an "Indirect Address" .
170 virtual const TargetRegisterClass *getIndirectAddrRegClass() const = 0;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000171
172 /// \brief Build instruction(s) for an indirect register write.
173 ///
174 /// \returns The instruction that performs the indirect register write
175 virtual MachineInstrBuilder buildIndirectWrite(MachineBasicBlock *MBB,
176 MachineBasicBlock::iterator I,
177 unsigned ValueReg, unsigned Address,
178 unsigned OffsetReg) const = 0;
179
180 /// \brief Build instruction(s) for an indirect register read.
181 ///
182 /// \returns The instruction that performs the indirect register read
183 virtual MachineInstrBuilder buildIndirectRead(MachineBasicBlock *MBB,
184 MachineBasicBlock::iterator I,
185 unsigned ValueReg, unsigned Address,
186 unsigned OffsetReg) const = 0;
187
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000188
Tom Stellard75aadc22012-12-11 21:25:42 +0000189 /// \brief Convert the AMDIL MachineInstr to a supported ISA
190 /// MachineInstr
Craig Topperee7b0f32014-04-30 05:53:27 +0000191 void convertToISA(MachineInstr & MI, MachineFunction &MF, DebugLoc DL) const;
Tom Stellard75aadc22012-12-11 21:25:42 +0000192
Tom Stellard26a3b672013-10-22 18:19:10 +0000193 /// \brief Build a MOV instruction.
194 virtual MachineInstr *buildMovInstr(MachineBasicBlock *MBB,
195 MachineBasicBlock::iterator I,
196 unsigned DstReg, unsigned SrcReg) const = 0;
197
Tom Stellard682bfbc2013-10-10 17:11:24 +0000198 /// \brief Given a MIMG \p Opcode that writes all 4 channels, return the
199 /// equivalent opcode that writes \p Channels Channels.
200 int getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const;
Tom Stellard26a3b672013-10-22 18:19:10 +0000201
Tom Stellard75aadc22012-12-11 21:25:42 +0000202};
203
Tom Stellard02661d92013-06-25 21:22:18 +0000204namespace AMDGPU {
205 int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIndex);
206} // End namespace AMDGPU
207
Tom Stellard75aadc22012-12-11 21:25:42 +0000208} // End llvm namespace
209
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000210#define AMDGPU_FLAG_REGISTER_LOAD (UINT64_C(1) << 63)
211#define AMDGPU_FLAG_REGISTER_STORE (UINT64_C(1) << 62)
212
Tom Stellard75aadc22012-12-11 21:25:42 +0000213#endif // AMDGPUINSTRINFO_H