LegalizeMutations.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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::changeElementSizeTo(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. const LLT NewEltTy = LLT::scalar(NewTy.getScalarSizeInBits());
  45. return std::make_pair(TypeIdx, OldTy.changeElementType(NewEltTy));
  46. };
  47. }
  48. LegalizeMutation LegalizeMutations::widenScalarOrEltToNextPow2(unsigned TypeIdx,
  49. unsigned Min) {
  50. return [=](const LegalityQuery &Query) {
  51. const LLT Ty = Query.Types[TypeIdx];
  52. unsigned NewEltSizeInBits =
  53. std::max(1u << Log2_32_Ceil(Ty.getScalarSizeInBits()), Min);
  54. return std::make_pair(TypeIdx, Ty.changeElementSize(NewEltSizeInBits));
  55. };
  56. }
  57. LegalizeMutation
  58. LegalizeMutations::widenScalarOrEltToNextMultipleOf(unsigned TypeIdx,
  59. unsigned Size) {
  60. return [=](const LegalityQuery &Query) {
  61. const LLT Ty = Query.Types[TypeIdx];
  62. unsigned NewEltSizeInBits = alignTo(Ty.getScalarSizeInBits(), Size);
  63. return std::make_pair(TypeIdx, Ty.changeElementSize(NewEltSizeInBits));
  64. };
  65. }
  66. LegalizeMutation LegalizeMutations::moreElementsToNextPow2(unsigned TypeIdx,
  67. unsigned Min) {
  68. return [=](const LegalityQuery &Query) {
  69. const LLT VecTy = Query.Types[TypeIdx];
  70. unsigned NewNumElements =
  71. std::max(1u << Log2_32_Ceil(VecTy.getNumElements()), Min);
  72. return std::make_pair(
  73. TypeIdx, LLT::fixed_vector(NewNumElements, VecTy.getElementType()));
  74. };
  75. }
  76. LegalizeMutation LegalizeMutations::scalarize(unsigned TypeIdx) {
  77. return [=](const LegalityQuery &Query) {
  78. return std::make_pair(TypeIdx, Query.Types[TypeIdx].getElementType());
  79. };
  80. }