xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/GlobalISel/LegalizeMutations.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- lib/CodeGen/GlobalISel/LegalizerMutations.cpp - Mutations ----------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // A library of mutation factories to use for LegalityMutation.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric using namespace llvm;
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric LegalizeMutation LegalizeMutations::changeTo(unsigned TypeIdx, LLT Ty) {
18*0b57cec5SDimitry Andric   return
19*0b57cec5SDimitry Andric       [=](const LegalityQuery &Query) { return std::make_pair(TypeIdx, Ty); };
20*0b57cec5SDimitry Andric }
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric LegalizeMutation LegalizeMutations::changeTo(unsigned TypeIdx,
23*0b57cec5SDimitry Andric                                              unsigned FromTypeIdx) {
24*0b57cec5SDimitry Andric   return [=](const LegalityQuery &Query) {
25*0b57cec5SDimitry Andric     return std::make_pair(TypeIdx, Query.Types[FromTypeIdx]);
26*0b57cec5SDimitry Andric   };
27*0b57cec5SDimitry Andric }
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric LegalizeMutation LegalizeMutations::changeElementTo(unsigned TypeIdx,
30*0b57cec5SDimitry Andric                                                     unsigned FromTypeIdx) {
31*0b57cec5SDimitry Andric   return [=](const LegalityQuery &Query) {
32*0b57cec5SDimitry Andric     const LLT OldTy = Query.Types[TypeIdx];
33*0b57cec5SDimitry Andric     const LLT NewTy = Query.Types[FromTypeIdx];
34*0b57cec5SDimitry Andric     return std::make_pair(TypeIdx, OldTy.changeElementType(NewTy));
35*0b57cec5SDimitry Andric   };
36*0b57cec5SDimitry Andric }
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric LegalizeMutation LegalizeMutations::changeElementTo(unsigned TypeIdx,
39*0b57cec5SDimitry Andric                                                     LLT NewEltTy) {
40*0b57cec5SDimitry Andric   return [=](const LegalityQuery &Query) {
41*0b57cec5SDimitry Andric     const LLT OldTy = Query.Types[TypeIdx];
42*0b57cec5SDimitry Andric     return std::make_pair(TypeIdx, OldTy.changeElementType(NewEltTy));
43*0b57cec5SDimitry Andric   };
44*0b57cec5SDimitry Andric }
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric LegalizeMutation LegalizeMutations::widenScalarOrEltToNextPow2(unsigned TypeIdx,
47*0b57cec5SDimitry Andric                                                                unsigned Min) {
48*0b57cec5SDimitry Andric   return [=](const LegalityQuery &Query) {
49*0b57cec5SDimitry Andric     const LLT Ty = Query.Types[TypeIdx];
50*0b57cec5SDimitry Andric     unsigned NewEltSizeInBits =
51*0b57cec5SDimitry Andric         std::max(1u << Log2_32_Ceil(Ty.getScalarSizeInBits()), Min);
52*0b57cec5SDimitry Andric     return std::make_pair(TypeIdx, Ty.changeElementSize(NewEltSizeInBits));
53*0b57cec5SDimitry Andric   };
54*0b57cec5SDimitry Andric }
55*0b57cec5SDimitry Andric 
56*0b57cec5SDimitry Andric LegalizeMutation LegalizeMutations::moreElementsToNextPow2(unsigned TypeIdx,
57*0b57cec5SDimitry Andric                                                            unsigned Min) {
58*0b57cec5SDimitry Andric   return [=](const LegalityQuery &Query) {
59*0b57cec5SDimitry Andric     const LLT VecTy = Query.Types[TypeIdx];
60*0b57cec5SDimitry Andric     unsigned NewNumElements =
61*0b57cec5SDimitry Andric         std::max(1u << Log2_32_Ceil(VecTy.getNumElements()), Min);
62*0b57cec5SDimitry Andric     return std::make_pair(TypeIdx,
63*0b57cec5SDimitry Andric                           LLT::vector(NewNumElements, VecTy.getElementType()));
64*0b57cec5SDimitry Andric   };
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric LegalizeMutation LegalizeMutations::scalarize(unsigned TypeIdx) {
68*0b57cec5SDimitry Andric   return [=](const LegalityQuery &Query) {
69*0b57cec5SDimitry Andric     return std::make_pair(TypeIdx, Query.Types[TypeIdx].getElementType());
70*0b57cec5SDimitry Andric   };
71*0b57cec5SDimitry Andric }
72