Skip to content

Commit 84447c0

Browse files
committed
[DAG] Add SelectionDAG::isADDLike helper. NFC.
Make the DAGCombine helper global so we can more easily reuse it.
1 parent c61d198 commit 84447c0

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,12 @@ class SelectionDAG {
21092109
bool ConsiderFlags = true,
21102110
unsigned Depth = 0) const;
21112111

2112+
/// Return true if the specified operand is an ISD::OR or ISD::XOR node
2113+
/// that can be treated as an ISD::ADD node.
2114+
/// or(x,y) == add(x,y) iff haveNoCommonBitsSet(x,y)
2115+
/// xor(x,y) == add(x,y) iff isMinSignedConstant(y)
2116+
bool isADDLike(SDValue Op) const;
2117+
21122118
/// Return true if the specified operand is an ISD::ADD with a ConstantSDNode
21132119
/// on the right-hand side, or if it is an ISD::OR with a ConstantSDNode that
21142120
/// is guaranteed to have the same semantics as an ADD. This handles the

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,15 +2678,6 @@ static SDValue foldAddSubOfSignBit(SDNode *N, SelectionDAG &DAG) {
26782678
return SDValue();
26792679
}
26802680

2681-
static bool isADDLike(SDValue V, const SelectionDAG &DAG) {
2682-
unsigned Opcode = V.getOpcode();
2683-
if (Opcode == ISD::OR)
2684-
return DAG.haveNoCommonBitsSet(V.getOperand(0), V.getOperand(1));
2685-
if (Opcode == ISD::XOR)
2686-
return isMinSignedConstant(V.getOperand(1));
2687-
return false;
2688-
}
2689-
26902681
static bool
26912682
areBitwiseNotOfEachother(SDValue Op0, SDValue Op1) {
26922683
return (isBitwiseNot(Op0) && Op0.getOperand(0) == Op1) ||
@@ -2768,7 +2759,7 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
27682759
// iff (or x, c0) is equivalent to (add x, c0).
27692760
// Fold (add (xor x, c0), c1) -> (add x, (c0 + c1))
27702761
// iff (xor x, c0) is equivalent to (add x, c0).
2771-
if (isADDLike(N0, DAG)) {
2762+
if (DAG.isADDLike(N0)) {
27722763
SDValue N01 = N0.getOperand(1);
27732764
if (SDValue Add = DAG.FoldConstantArithmetic(ISD::ADD, DL, VT, {N1, N01}))
27742765
return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), Add);
@@ -2789,7 +2780,7 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
27892780
// Do this optimization only when adding c does not introduce instructions
27902781
// for adding carries.
27912782
auto ReassociateAddOr = [&](SDValue N0, SDValue N1) {
2792-
if (isADDLike(N0, DAG) && N0.hasOneUse() &&
2783+
if (DAG.isADDLike(N0) && N0.hasOneUse() &&
27932784
isConstantOrConstantVector(N0.getOperand(1), /* NoOpaque */ true)) {
27942785
// If N0's type does not split or is a sign mask, it does not introduce
27952786
// add carry.

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5007,6 +5007,15 @@ bool SelectionDAG::canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts,
50075007
return true;
50085008
}
50095009

5010+
bool SelectionDAG::isADDLike(SDValue Op) const {
5011+
unsigned Opcode = Op.getOpcode();
5012+
if (Opcode == ISD::OR)
5013+
return haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5014+
if (Opcode == ISD::XOR)
5015+
return isMinSignedConstant(Op.getOperand(1));
5016+
return false;
5017+
}
5018+
50105019
bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
50115020
if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
50125021
!isa<ConstantSDNode>(Op.getOperand(1)))

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,28 +2500,14 @@ bool X86DAGToDAGISel::matchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
25002500
return false;
25012501
}
25022502

2503-
case ISD::ADD:
2504-
if (!matchAdd(N, AM, Depth))
2505-
return false;
2506-
break;
2507-
25082503
case ISD::OR:
2509-
// We want to look through a transform in InstCombine and DAGCombiner that
2510-
// turns 'add' into 'or', so we can treat this 'or' exactly like an 'add'.
2511-
// Example: (or (and x, 1), (shl y, 3)) --> (add (and x, 1), (shl y, 3))
2512-
// An 'lea' can then be used to match the shift (multiply) and add:
2513-
// and $1, %esi
2514-
// lea (%rsi, %rdi, 8), %rax
2515-
if (CurDAG->haveNoCommonBitsSet(N.getOperand(0), N.getOperand(1)) &&
2516-
!matchAdd(N, AM, Depth))
2517-
return false;
2518-
break;
2519-
25202504
case ISD::XOR:
2521-
// We want to look through a transform in InstCombine that
2522-
// turns 'add' with min_signed_val into 'xor', so we can treat this 'xor'
2523-
// exactly like an 'add'.
2524-
if (isMinSignedConstant(N.getOperand(1)) && !matchAdd(N, AM, Depth))
2505+
// See if we can treat the OR/XOR node as an ADD node.
2506+
if (!CurDAG->isADDLike(N))
2507+
break;
2508+
[[fallthrough]];
2509+
case ISD::ADD:
2510+
if (!matchAdd(N, AM, Depth))
25252511
return false;
25262512
break;
25272513

0 commit comments

Comments
 (0)