[PATCH] D105013: [InstCombine] try to fold the expression "(A & ~B) + B" to "A | B"

Yifeng Dong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 28 23:58:36 PDT 2021


dongAxis1944 updated this revision to Diff 355120.
dongAxis1944 added a comment.

update ut


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D105013/new/

https://reviews.llvm.org/D105013

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Analysis/ValueTracking/known-non-common-bits.ll


Index: llvm/test/Analysis/ValueTracking/known-non-common-bits.ll
===================================================================
--- /dev/null
+++ llvm/test/Analysis/ValueTracking/known-non-common-bits.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; Try to fold (A & ~B) + B to A | B
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+define dso_local i32 @_Z11and_to_xor1ii(i32 %0, i32 %1) {
+; CHECK-LABEL: @_Z11and_to_xor1ii(
+; CHECK-NEXT:    [[TMP3:%.*]] = or i32 [[TMP0:%.*]], [[TMP1:%.*]]
+; CHECK-NEXT:    ret i32 [[TMP3]]
+;
+  %3 = xor i32 %1, -1
+  %4 = and i32 %3, %0
+  %5 = add nsw i32 %4, %1
+  ret i32 %5
+}
+
+define dso_local i32 @_Z11and_to_xor1ii2(i32 %0, i32 %1) {
+; CHECK-LABEL: @_Z11and_to_xor1ii2(
+; CHECK-NEXT:    [[TMP3:%.*]] = or i32 [[TMP0:%.*]], [[TMP1:%.*]]
+; CHECK-NEXT:    ret i32 [[TMP3]]
+;
+  %3 = xor i32 %1, -1
+  %4 = and i32 %3, %0
+  %5 = or i32 %4, %1
+  ret i32 %5
+}
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -270,6 +270,12 @@
   if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
       match(LHS, m_c_And(m_Specific(M), m_Value())))
     return true;
+
+  // Look for a pattern:(A & ~B) op B
+  if (match(LHS, m_c_And(m_Not(m_Specific(RHS)), m_Value())) ||
+      match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())))
+    return true;
+
   IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
   KnownBits LHSKnown(IT->getBitWidth());
   KnownBits RHSKnown(IT->getBitWidth());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105013.355120.patch
Type: text/x-patch
Size: 1657 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210629/5dec636d/attachment.bin>


More information about the llvm-commits mailing list