Starting compiler work. Adding pass TODOs.

This commit is contained in:
Ben Vanik
2013-12-07 01:36:13 -08:00
parent fdb6a5cfa3
commit c2113c116d
16 changed files with 509 additions and 138 deletions

View File

@@ -1,22 +0,0 @@
/**
******************************************************************************
* 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/mem2reg_pass.h>
using namespace alloy;
using namespace alloy::compiler;
using namespace alloy::compiler::passes;
Mem2RegPass::Mem2RegPass() :
Pass() {
}
Mem2RegPass::~Mem2RegPass() {
}

View File

@@ -0,0 +1,83 @@
/**
******************************************************************************
* 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/simplification_pass.h>
#include <alloy/hir/function_builder.h>
using namespace alloy;
using namespace alloy::compiler;
using namespace alloy::compiler::passes;
using namespace alloy::hir;
SimplificationPass::SimplificationPass() :
Pass() {
}
SimplificationPass::~SimplificationPass() {
}
int SimplificationPass::Run(FunctionBuilder* builder) {
// Run over the instructions and rename assigned variables:
// v1 = v0
// v2 = v1
// v3 = add v0, v2
// becomes:
// v1 = v0
// v2 = v0
// v3 = add v0, v0
// This could be run several times, as it could make other passes faster
// to compute (for example, ConstantPropagation). DCE will take care of
// the useless assigns.
//
// We do this by walking each instruction. For each value op we
// look at its def instr to see if it's an assign - if so, we use the src
// of that instr. Because we may have chains, we do this recursively until
// we find a non-assign def.
Block* block = builder->first_block();
while (block) {
Instr* i = block->instr_head;
while (i) {
uint32_t signature = i->opcode->signature;
if (GET_OPCODE_SIG_TYPE_SRC1(signature) == OPCODE_SIG_TYPE_V) {
i->set_src1(CheckValue(i->src1.value));
}
if (GET_OPCODE_SIG_TYPE_SRC2(signature) == OPCODE_SIG_TYPE_V) {
i->set_src2(CheckValue(i->src2.value));
}
if (GET_OPCODE_SIG_TYPE_SRC3(signature) == OPCODE_SIG_TYPE_V) {
i->set_src3(CheckValue(i->src3.value));
}
i = i->next;
}
block = block->next;
}
return 0;
}
Value* SimplificationPass::CheckValue(Value* value) {
Instr* def = value->def;
if (def && def->opcode->num == OPCODE_ASSIGN) {
// Value comes from an assignment - recursively find if it comes from
// another assignment. It probably doesn't, if we already replaced it.
Value* replacement = def->src1.value;
while (true) {
def = replacement->def;
if (!def || def->opcode->num != OPCODE_ASSIGN) {
break;
}
replacement = def->src1.value;
}
return replacement;
}
return value;
}

View File

@@ -7,8 +7,8 @@
******************************************************************************
*/
#ifndef ALLOY_COMPILER_PASSES_MEM2REG_PASS_H_
#define ALLOY_COMPILER_PASSES_MEM2REG_PASS_H_
#ifndef ALLOY_COMPILER_PASSES_SIMPLIFICATION_PASS_H_
#define ALLOY_COMPILER_PASSES_SIMPLIFICATION_PASS_H_
#include <alloy/compiler/pass.h>
@@ -18,10 +18,15 @@ namespace compiler {
namespace passes {
class Mem2RegPass : public Pass {
class SimplificationPass : public Pass {
public:
Mem2RegPass();
virtual ~Mem2RegPass();
SimplificationPass();
virtual ~SimplificationPass();
virtual int Run(hir::FunctionBuilder* builder);
private:
hir::Value* CheckValue(hir::Value* value);
};
@@ -30,4 +35,4 @@ public:
} // namespace alloy
#endif // ALLOY_COMPILER_PASSES_MEM2REG_PASS_H_
#endif // ALLOY_COMPILER_PASSES_SIMPLIFICATION_PASS_H_

View File

@@ -1,7 +1,15 @@
# Copyright 2013 Ben Vanik. All Rights Reserved.
{
'sources': [
'mem2reg_pass.cc',
'mem2reg_pass.h',
#'constant_propagation_pass.cc',
#'constant_propagation_pass.h',
#'context_promotion_pass.cc',
#'context_promotion_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',
'simplification_pass.h',
],
}