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.6% of users enabled 2FA.

peephole_opt.rs 2.1 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

qinsoon's avatar
qinsoon committed
6 7
use std::any::Any;

8 9 10 11 12 13 14 15 16 17 18 19
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
20 21
        if cf.mc().is_move(inst) && !cf.mc().is_using_mem_op(inst) {
            cf.mc().trace_inst(inst);
22 23
            
            let src : MuID = {
qinsoon's avatar
qinsoon committed
24
                let uses = cf.mc().get_inst_reg_uses(inst);
qinsoon's avatar
qinsoon committed
25
                if uses.len() == 0 {
26 27 28 29 30
                    // moving immediate to register, its not redundant
                    return;
                }                
                uses[0]
            };
qinsoon's avatar
qinsoon committed
31
            let dst : MuID = cf.mc().get_inst_reg_defines(inst)[0];
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
            
            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
49
                cf.mc_mut().set_inst_nop(inst);
50 51 52 53 54 55 56 57 58
            }
        }
    }
}

impl CompilerPass for PeepholeOptimization {
    fn name(&self) -> &'static str {
        self.name
    }
qinsoon's avatar
qinsoon committed
59 60 61 62

    fn as_any(&self) -> &Any {
        self
    }
63
    
qinsoon's avatar
qinsoon committed
64 65
    fn visit_function(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
        let compiled_funcs = vm.compiled_funcs().read().unwrap();
qinsoon's avatar
qinsoon committed
66
        let mut cf = compiled_funcs.get(&func.id()).unwrap().write().unwrap();
67
        
qinsoon's avatar
qinsoon committed
68
        for i in 0..cf.mc().number_of_insts() {
69 70 71 72
            self.remove_redundant_move(i, &mut cf);
        }
        
        trace!("after peephole optimization:");
qinsoon's avatar
qinsoon committed
73
        cf.mc().trace_mc();
74
    }
75
}