1 //===- lib/CodeGen/GlobalISel/LegalizerPredicates.cpp - Predicates --------===// 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 // 9 // A library of predicate factories to use for LegalityPredicate. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" 14 15 using namespace llvm; 16 17 LegalityPredicate LegalityPredicates::typeIs(unsigned TypeIdx, LLT Type) { 18 return 19 [=](const LegalityQuery &Query) { return Query.Types[TypeIdx] == Type; }; 20 } 21 22 LegalityPredicate 23 LegalityPredicates::typeInSet(unsigned TypeIdx, 24 std::initializer_list<LLT> TypesInit) { 25 SmallVector<LLT, 4> Types = TypesInit; 26 return [=](const LegalityQuery &Query) { 27 return std::find(Types.begin(), Types.end(), Query.Types[TypeIdx]) != Types.end(); 28 }; 29 } 30 31 LegalityPredicate LegalityPredicates::typePairInSet( 32 unsigned TypeIdx0, unsigned TypeIdx1, 33 std::initializer_list<std::pair<LLT, LLT>> TypesInit) { 34 SmallVector<std::pair<LLT, LLT>, 4> Types = TypesInit; 35 return [=](const LegalityQuery &Query) { 36 std::pair<LLT, LLT> Match = {Query.Types[TypeIdx0], Query.Types[TypeIdx1]}; 37 return std::find(Types.begin(), Types.end(), Match) != Types.end(); 38 }; 39 } 40 41 LegalityPredicate LegalityPredicates::typePairAndMemSizeInSet( 42 unsigned TypeIdx0, unsigned TypeIdx1, unsigned MMOIdx, 43 std::initializer_list<TypePairAndMemSize> TypesAndMemSizeInit) { 44 SmallVector<TypePairAndMemSize, 4> TypesAndMemSize = TypesAndMemSizeInit; 45 return [=](const LegalityQuery &Query) { 46 TypePairAndMemSize Match = {Query.Types[TypeIdx0], Query.Types[TypeIdx1], 47 Query.MMODescrs[MMOIdx].SizeInBits}; 48 return std::find(TypesAndMemSize.begin(), TypesAndMemSize.end(), Match) != 49 TypesAndMemSize.end(); 50 }; 51 } 52 53 LegalityPredicate LegalityPredicates::isScalar(unsigned TypeIdx) { 54 return [=](const LegalityQuery &Query) { 55 return Query.Types[TypeIdx].isScalar(); 56 }; 57 } 58 59 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx) { 60 return [=](const LegalityQuery &Query) { 61 return Query.Types[TypeIdx].isPointer(); 62 }; 63 } 64 65 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx, 66 unsigned AddrSpace) { 67 return [=](const LegalityQuery &Query) { 68 LLT Ty = Query.Types[TypeIdx]; 69 return Ty.isPointer() && Ty.getAddressSpace() == AddrSpace; 70 }; 71 } 72 73 LegalityPredicate LegalityPredicates::narrowerThan(unsigned TypeIdx, 74 unsigned Size) { 75 return [=](const LegalityQuery &Query) { 76 const LLT &QueryTy = Query.Types[TypeIdx]; 77 return QueryTy.isScalar() && QueryTy.getSizeInBits() < Size; 78 }; 79 } 80 81 LegalityPredicate LegalityPredicates::widerThan(unsigned TypeIdx, 82 unsigned Size) { 83 return [=](const LegalityQuery &Query) { 84 const LLT &QueryTy = Query.Types[TypeIdx]; 85 return QueryTy.isScalar() && QueryTy.getSizeInBits() > Size; 86 }; 87 } 88 89 LegalityPredicate LegalityPredicates::sizeNotPow2(unsigned TypeIdx) { 90 return [=](const LegalityQuery &Query) { 91 const LLT &QueryTy = Query.Types[TypeIdx]; 92 return QueryTy.isScalar() && !isPowerOf2_32(QueryTy.getSizeInBits()); 93 }; 94 } 95 96 LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) { 97 return [=](const LegalityQuery &Query) { 98 return !isPowerOf2_32(Query.MMODescrs[MMOIdx].SizeInBits / 8); 99 }; 100 } 101 102 LegalityPredicate LegalityPredicates::numElementsNotPow2(unsigned TypeIdx) { 103 return [=](const LegalityQuery &Query) { 104 const LLT &QueryTy = Query.Types[TypeIdx]; 105 return QueryTy.isVector() && isPowerOf2_32(QueryTy.getNumElements()); 106 }; 107 } 108 109 LegalityPredicate LegalityPredicates::atomicOrderingAtLeastOrStrongerThan( 110 unsigned MMOIdx, AtomicOrdering Ordering) { 111 return [=](const LegalityQuery &Query) { 112 return isAtLeastOrStrongerThan(Query.MMODescrs[MMOIdx].Ordering, Ordering); 113 }; 114 } 115