Skip to content

[SelectionDAG][X86] Use disjoint flag in SelectionDAG::isADDLike. #76847

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 1 commit into from
Jan 3, 2024
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
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7987,7 +7987,7 @@ SDValue DAGCombiner::visitOR(SDNode *N) {

// If OR can be rewritten into ADD, try combines based on ADD.
if ((!LegalOperations || TLI.isOperationLegal(ISD::ADD, VT)) &&
DAG.haveNoCommonBitsSet(N0, N1))
DAG.isADDLike(SDValue(N, 0)))
if (SDValue Combined = visitADDLike(N))
return Combined;

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5088,7 +5088,8 @@ bool SelectionDAG::canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts,
bool SelectionDAG::isADDLike(SDValue Op) const {
unsigned Opcode = Op.getOpcode();
if (Opcode == ISD::OR)
return haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
return Op->getFlags().hasDisjoint() ||
haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
if (Opcode == ISD::XOR)
return isMinSignedConstant(Op.getOperand(1));
return false;
Expand Down
36 changes: 36 additions & 0 deletions llvm/test/CodeGen/X86/addsub-constant-folding.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1141,3 +1141,39 @@ define <4 x i32> @vec_const_sub_const_sub_nonsplat(<4 x i32> %arg) {
%t1 = sub <4 x i32> <i32 2, i32 3, i32 undef, i32 2>, %t0
ret <4 x i32> %t1
}

; (x|c1)+c2 where (x|c1) is addlike
define i32 @add_const_disjoint_or_const(i32 %arg) {
; X86-LABEL: add_const_disjoint_or_const:
; X86: # %bb.0:
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: addl $10, %eax
; X86-NEXT: retl
;
; X64-LABEL: add_const_disjoint_or_const:
; X64: # %bb.0:
; X64-NEXT: # kill: def $edi killed $edi def $rdi
; X64-NEXT: leal 10(%rdi), %eax
; X64-NEXT: retq
%t0 = or disjoint i32 %arg, 8
%t1 = add i32 %t0, 2
ret i32 %t1
}

; (x+c1)|c2 where the outer or is addlike
define i32 @disjoint_or_const_add_const(i32 %arg) {
; X86-LABEL: disjoint_or_const_add_const:
; X86: # %bb.0:
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: addl $10, %eax
; X86-NEXT: retl
;
; X64-LABEL: disjoint_or_const_add_const:
; X64: # %bb.0:
; X64-NEXT: # kill: def $edi killed $edi def $rdi
; X64-NEXT: leal 10(%rdi), %eax
; X64-NEXT: retq
%t0 = add i32 %arg, 8
%t1 = or disjoint i32 %t0, 2
ret i32 %t1
}
20 changes: 20 additions & 0 deletions llvm/test/CodeGen/X86/or-lea.ll
Original file line number Diff line number Diff line change
Expand Up @@ -825,3 +825,23 @@ entry:
%or = or i64 %sub, 549755813889 ; 0x8000000001
ret i64 %or
}

define i32 @or_shift1_disjoint(i32 %x, i32 %y) {
; X86-LABEL: or_shift1_disjoint:
; X86: # %bb.0:
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: addl %eax, %eax
; X86-NEXT: orl {{[0-9]+}}(%esp), %eax
; X86-NEXT: retl
;
; X64-LABEL: or_shift1_disjoint:
; X64: # %bb.0:
; X64-NEXT: # kill: def $esi killed $esi def $rsi
; X64-NEXT: # kill: def $edi killed $edi def $rdi
; X64-NEXT: leal (%rsi,%rdi,2), %eax
; X64-NEXT: retq
%shl = shl i32 %x, 1
%or = or disjoint i32 %y, %shl
ret i32 %or
}