[llvm] e6375ca - [InstCombine] Fix potentially buggy code in `((%x & C) == 0) --> %x u< (-C)` transform

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 9 02:44:21 PST 2023


Author: Noah Goldstein
Date: 2023-01-09T11:44:11+01:00
New Revision: e6375ca6dc5ba263ad3d9b6d779c5cf5b0f2e9ba

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

LOG: [InstCombine] Fix potentially buggy code in `((%x & C) == 0) --> %x u< (-C)` transform

While demanded bits constant shrinking appears to prevent this in
practice right now, it is principally possible for C2 to have
set bits that are known not-needed (zeroable). See: D140858

`+` will overflow here, `|` will get the right logic.

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

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index b0ebfcf91966..587700216008 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1722,7 +1722,7 @@ Instruction *InstCombinerImpl::foldICmpAndConstConst(ICmpInst &Cmp,
     APInt NewC2 = *C2;
     KnownBits Know = computeKnownBits(And->getOperand(0), 0, And);
     // Set high zeros of C2 to allow matching negated power-of-2.
-    NewC2 = *C2 + APInt::getHighBitsSet(C2->getBitWidth(),
+    NewC2 = *C2 | APInt::getHighBitsSet(C2->getBitWidth(),
                                         Know.countMinLeadingZeros());
 
     // Restrict this fold only for single-use 'and' (PR10267).


        


More information about the llvm-commits mailing list