ir_opt: Simplify dead-code elimination pass

This commit is contained in:
MerryMage 2017-02-19 11:04:31 +00:00
parent bbeea72eba
commit 157585887e

View file

@ -4,6 +4,7 @@
* General Public License version 2 or any later version. * General Public License version 2 or any later version.
*/ */
#include "common/iterator_util.h"
#include "frontend/ir/basic_block.h" #include "frontend/ir/basic_block.h"
#include "ir_opt/passes.h" #include "ir_opt/passes.h"
@ -13,19 +14,11 @@ namespace Optimization {
void DeadCodeElimination(IR::Block& block) { void DeadCodeElimination(IR::Block& block) {
// We iterate over the instructions in reverse order. // We iterate over the instructions in reverse order.
// This is because removing an instruction reduces the number of uses for earlier instructions. // This is because removing an instruction reduces the number of uses for earlier instructions.
for (auto& inst : Common::Reverse(block)) {
if (block.empty()) { if (!inst.HasUses() && !inst.MayHaveSideEffects()) {
return; inst.Invalidate();
}
auto iter = block.end();
do {
--iter;
if (!iter->HasUses() && !iter->MayHaveSideEffects()) {
iter->Invalidate();
iter = block.Instructions().erase(iter);
} }
} while (iter != block.begin()); }
} }
} // namespace Optimization } // namespace Optimization