-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
133 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::Optimizations::Pass; | ||
use crate::IR::{ir::Br, ir::BrCond, BlockId, Var}; | ||
|
||
/// ## Pass DeadBlockElimination <br> | ||
/// deletes unused blocks | ||
pub(crate) struct DeadBlockElimination { | ||
} | ||
|
||
/// Creates a new DeadBlockElimination pass which is heap allocated | ||
pub fn DeadBlockElimination() -> Box<dyn Pass> { | ||
Box::from( DeadBlockElimination {} ) | ||
} | ||
|
||
impl Pass for DeadBlockElimination { | ||
fn run_func(&self, func: &mut crate::prelude::Function) { | ||
let mut used_blocks = Vec::new(); | ||
|
||
// CHECK FOR ALL USED BLOCKS | ||
|
||
for block in &func.blocks { | ||
for node in &block.nodes { | ||
if let Some(br) = node.as_any().downcast_ref::<Br<BlockId>>() { | ||
used_blocks.push(br.inner1.name.to_owned()); | ||
} | ||
|
||
if let Some(br) = node.as_any().downcast_ref::<BrCond<Var, BlockId, BlockId>>() { | ||
used_blocks.push(br.inner2.name.to_owned()); | ||
used_blocks.push(br.inner3.name.to_owned()); | ||
} | ||
} | ||
} | ||
|
||
// REMOVE UNUSED BLOCKS | ||
|
||
let mut index = 0; | ||
|
||
for block in func.blocks.clone() { | ||
if !used_blocks.contains(&block.name) && index != 0 { // do not remove first block | ||
func.blocks.remove(index); | ||
} | ||
|
||
index += 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
use crate::IR::Block; | ||
use crate::IR::{Block, Function}; | ||
|
||
/// The trait all Passes need to implement | ||
pub trait Pass { | ||
/// Returns the pass | ||
fn run(&self, block: &mut Block); | ||
/// Runs the pass on a block | ||
fn run(&self, _block: &mut Block) {} | ||
|
||
/// Runs the pass on the entire function | ||
fn run_func(&self, _func: &mut Function) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# RUN: | ||
cargo run -p ylc -- -in=%s -O -fmt -passes=cp | ||
# IN: | ||
|
||
define i32 @main() { | ||
entry: | ||
%0 = i32 1 | ||
br cond %0 yes, no | ||
|
||
yes: | ||
ret i32 0 | ||
|
||
no: | ||
ret i32 0 | ||
} | ||
|
||
# STDERR: | ||
define i32 @main() { | ||
entry: | ||
%0 = i32 1 | ||
br yes | ||
|
||
yes: | ||
ret i32 0 | ||
|
||
no: | ||
ret i32 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# RUN: | ||
cargo run -p ylc -- -in=%s -passes=dbe -fmt | ||
# IN: | ||
|
||
define i32 @main() { | ||
entry: | ||
br ret | ||
|
||
ret: | ||
ret i32 5 | ||
|
||
should_be_removed: | ||
ret i32 1 | ||
} | ||
|
||
# STDOUT: | ||
define i32 @main() { | ||
entry: | ||
br ret | ||
|
||
ret: | ||
ret i32 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters