[llvm-branch-commits] [llvm] 89f4b84 - [InstCombine] Use copyMetadata in PointerReplacer::replace (#201827)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jun 5 09:27:31 PDT 2026


Author: Justin Lebar
Date: 2026-06-05T09:12:32-07:00
New Revision: 89f4b84d8b2cd0a5c54dc9a819090f131bf87d0d

URL: https://github.com/llvm/llvm-project/commit/89f4b84d8b2cd0a5c54dc9a819090f131bf87d0d
DIFF: https://github.com/llvm/llvm-project/commit/89f4b84d8b2cd0a5c54dc9a819090f131bf87d0d.diff

LOG: [InstCombine] Use copyMetadata in PointerReplacer::replace (#201827)

PointerReplacer::replace creates a new load that differs from the
original only in its pointer operand; the loaded type is unchanged.  It
was using copyMetadataForLoad(), which is meant for the case where the
load's *type* changes.  Since the type is the same here, plain
copyMetadata() is correct and preserves all metadata directly.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
    llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 0e917fb55aa1e..e6481c89c0265 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -416,7 +416,7 @@ void PointerReplacer::replace(Instruction *I) {
                               LT->getAlign(), LT->getOrdering(),
                               LT->getSyncScopeID());
     NewI->takeName(LT);
-    copyMetadataForLoad(*NewI, *LT);
+    NewI->copyMetadata(*LT);
 
     IC.InsertNewInstWith(NewI, LT->getIterator());
     IC.replaceInstUsesWith(*LT, NewI);

diff  --git a/llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll b/llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll
index f084fe38bb226..3490773b03b82 100644
--- a/llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll
+++ b/llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll
@@ -481,6 +481,26 @@ define i8 @call_readonly_keep_alloca2() {
   ret i8 %v
 }
 
+; The pointer changes but the load type doesn't, so all metadata (here
+; !annotation, which the type-changing copyMetadataForLoad would drop) must be
+; preserved.
+ at g3 = external constant [32 x i8], align 16
+
+define i8 @preserve_load_metadata(i64 %idx) {
+; CHECK-LABEL: @preserve_load_metadata(
+; CHECK-NEXT:    [[PTR:%.*]] = getelementptr inbounds i8, ptr @g3, i64 [[IDX:%.*]]
+; CHECK-NEXT:    [[LOAD:%.*]] = load i8, ptr [[PTR]], align 1, !annotation [[META0:![0-9]+]]
+; CHECK-NEXT:    ret i8 [[LOAD]]
+;
+  %alloca = alloca [32 x i8], align 1, addrspace(1)
+  call void @llvm.memcpy.p1.p0.i64(ptr addrspace(1) %alloca, ptr @g3, i64 32, i1 false)
+  %ptr = getelementptr inbounds [32 x i8], ptr addrspace(1) %alloca, i64 0, i64 %idx
+  %load = load i8, ptr addrspace(1) %ptr, !annotation !0
+  ret i8 %load
+}
+
+!0 = !{!"my-annotation"}
+
 declare void @llvm.memcpy.p1.p0.i64(ptr addrspace(1), ptr, i64, i1)
 declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
 declare void @llvm.memcpy.p0.p1.i64(ptr, ptr addrspace(1), i64, i1)


        


More information about the llvm-branch-commits mailing list