To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

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
}