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, HasTyCtxt, TargetDataLayout, HasDataLayout, Size,
23 use crate::interpret::{
24 self, InterpretCx, ScalarMaybeUndef, Immediate, OpTy, ImmTy, MemoryKind,
26 use crate::const_eval::{
27 CompileTimeInterpreter, error_to_const_error, eval_promoted, mk_eval_cx,
29 use crate::transform::{MirPass, MirSource};
33 impl MirPass for ConstProp {
34 fn run_pass<'a, 'tcx>(&self,
35 tcx: TyCtxt<'a, 'tcx, 'tcx>,
36 source: MirSource<'tcx>,
37 mir: &mut Body<'tcx>) {
38 // will be evaluated by miri and produce its errors there
39 if source.promoted.is_some() {
43 use rustc::hir::map::blocks::FnLikeNode;
44 let hir_id = tcx.hir().as_local_hir_id(source.def_id())
45 .expect("Non-local call to local provider is_const_fn");
47 let is_fn_like = FnLikeNode::from_node(tcx.hir().get_by_hir_id(hir_id)).is_some();
48 let is_assoc_const = match tcx.def_kind(source.def_id()) {
49 Some(DefKind::AssocConst) => true,
53 // Only run const prop on functions, methods, closures and associated constants
54 if !is_fn_like && !is_assoc_const {
55 // skip anon_const/statics/consts because they'll be evaluated by miri anyway
56 trace!("ConstProp skipped for {:?}", source.def_id());
60 trace!("ConstProp starting for {:?}", source.def_id());
62 // FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
63 // constants, instead of just checking for const-folding succeeding.
64 // That would require an uniform one-def no-mutation analysis
65 // and RPO (or recursing when needing the value of a local).
66 let mut optimization_finder = ConstPropagator::new(mir, tcx, source);
67 optimization_finder.visit_body(mir);
69 // put back the data we stole from `mir`
71 &mut mir.source_scope_local_data,
72 optimization_finder.source_scope_local_data
76 optimization_finder.promoted
79 trace!("ConstProp done for {:?}", source.def_id());
83 type Const<'tcx> = OpTy<'tcx>;
85 /// Finds optimization opportunities on the MIR.
86 struct ConstPropagator<'a, 'mir, 'tcx:'a+'mir> {
87 ecx: InterpretCx<'a, 'mir, 'tcx, CompileTimeInterpreter<'a, 'mir, 'tcx>>,
88 tcx: TyCtxt<'a, 'tcx, 'tcx>,
89 source: MirSource<'tcx>,
90 places: IndexVec<Local, Option<Const<'tcx>>>,
91 can_const_prop: IndexVec<Local, bool>,
92 param_env: ParamEnv<'tcx>,
93 source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
94 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
95 promoted: IndexVec<Promoted, Body<'tcx>>,
98 impl<'a, 'b, 'tcx> LayoutOf for ConstPropagator<'a, 'b, 'tcx> {
100 type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
102 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
103 self.tcx.layout_of(self.param_env.and(ty))
107 impl<'a, 'b, 'tcx> HasDataLayout for ConstPropagator<'a, 'b, 'tcx> {
109 fn data_layout(&self) -> &TargetDataLayout {
110 &self.tcx.data_layout
114 impl<'a, 'b, 'tcx> HasTyCtxt<'tcx> for ConstPropagator<'a, 'b, 'tcx> {
116 fn tcx<'c>(&'c self) -> TyCtxt<'c, 'tcx, 'tcx> {
121 impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
123 mir: &mut Body<'tcx>,
124 tcx: TyCtxt<'a, 'tcx, 'tcx>,
125 source: MirSource<'tcx>,
126 ) -> ConstPropagator<'a, 'mir, 'tcx> {
127 let param_env = tcx.param_env(source.def_id());
128 let ecx = mk_eval_cx(tcx, tcx.def_span(source.def_id()), param_env);
129 let can_const_prop = CanConstProp::check(mir);
130 let source_scope_local_data = std::mem::replace(
131 &mut mir.source_scope_local_data,
132 ClearCrossCrate::Clear
134 let promoted = std::mem::replace(
145 places: IndexVec::from_elem(None, &mir.local_decls),
146 source_scope_local_data,
147 //FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
148 local_decls: mir.local_decls.clone(),
155 source_info: SourceInfo,
159 F: FnOnce(&mut Self) -> EvalResult<'tcx, T>,
161 self.ecx.tcx.span = source_info.span;
162 let lint_root = match self.source_scope_local_data {
163 ClearCrossCrate::Set(ref ivs) => {
164 //FIXME(#51314): remove this check
165 if source_info.scope.index() >= ivs.len() {
168 ivs[source_info.scope].lint_root
170 ClearCrossCrate::Clear => return None,
172 let r = match f(self) {
173 Ok(val) => Some(val),
175 let diagnostic = error_to_const_error(&self.ecx, error);
176 use rustc::mir::interpret::InterpError::*;
177 match diagnostic.error {
178 // don't report these, they make no sense in a const prop context
181 // at runtime these transformations might make sense
182 // FIXME: figure out the rules and start linting
183 | FunctionAbiMismatch(..)
184 | FunctionArgMismatch(..)
185 | FunctionRetMismatch(..)
186 | FunctionArgCountMismatch
187 // fine at runtime, might be a register address or sth
192 // don't report const evaluator limits
193 | StackFrameLimitReached
198 | InvalidMemoryAccess
199 | DanglingPointerDeref
201 | InvalidFunctionPointer
203 | InvalidDiscriminant(..)
204 | PointerOutOfBounds { .. }
205 | InvalidNullPointerUsage
206 | ValidationFailure(..)
211 | DerefFunctionPointer
216 | AlignmentCheckFailed{..}
217 | CalledClosureAsFunction
218 | VtableForArgumentlessMethod
219 | ModifiedConstantMemory
222 // FIXME: should probably be removed and turned into a bug! call
223 | TypeNotPrimitive(_)
224 | ReallocatedWrongMemoryKind(_, _)
225 | DeallocatedWrongMemoryKind(_, _)
226 | ReallocateNonBasePtr
227 | DeallocateNonBasePtr
228 | IncorrectAllocationInformation(..)
229 | UnterminatedCString(_)
231 | HeapAllocNonPowerOfTwoAlignment(_)
233 | ReadFromReturnPointer
234 | GeneratorResumedAfterReturn
235 | GeneratorResumedAfterPanic
239 // FIXME: report UB here
245 => bug!("these should not be in rustc, but in miri's machine errors"),
248 | UnimplementedTraitSelection
251 // these are just noise
266 diagnostic.report_as_lint(
268 "this expression will panic at runtime",
277 self.ecx.tcx.span = DUMMY_SP;
284 ) -> Option<Const<'tcx>> {
285 self.ecx.tcx.span = c.span;
286 match self.ecx.eval_const_to_op(c.literal, None) {
291 let err = error_to_const_error(&self.ecx, error);
292 err.report_as_error(self.ecx.tcx, "erroneous constant used");
298 fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
299 trace!("eval_place(place={:?})", place);
300 place.iterate(|place_base, place_projection| {
301 let mut eval = match place_base {
302 PlaceBase::Local(loc) => self.places[*loc].clone()?,
303 PlaceBase::Static(box Static {kind: StaticKind::Promoted(promoted), ..}) => {
304 let generics = self.tcx.generics_of(self.source.def_id());
305 if generics.requires_monomorphization(self.tcx) {
306 // FIXME: can't handle code with generics
309 let substs = InternalSubsts::identity_for_item(self.tcx, self.source.def_id());
310 let instance = Instance::new(self.source.def_id(), substs);
313 promoted: Some(*promoted),
315 // cannot use `const_eval` here, because that would require having the MIR
316 // for the current function available, but we're producing said MIR right now
317 let res = self.use_ecx(source_info, |this| {
318 let mir = &this.promoted[*promoted];
319 eval_promoted(this.tcx, cid, mir, this.param_env)
321 trace!("evaluated promoted {:?} to {:?}", promoted, res);
327 for proj in place_projection {
329 ProjectionElem::Field(field, _) => {
330 trace!("field proj on {:?}", proj.base);
331 eval = self.use_ecx(source_info, |this| {
332 this.ecx.operand_field(eval, field.index() as u64)
335 ProjectionElem::Deref => {
336 trace!("processing deref");
337 eval = self.use_ecx(source_info, |this| {
338 this.ecx.deref_operand(eval)
341 // We could get more projections by using e.g., `operand_projection`,
342 // but we do not even have the stack frame set up properly so
343 // an `Index` projection would throw us off-track.
352 fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
354 Operand::Constant(ref c) => self.eval_constant(c),
355 | Operand::Move(ref place)
356 | Operand::Copy(ref place) => self.eval_place(place, source_info),
362 rvalue: &Rvalue<'tcx>,
363 place_layout: TyLayout<'tcx>,
364 source_info: SourceInfo,
365 ) -> Option<Const<'tcx>> {
366 let span = source_info.span;
368 Rvalue::Use(ref op) => {
369 self.eval_operand(op, source_info)
371 Rvalue::Ref(_, _, ref place) => {
372 let src = self.eval_place(place, source_info)?;
373 let mplace = src.try_as_mplace().ok()?;
374 Some(ImmTy::from_scalar(mplace.ptr.into(), place_layout).into())
377 Rvalue::Aggregate(..) |
378 Rvalue::NullaryOp(NullOp::Box, _) |
379 Rvalue::Discriminant(..) => None,
381 Rvalue::Cast(kind, ref operand, _) => {
382 let op = self.eval_operand(operand, source_info)?;
383 self.use_ecx(source_info, |this| {
384 let dest = this.ecx.allocate(place_layout, MemoryKind::Stack);
385 this.ecx.cast(op, kind, dest.into())?;
389 Rvalue::Len(ref place) => {
390 let place = self.eval_place(&place, source_info)?;
391 let mplace = place.try_as_mplace().ok()?;
393 if let ty::Slice(_) = mplace.layout.ty.sty {
394 let len = mplace.meta.unwrap().to_usize(&self.ecx).unwrap();
397 imm: Immediate::Scalar(
401 self.tcx.sess.target.usize_ty.bit_width().unwrap() as u64
405 layout: self.tcx.layout_of(self.param_env.and(self.tcx.types.usize)).ok()?,
408 trace!("not slice: {:?}", mplace.layout.ty.sty);
412 Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
413 type_size_of(self.tcx, self.param_env, ty).and_then(|n| Some(
415 imm: Immediate::Scalar(
416 Scalar::from_uint(n, self.tcx.data_layout.pointer_size).into()
418 layout: self.tcx.layout_of(self.param_env.and(self.tcx.types.usize)).ok()?,
422 Rvalue::UnaryOp(op, ref arg) => {
423 let def_id = if self.tcx.is_closure(self.source.def_id()) {
424 self.tcx.closure_base_def_id(self.source.def_id())
428 let generics = self.tcx.generics_of(def_id);
429 if generics.requires_monomorphization(self.tcx) {
430 // FIXME: can't handle code with generics
434 let arg = self.eval_operand(arg, source_info)?;
435 let val = self.use_ecx(source_info, |this| {
436 let prim = this.ecx.read_immediate(arg)?;
439 // Need to do overflow check here: For actual CTFE, MIR
440 // generation emits code that does this before calling the op.
441 if prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
442 return err!(OverflowNeg);
449 // Now run the actual operation.
450 this.ecx.unary_op(op, prim)
453 imm: Immediate::Scalar(val.into()),
454 layout: place_layout,
458 Rvalue::CheckedBinaryOp(op, ref left, ref right) |
459 Rvalue::BinaryOp(op, ref left, ref right) => {
460 trace!("rvalue binop {:?} for {:?} and {:?}", op, left, right);
461 let right = self.eval_operand(right, source_info)?;
462 let def_id = if self.tcx.is_closure(self.source.def_id()) {
463 self.tcx.closure_base_def_id(self.source.def_id())
467 let generics = self.tcx.generics_of(def_id);
468 if generics.requires_monomorphization(self.tcx) {
469 // FIXME: can't handle code with generics
473 let r = self.use_ecx(source_info, |this| {
474 this.ecx.read_immediate(right)
476 if op == BinOp::Shr || op == BinOp::Shl {
477 let left_ty = left.ty(&self.local_decls, self.tcx);
480 .layout_of(self.param_env.and(left_ty))
484 let right_size = right.layout.size;
485 let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
486 if r_bits.ok().map_or(false, |b| b >= left_bits as u128) {
487 let source_scope_local_data = match self.source_scope_local_data {
488 ClearCrossCrate::Set(ref data) => data,
489 ClearCrossCrate::Clear => return None,
491 let dir = if op == BinOp::Shr {
496 let hir_id = source_scope_local_data[source_info.scope].lint_root;
498 ::rustc::lint::builtin::EXCEEDING_BITSHIFTS,
501 &format!("attempt to shift {} with overflow", dir));
505 let left = self.eval_operand(left, source_info)?;
506 let l = self.use_ecx(source_info, |this| {
507 this.ecx.read_immediate(left)
509 trace!("const evaluating {:?} for {:?} and {:?}", op, left, right);
510 let (val, overflow) = self.use_ecx(source_info, |this| {
511 this.ecx.binary_op(op, l, r)
513 let val = if let Rvalue::CheckedBinaryOp(..) = *rvalue {
514 Immediate::ScalarPair(
516 Scalar::from_bool(overflow).into(),
520 let err = InterpError::Overflow(op).into();
521 let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
524 Immediate::Scalar(val.into())
528 layout: place_layout,
535 fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
536 Operand::Constant(Box::new(
541 literal: self.tcx.mk_const(*ty::Const::from_scalar(
550 fn replace_with_const(
552 rval: &mut Rvalue<'tcx>,
554 source_info: SourceInfo,
556 trace!("attepting to replace {:?} with {:?}", rval, value);
557 if let Err(e) = self.ecx.validate_operand(value, vec![], None, true) {
558 trace!("validation error, attempt failed: {:?}", e);
562 // FIXME> figure out what tho do when try_read_immediate fails
563 let imm = self.use_ecx(source_info, |this| {
564 this.ecx.try_read_immediate(value)
567 if let Some(Ok(imm)) = imm {
569 interpret::Immediate::Scalar(ScalarMaybeUndef::Scalar(scalar)) => {
571 self.operand_from_scalar(scalar, value.layout.ty, source_info.span));
573 Immediate::ScalarPair(
574 ScalarMaybeUndef::Scalar(one),
575 ScalarMaybeUndef::Scalar(two)
577 let ty = &value.layout.ty.sty;
578 if let ty::Tuple(substs) = ty {
579 *rval = Rvalue::Aggregate(
580 Box::new(AggregateKind::Tuple),
582 self.operand_from_scalar(
583 one, substs[0].expect_ty(), source_info.span
585 self.operand_from_scalar(
586 two, substs[1].expect_ty(), source_info.span
597 fn should_const_prop(&self) -> bool {
598 self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
602 fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
603 param_env: ty::ParamEnv<'tcx>,
604 ty: Ty<'tcx>) -> Option<u64> {
605 tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
608 struct CanConstProp {
609 can_const_prop: IndexVec<Local, bool>,
610 // false at the beginning, once set, there are not allowed to be any more assignments
611 found_assignment: IndexVec<Local, bool>,
615 /// returns true if `local` can be propagated
616 fn check(mir: &Body<'_>) -> IndexVec<Local, bool> {
617 let mut cpv = CanConstProp {
618 can_const_prop: IndexVec::from_elem(true, &mir.local_decls),
619 found_assignment: IndexVec::from_elem(false, &mir.local_decls),
621 for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
622 // cannot use args at all
623 // cannot use locals because if x < y { y - x } else { x - y } would
625 // FIXME(oli-obk): lint variables until they are used in a condition
626 // FIXME(oli-obk): lint if return value is constant
627 *val = mir.local_kind(local) == LocalKind::Temp;
630 trace!("local {:?} can't be propagated because it's not a temporary", local);
638 impl<'tcx> Visitor<'tcx> for CanConstProp {
642 context: PlaceContext,
645 use rustc::mir::visit::PlaceContext::*;
647 // Constants must have at most one write
648 // FIXME(oli-obk): we could be more powerful here, if the multiple writes
649 // only occur in independent execution paths
650 MutatingUse(MutatingUseContext::Store) => if self.found_assignment[local] {
651 trace!("local {:?} can't be propagated because of multiple assignments", local);
652 self.can_const_prop[local] = false;
654 self.found_assignment[local] = true
656 // Reading constants is allowed an arbitrary number of times
657 NonMutatingUse(NonMutatingUseContext::Copy) |
658 NonMutatingUse(NonMutatingUseContext::Move) |
659 NonMutatingUse(NonMutatingUseContext::Inspect) |
660 NonMutatingUse(NonMutatingUseContext::Projection) |
661 MutatingUse(MutatingUseContext::Projection) |
664 trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
665 self.can_const_prop[local] = false;
671 impl<'b, 'a, 'tcx> MutVisitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
674 constant: &mut Constant<'tcx>,
677 trace!("visit_constant: {:?}", constant);
678 self.super_constant(constant, location);
679 self.eval_constant(constant);
684 statement: &mut Statement<'tcx>,
687 trace!("visit_statement: {:?}", statement);
688 if let StatementKind::Assign(ref place, ref mut rval) = statement.kind {
689 let place_ty: Ty<'tcx> = place
690 .ty(&self.local_decls, self.tcx)
692 if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
693 if let Some(value) = self.const_prop(rval, place_layout, statement.source_info) {
694 if let Place::Base(PlaceBase::Local(local)) = *place {
695 trace!("checking whether {:?} can be stored to {:?}", value, local);
696 if self.can_const_prop[local] {
697 trace!("storing {:?} to {:?}", value, local);
698 assert!(self.places[local].is_none());
699 self.places[local] = Some(value);
701 if self.should_const_prop() {
702 self.replace_with_const(
705 statement.source_info,
713 self.super_statement(statement, location);
718 terminator: &mut Terminator<'tcx>,
721 self.super_terminator(terminator, location);
722 let source_info = terminator.source_info;
723 match &mut terminator.kind {
724 TerminatorKind::Assert { expected, msg, ref mut cond, .. } => {
725 if let Some(value) = self.eval_operand(&cond, source_info) {
726 trace!("assertion on {:?} should be {:?}", value, expected);
727 let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
728 let value_const = self.ecx.read_scalar(value).unwrap();
729 if expected != value_const {
730 // poison all places this operand references so that further code
731 // doesn't use the invalid value
733 Operand::Move(ref place) | Operand::Copy(ref place) => {
734 let mut place = place;
735 while let Place::Projection(ref proj) = *place {
738 if let Place::Base(PlaceBase::Local(local)) = *place {
739 self.places[local] = None;
742 Operand::Constant(_) => {}
744 let span = terminator.source_info.span;
748 .as_local_hir_id(self.source.def_id())
749 .expect("some part of a failing const eval must be local");
750 use rustc::mir::interpret::InterpError::*;
751 let msg = match msg {
755 RemainderByZero => msg.description().to_owned(),
756 BoundsCheck { ref len, ref index } => {
758 .eval_operand(len, source_info)
759 .expect("len must be const");
760 let len = match self.ecx.read_scalar(len) {
761 Ok(ScalarMaybeUndef::Scalar(Scalar::Raw {
764 other => bug!("const len not primitive: {:?}", other),
767 .eval_operand(index, source_info)
768 .expect("index must be const");
769 let index = match self.ecx.read_scalar(index) {
770 Ok(ScalarMaybeUndef::Scalar(Scalar::Raw {
773 other => bug!("const index not primitive: {:?}", other),
776 "index out of bounds: \
777 the len is {} but the index is {}",
782 // Need proper const propagator for these
786 ::rustc::lint::builtin::CONST_ERR,
792 if self.should_const_prop() {
793 if let ScalarMaybeUndef::Scalar(scalar) = value_const {
794 *cond = self.operand_from_scalar(
804 TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
805 if self.should_const_prop() {
806 if let Some(value) = self.eval_operand(&discr, source_info) {
807 if let ScalarMaybeUndef::Scalar(scalar) =
808 self.ecx.read_scalar(value).unwrap() {
809 *discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
814 //none of these have Operands to const-propagate
815 TerminatorKind::Goto { .. } |
816 TerminatorKind::Resume |
817 TerminatorKind::Abort |
818 TerminatorKind::Return |
819 TerminatorKind::Unreachable |
820 TerminatorKind::Drop { .. } |
821 TerminatorKind::DropAndReplace { .. } |
822 TerminatorKind::Yield { .. } |
823 TerminatorKind::GeneratorDrop |
824 TerminatorKind::FalseEdges { .. } |
825 TerminatorKind::FalseUnwind { .. } => { }
826 //FIXME(wesleywiser) Call does have Operands that could be const-propagated
827 TerminatorKind::Call { .. } => { }