mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-16 15:38:48 +00:00
Update homework 1 and 2
This commit is contained in:
11
src/opt/gvn.rs
Normal file
11
src/opt/gvn.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use crate::ir;
|
||||
use crate::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Gvn {}
|
||||
|
||||
impl Optimize<ir::TranslationUnit> for Gvn {
|
||||
fn optimize(&mut self, _code: &mut ir::TranslationUnit) -> bool {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
11
src/opt/mem2reg.rs
Normal file
11
src/opt/mem2reg.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use crate::ir;
|
||||
use crate::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Mem2reg {}
|
||||
|
||||
impl Optimize<ir::TranslationUnit> for Mem2reg {
|
||||
fn optimize(&mut self, _code: &mut ir::TranslationUnit) -> bool {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
55
src/opt/mod.rs
Normal file
55
src/opt/mod.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
mod gvn;
|
||||
mod mem2reg;
|
||||
mod simplify_cfg;
|
||||
|
||||
pub use gvn::Gvn;
|
||||
pub use mem2reg::Mem2reg;
|
||||
pub use simplify_cfg::SimplifyCfg;
|
||||
|
||||
use crate::ir;
|
||||
|
||||
pub trait Translate<S> {
|
||||
type Target;
|
||||
type Error;
|
||||
|
||||
fn translate(&mut self, source: &S) -> Result<Self::Target, Self::Error>;
|
||||
}
|
||||
|
||||
pub trait Optimize<T> {
|
||||
fn optimize(&mut self, code: &mut T) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Repeat<O> {
|
||||
inner: O,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct O0 {}
|
||||
|
||||
pub type O1 = Repeat<(SimplifyCfg, (Mem2reg, Gvn))>;
|
||||
|
||||
impl Optimize<ir::TranslationUnit> for O0 {
|
||||
fn optimize(&mut self, _code: &mut ir::TranslationUnit) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, O1: Optimize<T>, O2: Optimize<T>> Optimize<T> for (O1, O2) {
|
||||
fn optimize(&mut self, code: &mut T) -> bool {
|
||||
let changed1 = self.0.optimize(code);
|
||||
let changed2 = self.1.optimize(code);
|
||||
changed1 || changed2
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, O: Optimize<T>> Optimize<T> for Repeat<O> {
|
||||
fn optimize(&mut self, code: &mut T) -> bool {
|
||||
if !self.inner.optimize(code) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while self.inner.optimize(code) {}
|
||||
true
|
||||
}
|
||||
}
|
||||
48
src/opt/simplify_cfg.rs
Normal file
48
src/opt/simplify_cfg.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::ir::*;
|
||||
use crate::*;
|
||||
|
||||
pub type SimplifyCfg = Repeat<(SimplifyCfgConstProp, (SimplifyCfgReach, SimplifyCfgMerge))>;
|
||||
|
||||
impl Optimize<TranslationUnit> for SimplifyCfg {
|
||||
fn optimize(&mut self, code: &mut TranslationUnit) -> bool {
|
||||
code.decls.iter_mut().any(|(_, decl)| self.optimize(decl))
|
||||
}
|
||||
}
|
||||
|
||||
impl Optimize<Declaration> for SimplifyCfg {
|
||||
fn optimize(&mut self, code: &mut Declaration) -> bool {
|
||||
let (_fsig, fdef) = some_or!(code.get_function_mut(), return false);
|
||||
let fdef = some_or!(fdef, return false);
|
||||
self.optimize(fdef)
|
||||
}
|
||||
}
|
||||
|
||||
/// Simplifies block exits by propagating constants.
|
||||
#[derive(Default)]
|
||||
pub struct SimplifyCfgConstProp {}
|
||||
|
||||
/// Retains only those blocks that are reachable from the init.
|
||||
#[derive(Default)]
|
||||
pub struct SimplifyCfgReach {}
|
||||
|
||||
/// Merges two blocks if a block is pointed to only by another
|
||||
#[derive(Default)]
|
||||
pub struct SimplifyCfgMerge {}
|
||||
|
||||
impl Optimize<FunctionDefinition> for SimplifyCfgConstProp {
|
||||
fn optimize(&mut self, _code: &mut FunctionDefinition) -> bool {
|
||||
todo!("homework 3")
|
||||
}
|
||||
}
|
||||
|
||||
impl Optimize<FunctionDefinition> for SimplifyCfgReach {
|
||||
fn optimize(&mut self, _code: &mut FunctionDefinition) -> bool {
|
||||
todo!("homework 3")
|
||||
}
|
||||
}
|
||||
|
||||
impl Optimize<FunctionDefinition> for SimplifyCfgMerge {
|
||||
fn optimize(&mut self, _code: &mut FunctionDefinition) -> bool {
|
||||
todo!("homework 3")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user