1 //! Propagates constants for early reporting of statically known
4 use rustc::hir::def::DefKind;
6 AggregateKind, Constant, Location, Place, PlaceBase, Body, Operand, Rvalue,
7 Local, NullOp, UnOp, StatementKind, Statement, LocalKind, Static, StaticKind,
8 TerminatorKind, Terminator, ClearCrossCrate, SourceInfo, BinOp, ProjectionElem,
9 SourceScope, SourceScopeLocalData, LocalDecl, Promoted,
11 use rustc::mir::visit::{
12 Visitor, PlaceContext, MutatingUseContext, MutVisitor, NonMutatingUseContext,
14 use rustc::mir::interpret::{InterpError, Scalar, GlobalId, EvalResult};
15 use rustc::ty::{self, Instance, ParamEnv, Ty, TyCtxt};
16 use syntax_pos::{Span, DUMMY_SP};
17 use rustc::ty::subst::InternalSubsts;
18 use rustc_data_structures::indexed_vec::IndexVec;
19 use rustc::ty::layout::{
20 LayoutOf, TyLayout, LayoutError,
21 HasTyCtxt, TargetDataLayout, HasDataLayout,
24 use crate::interpret::{
25 self, InterpretCx, ScalarMaybeUndef, Immediate, OpTy, ImmTy, MemoryKind,
27 use crate::const_eval::{
28 CompileTimeInterpreter, error_to_const_error, eval_promoted, mk_eval_cx,
30 use crate::transform::{MirPass, MirSource};
34 impl MirPass for ConstProp {
35 fn run_pass<'a, 'tcx>(&self,
36 tcx: TyCtxt<'a, 'tcx, 'tcx>,
37 source: MirSource<'tcx>,
38 mir: &mut Body<'tcx>) {
39 // will be evaluated by miri and produce its errors there
40 if source.promoted.is_some() {
44 use rustc::hir::map::blocks::FnLikeNode;
45 let hir_id = tcx.hir().as_local_hir_id(source.def_id())
46 .expect("Non-local call to local provider is_const_fn");
48 let is_fn_like = FnLikeNode::from_node(tcx.hir().get_by_hir_id(hir_id)).is_some();
49 let is_assoc_const = match tcx.def_kind(source.def_id()) {
50 Some(DefKind::AssocConst) => true,
54 // Only run const prop on functions, methods, closures and associated constants
55 if !is_fn_like && !is_assoc_const {
56 // skip anon_const/statics/consts because they'll be evaluated by miri anyway
57 trace!("ConstProp skipped for {:?}", source.def_id());
61 trace!("ConstProp starting for {:?}", source.def_id());
63 // FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
64 // constants, instead of just checking for const-folding succeeding.
65 // That would require an uniform one-def no-mutation analysis
66 // and RPO (or recursing when needing the value of a local).
67 let mut optimization_finder = ConstPropagator::new(mir, tcx, source);
68 optimization_finder.visit_body(mir);
70 // put back the data we stole from `mir`
72 &mut mir.source_scope_local_data,
73 optimization_finder.source_scope_local_data
77 optimization_finder.promoted
80 trace!("ConstProp done for {:?}", source.def_id());
84 type Const<'tcx> = OpTy<'tcx>;
86 /// Finds optimization opportunities on the MIR.
87 struct ConstPropagator<'a, 'mir, 'tcx:'a+'mir> {
88 ecx: InterpretCx<'a, 'mir, 'tcx, CompileTimeInterpreter<'a, 'mir, 'tcx>>,
89 tcx: TyCtxt<'a, 'tcx, 'tcx>,
90 source: MirSource<'tcx>,
91 places: IndexVec<Local, Option<Const<'tcx>>>,
92 can_const_prop: IndexVec<Local, bool>,
93 param_env: ParamEnv<'tcx>,
94 source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
95 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
96 promoted: IndexVec<Promoted, Body<'tcx>>,
99 impl<'a, 'b, 'tcx> LayoutOf for ConstPropagator<'a, 'b, 'tcx> {
101 type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
103 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
104 self.tcx.layout_of(self.param_env.and(ty))
108 impl<'a, 'b, 'tcx> HasDataLayout for ConstPropagator<'a, 'b, 'tcx> {
110 fn data_layout(&self) -> &TargetDataLayout {
111 &self.tcx.data_layout
115 impl<'a, 'b, 'tcx> HasTyCtxt<'tcx> for ConstPropagator<'a, 'b, 'tcx> {
117 fn tcx<'c>(&'c self) -> TyCtxt<'c, 'tcx, 'tcx> {
122 impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
124 mir: &mut Body<'tcx>,
125 tcx: TyCtxt<'a, 'tcx, 'tcx>,
126 source: MirSource<'tcx>,
127 ) -> ConstPropagator<'a, 'mir, 'tcx> {
128 let param_env = tcx.param_env(source.def_id());
129 let ecx = mk_eval_cx(tcx, tcx.def_span(source.def_id()), param_env);
130 let can_const_prop = CanConstProp::check(mir);
131 let source_scope_local_data = std::mem::replace(
132 &mut mir.source_scope_local_data,
133 ClearCrossCrate::Clear
135 let promoted = std::mem::replace(
146 places: IndexVec::from_elem(None, &mir.local_decls),
147 source_scope_local_data,
148 //FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
149 local_decls: mir.local_decls.clone(),
156 source_info: SourceInfo,
160 F: FnOnce(&mut Self) -> EvalResult<'tcx, T>,
162 self.ecx.tcx.span = source_info.span;
163 let lint_root = match self.source_scope_local_data {
164 ClearCrossCrate::Set(ref ivs) => {
165 //FIXME(#51314): remove this check
166 if source_info.scope.index() >= ivs.len() {
169 ivs[source_info.scope].lint_root
171 ClearCrossCrate::Clear => return None,
173 let r = match f(self) {
174 Ok(val) => Some(val),
176 let diagnostic = error_to_const_error(&self.ecx, error);
177 use rustc::mir::interpret::InterpError::*;
178 match diagnostic.error {
179 // don't report these, they make no sense in a const prop context
182 // at runtime these transformations might make sense
183 // FIXME: figure out the rules and start linting
184 | FunctionAbiMismatch(..)
185 | FunctionArgMismatch(..)
186 | FunctionRetMismatch(..)
187 | FunctionArgCountMismatch
188 // fine at runtime, might be a register address or sth
193 // don't report const evaluator limits
194 | StackFrameLimitReached
199 | InvalidMemoryAccess
200 | DanglingPointerDeref
202 | InvalidFunctionPointer
204 | InvalidDiscriminant(..)
205 | PointerOutOfBounds { .. }
206 | InvalidNullPointerUsage
207 | ValidationFailure(..)
212 | DerefFunctionPointer
217 | AlignmentCheckFailed{..}
218 | CalledClosureAsFunction
219 | VtableForArgumentlessMethod
220 | ModifiedConstantMemory
223 // FIXME: should probably be removed and turned into a bug! call
224 | TypeNotPrimitive(_)
225 | ReallocatedWrongMemoryKind(_, _)
226 | DeallocatedWrongMemoryKind(_, _)
227 | ReallocateNonBasePtr
228 | DeallocateNonBasePtr
229 | IncorrectAllocationInformation(..)
230 | UnterminatedCString(_)
232 | HeapAllocNonPowerOfTwoAlignment(_)
234 | ReadFromReturnPointer
235 | GeneratorResumedAfterReturn
236 | GeneratorResumedAfterPanic
240 // FIXME: report UB here
246 => bug!("these should not be in rustc, but in miri's machine errors"),
249 | UnimplementedTraitSelection
252 // these are just noise
267 diagnostic.report_as_lint(
269 "this expression will panic at runtime",
278 self.ecx.tcx.span = DUMMY_SP;
285 ) -> Option<Const<'tcx>> {
286 self.ecx.tcx.span = c.span;
287 match self.ecx.eval_const_to_op(c.literal, None) {
292 let err = error_to_const_error(&self.ecx, error);
293 err.report_as_error(self.ecx.tcx, "erroneous constant used");
299 fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
300 trace!("eval_place(place={:?})", place);
301 place.iterate(|place_base, place_projection| {
302 let mut eval = match place_base {
303 PlaceBase::Local(loc) => self.places[*loc].clone()?,
304 PlaceBase::Static(box Static {kind: StaticKind::Promoted(promoted), ..}) => {
305 let generics = self.tcx.generics_of(self.source.def_id());
306 if generics.requires_monomorphization(self.tcx) {
307 // FIXME: can't handle code with generics
310 let substs = InternalSubsts::identity_for_item(self.tcx, self.source.def_id());
311 let instance = Instance::new(self.source.def_id(), substs);
314 promoted: Some(*promoted),
316 // cannot use `const_eval` here, because that would require having the MIR
317 // for the current function available, but we're producing said MIR right now
318 let res = self.use_ecx(source_info, |this| {
319 let mir = &this.promoted[*promoted];
320 eval_promoted(this.tcx, cid, mir, this.param_env)
322 trace!("evaluated promoted {:?} to {:?}", promoted, res);
328 for proj in place_projection {
330 ProjectionElem::Field(field, _) => {
331 trace!("field proj on {:?}", proj.base);
332 eval = self.use_ecx(source_info, |this| {
333 this.ecx.operand_field(eval, field.index() as u64)
336 // We could get more projections by using e.g., `operand_projection`,
337 // but we do not even have the stack frame set up properly so
338 // an `Index` projection would throw us off-track.
347 fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
349 Operand::Constant(ref c) => self.eval_constant(c),
350 | Operand::Move(ref place)
351 | Operand::Copy(ref place) => self.eval_place(place, source_info),
357 rvalue: &Rvalue<'tcx>,
358 place_layout: TyLayout<'tcx>,
359 source_info: SourceInfo,
360 ) -> Option<Const<'tcx>> {
361 let span = source_info.span;
363 Rvalue::Use(ref op) => {
364 self.eval_operand(op, source_info)
366 Rvalue::Ref(_, _, ref place) => {
367 let src = self.eval_place(place, source_info)?;
368 let mplace = src.try_as_mplace().ok()?;
369 Some(ImmTy::from_scalar(mplace.ptr.into(), place_layout).into())
372 Rvalue::Aggregate(..) |
373 Rvalue::NullaryOp(NullOp::Box, _) |
374 Rvalue::Discriminant(..) => None,
376 Rvalue::Cast(kind, ref operand, _) => {
377 let op = self.eval_operand(operand, source_info)?;
378 self.use_ecx(source_info, |this| {
379 let dest = this.ecx.allocate(place_layout, MemoryKind::Stack);
380 this.ecx.cast(op, kind, dest.into())?;
385 // FIXME(oli-obk): evaluate static/constant slice lengths
386 Rvalue::Len(_) => None,
387 Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
388 type_size_of(self.tcx, self.param_env, ty).and_then(|n| Some(
390 imm: Immediate::Scalar(
391 Scalar::from_uint(n, self.tcx.data_layout.pointer_size).into()
393 layout: self.tcx.layout_of(self.param_env.and(self.tcx.types.usize)).ok()?,
397 Rvalue::UnaryOp(op, ref arg) => {
398 let def_id = if self.tcx.is_closure(self.source.def_id()) {
399 self.tcx.closure_base_def_id(self.source.def_id())
403 let generics = self.tcx.generics_of(def_id);
404 if generics.requires_monomorphization(self.tcx) {
405 // FIXME: can't handle code with generics
409 let arg = self.eval_operand(arg, source_info)?;
410 let val = self.use_ecx(source_info, |this| {
411 let prim = this.ecx.read_immediate(arg)?;
414 // Need to do overflow check here: For actual CTFE, MIR
415 // generation emits code that does this before calling the op.
416 if prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
417 return err!(OverflowNeg);
424 // Now run the actual operation.
425 this.ecx.unary_op(op, prim)
428 imm: Immediate::Scalar(val.into()),
429 layout: place_layout,
433 Rvalue::CheckedBinaryOp(op, ref left, ref right) |
434 Rvalue::BinaryOp(op, ref left, ref right) => {
435 trace!("rvalue binop {:?} for {:?} and {:?}", op, left, right);
436 let right = self.eval_operand(right, source_info)?;
437 let def_id = if self.tcx.is_closure(self.source.def_id()) {
438 self.tcx.closure_base_def_id(self.source.def_id())
442 let generics = self.tcx.generics_of(def_id);
443 if generics.requires_monomorphization(self.tcx) {
444 // FIXME: can't handle code with generics
448 let r = self.use_ecx(source_info, |this| {
449 this.ecx.read_immediate(right)
451 if op == BinOp::Shr || op == BinOp::Shl {
452 let left_ty = left.ty(&self.local_decls, self.tcx);
455 .layout_of(self.param_env.and(left_ty))
459 let right_size = right.layout.size;
460 let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
461 if r_bits.ok().map_or(false, |b| b >= left_bits as u128) {
462 let source_scope_local_data = match self.source_scope_local_data {
463 ClearCrossCrate::Set(ref data) => data,
464 ClearCrossCrate::Clear => return None,
466 let dir = if op == BinOp::Shr {
471 let hir_id = source_scope_local_data[source_info.scope].lint_root;
473 ::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
476 &format!("attempt to shift {} with overflow", dir));
480 let left = self.eval_operand(left, source_info)?;
481 let l = self.use_ecx(source_info, |this| {
482 this.ecx.read_immediate(left)
484 trace!("const evaluating {:?} for {:?} and {:?}", op, left, right);
485 let (val, overflow) = self.use_ecx(source_info, |this| {
486 this.ecx.binary_op(op, l, r)
488 let val = if let Rvalue::CheckedBinaryOp(..) = *rvalue {
489 Immediate::ScalarPair(
491 Scalar::from_bool(overflow).into(),
495 let err = InterpError::Overflow(op).into();
496 let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
499 Immediate::Scalar(val.into())
503 layout: place_layout,
510 fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
511 Operand::Constant(Box::new(
516 literal: self.tcx.mk_const(*ty::Const::from_scalar(
525 fn replace_with_const(
527 rval: &mut Rvalue<'tcx>,
529 source_info: SourceInfo,
531 trace!("attepting to replace {:?} with {:?}", rval, value);
532 if let Err(e) = self.ecx.validate_operand(value, vec![], None, true) {
533 trace!("validation error, attempt failed: {:?}", e);
537 // FIXME> figure out what tho do when try_read_immediate fails
538 let imm = self.use_ecx(source_info, |this| {
539 this.ecx.try_read_immediate(value)
542 if let Some(Ok(imm)) = imm {
544 interpret::Immediate::Scalar(ScalarMaybeUndef::Scalar(scalar)) => {
546 self.operand_from_scalar(scalar, value.layout.ty, source_info.span));
548 Immediate::ScalarPair(
549 ScalarMaybeUndef::Scalar(one),
550 ScalarMaybeUndef::Scalar(two)
552 let ty = &value.layout.ty.sty;
553 if let ty::Tuple(substs) = ty {
554 *rval = Rvalue::Aggregate(
555 Box::new(AggregateKind::Tuple),
557 self.operand_from_scalar(
558 one, substs[0].expect_ty(), source_info.span
560 self.operand_from_scalar(
561 two, substs[1].expect_ty(), source_info.span
572 fn should_const_prop(&self) -> bool {
573 self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
577 fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
578 param_env: ty::ParamEnv<'tcx>,
579 ty: Ty<'tcx>) -> Option<u64> {
580 tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
583 struct CanConstProp {
584 can_const_prop: IndexVec<Local, bool>,
585 // false at the beginning, once set, there are not allowed to be any more assignments
586 found_assignment: IndexVec<Local, bool>,
590 /// returns true if `local` can be propagated
591 fn check(mir: &Body<'_>) -> IndexVec<Local, bool> {
592 let mut cpv = CanConstProp {
593 can_const_prop: IndexVec::from_elem(true, &mir.local_decls),
594 found_assignment: IndexVec::from_elem(false, &mir.local_decls),
596 for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
597 // cannot use args at all
598 // cannot use locals because if x < y { y - x } else { x - y } would
600 // FIXME(oli-obk): lint variables until they are used in a condition
601 // FIXME(oli-obk): lint if return value is constant
602 *val = mir.local_kind(local) == LocalKind::Temp;
605 trace!("local {:?} can't be propagated because it's not a temporary", local);
613 impl<'tcx> Visitor<'tcx> for CanConstProp {
617 context: PlaceContext,
620 use rustc::mir::visit::PlaceContext::*;
622 // Constants must have at most one write
623 // FIXME(oli-obk): we could be more powerful here, if the multiple writes
624 // only occur in independent execution paths
625 MutatingUse(MutatingUseContext::Store) => if self.found_assignment[local] {
626 trace!("local {:?} can't be propagated because of multiple assignments", local);
627 self.can_const_prop[local] = false;
629 self.found_assignment[local] = true
631 // Reading constants is allowed an arbitrary number of times
632 NonMutatingUse(NonMutatingUseContext::Copy) |
633 NonMutatingUse(NonMutatingUseContext::Move) |
634 NonMutatingUse(NonMutatingUseContext::Inspect) |
635 NonMutatingUse(NonMutatingUseContext::Projection) |
636 MutatingUse(MutatingUseContext::Projection) |
639 trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
640 self.can_const_prop[local] = false;
646 impl<'b, 'a, 'tcx> MutVisitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
649 constant: &mut Constant<'tcx>,
652 trace!("visit_constant: {:?}", constant);
653 self.super_constant(constant, location);
654 self.eval_constant(constant);
659 statement: &mut Statement<'tcx>,
662 trace!("visit_statement: {:?}", statement);
663 if let StatementKind::Assign(ref place, ref mut rval) = statement.kind {
664 let place_ty: Ty<'tcx> = place
665 .ty(&self.local_decls, self.tcx)
667 if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
668 if let Some(value) = self.const_prop(rval, place_layout, statement.source_info) {
669 if let Place::Base(PlaceBase::Local(local)) = *place {
670 trace!("checking whether {:?} can be stored to {:?}", value, local);
671 if self.can_const_prop[local] {
672 trace!("storing {:?} to {:?}", value, local);
673 assert!(self.places[local].is_none());
674 self.places[local] = Some(value);
676 if self.should_const_prop() {
677 self.replace_with_const(
680 statement.source_info,
688 self.super_statement(statement, location);
693 terminator: &mut Terminator<'tcx>,
696 self.super_terminator(terminator, location);
697 let source_info = terminator.source_info;
698 match &mut terminator.kind {
699 TerminatorKind::Assert { expected, msg, ref mut cond, .. } => {
700 if let Some(value) = self.eval_operand(&cond, source_info) {
701 trace!("assertion on {:?} should be {:?}", value, expected);
702 let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
703 let value_const = self.ecx.read_scalar(value).unwrap();
704 if expected != value_const {
705 // poison all places this operand references so that further code
706 // doesn't use the invalid value
708 Operand::Move(ref place) | Operand::Copy(ref place) => {
709 let mut place = place;
710 while let Place::Projection(ref proj) = *place {
713 if let Place::Base(PlaceBase::Local(local)) = *place {
714 self.places[local] = None;
717 Operand::Constant(_) => {}
719 let span = terminator.source_info.span;
723 .as_local_hir_id(self.source.def_id())
724 .expect("some part of a failing const eval must be local");
725 use rustc::mir::interpret::InterpError::*;
726 let msg = match msg {
730 RemainderByZero => msg.description().to_owned(),
731 BoundsCheck { ref len, ref index } => {
733 .eval_operand(len, source_info)
734 .expect("len must be const");
735 let len = match self.ecx.read_scalar(len) {
736 Ok(ScalarMaybeUndef::Scalar(Scalar::Raw {
739 other => bug!("const len not primitive: {:?}", other),
742 .eval_operand(index, source_info)
743 .expect("index must be const");
744 let index = match self.ecx.read_scalar(index) {
745 Ok(ScalarMaybeUndef::Scalar(Scalar::Raw {
748 other => bug!("const index not primitive: {:?}", other),
751 "index out of bounds: \
752 the len is {} but the index is {}",
757 // Need proper const propagator for these
761 ::rustc::lint::builtin::CONST_ERR,
767 if self.should_const_prop() {
768 if let ScalarMaybeUndef::Scalar(scalar) = value_const {
769 *cond = self.operand_from_scalar(
779 TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
780 if self.should_const_prop() {
781 if let Some(value) = self.eval_operand(&discr, source_info) {
782 if let ScalarMaybeUndef::Scalar(scalar) =
783 self.ecx.read_scalar(value).unwrap() {
784 *discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
789 //none of these have Operands to const-propagate
790 TerminatorKind::Goto { .. } |
791 TerminatorKind::Resume |
792 TerminatorKind::Abort |
793 TerminatorKind::Return |
794 TerminatorKind::Unreachable |
795 TerminatorKind::Drop { .. } |
796 TerminatorKind::DropAndReplace { .. } |
797 TerminatorKind::Yield { .. } |
798 TerminatorKind::GeneratorDrop |
799 TerminatorKind::FalseEdges { .. } |
800 TerminatorKind::FalseUnwind { .. } => { }
801 //FIXME(wesleywiser) Call does have Operands that could be const-propagated
802 TerminatorKind::Call { .. } => { }