Skip to content

[LLVM][SelectionDAG] Align poison/undef binop folds with IR. #149334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 61 additions & 20 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7843,69 +7843,110 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
}
}

// Perform trivial constant folding.
if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
return SV;
if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
switch (Opcode) {
case ISD::XOR:
case ISD::ADD:
case ISD::PTRADD:
case ISD::SUB:
case ISD::SIGN_EXTEND_INREG:
case ISD::UDIV:
case ISD::SDIV:
case ISD::UREM:
case ISD::SREM:
case ISD::MUL:
case ISD::AND:
case ISD::SSUBSAT:
case ISD::USUBSAT:
case ISD::UMIN:
case ISD::OR:
case ISD::SADDSAT:
case ISD::UADDSAT:
case ISD::UMAX:
case ISD::SMAX:
case ISD::SMIN:
// fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
return N2.getOpcode() == ISD::POISON ? N2 : N1;
}
}

// Canonicalize an UNDEF to the RHS, even over a constant.
if (N1.isUndef()) {
if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
if (TLI->isCommutativeBinOp(Opcode)) {
std::swap(N1, N2);
} else {
switch (Opcode) {
case ISD::PTRADD:
case ISD::SUB:
// fold op(undef, arg2) -> undef, fold op(poison, arg2) ->poison.
return N1.getOpcode() == ISD::POISON ? getPOISON(VT) : getUNDEF(VT);
// fold op(undef, non_undef_arg2) -> undef.
return N1;
case ISD::SIGN_EXTEND_INREG:
case ISD::UDIV:
case ISD::SDIV:
case ISD::UREM:
case ISD::SREM:
case ISD::SSUBSAT:
case ISD::USUBSAT:
// fold op(undef, arg2) -> 0, fold op(poison, arg2) -> poison.
return N1.getOpcode() == ISD::POISON ? getPOISON(VT)
: getConstant(0, DL, VT);
// fold op(undef, non_undef_arg2) -> 0.
return getConstant(0, DL, VT);
}
}
}

// Fold a bunch of operators when the RHS is undef.
if (N2.isUndef()) {
if (N2.getOpcode() == ISD::UNDEF) {
switch (Opcode) {
case ISD::XOR:
if (N1.isUndef())
if (N1.getOpcode() == ISD::UNDEF)
// Handle undef ^ undef -> 0 special case. This is a common
// idiom (misuse).
return getConstant(0, DL, VT);
[[fallthrough]];
case ISD::ADD:
case ISD::PTRADD:
case ISD::SUB:
// fold op(arg1, undef) -> undef.
return N2;
case ISD::UDIV:
case ISD::SDIV:
case ISD::UREM:
case ISD::SREM:
// fold op(arg1, undef) -> undef, fold op(arg1, poison) -> poison.
return N2.getOpcode() == ISD::POISON ? getPOISON(VT) : getUNDEF(VT);
// fold op(arg1, undef) -> poison.
return getPOISON(VT);
case ISD::MUL:
case ISD::AND:
case ISD::SSUBSAT:
case ISD::USUBSAT:
// fold op(arg1, undef) -> 0, fold op(arg1, poison) -> poison.
return N2.getOpcode() == ISD::POISON ? getPOISON(VT)
: getConstant(0, DL, VT);
case ISD::UMIN:
// fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
case ISD::OR:
case ISD::SADDSAT:
case ISD::UADDSAT:
// fold op(arg1, undef) -> an all-ones constant, fold op(arg1, poison) ->
// poison.
return N2.getOpcode() == ISD::POISON ? getPOISON(VT)
: getAllOnesConstant(DL, VT);
case ISD::UMAX:
// fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
case ISD::SMAX:
// fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
return N1.getOpcode() == ISD::UNDEF
? N2
: getConstant(
APInt::getSignedMaxValue(VT.getScalarSizeInBits()), DL,
VT);
case ISD::SMIN:
// fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
return N1.getOpcode() == ISD::UNDEF
? N2
: getConstant(
APInt::getSignedMinValue(VT.getScalarSizeInBits()), DL,
VT);
}
}

// Perform trivial constant folding.
if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
return SV;

// Memoize this node if possible.
SDNode *N;
SDVTList VTs = getVTList(VT);
Expand Down
1 change: 0 additions & 1 deletion llvm/test/CodeGen/AArch64/combine-and-like.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
define i32 @f(i32 %a0) {
; CHECK-LABEL: f:
; CHECK: // %bb.0:
; CHECK-NEXT: mov w0, wzr
; CHECK-NEXT: ret
%1 = lshr i32 %a0, 2147483647
%2 = add i32 %1, 2147483647
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AMDGPU/saddsat.ll
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ define <3 x i16> @v_saddsat_v3i16(<3 x i16> %lhs, <3 x i16> %rhs) {
; GFX6-NEXT: v_and_b32_e32 v0, 0xffff, v0
; GFX6-NEXT: v_med3_i32 v3, v2, s4, v4
; GFX6-NEXT: v_or_b32_e32 v0, v0, v1
; GFX6-NEXT: v_or_b32_e32 v2, 0xffff0000, v3
; GFX6-NEXT: v_and_b32_e32 v2, 0xffff, v3
; GFX6-NEXT: v_alignbit_b32 v1, v3, v1, 16
; GFX6-NEXT: s_setpc_b64 s[30:31]
;
Expand Down
5 changes: 2 additions & 3 deletions llvm/test/CodeGen/AMDGPU/uaddsat.ll
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,9 @@ define <3 x i16> @v_uaddsat_v3i16(<3 x i16> %lhs, <3 x i16> %rhs) {
; GFX6-NEXT: v_add_i32_e32 v2, vcc, v2, v5
; GFX6-NEXT: v_min_u32_e32 v0, 0xffff, v0
; GFX6-NEXT: v_lshlrev_b32_e32 v1, 16, v1
; GFX6-NEXT: v_min_u32_e32 v3, 0xffff, v2
; GFX6-NEXT: v_min_u32_e32 v2, 0xffff, v2
; GFX6-NEXT: v_or_b32_e32 v0, v0, v1
; GFX6-NEXT: v_or_b32_e32 v2, 0xffff0000, v3
; GFX6-NEXT: v_alignbit_b32 v1, v3, v1, 16
; GFX6-NEXT: v_alignbit_b32 v1, v2, v1, 16
; GFX6-NEXT: s_setpc_b64 s[30:31]
;
; GFX8-LABEL: v_uaddsat_v3i16:
Expand Down
Loading