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::narrowerThan(unsigned TypeIdx, 60 unsigned Size) { 61 return [=](const LegalityQuery &Query) { 62 const LLT &QueryTy = Query.Types[TypeIdx]; 63 return QueryTy.isScalar() && QueryTy.getSizeInBits() < Size; 64 }; 65 } 66 67 LegalityPredicate LegalityPredicates::widerThan(unsigned TypeIdx, 68 unsigned Size) { 69 return [=](const LegalityQuery &Query) { 70 const LLT &QueryTy = Query.Types[TypeIdx]; 71 return QueryTy.isScalar() && QueryTy.getSizeInBits() > Size; 72 }; 73 } 74 75 LegalityPredicate LegalityPredicates::sizeNotPow2(unsigned TypeIdx) { 76 return [=](const LegalityQuery &Query) { 77 const LLT &QueryTy = Query.Types[TypeIdx]; 78 return QueryTy.isScalar() && !isPowerOf2_32(QueryTy.getSizeInBits()); 79 }; 80 } 81 82 LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) { 83 return [=](const LegalityQuery &Query) { 84 return !isPowerOf2_32(Query.MMODescrs[MMOIdx].SizeInBits / 8); 85 }; 86 } 87 88 LegalityPredicate LegalityPredicates::numElementsNotPow2(unsigned TypeIdx) { 89 return [=](const LegalityQuery &Query) { 90 const LLT &QueryTy = Query.Types[TypeIdx]; 91 return QueryTy.isVector() && isPowerOf2_32(QueryTy.getNumElements()); 92 }; 93 } 94 95 LegalityPredicate LegalityPredicates::atomicOrderingAtLeastOrStrongerThan( 96 unsigned MMOIdx, AtomicOrdering Ordering) { 97 return [=](const LegalityQuery &Query) { 98 return isAtLeastOrStrongerThan(Query.MMODescrs[MMOIdx].Ordering, Ordering); 99 }; 100 } 101