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

peephole_opt.rs 1.99 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 vm::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
        if cf.mc.is_move(inst) && !cf.mc.is_using_mem_op(inst) {
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
            cf.mc.trace_inst(inst);
            
            let src : MuID = {
                let uses = cf.mc.get_inst_reg_uses(inst);
                if uses.len() != 1 {
                    // moving immediate to register, its not redundant
                    return;
                }                
                uses[0]
            };
            let dst : MuID = cf.mc.get_inst_reg_defines(inst)[0];
            
            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
                cf.mc.set_inst_nop(inst);
            }
        }
    }
}

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
62
63
64
65
66
67
68
        
        for i in 0..cf.mc.number_of_insts() {
            self.remove_redundant_move(i, &mut cf);
        }
        
        trace!("after peephole optimization:");
        cf.mc.trace_mc();
    }
69
}