(This is a resubmission of 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