[llvm] 6e6fe27 - [ValueTracking] peek through extends in haveNoCommonBitsSet (2nd try)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 14:03:39 PST 2022


Author: Sanjay Patel
Date: 2022-12-13T16:57:45-05:00
New Revision: 6e6fe2768934d0aabc720d05bcf3df55b1f1733d

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

LOG: [ValueTracking] peek through extends in haveNoCommonBitsSet (2nd try)

The 1st try was not clean because a portion of the code diff
made it into the pre-commit patch to add tests. This should
be the same end result without the muddied code diff.

Original commit message:

In cases with matching extends, this allows changing an 'add'
into an 'or' and narrowing the 'or' which then simplifies to
a constant.

In cases with opposite extends, we just convert to an 'or'
currently, but that could be reduced too.

https://alive2.llvm.org/ce/z/fTHzdb

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/InstCombine/add.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 584e64542d8fd..c59cf3fc43145 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -270,6 +270,15 @@ bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
       match(LHS, m_c_Xor(m_c_And(m_Specific(RHS), m_Value(Y)), m_Deferred(Y))))
     return true;
 
+  // Peek through extends to find a 'not' of the other side:
+  // (ext Y) op ext(~Y)
+  // (ext ~Y) op ext(Y)
+  if ((match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
+       match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))) ||
+      (match(RHS, m_ZExtOrSExt(m_Value(Y))) &&
+       match(LHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))))
+    return true;
+
   // Look for: (A & B) op ~(A | B)
   {
     Value *A, *B;

diff  --git a/llvm/test/Transforms/InstCombine/add.ll b/llvm/test/Transforms/InstCombine/add.ll
index 9a9e6ee13bcef..767944b412ecf 100644
--- a/llvm/test/Transforms/InstCombine/add.ll
+++ b/llvm/test/Transforms/InstCombine/add.ll
@@ -2348,11 +2348,7 @@ define <vscale x 1 x i32> @add_to_or_scalable(<vscale x 1 x i32> %in) {
 
 define i5 @zext_zext_not(i3 %x) {
 ; CHECK-LABEL: @zext_zext_not(
-; CHECK-NEXT:    [[ZX:%.*]] = zext i3 [[X:%.*]] to i5
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i3 [[X]], -1
-; CHECK-NEXT:    [[ZNOTX:%.*]] = zext i3 [[NOTX]] to i5
-; CHECK-NEXT:    [[R:%.*]] = add nuw nsw i5 [[ZX]], [[ZNOTX]]
-; CHECK-NEXT:    ret i5 [[R]]
+; CHECK-NEXT:    ret i5 7
 ;
   %zx = zext i3 %x to i5
   %notx = xor i3 %x, -1
@@ -2363,11 +2359,7 @@ define i5 @zext_zext_not(i3 %x) {
 
 define <2 x i5> @zext_zext_not_commute(<2 x i3> %x) {
 ; CHECK-LABEL: @zext_zext_not_commute(
-; CHECK-NEXT:    [[ZX:%.*]] = zext <2 x i3> [[X:%.*]] to <2 x i5>
-; CHECK-NEXT:    [[NOTX:%.*]] = xor <2 x i3> [[X]], <i3 -1, i3 poison>
-; CHECK-NEXT:    [[ZNOTX:%.*]] = zext <2 x i3> [[NOTX]] to <2 x i5>
-; CHECK-NEXT:    [[R:%.*]] = add nuw nsw <2 x i5> [[ZNOTX]], [[ZX]]
-; CHECK-NEXT:    ret <2 x i5> [[R]]
+; CHECK-NEXT:    ret <2 x i5> <i5 7, i5 7>
 ;
   %zx = zext <2 x i3> %x to <2 x i5>
   %notx = xor <2 x i3> %x, <i3 -1, i3 poison>
@@ -2378,11 +2370,7 @@ define <2 x i5> @zext_zext_not_commute(<2 x i3> %x) {
 
 define i9 @sext_sext_not(i3 %x) {
 ; CHECK-LABEL: @sext_sext_not(
-; CHECK-NEXT:    [[SX:%.*]] = sext i3 [[X:%.*]] to i9
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i3 [[X]], -1
-; CHECK-NEXT:    [[SNOTX:%.*]] = sext i3 [[NOTX]] to i9
-; CHECK-NEXT:    [[R:%.*]] = add nsw i9 [[SX]], [[SNOTX]]
-; CHECK-NEXT:    ret i9 [[R]]
+; CHECK-NEXT:    ret i9 -1
 ;
   %sx = sext i3 %x to i9
   %notx = xor i3 %x, -1
@@ -2395,10 +2383,7 @@ define i8 @sext_sext_not_commute(i3 %x) {
 ; CHECK-LABEL: @sext_sext_not_commute(
 ; CHECK-NEXT:    [[SX:%.*]] = sext i3 [[X:%.*]] to i8
 ; CHECK-NEXT:    call void @use(i8 [[SX]])
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i3 [[X]], -1
-; CHECK-NEXT:    [[SNOTX:%.*]] = sext i3 [[NOTX]] to i8
-; CHECK-NEXT:    [[R:%.*]] = add nsw i8 [[SNOTX]], [[SX]]
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 -1
 ;
 
   %sx = sext i3 %x to i8
@@ -2414,7 +2399,7 @@ define i5 @zext_sext_not(i4 %x) {
 ; CHECK-NEXT:    [[ZX:%.*]] = zext i4 [[X:%.*]] to i5
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i4 [[X]], -1
 ; CHECK-NEXT:    [[SNOTX:%.*]] = sext i4 [[NOTX]] to i5
-; CHECK-NEXT:    [[R:%.*]] = add i5 [[ZX]], [[SNOTX]]
+; CHECK-NEXT:    [[R:%.*]] = or i5 [[ZX]], [[SNOTX]]
 ; CHECK-NEXT:    ret i5 [[R]]
 ;
   %zx = zext i4 %x to i5
@@ -2431,7 +2416,7 @@ define i8 @zext_sext_not_commute(i4 %x) {
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i4 [[X]], -1
 ; CHECK-NEXT:    [[SNOTX:%.*]] = sext i4 [[NOTX]] to i8
 ; CHECK-NEXT:    call void @use(i8 [[SNOTX]])
-; CHECK-NEXT:    [[R:%.*]] = add nsw i8 [[SNOTX]], [[ZX]]
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[SNOTX]], [[ZX]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %zx = zext i4 %x to i8
@@ -2448,7 +2433,7 @@ define i9 @sext_zext_not(i4 %x) {
 ; CHECK-NEXT:    [[SX:%.*]] = sext i4 [[X:%.*]] to i9
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i4 [[X]], -1
 ; CHECK-NEXT:    [[ZNOTX:%.*]] = zext i4 [[NOTX]] to i9
-; CHECK-NEXT:    [[R:%.*]] = add nsw i9 [[SX]], [[ZNOTX]]
+; CHECK-NEXT:    [[R:%.*]] = or i9 [[SX]], [[ZNOTX]]
 ; CHECK-NEXT:    ret i9 [[R]]
 ;
   %sx = sext i4 %x to i9
@@ -2463,7 +2448,7 @@ define i9 @sext_zext_not_commute(i4 %x) {
 ; CHECK-NEXT:    [[SX:%.*]] = sext i4 [[X:%.*]] to i9
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i4 [[X]], -1
 ; CHECK-NEXT:    [[ZNOTX:%.*]] = zext i4 [[NOTX]] to i9
-; CHECK-NEXT:    [[R:%.*]] = add nsw i9 [[ZNOTX]], [[SX]]
+; CHECK-NEXT:    [[R:%.*]] = or i9 [[ZNOTX]], [[SX]]
 ; CHECK-NEXT:    ret i9 [[R]]
 ;
   %sx = sext i4 %x to i9


        


More information about the llvm-commits mailing list