]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/remove_zsts.rs
Rollup merge of #93392 - GKFX:char-docs, r=scottmcm
[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, local_decls) = body.basic_blocks_and_local_decls_mut();
22         for block in basic_blocks.iter_mut() {
23             for statement in block.statements.iter_mut() {
24                 if let StatementKind::Assign(box (place, _)) = statement.kind {
25                     let place_ty = place.ty(local_decls, tcx).ty;
26                     if !maybe_zst(place_ty) {
27                         continue;
28                     }
29                     let layout = match tcx.layout_of(param_env.and(place_ty)) {
30                         Ok(layout) => layout,
31                         Err(_) => continue,
32                     };
33                     if !layout.is_zst() {
34                         continue;
35                     }
36                     if involves_a_union(place, local_decls, tcx) {
37                         continue;
38                     }
39                     if tcx.consider_optimizing(|| {
40                         format!(
41                             "RemoveZsts - Place: {:?} SourceInfo: {:?}",
42                             place, statement.source_info
43                         )
44                     }) {
45                         statement.make_nop();
46                     }
47                 }
48             }
49         }
50     }
51 }
52
53 /// A cheap, approximate check to avoid unnecessary `layout_of` calls.
54 fn maybe_zst(ty: Ty<'_>) -> bool {
55     match ty.kind() {
56         // maybe ZST (could be more precise)
57         ty::Adt(..) | ty::Array(..) | ty::Closure(..) | ty::Tuple(..) | ty::Opaque(..) => true,
58         // definitely ZST
59         ty::FnDef(..) | ty::Never => true,
60         // unreachable or can't be ZST
61         _ => false,
62     }
63 }
64
65 /// Miri lazily allocates memory for locals on assignment,
66 /// so we must preserve writes to unions and union fields,
67 /// or it will ICE on reads of those fields.
68 fn involves_a_union<'tcx>(
69     place: Place<'tcx>,
70     local_decls: &LocalDecls<'tcx>,
71     tcx: TyCtxt<'tcx>,
72 ) -> bool {
73     let mut place_ty = PlaceTy::from_ty(local_decls[place.local].ty);
74     if place_ty.ty.is_union() {
75         return true;
76     }
77     for elem in place.projection {
78         place_ty = place_ty.projection_ty(tcx, elem);
79         if place_ty.ty.is_union() {
80             return true;
81         }
82     }
83     return false;
84 }