LegalityPredicates.cpp 7.4 KB

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