Skip to content

[SelectionDAG] Add MaskedValueIsZero check to allow folding of zero extended variables we know are safe to extend #85573

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 2 commits into from
Mar 21, 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
15 changes: 12 additions & 3 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13844,11 +13844,20 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
if (N0.getOpcode() == ISD::SHL) {
// If the original shl may be shifting out bits, do not perform this
// transformation.
// TODO: Add MaskedValueIsZero check.
unsigned KnownZeroBits = ShVal.getValueSizeInBits() -
ShVal.getOperand(0).getValueSizeInBits();
if (ShAmtC->getAPIntValue().ugt(KnownZeroBits))
return SDValue();
if (ShAmtC->getAPIntValue().ugt(KnownZeroBits)) {
// If the shift is too large, then see if we can deduce that the
// shift is safe anyway.
// Create a mask that has ones for the bits being shifted out.
APInt ShiftOutMask =
APInt::getHighBitsSet(ShVal.getValueSizeInBits(),
ShAmtC->getAPIntValue().getZExtValue());

// Check if the bits being shifted out are known to be zero.
if (!DAG.MaskedValueIsZero(ShVal, ShiftOutMask))
return SDValue();
}
}

// Ensure that the shift amount is wide enough for the shifted value.
Expand Down
127 changes: 127 additions & 0 deletions llvm/test/CodeGen/X86/dagcombine-shifts.ll
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,132 @@ define void @g(i32 %a) nounwind {
ret void
}

define i32 @shift_zext_shl(i8 zeroext %x) {
; X86-LABEL: shift_zext_shl:
; X86: # %bb.0:
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %eax
; X86-NEXT: andl $64, %eax
; X86-NEXT: shll $9, %eax
; X86-NEXT: retl
;
; X64-LABEL: shift_zext_shl:
; X64: # %bb.0:
; X64-NEXT: movl %edi, %eax
; X64-NEXT: andl $64, %eax
; X64-NEXT: shll $9, %eax
; X64-NEXT: retq
%a = and i8 %x, 64
%b = zext i8 %a to i16
%c = shl i16 %b, 9
%d = zext i16 %c to i32
ret i32 %d
}

define i32 @shift_zext_shl2(i8 zeroext %x) {
; X86-LABEL: shift_zext_shl2:
; X86: # %bb.0:
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %eax
; X86-NEXT: andl $64, %eax
; X86-NEXT: shll $9, %eax
; X86-NEXT: retl
;
; X64-LABEL: shift_zext_shl2:
; X64: # %bb.0:
; X64-NEXT: movl %edi, %eax
; X64-NEXT: andl $64, %eax
; X64-NEXT: shll $9, %eax
; X64-NEXT: retq
%a = and i8 %x, 64
%b = zext i8 %a to i32
%c = shl i32 %b, 9
ret i32 %c
}

define <4 x i32> @shift_zext_shl_vec(<4 x i8> %x) nounwind {
; X86-LABEL: shift_zext_shl_vec:
; X86: # %bb.0:
; X86-NEXT: pushl %edi
; X86-NEXT: pushl %esi
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %edi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %esi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %edx
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: andl $64, %ecx
; X86-NEXT: shll $9, %ecx
; X86-NEXT: andl $63, %edx
; X86-NEXT: shll $8, %edx
; X86-NEXT: andl $31, %esi
; X86-NEXT: shll $7, %esi
; X86-NEXT: andl $23, %edi
; X86-NEXT: shll $6, %edi
; X86-NEXT: movl %edi, 12(%eax)
; X86-NEXT: movl %esi, 8(%eax)
; X86-NEXT: movl %edx, 4(%eax)
; X86-NEXT: movl %ecx, (%eax)
; X86-NEXT: popl %esi
; X86-NEXT: popl %edi
; X86-NEXT: retl $4
;
; X64-LABEL: shift_zext_shl_vec:
; X64: # %bb.0:
; X64-NEXT: pand {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-NEXT: pxor %xmm1, %xmm1
; X64-NEXT: punpcklbw {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3],xmm0[4],xmm1[4],xmm0[5],xmm1[5],xmm0[6],xmm1[6],xmm0[7],xmm1[7]
; X64-NEXT: pmullw {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-NEXT: punpcklwd {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3]
; X64-NEXT: retq
%a = and <4 x i8> %x, <i8 64, i8 63, i8 31, i8 23>
%b = zext <4 x i8> %a to <4 x i16>
%c = shl <4 x i16> %b, <i16 9, i16 8, i16 7, i16 6>
%d = zext <4 x i16> %c to <4 x i32>
ret <4 x i32> %d
}

define <4 x i32> @shift_zext_shl2_vec(<4 x i8> %x) nounwind {
; X86-LABEL: shift_zext_shl2_vec:
; X86: # %bb.0:
; X86-NEXT: pushl %edi
; X86-NEXT: pushl %esi
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %edx
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %esi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %edi
; X86-NEXT: andl $23, %edi
; X86-NEXT: andl $31, %esi
; X86-NEXT: andl $63, %edx
; X86-NEXT: andl $64, %ecx
; X86-NEXT: shll $9, %ecx
; X86-NEXT: shll $8, %edx
; X86-NEXT: shll $7, %esi
; X86-NEXT: shll $6, %edi
; X86-NEXT: movl %edi, 12(%eax)
; X86-NEXT: movl %esi, 8(%eax)
; X86-NEXT: movl %edx, 4(%eax)
; X86-NEXT: movl %ecx, (%eax)
; X86-NEXT: popl %esi
; X86-NEXT: popl %edi
; X86-NEXT: retl $4
;
; X64-LABEL: shift_zext_shl2_vec:
; X64: # %bb.0:
; X64-NEXT: pand {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-NEXT: pxor %xmm1, %xmm1
; X64-NEXT: punpcklbw {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3],xmm0[4],xmm1[4],xmm0[5],xmm1[5],xmm0[6],xmm1[6],xmm0[7],xmm1[7]
; X64-NEXT: punpcklwd {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3]
; X64-NEXT: pshufd {{.*#+}} xmm1 = xmm0[1,1,3,3]
; X64-NEXT: pmuludq {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-NEXT: pshufd {{.*#+}} xmm0 = xmm0[0,2,2,3]
; X64-NEXT: pmuludq {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm1
; X64-NEXT: pshufd {{.*#+}} xmm1 = xmm1[0,2,2,3]
; X64-NEXT: punpckldq {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
; X64-NEXT: retq
%a = and <4 x i8> %x, <i8 64, i8 63, i8 31, i8 23>
%b = zext <4 x i8> %a to <4 x i32>
%c = shl <4 x i32> %b, <i32 9, i32 8, i32 7, i32 6>
ret <4 x i32> %c
}

declare dso_local void @f(i64)