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 LLT NewEltTy) {
59 return [=](const LegalityQuery &Query) {
60 const LLT OldTy = Query.Types[TypeIdx];
61 ElementCount NewEltCount = NewEltTy.isVector() ? NewEltTy.getElementCount()
62 : ElementCount::getFixed(MinVal: 1);
63 return std::make_pair(x: TypeIdx, y: OldTy.changeElementCount(EC: NewEltCount));
64 };
65}
66
67LegalizeMutation 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(SizeInBits: NewTy.getScalarSizeInBits());
73 return std::make_pair(x: TypeIdx, y: OldTy.changeElementType(NewEltTy));
74 };
75}
76
77LegalizeMutation LegalizeMutations::widenScalarOrEltToNextPow2(unsigned TypeIdx,
78 unsigned Min) {
79 return [=](const LegalityQuery &Query) {
80 const LLT Ty = Query.Types[TypeIdx];
81 unsigned NewEltSizeInBits =
82 std::max(a: 1u << Log2_32_Ceil(Value: Ty.getScalarSizeInBits()), b: Min);
83 return std::make_pair(x: TypeIdx, y: Ty.changeElementSize(NewEltSize: NewEltSizeInBits));
84 };
85}
86
87LegalizeMutation
88LegalizeMutations::widenScalarOrEltToNextMultipleOf(unsigned TypeIdx,
89 unsigned Size) {
90 return [=](const LegalityQuery &Query) {
91 const LLT Ty = Query.Types[TypeIdx];
92 unsigned NewEltSizeInBits = alignTo(Value: Ty.getScalarSizeInBits(), Align: Size);
93 return std::make_pair(x: TypeIdx, y: Ty.changeElementSize(NewEltSize: NewEltSizeInBits));
94 };
95}
96
97LegalizeMutation LegalizeMutations::moreElementsToNextPow2(unsigned TypeIdx,
98 unsigned Min) {
99 return [=](const LegalityQuery &Query) {
100 const LLT VecTy = Query.Types[TypeIdx];
101 unsigned NewNumElements =
102 std::max(a: 1u << Log2_32_Ceil(Value: VecTy.getNumElements()), b: Min);
103 return std::make_pair(
104 x: TypeIdx, y: LLT::fixed_vector(NumElements: NewNumElements, ScalarTy: VecTy.getElementType()));
105 };
106}
107
108LegalizeMutation LegalizeMutations::scalarize(unsigned TypeIdx) {
109 return [=](const LegalityQuery &Query) {
110 return std::make_pair(x: TypeIdx, y: Query.Types[TypeIdx].getElementType());
111 };
112}
113