Skip to content

Commit f9f93e7

Browse files
committed
FPInfo: Legalizer DSL use bitcasts
1 parent a7fa303 commit f9f93e7

File tree

4 files changed

+206
-41
lines changed

4 files changed

+206
-41
lines changed

llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ LegalityPredicate typePairAndMemDescInSet(
282282
std::initializer_list<TypePairAndMemDesc> TypesAndMemDescInit);
283283
/// True iff the specified type index is a scalar.
284284
LegalityPredicate isScalar(unsigned TypeIdx);
285+
/// True iff the specified type index is a integer.
286+
LegalityPredicate isInteger(unsigned TypeIdx);
287+
/// True iff the specified type index is a float.
288+
LegalityPredicate isFloat(unsigned TypeIdx);
285289
/// True iff the specified type index is a vector.
286290
LegalityPredicate isVector(unsigned TypeIdx);
287291
/// True iff the specified type index is a pointer (with any address space).
@@ -292,6 +296,14 @@ LegalityPredicate isPointer(unsigned TypeIdx, unsigned AddrSpace);
292296
/// True iff the specified type index is a vector of pointers (with any address
293297
/// space).
294298
LegalityPredicate isPointerVector(unsigned TypeIdx);
299+
/// True iff the specified type index is a vector of integer
300+
LegalityPredicate isIntegerVector(unsigned TypeIdx);
301+
/// True iff the specified type index is a vector of floats.
302+
LegalityPredicate isFloatVector(unsigned TypeIdx);
303+
304+
LegalityPredicate isFloatOrFloatVector(unsigned TypeIdx);
305+
306+
LegalityPredicate isIntegerOrIntegerVector(unsigned TypeIdx);
295307

296308
/// True if the type index is a vector with element type \p EltTy
297309
LegalityPredicate elementTypeIs(unsigned TypeIdx, LLT EltTy);
@@ -330,6 +342,10 @@ LegalityPredicate sizeIs(unsigned TypeIdx, unsigned Size);
330342
/// True iff the specified type indices are both the same bit size.
331343
LegalityPredicate sameSize(unsigned TypeIdx0, unsigned TypeIdx1);
332344

345+
LegalityPredicate sameScalarKind(unsigned TypeIdx, LLT Ty);
346+
347+
LegalityPredicate sameKind(unsigned TypeIdx, LLT Ty);
348+
333349
/// True iff the first type index has a larger total bit size than second type
334350
/// index.
335351
LegalityPredicate largerThan(unsigned TypeIdx0, unsigned TypeIdx1);
@@ -381,6 +397,8 @@ LegalizeMutation changeElementCountTo(unsigned TypeIdx, ElementCount EC);
381397
/// only changes the size.
382398
LegalizeMutation changeElementSizeTo(unsigned TypeIdx, unsigned FromTypeIdx);
383399

400+
LegalizeMutation changeToInteger(unsigned TypeIdx);
401+
384402
/// Widen the scalar type or vector element type for the given type index to the
385403
/// next power of 2.
386404
LegalizeMutation widenScalarOrEltToNextPow2(unsigned TypeIdx, unsigned Min = 0);
@@ -942,6 +960,16 @@ class LegalizeRuleSet {
942960
LegalizeMutations::widenScalarOrEltToNextPow2(TypeIdx, MinSize));
943961
}
944962

