1 //===- AMDGPULegalizerInfo.cpp -----------------------------------*- C++ -*-==// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements the targeting of the Machinelegalizer class for 10 /// AMDGPU. 11 /// \todo This should be generated by TableGen. 12 //===----------------------------------------------------------------------===// 13 14 #include "AMDGPU.h" 15 #include "AMDGPULegalizerInfo.h" 16 #include "AMDGPUTargetMachine.h" 17 #include "llvm/CodeGen/TargetOpcodes.h" 18 #include "llvm/CodeGen/ValueTypes.h" 19 #include "llvm/IR/DerivedTypes.h" 20 #include "llvm/IR/Type.h" 21 #include "llvm/Support/Debug.h" 22 23 using namespace llvm; 24 using namespace LegalizeActions; 25 using namespace LegalityPredicates; 26 27 AMDGPULegalizerInfo::AMDGPULegalizerInfo(const GCNSubtarget &ST, 28 const GCNTargetMachine &TM) { 29 using namespace TargetOpcode; 30 31 auto GetAddrSpacePtr = [&TM](unsigned AS) { 32 return LLT::pointer(AS, TM.getPointerSizeInBits(AS)); 33 }; 34 35 const LLT S1 = LLT::scalar(1); 36 const LLT S16 = LLT::scalar(16); 37 const LLT S32 = LLT::scalar(32); 38 const LLT S64 = LLT::scalar(64); 39 const LLT S256 = LLT::scalar(256); 40 const LLT S512 = LLT::scalar(512); 41 42 const LLT V2S16 = LLT::vector(2, 16); 43 const LLT V4S16 = LLT::vector(4, 16); 44 const LLT V8S16 = LLT::vector(8, 16); 45 46 const LLT V2S32 = LLT::vector(2, 32); 47 const LLT V3S32 = LLT::vector(3, 32); 48 const LLT V4S32 = LLT::vector(4, 32); 49 const LLT V5S32 = LLT::vector(5, 32); 50 const LLT V6S32 = LLT::vector(6, 32); 51 const LLT V7S32 = LLT::vector(7, 32); 52 const LLT V8S32 = LLT::vector(8, 32); 53 const LLT V9S32 = LLT::vector(9, 32); 54 const LLT V10S32 = LLT::vector(10, 32); 55 const LLT V11S32 = LLT::vector(11, 32); 56 const LLT V12S32 = LLT::vector(12, 32); 57 const LLT V13S32 = LLT::vector(13, 32); 58 const LLT V14S32 = LLT::vector(14, 32); 59 const LLT V15S32 = LLT::vector(15, 32); 60 const LLT V16S32 = LLT::vector(16, 32); 61 62 const LLT V2S64 = LLT::vector(2, 64); 63 const LLT V3S64 = LLT::vector(3, 64); 64 const LLT V4S64 = LLT::vector(4, 64); 65 const LLT V5S64 = LLT::vector(5, 64); 66 const LLT V6S64 = LLT::vector(6, 64); 67 const LLT V7S64 = LLT::vector(7, 64); 68 const LLT V8S64 = LLT::vector(8, 64); 69 70 std::initializer_list<LLT> AllS32Vectors = 71 {V2S32, V3S32, V4S32, V5S32, V6S32, V7S32, V8S32, 72 V9S32, V10S32, V11S32, V12S32, V13S32, V14S32, V15S32, V16S32}; 73 std::initializer_list<LLT> AllS64Vectors = 74 {V2S64, V3S64, V4S64, V5S64, V6S64, V7S64, V8S64}; 75 76 const LLT GlobalPtr = GetAddrSpacePtr(AMDGPUAS::GLOBAL_ADDRESS); 77 const LLT ConstantPtr = GetAddrSpacePtr(AMDGPUAS::CONSTANT_ADDRESS); 78 const LLT LocalPtr = GetAddrSpacePtr(AMDGPUAS::LOCAL_ADDRESS); 79 const LLT FlatPtr = GetAddrSpacePtr(AMDGPUAS::FLAT_ADDRESS); 80 const LLT PrivatePtr = GetAddrSpacePtr(AMDGPUAS::PRIVATE_ADDRESS); 81 82 const LLT CodePtr = FlatPtr; 83 84 const LLT AddrSpaces[] = { 85 GlobalPtr, 86 ConstantPtr, 87 LocalPtr, 88 FlatPtr, 89 PrivatePtr 90 }; 91 92 setAction({G_BRCOND, S1}, Legal); 93 94 setAction({G_ADD, S32}, Legal); 95 setAction({G_ASHR, S32}, Legal); 96 setAction({G_SUB, S32}, Legal); 97 setAction({G_MUL, S32}, Legal); 98 99 // FIXME: 64-bit ones only legal for scalar 100 getActionDefinitionsBuilder({G_AND, G_OR, G_XOR}) 101 .legalFor({S32, S1, S64, V2S32}); 102 103 getActionDefinitionsBuilder({G_UADDO, G_SADDO, G_USUBO, G_SSUBO, 104 G_UADDE, G_SADDE, G_USUBE, G_SSUBE}) 105 .legalFor({{S32, S1}}); 106 107 getActionDefinitionsBuilder(G_BITCAST) 108 .legalForCartesianProduct({S32, V2S16}) 109 .legalForCartesianProduct({S64, V2S32, V4S16}) 110 .legalForCartesianProduct({V2S64, V4S32}) 111 // Don't worry about the size constraint. 112 .legalIf(all(isPointer(0), isPointer(1))); 113 114 getActionDefinitionsBuilder(G_FCONSTANT) 115 .legalFor({S32, S64, S16}); 116 117 // G_IMPLICIT_DEF is a no-op so we can make it legal for any value type that 118 // can fit in a register. 119 // FIXME: We need to legalize several more operations before we can add 120 // a test case for size > 512. 121 getActionDefinitionsBuilder(G_IMPLICIT_DEF) 122 .legalIf([=](const LegalityQuery &Query) { 123 return Query.Types[0].getSizeInBits() <= 512; 124 }) 125 .clampScalar(0, S1, S512); 126 127 128 // FIXME: i1 operands to intrinsics should always be legal, but other i1 129 // values may not be legal. We need to figure out how to distinguish 130 // between these two scenarios. 131 // FIXME: Pointer types 132 getActionDefinitionsBuilder(G_CONSTANT) 133 .legalFor({S1, S32, S64, V2S32, V2S16}) 134 .clampScalar(0, S32, S64) 135 .widenScalarToNextPow2(0); 136 137 setAction({G_FRAME_INDEX, PrivatePtr}, Legal); 138 139 getActionDefinitionsBuilder( 140 { G_FADD, G_FMUL, G_FNEG, G_FABS, G_FMA}) 141 .legalFor({S32, S64}) 142 .clampScalar(0, S32, S64); 143 144 getActionDefinitionsBuilder(G_FPTRUNC) 145 .legalFor({{S32, S64}, {S16, S32}}); 146 147 getActionDefinitionsBuilder(G_FPEXT) 148 .legalFor({{S64, S32}, {S32, S16}}) 149 .lowerFor({{S64, S16}}); // FIXME: Implement 150 151 getActionDefinitionsBuilder(G_FSUB) 152 // Use actual fsub instruction 153 .legalFor({S32}) 154 // Must use fadd + fneg 155 .lowerFor({S64, S16}) 156 .clampScalar(0, S32, S64); 157 158 setAction({G_FCMP, S1}, Legal); 159 setAction({G_FCMP, 1, S32}, Legal); 160 setAction({G_FCMP, 1, S64}, Legal); 161 162 getActionDefinitionsBuilder({G_SEXT, G_ZEXT, G_ANYEXT}) 163 .legalFor({{S64, S32}, {S32, S16}, {S64, S16}, 164 {S32, S1}, {S64, S1}, {S16, S1}}); 165 166 setAction({G_FPTOSI, S32}, Legal); 167 setAction({G_FPTOSI, 1, S32}, Legal); 168 169 setAction({G_SITOFP, S32}, Legal); 170 setAction({G_SITOFP, 1, S32}, Legal); 171 172 setAction({G_UITOFP, S32}, Legal); 173 setAction({G_UITOFP, 1, S32}, Legal); 174 175 setAction({G_FPTOUI, S32}, Legal); 176 setAction({G_FPTOUI, 1, S32}, Legal); 177 178 setAction({G_FPOW, S32}, Legal); 179 setAction({G_FEXP2, S32}, Legal); 180 setAction({G_FLOG2, S32}, Legal); 181 182 getActionDefinitionsBuilder({G_INTRINSIC_TRUNC, G_INTRINSIC_ROUND}) 183 .legalFor({S32, S64}); 184 185 for (LLT PtrTy : AddrSpaces) { 186 LLT IdxTy = LLT::scalar(PtrTy.getSizeInBits()); 187 setAction({G_GEP, PtrTy}, Legal); 188 setAction({G_GEP, 1, IdxTy}, Legal); 189 } 190 191 setAction({G_BLOCK_ADDR, CodePtr}, Legal); 192 193 setAction({G_ICMP, S1}, Legal); 194 setAction({G_ICMP, 1, S32}, Legal); 195 196 setAction({G_CTLZ, S32}, Legal); 197 setAction({G_CTLZ_ZERO_UNDEF, S32}, Legal); 198 setAction({G_CTTZ, S32}, Legal); 199 setAction({G_CTTZ_ZERO_UNDEF, S32}, Legal); 200 setAction({G_BSWAP, S32}, Legal); 201 setAction({G_CTPOP, S32}, Legal); 202 203 getActionDefinitionsBuilder(G_INTTOPTR) 204 .legalIf([](const LegalityQuery &Query) { 205 return true; 206 }); 207 208 getActionDefinitionsBuilder(G_PTRTOINT) 209 .legalIf([](const LegalityQuery &Query) { 210 return true; 211 }); 212 213 getActionDefinitionsBuilder({G_LOAD, G_STORE}) 214 .legalIf([=, &ST](const LegalityQuery &Query) { 215 const LLT &Ty0 = Query.Types[0]; 216 217 // TODO: Decompose private loads into 4-byte components. 218 // TODO: Illegal flat loads on SI 219 switch (Ty0.getSizeInBits()) { 220 case 32: 221 case 64: 222 case 128: 223 return true; 224 225 case 96: 226 // XXX hasLoadX3 227 return (ST.getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS); 228 229 case 256: 230 case 512: 231 // TODO: constant loads 232 default: 233 return false; 234 } 235 }); 236 237 238 auto &Atomics = getActionDefinitionsBuilder( 239 {G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB, 240 G_ATOMICRMW_AND, G_ATOMICRMW_OR, G_ATOMICRMW_XOR, 241 G_ATOMICRMW_MAX, G_ATOMICRMW_MIN, G_ATOMICRMW_UMAX, 242 G_ATOMICRMW_UMIN, G_ATOMIC_CMPXCHG}) 243 .legalFor({{S32, GlobalPtr}, {S32, LocalPtr}, 244 {S64, GlobalPtr}, {S64, LocalPtr}}); 245 if (ST.hasFlatAddressSpace()) { 246 Atomics.legalFor({{S32, FlatPtr}, {S64, FlatPtr}}); 247 } 248 249 // TODO: Pointer types, any 32-bit or 64-bit vector 250 getActionDefinitionsBuilder(G_SELECT) 251 .legalFor({{S32, S1}, {S64, S1}, {V2S32, S1}, {V2S16, S1}}) 252 .clampScalar(0, S32, S64); 253 254 setAction({G_SHL, S32}, Legal); 255 256 257 // FIXME: When RegBankSelect inserts copies, it will only create new 258 // registers with scalar types. This means we can end up with 259 // G_LOAD/G_STORE/G_GEP instruction with scalar types for their pointer 260 // operands. In assert builds, the instruction selector will assert 261 // if it sees a generic instruction which isn't legal, so we need to 262 // tell it that scalar types are legal for pointer operands 263 setAction({G_GEP, S64}, Legal); 264 265 for (unsigned Op : {G_EXTRACT_VECTOR_ELT, G_INSERT_VECTOR_ELT}) { 266 getActionDefinitionsBuilder(Op) 267 .legalIf([=](const LegalityQuery &Query) { 268 const LLT &VecTy = Query.Types[1]; 269 const LLT &IdxTy = Query.Types[2]; 270 return VecTy.getSizeInBits() % 32 == 0 && 271 VecTy.getSizeInBits() <= 512 && 272 IdxTy.getSizeInBits() == 32; 273 }); 274 } 275 276 // FIXME: Doesn't handle extract of illegal sizes. 277 getActionDefinitionsBuilder({G_EXTRACT, G_INSERT}) 278 .legalIf([=](const LegalityQuery &Query) { 279 const LLT &Ty0 = Query.Types[0]; 280 const LLT &Ty1 = Query.Types[1]; 281 return (Ty0.getSizeInBits() % 32 == 0) && 282 (Ty1.getSizeInBits() % 32 == 0); 283 }); 284 285 getActionDefinitionsBuilder(G_BUILD_VECTOR) 286 .legalForCartesianProduct(AllS32Vectors, {S32}) 287 .legalForCartesianProduct(AllS64Vectors, {S64}) 288 .clampNumElements(0, V16S32, V16S32) 289 .clampNumElements(0, V2S64, V8S64) 290 .minScalarSameAs(1, 0); 291 292 // TODO: Support any combination of v2s32 293 getActionDefinitionsBuilder(G_CONCAT_VECTORS) 294 .legalFor({{V4S32, V2S32}, 295 {V8S32, V2S32}, 296 {V8S32, V4S32}, 297 {V4S64, V2S64}, 298 {V4S16, V2S16}, 299 {V8S16, V2S16}, 300 {V8S16, V4S16}}); 301 302 // Merge/Unmerge 303 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) { 304 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1; 305 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0; 306 307 auto notValidElt = [=](const LegalityQuery &Query, unsigned TypeIdx) { 308 const LLT &Ty = Query.Types[TypeIdx]; 309 if (Ty.isVector()) { 310 const LLT &EltTy = Ty.getElementType(); 311 if (EltTy.getSizeInBits() < 8 || EltTy.getSizeInBits() > 64) 312 return true; 313 if (!isPowerOf2_32(EltTy.getSizeInBits())) 314 return true; 315 } 316 return false; 317 }; 318 319 auto scalarize = 320 [=](const LegalityQuery &Query, unsigned TypeIdx) { 321 const LLT &Ty = Query.Types[TypeIdx]; 322 return std::make_pair(TypeIdx, Ty.getElementType()); 323 }; 324 325 getActionDefinitionsBuilder(Op) 326 // Break up vectors with weird elements into scalars 327 .fewerElementsIf( 328 [=](const LegalityQuery &Query) { return notValidElt(Query, 0); }, 329 [=](const LegalityQuery &Query) { return scalarize(Query, 0); }) 330 .fewerElementsIf( 331 [=](const LegalityQuery &Query) { return notValidElt(Query, 1); }, 332 [=](const LegalityQuery &Query) { return scalarize(Query, 1); }) 333 .clampScalar(BigTyIdx, S32, S512) 334 .widenScalarIf( 335 [=](const LegalityQuery &Query) { 336 const LLT &Ty = Query.Types[BigTyIdx]; 337 return !isPowerOf2_32(Ty.getSizeInBits()) && 338 Ty.getSizeInBits() % 16 != 0; 339 }, 340 [=](const LegalityQuery &Query) { 341 // Pick the next power of 2, or a multiple of 64 over 128. 342 // Whichever is smaller. 343 const LLT &Ty = Query.Types[BigTyIdx]; 344 unsigned NewSizeInBits = 1 << Log2_32_Ceil(Ty.getSizeInBits() + 1); 345 if (NewSizeInBits >= 256) { 346 unsigned RoundedTo = alignTo<64>(Ty.getSizeInBits() + 1); 347 if (RoundedTo < NewSizeInBits) 348 NewSizeInBits = RoundedTo; 349 } 350 return std::make_pair(BigTyIdx, LLT::scalar(NewSizeInBits)); 351 }) 352 .widenScalarToNextPow2(LitTyIdx, /*Min*/ 16) 353 // Clamp the little scalar to s8-s256 and make it a power of 2. It's not 354 // worth considering the multiples of 64 since 2*192 and 2*384 are not 355 // valid. 356 .clampScalar(LitTyIdx, S16, S256) 357 .widenScalarToNextPow2(LitTyIdx, /*Min*/ 32) 358 .legalIf([=](const LegalityQuery &Query) { 359 const LLT &BigTy = Query.Types[BigTyIdx]; 360 const LLT &LitTy = Query.Types[LitTyIdx]; 361 362 if (BigTy.isVector() && BigTy.getSizeInBits() < 32) 363 return false; 364 if (LitTy.isVector() && LitTy.getSizeInBits() < 32) 365 return false; 366 367 return BigTy.getSizeInBits() % 16 == 0 && 368 LitTy.getSizeInBits() % 16 == 0 && 369 BigTy.getSizeInBits() <= 512; 370 }) 371 // Any vectors left are the wrong size. Scalarize them. 372 .fewerElementsIf([](const LegalityQuery &Query) { 373 return Query.Types[0].isVector(); 374 }, 375 [](const LegalityQuery &Query) { 376 return std::make_pair( 377 0, Query.Types[0].getElementType()); 378 }) 379 .fewerElementsIf([](const LegalityQuery &Query) { 380 return Query.Types[1].isVector(); 381 }, 382 [](const LegalityQuery &Query) { 383 return std::make_pair( 384 1, Query.Types[1].getElementType()); 385 }); 386 387 } 388 389 computeTables(); 390 verify(*ST.getInstrInfo()); 391 } 392