]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/const_prop.rs
Rollup merge of #69949 - rust-lang:triagebot-ping-alias, r=Mark-Simulacrum
[rust.git] / src / librustc_mir / transform / const_prop.rs
1 //! Propagates constants for early reporting of statically known
2 //! assertion failures
3
4 use std::borrow::Cow;
5 use std::cell::Cell;
6
7 use rustc::lint;
8 use rustc::mir::interpret::{InterpResult, Scalar};
9 use rustc::mir::visit::{
10     MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
11 };
12 use rustc::mir::{
13     read_only, AggregateKind, AssertKind, BasicBlock, BinOp, Body, BodyAndCache, ClearCrossCrate,
14     Constant, Local, LocalDecl, LocalKind, Location, Operand, Place, ReadOnlyBodyAndCache, Rvalue,
15     SourceInfo, SourceScope, SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind,
16     UnOp, RETURN_PLACE,
17 };
18 use rustc::ty::layout::{
19     HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
20 };
21 use rustc::ty::subst::{InternalSubsts, Subst};
22 use rustc::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeFoldable};
23 use rustc_ast::ast::Mutability;
24 use rustc_data_structures::fx::FxHashMap;
25 use rustc_hir::def::DefKind;
26 use rustc_hir::HirId;
27 use rustc_index::vec::IndexVec;
28 use rustc_infer::traits;
29 use rustc_span::Span;
30
31 use crate::const_eval::error_to_const_error;
32 use crate::interpret::{
33     self, intern_const_alloc_recursive, AllocId, Allocation, Frame, ImmTy, Immediate, InternKind,
34     InterpCx, LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy,
35     Pointer, ScalarMaybeUndef, StackPopCleanup,
36 };
37 use crate::transform::{MirPass, MirSource};
38
39 /// The maximum number of bytes that we'll allocate space for a return value.
40 const MAX_ALLOC_LIMIT: u64 = 1024;
41
42 pub struct ConstProp;
43
44 impl<'tcx> MirPass<'tcx> for ConstProp {
45     fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
46         // will be evaluated by miri and produce its errors there
47         if source.promoted.is_some() {
48             return;
49         }
50
51         use rustc::hir::map::blocks::FnLikeNode;
52         let hir_id = tcx
53             .hir()
54             .as_local_hir_id(source.def_id())
55             .expect("Non-local call to local provider is_const_fn");
56
57         let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some();
58         let is_assoc_const = match tcx.def_kind(source.def_id()) {
59             Some(DefKind::AssocConst) => true,
60             _ => false,
61         };
62
63         // Only run const prop on functions, methods, closures and associated constants
64         if !is_fn_like && !is_assoc_const {
65             // skip anon_const/statics/consts because they'll be evaluated by miri anyway
66             trace!("ConstProp skipped for {:?}", source.def_id());
67             return;
68         }
69
70         let is_generator = tcx.type_of(source.def_id()).is_generator();
71         // FIXME(welseywiser) const prop doesn't work on generators because of query cycles
72         // computing their layout.
73         if is_generator {
74             trace!("ConstProp skipped for generator {:?}", source.def_id());
75             return;
76         }
77
78         // Check if it's even possible to satisfy the 'where' clauses
79         // for this item.
80         // This branch will never be taken for any normal function.
81         // However, it's possible to `#!feature(trivial_bounds)]` to write
82         // a function with impossible to satisfy clauses, e.g.:
83         // `fn foo() where String: Copy {}`
84         //
85         // We don't usually need to worry about this kind of case,
86         // since we would get a compilation error if the user tried
87         // to call it. However, since we can do const propagation
88         // even without any calls to the function, we need to make
89         // sure that it even makes sense to try to evaluate the body.
90         // If there are unsatisfiable where clauses, then all bets are
91         // off, and we just give up.
92         //
93         // We manually filter the predicates, skipping anything that's not
94         // "global". We are in a potentially generic context
95         // (e.g. we are evaluating a function without substituting generic
96         // parameters, so this filtering serves two purposes:
97         //
98         // 1. We skip evaluating any predicates that we would
99         // never be able prove are unsatisfiable (e.g. `<T as Foo>`
100         // 2. We avoid trying to normalize predicates involving generic
101         // parameters (e.g. `<T as Foo>::MyItem`). This can confuse
102         // the normalization code (leading to cycle errors), since
103         // it's usually never invoked in this way.
104         let predicates = tcx
105             .predicates_of(source.def_id())
106             .predicates
107             .iter()
108             .filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None })
109             .collect();
110         if !traits::normalize_and_test_predicates(
111             tcx,
112             traits::elaborate_predicates(tcx, predicates).collect(),
113         ) {
114             trace!("ConstProp skipped for {:?}: found unsatisfiable predicates", source.def_id());
115             return;
116         }
117
118         trace!("ConstProp starting for {:?}", source.def_id());
119
120         let dummy_body = &Body::new(
121             body.basic_blocks().clone(),
122             body.source_scopes.clone(),
123             body.local_decls.clone(),
124             Default::default(),
125             body.arg_count,
126             Default::default(),
127             tcx.def_span(source.def_id()),
128             Default::default(),
129             body.generator_kind,
130         );
131
132         // FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
133         // constants, instead of just checking for const-folding succeeding.
134         // That would require an uniform one-def no-mutation analysis
135         // and RPO (or recursing when needing the value of a local).
136         let mut optimization_finder =
137             ConstPropagator::new(read_only!(body), dummy_body, tcx, source);
138         optimization_finder.visit_body(body);
139
140         trace!("ConstProp done for {:?}", source.def_id());
141     }
142 }
143
144 struct ConstPropMachine;
145
146 impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
147     type MemoryKinds = !;
148     type PointerTag = ();
149     type ExtraFnVal = !;
150
151     type FrameExtra = ();
152     type MemoryExtra = ();
153     type AllocExtra = ();
154
155     type MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
156
157     const STATIC_KIND: Option<!> = None;
158
159     const CHECK_ALIGN: bool = false;
160
161     #[inline(always)]
162     fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
163         false
164     }
165
166     fn find_mir_or_eval_fn(
167         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
168         _span: Span,
169         _instance: ty::Instance<'tcx>,
170         _args: &[OpTy<'tcx>],
171         _ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
172         _unwind: Option<BasicBlock>,
173     ) -> InterpResult<'tcx, Option<&'mir Body<'tcx>>> {
174         Ok(None)
175     }
176
177     fn call_extra_fn(
178         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
179         fn_val: !,
180         _args: &[OpTy<'tcx>],
181         _ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
182         _unwind: Option<BasicBlock>,
183     ) -> InterpResult<'tcx> {
184         match fn_val {}
185     }
186
187     fn call_intrinsic(
188         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
189         _span: Span,
190         _instance: ty::Instance<'tcx>,
191         _args: &[OpTy<'tcx>],
192         _ret: Option<(PlaceTy<'tcx>, BasicBlock)>,
193         _unwind: Option<BasicBlock>,
194     ) -> InterpResult<'tcx> {
195         throw_unsup!(ConstPropUnsupported("calling intrinsics isn't supported in ConstProp"));
196     }
197
198     fn assert_panic(
199         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
200         _msg: &rustc::mir::AssertMessage<'tcx>,
201         _unwind: Option<rustc::mir::BasicBlock>,
202     ) -> InterpResult<'tcx> {
203         bug!("panics terminators are not evaluated in ConstProp");
204     }
205
206     fn ptr_to_int(_mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer) -> InterpResult<'tcx, u64> {
207         throw_unsup!(ConstPropUnsupported("ptr-to-int casts aren't supported in ConstProp"));
208     }
209
210     fn binary_ptr_op(
211         _ecx: &InterpCx<'mir, 'tcx, Self>,
212         _bin_op: BinOp,
213         _left: ImmTy<'tcx>,
214         _right: ImmTy<'tcx>,
215     ) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
216         // We can't do this because aliasing of memory can differ between const eval and llvm
217         throw_unsup!(ConstPropUnsupported(
218             "pointer arithmetic or comparisons aren't supported \
219             in ConstProp"
220         ));
221     }
222
223     #[inline(always)]
224     fn init_allocation_extra<'b>(
225         _memory_extra: &(),
226         _id: AllocId,
227         alloc: Cow<'b, Allocation>,
228         _kind: Option<MemoryKind<!>>,
229     ) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
230         // We do not use a tag so we can just cheaply forward the allocation
231         (alloc, ())
232     }
233
234     #[inline(always)]
235     fn tag_static_base_pointer(_memory_extra: &(), _id: AllocId) -> Self::PointerTag {
236         ()
237     }
238
239     fn box_alloc(
240         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
241         _dest: PlaceTy<'tcx>,
242     ) -> InterpResult<'tcx> {
243         throw_unsup!(ConstPropUnsupported("can't const prop `box` keyword"));
244     }
245
246     fn access_local(
247         _ecx: &InterpCx<'mir, 'tcx, Self>,
248         frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
249         local: Local,
250     ) -> InterpResult<'tcx, InterpOperand<Self::PointerTag>> {
251         let l = &frame.locals[local];
252
253         if l.value == LocalValue::Uninitialized {
254             throw_unsup!(ConstPropUnsupported("tried to access an uninitialized local"));
255         }
256
257         l.access()
258     }
259
260     fn before_access_static(
261         _memory_extra: &(),
262         allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
263     ) -> InterpResult<'tcx> {
264         // if the static allocation is mutable or if it has relocations (it may be legal to mutate
265         // the memory behind that in the future), then we can't const prop it
266         if allocation.mutability == Mutability::Mut || allocation.relocations().len() > 0 {
267             throw_unsup!(ConstPropUnsupported("can't eval mutable statics in ConstProp"));
268         }
269
270         Ok(())
271     }
272
273     #[inline(always)]
274     fn stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
275         Ok(())
276     }
277 }
278
279 /// Finds optimization opportunities on the MIR.
280 struct ConstPropagator<'mir, 'tcx> {
281     ecx: InterpCx<'mir, 'tcx, ConstPropMachine>,
282     tcx: TyCtxt<'tcx>,
283     can_const_prop: IndexVec<Local, ConstPropMode>,
284     param_env: ParamEnv<'tcx>,
285     // FIXME(eddyb) avoid cloning these two fields more than once,
286     // by accessing them through `ecx` instead.
287     source_scopes: IndexVec<SourceScope, SourceScopeData>,
288     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
289     ret: Option<OpTy<'tcx, ()>>,
290     // Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
291     // the last known `SourceInfo` here and just keep revisiting it.
292     source_info: Option<SourceInfo>,
293 }
294
295 impl<'mir, 'tcx> LayoutOf for ConstPropagator<'mir, 'tcx> {
296     type Ty = Ty<'tcx>;
297     type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
298
299     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
300         self.tcx.layout_of(self.param_env.and(ty))
301     }
302 }
303
304 impl<'mir, 'tcx> HasDataLayout for ConstPropagator<'mir, 'tcx> {
305     #[inline]
306     fn data_layout(&self) -> &TargetDataLayout {
307         &self.tcx.data_layout
308     }
309 }
310
311 impl<'mir, 'tcx> HasTyCtxt<'tcx> for ConstPropagator<'mir, 'tcx> {
312     #[inline]
313     fn tcx(&self) -> TyCtxt<'tcx> {
314         self.tcx
315     }
316 }
317
318 impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
319     fn new(
320         body: ReadOnlyBodyAndCache<'_, 'tcx>,
321         dummy_body: &'mir Body<'tcx>,
322         tcx: TyCtxt<'tcx>,
323         source: MirSource<'tcx>,
324     ) -> ConstPropagator<'mir, 'tcx> {
325         let def_id = source.def_id();
326         let substs = &InternalSubsts::identity_for_item(tcx, def_id);
327         let mut param_env = tcx.param_env(def_id);
328
329         // If we're evaluating inside a monomorphic function, then use `Reveal::All` because
330         // we want to see the same instances that codegen will see. This allows us to `resolve()`
331         // specializations.
332         if !substs.needs_subst() {
333             param_env = param_env.with_reveal_all();
334         }
335
336         let span = tcx.def_span(def_id);
337         let mut ecx = InterpCx::new(tcx.at(span), param_env, ConstPropMachine, ());
338         let can_const_prop = CanConstProp::check(body);
339
340         let ret = ecx
341             .layout_of(body.return_ty().subst(tcx, substs))
342             .ok()
343             // Don't bother allocating memory for ZST types which have no values
344             // or for large values.
345             .filter(|ret_layout| {
346                 !ret_layout.is_zst() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
347             })
348             .map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack));
349
350         ecx.push_stack_frame(
351             Instance::new(def_id, substs),
352             span,
353             dummy_body,
354             ret.map(Into::into),
355             StackPopCleanup::None { cleanup: false },
356         )
357         .expect("failed to push initial stack frame");
358
359         ConstPropagator {
360             ecx,
361             tcx,
362             param_env,
363             can_const_prop,
364             // FIXME(eddyb) avoid cloning these two fields more than once,
365             // by accessing them through `ecx` instead.
366             source_scopes: body.source_scopes.clone(),
367             //FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
368             local_decls: body.local_decls.clone(),
369             ret: ret.map(Into::into),
370             source_info: None,
371         }
372     }
373
374     fn get_const(&self, local: Local) -> Option<OpTy<'tcx>> {
375         if local == RETURN_PLACE {
376             // Try to read the return place as an immediate so that if it is representable as a
377             // scalar, we can handle it as such, but otherwise, just return the value as is.
378             return match self.ret.map(|ret| self.ecx.try_read_immediate(ret)) {
379                 Some(Ok(Ok(imm))) => Some(imm.into()),
380                 _ => self.ret,
381             };
382         }
383
384         self.ecx.access_local(self.ecx.frame(), local, None).ok()
385     }
386
387     fn remove_const(&mut self, local: Local) {
388         self.ecx.frame_mut().locals[local] =
389             LocalState { value: LocalValue::Uninitialized, layout: Cell::new(None) };
390     }
391
392     fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
393         match &self.source_scopes[source_info.scope].local_data {
394             ClearCrossCrate::Set(data) => Some(data.lint_root),
395             ClearCrossCrate::Clear => None,
396         }
397     }
398
399     fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
400     where
401         F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
402     {
403         let r = match f(self) {
404             Ok(val) => Some(val),
405             Err(error) => {
406                 // Some errors shouldn't come up because creating them causes
407                 // an allocation, which we should avoid. When that happens,
408                 // dedicated error variants should be introduced instead.
409                 // Only test this in debug builds though to avoid disruptions.
410                 debug_assert!(
411                     !error.kind.allocates(),
412                     "const-prop encountered allocating error: {}",
413                     error
414                 );
415                 None
416             }
417         };
418         r
419     }
420
421     fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
422         self.ecx.tcx.span = c.span;
423
424         // FIXME we need to revisit this for #67176
425         if c.needs_subst() {
426             return None;
427         }
428
429         match self.ecx.eval_const_to_op(c.literal, None) {
430             Ok(op) => Some(op),
431             Err(error) => {
432                 let err = error_to_const_error(&self.ecx, error);
433                 if let Some(lint_root) = self.lint_root(source_info) {
434                     let lint_only = match c.literal.val {
435                         // Promoteds must lint and not error as the user didn't ask for them
436                         ConstKind::Unevaluated(_, _, Some(_)) => true,
437                         // Out of backwards compatibility we cannot report hard errors in unused
438                         // generic functions using associated constants of the generic parameters.
439                         _ => c.literal.needs_subst(),
440                     };
441                     if lint_only {
442                         // Out of backwards compatibility we cannot report hard errors in unused
443                         // generic functions using associated constants of the generic parameters.
444                         err.report_as_lint(
445                             self.ecx.tcx,
446                             "erroneous constant used",
447                             lint_root,
448                             Some(c.span),
449                         );
450                     } else {
451                         err.report_as_error(self.ecx.tcx, "erroneous constant used");
452                     }
453                 } else {
454                     err.report_as_error(self.ecx.tcx, "erroneous constant used");
455                 }
456                 None
457             }
458         }
459     }
460
461     fn eval_place(&mut self, place: &Place<'tcx>) -> Option<OpTy<'tcx>> {
462         trace!("eval_place(place={:?})", place);
463         self.use_ecx(|this| this.ecx.eval_place_to_op(place, None))
464     }
465
466     fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
467         match *op {
468             Operand::Constant(ref c) => self.eval_constant(c, source_info),
469             Operand::Move(ref place) | Operand::Copy(ref place) => self.eval_place(place),
470         }
471     }
472
473     fn report_assert_as_lint(
474         &self,
475         lint: &'static lint::Lint,
476         source_info: SourceInfo,
477         message: &'static str,
478         panic: AssertKind<u64>,
479     ) -> Option<()> {
480         let lint_root = self.lint_root(source_info)?;
481         self.tcx.struct_span_lint_hir(lint, lint_root, source_info.span, |lint| {
482             let mut err = lint.build(message);
483             err.span_label(source_info.span, format!("{:?}", panic));
484             err.emit()
485         });
486         return None;
487     }
488
489     fn check_unary_op(
490         &mut self,
491         op: UnOp,
492         arg: &Operand<'tcx>,
493         source_info: SourceInfo,
494     ) -> Option<()> {
495         if self.use_ecx(|this| {
496             let val = this.ecx.read_immediate(this.ecx.eval_operand(arg, None)?)?;
497             let (_res, overflow, _ty) = this.ecx.overflowing_unary_op(op, val)?;
498             Ok(overflow)
499         })? {
500             // `AssertKind` only has an `OverflowNeg` variant, so make sure that is
501             // appropriate to use.
502             assert_eq!(op, UnOp::Neg, "Neg is the only UnOp that can overflow");
503             self.report_assert_as_lint(
504                 lint::builtin::ARITHMETIC_OVERFLOW,
505                 source_info,
506                 "this arithmetic operation will overflow",
507                 AssertKind::OverflowNeg,
508             )?;
509         }
510
511         Some(())
512     }
513
514     fn check_binary_op(
515         &mut self,
516         op: BinOp,
517         left: &Operand<'tcx>,
518         right: &Operand<'tcx>,
519         source_info: SourceInfo,
520     ) -> Option<()> {
521         let r =
522             self.use_ecx(|this| this.ecx.read_immediate(this.ecx.eval_operand(right, None)?))?;
523         // Check for exceeding shifts *even if* we cannot evaluate the LHS.
524         if op == BinOp::Shr || op == BinOp::Shl {
525             // We need the type of the LHS. We cannot use `place_layout` as that is the type
526             // of the result, which for checked binops is not the same!
527             let left_ty = left.ty(&self.local_decls, self.tcx);
528             let left_size_bits = self.ecx.layout_of(left_ty).ok()?.size.bits();
529             let right_size = r.layout.size;
530             let r_bits = r.to_scalar().ok();
531             // This is basically `force_bits`.
532             let r_bits = r_bits.and_then(|r| r.to_bits_or_ptr(right_size, &self.tcx).ok());
533             if r_bits.map_or(false, |b| b >= left_size_bits as u128) {
534                 self.report_assert_as_lint(
535                     lint::builtin::ARITHMETIC_OVERFLOW,
536                     source_info,
537                     "this arithmetic operation will overflow",
538                     AssertKind::Overflow(op),
539                 )?;
540             }
541         }
542
543         // The remaining operators are handled through `overflowing_binary_op`.
544         if self.use_ecx(|this| {
545             let l = this.ecx.read_immediate(this.ecx.eval_operand(left, None)?)?;
546             let (_res, overflow, _ty) = this.ecx.overflowing_binary_op(op, l, r)?;
547             Ok(overflow)
548         })? {
549             self.report_assert_as_lint(
550                 lint::builtin::ARITHMETIC_OVERFLOW,
551                 source_info,
552                 "this arithmetic operation will overflow",
553                 AssertKind::Overflow(op),
554             )?;
555         }
556
557         Some(())
558     }
559
560     fn const_prop(
561         &mut self,
562         rvalue: &Rvalue<'tcx>,
563         place_layout: TyLayout<'tcx>,
564         source_info: SourceInfo,
565         place: &Place<'tcx>,
566     ) -> Option<()> {
567         // #66397: Don't try to eval into large places as that can cause an OOM
568         if place_layout.size >= Size::from_bytes(MAX_ALLOC_LIMIT) {
569             return None;
570         }
571
572         // FIXME we need to revisit this for #67176
573         if rvalue.needs_subst() {
574             return None;
575         }
576
577         // Perform any special handling for specific Rvalue types.
578         // Generally, checks here fall into one of two categories:
579         //   1. Additional checking to provide useful lints to the user
580         //        - In this case, we will do some validation and then fall through to the
581         //          end of the function which evals the assignment.
582         //   2. Working around bugs in other parts of the compiler
583         //        - In this case, we'll return `None` from this function to stop evaluation.
584         match rvalue {
585             // Additional checking: give lints to the user if an overflow would occur.
586             // We do this here and not in the `Assert` terminator as that terminator is
587             // only sometimes emitted (overflow checks can be disabled), but we want to always
588             // lint.
589             Rvalue::UnaryOp(op, arg) => {
590                 trace!("checking UnaryOp(op = {:?}, arg = {:?})", op, arg);
591                 self.check_unary_op(*op, arg, source_info)?;
592             }
593             Rvalue::BinaryOp(op, left, right) => {
594                 trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
595                 self.check_binary_op(*op, left, right, source_info)?;
596             }
597             Rvalue::CheckedBinaryOp(op, left, right) => {
598                 trace!(
599                     "checking CheckedBinaryOp(op = {:?}, left = {:?}, right = {:?})",
600                     op,
601                     left,
602                     right
603                 );
604                 self.check_binary_op(*op, left, right, source_info)?;
605             }
606
607             // Do not try creating references (#67862)
608             Rvalue::Ref(_, _, place_ref) => {
609                 trace!("skipping Ref({:?})", place_ref);
610
611                 return None;
612             }
613
614             _ => {}
615         }
616
617         self.use_ecx(|this| {
618             trace!("calling eval_rvalue_into_place(rvalue = {:?}, place = {:?})", rvalue, place);
619             this.ecx.eval_rvalue_into_place(rvalue, place)?;
620             Ok(())
621         })
622     }
623
624     fn operand_from_scalar(&self, scalar: Scalar, ty: Ty<'tcx>, span: Span) -> Operand<'tcx> {
625         Operand::Constant(Box::new(Constant {
626             span,
627             user_ty: None,
628             literal: self.tcx.mk_const(*ty::Const::from_scalar(self.tcx, scalar, ty)),
629         }))
630     }
631
632     fn replace_with_const(
633         &mut self,
634         rval: &mut Rvalue<'tcx>,
635         value: OpTy<'tcx>,
636         source_info: SourceInfo,
637     ) {
638         trace!("attepting to replace {:?} with {:?}", rval, value);
639         if let Err(e) = self.ecx.const_validate_operand(
640             value,
641             vec![],
642             // FIXME: is ref tracking too expensive?
643             &mut interpret::RefTracking::empty(),
644             /*may_ref_to_static*/ true,
645         ) {
646             trace!("validation error, attempt failed: {:?}", e);
647             return;
648         }
649
650         // FIXME> figure out what to do when try_read_immediate fails
651         let imm = self.use_ecx(|this| this.ecx.try_read_immediate(value));
652
653         if let Some(Ok(imm)) = imm {
654             match *imm {
655                 interpret::Immediate::Scalar(ScalarMaybeUndef::Scalar(scalar)) => {
656                     *rval = Rvalue::Use(self.operand_from_scalar(
657                         scalar,
658                         value.layout.ty,
659                         source_info.span,
660                     ));
661                 }
662                 Immediate::ScalarPair(
663                     ScalarMaybeUndef::Scalar(one),
664                     ScalarMaybeUndef::Scalar(two),
665                 ) => {
666                     // Found a value represented as a pair. For now only do cont-prop if type of
667                     // Rvalue is also a pair with two scalars. The more general case is more
668                     // complicated to implement so we'll do it later.
669                     let ty = &value.layout.ty.kind;
670                     // Only do it for tuples
671                     if let ty::Tuple(substs) = ty {
672                         // Only do it if tuple is also a pair with two scalars
673                         if substs.len() == 2 {
674                             let opt_ty1_ty2 = self.use_ecx(|this| {
675                                 let ty1 = substs[0].expect_ty();
676                                 let ty2 = substs[1].expect_ty();
677                                 let ty_is_scalar = |ty| {
678                                     this.ecx.layout_of(ty).ok().map(|ty| ty.details.abi.is_scalar())
679                                         == Some(true)
680                                 };
681                                 if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
682                                     Ok(Some((ty1, ty2)))
683                                 } else {
684                                     Ok(None)
685                                 }
686                             });
687
688                             if let Some(Some((ty1, ty2))) = opt_ty1_ty2 {
689                                 *rval = Rvalue::Aggregate(
690                                     Box::new(AggregateKind::Tuple),
691                                     vec![
692                                         self.operand_from_scalar(one, ty1, source_info.span),
693                                         self.operand_from_scalar(two, ty2, source_info.span),
694                                     ],
695                                 );
696                             }
697                         }
698                     }
699                 }
700                 _ => {}
701             }
702         }
703     }
704
705     fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
706         let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
707
708         if mir_opt_level == 0 {
709             return false;
710         }
711
712         match *op {
713             interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Scalar(s))) => {
714                 s.is_bits()
715             }
716             interpret::Operand::Immediate(Immediate::ScalarPair(
717                 ScalarMaybeUndef::Scalar(l),
718                 ScalarMaybeUndef::Scalar(r),
719             )) => l.is_bits() && r.is_bits(),
720             interpret::Operand::Indirect(_) if mir_opt_level >= 2 => {
721                 let mplace = op.assert_mem_place(&self.ecx);
722                 intern_const_alloc_recursive(&mut self.ecx, InternKind::ConstProp, mplace, false)
723                     .expect("failed to intern alloc");
724                 true
725             }
726             _ => false,
727         }
728     }
729 }
730
731 /// The mode that `ConstProp` is allowed to run in for a given `Local`.
732 #[derive(Clone, Copy, Debug, PartialEq)]
733 enum ConstPropMode {
734     /// The `Local` can be propagated into and reads of this `Local` can also be propagated.
735     FullConstProp,
736     /// The `Local` can be propagated into but reads cannot be propagated.
737     OnlyPropagateInto,
738     /// No propagation is allowed at all.
739     NoPropagation,
740 }
741
742 struct CanConstProp {
743     can_const_prop: IndexVec<Local, ConstPropMode>,
744     // false at the beginning, once set, there are not allowed to be any more assignments
745     found_assignment: IndexVec<Local, bool>,
746 }
747
748 impl CanConstProp {
749     /// returns true if `local` can be propagated
750     fn check(body: ReadOnlyBodyAndCache<'_, '_>) -> IndexVec<Local, ConstPropMode> {
751         let mut cpv = CanConstProp {
752             can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
753             found_assignment: IndexVec::from_elem(false, &body.local_decls),
754         };
755         for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
756             // cannot use args at all
757             // cannot use locals because if x < y { y - x } else { x - y } would
758             //        lint for x != y
759             // FIXME(oli-obk): lint variables until they are used in a condition
760             // FIXME(oli-obk): lint if return value is constant
761             let local_kind = body.local_kind(local);
762
763             if local_kind == LocalKind::Arg || local_kind == LocalKind::Var {
764                 *val = ConstPropMode::OnlyPropagateInto;
765                 trace!("local {:?} can't be const propagated because it's not a temporary", local);
766             }
767         }
768         cpv.visit_body(body);
769         cpv.can_const_prop
770     }
771 }
772
773 impl<'tcx> Visitor<'tcx> for CanConstProp {
774     fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) {
775         use rustc::mir::visit::PlaceContext::*;
776         match context {
777             // Constants must have at most one write
778             // FIXME(oli-obk): we could be more powerful here, if the multiple writes
779             // only occur in independent execution paths
780             MutatingUse(MutatingUseContext::Store) => {
781                 if self.found_assignment[local] {
782                     trace!("local {:?} can't be propagated because of multiple assignments", local);
783                     self.can_const_prop[local] = ConstPropMode::NoPropagation;
784                 } else {
785                     self.found_assignment[local] = true
786                 }
787             }
788             // Reading constants is allowed an arbitrary number of times
789             NonMutatingUse(NonMutatingUseContext::Copy)
790             | NonMutatingUse(NonMutatingUseContext::Move)
791             | NonMutatingUse(NonMutatingUseContext::Inspect)
792             | NonMutatingUse(NonMutatingUseContext::Projection)
793             | MutatingUse(MutatingUseContext::Projection)
794             | NonUse(_) => {}
795             _ => {
796                 trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
797                 self.can_const_prop[local] = ConstPropMode::NoPropagation;
798             }
799         }
800     }
801 }
802
803 impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
804     fn tcx(&self) -> TyCtxt<'tcx> {
805         self.tcx
806     }
807
808     fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location) {
809         trace!("visit_constant: {:?}", constant);
810         self.super_constant(constant, location);
811         self.eval_constant(constant, self.source_info.unwrap());
812     }
813
814     fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
815         trace!("visit_statement: {:?}", statement);
816         let source_info = statement.source_info;
817         self.source_info = Some(source_info);
818         if let StatementKind::Assign(box (ref place, ref mut rval)) = statement.kind {
819             let place_ty: Ty<'tcx> = place.ty(&self.local_decls, self.tcx).ty;
820             if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
821                 if let Some(local) = place.as_local() {
822                     let can_const_prop = self.can_const_prop[local];
823                     if let Some(()) = self.const_prop(rval, place_layout, source_info, place) {
824                         if can_const_prop == ConstPropMode::FullConstProp
825                             || can_const_prop == ConstPropMode::OnlyPropagateInto
826                         {
827                             if let Some(value) = self.get_const(local) {
828                                 if self.should_const_prop(value) {
829                                     trace!("replacing {:?} with {:?}", rval, value);
830                                     self.replace_with_const(rval, value, statement.source_info);
831
832                                     if can_const_prop == ConstPropMode::FullConstProp {
833                                         trace!("propagated into {:?}", local);
834                                     }
835                                 }
836                             }
837                         }
838                     }
839                     if self.can_const_prop[local] != ConstPropMode::FullConstProp {
840                         trace!("can't propagate into {:?}", local);
841                         if local != RETURN_PLACE {
842                             self.remove_const(local);
843                         }
844                     }
845                 }
846             }
847         } else {
848             match statement.kind {
849                 StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
850                     let frame = self.ecx.frame_mut();
851                     frame.locals[local].value =
852                         if let StatementKind::StorageLive(_) = statement.kind {
853                             LocalValue::Uninitialized
854                         } else {
855                             LocalValue::Dead
856                         };
857                 }
858                 _ => {}
859             }
860         }
861
862         self.super_statement(statement, location);
863     }
864
865     fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
866         let source_info = terminator.source_info;
867         self.source_info = Some(source_info);
868         self.super_terminator(terminator, location);
869         match &mut terminator.kind {
870             TerminatorKind::Assert { expected, ref msg, ref mut cond, .. } => {
871                 if let Some(value) = self.eval_operand(&cond, source_info) {
872                     trace!("assertion on {:?} should be {:?}", value, expected);
873                     let expected = ScalarMaybeUndef::from(Scalar::from_bool(*expected));
874                     let value_const = self.ecx.read_scalar(value).unwrap();
875                     if expected != value_const {
876                         // poison all places this operand references so that further code
877                         // doesn't use the invalid value
878                         match cond {
879                             Operand::Move(ref place) | Operand::Copy(ref place) => {
880                                 self.remove_const(place.local);
881                             }
882                             Operand::Constant(_) => {}
883                         }
884                         let msg = match msg {
885                             AssertKind::DivisionByZero => AssertKind::DivisionByZero,
886                             AssertKind::RemainderByZero => AssertKind::RemainderByZero,
887                             AssertKind::BoundsCheck { ref len, ref index } => {
888                                 let len =
889                                     self.eval_operand(len, source_info).expect("len must be const");
890                                 let len = self
891                                     .ecx
892                                     .read_scalar(len)
893                                     .unwrap()
894                                     .to_machine_usize(&self.tcx)
895                                     .unwrap();
896                                 let index = self
897                                     .eval_operand(index, source_info)
898                                     .expect("index must be const");
899                                 let index = self
900                                     .ecx
901                                     .read_scalar(index)
902                                     .unwrap()
903                                     .to_machine_usize(&self.tcx)
904                                     .unwrap();
905                                 AssertKind::BoundsCheck { len, index }
906                             }
907                             // Overflow is are already covered by checks on the binary operators.
908                             AssertKind::Overflow(_) | AssertKind::OverflowNeg => return,
909                             // Need proper const propagator for these.
910                             _ => return,
911                         };
912                         self.report_assert_as_lint(
913                             lint::builtin::UNCONDITIONAL_PANIC,
914                             source_info,
915                             "this operation will panic at runtime",
916                             msg,
917                         );
918                     } else {
919                         if self.should_const_prop(value) {
920                             if let ScalarMaybeUndef::Scalar(scalar) = value_const {
921                                 *cond = self.operand_from_scalar(
922                                     scalar,
923                                     self.tcx.types.bool,
924                                     source_info.span,
925                                 );
926                             }
927                         }
928                     }
929                 }
930             }
931             TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
932                 if let Some(value) = self.eval_operand(&discr, source_info) {
933                     if self.should_const_prop(value) {
934                         if let ScalarMaybeUndef::Scalar(scalar) =
935                             self.ecx.read_scalar(value).unwrap()
936                         {
937                             *discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
938                         }
939                     }
940                 }
941             }
942             //none of these have Operands to const-propagate
943             TerminatorKind::Goto { .. }
944             | TerminatorKind::Resume
945             | TerminatorKind::Abort
946             | TerminatorKind::Return
947             | TerminatorKind::Unreachable
948             | TerminatorKind::Drop { .. }
949             | TerminatorKind::DropAndReplace { .. }
950             | TerminatorKind::Yield { .. }
951             | TerminatorKind::GeneratorDrop
952             | TerminatorKind::FalseEdges { .. }
953             | TerminatorKind::FalseUnwind { .. } => {}
954             //FIXME(wesleywiser) Call does have Operands that could be const-propagated
955             TerminatorKind::Call { .. } => {}
956         }
957     }
958 }