[llvm] [ConstantRange] Improve ConstantRange::binaryXor (PR #80146)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 7 20:02:51 PST 2024


================
@@ -1467,7 +1467,22 @@ ConstantRange ConstantRange::binaryXor(const ConstantRange &Other) const {
   if (isSingleElement() && getSingleElement()->isAllOnes())
     return Other.binaryNot();
 
-  return fromKnownBits(toKnownBits() ^ Other.toKnownBits(), /*IsSigned*/false);
+  KnownBits LHSKnown = toKnownBits();
+  KnownBits RHSKnown = Other.toKnownBits();
+  KnownBits Known = LHSKnown ^ RHSKnown;
+  ConstantRange CR = fromKnownBits(Known, /*IsSigned*/ false);
+  // Typically the following code doesn't improve the result if BW = 1.
+  if (getBitWidth() == 1)
+    return CR;
+
+  // If LHS is known to be the subset of RHS, treat LHS ^ RHS as RHS -nuw/nsw
+  // LHS. If RHS is known to be the subset of LHS, treat LHS ^ RHS as LHS
+  // -nuw/nsw RHS.
+  if ((~LHSKnown.Zero).isSubsetOf(RHSKnown.One))
----------------
topperc wrote:

Does RHSKnown.One here need to be ~RHSKnown.Zero? Bits that aren't set in One might still be a 1, we just don't know for sure.

https://github.com/llvm/llvm-project/pull/80146


More information about the llvm-commits mailing list