Polished documentation, removed not-so-useful-anymore traces, and added some doc comments in mir/transform/const_prop.rs
This commit is contained in:
parent
05d7a60ff8
commit
16ebaf9273
1 changed files with 13 additions and 6 deletions
|
@ -446,6 +446,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the value, if any, of evaluating `c`.
|
||||||
fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
|
fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
|
||||||
// FIXME we need to revisit this for #67176
|
// FIXME we need to revisit this for #67176
|
||||||
if c.needs_subst() {
|
if c.needs_subst() {
|
||||||
|
@ -486,11 +487,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the value, if any, of evaluating `place`.
|
||||||
fn eval_place(&mut self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
|
fn eval_place(&mut self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
|
||||||
trace!("eval_place(place={:?})", place);
|
trace!("eval_place(place={:?})", place);
|
||||||
self.use_ecx(|this| this.ecx.eval_place_to_op(place, None))
|
self.use_ecx(|this| this.ecx.eval_place_to_op(place, None))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
|
||||||
|
/// or `eval_place`, depending on the variant of `Operand` used.
|
||||||
fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
|
fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
|
||||||
match *op {
|
match *op {
|
||||||
Operand::Constant(ref c) => self.eval_constant(c, source_info),
|
Operand::Constant(ref c) => self.eval_constant(c, source_info),
|
||||||
|
@ -649,6 +653,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new `Operand::Constant` from a `Scalar` value
|
||||||
fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
|
fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
|
||||||
Operand::Constant(Box::new(Constant {
|
Operand::Constant(Box::new(Constant {
|
||||||
span,
|
span,
|
||||||
|
@ -694,6 +699,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
// Found a value represented as a pair. For now only do cont-prop if type of
|
// Found a value represented as a pair. For now only do cont-prop if type of
|
||||||
// Rvalue is also a pair with two scalars. The more general case is more
|
// Rvalue is also a pair with two scalars. The more general case is more
|
||||||
// complicated to implement so we'll do it later.
|
// complicated to implement so we'll do it later.
|
||||||
|
// FIXME: implement the general case stated above ^.
|
||||||
let ty = &value.layout.ty.kind;
|
let ty = &value.layout.ty.kind;
|
||||||
// Only do it for tuples
|
// Only do it for tuples
|
||||||
if let ty::Tuple(substs) = ty {
|
if let ty::Tuple(substs) = ty {
|
||||||
|
@ -730,6 +736,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if and only if this `op` should be const-propagated into.
|
||||||
fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
|
fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
|
||||||
let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
|
let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
|
||||||
|
|
||||||
|
@ -771,14 +778,14 @@ enum ConstPropMode {
|
||||||
|
|
||||||
struct CanConstProp {
|
struct CanConstProp {
|
||||||
can_const_prop: IndexVec<Local, ConstPropMode>,
|
can_const_prop: IndexVec<Local, ConstPropMode>,
|
||||||
// false at the beginning, once set, there are not allowed to be any more assignments
|
// False at the beginning. Once set, no more assignments are allowed to that local.
|
||||||
found_assignment: BitSet<Local>,
|
found_assignment: BitSet<Local>,
|
||||||
// Cache of locals' information
|
// Cache of locals' information
|
||||||
local_kinds: IndexVec<Local, LocalKind>,
|
local_kinds: IndexVec<Local, LocalKind>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CanConstProp {
|
impl CanConstProp {
|
||||||
/// returns true if `local` can be propagated
|
/// Returns true if `local` can be propagated
|
||||||
fn check(body: &Body<'_>) -> IndexVec<Local, ConstPropMode> {
|
fn check(body: &Body<'_>) -> IndexVec<Local, ConstPropMode> {
|
||||||
let mut cpv = CanConstProp {
|
let mut cpv = CanConstProp {
|
||||||
can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
|
can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
|
||||||
|
@ -789,8 +796,8 @@ impl CanConstProp {
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
|
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
|
||||||
// cannot use args at all
|
// Cannot use args at all
|
||||||
// cannot use locals because if x < y { y - x } else { x - y } would
|
// Cannot use locals because if x < y { y - x } else { x - y } would
|
||||||
// lint for x != y
|
// lint for x != y
|
||||||
// FIXME(oli-obk): lint variables until they are used in a condition
|
// FIXME(oli-obk): lint variables until they are used in a condition
|
||||||
// FIXME(oli-obk): lint if return value is constant
|
// FIXME(oli-obk): lint if return value is constant
|
||||||
|
@ -936,7 +943,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
|
||||||
let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
|
let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
|
||||||
let value_const = self.ecx.read_scalar(value).unwrap();
|
let value_const = self.ecx.read_scalar(value).unwrap();
|
||||||
if expected != value_const {
|
if expected != value_const {
|
||||||
// poison all places this operand references so that further code
|
// Poison all places this operand references so that further code
|
||||||
// doesn't use the invalid value
|
// doesn't use the invalid value
|
||||||
match cond {
|
match cond {
|
||||||
Operand::Move(ref place) | Operand::Copy(ref place) => {
|
Operand::Move(ref place) | Operand::Copy(ref place) => {
|
||||||
|
@ -1002,7 +1009,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//none of these have Operands to const-propagate
|
// None of these have Operands to const-propagate
|
||||||
TerminatorKind::Goto { .. }
|
TerminatorKind::Goto { .. }
|
||||||
| TerminatorKind::Resume
|
| TerminatorKind::Resume
|
||||||
| TerminatorKind::Abort
|
| TerminatorKind::Abort
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue