[InstCombine] Fix potentially buggy code in `((%x & C) == 0) --> %x u< (-C)` transform
authorNoah Goldstein <goldstein.w.n@gmail.com>
Mon, 9 Jan 2023 10:38:36 +0000 (11:38 +0100)
committerNikita Popov <npopov@redhat.com>
Mon, 9 Jan 2023 10:44:11 +0000 (11:44 +0100)
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

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

index b0ebfcf..5877002 100644 (file)
@@ -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).