[PATCH] D15947: Check dominance tree before lifting memcpy in MemCpyOpt

Amaury SECHET via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 7 03:31:12 PST 2016


deadalnix created this revision.
deadalnix added reviewers: craig.topper, spatel, dexonsmith, Prazek, chandlerc, joker.eph, majnemer.
deadalnix added a subscriber: llvm-commits.

The load to store forwarding of aggregate to memcpy transformation can create faulty IR. This happens when the memecpy would be materialized before the argument of the store.

This diff add a check so that dominance tree remains correct.

http://reviews.llvm.org/D15947

Files:
  lib/Transforms/Scalar/MemCpyOptimizer.cpp
  test/Transforms/MemCpyOpt/fca2memcpy.ll

Index: test/Transforms/MemCpyOpt/fca2memcpy.ll
===================================================================
--- test/Transforms/MemCpyOpt/fca2memcpy.ll
+++ test/Transforms/MemCpyOpt/fca2memcpy.ll
@@ -73,11 +73,10 @@
   ret void
 }
 
-
 ; The GEP is present after the aliasing store, preventing to move the memcpy before
 ; (without further analysis/transformation)
 define void @copyaliaswithproducerinbetween(%S* %src, %S* %dst) {
-; CHECK-LABEL: copyalias
+; CHECK-LABEL: copyaliaswithproducerinbetween
 ; CHECK-NEXT: [[LOAD:%[a-z0-9\.]+]] = load %S, %S* %src
 ; CHECK-NOT: call
   %1 = load %S, %S* %src
Index: lib/Transforms/Scalar/MemCpyOptimizer.cpp
===================================================================
--- lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -532,10 +532,19 @@
           // position if nothing alias the store memory after this and the store
           // destination is not in the range.
           P = &*I;
+
+          // Make sure this position dominate store's address.
+          if (auto *Addr = dyn_cast<Instruction>(SI->getPointerOperand())) {
+            DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
+            if (DT.dominates(P, Addr)) {
+              P = nullptr;
+              break;
+            }
+          }
+
           for (; I != E; ++I) {
             MemoryLocation StoreLoc = MemoryLocation::get(SI);
-            if (&*I == SI->getOperand(1) ||
-                AA.getModRefInfo(&*I, StoreLoc) != MRI_NoModRef) {
+            if (AA.getModRefInfo(&*I, StoreLoc) != MRI_NoModRef) {
               P = nullptr;
               break;
             }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15947.44202.patch
Type: text/x-patch
Size: 1690 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160107/c95d0a2a/attachment.bin>


More information about the llvm-commits mailing list