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
15using namespace llvm;
16
17LegalizeMutation LegalizeMutations::changeTo(unsigned TypeIdx, LLT Ty) {
18 return
19 [=](const LegalityQuery &Query) { return std::make_pair(x: TypeIdx, y: Ty); };
20}
21
22LegalizeMutation LegalizeMutations::changeTo(unsigned TypeIdx,
23 unsigned FromTypeIdx) {
24 return [=](const LegalityQuery &Query) {
25 return std::make_pair(x: TypeIdx, y: Query.Types[FromTypeIdx]);
26 };
27}
28
29LegalizeMutation 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(x: TypeIdx, y: OldTy.changeElementType(NewEltTy: NewTy));
35 };
36}
37
38LegalizeMutation LegalizeMutations::changeElementTo(unsigned TypeIdx,
39 LLT NewEltTy) {
40 return [=](const LegalityQuery &Query) {
41 const LLT OldTy = Query.Types[TypeIdx];
42 return std::make_pair(x: TypeIdx, y: OldTy.changeElementType(NewEltTy));
43 };
44}
45
46LegalizeMutation 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(MinVal: 1);
53 return std::make_pair(x: TypeIdx, y: OldTy.changeElementCount(EC: NewEltCount));
54 };
55}
56
57LegalizeMutation LegalizeMutations::changeElementCountTo(unsigned TypeIdx,
58 ElementCount EC) {
59 return [=](const LegalityQuery &Query) {
60 const LLT OldTy = Query.Types[TypeIdx];
61 return std::make_pair(x: TypeIdx, y: OldTy.changeElementCount(EC));
62 };
63}
64
65LegalizeMutation LegalizeMutations::changeElementSizeTo(unsigned TypeIdx,
66 unsigned FromTypeIdx) {
67 return [=](const LegalityQuery &Query) {
68 const LLT OldTy = Query.Types[TypeIdx];
69 const LLT NewTy = Query.Types[FromTypeIdx];
70 const LLT NewEltTy = LLT::scalar(SizeInBits: NewTy.getScalarSizeInBits());
71 return std::make_pair(x: TypeIdx, y: OldTy.changeElementType(NewEltTy));
72 };
73}
74
75LegalizeMutation LegalizeMutations::widenScalarOrEltToNextPow2(unsigned TypeIdx,
76 unsigned Min) {
77 return [=](const LegalityQuery &Query) {
78 const LLT Ty = Query.Types[TypeIdx];
79 unsigned NewEltSizeInBits =
80 std::max(a: 1u << Log2_32_Ceil(Value: Ty.getScalarSizeInBits()), b: Min);
81 return std::make_pair(x: TypeIdx, y: Ty.changeElementSize(NewEltSize: NewEltSizeInBits));
82 };
83}
84
85LegalizeMutation
86LegalizeMutations::widenScalarOrEltToNextMultipleOf(unsigned TypeIdx,
87 unsigned Size) {
88 return [=](const LegalityQuery &Query) {
89 const LLT Ty = Query.Types[TypeIdx];
90 unsigned NewEltSizeInBits = alignTo(Value: Ty.getScalarSizeInBits(), Align: Size);
91 return std::make_pair(x: TypeIdx, y: Ty.changeElementSize(NewEltSize: NewEltSizeInBits));
92 };
93}
94
95LegalizeMutation LegalizeMutations::moreElementsToNextPow2(unsigned TypeIdx,
96 unsigned Min) {
97 return [=](const LegalityQuery &Query) {
98 const LLT VecTy = Query.Types[TypeIdx];
99 unsigned NewNumElements =
100 std::max(a: 1u << Log2_32_Ceil(Value: VecTy.getNumElements()), b: Min);
101 return std::make_pair(
102 x: TypeIdx, y: LLT::fixed_vector(NumElements: NewNumElements, ScalarTy: VecTy.getElementType()));
103 };
104}
105
106LegalizeMutation LegalizeMutations::scalarize(unsigned TypeIdx) {
107 return [=](const LegalityQuery &Query) {
108 return std::make_pair(x: TypeIdx, y: Query.Types[TypeIdx].getElementType());
109 };
110}
111