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

def_use.rs 1.59 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
2
3
4
5
use ast::ir::*;
use vm::context::VMContext;

use compiler::CompilerPass;

qinsoon's avatar
qinsoon committed
6
pub struct DefUse {
qinsoon's avatar
qinsoon committed
7
8
9
    name: &'static str,
}

qinsoon's avatar
qinsoon committed
10
11
12
impl DefUse {
    pub fn new() -> DefUse {
        DefUse{name: "Def-Use Pass"}
qinsoon's avatar
qinsoon committed
13
14
15
    }
}

qinsoon's avatar
qinsoon committed
16
impl CompilerPass for DefUse {
qinsoon's avatar
qinsoon committed
17
18
19
20
    fn name(&self) -> &'static str {
        self.name
    }
    
qinsoon's avatar
qinsoon committed
21
    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
22
23
24
25
26
27
28
29
    fn visit_inst(&mut self, vm_context: &VMContext, func_context: &mut FunctionContext, node: &mut TreeNode) {
        match node.v {
            TreeNode_::Instruction(ref inst) => {
                for op in inst.list_operands() {
                    match op.v {
                        TreeNode_::Value(ref val) => {
                            match val.v {
                                Value_::SSAVar(ref id) => {
qinsoon's avatar
qinsoon committed
30
                                    let entry = func_context.values.get_mut(id).unwrap();
qinsoon's avatar
qinsoon committed
31
32
33
34
35
36
37
38
39
40
41
42
43
                                    entry.use_count.set(entry.use_count.get() + 1);
                                },
                                _ => {} // dont worry about constants
                            }
                        },
                        _ => {} // dont worry about instruction
                    }
                }
            },
            _ => panic!("expected instruction node in visit_inst()")
        }
    }
    
qinsoon's avatar
qinsoon committed
44
    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
45
46
47
48
49
50
    fn finish_function(&mut self, vm_context: &VMContext, func: &mut MuFunction) {
        debug!("check use count for variables");
        
        for entry in func.context.values.values() {
            debug!("{}({}): {}", entry.tag, entry.id, entry.use_count.get())
        }
qinsoon's avatar
qinsoon committed
51
52
    }
}