WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

peephole_opt.rs 2.03 KB
Newer Older
1
2
use compiler::CompilerPass;
use ast::ir::*;
qinsoon's avatar
qinsoon committed
3
use vm::VM;
qinsoon's avatar
qinsoon committed
4
use compiler::machine_code::CompiledFunction;
5
6
7
8
9
10
11
12
13
14
15
16
17

pub struct PeepholeOptimization {
    name: &'static str
}

impl PeepholeOptimization {
    pub fn new() -> PeepholeOptimization {
        PeepholeOptimization {
            name: "Peephole Optimization"
        }
    }
    
    pub fn remove_redundant_move(&mut self, inst: usize, cf: &mut CompiledFunction) {
qinsoon's avatar
qinsoon committed
18
19
        if cf.mc().is_move(inst) && !cf.mc().is_using_mem_op(inst) {
            cf.mc().trace_inst(inst);
20
21
            
            let src : MuID = {
qinsoon's avatar
qinsoon committed
22
                let uses = cf.mc().get_inst_reg_uses(inst);
qinsoon's avatar
qinsoon committed
23
                if uses.len() == 0 {
24
25
26
27
28
                    // moving immediate to register, its not redundant
                    return;
                }                
                uses[0]
            };
qinsoon's avatar
qinsoon committed
29
            let dst : MuID = cf.mc().get_inst_reg_defines(inst)[0];
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
            
            let src_machine_reg : MuID = {
                match cf.temps.get(&src) {
                    Some(reg) => *reg,
                    None => src
                }
            };
            let dst_machine_reg : MuID = {
                match cf.temps.get(&dst) {
                    Some(reg) => *reg,
                    None => dst
                }
            };
            
            if src_machine_reg == dst_machine_reg {
                trace!("Redundant! removed");
                // redundant, remove this move
qinsoon's avatar
qinsoon committed
47
                cf.mc_mut().set_inst_nop(inst);
48
49
50
51
52
53
54
55
56
57
            }
        }
    }
}

impl CompilerPass for PeepholeOptimization {
    fn name(&self) -> &'static str {
        self.name
    }
    
qinsoon's avatar
qinsoon committed
58
59
    fn visit_function(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
        let compiled_funcs = vm.compiled_funcs().read().unwrap();
qinsoon's avatar
qinsoon committed
60
        let mut cf = compiled_funcs.get(&func.id()).unwrap().write().unwrap();
61
        
qinsoon's avatar
qinsoon committed
62
        for i in 0..cf.mc().number_of_insts() {
63
64
65
66
            self.remove_redundant_move(i, &mut cf);
        }
        
        trace!("after peephole optimization:");
qinsoon's avatar
qinsoon committed
67
        cf.mc().trace_mc();
68
    }
69
}