]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/lower_slice_len.rs
Rollup merge of #88361 - WaffleLapkin:patch-2, r=jyn514
[rust.git] / compiler / rustc_mir_transform / src / lower_slice_len.rs
1 //! This pass lowers calls to core::slice::len to just Len op.
2 //! It should run before inlining!
3
4 use crate::MirPass;
5 use rustc_hir::def_id::DefId;
6 use rustc_index::vec::IndexVec;
7 use rustc_middle::mir::*;
8 use rustc_middle::ty::{self, TyCtxt};
9
10 pub struct LowerSliceLenCalls;
11
12 impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls {
13     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
14         lower_slice_len_calls(tcx, body)
15     }
16 }
17
18 pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19     let language_items = tcx.lang_items();
20     let Some(slice_len_fn_item_def_id) = language_items.slice_len_fn() else {
21         // there is no language item to compare to :)
22         return;
23     };
24
25     let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
26
27     for block in basic_blocks {
28         // lower `<[_]>::len` calls
29         lower_slice_len_call(tcx, block, &*local_decls, slice_len_fn_item_def_id);
30     }
31 }
32
33 struct SliceLenPatchInformation<'tcx> {
34     add_statement: Statement<'tcx>,
35     new_terminator_kind: TerminatorKind<'tcx>,
36 }
37
38 fn lower_slice_len_call<'tcx>(
39     tcx: TyCtxt<'tcx>,
40     block: &mut BasicBlockData<'tcx>,
41     local_decls: &IndexVec<Local, LocalDecl<'tcx>>,
42     slice_len_fn_item_def_id: DefId,
43 ) {
44     let mut patch_found: Option<SliceLenPatchInformation<'_>> = None;
45
46     let terminator = block.terminator();
47     match &terminator.kind {
48         TerminatorKind::Call {
49             func,
50             args,
51             destination: Some((dest, bb)),
52             cleanup: None,
53             from_hir_call: true,
54             ..
55         } => {
56             // some heuristics for fast rejection
57             if args.len() != 1 {
58                 return;
59             }
60             let arg = match args[0].place() {
61                 Some(arg) => arg,
62                 None => return,
63             };
64             let func_ty = func.ty(local_decls, tcx);
65             match func_ty.kind() {
66                 ty::FnDef(fn_def_id, _) if fn_def_id == &slice_len_fn_item_def_id => {
67                     // perform modifications
68                     // from something like `_5 = core::slice::<impl [u8]>::len(move _6) -> bb1`
69                     // into `_5 = Len(*_6)
70                     // goto bb1
71
72                     // make new RValue for Len
73                     let deref_arg = tcx.mk_place_deref(arg);
74                     let r_value = Rvalue::Len(deref_arg);
75                     let len_statement_kind = StatementKind::Assign(Box::new((*dest, r_value)));
76                     let add_statement =
77                         Statement { kind: len_statement_kind, source_info: terminator.source_info };
78
79                     // modify terminator into simple Goto
80                     let new_terminator_kind = TerminatorKind::Goto { target: *bb };
81
82                     let patch = SliceLenPatchInformation { add_statement, new_terminator_kind };
83
84                     patch_found = Some(patch);
85                 }
86                 _ => {}
87             }
88         }
89         _ => {}
90     }
91
92     if let Some(SliceLenPatchInformation { add_statement, new_terminator_kind }) = patch_found {
93         block.statements.push(add_statement);
94         block.terminator_mut().kind = new_terminator_kind;
95     }
96 }