]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/remove_zsts.rs
simplify more, ret_deref -> has_deref
[rust.git] / compiler / rustc_mir_transform / src / remove_zsts.rs
1 //! Removes assignments to ZST places.
2
3 use crate::MirPass;
4 use rustc_middle::mir::tcx::PlaceTy;
5 use rustc_middle::mir::{Body, LocalDecls, Place, StatementKind};
6 use rustc_middle::ty::{self, Ty, TyCtxt};
7
8 pub struct RemoveZsts;
9
10 impl<'tcx> MirPass<'tcx> for RemoveZsts {
11     fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
12         sess.mir_opt_level() > 0
13     }
14
15     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
16         // Avoid query cycles (generators require optimized MIR for layout).
17         if tcx.type_of(body.source.def_id()).is_generator() {
18             return;
19         }
20         let param_env = tcx.param_env(body.source.def_id());
21         let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
22         let local_decls = &body.local_decls;
23         for block in basic_blocks {
24             for statement in block.statements.iter_mut() {
25                 if let StatementKind::Assign(box (place, _)) | StatementKind::Deinit(box place) =
26                     statement.kind
27                 {
28                     let place_ty = place.ty(local_decls, tcx).ty;
29                     if !maybe_zst(place_ty) {
30                         continue;
31                     }
32                     let Ok(layout) = tcx.layout_of(param_env.and(place_ty)) else {
33                         continue;
34                     };
35                     if !layout.is_zst() {
36                         continue;
37                     }
38                     if involves_a_union(place, local_decls, tcx) {
39                         continue;
40                     }
41                     if tcx.consider_optimizing(|| {
42                         format!(
43                             "RemoveZsts - Place: {:?} SourceInfo: {:?}",
44                             place, statement.source_info
45                         )
46                     }) {
47                         statement.make_nop();
48                     }
49                 }
50             }
51         }
52     }
53 }
54
55 /// A cheap, approximate check to avoid unnecessary `layout_of` calls.
56 fn maybe_zst(ty: Ty<'_>) -> bool {
57     match ty.kind() {
58         // maybe ZST (could be more precise)
59         ty::Adt(..) | ty::Array(..) | ty::Closure(..) | ty::Tuple(..) | ty::Opaque(..) => true,
60         // definitely ZST
61         ty::FnDef(..) | ty::Never => true,
62         // unreachable or can't be ZST
63         _ => false,
64     }
65 }
66
67 /// Miri lazily allocates memory for locals on assignment,
68 /// so we must preserve writes to unions and union fields,
69 /// or it will ICE on reads of those fields.
70 fn involves_a_union<'tcx>(
71     place: Place<'tcx>,
72     local_decls: &LocalDecls<'tcx>,
73     tcx: TyCtxt<'tcx>,
74 ) -> bool {
75     let mut place_ty = PlaceTy::from_ty(local_decls[place.local].ty);
76     if place_ty.ty.is_union() {
77         return true;
78     }
79     for elem in place.projection {
80         place_ty = place_ty.projection_ty(tcx, elem);
81         if place_ty.ty.is_union() {
82             return true;
83         }
84     }
85     return false;
86 }