1 //===- lib/CodeGen/GlobalISel/LegalizerMutations.cpp - Mutations ----------===//
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 mutation factories to use for LegalityMutation.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
17 LegalizeMutation
LegalizeMutations::changeTo(unsigned TypeIdx
, LLT Ty
) {
19 [=](const LegalityQuery
&Query
) { return std::make_pair(TypeIdx
, Ty
); };
22 LegalizeMutation
LegalizeMutations::changeTo(unsigned TypeIdx
,
23 unsigned FromTypeIdx
) {
24 return [=](const LegalityQuery
&Query
) {
25 return std::make_pair(TypeIdx
, Query
.Types
[FromTypeIdx
]);
29 LegalizeMutation
LegalizeMutations::changeElementTo(unsigned TypeIdx
,
30 unsigned FromTypeIdx
) {
31 return [=](const LegalityQuery
&Query
) {
32 const LLT OldTy
= Query
.Types
[TypeIdx
];
33 const LLT NewTy
= Query
.Types
[FromTypeIdx
];
34 return std::make_pair(TypeIdx
, OldTy
.changeElementType(NewTy
));
38 LegalizeMutation
LegalizeMutations::changeElementTo(unsigned TypeIdx
,
40 return [=](const LegalityQuery
&Query
) {
41 const LLT OldTy
= Query
.Types
[TypeIdx
];
42 return std::make_pair(TypeIdx
, OldTy
.changeElementType(NewEltTy
));
46 LegalizeMutation
LegalizeMutations::changeElementCountTo(unsigned TypeIdx
,
47 unsigned FromTypeIdx
) {
48 return [=](const LegalityQuery
&Query
) {
49 const LLT OldTy
= Query
.Types
[TypeIdx
];
50 const LLT NewTy
= Query
.Types
[FromTypeIdx
];
51 ElementCount NewEltCount
=
52 NewTy
.isVector() ? NewTy
.getElementCount() : ElementCount::getFixed(1);
53 return std::make_pair(TypeIdx
, OldTy
.changeElementCount(NewEltCount
));
57 LegalizeMutation
LegalizeMutations::changeElementCountTo(unsigned TypeIdx
,
59 return [=](const LegalityQuery
&Query
) {
60 const LLT OldTy
= Query
.Types
[TypeIdx
];
61 ElementCount NewEltCount
= NewEltTy
.isVector() ? NewEltTy
.getElementCount()
62 : ElementCount::getFixed(1);
63 return std::make_pair(TypeIdx
, OldTy
.changeElementCount(NewEltCount
));
67 LegalizeMutation
LegalizeMutations::changeElementSizeTo(unsigned TypeIdx
,
68 unsigned FromTypeIdx
) {
69 return [=](const LegalityQuery
&Query
) {
70 const LLT OldTy
= Query
.Types
[TypeIdx
];
71 const LLT NewTy
= Query
.Types
[FromTypeIdx
];
72 const LLT NewEltTy
= LLT::scalar(NewTy
.getScalarSizeInBits());
73 return std::make_pair(TypeIdx
, OldTy
.changeElementType(NewEltTy
));
77 LegalizeMutation
LegalizeMutations::widenScalarOrEltToNextPow2(unsigned TypeIdx
,
79 return [=](const LegalityQuery
&Query
) {
80 const LLT Ty
= Query
.Types
[TypeIdx
];
81 unsigned NewEltSizeInBits
=
82 std::max(1u << Log2_32_Ceil(Ty
.getScalarSizeInBits()), Min
);
83 return std::make_pair(TypeIdx
, Ty
.changeElementSize(NewEltSizeInBits
));
88 LegalizeMutations::widenScalarOrEltToNextMultipleOf(unsigned TypeIdx
,
90 return [=](const LegalityQuery
&Query
) {
91 const LLT Ty
= Query
.Types
[TypeIdx
];
92 unsigned NewEltSizeInBits
= alignTo(Ty
.getScalarSizeInBits(), Size
);
93 return std::make_pair(TypeIdx
, Ty
.changeElementSize(NewEltSizeInBits
));
97 LegalizeMutation
LegalizeMutations::moreElementsToNextPow2(unsigned TypeIdx
,
99 return [=](const LegalityQuery
&Query
) {
100 const LLT VecTy
= Query
.Types
[TypeIdx
];
101 unsigned NewNumElements
=
102 std::max(1u << Log2_32_Ceil(VecTy
.getNumElements()), Min
);
103 return std::make_pair(
104 TypeIdx
, LLT::fixed_vector(NewNumElements
, VecTy
.getElementType()));
108 LegalizeMutation
LegalizeMutations::scalarize(unsigned TypeIdx
) {
109 return [=](const LegalityQuery
&Query
) {
110 return std::make_pair(TypeIdx
, Query
.Types
[TypeIdx
].getElementType());