Skip to content

Commit 2e2c413

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 2e2c413

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9979,13 +9979,16 @@ 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 do this with single-use sub, but this causes an infinite loop
9984+
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB) {
9985+
SDValue N00 = N0.getOperand(0), N01 = N0.getOperand(1);
9986+
if (isa<ConstantSDNode>(N00)) {
9987+
SDValue NotY =
9988+
DAG.getNode(ISD::XOR, SDLoc(N00), VT, N00, N1); // N00 = ~N00
9989+
AddToWorklist(NotY.getNode());
9990+
return DAG.getNode(ISD::ADD, DL, VT, N01, NotY);
9991+
}
99899992
}
99909993

99919994
// 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)