rustc_mir_transform/
dataflow_const_prop.rs

1//! A constant propagation optimization pass based on dataflow analysis.
2//!
3//! Currently, this pass only propagates scalar values.
4
5use std::assert_matches::assert_matches;
6use std::cell::RefCell;
7use std::fmt::Formatter;
8
9use rustc_abi::{BackendRepr, FIRST_VARIANT, FieldIdx, Size, VariantIdx};
10use rustc_const_eval::const_eval::{DummyMachine, throw_machine_stop_str};
11use rustc_const_eval::interpret::{
12    ImmTy, Immediate, InterpCx, OpTy, PlaceTy, Projectable, interp_ok,
13};
14use rustc_data_structures::fx::FxHashMap;
15use rustc_hir::def::DefKind;
16use rustc_middle::bug;
17use rustc_middle::mir::interpret::{InterpResult, Scalar};
18use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
19use rustc_middle::mir::*;
20use rustc_middle::ty::{self, Ty, TyCtxt};
21use rustc_mir_dataflow::fmt::DebugWithContext;
22use rustc_mir_dataflow::lattice::{FlatSet, HasBottom};
23use rustc_mir_dataflow::value_analysis::{
24    Map, PlaceIndex, State, TrackElem, ValueOrPlace, debug_with_context,
25};
26use rustc_mir_dataflow::{Analysis, ResultsVisitor, visit_reachable_results};
27use rustc_span::DUMMY_SP;
28use tracing::{debug, debug_span, instrument};
29
30// These constants are somewhat random guesses and have not been optimized.
31// If `tcx.sess.mir_opt_level() >= 4`, we ignore the limits (this can become very expensive).
32const BLOCK_LIMIT: usize = 100;
33const PLACE_LIMIT: usize = 100;
34
35pub(super) struct DataflowConstProp;
36
37impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
38    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
39        sess.mir_opt_level() >= 3
40    }
41
42    #[instrument(skip_all level = "debug")]
43    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
44        debug!(def_id = ?body.source.def_id());
45        if tcx.sess.mir_opt_level() < 4 && body.basic_blocks.len() > BLOCK_LIMIT {
46            debug!("aborted dataflow const prop due too many basic blocks");
47            return;
48        }
49
50        // We want to have a somewhat linear runtime w.r.t. the number of statements/terminators.
51        // Let's call this number `n`. Dataflow analysis has `O(h*n)` transfer function
52        // applications, where `h` is the height of the lattice. Because the height of our lattice
53        // is linear w.r.t. the number of tracked places, this is `O(tracked_places * n)`. However,
54        // because every transfer function application could traverse the whole map, this becomes
55        // `O(num_nodes * tracked_places * n)` in terms of time complexity. Since the number of
56        // map nodes is strongly correlated to the number of tracked places, this becomes more or
57        // less `O(n)` if we place a constant limit on the number of tracked places.
58        let place_limit = if tcx.sess.mir_opt_level() < 4 { Some(PLACE_LIMIT) } else { None };
59
60        // Decide which places to track during the analysis.
61        let map = Map::new(tcx, body, place_limit);
62
63        // Perform the actual dataflow analysis.
64        let const_ = debug_span!("analyze")
65            .in_scope(|| ConstAnalysis::new(tcx, body, map).iterate_to_fixpoint(tcx, body, None));
66
67        // Collect results and patch the body afterwards.
68        let mut visitor = Collector::new(tcx, &body.local_decls);
69        debug_span!("collect").in_scope(|| visit_reachable_results(body, &const_, &mut visitor));
70        let mut patch = visitor.patch;
71        debug_span!("patch").in_scope(|| patch.visit_body_preserves_cfg(body));
72    }
73
74    fn is_required(&self) -> bool {
75        false
76    }
77}
78
79// Note: Currently, places that have their reference taken cannot be tracked. Although this would
80// be possible, it has to rely on some aliasing model, which we are not ready to commit to yet.
81// Because of that, we can assume that the only way to change the value behind a tracked place is
82// by direct assignment.
83struct ConstAnalysis<'a, 'tcx> {
84    map: Map<'tcx>,
85    tcx: TyCtxt<'tcx>,
86    local_decls: &'a LocalDecls<'tcx>,
87    ecx: RefCell<InterpCx<'tcx, DummyMachine>>,
88    typing_env: ty::TypingEnv<'tcx>,
89}
90
91impl<'tcx> Analysis<'tcx> for ConstAnalysis<'_, 'tcx> {
92    type Domain = State<FlatSet<Scalar>>;
93
94    const NAME: &'static str = "ConstAnalysis";
95
96    // The bottom state denotes uninitialized memory. Because we are only doing a sound
97    // approximation of the actual execution, we can also use this state for places where access
98    // would be UB.
99    fn bottom_value(&self, _body: &Body<'tcx>) -> Self::Domain {
100        State::Unreachable
101    }
102
103    fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
104        // The initial state maps all tracked places of argument projections to ⊤ and the rest to ⊥.
105        assert_matches!(state, State::Unreachable);
106        *state = State::new_reachable();
107        for arg in body.args_iter() {
108            state.flood(PlaceRef { local: arg, projection: &[] }, &self.map);
109        }
110    }
111
112    fn apply_primary_statement_effect(
113        &self,
114        state: &mut Self::Domain,
115        statement: &Statement<'tcx>,
116        _location: Location,
117    ) {
118        if state.is_reachable() {
119            self.handle_statement(statement, state);
120        }
121    }
122
123    fn apply_primary_terminator_effect<'mir>(
124        &self,
125        state: &mut Self::Domain,
126        terminator: &'mir Terminator<'tcx>,
127        _location: Location,
128    ) -> TerminatorEdges<'mir, 'tcx> {
129        if state.is_reachable() {
130            self.handle_terminator(terminator, state)
131        } else {
132            TerminatorEdges::None
133        }
134    }
135
136    fn apply_call_return_effect(
137        &self,
138        state: &mut Self::Domain,
139        _block: BasicBlock,
140        return_places: CallReturnPlaces<'_, 'tcx>,
141    ) {
142        if state.is_reachable() {
143            self.handle_call_return(return_places, state)
144        }
145    }
146}
147
148impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
149    fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: Map<'tcx>) -> Self {
150        let typing_env = body.typing_env(tcx);
151        Self {
152            map,
153            tcx,
154            local_decls: &body.local_decls,
155            ecx: RefCell::new(InterpCx::new(tcx, DUMMY_SP, typing_env, DummyMachine)),
156            typing_env,
157        }
158    }
159
160    fn handle_statement(&self, statement: &Statement<'tcx>, state: &mut State<FlatSet<Scalar>>) {
161        match &statement.kind {
162            StatementKind::Assign(box (place, rvalue)) => {
163                self.handle_assign(*place, rvalue, state);
164            }
165            StatementKind::SetDiscriminant { box place, variant_index } => {
166                self.handle_set_discriminant(*place, *variant_index, state);
167            }
168            StatementKind::Intrinsic(box intrinsic) => {
169                self.handle_intrinsic(intrinsic);
170            }
171            StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
172                // StorageLive leaves the local in an uninitialized state.
173                // StorageDead makes it UB to access the local afterwards.
174                state.flood_with(
175                    Place::from(*local).as_ref(),
176                    &self.map,
177                    FlatSet::<Scalar>::BOTTOM,
178                );
179            }
180            StatementKind::Retag(..) => {
181                // We don't track references.
182            }
183            StatementKind::ConstEvalCounter
184            | StatementKind::Nop
185            | StatementKind::FakeRead(..)
186            | StatementKind::PlaceMention(..)
187            | StatementKind::Coverage(..)
188            | StatementKind::BackwardIncompatibleDropHint { .. }
189            | StatementKind::AscribeUserType(..) => {}
190        }
191    }
192
193    fn handle_intrinsic(&self, intrinsic: &NonDivergingIntrinsic<'tcx>) {
194        match intrinsic {
195            NonDivergingIntrinsic::Assume(..) => {
196                // Could use this, but ignoring it is sound.
197            }
198            NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
199                dst: _,
200                src: _,
201                count: _,
202            }) => {
203                // This statement represents `*dst = *src`, `count` times.
204            }
205        }
206    }
207
208    fn handle_operand(
209        &self,
210        operand: &Operand<'tcx>,
211        state: &mut State<FlatSet<Scalar>>,
212    ) -> ValueOrPlace<FlatSet<Scalar>> {
213        match operand {
214            Operand::Constant(box constant) => {
215                ValueOrPlace::Value(self.handle_constant(constant, state))
216            }
217            Operand::Copy(place) | Operand::Move(place) => {
218                // On move, we would ideally flood the place with bottom. But with the current
219                // framework this is not possible (similar to `InterpCx::eval_operand`).
220                self.map.find(place.as_ref()).map(ValueOrPlace::Place).unwrap_or(ValueOrPlace::TOP)
221            }
222        }
223    }
224
225    /// The effect of a successful function call return should not be
226    /// applied here, see [`Analysis::apply_primary_terminator_effect`].
227    fn handle_terminator<'mir>(
228        &self,
229        terminator: &'mir Terminator<'tcx>,
230        state: &mut State<FlatSet<Scalar>>,
231    ) -> TerminatorEdges<'mir, 'tcx> {
232        match &terminator.kind {
233            TerminatorKind::Call { .. } | TerminatorKind::InlineAsm { .. } => {
234                // Effect is applied by `handle_call_return`.
235            }
236            TerminatorKind::Drop { place, .. } => {
237                state.flood_with(place.as_ref(), &self.map, FlatSet::<Scalar>::BOTTOM);
238            }
239            TerminatorKind::Yield { .. } => {
240                // They would have an effect, but are not allowed in this phase.
241                bug!("encountered disallowed terminator");
242            }
243            TerminatorKind::SwitchInt { discr, targets } => {
244                return self.handle_switch_int(discr, targets, state);
245            }
246            TerminatorKind::TailCall { .. } => {
247                // FIXME(explicit_tail_calls): determine if we need to do something here (probably
248                // not)
249            }
250            TerminatorKind::Goto { .. }
251            | TerminatorKind::UnwindResume
252            | TerminatorKind::UnwindTerminate(_)
253            | TerminatorKind::Return
254            | TerminatorKind::Unreachable
255            | TerminatorKind::Assert { .. }
256            | TerminatorKind::CoroutineDrop
257            | TerminatorKind::FalseEdge { .. }
258            | TerminatorKind::FalseUnwind { .. } => {
259                // These terminators have no effect on the analysis.
260            }
261        }
262        terminator.edges()
263    }
264
265    fn handle_call_return(
266        &self,
267        return_places: CallReturnPlaces<'_, 'tcx>,
268        state: &mut State<FlatSet<Scalar>>,
269    ) {
270        return_places.for_each(|place| {
271            state.flood(place.as_ref(), &self.map);
272        })
273    }
274
275    fn handle_set_discriminant(
276        &self,
277        place: Place<'tcx>,
278        variant_index: VariantIdx,
279        state: &mut State<FlatSet<Scalar>>,
280    ) {
281        state.flood_discr(place.as_ref(), &self.map);
282        if self.map.find_discr(place.as_ref()).is_some() {
283            let enum_ty = place.ty(self.local_decls, self.tcx).ty;
284            if let Some(discr) = self.eval_discriminant(enum_ty, variant_index) {
285                state.assign_discr(
286                    place.as_ref(),
287                    ValueOrPlace::Value(FlatSet::Elem(discr)),
288                    &self.map,
289                );
290            }
291        }
292    }
293
294    fn handle_assign(
295        &self,
296        target: Place<'tcx>,
297        rvalue: &Rvalue<'tcx>,
298        state: &mut State<FlatSet<Scalar>>,
299    ) {
300        match rvalue {
301            Rvalue::Use(operand) => {
302                state.flood(target.as_ref(), &self.map);
303                if let Some(target) = self.map.find(target.as_ref()) {
304                    self.assign_operand(state, target, operand);
305                }
306            }
307            Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"),
308            Rvalue::Aggregate(kind, operands) => {
309                // If we assign `target = Enum::Variant#0(operand)`,
310                // we must make sure that all `target as Variant#i` are `Top`.
311                state.flood(target.as_ref(), &self.map);
312
313                let Some(target_idx) = self.map.find(target.as_ref()) else { return };
314
315                let (variant_target, variant_index) = match **kind {
316                    AggregateKind::Tuple | AggregateKind::Closure(..) => (Some(target_idx), None),
317                    AggregateKind::Adt(def_id, variant_index, ..) => {
318                        match self.tcx.def_kind(def_id) {
319                            DefKind::Struct => (Some(target_idx), None),
320                            DefKind::Enum => (
321                                self.map.apply(target_idx, TrackElem::Variant(variant_index)),
322                                Some(variant_index),
323                            ),
324                            _ => return,
325                        }
326                    }
327                    _ => return,
328                };
329                if let Some(variant_target_idx) = variant_target {
330                    for (field_index, operand) in operands.iter_enumerated() {
331                        if let Some(field) =
332                            self.map.apply(variant_target_idx, TrackElem::Field(field_index))
333                        {
334                            self.assign_operand(state, field, operand);
335                        }
336                    }
337                }
338                if let Some(variant_index) = variant_index
339                    && let Some(discr_idx) = self.map.apply(target_idx, TrackElem::Discriminant)
340                {
341                    // We are assigning the discriminant as part of an aggregate.
342                    // This discriminant can only alias a variant field's value if the operand
343                    // had an invalid value for that type.
344                    // Using invalid values is UB, so we are allowed to perform the assignment
345                    // without extra flooding.
346                    let enum_ty = target.ty(self.local_decls, self.tcx).ty;
347                    if let Some(discr_val) = self.eval_discriminant(enum_ty, variant_index) {
348                        state.insert_value_idx(discr_idx, FlatSet::Elem(discr_val), &self.map);
349                    }
350                }
351            }
352            Rvalue::BinaryOp(op, box (left, right)) if op.is_overflowing() => {
353                // Flood everything now, so we can use `insert_value_idx` directly later.
354                state.flood(target.as_ref(), &self.map);
355
356                let Some(target) = self.map.find(target.as_ref()) else { return };
357
358                let value_target = self.map.apply(target, TrackElem::Field(0_u32.into()));
359                let overflow_target = self.map.apply(target, TrackElem::Field(1_u32.into()));
360
361                if value_target.is_some() || overflow_target.is_some() {
362                    let (val, overflow) = self.binary_op(state, *op, left, right);
363
364                    if let Some(value_target) = value_target {
365                        // We have flooded `target` earlier.
366                        state.insert_value_idx(value_target, val, &self.map);
367                    }
368                    if let Some(overflow_target) = overflow_target {
369                        // We have flooded `target` earlier.
370                        state.insert_value_idx(overflow_target, overflow, &self.map);
371                    }
372                }
373            }
374            Rvalue::Cast(
375                CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize, _),
376                operand,
377                _,
378            ) => {
379                let pointer = self.handle_operand(operand, state);
380                state.assign(target.as_ref(), pointer, &self.map);
381
382                if let Some(target_len) = self.map.find_len(target.as_ref())
383                    && let operand_ty = operand.ty(self.local_decls, self.tcx)
384                    && let Some(operand_ty) = operand_ty.builtin_deref(true)
385                    && let ty::Array(_, len) = operand_ty.kind()
386                    && let Some(len) = Const::Ty(self.tcx.types.usize, *len)
387                        .try_eval_scalar_int(self.tcx, self.typing_env)
388                {
389                    state.insert_value_idx(target_len, FlatSet::Elem(len.into()), &self.map);
390                }
391            }
392            _ => {
393                let result = self.handle_rvalue(rvalue, state);
394                state.assign(target.as_ref(), result, &self.map);
395            }
396        }
397    }
398
399    fn handle_rvalue(
400        &self,
401        rvalue: &Rvalue<'tcx>,
402        state: &mut State<FlatSet<Scalar>>,
403    ) -> ValueOrPlace<FlatSet<Scalar>> {
404        let val = match rvalue {
405            Rvalue::Cast(CastKind::IntToInt | CastKind::IntToFloat, operand, ty) => {
406                let Ok(layout) = self.tcx.layout_of(self.typing_env.as_query_input(*ty)) else {
407                    return ValueOrPlace::Value(FlatSet::Top);
408                };
409                match self.eval_operand(operand, state) {
410                    FlatSet::Elem(op) => self
411                        .ecx
412                        .borrow()
413                        .int_to_int_or_float(&op, layout)
414                        .discard_err()
415                        .map_or(FlatSet::Top, |result| self.wrap_immediate(*result)),
416                    FlatSet::Bottom => FlatSet::Bottom,
417                    FlatSet::Top => FlatSet::Top,
418                }
419            }
420            Rvalue::Cast(CastKind::FloatToInt | CastKind::FloatToFloat, operand, ty) => {
421                let Ok(layout) = self.tcx.layout_of(self.typing_env.as_query_input(*ty)) else {
422                    return ValueOrPlace::Value(FlatSet::Top);
423                };
424                match self.eval_operand(operand, state) {
425                    FlatSet::Elem(op) => self
426                        .ecx
427                        .borrow()
428                        .float_to_float_or_int(&op, layout)
429                        .discard_err()
430                        .map_or(FlatSet::Top, |result| self.wrap_immediate(*result)),
431                    FlatSet::Bottom => FlatSet::Bottom,
432                    FlatSet::Top => FlatSet::Top,
433                }
434            }
435            Rvalue::Cast(CastKind::Transmute | CastKind::Subtype, operand, _) => {
436                match self.eval_operand(operand, state) {
437                    FlatSet::Elem(op) => self.wrap_immediate(*op),
438                    FlatSet::Bottom => FlatSet::Bottom,
439                    FlatSet::Top => FlatSet::Top,
440                }
441            }
442            Rvalue::BinaryOp(op, box (left, right)) if !op.is_overflowing() => {
443                // Overflows must be ignored here.
444                // The overflowing operators are handled in `handle_assign`.
445                let (val, _overflow) = self.binary_op(state, *op, left, right);
446                val
447            }
448            Rvalue::UnaryOp(op, operand) => {
449                if let UnOp::PtrMetadata = op
450                    && let Some(place) = operand.place()
451                    && let Some(len) = self.map.find_len(place.as_ref())
452                {
453                    return ValueOrPlace::Place(len);
454                }
455                match self.eval_operand(operand, state) {
456                    FlatSet::Elem(value) => self
457                        .ecx
458                        .borrow()
459                        .unary_op(*op, &value)
460                        .discard_err()
461                        .map_or(FlatSet::Top, |val| self.wrap_immediate(*val)),
462                    FlatSet::Bottom => FlatSet::Bottom,
463                    FlatSet::Top => FlatSet::Top,
464                }
465            }
466            Rvalue::NullaryOp(NullOp::RuntimeChecks(_)) => {
467                return ValueOrPlace::TOP;
468            }
469            Rvalue::Discriminant(place) => state.get_discr(place.as_ref(), &self.map),
470            Rvalue::Use(operand) => return self.handle_operand(operand, state),
471            Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"),
472            Rvalue::ShallowInitBox(..) => bug!("`ShallowInitBox` in runtime MIR"),
473            Rvalue::Ref(..) | Rvalue::RawPtr(..) => {
474                // We don't track such places.
475                return ValueOrPlace::TOP;
476            }
477            Rvalue::Repeat(..)
478            | Rvalue::ThreadLocalRef(..)
479            | Rvalue::Cast(..)
480            | Rvalue::BinaryOp(..)
481            | Rvalue::Aggregate(..)
482            | Rvalue::WrapUnsafeBinder(..) => {
483                // No modification is possible through these r-values.
484                return ValueOrPlace::TOP;
485            }
486        };
487        ValueOrPlace::Value(val)
488    }
489
490    fn handle_constant(
491        &self,
492        constant: &ConstOperand<'tcx>,
493        _state: &mut State<FlatSet<Scalar>>,
494    ) -> FlatSet<Scalar> {
495        constant
496            .const_
497            .try_eval_scalar(self.tcx, self.typing_env)
498            .map_or(FlatSet::Top, FlatSet::Elem)
499    }
500
501    fn handle_switch_int<'mir>(
502        &self,
503        discr: &'mir Operand<'tcx>,
504        targets: &'mir SwitchTargets,
505        state: &mut State<FlatSet<Scalar>>,
506    ) -> TerminatorEdges<'mir, 'tcx> {
507        let value = match self.handle_operand(discr, state) {
508            ValueOrPlace::Value(value) => value,
509            ValueOrPlace::Place(place) => state.get_idx(place, &self.map),
510        };
511        match value {
512            // We are branching on uninitialized data, this is UB, treat it as unreachable.
513            // This allows the set of visited edges to grow monotonically with the lattice.
514            FlatSet::Bottom => TerminatorEdges::None,
515            FlatSet::Elem(scalar) => {
516                if let Ok(scalar_int) = scalar.try_to_scalar_int() {
517                    TerminatorEdges::Single(
518                        targets.target_for_value(scalar_int.to_bits_unchecked()),
519                    )
520                } else {
521                    TerminatorEdges::SwitchInt { discr, targets }
522                }
523            }
524            FlatSet::Top => TerminatorEdges::SwitchInt { discr, targets },
525        }
526    }
527
528    /// The caller must have flooded `place`.
529    fn assign_operand(
530        &self,
531        state: &mut State<FlatSet<Scalar>>,
532        place: PlaceIndex,
533        operand: &Operand<'tcx>,
534    ) {
535        match operand {
536            Operand::Copy(rhs) | Operand::Move(rhs) => {
537                if let Some(rhs) = self.map.find(rhs.as_ref()) {
538                    state.insert_place_idx(place, rhs, &self.map);
539                } else if rhs.projection.first() == Some(&PlaceElem::Deref)
540                    && let FlatSet::Elem(pointer) = state.get(rhs.local.into(), &self.map)
541                    && let rhs_ty = self.local_decls[rhs.local].ty
542                    && let Ok(rhs_layout) =
543                        self.tcx.layout_of(self.typing_env.as_query_input(rhs_ty))
544                {
545                    let op = ImmTy::from_scalar(pointer, rhs_layout).into();
546                    self.assign_constant(state, place, op, rhs.projection);
547                }
548            }
549            Operand::Constant(box constant) => {
550                if let Some(constant) = self
551                    .ecx
552                    .borrow()
553                    .eval_mir_constant(&constant.const_, constant.span, None)
554                    .discard_err()
555                {
556                    self.assign_constant(state, place, constant, &[]);
557                }
558            }
559        }
560    }
561
562    /// The caller must have flooded `place`.
563    ///
564    /// Perform: `place = operand.projection`.
565    #[instrument(level = "trace", skip(self, state))]
566    fn assign_constant(
567        &self,
568        state: &mut State<FlatSet<Scalar>>,
569        place: PlaceIndex,
570        mut operand: OpTy<'tcx>,
571        projection: &[PlaceElem<'tcx>],
572    ) {
573        for &(mut proj_elem) in projection {
574            if let PlaceElem::Index(index) = proj_elem {
575                if let FlatSet::Elem(index) = state.get(index.into(), &self.map)
576                    && let Some(offset) = index.to_target_usize(&self.tcx).discard_err()
577                    && let Some(min_length) = offset.checked_add(1)
578                {
579                    proj_elem = PlaceElem::ConstantIndex { offset, min_length, from_end: false };
580                } else {
581                    return;
582                }
583            }
584            operand = if let Some(operand) =
585                self.ecx.borrow().project(&operand, proj_elem).discard_err()
586            {
587                operand
588            } else {
589                return;
590            }
591        }
592
593        self.map.for_each_projection_value(
594            place,
595            operand,
596            &mut |elem, op| match elem {
597                TrackElem::Field(idx) => self.ecx.borrow().project_field(op, idx).discard_err(),
598                TrackElem::Variant(idx) => {
599                    self.ecx.borrow().project_downcast(op, idx).discard_err()
600                }
601                TrackElem::Discriminant => {
602                    let variant = self.ecx.borrow().read_discriminant(op).discard_err()?;
603                    let discr_value = self
604                        .ecx
605                        .borrow()
606                        .discriminant_for_variant(op.layout.ty, variant)
607                        .discard_err()?;
608                    Some(discr_value.into())
609                }
610                TrackElem::DerefLen => {
611                    let op: OpTy<'_> = self.ecx.borrow().deref_pointer(op).discard_err()?.into();
612                    let len_usize = op.len(&self.ecx.borrow()).discard_err()?;
613                    let layout = self
614                        .tcx
615                        .layout_of(self.typing_env.as_query_input(self.tcx.types.usize))
616                        .unwrap();
617                    Some(ImmTy::from_uint(len_usize, layout).into())
618                }
619            },
620            &mut |place, op| {
621                if let Some(imm) = self.ecx.borrow().read_immediate_raw(op).discard_err()
622                    && let Some(imm) = imm.right()
623                {
624                    let elem = self.wrap_immediate(*imm);
625                    state.insert_value_idx(place, elem, &self.map);
626                }
627            },
628        );
629    }
630
631    fn binary_op(
632        &self,
633        state: &mut State<FlatSet<Scalar>>,
634        op: BinOp,
635        left: &Operand<'tcx>,
636        right: &Operand<'tcx>,
637    ) -> (FlatSet<Scalar>, FlatSet<Scalar>) {
638        let left = self.eval_operand(left, state);
639        let right = self.eval_operand(right, state);
640
641        match (left, right) {
642            (FlatSet::Bottom, _) | (_, FlatSet::Bottom) => (FlatSet::Bottom, FlatSet::Bottom),
643            // Both sides are known, do the actual computation.
644            (FlatSet::Elem(left), FlatSet::Elem(right)) => {
645                match self.ecx.borrow().binary_op(op, &left, &right).discard_err() {
646                    // Ideally this would return an Immediate, since it's sometimes
647                    // a pair and sometimes not. But as a hack we always return a pair
648                    // and just make the 2nd component `Bottom` when it does not exist.
649                    Some(val) => {
650                        if matches!(val.layout.backend_repr, BackendRepr::ScalarPair(..)) {
651                            let (val, overflow) = val.to_scalar_pair();
652                            (FlatSet::Elem(val), FlatSet::Elem(overflow))
653                        } else {
654                            (FlatSet::Elem(val.to_scalar()), FlatSet::Bottom)
655                        }
656                    }
657                    _ => (FlatSet::Top, FlatSet::Top),
658                }
659            }
660            // Exactly one side is known, attempt some algebraic simplifications.
661            (FlatSet::Elem(const_arg), _) | (_, FlatSet::Elem(const_arg)) => {
662                let layout = const_arg.layout;
663                if !matches!(layout.backend_repr, rustc_abi::BackendRepr::Scalar(..)) {
664                    return (FlatSet::Top, FlatSet::Top);
665                }
666
667                let arg_scalar = const_arg.to_scalar();
668                let Some(arg_value) = arg_scalar.to_bits(layout.size).discard_err() else {
669                    return (FlatSet::Top, FlatSet::Top);
670                };
671
672                match op {
673                    BinOp::BitAnd if arg_value == 0 => (FlatSet::Elem(arg_scalar), FlatSet::Bottom),
674                    BinOp::BitOr
675                        if arg_value == layout.size.truncate(u128::MAX)
676                            || (layout.ty.is_bool() && arg_value == 1) =>
677                    {
678                        (FlatSet::Elem(arg_scalar), FlatSet::Bottom)
679                    }
680                    BinOp::Mul if layout.ty.is_integral() && arg_value == 0 => {
681                        (FlatSet::Elem(arg_scalar), FlatSet::Elem(Scalar::from_bool(false)))
682                    }
683                    _ => (FlatSet::Top, FlatSet::Top),
684                }
685            }
686            (FlatSet::Top, FlatSet::Top) => (FlatSet::Top, FlatSet::Top),
687        }
688    }
689
690    fn eval_operand(
691        &self,
692        op: &Operand<'tcx>,
693        state: &mut State<FlatSet<Scalar>>,
694    ) -> FlatSet<ImmTy<'tcx>> {
695        let value = match self.handle_operand(op, state) {
696            ValueOrPlace::Value(value) => value,
697            ValueOrPlace::Place(place) => state.get_idx(place, &self.map),
698        };
699        match value {
700            FlatSet::Top => FlatSet::Top,
701            FlatSet::Elem(scalar) => {
702                let ty = op.ty(self.local_decls, self.tcx);
703                self.tcx
704                    .layout_of(self.typing_env.as_query_input(ty))
705                    .map_or(FlatSet::Top, |layout| {
706                        FlatSet::Elem(ImmTy::from_scalar(scalar, layout))
707                    })
708            }
709            FlatSet::Bottom => FlatSet::Bottom,
710        }
711    }
712
713    fn eval_discriminant(&self, enum_ty: Ty<'tcx>, variant_index: VariantIdx) -> Option<Scalar> {
714        if !enum_ty.is_enum() {
715            return None;
716        }
717        let enum_ty_layout = self.tcx.layout_of(self.typing_env.as_query_input(enum_ty)).ok()?;
718        let discr_value = self
719            .ecx
720            .borrow()
721            .discriminant_for_variant(enum_ty_layout.ty, variant_index)
722            .discard_err()?;
723        Some(discr_value.to_scalar())
724    }
725
726    fn wrap_immediate(&self, imm: Immediate) -> FlatSet<Scalar> {
727        match imm {
728            Immediate::Scalar(scalar) => FlatSet::Elem(scalar),
729            Immediate::Uninit => FlatSet::Bottom,
730            _ => FlatSet::Top,
731        }
732    }
733}
734
735/// This is used to visualize the dataflow analysis.
736impl<'tcx> DebugWithContext<ConstAnalysis<'_, 'tcx>> for State<FlatSet<Scalar>> {
737    fn fmt_with(&self, ctxt: &ConstAnalysis<'_, 'tcx>, f: &mut Formatter<'_>) -> std::fmt::Result {
738        match self {
739            State::Reachable(values) => debug_with_context(values, None, &ctxt.map, f),
740            State::Unreachable => write!(f, "unreachable"),
741        }
742    }
743
744    fn fmt_diff_with(
745        &self,
746        old: &Self,
747        ctxt: &ConstAnalysis<'_, 'tcx>,
748        f: &mut Formatter<'_>,
749    ) -> std::fmt::Result {
750        match (self, old) {
751            (State::Reachable(this), State::Reachable(old)) => {
752                debug_with_context(this, Some(old), &ctxt.map, f)
753            }
754            _ => Ok(()), // Consider printing something here.
755        }
756    }
757}
758
759struct Patch<'tcx> {
760    tcx: TyCtxt<'tcx>,
761
762    /// For a given MIR location, this stores the values of the operands used by that location. In
763    /// particular, this is before the effect, such that the operands of `_1 = _1 + _2` are
764    /// properly captured. (This may become UB soon, but it is currently emitted even by safe code.)
765    before_effect: FxHashMap<(Location, Place<'tcx>), Const<'tcx>>,
766
767    /// Stores the assigned values for assignments where the Rvalue is constant.
768    assignments: FxHashMap<Location, Const<'tcx>>,
769}
770
771impl<'tcx> Patch<'tcx> {
772    pub(crate) fn new(tcx: TyCtxt<'tcx>) -> Self {
773        Self { tcx, before_effect: FxHashMap::default(), assignments: FxHashMap::default() }
774    }
775
776    fn make_operand(&self, const_: Const<'tcx>) -> Operand<'tcx> {
777        Operand::Constant(Box::new(ConstOperand { span: DUMMY_SP, user_ty: None, const_ }))
778    }
779}
780
781struct Collector<'a, 'tcx> {
782    patch: Patch<'tcx>,
783    local_decls: &'a LocalDecls<'tcx>,
784}
785
786impl<'a, 'tcx> Collector<'a, 'tcx> {
787    pub(crate) fn new(tcx: TyCtxt<'tcx>, local_decls: &'a LocalDecls<'tcx>) -> Self {
788        Self { patch: Patch::new(tcx), local_decls }
789    }
790
791    #[instrument(level = "trace", skip(self, ecx, map), ret)]
792    fn try_make_constant(
793        &self,
794        ecx: &mut InterpCx<'tcx, DummyMachine>,
795        place: Place<'tcx>,
796        state: &State<FlatSet<Scalar>>,
797        map: &Map<'tcx>,
798    ) -> Option<Const<'tcx>> {
799        let ty = place.ty(self.local_decls, self.patch.tcx).ty;
800        let layout = ecx.layout_of(ty).ok()?;
801
802        if layout.is_zst() {
803            return Some(Const::zero_sized(ty));
804        }
805
806        if layout.is_unsized() {
807            return None;
808        }
809
810        let place = map.find(place.as_ref())?;
811        if layout.backend_repr.is_scalar()
812            && let Some(value) = propagatable_scalar(place, state, map)
813        {
814            return Some(Const::Val(ConstValue::Scalar(value), ty));
815        }
816
817        if matches!(layout.backend_repr, BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)) {
818            let alloc_id = ecx
819                .intern_with_temp_alloc(layout, |ecx, dest| {
820                    try_write_constant(ecx, dest, place, ty, state, map)
821                })
822                .discard_err()?;
823            return Some(Const::Val(ConstValue::Indirect { alloc_id, offset: Size::ZERO }, ty));
824        }
825
826        None
827    }
828}
829
830#[instrument(level = "trace", skip(map), ret)]
831fn propagatable_scalar(
832    place: PlaceIndex,
833    state: &State<FlatSet<Scalar>>,
834    map: &Map<'_>,
835) -> Option<Scalar> {
836    if let FlatSet::Elem(value) = state.get_idx(place, map)
837        && value.try_to_scalar_int().is_ok()
838    {
839        // Do not attempt to propagate pointers, as we may fail to preserve their identity.
840        Some(value)
841    } else {
842        None
843    }
844}
845
846#[instrument(level = "trace", skip(ecx, state, map), ret)]
847fn try_write_constant<'tcx>(
848    ecx: &mut InterpCx<'tcx, DummyMachine>,
849    dest: &PlaceTy<'tcx>,
850    place: PlaceIndex,
851    ty: Ty<'tcx>,
852    state: &State<FlatSet<Scalar>>,
853    map: &Map<'tcx>,
854) -> InterpResult<'tcx> {
855    let layout = ecx.layout_of(ty)?;
856
857    // Fast path for ZSTs.
858    if layout.is_zst() {
859        return interp_ok(());
860    }
861
862    // Fast path for scalars.
863    if layout.backend_repr.is_scalar()
864        && let Some(value) = propagatable_scalar(place, state, map)
865    {
866        return ecx.write_immediate(Immediate::Scalar(value), dest);
867    }
868
869    match ty.kind() {
870        // ZSTs. Nothing to do.
871        ty::FnDef(..) => {}
872
873        // Those are scalars, must be handled above.
874        ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char =>
875            throw_machine_stop_str!("primitive type with provenance"),
876
877        ty::Tuple(elem_tys) => {
878            for (i, elem) in elem_tys.iter().enumerate() {
879                let i = FieldIdx::from_usize(i);
880                let Some(field) = map.apply(place, TrackElem::Field(i)) else {
881                    throw_machine_stop_str!("missing field in tuple")
882                };
883                let field_dest = ecx.project_field(dest, i)?;
884                try_write_constant(ecx, &field_dest, field, elem, state, map)?;
885            }
886        }
887
888        ty::Adt(def, args) => {
889            if def.is_union() {
890                throw_machine_stop_str!("cannot propagate unions")
891            }
892
893            let (variant_idx, variant_def, variant_place, variant_dest) = if def.is_enum() {
894                let Some(discr) = map.apply(place, TrackElem::Discriminant) else {
895                    throw_machine_stop_str!("missing discriminant for enum")
896                };
897                let FlatSet::Elem(Scalar::Int(discr)) = state.get_idx(discr, map) else {
898                    throw_machine_stop_str!("discriminant with provenance")
899                };
900                let discr_bits = discr.to_bits(discr.size());
901                let Some((variant, _)) = def.discriminants(*ecx.tcx).find(|(_, var)| discr_bits == var.val) else {
902                    throw_machine_stop_str!("illegal discriminant for enum")
903                };
904                let Some(variant_place) = map.apply(place, TrackElem::Variant(variant)) else {
905                    throw_machine_stop_str!("missing variant for enum")
906                };
907                let variant_dest = ecx.project_downcast(dest, variant)?;
908                (variant, def.variant(variant), variant_place, variant_dest)
909            } else {
910                (FIRST_VARIANT, def.non_enum_variant(), place, dest.clone())
911            };
912
913            for (i, field) in variant_def.fields.iter_enumerated() {
914                let ty = field.ty(*ecx.tcx, args);
915                let Some(field) = map.apply(variant_place, TrackElem::Field(i)) else {
916                    throw_machine_stop_str!("missing field in ADT")
917                };
918                let field_dest = ecx.project_field(&variant_dest, i)?;
919                try_write_constant(ecx, &field_dest, field, ty, state, map)?;
920            }
921            ecx.write_discriminant(variant_idx, dest)?;
922        }
923
924        // Unsupported for now.
925        ty::Array(_, _)
926        | ty::Pat(_, _)
927
928        // Do not attempt to support indirection in constants.
929        | ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Str | ty::Slice(_)
930
931        | ty::Never
932        | ty::Foreign(..)
933        | ty::Alias(..)
934        | ty::Param(_)
935        | ty::Bound(..)
936        | ty::Placeholder(..)
937        | ty::Closure(..)
938        | ty::CoroutineClosure(..)
939        | ty::Coroutine(..)
940        | ty::Dynamic(..)
941        | ty::UnsafeBinder(_) => throw_machine_stop_str!("unsupported type"),
942
943        ty::Error(_) | ty::Infer(..) | ty::CoroutineWitness(..) => bug!(),
944    }
945
946    interp_ok(())
947}
948
949impl<'tcx> ResultsVisitor<'tcx, ConstAnalysis<'_, 'tcx>> for Collector<'_, 'tcx> {
950    #[instrument(level = "trace", skip(self, analysis, statement))]
951    fn visit_after_early_statement_effect(
952        &mut self,
953        analysis: &ConstAnalysis<'_, 'tcx>,
954        state: &State<FlatSet<Scalar>>,
955        statement: &Statement<'tcx>,
956        location: Location,
957    ) {
958        match &statement.kind {
959            StatementKind::Assign(box (_, rvalue)) => {
960                OperandCollector {
961                    state,
962                    visitor: self,
963                    ecx: &mut analysis.ecx.borrow_mut(),
964                    map: &analysis.map,
965                }
966                .visit_rvalue(rvalue, location);
967            }
968            _ => (),
969        }
970    }
971
972    #[instrument(level = "trace", skip(self, analysis, statement))]
973    fn visit_after_primary_statement_effect(
974        &mut self,
975        analysis: &ConstAnalysis<'_, 'tcx>,
976        state: &State<FlatSet<Scalar>>,
977        statement: &Statement<'tcx>,
978        location: Location,
979    ) {
980        match statement.kind {
981            StatementKind::Assign(box (_, Rvalue::Use(Operand::Constant(_)))) => {
982                // Don't overwrite the assignment if it already uses a constant (to keep the span).
983            }
984            StatementKind::Assign(box (place, _)) => {
985                if let Some(value) = self.try_make_constant(
986                    &mut analysis.ecx.borrow_mut(),
987                    place,
988                    state,
989                    &analysis.map,
990                ) {
991                    self.patch.assignments.insert(location, value);
992                }
993            }
994            _ => (),
995        }
996    }
997
998    fn visit_after_early_terminator_effect(
999        &mut self,
1000        analysis: &ConstAnalysis<'_, 'tcx>,
1001        state: &State<FlatSet<Scalar>>,
1002        terminator: &Terminator<'tcx>,
1003        location: Location,
1004    ) {
1005        OperandCollector {
1006            state,
1007            visitor: self,
1008            ecx: &mut analysis.ecx.borrow_mut(),
1009            map: &analysis.map,
1010        }
1011        .visit_terminator(terminator, location);
1012    }
1013}
1014
1015impl<'tcx> MutVisitor<'tcx> for Patch<'tcx> {
1016    fn tcx(&self) -> TyCtxt<'tcx> {
1017        self.tcx
1018    }
1019
1020    fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
1021        if let Some(value) = self.assignments.get(&location) {
1022            match &mut statement.kind {
1023                StatementKind::Assign(box (_, rvalue)) => {
1024                    *rvalue = Rvalue::Use(self.make_operand(*value));
1025                }
1026                _ => bug!("found assignment info for non-assign statement"),
1027            }
1028        } else {
1029            self.super_statement(statement, location);
1030        }
1031    }
1032
1033    fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
1034        match operand {
1035            Operand::Copy(place) | Operand::Move(place) => {
1036                if let Some(value) = self.before_effect.get(&(location, *place)) {
1037                    *operand = self.make_operand(*value);
1038                } else if !place.projection.is_empty() {
1039                    self.super_operand(operand, location)
1040                }
1041            }
1042            Operand::Constant(_) => {}
1043        }
1044    }
1045
1046    fn process_projection_elem(
1047        &mut self,
1048        elem: PlaceElem<'tcx>,
1049        location: Location,
1050    ) -> Option<PlaceElem<'tcx>> {
1051        if let PlaceElem::Index(local) = elem {
1052            let offset = self.before_effect.get(&(location, local.into()))?;
1053            let offset = offset.try_to_scalar()?;
1054            let offset = offset.to_target_usize(&self.tcx).discard_err()?;
1055            let min_length = offset.checked_add(1)?;
1056            Some(PlaceElem::ConstantIndex { offset, min_length, from_end: false })
1057        } else {
1058            None
1059        }
1060    }
1061}
1062
1063struct OperandCollector<'a, 'b, 'tcx> {
1064    state: &'a State<FlatSet<Scalar>>,
1065    visitor: &'a mut Collector<'b, 'tcx>,
1066    ecx: &'a mut InterpCx<'tcx, DummyMachine>,
1067    map: &'a Map<'tcx>,
1068}
1069
1070impl<'tcx> Visitor<'tcx> for OperandCollector<'_, '_, 'tcx> {
1071    fn visit_projection_elem(
1072        &mut self,
1073        _: PlaceRef<'tcx>,
1074        elem: PlaceElem<'tcx>,
1075        _: PlaceContext,
1076        location: Location,
1077    ) {
1078        if let PlaceElem::Index(local) = elem
1079            && let Some(value) =
1080                self.visitor.try_make_constant(self.ecx, local.into(), self.state, self.map)
1081        {
1082            self.visitor.patch.before_effect.insert((location, local.into()), value);
1083        }
1084    }
1085
1086    fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
1087        if let Some(place) = operand.place() {
1088            if let Some(value) =
1089                self.visitor.try_make_constant(self.ecx, place, self.state, self.map)
1090            {
1091                self.visitor.patch.before_effect.insert((location, place), value);
1092            } else if !place.projection.is_empty() {
1093                // Try to propagate into `Index` projections.
1094                self.super_operand(operand, location)
1095            }
1096        }
1097    }
1098}