1 //===- lib/CodeGen/GlobalISel/LegalizerPredicates.cpp - Predicates --------===//
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
7 //===----------------------------------------------------------------------===//
9 // A library of predicate factories to use for LegalityPredicate.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
17 LegalityPredicate
LegalityPredicates::typeIs(unsigned TypeIdx
, LLT Type
) {
19 [=](const LegalityQuery
&Query
) { return Query
.Types
[TypeIdx
] == Type
; };
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();
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();
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
};
50 TypesAndMemDesc
.begin(), TypesAndMemDesc
.end(),
51 [=](const TypePairAndMemDesc
&Entry
) ->bool {
52 return Match
.isCompatible(Entry
);
53 }) != TypesAndMemDesc
.end();
57 LegalityPredicate
LegalityPredicates::isScalar(unsigned TypeIdx
) {
58 return [=](const LegalityQuery
&Query
) {
59 return Query
.Types
[TypeIdx
].isScalar();
63 LegalityPredicate
LegalityPredicates::isVector(unsigned TypeIdx
) {
64 return [=](const LegalityQuery
&Query
) {
65 return Query
.Types
[TypeIdx
].isVector();
69 LegalityPredicate
LegalityPredicates::isPointer(unsigned TypeIdx
) {
70 return [=](const LegalityQuery
&Query
) {
71 return Query
.Types
[TypeIdx
].isPointer();
75 LegalityPredicate
LegalityPredicates::isPointer(unsigned TypeIdx
,
77 return [=](const LegalityQuery
&Query
) {
78 LLT Ty
= Query
.Types
[TypeIdx
];
79 return Ty
.isPointer() && Ty
.getAddressSpace() == AddrSpace
;
83 LegalityPredicate
LegalityPredicates::narrowerThan(unsigned TypeIdx
,
85 return [=](const LegalityQuery
&Query
) {
86 const LLT QueryTy
= Query
.Types
[TypeIdx
];
87 return QueryTy
.isScalar() && QueryTy
.getSizeInBits() < Size
;
91 LegalityPredicate
LegalityPredicates::widerThan(unsigned TypeIdx
,
93 return [=](const LegalityQuery
&Query
) {
94 const LLT QueryTy
= Query
.Types
[TypeIdx
];
95 return QueryTy
.isScalar() && QueryTy
.getSizeInBits() > Size
;
99 LegalityPredicate
LegalityPredicates::scalarOrEltNarrowerThan(unsigned TypeIdx
,
101 return [=](const LegalityQuery
&Query
) {
102 const LLT QueryTy
= Query
.Types
[TypeIdx
];
103 return QueryTy
.getScalarSizeInBits() < Size
;
107 LegalityPredicate
LegalityPredicates::scalarOrEltWiderThan(unsigned TypeIdx
,
109 return [=](const LegalityQuery
&Query
) {
110 const LLT QueryTy
= Query
.Types
[TypeIdx
];
111 return QueryTy
.getScalarSizeInBits() > Size
;
115 LegalityPredicate
LegalityPredicates::scalarOrEltSizeNotPow2(unsigned TypeIdx
) {
116 return [=](const LegalityQuery
&Query
) {
117 const LLT QueryTy
= Query
.Types
[TypeIdx
];
118 return !isPowerOf2_32(QueryTy
.getScalarSizeInBits());
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());
129 LegalityPredicate
LegalityPredicates::sameSize(unsigned TypeIdx0
,
131 return [=](const LegalityQuery
&Query
) {
132 return Query
.Types
[TypeIdx0
].getSizeInBits() ==
133 Query
.Types
[TypeIdx1
].getSizeInBits();
137 LegalityPredicate
LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx
) {
138 return [=](const LegalityQuery
&Query
) {
139 return !isPowerOf2_32(Query
.MMODescrs
[MMOIdx
].SizeInBits
/ 8);
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());
150 LegalityPredicate
LegalityPredicates::atomicOrderingAtLeastOrStrongerThan(
151 unsigned MMOIdx
, AtomicOrdering Ordering
) {
152 return [=](const LegalityQuery
&Query
) {
153 return isAtLeastOrStrongerThan(Query
.MMODescrs
[MMOIdx
].Ordering
, Ordering
);