[llvm] [SelectionDAG] Preserve poison for abs INT_MIN lowering (PR #183851)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Thu May 7 01:47:54 PDT 2026
================
@@ -12109,6 +12112,54 @@ SDValue DAGCombiner::visitABS(SDNode *N) {
return SDValue();
}
+SDValue DAGCombiner::visitABS_MIN_POISON(SDNode *N) {
+ SDValue N0 = N->getOperand(0);
+ EVT VT = N->getValueType(0);
+ SDLoc DL(N);
+
+ // fold (abs_min_poison c1) -> c2 (or poison if c1 == INT_MIN)
+ if (SDValue C =
+ DAG.FoldConstantArithmetic(ISD::ABS_MIN_POISON, DL, VT, {N0}))
+ return C;
+ // fold (abs_min_poison (abs_min_poison x)) -> (abs_min_poison x)
+ // fold (abs_min_poison (abs x)) -> (abs x)
+ if (ISD::isAbsOpcode(N0.getOpcode()))
+ return N0;
+ // fold (abs_min_poison (freeze (abs x))) -> (freeze (abs x))
+ // fold (abs_min_poison (freeze (abs_min_poison x))) -> (freeze (abs_min_poison x))
+ // Valid because: for x != INT_MIN both sides equal abs(x); for x == INT_MIN
+ // both forms produce a non-deterministic but well-defined value since freeze
+ // already consumed the poison.
+ if (N0.getOpcode() == ISD::FREEZE &&
+ ISD::isAbsOpcode(N0.getOperand(0).getOpcode()))
+ return N0;
----------------
RKSimon wrote:
You could merge this with the previous fold with:
```suggestion
if (ISD::isAbsOpcode(peekThroughFreeze(N0).getOpcode()))
return N0;
```
https://github.com/llvm/llvm-project/pull/183851
More information about the llvm-commits
mailing list