1 //===-- AMDGPUInstrInfo.cpp - Base class for AMD GPU InstrInfo ------------===//
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 Implementation of the TargetInstrInfo class that is common to all
14 //===----------------------------------------------------------------------===//
16 #include "AMDGPUInstrInfo.h"
17 #include "AMDGPURegisterInfo.h"
18 #include "AMDGPUTargetMachine.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #define GET_INSTRINFO_CTOR_DTOR
24 #define GET_INSTRINFO_NAMED_OPS
25 #define GET_INSTRMAP_INFO
26 #include "AMDGPUGenInstrInfo.inc"
31 // Pin the vtable to this file.
32 void AMDGPUInstrInfo::anchor() {}
34 AMDGPUInstrInfo::AMDGPUInstrInfo(TargetMachine &tm)
35 : AMDGPUGenInstrInfo(-1,-1), RI(tm), TM(tm) { }
37 const AMDGPURegisterInfo &AMDGPUInstrInfo::getRegisterInfo() const {
41 bool AMDGPUInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
42 unsigned &SrcReg, unsigned &DstReg,
43 unsigned &SubIdx) const {
44 // TODO: Implement this function
48 unsigned AMDGPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
49 int &FrameIndex) const {
50 // TODO: Implement this function
54 unsigned AMDGPUInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
55 int &FrameIndex) const {
56 // TODO: Implement this function
60 bool AMDGPUInstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
61 const MachineMemOperand *&MMO,
62 int &FrameIndex) const {
63 // TODO: Implement this function
66 unsigned AMDGPUInstrInfo::isStoreFromStackSlot(const MachineInstr *MI,
67 int &FrameIndex) const {
68 // TODO: Implement this function
71 unsigned AMDGPUInstrInfo::isStoreFromStackSlotPostFE(const MachineInstr *MI,
72 int &FrameIndex) const {
73 // TODO: Implement this function
76 bool AMDGPUInstrInfo::hasStoreFromStackSlot(const MachineInstr *MI,
77 const MachineMemOperand *&MMO,
78 int &FrameIndex) const {
79 // TODO: Implement this function
84 AMDGPUInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
85 MachineBasicBlock::iterator &MBBI,
86 LiveVariables *LV) const {
87 // TODO: Implement this function
90 bool AMDGPUInstrInfo::getNextBranchInstr(MachineBasicBlock::iterator &iter,
91 MachineBasicBlock &MBB) const {
92 while (iter != MBB.end()) {
93 switch (iter->getOpcode()) {
96 case AMDGPU::BRANCH_COND_i32:
97 case AMDGPU::BRANCH_COND_f32:
107 AMDGPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
108 MachineBasicBlock::iterator MI,
109 unsigned SrcReg, bool isKill,
111 const TargetRegisterClass *RC,
112 const TargetRegisterInfo *TRI) const {
113 llvm_unreachable("Not Implemented");
117 AMDGPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
118 MachineBasicBlock::iterator MI,
119 unsigned DestReg, int FrameIndex,
120 const TargetRegisterClass *RC,
121 const TargetRegisterInfo *TRI) const {
122 llvm_unreachable("Not Implemented");
125 bool AMDGPUInstrInfo::expandPostRAPseudo (MachineBasicBlock::iterator MI) const {
126 MachineBasicBlock *MBB = MI->getParent();
127 int OffsetOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
128 AMDGPU::OpName::addr);
129 // addr is a custom operand with multiple MI operands, and only the
130 // first MI operand is given a name.
131 int RegOpIdx = OffsetOpIdx + 1;
132 int ChanOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
133 AMDGPU::OpName::chan);
134 if (isRegisterLoad(*MI)) {
135 int DstOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
136 AMDGPU::OpName::dst);
137 unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
138 unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
139 unsigned Address = calculateIndirectAddress(RegIndex, Channel);
140 unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
141 if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
142 buildMovInstr(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
143 getIndirectAddrRegClass()->getRegister(Address));
145 buildIndirectRead(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
148 } else if (isRegisterStore(*MI)) {
149 int ValOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
150 AMDGPU::OpName::val);
151 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
152 unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
153 unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
154 unsigned Address = calculateIndirectAddress(RegIndex, Channel);
155 unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
156 if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
157 buildMovInstr(MBB, MI, getIndirectAddrRegClass()->getRegister(Address),
158 MI->getOperand(ValOpIdx).getReg());
160 buildIndirectWrite(MBB, MI, MI->getOperand(ValOpIdx).getReg(),
161 calculateIndirectAddress(RegIndex, Channel),
174 AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
176 const SmallVectorImpl<unsigned> &Ops,
177 int FrameIndex) const {
178 // TODO: Implement this function
182 AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
184 const SmallVectorImpl<unsigned> &Ops,
185 MachineInstr *LoadMI) const {
186 // TODO: Implement this function
190 AMDGPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
191 const SmallVectorImpl<unsigned> &Ops) const {
192 // TODO: Implement this function
196 AMDGPUInstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
197 unsigned Reg, bool UnfoldLoad,
199 SmallVectorImpl<MachineInstr*> &NewMIs) const {
200 // TODO: Implement this function
205 AMDGPUInstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
206 SmallVectorImpl<SDNode*> &NewNodes) const {
207 // TODO: Implement this function
212 AMDGPUInstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
213 bool UnfoldLoad, bool UnfoldStore,
214 unsigned *LoadRegIndex) const {
215 // TODO: Implement this function
219 bool AMDGPUInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
220 int64_t Offset1, int64_t Offset2,
221 unsigned NumLoads) const {
222 assert(Offset2 > Offset1
223 && "Second offset should be larger than first offset!");
224 // If we have less than 16 loads in a row, and the offsets are within 16,
225 // then schedule together.
226 // TODO: Make the loads schedule near if it fits in a cacheline
227 return (NumLoads < 16 && (Offset2 - Offset1) < 16);
231 AMDGPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
233 // TODO: Implement this function
236 void AMDGPUInstrInfo::insertNoop(MachineBasicBlock &MBB,
237 MachineBasicBlock::iterator MI) const {
238 // TODO: Implement this function
241 bool AMDGPUInstrInfo::isPredicated(const MachineInstr *MI) const {
242 // TODO: Implement this function
246 AMDGPUInstrInfo::SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
247 const SmallVectorImpl<MachineOperand> &Pred2)
249 // TODO: Implement this function
253 bool AMDGPUInstrInfo::DefinesPredicate(MachineInstr *MI,
254 std::vector<MachineOperand> &Pred) const {
255 // TODO: Implement this function
259 bool AMDGPUInstrInfo::isPredicable(MachineInstr *MI) const {
260 // TODO: Implement this function
261 return MI->getDesc().isPredicable();
265 AMDGPUInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
266 // TODO: Implement this function
270 bool AMDGPUInstrInfo::isRegisterStore(const MachineInstr &MI) const {
271 return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_STORE;
274 bool AMDGPUInstrInfo::isRegisterLoad(const MachineInstr &MI) const {
275 return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_LOAD;
278 int AMDGPUInstrInfo::getIndirectIndexBegin(const MachineFunction &MF) const {
279 const MachineRegisterInfo &MRI = MF.getRegInfo();
280 const MachineFrameInfo *MFI = MF.getFrameInfo();
283 if (MFI->getNumObjects() == 0) {
287 if (MRI.livein_empty()) {
291 const TargetRegisterClass *IndirectRC = getIndirectAddrRegClass();
292 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
293 LE = MRI.livein_end();
295 unsigned Reg = LI->first;
296 if (TargetRegisterInfo::isVirtualRegister(Reg) ||
297 !IndirectRC->contains(Reg))
302 for (RegIndex = 0, RegEnd = IndirectRC->getNumRegs(); RegIndex != RegEnd;
304 if (IndirectRC->getRegister(RegIndex) == Reg)
307 Offset = std::max(Offset, (int)RegIndex);
313 int AMDGPUInstrInfo::getIndirectIndexEnd(const MachineFunction &MF) const {
315 const MachineFrameInfo *MFI = MF.getFrameInfo();
317 // Variable sized objects are not supported
318 assert(!MFI->hasVarSizedObjects());
320 if (MFI->getNumObjects() == 0) {
324 Offset = TM.getFrameLowering()->getFrameIndexOffset(MF, -1);
326 return getIndirectIndexBegin(MF) + Offset;
330 void AMDGPUInstrInfo::convertToISA(MachineInstr & MI, MachineFunction &MF,
332 MachineRegisterInfo &MRI = MF.getRegInfo();
333 const AMDGPURegisterInfo & RI = getRegisterInfo();
335 for (unsigned i = 0; i < MI.getNumOperands(); i++) {
336 MachineOperand &MO = MI.getOperand(i);
337 // Convert dst regclass to one that is supported by the ISA
338 if (MO.isReg() && MO.isDef()) {
339 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
340 const TargetRegisterClass * oldRegClass = MRI.getRegClass(MO.getReg());
341 const TargetRegisterClass * newRegClass = RI.getISARegClass(oldRegClass);
345 MRI.setRegClass(MO.getReg(), newRegClass);
351 int AMDGPUInstrInfo::getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const {
353 default: return Opcode;
354 case 1: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_1);
355 case 2: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_2);
356 case 3: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_3);