]> git.lizzy.rs Git - rust.git/blob - src/analyze.rs
fmt: Run cargo fmt since it is available
[rust.git] / src / analyze.rs
1 use crate::prelude::*;
2
3 use rustc_index::vec::IndexVec;
4 use rustc_middle::mir::StatementKind::*;
5
6 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
7 pub(crate) enum SsaKind {
8     NotSsa,
9     Ssa,
10 }
11
12 pub(crate) fn analyze(fx: &FunctionCx<'_, '_, impl Backend>) -> IndexVec<Local, SsaKind> {
13     let mut flag_map = fx
14         .mir
15         .local_decls
16         .iter()
17         .map(|local_decl| {
18             let ty = fx.monomorphize(&local_decl.ty);
19             if fx.clif_type(ty).is_some() || fx.clif_pair_type(ty).is_some() {
20                 SsaKind::Ssa
21             } else {
22                 SsaKind::NotSsa
23             }
24         })
25         .collect::<IndexVec<Local, SsaKind>>();
26
27     for bb in fx.mir.basic_blocks().iter() {
28         for stmt in bb.statements.iter() {
29             match &stmt.kind {
30                 Assign(place_and_rval) => match &place_and_rval.1 {
31                     Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => {
32                         not_ssa(&mut flag_map, place.local)
33                     }
34                     _ => {}
35                 },
36                 _ => {}
37             }
38         }
39
40         match &bb.terminator().kind {
41             TerminatorKind::Call { destination, .. } => {
42                 if let Some((dest_place, _dest_bb)) = destination {
43                     let dest_layout = fx
44                         .layout_of(fx.monomorphize(&dest_place.ty(&fx.mir.local_decls, fx.tcx).ty));
45                     if !crate::abi::can_return_to_ssa_var(fx.tcx, dest_layout) {
46                         not_ssa(&mut flag_map, dest_place.local)
47                     }
48                 }
49             }
50             _ => {}
51         }
52     }
53
54     flag_map
55 }
56
57 fn not_ssa(flag_map: &mut IndexVec<Local, SsaKind>, local: Local) {
58     flag_map[local] = SsaKind::NotSsa;
59 }