[InstCombine] DCE instructions earlier
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 20 Feb 2020 21:42:42 +0000 (22:42 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 27 Feb 2020 17:45:59 +0000 (18:45 +0100)
commit4ef272ec9c5f737b8d63a3cc8870d8d63bf0385e
tree0b5418043a8fef9f8c02069bedcf182d1b6fce0b
parent75d4d4bd028f6a5f24ef41dbbc45bed65b2305cf
[InstCombine] DCE instructions earlier

When InstCombine initially populates the worklist, it already
performs constant folding and DCE. However, as the instructions
are initially visited in program order, this DCE can pick up only
the last instruction of a dead chain, the rest would only get
picked up in the main InstCombine run.

To avoid this, we instead perform the DCE in separate pass over the
collected instructions in reverse order, which will allow us to
pick up full dead instruction chains. We already need to do this
reverse iteration anyway to populate the worklist, so this
shouldn't add extra cost.

This by itself only fixes a small part of the problem though:
The same basic issue also applies during the main InstCombine loop.
We generally always want DCE to occur as early as possible,
because it will allow one-use folds to happen. Address this by also
performing DCE while adding deferred instructions to the main worklist.

This drops the number of tests that perform more than 2 InstCombine
iterations from ~80 to ~40. There's some spurious test changes due
to operand order / icmp toggling.

Differential Revision: https://reviews.llvm.org/D75008
12 files changed:
llvm/include/llvm/Transforms/InstCombine/InstCombineWorklist.h
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/2010-11-01-lshr-mask.ll
llvm/test/Transforms/InstCombine/demorgan-sink-not-into-xor.ll
llvm/test/Transforms/InstCombine/logical-select.ll
llvm/test/Transforms/InstCombine/pr44245.ll
llvm/test/Transforms/InstCombine/select-imm-canon.ll
llvm/test/Transforms/InstCombine/sub-ashr-and-to-icmp-select.ll
llvm/test/Transforms/InstCombine/sub-ashr-or-to-icmp-select.ll
llvm/test/Transforms/InstCombine/vec_sext.ll
llvm/test/Transforms/SimplifyCFG/merge-cond-stores.ll