use references to avoid function calls

This commit is contained in:
SparrowLii 2022-04-22 08:42:38 +08:00
parent 3d256b3ecb
commit db23e773e3
2 changed files with 29 additions and 12 deletions

View File

@ -12,8 +12,9 @@
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor, MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
}; };
use rustc_middle::mir::{ use rustc_middle::mir::{
BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalKind, Location, Operand, Place, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind, Location,
Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE, Operand, Place, Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
RETURN_PLACE,
}; };
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout}; use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
use rustc_middle::ty::subst::{InternalSubsts, Subst}; use rustc_middle::ty::subst::{InternalSubsts, Subst};
@ -312,6 +313,7 @@ struct ConstPropagator<'mir, 'tcx> {
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>, ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>, param_env: ParamEnv<'tcx>,
local_decls: &'mir IndexVec<Local, LocalDecl<'tcx>>,
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store // Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
// the last known `SourceInfo` here and just keep revisiting it. // the last known `SourceInfo` here and just keep revisiting it.
source_info: Option<SourceInfo>, source_info: Option<SourceInfo>,
@ -393,7 +395,13 @@ fn new(
) )
.expect("failed to push initial stack frame"); .expect("failed to push initial stack frame");
ConstPropagator { ecx, tcx, param_env, source_info: None } ConstPropagator {
ecx,
tcx,
param_env,
local_decls: &dummy_body.local_decls,
source_info: None,
}
} }
fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> { fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
@ -494,7 +502,7 @@ fn check_binary_op(
let r = r?; let r = r?;
// We need the type of the LHS. We cannot use `place_layout` as that is the type // We need the type of the LHS. We cannot use `place_layout` as that is the type
// of the result, which for checked binops is not the same! // of the result, which for checked binops is not the same!
let left_ty = left.ty(&self.ecx.frame().body.local_decls, self.tcx); let left_ty = left.ty(self.local_decls, self.tcx);
let left_size = self.ecx.layout_of(left_ty).ok()?.size; let left_size = self.ecx.layout_of(left_ty).ok()?.size;
let right_size = r.layout.size; let right_size = r.layout.size;
let r_bits = r.to_scalar().ok(); let r_bits = r.to_scalar().ok();
@ -1116,7 +1124,7 @@ fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Loca
assert!( assert!(
self.get_const(local.into()).is_none() self.get_const(local.into()).is_none()
|| self || self
.layout_of(self.ecx.frame().body.local_decls[local].ty) .layout_of(self.local_decls[local].ty)
.map_or(true, |layout| layout.is_zst()) .map_or(true, |layout| layout.is_zst())
) )
} }

View File

@ -11,9 +11,9 @@
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::{ use rustc_middle::mir::{
AssertKind, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalKind, Location, AssertKind, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind,
Operand, Place, Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp, Location, Operand, Place, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
RETURN_PLACE, StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
}; };
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout}; use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
use rustc_middle::ty::subst::{InternalSubsts, Subst}; use rustc_middle::ty::subst::{InternalSubsts, Subst};
@ -308,6 +308,8 @@ struct ConstPropagator<'mir, 'tcx> {
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>, ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>, param_env: ParamEnv<'tcx>,
source_scopes: &'mir IndexVec<SourceScope, SourceScopeData<'tcx>>,
local_decls: &'mir IndexVec<Local, LocalDecl<'tcx>>,
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store // Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
// the last known `SourceInfo` here and just keep revisiting it. // the last known `SourceInfo` here and just keep revisiting it.
source_info: Option<SourceInfo>, source_info: Option<SourceInfo>,
@ -389,7 +391,14 @@ fn new(
) )
.expect("failed to push initial stack frame"); .expect("failed to push initial stack frame");
ConstPropagator { ecx, tcx, param_env, source_info: None } ConstPropagator {
ecx,
tcx,
param_env,
source_scopes: &dummy_body.source_scopes,
local_decls: &dummy_body.local_decls,
source_info: None,
}
} }
fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> { fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
@ -417,7 +426,7 @@ fn remove_const(ecx: &mut InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>, lo
} }
fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> { fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
source_info.scope.lint_root(&self.ecx.frame().body.source_scopes) source_info.scope.lint_root(self.source_scopes)
} }
fn use_ecx<F, T>(&mut self, f: F) -> Option<T> fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
@ -554,7 +563,7 @@ fn check_binary_op(
let r = r?; let r = r?;
// We need the type of the LHS. We cannot use `place_layout` as that is the type // We need the type of the LHS. We cannot use `place_layout` as that is the type
// of the result, which for checked binops is not the same! // of the result, which for checked binops is not the same!
let left_ty = left.ty(&self.ecx.frame().body.local_decls, self.tcx); let left_ty = left.ty(self.local_decls, self.tcx);
let left_size = self.ecx.layout_of(left_ty).ok()?.size; let left_size = self.ecx.layout_of(left_ty).ok()?.size;
let right_size = r.layout.size; let right_size = r.layout.size;
let r_bits = r.to_scalar().ok(); let r_bits = r.to_scalar().ok();
@ -1008,7 +1017,7 @@ fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
assert!( assert!(
self.get_const(local.into()).is_none() self.get_const(local.into()).is_none()
|| self || self
.layout_of(self.ecx.frame().body.local_decls[local].ty) .layout_of(self.local_decls[local].ty)
.map_or(true, |layout| layout.is_zst()) .map_or(true, |layout| layout.is_zst())
) )
} }