[llvm] e471087 - [InstCombine] Use disjoint flag in add of or fold

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 5 06:06:52 PST 2023


Author: Nikita Popov
Date: 2023-12-05T15:06:40+01:00
New Revision: e4710872e98a931c6d5e89bc965b778746ead2c0

URL: https://github.com/llvm/llvm-project/commit/e4710872e98a931c6d5e89bc965b778746ead2c0
DIFF: https://github.com/llvm/llvm-project/commit/e4710872e98a931c6d5e89bc965b778746ead2c0.diff

LOG: [InstCombine] Use disjoint flag in add of or fold

Use disjoint instead of haveNoCommonBitsSet(), which is slightly
stronger in case the information used to infer disjoint has been
lost.

Introduce the m_DisjointOr() matcher to make handling cases like
this cleaner.

Added: 
    

Modified: 
    llvm/include/llvm/IR/PatternMatch.h
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/test/Transforms/InstCombine/add.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index f709a5ac52a41..80655d6c69eed 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -1239,6 +1239,29 @@ inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
   return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
 }
 
+template <typename LHS, typename RHS>
+struct DisjointOr_match {
+  LHS L;
+  RHS R;
+
+  DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
+
+  template <typename OpTy> bool match(OpTy *V) {
+    if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
+      assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
+      if (!PDI->isDisjoint())
+        return false;
+      return L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1));
+    }
+    return false;
+  }
+};
+
+template <typename LHS, typename RHS>
+inline DisjointOr_match<LHS, RHS> m_DisjointOr(const LHS &L, const RHS &R) {
+  return DisjointOr_match<LHS, RHS>(L, R);
+}
+
 //===----------------------------------------------------------------------===//
 // Class that matches a group of binary opcodes.
 //

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 427558f309056..c6d19c9cabc28 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -903,8 +903,7 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
 
   // (X | Op01C) + Op1C --> X + (Op01C + Op1C) iff the `or` is actually an `add`
   Constant *Op01C;
-  if (match(Op0, m_Or(m_Value(X), m_ImmConstant(Op01C))) &&
-      haveNoCommonBitsSet(X, Op01C, SQ.getWithInstruction(&Add)))
+  if (match(Op0, m_DisjointOr(m_Value(X), m_ImmConstant(Op01C))))
     return BinaryOperator::CreateAdd(X, ConstantExpr::getAdd(Op01C, Op1C));
 
   // (X | C2) + C --> (X | C2) ^ C2 iff (C2 == -C)

diff  --git a/llvm/test/Transforms/InstCombine/add.ll b/llvm/test/Transforms/InstCombine/add.ll
index db9eafe998ebb..42558a6d98f8f 100644
--- a/llvm/test/Transforms/InstCombine/add.ll
+++ b/llvm/test/Transforms/InstCombine/add.ll
@@ -657,8 +657,8 @@ define <2 x i1> @test21vec(<2 x i32> %x) {
 define i32 @test22(i32 %V) {
 ; CHECK-LABEL: @test22(
 ; CHECK-NEXT:    switch i32 [[V:%.*]], label [[DEFAULT:%.*]] [
-; CHECK-NEXT:    i32 10, label [[LAB1:%.*]]
-; CHECK-NEXT:    i32 20, label [[LAB2:%.*]]
+; CHECK-NEXT:      i32 10, label [[LAB1:%.*]]
+; CHECK-NEXT:      i32 20, label [[LAB2:%.*]]
 ; CHECK-NEXT:    ]
 ; CHECK:       Default:
 ; CHECK-NEXT:    ret i32 123
@@ -1511,6 +1511,16 @@ define i8 @add_like_or_t2_extrause(i8 %x) {
   ret i8 %r
 }
 
+define i8 @add_like_or_disjoint(i8 %x) {
+; CHECK-LABEL: @add_like_or_disjoint(
+; CHECK-NEXT:    [[R:%.*]] = add i8 [[X:%.*]], 57
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %i1 = or disjoint i8 %x, 15
+  %r = add i8 %i1, 42
+  ret i8 %r
+}
+
 define i8 @add_and_xor(i8 %x, i8 %y) {
 ; CHECK-LABEL: @add_and_xor(
 ; CHECK-NEXT:    [[ADD:%.*]] = or i8 [[Y:%.*]], [[X:%.*]]


        


More information about the llvm-commits mailing list