[DSE] Eliminates redundant store of an exisiting value (PR16520)
authorDawid Jurczak <dawid_jurek@vp.pl>
Fri, 22 Oct 2021 12:11:12 +0000 (14:11 +0200)
committerDawid Jurczak <dawid_jurek@vp.pl>
Thu, 28 Oct 2021 14:20:09 +0000 (16:20 +0200)
commitf87e0c68d78652aa9973e742e6c8df86fd7eee17
treeed45d7aec6d28580f59f5fab5bddf55096890d7d
parent79011c705b5849661cc791016c54ee62d9ec9cb0
[DSE] Eliminates redundant store of an exisiting value (PR16520)

That's https://reviews.llvm.org/D90328 follow-up.

This change eliminates writes to variables where the value that is being written is already stored in the variable.
This achieves the goal by looping through all memory definitions in the current state and getting defining access from each of them.
When there is defining access where the write instruction is identical to the original instruction it will remove this redundant write.

For example:

void f() {

x = 1;
if foo() {
   x = 1;
   g();
} else {
  h();
}

}
void g();
void h();

The second x=1 will be eliminated since it is rewriting 1 to x. This pass will produce this:

void f() {

x = 1;
if foo() {
   g();
} else {
  h();
}

}
void g();
void h();

Differential Revision: https://reviews.llvm.org/D111727
llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
llvm/test/Transforms/DeadStoreElimination/stores-of-existing-values.ll