Skip to content

Commit 905f1af

Browse files
committed
Move MaybeUninitializedLocals to the ssa module
1 parent 4281c53 commit 905f1af

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
@@ -602,80 +602,6 @@ impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
602602
}
603603
}
604604

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

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
@@ -104,13 +104,12 @@ use rustc_middle::mir::visit::*;
104104
use rustc_middle::mir::*;
105105
use rustc_middle::ty::layout::HasTypingEnv;
106106
use rustc_middle::ty::{self, Ty, TyCtxt};
107-
use rustc_mir_dataflow::impls::MaybeUninitializedLocals;
108107
use rustc_mir_dataflow::{Analysis, ResultsCursor};
109108
use rustc_span::DUMMY_SP;
110109
use smallvec::SmallVec;
111110
use tracing::{debug, instrument, trace};
112111

113-
use crate::ssa::SsaLocals;
112+
use crate::ssa::{MaybeUninitializedLocals, SsaLocals};
114113

115114
pub(super) struct GVN;
116115

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)