2019-09-25 12:30:25 -07:00
|
|
|
//! Propagate `Qualif`s between locals and query the results.
|
|
|
|
//!
|
2019-10-23 12:10:08 -07:00
|
|
|
//! This contains the dataflow analysis used to track `Qualif`s on complex control-flow graphs.
|
2019-09-25 12:30:25 -07:00
|
|
|
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::visit::Visitor;
|
|
|
|
use rustc_middle::mir::{self, BasicBlock, Local, Location};
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2020-03-23 14:02:58 +01:00
|
|
|
use super::{qualifs, ConstCx, Qualif};
|
2020-02-28 22:02:20 -08:00
|
|
|
use crate::dataflow;
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
/// A `Visitor` that propagates qualifs between locals. This defines the transfer function of
|
2019-10-23 12:10:08 -07:00
|
|
|
/// `FlowSensitiveAnalysis`.
|
2019-09-17 16:25:40 -07:00
|
|
|
///
|
|
|
|
/// This transfer does nothing when encountering an indirect assignment. Consumers should rely on
|
2020-02-12 13:42:31 -08:00
|
|
|
/// the `MaybeMutBorrowedLocals` dataflow pass to see if a `Local` may have become qualified via
|
2019-09-17 16:25:40 -07:00
|
|
|
/// an indirect assignment or function call.
|
|
|
|
struct TransferFunction<'a, 'mir, 'tcx, Q> {
|
2020-03-23 14:02:58 +01:00
|
|
|
ccx: &'a ConstCx<'mir, 'tcx>,
|
2019-09-17 16:25:40 -07:00
|
|
|
qualifs_per_local: &'a mut BitSet<Local>,
|
|
|
|
|
|
|
|
_qualif: PhantomData<Q>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Q> TransferFunction<'a, 'mir, 'tcx, Q>
|
|
|
|
where
|
|
|
|
Q: Qualif,
|
|
|
|
{
|
2020-03-23 14:02:58 +01:00
|
|
|
fn new(ccx: &'a ConstCx<'mir, 'tcx>, qualifs_per_local: &'a mut BitSet<Local>) -> Self {
|
|
|
|
TransferFunction { ccx, qualifs_per_local, _qualif: PhantomData }
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn initialize_state(&mut self) {
|
|
|
|
self.qualifs_per_local.clear();
|
|
|
|
|
2020-03-23 14:02:58 +01:00
|
|
|
for arg in self.ccx.body.args_iter() {
|
|
|
|
let arg_ty = self.ccx.body.local_decls[arg].ty;
|
|
|
|
if Q::in_any_value_of_ty(self.ccx, arg_ty) {
|
2019-09-17 16:25:40 -07:00
|
|
|
self.qualifs_per_local.insert(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assign_qualif_direct(&mut self, place: &mir::Place<'tcx>, value: bool) {
|
|
|
|
debug_assert!(!place.is_indirect());
|
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
match (value, place.as_ref()) {
|
2019-12-11 16:50:03 -03:00
|
|
|
(true, mir::PlaceRef { local, .. }) => {
|
2020-01-14 02:10:05 -03:00
|
|
|
self.qualifs_per_local.insert(local);
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// For now, we do not clear the qualif if a local is overwritten in full by
|
|
|
|
// an unqualified rvalue (e.g. `y = 5`). This is to be consistent
|
|
|
|
// with aggregates where we overwrite all fields with assignments, which would not
|
|
|
|
// get this feature.
|
2019-12-11 16:50:03 -03:00
|
|
|
(false, mir::PlaceRef { local: _, projection: &[] }) => {
|
2019-09-17 16:25:40 -07:00
|
|
|
// self.qualifs_per_local.remove(*local);
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_call_return_effect(
|
|
|
|
&mut self,
|
|
|
|
_block: BasicBlock,
|
2019-12-13 13:20:16 -08:00
|
|
|
_func: &mir::Operand<'tcx>,
|
|
|
|
_args: &[mir::Operand<'tcx>],
|
2020-03-31 13:54:20 -03:00
|
|
|
return_place: mir::Place<'tcx>,
|
2019-09-17 16:25:40 -07:00
|
|
|
) {
|
2019-12-13 13:20:16 -08:00
|
|
|
// We cannot reason about another function's internals, so use conservative type-based
|
|
|
|
// qualification for the result of a function call.
|
2020-03-23 14:02:58 +01:00
|
|
|
let return_ty = return_place.ty(self.ccx.body, self.ccx.tcx).ty;
|
|
|
|
let qualif = Q::in_any_value_of_ty(self.ccx, return_ty);
|
2019-12-13 13:20:16 -08:00
|
|
|
|
2019-09-17 16:25:40 -07:00
|
|
|
if !return_place.is_indirect() {
|
2020-03-31 13:54:20 -03:00
|
|
|
self.assign_qualif_direct(&return_place, qualif);
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Q> Visitor<'tcx> for TransferFunction<'_, '_, 'tcx, Q>
|
|
|
|
where
|
|
|
|
Q: Qualif,
|
|
|
|
{
|
|
|
|
fn visit_operand(&mut self, operand: &mir::Operand<'tcx>, location: Location) {
|
|
|
|
self.super_operand(operand, location);
|
|
|
|
|
|
|
|
if !Q::IS_CLEARED_ON_MOVE {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a local with no projections is moved from (e.g. `x` in `y = x`), record that
|
|
|
|
// it no longer needs to be dropped.
|
2019-10-20 16:09:36 -04:00
|
|
|
if let mir::Operand::Move(place) = operand {
|
|
|
|
if let Some(local) = place.as_local() {
|
|
|
|
self.qualifs_per_local.remove(local);
|
|
|
|
}
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_assign(
|
|
|
|
&mut self,
|
|
|
|
place: &mir::Place<'tcx>,
|
|
|
|
rvalue: &mir::Rvalue<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
2019-12-13 13:20:16 -08:00
|
|
|
let qualif = qualifs::in_rvalue::<Q, _>(
|
2020-03-23 14:02:58 +01:00
|
|
|
self.ccx,
|
2019-12-13 13:20:16 -08:00
|
|
|
&mut |l| self.qualifs_per_local.contains(l),
|
|
|
|
rvalue,
|
|
|
|
);
|
2019-09-17 16:25:40 -07:00
|
|
|
if !place.is_indirect() {
|
|
|
|
self.assign_qualif_direct(place, qualif);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to assign qualifs to the left-hand side before visiting `rvalue` since
|
|
|
|
// qualifs can be cleared on move.
|
|
|
|
self.super_assign(place, rvalue, location);
|
|
|
|
}
|
|
|
|
|
2020-05-31 12:13:29 +02:00
|
|
|
fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
|
2019-09-17 16:25:40 -07:00
|
|
|
// The effect of assignment to the return place in `TerminatorKind::Call` is not applied
|
|
|
|
// here; that occurs in `apply_call_return_effect`.
|
|
|
|
|
2020-06-10 09:56:54 +02:00
|
|
|
if let mir::TerminatorKind::DropAndReplace { value, place, .. } = &terminator.kind {
|
2019-12-13 13:20:16 -08:00
|
|
|
let qualif = qualifs::in_operand::<Q, _>(
|
2020-03-23 14:02:58 +01:00
|
|
|
self.ccx,
|
2019-12-13 13:20:16 -08:00
|
|
|
&mut |l| self.qualifs_per_local.contains(l),
|
|
|
|
value,
|
|
|
|
);
|
|
|
|
|
2020-06-10 09:56:54 +02:00
|
|
|
if !place.is_indirect() {
|
|
|
|
self.assign_qualif_direct(place, qualif);
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to assign qualifs to the dropped location before visiting the operand that
|
|
|
|
// replaces it since qualifs can be cleared on move.
|
2020-05-31 12:13:29 +02:00
|
|
|
self.super_terminator(terminator, location);
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The dataflow analysis used to propagate qualifs on arbitrary CFGs.
|
|
|
|
pub(super) struct FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q> {
|
2020-03-23 14:02:58 +01:00
|
|
|
ccx: &'a ConstCx<'mir, 'tcx>,
|
2019-09-17 16:25:40 -07:00
|
|
|
_qualif: PhantomData<Q>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'mir, 'tcx, Q> FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>
|
|
|
|
where
|
|
|
|
Q: Qualif,
|
|
|
|
{
|
2020-03-23 14:02:58 +01:00
|
|
|
pub(super) fn new(_: Q, ccx: &'a ConstCx<'mir, 'tcx>) -> Self {
|
|
|
|
FlowSensitiveAnalysis { ccx, _qualif: PhantomData }
|
2019-10-23 12:10:08 -07:00
|
|
|
}
|
|
|
|
|
2019-09-17 16:25:40 -07:00
|
|
|
fn transfer_function(
|
|
|
|
&self,
|
|
|
|
state: &'a mut BitSet<Local>,
|
|
|
|
) -> TransferFunction<'a, 'mir, 'tcx, Q> {
|
2020-03-23 14:02:58 +01:00
|
|
|
TransferFunction::<Q>::new(self.ccx, state)
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:02:20 -08:00
|
|
|
impl<Q> dataflow::BottomValue for FlowSensitiveAnalysis<'_, '_, '_, Q> {
|
2019-09-17 16:25:40 -07:00
|
|
|
const BOTTOM_VALUE: bool = false;
|
|
|
|
}
|
|
|
|
|
2019-11-11 11:50:27 -08:00
|
|
|
impl<Q> dataflow::AnalysisDomain<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
|
2019-09-17 16:25:40 -07:00
|
|
|
where
|
|
|
|
Q: Qualif,
|
|
|
|
{
|
|
|
|
type Idx = Local;
|
|
|
|
|
2019-09-29 21:43:09 -07:00
|
|
|
const NAME: &'static str = Q::ANALYSIS_NAME;
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
fn bits_per_block(&self, body: &mir::Body<'tcx>) -> usize {
|
|
|
|
body.local_decls.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn initialize_start_block(&self, _body: &mir::Body<'tcx>, state: &mut BitSet<Self::Idx>) {
|
|
|
|
self.transfer_function(state).initialize_state();
|
|
|
|
}
|
2019-11-11 11:50:27 -08:00
|
|
|
}
|
2019-09-17 16:25:40 -07:00
|
|
|
|
2019-11-11 11:50:27 -08:00
|
|
|
impl<Q> dataflow::Analysis<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
|
|
|
|
where
|
|
|
|
Q: Qualif,
|
|
|
|
{
|
2019-09-17 16:25:40 -07:00
|
|
|
fn apply_statement_effect(
|
|
|
|
&self,
|
|
|
|
state: &mut BitSet<Self::Idx>,
|
|
|
|
statement: &mir::Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
self.transfer_function(state).visit_statement(statement, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_terminator_effect(
|
|
|
|
&self,
|
|
|
|
state: &mut BitSet<Self::Idx>,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
self.transfer_function(state).visit_terminator(terminator, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_call_return_effect(
|
|
|
|
&self,
|
|
|
|
state: &mut BitSet<Self::Idx>,
|
|
|
|
block: BasicBlock,
|
|
|
|
func: &mir::Operand<'tcx>,
|
|
|
|
args: &[mir::Operand<'tcx>],
|
2020-03-31 13:54:20 -03:00
|
|
|
return_place: mir::Place<'tcx>,
|
2019-09-17 16:25:40 -07:00
|
|
|
) {
|
|
|
|
self.transfer_function(state).apply_call_return_effect(block, func, args, return_place)
|
|
|
|
}
|
|
|
|
}
|