3 use rustc::mir::visit::{MutVisitor, PlaceContext, Visitor};
5 Body, BodyAndCache, Local, Location, PlaceElem, ReadOnlyBodyAndCache, VarDebugInfo,
8 use rustc_index::vec::IndexVec;
11 pub struct DefUseAnalysis {
12 info: IndexVec<Local, Info>,
17 // FIXME(eddyb) use smallvec where possible.
18 pub defs_and_uses: Vec<Use>,
19 var_debug_info_indices: Vec<usize>,
24 pub context: PlaceContext,
25 pub location: Location,
29 pub fn new(body: &Body<'_>) -> DefUseAnalysis {
30 DefUseAnalysis { info: IndexVec::from_elem_n(Info::new(), body.local_decls.len()) }
33 pub fn analyze(&mut self, body: ReadOnlyBodyAndCache<'_, '_>) {
36 let mut finder = DefUseFinder {
37 info: mem::take(&mut self.info),
38 var_debug_info_index: 0,
39 in_var_debug_info: false,
41 finder.visit_body(body);
42 self.info = finder.info
46 for info in &mut self.info {
51 pub fn local_info(&self, local: Local) -> &Info {
55 fn mutate_defs_and_uses(
58 body: &mut BodyAndCache<'tcx>,
62 let mut visitor = MutateUseVisitor::new(local, new_local, tcx);
63 let info = &self.info[local];
64 for place_use in &info.defs_and_uses {
65 visitor.visit_location(body, place_use.location)
67 // Update debuginfo as well, alongside defs/uses.
68 for &i in &info.var_debug_info_indices {
69 visitor.visit_var_debug_info(&mut body.var_debug_info[i]);
73 // FIXME(pcwalton): this should update the def-use chains.
74 pub fn replace_all_defs_and_uses_with(
77 body: &mut BodyAndCache<'tcx>,
81 self.mutate_defs_and_uses(local, body, new_local, tcx)
86 info: IndexVec<Local, Info>,
87 var_debug_info_index: usize,
88 in_var_debug_info: bool,
91 impl Visitor<'_> for DefUseFinder {
92 fn visit_local(&mut self, &local: &Local, context: PlaceContext, location: Location) {
93 let info = &mut self.info[local];
94 if self.in_var_debug_info {
95 info.var_debug_info_indices.push(self.var_debug_info_index);
97 info.defs_and_uses.push(Use { context, location });
100 fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) {
101 assert!(!self.in_var_debug_info);
102 self.in_var_debug_info = true;
103 self.super_var_debug_info(var_debug_info);
104 self.in_var_debug_info = false;
105 self.var_debug_info_index += 1;
111 Info { defs_and_uses: vec![], var_debug_info_indices: vec![] }
114 fn clear(&mut self) {
115 self.defs_and_uses.clear();
116 self.var_debug_info_indices.clear();
119 pub fn def_count(&self) -> usize {
120 self.defs_and_uses.iter().filter(|place_use| place_use.context.is_mutating_use()).count()
123 pub fn def_count_not_including_drop(&self) -> usize {
124 self.defs_not_including_drop().count()
127 pub fn defs_not_including_drop(&self) -> impl Iterator<Item = &Use> {
130 .filter(|place_use| place_use.context.is_mutating_use() && !place_use.context.is_drop())
133 pub fn use_count(&self) -> usize {
134 self.defs_and_uses.iter().filter(|place_use| place_use.context.is_nonmutating_use()).count()
138 struct MutateUseVisitor<'tcx> {
144 impl MutateUseVisitor<'tcx> {
145 fn new(query: Local, new_local: Local, tcx: TyCtxt<'tcx>) -> MutateUseVisitor<'tcx> {
146 MutateUseVisitor { query, new_local, tcx }
150 impl MutVisitor<'tcx> for MutateUseVisitor<'tcx> {
151 fn tcx(&self) -> TyCtxt<'tcx> {
155 fn visit_local(&mut self, local: &mut Local, _context: PlaceContext, _location: Location) {
156 if *local == self.query {
157 *local = self.new_local;
161 fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
163 PlaceElem::Index(local) if *local == self.query => {
164 Some(PlaceElem::Index(self.new_local))