[PATCH] D68006: DSE miscompile when store is clobbered across loop iterations

Artur Pilipenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 24 19:34:19 PDT 2019


apilipenko created this revision.
apilipenko added reviewers: hfinkel, eli.friedman, bjope.

(This is a resubmission of D67870 <https://reviews.llvm.org/D67870> with llvm-commits is subscribers)

Currently DSE miscompiles the following example:

a = calloc(n+1)
for (int i = 0; i < n; i++) {

  store 1, a[i+1] // (1)
  store 0, a[i]   // (2)

}
It eliminates the second store thinking that it's redundant. This happens because memoryIsNotModifiedBetween doesn't see a[i] being clobbered in between the allocation and the store.

memoryIsNotModifiedBetween does a backwards scan through the CFG from SecondI to FirstI. It looks for instructions which can modify the memory location accessed by SecondI. For every may-write-to-memory instruction is asks AA whether this instruction modifies memory location accessed by SecondI.

The problem occurs when it visits the loop block through a backedge. It asks the AA about aliasing between stores (1) and (2). BasicAA sees that two stores access addresses which are distincs offsets from the same base and concludes that they don't alias. This is true for accesses within one loop iteration, but this is not true across iterations.

The change is to keep track whether we visit a block through a backedge. If we visit the block through a backedge, be conservative and treat any may-write-to-memory instruction as a clobber.

Note that there is a somewhat similar problem in DA:
https://bugs.llvm.org/show_bug.cgi?id=42143


https://reviews.llvm.org/D68006

Files:
  lib/Transforms/Scalar/DeadStoreElimination.cpp
  test/Transforms/DeadStoreElimination/simple.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68006.221656.patch
Type: text/x-patch
Size: 11448 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190925/90aeea5a/attachment.bin>


More information about the llvm-commits mailing list