|
1 | 1 | use crate::MirPass;
|
| 2 | +use rustc_index::vec::IndexVec; |
2 | 3 | use rustc_middle::mir::patch::MirPatch;
|
| 4 | +use rustc_middle::mir::visit::{MutVisitor, PlaceContext}; |
3 | 5 | use rustc_middle::mir::*;
|
4 | 6 | use rustc_middle::ty::TyCtxt;
|
5 | 7 | pub struct Derefer;
|
6 | 8 |
|
7 |
| -pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
8 |
| - let mut patch = MirPatch::new(body); |
9 |
| - let (basic_blocks, local_decl) = body.basic_blocks_and_local_decls_mut(); |
10 |
| - for (block, data) in basic_blocks.iter_enumerated_mut() { |
11 |
| - for (i, stmt) in data.statements.iter_mut().enumerate() { |
12 |
| - match stmt.kind { |
13 |
| - StatementKind::Assign(box (og_place, Rvalue::Ref(region, borrow_knd, place))) => { |
14 |
| - let mut place_local = place.local; |
15 |
| - let mut last_len = 0; |
16 |
| - for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() { |
17 |
| - if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() { |
18 |
| - // The type that we are derefing. |
19 |
| - let ty = p_ref.ty(local_decl, tcx).ty; |
20 |
| - let temp = patch.new_temp(ty, stmt.source_info.span); |
21 |
| - |
22 |
| - // Because we are assigning this right before original statement |
23 |
| - // we are using index i of statement. |
24 |
| - let loc = Location { block: block, statement_index: i }; |
25 |
| - patch.add_statement(loc, StatementKind::StorageLive(temp)); |
26 |
| - |
27 |
| - // We are adding current p_ref's projections to our |
28 |
| - // temp value, excluding projections we already covered. |
29 |
| - let deref_place = Place::from(place_local) |
30 |
| - .project_deeper(&p_ref.projection[last_len..], tcx); |
31 |
| - patch.add_assign( |
32 |
| - loc, |
33 |
| - Place::from(temp), |
34 |
| - Rvalue::Use(Operand::Move(deref_place)), |
35 |
| - ); |
36 |
| - |
37 |
| - place_local = temp; |
38 |
| - last_len = p_ref.projection.len(); |
39 |
| - |
40 |
| - // We are creating a place by using our temp value's location |
41 |
| - // and copying derefed values which we need to create new statement. |
42 |
| - let temp_place = |
43 |
| - Place::from(temp).project_deeper(&place.projection[idx..], tcx); |
44 |
| - let new_stmt = Statement { |
45 |
| - source_info: stmt.source_info, |
46 |
| - kind: StatementKind::Assign(Box::new(( |
47 |
| - og_place, |
48 |
| - Rvalue::Ref(region, borrow_knd, temp_place), |
49 |
| - ))), |
50 |
| - }; |
51 |
| - |
52 |
| - // Replace current statement with newly created one. |
53 |
| - *stmt = new_stmt; |
54 |
| - |
55 |
| - // Since our job with the temp is done it should be gone |
56 |
| - let loc = Location { block: block, statement_index: i + 1 }; |
57 |
| - patch.add_statement(loc, StatementKind::StorageDead(temp)); |
58 |
| - } |
59 |
| - } |
| 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)); |
60 | 64 | }
|
61 |
| - _ => (), |
| 65 | + |
| 66 | + prev_temp = Some(temp); |
62 | 67 | }
|
63 | 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 | + } |
64 | 75 | }
|
65 |
| - patch.apply(body); |
| 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); |
66 | 87 | }
|
67 | 88 |
|
68 | 89 | impl<'tcx> MirPass<'tcx> for Derefer {
|
|
0 commit comments