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
typeIs(unsigned TypeIdx,LLT Type)17 LegalityPredicate LegalityPredicates::typeIs(unsigned TypeIdx, LLT Type) {
18 return
19 [=](const LegalityQuery &Query) { return Query.Types[TypeIdx] == Type; };
20 }
21
22 LegalityPredicate
typeInSet(unsigned TypeIdx,std::initializer_list<LLT> TypesInit)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
typePairInSet(unsigned TypeIdx0,unsigned TypeIdx1,std::initializer_list<std::pair<LLT,LLT>> TypesInit)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
typePairAndMemDescInSet(unsigned TypeIdx0,unsigned TypeIdx1,unsigned MMOIdx,std::initializer_list<TypePairAndMemDesc> TypesAndMemDescInit)41 LegalityPredicate LegalityPredicates::typePairAndMemDescInSet(
42 unsigned TypeIdx0, unsigned TypeIdx1, unsigned MMOIdx,
43 std::initializer_list<TypePairAndMemDesc> TypesAndMemDescInit) {
44 SmallVector<TypePairAndMemDesc, 4> TypesAndMemDesc = TypesAndMemDescInit;
45 return [=](const LegalityQuery &Query) {
46 TypePairAndMemDesc Match = {Query.Types[TypeIdx0], Query.Types[TypeIdx1],
47 Query.MMODescrs[MMOIdx].SizeInBits,
48 Query.MMODescrs[MMOIdx].AlignInBits};
49 return std::find_if(
50 TypesAndMemDesc.begin(), TypesAndMemDesc.end(),
51 [=](const TypePairAndMemDesc &Entry) ->bool {
52 return Match.isCompatible(Entry);
53 }) != TypesAndMemDesc.end();
54 };
55 }
56
isScalar(unsigned TypeIdx)57 LegalityPredicate LegalityPredicates::isScalar(unsigned TypeIdx) {
58 return [=](const LegalityQuery &Query) {
59 return Query.Types[TypeIdx].isScalar();
60 };
61 }
62
isVector(unsigned TypeIdx)63 LegalityPredicate LegalityPredicates::isVector(unsigned TypeIdx) {
64 return [=](const LegalityQuery &Query) {
65 return Query.Types[TypeIdx].isVector();
66 };
67 }
68
isPointer(unsigned TypeIdx)69 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx) {
70 return [=](const LegalityQuery &Query) {
71 return Query.Types[TypeIdx].isPointer();
72 };
73 }
74
isPointer(unsigned TypeIdx,unsigned AddrSpace)75 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx,
76 unsigned AddrSpace) {
77 return [=](const LegalityQuery &Query) {
78 LLT Ty = Query.Types[TypeIdx];
79 return Ty.isPointer() && Ty.getAddressSpace() == AddrSpace;
80 };
81 }
82
narrowerThan(unsigned TypeIdx,unsigned Size)83 LegalityPredicate LegalityPredicates::narrowerThan(unsigned TypeIdx,
84 unsigned Size) {
85 return [=](const LegalityQuery &Query) {
86 const LLT QueryTy = Query.Types[TypeIdx];
87 return QueryTy.isScalar() && QueryTy.getSizeInBits() < Size;
88 };
89 }
90
widerThan(unsigned TypeIdx,unsigned Size)91 LegalityPredicate LegalityPredicates::widerThan(unsigned TypeIdx,
92 unsigned Size) {
93 return [=](const LegalityQuery &Query) {
94 const LLT QueryTy = Query.Types[TypeIdx];
95 return QueryTy.isScalar() && QueryTy.getSizeInBits() > Size;
96 };
97 }
98
scalarOrEltNarrowerThan(unsigned TypeIdx,unsigned Size)99 LegalityPredicate LegalityPredicates::scalarOrEltNarrowerThan(unsigned TypeIdx,
100 unsigned Size) {
101 return [=](const LegalityQuery &Query) {
102 const LLT QueryTy = Query.Types[TypeIdx];
103 return QueryTy.getScalarSizeInBits() < Size;
104 };
105 }
106
scalarOrEltWiderThan(unsigned TypeIdx,unsigned Size)107 LegalityPredicate LegalityPredicates::scalarOrEltWiderThan(unsigned TypeIdx,
108 unsigned Size) {
109 return [=](const LegalityQuery &Query) {
110 const LLT QueryTy = Query.Types[TypeIdx];
111 return QueryTy.getScalarSizeInBits() > Size;
112 };
113 }
114
scalarOrEltSizeNotPow2(unsigned TypeIdx)115 LegalityPredicate LegalityPredicates::scalarOrEltSizeNotPow2(unsigned TypeIdx) {
116 return [=](const LegalityQuery &Query) {
117 const LLT QueryTy = Query.Types[TypeIdx];
118 return !isPowerOf2_32(QueryTy.getScalarSizeInBits());
119 };
120 }
121
sizeNotPow2(unsigned TypeIdx)122 LegalityPredicate LegalityPredicates::sizeNotPow2(unsigned TypeIdx) {
123 return [=](const LegalityQuery &Query) {
124 const LLT QueryTy = Query.Types[TypeIdx];
125 return QueryTy.isScalar() && !isPowerOf2_32(QueryTy.getSizeInBits());
126 };
127 }
128
sameSize(unsigned TypeIdx0,unsigned TypeIdx1)129 LegalityPredicate LegalityPredicates::sameSize(unsigned TypeIdx0,
130 unsigned TypeIdx1) {
131 return [=](const LegalityQuery &Query) {
132 return Query.Types[TypeIdx0].getSizeInBits() ==
133 Query.Types[TypeIdx1].getSizeInBits();
134 };
135 }
136
memSizeInBytesNotPow2(unsigned MMOIdx)137 LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) {
138 return [=](const LegalityQuery &Query) {
139 return !isPowerOf2_32(Query.MMODescrs[MMOIdx].SizeInBits / 8);
140 };
141 }
142
numElementsNotPow2(unsigned TypeIdx)143 LegalityPredicate LegalityPredicates::numElementsNotPow2(unsigned TypeIdx) {
144 return [=](const LegalityQuery &Query) {
145 const LLT QueryTy = Query.Types[TypeIdx];
146 return QueryTy.isVector() && !isPowerOf2_32(QueryTy.getNumElements());
147 };
148 }
149
atomicOrderingAtLeastOrStrongerThan(unsigned MMOIdx,AtomicOrdering Ordering)150 LegalityPredicate LegalityPredicates::atomicOrderingAtLeastOrStrongerThan(
151 unsigned MMOIdx, AtomicOrdering Ordering) {
152 return [=](const LegalityQuery &Query) {
153 return isAtLeastOrStrongerThan(Query.MMODescrs[MMOIdx].Ordering, Ordering);
154 };
155 }
156