]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/deref_separator.rs
Auto merge of #93397 - joshtriplett:sort-floats, r=Amanieu
[rust.git] / compiler / rustc_mir_transform / src / deref_separator.rs
1 use crate::MirPass;
2 use rustc_index::vec::IndexVec;
3 use rustc_middle::mir::patch::MirPatch;
4 use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
5 use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
6 use rustc_middle::mir::*;
7 use rustc_middle::ty::TyCtxt;
8
9 pub struct Derefer;
10
11 pub struct DerefChecker<'tcx> {
12     tcx: TyCtxt<'tcx>,
13     patcher: MirPatch<'tcx>,
14     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
15 }
16
17 impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
18     fn tcx(&self) -> TyCtxt<'tcx> {
19         self.tcx
20     }
21
22     fn visit_place(&mut self, place: &mut Place<'tcx>, cntxt: PlaceContext, loc: Location) {
23         if !place.projection.is_empty()
24             && cntxt != PlaceContext::NonUse(VarDebugInfo)
25             && place.projection[1..].contains(&ProjectionElem::Deref)
26         {
27             let mut place_local = place.local;
28             let mut last_len = 0;
29             let mut last_deref_idx = 0;
30
31             let mut prev_temp: Option<Local> = None;
32
33             for (idx, elem) in place.projection[0..].iter().enumerate() {
34                 if *elem == ProjectionElem::Deref {
35                     last_deref_idx = idx;
36                 }
37             }
38
39             for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
40                 if !p_ref.projection.is_empty() && p_elem == ProjectionElem::Deref {
41                     let ty = p_ref.ty(&self.local_decls, self.tcx).ty;
42                     let temp = self.patcher.new_local_with_info(
43                         ty,
44                         self.local_decls[p_ref.local].source_info.span,
45                         Some(Box::new(LocalInfo::DerefTemp)),
46                     );
47
48                     self.patcher.add_statement(loc, StatementKind::StorageLive(temp));
49
50                     // We are adding current p_ref's projections to our
51                     // temp value, excluding projections we already covered.
52                     let deref_place = Place::from(place_local)
53                         .project_deeper(&p_ref.projection[last_len..], self.tcx);
54
55                     self.patcher.add_assign(
56                         loc,
57                         Place::from(temp),
58                         Rvalue::CopyForDeref(deref_place),
59                     );
60                     place_local = temp;
61                     last_len = p_ref.projection.len();
62
63                     // Change `Place` only if we are actually at the Place's last deref
64                     if idx == last_deref_idx {
65                         let temp_place =
66                             Place::from(temp).project_deeper(&place.projection[idx..], self.tcx);
67                         *place = temp_place;
68                     }
69
70                     // We are destroying the previous temp since it's no longer used.
71                     if let Some(prev_temp) = prev_temp {
72                         self.patcher.add_statement(loc, StatementKind::StorageDead(prev_temp));
73                     }
74
75                     prev_temp = Some(temp);
76                 }
77             }
78
79             // Since we won't be able to reach final temp, we destroy it outside the loop.
80             if let Some(prev_temp) = prev_temp {
81                 let last_loc =
82                     Location { block: loc.block, statement_index: loc.statement_index + 1 };
83                 self.patcher.add_statement(last_loc, StatementKind::StorageDead(prev_temp));
84             }
85         }
86     }
87 }
88
89 pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
90     let patch = MirPatch::new(body);
91     let mut checker = DerefChecker { tcx, patcher: patch, local_decls: body.local_decls.clone() };
92
93     for (bb, data) in body.basic_blocks_mut().iter_enumerated_mut() {
94         checker.visit_basic_block_data(bb, data);
95     }
96
97     checker.patcher.apply(body);
98 }
99
100 impl<'tcx> MirPass<'tcx> for Derefer {
101     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
102         deref_finder(tcx, body);
103         body.phase = MirPhase::Derefered;
104     }
105 }