Skip to content

Commit 5124d36

Browse files
committed
Move MaybeUninitializedLocals to the ssa module
1 parent 56cda8d commit 5124d36

File tree

5 files changed

+79
-83
lines changed

5 files changed

+79
-83
lines changed

compiler/rustc_mir_dataflow/src/impls/initialized.rs

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -605,80 +605,6 @@ impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
605605
}
606606
}
607607

608-
/// A dataflow analysis that tracks locals that are maybe uninitialized.
609-
///
610-
/// This is a simpler analysis than `MaybeUninitializedPlaces`, because it does not track
611-
/// individual fields.
612-
pub struct MaybeUninitializedLocals;
613-
614-
impl MaybeUninitializedLocals {
615-
pub fn new() -> Self {
616-
Self {}
617-
}
618-
}
619-
620-
impl<'tcx> Analysis<'tcx> for MaybeUninitializedLocals {
621-
type Domain = DenseBitSet<mir::Local>;
622-
623-
const NAME: &'static str = "maybe_uninit_locals";
624-
625-
fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
626-
// bottom = all locals are initialized.
627-
DenseBitSet::new_empty(body.local_decls.len())
628-
}
629-
630-
fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
631-
// All locals start as uninitialized...
632-
state.insert_all();
633-
// ...except for arguments, which are definitely initialized.
634-
for arg in body.args_iter() {
635-
state.remove(arg);
636-
}
637-
}
638-
639-
fn apply_primary_statement_effect(
640-
&mut self,
641-
state: &mut Self::Domain,
642-
statement: &mir::Statement<'tcx>,
643-
_location: Location,
644-
) {
645-
match statement.kind {
646-
// An assignment makes a local initialized.
647-
mir::StatementKind::Assign(box (place, _)) => {
648-
if let Some(local) = place.as_local() {
649-
state.remove(local);
650-
}
651-
}
652-
// Deinit makes the local uninitialized.
653-
mir::StatementKind::Deinit(box place) => {
654-
// A deinit makes a local uninitialized.
655-
if let Some(local) = place.as_local() {
656-
state.insert(local);
657-
}
658-
}
659-
// Storage{Live,Dead} makes a local uninitialized.
660-
mir::StatementKind::StorageLive(local) | mir::StatementKind::StorageDead(local) => {
661-
state.insert(local);
662-
}
663-
_ => {}
664-
}
665-
}
666-
667-
fn apply_call_return_effect(
668-
&mut self,
669-
state: &mut Self::Domain,
670-
_block: mir::BasicBlock,
671-
return_places: CallReturnPlaces<'_, 'tcx>,
672-
) {
673-
// The return place of a call is initialized.
674-
return_places.for_each(|place| {
675-
if let Some(local) = place.as_local() {
676-
state.remove(local);
677-
}
678-
});
679-
}
680-
}
681-
682608
/// There can be many more `InitIndex` than there are locals in a MIR body.
683609
/// We use a mixed bitset to avoid paying too high a memory footprint.
684610
pub type EverInitializedPlacesDomain = MixedBitSet<InitIndex>;

