[llvm] f52dd92 - [InstCombine] Fix bug when folding x + (x | -x) to x & (x - 1)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 18 10:16:47 PDT 2022


Author: Marc Auberer
Date: 2022-09-18T13:16:12-04:00
New Revision: f52dd920d400e35d0825ac418dd56f6419312caa

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

LOG: [InstCombine] Fix bug when folding x + (x | -x) to x & (x - 1)

Addresses concern: https://reviews.llvm.org/rG09cdddea0c4d284c2c22f5dfade40a60850c5ea7

There was a copy/paste mistake in the code. Updated code and test ref.

Differential Revision: https://reviews.llvm.org/D134135

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/test/Transforms/InstCombine/add_or_sub.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 0965a5761d07..af72e8af75ef 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1404,7 +1404,7 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
                                                       m_Deferred(A)))))) {
     Value *Add =
         Builder.CreateAdd(A, Constant::getAllOnesValue(A->getType()), "",
-                          I.hasNoSignedWrap(), I.hasNoSignedWrap());
+                          I.hasNoUnsignedWrap(), I.hasNoSignedWrap());
     return BinaryOperator::CreateAnd(Add, A);
   }
 

diff  --git a/llvm/test/Transforms/InstCombine/add_or_sub.ll b/llvm/test/Transforms/InstCombine/add_or_sub.ll
index fb6183e853e1..5f1234618b9a 100644
--- a/llvm/test/Transforms/InstCombine/add_or_sub.ll
+++ b/llvm/test/Transforms/InstCombine/add_or_sub.ll
@@ -7,9 +7,7 @@ declare void @use2(i2)
 
 define i32 @add_or_sub_comb_i32_commuted1_nuw(i32 %x) {
 ; CHECK-LABEL: @add_or_sub_comb_i32_commuted1_nuw(
-; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X:%.*]], -1
-; CHECK-NEXT:    [[ADD:%.*]] = and i32 [[TMP1]], [[X]]
-; CHECK-NEXT:    ret i32 [[ADD]]
+; CHECK-NEXT:    ret i32 [[X:%.*]]
 ;
   %sub = sub i32 0, %x
   %or = or i32 %sub, %x
@@ -20,7 +18,9 @@ define i32 @add_or_sub_comb_i32_commuted1_nuw(i32 %x) {
 define i8 @add_or_sub_comb_i8_commuted2_nsw(i8 %p) {
 ; CHECK-LABEL: @add_or_sub_comb_i8_commuted2_nsw(
 ; CHECK-NEXT:    [[X:%.*]] = mul i8 [[P:%.*]], [[P]]
-; CHECK-NEXT:    ret i8 [[X]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add nsw i8 [[X]], -1
+; CHECK-NEXT:    [[ADD:%.*]] = and i8 [[TMP1]], [[X]]
+; CHECK-NEXT:    ret i8 [[ADD]]
 ;
   %x = mul i8 %p, %p ; thwart complexity-based canonicalization
   %sub = sub i8 0, %x


        


More information about the llvm-commits mailing list