• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Enable optimizations to work around MSVC debug mode bug in 32-bit:
14 // https://developercommunity.visualstudio.com/content/problem/1179643/msvc-copies-overaligned-non-trivially-copyable-par.html
15 // FIXME: Remove this when the issue is closed.
16 #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_IX86)
17 // We have to disable runtime checks in order to enable optimizations. This is
18 // done for the entire file because the problem is actually observed in STL
19 // template functions.
20 #pragma runtime_checks("", off)
21 #pragma optimize("gs", on)
22 #endif
23 
24 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
25 
26 using namespace llvm;
27 
typeIs(unsigned TypeIdx,LLT Type)28 LegalityPredicate LegalityPredicates::typeIs(unsigned TypeIdx, LLT Type) {
29   return
30       [=](const LegalityQuery &Query) { return Query.Types[TypeIdx] == Type; };
31 }
32 
33 LegalityPredicate
typeInSet(unsigned TypeIdx,std::initializer_list<LLT> TypesInit)34 LegalityPredicates::typeInSet(unsigned TypeIdx,
35                               std::initializer_list<LLT> TypesInit) {
36   SmallVector<LLT, 4> Types = TypesInit;
37   return [=](const LegalityQuery &Query) {
38     return std::find(Types.begin(), Types.end(), Query.Types[TypeIdx]) != Types.end();
39   };
40 }
41 
typePairInSet(unsigned TypeIdx0,unsigned TypeIdx1,std::initializer_list<std::pair<LLT,LLT>> TypesInit)42 LegalityPredicate LegalityPredicates::typePairInSet(
43     unsigned TypeIdx0, unsigned TypeIdx1,
44     std::initializer_list<std::pair<LLT, LLT>> TypesInit) {
45   SmallVector<std::pair<LLT, LLT>, 4> Types = TypesInit;
46   return [=](const LegalityQuery &Query) {
47     std::pair<LLT, LLT> Match = {Query.Types[TypeIdx0], Query.Types[TypeIdx1]};
48     return std::find(Types.begin(), Types.end(), Match) != Types.end();
49   };
50 }
51 
typePairAndMemDescInSet(unsigned TypeIdx0,unsigned TypeIdx1,unsigned MMOIdx,std::initializer_list<TypePairAndMemDesc> TypesAndMemDescInit)52 LegalityPredicate LegalityPredicates::typePairAndMemDescInSet(
53     unsigned TypeIdx0, unsigned TypeIdx1, unsigned MMOIdx,
54     std::initializer_list<TypePairAndMemDesc> TypesAndMemDescInit) {
55   SmallVector<TypePairAndMemDesc, 4> TypesAndMemDesc = TypesAndMemDescInit;
56   return [=](const LegalityQuery &Query) {
57     TypePairAndMemDesc Match = {Query.Types[TypeIdx0], Query.Types[TypeIdx1],
58                                 Query.MMODescrs[MMOIdx].SizeInBits,
59                                 Query.MMODescrs[MMOIdx].AlignInBits};
60     return std::find_if(
61       TypesAndMemDesc.begin(), TypesAndMemDesc.end(),
62       [=](const TypePairAndMemDesc &Entry) ->bool {
63         return Match.isCompatible(Entry);
64       }) != TypesAndMemDesc.end();
65   };
66 }
67 
isScalar(unsigned TypeIdx)68 LegalityPredicate LegalityPredicates::isScalar(unsigned TypeIdx) {
69   return [=](const LegalityQuery &Query) {
70     return Query.Types[TypeIdx].isScalar();
71   };
72 }
73 
isVector(unsigned TypeIdx)74 LegalityPredicate LegalityPredicates::isVector(unsigned TypeIdx) {
75   return [=](const LegalityQuery &Query) {
76     return Query.Types[TypeIdx].isVector();
77   };
78 }
79 
isPointer(unsigned TypeIdx)80 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx) {
81   return [=](const LegalityQuery &Query) {
82     return Query.Types[TypeIdx].isPointer();
83   };
84 }
85 
isPointer(unsigned TypeIdx,unsigned AddrSpace)86 LegalityPredicate LegalityPredicates::isPointer(unsigned TypeIdx,
87                                                 unsigned AddrSpace) {
88   return [=](const LegalityQuery &Query) {
89     LLT Ty = Query.Types[TypeIdx];
90     return Ty.isPointer() && Ty.getAddressSpace() == AddrSpace;
91   };
92 }
93 
narrowerThan(unsigned TypeIdx,unsigned Size)94 LegalityPredicate LegalityPredicates::narrowerThan(unsigned TypeIdx,
95                                                    unsigned Size) {
96   return [=](const LegalityQuery &Query) {
97     const LLT QueryTy = Query.Types[TypeIdx];
98     return QueryTy.isScalar() && QueryTy.getSizeInBits() < Size;
99   };
100 }
101 
widerThan(unsigned TypeIdx,unsigned Size)102 LegalityPredicate LegalityPredicates::widerThan(unsigned TypeIdx,
103                                                 unsigned Size) {
104   return [=](const LegalityQuery &Query) {
105     const LLT QueryTy = Query.Types[TypeIdx];
106     return QueryTy.isScalar() && QueryTy.getSizeInBits() > Size;
107   };
108 }
109 
scalarOrEltNarrowerThan(unsigned TypeIdx,unsigned Size)110 LegalityPredicate LegalityPredicates::scalarOrEltNarrowerThan(unsigned TypeIdx,
111                                                               unsigned Size) {
112   return [=](const LegalityQuery &Query) {
113     const LLT QueryTy = Query.Types[TypeIdx];
114     return QueryTy.getScalarSizeInBits() < Size;
115   };
116 }
117 
scalarOrEltWiderThan(unsigned TypeIdx,unsigned Size)118 LegalityPredicate LegalityPredicates::scalarOrEltWiderThan(unsigned TypeIdx,
119                                                            unsigned Size) {
120   return [=](const LegalityQuery &Query) {
121     const LLT QueryTy = Query.Types[TypeIdx];
122     return QueryTy.getScalarSizeInBits() > Size;
123   };
124 }
125 
scalarOrEltSizeNotPow2(unsigned TypeIdx)126 LegalityPredicate LegalityPredicates::scalarOrEltSizeNotPow2(unsigned TypeIdx) {
127   return [=](const LegalityQuery &Query) {
128     const LLT QueryTy = Query.Types[TypeIdx];
129     return !isPowerOf2_32(QueryTy.getScalarSizeInBits());
130   };
131 }
132 
sizeNotPow2(unsigned TypeIdx)133 LegalityPredicate LegalityPredicates::sizeNotPow2(unsigned TypeIdx) {
134   return [=](const LegalityQuery &Query) {
135     const LLT QueryTy = Query.Types[TypeIdx];
136     return QueryTy.isScalar() && !isPowerOf2_32(QueryTy.getSizeInBits());
137   };
138 }
139 
sameSize(unsigned TypeIdx0,unsigned TypeIdx1)140 LegalityPredicate LegalityPredicates::sameSize(unsigned TypeIdx0,
141                                                unsigned TypeIdx1) {
142   return [=](const LegalityQuery &Query) {
143     return Query.Types[TypeIdx0].getSizeInBits() ==
144            Query.Types[TypeIdx1].getSizeInBits();
145   };
146 }
147 
memSizeInBytesNotPow2(unsigned MMOIdx)148 LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) {
149   return [=](const LegalityQuery &Query) {
150     return !isPowerOf2_32(Query.MMODescrs[MMOIdx].SizeInBits / 8);
151   };
152 }
153 
numElementsNotPow2(unsigned TypeIdx)154 LegalityPredicate LegalityPredicates::numElementsNotPow2(unsigned TypeIdx) {
155   return [=](const LegalityQuery &Query) {
156     const LLT QueryTy = Query.Types[TypeIdx];
157     return QueryTy.isVector() && !isPowerOf2_32(QueryTy.getNumElements());
158   };
159 }
160 
atomicOrderingAtLeastOrStrongerThan(unsigned MMOIdx,AtomicOrdering Ordering)161 LegalityPredicate LegalityPredicates::atomicOrderingAtLeastOrStrongerThan(
162     unsigned MMOIdx, AtomicOrdering Ordering) {
163   return [=](const LegalityQuery &Query) {
164     return isAtLeastOrStrongerThan(Query.MMODescrs[MMOIdx].Ordering, Ordering);
165   };
166 }
167