[X86] combineTruncate - trunc(srl(load(p),amt)) -> load(p+amt/8) - ensure there isn't an interdependency between the load and amt (#165850)

Fixes #165755
This commit is contained in:
Simon Pilgrim 2025-10-31 11:32:01 +00:00 committed by GitHub
parent b6a331b2e5
commit 1d5580f1b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View File

@ -54768,9 +54768,11 @@ static SDValue combineTruncate(SDNode *N, SelectionDAG &DAG,
KnownBits KnownAmt = DAG.computeKnownBits(ShAmt);
// Check the shift amount is byte aligned.
// Check the truncation doesn't use any shifted in (zero) top bits.
// Check the shift amount doesn't depend on the original load.
if (KnownAmt.countMinTrailingZeros() >= 3 &&
KnownAmt.getMaxValue().ule(SrcVT.getSizeInBits() -
VT.getSizeInBits())) {
VT.getSizeInBits()) &&
!Ld->isPredecessorOf(ShAmt.getNode())) {
EVT PtrVT = Ld->getBasePtr().getValueType();
SDValue PtrBitOfs = DAG.getZExtOrTrunc(ShAmt, DL, PtrVT);
SDValue PtrByteOfs =

View File

@ -0,0 +1,26 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
; RUN: llc < %s -mtriple=i686-- | FileCheck %s --check-prefixes=X86
; RUN: llc < %s -mtriple=x86_64-- | FileCheck %s --check-prefixes=X64
define i32 @PR165755(ptr %p0) {
; X86-LABEL: PR165755:
; X86: # %bb.0:
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: movl (%ecx), %eax
; X86-NEXT: movb $0, (%ecx)
; X86-NEXT: retl
;
; X64-LABEL: PR165755:
; X64: # %bb.0:
; X64-NEXT: movl (%rdi), %eax
; X64-NEXT: movb $0, (%rdi)
; X64-NEXT: retq
%ld64 = load i64, ptr %p0, align 8
store i8 0, ptr %p0, align 1
%ld32 = load i32, ptr %p0, align 8
%mask = and i32 %ld32, 32
%zext = zext i32 %mask to i64
%srl = lshr i64 %ld64, %zext
%res = trunc i64 %srl to i32
ret i32 %res
}