963+
LegalizeRuleSet &widenScalarToNextPow2Bitcast(unsigned TypeIdx,
964+
unsigned MinSize = 0) {
965+
using namespace LegalityPredicates;
966+
using namespace LegalizeMutations;
967+
return actionIf(
968+
LegalizeAction::Bitcast,
969+
all(isFloatOrFloatVector(TypeIdx), sizeNotPow2(typeIdx(TypeIdx))),
970+
changeToInteger(TypeIdx));
971+
}
972+
945973
/// Widen the scalar to the next multiple of Size. No effect if the
946974
/// type is not a scalar or is a multiple of Size.
947975
LegalizeRuleSet &widenScalarToNextMultipleOf(unsigned TypeIdx,
@@ -997,20 +1025,32 @@ class LegalizeRuleSet {
9971025
LegalizeRuleSet &minScalarOrElt(unsigned TypeIdx, const LLT Ty) {
9981026
using namespace LegalityPredicates;
9991027
using namespace LegalizeMutations;
1000-
return actionIf(LegalizeAction::WidenScalar,
1001-
scalarOrEltNarrowerThan(TypeIdx, Ty.getScalarSizeInBits()),
1002-
changeElementTo(typeIdx(TypeIdx), Ty));
1028+
return actionIf(
1029+
LegalizeAction::WidenScalar,
1030+
all(sameScalarKind(TypeIdx, Ty),
1031+
scalarOrEltNarrowerThan(TypeIdx, Ty.getScalarSizeInBits())),
1032+
changeElementTo(typeIdx(TypeIdx), Ty));
1033+
}
1034+
LegalizeRuleSet &minScalarOrEltBitcast(unsigned TypeIdx, const LLT Ty) {
1035+
using namespace LegalityPredicates;
1036+
using namespace LegalizeMutations;
1037+
return actionIf(
1038+
LegalizeAction::Bitcast,
1039+
all(isFloatOrFloatVector(TypeIdx),
1040+
scalarOrEltNarrowerThan(TypeIdx, Ty.getScalarSizeInBits())),
1041+
changeToInteger(typeIdx(TypeIdx)));
10031042
}
10041043

10051044
/// Ensure the scalar or element is at least as wide as Ty.
10061045
LegalizeRuleSet &minScalarOrEltIf(LegalityPredicate Predicate,
10071046
unsigned TypeIdx, const LLT Ty) {
10081047
using namespace LegalityPredicates;
10091048
using namespace LegalizeMutations;
1010-
return actionIf(LegalizeAction::WidenScalar,
1011-
all(Predicate, scalarOrEltNarrowerThan(
1012-
TypeIdx, Ty.getScalarSizeInBits())),
1013-
changeElementTo(typeIdx(TypeIdx), Ty));
1049+
return actionIf(
1050+
LegalizeAction::WidenScalar,
1051+
all(Predicate, sameScalarKind(TypeIdx, Ty),
1052+
scalarOrEltNarrowerThan(TypeIdx, Ty.getScalarSizeInBits())),
1053+
changeElementTo(typeIdx(TypeIdx), Ty));
10141054
}
10151055

10161056
/// Ensure the vector size is at least as wide as VectorSize by promoting the
@@ -1039,14 +1079,23 @@ class LegalizeRuleSet {
10391079
using namespace LegalityPredicates;
10401080
using namespace LegalizeMutations;
10411081
return actionIf(LegalizeAction::WidenScalar,
1042-
scalarNarrowerThan(TypeIdx, Ty.getSizeInBits()),
1082+
all(sameKind(TypeIdx, Ty),
1083+
scalarNarrowerThan(TypeIdx, Ty.getSizeInBits())),
10431084
changeTo(typeIdx(TypeIdx), Ty));
10441085
}
10451086
LegalizeRuleSet &minScalar(bool Pred, unsigned TypeIdx, const LLT Ty) {
10461087
if (!Pred)
10471088
return *this;
10481089
return minScalar(TypeIdx, Ty);
10491090
}
1091+
LegalizeRuleSet &minScalarBitcast(unsigned TypeIdx, const LLT Ty) {
1092+
using namespace LegalityPredicates;
1093+
using namespace LegalizeMutations;
1094+
return actionIf(LegalizeAction::Bitcast,
1095+
all(isFloatOrFloatVector(TypeIdx),
1096+
scalarNarrowerThan(TypeIdx, Ty.getSizeInBits())),
1097+
changeToInteger(typeIdx(TypeIdx)));
1098+
}
10501099

10511100
/// Ensure the scalar is at least as wide as Ty if condition is met.
10521101
LegalizeRuleSet &minScalarIf(LegalityPredicate Predicate, unsigned TypeIdx,
@@ -1068,19 +1117,39 @@ class LegalizeRuleSet {
10681117
LegalizeRuleSet &maxScalarOrElt(unsigned TypeIdx, const LLT Ty) {
10691118
using namespace LegalityPredicates;
10701119
using namespace LegalizeMutations;
1071-
return actionIf(LegalizeAction::NarrowScalar,
1072-
scalarOrEltWiderThan(TypeIdx, Ty.getScalarSizeInBits()),
1073-
changeElementTo(typeIdx(TypeIdx), Ty));
1120+
return actionIf(
1121+
LegalizeAction::NarrowScalar,
1122+
all(sameScalarKind(TypeIdx, Ty),
1123+
scalarOrEltWiderThan(TypeIdx, Ty.getScalarSizeInBits())),
1124+
changeElementTo(typeIdx(TypeIdx), Ty));
1125+
}
1126+
LegalizeRuleSet &maxScalarOrEltBitcast(unsigned TypeIdx, const LLT Ty) {
1127+
using namespace LegalityPredicates;
1128+
using namespace LegalizeMutations;
1129+
return actionIf(
1130+
LegalizeAction::NarrowScalar,
1131+
all(isFloatOrFloatVector(TypeIdx),
1132+
scalarOrEltWiderThan(TypeIdx, Ty.getScalarSizeInBits())),
1133+
changeToInteger(typeIdx(TypeIdx)));
10741134
}
10751135

10761136
/// Ensure the scalar is at most as wide as Ty.
10771137
LegalizeRuleSet &maxScalar(unsigned TypeIdx, const LLT Ty) {
10781138
using namespace LegalityPredicates;
10791139
using namespace LegalizeMutations;
10801140
return actionIf(LegalizeAction::NarrowScalar,
1081-
scalarWiderThan(TypeIdx, Ty.getSizeInBits()),
1141+
all(sameKind(TypeIdx, Ty),
1142+
scalarWiderThan(TypeIdx, Ty.getSizeInBits())),
10821143
changeTo(typeIdx(TypeIdx), Ty));
10831144
}
1145+
LegalizeRuleSet &maxScalarBitcast(unsigned TypeIdx, const LLT Ty) {
1146+
using namespace LegalityPredicates;
1147+
using namespace LegalizeMutations;
1148+
return actionIf(LegalizeAction::NarrowScalar,
1149+
all(isFloatOrFloatVector(TypeIdx),
1150+
scalarWiderThan(TypeIdx, Ty.getSizeInBits())),
1151+
changeToInteger(typeIdx(TypeIdx)));
1152+
}
10841153

10851154
/// Conditionally limit the maximum size of the scalar.
10861155
/// For example, when the maximum size of one type depends on the size of
@@ -1103,10 +1172,20 @@ class LegalizeRuleSet {
11031172
/// Limit the range of scalar sizes to MinTy and MaxTy.
11041173
LegalizeRuleSet &clampScalar(unsigned TypeIdx, const LLT MinTy,
11051174
const LLT MaxTy) {
1175+
assert(MinTy.getKind() == MaxTy.getKind() &&
1176+
"Expected LLT of the same kind");
11061177
assert(MinTy.isScalar() && MaxTy.isScalar() && "Expected scalar types");
11071178
return minScalar(TypeIdx, MinTy).maxScalar(TypeIdx, MaxTy);
11081179
}
11091180

1181+
LegalizeRuleSet &clampScalarBitcast(unsigned TypeIdx, const LLT MinTy,
1182+
const LLT MaxTy) {
1183+
assert(MinTy.getKind() == MaxTy.getKind() &&
1184+
"Expected LLT of the same kind");
1185+
assert(MinTy.isScalar() && MaxTy.isScalar() && "Expected scalar types");
1186+
return minScalarBitcast(TypeIdx, MinTy).maxScalarBitcast(TypeIdx, MaxTy);
1187+
}
1188+
11101189
LegalizeRuleSet &clampScalar(bool Pred, unsigned TypeIdx, const LLT MinTy,
11111190
const LLT MaxTy) {
11121191
if (!Pred)
@@ -1120,6 +1199,12 @@ class LegalizeRuleSet {
11201199
return minScalarOrElt(TypeIdx, MinTy).maxScalarOrElt(TypeIdx, MaxTy);
11211200
}
11221201

1202+
LegalizeRuleSet &clampScalarOrEltBitcast(unsigned TypeIdx, const LLT MinTy,
1203+
const LLT MaxTy) {
1204+
return minScalarOrEltBitcast(TypeIdx, MinTy)
1205+
.maxScalarOrEltBitcast(TypeIdx, MaxTy);
1206+
}
1207+
11231208
/// Widen the scalar to match the size of another.
11241209
LegalizeRuleSet &minScalarSameAs(unsigned TypeIdx, unsigned LargeTypeIdx) {
11251210
typeIdx(TypeIdx);

llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ LegalityPredicate LegalityPredicates::isScalar(unsigned TypeIdx) {
8181
};
8282
}
8383

84+
LegalityPredicate LegalityPredicates::isInteger(unsigned TypeIdx) {
85+
return [=](const LegalityQuery &Query) {
86+
return Query.Types[TypeIdx].isInteger();
87+
};
88+
}
89+
90+
LegalityPredicate LegalityPredicates::isFloat(unsigned TypeIdx) {
91+
return [=](const LegalityQuery &Query) {
92+
return Query.Types[TypeIdx].isFloat();
93+
};
94+
}
95+
8496
LegalityPredicate LegalityPredicates::isVector(unsigned TypeIdx) {
8597
return [=](const LegalityQuery &Query) {
8698
return Query.Types[TypeIdx].isVector();
@@ -107,6 +119,30 @@ LegalityPredicate LegalityPredicates::isPointerVector(unsigned TypeIdx) {
107119
};
108120
}
109121

122+
LegalityPredicate LegalityPredicates::isIntegerVector(unsigned TypeIdx) {
123+
return [=](const LegalityQuery &Query) {
124+
return Query.Types[TypeIdx].isIntegerVector();
125+
};
126+
}
127+
LegalityPredicate
128+
LegalityPredicates::isIntegerOrIntegerVector(unsigned TypeIdx) {
129+
return [=](const LegalityQuery &Query) {
130+
LLT Ty = Query.Types[TypeIdx];
131+
return Ty.isInteger() || Ty.isIntegerVector();
132+
};
133+
}
134+
LegalityPredicate LegalityPredicates::isFloatVector(unsigned TypeIdx) {
135+
return [=](const LegalityQuery &Query) {
136+
return Query.Types[TypeIdx].isFloatVector();
137+
};
138+
}
139+
LegalityPredicate LegalityPredicates::isFloatOrFloatVector(unsigned TypeIdx) {
140+
return [=](const LegalityQuery &Query) {
141+
LLT Ty = Query.Types[TypeIdx];
142+
return Ty.isFloat() || Ty.isFloatVector();
143+
};
144+
}
145+
110146
LegalityPredicate LegalityPredicates::elementTypeIs(unsigned TypeIdx,
111147
LLT EltTy) {
112148
return [=](const LegalityQuery &Query) {
@@ -200,6 +236,28 @@ LegalityPredicate LegalityPredicates::sameSize(unsigned TypeIdx0,
200236
};
201237
}
202238

239+
LegalityPredicate LegalityPredicates::sameScalarKind(unsigned TypeIdx, LLT Ty) {
240+
return [=](const LegalityQuery &Query) {
241+
LLT QueryTy = Query.Types[TypeIdx].getScalarType();
242+
if (Ty.isFloat())
243+
return QueryTy.getKind() == Ty.getKind() &&
244+
QueryTy.getFPVariant() == Ty.getFPVariant();
245+
246+
return QueryTy.getKind() == Ty.getKind();
247+
};
248+
}
249+
250+
LegalityPredicate LegalityPredicates::sameKind(unsigned TypeIdx, LLT Ty) {
251+
return [=](const LegalityQuery &Query) {
252+
LLT QueryTy = Query.Types[TypeIdx];
253+
if (Ty.isFloat() || Ty.isFloatVector())
254+
return QueryTy.getKind() == Ty.getKind() &&
255+
QueryTy.getFPVariant() == Ty.getFPVariant();
256+
257+
return QueryTy.getKind() == Ty.getKind();
258+
};
259+
}
260+
203261
LegalityPredicate LegalityPredicates::memSizeInBytesNotPow2(unsigned MMOIdx) {
204262
return [=](const LegalityQuery &Query) {
205263
return !llvm::has_single_bit<uint32_t>(

llvm/lib/CodeGen/GlobalISel/LegalizeMutations.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ LegalizeMutation LegalizeMutations::changeElementSizeTo(unsigned TypeIdx,
7272
};
7373
}
7474

75+
LegalizeMutation LegalizeMutations::changeToInteger(unsigned TypeIdx) {
76+
return [=](const LegalityQuery &Query) {
77+
const LLT OldTy = Query.Types[TypeIdx];
78+
const LLT NewTy = OldTy.changeToInteger();
79+
return std::make_pair(TypeIdx, NewTy);
80+
};
81+
}
82+
7583
LegalizeMutation LegalizeMutations::widenScalarOrEltToNextPow2(unsigned TypeIdx,
7684
unsigned Min) {
7785
return [=](const LegalityQuery &Query) {

0 commit comments

Comments
 (0)