Squashed implementation of the pass
This commit is contained in:
parent
304441960e
commit
aa53928ed7
8 changed files with 185 additions and 1 deletions
100
compiler/rustc_mir/src/transform/lower_slice_len.rs
Normal file
100
compiler/rustc_mir/src/transform/lower_slice_len.rs
Normal file
|
@ -0,0 +1,100 @@
|
|||
//! This pass lowers calls to core::slice::len to just Len op.
|
||||
//! It should run before inlining!
|
||||
|
||||
use crate::transform::MirPass;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_index::vec::IndexVec;
|
||||
use rustc_middle::mir::*;
|
||||
use rustc_middle::ty::{self, TyCtxt};
|
||||
|
||||
pub struct LowerSliceLenCalls;
|
||||
|
||||
impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls {
|
||||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
||||
lower_slice_len_calls(tcx, body)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
||||
let language_items = tcx.lang_items();
|
||||
let slice_len_fn_item_def_id = if let Some(slice_len_fn_item) = language_items.slice_len_fn() {
|
||||
slice_len_fn_item
|
||||
} else {
|
||||
// there is no language item to compare to :)
|
||||
return;
|
||||
};
|
||||
|
||||
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
|
||||
|
||||
for block in basic_blocks {
|
||||
// lower `<[_]>::len` calls
|
||||
lower_slice_len_call(tcx, block, &*local_decls, slice_len_fn_item_def_id);
|
||||
}
|
||||
}
|
||||
|
||||
struct SliceLenPatchInformation<'tcx> {
|
||||
add_statement: Statement<'tcx>,
|
||||
new_terminator_kind: TerminatorKind<'tcx>,
|
||||
}
|
||||
|
||||
fn lower_slice_len_call<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
block: &mut BasicBlockData<'tcx>,
|
||||
local_decls: &IndexVec<Local, LocalDecl<'tcx>>,
|
||||
slice_len_fn_item_def_id: DefId,
|
||||
) {
|
||||
let mut patch_found: Option<SliceLenPatchInformation<'_>> = None;
|
||||
|
||||
let terminator = block.terminator();
|
||||
match &terminator.kind {
|
||||
TerminatorKind::Call {
|
||||
func,
|
||||
args,
|
||||
destination: Some((dest, bb)),
|
||||
cleanup: None,
|
||||
from_hir_call: true,
|
||||
..
|
||||
} => {
|
||||
// some heuristics for fast rejection
|
||||
if args.len() != 1 {
|
||||
return;
|
||||
}
|
||||
let arg = match args[0].place() {
|
||||
Some(arg) => arg,
|
||||
None => return,
|
||||
};
|
||||
let func_ty = func.ty(local_decls, tcx);
|
||||
match func_ty.kind() {
|
||||
ty::FnDef(fn_def_id, _) if fn_def_id == &slice_len_fn_item_def_id => {
|
||||
// perform modifications
|
||||
// from something like `_5 = core::slice::<impl [u8]>::len(move _6) -> bb1`
|
||||
// into `_5 = Len(*_6)
|
||||
// goto bb1
|
||||
|
||||
// make new RValue for Len
|
||||
let deref_arg = tcx.mk_place_deref(arg);
|
||||
let r_value = Rvalue::Len(deref_arg);
|
||||
let len_statement_kind = StatementKind::Assign(Box::new((*dest, r_value)));
|
||||
let add_statement = Statement {
|
||||
kind: len_statement_kind,
|
||||
source_info: terminator.source_info.clone(),
|
||||
};
|
||||
|
||||
// modify terminator into simple Goto
|
||||
let new_terminator_kind = TerminatorKind::Goto { target: bb.clone() };
|
||||
|
||||
let patch = SliceLenPatchInformation { add_statement, new_terminator_kind };
|
||||
|
||||
patch_found = Some(patch);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if let Some(SliceLenPatchInformation { add_statement, new_terminator_kind }) = patch_found {
|
||||
block.statements.push(add_statement);
|
||||
block.terminator_mut().kind = new_terminator_kind;
|
||||
}
|
||||
}
|
|
@ -36,6 +36,7 @@ pub mod generator;
|
|||
pub mod inline;
|
||||
pub mod instcombine;
|
||||
pub mod lower_intrinsics;
|
||||
pub mod lower_slice_len;
|
||||
pub mod match_branches;
|
||||
pub mod multiple_return_terminators;
|
||||
pub mod no_landing_pads;
|
||||
|
@ -479,6 +480,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|||
// to them. We run some optimizations before that, because they may be harder to do on the state
|
||||
// machine than on MIR with async primitives.
|
||||
let optimizations_with_generators: &[&dyn MirPass<'tcx>] = &[
|
||||
&lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first
|
||||
&unreachable_prop::UnreachablePropagation,
|
||||
&uninhabited_enum_branching::UninhabitedEnumBranching,
|
||||
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue