4 Body, BodyAndCache, Local, Location, PlaceElem, ReadOnlyBodyAndCache, VarDebugInfo,
6 use rustc::mir::visit::{PlaceContext, MutVisitor, Visitor};
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 {
31 info: IndexVec::from_elem_n(Info::new(), body.local_decls.len()),
35 pub fn analyze(&mut self, body: ReadOnlyBodyAndCache<'_, '_>) {
38 let mut finder = DefUseFinder {
39 info: mem::take(&mut self.info),
40 var_debug_info_index: 0,
41 in_var_debug_info: false,
43 finder.visit_body(body);
44 self.info = finder.info
48 for info in &mut self.info {
53 pub fn local_info(&self, local: Local) -> &Info {
57 fn mutate_defs_and_uses(
60 body: &mut BodyAndCache<'tcx>,
64 let mut visitor = MutateUseVisitor::new(local, new_local, tcx);
65 let info = &self.info[local];
66 for place_use in &info.defs_and_uses {
67 visitor.visit_location(body, place_use.location)
69 // Update debuginfo as well, alongside defs/uses.
70 for &i in &info.var_debug_info_indices {
71 visitor.visit_var_debug_info(&mut body.var_debug_info[i]);
75 // FIXME(pcwalton): this should update the def-use chains.
76 pub fn replace_all_defs_and_uses_with(&self,
78 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,
94 context: PlaceContext,
96 let info = &mut self.info[local];
97 if self.in_var_debug_info {
98 info.var_debug_info_indices.push(self.var_debug_info_index);
100 info.defs_and_uses.push(Use {
106 fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) {
107 assert!(!self.in_var_debug_info);
108 self.in_var_debug_info = true;
109 self.super_var_debug_info(var_debug_info);
110 self.in_var_debug_info = false;
111 self.var_debug_info_index += 1;
118 defs_and_uses: vec![],
119 var_debug_info_indices: vec![],
123 fn clear(&mut self) {
124 self.defs_and_uses.clear();
125 self.var_debug_info_indices.clear();
128 pub fn def_count(&self) -> usize {
129 self.defs_and_uses.iter().filter(|place_use| place_use.context.is_mutating_use()).count()
132 pub fn def_count_not_including_drop(&self) -> usize {
133 self.defs_not_including_drop().count()
136 pub fn defs_not_including_drop(
138 ) -> impl Iterator<Item=&Use> {
139 self.defs_and_uses.iter().filter(|place_use| {
140 place_use.context.is_mutating_use() && !place_use.context.is_drop()
144 pub fn use_count(&self) -> usize {
145 self.defs_and_uses.iter().filter(|place_use| {
146 place_use.context.is_nonmutating_use()
151 struct MutateUseVisitor<'tcx> {
157 impl MutateUseVisitor<'tcx> {
162 ) -> MutateUseVisitor<'tcx> {
163 MutateUseVisitor { query, new_local, tcx }
167 impl MutVisitor<'tcx> for MutateUseVisitor<'tcx> {
168 fn tcx(&self) -> TyCtxt<'tcx> {
172 fn visit_local(&mut self,
174 _context: PlaceContext,
175 _location: Location) {
176 if *local == self.query {
177 *local = self.new_local;
181 fn process_projection_elem(
183 elem: &PlaceElem<'tcx>,
184 ) -> Option<PlaceElem<'tcx>> {
186 PlaceElem::Index(local) if *local == self.query => {
187 Some(PlaceElem::Index(self.new_local))