2022-10-19 08:56:58 -05:00
|
|
|
//! A constant propagation optimization pass based on dataflow analysis.
|
|
|
|
//!
|
2022-10-24 17:28:41 -05:00
|
|
|
//! Currently, this pass only propagates scalar values.
|
2022-10-19 08:56:58 -05:00
|
|
|
|
2022-11-21 10:51:16 -06:00
|
|
|
use rustc_const_eval::const_eval::CheckAlignment;
|
2022-08-25 11:43:46 -05:00
|
|
|
use rustc_const_eval::interpret::{ConstValue, ImmTy, Immediate, InterpCx, Scalar};
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2023-01-24 11:43:07 -06:00
|
|
|
use rustc_hir::def::DefKind;
|
2022-08-25 11:43:46 -05:00
|
|
|
use rustc_middle::mir::visit::{MutVisitor, Visitor};
|
|
|
|
use rustc_middle::mir::*;
|
2023-03-21 03:52:31 -05:00
|
|
|
use rustc_middle::ty::layout::TyAndLayout;
|
2022-09-23 08:15:14 -05:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2022-11-09 11:21:42 -06:00
|
|
|
use rustc_mir_dataflow::value_analysis::{Map, State, TrackElem, ValueAnalysis, ValueOrPlace};
|
2022-08-25 11:43:46 -05:00
|
|
|
use rustc_mir_dataflow::{lattice::FlatSet, Analysis, ResultsVisitor, SwitchIntEdgeEffects};
|
2022-10-23 07:49:38 -05:00
|
|
|
use rustc_span::DUMMY_SP;
|
2023-03-28 14:32:57 -05:00
|
|
|
use rustc_target::abi::{Align, FieldIdx, VariantIdx};
|
2022-08-25 11:43:46 -05:00
|
|
|
|
|
|
|
use crate::MirPass;
|
|
|
|
|
2022-11-12 07:07:54 -06:00
|
|
|
// These constants are somewhat random guesses and have not been optimized.
|
2022-11-12 13:14:34 -06:00
|
|
|
// If `tcx.sess.mir_opt_level() >= 4`, we ignore the limits (this can become very expensive).
|
2022-10-27 09:50:39 -05:00
|
|
|
const BLOCK_LIMIT: usize = 100;
|
|
|
|
const PLACE_LIMIT: usize = 100;
|
2022-10-25 14:54:39 -05:00
|
|
|
|
2022-08-25 11:43:46 -05:00
|
|
|
pub struct DataflowConstProp;
|
|
|
|
|
|
|
|
impl<'tcx> MirPass<'tcx> for DataflowConstProp {
|
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
2022-11-12 08:24:23 -06:00
|
|
|
sess.mir_opt_level() >= 3
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:54:39 -05:00
|
|
|
#[instrument(skip_all level = "debug")]
|
2022-08-25 11:43:46 -05:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2023-01-21 16:28:54 -06:00
|
|
|
debug!(def_id = ?body.source.def_id());
|
2022-11-12 13:14:34 -06:00
|
|
|
if tcx.sess.mir_opt_level() < 4 && body.basic_blocks.len() > BLOCK_LIMIT {
|
2022-10-27 09:50:39 -05:00
|
|
|
debug!("aborted dataflow const prop due too many basic blocks");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-25 14:54:39 -05:00
|
|
|
// We want to have a somewhat linear runtime w.r.t. the number of statements/terminators.
|
|
|
|
// Let's call this number `n`. Dataflow analysis has `O(h*n)` transfer function
|
|
|
|
// applications, where `h` is the height of the lattice. Because the height of our lattice
|
|
|
|
// is linear w.r.t. the number of tracked places, this is `O(tracked_places * n)`. However,
|
|
|
|
// because every transfer function application could traverse the whole map, this becomes
|
|
|
|
// `O(num_nodes * tracked_places * n)` in terms of time complexity. Since the number of
|
|
|
|
// map nodes is strongly correlated to the number of tracked places, this becomes more or
|
|
|
|
// less `O(n)` if we place a constant limit on the number of tracked places.
|
2023-01-28 05:23:18 -06:00
|
|
|
let place_limit = if tcx.sess.mir_opt_level() < 4 { Some(PLACE_LIMIT) } else { None };
|
|
|
|
|
|
|
|
// Decide which places to track during the analysis.
|
|
|
|
let map = Map::from_filter(tcx, body, Ty::is_scalar, place_limit);
|
2022-10-25 14:54:39 -05:00
|
|
|
|
2022-08-25 11:43:46 -05:00
|
|
|
// Perform the actual dataflow analysis.
|
|
|
|
let analysis = ConstAnalysis::new(tcx, body, map);
|
2022-10-25 14:54:39 -05:00
|
|
|
let results = debug_span!("analyze")
|
|
|
|
.in_scope(|| analysis.wrap().into_engine(tcx, body).iterate_to_fixpoint());
|
2022-08-25 11:43:46 -05:00
|
|
|
|
|
|
|
// Collect results and patch the body afterwards.
|
|
|
|
let mut visitor = CollectAndPatch::new(tcx, &results.analysis.0.map);
|
2022-10-25 14:54:39 -05:00
|
|
|
debug_span!("collect").in_scope(|| results.visit_reachable_with(body, &mut visitor));
|
|
|
|
debug_span!("patch").in_scope(|| visitor.visit_body(body));
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
struct ConstAnalysis<'a, 'tcx> {
|
2022-08-25 11:43:46 -05:00
|
|
|
map: Map,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-01-21 16:28:54 -06:00
|
|
|
local_decls: &'a LocalDecls<'tcx>,
|
2022-08-25 11:43:46 -05:00
|
|
|
ecx: InterpCx<'tcx, 'tcx, DummyMachine>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
impl<'tcx> ConstAnalysis<'_, 'tcx> {
|
2023-02-13 11:01:03 -06:00
|
|
|
fn eval_discriminant(
|
2023-01-21 16:28:54 -06:00
|
|
|
&self,
|
|
|
|
enum_ty: Ty<'tcx>,
|
|
|
|
variant_index: VariantIdx,
|
|
|
|
) -> Option<ScalarTy<'tcx>> {
|
|
|
|
if !enum_ty.is_enum() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let discr = enum_ty.discriminant_for_variant(self.tcx, variant_index)?;
|
|
|
|
let discr_layout = self.tcx.layout_of(self.param_env.and(discr.ty)).ok()?;
|
|
|
|
let discr_value = Scalar::try_from_uint(discr.val, discr_layout.size)?;
|
|
|
|
Some(ScalarTy(discr_value, discr.ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
|
2022-09-23 08:15:14 -05:00
|
|
|
type Value = FlatSet<ScalarTy<'tcx>>;
|
2022-08-25 11:43:46 -05:00
|
|
|
|
|
|
|
const NAME: &'static str = "ConstAnalysis";
|
|
|
|
|
|
|
|
fn map(&self) -> &Map {
|
|
|
|
&self.map
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
fn handle_statement(&self, statement: &Statement<'tcx>, state: &mut State<Self::Value>) {
|
|
|
|
match statement.kind {
|
|
|
|
StatementKind::SetDiscriminant { box ref place, variant_index } => {
|
|
|
|
state.flood_discr(place.as_ref(), &self.map);
|
|
|
|
if self.map.find_discr(place.as_ref()).is_some() {
|
|
|
|
let enum_ty = place.ty(self.local_decls, self.tcx).ty;
|
2023-02-13 11:01:03 -06:00
|
|
|
if let Some(discr) = self.eval_discriminant(enum_ty, variant_index) {
|
2023-01-21 16:28:54 -06:00
|
|
|
state.assign_discr(
|
|
|
|
place.as_ref(),
|
|
|
|
ValueOrPlace::Value(FlatSet::Elem(discr)),
|
|
|
|
&self.map,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.super_statement(statement, state),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:43:46 -05:00
|
|
|
fn handle_assign(
|
|
|
|
&self,
|
|
|
|
target: Place<'tcx>,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
) {
|
|
|
|
match rvalue {
|
2023-01-24 11:43:07 -06:00
|
|
|
Rvalue::Aggregate(kind, operands) => {
|
2023-02-18 03:57:13 -06:00
|
|
|
// If we assign `target = Enum::Variant#0(operand)`,
|
|
|
|
// we must make sure that all `target as Variant#i` are `Top`.
|
|
|
|
state.flood(target.as_ref(), self.map());
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
if let Some(target_idx) = self.map().find(target.as_ref()) {
|
|
|
|
let (variant_target, variant_index) = match **kind {
|
|
|
|
AggregateKind::Tuple | AggregateKind::Closure(..) => {
|
|
|
|
(Some(target_idx), None)
|
|
|
|
}
|
|
|
|
AggregateKind::Adt(def_id, variant_index, ..) => {
|
|
|
|
match self.tcx.def_kind(def_id) {
|
|
|
|
DefKind::Struct => (Some(target_idx), None),
|
2023-02-18 04:04:14 -06:00
|
|
|
DefKind::Enum => (
|
|
|
|
self.map.apply(target_idx, TrackElem::Variant(variant_index)),
|
|
|
|
Some(variant_index),
|
|
|
|
),
|
2023-01-21 16:28:54 -06:00
|
|
|
_ => (None, None),
|
|
|
|
}
|
2023-01-24 11:43:07 -06:00
|
|
|
}
|
2023-01-21 16:28:54 -06:00
|
|
|
_ => (None, None),
|
2023-01-24 11:43:07 -06:00
|
|
|
};
|
2023-02-18 04:04:14 -06:00
|
|
|
if let Some(variant_target_idx) = variant_target {
|
2023-01-24 11:43:07 -06:00
|
|
|
for (field_index, operand) in operands.iter().enumerate() {
|
2023-02-18 04:04:14 -06:00
|
|
|
if let Some(field) = self.map().apply(
|
|
|
|
variant_target_idx,
|
2023-03-28 14:32:57 -05:00
|
|
|
TrackElem::Field(FieldIdx::from_usize(field_index)),
|
2023-02-18 04:04:14 -06:00
|
|
|
) {
|
2023-01-24 11:43:07 -06:00
|
|
|
let result = self.handle_operand(operand, state);
|
2023-02-03 10:39:10 -06:00
|
|
|
state.insert_idx(field, result, self.map());
|
2023-01-24 11:43:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 16:28:54 -06:00
|
|
|
if let Some(variant_index) = variant_index
|
|
|
|
&& let Some(discr_idx) = self.map().apply(target_idx, TrackElem::Discriminant)
|
|
|
|
{
|
2023-02-18 04:04:14 -06:00
|
|
|
// We are assigning the discriminant as part of an aggregate.
|
|
|
|
// This discriminant can only alias a variant field's value if the operand
|
|
|
|
// had an invalid value for that type.
|
|
|
|
// Using invalid values is UB, so we are allowed to perform the assignment
|
|
|
|
// without extra flooding.
|
2023-01-21 16:28:54 -06:00
|
|
|
let enum_ty = target.ty(self.local_decls, self.tcx).ty;
|
2023-02-13 11:01:03 -06:00
|
|
|
if let Some(discr_val) = self.eval_discriminant(enum_ty, variant_index) {
|
2023-02-03 10:39:10 -06:00
|
|
|
state.insert_value_idx(discr_idx, FlatSet::Elem(discr_val), &self.map);
|
2023-01-21 16:28:54 -06:00
|
|
|
}
|
|
|
|
}
|
2023-01-24 11:43:07 -06:00
|
|
|
}
|
|
|
|
}
|
2022-08-25 11:43:46 -05:00
|
|
|
Rvalue::CheckedBinaryOp(op, box (left, right)) => {
|
2023-02-03 10:39:10 -06:00
|
|
|
// Flood everything now, so we can use `insert_value_idx` directly later.
|
2023-01-21 16:28:54 -06:00
|
|
|
state.flood(target.as_ref(), self.map());
|
|
|
|
|
2022-08-25 11:43:46 -05:00
|
|
|
let target = self.map().find(target.as_ref());
|
2022-09-01 07:17:15 -05:00
|
|
|
|
2022-10-19 08:56:58 -05:00
|
|
|
let value_target = target
|
|
|
|
.and_then(|target| self.map().apply(target, TrackElem::Field(0_u32.into())));
|
|
|
|
let overflow_target = target
|
|
|
|
.and_then(|target| self.map().apply(target, TrackElem::Field(1_u32.into())));
|
2022-08-25 11:43:46 -05:00
|
|
|
|
|
|
|
if value_target.is_some() || overflow_target.is_some() {
|
2022-10-15 16:40:03 -05:00
|
|
|
let (val, overflow) = self.binary_op(state, *op, left, right);
|
2022-08-25 11:43:46 -05:00
|
|
|
|
|
|
|
if let Some(value_target) = value_target {
|
2023-02-03 10:39:10 -06:00
|
|
|
// We have flooded `target` earlier.
|
|
|
|
state.insert_value_idx(value_target, val, self.map());
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
if let Some(overflow_target) = overflow_target {
|
2022-10-15 16:40:03 -05:00
|
|
|
let overflow = match overflow {
|
|
|
|
FlatSet::Top => FlatSet::Top,
|
|
|
|
FlatSet::Elem(overflow) => {
|
2023-02-08 14:28:39 -06:00
|
|
|
self.wrap_scalar(Scalar::from_bool(overflow), self.tcx.types.bool)
|
2022-10-15 16:40:03 -05:00
|
|
|
}
|
|
|
|
FlatSet::Bottom => FlatSet::Bottom,
|
|
|
|
};
|
2023-02-03 10:39:10 -06:00
|
|
|
// We have flooded `target` earlier.
|
|
|
|
state.insert_value_idx(overflow_target, overflow, self.map());
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.super_assign(target, rvalue, state),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_rvalue(
|
|
|
|
&self,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
state: &mut State<Self::Value>,
|
2022-11-09 11:03:30 -06:00
|
|
|
) -> ValueOrPlace<Self::Value> {
|
2022-08-25 11:43:46 -05:00
|
|
|
match rvalue {
|
2022-10-12 18:02:18 -05:00
|
|
|
Rvalue::Cast(
|
2022-10-26 07:46:27 -05:00
|
|
|
kind @ (CastKind::IntToInt
|
2022-10-12 18:02:18 -05:00
|
|
|
| CastKind::FloatToInt
|
|
|
|
| CastKind::FloatToFloat
|
2022-10-26 07:46:27 -05:00
|
|
|
| CastKind::IntToFloat),
|
2022-10-12 18:02:18 -05:00
|
|
|
operand,
|
|
|
|
ty,
|
2022-10-26 07:46:27 -05:00
|
|
|
) => match self.eval_operand(operand, state) {
|
|
|
|
FlatSet::Elem(op) => match kind {
|
|
|
|
CastKind::IntToInt | CastKind::IntToFloat => {
|
|
|
|
self.ecx.int_to_int_or_float(&op, *ty)
|
|
|
|
}
|
|
|
|
CastKind::FloatToInt | CastKind::FloatToFloat => {
|
|
|
|
self.ecx.float_to_float_or_int(&op, *ty)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
2022-11-09 11:03:30 -06:00
|
|
|
.map(|result| ValueOrPlace::Value(self.wrap_immediate(result, *ty)))
|
|
|
|
.unwrap_or(ValueOrPlace::top()),
|
|
|
|
_ => ValueOrPlace::top(),
|
2022-10-26 07:46:27 -05:00
|
|
|
},
|
2022-08-25 11:43:46 -05:00
|
|
|
Rvalue::BinaryOp(op, box (left, right)) => {
|
2022-10-15 16:40:03 -05:00
|
|
|
// Overflows must be ignored here.
|
2022-08-25 11:43:46 -05:00
|
|
|
let (val, _overflow) = self.binary_op(state, *op, left, right);
|
2022-11-09 11:03:30 -06:00
|
|
|
ValueOrPlace::Value(val)
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
Rvalue::UnaryOp(op, operand) => match self.eval_operand(operand, state) {
|
|
|
|
FlatSet::Elem(value) => self
|
|
|
|
.ecx
|
|
|
|
.unary_op(*op, &value)
|
2022-11-09 11:03:30 -06:00
|
|
|
.map(|val| ValueOrPlace::Value(self.wrap_immty(val)))
|
|
|
|
.unwrap_or(ValueOrPlace::Value(FlatSet::Top)),
|
|
|
|
FlatSet::Bottom => ValueOrPlace::Value(FlatSet::Bottom),
|
|
|
|
FlatSet::Top => ValueOrPlace::Value(FlatSet::Top),
|
2022-08-25 11:43:46 -05:00
|
|
|
},
|
2023-01-21 16:28:54 -06:00
|
|
|
Rvalue::Discriminant(place) => {
|
|
|
|
ValueOrPlace::Value(state.get_discr(place.as_ref(), self.map()))
|
|
|
|
}
|
2022-08-25 11:43:46 -05:00
|
|
|
_ => self.super_rvalue(rvalue, state),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_constant(
|
|
|
|
&self,
|
|
|
|
constant: &Constant<'tcx>,
|
|
|
|
_state: &mut State<Self::Value>,
|
|
|
|
) -> Self::Value {
|
|
|
|
constant
|
|
|
|
.literal
|
|
|
|
.eval(self.tcx, self.param_env)
|
|
|
|
.try_to_scalar()
|
2022-09-23 08:15:14 -05:00
|
|
|
.map(|value| FlatSet::Elem(ScalarTy(value, constant.ty())))
|
2022-08-25 11:43:46 -05:00
|
|
|
.unwrap_or(FlatSet::Top)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_switch_int(
|
|
|
|
&self,
|
|
|
|
discr: &Operand<'tcx>,
|
|
|
|
apply_edge_effects: &mut impl SwitchIntEdgeEffects<State<Self::Value>>,
|
|
|
|
) {
|
|
|
|
// FIXME: The dataflow framework only provides the state if we call `apply()`, which makes
|
|
|
|
// this more inefficient than it has to be.
|
|
|
|
let mut discr_value = None;
|
|
|
|
let mut handled = false;
|
|
|
|
apply_edge_effects.apply(|state, target| {
|
|
|
|
let discr_value = match discr_value {
|
|
|
|
Some(value) => value,
|
|
|
|
None => {
|
|
|
|
let value = match self.handle_operand(discr, state) {
|
|
|
|
ValueOrPlace::Value(value) => value,
|
|
|
|
ValueOrPlace::Place(place) => state.get_idx(place, self.map()),
|
|
|
|
};
|
|
|
|
let result = match value {
|
|
|
|
FlatSet::Top => FlatSet::Top,
|
2022-09-23 08:15:14 -05:00
|
|
|
FlatSet::Elem(ScalarTy(scalar, _)) => {
|
|
|
|
let int = scalar.assert_int();
|
|
|
|
FlatSet::Elem(int.assert_bits(int.size()))
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
FlatSet::Bottom => FlatSet::Bottom,
|
|
|
|
};
|
|
|
|
discr_value = Some(result);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let FlatSet::Elem(choice) = discr_value else {
|
|
|
|
// Do nothing if we don't know which branch will be taken.
|
|
|
|
return
|
|
|
|
};
|
|
|
|
|
|
|
|
if target.value.map(|n| n == choice).unwrap_or(!handled) {
|
|
|
|
// Branch is taken. Has no effect on state.
|
|
|
|
handled = true;
|
|
|
|
} else {
|
2022-09-02 07:41:27 -05:00
|
|
|
// Branch is not taken.
|
|
|
|
state.mark_unreachable();
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2022-09-23 08:15:14 -05:00
|
|
|
struct ScalarTy<'tcx>(Scalar, Ty<'tcx>);
|
2022-08-25 11:43:46 -05:00
|
|
|
|
2022-09-23 08:15:14 -05:00
|
|
|
impl<'tcx> std::fmt::Debug for ScalarTy<'tcx> {
|
2022-08-25 11:43:46 -05:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2022-10-19 08:56:58 -05:00
|
|
|
// This is used for dataflow visualization, so we return something more concise.
|
2022-09-23 08:15:14 -05:00
|
|
|
std::fmt::Display::fmt(&ConstantKind::Val(ConstValue::Scalar(self.0), self.1), f)
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:28:54 -06:00
|
|
|
impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
|
|
|
|
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: Map) -> Self {
|
2022-10-23 08:31:46 -05:00
|
|
|
let param_env = tcx.param_env(body.source.def_id());
|
2022-08-25 11:43:46 -05:00
|
|
|
Self {
|
|
|
|
map,
|
|
|
|
tcx,
|
2023-01-21 16:28:54 -06:00
|
|
|
local_decls: &body.local_decls,
|
2022-10-23 08:31:46 -05:00
|
|
|
ecx: InterpCx::new(tcx, DUMMY_SP, param_env, DummyMachine),
|
|
|
|
param_env: param_env,
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn binary_op(
|
|
|
|
&self,
|
2022-09-23 08:15:14 -05:00
|
|
|
state: &mut State<FlatSet<ScalarTy<'tcx>>>,
|
2022-08-25 11:43:46 -05:00
|
|
|
op: BinOp,
|
|
|
|
left: &Operand<'tcx>,
|
|
|
|
right: &Operand<'tcx>,
|
2022-10-15 16:40:03 -05:00
|
|
|
) -> (FlatSet<ScalarTy<'tcx>>, FlatSet<bool>) {
|
2022-08-25 11:43:46 -05:00
|
|
|
let left = self.eval_operand(left, state);
|
|
|
|
let right = self.eval_operand(right, state);
|
|
|
|
match (left, right) {
|
|
|
|
(FlatSet::Elem(left), FlatSet::Elem(right)) => {
|
|
|
|
match self.ecx.overflowing_binary_op(op, &left, &right) {
|
2022-10-15 16:40:03 -05:00
|
|
|
Ok((val, overflow, ty)) => (self.wrap_scalar(val, ty), FlatSet::Elem(overflow)),
|
2022-08-25 11:43:46 -05:00
|
|
|
_ => (FlatSet::Top, FlatSet::Top),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(FlatSet::Bottom, _) | (_, FlatSet::Bottom) => (FlatSet::Bottom, FlatSet::Bottom),
|
|
|
|
(_, _) => {
|
|
|
|
// Could attempt some algebraic simplifcations here.
|
|
|
|
(FlatSet::Top, FlatSet::Top)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_operand(
|
|
|
|
&self,
|
|
|
|
op: &Operand<'tcx>,
|
2022-09-23 08:15:14 -05:00
|
|
|
state: &mut State<FlatSet<ScalarTy<'tcx>>>,
|
2022-08-25 11:43:46 -05:00
|
|
|
) -> FlatSet<ImmTy<'tcx>> {
|
|
|
|
let value = match self.handle_operand(op, state) {
|
|
|
|
ValueOrPlace::Value(value) => value,
|
|
|
|
ValueOrPlace::Place(place) => state.get_idx(place, &self.map),
|
|
|
|
};
|
|
|
|
match value {
|
|
|
|
FlatSet::Top => FlatSet::Top,
|
2022-10-23 08:31:46 -05:00
|
|
|
FlatSet::Elem(ScalarTy(scalar, ty)) => self
|
|
|
|
.tcx
|
|
|
|
.layout_of(self.param_env.and(ty))
|
|
|
|
.map(|layout| FlatSet::Elem(ImmTy::from_scalar(scalar, layout)))
|
|
|
|
.unwrap_or(FlatSet::Top),
|
2022-08-25 11:43:46 -05:00
|
|
|
FlatSet::Bottom => FlatSet::Bottom,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 08:15:14 -05:00
|
|
|
fn wrap_scalar(&self, scalar: Scalar, ty: Ty<'tcx>) -> FlatSet<ScalarTy<'tcx>> {
|
|
|
|
FlatSet::Elem(ScalarTy(scalar, ty))
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
|
2022-09-23 08:15:14 -05:00
|
|
|
fn wrap_immediate(&self, imm: Immediate, ty: Ty<'tcx>) -> FlatSet<ScalarTy<'tcx>> {
|
2022-08-25 11:43:46 -05:00
|
|
|
match imm {
|
2022-09-23 08:15:14 -05:00
|
|
|
Immediate::Scalar(scalar) => self.wrap_scalar(scalar, ty),
|
2022-08-25 11:43:46 -05:00
|
|
|
_ => FlatSet::Top,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 08:15:14 -05:00
|
|
|
fn wrap_immty(&self, val: ImmTy<'tcx>) -> FlatSet<ScalarTy<'tcx>> {
|
2022-08-25 11:43:46 -05:00
|
|
|
self.wrap_immediate(*val, val.layout.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CollectAndPatch<'tcx, 'map> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
map: &'map Map,
|
2022-10-24 17:28:41 -05:00
|
|
|
|
|
|
|
/// For a given MIR location, this stores the values of the operands used by that location. In
|
|
|
|
/// particular, this is before the effect, such that the operands of `_1 = _1 + _2` are
|
2022-11-12 13:05:52 -06:00
|
|
|
/// properly captured. (This may become UB soon, but it is currently emitted even by safe code.)
|
2022-09-23 08:15:14 -05:00
|
|
|
before_effect: FxHashMap<(Location, Place<'tcx>), ScalarTy<'tcx>>,
|
2022-10-24 17:28:41 -05:00
|
|
|
|
|
|
|
/// Stores the assigned values for assignments where the Rvalue is constant.
|
2022-09-23 08:15:14 -05:00
|
|
|
assignments: FxHashMap<Location, ScalarTy<'tcx>>,
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, 'map> CollectAndPatch<'tcx, 'map> {
|
|
|
|
fn new(tcx: TyCtxt<'tcx>, map: &'map Map) -> Self {
|
|
|
|
Self { tcx, map, before_effect: FxHashMap::default(), assignments: FxHashMap::default() }
|
|
|
|
}
|
|
|
|
|
2022-09-23 08:15:14 -05:00
|
|
|
fn make_operand(&self, scalar: ScalarTy<'tcx>) -> Operand<'tcx> {
|
2022-08-25 11:43:46 -05:00
|
|
|
Operand::Constant(Box::new(Constant {
|
|
|
|
span: DUMMY_SP,
|
|
|
|
user_ty: None,
|
2022-09-23 08:15:14 -05:00
|
|
|
literal: ConstantKind::Val(ConstValue::Scalar(scalar.0), scalar.1),
|
2022-08-25 11:43:46 -05:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'mir, 'tcx, 'map> ResultsVisitor<'mir, 'tcx> for CollectAndPatch<'tcx, 'map> {
|
2022-09-23 08:15:14 -05:00
|
|
|
type FlowState = State<FlatSet<ScalarTy<'tcx>>>;
|
2022-08-25 11:43:46 -05:00
|
|
|
|
|
|
|
fn visit_statement_before_primary_effect(
|
|
|
|
&mut self,
|
|
|
|
state: &Self::FlowState,
|
|
|
|
statement: &'mir Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
match &statement.kind {
|
|
|
|
StatementKind::Assign(box (_, rvalue)) => {
|
|
|
|
OperandCollector { state, visitor: self }.visit_rvalue(rvalue, location);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement_after_primary_effect(
|
|
|
|
&mut self,
|
|
|
|
state: &Self::FlowState,
|
|
|
|
statement: &'mir Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
match statement.kind {
|
2022-10-19 08:56:58 -05:00
|
|
|
StatementKind::Assign(box (_, Rvalue::Use(Operand::Constant(_)))) => {
|
|
|
|
// Don't overwrite the assignment if it already uses a constant (to keep the span).
|
|
|
|
}
|
2022-08-25 11:43:46 -05:00
|
|
|
StatementKind::Assign(box (place, _)) => match state.get(place.as_ref(), self.map) {
|
|
|
|
FlatSet::Top => (),
|
|
|
|
FlatSet::Elem(value) => {
|
|
|
|
self.assignments.insert(location, value);
|
|
|
|
}
|
|
|
|
FlatSet::Bottom => {
|
2022-10-19 08:56:58 -05:00
|
|
|
// This assignment is either unreachable, or an uninitialized value is assigned.
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_terminator_before_primary_effect(
|
|
|
|
&mut self,
|
|
|
|
state: &Self::FlowState,
|
|
|
|
terminator: &'mir Terminator<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
OperandCollector { state, visitor: self }.visit_terminator(terminator, location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, 'map> MutVisitor<'tcx> for CollectAndPatch<'tcx, 'map> {
|
|
|
|
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
|
|
|
|
if let Some(value) = self.assignments.get(&location) {
|
|
|
|
match &mut statement.kind {
|
|
|
|
StatementKind::Assign(box (_, rvalue)) => {
|
|
|
|
*rvalue = Rvalue::Use(self.make_operand(value.clone()));
|
|
|
|
}
|
|
|
|
_ => bug!("found assignment info for non-assign statement"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.super_statement(statement, location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) {
|
|
|
|
match operand {
|
|
|
|
Operand::Copy(place) | Operand::Move(place) => {
|
|
|
|
if let Some(value) = self.before_effect.get(&(location, *place)) {
|
|
|
|
*operand = self.make_operand(value.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OperandCollector<'tcx, 'map, 'a> {
|
2022-09-23 08:15:14 -05:00
|
|
|
state: &'a State<FlatSet<ScalarTy<'tcx>>>,
|
2022-08-25 11:43:46 -05:00
|
|
|
visitor: &'a mut CollectAndPatch<'tcx, 'map>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, 'map, 'a> Visitor<'tcx> for OperandCollector<'tcx, 'map, 'a> {
|
|
|
|
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
|
|
|
|
match operand {
|
|
|
|
Operand::Copy(place) | Operand::Move(place) => {
|
|
|
|
match self.state.get(place.as_ref(), self.visitor.map) {
|
|
|
|
FlatSet::Top => (),
|
|
|
|
FlatSet::Elem(value) => {
|
|
|
|
self.visitor.before_effect.insert((location, *place), value);
|
|
|
|
}
|
2022-10-05 15:01:33 -05:00
|
|
|
FlatSet::Bottom => (),
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 16:28:54 -06:00
|
|
|
|
|
|
|
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
|
|
|
|
match rvalue {
|
|
|
|
Rvalue::Discriminant(place) => {
|
|
|
|
match self.state.get_discr(place.as_ref(), self.visitor.map) {
|
|
|
|
FlatSet::Top => (),
|
|
|
|
FlatSet::Elem(value) => {
|
|
|
|
self.visitor.before_effect.insert((location, *place), value);
|
|
|
|
}
|
|
|
|
FlatSet::Bottom => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.super_rvalue(rvalue, location),
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DummyMachine;
|
|
|
|
|
|
|
|
impl<'mir, 'tcx> rustc_const_eval::interpret::Machine<'mir, 'tcx> for DummyMachine {
|
|
|
|
rustc_const_eval::interpret::compile_time_machine!(<'mir, 'tcx>);
|
|
|
|
type MemoryKind = !;
|
|
|
|
const PANIC_ON_ALLOC_FAIL: bool = true;
|
|
|
|
|
2022-11-21 10:51:16 -06:00
|
|
|
fn enforce_alignment(_ecx: &InterpCx<'mir, 'tcx, Self>) -> CheckAlignment {
|
2022-08-25 11:43:46 -05:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2023-03-21 03:52:31 -05:00
|
|
|
fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>, _layout: TyAndLayout<'tcx>) -> bool {
|
2022-08-25 11:43:46 -05:00
|
|
|
unimplemented!()
|
|
|
|
}
|
2022-11-22 05:16:33 -06:00
|
|
|
fn alignment_check_failed(
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_has: Align,
|
|
|
|
_required: Align,
|
|
|
|
_check: CheckAlignment,
|
|
|
|
) -> interpret::InterpResult<'tcx, ()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2022-08-25 11:43:46 -05:00
|
|
|
|
|
|
|
fn find_mir_or_eval_fn(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_instance: ty::Instance<'tcx>,
|
|
|
|
_abi: rustc_target::spec::abi::Abi,
|
|
|
|
_args: &[rustc_const_eval::interpret::OpTy<'tcx, Self::Provenance>],
|
|
|
|
_destination: &rustc_const_eval::interpret::PlaceTy<'tcx, Self::Provenance>,
|
|
|
|
_target: Option<BasicBlock>,
|
2022-11-15 12:27:21 -06:00
|
|
|
_unwind: UnwindAction,
|
2022-08-25 11:43:46 -05:00
|
|
|
) -> interpret::InterpResult<'tcx, Option<(&'mir Body<'tcx>, ty::Instance<'tcx>)>> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_intrinsic(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_instance: ty::Instance<'tcx>,
|
|
|
|
_args: &[rustc_const_eval::interpret::OpTy<'tcx, Self::Provenance>],
|
|
|
|
_destination: &rustc_const_eval::interpret::PlaceTy<'tcx, Self::Provenance>,
|
|
|
|
_target: Option<BasicBlock>,
|
2022-11-15 12:27:21 -06:00
|
|
|
_unwind: UnwindAction,
|
2022-08-25 11:43:46 -05:00
|
|
|
) -> interpret::InterpResult<'tcx> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_panic(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_msg: &rustc_middle::mir::AssertMessage<'tcx>,
|
2022-11-15 12:27:21 -06:00
|
|
|
_unwind: UnwindAction,
|
2022-08-25 11:43:46 -05:00
|
|
|
) -> interpret::InterpResult<'tcx> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn binary_ptr_op(
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_bin_op: BinOp,
|
|
|
|
_left: &rustc_const_eval::interpret::ImmTy<'tcx, Self::Provenance>,
|
|
|
|
_right: &rustc_const_eval::interpret::ImmTy<'tcx, Self::Provenance>,
|
|
|
|
) -> interpret::InterpResult<'tcx, (interpret::Scalar<Self::Provenance>, bool, Ty<'tcx>)> {
|
2022-10-19 09:29:01 -05:00
|
|
|
throw_unsup!(Unsupported("".into()))
|
2022-08-25 11:43:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expose_ptr(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_ptr: interpret::Pointer<Self::Provenance>,
|
|
|
|
) -> interpret::InterpResult<'tcx> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_frame_extra(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_frame: rustc_const_eval::interpret::Frame<'mir, 'tcx, Self::Provenance>,
|
|
|
|
) -> interpret::InterpResult<
|
|
|
|
'tcx,
|
|
|
|
rustc_const_eval::interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
|
|
|
|
> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stack<'a>(
|
|
|
|
_ecx: &'a InterpCx<'mir, 'tcx, Self>,
|
|
|
|
) -> &'a [rustc_const_eval::interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>]
|
|
|
|
{
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stack_mut<'a>(
|
|
|
|
_ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
) -> &'a mut Vec<
|
|
|
|
rustc_const_eval::interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
|
|
|
|
> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|