control_flow.rs 8.29 KB
Newer Older
1 2
use ast::ir::*;
use ast::inst::Instruction_::*;
3
use utils::vec_utils::as_str as vector_as_str;
qinsoon's avatar
qinsoon committed
4
use vm::VM;
5 6
use compiler::CompilerPass;

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

9 10 11 12 13 14 15 16 17 18
pub struct ControlFlowAnalysis {
    name: &'static str
}

impl ControlFlowAnalysis {
    pub fn new() -> ControlFlowAnalysis {
        ControlFlowAnalysis{name: "Control Flow Analysis"}
    }
}

qinsoon's avatar
qinsoon committed
19
fn check_edge_kind(target: MuID, stack: &Vec<MuID>) -> EdgeKind {
20 21 22 23 24 25 26
    if stack.contains(&target) {
        EdgeKind::Backward
    } else {
        EdgeKind::Forward
    }
}

qinsoon's avatar
qinsoon committed
27
fn new_edge(cur: MuID, edge: BlockEdge, stack: &mut Vec<MuID>, visited: &mut Vec<MuID>, func: &mut MuFunctionVersion) {
28 29
    // add current block to target's predecessors
    {
qinsoon's avatar
qinsoon committed
30
        let target = func.content.as_mut().unwrap().get_block_mut(edge.target);
31 32
        target.control_flow.preds.push(cur);
    }
33

qinsoon's avatar
qinsoon committed
34 35
    // add target as current block's successors and start dfs
    let succ = edge.target;
36
    {
qinsoon's avatar
qinsoon committed
37
        let cur = func.content.as_mut().unwrap().get_block_mut(cur);
38 39
        cur.control_flow.succs.push(edge);
    }
qinsoon's avatar
qinsoon committed
40 41 42
    if !visited.contains(&succ) {
        dfs(succ, stack, visited, func);
    }
43

44 45 46 47 48 49 50
}

const WATCHPOINT_DISABLED_CHANCE : f32 = 0.9f32;

const NORMAL_RESUME_CHANCE       : f32 = 0.6f32;
const EXN_RESUME_CHANCE          : f32 = 1f32 - NORMAL_RESUME_CHANCE;

