Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 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" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 22 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 25 | #define GET_INSTRINFO_CTOR_DTOR |
Tom Stellard | 02661d9 | 2013-06-25 21:22:18 +0000 | [diff] [blame] | 26 | #define GET_INSTRINFO_NAMED_OPS |
Christian Konig | f741fbf | 2013-02-26 17:52:42 +0000 | [diff] [blame] | 27 | #define GET_INSTRMAP_INFO |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 28 | #include "AMDGPUGenInstrInfo.inc" |
| 29 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 30 | // Pin the vtable to this file. |
| 31 | void AMDGPUInstrInfo::anchor() {} |
| 32 | |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 33 | AMDGPUInstrInfo::AMDGPUInstrInfo(const AMDGPUSubtarget &st) |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 34 | : AMDGPUGenInstrInfo(-1, -1), ST(st) {} |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 35 | |
| 36 | const AMDGPURegisterInfo &AMDGPUInstrInfo::getRegisterInfo() const { |
| 37 | return RI; |
| 38 | } |
| 39 | |
Matt Arsenault | 034d666 | 2014-07-24 02:10:17 +0000 | [diff] [blame] | 40 | bool AMDGPUInstrInfo::enableClusterLoads() const { |
| 41 | return true; |
| 42 | } |
| 43 | |
Matt Arsenault | d5f4de2 | 2014-08-06 00:29:49 +0000 | [diff] [blame] | 44 | // FIXME: This behaves strangely. If, for example, you have 32 load + stores, |
| 45 | // the first 16 loads will be interleaved with the stores, and the next 16 will |
| 46 | // be clustered as expected. It should really split into 2 16 store batches. |
| 47 | // |
| 48 | // Loads are clustered until this returns false, rather than trying to schedule |
| 49 | // groups of stores. This also means we have to deal with saying different |
| 50 | // address space loads should be clustered, and ones which might cause bank |
| 51 | // conflicts. |
| 52 | // |
| 53 | // This might be deprecated so it might not be worth that much effort to fix. |
| 54 | bool AMDGPUInstrInfo::shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1, |
| 55 | int64_t Offset0, int64_t Offset1, |
| 56 | unsigned NumLoads) const { |
| 57 | assert(Offset1 > Offset0 && |
| 58 | "Second offset should be larger than first offset!"); |
| 59 | // If we have less than 16 loads in a row, and the offsets are within 64 |
| 60 | // bytes, then schedule together. |
| 61 | |
| 62 | // A cacheline is 64 bytes (for global memory). |
| 63 | return (NumLoads <= 16 && (Offset1 - Offset0) < 64); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 66 | int AMDGPUInstrInfo::getIndirectIndexBegin(const MachineFunction &MF) const { |
| 67 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 68 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 69 | int Offset = -1; |
| 70 | |
| 71 | if (MFI->getNumObjects() == 0) { |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | if (MRI.livein_empty()) { |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | const TargetRegisterClass *IndirectRC = getIndirectAddrRegClass(); |
| 80 | for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(), |
| 81 | LE = MRI.livein_end(); |
| 82 | LI != LE; ++LI) { |
| 83 | unsigned Reg = LI->first; |
| 84 | if (TargetRegisterInfo::isVirtualRegister(Reg) || |
| 85 | !IndirectRC->contains(Reg)) |
| 86 | continue; |
| 87 | |
| 88 | unsigned RegIndex; |
| 89 | unsigned RegEnd; |
| 90 | for (RegIndex = 0, RegEnd = IndirectRC->getNumRegs(); RegIndex != RegEnd; |
| 91 | ++RegIndex) { |
| 92 | if (IndirectRC->getRegister(RegIndex) == Reg) |
| 93 | break; |
| 94 | } |
| 95 | Offset = std::max(Offset, (int)RegIndex); |
| 96 | } |
| 97 | |
| 98 | return Offset + 1; |
| 99 | } |
| 100 | |
| 101 | int AMDGPUInstrInfo::getIndirectIndexEnd(const MachineFunction &MF) const { |
| 102 | int Offset = 0; |
| 103 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 104 | |
| 105 | // Variable sized objects are not supported |
Diana Picus | e440f99 | 2016-06-23 09:19:16 +0000 | [diff] [blame] | 106 | if (MFI->hasVarSizedObjects()) { |
| 107 | return -1; |
| 108 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 109 | |
| 110 | if (MFI->getNumObjects() == 0) { |
| 111 | return -1; |
| 112 | } |
| 113 | |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 114 | unsigned IgnoredFrameReg; |
| 115 | Offset = MF.getSubtarget().getFrameLowering()->getFrameIndexReference( |
| 116 | MF, -1, IgnoredFrameReg); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 117 | |
| 118 | return getIndirectIndexBegin(MF) + Offset; |
| 119 | } |
| 120 | |
Tom Stellard | 682bfbc | 2013-10-10 17:11:24 +0000 | [diff] [blame] | 121 | int AMDGPUInstrInfo::getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const { |
| 122 | switch (Channels) { |
| 123 | default: return Opcode; |
| 124 | case 1: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_1); |
| 125 | case 2: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_2); |
| 126 | case 3: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_3); |
| 127 | } |
| 128 | } |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 129 | |
| 130 | // Wrapper for Tablegen'd function. enum Subtarget is not defined in any |
Matt Arsenault | 1f0227a | 2014-10-07 21:29:56 +0000 | [diff] [blame] | 131 | // header files, so we need to wrap it in a function that takes unsigned |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 132 | // instead. |
| 133 | namespace llvm { |
| 134 | namespace AMDGPU { |
Marek Olsak | a93603d | 2015-01-15 18:42:51 +0000 | [diff] [blame] | 135 | static int getMCOpcode(uint16_t Opcode, unsigned Gen) { |
Marek Olsak | 5df00d6 | 2014-12-07 12:18:57 +0000 | [diff] [blame] | 136 | return getMCOpcodeGen(Opcode, (enum Subtarget)Gen); |
Tom Stellard | c721a23 | 2014-05-16 20:56:47 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
Marek Olsak | a93603d | 2015-01-15 18:42:51 +0000 | [diff] [blame] | 140 | |
| 141 | // This must be kept in sync with the SISubtarget class in SIInstrInfo.td |
| 142 | enum SISubtarget { |
| 143 | SI = 0, |
| 144 | VI = 1 |
| 145 | }; |
| 146 | |
Benjamin Kramer | 970eac4 | 2015-02-06 17:51:54 +0000 | [diff] [blame] | 147 | static enum SISubtarget AMDGPUSubtargetToSISubtarget(unsigned Gen) { |
Marek Olsak | a93603d | 2015-01-15 18:42:51 +0000 | [diff] [blame] | 148 | switch (Gen) { |
| 149 | default: |
| 150 | return SI; |
| 151 | case AMDGPUSubtarget::VOLCANIC_ISLANDS: |
| 152 | return VI; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | int AMDGPUInstrInfo::pseudoToMCOpcode(int Opcode) const { |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 157 | int MCOp = AMDGPU::getMCOpcode( |
| 158 | Opcode, AMDGPUSubtargetToSISubtarget(ST.getGeneration())); |
Marek Olsak | a93603d | 2015-01-15 18:42:51 +0000 | [diff] [blame] | 159 | |
| 160 | // -1 means that Opcode is already a native instruction. |
| 161 | if (MCOp == -1) |
| 162 | return Opcode; |
| 163 | |
| 164 | // (uint16_t)-1 means that Opcode is a pseudo instruction that has |
| 165 | // no encoding in the given subtarget generation. |
| 166 | if (MCOp == (uint16_t)-1) |
| 167 | return -1; |
| 168 | |
| 169 | return MCOp; |
| 170 | } |