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
24 #define GET_INSTRINFO_NAMED_OPS
25 #define GET_INSTRMAP_INFO
26 #include "AMDGPUGenInstrInfo.inc"
30 AMDGPUInstrInfo::AMDGPUInstrInfo(TargetMachine &tm)
31 : AMDGPUGenInstrInfo(-1,-1), RI(tm), TM(tm) { }
33 const AMDGPURegisterInfo &AMDGPUInstrInfo::getRegisterInfo() const {
37 bool AMDGPUInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
38 unsigned &SrcReg, unsigned &DstReg,
39 unsigned &SubIdx) const {
40 // TODO: Implement this function
44 unsigned AMDGPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
45 int &FrameIndex) const {
46 // TODO: Implement this function
50 unsigned AMDGPUInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
51 int &FrameIndex) const {
52 // TODO: Implement this function
56 bool AMDGPUInstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
57 const MachineMemOperand *&MMO,
58 int &FrameIndex) const {
59 // TODO: Implement this function
62 unsigned AMDGPUInstrInfo::isStoreFromStackSlot(const MachineInstr *MI,
63 int &FrameIndex) const {
64 // TODO: Implement this function
67 unsigned AMDGPUInstrInfo::isStoreFromStackSlotPostFE(const MachineInstr *MI,
68 int &FrameIndex) const {
69 // TODO: Implement this function
72 bool AMDGPUInstrInfo::hasStoreFromStackSlot(const MachineInstr *MI,
73 const MachineMemOperand *&MMO,
74 int &FrameIndex) const {
75 // TODO: Implement this function
80 AMDGPUInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
81 MachineBasicBlock::iterator &MBBI,
82 LiveVariables *LV) const {
83 // TODO: Implement this function
86 bool AMDGPUInstrInfo::getNextBranchInstr(MachineBasicBlock::iterator &iter,
87 MachineBasicBlock &MBB) const {
88 while (iter != MBB.end()) {
89 switch (iter->getOpcode()) {
92 case AMDGPU::BRANCH_COND_i32:
93 case AMDGPU::BRANCH_COND_f32:
103 AMDGPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
104 MachineBasicBlock::iterator MI,
105 unsigned SrcReg, bool isKill,
107 const TargetRegisterClass *RC,
108 const TargetRegisterInfo *TRI) const {
109 assert(!"Not Implemented");
113 AMDGPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
114 MachineBasicBlock::iterator MI,
115 unsigned DestReg, int FrameIndex,
116 const TargetRegisterClass *RC,
117 const TargetRegisterInfo *TRI) const {
118 assert(!"Not Implemented");
121 bool AMDGPUInstrInfo::expandPostRAPseudo (MachineBasicBlock::iterator MI) const {
122 MachineBasicBlock *MBB = MI->getParent();
124 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::addr);
125 // addr is a custom operand with multiple MI operands, and only the
126 // first MI operand is given a name.
127 int RegOpIdx = OffsetOpIdx + 1;
129 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::chan);
131 if (isRegisterLoad(*MI)) {
133 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
134 unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
135 unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
136 unsigned Address = calculateIndirectAddress(RegIndex, Channel);
137 unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
138 if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
139 buildMovInstr(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
140 getIndirectAddrRegClass()->getRegister(Address));
142 buildIndirectRead(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
145 } else if (isRegisterStore(*MI)) {
147 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::val);
148 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
149 unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
150 unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
151 unsigned Address = calculateIndirectAddress(RegIndex, Channel);
152 unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
153 if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
154 buildMovInstr(MBB, MI, getIndirectAddrRegClass()->getRegister(Address),
155 MI->getOperand(ValOpIdx).getReg());
157 buildIndirectWrite(MBB, MI, MI->getOperand(ValOpIdx).getReg(),
158 calculateIndirectAddress(RegIndex, Channel),
171 AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
173 const SmallVectorImpl<unsigned> &Ops,
174 int FrameIndex) const {
175 // TODO: Implement this function
179 AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
181 const SmallVectorImpl<unsigned> &Ops,
182 MachineInstr *LoadMI) const {
183 // TODO: Implement this function
187 AMDGPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
188 const SmallVectorImpl<unsigned> &Ops) const {
189 // TODO: Implement this function
193 AMDGPUInstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
194 unsigned Reg, bool UnfoldLoad,
196 SmallVectorImpl<MachineInstr*> &NewMIs) const {
197 // TODO: Implement this function
202 AMDGPUInstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
203 SmallVectorImpl<SDNode*> &NewNodes) const {
204 // TODO: Implement this function
209 AMDGPUInstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
210 bool UnfoldLoad, bool UnfoldStore,
211 unsigned *LoadRegIndex) const {
212 // TODO: Implement this function
216 bool AMDGPUInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
217 int64_t Offset1, int64_t Offset2,
218 unsigned NumLoads) const {
219 assert(Offset2 > Offset1
220 && "Second offset should be larger than first offset!");
221 // If we have less than 16 loads in a row, and the offsets are within 16,
222 // then schedule together.
223 // TODO: Make the loads schedule near if it fits in a cacheline
224 return (NumLoads < 16 && (Offset2 - Offset1) < 16);
228 AMDGPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
230 // TODO: Implement this function
233 void AMDGPUInstrInfo::insertNoop(MachineBasicBlock &MBB,
234 MachineBasicBlock::iterator MI) const {
235 // TODO: Implement this function
238 bool AMDGPUInstrInfo::isPredicated(const MachineInstr *MI) const {
239 // TODO: Implement this function
243 AMDGPUInstrInfo::SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
244 const SmallVectorImpl<MachineOperand> &Pred2)
246 // TODO: Implement this function
250 bool AMDGPUInstrInfo::DefinesPredicate(MachineInstr *MI,
251 std::vector<MachineOperand> &Pred) const {
252 // TODO: Implement this function
256 bool AMDGPUInstrInfo::isPredicable(MachineInstr *MI) const {
257 // TODO: Implement this function
258 return MI->getDesc().isPredicable();
262 AMDGPUInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
263 // TODO: Implement this function
267 bool AMDGPUInstrInfo::isRegisterStore(const MachineInstr &MI) const {
268 return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_STORE;
271 bool AMDGPUInstrInfo::isRegisterLoad(const MachineInstr &MI) const {
272 return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_LOAD;
275 int AMDGPUInstrInfo::getIndirectIndexBegin(const MachineFunction &MF) const {
276 const MachineRegisterInfo &MRI = MF.getRegInfo();
277 const MachineFrameInfo *MFI = MF.getFrameInfo();
280 if (MFI->getNumObjects() == 0) {
284 if (MRI.livein_empty()) {
288 const TargetRegisterClass *IndirectRC = getIndirectAddrRegClass();
289 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
290 LE = MRI.livein_end();
292 unsigned Reg = LI->first;
293 if (TargetRegisterInfo::isVirtualRegister(Reg) ||
294 !IndirectRC->contains(Reg))
299 for (RegIndex = 0, RegEnd = IndirectRC->getNumRegs(); RegIndex != RegEnd;
301 if (IndirectRC->getRegister(RegIndex) == Reg)
304 Offset = std::max(Offset, (int)RegIndex);
310 int AMDGPUInstrInfo::getIndirectIndexEnd(const MachineFunction &MF) const {
312 const MachineFrameInfo *MFI = MF.getFrameInfo();
314 // Variable sized objects are not supported
315 assert(!MFI->hasVarSizedObjects());
317 if (MFI->getNumObjects() == 0) {
321 Offset = TM.getFrameLowering()->getFrameIndexOffset(MF, -1);
323 return getIndirectIndexBegin(MF) + Offset;
327 void AMDGPUInstrInfo::convertToISA(MachineInstr & MI, MachineFunction &MF,
329 MachineRegisterInfo &MRI = MF.getRegInfo();
330 const AMDGPURegisterInfo & RI = getRegisterInfo();
332 for (unsigned i = 0; i < MI.getNumOperands(); i++) {
333 MachineOperand &MO = MI.getOperand(i);
334 // Convert dst regclass to one that is supported by the ISA
335 if (MO.isReg() && MO.isDef()) {
336 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
337 const TargetRegisterClass * oldRegClass = MRI.getRegClass(MO.getReg());
338 const TargetRegisterClass * newRegClass = RI.getISARegClass(oldRegClass);
342 MRI.setRegClass(MO.getReg(), newRegClass);
348 int AMDGPUInstrInfo::getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const {
350 default: return Opcode;
351 case 1: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_1);
352 case 2: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_2);
353 case 3: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_3);