compiler/rustc_mir_dataflow/src/impls/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod storage_liveness;
66
pub use self::borrowed_locals::{MaybeBorrowedLocals, borrowed_locals};
77
pub use self::initialized::{
88
EverInitializedPlaces, EverInitializedPlacesDomain, MaybeInitializedPlaces,
9-
MaybeUninitializedLocals, MaybeUninitializedPlaces, MaybeUninitializedPlacesDomain,
9+
MaybeUninitializedPlaces, MaybeUninitializedPlacesDomain,
1010
};
1111
pub use self::liveness::{
1212
DefUse, MaybeLiveLocals, MaybeTransitiveLiveLocals,

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use rustc_index::bit_set::DenseBitSet;
33
use rustc_middle::mir::visit::*;
44
use rustc_middle::mir::*;
55
use rustc_middle::ty::TyCtxt;
6-
use rustc_mir_dataflow::impls::MaybeUninitializedLocals;
76
use rustc_mir_dataflow::{Analysis, ResultsCursor};
87
use tracing::{debug, instrument};
98

10-
use crate::ssa::SsaLocals;
9+
use crate::ssa::{MaybeUninitializedLocals, SsaLocals};
1110

1211
/// Unify locals that copy each other.
1312
///
@@ -37,10 +36,7 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
3736
debug!(copy_classes = ?ssa.copy_classes());
3837

3938
let mut any_replacement = false;
40-
let fully_moved = fully_moved_locals(&ssa, body);
41-
debug!(?fully_moved);
42-
43-
let mut storage_to_remove = DenseBitSet::new_empty(fully_moved.domain_size());
39+
let mut storage_to_remove = DenseBitSet::new_empty(body.local_decls.len());
4440

4541
for (local, &head) in ssa.copy_classes().iter_enumerated() {
4642
if local != head {

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,12 @@ use rustc_middle::mir::visit::*;
107107
use rustc_middle::mir::*;
108108
use rustc_middle::ty::layout::HasTypingEnv;
109109
use rustc_middle::ty::{self, Ty, TyCtxt};
110-
use rustc_mir_dataflow::impls::MaybeUninitializedLocals;
111110
use rustc_mir_dataflow::{Analysis, ResultsCursor};
112111
use rustc_span::DUMMY_SP;
113112
use smallvec::SmallVec;
114113
use tracing::{debug, instrument, trace};
115114

116-
use crate::ssa::SsaLocals;
115+
use crate::ssa::{MaybeUninitializedLocals, SsaLocals};
117116

118117
pub(super) struct GVN;
119118

compiler/rustc_mir_transform/src/ssa.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_middle::middle::resolve_bound_vars::Set1;
1414
use rustc_middle::mir::visit::*;
1515
use rustc_middle::mir::*;
1616
use rustc_middle::ty::{self, TyCtxt};
17+
use rustc_mir_dataflow::Analysis;
1718
use tracing::{debug, instrument, trace};
1819

1920
pub(super) struct SsaLocals {
@@ -393,3 +394,77 @@ impl StorageLiveLocals {
393394
matches!(self.storage_live[local], Set1::One(_))
394395
}
395396
}
397+
398+
/// A dataflow analysis that tracks locals that are maybe uninitialized.
399+
///
400+
/// This is a simpler analysis than `MaybeUninitializedPlaces`, because it does not track
401+
/// individual fields.
402+
pub(crate) struct MaybeUninitializedLocals;
403+
404+
impl MaybeUninitializedLocals {
405+
pub(crate) fn new() -> Self {
406+
Self {}
407+
}
408+
}
409+
410+
impl<'tcx> Analysis<'tcx> for MaybeUninitializedLocals {
411+
type Domain = DenseBitSet<Local>;
412+
413+
const NAME: &'static str = "maybe_uninit_locals";
414+
415+
fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
416+
// bottom = all locals are initialized.
417+
DenseBitSet::new_empty(body.local_decls.len())
418+
}
419+
420+
fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
421+
// All locals start as uninitialized...
422+
state.insert_all();
423+
// ...except for arguments, which are definitely initialized.
424+
for arg in body.args_iter() {
425+
state.remove(arg);
426+
}
427+
}
428+
429+
fn apply_primary_statement_effect(
430+
&mut self,
431+
state: &mut Self::Domain,
432+
statement: &Statement<'tcx>,
433+
_location: Location,
434+
) {
435+
match statement.kind {
436+
// An assignment makes a local initialized.
437+
StatementKind::Assign(box (place, _)) => {
438+
if let Some(local) = place.as_local() {
439+
state.remove(local);
440+
}
441+
}
442+
// Deinit makes the local uninitialized.
443+
StatementKind::Deinit(box place) => {
444+
// A deinit makes a local uninitialized.
445+
if let Some(local) = place.as_local() {
446+
state.insert(local);
447+
}
448+
}
449+
// Storage{Live,Dead} makes a local uninitialized.
450+
StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
451+
state.insert(local);
452+
}
453+
_ => {}
454+
}
455+
}
456+
457+
fn apply_call_return_effect(
458+
&mut self,
459+
state: &mut Self::Domain,
460+
_block: BasicBlock,
461+
return_places: CallReturnPlaces<'_, 'tcx>,
462+
) {
463+
// The return place of a call is initialized.
464+
return_places.for_each(|place| {
465+
if let Some(local) = place.as_local() {
466+
state.remove(local);
467+
}
468+
});
469+
}
470+
}

0 commit comments

Comments
 (0)