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::widenScalarToNextPow2(unsigned TypeIdx,
30                                                           unsigned Min) {
31   return [=](const LegalityQuery &Query) {
32     unsigned NewSizeInBits =
33         1 << Log2_32_Ceil(Query.Types[TypeIdx].getSizeInBits());
34     if (NewSizeInBits < Min)
35       NewSizeInBits = Min;
36     return std::make_pair(TypeIdx, LLT::scalar(NewSizeInBits));
37   };
38 }
39 
40 LegalizeMutation LegalizeMutations::moreElementsToNextPow2(unsigned TypeIdx,
41                                                            unsigned Min) {
42   return [=](const LegalityQuery &Query) {
43     const LLT &VecTy = Query.Types[TypeIdx];
44     unsigned NewNumElements = 1 << Log2_32_Ceil(VecTy.getNumElements());
45     if (NewNumElements < Min)
46       NewNumElements = Min;
47     return std::make_pair(
48         TypeIdx, LLT::vector(NewNumElements, VecTy.getScalarSizeInBits()));
49   };
50 }
51 
52 LegalizeMutation LegalizeMutations::scalarize(unsigned TypeIdx) {
53   return [=](const LegalityQuery &Query) {
54     return std::make_pair(TypeIdx, Query.Types[TypeIdx].getElementType());
55   };
56 }
57