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