Skip to content

Commit 45f1775

Browse files
committed
Polished documentation, removed not-so-useful-anymore traces, and added some doc comments in mir/transform/const_prop.rs
1 parent f64a0e4 commit 45f1775

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
423423
self.ecx.access_local(self.ecx.frame(), local, None).ok()
424424
}
425425

426+
/// Remove `local` from the pool of `Locals`. Allows writing to them,
427+
/// but not reading from them anymore.
426428
fn remove_const(&mut self, local: Local) {
427429
self.ecx.frame_mut().locals[local] =
428430
LocalState { value: LocalValue::Uninitialized, layout: Cell::new(None) };
@@ -455,6 +457,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
455457
}
456458
}
457459

460+
/// Returns the value, if any, of evaluating `c`.
458461
fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
459462
// FIXME we need to revisit this for #67176
460463
if c.needs_subst() {
@@ -495,11 +498,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
495498
}
496499
}
497500

501+
/// Returns the value, if any, of evaluating `place`.
498502
fn eval_place(&mut self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
499503
trace!("eval_place(place={:?})", place);
500504
self.use_ecx(|this| this.ecx.eval_place_to_op(place, None))
501505
}
502506

507+
/// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
508+
/// or `eval_place`, depending on the variant of `Operand` used.
503509
fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
504510
match *op {
505511
Operand::Constant(ref c) => self.eval_constant(c, source_info),
@@ -658,6 +664,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
658664
})
659665
}
660666

667+
/// Creates a new `Operand::Constant` from a `Scalar` value
661668
fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
662669
Operand::Constant(Box::new(Constant {
663670
span,
@@ -703,6 +710,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
703710
// Found a value represented as a pair. For now only do cont-prop if type of
704711
// Rvalue is also a pair with two scalars. The more general case is more
705712
// complicated to implement so we'll do it later.
713+
// FIXME: implement the general case stated above ^.
706714
let ty = &value.layout.ty.kind;
707715
// Only do it for tuples
708716
if let ty::Tuple(substs) = ty {
@@ -739,6 +747,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
739747
}
740748
}
741749

750+
/// Returns `true` if and only if this `op` should be const-propagated into.
742751
fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
743752
let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
744753

@@ -780,14 +789,14 @@ enum ConstPropMode {
780789

781790
struct CanConstProp {
782791
can_const_prop: IndexVec<Local, ConstPropMode>,
783-
// false at the beginning, once set, there are not allowed to be any more assignments
792+
// False at the beginning. Once set, no more assignments are allowed to that local.
784793
found_assignment: BitSet<Local>,
785794
// Cache of locals' information
786795
local_kinds: IndexVec<Local, LocalKind>,
787796
}
788797

789798
impl CanConstProp {
790-
/// returns true if `local` can be propagated
799+
/// Returns true if `local` can be propagated
791800
fn check(body: &Body<'_>) -> IndexVec<Local, ConstPropMode> {
792801
let mut cpv = CanConstProp {
793802
can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
@@ -798,8 +807,8 @@ impl CanConstProp {
798807
),
799808
};
800809
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
801-
// cannot use args at all
802-
// cannot use locals because if x < y { y - x } else { x - y } would
810+
// Cannot use args at all
811+
// Cannot use locals because if x < y { y - x } else { x - y } would
803812
// lint for x != y
804813
// FIXME(oli-obk): lint variables until they are used in a condition
805814
// FIXME(oli-obk): lint if return value is constant
@@ -939,7 +948,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
939948
let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
940949
let value_const = self.ecx.read_scalar(value).unwrap();
941950
if expected != value_const {
942-
// poison all places this operand references so that further code
951+
// Poison all places this operand references so that further code
943952
// doesn't use the invalid value
944953
match cond {
945954
Operand::Move(ref place) | Operand::Copy(ref place) => {
@@ -1005,7 +1014,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
10051014
}
10061015
}
10071016
}
1008-
//none of these have Operands to const-propagate
1017+
// None of these have Operands to const-propagate
10091018
TerminatorKind::Goto { .. }
10101019
| TerminatorKind::Resume
10111020
| TerminatorKind::Abort
@@ -1023,22 +1032,17 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
10231032
for opr in args {
10241033
// We see if the operand can be evaluated, and if so, we continue.
10251034
if let Some(op_ty) = self.eval_operand(&opr, source_info) {
1026-
trace!("OpTy of the place: {:?}", &op_ty);
10271035
// We must also ask if it *should* be evaluated.
10281036
if self.should_const_prop(op_ty) {
10291037
if let interpret::Operand::Immediate(immediate) = *op_ty {
10301038
// Only Immediate from here on
10311039
// FIXME(felix91gr): The code only works for Immediate. Investigate
10321040
// if it could be made to work for Indirect as well.
1033-
trace!("Immediate of the OpTy: {:?}", &immediate);
10341041
if let interpret::Immediate::Scalar(scalar_mu) = immediate {
10351042
// FIXME(felix91gr): This only works for Scalar
10361043
// Could probably be expanded for Scalar Tuples (inside the Immediate)
1037-
trace!("ScalarMaybeUndef of the OpTy: {:?}", &scalar_mu);
10381044
if let ScalarMaybeUndef::Scalar(scalar) = scalar_mu {
1039-
trace!("Scalar of the ScalarMU: {:?}", &scalar);
10401045
let operand_type = opr.ty(&self.local_decls, self.tcx);
1041-
trace!("Type of the OpTy: {:?}", &operand_type);
10421046
*opr = self.operand_from_scalar(
10431047
scalar,
10441048
operand_type,

0 commit comments

Comments
 (0)