]> granicus.if.org Git - llvm/commitdiff
[NFC][PatternMatch] Refactor code into a proper "matcher for any integral constant"
authorRoman Lebedev <lebedev.ri@gmail.com>
Mon, 22 Jul 2019 22:09:24 +0000 (22:09 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Mon, 22 Jul 2019 22:09:24 +0000 (22:09 +0000)
Having it as a proper matcher is better for reusability elsewhere
(in a follow-up patch.)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366752 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/PatternMatch.h
lib/Transforms/InstCombine/InstCombineInternal.h

index 0f03d7cc56b886f83498905310d96abacb85abb8..b6b2fa88f5b7484ee779a797f97c9b9457a6533f 100644 (file)
@@ -300,6 +300,15 @@ template <typename Predicate> struct cstfp_pred_ty : public Predicate {
 //
 ///////////////////////////////////////////////////////////////////////////////
 
+struct is_any_apint {
+  bool isValue(const APInt &C) { return true; }
+};
+/// Match an integer or vector with any integral constant.
+/// For vectors, this includes constants with undefined elements.
+inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() {
+  return cst_pred_ty<is_any_apint>();
+}
+
 struct is_all_ones {
   bool isValue(const APInt &C) { return C.isAllOnesValue(); }
 };
index 434b0d5912157460085aa1b12468afa189cd2ae6..327e7ab4fe4ff9db190f6e040a06ccfe877694d4 100644 (file)
@@ -145,26 +145,9 @@ static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) {
     return true;
 
   // Constants can be considered to be not'ed values.
-  if (isa<ConstantInt>(V))
+  if (match(V, m_AnyIntegralConstant()))
     return true;
 
-  // A vector of constant integers can be inverted easily.
-  if (V->getType()->isVectorTy() && isa<Constant>(V)) {
-    unsigned NumElts = V->getType()->getVectorNumElements();
-    for (unsigned i = 0; i != NumElts; ++i) {
-      Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
-      if (!Elt)
-        return false;
-
-      if (isa<UndefValue>(Elt))
-        continue;
-
-      if (!isa<ConstantInt>(Elt))
-        return false;
-    }
-    return true;
-  }
-
   // Compares can be inverted if all of their uses are being modified to use the
   // ~V.
   if (isa<CmpInst>(V))