소스 검색

implementing dfg optimizations

Nicolas Winkler 6 년 전
부모
커밋
ab18e480dd
4개의 변경된 파일24개의 추가작업 그리고 32개의 파일을 삭제
  1. 7 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 1 0
      src/main.rs
  4. 15 32
      src/optimize.rs

+ 7 - 0
Cargo.lock

@@ -67,6 +67,11 @@ version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "typed-arena"
+version = "1.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
 name = "winapi"
 version = "0.3.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -91,6 +96,7 @@ version = "0.1.0"
 dependencies = [
  "dynasm 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "dynasmrt 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "typed-arena 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -105,6 +111,7 @@ dependencies = [
 "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
 "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
 "checksum take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60"
+"checksum typed-arena 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c6c06a92aef38bb4dc5b0df00d68496fc31307c5344c867bb61678c6e1671ec5"
 "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
 "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

+ 1 - 0
Cargo.toml

@@ -12,3 +12,4 @@ edition = "2018"
 dynasm = "*"
 dynasmrt = "*"
 winapi = "*"
+typed-arena = "1.4.1"

+ 1 - 0
src/main.rs

@@ -5,6 +5,7 @@
 //extern crate dynasm;
 
 extern crate winapi;
+extern crate typed_arena;
 
 use std::io::{self, Read};
 use std::fs::File;

+ 15 - 32
src/optimize.rs

@@ -2,36 +2,33 @@
 use std::collections::BTreeMap;
 use super::ir;
 use super::ir::Instruction;
+use typed_arena::Arena;
 
-
-
-
-pub struct DfgOptimizer {
-    dfg: DataflowGraph,
-    tape_state: TapeState,
+pub struct DfgOptimizer<'a> {
+    dfg: DataflowGraph<'a>,
+    tape_state: TapeState<'a>,
 }
 
-struct DataflowGraph {
-    
+struct DataflowGraph<'a> {
+    nodes: Arena<DfgNode<'a>>
 }
 
-
-enum CellState {
-    Value(i64),
-    Added(i64)
+enum DfgNode<'a> {
+    Offset(i64),
+    ConstAdd(&'a Cell<DfgNode<'a>>),
+    Const(i64),
+    AddMultiplied(&'a Cell<DfgNode<'a>>, i64, &'a Cell<DfgNode<'a>>),
 }
 
-struct TapeState {
-    pub cell_states: BTreeMap<i64, CellState>
+struct TapeState<'a> {
+    pub cell_states: BTreeMap<i64, DfgNode<'a>>
 }
 
 pub struct Optimizer {
-    state: TapeState
 }
 
-impl TapeState {
-    fn add(&mut self, offset: i64, value: i64) {
-        let cell_state = self.cell_states.get_mut(&offset);
+impl TapeState<'a> {
+    fn add(&'a mut self, offset: i64, value: i64) {
         if let Some(cell) = cell_state {
             let new_cell = match cell {
                 CellState::Value(val) => CellState::Value(*val + value),
@@ -62,9 +59,6 @@ impl TapeState {
 impl Optimizer {
     pub fn new() -> Self {
         Optimizer {
-            state: TapeState {
-                cell_states: BTreeMap::new()
-            }
         }
     }
 }
@@ -79,16 +73,6 @@ impl ir::MutVisitor for Optimizer {
     }
 
     fn visit_add(&mut self, add: &mut Instruction) {
-        if let Instruction::Add{ offset, value } = add {
-            let cell_state = self.state.get(*offset);
-            if let Some(CellState::Value(v)) = cell_state {
-                self.state.add(*offset, *value);
-                //std::mem::replace(add, Instruction::Set{ offset: *offset, value: *value + v });
-            }
-            else {
-                self.state.add(*offset, *value);
-            }
-        }
     }
 
     fn visit_set(&mut self, set: &mut Instruction) {
@@ -126,7 +110,6 @@ impl ir::MutVisitor for Optimizer {
                 std::mem::replace(l, Instruction::LinearLoop(increments));
             }
             // set cell at offset 0 to 0
-            self.state.set(0, 0);
         }
     }