[PATCH] D136822: [InstCombine] Allow memcpys from constant memory to readonly nocapture parameters to be elided.

Patrick Walton via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 30 13:22:15 PDT 2022


pcwalton updated this revision to Diff 471862.
pcwalton added a comment.

Rebased to make sure CI succeeds.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136822/new/

https://reviews.llvm.org/D136822

Files:
  llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
  llvm/test/Transforms/InstCombine/memcpy-from-global.ll


Index: llvm/test/Transforms/InstCombine/memcpy-from-global.ll
===================================================================
--- llvm/test/Transforms/InstCombine/memcpy-from-global.ll
+++ llvm/test/Transforms/InstCombine/memcpy-from-global.ll
@@ -380,13 +380,11 @@
   ret void
 }
 
-; Test that we don't yet elide a memcpy when copying a constant value onto the
-; stack and then forwarding it by readonly nocapture reference.
+; Test that we can elide a memcpy when copying a constant value onto the stack
+; and then forwarding it by readonly nocapture reference.
 define void @memcpy_to_nocapture_readonly() {
 ; CHECK-LABEL: @memcpy_to_nocapture_readonly(
-; CHECK-NEXT:    [[A:%.*]] = alloca [[U:%.*]], align 16
-; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(20) [[A]], ptr noundef nonnull align 16 dereferenceable(20) @H, i64 20, i1 false)
-; CHECK-NEXT:    call void @bar(ptr nocapture nonnull readonly [[A]])
+; CHECK-NEXT:    call void @bar(ptr nocapture nonnull readonly @H)
 ; CHECK-NEXT:    ret void
 ;
   %A = alloca %U, align 16
Index: llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -39,8 +39,8 @@
 /// the alloca, and if the source pointer is a pointer to a constant global, we
 /// can optimize this.
 static bool
-isOnlyCopiedFromConstantMemory(AAResults *AA,
-                               Value *V, MemTransferInst *&TheCopy,
+isOnlyCopiedFromConstantMemory(AAResults *AA, AllocaInst *V,
+                               MemTransferInst *&TheCopy,
                                SmallVectorImpl<Instruction *> &ToDelete) {
   // We track lifetime intrinsics as we encounter them.  If we decide to go
   // ahead and replace the value with the global, this lets the caller quickly
@@ -85,11 +85,12 @@
         if (IsArgOperand && Call->isInAllocaArgument(DataOpNo))
           return false;
 
-        // If this is a readonly/readnone call site, then we know it is just a
-        // load (but one that potentially returns the value itself), so we can
+        // If this call site doesn't modify the memory, then we know it is just
+        // a load (but one that potentially returns the value itself), so we can
         // ignore it if we know that the value isn't captured.
-        if (Call->onlyReadsMemory() &&
-            (Call->use_empty() || Call->doesNotCapture(DataOpNo)))
+        bool NoCapture = Call->doesNotCapture(DataOpNo);
+        if ((Call->onlyReadsMemory() && (Call->use_empty() || NoCapture)) ||
+            (Call->onlyReadsMemory(DataOpNo) && NoCapture))
           continue;
 
         // If this is being passed as a byval argument, the caller is making a


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136822.471862.patch
Type: text/x-patch
Size: 2882 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221030/0476a753/attachment.bin>


More information about the llvm-commits mailing list