Reducing value count. IVM could take advantage of this.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include <alloy/compiler/passes/finalization_pass.h>
|
||||
//#include <alloy/compiler/passes/dead_store_elimination_pass.h>
|
||||
#include <alloy/compiler/passes/simplification_pass.h>
|
||||
#include <alloy/compiler/passes/value_reduction_pass.h>
|
||||
|
||||
// TODO:
|
||||
// - mark_use/mark_set
|
||||
|
||||
@@ -63,14 +63,6 @@ int FinalizationPass::Run(HIRBuilder* builder) {
|
||||
}
|
||||
}
|
||||
|
||||
// Renumber all instructions to make liveness tracking easier.
|
||||
uint32_t instr_ordinal = 0;
|
||||
auto instr = block->instr_head;
|
||||
while (instr) {
|
||||
instr->ordinal = instr_ordinal++;
|
||||
instr = instr->next;
|
||||
}
|
||||
|
||||
block = block->next;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,5 +13,7 @@
|
||||
#'dead_store_elimination_pass.h',
|
||||
'simplification_pass.cc',
|
||||
'simplification_pass.h',
|
||||
'value_reduction_pass.cc',
|
||||
'value_reduction_pass.h',
|
||||
],
|
||||
}
|
||||
|
||||
125
src/alloy/compiler/passes/value_reduction_pass.cc
Normal file
125
src/alloy/compiler/passes/value_reduction_pass.cc
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2014 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <alloy/compiler/passes/value_reduction_pass.h>
|
||||
|
||||
#include <alloy/backend/backend.h>
|
||||
#include <alloy/compiler/compiler.h>
|
||||
#include <alloy/runtime/runtime.h>
|
||||
|
||||
#include <bitset>
|
||||
|
||||
using namespace alloy;
|
||||
using namespace alloy::backend;
|
||||
using namespace alloy::compiler;
|
||||
using namespace alloy::compiler::passes;
|
||||
using namespace alloy::frontend;
|
||||
using namespace alloy::hir;
|
||||
using namespace alloy::runtime;
|
||||
|
||||
|
||||
ValueReductionPass::ValueReductionPass() :
|
||||
CompilerPass() {
|
||||
}
|
||||
|
||||
ValueReductionPass::~ValueReductionPass() {
|
||||
}
|
||||
|
||||
void ValueReductionPass::ComputeLastUse(Value* value) {
|
||||
uint32_t max_ordinal = 0;
|
||||
Value::Use* last_use = NULL;
|
||||
auto use = value->use_head;
|
||||
while (use) {
|
||||
if (!last_use || use->instr->ordinal >= max_ordinal) {
|
||||
last_use = use;
|
||||
max_ordinal = use->instr->ordinal;
|
||||
}
|
||||
use = use->next;
|
||||
}
|
||||
value->tag = last_use->instr;
|
||||
}
|
||||
|
||||
int ValueReductionPass::Run(HIRBuilder* builder) {
|
||||
// Walk each block and reuse variable ordinals as much as possible.
|
||||
|
||||
// Let's hope this is enough.
|
||||
std::bitset<1024> ordinals;
|
||||
|
||||
auto block = builder->first_block();
|
||||
while (block) {
|
||||
// Reset used ordinals.
|
||||
ordinals.reset();
|
||||
|
||||
// Renumber all instructions to make liveness tracking easier.
|
||||
uint32_t instr_ordinal = 0;
|
||||
auto instr = block->instr_head;
|
||||
while (instr) {
|
||||
instr->ordinal = instr_ordinal++;
|
||||
instr = instr->next;
|
||||
}
|
||||
|
||||
instr = block->instr_head;
|
||||
while (instr) {
|
||||
const OpcodeInfo* info = instr->opcode;
|
||||
OpcodeSignatureType dest_type = GET_OPCODE_SIG_TYPE_DEST(info->signature);
|
||||
OpcodeSignatureType src1_type = GET_OPCODE_SIG_TYPE_SRC1(info->signature);
|
||||
OpcodeSignatureType src2_type = GET_OPCODE_SIG_TYPE_SRC2(info->signature);
|
||||
OpcodeSignatureType src3_type = GET_OPCODE_SIG_TYPE_SRC3(info->signature);
|
||||
if (src1_type == OPCODE_SIG_TYPE_V && !instr->src1.value->IsConstant()) {
|
||||
auto v = instr->src1.value;
|
||||
if (!v->tag) {
|
||||
ComputeLastUse(v);
|
||||
}
|
||||
if (v->tag == instr) {
|
||||
// Available.
|
||||
ordinals.set(v->ordinal, false);
|
||||
}
|
||||
}
|
||||
if (src2_type == OPCODE_SIG_TYPE_V && !instr->src2.value->IsConstant()) {
|
||||
auto v = instr->src2.value;
|
||||
if (!v->tag) {
|
||||
ComputeLastUse(v);
|
||||
}
|
||||
if (v->tag == instr) {
|
||||
// Available.
|
||||
ordinals.set(v->ordinal, false);
|
||||
}
|
||||
}
|
||||
if (src3_type == OPCODE_SIG_TYPE_V && !instr->src3.value->IsConstant()) {
|
||||
auto v = instr->src3.value;
|
||||
if (!v->tag) {
|
||||
ComputeLastUse(v);
|
||||
}
|
||||
if (v->tag == instr) {
|
||||
// Available.
|
||||
ordinals.set(v->ordinal, false);
|
||||
}
|
||||
}
|
||||
if (dest_type == OPCODE_SIG_TYPE_V) {
|
||||
// Dest values are processed last, as they may be able to reuse a
|
||||
// source value ordinal.
|
||||
auto v = instr->dest;
|
||||
// Find a lower ordinal.
|
||||
for (auto n = 0; n < ordinals.size(); n++) {
|
||||
if (!ordinals.test(n)) {
|
||||
ordinals.set(n);
|
||||
v->ordinal = n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
instr = instr->next;
|
||||
}
|
||||
|
||||
block = block->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
38
src/alloy/compiler/passes/value_reduction_pass.h
Normal file
38
src/alloy/compiler/passes/value_reduction_pass.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2014 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef ALLOY_COMPILER_PASSES_VALUE_REDUCTION_PASS_H_
|
||||
#define ALLOY_COMPILER_PASSES_VALUE_REDUCTION_PASS_H_
|
||||
|
||||
#include <alloy/compiler/compiler_pass.h>
|
||||
|
||||
|
||||
namespace alloy {
|
||||
namespace compiler {
|
||||
namespace passes {
|
||||
|
||||
|
||||
class ValueReductionPass : public CompilerPass {
|
||||
public:
|
||||
ValueReductionPass();
|
||||
virtual ~ValueReductionPass();
|
||||
|
||||
virtual int Run(hir::HIRBuilder* builder);
|
||||
|
||||
private:
|
||||
void ComputeLastUse(hir::Value* value);
|
||||
};
|
||||
|
||||
|
||||
} // namespace passes
|
||||
} // namespace compiler
|
||||
} // namespace alloy
|
||||
|
||||
|
||||
#endif // ALLOY_COMPILER_PASSES_VALUE_REDUCTION_PASS_H_
|
||||
Reference in New Issue
Block a user