xref: /llvm-project/llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp (revision cbaada6bc18188853b311d42c0356b8a6ca24c7a)
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::sizeNotPow2(unsigned TypeIdx) {
96   return [=](const LegalityQuery &Query) {
97     const LLT &QueryTy = Query.Types[TypeIdx];
98     return QueryTy.isScalar() && !isPowerOf2_32(QueryTy.getSizeInBits());
99   };
100 }
101 
102 LegalityPredicate LegalityPredicates::sameSize(unsigned TypeIdx0,
103                                                unsigned TypeIdx1) {
104   return [=](const LegalityQuery &Query) {
105     return Query.Types[TypeIdx0].getSizeInBits() ==
106            Query.Types[TypeIdx1].getSizeInBits();
107   };
108 }
109 
110 LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) {
111   return [=](const LegalityQuery &Query) {
112     return !isPowerOf2_32(Query.MMODescrs[MMOIdx].SizeInBits / 8);
113   };
114 }
115 
116 LegalityPredicate LegalityPredicates::numElementsNotPow2(unsigned TypeIdx) {
117   return [=](const LegalityQuery &Query) {
118     const LLT &QueryTy = Query.Types[TypeIdx];
119     return QueryTy.isVector() && isPowerOf2_32(QueryTy.getNumElements());
120   };
121 }
122 
123 LegalityPredicate LegalityPredicates::atomicOrderingAtLeastOrStrongerThan(
124     unsigned MMOIdx, AtomicOrdering Ordering) {
125   return [=](const LegalityQuery &Query) {
126     return isAtLeastOrStrongerThan(Query.MMODescrs[MMOIdx].Ordering, Ordering);
127   };
128 }
129