Remove line with no effect
[oota-llvm.git] / lib / Target / R600 / AMDGPUInstrInfo.cpp
1 //===-- AMDGPUInstrInfo.cpp - Base class for AMD GPU InstrInfo ------------===//
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 Implementation of the TargetInstrInfo class that is common to all
12 /// AMD GPUs.
13 //
14 //===----------------------------------------------------------------------===//
15
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"
22
23 using namespace llvm;
24
25 #define GET_INSTRINFO_CTOR_DTOR
26 #define GET_INSTRINFO_NAMED_OPS
27 #define GET_INSTRMAP_INFO
28 #include "AMDGPUGenInstrInfo.inc"
29
30 // Pin the vtable to this file.
31 void AMDGPUInstrInfo::anchor() {}
32
33 AMDGPUInstrInfo::AMDGPUInstrInfo(const AMDGPUSubtarget &st)
34   : AMDGPUGenInstrInfo(-1,-1), RI(st), ST(st) { }
35
36 const AMDGPURegisterInfo &AMDGPUInstrInfo::getRegisterInfo() const {
37   return RI;
38 }
39
40 bool AMDGPUInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
41                                            unsigned &SrcReg, unsigned &DstReg,
42                                            unsigned &SubIdx) const {
43 // TODO: Implement this function
44   return false;
45 }
46
47 unsigned AMDGPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
48                                              int &FrameIndex) const {
49 // TODO: Implement this function
50   return 0;
51 }
52
53 unsigned AMDGPUInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
54                                                    int &FrameIndex) const {
55 // TODO: Implement this function
56   return 0;
57 }
58
59 bool AMDGPUInstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
60                                           const MachineMemOperand *&MMO,
61                                           int &FrameIndex) const {
62 // TODO: Implement this function
63   return false;
64 }
65 unsigned AMDGPUInstrInfo::isStoreFromStackSlot(const MachineInstr *MI,
66                                               int &FrameIndex) const {
67 // TODO: Implement this function
68   return 0;
69 }
70 unsigned AMDGPUInstrInfo::isStoreFromStackSlotPostFE(const MachineInstr *MI,
71                                                     int &FrameIndex) const {
72 // TODO: Implement this function
73   return 0;
74 }
75 bool AMDGPUInstrInfo::hasStoreFromStackSlot(const MachineInstr *MI,
76                                            const MachineMemOperand *&MMO,
77                                            int &FrameIndex) const {
78 // TODO: Implement this function
79   return false;
80 }
81
82 MachineInstr *
83 AMDGPUInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
84                                       MachineBasicBlock::iterator &MBBI,
85                                       LiveVariables *LV) const {
86 // TODO: Implement this function
87   return nullptr;
88 }
89 bool AMDGPUInstrInfo::getNextBranchInstr(MachineBasicBlock::iterator &iter,
90                                         MachineBasicBlock &MBB) const {
91   while (iter != MBB.end()) {
92     switch (iter->getOpcode()) {
93     default:
94       break;
95     case AMDGPU::BRANCH_COND_i32:
96     case AMDGPU::BRANCH_COND_f32:
97     case AMDGPU::BRANCH:
98       return true;
99     };
100     ++iter;
101   }
102   return false;
103 }
104
105 void
106 AMDGPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
107                                     MachineBasicBlock::iterator MI,
108                                     unsigned SrcReg, bool isKill,
109                                     int FrameIndex,
110                                     const TargetRegisterClass *RC,
111                                     const TargetRegisterInfo *TRI) const {
112   llvm_unreachable("Not Implemented");
113 }
114
115 void
116 AMDGPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
117                                      MachineBasicBlock::iterator MI,
118                                      unsigned DestReg, int FrameIndex,
119                                      const TargetRegisterClass *RC,
120                                      const TargetRegisterInfo *TRI) const {
121   llvm_unreachable("Not Implemented");
122 }
123
124 bool AMDGPUInstrInfo::expandPostRAPseudo (MachineBasicBlock::iterator MI) const {
125   MachineBasicBlock *MBB = MI->getParent();
126   int OffsetOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
127                                                AMDGPU::OpName::addr);
128    // addr is a custom operand with multiple MI operands, and only the
129    // first MI operand is given a name.
130   int RegOpIdx = OffsetOpIdx + 1;
131   int ChanOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
132                                              AMDGPU::OpName::chan);
133   if (isRegisterLoad(*MI)) {
134     int DstOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
135                                               AMDGPU::OpName::dst);
136     unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
137     unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
138     unsigned Address = calculateIndirectAddress(RegIndex, Channel);
139     unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
140     if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
141       buildMovInstr(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
142                     getIndirectAddrRegClass()->getRegister(Address));
143     } else {
144       buildIndirectRead(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
145                         Address, OffsetReg);
146     }
147   } else if (isRegisterStore(*MI)) {
148     int ValOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
149                                               AMDGPU::OpName::val);
150     unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
151     unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
152     unsigned Address = calculateIndirectAddress(RegIndex, Channel);
153     unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
154     if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
155       buildMovInstr(MBB, MI, getIndirectAddrRegClass()->getRegister(Address),
156                     MI->getOperand(ValOpIdx).getReg());
157     } else {
158       buildIndirectWrite(MBB, MI, MI->getOperand(ValOpIdx).getReg(),
159                          calculateIndirectAddress(RegIndex, Channel),
160                          OffsetReg);
161     }
162   } else {
163     return false;
164   }
165
166   MBB->erase(MI);
167   return true;
168 }
169
170
171 MachineInstr *
172 AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
173                                       MachineInstr *MI,
174                                       const SmallVectorImpl<unsigned> &Ops,
175                                       int FrameIndex) const {
176 // TODO: Implement this function
177   return nullptr;
178 }
179 MachineInstr*
180 AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
181                                       MachineInstr *MI,
182                                       const SmallVectorImpl<unsigned> &Ops,
183                                       MachineInstr *LoadMI) const {
184   // TODO: Implement this function
185   return nullptr;
186 }
187 bool
188 AMDGPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
189                                      const SmallVectorImpl<unsigned> &Ops) const {
190   // TODO: Implement this function
191   return false;
192 }
193 bool
194 AMDGPUInstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
195                                  unsigned Reg, bool UnfoldLoad,
196                                  bool UnfoldStore,
197                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
198   // TODO: Implement this function
199   return false;
200 }
201
202 bool
203 AMDGPUInstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
204                                     SmallVectorImpl<SDNode*> &NewNodes) const {
205   // TODO: Implement this function
206   return false;
207 }
208
209 unsigned
210 AMDGPUInstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
211                                            bool UnfoldLoad, bool UnfoldStore,
212                                            unsigned *LoadRegIndex) const {
213   // TODO: Implement this function
214   return 0;
215 }
216
217 bool AMDGPUInstrInfo::enableClusterLoads() const {
218   return true;
219 }
220
221 bool AMDGPUInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
222                                              int64_t Offset1, int64_t Offset2,
223                                              unsigned NumLoads) const {
224   assert(Offset2 > Offset1
225          && "Second offset should be larger than first offset!");
226   // If we have less than 16 loads in a row, and the offsets are within 16,
227   // then schedule together.
228   // TODO: Make the loads schedule near if it fits in a cacheline
229   return (NumLoads < 16 && (Offset2 - Offset1) < 16);
230 }
231
232 bool
233 AMDGPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
234   const {
235   // TODO: Implement this function
236   return true;
237 }
238 void AMDGPUInstrInfo::insertNoop(MachineBasicBlock &MBB,
239                                 MachineBasicBlock::iterator MI) const {
240   // TODO: Implement this function
241 }
242
243 bool AMDGPUInstrInfo::isPredicated(const MachineInstr *MI) const {
244   // TODO: Implement this function
245   return false;
246 }
247 bool
248 AMDGPUInstrInfo::SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
249                                   const SmallVectorImpl<MachineOperand> &Pred2)
250   const {
251   // TODO: Implement this function
252   return false;
253 }
254
255 bool AMDGPUInstrInfo::DefinesPredicate(MachineInstr *MI,
256                                       std::vector<MachineOperand> &Pred) const {
257   // TODO: Implement this function
258   return false;
259 }
260
261 bool AMDGPUInstrInfo::isPredicable(MachineInstr *MI) const {
262   // TODO: Implement this function
263   return MI->getDesc().isPredicable();
264 }
265
266 bool
267 AMDGPUInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
268   // TODO: Implement this function
269   return true;
270 }
271
272 bool AMDGPUInstrInfo::isRegisterStore(const MachineInstr &MI) const {
273   return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_STORE;
274 }
275
276 bool AMDGPUInstrInfo::isRegisterLoad(const MachineInstr &MI) const {
277   return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_LOAD;
278 }
279
280 int AMDGPUInstrInfo::getIndirectIndexBegin(const MachineFunction &MF) const {
281   const MachineRegisterInfo &MRI = MF.getRegInfo();
282   const MachineFrameInfo *MFI = MF.getFrameInfo();
283   int Offset = -1;
284
285   if (MFI->getNumObjects() == 0) {
286     return -1;
287   }
288
289   if (MRI.livein_empty()) {
290     return 0;
291   }
292
293   const TargetRegisterClass *IndirectRC = getIndirectAddrRegClass();
294   for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
295                                             LE = MRI.livein_end();
296                                             LI != LE; ++LI) {
297     unsigned Reg = LI->first;
298     if (TargetRegisterInfo::isVirtualRegister(Reg) ||
299         !IndirectRC->contains(Reg))
300       continue;
301
302     unsigned RegIndex;
303     unsigned RegEnd;
304     for (RegIndex = 0, RegEnd = IndirectRC->getNumRegs(); RegIndex != RegEnd;
305                                                           ++RegIndex) {
306       if (IndirectRC->getRegister(RegIndex) == Reg)
307         break;
308     }
309     Offset = std::max(Offset, (int)RegIndex);
310   }
311
312   return Offset + 1;
313 }
314
315 int AMDGPUInstrInfo::getIndirectIndexEnd(const MachineFunction &MF) const {
316   int Offset = 0;
317   const MachineFrameInfo *MFI = MF.getFrameInfo();
318
319   // Variable sized objects are not supported
320   assert(!MFI->hasVarSizedObjects());
321
322   if (MFI->getNumObjects() == 0) {
323     return -1;
324   }
325
326   Offset = MF.getTarget().getFrameLowering()->getFrameIndexOffset(MF, -1);
327
328   return getIndirectIndexBegin(MF) + Offset;
329 }
330
331 int AMDGPUInstrInfo::getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const {
332   switch (Channels) {
333   default: return Opcode;
334   case 1: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_1);
335   case 2: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_2);
336   case 3: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_3);
337   }
338 }
339
340 // Wrapper for Tablegen'd function.  enum Subtarget is not defined in any
341 // header files, so we need to wrap it in a function that takes unsigned 
342 // instead.
343 namespace llvm {
344 namespace AMDGPU {
345 int getMCOpcode(uint16_t Opcode, unsigned Gen) {
346   return getMCOpcode(Opcode);
347 }
348 }
349 }