diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h --- a/llvm/include/llvm/IR/Constants.h +++ b/llvm/include/llvm/IR/Constants.h @@ -308,6 +308,7 @@ /// Return true if Ty is big enough to represent V. static bool isValueValidForType(Type *Ty, const APFloat &V); inline const APFloat &getValueAPF() const { return Val; } + inline const APFloat &getValue() const { return Val; } /// Return true if the value is positive or negative zero. bool isZero() const { return Val.isZero(); } diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h --- a/llvm/include/llvm/IR/PatternMatch.h +++ b/llvm/include/llvm/IR/PatternMatch.h @@ -262,17 +262,18 @@ return constantint_match(); } -/// This helper class is used to match integer constant scalars, vector splats, +/// This helper class is used to match constant scalars, vector splats, /// and fixed width vectors that satisfy a specified predicate. /// For fixed width vector constants, undefined elements are ignored. -template struct cst_pred_ty : public Predicate { +template +struct cstval_pred_ty : public Predicate { template bool match(ITy *V) { - if (const auto *CI = dyn_cast(V)) - return this->isValue(CI->getValue()); + if (const auto *CV = dyn_cast(V)) + return this->isValue(CV->getValue()); if (const auto *VTy = dyn_cast(V->getType())) { if (const auto *C = dyn_cast(V)) { - if (const auto *CI = dyn_cast_or_null(C->getSplatValue())) - return this->isValue(CI->getValue()); + if (const auto *CV = dyn_cast_or_null(C->getSplatValue())) + return this->isValue(CV->getValue()); // Number of elements of a scalable vector unknown at compile time auto *FVTy = dyn_cast(VTy); @@ -289,8 +290,8 @@ return false; if (isa(Elt)) continue; - auto *CI = dyn_cast(Elt); - if (!CI || !this->isValue(CI->getValue())) + auto *CV = dyn_cast(Elt); + if (!CV || !this->isValue(CV->getValue())) return false; HasNonUndefElements = true; } @@ -301,6 +302,14 @@ } }; +/// specialization of cstval_pred_ty for ConstantInt +template +using cst_pred_ty = cstval_pred_ty; + +/// specialization of cstval_pred_ty for ConstantFP +template +using cstfp_pred_ty = cstval_pred_ty; + /// This helper class is used to match scalar and vector constants that /// satisfy a specified predicate, and bind them to an APInt. template struct api_pred_ty : public Predicate { @@ -326,45 +335,6 @@ } }; -/// This helper class is used to match float constant scalars, vector splats, -/// and fixed width vectors that satisfy a specified predicate. -/// For fixed width vector constants, undefined elements are ignored. -template struct cstfp_pred_ty : public Predicate { - template bool match(ITy *V) { - if (const auto *CF = dyn_cast(V)) - return this->isValue(CF->getValueAPF()); - if (const auto *VTy = dyn_cast(V->getType())) { - if (const auto *C = dyn_cast(V)) { - if (const auto *CF = dyn_cast_or_null(C->getSplatValue())) - return this->isValue(CF->getValueAPF()); - - // Number of elements of a scalable vector unknown at compile time - auto *FVTy = dyn_cast(VTy); - if (!FVTy) - return false; - - // Non-splat vector constant: check each element for a match. - unsigned NumElts = FVTy->getNumElements(); - assert(NumElts != 0 && "Constant vector with no elements?"); - bool HasNonUndefElements = false; - for (unsigned i = 0; i != NumElts; ++i) { - Constant *Elt = C->getAggregateElement(i); - if (!Elt) - return false; - if (isa(Elt)) - continue; - auto *CF = dyn_cast(Elt); - if (!CF || !this->isValue(CF->getValueAPF())) - return false; - HasNonUndefElements = true; - } - return HasNonUndefElements; - } - } - return false; - } -}; - /////////////////////////////////////////////////////////////////////////////// // // Encapsulate constant value queries for use in templated predicate matchers.