xref: /llvm-project/llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp (revision 91be65be656072a68b51a8c4e7bb751ea475d896)
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::isVector(unsigned TypeIdx) {
60   return [=](const LegalityQuery &Query) {
61     return Query.Types[TypeIdx].isVector();
62   };
63 }
64 
65 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx) {
66   return [=](const LegalityQuery &Query) {
67     return Query.Types[TypeIdx].isPointer();
68   };
69 }
70 
71 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx,
72                                                 unsigned AddrSpace) {
73   return [=](const LegalityQuery &Query) {
74     LLT Ty = Query.Types[TypeIdx];
75     return Ty.isPointer() && Ty.getAddressSpace() == AddrSpace;
76   };
77 }
78 
79 LegalityPredicate LegalityPredicates::narrowerThan(unsigned TypeIdx,
80                                                    unsigned Size) {
81   return [=](const LegalityQuery &Query) {
82     const LLT QueryTy = Query.Types[TypeIdx];
83     return QueryTy.isScalar() && QueryTy.getSizeInBits() < Size;
84   };
85 }
86 
87 LegalityPredicate LegalityPredicates::widerThan(unsigned TypeIdx,
88                                                 unsigned Size) {
89   return [=](const LegalityQuery &Query) {
90     const LLT QueryTy = Query.Types[TypeIdx];
91     return QueryTy.isScalar() && QueryTy.getSizeInBits() > Size;
92   };
93 }
94 
95 LegalityPredicate LegalityPredicates::scalarOrEltNarrowerThan(unsigned TypeIdx,
96                                                               unsigned Size) {
97   return [=](const LegalityQuery &Query) {
98     const LLT QueryTy = Query.Types[TypeIdx];
99     return QueryTy.getScalarSizeInBits() < Size;
100   };
101 }
102 
103 LegalityPredicate LegalityPredicates::scalarOrEltWiderThan(unsigned TypeIdx,
104                                                            unsigned Size) {
105   return [=](const LegalityQuery &Query) {
106     const LLT QueryTy = Query.Types[TypeIdx];
107     return QueryTy.getScalarSizeInBits() > Size;
108   };
109 }
110 
111 LegalityPredicate LegalityPredicates::scalarOrEltSizeNotPow2(unsigned TypeIdx) {
112   return [=](const LegalityQuery &Query) {
113     const LLT QueryTy = Query.Types[TypeIdx];
114     return !isPowerOf2_32(QueryTy.getScalarSizeInBits());
115   };
116 }
117 
118 LegalityPredicate LegalityPredicates::sizeNotPow2(unsigned TypeIdx) {
119   return [=](const LegalityQuery &Query) {
120     const LLT QueryTy = Query.Types[TypeIdx];
121     return QueryTy.isScalar() && !isPowerOf2_32(QueryTy.getSizeInBits());
122   };
123 }
124 
125 LegalityPredicate LegalityPredicates::sameSize(unsigned TypeIdx0,
126                                                unsigned TypeIdx1) {
127   return [=](const LegalityQuery &Query) {
128     return Query.Types[TypeIdx0].getSizeInBits() ==
129            Query.Types[TypeIdx1].getSizeInBits();
130   };
131 }
132 
133 LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) {
134   return [=](const LegalityQuery &Query) {
135     return !isPowerOf2_32(Query.MMODescrs[MMOIdx].SizeInBits / 8);
136   };
137 }
138 
139 LegalityPredicate LegalityPredicates::numElementsNotPow2(unsigned TypeIdx) {
140   return [=](const LegalityQuery &Query) {
141     const LLT QueryTy = Query.Types[TypeIdx];
142     return QueryTy.isVector() && !isPowerOf2_32(QueryTy.getNumElements());
143   };
144 }
145 
146 LegalityPredicate LegalityPredicates::atomicOrderingAtLeastOrStrongerThan(
147     unsigned MMOIdx, AtomicOrdering Ordering) {
148   return [=](const LegalityQuery &Query) {
149     return isAtLeastOrStrongerThan(Query.MMODescrs[MMOIdx].Ordering, Ordering);
150   };
151 }
152