2022-03-08 17:20:31 +00:00
|
|
|
//! Propagates constants for early reporting of statically known
|
|
|
|
//! assertion failures
|
|
|
|
|
2023-04-30 02:20:53 +01:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2024-01-05 17:19:18 +00:00
|
|
|
use rustc_const_eval::interpret::{ImmTy, Projectable};
|
|
|
|
use rustc_const_eval::interpret::{InterpCx, InterpResult, OpTy, Scalar};
|
2024-01-05 17:26:59 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2022-03-08 17:20:31 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
|
|
|
use rustc_hir::HirId;
|
2023-03-30 18:40:47 +00:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2024-01-05 17:08:58 +00:00
|
|
|
use rustc_index::{Idx, IndexVec};
|
2022-07-25 13:54:49 +00:00
|
|
|
use rustc_middle::mir::visit::Visitor;
|
2023-03-07 14:31:43 +00:00
|
|
|
use rustc_middle::mir::*;
|
2022-03-08 17:20:31 +00:00
|
|
|
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
|
2024-01-05 17:19:18 +00:00
|
|
|
use rustc_middle::ty::{self, ConstInt, ParamEnv, ScalarInt, Ty, TyCtxt, TypeVisitableExt};
|
2022-07-12 12:17:58 +00:00
|
|
|
use rustc_span::Span;
|
2024-01-05 17:08:58 +00:00
|
|
|
use rustc_target::abi::{Abi, FieldIdx, HasDataLayout, Size, TargetDataLayout, VariantIdx};
|
2022-11-18 10:18:32 +01:00
|
|
|
|
|
|
|
use crate::const_prop::CanConstProp;
|
|
|
|
use crate::const_prop::ConstPropMode;
|
2024-01-15 13:33:58 +00:00
|
|
|
use crate::dataflow_const_prop::DummyMachine;
|
2024-01-15 13:18:50 +00:00
|
|
|
use crate::errors::{AssertLint, AssertLintKind};
|
2022-11-18 10:18:32 +01:00
|
|
|
use crate::MirLint;
|
2022-03-08 17:20:31 +00:00
|
|
|
|
2023-10-01 13:49:19 +00:00
|
|
|
pub struct ConstPropLint;
|
2022-03-08 17:20:31 +00:00
|
|
|
|
2023-10-01 13:49:19 +00:00
|
|
|
impl<'tcx> MirLint<'tcx> for ConstPropLint {
|
2022-03-11 14:52:58 +00:00
|
|
|
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
|
2023-09-08 01:39:26 +08:00
|
|
|
if body.tainted_by_errors.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:20:31 +00:00
|
|
|
// will be evaluated by miri and produce its errors there
|
|
|
|
if body.source.promoted.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let def_id = body.source.def_id().expect_local();
|
2023-10-01 13:47:50 +00:00
|
|
|
let def_kind = tcx.def_kind(def_id);
|
|
|
|
let is_fn_like = def_kind.is_fn_like();
|
|
|
|
let is_assoc_const = def_kind == DefKind::AssocConst;
|
2022-03-08 17:20:31 +00:00
|
|
|
|
|
|
|
// Only run const prop on functions, methods, closures and associated constants
|
|
|
|
if !is_fn_like && !is_assoc_const {
|
|
|
|
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
|
2023-10-01 13:49:19 +00:00
|
|
|
trace!("ConstPropLint skipped for {:?}", def_id);
|
2022-03-08 17:20:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
// FIXME(welseywiser) const prop doesn't work on coroutines because of query cycles
|
2022-03-08 17:20:31 +00:00
|
|
|
// computing their layout.
|
2023-11-26 21:05:08 +08:00
|
|
|
if tcx.is_coroutine(def_id.to_def_id()) {
|
2023-10-19 21:46:28 +00:00
|
|
|
trace!("ConstPropLint skipped for coroutine {:?}", def_id);
|
2022-03-08 17:20:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-01 13:49:19 +00:00
|
|
|
trace!("ConstPropLint starting for {:?}", def_id);
|
2022-03-08 17:20:31 +00:00
|
|
|
|
|
|
|
// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
|
|
|
|
// constants, instead of just checking for const-folding succeeding.
|
|
|
|
// That would require a uniform one-def no-mutation analysis
|
|
|
|
// and RPO (or recursing when needing the value of a local).
|
2023-09-17 08:54:56 +00:00
|
|
|
let mut linter = ConstPropagator::new(body, tcx);
|
|
|
|
linter.visit_body(body);
|
2022-03-08 17:20:31 +00:00
|
|
|
|
2023-10-01 13:49:19 +00:00
|
|
|
trace!("ConstPropLint done for {:?}", def_id);
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds optimization opportunities on the MIR.
|
|
|
|
struct ConstPropagator<'mir, 'tcx> {
|
2024-01-15 13:33:58 +00:00
|
|
|
ecx: InterpCx<'mir, 'tcx, DummyMachine>,
|
2022-03-08 17:20:31 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ParamEnv<'tcx>,
|
2023-03-30 18:40:47 +00:00
|
|
|
worklist: Vec<BasicBlock>,
|
|
|
|
visited_blocks: BitSet<BasicBlock>,
|
2024-01-05 17:08:58 +00:00
|
|
|
locals: IndexVec<Local, Value<'tcx>>,
|
2024-01-05 17:19:18 +00:00
|
|
|
body: &'mir Body<'tcx>,
|
2024-01-05 17:26:59 +00:00
|
|
|
written_only_inside_own_block_locals: FxHashSet<Local>,
|
|
|
|
can_const_prop: IndexVec<Local, ConstPropMode>,
|
2024-01-05 17:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Value<'tcx> {
|
|
|
|
Immediate(OpTy<'tcx>),
|
|
|
|
Aggregate { variant: VariantIdx, fields: IndexVec<FieldIdx, Value<'tcx>> },
|
|
|
|
Uninit,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<OpTy<'tcx>> for Value<'tcx> {
|
|
|
|
fn from(v: OpTy<'tcx>) -> Self {
|
|
|
|
Self::Immediate(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<ImmTy<'tcx>> for Value<'tcx> {
|
|
|
|
fn from(v: ImmTy<'tcx>) -> Self {
|
|
|
|
Self::Immediate(v.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Value<'tcx> {
|
2024-01-15 14:38:45 +00:00
|
|
|
fn project(&self, proj: impl Iterator<Item = Option<PlaceElem<'tcx>>>) -> Option<&Value<'tcx>> {
|
2024-01-05 17:08:58 +00:00
|
|
|
let mut this = self;
|
|
|
|
for proj in proj {
|
|
|
|
this = match (proj?, this) {
|
|
|
|
(ProjectionElem::Field(idx, _), Value::Aggregate { fields, .. }) => {
|
|
|
|
fields.get(idx).unwrap_or(&Value::Uninit)
|
|
|
|
}
|
2024-01-15 14:38:45 +00:00
|
|
|
(
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length: 1, from_end: false },
|
|
|
|
Value::Aggregate { fields, .. },
|
|
|
|
) => fields
|
|
|
|
.get(FieldIdx::from_u32(offset.try_into().ok()?))
|
|
|
|
.unwrap_or(&Value::Uninit),
|
2024-01-05 17:08:58 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Some(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn project_mut(&mut self, proj: &[PlaceElem<'_>]) -> Option<&mut Value<'tcx>> {
|
|
|
|
let mut this = self;
|
|
|
|
for proj in proj {
|
|
|
|
this = match (proj, this) {
|
|
|
|
(PlaceElem::Field(idx, _), Value::Aggregate { fields, .. }) => {
|
|
|
|
fields.ensure_contains_elem(*idx, || Value::Uninit)
|
|
|
|
}
|
|
|
|
(PlaceElem::Field(..), val @ Value::Uninit) => {
|
|
|
|
*val = Value::Aggregate {
|
|
|
|
variant: VariantIdx::new(0),
|
|
|
|
fields: Default::default(),
|
|
|
|
};
|
|
|
|
val.project_mut(&[*proj])?
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Some(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn immediate(&self) -> Option<&OpTy<'tcx>> {
|
|
|
|
match self {
|
|
|
|
Value::Immediate(op) => Some(op),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> LayoutOfHelpers<'tcx> for ConstPropagator<'_, 'tcx> {
|
|
|
|
type LayoutOfResult = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn handle_layout_err(&self, err: LayoutError<'tcx>, _: Span, _: Ty<'tcx>) -> LayoutError<'tcx> {
|
|
|
|
err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasDataLayout for ConstPropagator<'_, '_> {
|
|
|
|
#[inline]
|
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ty::layout::HasTyCtxt<'tcx> for ConstPropagator<'_, 'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ty::layout::HasParamEnv<'tcx> for ConstPropagator<'_, 'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
|
|
|
self.param_env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
2023-09-17 08:54:56 +00:00
|
|
|
fn new(body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>) -> ConstPropagator<'mir, 'tcx> {
|
2022-03-08 17:20:31 +00:00
|
|
|
let def_id = body.source.def_id();
|
|
|
|
let param_env = tcx.param_env_reveal_all_normalized(def_id);
|
|
|
|
|
|
|
|
let can_const_prop = CanConstProp::check(tcx, param_env, body);
|
2024-01-15 13:33:58 +00:00
|
|
|
let ecx = InterpCx::new(tcx, tcx.def_span(def_id), param_env, DummyMachine);
|
2022-03-08 17:20:31 +00:00
|
|
|
|
2023-03-30 18:40:47 +00:00
|
|
|
ConstPropagator {
|
|
|
|
ecx,
|
|
|
|
tcx,
|
|
|
|
param_env,
|
|
|
|
worklist: vec![START_BLOCK],
|
|
|
|
visited_blocks: BitSet::new_empty(body.basic_blocks.len()),
|
2024-01-05 17:08:58 +00:00
|
|
|
locals: IndexVec::from_elem_n(Value::Uninit, body.local_decls.len()),
|
2024-01-05 17:19:18 +00:00
|
|
|
body,
|
2024-01-05 17:26:59 +00:00
|
|
|
can_const_prop,
|
|
|
|
written_only_inside_own_block_locals: Default::default(),
|
2023-03-30 18:40:47 +00:00
|
|
|
}
|
2023-03-30 18:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn local_decls(&self) -> &'mir LocalDecls<'tcx> {
|
2024-01-05 17:19:18 +00:00
|
|
|
&self.body.local_decls
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 17:08:58 +00:00
|
|
|
fn get_const(&self, place: Place<'tcx>) -> Option<&Value<'tcx>> {
|
|
|
|
self.locals[place.local]
|
2024-01-15 14:38:45 +00:00
|
|
|
.project(place.projection.iter().map(|proj| self.try_eval_index_offset(proj)))
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove `local` from the pool of `Locals`. Allows writing to them,
|
|
|
|
/// but not reading from them anymore.
|
2024-01-05 17:08:58 +00:00
|
|
|
fn remove_const(&mut self, local: Local) {
|
|
|
|
self.locals[local] = Value::Uninit;
|
2024-01-05 17:26:59 +00:00
|
|
|
self.written_only_inside_own_block_locals.remove(&local);
|
2024-01-05 17:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn access_mut(&mut self, place: &Place<'_>) -> Option<&mut Value<'tcx>> {
|
2024-01-05 17:26:59 +00:00
|
|
|
match self.can_const_prop[place.local] {
|
2024-01-05 17:08:58 +00:00
|
|
|
ConstPropMode::NoPropagation => return None,
|
|
|
|
ConstPropMode::OnlyInsideOwnBlock => {
|
2024-01-05 17:26:59 +00:00
|
|
|
self.written_only_inside_own_block_locals.insert(place.local);
|
2024-01-05 17:08:58 +00:00
|
|
|
}
|
|
|
|
ConstPropMode::FullConstProp => {}
|
|
|
|
}
|
|
|
|
self.locals[place.local].project_mut(place.projection)
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
|
2024-01-05 17:19:18 +00:00
|
|
|
source_info.scope.lint_root(&self.body.source_scopes)
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 17:12:44 +00:00
|
|
|
fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
|
2022-03-08 17:20:31 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
|
|
|
|
{
|
|
|
|
match f(self) {
|
|
|
|
Ok(val) => Some(val),
|
|
|
|
Err(error) => {
|
|
|
|
trace!("InterpCx operation failed: {:?}", error);
|
|
|
|
// Some errors shouldn't come up because creating them causes
|
|
|
|
// an allocation, which we should avoid. When that happens,
|
|
|
|
// dedicated error variants should be introduced instead.
|
|
|
|
assert!(
|
|
|
|
!error.kind().formatted_string(),
|
2023-08-27 14:41:35 +02:00
|
|
|
"const-prop encountered formatting error: {}",
|
|
|
|
self.ecx.format_error(error),
|
2022-03-08 17:20:31 +00:00
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the value, if any, of evaluating `c`.
|
2024-01-15 14:48:17 +00:00
|
|
|
fn eval_constant(&mut self, c: &ConstOperand<'tcx>) -> Option<OpTy<'tcx>> {
|
2022-03-08 17:20:31 +00:00
|
|
|
// FIXME we need to revisit this for #67176
|
2023-04-27 07:52:17 +01:00
|
|
|
if c.has_param() {
|
2022-03-08 17:20:31 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-04-04 10:39:26 +00:00
|
|
|
// Normalization needed b/c const prop lint runs in
|
|
|
|
// `mir_drops_elaborated_and_const_checked`, which happens before
|
|
|
|
// optimized MIR. Only after optimizing the MIR can we guarantee
|
|
|
|
// that the `RevealAll` pass has happened and that the body's consts
|
|
|
|
// are normalized, so any call to resolve before that needs to be
|
|
|
|
// manually normalized.
|
2023-09-20 20:51:14 +02:00
|
|
|
let val = self.tcx.try_normalize_erasing_regions(self.param_env, c.const_).ok()?;
|
2023-04-04 10:39:26 +00:00
|
|
|
|
2024-01-15 14:48:17 +00:00
|
|
|
self.use_ecx(|this| this.ecx.eval_mir_constant(&val, Some(c.span), None))
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the value, if any, of evaluating `place`.
|
2024-01-05 17:08:58 +00:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
2024-01-15 14:48:17 +00:00
|
|
|
fn eval_place(&mut self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
|
2024-01-05 17:08:58 +00:00
|
|
|
match self.get_const(place)? {
|
|
|
|
Value::Immediate(op) => Some(op.clone()),
|
|
|
|
Value::Aggregate { .. } => None,
|
|
|
|
Value::Uninit => None,
|
|
|
|
}
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
|
|
|
|
/// or `eval_place`, depending on the variant of `Operand` used.
|
2024-01-15 14:48:17 +00:00
|
|
|
fn eval_operand(&mut self, op: &Operand<'tcx>) -> Option<OpTy<'tcx>> {
|
2022-03-08 17:20:31 +00:00
|
|
|
match *op {
|
2024-01-15 14:48:17 +00:00
|
|
|
Operand::Constant(ref c) => self.eval_constant(c),
|
|
|
|
Operand::Move(place) | Operand::Copy(place) => self.eval_place(place),
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:18:50 +00:00
|
|
|
fn report_assert_as_lint(
|
|
|
|
&self,
|
|
|
|
location: Location,
|
|
|
|
lint_kind: AssertLintKind,
|
|
|
|
assert_kind: AssertKind<impl Debug>,
|
|
|
|
) {
|
|
|
|
let source_info = self.body.source_info(location);
|
2023-03-30 18:08:09 +00:00
|
|
|
if let Some(lint_root) = self.lint_root(*source_info) {
|
2024-01-15 13:18:50 +00:00
|
|
|
let span = source_info.span;
|
|
|
|
self.tcx.emit_node_span_lint(
|
|
|
|
lint_kind.lint(),
|
|
|
|
lint_root,
|
|
|
|
span,
|
|
|
|
AssertLint { span, assert_kind, lint_kind },
|
|
|
|
);
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:08:09 +00:00
|
|
|
fn check_unary_op(&mut self, op: UnOp, arg: &Operand<'tcx>, location: Location) -> Option<()> {
|
2024-01-15 14:48:17 +00:00
|
|
|
let arg = self.eval_operand(arg)?;
|
2024-01-05 17:12:44 +00:00
|
|
|
if let (val, true) = self.use_ecx(|this| {
|
2024-01-05 17:08:58 +00:00
|
|
|
let val = this.ecx.read_immediate(&arg)?;
|
2023-09-20 21:49:30 +02:00
|
|
|
let (_res, overflow) = this.ecx.overflowing_unary_op(op, &val)?;
|
2022-03-08 17:20:31 +00:00
|
|
|
Ok((val, overflow))
|
|
|
|
})? {
|
|
|
|
// `AssertKind` only has an `OverflowNeg` variant, so make sure that is
|
|
|
|
// appropriate to use.
|
|
|
|
assert_eq!(op, UnOp::Neg, "Neg is the only UnOp that can overflow");
|
|
|
|
self.report_assert_as_lint(
|
2024-01-15 13:18:50 +00:00
|
|
|
location,
|
|
|
|
AssertLintKind::ArithmeticOverflow,
|
|
|
|
AssertKind::OverflowNeg(val.to_const_int()),
|
2022-03-08 17:20:31 +00:00
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_binary_op(
|
|
|
|
&mut self,
|
|
|
|
op: BinOp,
|
|
|
|
left: &Operand<'tcx>,
|
|
|
|
right: &Operand<'tcx>,
|
2023-03-30 18:08:09 +00:00
|
|
|
location: Location,
|
2022-03-08 17:20:31 +00:00
|
|
|
) -> Option<()> {
|
2024-01-15 14:48:17 +00:00
|
|
|
let r =
|
|
|
|
self.eval_operand(right).and_then(|r| self.use_ecx(|this| this.ecx.read_immediate(&r)));
|
|
|
|
let l =
|
|
|
|
self.eval_operand(left).and_then(|l| self.use_ecx(|this| this.ecx.read_immediate(&l)));
|
2022-03-08 17:20:31 +00:00
|
|
|
// Check for exceeding shifts *even if* we cannot evaluate the LHS.
|
2023-01-30 12:01:09 +00:00
|
|
|
if matches!(op, BinOp::Shr | BinOp::Shl) {
|
2022-07-15 22:58:20 -04:00
|
|
|
let r = r.clone()?;
|
2022-03-08 17:20:31 +00:00
|
|
|
// 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!
|
2023-03-30 18:01:42 +00:00
|
|
|
let left_ty = left.ty(self.local_decls(), self.tcx);
|
2022-03-08 17:20:31 +00:00
|
|
|
let left_size = self.ecx.layout_of(left_ty).ok()?.size;
|
|
|
|
let right_size = r.layout.size;
|
2022-08-01 19:05:20 -04:00
|
|
|
let r_bits = r.to_scalar().to_bits(right_size).ok();
|
2023-05-24 14:19:22 +00:00
|
|
|
if r_bits.is_some_and(|b| b >= left_size.bits() as u128) {
|
2023-03-30 18:08:09 +00:00
|
|
|
debug!("check_binary_op: reporting assert for {:?}", location);
|
2023-04-30 02:20:53 +01:00
|
|
|
let panic = AssertKind::Overflow(
|
|
|
|
op,
|
|
|
|
match l {
|
|
|
|
Some(l) => l.to_const_int(),
|
|
|
|
// Invent a dummy value, the diagnostic ignores it anyway
|
|
|
|
None => ConstInt::new(
|
|
|
|
ScalarInt::try_from_uint(1_u8, left_size).unwrap(),
|
|
|
|
left_ty.is_signed(),
|
|
|
|
left_ty.is_ptr_sized_integral(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
r.to_const_int(),
|
|
|
|
);
|
2024-01-15 13:18:50 +00:00
|
|
|
self.report_assert_as_lint(location, AssertLintKind::ArithmeticOverflow, panic);
|
2022-03-08 17:20:31 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 22:58:20 -04:00
|
|
|
if let (Some(l), Some(r)) = (l, r) {
|
2022-03-08 17:20:31 +00:00
|
|
|
// The remaining operators are handled through `overflowing_binary_op`.
|
2024-01-05 17:12:44 +00:00
|
|
|
if self.use_ecx(|this| {
|
2023-09-20 21:49:30 +02:00
|
|
|
let (_res, overflow) = this.ecx.overflowing_binary_op(op, &l, &r)?;
|
2022-03-08 17:20:31 +00:00
|
|
|
Ok(overflow)
|
|
|
|
})? {
|
|
|
|
self.report_assert_as_lint(
|
2024-01-15 13:18:50 +00:00
|
|
|
location,
|
|
|
|
AssertLintKind::ArithmeticOverflow,
|
|
|
|
AssertKind::Overflow(op, l.to_const_int(), r.to_const_int()),
|
2022-03-08 17:20:31 +00:00
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:08:09 +00:00
|
|
|
fn check_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) -> Option<()> {
|
2022-03-08 17:20:31 +00:00
|
|
|
// Perform any special handling for specific Rvalue types.
|
|
|
|
// Generally, checks here fall into one of two categories:
|
|
|
|
// 1. Additional checking to provide useful lints to the user
|
|
|
|
// - In this case, we will do some validation and then fall through to the
|
|
|
|
// end of the function which evals the assignment.
|
|
|
|
// 2. Working around bugs in other parts of the compiler
|
|
|
|
// - In this case, we'll return `None` from this function to stop evaluation.
|
|
|
|
match rvalue {
|
|
|
|
// Additional checking: give lints to the user if an overflow would occur.
|
|
|
|
// We do this here and not in the `Assert` terminator as that terminator is
|
|
|
|
// only sometimes emitted (overflow checks can be disabled), but we want to always
|
|
|
|
// lint.
|
|
|
|
Rvalue::UnaryOp(op, arg) => {
|
|
|
|
trace!("checking UnaryOp(op = {:?}, arg = {:?})", op, arg);
|
2023-03-30 18:08:09 +00:00
|
|
|
self.check_unary_op(*op, arg, location)?;
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
Rvalue::BinaryOp(op, box (left, right)) => {
|
|
|
|
trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
|
2023-03-30 18:08:09 +00:00
|
|
|
self.check_binary_op(*op, left, right, location)?;
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
Rvalue::CheckedBinaryOp(op, box (left, right)) => {
|
|
|
|
trace!(
|
|
|
|
"checking CheckedBinaryOp(op = {:?}, left = {:?}, right = {:?})",
|
|
|
|
op,
|
|
|
|
left,
|
|
|
|
right
|
|
|
|
);
|
2023-03-30 18:08:09 +00:00
|
|
|
self.check_binary_op(*op, left, right, location)?;
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not try creating references (#67862)
|
|
|
|
Rvalue::AddressOf(_, place) | Rvalue::Ref(_, _, place) => {
|
|
|
|
trace!("skipping AddressOf | Ref for {:?}", place);
|
|
|
|
|
|
|
|
// This may be creating mutable references or immutable references to cells.
|
|
|
|
// If that happens, the pointed to value could be mutated via that reference.
|
|
|
|
// Since we aren't tracking references, the const propagator loses track of what
|
|
|
|
// value the local has right now.
|
|
|
|
// Thus, all locals that have their reference taken
|
|
|
|
// must not take part in propagation.
|
2024-01-05 17:08:58 +00:00
|
|
|
self.remove_const(place.local);
|
2022-03-08 17:20:31 +00:00
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Rvalue::ThreadLocalRef(def_id) => {
|
|
|
|
trace!("skipping ThreadLocalRef({:?})", def_id);
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's no other checking to do at this time.
|
|
|
|
Rvalue::Aggregate(..)
|
|
|
|
| Rvalue::Use(..)
|
2022-06-13 16:37:41 +03:00
|
|
|
| Rvalue::CopyForDeref(..)
|
2022-03-08 17:20:31 +00:00
|
|
|
| Rvalue::Repeat(..)
|
|
|
|
| Rvalue::Len(..)
|
|
|
|
| Rvalue::Cast(..)
|
|
|
|
| Rvalue::ShallowInitBox(..)
|
|
|
|
| Rvalue::Discriminant(..)
|
|
|
|
| Rvalue::NullaryOp(..) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME we need to revisit this for #67176
|
2023-04-27 07:52:17 +01:00
|
|
|
if rvalue.has_param() {
|
2022-03-08 17:20:31 +00:00
|
|
|
return None;
|
|
|
|
}
|
2023-03-30 18:01:42 +00:00
|
|
|
if !rvalue.ty(self.local_decls(), self.tcx).is_sized(self.tcx, self.param_env) {
|
2022-08-07 10:36:42 -04:00
|
|
|
// the interpreter doesn't support unsized locals (only unsized arguments),
|
|
|
|
// but rustc does (in a kinda broken way), so we have to skip them here
|
|
|
|
return None;
|
|
|
|
}
|
2022-03-08 17:20:31 +00:00
|
|
|
|
2023-03-07 16:34:11 +00:00
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:19:20 +00:00
|
|
|
fn check_assertion(
|
|
|
|
&mut self,
|
|
|
|
expected: bool,
|
|
|
|
msg: &AssertKind<Operand<'tcx>>,
|
|
|
|
cond: &Operand<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) -> Option<!> {
|
2024-01-15 14:48:17 +00:00
|
|
|
let value = &self.eval_operand(cond)?;
|
2023-03-30 18:19:20 +00:00
|
|
|
trace!("assertion on {:?} should be {:?}", value, expected);
|
|
|
|
|
|
|
|
let expected = Scalar::from_bool(expected);
|
2024-01-05 17:12:44 +00:00
|
|
|
let value_const = self.use_ecx(|this| this.ecx.read_scalar(value))?;
|
2023-03-30 18:19:20 +00:00
|
|
|
|
|
|
|
if expected != value_const {
|
|
|
|
// Poison all places this operand references so that further code
|
|
|
|
// doesn't use the invalid value
|
2023-03-30 18:40:47 +00:00
|
|
|
if let Some(place) = cond.place() {
|
2024-01-05 17:08:58 +00:00
|
|
|
self.remove_const(place.local);
|
2023-03-30 18:19:20 +00:00
|
|
|
}
|
2023-03-30 18:40:47 +00:00
|
|
|
|
2023-03-30 18:19:20 +00:00
|
|
|
enum DbgVal<T> {
|
|
|
|
Val(T),
|
|
|
|
Underscore,
|
|
|
|
}
|
|
|
|
impl<T: std::fmt::Debug> std::fmt::Debug for DbgVal<T> {
|
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Val(val) => val.fmt(fmt),
|
|
|
|
Self::Underscore => fmt.write_str("_"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut eval_to_int = |op| {
|
|
|
|
// This can be `None` if the lhs wasn't const propagated and we just
|
|
|
|
// triggered the assert on the value of the rhs.
|
2024-01-15 14:48:17 +00:00
|
|
|
self.eval_operand(op)
|
2023-03-30 18:19:20 +00:00
|
|
|
.and_then(|op| self.ecx.read_immediate(&op).ok())
|
|
|
|
.map_or(DbgVal::Underscore, |op| DbgVal::Val(op.to_const_int()))
|
|
|
|
};
|
|
|
|
let msg = match msg {
|
|
|
|
AssertKind::DivisionByZero(op) => AssertKind::DivisionByZero(eval_to_int(op)),
|
|
|
|
AssertKind::RemainderByZero(op) => AssertKind::RemainderByZero(eval_to_int(op)),
|
|
|
|
AssertKind::Overflow(bin_op @ (BinOp::Div | BinOp::Rem), op1, op2) => {
|
|
|
|
// Division overflow is *UB* in the MIR, and different than the
|
|
|
|
// other overflow checks.
|
|
|
|
AssertKind::Overflow(*bin_op, eval_to_int(op1), eval_to_int(op2))
|
|
|
|
}
|
|
|
|
AssertKind::BoundsCheck { ref len, ref index } => {
|
|
|
|
let len = eval_to_int(len);
|
|
|
|
let index = eval_to_int(index);
|
|
|
|
AssertKind::BoundsCheck { len, index }
|
|
|
|
}
|
|
|
|
// Remaining overflow errors are already covered by checks on the binary operators.
|
|
|
|
AssertKind::Overflow(..) | AssertKind::OverflowNeg(_) => return None,
|
|
|
|
// Need proper const propagator for these.
|
|
|
|
_ => return None,
|
|
|
|
};
|
2024-01-15 13:18:50 +00:00
|
|
|
self.report_assert_as_lint(location, AssertLintKind::UnconditionalPanic, msg);
|
2023-03-30 18:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:43:14 +00:00
|
|
|
fn ensure_not_propagated(&self, local: Local) {
|
2023-03-07 16:34:11 +00:00
|
|
|
if cfg!(debug_assertions) {
|
2024-01-05 17:08:58 +00:00
|
|
|
let val = self.get_const(local.into());
|
2023-03-07 16:34:11 +00:00
|
|
|
assert!(
|
2024-01-05 17:08:58 +00:00
|
|
|
matches!(val, Some(Value::Uninit))
|
2023-03-07 16:34:11 +00:00
|
|
|
|| self
|
2023-03-30 18:01:42 +00:00
|
|
|
.layout_of(self.local_decls()[local].ty)
|
2023-03-07 16:34:11 +00:00
|
|
|
.map_or(true, |layout| layout.is_zst()),
|
2024-01-05 17:08:58 +00:00
|
|
|
"failed to remove values for `{local:?}`, value={val:?}",
|
2023-03-07 16:34:11 +00:00
|
|
|
)
|
|
|
|
}
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
2024-01-04 16:27:05 +00:00
|
|
|
|
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn eval_rvalue(
|
|
|
|
&mut self,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
dest: &Place<'tcx>,
|
|
|
|
) -> Option<()> {
|
|
|
|
if !dest.projection.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
use rustc_middle::mir::Rvalue::*;
|
2024-01-05 17:19:18 +00:00
|
|
|
let layout = self.ecx.layout_of(dest.ty(self.body, self.tcx).ty).ok()?;
|
2024-01-05 17:08:58 +00:00
|
|
|
trace!(?layout);
|
2024-01-04 16:27:05 +00:00
|
|
|
|
2024-01-05 17:08:58 +00:00
|
|
|
let val: Value<'_> = match *rvalue {
|
2024-01-04 16:27:05 +00:00
|
|
|
ThreadLocalRef(_) => return None,
|
|
|
|
|
2024-01-15 14:48:17 +00:00
|
|
|
Use(ref operand) => self.eval_operand(operand)?.into(),
|
2024-01-04 16:27:05 +00:00
|
|
|
|
2024-01-15 14:48:17 +00:00
|
|
|
CopyForDeref(place) => self.eval_place(place)?.into(),
|
2024-01-04 16:27:05 +00:00
|
|
|
|
|
|
|
BinaryOp(bin_op, box (ref left, ref right)) => {
|
2024-01-15 14:48:17 +00:00
|
|
|
let left = self.eval_operand(left)?;
|
2024-01-05 17:12:44 +00:00
|
|
|
let left = self.use_ecx(|this| this.ecx.read_immediate(&left))?;
|
2024-01-04 16:27:05 +00:00
|
|
|
|
2024-01-15 14:48:17 +00:00
|
|
|
let right = self.eval_operand(right)?;
|
2024-01-05 17:12:44 +00:00
|
|
|
let right = self.use_ecx(|this| this.ecx.read_immediate(&right))?;
|
2024-01-04 16:27:05 +00:00
|
|
|
|
2024-01-05 17:12:44 +00:00
|
|
|
let val =
|
|
|
|
self.use_ecx(|this| this.ecx.wrapping_binary_op(bin_op, &left, &right))?;
|
2024-01-04 16:27:05 +00:00
|
|
|
val.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckedBinaryOp(bin_op, box (ref left, ref right)) => {
|
2024-01-15 14:48:17 +00:00
|
|
|
let left = self.eval_operand(left)?;
|
2024-01-05 17:12:44 +00:00
|
|
|
let left = self.use_ecx(|this| this.ecx.read_immediate(&left))?;
|
2024-01-04 16:27:05 +00:00
|
|
|
|
2024-01-15 14:48:17 +00:00
|
|
|
let right = self.eval_operand(right)?;
|
2024-01-05 17:12:44 +00:00
|
|
|
let right = self.use_ecx(|this| this.ecx.read_immediate(&right))?;
|
2024-01-04 16:27:05 +00:00
|
|
|
|
2024-01-05 17:12:44 +00:00
|
|
|
let (val, overflowed) =
|
|
|
|
self.use_ecx(|this| this.ecx.overflowing_binary_op(bin_op, &left, &right))?;
|
2024-01-05 17:08:58 +00:00
|
|
|
let overflowed = ImmTy::from_bool(overflowed, self.tcx);
|
|
|
|
Value::Aggregate {
|
|
|
|
variant: VariantIdx::new(0),
|
|
|
|
fields: [Value::from(val), overflowed.into()].into_iter().collect(),
|
|
|
|
}
|
2024-01-04 16:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnaryOp(un_op, ref operand) => {
|
2024-01-15 14:48:17 +00:00
|
|
|
let operand = self.eval_operand(operand)?;
|
2024-01-05 17:12:44 +00:00
|
|
|
let val = self.use_ecx(|this| this.ecx.read_immediate(&operand))?;
|
2024-01-04 16:27:05 +00:00
|
|
|
|
2024-01-05 17:12:44 +00:00
|
|
|
let val = self.use_ecx(|this| this.ecx.wrapping_unary_op(un_op, &val))?;
|
2024-01-04 16:27:05 +00:00
|
|
|
val.into()
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:08:58 +00:00
|
|
|
Aggregate(ref kind, ref fields) => Value::Aggregate {
|
|
|
|
fields: fields
|
|
|
|
.iter()
|
2024-01-15 14:48:17 +00:00
|
|
|
.map(|field| self.eval_operand(field).map_or(Value::Uninit, Value::Immediate))
|
2024-01-05 17:08:58 +00:00
|
|
|
.collect(),
|
|
|
|
variant: match **kind {
|
|
|
|
AggregateKind::Adt(_, variant, _, _, _) => variant,
|
|
|
|
AggregateKind::Array(_)
|
|
|
|
| AggregateKind::Tuple
|
|
|
|
| AggregateKind::Closure(_, _)
|
|
|
|
| AggregateKind::Coroutine(_, _) => VariantIdx::new(0),
|
|
|
|
},
|
|
|
|
},
|
2024-01-04 16:27:05 +00:00
|
|
|
|
|
|
|
Repeat(ref op, n) => {
|
|
|
|
trace!(?op, ?n);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Len(place) => {
|
2024-01-05 17:08:58 +00:00
|
|
|
let len = match self.get_const(place)? {
|
|
|
|
Value::Immediate(src) => src.len(&self.ecx).ok()?,
|
|
|
|
Value::Aggregate { fields, .. } => fields.len() as u64,
|
|
|
|
Value::Uninit => match place.ty(self.local_decls(), self.tcx).ty.kind() {
|
|
|
|
ty::Array(_, n) => n.try_eval_target_usize(self.tcx, self.param_env)?,
|
|
|
|
_ => return None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
ImmTy::from_scalar(Scalar::from_target_usize(len, self), layout).into()
|
2024-01-04 16:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref(..) | AddressOf(..) => return None,
|
|
|
|
|
|
|
|
NullaryOp(ref null_op, ty) => {
|
2024-01-05 17:12:44 +00:00
|
|
|
let op_layout = self.use_ecx(|this| this.ecx.layout_of(ty))?;
|
2024-01-04 16:27:05 +00:00
|
|
|
let val = match null_op {
|
2024-01-05 17:08:58 +00:00
|
|
|
NullOp::SizeOf => op_layout.size.bytes(),
|
|
|
|
NullOp::AlignOf => op_layout.align.abi.bytes(),
|
2024-01-04 16:27:05 +00:00
|
|
|
NullOp::OffsetOf(fields) => {
|
2024-01-05 17:08:58 +00:00
|
|
|
op_layout.offset_of_subfield(self, fields.iter()).bytes()
|
2024-01-04 16:27:05 +00:00
|
|
|
}
|
|
|
|
};
|
2024-01-05 17:08:58 +00:00
|
|
|
ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into()
|
2024-01-04 16:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShallowInitBox(..) => return None,
|
|
|
|
|
|
|
|
Cast(ref kind, ref value, to) => match kind {
|
|
|
|
CastKind::IntToInt | CastKind::IntToFloat => {
|
2024-01-15 14:48:17 +00:00
|
|
|
let value = self.eval_operand(value)?;
|
2024-01-04 16:27:05 +00:00
|
|
|
let value = self.ecx.read_immediate(&value).ok()?;
|
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
|
|
|
let res = self.ecx.int_to_int_or_float(&value, to).ok()?;
|
|
|
|
res.into()
|
|
|
|
}
|
|
|
|
CastKind::FloatToFloat | CastKind::FloatToInt => {
|
2024-01-15 14:48:17 +00:00
|
|
|
let value = self.eval_operand(value)?;
|
2024-01-04 16:27:05 +00:00
|
|
|
let value = self.ecx.read_immediate(&value).ok()?;
|
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
|
|
|
let res = self.ecx.float_to_float_or_int(&value, to).ok()?;
|
|
|
|
res.into()
|
|
|
|
}
|
|
|
|
CastKind::Transmute => {
|
2024-01-15 14:48:17 +00:00
|
|
|
let value = self.eval_operand(value)?;
|
2024-01-04 16:27:05 +00:00
|
|
|
let to = self.ecx.layout_of(to).ok()?;
|
|
|
|
// `offset` for immediates only supports scalar/scalar-pair ABIs,
|
|
|
|
// so bail out if the target is not one.
|
|
|
|
if value.as_mplace_or_imm().is_right() {
|
|
|
|
match (value.layout.abi, to.abi) {
|
|
|
|
(Abi::Scalar(..), Abi::Scalar(..)) => {}
|
|
|
|
(Abi::ScalarPair(..), Abi::ScalarPair(..)) => {}
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
2024-01-05 17:08:58 +00:00
|
|
|
value.offset(Size::ZERO, to, &self.ecx).ok()?.into()
|
2024-01-04 16:27:05 +00:00
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
},
|
|
|
|
|
|
|
|
Discriminant(place) => {
|
2024-01-05 17:08:58 +00:00
|
|
|
let variant = match self.get_const(place)? {
|
|
|
|
Value::Immediate(op) => {
|
|
|
|
let op = op.clone();
|
2024-01-05 17:12:44 +00:00
|
|
|
self.use_ecx(|this| this.ecx.read_discriminant(&op))?
|
2024-01-05 17:08:58 +00:00
|
|
|
}
|
|
|
|
Value::Aggregate { variant, .. } => *variant,
|
|
|
|
Value::Uninit => return None,
|
|
|
|
};
|
2024-01-05 17:12:44 +00:00
|
|
|
let imm = self.use_ecx(|this| {
|
2024-01-05 17:08:58 +00:00
|
|
|
this.ecx.discriminant_for_variant(
|
|
|
|
place.ty(this.local_decls(), this.tcx).ty,
|
|
|
|
variant,
|
|
|
|
)
|
2024-01-04 16:27:05 +00:00
|
|
|
})?;
|
|
|
|
imm.into()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
trace!(?val);
|
|
|
|
|
2024-01-05 17:08:58 +00:00
|
|
|
*self.access_mut(dest)? = val;
|
2024-01-04 16:27:05 +00:00
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
2024-01-05 17:08:58 +00:00
|
|
|
|
2024-01-15 14:38:45 +00:00
|
|
|
fn try_eval_index_offset(&self, proj: PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
|
2024-01-05 17:08:58 +00:00
|
|
|
Some(match proj {
|
|
|
|
ProjectionElem::Index(local) => {
|
|
|
|
let val = self.get_const(local.into())?;
|
|
|
|
let op = val.immediate()?;
|
2024-01-15 14:38:45 +00:00
|
|
|
ProjectionElem::ConstantIndex {
|
|
|
|
offset: self.ecx.read_target_usize(op).ok()?,
|
|
|
|
min_length: 1,
|
|
|
|
from_end: false,
|
|
|
|
}
|
2024-01-05 17:08:58 +00:00
|
|
|
}
|
2024-01-15 14:38:45 +00:00
|
|
|
other => other,
|
2024-01-05 17:08:58 +00:00
|
|
|
})
|
|
|
|
}
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 14:52:58 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
|
|
|
|
fn visit_body(&mut self, body: &Body<'tcx>) {
|
2023-03-30 18:40:47 +00:00
|
|
|
while let Some(bb) = self.worklist.pop() {
|
|
|
|
if !self.visited_blocks.insert(bb) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = &body.basic_blocks[bb];
|
2022-03-08 17:20:31 +00:00
|
|
|
self.visit_basic_block_data(bb, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:52:58 +00:00
|
|
|
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
|
2022-03-08 17:20:31 +00:00
|
|
|
self.super_operand(operand, location);
|
|
|
|
}
|
|
|
|
|
2023-09-20 20:51:14 +02:00
|
|
|
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
|
2022-03-08 17:20:31 +00:00
|
|
|
trace!("visit_constant: {:?}", constant);
|
|
|
|
self.super_constant(constant, location);
|
2024-01-15 14:48:17 +00:00
|
|
|
self.eval_constant(constant);
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 16:34:11 +00:00
|
|
|
fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
|
|
|
|
self.super_assign(place, rvalue, location);
|
|
|
|
|
2023-03-30 18:08:09 +00:00
|
|
|
let Some(()) = self.check_rvalue(rvalue, location) else { return };
|
2023-03-07 16:34:11 +00:00
|
|
|
|
2024-01-05 17:26:59 +00:00
|
|
|
match self.can_const_prop[place.local] {
|
2023-03-07 16:34:11 +00:00
|
|
|
// Do nothing if the place is indirect.
|
|
|
|
_ if place.is_indirect() => {}
|
|
|
|
ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
|
|
|
|
ConstPropMode::OnlyInsideOwnBlock | ConstPropMode::FullConstProp => {
|
2024-01-04 16:27:05 +00:00
|
|
|
if self.eval_rvalue(rvalue, location, place).is_none() {
|
2023-03-07 14:41:13 +00:00
|
|
|
// Const prop failed, so erase the destination, ensuring that whatever happens
|
|
|
|
// from here on, does not know about the previous value.
|
|
|
|
// This is important in case we have
|
|
|
|
// ```rust
|
|
|
|
// let mut x = 42;
|
|
|
|
// x = SOME_MUTABLE_STATIC;
|
|
|
|
// // x must now be uninit
|
|
|
|
// ```
|
|
|
|
// FIXME: we overzealously erase the entire local, because that's easier to
|
|
|
|
// implement.
|
|
|
|
trace!(
|
|
|
|
"propagation into {:?} failed.
|
|
|
|
Nuking the entire site from orbit, it's the only way to be sure",
|
|
|
|
place,
|
|
|
|
);
|
2024-01-05 17:08:58 +00:00
|
|
|
self.remove_const(place.local);
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
2023-03-07 14:41:13 +00:00
|
|
|
}
|
2023-03-07 16:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:52:58 +00:00
|
|
|
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
|
2022-03-08 17:20:31 +00:00
|
|
|
trace!("visit_statement: {:?}", statement);
|
2023-03-07 16:34:11 +00:00
|
|
|
|
2023-03-07 17:12:42 +00:00
|
|
|
// We want to evaluate operands before any change to the assigned-to value,
|
|
|
|
// so we recurse first.
|
2023-03-07 16:34:11 +00:00
|
|
|
self.super_statement(statement, location);
|
|
|
|
|
|
|
|
match statement.kind {
|
2024-01-05 17:08:58 +00:00
|
|
|
StatementKind::SetDiscriminant { ref place, variant_index } => {
|
2024-01-05 17:26:59 +00:00
|
|
|
match self.can_const_prop[place.local] {
|
2023-03-07 16:34:11 +00:00
|
|
|
// Do nothing if the place is indirect.
|
|
|
|
_ if place.is_indirect() => {}
|
|
|
|
ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
|
2023-03-07 14:41:13 +00:00
|
|
|
ConstPropMode::FullConstProp | ConstPropMode::OnlyInsideOwnBlock => {
|
2024-01-05 17:08:58 +00:00
|
|
|
match self.access_mut(place) {
|
|
|
|
Some(Value::Aggregate { variant, .. }) => *variant = variant_index,
|
|
|
|
_ => self.remove_const(place.local),
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 14:41:13 +00:00
|
|
|
StatementKind::StorageLive(local) => {
|
2024-01-05 17:08:58 +00:00
|
|
|
self.remove_const(local);
|
2023-03-07 14:41:13 +00:00
|
|
|
}
|
|
|
|
StatementKind::StorageDead(local) => {
|
2024-01-05 17:08:58 +00:00
|
|
|
self.remove_const(local);
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
2023-03-07 14:41:13 +00:00
|
|
|
_ => {}
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:52:58 +00:00
|
|
|
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
|
2022-03-08 17:20:31 +00:00
|
|
|
self.super_terminator(terminator, location);
|
2022-03-11 14:52:58 +00:00
|
|
|
match &terminator.kind {
|
|
|
|
TerminatorKind::Assert { expected, ref msg, ref cond, .. } => {
|
2023-03-30 18:19:20 +00:00
|
|
|
self.check_assertion(*expected, msg, cond, location);
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
2023-03-30 18:40:47 +00:00
|
|
|
TerminatorKind::SwitchInt { ref discr, ref targets } => {
|
2024-01-15 14:48:17 +00:00
|
|
|
if let Some(ref value) = self.eval_operand(discr)
|
2024-01-05 17:12:44 +00:00
|
|
|
&& let Some(value_const) = self.use_ecx(|this| this.ecx.read_scalar(value))
|
2023-03-30 18:40:47 +00:00
|
|
|
&& let Ok(constant) = value_const.try_to_int()
|
|
|
|
&& let Ok(constant) = constant.to_bits(constant.size())
|
|
|
|
{
|
2023-04-04 17:14:53 +00:00
|
|
|
// We managed to evaluate the discriminant, so we know we only need to visit
|
|
|
|
// one target.
|
2023-03-30 18:40:47 +00:00
|
|
|
let target = targets.target_for_value(constant);
|
|
|
|
self.worklist.push(target);
|
|
|
|
return;
|
|
|
|
}
|
2023-04-04 17:14:53 +00:00
|
|
|
// We failed to evaluate the discriminant, fallback to visiting all successors.
|
2023-03-30 18:40:47 +00:00
|
|
|
}
|
2022-03-08 17:20:31 +00:00
|
|
|
// None of these have Operands to const-propagate.
|
|
|
|
TerminatorKind::Goto { .. }
|
2023-08-19 13:10:25 +02:00
|
|
|
| TerminatorKind::UnwindResume
|
2023-08-21 09:57:10 +02:00
|
|
|
| TerminatorKind::UnwindTerminate(_)
|
2022-03-08 17:20:31 +00:00
|
|
|
| TerminatorKind::Return
|
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
| TerminatorKind::Drop { .. }
|
|
|
|
| TerminatorKind::Yield { .. }
|
2023-10-19 16:06:43 +00:00
|
|
|
| TerminatorKind::CoroutineDrop
|
2022-03-08 17:20:31 +00:00
|
|
|
| TerminatorKind::FalseEdge { .. }
|
|
|
|
| TerminatorKind::FalseUnwind { .. }
|
2022-03-11 14:52:58 +00:00
|
|
|
| TerminatorKind::Call { .. }
|
2022-03-08 17:20:31 +00:00
|
|
|
| TerminatorKind::InlineAsm { .. } => {}
|
|
|
|
}
|
2023-03-30 18:40:47 +00:00
|
|
|
|
|
|
|
self.worklist.extend(terminator.successors());
|
2023-03-07 14:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
|
|
|
|
self.super_basic_block_data(block, data);
|
2022-03-08 17:20:31 +00:00
|
|
|
|
|
|
|
// We remove all Locals which are restricted in propagation to their containing blocks and
|
|
|
|
// which were modified in the current block.
|
|
|
|
// Take it out of the ecx so we can get a mutable reference to the ecx for `remove_const`.
|
2023-03-13 17:43:14 +00:00
|
|
|
let mut written_only_inside_own_block_locals =
|
2024-01-05 17:26:59 +00:00
|
|
|
std::mem::take(&mut self.written_only_inside_own_block_locals);
|
2023-03-13 17:43:14 +00:00
|
|
|
|
|
|
|
// This loop can get very hot for some bodies: it check each local in each bb.
|
|
|
|
// To avoid this quadratic behaviour, we only clear the locals that were modified inside
|
|
|
|
// the current block.
|
2023-12-23 15:03:52 +01:00
|
|
|
// The order in which we remove consts does not matter.
|
|
|
|
#[allow(rustc::potential_query_instability)]
|
2023-03-13 17:43:14 +00:00
|
|
|
for local in written_only_inside_own_block_locals.drain() {
|
2024-01-05 17:26:59 +00:00
|
|
|
debug_assert_eq!(self.can_const_prop[local], ConstPropMode::OnlyInsideOwnBlock);
|
2024-01-05 17:08:58 +00:00
|
|
|
self.remove_const(local);
|
2023-03-13 17:43:14 +00:00
|
|
|
}
|
2024-01-05 17:26:59 +00:00
|
|
|
self.written_only_inside_own_block_locals = written_only_inside_own_block_locals;
|
2023-03-13 17:43:14 +00:00
|
|
|
|
2023-03-19 08:59:11 +00:00
|
|
|
if cfg!(debug_assertions) {
|
2024-01-05 17:26:59 +00:00
|
|
|
for (local, &mode) in self.can_const_prop.iter_enumerated() {
|
2023-03-19 08:59:11 +00:00
|
|
|
match mode {
|
|
|
|
ConstPropMode::FullConstProp => {}
|
|
|
|
ConstPropMode::NoPropagation | ConstPropMode::OnlyInsideOwnBlock => {
|
|
|
|
self.ensure_not_propagated(local);
|
|
|
|
}
|
2023-03-07 15:20:57 +00:00
|
|
|
}
|
2022-03-08 17:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|