[DAGCombiner] Fix abs(add) to abdu miscompile in foldABSToABD (#196782)
The abs(add(x, y)) → abdu(x, -y) fold added in #186659 is incorrect when
both operands are known non-negative and their sum does not overflow
signed.
When both x and y are non-negative and `x + y < 2^31`, `abs(x + y) = x +
y`, but `abdu(x, -y) = 2^32 - y - x ≠ x + y`.
For example, `abs(add(0, 1)) = 1`, but `abdu(0, -1) = 0xFFFFFFFF`.
Related: #185467 #175801
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index bf4d4cc..3d65c82 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -12025,10 +12025,8 @@
return CreateZextedAbd(ISD::ABDS);
// fold (abs (sub x, y)) -> abdu(x, y)
- // fold (abs (add x, -y)) -> abdu(x, y)
bool Op1SignBitIsOne = DAG.computeKnownBits(Op1).isNegative();
- bool AbsOpWillNUW = DAG.SignBitIsZero(Op0) &&
- (IsAdd ? DAG.SignBitIsZero(Op1) : Op1SignBitIsOne);
+ bool AbsOpWillNUW = !IsAdd && DAG.SignBitIsZero(Op0) && Op1SignBitIsOne;
if (hasOperation(ISD::ABDU, VT) && AbsOpWillNUW)
return CreateZextedAbd(ISD::ABDU);
diff --git a/llvm/test/CodeGen/X86/abds.ll b/llvm/test/CodeGen/X86/abds.ll
index 5948af5..a3056e9 100644
--- a/llvm/test/CodeGen/X86/abds.ll
+++ b/llvm/test/CodeGen/X86/abds.ll
@@ -1389,6 +1389,32 @@
ret i32 %absx
}
+define i32 @abs_add_known_positive(i32 %a) {
+; X86-LABEL: abs_add_known_positive:
+; X86: # %bb.0:
+; X86-NEXT: movl $2147483647, %ecx # imm = 0x7FFFFFFF
+; X86-NEXT: andl {{[0-9]+}}(%esp), %ecx
+; X86-NEXT: incl %ecx
+; X86-NEXT: movl %ecx, %eax
+; X86-NEXT: negl %eax
+; X86-NEXT: cmovsl %ecx, %eax
+; X86-NEXT: retl
+;
+; X64-LABEL: abs_add_known_positive:
+; X64: # %bb.0:
+; X64-NEXT: # kill: def $edi killed $edi def $rdi
+; X64-NEXT: andl $2147483647, %edi # imm = 0x7FFFFFFF
+; X64-NEXT: leal 1(%rdi), %ecx
+; X64-NEXT: movl %ecx, %eax
+; X64-NEXT: negl %eax
+; X64-NEXT: cmovsl %ecx, %eax
+; X64-NEXT: retq
+ %x = and i32 %a, 2147483647
+ %add = add i32 %x, 1
+ %abs = call i32 @llvm.abs.i32(i32 %add, i1 false)
+ ret i32 %abs
+}
+
declare i8 @llvm.abs.i8(i8, i1)
declare i16 @llvm.abs.i16(i16, i1)
declare i32 @llvm.abs.i32(i32, i1)