From 996d5cb841490f650a43847faad5d04e671bc1bb Mon Sep 17 00:00:00 2001 From: MerryMage Date: Mon, 20 Apr 2020 16:12:19 +0100 Subject: [PATCH] ir_opt: Add IdentityRemovalPass --- src/CMakeLists.txt | 1 + src/ir_opt/identity_removal_pass.cpp | 46 ++++++++++++++++++++++++++++ src/ir_opt/passes.h | 3 +- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/ir_opt/identity_removal_pass.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9f5b45b4..68dfb6cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -95,6 +95,7 @@ add_library(dynarmic frontend/ir/value.h ir_opt/constant_propagation_pass.cpp ir_opt/dead_code_elimination_pass.cpp + ir_opt/identity_removal_pass.cpp ir_opt/passes.h ir_opt/verification_pass.cpp ) diff --git a/src/ir_opt/identity_removal_pass.cpp b/src/ir_opt/identity_removal_pass.cpp new file mode 100644 index 00000000..1d744b48 --- /dev/null +++ b/src/ir_opt/identity_removal_pass.cpp @@ -0,0 +1,46 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2020 MerryMage + * This software may be used and distributed according to the terms of the GNU + * General Public License version 2 or any later version. + */ + +#include + +#include "common/iterator_util.h" +#include "frontend/ir/basic_block.h" +#include "frontend/ir/opcodes.h" +#include "ir_opt/passes.h" + +namespace Dynarmic::Optimization { + +void IdentityRemovalPass(IR::Block& block) { + std::vector to_invalidate; + + auto iter = block.begin(); + while (iter != block.end()) { + IR::Inst& inst = *iter; + + const size_t num_args = inst.NumArgs(); + for (size_t i = 0; i < num_args; i++) { + while (true) { + IR::Value arg = inst.GetArg(i); + if (!arg.IsIdentity()) + break; + inst.SetArg(i, arg.GetInst()->GetArg(0)); + } + } + + if (inst.GetOpcode() == IR::Opcode::Identity) { + iter = block.Instructions().erase(inst); + to_invalidate.push_back(&inst); + } else { + ++iter; + } + } + + for (IR::Inst* inst : to_invalidate) { + inst->Invalidate(); + } +} + +} // namespace Dynarmic diff --git a/src/ir_opt/passes.h b/src/ir_opt/passes.h index 835f3aad..94e389a2 100644 --- a/src/ir_opt/passes.h +++ b/src/ir_opt/passes.h @@ -21,13 +21,14 @@ class Block; namespace Dynarmic::Optimization { -void A32GetSetElimination(IR::Block& block); void A32ConstantMemoryReads(IR::Block& block, A32::UserCallbacks* cb); +void A32GetSetElimination(IR::Block& block); void A64CallbackConfigPass(IR::Block& block, const A64::UserConfig& conf); void A64GetSetElimination(IR::Block& block); void A64MergeInterpretBlocksPass(IR::Block& block, A64::UserCallbacks* cb); void ConstantPropagation(IR::Block& block); void DeadCodeElimination(IR::Block& block); +void IdentityRemovalPass(IR::Block& block); void VerificationPass(const IR::Block& block); } // namespace Dynarmic::Optimization