xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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::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 
57 LegalityPredicate LegalityPredicates::isScalar(unsigned TypeIdx) {
58   return [=](const LegalityQuery &Query) {
59     return Query.Types[TypeIdx].isScalar();
60   };
61 }
62 
63 LegalityPredicate LegalityPredicates::isVector(unsigned TypeIdx) {
64   return [=](const LegalityQuery &Query) {
65     return Query.Types[TypeIdx].isVector();
66   };
67 }
68 
69 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx) {
70   return [=](const LegalityQuery &Query) {
71     return Query.Types[TypeIdx].isPointer();
72   };
73 }
74 
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 
83 LegalityPredicate LegalityPredicates::elementTypeIs(unsigned TypeIdx,
84                                                     LLT EltTy) {
85   return [=](const LegalityQuery &Query) {
86     const LLT QueryTy = Query.Types[TypeIdx];
87     return QueryTy.isVector() && QueryTy.getElementType() == EltTy;
88   };
89 }
90 
91 LegalityPredicate LegalityPredicates::scalarNarrowerThan(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 
99 LegalityPredicate LegalityPredicates::scalarWiderThan(unsigned TypeIdx,
100                                                       unsigned Size) {
101   return [=](const LegalityQuery &Query) {
102     const LLT QueryTy = Query.Types[TypeIdx];
103     return QueryTy.isScalar() && QueryTy.getSizeInBits() > Size;
104   };
105 }
106 
107 LegalityPredicate LegalityPredicates::smallerThan(unsigned TypeIdx0,
108                                                   unsigned TypeIdx1) {
109   return [=](const LegalityQuery &Query) {
110     return Query.Types[TypeIdx0].getSizeInBits() <
111            Query.Types[TypeIdx1].getSizeInBits();
112   };
113 }
114 
115 LegalityPredicate LegalityPredicates::largerThan(unsigned TypeIdx0,
116                                                   unsigned TypeIdx1) {
117   return [=](const LegalityQuery &Query) {
118     return Query.Types[TypeIdx0].getSizeInBits() >
119            Query.Types[TypeIdx1].getSizeInBits();
120   };
121 }
122 
123 LegalityPredicate LegalityPredicates::scalarOrEltNarrowerThan(unsigned TypeIdx,
124                                                               unsigned Size) {
125   return [=](const LegalityQuery &Query) {
126     const LLT QueryTy = Query.Types[TypeIdx];
127     return QueryTy.getScalarSizeInBits() < Size;
128   };
129 }
130 
131 LegalityPredicate LegalityPredicates::scalarOrEltWiderThan(unsigned TypeIdx,
132                                                            unsigned Size) {
133   return [=](const LegalityQuery &Query) {
134     const LLT QueryTy = Query.Types[TypeIdx];
135     return QueryTy.getScalarSizeInBits() > Size;
136   };
137 }
138 
139 LegalityPredicate LegalityPredicates::scalarOrEltSizeNotPow2(unsigned TypeIdx) {
140   return [=](const LegalityQuery &Query) {
141     const LLT QueryTy = Query.Types[TypeIdx];
142     return !isPowerOf2_32(QueryTy.getScalarSizeInBits());
143   };
144 }
145 
146 LegalityPredicate LegalityPredicates::sizeNotPow2(unsigned TypeIdx) {
147   return [=](const LegalityQuery &Query) {
148     const LLT QueryTy = Query.Types[TypeIdx];
149     return QueryTy.isScalar() && !isPowerOf2_32(QueryTy.getSizeInBits());
150   };
151 }
152 
153 LegalityPredicate LegalityPredicates::sizeIs(unsigned TypeIdx, unsigned Size) {
154   return [=](const LegalityQuery &Query) {
155     return Query.Types[TypeIdx].getSizeInBits() == Size;
156   };
157 }
158 
159 LegalityPredicate LegalityPredicates::sameSize(unsigned TypeIdx0,
160                                                unsigned TypeIdx1) {
161   return [=](const LegalityQuery &Query) {
162     return Query.Types[TypeIdx0].getSizeInBits() ==
163            Query.Types[TypeIdx1].getSizeInBits();
164   };
165 }
166 
167 LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) {
168   return [=](const LegalityQuery &Query) {
169     return !isPowerOf2_32(Query.MMODescrs[MMOIdx].SizeInBits / 8);
170   };
171 }
172 
173 LegalityPredicate LegalityPredicates::numElementsNotPow2(unsigned TypeIdx) {
174   return [=](const LegalityQuery &Query) {
175     const LLT QueryTy = Query.Types[TypeIdx];
176     return QueryTy.isVector() && !isPowerOf2_32(QueryTy.getNumElements());
177   };
178 }
179 
180 LegalityPredicate LegalityPredicates::atomicOrderingAtLeastOrStrongerThan(
181     unsigned MMOIdx, AtomicOrdering Ordering) {
182   return [=](const LegalityQuery &Query) {
183     return isAtLeastOrStrongerThan(Query.MMODescrs[MMOIdx].Ordering, Ordering);
184   };
185 }
186