[llvm] [SelectionDAG] Preserver poison for abs INT_MIN lowering (PR #183851)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 3 12:03:19 PST 2026


================
@@ -11942,6 +11946,29 @@ 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 (N0.getOpcode() == ISD::ABS_MIN_POISON || N0.getOpcode() == ISD::ABS)
+    return N0;
+  // fold (abs_min_poison x) -> x iff not-negative
+  if (DAG.SignBitIsZero(N0))
+    return N0;
+
+  if (SDValue ABD = foldABSToABD(N, DL))
+    return ABD;
----------------
nikic wrote:

This is missing the "fold (abs (sign_extend_inreg x)) -> (zero_extend (abs (truncate x)))" fold, is that intentional?

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


More information about the llvm-commits mailing list