]> git.lizzy.rs Git - rust.git/blob - src/analyze.rs
Rustfmt
[rust.git] / src / analyze.rs
1 use crate::prelude::*;
2
3 use rustc::mir::StatementKind::*;
4
5 bitflags! {
6     pub struct Flags: u8 {
7         const NOT_SSA = 0b00000001;
8     }
9 }
10
11 pub fn analyze<'a, 'tcx: 'a>(fx: &FunctionCx<'a, 'tcx>) -> HashMap<Local, Flags> {
12     let mut flag_map = HashMap::new();
13
14     for local in fx.mir.local_decls.indices() {
15         flag_map.insert(local, Flags::empty());
16     }
17
18     not_ssa(&mut flag_map, RETURN_PLACE);
19
20     for (local, local_decl) in fx.mir.local_decls.iter_enumerated() {
21         if fx.cton_type(local_decl.ty).is_none() {
22             not_ssa(&mut flag_map, local);
23         }
24     }
25
26     for bb in fx.mir.basic_blocks().iter() {
27         for stmt in bb.statements.iter() {
28             match &stmt.kind {
29                 Assign(_, Rvalue::Ref(_, _, place)) => analyze_non_ssa_place(&mut flag_map, place),
30                 _ => {}
31             }
32         }
33
34         match &bb.terminator().kind {
35             TerminatorKind::Call {
36                 destination: Some((place, _)),
37                 ..
38             } => analyze_non_ssa_place(&mut flag_map, place),
39             _ => {}
40         }
41     }
42
43     flag_map
44 }
45
46 fn analyze_non_ssa_place(flag_map: &mut HashMap<Local, Flags>, place: &Place) {
47     match place {
48         Place::Local(local) => not_ssa(flag_map, local),
49         _ => {}
50     }
51 }
52
53 fn not_ssa<L: ::std::borrow::Borrow<Local>>(flag_map: &mut HashMap<Local, Flags>, local: L) {
54     *flag_map.get_mut(local.borrow()).unwrap() |= Flags::NOT_SSA;
55 }