Skip to content

Commit a972c3a

Browse files
committed
[SelectionDAG] fold (not (sub Y, X)) -> (add X, ~Y)
This replaces (not (neg x)) -> (add X, -1) because that is covered by this.
1 parent a8280c4 commit a972c3a

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9979,13 +9979,15 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
99799979
}
99809980
}
99819981

9982-
// fold (not (neg x)) -> (add X, -1)
9983-
// FIXME: This can be generalized to (not (sub Y, X)) -> (add X, ~Y) if
9984-
// Y is a constant or the subtract has a single use.
9985-
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB &&
9986-
isNullConstant(N0.getOperand(0))) {
9987-
return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(1),
9988-
DAG.getAllOnesConstant(DL, VT));
9982+
// fold (not (sub Y, X)) -> (add X, ~Y) if Y is a constant.
9983+
// FIXME: We can also do this with single-use sub, but this causes an infinite
9984+
// loop
9985+
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB) {
9986+
SDValue N00 = N0.getOperand(0), N01 = N0.getOperand(1);
9987+
if (isa<ConstantSDNode>(N00)) {
9988+
SDValue NotY = DAG.getNOT(DL, N00, VT); // N00 = ~N00
9989+
return DAG.getNode(ISD::ADD, DL, VT, N01, NotY);
9990+
}
99899991
}
99909992

99919993
// fold (not (add X, -1)) -> (neg X)

llvm/test/CodeGen/X86/pr31045.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ define void @_Z1av() local_unnamed_addr #0 {
2121
; CHECK-NEXT: movl struct_obj_3+8(%rip), %eax
2222
; CHECK-NEXT: movzbl var_46(%rip), %ecx
2323
; CHECK-NEXT: movzbl var_49(%rip), %edx
24-
; CHECK-NEXT: andl $1, %eax
25-
; CHECK-NEXT: addl %eax, %eax
26-
; CHECK-NEXT: subl %ecx, %eax
27-
; CHECK-NEXT: subl %edx, %eax
24+
; CHECK-NEXT: addl %ecx, %edx
2825
; CHECK-NEXT: notl %eax
26+
; CHECK-NEXT: addl %eax, %eax
27+
; CHECK-NEXT: orl $253, %eax
28+
; CHECK-NEXT: addl %edx, %eax
2929
; CHECK-NEXT: movzbl %al, %eax
3030
; CHECK-NEXT: movw %ax, struct_obj_12+5(%rip)
3131
; CHECK-NEXT: movb $0, var_163(%rip)

0 commit comments

Comments
 (0)