[llvm] [JumpThreading][GVN] Copy metadata when inserting preload into preds (PR #134403)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 20 12:59:07 PDT 2025


================
@@ -1409,6 +1409,13 @@ bool JumpThreadingPass::simplifyPartiallyRedundantLoad(LoadInst *LoadI) {
     if (AATags)
       NewVal->setAAMetadata(AATags);
 
+    if (auto *MD = LoadI->getMetadata(LLVMContext::MD_invariant_load))
+      NewVal->setMetadata(LLVMContext::MD_invariant_load, MD);
+    if (auto *InvGroupMD = LoadI->getMetadata(LLVMContext::MD_invariant_group))
+      NewVal->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
+    if (auto *RangeMD = LoadI->getMetadata(LLVMContext::MD_range))
+      NewVal->setMetadata(LLVMContext::MD_range, RangeMD);
----------------
nikic wrote:

There are two cases here. If the instructions we're hoisting over are willreturn, then we can preserve all metadata. The use of copyMetadataForLoad() is not necessary, as we're not changing the load type. We can directly copy all metadata.

If the instructions we're hoisting over are not willreturn, but the load is speculatable, we have to be more careful and typically cannot preserve UB-implying metadata. Using copyMetadataForLoad() is incorrect in this case, as it will preserve `!noundef` for example.

https://github.com/llvm/llvm-project/pull/134403


More information about the llvm-commits mailing list