Skip to content

Commit a60983c

Browse files
committed
next_opaque is no longer an Option
1 parent 6542c21 commit a60983c

File tree

1 file changed

+35
-40
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+35
-40
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+35-40
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
130130
let mut state = VnState::new(tcx, body, param_env, &ssa, dominators, &body.local_decls);
131131

132132
for local in body.args_iter().filter(|&local| ssa.is_ssa(local)) {
133-
let opaque = state.new_opaque().unwrap();
133+
let opaque = state.new_opaque();
134134
state.assign(local, opaque);
135135
}
136136

@@ -234,8 +234,7 @@ struct VnState<'body, 'tcx> {
234234
/// Values evaluated as constants if possible.
235235
evaluated: IndexVec<VnIndex, Option<OpTy<'tcx>>>,
236236
/// Counter to generate different values.
237-
/// This is an option to stop creating opaques during replacement.
238-
next_opaque: Option<usize>,
237+
next_opaque: usize,
239238
/// Cache the value of the `unsized_locals` features, to avoid fetching it repeatedly in a loop.
240239
feature_unsized_locals: bool,
241240
ssa: &'body SsaLocals,
@@ -268,7 +267,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
268267
rev_locals: IndexVec::with_capacity(num_values),
269268
values: FxIndexSet::with_capacity_and_hasher(num_values, Default::default()),
270269
evaluated: IndexVec::with_capacity(num_values),
271-
next_opaque: Some(1),
270+
next_opaque: 1,
272271
feature_unsized_locals: tcx.features().unsized_locals(),
273272
ssa,
274273
dominators,
@@ -285,32 +284,31 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
285284
let evaluated = self.eval_to_const(index);
286285
let _index = self.evaluated.push(evaluated);
287286
debug_assert_eq!(index, _index);
288-
// No need to push to `rev_locals` if we finished listing assignments.
289-
if self.next_opaque.is_some() {
290-
let _index = self.rev_locals.push(SmallVec::new());
291-
debug_assert_eq!(index, _index);
292-
}
287+
let _index = self.rev_locals.push(SmallVec::new());
288+
debug_assert_eq!(index, _index);
293289
}
294290
index
295291
}
296292

293+
fn next_opaque(&mut self) -> usize {
294+
let next_opaque = self.next_opaque;
295+
self.next_opaque += 1;
296+
next_opaque
297+
}
298+
297299
/// Create a new `Value` for which we have no information at all, except that it is distinct
298300
/// from all the others.
299301
#[instrument(level = "trace", skip(self), ret)]
300-
fn new_opaque(&mut self) -> Option<VnIndex> {
301-
let next_opaque = self.next_opaque.as_mut()?;
302-
let value = Value::Opaque(*next_opaque);
303-
*next_opaque += 1;
304-
Some(self.insert(value))
302+
fn new_opaque(&mut self) -> VnIndex {
303+
let value = Value::Opaque(self.next_opaque());
304+
self.insert(value)
305305
}
306306

307307
/// Create a new `Value::Address` distinct from all the others.
308308
#[instrument(level = "trace", skip(self), ret)]
309-
fn new_pointer(&mut self, place: Place<'tcx>, kind: AddressKind) -> Option<VnIndex> {
310-
let next_opaque = self.next_opaque.as_mut()?;
311-
let value = Value::Address { place, kind, provenance: *next_opaque };
312-
*next_opaque += 1;
313-
Some(self.insert(value))
309+
fn new_pointer(&mut self, place: Place<'tcx>, kind: AddressKind) -> VnIndex {
310+
let value = Value::Address { place, kind, provenance: self.next_opaque() };
311+
self.insert(value)
314312
}
315313

316314
fn get(&self, index: VnIndex) -> &Value<'tcx> {
@@ -331,21 +329,19 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
331329
}
332330
}
333331

