LegalizeMutations.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===- lib/CodeGen/GlobalISel/LegalizerMutations.cpp - Mutations ----------===//
  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 mutation factories to use for LegalityMutation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
  13. using namespace llvm;
  14. LegalizeMutation LegalizeMutations::changeTo(unsigned TypeIdx, LLT Ty) {
  15. return
  16. [=](const LegalityQuery &Query) { return std::make_pair(TypeIdx, Ty); };
  17. }
  18. LegalizeMutation LegalizeMutations::changeTo(unsigned TypeIdx,
  19. unsigned FromTypeIdx) {
  20. return [=](const LegalityQuery &Query) {
  21. return std::make_pair(TypeIdx, Query.Types[FromTypeIdx]);
  22. };
  23. }
  24. LegalizeMutation LegalizeMutations::changeElementTo(unsigned TypeIdx,
  25. unsigned FromTypeIdx) {
  26. return [=](const LegalityQuery &Query) {
  27. const LLT OldTy = Query.Types[TypeIdx];
  28. const LLT NewTy = Query.Types[FromTypeIdx];
  29. return std::make_pair(TypeIdx, OldTy.changeElementType(NewTy));
  30. };
  31. }
  32. LegalizeMutation LegalizeMutations::changeElementTo(unsigned TypeIdx,
  33. LLT NewEltTy) {
  34. return [=](const LegalityQuery &Query) {
  35. const LLT OldTy = Query.Types[TypeIdx];
  36. return std::make_pair(TypeIdx, OldTy.changeElementType(NewEltTy));
  37. };
  38. }
  39. LegalizeMutation LegalizeMutations::changeElementCountTo(unsigned TypeIdx,
  40. unsigned FromTypeIdx) {
  41. return [=](const LegalityQuery &Query) {
  42. const LLT OldTy = Query.Types[TypeIdx];
  43. const LLT NewTy = Query.Types[FromTypeIdx];
  44. ElementCount NewEltCount =
  45. NewTy.isVector() ? NewTy.getElementCount() : ElementCount::getFixed(1);
  46. return std::make_pair(TypeIdx, OldTy.changeElementCount(NewEltCount));
  47. };
  48. }
  49. LegalizeMutation LegalizeMutations::changeElementCountTo(unsigned TypeIdx,
  50. LLT NewEltTy) {
  51. return [=](const LegalityQuery &Query) {
  52. const LLT OldTy = Query.Types[TypeIdx];
  53. ElementCount NewEltCount = NewEltTy.isVector() ? NewEltTy.getElementCount()
  54. : ElementCount::getFixed(1);
  55. return std::make_pair(TypeIdx, OldTy.changeElementCount(NewEltCount));
  56. };
  57. }
  58. LegalizeMutation LegalizeMutations::changeElementSizeTo(unsigned TypeIdx,
  59. unsigned FromTypeIdx) {
  60. return [=](const LegalityQuery &Query) {
  61. const LLT OldTy = Query.Types[TypeIdx];
  62. const LLT NewTy = Query.Types[FromTypeIdx];
  63. const LLT NewEltTy = LLT::scalar(NewTy.getScalarSizeInBits());
  64. return std::make_pair(TypeIdx, OldTy.changeElementType(NewEltTy));
  65. };
  66. }
  67. LegalizeMutation LegalizeMutations::widenScalarOrEltToNextPow2(unsigned TypeIdx,
  68. unsigned Min) {
  69. return [=](const LegalityQuery &Query) {
  70. const LLT Ty = Query.Types[TypeIdx];
  71. unsigned NewEltSizeInBits =
  72. std::max(1u << Log2_32_Ceil(Ty.getScalarSizeInBits()), Min);
  73. return std::make_pair(TypeIdx, Ty.changeElementSize(NewEltSizeInBits));
  74. };
  75. }
  76. LegalizeMutation
  77. LegalizeMutations::widenScalarOrEltToNextMultipleOf(unsigned TypeIdx,
  78. unsigned Size) {
  79. return [=](const LegalityQuery &Query) {
  80. const LLT Ty = Query.Types[TypeIdx];
  81. unsigned NewEltSizeInBits = alignTo(Ty.getScalarSizeInBits(), Size);
  82. return std::make_pair(TypeIdx, Ty.changeElementSize(NewEltSizeInBits));
  83. };
  84. }
  85. LegalizeMutation LegalizeMutations::moreElementsToNextPow2(unsigned TypeIdx,
  86. unsigned Min) {
  87. return [=](const LegalityQuery &Query) {
  88. const LLT VecTy = Query.Types[TypeIdx];
  89. unsigned NewNumElements =
  90. std::max(1u << Log2_32_Ceil(VecTy.getNumElements()), Min);
  91. return std::make_pair(
  92. TypeIdx, LLT::fixed_vector(NewNumElements, VecTy.getElementType()));
  93. };
  94. }
  95. LegalizeMutation LegalizeMutations::scalarize(unsigned TypeIdx) {
  96. return [=](const LegalityQuery &Query) {
  97. return std::make_pair(TypeIdx, Query.Types[TypeIdx].getElementType());
  98. };
  99. }