[llvm] r369842 - [Constant] Add 'isElementWiseEqual()' method

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 23 23:49:51 PDT 2019


Author: lebedevri
Date: Fri Aug 23 23:49:51 2019
New Revision: 369842

URL: http://llvm.org/viewvc/llvm-project?rev=369842&view=rev
Log:
[Constant] Add 'isElementWiseEqual()' method

Promoting it from InstCombine's tryToReuseConstantFromSelectInComparison().

Return true if this constant and a constant 'Y' are element-wise equal.
This is identical to just comparing the pointers, with the exception that
for vectors, if only one of the constants has an `undef` element in some
lane, the constants still match.

Modified:
    llvm/trunk/include/llvm/IR/Constant.h
    llvm/trunk/lib/IR/Constants.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp

Modified: llvm/trunk/include/llvm/IR/Constant.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Constant.h?rev=369842&r1=369841&r2=369842&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Constant.h (original)
+++ llvm/trunk/include/llvm/IR/Constant.h Fri Aug 23 23:49:51 2019
@@ -86,6 +86,12 @@ public:
   /// floating-point constant with all NaN elements.
   bool isNaN() const;
 
+  /// Return true if this constant and a constant 'Y' are element-wise equal.
+  /// This is identical to just comparing the pointers, with the exception that
+  /// for vectors, if only one of the constants has an `undef` element in some
+  /// lane, the constants still match.
+  bool isElementWiseEqual(Value *Y) const;
+
   /// Return true if this is a vector constant that includes any undefined
   /// elements.
   bool containsUndefElement() const;

Modified: llvm/trunk/lib/IR/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Constants.cpp?rev=369842&r1=369841&r2=369842&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Constants.cpp (original)
+++ llvm/trunk/lib/IR/Constants.cpp Fri Aug 23 23:49:51 2019
@@ -22,6 +22,7 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Operator.h"
+#include "llvm/IR/PatternMatch.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -250,6 +251,20 @@ bool Constant::isNaN() const {
   return true;
 }
 
+bool Constant::isElementWiseEqual(Value *Y) const {
+  // Are they fully identical?
+  if (this == Y)
+    return true;
+  // They may still be identical element-wise (if they have `undef`s).
+  auto *Cy = dyn_cast<Constant>(Y);
+  if (!Cy)
+    return false;
+  return PatternMatch::match(ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_EQ,
+                                                   const_cast<Constant *>(this),
+                                                   Cy),
+                             PatternMatch::m_One());
+}
+
 bool Constant::containsUndefElement() const {
   if (!getType()->isVectorTy())
     return false;

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp?rev=369842&r1=369841&r2=369842&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp Fri Aug 23 23:49:51 2019
@@ -1296,18 +1296,6 @@ tryToReuseConstantFromSelectInComparison
 
   // FIXME: are there any magic icmp predicate+constant pairs we must not touch?
 
-  auto ConstantsAreElementWiseEqual = [](Constant *Cx, Value *Y) {
-    // Are they fully identical?
-    if (Cx == Y)
-      return true;
-    // They may still be identical element-wise (if they have `undef`s).
-    auto *Cy = dyn_cast<Constant>(Y);
-    if (!Cy)
-      return false;
-    return match(ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_EQ, Cx, Cy),
-                 m_One());
-  };
-
   Value *SelVal0, *SelVal1; // We do not care which one is from where.
   match(&Sel, m_Select(m_Value(), m_Value(SelVal0), m_Value(SelVal1)));
   // At least one of these values we are selecting between must be a constant
@@ -1317,10 +1305,8 @@ tryToReuseConstantFromSelectInComparison
     return nullptr;
 
   // Does this constant C match any of the `select` values?
-  auto MatchesSelectValue = [ConstantsAreElementWiseEqual, SelVal0,
-                             SelVal1](Constant *C) {
-    return ConstantsAreElementWiseEqual(C, SelVal0) ||
-           ConstantsAreElementWiseEqual(C, SelVal1);
+  auto MatchesSelectValue = [SelVal0, SelVal1](Constant *C) {
+    return C->isElementWiseEqual(SelVal0) || C->isElementWiseEqual(SelVal1);
   };
 
   // If C0 *already* matches true/false value of select, we are done.




More information about the llvm-commits mailing list