Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 1 | //===- AMDGPULegalizerInfo.cpp -----------------------------------*- 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 | /// \file |
| 10 | /// This file implements the targeting of the Machinelegalizer class for |
| 11 | /// AMDGPU. |
| 12 | /// \todo This should be generated by TableGen. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
David Blaikie | 36a0f22 | 2018-03-23 23:58:31 +0000 | [diff] [blame] | 15 | #include "AMDGPU.h" |
Craig Topper | 2fa1436 | 2018-03-29 17:21:10 +0000 | [diff] [blame] | 16 | #include "AMDGPULegalizerInfo.h" |
Matt Arsenault | 8580336 | 2018-03-17 15:17:41 +0000 | [diff] [blame] | 17 | #include "AMDGPUTargetMachine.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/TargetOpcodes.h" |
Craig Topper | 2fa1436 | 2018-03-29 17:21:10 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/ValueTypes.h" |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Type.h" |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
| 23 | |
| 24 | using namespace llvm; |
Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 25 | using namespace LegalizeActions; |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 26 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 27 | AMDGPULegalizerInfo::AMDGPULegalizerInfo(const GCNSubtarget &ST, |
Matt Arsenault | c3fe46b | 2018-03-08 16:24:16 +0000 | [diff] [blame] | 28 | const GCNTargetMachine &TM) { |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 29 | using namespace TargetOpcode; |
| 30 | |
Matt Arsenault | 8580336 | 2018-03-17 15:17:41 +0000 | [diff] [blame] | 31 | auto GetAddrSpacePtr = [&TM](unsigned AS) { |
| 32 | return LLT::pointer(AS, TM.getPointerSizeInBits(AS)); |
| 33 | }; |
| 34 | |
| 35 | const LLT S1 = LLT::scalar(1); |
Tom Stellard | ff63ee0 | 2017-06-19 13:15:45 +0000 | [diff] [blame] | 36 | const LLT V2S16 = LLT::vector(2, 16); |
Matt Arsenault | 8580336 | 2018-03-17 15:17:41 +0000 | [diff] [blame] | 37 | |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 38 | const LLT S32 = LLT::scalar(32); |
| 39 | const LLT S64 = LLT::scalar(64); |
Tom Stellard | eebbfc2 | 2018-06-30 04:09:44 +0000 | [diff] [blame] | 40 | const LLT S512 = LLT::scalar(512); |
Matt Arsenault | 8580336 | 2018-03-17 15:17:41 +0000 | [diff] [blame] | 41 | |
| 42 | const LLT GlobalPtr = GetAddrSpacePtr(AMDGPUAS::GLOBAL_ADDRESS); |
| 43 | const LLT ConstantPtr = GetAddrSpacePtr(AMDGPUAS::CONSTANT_ADDRESS); |
Matt Arsenault | 685d1e8 | 2018-03-17 15:17:45 +0000 | [diff] [blame] | 44 | const LLT LocalPtr = GetAddrSpacePtr(AMDGPUAS::LOCAL_ADDRESS); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 45 | const LLT FlatPtr = GetAddrSpacePtr(AMDGPUAS::FLAT_ADDRESS); |
| 46 | const LLT PrivatePtr = GetAddrSpacePtr(AMDGPUAS::PRIVATE_ADDRESS); |
Matt Arsenault | 8580336 | 2018-03-17 15:17:41 +0000 | [diff] [blame] | 47 | |
Matt Arsenault | 934e534 | 2018-12-13 20:34:15 +0000 | [diff] [blame] | 48 | const LLT CodePtr = FlatPtr; |
| 49 | |
Matt Arsenault | 685d1e8 | 2018-03-17 15:17:45 +0000 | [diff] [blame] | 50 | const LLT AddrSpaces[] = { |
| 51 | GlobalPtr, |
| 52 | ConstantPtr, |
| 53 | LocalPtr, |
| 54 | FlatPtr, |
| 55 | PrivatePtr |
| 56 | }; |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 57 | |
Tom Stellard | ee6e645 | 2017-06-12 20:54:56 +0000 | [diff] [blame] | 58 | setAction({G_ADD, S32}, Legal); |
Tom Stellard | 26fac0f | 2018-06-22 02:54:57 +0000 | [diff] [blame] | 59 | setAction({G_ASHR, S32}, Legal); |
Matt Arsenault | fed0a45 | 2018-03-19 14:07:23 +0000 | [diff] [blame] | 60 | setAction({G_SUB, S32}, Legal); |
Matt Arsenault | dc14ec0 | 2018-03-01 19:22:05 +0000 | [diff] [blame] | 61 | setAction({G_MUL, S32}, Legal); |
Tom Stellard | af552dc | 2017-06-23 15:17:17 +0000 | [diff] [blame] | 62 | setAction({G_AND, S32}, Legal); |
Matt Arsenault | 3f6a204 | 2018-03-01 19:09:21 +0000 | [diff] [blame] | 63 | setAction({G_OR, S32}, Legal); |
| 64 | setAction({G_XOR, S32}, Legal); |
Tom Stellard | ee6e645 | 2017-06-12 20:54:56 +0000 | [diff] [blame] | 65 | |
Tom Stellard | ff63ee0 | 2017-06-19 13:15:45 +0000 | [diff] [blame] | 66 | setAction({G_BITCAST, V2S16}, Legal); |
| 67 | setAction({G_BITCAST, 1, S32}, Legal); |
| 68 | |
| 69 | setAction({G_BITCAST, S32}, Legal); |
| 70 | setAction({G_BITCAST, 1, V2S16}, Legal); |
| 71 | |
Matt Arsenault | abdc4f2 | 2018-03-17 15:17:48 +0000 | [diff] [blame] | 72 | getActionDefinitionsBuilder(G_FCONSTANT) |
| 73 | .legalFor({S32, S64}); |
Tom Stellard | eebbfc2 | 2018-06-30 04:09:44 +0000 | [diff] [blame] | 74 | |
| 75 | // G_IMPLICIT_DEF is a no-op so we can make it legal for any value type that |
| 76 | // can fit in a register. |
| 77 | // FIXME: We need to legalize several more operations before we can add |
| 78 | // a test case for size > 512. |
Matt Arsenault | b3feccd | 2018-06-25 15:42:12 +0000 | [diff] [blame] | 79 | getActionDefinitionsBuilder(G_IMPLICIT_DEF) |
Tom Stellard | eebbfc2 | 2018-06-30 04:09:44 +0000 | [diff] [blame] | 80 | .legalIf([=](const LegalityQuery &Query) { |
| 81 | return Query.Types[0].getSizeInBits() <= 512; |
| 82 | }) |
| 83 | .clampScalar(0, S1, S512); |
Matt Arsenault | b3feccd | 2018-06-25 15:42:12 +0000 | [diff] [blame] | 84 | |
| 85 | getActionDefinitionsBuilder(G_CONSTANT) |
Matt Arsenault | abdc4f2 | 2018-03-17 15:17:48 +0000 | [diff] [blame] | 86 | .legalFor({S1, S32, S64}); |
| 87 | |
Tom Stellard | e042412 | 2017-06-03 01:13:33 +0000 | [diff] [blame] | 88 | // FIXME: i1 operands to intrinsics should always be legal, but other i1 |
| 89 | // values may not be legal. We need to figure out how to distinguish |
| 90 | // between these two scenarios. |
| 91 | setAction({G_CONSTANT, S1}, Legal); |
Matt Arsenault | 06cbb27 | 2018-03-01 19:16:52 +0000 | [diff] [blame] | 92 | |
Matt Arsenault | 577b9fc | 2018-12-13 08:27:48 +0000 | [diff] [blame] | 93 | getActionDefinitionsBuilder( |
Matt Arsenault | e01e7c8 | 2018-12-18 09:19:03 +0000 | [diff] [blame^] | 94 | { G_FADD, G_FMUL, G_FNEG, G_FABS}) |
Matt Arsenault | 577b9fc | 2018-12-13 08:27:48 +0000 | [diff] [blame] | 95 | .legalFor({S32, S64}); |
Tom Stellard | d0c6cf2 | 2017-10-27 23:57:41 +0000 | [diff] [blame] | 96 | |
Matt Arsenault | e01e7c8 | 2018-12-18 09:19:03 +0000 | [diff] [blame^] | 97 | // Use actual fsub instruction |
| 98 | setAction({G_FSUB, S32}, Legal); |
| 99 | |
| 100 | // Must use fadd + fneg |
| 101 | setAction({G_FSUB, S64}, Lower); |
| 102 | |
Matt Arsenault | 8e80a5f | 2018-03-01 19:09:16 +0000 | [diff] [blame] | 103 | setAction({G_FCMP, S1}, Legal); |
| 104 | setAction({G_FCMP, 1, S32}, Legal); |
| 105 | setAction({G_FCMP, 1, S64}, Legal); |
| 106 | |
Matt Arsenault | 0529a8e | 2018-03-01 20:56:21 +0000 | [diff] [blame] | 107 | setAction({G_ZEXT, S64}, Legal); |
| 108 | setAction({G_ZEXT, 1, S32}, Legal); |
| 109 | |
Matt Arsenault | f38f483 | 2018-12-13 08:23:51 +0000 | [diff] [blame] | 110 | setAction({G_SEXT, S64}, Legal); |
| 111 | setAction({G_SEXT, 1, S32}, Legal); |
| 112 | |
| 113 | setAction({G_ANYEXT, S64}, Legal); |
| 114 | setAction({G_ANYEXT, 1, S32}, Legal); |
| 115 | |
Matt Arsenault | dd022ce | 2018-03-01 19:04:25 +0000 | [diff] [blame] | 116 | setAction({G_FPTOSI, S32}, Legal); |
| 117 | setAction({G_FPTOSI, 1, S32}, Legal); |
| 118 | |
Tom Stellard | 9a65357 | 2018-06-22 02:34:29 +0000 | [diff] [blame] | 119 | setAction({G_SITOFP, S32}, Legal); |
| 120 | setAction({G_SITOFP, 1, S32}, Legal); |
| 121 | |
Tom Stellard | 3344576 | 2018-02-07 04:47:59 +0000 | [diff] [blame] | 122 | setAction({G_FPTOUI, S32}, Legal); |
| 123 | setAction({G_FPTOUI, 1, S32}, Legal); |
| 124 | |
Matt Arsenault | 685d1e8 | 2018-03-17 15:17:45 +0000 | [diff] [blame] | 125 | for (LLT PtrTy : AddrSpaces) { |
| 126 | LLT IdxTy = LLT::scalar(PtrTy.getSizeInBits()); |
| 127 | setAction({G_GEP, PtrTy}, Legal); |
| 128 | setAction({G_GEP, 1, IdxTy}, Legal); |
| 129 | } |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 130 | |
Matt Arsenault | 934e534 | 2018-12-13 20:34:15 +0000 | [diff] [blame] | 131 | setAction({G_BLOCK_ADDR, CodePtr}, Legal); |
| 132 | |
Tom Stellard | 8cd60a5 | 2017-06-06 14:16:50 +0000 | [diff] [blame] | 133 | setAction({G_ICMP, S1}, Legal); |
| 134 | setAction({G_ICMP, 1, S32}, Legal); |
| 135 | |
Matt Arsenault | f38f483 | 2018-12-13 08:23:51 +0000 | [diff] [blame] | 136 | setAction({G_CTLZ, S32}, Legal); |
| 137 | setAction({G_CTLZ_ZERO_UNDEF, S32}, Legal); |
| 138 | setAction({G_CTTZ, S32}, Legal); |
| 139 | setAction({G_CTTZ_ZERO_UNDEF, S32}, Legal); |
| 140 | setAction({G_BSWAP, S32}, Legal); |
| 141 | setAction({G_CTPOP, S32}, Legal); |
| 142 | |
Tom Stellard | 7c65078 | 2018-10-05 04:34:09 +0000 | [diff] [blame] | 143 | getActionDefinitionsBuilder(G_INTTOPTR) |
| 144 | .legalIf([](const LegalityQuery &Query) { |
| 145 | return true; |
| 146 | }); |
Matt Arsenault | 8580336 | 2018-03-17 15:17:41 +0000 | [diff] [blame] | 147 | |
Matt Arsenault | f38f483 | 2018-12-13 08:23:51 +0000 | [diff] [blame] | 148 | getActionDefinitionsBuilder(G_PTRTOINT) |
| 149 | .legalIf([](const LegalityQuery &Query) { |
| 150 | return true; |
| 151 | }); |
| 152 | |
Matt Arsenault | 8580336 | 2018-03-17 15:17:41 +0000 | [diff] [blame] | 153 | getActionDefinitionsBuilder({G_LOAD, G_STORE}) |
| 154 | .legalIf([=, &ST](const LegalityQuery &Query) { |
| 155 | const LLT &Ty0 = Query.Types[0]; |
| 156 | |
| 157 | // TODO: Decompose private loads into 4-byte components. |
| 158 | // TODO: Illegal flat loads on SI |
| 159 | switch (Ty0.getSizeInBits()) { |
| 160 | case 32: |
| 161 | case 64: |
| 162 | case 128: |
| 163 | return true; |
| 164 | |
| 165 | case 96: |
| 166 | // XXX hasLoadX3 |
| 167 | return (ST.getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS); |
| 168 | |
| 169 | case 256: |
| 170 | case 512: |
| 171 | // TODO: constant loads |
| 172 | default: |
| 173 | return false; |
| 174 | } |
| 175 | }); |
| 176 | |
| 177 | |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 178 | |
Tom Stellard | 2860a42 | 2017-06-07 13:54:51 +0000 | [diff] [blame] | 179 | setAction({G_SELECT, S32}, Legal); |
| 180 | setAction({G_SELECT, 1, S1}, Legal); |
| 181 | |
Tom Stellard | eb8f1e2 | 2017-06-26 15:56:52 +0000 | [diff] [blame] | 182 | setAction({G_SHL, S32}, Legal); |
| 183 | |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 184 | |
| 185 | // FIXME: When RegBankSelect inserts copies, it will only create new |
| 186 | // registers with scalar types. This means we can end up with |
| 187 | // G_LOAD/G_STORE/G_GEP instruction with scalar types for their pointer |
| 188 | // operands. In assert builds, the instruction selector will assert |
| 189 | // if it sees a generic instruction which isn't legal, so we need to |
| 190 | // tell it that scalar types are legal for pointer operands |
| 191 | setAction({G_GEP, S64}, Legal); |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 192 | |
Matt Arsenault | 7b9ed89 | 2018-03-12 13:35:53 +0000 | [diff] [blame] | 193 | for (unsigned Op : {G_EXTRACT_VECTOR_ELT, G_INSERT_VECTOR_ELT}) { |
| 194 | getActionDefinitionsBuilder(Op) |
| 195 | .legalIf([=](const LegalityQuery &Query) { |
| 196 | const LLT &VecTy = Query.Types[1]; |
| 197 | const LLT &IdxTy = Query.Types[2]; |
| 198 | return VecTy.getSizeInBits() % 32 == 0 && |
| 199 | VecTy.getSizeInBits() <= 512 && |
| 200 | IdxTy.getSizeInBits() == 32; |
| 201 | }); |
| 202 | } |
| 203 | |
Matt Arsenault | 71272e6 | 2018-03-05 16:25:15 +0000 | [diff] [blame] | 204 | // FIXME: Doesn't handle extract of illegal sizes. |
Tom Stellard | b7f19e6 | 2018-07-24 02:19:20 +0000 | [diff] [blame] | 205 | getActionDefinitionsBuilder({G_EXTRACT, G_INSERT}) |
Matt Arsenault | 71272e6 | 2018-03-05 16:25:15 +0000 | [diff] [blame] | 206 | .legalIf([=](const LegalityQuery &Query) { |
| 207 | const LLT &Ty0 = Query.Types[0]; |
| 208 | const LLT &Ty1 = Query.Types[1]; |
| 209 | return (Ty0.getSizeInBits() % 32 == 0) && |
| 210 | (Ty1.getSizeInBits() % 32 == 0); |
| 211 | }); |
| 212 | |
Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 213 | getActionDefinitionsBuilder(G_BUILD_VECTOR) |
| 214 | .legalIf([=](const LegalityQuery &Query) { |
| 215 | const LLT &VecTy = Query.Types[0]; |
| 216 | const LLT &ScalarTy = Query.Types[1]; |
| 217 | return VecTy.getSizeInBits() % 32 == 0 && |
| 218 | ScalarTy.getSizeInBits() % 32 == 0 && |
| 219 | VecTy.getSizeInBits() <= 512; |
| 220 | }); |
Matt Arsenault | 503afda | 2018-03-12 13:35:43 +0000 | [diff] [blame] | 221 | // Merge/Unmerge |
| 222 | for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) { |
| 223 | unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1; |
| 224 | unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0; |
| 225 | |
| 226 | getActionDefinitionsBuilder(Op) |
| 227 | .legalIf([=](const LegalityQuery &Query) { |
| 228 | const LLT &BigTy = Query.Types[BigTyIdx]; |
| 229 | const LLT &LitTy = Query.Types[LitTyIdx]; |
| 230 | return BigTy.getSizeInBits() % 32 == 0 && |
| 231 | LitTy.getSizeInBits() % 32 == 0 && |
| 232 | BigTy.getSizeInBits() <= 512; |
| 233 | }) |
| 234 | // Any vectors left are the wrong size. Scalarize them. |
| 235 | .fewerElementsIf([](const LegalityQuery &Query) { return true; }, |
| 236 | [](const LegalityQuery &Query) { |
| 237 | return std::make_pair( |
| 238 | 0, Query.Types[0].getElementType()); |
| 239 | }) |
| 240 | .fewerElementsIf([](const LegalityQuery &Query) { return true; }, |
| 241 | [](const LegalityQuery &Query) { |
| 242 | return std::make_pair( |
| 243 | 1, Query.Types[1].getElementType()); |
| 244 | }); |
| 245 | |
| 246 | } |
| 247 | |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 248 | computeTables(); |
Roman Tereshin | 76c29c6 | 2018-05-31 16:16:48 +0000 | [diff] [blame] | 249 | verify(*ST.getInstrInfo()); |
Tom Stellard | ca16621 | 2017-01-30 21:56:46 +0000 | [diff] [blame] | 250 | } |