Skip to content

Commit 86ba355

Browse files
committed
Move MaybeUninitializedLocals to the ssa module
1 parent 59e3caf commit 86ba355

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
@@ -108,13 +108,12 @@ use rustc_middle::mir::visit::*;
108108
use rustc_middle::mir::*;
109109
use rustc_middle::ty::layout::HasTypingEnv;
110110
use rustc_middle::ty::{self, Ty, TyCtxt};
111-
use rustc_mir_dataflow::impls::MaybeUninitializedLocals;
112111
use rustc_mir_dataflow::{Analysis, ResultsCursor};
113112
use rustc_span::DUMMY_SP;
114113
use smallvec::SmallVec;
115114
use tracing::{debug, instrument, trace};
116115

117-
use crate::ssa::SsaLocals;
116+
use crate::ssa::{MaybeUninitializedLocals, SsaLocals};
118117

119118
pub(super) struct GVN;
120119

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

0 commit comments

Comments
 (0)