[llvm] 2df25a4 - Invalidate range metadata when folding bitcast into load (#133095)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 27 00:10:59 PDT 2025


Author: LU-JOHN
Date: 2025-03-27T14:10:55+07:00
New Revision: 2df25a47339c38875476e559d80b225dfe07ff02

URL: https://github.com/llvm/llvm-project/commit/2df25a47339c38875476e559d80b225dfe07ff02
DIFF: https://github.com/llvm/llvm-project/commit/2df25a47339c38875476e559d80b225dfe07ff02.diff

LOG: Invalidate range metadata when folding bitcast into load (#133095)

Added: 
    llvm/test/CodeGen/X86/fold_bitcast_md_range.ll

Modified: 
    llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index bf49642634291..a8e516b8c2937 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -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());

diff  --git a/llvm/test/CodeGen/X86/fold_bitcast_md_range.ll b/llvm/test/CodeGen/X86/fold_bitcast_md_range.ll
new file mode 100644
index 0000000000000..88eee984477b2
--- /dev/null
+++ b/llvm/test/CodeGen/X86/fold_bitcast_md_range.ll
@@ -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 = !{}


        


More information about the llvm-commits mailing list