From 4db1b13e9865b5fdf471b0c78144728108021814 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Sat, 4 Jan 2014 11:56:05 -0800 Subject: [PATCH] DCE pass removes assigns. --- .../passes/dead_code_elimination_pass.cc | 32 +++++++++++++++++++ .../passes/dead_code_elimination_pass.h | 1 + 2 files changed, 33 insertions(+) diff --git a/src/alloy/compiler/passes/dead_code_elimination_pass.cc b/src/alloy/compiler/passes/dead_code_elimination_pass.cc index 9bc564c90..a9b7c7bdb 100644 --- a/src/alloy/compiler/passes/dead_code_elimination_pass.cc +++ b/src/alloy/compiler/passes/dead_code_elimination_pass.cc @@ -43,6 +43,12 @@ int DeadCodeEliminationPass::Run(HIRBuilder* builder) { // v35.i8 = compare_eq v31.i32, 0 // branch_true v35.i8, loc_8201A484 + // This also removes useless ASSIGNs: + // v1 = v0 + // v2 = add v1, v1 + // becomes: + // v2 = add v0, v0 + // We process DCE by reverse iterating over instructions and looking at the // use count of the dest value. If it's zero, we can safely remove the // instruction. Once we do that, the use counts of any of the src ops may @@ -67,6 +73,10 @@ int DeadCodeEliminationPass::Run(HIRBuilder* builder) { // Has no uses and is not volatile. This instruction can die! MakeNopRecursive(i); any_removed = true; + } else if (opcode == &OPCODE_ASSIGN_info) { + // Assignment. These are useless, so just try to remove by completely + // replacing the value. + ReplaceAssignment(i); } i = prev; @@ -118,3 +128,25 @@ void DeadCodeEliminationPass::MakeNopRecursive(Instr* i) { MAKE_NOP_SRC(2); MAKE_NOP_SRC(3); } + +void DeadCodeEliminationPass::ReplaceAssignment(Instr* i) { + auto src = i->src1.value; + auto dest = i->dest; + + auto use = dest->use_head; + while (use) { + auto use_instr = use->instr; + if (use_instr->src1.value == dest) { + use_instr->set_src1(src); + } + if (use_instr->src2.value == dest) { + use_instr->set_src2(src); + } + if (use_instr->src3.value == dest) { + use_instr->set_src3(src); + } + use = use->next; + } + + i->Remove(); +} diff --git a/src/alloy/compiler/passes/dead_code_elimination_pass.h b/src/alloy/compiler/passes/dead_code_elimination_pass.h index 1d414d5c2..9a8cfc43a 100644 --- a/src/alloy/compiler/passes/dead_code_elimination_pass.h +++ b/src/alloy/compiler/passes/dead_code_elimination_pass.h @@ -27,6 +27,7 @@ public: private: void MakeNopRecursive(hir::Instr* i); + void ReplaceAssignment(hir::Instr* i); };