2018-01-28 07:41:17 -06:00
|
|
|
//! Propagates constants for early reporting of statically known
|
|
|
|
//! assertion failures
|
|
|
|
|
2019-09-24 20:12:59 -05:00
|
|
|
use std::borrow::Cow;
|
2019-06-13 19:20:14 -05:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
2019-04-20 11:46:19 -05:00
|
|
|
use rustc::hir::def::DefKind;
|
2019-09-24 20:12:59 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2019-04-29 17:32:35 -05:00
|
|
|
use rustc::mir::{
|
2019-12-03 10:51:58 -06:00
|
|
|
AggregateKind, Constant, Location, Place, PlaceBase, Body, BodyAndCache, Operand, Local, UnOp,
|
|
|
|
Rvalue, StatementKind, Statement, LocalKind, TerminatorKind, Terminator, ClearCrossCrate,
|
|
|
|
SourceInfo, BinOp, SourceScope, SourceScopeData, LocalDecl, BasicBlock, ReadOnlyBodyAndCache,
|
2019-10-27 20:15:02 -05:00
|
|
|
read_only, RETURN_PLACE
|
2019-04-29 17:32:35 -05:00
|
|
|
};
|
2019-04-28 20:58:40 -05:00
|
|
|
use rustc::mir::visit::{
|
|
|
|
Visitor, PlaceContext, MutatingUseContext, MutVisitor, NonMutatingUseContext,
|
|
|
|
};
|
2019-09-03 20:56:15 -05:00
|
|
|
use rustc::mir::interpret::{Scalar, InterpResult, PanicInfo};
|
2019-04-29 17:32:35 -05:00
|
|
|
use rustc::ty::{self, Instance, ParamEnv, Ty, TyCtxt};
|
2019-09-24 21:08:22 -05:00
|
|
|
use syntax::ast::Mutability;
|
2019-04-28 20:58:40 -05:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2019-02-25 19:30:34 -06:00
|
|
|
use rustc::ty::subst::InternalSubsts;
|
2019-09-24 20:12:59 -05:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-09-26 00:38:33 -05:00
|
|
|
use rustc_index::vec::IndexVec;
|
2018-03-06 05:43:02 -06:00
|
|
|
use rustc::ty::layout::{
|
2019-11-13 18:19:25 -06:00
|
|
|
LayoutOf, TyLayout, LayoutError, HasTyCtxt, TargetDataLayout, HasDataLayout, Size,
|
2018-03-06 05:43:02 -06:00
|
|
|
};
|
2018-01-28 07:41:17 -06:00
|
|
|
|
2019-11-07 18:13:03 -06:00
|
|
|
use crate::rustc::ty::subst::Subst;
|
2019-06-01 13:08:04 -05:00
|
|
|
use crate::interpret::{
|
2019-06-27 04:36:01 -05:00
|
|
|
self, InterpCx, ScalarMaybeUndef, Immediate, OpTy,
|
2019-09-24 20:12:59 -05:00
|
|
|
StackPopCleanup, LocalValue, LocalState, AllocId, Frame,
|
|
|
|
Allocation, MemoryKind, ImmTy, Pointer, Memory, PlaceTy,
|
2019-11-22 19:48:18 -06:00
|
|
|
Operand as InterpOperand, intern_const_alloc_recursive,
|
2018-11-06 09:16:27 -06:00
|
|
|
};
|
2019-09-24 20:12:59 -05:00
|
|
|
use crate::const_eval::error_to_const_error;
|
2019-02-07 15:28:15 -06:00
|
|
|
use crate::transform::{MirPass, MirSource};
|
2018-09-18 04:01:13 -05:00
|
|
|
|
2019-11-13 18:19:25 -06:00
|
|
|
/// The maximum number of bytes that we'll allocate space for a return value.
|
|
|
|
const MAX_ALLOC_LIMIT: u64 = 1024;
|
|
|
|
|
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 {
|
2019-10-28 17:16:25 -05:00
|
|
|
fn run_pass(
|
2019-12-03 10:51:58 -06:00
|
|
|
&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>
|
2019-10-28 17:16:25 -05:00
|
|
|
) {
|
2018-03-06 05:43:02 -06:00
|
|
|
// will be evaluated by miri and produce its errors there
|
|
|
|
if source.promoted.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-31 07:30:59 -05:00
|
|
|
|
|
|
|
use rustc::hir::map::blocks::FnLikeNode;
|
2019-03-04 02:00:30 -06:00
|
|
|
let hir_id = tcx.hir().as_local_hir_id(source.def_id())
|
|
|
|
.expect("Non-local call to local provider is_const_fn");
|
2018-08-31 07:30:59 -05:00
|
|
|
|
2019-06-20 03:39:19 -05:00
|
|
|
let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some();
|
2019-04-20 11:46:19 -05:00
|
|
|
let is_assoc_const = match tcx.def_kind(source.def_id()) {
|
2019-05-19 03:26:08 -05:00
|
|
|
Some(DefKind::AssocConst) => true,
|
2018-08-31 07:30:59 -05:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
2019-02-03 04:51:07 -06:00
|
|
|
trace!("ConstProp skipped for {:?}", source.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
|
|
|
|
2019-09-12 21:03:20 -05:00
|
|
|
let is_generator = tcx.type_of(source.def_id()).is_generator();
|
|
|
|
// FIXME(welseywiser) const prop doesn't work on generators because of query cycles
|
|
|
|
// computing their layout.
|
|
|
|
if is_generator {
|
|
|
|
trace!("ConstProp skipped for generator {:?}", source.def_id());
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-03 04:51:07 -06:00
|
|
|
trace!("ConstProp starting for {:?}", source.def_id());
|
2018-01-28 07:41:17 -06:00
|
|
|
|
2019-06-13 19:20:14 -05:00
|
|
|
let dummy_body =
|
|
|
|
&Body::new(
|
|
|
|
body.basic_blocks().clone(),
|
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(),
|
|
|
|
tcx.def_span(source.def_id()),
|
|
|
|
Default::default(),
|
2019-11-25 06:58:40 -06:00
|
|
|
body.generator_kind,
|
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.
|
|
|
|
// That would require an uniform one-def no-mutation analysis
|
|
|
|
// and RPO (or recursing when needing the value of a local).
|
2019-06-13 19:20:14 -05:00
|
|
|
let mut optimization_finder = ConstPropagator::new(
|
2019-11-06 11:00:46 -06:00
|
|
|
read_only!(body),
|
2019-06-13 19:20:14 -05:00
|
|
|
dummy_body,
|
|
|
|
tcx,
|
|
|
|
source
|
|
|
|
);
|
2019-06-03 17:26:48 -05:00
|
|
|
optimization_finder.visit_body(body);
|
2018-01-28 07:41:17 -06:00
|
|
|
|
2019-02-03 04:51:07 -06:00
|
|
|
trace!("ConstProp done for {:?}", source.def_id());
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:12:59 -05:00
|
|
|
struct ConstPropMachine;
|
|
|
|
|
|
|
|
impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
|
2019-09-15 11:08:09 -05:00
|
|
|
type MemoryKinds = !;
|
2019-09-24 20:12:59 -05:00
|
|
|
type PointerTag = ();
|
|
|
|
type ExtraFnVal = !;
|
|
|
|
|
|
|
|
type FrameExtra = ();
|
|
|
|
type MemoryExtra = ();
|
|
|
|
type AllocExtra = ();
|
|
|
|
|
|
|
|
type MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
|
|
|
|
|
|
|
|
const STATIC_KIND: Option<!> = None;
|
|
|
|
|
|
|
|
const CHECK_ALIGN: bool = false;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
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>,
|
|
|
|
_args: &[OpTy<'tcx>],
|
2019-11-25 15:00:58 -06:00
|
|
|
_ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
|
2019-10-09 16:33:41 -05:00
|
|
|
_unwind: Option<BasicBlock>,
|
2019-09-24 20:12:59 -05:00
|
|
|
) -> InterpResult<'tcx, Option<&'mir Body<'tcx>>> {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_extra_fn(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
fn_val: !,
|
|
|
|
_args: &[OpTy<'tcx>],
|
2019-11-25 15:00:58 -06:00
|
|
|
_ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
|
|
|
|
_unwind: Option<BasicBlock>
|
2019-09-24 20:12:59 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
match fn_val {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_intrinsic(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2019-10-24 10:03:57 -05:00
|
|
|
_span: Span,
|
2019-09-24 20:12:59 -05:00
|
|
|
_instance: ty::Instance<'tcx>,
|
|
|
|
_args: &[OpTy<'tcx>],
|
2019-11-25 15:00:58 -06:00
|
|
|
_ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
|
2019-11-04 23:06:05 -06:00
|
|
|
_unwind: Option<BasicBlock>
|
2019-09-24 20:12:59 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-11-28 02:03:00 -06:00
|
|
|
throw_unsup!(ConstPropUnsupported("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>,
|
2019-11-29 03:13:40 -06:00
|
|
|
_span: Span,
|
2019-11-29 02:59:52 -06:00
|
|
|
_msg: &rustc::mir::interpret::AssertMessage<'tcx>,
|
|
|
|
_unwind: Option<rustc::mir::BasicBlock>,
|
|
|
|
) -> InterpResult<'tcx> {
|
2019-11-30 07:21:41 -06: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 ptr_to_int(
|
|
|
|
_mem: &Memory<'mir, 'tcx, Self>,
|
|
|
|
_ptr: Pointer,
|
|
|
|
) -> InterpResult<'tcx, u64> {
|
2019-11-28 02:03:00 -06:00
|
|
|
throw_unsup!(ConstPropUnsupported("ptr-to-int casts aren't supported in ConstProp"));
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn binary_ptr_op(
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_bin_op: BinOp,
|
|
|
|
_left: ImmTy<'tcx>,
|
|
|
|
_right: ImmTy<'tcx>,
|
|
|
|
) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
|
|
|
|
// We can't do this because aliasing of memory can differ between const eval and llvm
|
2019-11-28 02:03:00 -06:00
|
|
|
throw_unsup!(ConstPropUnsupported("pointer arithmetic or comparisons aren't supported \
|
|
|
|
in ConstProp"));
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn find_foreign_static(
|
|
|
|
_tcx: TyCtxt<'tcx>,
|
|
|
|
_def_id: DefId,
|
|
|
|
) -> InterpResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
|
|
|
|
throw_unsup!(ReadForeignStatic)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2019-11-29 12:42:37 -06:00
|
|
|
fn init_allocation_extra<'b>(
|
2019-09-24 20:12:59 -05:00
|
|
|
_memory_extra: &(),
|
|
|
|
_id: AllocId,
|
|
|
|
alloc: Cow<'b, Allocation>,
|
|
|
|
_kind: Option<MemoryKind<!>>,
|
2019-12-01 03:02:41 -06:00
|
|
|
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
|
2019-09-24 20:12:59 -05:00
|
|
|
// We do not use a tag so we can just cheaply forward the allocation
|
2019-12-01 03:02:41 -06:00
|
|
|
(alloc, ())
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn tag_static_base_pointer(
|
|
|
|
_memory_extra: &(),
|
|
|
|
_id: AllocId,
|
|
|
|
) -> Self::PointerTag {
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn box_alloc(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_dest: PlaceTy<'tcx>,
|
|
|
|
) -> InterpResult<'tcx> {
|
2019-11-28 02:03:00 -06:00
|
|
|
throw_unsup!(ConstPropUnsupported("can't const prop `box` keyword"));
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn access_local(
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
|
|
|
|
local: Local,
|
|
|
|
) -> InterpResult<'tcx, InterpOperand<Self::PointerTag>> {
|
|
|
|
let l = &frame.locals[local];
|
|
|
|
|
|
|
|
if l.value == LocalValue::Uninitialized {
|
2019-11-28 02:03:00 -06:00
|
|
|
throw_unsup!(ConstPropUnsupported("tried to access an uninitialized local"));
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
l.access()
|
|
|
|
}
|
|
|
|
|
2019-09-24 21:08:22 -05:00
|
|
|
fn before_access_static(
|
|
|
|
allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
|
2019-09-24 20:12:59 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-09-24 21:08:22 -05:00
|
|
|
// if the static allocation is mutable or if it has relocations (it may be legal to mutate
|
|
|
|
// the memory behind that in the future), then we can't const prop it
|
2019-12-16 10:28:40 -06:00
|
|
|
if allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0 {
|
2019-11-28 02:03:00 -06:00
|
|
|
throw_unsup!(ConstPropUnsupported("can't eval mutable statics in ConstProp"));
|
2019-09-24 21:08:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2019-09-24 20:12:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 17:32:35 -05:00
|
|
|
type Const<'tcx> = OpTy<'tcx>;
|
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> {
|
2019-09-24 20:12:59 -05:00
|
|
|
ecx: InterpCx<'mir, 'tcx, ConstPropMachine>,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-02-03 04:51:07 -06:00
|
|
|
source: MirSource<'tcx>,
|
2019-11-23 11:23:56 -06:00
|
|
|
can_const_prop: IndexVec<Local, ConstPropMode>,
|
2018-01-31 02:31:24 -06:00
|
|
|
param_env: ParamEnv<'tcx>,
|
2019-11-26 11:55:03 -06:00
|
|
|
// FIXME(eddyb) avoid cloning these two fields more than once,
|
|
|
|
// by accessing them through `ecx` instead.
|
2019-11-26 14:17:35 -06:00
|
|
|
source_scopes: IndexVec<SourceScope, SourceScopeData>,
|
2019-04-29 17:32:35 -05:00
|
|
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
2019-11-07 18:13:03 -06:00
|
|
|
ret: Option<OpTy<'tcx, ()>>,
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl<'mir, 'tcx> LayoutOf for ConstPropagator<'mir, 'tcx> {
|
2019-04-26 07:26:49 -05:00
|
|
|
type Ty = Ty<'tcx>;
|
2018-03-06 05:43:02 -06:00
|
|
|
type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
|
|
|
|
|
2019-04-26 07:26:49 -05:00
|
|
|
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
|
2018-03-06 05:43:02 -06:00
|
|
|
self.tcx.layout_of(self.param_env.and(ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl<'mir, 'tcx> HasDataLayout for ConstPropagator<'mir, 'tcx> {
|
2018-03-06 05:43:02 -06:00
|
|
|
#[inline]
|
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl<'mir, 'tcx> HasTyCtxt<'tcx> for ConstPropagator<'mir, '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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
2018-01-28 07:41:17 -06:00
|
|
|
fn new(
|
2019-12-03 10:51:58 -06:00
|
|
|
body: ReadOnlyBodyAndCache<'_, '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-02-03 04:51:07 -06:00
|
|
|
source: MirSource<'tcx>,
|
2019-06-11 14:03:44 -05:00
|
|
|
) -> ConstPropagator<'mir, 'tcx> {
|
2019-06-13 19:20:14 -05:00
|
|
|
let def_id = source.def_id();
|
|
|
|
let param_env = tcx.param_env(def_id);
|
|
|
|
let span = tcx.def_span(def_id);
|
2019-09-24 20:12:59 -05:00
|
|
|
let mut ecx = InterpCx::new(tcx.at(span), param_env, ConstPropMachine, ());
|
2019-06-03 17:26:48 -05:00
|
|
|
let can_const_prop = CanConstProp::check(body);
|
2019-06-13 19:20:14 -05:00
|
|
|
|
2019-11-07 18:13:03 -06:00
|
|
|
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
|
|
|
|
|
|
|
|
let ret =
|
|
|
|
ecx
|
|
|
|
.layout_of(body.return_ty().subst(tcx, substs))
|
|
|
|
.ok()
|
2019-11-13 18:19:25 -06:00
|
|
|
// Don't bother allocating memory for ZST types which have no values
|
|
|
|
// or for large values.
|
|
|
|
.filter(|ret_layout| !ret_layout.is_zst() &&
|
|
|
|
ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT))
|
2019-11-07 18:13:03 -06:00
|
|
|
.map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack));
|
|
|
|
|
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
|
|
|
span,
|
|
|
|
dummy_body,
|
2019-11-07 18:13:03 -06:00
|
|
|
ret.map(Into::into),
|
2019-06-13 19:20:14 -05:00
|
|
|
StackPopCleanup::None {
|
|
|
|
cleanup: false,
|
|
|
|
},
|
|
|
|
).expect("failed to push initial stack frame");
|
2019-04-29 17:32:35 -05:00
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
ConstPropagator {
|
2018-04-26 02:18:19 -05:00
|
|
|
ecx,
|
2018-01-28 07:41:17 -06:00
|
|
|
tcx,
|
|
|
|
source,
|
2018-01-31 02:31:24 -06:00
|
|
|
param_env,
|
2019-04-29 17:32:35 -05:00
|
|
|
can_const_prop,
|
2019-11-26 11:55:03 -06:00
|
|
|
// FIXME(eddyb) avoid cloning these two fields more than once,
|
|
|
|
// by accessing them through `ecx` instead.
|
2019-11-26 14:17:35 -06:00
|
|
|
source_scopes: body.source_scopes.clone(),
|
2019-05-17 16:55:04 -05:00
|
|
|
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
|
2019-06-03 17:26:48 -05:00
|
|
|
local_decls: body.local_decls.clone(),
|
2019-11-07 18:13:03 -06:00
|
|
|
ret: ret.map(Into::into),
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 20:06:07 -05:00
|
|
|
fn get_const(&self, local: Local) -> Option<Const<'tcx>> {
|
2019-11-07 18:13:03 -06:00
|
|
|
if local == RETURN_PLACE {
|
|
|
|
// Try to read the return place 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.
|
|
|
|
return match self.ret.map(|ret| self.ecx.try_read_immediate(ret)) {
|
|
|
|
Some(Ok(Ok(imm))) => Some(imm.into()),
|
|
|
|
_ => self.ret,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:20:14 -05:00
|
|
|
self.ecx.access_local(self.ecx.frame(), local, None).ok()
|
2019-06-12 20:06:07 -05:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:36:04 -05:00
|
|
|
fn remove_const(&mut self, local: Local) {
|
|
|
|
self.ecx.frame_mut().locals[local] = LocalState {
|
|
|
|
value: LocalValue::Uninitialized,
|
|
|
|
layout: Cell::new(None),
|
|
|
|
};
|
2019-06-12 20:06:07 -05:00
|
|
|
}
|
|
|
|
|
2018-04-26 02:18:19 -05:00
|
|
|
fn use_ecx<F, T>(
|
|
|
|
&mut self,
|
2018-06-02 16:38:57 -05:00
|
|
|
source_info: SourceInfo,
|
2018-04-26 02:18:19 -05:00
|
|
|
f: F
|
|
|
|
) -> Option<T>
|
|
|
|
where
|
2019-06-07 11:56:27 -05:00
|
|
|
F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
|
2018-04-26 02:18:19 -05:00
|
|
|
{
|
2018-06-02 16:38:57 -05:00
|
|
|
self.ecx.tcx.span = source_info.span;
|
2019-11-26 11:55:03 -06:00
|
|
|
// FIXME(eddyb) move this to the `Panic(_)` error case, so that
|
2019-11-26 14:17:35 -06:00
|
|
|
// `f(self)` is always called, and that the only difference when the
|
|
|
|
// scope's `local_data` is missing, is that the lint isn't emitted.
|
|
|
|
let lint_root = match &self.source_scopes[source_info.scope].local_data {
|
2019-11-26 11:55:03 -06:00
|
|
|
ClearCrossCrate::Set(data) => data.lint_root,
|
2018-06-02 16:38:57 -05:00
|
|
|
ClearCrossCrate::Clear => return None,
|
|
|
|
};
|
2018-04-26 02:18:19 -05:00
|
|
|
let r = match f(self) {
|
|
|
|
Ok(val) => Some(val),
|
2018-07-25 06:05:05 -05:00
|
|
|
Err(error) => {
|
2019-11-28 02:03:00 -06:00
|
|
|
use rustc::mir::interpret::{
|
|
|
|
UnsupportedOpInfo,
|
|
|
|
UndefinedBehaviorInfo,
|
|
|
|
InterpError::*
|
|
|
|
};
|
2019-08-27 15:18:25 -05:00
|
|
|
match error.kind {
|
2019-12-01 04:42:19 -06:00
|
|
|
MachineStop(_) => bug!("ConstProp does not stop"),
|
2019-11-28 02:03:00 -06:00
|
|
|
|
|
|
|
// Some error shouldn't come up because creating them causes
|
|
|
|
// an allocation, which we should avoid. When that happens,
|
|
|
|
// dedicated error variants should be introduced instead.
|
|
|
|
// Only test this in debug builds though to avoid disruptions.
|
|
|
|
Unsupported(UnsupportedOpInfo::Unsupported(_))
|
|
|
|
| Unsupported(UnsupportedOpInfo::ValidationFailure(_))
|
|
|
|
| UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
|
|
|
|
| UndefinedBehavior(UndefinedBehaviorInfo::UbExperimental(_))
|
|
|
|
if cfg!(debug_assertions) => {
|
|
|
|
bug!("const-prop encountered allocating error: {:?}", error.kind);
|
|
|
|
}
|
|
|
|
|
2019-08-01 01:43:49 -05:00
|
|
|
Unsupported(_)
|
2019-08-02 16:24:27 -05:00
|
|
|
| UndefinedBehavior(_)
|
2019-08-01 01:43:49 -05:00
|
|
|
| InvalidProgram(_)
|
|
|
|
| ResourceExhaustion(_) => {
|
|
|
|
// Ignore these errors.
|
|
|
|
}
|
2019-07-31 23:19:01 -05:00
|
|
|
Panic(_) => {
|
2019-08-27 15:18:25 -05:00
|
|
|
let diagnostic = error_to_const_error(&self.ecx, error);
|
2018-07-25 06:05:05 -05:00
|
|
|
diagnostic.report_as_lint(
|
2018-07-18 07:23:07 -05:00
|
|
|
self.ecx.tcx,
|
|
|
|
"this expression will panic at runtime",
|
|
|
|
lint_root,
|
2019-03-10 19:28:52 -05:00
|
|
|
None,
|
2018-07-18 07:23:07 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-04-26 02:18:19 -05:00
|
|
|
None
|
|
|
|
},
|
|
|
|
};
|
|
|
|
self.ecx.tcx.span = DUMMY_SP;
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-06-02 16:38:57 -05:00
|
|
|
fn eval_constant(
|
|
|
|
&mut self,
|
|
|
|
c: &Constant<'tcx>,
|
|
|
|
) -> Option<Const<'tcx>> {
|
2019-04-29 17:32:35 -05:00
|
|
|
self.ecx.tcx.span = c.span;
|
2019-04-03 08:29:31 -05:00
|
|
|
match self.ecx.eval_const_to_op(c.literal, None) {
|
2018-08-13 09:14:22 -05:00
|
|
|
Ok(op) => {
|
2019-04-29 17:32:35 -05:00
|
|
|
Some(op)
|
2018-06-04 11:32:06 -05:00
|
|
|
},
|
2018-07-21 18:01:07 -05:00
|
|
|
Err(error) => {
|
2018-09-18 04:01:13 -05:00
|
|
|
let err = error_to_const_error(&self.ecx, error);
|
2018-08-26 08:19:34 -05:00
|
|
|
err.report_as_error(self.ecx.tcx, "erroneous constant used");
|
2018-07-21 18:01:07 -05:00
|
|
|
None
|
|
|
|
},
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 12:54:50 -05:00
|
|
|
fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
|
2019-05-25 09:59:05 -05:00
|
|
|
trace!("eval_place(place={:?})", place);
|
2019-09-03 20:56:15 -05:00
|
|
|
self.use_ecx(source_info, |this| {
|
|
|
|
this.ecx.eval_place_to_op(place, None)
|
|
|
|
})
|
2018-03-06 05:43:02 -06:00
|
|
|
}
|
|
|
|
|
2018-06-02 16:38:57 -05:00
|
|
|
fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
|
2018-01-28 07:41:17 -06:00
|
|
|
match *op {
|
2019-04-29 17:32:35 -05:00
|
|
|
Operand::Constant(ref c) => self.eval_constant(c),
|
2018-06-05 12:54:50 -05:00
|
|
|
| Operand::Move(ref place)
|
|
|
|
| Operand::Copy(ref place) => self.eval_place(place, source_info),
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn const_prop(
|
|
|
|
&mut self,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
2018-06-04 11:32:06 -05:00
|
|
|
place_layout: TyLayout<'tcx>,
|
2018-01-29 08:12:45 -06:00
|
|
|
source_info: SourceInfo,
|
2019-09-05 20:06:57 -05:00
|
|
|
place: &Place<'tcx>,
|
2019-10-12 07:21:51 -05:00
|
|
|
) -> Option<()> {
|
2018-01-29 08:12:45 -06:00
|
|
|
let span = source_info.span;
|
2019-09-14 06:00:16 -05:00
|
|
|
|
2019-11-13 19:37:33 -06:00
|
|
|
// #66397: Don't try to eval into large places as that can cause an OOM
|
|
|
|
if place_layout.size >= Size::from_bytes(MAX_ALLOC_LIMIT) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-10-13 12:48:26 -05:00
|
|
|
let overflow_check = self.tcx.sess.overflow_checks();
|
|
|
|
|
|
|
|
// 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 {
|
2019-10-13 12:48:26 -05:00
|
|
|
// Additional checking: if overflow checks are disabled (which is usually the case in
|
|
|
|
// release mode), then we need to do additional checking here to give lints to the user
|
|
|
|
// if an overflow would occur.
|
|
|
|
Rvalue::UnaryOp(UnOp::Neg, arg) if !overflow_check => {
|
2019-09-15 11:08:09 -05:00
|
|
|
trace!("checking UnaryOp(op = Neg, arg = {:?})", arg);
|
|
|
|
|
|
|
|
self.use_ecx(source_info, |this| {
|
2019-10-13 12:48:26 -05:00
|
|
|
let ty = arg.ty(&this.local_decls, this.tcx);
|
|
|
|
|
|
|
|
if ty.is_integral() {
|
|
|
|
let arg = this.ecx.eval_operand(arg, None)?;
|
|
|
|
let prim = this.ecx.read_immediate(arg)?;
|
|
|
|
// Need to do overflow check here: For actual CTFE, MIR
|
|
|
|
// generation emits code that does this before calling the op.
|
|
|
|
if prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
|
|
|
|
throw_panic!(OverflowNeg)
|
2019-09-01 11:05:38 -05:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 21:28:02 -05:00
|
|
|
|
2019-09-15 11:08:09 -05:00
|
|
|
Ok(())
|
|
|
|
})?;
|
2019-09-14 06:00:16 -05:00
|
|
|
}
|
2019-09-15 11:08:09 -05:00
|
|
|
|
2019-10-13 12:48:26 -05:00
|
|
|
// Additional checking: check for overflows on integer binary operations and report
|
|
|
|
// them to the user as lints.
|
2019-09-15 11:08:09 -05:00
|
|
|
Rvalue::BinaryOp(op, left, right) => {
|
|
|
|
trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
|
|
|
|
|
|
|
|
let r = self.use_ecx(source_info, |this| {
|
|
|
|
this.ecx.read_immediate(this.ecx.eval_operand(right, None)?)
|
|
|
|
})?;
|
|
|
|
if *op == BinOp::Shr || *op == BinOp::Shl {
|
|
|
|
let left_bits = place_layout.size.bits();
|
|
|
|
let right_size = r.layout.size;
|
|
|
|
let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
|
2019-11-25 07:22:55 -06:00
|
|
|
if r_bits.map_or(false, |b| b >= left_bits as u128) {
|
2019-11-26 14:17:35 -06:00
|
|
|
let lint_root = match &self.source_scopes[source_info.scope].local_data {
|
2019-11-26 11:55:03 -06:00
|
|
|
ClearCrossCrate::Set(data) => data.lint_root,
|
2019-09-15 11:08:09 -05:00
|
|
|
ClearCrossCrate::Clear => return None,
|
|
|
|
};
|
|
|
|
let dir = if *op == BinOp::Shr {
|
|
|
|
"right"
|
|
|
|
} else {
|
|
|
|
"left"
|
|
|
|
};
|
|
|
|
self.tcx.lint_hir(
|
|
|
|
::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
|
2019-11-26 11:55:03 -06:00
|
|
|
lint_root,
|
2019-09-15 11:08:09 -05:00
|
|
|
span,
|
|
|
|
&format!("attempt to shift {} with overflow", dir));
|
|
|
|
return None;
|
|
|
|
}
|
2019-09-14 06:00:16 -05:00
|
|
|
}
|
2019-09-15 11:08:09 -05:00
|
|
|
|
2019-10-13 12:48:26 -05:00
|
|
|
// If overflow checking is enabled (like in debug mode by default),
|
|
|
|
// then we'll already catch overflow when we evaluate the `Assert` statement
|
|
|
|
// in MIR. However, if overflow checking is disabled, then there won't be any
|
|
|
|
// `Assert` statement and so we have to do additional checking here.
|
|
|
|
if !overflow_check {
|
|
|
|
self.use_ecx(source_info, |this| {
|
|
|
|
let l = this.ecx.read_immediate(this.ecx.eval_operand(left, None)?)?;
|
|
|
|
let (_, overflow, _ty) = this.ecx.overflowing_binary_op(*op, l, r)?;
|
|
|
|
|
|
|
|
if overflow {
|
|
|
|
let err = err_panic!(Overflow(*op)).into();
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
}
|
2019-09-15 11:08:09 -05:00
|
|
|
}
|
2019-09-09 21:28:02 -05:00
|
|
|
|
2019-10-19 04:46:56 -05:00
|
|
|
// Work around: avoid ICE in miri. FIXME(wesleywiser)
|
|
|
|
// The Miri engine ICEs when taking a reference to an uninitialized unsized
|
|
|
|
// local. There's nothing it can do here: taking a reference needs an allocation
|
|
|
|
// which needs to know the size. Normally that's okay as during execution
|
|
|
|
// (e.g. for CTFE) it can never happen. But here in const_prop
|
2019-10-19 11:16:22 -05:00
|
|
|
// unknown data is uninitialized, so if e.g. a function argument is unsized
|
2019-10-19 04:46:56 -05:00
|
|
|
// and has a reference taken, we get an ICE.
|
2019-10-20 15:09:36 -05:00
|
|
|
Rvalue::Ref(_, _, place_ref) => {
|
|
|
|
trace!("checking Ref({:?})", place_ref);
|
|
|
|
|
|
|
|
if let Some(local) = place_ref.as_local() {
|
|
|
|
let alive =
|
|
|
|
if let LocalValue::Live(_) = self.ecx.frame().locals[local].value {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
if !alive {
|
|
|
|
trace!("skipping Ref({:?}) to uninitialized local", place);
|
|
|
|
return None;
|
|
|
|
}
|
2019-09-14 06:00:16 -05:00
|
|
|
}
|
|
|
|
}
|
2019-09-15 11:08:09 -05:00
|
|
|
|
|
|
|
_ => { }
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2019-09-14 06:00:16 -05:00
|
|
|
|
|
|
|
self.use_ecx(source_info, |this| {
|
|
|
|
trace!("calling eval_rvalue_into_place(rvalue = {:?}, place = {:?})", rvalue, place);
|
|
|
|
this.ecx.eval_rvalue_into_place(rvalue, place)?;
|
2019-10-12 07:21:51 -05:00
|
|
|
Ok(())
|
2019-09-14 06:00:16 -05:00
|
|
|
})
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2019-04-28 20:58:40 -05:00
|
|
|
|
|
|
|
fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
|
|
|
|
Operand::Constant(Box::new(
|
|
|
|
Constant {
|
|
|
|
span,
|
|
|
|
user_ty: None,
|
2019-05-24 14:47:39 -05:00
|
|
|
literal: self.tcx.mk_const(*ty::Const::from_scalar(
|
|
|
|
self.tcx,
|
2019-04-28 20:58:40 -05:00
|
|
|
scalar,
|
|
|
|
ty,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-06-01 13:08:04 -05:00
|
|
|
fn replace_with_const(
|
|
|
|
&mut self,
|
|
|
|
rval: &mut Rvalue<'tcx>,
|
|
|
|
value: Const<'tcx>,
|
|
|
|
source_info: SourceInfo,
|
2019-06-04 05:30:36 -05:00
|
|
|
) {
|
2019-05-25 09:59:05 -05:00
|
|
|
trace!("attepting to replace {:?} with {:?}", rval, value);
|
2019-02-10 07:59:13 -06:00
|
|
|
if let Err(e) = self.ecx.validate_operand(
|
|
|
|
value,
|
|
|
|
vec![],
|
|
|
|
// FIXME: is ref tracking too expensive?
|
|
|
|
Some(&mut interpret::RefTracking::empty()),
|
|
|
|
) {
|
2019-06-04 20:51:30 -05:00
|
|
|
trace!("validation error, attempt failed: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
2019-06-01 13:08:04 -05:00
|
|
|
|
|
|
|
// FIXME> figure out what tho do when try_read_immediate fails
|
|
|
|
let imm = self.use_ecx(source_info, |this| {
|
|
|
|
this.ecx.try_read_immediate(value)
|
|
|
|
});
|
2019-04-28 20:58:40 -05:00
|
|
|
|
2019-06-01 13:08:04 -05:00
|
|
|
if let Some(Ok(imm)) = imm {
|
2019-06-11 06:23:08 -05:00
|
|
|
match *imm {
|
2019-04-28 20:58:40 -05:00
|
|
|
interpret::Immediate::Scalar(ScalarMaybeUndef::Scalar(scalar)) => {
|
2019-06-01 13:08:04 -05:00
|
|
|
*rval = Rvalue::Use(
|
|
|
|
self.operand_from_scalar(scalar, value.layout.ty, source_info.span));
|
2019-04-28 20:58:40 -05:00
|
|
|
},
|
|
|
|
Immediate::ScalarPair(
|
|
|
|
ScalarMaybeUndef::Scalar(one),
|
|
|
|
ScalarMaybeUndef::Scalar(two)
|
|
|
|
) => {
|
2019-12-05 01:40:24 -06:00
|
|
|
// Found a value represented as a pair. For now only do cont-prop if type of
|
|
|
|
// Rvalue is also a pair with two scalars. The more general case is more
|
|
|
|
// complicated to implement so we'll do it later.
|
2019-09-16 13:08:35 -05:00
|
|
|
let ty = &value.layout.ty.kind;
|
2019-12-05 01:40:24 -06:00
|
|
|
// Only do it for tuples
|
2019-04-28 20:58:40 -05:00
|
|
|
if let ty::Tuple(substs) = ty {
|
2019-12-05 01:40:24 -06:00
|
|
|
// Only do it if tuple is also a pair with two scalars
|
|
|
|
if substs.len() == 2 {
|
|
|
|
let opt_ty1_ty2 = self.use_ecx(source_info, |this| {
|
|
|
|
let ty1 = substs[0].expect_ty();
|
|
|
|
let ty2 = substs[1].expect_ty();
|
|
|
|
let ty_is_scalar = |ty| {
|
|
|
|
this.ecx
|
|
|
|
.layout_of(ty)
|
|
|
|
.ok()
|
|
|
|
.map(|ty| ty.details.abi.is_scalar())
|
|
|
|
== Some(true)
|
|
|
|
};
|
|
|
|
if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
|
|
|
|
Ok(Some((ty1, ty2)))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(Some((ty1, ty2))) = opt_ty1_ty2 {
|
|
|
|
*rval = Rvalue::Aggregate(
|
|
|
|
Box::new(AggregateKind::Tuple),
|
|
|
|
vec![
|
|
|
|
self.operand_from_scalar(
|
|
|
|
one, ty1, source_info.span
|
|
|
|
),
|
|
|
|
self.operand_from_scalar(
|
|
|
|
two, ty2, source_info.span
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-04-28 20:58:40 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-11 11:33:10 -05:00
|
|
|
|
2019-10-28 04:59:59 -05:00
|
|
|
fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
|
2019-11-24 18:09:58 -06:00
|
|
|
let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
|
|
|
|
|
|
|
|
if mir_opt_level == 0 {
|
2019-10-28 04:59:59 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
match *op {
|
|
|
|
interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Scalar(s))) =>
|
|
|
|
s.is_bits(),
|
|
|
|
interpret::Operand::Immediate(Immediate::ScalarPair(ScalarMaybeUndef::Scalar(l),
|
|
|
|
ScalarMaybeUndef::Scalar(r))) =>
|
|
|
|
l.is_bits() && r.is_bits(),
|
2019-11-24 18:09:58 -06:00
|
|
|
interpret::Operand::Indirect(_) if mir_opt_level >= 2 => {
|
2019-11-22 19:48:18 -06:00
|
|
|
intern_const_alloc_recursive(
|
|
|
|
&mut self.ecx,
|
|
|
|
None,
|
|
|
|
op.assert_mem_place()
|
|
|
|
).expect("failed to intern alloc");
|
2019-11-24 18:09:58 -06:00
|
|
|
true
|
|
|
|
},
|
2019-10-28 04:59:59 -05:00
|
|
|
_ => false
|
|
|
|
}
|
2019-05-11 11:33:10 -05:00
|
|
|
}
|
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)]
|
|
|
|
enum ConstPropMode {
|
|
|
|
/// The `Local` can be propagated into and reads of this `Local` can also be propagated.
|
|
|
|
FullConstProp,
|
|
|
|
/// The `Local` can be propagated into but reads cannot be propagated.
|
|
|
|
OnlyPropagateInto,
|
|
|
|
/// No propagation is allowed at all.
|
|
|
|
NoPropagation,
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
struct CanConstProp {
|
2019-11-23 11:23:56 -06:00
|
|
|
can_const_prop: IndexVec<Local, ConstPropMode>,
|
2018-01-28 07:41:17 -06:00
|
|
|
// false at the beginning, once set, there are not allowed to be any more assignments
|
2018-01-30 02:40:46 -06:00
|
|
|
found_assignment: IndexVec<Local, bool>,
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
impl CanConstProp {
|
2018-01-28 07:41:17 -06:00
|
|
|
/// returns true if `local` can be propagated
|
2019-11-23 11:23:56 -06:00
|
|
|
fn check(body: ReadOnlyBodyAndCache<'_, '_>) -> 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),
|
2019-06-03 17:26:48 -05:00
|
|
|
found_assignment: IndexVec::from_elem(false, &body.local_decls),
|
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() {
|
2018-01-30 09:38:14 -06:00
|
|
|
// cannot use args at all
|
|
|
|
// cannot use locals because if x < y { y - x } else { x - y } would
|
|
|
|
// lint for x != y
|
|
|
|
// FIXME(oli-obk): lint variables until they are used in a condition
|
|
|
|
// FIXME(oli-obk): lint if return value is constant
|
2019-11-07 18:13:03 -06:00
|
|
|
let local_kind = body.local_kind(local);
|
2019-05-25 09:59:05 -05:00
|
|
|
|
2019-11-23 11:23:56 -06:00
|
|
|
if local_kind == LocalKind::Arg || local_kind == LocalKind::Var {
|
|
|
|
*val = ConstPropMode::OnlyPropagateInto;
|
|
|
|
trace!("local {:?} can't be const propagated because it's not a temporary", local);
|
2019-05-25 09:59:05 -05:00
|
|
|
}
|
2018-01-30 02:40:46 -06:00
|
|
|
}
|
2019-06-03 17:26:48 -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
|
|
|
|
2018-01-30 07:12:16 -06:00
|
|
|
impl<'tcx> Visitor<'tcx> for CanConstProp {
|
2018-01-30 06:57:13 -06:00
|
|
|
fn visit_local(
|
2018-01-28 07:41:17 -06:00
|
|
|
&mut self,
|
2018-01-30 06:57:13 -06:00
|
|
|
&local: &Local,
|
2019-04-24 13:41:43 -05:00
|
|
|
context: PlaceContext,
|
2018-01-30 06:57:13 -06:00
|
|
|
_: Location,
|
2018-01-28 07:41:17 -06:00
|
|
|
) {
|
2018-01-30 06:57:13 -06:00
|
|
|
use rustc::mir::visit::PlaceContext::*;
|
|
|
|
match context {
|
|
|
|
// Constants must have at most one write
|
|
|
|
// FIXME(oli-obk): we could be more powerful here, if the multiple writes
|
|
|
|
// only occur in independent execution paths
|
2018-10-26 06:22:45 -05:00
|
|
|
MutatingUse(MutatingUseContext::Store) => if self.found_assignment[local] {
|
2019-05-25 09:59:05 -05:00
|
|
|
trace!("local {:?} can't be propagated because of multiple assignments", local);
|
2019-11-23 11:23:56 -06:00
|
|
|
self.can_const_prop[local] = ConstPropMode::NoPropagation;
|
2018-01-30 06:57:13 -06:00
|
|
|
} else {
|
|
|
|
self.found_assignment[local] = true
|
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) |
|
|
|
|
MutatingUse(MutatingUseContext::Projection) |
|
|
|
|
NonUse(_) => {},
|
2019-05-25 09:59:05 -05:00
|
|
|
_ => {
|
|
|
|
trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
|
2019-11-23 11:23:56 -06:00
|
|
|
self.can_const_prop[local] = ConstPropMode::NoPropagation;
|
2019-05-25 09:59:05 -05:00
|
|
|
},
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
|
2019-10-20 15:11:04 -05:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2018-01-28 07:41:17 -06:00
|
|
|
fn visit_constant(
|
|
|
|
&mut self,
|
2019-04-28 20:58:40 -05:00
|
|
|
constant: &mut Constant<'tcx>,
|
2018-01-28 07:41:17 -06:00
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
trace!("visit_constant: {:?}", constant);
|
|
|
|
self.super_constant(constant, location);
|
2019-04-29 17:32:35 -05:00
|
|
|
self.eval_constant(constant);
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(
|
|
|
|
&mut self,
|
2019-04-28 20:58:40 -05:00
|
|
|
statement: &mut Statement<'tcx>,
|
2018-01-28 07:41:17 -06:00
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
trace!("visit_statement: {:?}", statement);
|
2019-09-11 14:05:45 -05:00
|
|
|
if let StatementKind::Assign(box(ref place, ref mut rval)) = statement.kind {
|
2019-04-26 07:26:49 -05:00
|
|
|
let place_ty: Ty<'tcx> = place
|
2019-04-29 17:32:35 -05:00
|
|
|
.ty(&self.local_decls, self.tcx)
|
2019-03-28 21:08:31 -05:00
|
|
|
.ty;
|
2018-06-04 11:32:06 -05:00
|
|
|
if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
|
2019-10-20 15:09:36 -05:00
|
|
|
if let Some(local) = place.as_local() {
|
2019-10-12 07:21:51 -05:00
|
|
|
let source = statement.source_info;
|
2019-11-23 11:23:56 -06:00
|
|
|
let can_const_prop = self.can_const_prop[local];
|
2019-10-12 07:21:51 -05:00
|
|
|
if let Some(()) = self.const_prop(rval, place_layout, source, place) {
|
2019-11-23 11:23:56 -06:00
|
|
|
if can_const_prop == ConstPropMode::FullConstProp ||
|
|
|
|
can_const_prop == ConstPropMode::OnlyPropagateInto {
|
2019-10-28 04:59:59 -05:00
|
|
|
if let Some(value) = self.get_const(local) {
|
|
|
|
if self.should_const_prop(value) {
|
|
|
|
trace!("replacing {:?} with {:?}", rval, value);
|
|
|
|
self.replace_with_const(
|
|
|
|
rval,
|
|
|
|
value,
|
|
|
|
statement.source_info,
|
|
|
|
);
|
2019-11-23 11:23:56 -06:00
|
|
|
|
|
|
|
if can_const_prop == ConstPropMode::FullConstProp {
|
|
|
|
trace!("propagated into {:?}", local);
|
|
|
|
}
|
2019-10-28 04:59:59 -05:00
|
|
|
}
|
2019-04-28 20:58:40 -05:00
|
|
|
}
|
2019-11-23 11:23:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.can_const_prop[local] != ConstPropMode::FullConstProp {
|
|
|
|
trace!("can't propagate into {:?}", local);
|
|
|
|
if local != RETURN_PLACE {
|
|
|
|
self.remove_const(local);
|
2018-06-04 11:32:06 -05:00
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 07:40:43 -05:00
|
|
|
} else {
|
|
|
|
match statement.kind {
|
|
|
|
StatementKind::StorageLive(local) |
|
2019-11-23 11:23:56 -06:00
|
|
|
StatementKind::StorageDead(local) => {
|
2019-09-13 07:40:43 -05:00
|
|
|
let frame = self.ecx.frame_mut();
|
|
|
|
frame.locals[local].value =
|
|
|
|
if let StatementKind::StorageLive(_) = statement.kind {
|
|
|
|
LocalValue::Uninitialized
|
|
|
|
} else {
|
|
|
|
LocalValue::Dead
|
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => {}
|
2019-09-05 20:06:57 -05:00
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2019-09-13 07:40:43 -05:00
|
|
|
|
2019-04-22 15:07:14 -05:00
|
|
|
self.super_statement(statement, location);
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
|
2019-04-29 17:32:35 -05:00
|
|
|
fn visit_terminator(
|
2018-01-28 07:41:17 -06:00
|
|
|
&mut self,
|
2019-04-28 20:58:40 -05:00
|
|
|
terminator: &mut Terminator<'tcx>,
|
2018-01-30 07:28:45 -06:00
|
|
|
location: Location,
|
2018-01-28 07:41:17 -06:00
|
|
|
) {
|
2019-04-29 17:32:35 -05:00
|
|
|
self.super_terminator(terminator, location);
|
2019-05-11 09:55:34 -05:00
|
|
|
let source_info = terminator.source_info;
|
|
|
|
match &mut terminator.kind {
|
2019-07-24 02:12:21 -05:00
|
|
|
TerminatorKind::Assert { expected, ref msg, ref mut cond, .. } => {
|
2019-05-11 09:55:34 -05:00
|
|
|
if let Some(value) = self.eval_operand(&cond, source_info) {
|
|
|
|
trace!("assertion on {:?} should be {:?}", value, expected);
|
|
|
|
let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
|
|
|
|
let value_const = self.ecx.read_scalar(value).unwrap();
|
|
|
|
if expected != value_const {
|
|
|
|
// poison all places this operand references so that further code
|
|
|
|
// doesn't use the invalid value
|
|
|
|
match cond {
|
|
|
|
Operand::Move(ref place) | Operand::Copy(ref place) => {
|
2019-04-30 11:58:24 -05:00
|
|
|
if let PlaceBase::Local(local) = place.base {
|
2019-06-21 06:36:04 -05:00
|
|
|
self.remove_const(local);
|
2019-05-11 09:55:34 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Operand::Constant(_) => {}
|
|
|
|
}
|
|
|
|
let span = terminator.source_info.span;
|
|
|
|
let hir_id = self
|
|
|
|
.tcx
|
|
|
|
.hir()
|
|
|
|
.as_local_hir_id(self.source.def_id())
|
|
|
|
.expect("some part of a failing const eval must be local");
|
|
|
|
let msg = match msg {
|
2019-07-29 02:58:55 -05:00
|
|
|
PanicInfo::Overflow(_) |
|
|
|
|
PanicInfo::OverflowNeg |
|
|
|
|
PanicInfo::DivisionByZero |
|
|
|
|
PanicInfo::RemainderByZero =>
|
2019-07-24 03:24:55 -05:00
|
|
|
msg.description().to_owned(),
|
2019-07-29 02:58:55 -05:00
|
|
|
PanicInfo::BoundsCheck { ref len, ref index } => {
|
2019-05-11 09:55:34 -05:00
|
|
|
let len = self
|
|
|
|
.eval_operand(len, source_info)
|
|
|
|
.expect("len must be const");
|
|
|
|
let len = match self.ecx.read_scalar(len) {
|
2019-05-25 03:59:09 -05:00
|
|
|
Ok(ScalarMaybeUndef::Scalar(Scalar::Raw {
|
|
|
|
data, ..
|
|
|
|
})) => data,
|
2019-05-11 09:55:34 -05:00
|
|
|
other => bug!("const len not primitive: {:?}", other),
|
|
|
|
};
|
|
|
|
let index = self
|
|
|
|
.eval_operand(index, source_info)
|
|
|
|
.expect("index must be const");
|
|
|
|
let index = match self.ecx.read_scalar(index) {
|
2019-05-25 03:59:09 -05:00
|
|
|
Ok(ScalarMaybeUndef::Scalar(Scalar::Raw {
|
|
|
|
data, ..
|
|
|
|
})) => data,
|
2019-05-11 09:55:34 -05:00
|
|
|
other => bug!("const index not primitive: {:?}", other),
|
|
|
|
};
|
|
|
|
format!(
|
|
|
|
"index out of bounds: \
|
|
|
|
the len is {} but the index is {}",
|
|
|
|
len,
|
|
|
|
index,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
// Need proper const propagator for these
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
self.tcx.lint_hir(
|
|
|
|
::rustc::lint::builtin::CONST_ERR,
|
|
|
|
hir_id,
|
|
|
|
span,
|
|
|
|
&msg,
|
|
|
|
);
|
|
|
|
} else {
|
2019-10-28 04:59:59 -05:00
|
|
|
if self.should_const_prop(value) {
|
2019-05-11 11:33:10 -05:00
|
|
|
if let ScalarMaybeUndef::Scalar(scalar) = value_const {
|
|
|
|
*cond = self.operand_from_scalar(
|
|
|
|
scalar,
|
|
|
|
self.tcx.types.bool,
|
|
|
|
source_info.span,
|
|
|
|
);
|
|
|
|
}
|
2019-05-11 09:55:34 -05:00
|
|
|
}
|
2018-03-06 05:43:02 -06:00
|
|
|
}
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
2019-05-11 09:55:34 -05:00
|
|
|
},
|
2019-05-11 10:24:14 -05:00
|
|
|
TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
|
2019-10-28 04:59:59 -05:00
|
|
|
if let Some(value) = self.eval_operand(&discr, source_info) {
|
|
|
|
if self.should_const_prop(value) {
|
2019-05-11 11:33:10 -05:00
|
|
|
if let ScalarMaybeUndef::Scalar(scalar) =
|
|
|
|
self.ecx.read_scalar(value).unwrap() {
|
|
|
|
*discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
|
|
|
|
}
|
2019-05-11 10:24:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-05-11 11:28:53 -05:00
|
|
|
//none of these have Operands to const-propagate
|
|
|
|
TerminatorKind::Goto { .. } |
|
|
|
|
TerminatorKind::Resume |
|
|
|
|
TerminatorKind::Abort |
|
|
|
|
TerminatorKind::Return |
|
|
|
|
TerminatorKind::Unreachable |
|
|
|
|
TerminatorKind::Drop { .. } |
|
|
|
|
TerminatorKind::DropAndReplace { .. } |
|
|
|
|
TerminatorKind::Yield { .. } |
|
|
|
|
TerminatorKind::GeneratorDrop |
|
|
|
|
TerminatorKind::FalseEdges { .. } |
|
|
|
|
TerminatorKind::FalseUnwind { .. } => { }
|
|
|
|
//FIXME(wesleywiser) Call does have Operands that could be const-propagated
|
|
|
|
TerminatorKind::Call { .. } => { }
|
2018-01-28 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|