]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_mir_dataflow/src/value_analysis.rs
Auto merge of #97870 - eggyal:inplace_fold_spec, r=wesleywiser
[rust.git] / compiler / rustc_mir_dataflow / src / value_analysis.rs
index 13072a30282c4c2bc176f73732dbc44a79c78520..db4b0a3deda9dba74aea18e8e184eac255f55b70 100644 (file)
@@ -34,7 +34,7 @@
 
 use std::fmt::{Debug, Formatter};
 
-use rustc_data_structures::fx::{FxHashMap, FxHashSet};
+use rustc_data_structures::fx::FxHashMap;
 use rustc_index::vec::IndexVec;
 use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
 use rustc_middle::mir::*;
@@ -587,7 +587,8 @@ pub fn from_filter<'tcx>(
         filter: impl FnMut(Ty<'tcx>) -> bool,
     ) -> Self {
         let mut map = Self::new();
-        map.register_with_filter(tcx, body, filter, &escaped_places(body));
+        let exclude = excluded_locals(body);
+        map.register_with_filter(tcx, body, filter, &exclude);
         debug!("registered {} places ({} nodes in total)", map.value_count, map.places.len());
         map
     }
@@ -598,23 +599,18 @@ fn register_with_filter<'tcx>(
         tcx: TyCtxt<'tcx>,
         body: &Body<'tcx>,
         mut filter: impl FnMut(Ty<'tcx>) -> bool,
-        exclude: &FxHashSet<Place<'tcx>>,
+        exclude: &IndexVec<Local, bool>,
     ) {
         // We use this vector as stack, pushing and popping projections.
         let mut projection = Vec::new();
         for (local, decl) in body.local_decls.iter_enumerated() {
-            self.register_with_filter_rec(
-                tcx,
-                local,
-                &mut projection,
-                decl.ty,
-                &mut filter,
-                exclude,
-            );
+            if !exclude[local] {
+                self.register_with_filter_rec(tcx, local, &mut projection, decl.ty, &mut filter);
+            }
         }
     }
 
-    /// Register fields of the given (local, projection) place.
+    /// Potentially register the (local, projection) place and its fields, recursively.
     ///
     /// Invariant: The projection must only contain fields.
     fn register_with_filter_rec<'tcx>(
@@ -624,13 +620,7 @@ fn register_with_filter_rec<'tcx>(
         projection: &mut Vec<PlaceElem<'tcx>>,
         ty: Ty<'tcx>,
         filter: &mut impl FnMut(Ty<'tcx>) -> bool,
-        exclude: &FxHashSet<Place<'tcx>>,
     ) {
-        if exclude.contains(&Place { local, projection: tcx.intern_place_elems(projection) }) {
-            // This will also exclude all projections of the excluded place.
-            return;
-        }
-
         // Note: The framework supports only scalars for now.
         if filter(ty) && ty.is_scalar() {
             // We know that the projection only contains trackable elements.
@@ -650,7 +640,7 @@ fn register_with_filter_rec<'tcx>(
                 return;
             }
             projection.push(PlaceElem::Field(field, ty));
-            self.register_with_filter_rec(tcx, local, projection, ty, filter, exclude);
+            self.register_with_filter_rec(tcx, local, projection, ty, filter);
             projection.pop();
         });
     }
@@ -832,27 +822,27 @@ fn iter_fields<'tcx>(
     }
 }
 
-/// Returns all places, that have their reference or address taken.
-///
-/// This includes shared references, and also drops and `InlineAsm` out parameters.
-fn escaped_places<'tcx>(body: &Body<'tcx>) -> FxHashSet<Place<'tcx>> {
-    struct Collector<'tcx> {
-        result: FxHashSet<Place<'tcx>>,
+/// Returns all locals with projections that have their reference or address taken.
+fn excluded_locals<'tcx>(body: &Body<'tcx>) -> IndexVec<Local, bool> {
+    struct Collector {
+        result: IndexVec<Local, bool>,
     }
 
-    impl<'tcx> Visitor<'tcx> for Collector<'tcx> {
+    impl<'tcx> Visitor<'tcx> for Collector {
         fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
             if context.is_borrow()
                 || context.is_address_of()
                 || context.is_drop()
                 || context == PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
             {
-                self.result.insert(*place);
+                // A pointer to a place could be used to access other places with the same local,
+                // hence we have to exclude the local completely.
+                self.result[place.local] = true;
             }
         }
     }
 
-    let mut collector = Collector { result: FxHashSet::default() };
+    let mut collector = Collector { result: IndexVec::from_elem(false, &body.local_decls) };
     collector.visit_body(body);
     collector.result
 }