2018-01-28 07:41:17 -06:00
|
|
|
//! Propagates constants for early reporting of statically known
|
|
|
|
//! assertion failures
|
|
|
|
|
2022-11-18 07:24:48 -06:00
|
|
|
use either::Right;
|
2022-11-18 03:18:32 -06:00
|
|
|
|
2022-11-21 10:51:16 -06:00
|
|
|
use rustc_const_eval::const_eval::CheckAlignment;
|
2023-03-13 12:43:14 -05:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-04-18 23:35:26 -05:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2023-03-31 02:32:44 -05:00
|
|
|
use rustc_index::vec::{IndexSlice, IndexVec};
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::mir::visit::{
|
2019-04-28 20:58:40 -05:00
|
|
|
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
|
2019-12-22 16:42:04 -06:00
|
|
|
};
|
2023-02-08 14:30:44 -06:00
|
|
|
use rustc_middle::mir::*;
|
2021-08-30 12:37:36 -05:00
|
|
|
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
|
2022-09-16 08:31:10 -05:00
|
|
|
use rustc_middle::ty::InternalSubsts;
|
2023-02-21 20:18:40 -06:00
|
|
|
use rustc_middle::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeVisitableExt};
|
2023-03-05 15:37:39 -06:00
|
|
|
use rustc_span::{def_id::DefId, Span, DUMMY_SP};
|
2022-11-22 05:16:33 -06:00
|
|
|
use rustc_target::abi::{self, Align, HasDataLayout, Size, TargetDataLayout};
|
2022-07-07 11:01:36 -05:00
|
|
|
use rustc_target::spec::abi::Abi as CallAbi;
|
2020-02-11 14:19:40 -06:00
|
|
|
use rustc_trait_selection::traits;
|
2018-01-28 07:41:17 -06:00
|
|
|
|
2020-12-31 18:53:25 -06:00
|
|
|
use crate::MirPass;
|
2021-01-05 13:08:11 -06:00
|
|
|
use rustc_const_eval::interpret::{
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-01 14:15:04 -06:00
|
|
|
self, compile_time_machine, AllocId, ConstAllocation, ConstValue, CtfeValidationMode, Frame,
|
2023-03-07 08:32:16 -06:00
|
|
|
ImmTy, Immediate, InterpCx, InterpResult, LocalValue, MemoryKind, OpTy, PlaceTy, Pointer,
|
2022-10-10 13:50:49 -05:00
|
|
|
Scalar, StackPopCleanup,
|
2018-11-06 09:16:27 -06:00
|
|
|
};
|
2018-09-18 04:01:13 -05:00
|
|
|
|
2020-07-27 06:52:40 -05:00
|
|
|
/// The maximum number of bytes that we'll allocate space for a local or the return value.
|
|
|
|
/// Needed for #66397, because otherwise we eval into large places and that can cause OOM or just
|
|
|
|
/// Severely regress performance.
|
2019-11-13 18:19:25 -06:00
|
|
|
const MAX_ALLOC_LIMIT: u64 = 1024;
|
|
|
|
|
2020-03-23 04:58:43 -05:00
|
|
|
/// Macro for machine-specific `InterpError` without allocation.
|
|
|
|
/// (These will never be shown to the user, but they help diagnose ICEs.)
|
|
|
|
macro_rules! throw_machine_stop_str {
|
|
|
|
($($tt:tt)*) => {{
|
|
|
|
// We make a new local type for it. The type itself does not carry any information,
|
|
|
|
// but its vtable (for the `MachineStopType` trait) does.
|
|
|
|
struct Zst;
|
2020-04-30 04:03:55 -05:00
|
|
|
// Printing this type shows the desired string.
|
|
|
|
impl std::fmt::Display for Zst {
|
2020-03-23 04:58:43 -05:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, $($tt)*)
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 09:41:09 -05:00
|
|
|
impl rustc_middle::mir::interpret::MachineStopType for Zst {}
|
2020-03-23 04:58:43 -05:00
|
|
|
throw_machine_stop!(Zst)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2018-01-28 07:41:17 -06:00
|
|
|
pub struct ConstProp;
|
|
|
|
|
2019-08-04 15:20:00 -05:00
|
|
|
impl<'tcx> MirPass<'tcx> for ConstProp {
|
2022-07-05 15:38:52 -05:00
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
2022-07-05 16:03:32 -05:00
|
|
|
sess.mir_opt_level() >= 1
|
2021-12-02 11:17:32 -06:00
|
|
|
}
|
|
|
|
|
2022-02-16 03:56:01 -06:00
|
|
|
#[instrument(skip(self, tcx), level = "debug")]
|
2020-10-04 13:01:38 -05:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2018-03-06 05:43:02 -06:00
|
|
|
// will be evaluated by miri and produce its errors there
|
2020-10-04 13:01:38 -05:00
|
|
|
if body.source.promoted.is_some() {
|
2018-03-06 05:43:02 -06:00
|
|
|
return;
|
|
|
|
}
|
2018-08-31 07:30:59 -05:00
|
|
|
|
2020-10-04 13:01:38 -05:00
|
|
|
let def_id = body.source.def_id().expect_local();
|
2022-03-29 16:50:01 -05: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;
|
2018-08-31 07:30:59 -05:00
|
|
|
|
|
|
|
// Only run const prop on functions, methods, closures and associated constants
|
|
|
|
if !is_fn_like && !is_assoc_const {
|
2018-08-26 08:19:34 -05:00
|
|
|
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
|
2020-10-04 13:01:38 -05:00
|
|
|
trace!("ConstProp skipped for {:?}", def_id);
|
2018-08-31 07:30:59 -05:00
|
|
|
return;
|
2018-03-06 05:43:02 -06:00
|
|
|
}
|
2018-08-31 07:30:59 -05:00
|
|
|
|
2023-02-07 02:29:48 -06:00
|
|
|
let is_generator = tcx.type_of(def_id.to_def_id()).subst_identity().is_generator();
|
2019-09-12 21:03:20 -05:00
|
|
|
// FIXME(welseywiser) const prop doesn't work on generators because of query cycles
|
|
|
|
// computing their layout.
|
|
|
|
if is_generator {
|
2020-10-04 13:01:38 -05:00
|
|
|
trace!("ConstProp skipped for generator {:?}", def_id);
|
2019-09-12 21:03:20 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-07 09:53:04 -06:00
|
|
|
// Check if it's even possible to satisfy the 'where' clauses
|
2020-01-05 21:32:53 -06:00
|
|
|
// for this item.
|
|
|
|
// This branch will never be taken for any normal function.
|
|
|
|
// However, it's possible to `#!feature(trivial_bounds)]` to write
|
|
|
|
// a function with impossible to satisfy clauses, e.g.:
|
|
|
|
// `fn foo() where String: Copy {}`
|
|
|
|
//
|
|
|
|
// We don't usually need to worry about this kind of case,
|
|
|
|
// since we would get a compilation error if the user tried
|
|
|
|
// to call it. However, since we can do const propagation
|
|
|
|
// even without any calls to the function, we need to make
|
|
|
|
// sure that it even makes sense to try to evaluate the body.
|
|
|
|
// If there are unsatisfiable where clauses, then all bets are
|
|
|
|
// off, and we just give up.
|
2020-01-13 05:06:42 -06:00
|
|
|
//
|
2020-01-16 17:53:51 -06:00
|
|
|
// We manually filter the predicates, skipping anything that's not
|
|
|
|
// "global". We are in a potentially generic context
|
2020-01-17 07:13:04 -06:00
|
|
|
// (e.g. we are evaluating a function without substituting generic
|
2020-01-16 17:53:51 -06:00
|
|
|
// parameters, so this filtering serves two purposes:
|
2020-01-13 05:06:42 -06:00
|
|
|
//
|
2020-01-16 17:53:51 -06:00
|
|
|
// 1. We skip evaluating any predicates that we would
|
|
|
|
// never be able prove are unsatisfiable (e.g. `<T as Foo>`
|
|
|
|
// 2. We avoid trying to normalize predicates involving generic
|
|
|
|
// parameters (e.g. `<T as Foo>::MyItem`). This can confuse
|
|
|
|
// the normalization code (leading to cycle errors), since
|
|
|
|
// it's usually never invoked in this way.
|
|
|
|
let predicates = tcx
|
2020-10-04 13:01:38 -05:00
|
|
|
.predicates_of(def_id.to_def_id())
|
2020-01-16 17:53:51 -06:00
|
|
|
.predicates
|
|
|
|
.iter()
|
2022-01-11 21:19:52 -06:00
|
|
|
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
|
2023-04-06 18:11:19 -05:00
|
|
|
if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) {
|
2020-10-04 13:01:38 -05:00
|
|
|
trace!("ConstProp skipped for {:?}: found unsatisfiable predicates", def_id);
|
2020-01-05 21:32:53 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-04 13:01:38 -05:00
|
|
|
trace!("ConstProp starting for {:?}", def_id);
|
2018-01-28 07:41:17 -06:00
|
|
|
|
2019-06-13 19:20:14 -05:00
|
|
|
let dummy_body = &Body::new(
|
2020-10-04 13:01:38 -05:00
|
|
|
body.source,
|
2023-03-31 02:32:44 -05:00
|
|
|
(*body.basic_blocks).to_owned(),
|
2019-11-26 14:17:35 -06:00
|
|
|
body.source_scopes.clone(),
|
2019-06-13 19:20:14 -05:00
|
|
|
body.local_decls.clone(),
|
|
|
|
Default::default(),
|
|
|
|
body.arg_count,
|
|
|
|
Default::default(),
|
2021-01-17 07:50:47 -06:00
|
|
|
body.span,
|
2021-01-17 06:27:05 -06:00
|
|
|
body.generator_kind(),
|
2022-02-08 00:00:15 -06:00
|
|
|
body.tainted_by_errors,
|
2019-06-13 19:20:14 -05:00
|
|
|
);
|
|
|
|
|
2018-01-29 08:12:45 -06:00
|
|
|
// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
|
|
|
|
// constants, instead of just checking for const-folding succeeding.
|
2021-08-22 11:15:49 -05:00
|
|
|
// That would require a uniform one-def no-mutation analysis
|
2018-01-29 08:12:45 -06:00
|
|
|
// and RPO (or recursing when needing the value of a local).
|
2020-10-04 13:01:38 -05:00
|
|
|
let mut optimization_finder = ConstPropagator::new(body, dummy_body, tcx);
|
2019-06-03 17:26:48 -05:00
|
|
|
optimization_finder.visit_body(body);
|
2018-01-28 07:41:17 -06:00
|
|
|
|
2020-10-04 13:01:38 -05:00
|
|
|
trace!("ConstProp done for {:?}", def_id);
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:17:58 -05:00
|
|
|
pub struct ConstPropMachine<'mir, 'tcx> {
|
2020-03-16 17:12:42 -05:00
|
|
|
/// The virtual call stack.
|
2021-07-12 11:22:15 -05:00
|
|
|
stack: Vec<Frame<'mir, 'tcx>>,
|
2023-03-13 12:43:14 -05:00
|
|
|
pub written_only_inside_own_block_locals: FxHashSet<Local>,
|
2022-07-12 07:17:58 -05:00
|
|
|
pub can_const_prop: IndexVec<Local, ConstPropMode>,
|
2020-03-16 17:12:42 -05:00
|
|
|
}
|
2019-09-24 20:12:59 -05:00
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl ConstPropMachine<'_, '_> {
|
2023-03-07 09:20:57 -06:00
|
|
|
pub fn new(can_const_prop: IndexVec<Local, ConstPropMode>) -> Self {
|
2023-03-13 12:43:14 -05:00
|
|
|
Self {
|
|
|
|
stack: Vec::new(),
|
|
|
|
written_only_inside_own_block_locals: Default::default(),
|
|
|
|
can_const_prop,
|
|
|
|
}
|
2020-03-16 17:12:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> {
|
2020-04-27 12:01:30 -05:00
|
|
|
compile_time_machine!(<'mir, 'tcx>);
|
2021-07-03 10:15:14 -05:00
|
|
|
const PANIC_ON_ALLOC_FAIL: bool = true; // all allocations are small (see `MAX_ALLOC_LIMIT`)
|
2019-09-24 20:12:59 -05:00
|
|
|
|
2020-12-03 09:39:39 -06:00
|
|
|
type MemoryKind = !;
|
|
|
|
|
2022-08-09 07:23:16 -05:00
|
|
|
#[inline(always)]
|
2022-11-21 10:51:16 -06:00
|
|
|
fn enforce_alignment(_ecx: &InterpCx<'mir, 'tcx, Self>) -> CheckAlignment {
|
2022-08-09 07:23:16 -05:00
|
|
|
// We do not check for alignment to avoid having to carry an `Align`
|
|
|
|
// in `ConstValue::ByRef`.
|
2022-11-21 10:51:16 -06:00
|
|
|
CheckAlignment::No
|
2022-08-09 07:23:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2023-03-21 03:52:31 -05:00
|
|
|
fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>, _layout: TyAndLayout<'tcx>) -> bool {
|
2022-08-09 07:23:16 -05:00
|
|
|
false // for now, we don't enforce validity
|
|
|
|
}
|
2022-11-22 05:16:33 -06:00
|
|
|
fn alignment_check_failed(
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_has: Align,
|
|
|
|
_required: Align,
|
|
|
|
_check: CheckAlignment,
|
|
|
|
) -> InterpResult<'tcx, ()> {
|
|
|
|
span_bug!(
|
|
|
|
ecx.cur_span(),
|
|
|
|
"`alignment_check_failed` called when no alignment check requested"
|
|
|
|
)
|
|
|
|
}
|
2022-08-09 07:23:16 -05:00
|
|
|
|
2020-12-07 08:27:46 -06:00
|
|
|
fn load_mir(
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_instance: ty::InstanceDef<'tcx>,
|
|
|
|
) -> InterpResult<'tcx, &'tcx Body<'tcx>> {
|
|
|
|
throw_machine_stop_str!("calling functions isn't supported in ConstProp")
|
|
|
|
}
|
|
|
|
|
2019-11-30 10:53:02 -06:00
|
|
|
fn find_mir_or_eval_fn(
|
2019-09-24 20:12:59 -05:00
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_instance: ty::Instance<'tcx>,
|
2022-07-07 11:01:36 -05:00
|
|
|
_abi: CallAbi,
|
2019-09-24 20:12:59 -05:00
|
|
|
_args: &[OpTy<'tcx>],
|
2022-04-16 08:27:54 -05:00
|
|
|
_destination: &PlaceTy<'tcx>,
|
|
|
|
_target: Option<BasicBlock>,
|
2022-10-10 13:50:49 -05:00
|
|
|
_unwind: UnwindAction,
|
2021-11-28 18:35:50 -06:00
|
|
|
) -> InterpResult<'tcx, Option<(&'mir Body<'tcx>, ty::Instance<'tcx>)>> {
|
2019-09-24 20:12:59 -05:00
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_intrinsic(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_instance: ty::Instance<'tcx>,
|
|
|
|
_args: &[OpTy<'tcx>],
|
2022-04-16 08:27:54 -05:00
|
|
|
_destination: &PlaceTy<'tcx>,
|
|
|
|
_target: Option<BasicBlock>,
|
2022-10-10 13:50:49 -05:00
|
|
|
_unwind: UnwindAction,
|
2019-09-24 20:12:59 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-03-23 04:58:43 -05:00
|
|
|
throw_machine_stop_str!("calling intrinsics isn't supported in ConstProp")
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
2019-11-29 02:59:52 -06:00
|
|
|
fn assert_panic(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2020-03-29 09:41:09 -05:00
|
|
|
_msg: &rustc_middle::mir::AssertMessage<'tcx>,
|
2022-10-08 17:47:59 -05:00
|
|
|
_unwind: rustc_middle::mir::UnwindAction,
|
2019-11-29 02:59:52 -06:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-03-19 03:07:43 -05:00
|
|
|
bug!("panics terminators are not evaluated in ConstProp")
|
2019-11-29 02:59:52 -06:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:12:59 -05:00
|
|
|
fn binary_ptr_op(
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_bin_op: BinOp,
|
2021-02-14 18:00:00 -06:00
|
|
|
_left: &ImmTy<'tcx>,
|
|
|
|
_right: &ImmTy<'tcx>,
|
2019-09-24 20:12:59 -05:00
|
|
|
) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
|
|
|
|
// We can't do this because aliasing of memory can differ between const eval and llvm
|
2020-03-23 04:58:43 -05:00
|
|
|
throw_machine_stop_str!("pointer arithmetic or comparisons aren't supported in ConstProp")
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
2020-06-26 04:02:43 -05:00
|
|
|
fn access_local_mut<'a>(
|
|
|
|
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
frame: usize,
|
|
|
|
local: Local,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> InterpResult<'tcx, &'a mut interpret::Operand<Self::Provenance>> {
|
2023-03-07 09:20:57 -06:00
|
|
|
assert_eq!(frame, 0);
|
|
|
|
match ecx.machine.can_const_prop[local] {
|
|
|
|
ConstPropMode::NoPropagation => {
|
|
|
|
throw_machine_stop_str!(
|
|
|
|
"tried to write to a local that is marked as not propagatable"
|
|
|
|
)
|
|
|
|
}
|
2023-03-13 12:43:14 -05:00
|
|
|
ConstPropMode::OnlyInsideOwnBlock => {
|
|
|
|
ecx.machine.written_only_inside_own_block_locals.insert(local);
|
|
|
|
}
|
|
|
|
ConstPropMode::FullConstProp => {}
|
2020-06-26 04:02:43 -05:00
|
|
|
}
|
|
|
|
ecx.machine.stack[frame].locals[local].access_mut()
|
|
|
|
}
|
|
|
|
|
2020-03-21 13:19:10 -05:00
|
|
|
fn before_access_global(
|
2022-04-05 12:31:51 -05:00
|
|
|
_tcx: TyCtxt<'tcx>,
|
2022-04-03 12:05:49 -05:00
|
|
|
_machine: &Self,
|
2020-03-21 14:44:39 -05:00
|
|
|
_alloc_id: AllocId,
|
2022-11-14 06:37:08 -06:00
|
|
|
alloc: ConstAllocation<'tcx>,
|
2020-03-31 08:25:12 -05:00
|
|
|
_static_def_id: Option<DefId>,
|
2020-03-21 13:19:10 -05:00
|
|
|
is_write: bool,
|
2019-09-24 20:12:59 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-03-21 13:19:10 -05:00
|
|
|
if is_write {
|
|
|
|
throw_machine_stop_str!("can't write to global");
|
|
|
|
}
|
2020-03-31 08:25:12 -05:00
|
|
|
// If the static allocation is mutable, then we can't const prop it as its content
|
|
|
|
// might be different at runtime.
|
2023-01-30 06:00:11 -06:00
|
|
|
if alloc.inner().mutability.is_mut() {
|
2020-03-31 08:25:12 -05:00
|
|
|
throw_machine_stop_str!("can't access mutable globals in ConstProp");
|
2019-09-24 21:08:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
2022-05-13 12:30:25 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn expose_ptr(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_ptr: Pointer<AllocId>,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
throw_machine_stop_str!("exposing pointers isn't supported in ConstProp")
|
|
|
|
}
|
|
|
|
|
2020-08-12 03:18:21 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn init_frame_extra(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
frame: Frame<'mir, 'tcx>,
|
|
|
|
) -> InterpResult<'tcx, Frame<'mir, 'tcx>> {
|
|
|
|
Ok(frame)
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:57:12 -05:00
|
|
|
#[inline(always)]
|
2021-12-06 02:48:37 -06:00
|
|
|
fn stack<'a>(
|
2020-04-16 11:57:12 -05:00
|
|
|
ecx: &'a InterpCx<'mir, 'tcx, Self>,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> &'a [Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>] {
|
2020-04-16 11:57:12 -05:00
|
|
|
&ecx.machine.stack
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2021-12-06 02:48:37 -06:00
|
|
|
fn stack_mut<'a>(
|
2020-04-16 11:57:12 -05:00
|
|
|
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
|
2022-07-18 17:47:31 -05:00
|
|
|
) -> &'a mut Vec<Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>> {
|
2020-04-16 11:57:12 -05:00
|
|
|
&mut ecx.machine.stack
|
|
|
|
}
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
2018-01-28 07:41:17 -06:00
|
|
|
/// Finds optimization opportunities on the MIR.
|
2019-06-11 14:03:44 -05:00
|
|
|
struct ConstPropagator<'mir, 'tcx> {
|
2020-03-16 17:12:42 -05:00
|
|
|
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-01-31 02:31:24 -06:00
|
|
|
param_env: ParamEnv<'tcx>,
|
2023-03-31 02:32:44 -05:00
|
|
|
local_decls: &'mir IndexSlice<Local, LocalDecl<'tcx>>,
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl<'tcx> LayoutOfHelpers<'tcx> for ConstPropagator<'_, 'tcx> {
|
2021-08-30 09:38:27 -05:00
|
|
|
type LayoutOfResult = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
|
2018-03-06 05:43:02 -06:00
|
|
|
|
2021-08-30 10:01:58 -05:00
|
|
|
#[inline]
|
|
|
|
fn handle_layout_err(&self, err: LayoutError<'tcx>, _: Span, _: Ty<'tcx>) -> LayoutError<'tcx> {
|
|
|
|
err
|
2018-03-06 05:43:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl HasDataLayout for ConstPropagator<'_, '_> {
|
2018-03-06 05:43:02 -06:00
|
|
|
#[inline]
|
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl<'tcx> ty::layout::HasTyCtxt<'tcx> for ConstPropagator<'_, 'tcx> {
|
2018-03-06 05:43:02 -06:00
|
|
|
#[inline]
|
2019-06-13 16:48:52 -05:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2018-03-06 05:43:02 -06:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl<'tcx> ty::layout::HasParamEnv<'tcx> for ConstPropagator<'_, 'tcx> {
|
2021-08-25 08:40:39 -05:00
|
|
|
#[inline]
|
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
|
|
|
self.param_env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
2018-01-28 07:41:17 -06:00
|
|
|
fn new(
|
2020-04-12 12:31:00 -05:00
|
|
|
body: &Body<'tcx>,
|
2019-06-13 19:20:14 -05:00
|
|
|
dummy_body: &'mir Body<'tcx>,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 14:03:44 -05:00
|
|
|
) -> ConstPropagator<'mir, 'tcx> {
|
2020-10-04 13:01:38 -05:00
|
|
|
let def_id = body.source.def_id();
|
2019-12-27 10:44:36 -06:00
|
|
|
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
|
2020-04-10 23:50:02 -05:00
|
|
|
let param_env = tcx.param_env_reveal_all_normalized(def_id);
|
2019-12-27 10:44:36 -06:00
|
|
|
|
2020-07-27 06:52:40 -05:00
|
|
|
let can_const_prop = CanConstProp::check(tcx, param_env, body);
|
2020-06-26 04:02:43 -05:00
|
|
|
let mut ecx = InterpCx::new(
|
|
|
|
tcx,
|
2022-04-21 05:26:43 -05:00
|
|
|
tcx.def_span(def_id),
|
2020-06-26 04:02:43 -05:00
|
|
|
param_env,
|
2023-03-07 09:20:57 -06:00
|
|
|
ConstPropMachine::new(can_const_prop),
|
2020-06-26 04:02:43 -05:00
|
|
|
);
|
2019-06-13 19:20:14 -05:00
|
|
|
|
2022-04-16 08:27:54 -05:00
|
|
|
let ret_layout = ecx
|
2022-08-03 00:02:46 -05:00
|
|
|
.layout_of(body.bound_return_ty().subst(tcx, substs))
|
2021-06-15 16:57:54 -05:00
|
|
|
.ok()
|
2022-04-16 08:27:54 -05:00
|
|
|
// Don't bother allocating memory for large values.
|
2022-07-02 21:02:12 -05:00
|
|
|
// I don't know how return types can seem to be unsized but this happens in the
|
|
|
|
// `type/type-unsatisfiable.rs` test.
|
|
|
|
.filter(|ret_layout| {
|
2022-11-13 05:14:59 -06:00
|
|
|
ret_layout.is_sized() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
|
2022-07-02 21:02:12 -05:00
|
|
|
})
|
2022-04-16 08:27:54 -05:00
|
|
|
.unwrap_or_else(|| ecx.layout_of(tcx.types.unit).unwrap());
|
|
|
|
|
|
|
|
let ret = ecx
|
|
|
|
.allocate(ret_layout, MemoryKind::Stack)
|
|
|
|
.expect("couldn't perform small allocation")
|
|
|
|
.into();
|
2019-11-07 18:13:03 -06:00
|
|
|
|
2019-06-13 19:20:14 -05:00
|
|
|
ecx.push_stack_frame(
|
2019-11-07 18:13:03 -06:00
|
|
|
Instance::new(def_id, substs),
|
2019-06-13 19:20:14 -05:00
|
|
|
dummy_body,
|
2022-04-16 08:27:54 -05:00
|
|
|
&ret,
|
2022-01-03 16:07:07 -06:00
|
|
|
StackPopCleanup::Root { cleanup: false },
|
2019-06-13 19:20:14 -05:00
|
|
|
)
|
|
|
|
.expect("failed to push initial stack frame");
|
2019-04-29 17:32:35 -05:00
|
|
|
|
2023-03-05 15:37:39 -06:00
|
|
|
ConstPropagator { ecx, tcx, param_env, local_decls: &dummy_body.local_decls }
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2020-05-06 12:28:48 -05:00
|
|
|
fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
|
2020-06-25 12:08:06 -05:00
|
|
|
let op = match self.ecx.eval_place_to_op(place, None) {
|
2022-08-07 09:36:42 -05:00
|
|
|
Ok(op) => {
|
|
|
|
if matches!(*op, interpret::Operand::Immediate(Immediate::Uninit)) {
|
|
|
|
// Make sure nobody accidentally uses this value.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
op
|
|
|
|
}
|
2020-06-25 12:08:06 -05:00
|
|
|
Err(e) => {
|
|
|
|
trace!("get_const failed: {}", e);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2019-11-07 18:13:03 -06:00
|
|
|
|
2020-04-19 07:25:07 -05:00
|
|
|
// Try to read the local as an immediate so that if it is representable as a scalar, we can
|
|
|
|
// handle it as such, but otherwise, just return the value as is.
|
2022-08-01 18:05:20 -05:00
|
|
|
Some(match self.ecx.read_immediate_raw(&op) {
|
2022-11-18 07:24:48 -06:00
|
|
|
Ok(Right(imm)) => imm.into(),
|
2020-04-19 07:25:07 -05:00
|
|
|
_ => op,
|
2020-06-25 12:08:06 -05:00
|
|
|
})
|
2019-06-12 20:06:07 -05:00
|
|
|
}
|
|
|
|
|
2020-04-23 11:25:28 -05:00
|
|
|
/// Remove `local` from the pool of `Locals`. Allows writing to them,
|
|
|
|
/// but not reading from them anymore.
|
|
|
|
fn remove_const(ecx: &mut InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>, local: Local) {
|
2023-03-07 08:32:16 -06:00
|
|
|
ecx.frame_mut().locals[local].value =
|
|
|
|
LocalValue::Live(interpret::Operand::Immediate(interpret::Immediate::Uninit));
|
2023-03-13 12:43:14 -05:00
|
|
|
ecx.machine.written_only_inside_own_block_locals.remove(&local);
|
2019-06-12 20:06:07 -05:00
|
|
|
}
|
|
|
|
|
2020-04-22 21:43:52 -05:00
|
|
|
/// Returns the value, if any, of evaluating `c`.
|
2022-03-08 11:20:31 -06:00
|
|
|
fn eval_constant(&mut self, c: &Constant<'tcx>) -> Option<OpTy<'tcx>> {
|
2019-11-22 14:26:09 -06:00
|
|
|
// FIXME we need to revisit this for #67176
|
2022-01-11 21:19:52 -06:00
|
|
|
if c.needs_subst() {
|
2019-11-22 14:26:09 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-11-15 05:06:20 -06:00
|
|
|
// No span, we don't want errors to be shown.
|
|
|
|
self.ecx.eval_mir_constant(&c.literal, None, None).ok()
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2020-04-22 21:43:52 -05:00
|
|
|
/// Returns the value, if any, of evaluating `place`.
|
2020-03-31 10:19:29 -05:00
|
|
|
fn eval_place(&mut self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
|
2019-05-25 09:59:05 -05:00
|
|
|
trace!("eval_place(place={:?})", place);
|
2023-02-10 12:33:41 -06:00
|
|
|
self.ecx.eval_place_to_op(place, None).ok()
|
2018-03-06 05:43:02 -06:00
|
|
|
}
|
|
|
|
|
2020-04-22 21:43:52 -05:00
|
|
|
/// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
|
|
|
|
/// or `eval_place`, depending on the variant of `Operand` used.
|
2022-03-08 11:20:31 -06:00
|
|
|
fn eval_operand(&mut self, op: &Operand<'tcx>) -> Option<OpTy<'tcx>> {
|
2018-01-28 07:41:17 -06:00
|
|
|
match *op {
|
2022-03-08 11:20:31 -06:00
|
|
|
Operand::Constant(ref c) => self.eval_constant(c),
|
2020-03-31 10:19:29 -05:00
|
|
|
Operand::Move(place) | Operand::Copy(place) => self.eval_place(place),
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:58:54 -05:00
|
|
|
fn propagate_operand(&mut self, operand: &mut Operand<'tcx>) {
|
2020-07-22 03:29:37 -05:00
|
|
|
match *operand {
|
|
|
|
Operand::Copy(l) | Operand::Move(l) => {
|
2022-03-01 04:43:12 -06:00
|
|
|
if let Some(value) = self.get_const(l) && self.should_const_prop(&value) {
|
|
|
|
// FIXME(felix91gr): this code only handles `Scalar` cases.
|
|
|
|
// For now, we're not handling `ScalarPair` cases because
|
|
|
|
// doing so here would require a lot of code duplication.
|
|
|
|
// We should hopefully generalize `Operand` handling into a fn,
|
|
|
|
// and use it to do const-prop here and everywhere else
|
|
|
|
// where it makes sense.
|
|
|
|
if let interpret::Operand::Immediate(interpret::Immediate::Scalar(
|
2022-08-01 18:05:20 -05:00
|
|
|
scalar,
|
2022-03-01 04:43:12 -06:00
|
|
|
)) = *value
|
|
|
|
{
|
2023-03-08 08:41:37 -06:00
|
|
|
*operand = self.operand_from_scalar(scalar, value.layout.ty);
|
2020-07-22 03:29:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 10:58:54 -05:00
|
|
|
Operand::Constant(_) => (),
|
2020-07-22 03:29:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:34:11 -06:00
|
|
|
fn check_rvalue(&mut self, rvalue: &Rvalue<'tcx>) -> Option<()> {
|
2019-10-13 12:48:26 -05: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.
|
2019-09-15 11:08:09 -05:00
|
|
|
match rvalue {
|
2020-01-12 20:19:44 -06:00
|
|
|
// Do not try creating references (#67862)
|
2020-06-22 07:03:18 -05:00
|
|
|
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.
|
|
|
|
Self::remove_const(&mut self.ecx, place.local);
|
2019-10-20 15:09:36 -05:00
|
|
|
|
2020-01-12 20:19:44 -06:00
|
|
|
return None;
|
2019-09-14 06:00:16 -05:00
|
|
|
}
|
2020-06-24 07:49:09 -05:00
|
|
|
Rvalue::ThreadLocalRef(def_id) => {
|
|
|
|
trace!("skipping ThreadLocalRef({:?})", def_id);
|
2019-09-15 11:08:09 -05:00
|
|
|
|
2020-06-24 07:49:09 -05:00
|
|
|
return None;
|
|
|
|
}
|
2023-02-24 20:32:52 -06:00
|
|
|
// Do not try creating references, nor any types with potentially-complex
|
|
|
|
// invariants. This avoids an issue where checking validity would do a
|
|
|
|
// bunch of work generating a nice message about the invariant violation,
|
|
|
|
// only to not show it to anyone (since this isn't the lint).
|
|
|
|
Rvalue::Cast(CastKind::Transmute, op, dst_ty) if !dst_ty.is_primitive() => {
|
|
|
|
trace!("skipping Transmute of {:?} to {:?}", op, dst_ty);
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
2020-06-24 07:49:09 -05:00
|
|
|
|
|
|
|
// There's no other checking to do at this time.
|
|
|
|
Rvalue::Aggregate(..)
|
|
|
|
| Rvalue::Use(..)
|
2022-06-13 08:37:41 -05:00
|
|
|
| Rvalue::CopyForDeref(..)
|
2020-06-24 07:49:09 -05:00
|
|
|
| Rvalue::Repeat(..)
|
|
|
|
| Rvalue::Len(..)
|
|
|
|
| Rvalue::Cast(..)
|
2021-09-06 12:33:23 -05:00
|
|
|
| Rvalue::ShallowInitBox(..)
|
2020-06-24 07:49:09 -05:00
|
|
|
| Rvalue::Discriminant(..)
|
2023-02-08 14:28:39 -06:00
|
|
|
| Rvalue::NullaryOp(..)
|
|
|
|
| Rvalue::UnaryOp(..)
|
|
|
|
| Rvalue::BinaryOp(..)
|
|
|
|
| Rvalue::CheckedBinaryOp(..) => {}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2019-09-14 06:00:16 -05:00
|
|
|
|
2020-04-29 04:35:45 -05:00
|
|
|
// FIXME we need to revisit this for #67176
|
2022-01-11 21:19:52 -06:00
|
|
|
if rvalue.needs_subst() {
|
2020-04-29 04:35:45 -05:00
|
|
|
return None;
|
|
|
|
}
|
2022-08-07 09:36:42 -05:00
|
|
|
if !rvalue
|
|
|
|
.ty(&self.ecx.frame().body.local_decls, *self.ecx.tcx)
|
2022-10-27 05:45:02 -05:00
|
|
|
.is_sized(*self.ecx.tcx, self.param_env)
|
2022-08-07 09:36:42 -05: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;
|
|
|
|
}
|
2020-04-29 04:35:45 -05:00
|
|
|
|
2023-03-07 10:34:11 -06:00
|
|
|
Some(())
|
2020-07-18 17:43:51 -05:00
|
|
|
}
|
|
|
|
|
2022-03-30 14:14:15 -05:00
|
|
|
// Attempt to use algebraic identities to eliminate constant expressions
|
2020-07-18 17:43:51 -05:00
|
|
|
fn eval_rvalue_with_identities(
|
|
|
|
&mut self,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
place: Place<'tcx>,
|
|
|
|
) -> Option<()> {
|
2023-02-10 12:33:41 -06:00
|
|
|
match rvalue {
|
2021-12-10 18:00:00 -06:00
|
|
|
Rvalue::BinaryOp(op, box (left, right))
|
|
|
|
| Rvalue::CheckedBinaryOp(op, box (left, right)) => {
|
2023-02-10 12:33:41 -06:00
|
|
|
let l = self.ecx.eval_operand(left, None).and_then(|x| self.ecx.read_immediate(&x));
|
2022-08-07 09:36:42 -05:00
|
|
|
let r =
|
2023-02-10 12:33:41 -06:00
|
|
|
self.ecx.eval_operand(right, None).and_then(|x| self.ecx.read_immediate(&x));
|
2021-12-10 18:00:00 -06:00
|
|
|
|
|
|
|
let const_arg = match (l, r) {
|
2022-08-07 09:36:42 -05:00
|
|
|
(Ok(x), Err(_)) | (Err(_), Ok(x)) => x, // exactly one side is known
|
2023-02-10 12:33:41 -06:00
|
|
|
(Err(_), Err(_)) => return None, // neither side is known
|
|
|
|
(Ok(_), Ok(_)) => return self.ecx.eval_rvalue_into_place(rvalue, place).ok(), // both sides are known
|
2021-12-10 18:00:00 -06:00
|
|
|
};
|
|
|
|
|
2022-07-06 10:44:35 -05:00
|
|
|
if !matches!(const_arg.layout.abi, abi::Abi::Scalar(..)) {
|
|
|
|
// We cannot handle Scalar Pair stuff.
|
2022-08-07 09:36:42 -05:00
|
|
|
// No point in calling `eval_rvalue_into_place`, since only one side is known
|
2023-02-10 12:33:41 -06:00
|
|
|
return None;
|
2022-07-06 10:44:35 -05:00
|
|
|
}
|
|
|
|
|
2023-02-10 12:33:41 -06:00
|
|
|
let arg_value = const_arg.to_scalar().to_bits(const_arg.layout.size).ok()?;
|
|
|
|
let dest = self.ecx.eval_place(place).ok()?;
|
2021-12-10 18:00:00 -06:00
|
|
|
|
|
|
|
match op {
|
2023-02-10 12:33:41 -06:00
|
|
|
BinOp::BitAnd if arg_value == 0 => {
|
|
|
|
self.ecx.write_immediate(*const_arg, &dest).ok()
|
|
|
|
}
|
2021-12-10 18:00:00 -06:00
|
|
|
BinOp::BitOr
|
|
|
|
if arg_value == const_arg.layout.size.truncate(u128::MAX)
|
|
|
|
|| (const_arg.layout.ty.is_bool() && arg_value == 1) =>
|
|
|
|
{
|
2023-02-10 12:33:41 -06:00
|
|
|
self.ecx.write_immediate(*const_arg, &dest).ok()
|
2021-12-10 18:00:00 -06:00
|
|
|
}
|
|
|
|
BinOp::Mul if const_arg.layout.ty.is_integral() && arg_value == 0 => {
|
|
|
|
if let Rvalue::CheckedBinaryOp(_, _) = rvalue {
|
|
|
|
let val = Immediate::ScalarPair(
|
2022-12-18 08:01:26 -06:00
|
|
|
const_arg.to_scalar(),
|
|
|
|
Scalar::from_bool(false),
|
2021-12-10 18:00:00 -06:00
|
|
|
);
|
2023-02-10 12:33:41 -06:00
|
|
|
self.ecx.write_immediate(val, &dest).ok()
|
2021-12-10 18:00:00 -06:00
|
|
|
} else {
|
2023-02-10 12:33:41 -06:00
|
|
|
self.ecx.write_immediate(*const_arg, &dest).ok()
|
2020-07-18 17:43:51 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-10 12:33:41 -06:00
|
|
|
_ => None,
|
2020-07-18 17:43:51 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-10 12:33:41 -06:00
|
|
|
_ => self.ecx.eval_rvalue_into_place(rvalue, place).ok(),
|
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2019-04-28 20:58:40 -05:00
|
|
|
|
2020-04-22 21:43:52 -05:00
|
|
|
/// Creates a new `Operand::Constant` from a `Scalar` value
|
2023-03-08 08:41:37 -06:00
|
|
|
fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>) -> Operand<'tcx> {
|
2019-04-28 20:58:40 -05:00
|
|
|
Operand::Constant(Box::new(Constant {
|
2023-03-08 08:41:37 -06:00
|
|
|
span: DUMMY_SP,
|
2019-04-28 20:58:40 -05:00
|
|
|
user_ty: None,
|
2022-02-16 03:56:01 -06:00
|
|
|
literal: ConstantKind::from_scalar(self.tcx, scalar, ty),
|
2019-04-28 20:58:40 -05:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:34:11 -06:00
|
|
|
fn replace_with_const(&mut self, place: Place<'tcx>, rval: &mut Rvalue<'tcx>) {
|
|
|
|
// This will return None if the above `const_prop` invocation only "wrote" a
|
|
|
|
// type whose creation requires no write. E.g. a generator whose initial state
|
|
|
|
// consists solely of uninitialized memory (so it doesn't capture any locals).
|
|
|
|
let Some(ref value) = self.get_const(place) else { return };
|
|
|
|
if !self.should_const_prop(value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace!("replacing {:?}={:?} with {:?}", place, rval, value);
|
|
|
|
|
2020-04-23 16:09:21 -05:00
|
|
|
if let Rvalue::Use(Operand::Constant(c)) = rval {
|
2021-03-08 10:18:03 -06:00
|
|
|
match c.literal {
|
2022-06-09 20:18:06 -05:00
|
|
|
ConstantKind::Ty(c) if matches!(c.kind(), ConstKind::Unevaluated(..)) => {}
|
2021-03-08 10:18:03 -06:00
|
|
|
_ => {
|
|
|
|
trace!("skipping replace of Rvalue::Use({:?} because it is already a const", c);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-23 16:09:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 07:49:46 -06:00
|
|
|
trace!("attempting to replace {:?} with {:?}", rval, value);
|
2020-03-05 16:31:39 -06:00
|
|
|
if let Err(e) = self.ecx.const_validate_operand(
|
2019-02-10 07:59:13 -06:00
|
|
|
value,
|
|
|
|
vec![],
|
|
|
|
// FIXME: is ref tracking too expensive?
|
2020-10-24 13:49:17 -05:00
|
|
|
// FIXME: what is the point of ref tracking if we do not even check the tracked refs?
|
2020-03-05 16:31:39 -06:00
|
|
|
&mut interpret::RefTracking::empty(),
|
2020-10-24 13:49:17 -05:00
|
|
|
CtfeValidationMode::Regular,
|
2019-02-10 07:59:13 -06:00
|
|
|
) {
|
2019-06-04 20:51:30 -05:00
|
|
|
trace!("validation error, attempt failed: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
2019-06-01 13:08:04 -05:00
|
|
|
|
2022-05-05 02:55:38 -05:00
|
|
|
// FIXME> figure out what to do when read_immediate_raw fails
|
2023-02-10 12:33:41 -06:00
|
|
|
let imm = self.ecx.read_immediate_raw(value).ok();
|
2019-04-28 20:58:40 -05:00
|
|
|
|
2022-11-18 07:24:48 -06:00
|
|
|
if let Some(Right(imm)) = imm {
|
2019-06-11 06:23:08 -05:00
|
|
|
match *imm {
|
2022-08-01 18:05:20 -05:00
|
|
|
interpret::Immediate::Scalar(scalar) => {
|
2023-03-08 08:41:37 -06:00
|
|
|
*rval = Rvalue::Use(self.operand_from_scalar(scalar, value.layout.ty));
|
2019-04-28 20:58:40 -05:00
|
|
|
}
|
2022-08-01 18:05:20 -05:00
|
|
|
Immediate::ScalarPair(..) => {
|
2020-08-14 11:01:14 -05:00
|
|
|
// Found a value represented as a pair. For now only do const-prop if the type
|
|
|
|
// of `rvalue` is also a tuple with two scalars.
|
|
|
|
// FIXME: enable the general case stated above ^.
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-24 21:13:38 -06:00
|
|
|
let ty = value.layout.ty;
|
2019-12-05 01:40:24 -06:00
|
|
|
// Only do it for tuples
|
2022-02-07 09:06:31 -06:00
|
|
|
if let ty::Tuple(types) = ty.kind() {
|
2019-12-05 01:40:24 -06:00
|
|
|
// Only do it if tuple is also a pair with two scalars
|
2022-02-07 09:06:31 -06:00
|
|
|
if let [ty1, ty2] = types[..] {
|
2023-02-10 12:33:41 -06:00
|
|
|
let ty_is_scalar = |ty| {
|
|
|
|
self.ecx.layout_of(ty).ok().map(|layout| layout.abi.is_scalar())
|
|
|
|
== Some(true)
|
|
|
|
};
|
|
|
|
let alloc = if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
|
|
|
|
let alloc = self
|
|
|
|
.ecx
|
|
|
|
.intern_with_temp_alloc(value.layout, |ecx, dest| {
|
|
|
|
ecx.write_immediate(*imm, dest)
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
Some(alloc)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(alloc) = alloc {
|
2020-08-18 06:57:32 -05:00
|
|
|
// Assign entire constant in a single statement.
|
2020-08-20 09:56:19 -05:00
|
|
|
// We can't use aggregates, as we run after the aggregate-lowering `MirPhase`.
|
2022-02-16 03:56:01 -06:00
|
|
|
let const_val = ConstValue::ByRef { alloc, offset: Size::ZERO };
|
|
|
|
let literal = ConstantKind::Val(const_val, ty);
|
2020-08-14 11:01:14 -05:00
|
|
|
*rval = Rvalue::Use(Operand::Constant(Box::new(Constant {
|
2023-03-05 15:37:39 -06:00
|
|
|
span: DUMMY_SP,
|
2020-08-14 11:01:14 -05:00
|
|
|
user_ty: None,
|
2022-02-16 03:56:01 -06:00
|
|
|
literal,
|
2020-08-14 11:01:14 -05:00
|
|
|
})));
|
2019-12-05 01:40:24 -06:00
|
|
|
}
|
|
|
|
}
|
2019-04-28 20:58:40 -05:00
|
|
|
}
|
|
|
|
}
|
2020-08-14 11:01:14 -05:00
|
|
|
// Scalars or scalar pairs that contain undef values are assumed to not have
|
|
|
|
// successfully evaluated and are thus not propagated.
|
2019-04-28 20:58:40 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-11 11:33:10 -05:00
|
|
|
|
2020-04-22 21:43:52 -05:00
|
|
|
/// Returns `true` if and only if this `op` should be const-propagated into.
|
2021-02-14 18:00:00 -06:00
|
|
|
fn should_const_prop(&mut self, op: &OpTy<'tcx>) -> bool {
|
2020-11-18 07:49:46 -06:00
|
|
|
if !self.tcx.consider_optimizing(|| format!("ConstantPropagation - OpTy: {:?}", op)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:00:00 -06:00
|
|
|
match **op {
|
2022-08-01 18:05:20 -05:00
|
|
|
interpret::Operand::Immediate(Immediate::Scalar(s)) => s.try_to_int().is_ok(),
|
|
|
|
interpret::Operand::Immediate(Immediate::ScalarPair(l, r)) => {
|
|
|
|
l.try_to_int().is_ok() && r.try_to_int().is_ok()
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2019-10-28 04:59:59 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
2019-05-11 11:33:10 -05:00
|
|
|
}
|
2023-03-07 10:34:11 -06:00
|
|
|
|
2023-03-13 12:43:14 -05:00
|
|
|
fn ensure_not_propagated(&self, local: Local) {
|
2023-03-07 10:34:11 -06:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
assert!(
|
|
|
|
self.get_const(local.into()).is_none()
|
|
|
|
|| self
|
|
|
|
.layout_of(self.local_decls[local].ty)
|
|
|
|
.map_or(true, |layout| layout.is_zst()),
|
|
|
|
"failed to remove values for `{local:?}`, value={:?}",
|
|
|
|
self.get_const(local.into()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2019-11-23 11:23:56 -06:00
|
|
|
/// The mode that `ConstProp` is allowed to run in for a given `Local`.
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2022-07-12 07:17:58 -05:00
|
|
|
pub enum ConstPropMode {
|
2019-11-23 11:23:56 -06:00
|
|
|
/// The `Local` can be propagated into and reads of this `Local` can also be propagated.
|
|
|
|
FullConstProp,
|
2020-04-23 11:25:28 -05:00
|
|
|
/// The `Local` can only be propagated into and from its own block.
|
|
|
|
OnlyInsideOwnBlock,
|
2020-06-22 07:03:18 -05:00
|
|
|
/// The `Local` cannot be part of propagation at all. Any statement
|
|
|
|
/// referencing it either for reading or writing will not get propagated.
|
|
|
|
NoPropagation,
|
2019-11-23 11:23:56 -06:00
|
|
|
}
|
|
|
|
|
2022-07-25 08:54:49 -05:00
|
|
|
pub struct CanConstProp {
|
2019-11-23 11:23:56 -06:00
|
|
|
can_const_prop: IndexVec<Local, ConstPropMode>,
|
2020-04-22 21:43:52 -05:00
|
|
|
// False at the beginning. Once set, no more assignments are allowed to that local.
|
2020-04-18 23:35:26 -05:00
|
|
|
found_assignment: BitSet<Local>,
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
impl CanConstProp {
|
2020-04-22 21:43:52 -05:00
|
|
|
/// Returns true if `local` can be propagated
|
2022-07-25 08:54:49 -05:00
|
|
|
pub fn check<'tcx>(
|
2020-07-27 06:52:40 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ParamEnv<'tcx>,
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
) -> IndexVec<Local, ConstPropMode> {
|
2018-01-28 07:41:17 -06:00
|
|
|
let mut cpv = CanConstProp {
|
2019-11-23 11:23:56 -06:00
|
|
|
can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
|
2020-04-18 23:35:26 -05:00
|
|
|
found_assignment: BitSet::new_empty(body.local_decls.len()),
|
2018-01-28 07:41:17 -06:00
|
|
|
};
|
2018-01-30 02:40:46 -06:00
|
|
|
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
|
2020-07-27 06:52:40 -05:00
|
|
|
let ty = body.local_decls[local].ty;
|
|
|
|
match tcx.layout_of(param_env.and(ty)) {
|
|
|
|
Ok(layout) if layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) => {}
|
|
|
|
// Either the layout fails to compute, then we can't use this local anyway
|
|
|
|
// or the local is too large, then we don't want to.
|
|
|
|
_ => {
|
|
|
|
*val = ConstPropMode::NoPropagation;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 09:04:53 -06:00
|
|
|
}
|
|
|
|
// Consider that arguments are assigned on entry.
|
|
|
|
for arg in body.args_iter() {
|
|
|
|
cpv.found_assignment.insert(arg);
|
2018-01-30 02:40:46 -06:00
|
|
|
}
|
2020-03-28 16:54:41 -05:00
|
|
|
cpv.visit_body(&body);
|
2018-01-28 07:41:17 -06:00
|
|
|
cpv.can_const_prop
|
|
|
|
}
|
2018-01-30 02:40:46 -06:00
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl Visitor<'_> for CanConstProp {
|
2022-07-01 09:21:21 -05:00
|
|
|
fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::mir::visit::PlaceContext::*;
|
2018-01-30 06:57:13 -06:00
|
|
|
match context {
|
2020-05-06 12:28:48 -05:00
|
|
|
// Projections are fine, because `&mut foo.x` will be caught by
|
|
|
|
// `MutatingUseContext::Borrow` elsewhere.
|
|
|
|
MutatingUse(MutatingUseContext::Projection)
|
2020-05-12 06:14:47 -05:00
|
|
|
// These are just stores, where the storing is not propagatable, but there may be later
|
|
|
|
// mutations of the same local via `Store`
|
|
|
|
| MutatingUse(MutatingUseContext::Call)
|
2021-08-29 19:23:33 -05:00
|
|
|
| MutatingUse(MutatingUseContext::AsmOutput)
|
2022-04-11 05:04:53 -05:00
|
|
|
| MutatingUse(MutatingUseContext::Deinit)
|
2020-05-12 06:14:47 -05:00
|
|
|
// Actual store that can possibly even propagate a value
|
2022-04-11 05:04:53 -05:00
|
|
|
| MutatingUse(MutatingUseContext::Store)
|
|
|
|
| MutatingUse(MutatingUseContext::SetDiscriminant) => {
|
2020-04-18 23:35:26 -05:00
|
|
|
if !self.found_assignment.insert(local) {
|
2020-05-06 12:28:48 -05:00
|
|
|
match &mut self.can_const_prop[local] {
|
|
|
|
// If the local can only get propagated in its own block, then we don't have
|
|
|
|
// to worry about multiple assignments, as we'll nuke the const state at the
|
|
|
|
// end of the block anyway, and inside the block we overwrite previous
|
|
|
|
// states as applicable.
|
|
|
|
ConstPropMode::OnlyInsideOwnBlock => {}
|
2020-06-22 07:03:18 -05:00
|
|
|
ConstPropMode::NoPropagation => {}
|
|
|
|
other @ ConstPropMode::FullConstProp => {
|
2020-05-06 12:28:48 -05:00
|
|
|
trace!(
|
2020-05-17 08:39:35 -05:00
|
|
|
"local {:?} can't be propagated because of multiple assignments. Previous state: {:?}",
|
|
|
|
local, other,
|
2020-05-06 12:28:48 -05:00
|
|
|
);
|
2020-05-17 08:39:35 -05:00
|
|
|
*other = ConstPropMode::OnlyInsideOwnBlock;
|
2020-05-06 12:28:48 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2018-01-30 06:57:13 -06:00
|
|
|
// Reading constants is allowed an arbitrary number of times
|
2018-10-26 06:22:45 -05:00
|
|
|
NonMutatingUse(NonMutatingUseContext::Copy)
|
|
|
|
| NonMutatingUse(NonMutatingUseContext::Move)
|
|
|
|
| NonMutatingUse(NonMutatingUseContext::Inspect)
|
|
|
|
| NonMutatingUse(NonMutatingUseContext::Projection)
|
|
|
|
| NonUse(_) => {}
|
2020-05-12 06:14:47 -05:00
|
|
|
|
|
|
|
// These could be propagated with a smarter analysis or just some careful thinking about
|
|
|
|
// whether they'd be fine right now.
|
2022-01-11 18:00:00 -06:00
|
|
|
MutatingUse(MutatingUseContext::Yield)
|
2020-05-12 06:14:47 -05:00
|
|
|
| MutatingUse(MutatingUseContext::Drop)
|
|
|
|
| MutatingUse(MutatingUseContext::Retag)
|
|
|
|
// These can't ever be propagated under any scheme, as we can't reason about indirect
|
|
|
|
// mutation.
|
|
|
|
| NonMutatingUse(NonMutatingUseContext::SharedBorrow)
|
|
|
|
| NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
|
|
|
|
| NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
|
|
|
|
| NonMutatingUse(NonMutatingUseContext::AddressOf)
|
|
|
|
| MutatingUse(MutatingUseContext::Borrow)
|
|
|
|
| MutatingUse(MutatingUseContext::AddressOf) => {
|
2019-05-25 09:59:05 -05:00
|
|
|
trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
|
2020-06-22 07:03:18 -05:00
|
|
|
self.can_const_prop[local] = ConstPropMode::NoPropagation;
|
2019-05-25 09:59:05 -05:00
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
|
2019-10-20 15:11:04 -05:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:53:00 -05:00
|
|
|
fn visit_body(&mut self, body: &mut Body<'tcx>) {
|
2022-08-02 22:06:16 -05:00
|
|
|
for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
|
2020-04-21 13:53:00 -05:00
|
|
|
self.visit_basic_block_data(bb, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 03:29:37 -05:00
|
|
|
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
|
2020-07-23 10:58:54 -05:00
|
|
|
self.super_operand(operand, location);
|
|
|
|
|
2021-03-03 15:56:00 -06:00
|
|
|
// Only const prop copies and moves on `mir_opt_level=3` as doing so
|
2020-09-23 08:54:21 -05:00
|
|
|
// currently slightly increases compile time in some cases.
|
2021-03-03 15:56:00 -06:00
|
|
|
if self.tcx.sess.mir_opt_level() >= 3 {
|
2020-07-23 10:58:54 -05:00
|
|
|
self.propagate_operand(operand)
|
2020-07-22 03:29:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 20:58:40 -05:00
|
|
|
fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location) {
|
2018-01-28 07:41:17 -06:00
|
|
|
trace!("visit_constant: {:?}", constant);
|
|
|
|
self.super_constant(constant, location);
|
2022-03-08 11:20:31 -06:00
|
|
|
self.eval_constant(constant);
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2023-03-07 10:34:11 -06:00
|
|
|
fn visit_assign(
|
|
|
|
&mut self,
|
|
|
|
place: &mut Place<'tcx>,
|
|
|
|
rvalue: &mut Rvalue<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
self.super_assign(place, rvalue, location);
|
|
|
|
|
|
|
|
let Some(()) = self.check_rvalue(rvalue) else { return };
|
|
|
|
|
|
|
|
match self.ecx.machine.can_const_prop[place.local] {
|
|
|
|
// Do nothing if the place is indirect.
|
|
|
|
_ if place.is_indirect() => {}
|
|
|
|
ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
|
|
|
|
ConstPropMode::OnlyInsideOwnBlock | ConstPropMode::FullConstProp => {
|
|
|
|
if let Some(()) = self.eval_rvalue_with_identities(rvalue, *place) {
|
|
|
|
self.replace_with_const(*place, rvalue);
|
2023-02-08 14:32:28 -06:00
|
|
|
} else {
|
|
|
|
// 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,
|
|
|
|
);
|
|
|
|
Self::remove_const(&mut self.ecx, place.local);
|
2020-05-17 08:39:35 -05:00
|
|
|
}
|
2023-02-08 14:32:28 -06:00
|
|
|
}
|
2023-03-07 10:34:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
|
|
|
|
trace!("visit_statement: {:?}", statement);
|
|
|
|
|
2023-03-07 11:12:42 -06:00
|
|
|
// We want to evaluate operands before any change to the assigned-to value,
|
|
|
|
// so we recurse first.
|
2023-03-07 10:34:11 -06:00
|
|
|
self.super_statement(statement, location);
|
|
|
|
|
|
|
|
match statement.kind {
|
2023-02-08 14:32:28 -06:00
|
|
|
StatementKind::SetDiscriminant { ref place, .. } => {
|
|
|
|
match self.ecx.machine.can_const_prop[place.local] {
|
2023-03-07 10:34:11 -06:00
|
|
|
// Do nothing if the place is indirect.
|
|
|
|
_ if place.is_indirect() => {}
|
|
|
|
ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
|
2023-02-08 14:32:28 -06:00
|
|
|
ConstPropMode::FullConstProp | ConstPropMode::OnlyInsideOwnBlock => {
|
2023-02-10 12:33:41 -06:00
|
|
|
if self.ecx.statement(statement).is_ok() {
|
2023-02-08 14:32:28 -06:00
|
|
|
trace!("propped discriminant into {:?}", place);
|
2019-09-13 07:40:43 -05:00
|
|
|
} else {
|
2023-02-08 14:32:28 -06:00
|
|
|
Self::remove_const(&mut self.ecx, place.local);
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 07:40:43 -05:00
|
|
|
}
|
2019-09-05 20:06:57 -05:00
|
|
|
}
|
2023-03-07 08:41:13 -06:00
|
|
|
StatementKind::StorageLive(local) => {
|
2023-02-08 14:32:28 -06:00
|
|
|
let frame = self.ecx.frame_mut();
|
2023-03-07 08:41:13 -06:00
|
|
|
frame.locals[local].value =
|
|
|
|
LocalValue::Live(interpret::Operand::Immediate(interpret::Immediate::Uninit));
|
|
|
|
}
|
|
|
|
StatementKind::StorageDead(local) => {
|
|
|
|
let frame = self.ecx.frame_mut();
|
|
|
|
frame.locals[local].value = LocalValue::Dead;
|
2023-02-08 14:32:28 -06:00
|
|
|
}
|
|
|
|
_ => {}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 20:58:40 -05:00
|
|
|
fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
|
2019-12-28 17:26:25 -06:00
|
|
|
self.super_terminator(terminator, location);
|
2023-02-08 14:30:44 -06:00
|
|
|
|
2019-05-11 09:55:34 -05:00
|
|
|
match &mut terminator.kind {
|
2022-03-08 11:20:31 -06:00
|
|
|
TerminatorKind::Assert { expected, ref mut cond, .. } => {
|
2023-02-08 14:28:39 -06:00
|
|
|
if let Some(ref value) = self.eval_operand(&cond)
|
|
|
|
&& let Ok(value_const) = self.ecx.read_scalar(&value)
|
|
|
|
&& self.should_const_prop(value)
|
|
|
|
{
|
|
|
|
trace!("assertion on {:?} should be {:?}", value, expected);
|
2023-03-08 08:41:37 -06:00
|
|
|
*cond = self.operand_from_scalar(value_const, self.tcx.types.bool);
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2019-05-11 09:55:34 -05:00
|
|
|
}
|
2020-07-22 03:29:37 -05:00
|
|
|
TerminatorKind::SwitchInt { ref mut discr, .. } => {
|
|
|
|
// FIXME: This is currently redundant with `visit_operand`, but sadly
|
2020-07-22 05:32:02 -05:00
|
|
|
// always visiting operands currently causes a perf regression in LLVM codegen, so
|
2021-03-03 15:30:42 -06:00
|
|
|
// `visit_operand` currently only runs for propagates places for `mir_opt_level=4`.
|
2020-07-23 10:58:54 -05:00
|
|
|
self.propagate_operand(discr)
|
2019-05-11 10:24:14 -05:00
|
|
|
}
|
2020-07-22 03:29:37 -05:00
|
|
|
// None of these have Operands to const-propagate.
|
2019-05-11 11:28:53 -05:00
|
|
|
TerminatorKind::Goto { .. }
|
|
|
|
| TerminatorKind::Resume
|
2022-10-30 20:01:24 -05:00
|
|
|
| TerminatorKind::Terminate
|
2019-05-11 11:28:53 -05:00
|
|
|
| TerminatorKind::Return
|
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
| TerminatorKind::Drop { .. }
|
|
|
|
| TerminatorKind::Yield { .. }
|
|
|
|
| TerminatorKind::GeneratorDrop
|
2020-06-02 02:15:24 -05:00
|
|
|
| TerminatorKind::FalseEdge { .. }
|
2020-02-14 12:17:50 -06:00
|
|
|
| TerminatorKind::FalseUnwind { .. }
|
|
|
|
| TerminatorKind::InlineAsm { .. } => {}
|
2020-07-22 03:29:37 -05:00
|
|
|
// Every argument in our function calls have already been propagated in `visit_operand`.
|
|
|
|
//
|
2020-09-23 08:54:21 -05:00
|
|
|
// NOTE: because LLVM codegen gives slight performance regressions with it, so this is
|
2021-03-04 08:15:49 -06:00
|
|
|
// gated on `mir_opt_level=3`.
|
2020-07-22 03:29:37 -05:00
|
|
|
TerminatorKind::Call { .. } => {}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2023-02-08 14:30:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
|
|
|
|
self.super_basic_block_data(block, data);
|
2020-06-26 04:02:43 -05:00
|
|
|
|
|
|
|
// We remove all Locals which are restricted in propagation to their containing blocks and
|
|
|
|
// which were modified in the current block.
|
2020-07-22 03:29:37 -05:00
|
|
|
// Take it out of the ecx so we can get a mutable reference to the ecx for `remove_const`.
|
2023-03-13 12:43:14 -05:00
|
|
|
let mut written_only_inside_own_block_locals =
|
|
|
|
std::mem::take(&mut self.ecx.machine.written_only_inside_own_block_locals);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
for local in written_only_inside_own_block_locals.drain() {
|
|
|
|
debug_assert_eq!(
|
|
|
|
self.ecx.machine.can_const_prop[local],
|
|
|
|
ConstPropMode::OnlyInsideOwnBlock
|
|
|
|
);
|
|
|
|
Self::remove_const(&mut self.ecx, local);
|
|
|
|
}
|
|
|
|
self.ecx.machine.written_only_inside_own_block_locals =
|
|
|
|
written_only_inside_own_block_locals;
|
|
|
|
|
2023-03-19 03:59:11 -05:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
for (local, &mode) in self.ecx.machine.can_const_prop.iter_enumerated() {
|
|
|
|
match mode {
|
|
|
|
ConstPropMode::FullConstProp => {}
|
|
|
|
ConstPropMode::NoPropagation | ConstPropMode::OnlyInsideOwnBlock => {
|
|
|
|
self.ensure_not_propagated(local);
|
|
|
|
}
|
2023-03-07 09:20:57 -06:00
|
|
|
}
|
2020-06-26 04:02:43 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|