Skip to content

Invalidate range metadata when folding bitcast into load #133095

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 3 commits into from
Mar 27, 2025
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
9 changes: 9 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15964,6 +15964,15 @@ SDValue DAGCombiner::visitBITCAST(SDNode *N) {

if (TLI.isLoadBitCastBeneficial(N0.getValueType(), VT, DAG,
*LN0->getMemOperand())) {
// If the range metadata type does not match the new memory
// operation type, remove the range metadata.
if (const MDNode *MD = LN0->getRanges()) {
ConstantInt *Lower = mdconst::extract<ConstantInt>(MD->getOperand(0));
if (Lower->getBitWidth() != VT.getScalarSizeInBits() ||
!VT.isInteger()) {
LN0->getMemOperand()->clearRanges();
}
}
SDValue Load =
DAG.getLoad(VT, SDLoc(N), LN0->getChain(), LN0->getBasePtr(),
LN0->getMemOperand());
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/CodeGen/X86/fold_bitcast_md_range.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=x86_64-apple-macosx10.12.0 < %s | FileCheck %s

; Ensure that when a bitcast is folded into a load, range metadata is invalidated
; if it does not match the new type.

define i1 @fold_bitcast_range_metadata(ptr %valptr) {
; CHECK-LABEL: fold_bitcast_range_metadata:
; CHECK: ## %bb.0: ## %start
; CHECK-NEXT: movdqa (%rdi), %xmm0
; CHECK-NEXT: pcmpeqb {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; CHECK-NEXT: pmovmskb %xmm0, %eax
; CHECK-NEXT: cmpl $65535, %eax ## imm = 0xFFFF
; CHECK-NEXT: sete %al
; CHECK-NEXT: retq
start:
%val = load i128, ptr %valptr, align 16, !range !0, !noundef !1
%bool = icmp eq i128 %val, 1
ret i1 %bool
}

!0 = !{i128 0, i128 3}
!1 = !{}