[PATCH] D130286: [PatternMatch][InstCombine] match a vector with constant expression element(s) as a constant expression

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 21 09:49:15 PDT 2022


spatel created this revision.
spatel added reviewers: craig.topper, nikic, vzakhari, RKSimon.
Herald added subscribers: StephenFan, lebedev.ri, mcrosier.
Herald added a reviewer: lebedev.ri.
Herald added a project: All.
spatel requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The InstCombine test is reduced from issue #56601 <https://github.com/llvm/llvm-project/issues/56601>. Without the more liberal match for ConstantExpr, we try to rearrange constants in Negator forever.

Alternatively, we could adjust the definition of m_ImmConstant to be more conservative, but that's probably a larger patch, and I don't see any downside to changing `m_ConstantExpr`. We never capture and modify a ConstantExpr; transforms just want to avoid it.


https://reviews.llvm.org/D130286

Files:
  llvm/include/llvm/IR/PatternMatch.h
  llvm/test/Transforms/InstCombine/sub-of-negatible.ll
  llvm/unittests/IR/PatternMatch.cpp


Index: llvm/unittests/IR/PatternMatch.cpp
===================================================================
--- llvm/unittests/IR/PatternMatch.cpp
+++ llvm/unittests/IR/PatternMatch.cpp
@@ -1798,8 +1798,10 @@
   PoisonValue *P = PoisonValue::get(VecTy);
   Constant *V = ConstantExpr::getInsertElement(P, S, IRB.getInt32(0));
 
+  // The match succeeds on a constant that is a constant expression itself
+  // or a constant that contains a constant expression.
   EXPECT_TRUE(match(S, m_ConstantExpr()));
-  EXPECT_FALSE(match(V, m_ConstantExpr()));
+  EXPECT_TRUE(match(V, m_ConstantExpr()));
 }
 
 } // anonymous namespace.
Index: llvm/test/Transforms/InstCombine/sub-of-negatible.ll
===================================================================
--- llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -1426,5 +1426,28 @@
   br label %if.end
 }
 
+; This would infinite loop because we failed to match a
+; vector constant with constant expression elements as
+; a constant expression.
+
+ at g = external hidden global [1 x [1 x double]]
+
+define <1 x i64> @PR56601(<1 x i64> %x, <1 x i64> %y) {
+; CHECK-LABEL: @PR56601(
+; CHECK-NEXT:    [[M1:%.*]] = mul nsw <1 x i64> [[X:%.*]], <i64 42>
+; CHECK-NEXT:    [[M2:%.*]] = mul nsw <1 x i64> [[Y:%.*]], <i64 12>
+; CHECK-NEXT:    [[A1:%.*]] = add <1 x i64> [[M1]], <i64 add (i64 ptrtoint (ptr @g to i64), i64 -4)>
+; CHECK-NEXT:    [[A2:%.*]] = add <1 x i64> [[M2]], <i64 add (i64 ptrtoint (ptr @g to i64), i64 -3)>
+; CHECK-NEXT:    [[R:%.*]] = sub <1 x i64> [[A1]], [[A2]]
+; CHECK-NEXT:    ret <1 x i64> [[R]]
+;
+  %m1 = mul nsw <1 x i64> %x, <i64 42>
+  %m2 = mul nsw <1 x i64> %y, <i64 12>
+  %a1 = add <1 x i64> %m1, <i64 add (i64 ptrtoint (ptr @g to i64), i64 -4)>
+  %a2 = add <1 x i64> %m2, <i64 add (i64 ptrtoint (ptr @g to i64), i64 -3)>
+  %r = sub <1 x i64> %a1, %a2
+  ret <1 x i64> %r
+}
+
 ; CHECK: !0 = !{!"branch_weights", i32 40, i32 1}
 !0 = !{!"branch_weights", i32 40, i32 1}
Index: llvm/include/llvm/IR/PatternMatch.h
===================================================================
--- llvm/include/llvm/IR/PatternMatch.h
+++ llvm/include/llvm/IR/PatternMatch.h
@@ -153,10 +153,16 @@
   return class_match<ConstantFP>();
 }
 
-/// Match an arbitrary ConstantExpr and ignore it.
-inline class_match<ConstantExpr> m_ConstantExpr() {
-  return class_match<ConstantExpr>();
-}
+struct constantexpr_match {
+  template <typename ITy> bool match(ITy *V) {
+    auto *C = dyn_cast<Constant>(V);
+    return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
+  }
+};
+
+/// Match a constant expression or a constant that contains a constant
+/// expression.
+inline constantexpr_match m_ConstantExpr() { return constantexpr_match(); }
 
 /// Match an arbitrary basic block value and ignore it.
 inline class_match<BasicBlock> m_BasicBlock() {
@@ -741,14 +747,14 @@
 
 /// Match an arbitrary immediate Constant and ignore it.
 inline match_combine_and<class_match<Constant>,
-                         match_unless<class_match<ConstantExpr>>>
+                         match_unless<constantexpr_match>>
 m_ImmConstant() {
   return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr()));
 }
 
 /// Match an immediate Constant, capturing the value if we match.
 inline match_combine_and<bind_ty<Constant>,
-                         match_unless<class_match<ConstantExpr>>>
+                         match_unless<constantexpr_match>>
 m_ImmConstant(Constant *&C) {
   return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr()));
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130286.446540.patch
Type: text/x-patch
Size: 3583 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220721/824cfb2c/attachment.bin>


More information about the llvm-commits mailing list