]> git.lizzy.rs Git - rust.git/blob - src/analyze.rs
Merge pull request #63 from bjorn3/dependabot/cargo/cranelift-191638e
[rust.git] / src / analyze.rs
1 use crate::prelude::*;
2
3 use crate::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, impl Backend>) -> 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(_, rval) => {
30                     match &**rval {
31                         Rvalue::Ref(_, _, place) => analyze_non_ssa_place(&mut flag_map, place),
32                         _ => {}
33                     }
34                 }
35                 _ => {}
36             }
37         }
38
39         match &bb.terminator().kind {
40             TerminatorKind::Call {
41                 destination: Some((place, _)),
42                 ..
43             } => analyze_non_ssa_place(&mut flag_map, place),
44             _ => {}
45         }
46     }
47
48     flag_map
49 }
50
51 fn analyze_non_ssa_place(flag_map: &mut HashMap<Local, Flags>, place: &Place) {
52     match place {
53         Place::Local(local) => not_ssa(flag_map, local),
54         _ => {}
55     }
56 }
57
58 fn not_ssa<L: ::std::borrow::Borrow<Local>>(flag_map: &mut HashMap<Local, Flags>, local: L) {
59     *flag_map.get_mut(local.borrow()).unwrap() |= Flags::NOT_SSA;
60 }