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 
13 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
14 
15 using namespace llvm;
16 
17 LegalizeMutation LegalizeMutations::changeTo(unsigned TypeIdx, LLT Ty) {
18   return
19       [=](const LegalityQuery &Query) { return std::make_pair(TypeIdx, Ty); };
20 }
21 
22 LegalizeMutation LegalizeMutations::changeTo(unsigned TypeIdx,
23                                              unsigned FromTypeIdx) {
24   return [=](const LegalityQuery &Query) {
25     return std::make_pair(TypeIdx, Query.Types[FromTypeIdx]);
26   };
27 }
28 
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));
35   };
36 }
37 
38 LegalizeMutation LegalizeMutations::changeElementTo(unsigned TypeIdx,
39                                                     LLT NewEltTy) {
40   return [=](const LegalityQuery &Query) {
41     const LLT OldTy = Query.Types[TypeIdx];
42     return std::make_pair(TypeIdx, OldTy.changeElementType(NewEltTy));
43   };
44 }
45 
46 LegalizeMutation LegalizeMutations::changeElementSizeTo(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     const LLT NewEltTy = LLT::scalar(NewTy.getScalarSizeInBits());
52     return std::make_pair(TypeIdx, OldTy.changeElementType(NewEltTy));
53   };
54 }
55 
56 LegalizeMutation LegalizeMutations::widenScalarOrEltToNextPow2(unsigned TypeIdx,
57                                                                unsigned Min) {
58   return [=](const LegalityQuery &Query) {
59     const LLT Ty = Query.Types[TypeIdx];
60     unsigned NewEltSizeInBits =
61         std::max(1u << Log2_32_Ceil(Ty.getScalarSizeInBits()), Min);
62     return std::make_pair(TypeIdx, Ty.changeElementSize(NewEltSizeInBits));
63   };
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 
77 LegalizeMutation LegalizeMutations::scalarize(unsigned TypeIdx) {
78   return [=](const LegalityQuery &Query) {
79     return std::make_pair(TypeIdx, Query.Types[TypeIdx].getElementType());
80   };
81 }
82