qinsoon's avatar
qinsoon committed
51
fn dfs(cur: MuID, stack: &mut Vec<MuID>, visited: &mut Vec<MuID>, func: &mut MuFunctionVersion) {
qinsoon's avatar
qinsoon committed
52 53 54
    trace!("dfs visiting block {}", cur);
    trace!("current stack: {:?}", stack);
    trace!("current visited: {:?}", visited);
55

56 57
    stack.push(cur);
    visited.push(cur);
58 59

    // find all the successors for current block, and push them to the stack
60
    let out_edges : Vec<BlockEdge> = {
61
        let cur = func.content.as_mut().unwrap().get_block_mut(cur);
62 63
        let ref body = cur.content.as_ref().unwrap().body;
        let last_inst = body.last().unwrap();
64

65 66 67 68 69 70 71 72 73 74
        match last_inst.v {
            TreeNode_::Instruction(ref inst) => {
                match inst.v {
                    // unconditional branch, definitely branch to the target
                    Branch1(ref dest) => vec![BlockEdge{
                        target: dest.target,
                        kind: check_edge_kind(dest.target, stack),
                        is_exception: false,
                        probability: 1.0f32
                    }],
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
                    // conditional branch
                    Branch2{ref true_dest, ref false_dest, true_prob, ..} => vec![
                        BlockEdge{
                            target: true_dest.target,
                            kind: check_edge_kind(true_dest.target, stack),
                            is_exception: false,
                            probability: true_prob
                        },
                        BlockEdge{
                            target: false_dest.target,
                            kind: check_edge_kind(false_dest.target, stack),
                            is_exception: false,
                            probability: 1.0f32 - true_prob
                        }
                    ],
91

92 93 94
                    // watchpoints
                    Watchpoint{ref id, ref disable_dest, ref resume} => {
                        let ref normal = resume.normal_dest;
95 96
                        let ref exn    = resume.exn_dest;

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                        if id.is_none() {
                            // unconditional trap
                            vec![
                                BlockEdge{
                                    target: normal.target,
                                    kind: check_edge_kind(normal.target, stack),
                                    is_exception: false,
                                    probability: 1.0f32 * NORMAL_RESUME_CHANCE
                                },
                                BlockEdge{
                                    target: exn.target,
                                    kind: check_edge_kind(exn.target, stack),
                                    is_exception: true,
                                    probability: 1.0f32 * EXN_RESUME_CHANCE
                                }
                            ]
                        } else {
                            // watchpoint. jump to disable_dest when disabled. otherwise trap
                            vec![
                                BlockEdge{
                                    target: disable_dest.as_ref().unwrap().target,
                                    kind: check_edge_kind(disable_dest.as_ref().unwrap().target, stack),
                                    is_exception: false,
                                    probability: WATCHPOINT_DISABLED_CHANCE
                                },
                                BlockEdge{
                                    target: normal.target,
                                    kind: check_edge_kind(normal.target, stack),
                                    is_exception: false,
                                    probability: (1.0f32 - WATCHPOINT_DISABLED_CHANCE) * NORMAL_RESUME_CHANCE
                                },
                                BlockEdge{
                                    target: exn.target,
                                    kind: check_edge_kind(exn.target, stack),
                                    is_exception: true,
                                    probability: (1.0f32 - WATCHPOINT_DISABLED_CHANCE) * EXN_RESUME_CHANCE
                                }
                            ]
                        }
                    },
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                    // wpbranch
                    WPBranch{ref disable_dest, ref enable_dest, ..} => vec![
                        BlockEdge{
                            target: disable_dest.target,
                            kind: check_edge_kind(disable_dest.target, stack),
                            is_exception: false,
                            probability: WATCHPOINT_DISABLED_CHANCE
                        },
                        BlockEdge{
                            target: enable_dest.target,
                            kind: check_edge_kind(enable_dest.target, stack),
                            is_exception: false,
                            probability: 1.0f32 - WATCHPOINT_DISABLED_CHANCE
                        }
                    ],
153

154
                    // call
155
                    Call{ref resume, ..}
156 157 158 159
                    | SwapStack{ref resume, ..}
                    | ExnInstruction{ref resume, ..} => {
                        let ref normal = resume.normal_dest;
                        let ref exn    = resume.exn_dest;
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
                        vec![
                                BlockEdge{
                                    target: normal.target,
                                    kind: check_edge_kind(normal.target, stack),
                                    is_exception: false,
                                    probability: 1.0f32 * NORMAL_RESUME_CHANCE
                                },
                                BlockEdge{
                                    target: exn.target,
                                    kind: check_edge_kind(exn.target, stack),
                                    is_exception: true,
                                    probability: 1.0f32 * EXN_RESUME_CHANCE
                                }

                        ]
                    },
177

178 179 180 181 182 183
                    _ => vec![]
                }
            },
            _ => panic!("expected an instruction")
        }
    };
184

qinsoon's avatar
qinsoon committed
185
    trace!("out edges for {}: {}", cur, vector_as_str(&out_edges));
186

187
    for edge in out_edges {
qinsoon's avatar
qinsoon committed
188
        new_edge(cur, edge, stack, visited, func);
189
    }
190

191 192 193 194 195 196 197
    stack.pop();
}

impl CompilerPass for ControlFlowAnalysis {
    fn name(&self) -> &'static str {
        self.name
    }
198

qinsoon's avatar
qinsoon committed
199 200 201 202
    fn as_any(&self) -> &Any {
        self
    }

203
    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
204
    fn visit_function(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
qinsoon's avatar
qinsoon committed
205 206
        let mut stack   : Vec<MuID> = vec![];
        let mut visited : Vec<MuID> = vec![];
207

208 209
        dfs(func.content.as_ref().unwrap().entry, &mut stack, &mut visited, func);
    }
210

211
    #[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
212
    fn finish_function(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
qinsoon's avatar
qinsoon committed
213
        debug!("check control flow for {}", func);
214

215 216
        for entry in func.content.as_ref().unwrap().blocks.iter() {
            debug!("block {}", entry.0);
217

218 219 220
            debug!("{}", entry.1.control_flow);
        }
    }
221
}