[llvm] [InstCombine] Change (add x, c) to (xor x, c) (PR #75129)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 05:53:19 PST 2023


================
@@ -552,6 +552,14 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
     if (DemandedFromOps.isSubsetOf(LHSKnown.Zero))
       return I->getOperand(1);
 
+    // (add X, C) --> (xor X, C) IFF C is equal to the top bit of the DemandMask
+    {
+      const APInt *C;
+      if (match(I->getOperand(1), m_APInt(C)) &&
+          C->isOneBitSet(DemandedMask.getActiveBits() - 1))
+        return Builder.CreateXor(I->getOperand(0), ConstantInt::get(VTy, *C));
----------------
dtcxzyw wrote:

```
Instruction *Xor = BinaryOperator::CreateXor(I->getOperand(0), ConstantInt::get(VTy, *C));
return InsertNewInstWith(Xor, I->getIterator());
```
I guess it will fix the assertion failure.



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


More information about the llvm-commits mailing list