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