Basic DCE pass.
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
|
||||
//#include <alloy/compiler/passes/constant_propagation_pass.h>
|
||||
//#include <alloy/compiler/passes/context_promotion_pass.h>
|
||||
//#include <alloy/compiler/passes/dead_code_elimination_pass.h>
|
||||
#include <alloy/compiler/passes/dead_code_elimination_pass.h>
|
||||
//#include <alloy/compiler/passes/dead_store_elimination_pass.h>
|
||||
#include <alloy/compiler/passes/simplification_pass.h>
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
// store_context +100, v0 <-- removed due to following store
|
||||
// store_context +100, v1
|
||||
// This is more generally done by DSE, however if it could be done here
|
||||
// instead as it may be faster (at least on the block-level).
|
||||
// instead as it may be faster (at least on the block-level).
|
||||
//
|
||||
// - ConstantPropagation
|
||||
// One ContextPromotion has run there will likely be a whole slew of
|
||||
|
||||
123
src/alloy/compiler/passes/dead_code_elimination_pass.cc
Normal file
123
src/alloy/compiler/passes/dead_code_elimination_pass.cc
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <alloy/compiler/passes/dead_code_elimination_pass.h>
|
||||
|
||||
using namespace alloy;
|
||||
using namespace alloy::compiler;
|
||||
using namespace alloy::compiler::passes;
|
||||
using namespace alloy::hir;
|
||||
|
||||
|
||||
DeadCodeEliminationPass::DeadCodeEliminationPass() :
|
||||
Pass() {
|
||||
}
|
||||
|
||||
DeadCodeEliminationPass::~DeadCodeEliminationPass() {
|
||||
}
|
||||
|
||||
int DeadCodeEliminationPass::Run(FunctionBuilder* builder) {
|
||||
// ContextPromotion/DSE will likely leave around a lot of dead statements.
|
||||
// Code generated for comparison/testing produces many unused statements and
|
||||
// with proper use analysis it should be possible to remove most of them:
|
||||
// After context promotion/simplification:
|
||||
// v33.i8 = compare_ult v31.i32, 0
|
||||
// v34.i8 = compare_ugt v31.i32, 0
|
||||
// v35.i8 = compare_eq v31.i32, 0
|
||||
// store_context +300, v33.i8
|
||||
// store_context +301, v34.i8
|
||||
// store_context +302, v35.i8
|
||||
// branch_true v35.i8, loc_8201A484
|
||||
// After DSE:
|
||||
// v33.i8 = compare_ult v31.i32, 0
|
||||
// v34.i8 = compare_ugt v31.i32, 0
|
||||
// v35.i8 = compare_eq v31.i32, 0
|
||||
// branch_true v35.i8, loc_8201A484
|
||||
// After DCE:
|
||||
// v35.i8 = compare_eq v31.i32, 0
|
||||
// branch_true v35.i8, loc_8201A484
|
||||
|
||||
// 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
|
||||
// go to zero and we recursively kill up the graph. This is kind of
|
||||
// nasty in that we walk randomly and scribble all over memory, but (I think)
|
||||
// it's better than doing passes over all instructions until we quiesce.
|
||||
// To prevent our iteration from getting all messed up we just replace
|
||||
// all removed ops with NOP and then do a single pass that removes them
|
||||
// all.
|
||||
|
||||
const OpcodeInfo* nop = builder->GetNopOpcode();
|
||||
|
||||
bool any_removed = false;
|
||||
Block* block = builder->first_block();
|
||||
while (block) {
|
||||
Instr* i = block->instr_tail;
|
||||
while (i) {
|
||||
Instr* prev = i->prev;
|
||||
|
||||
const OpcodeInfo* opcode = i->opcode;
|
||||
uint32_t signature = opcode->signature;
|
||||
if (!(opcode->flags & OPCODE_FLAG_VOLATILE) &&
|
||||
i->dest && !i->dest->use_head) {
|
||||
// Has no uses and is not volatile. This instruction can die!
|
||||
MakeNopRecursive(nop, i);
|
||||
any_removed = true;
|
||||
}
|
||||
|
||||
i = prev;
|
||||
}
|
||||
|
||||
block = block->next;
|
||||
}
|
||||
|
||||
// Remove all nops.
|
||||
if (any_removed) {
|
||||
Block* block = builder->first_block();
|
||||
while (block) {
|
||||
Instr* i = block->instr_head;
|
||||
while (i) {
|
||||
Instr* next = i->next;
|
||||
if (i->opcode->num == OPCODE_NOP) {
|
||||
// Nop - remove!
|
||||
i->Remove();
|
||||
}
|
||||
i = next;
|
||||
}
|
||||
block = block->next;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DeadCodeEliminationPass::MakeNopRecursive(
|
||||
const OpcodeInfo* nop, Instr* i) {
|
||||
i->opcode = nop;
|
||||
i->dest->def = NULL;
|
||||
i->dest = NULL;
|
||||
|
||||
#define MAKE_NOP_SRC(n) \
|
||||
if (i->src##n##_use) { \
|
||||
Value::Use* use = i->src##n##_use; \
|
||||
Value* value = i->src##n##.value; \
|
||||
i->src##n##_use = NULL; \
|
||||
i->src##n##.value = NULL; \
|
||||
value->RemoveUse(use); \
|
||||
if (!value->use_head) { \
|
||||
/* Value is now unused, so recursively kill it. */ \
|
||||
if (value->def && value->def != i) { \
|
||||
MakeNopRecursive(nop, value->def); \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
MAKE_NOP_SRC(1);
|
||||
MAKE_NOP_SRC(2);
|
||||
MAKE_NOP_SRC(3);
|
||||
}
|
||||
38
src/alloy/compiler/passes/dead_code_elimination_pass.h
Normal file
38
src/alloy/compiler/passes/dead_code_elimination_pass.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef ALLOY_COMPILER_PASSES_DEAD_CODE_ELIMINATION_PASS_H_
|
||||
#define ALLOY_COMPILER_PASSES_DEAD_CODE_ELIMINATION_PASS_H_
|
||||
|
||||
#include <alloy/compiler/pass.h>
|
||||
|
||||
|
||||
namespace alloy {
|
||||
namespace compiler {
|
||||
namespace passes {
|
||||
|
||||
|
||||
class DeadCodeEliminationPass : public Pass {
|
||||
public:
|
||||
DeadCodeEliminationPass();
|
||||
virtual ~DeadCodeEliminationPass();
|
||||
|
||||
virtual int Run(hir::FunctionBuilder* builder);
|
||||
|
||||
private:
|
||||
void MakeNopRecursive(const hir::OpcodeInfo* nop, hir::Instr* i);
|
||||
};
|
||||
|
||||
|
||||
} // namespace passes
|
||||
} // namespace compiler
|
||||
} // namespace alloy
|
||||
|
||||
|
||||
#endif // ALLOY_COMPILER_PASSES_DEAD_CODE_ELIMINATION_PASS_H_
|
||||
@@ -8,7 +8,6 @@
|
||||
*/
|
||||
|
||||
#include <alloy/compiler/passes/simplification_pass.h>
|
||||
#include <alloy/hir/function_builder.h>
|
||||
|
||||
using namespace alloy;
|
||||
using namespace alloy::compiler;
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
#'constant_propagation_pass.h',
|
||||
#'context_promotion_pass.cc',
|
||||
#'context_promotion_pass.h',
|
||||
#'dead_code_elimination_pass.cc',
|
||||
#'dead_code_elimination_pass.h',
|
||||
'dead_code_elimination_pass.cc',
|
||||
'dead_code_elimination_pass.h',
|
||||
#'dead_store_elimination_pass.cc',
|
||||
#'dead_store_elimination_pass.h',
|
||||
'simplification_pass.cc',
|
||||
|
||||
Reference in New Issue
Block a user