334-
fn insert_constant(&mut self, value: Const<'tcx>) -> Option<VnIndex> {
332+
fn insert_constant(&mut self, value: Const<'tcx>) -> VnIndex {
335333
let disambiguator = if value.is_deterministic() {
336334
// The constant is deterministic, no need to disambiguate.
337335
0
338336
} else {
339337
// Multiple mentions of this constant will yield different values,
340338
// so assign a different `disambiguator` to ensure they do not get the same `VnIndex`.
341-
let next_opaque = self.next_opaque.as_mut()?;
342-
let disambiguator = *next_opaque;
343-
*next_opaque += 1;
339+
let disambiguator = self.next_opaque();
344340
// `disambiguator: 0` means deterministic.
345341
debug_assert_ne!(disambiguator, 0);
346342
disambiguator
347343
};
348-
Some(self.insert(Value::Constant { value, disambiguator }))
344+
self.insert(Value::Constant { value, disambiguator })
349345
}
350346

351347
fn insert_bool(&mut self, flag: bool) -> VnIndex {
@@ -797,7 +793,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
797793
location: Location,
798794
) -> Option<VnIndex> {
799795
match *operand {
800-
Operand::Constant(ref constant) => self.insert_constant(constant.const_),
796+
Operand::Constant(ref constant) => Some(self.insert_constant(constant.const_)),
801797
Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
802798
let value = self.simplify_place_value(place, location)?;
803799
if let Some(const_) = self.try_as_constant(value) {
@@ -833,11 +829,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
833829
Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
834830
Rvalue::Ref(_, borrow_kind, ref mut place) => {
835831
self.simplify_place_projection(place, location);
836-
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
832+
return Some(self.new_pointer(*place, AddressKind::Ref(borrow_kind)));
837833
}
838834
Rvalue::RawPtr(mutbl, ref mut place) => {
839835
self.simplify_place_projection(place, location);
840-
return self.new_pointer(*place, AddressKind::Address(mutbl));
836+
return Some(self.new_pointer(*place, AddressKind::Address(mutbl)));
841837
}
842838

843839
// Operations.
@@ -991,7 +987,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
991987

992988
if is_zst {
993989
let ty = rvalue.ty(self.local_decls, tcx);
994-
return self.insert_constant(Const::zero_sized(ty));
990+
return Some(self.insert_constant(Const::zero_sized(ty)));
995991
}
996992
}
997993

@@ -1020,11 +1016,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10201016
}
10211017
};
10221018

1023-
let fields: Option<Vec<_>> = field_ops
1019+
let mut fields: Vec<_> = field_ops
10241020
.iter_mut()
1025-
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
1021+
.map(|op| self.simplify_operand(op, location).unwrap_or_else(|| self.new_opaque()))
10261022
.collect();
1027-
let mut fields = fields?;
10281023

10291024
if let AggregateTy::RawPtr { data_pointer_ty, output_pointer_ty } = &mut ty {
10301025
let mut was_updated = false;
@@ -1152,11 +1147,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
11521147
) if let ty::Slice(..) = to.builtin_deref(true).unwrap().kind()
11531148
&& let ty::Array(_, len) = from.builtin_deref(true).unwrap().kind() =>
11541149
{
1155-
return self.insert_constant(Const::from_ty_const(
1150+
return Some(self.insert_constant(Const::from_ty_const(
11561151
*len,
11571152
self.tcx.types.usize,
11581153
self.tcx,
1159-
));
1154+
)));
11601155
}
11611156
_ => Value::UnaryOp(op, arg_index),
11621157
};
@@ -1351,7 +1346,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
13511346
if let CastKind::PointerCoercion(ReifyFnPointer | ClosureFnPointer(_), _) = kind {
13521347
// Each reification of a generic fn may get a different pointer.
13531348
// Do not try to merge them.
1354-
return self.new_opaque();
1349+
return Some(self.new_opaque());
13551350
}
13561351

13571352
let mut was_updated = false;
@@ -1415,11 +1410,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14151410
// Trivial case: we are fetching a statically known length.
14161411
let place_ty = place.ty(self.local_decls, self.tcx).ty;
14171412
if let ty::Array(_, len) = place_ty.kind() {
1418-
return self.insert_constant(Const::from_ty_const(
1413+
return Some(self.insert_constant(Const::from_ty_const(
14191414
*len,
14201415
self.tcx.types.usize,
14211416
self.tcx,
1422-
));
1417+
)));
14231418
}
14241419

14251420
let mut inner = self.simplify_place_value(place, location)?;
@@ -1441,11 +1436,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14411436
&& let Some(to) = to.builtin_deref(true)
14421437
&& let ty::Slice(..) = to.kind()
14431438
{
1444-
return self.insert_constant(Const::from_ty_const(
1439+
return Some(self.insert_constant(Const::from_ty_const(
14451440
*len,
14461441
self.tcx.types.usize,
14471442
self.tcx,
1448-
));
1443+
)));
14491444
}
14501445

14511446
// Fallback: a symbolic `Len`.
@@ -1622,7 +1617,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
16221617
// `local` as reusable if we have an exact type match.
16231618
&& self.local_decls[local].ty == rvalue.ty(self.local_decls, self.tcx)
16241619
{
1625-
let value = value.or_else(|| self.new_opaque()).unwrap();
1620+
let value = value.unwrap_or_else(|| self.new_opaque());
16261621
self.assign(local, value);
16271622
Some(value)
16281623
} else {
@@ -1649,7 +1644,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
16491644
&& let Some(local) = destination.as_local()
16501645
&& self.ssa.is_ssa(local)
16511646
{
1652-
let opaque = self.new_opaque().unwrap();
1647+
let opaque = self.new_opaque();
16531648
self.assign(local, opaque);
16541649
}
16551650
self.super_terminator(terminator, location);

0 commit comments

Comments
 (0)