ir_opt: Simplify dead-code elimination pass
This commit is contained in:
parent
bbeea72eba
commit
157585887e
1 changed files with 5 additions and 12 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue