From 9a2dd840d2f95630edf8b17172ee8e9ccf9552ea Mon Sep 17 00:00:00 2001 From: Jeehoon Kang Date: Sun, 3 May 2020 06:01:46 +0000 Subject: [PATCH] Add deadcode examples --- examples/deadcode/deadcode.input.ir | 39 ++++++++++++++++++++++++++++ examples/deadcode/deadcode.output.ir | 35 +++++++++++++++++++++++++ src/ir/parse.rs | 2 +- src/opt/deadcode.rs | 2 +- src/opt/mod.rs | 2 +- tests/test_examples.rs | 9 +++++++ 6 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 examples/deadcode/deadcode.input.ir create mode 100644 examples/deadcode/deadcode.output.ir diff --git a/examples/deadcode/deadcode.input.ir b/examples/deadcode/deadcode.input.ir new file mode 100644 index 0000000..a2ca746 --- /dev/null +++ b/examples/deadcode/deadcode.input.ir @@ -0,0 +1,39 @@ +fun unit @sink { +init: + bid: b0 + allocations: + +block b0: + %b0:p0:unit + ret unit:unit +} + +fun i32 @deadcode { +init: + bid: b0 + allocations: + %l0:i32:x + %l1:i32:y + +block b0: + %b0:i0:i32 = add 100:i32 200:i32 + %b0:i1:i32 = add %b0:i0:i32 300:i32 + %b0:i2:i32 = add %b0:i1:i32 400:i32 + %b0:i3:unit = store %l1:i32 %b0:i0:i32 + + br undef:i1 b1() b2() + +block b1: + %b1:i0:unit = nop + %b1:i1:unit = call @sink(%b1:i0:unit) + + j b3() + +block b2: + %b2:i0:unit = store %l1:i32 42:i32 + + j b3() + +block b3: + ret 0:i32 +} diff --git a/examples/deadcode/deadcode.output.ir b/examples/deadcode/deadcode.output.ir new file mode 100644 index 0000000..d5b3892 --- /dev/null +++ b/examples/deadcode/deadcode.output.ir @@ -0,0 +1,35 @@ +fun unit @sink { +init: + bid: b0 + allocations: + +block b0: + %b0:p0:unit + ret unit:unit +} + +fun i32 @deadcode { +init: + bid: b0 + allocations: + %l0:i32:y + +block b0: + %b0:i0:i32 = add 100:i32 200:i32 + %b0:i1:unit = store %l0:i32 %b0:i0:i32 + + br undef:i1 b1() b2() + +block b1: + %b1:i0:unit = call @sink(unit:unit) + + j b3() + +block b2: + %b2:i0:unit = store %l0:i32 42:i32 + + j b3() + +block b3: + ret 0:i32 +} diff --git a/src/ir/parse.rs b/src/ir/parse.rs index 6bedc45..a71315f 100644 --- a/src/ir/parse.rs +++ b/src/ir/parse.rs @@ -238,7 +238,7 @@ peg::parser! { } / "unit" { - Constant::undef(Dtype::unit()) // TODO + Constant::unit() } / "" { diff --git a/src/opt/deadcode.rs b/src/opt/deadcode.rs index 85be8e7..d36faa2 100644 --- a/src/opt/deadcode.rs +++ b/src/opt/deadcode.rs @@ -2,7 +2,7 @@ use crate::ir::*; use crate::opt::FunctionPass; use crate::*; -pub type Deadcode = FunctionPass; +pub type Deadcode = FunctionPass>; #[derive(Default)] pub struct DeadcodeInner {} diff --git a/src/opt/mod.rs b/src/opt/mod.rs index 7e10fa4..2be97fb 100644 --- a/src/opt/mod.rs +++ b/src/opt/mod.rs @@ -20,7 +20,7 @@ pub trait Optimize { } pub type O0 = Null; -pub type O1 = Repeat<(SimplifyCfg, (Mem2reg, (Deadcode, Gvn)))>; +pub type O1 = Repeat<(SimplifyCfg, (Mem2reg, (Gvn, Deadcode)))>; #[derive(Default)] pub struct Null {} diff --git a/tests/test_examples.rs b/tests/test_examples.rs index 0ee2a34..0cc7d75 100644 --- a/tests/test_examples.rs +++ b/tests/test_examples.rs @@ -92,3 +92,12 @@ fn test_examples_gvn() { &mut Gvn::default(), ); } + +#[test] +fn test_examples_deadcode() { + test_opt( + &Path::new("examples/deadcode/deadcode.input.ir"), + &Path::new("examples/deadcode/deadcode.output.ir"), + &mut Deadcode::default(), + ); +}