[clang] [llvm] [InstCombine] remove dead loads, such as memcpy from undef (PR #143958)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 16 06:33:50 PDT 2025


================
@@ -3382,6 +3418,17 @@ Instruction *InstCombinerImpl::visitAllocSite(Instruction &MI) {
           eraseInstFromFunction(*I);
           Users[i] = nullptr; // Skip examining in the next loop.
         }
+        if (auto *MTI = dyn_cast<MemTransferInst>(I)) {
+          if (KnowInitZero && getUnderlyingObject(MTI->getRawDest()) != &MI) {
+            IRBuilderBase::InsertPointGuard Guard(Builder);
+            Builder.SetInsertPoint(MTI);
+            auto *M = Builder.CreateMemSet(MTI->getRawDest(),
+                    ConstantInt::get(Type::getInt8Ty(MI.getContext()), 0),
+                    MTI->getLength(),
+                    MTI->getDestAlign());
+            M->copyMetadata(*MTI, LLVMContext::MD_DIAssignID);
----------------
nikic wrote:

This depends a lot on the specific transform. Dropping or adjusting metadata is often required when we're merging operations in some way (e.g. memcpy to memcpy forwarding).

However, I believe that for this specific case where we're converting memcpy to memset, preserving metadata should be fine. (We're only *removing* a read from a location.)

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


More information about the llvm-commits mailing list