Skip to content

Commit db23e77

Browse files
committed
use references to avoid function calls
1 parent 3d256b3 commit db23e77

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use rustc_middle::mir::visit::{
1212
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
1313
};
1414
use rustc_middle::mir::{
15-
BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalKind, Location, Operand, Place,
16-
Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
15+
BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind, Location,
16+
Operand, Place, Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
17+
RETURN_PLACE,
1718
};
1819
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
1920
use rustc_middle::ty::subst::{InternalSubsts, Subst};
@@ -312,6 +313,7 @@ struct ConstPropagator<'mir, 'tcx> {
312313
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
313314
tcx: TyCtxt<'tcx>,
314315
param_env: ParamEnv<'tcx>,
316+
local_decls: &'mir IndexVec<Local, LocalDecl<'tcx>>,
315317
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
316318
// the last known `SourceInfo` here and just keep revisiting it.
317319
source_info: Option<SourceInfo>,
@@ -393,7 +395,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
393395
)
394396
.expect("failed to push initial stack frame");
395397

396-
ConstPropagator { ecx, tcx, param_env, source_info: None }
398+
ConstPropagator {
399+
ecx,
400+
tcx,
401+
param_env,
402+
local_decls: &dummy_body.local_decls,
403+
source_info: None,
404+
}
397405
}
398406

399407
fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
@@ -494,7 +502,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
494502
let r = r?;
495503
// We need the type of the LHS. We cannot use `place_layout` as that is the type
496504
// of the result, which for checked binops is not the same!
497-
let left_ty = left.ty(&self.ecx.frame().body.local_decls, self.tcx);
505+
let left_ty = left.ty(self.local_decls, self.tcx);
498506
let left_size = self.ecx.layout_of(left_ty).ok()?.size;
499507
let right_size = r.layout.size;
500508
let r_bits = r.to_scalar().ok();
@@ -1116,7 +1124,7 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
11161124
assert!(
11171125
self.get_const(local.into()).is_none()
11181126
|| self
1119-
.layout_of(self.ecx.frame().body.local_decls[local].ty)
1127+
.layout_of(self.local_decls[local].ty)
11201128
.map_or(true, |layout| layout.is_zst())
11211129
)
11221130
}

compiler/rustc_mir_transform/src/const_prop_lint.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use rustc_index::bit_set::BitSet;
1111
use rustc_index::vec::IndexVec;
1212
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
1313
use rustc_middle::mir::{
14-
AssertKind, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalKind, Location,
15-
Operand, Place, Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
16-
RETURN_PLACE,
14+
AssertKind, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind,
15+
Location, Operand, Place, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
16+
StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
1717
};
1818
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
1919
use rustc_middle::ty::subst::{InternalSubsts, Subst};
@@ -308,6 +308,8 @@ struct ConstPropagator<'mir, 'tcx> {
308308
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
309309
tcx: TyCtxt<'tcx>,
310310
param_env: ParamEnv<'tcx>,
311+
source_scopes: &'mir IndexVec<SourceScope, SourceScopeData<'tcx>>,
312+
local_decls: &'mir IndexVec<Local, LocalDecl<'tcx>>,
311313
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
312314
// the last known `SourceInfo` here and just keep revisiting it.
313315
source_info: Option<SourceInfo>,
@@ -389,7 +391,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
389391
)
390392
.expect("failed to push initial stack frame");
391393

392-
ConstPropagator { ecx, tcx, param_env, source_info: None }
394+
ConstPropagator {
395+
ecx,
396+
tcx,
397+
param_env,
398+
source_scopes: &dummy_body.source_scopes,
399+
local_decls: &dummy_body.local_decls,
400+
source_info: None,
401+
}
393402
}
394403

395404
fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
@@ -417,7 +426,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
417426
}
418427

419428
fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
420-
source_info.scope.lint_root(&self.ecx.frame().body.source_scopes)
429+
source_info.scope.lint_root(self.source_scopes)
421430
}
422431

423432
fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
@@ -554,7 +563,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
554563
let r = r?;
555564
// We need the type of the LHS. We cannot use `place_layout` as that is the type
556565
// of the result, which for checked binops is not the same!
557-
let left_ty = left.ty(&self.ecx.frame().body.local_decls, self.tcx);
566+
let left_ty = left.ty(self.local_decls, self.tcx);
558567
let left_size = self.ecx.layout_of(left_ty).ok()?.size;
559568
let right_size = r.layout.size;
560569
let r_bits = r.to_scalar().ok();
@@ -1008,7 +1017,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
10081017
assert!(
10091018
self.get_const(local.into()).is_none()
10101019
|| self
1011-
.layout_of(self.ecx.frame().body.local_decls[local].ty)
1020+
.layout_of(self.local_decls[local].ty)
10121021
.map_or(true, |layout| layout.is_zst())
10131022
)
10141023
}

0 commit comments

Comments
 (0)