]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/deref_separator.rs
Merge commit 'e8dca3e87d164d2806098c462c6ce41301341f68' into sync_from_cg_gcc
[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             for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
39                 if !p_ref.projection.is_empty() && p_elem == ProjectionElem::Deref {
40                     let ty = p_ref.ty(&self.local_decls, self.tcx).ty;
41                     let temp = self.patcher.new_local_with_info(
42                         ty,
43                         self.local_decls[p_ref.local].source_info.span,
44                         Some(Box::new(LocalInfo::DerefTemp)),
45                     );
46
47                     self.patcher.add_statement(loc, StatementKind::StorageLive(temp));
48
49                     // We are adding current p_ref's projections to our
50                     // temp value, excluding projections we already covered.
51                     let deref_place = Place::from(place_local)
52                         .project_deeper(&p_ref.projection[last_len..], self.tcx);
53
54                     self.patcher.add_assign(
55                         loc,
56                         Place::from(temp),
57                         Rvalue::Use(Operand::Move(deref_place)),
58                     );
59                     place_local = temp;
60                     last_len = p_ref.projection.len();
61
62                     // Change `Place` only if we are actually at the Place's last deref
63                     if idx == last_deref_idx {
64                         let temp_place =
65                             Place::from(temp).project_deeper(&place.projection[idx..], self.tcx);
66                         *place = temp_place;
67                     }
68
69                     // We are destroying the previous temp since it's no longer used.
70                     if let Some(prev_temp) = prev_temp {
71                         self.patcher.add_statement(loc, StatementKind::StorageDead(prev_temp));
72                     }
73
74                     prev_temp = Some(temp);
75                 }
76             }
77
78             // Since we won't be able to reach final temp, we destroy it outside the loop.
79             if let Some(prev_temp) = prev_temp {
80                 let last_loc =
81                     Location { block: loc.block, statement_index: loc.statement_index + 1 };
82                 self.patcher.add_statement(last_loc, StatementKind::StorageDead(prev_temp));
83             }
84         }
85     }
86 }
87
88 pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
89     let patch = MirPatch::new(body);
90     let mut checker = DerefChecker { tcx, patcher: patch, local_decls: body.local_decls.clone() };
91
92     for (bb, data) in body.basic_blocks_mut().iter_enumerated_mut() {
93         checker.visit_basic_block_data(bb, data);
94     }
95
96     checker.patcher.apply(body);
97 }
98
99 impl<'tcx> MirPass<'tcx> for Derefer {
100     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
101         deref_finder(tcx, body);
102         body.phase = MirPhase::Derefered;
103     }
104 }