[llvm] [SelectionDAG] fold (not (sub Y, X)) -> (add X, ~Y) (PR #147825)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 9 13:50:01 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-selectiondag
Author: AZero13 (AZero13)
<details>
<summary>Changes</summary>
This replaces (not (neg x)) -> (add X, -1) because that is covered by this.
---
Full diff: https://github.com/llvm/llvm-project/pull/147825.diff
1 Files Affected:
- (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+7-7)
``````````diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 9ffdda28f7899..90c673dd7e83b 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -9979,13 +9979,13 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
}
}
- // fold (not (neg x)) -> (add X, -1)
- // FIXME: This can be generalized to (not (sub Y, X)) -> (add X, ~Y) if
- // Y is a constant or the subtract has a single use.
- if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB &&
- isNullConstant(N0.getOperand(0))) {
- return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(1),
- DAG.getAllOnesConstant(DL, VT));
+ // fold (not (sub Y, X)) -> (add X, ~Y) is subtract is a single use
+ // or Y is a constant.
+ if (isAllOnesOrAllOnesSplat(N1) && N0.getOpcode() == ISD::SUB) {
+ ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N0.getOperand(0));
+ if (Const || N0.hasOneUse())
+ return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(1),
+ DAG.getNOT(DL, N0.getOperand(0), VT));
}
// fold (not (add X, -1)) -> (neg X)
``````````
</details>
https://github.com/llvm/llvm-project/pull/147825
More information about the llvm-commits
mailing list