Skip to content

Commit 10847f5

Browse files
authored
[PatternMatch] Unify ap(int|float)_match (NFC) (#159575)
1 parent e08d8e3 commit 10847f5

File tree

1 file changed

+22
-43
lines changed

1 file changed

+22
-43
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -259,86 +259,65 @@ inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
259259
return match_combine_and<LTy, RTy>(L, R);
260260
}
261261

262-
struct apint_match {
263-
const APInt *&Res;
262+
template <typename APTy> struct ap_match {
263+
static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
264+
using ConstantTy =
265+
std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
266+
267+
const APTy *&Res;
264268
bool AllowPoison;
265269

266-
apint_match(const APInt *&Res, bool AllowPoison)
270+
ap_match(const APTy *&Res, bool AllowPoison)
267271
: Res(Res), AllowPoison(AllowPoison) {}
268272

269273
template <typename ITy> bool match(ITy *V) const {
270-
if (auto *CI = dyn_cast<ConstantInt>(V)) {
274+
if (auto *CI = dyn_cast<ConstantTy>(V)) {
271275
Res = &CI->getValue();
272276
return true;
273277
}
274278
if (V->getType()->isVectorTy())
275279
if (const auto *C = dyn_cast<Constant>(V))
276280
if (auto *CI =
277-
dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison))) {
281+
dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
278282
Res = &CI->getValue();
279283
return true;
280284
}
281285
return false;
282286
}
283287
};
284-
// Either constexpr if or renaming ConstantFP::getValueAPF to
285-
// ConstantFP::getValue is needed to do it via single template
286-
// function for both apint/apfloat.
287-
struct apfloat_match {
288-
const APFloat *&Res;
289-
bool AllowPoison;
290-
291-
apfloat_match(const APFloat *&Res, bool AllowPoison)
292-
: Res(Res), AllowPoison(AllowPoison) {}
293-
294-
template <typename ITy> bool match(ITy *V) const {
295-
if (auto *CI = dyn_cast<ConstantFP>(V)) {
296-
Res = &CI->getValueAPF();
297-
return true;
298-
}
299-
if (V->getType()->isVectorTy())
300-
if (const auto *C = dyn_cast<Constant>(V))
301-
if (auto *CI =
302-
dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowPoison))) {
303-
Res = &CI->getValueAPF();
304-
return true;
305-
}
306-
return false;
307-
}
308-
};
309288

310289
/// Match a ConstantInt or splatted ConstantVector, binding the
311290
/// specified pointer to the contained APInt.
312-
inline apint_match m_APInt(const APInt *&Res) {
291+
inline ap_match<APInt> m_APInt(const APInt *&Res) {
313292
// Forbid poison by default to maintain previous behavior.
314-
return apint_match(Res, /* AllowPoison */ false);
293+
return ap_match<APInt>(Res, /* AllowPoison */ false);
315294
}
316295

317296
/// Match APInt while allowing poison in splat vector constants.
318-
inline apint_match m_APIntAllowPoison(const APInt *&Res) {
319-
return apint_match(Res, /* AllowPoison */ true);
297+
inline ap_match<APInt> m_APIntAllowPoison(const APInt *&Res) {
298+
return ap_match<APInt>(Res, /* AllowPoison */ true);
320299
}
321300

322301
/// Match APInt while forbidding poison in splat vector constants.
323-
inline apint_match m_APIntForbidPoison(const APInt *&Res) {
324-
return apint_match(Res, /* AllowPoison */ false);
302+
inline ap_match<APInt> m_APIntForbidPoison(const APInt *&Res) {
303+
return ap_match<APInt>(Res, /* AllowPoison */ false);
325304
}
326305

327306
/// Match a ConstantFP or splatted ConstantVector, binding the
328307
/// specified pointer to the contained APFloat.
329-
inline apfloat_match m_APFloat(const APFloat *&Res) {
308+
inline ap_match<APFloat> m_APFloat(const APFloat *&Res) {
330309
// Forbid undefs by default to maintain previous behavior.
331-
return apfloat_match(Res, /* AllowPoison */ false);
310+
return ap_match<APFloat>(Res, /* AllowPoison */ false);
332311
}
333312

334313
/// Match APFloat while allowing poison in splat vector constants.
335-
inline apfloat_match m_APFloatAllowPoison(const APFloat *&Res) {
336-
return apfloat_match(Res, /* AllowPoison */ true);
314+
inline ap_match<APFloat> m_APFloatAllowPoison(const APFloat *&Res) {
315+
return ap_match<APFloat>(Res, /* AllowPoison */ true);
337316
}
338317

339318
/// Match APFloat while forbidding poison in splat vector constants.
340-
inline apfloat_match m_APFloatForbidPoison(const APFloat *&Res) {
341-
return apfloat_match(Res, /* AllowPoison */ false);
319+
inline ap_match<APFloat> m_APFloatForbidPoison(const APFloat *&Res) {
320+
return ap_match<APFloat>(Res, /* AllowPoison */ false);
342321
}
343322

344323
template <int64_t Val> struct constantint_match {
@@ -1027,7 +1006,7 @@ struct bind_const_intval_ty {
10271006

10281007
template <typename ITy> bool match(ITy *V) const {
10291008
const APInt *ConstInt;
1030-
if (!apint_match(ConstInt, /*AllowPoison=*/false).match(V))
1009+
if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
10311010
return false;
10321011
if (ConstInt->getActiveBits() > 64)
10331012
return false;

0 commit comments

Comments
 (0)