1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::frontend::ao::{
engine::{
Engine,
Value,
},
Function,
Op,
};
pub(crate) struct EliminateArithmeticPass {}
impl EliminateArithmeticPass {
#[allow(clippy::new_without_default)]
pub(crate) fn new() -> Self {
Self {}
}
pub(crate) fn run(&mut self, func: &mut Function) -> Result<(), String> {
for bb in func.cfg_mut().iter_basic_blocks_mut() {
let vars = {
let mut engine = Engine::<()>::attach(bb, None);
if let Err(err) = engine.execute() {
return Err(format!("EngineError: {}", err));
}
engine.vars().to_owned()
};
bb.set_cursor(0);
while let Some(op) = bb.cursor_op() {
#[allow(clippy::single_match)]
match op {
Op::Add {
dst,
src1,
src2,
..
} => {
if let Value::Integer(0) = &vars[src1.id()] {
bb.replace_op(Op::Copy {
dst: *dst,
src: *src2,
});
} else if let Value::Integer(0) = &vars[src2.id()] {
bb.replace_op(Op::Copy {
dst: *dst,
src: *src1,
});
}
},
//TODO: maybe support more arithmetic operators
_ => {},
}
if !bb.move_cursor_forward() {
break;
}
}
}
Ok(())
}
}