Skip to content

Commit 3e74727

Browse files
Use Option<Location> to indicate the first assignment
This is the same size as `Location`. Using an invalid location is premature optimization.
1 parent f0ed021 commit 3e74727

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/librustc_codegen_ssa/mir/analyze.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::FunctionCx;
55
use crate::traits::*;
66
use rustc_data_structures::graph::dominators::Dominators;
77
use rustc_index::bit_set::BitSet;
8-
use rustc_index::vec::{Idx, IndexVec};
8+
use rustc_index::vec::IndexVec;
99
use rustc_middle::mir::traversal;
1010
use rustc_middle::mir::visit::{
1111
MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext, Visitor,
@@ -40,37 +40,31 @@ struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
4040
fx: &'mir FunctionCx<'a, 'tcx, Bx>,
4141
dominators: Dominators<mir::BasicBlock>,
4242
non_ssa_locals: BitSet<mir::Local>,
43-
// The location of the first visited direct assignment to each
44-
// local, or an invalid location (out of bounds `block` index).
45-
first_assignment: IndexVec<mir::Local, Location>,
43+
44+
/// The location of the first visited direct assignment to each local.
45+
first_assignment: IndexVec<mir::Local, Option<Location>>,
4646
}
4747

4848
impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
4949
fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>) -> Self {
50-
let invalid_location = mir::BasicBlock::new(fx.mir.basic_blocks().len()).start_location();
5150
let dominators = fx.mir.dominators();
5251
let mut analyzer = LocalAnalyzer {
5352
fx,
5453
dominators,
5554
non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
56-
first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls),
55+
first_assignment: IndexVec::from_elem(None, &fx.mir.local_decls),
5756
};
5857

5958
// Arguments get assigned to by means of the function being called
6059
for arg in fx.mir.args_iter() {
61-
analyzer.first_assignment[arg] = mir::START_BLOCK.start_location();
60+
analyzer.assign(arg, mir::START_BLOCK.start_location());
6261
}
6362

6463
analyzer
6564
}
6665

6766
fn first_assignment(&self, local: mir::Local) -> Option<Location> {
68-
let location = self.first_assignment[local];
69-
if location.block.index() < self.fx.mir.basic_blocks().len() {
70-
Some(location)
71-
} else {
72-
None
73-
}
67+
self.first_assignment[local]
7468
}
7569

7670
fn not_ssa(&mut self, local: mir::Local) {
@@ -79,10 +73,10 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
7973
}
8074

8175
fn assign(&mut self, local: mir::Local, location: Location) {
82-
if self.first_assignment(local).is_some() {
76+
if self.first_assignment[local].is_some() {
8377
self.not_ssa(local);
8478
} else {
85-
self.first_assignment[local] = location;
79+
self.first_assignment[local] = Some(location);
8680
}
8781
}
8882

0 commit comments

Comments
 (0)