Skip to content

Commit 5c08175

Browse files
committed
next_opaque is no longer an Option
1 parent 75ee137 commit 5c08175

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, typing_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

@@ -233,8 +233,7 @@ struct VnState<'body, 'tcx> {
233233
/// Values evaluated as constants if possible.
234234
evaluated: IndexVec<VnIndex, Option<OpTy<'tcx>>>,
235235
/// Counter to generate different values.
236-
/// This is an option to stop creating opaques during replacement.
237-
next_opaque: Option<usize>,
236+
next_opaque: usize,
238237
/// Cache the value of the `unsized_locals` features, to avoid fetching it repeatedly in a loop.
239238
feature_unsized_locals: bool,
240239
ssa: &'body SsaLocals,
@@ -266,7 +265,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
266265
rev_locals: IndexVec::with_capacity(num_values),
267266
values: FxIndexSet::with_capacity_and_hasher(num_values, Default::default()),
268267
evaluated: IndexVec::with_capacity(num_values),
269-
next_opaque: Some(1),
268+
next_opaque: 1,
270269
feature_unsized_locals: tcx.features().unsized_locals(),
271270
ssa,
272271
dominators,
@@ -287,32 +286,31 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
287286
let evaluated = self.eval_to_const(index);
288287
let _index = self.evaluated.push(evaluated);
289288
debug_assert_eq!(index, _index);
290-
// No need to push to `rev_locals` if we finished listing assignments.
291-
if self.next_opaque.is_some() {
292-
let _index = self.rev_locals.push(SmallVec::new());
293-
debug_assert_eq!(index, _index);
294-
}
289+
let _index = self.rev_locals.push(SmallVec::new());
290+
debug_assert_eq!(index, _index);
295291
}
296292
index
297293
}
298294

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

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

318316
fn get(&self, index: VnIndex) -> &Value<'tcx> {
@@ -333,21 +331,19 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
333331
}
334332
}
335333

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

353349
fn insert_bool(&mut self, flag: bool) -> VnIndex {
@@ -801,7 +797,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
801797
location: Location,
802798
) -> Option<VnIndex> {
803799
match *operand {
804-
Operand::Constant(ref constant) => self.insert_constant(constant.const_),
800+
Operand::Constant(ref constant) => Some(self.insert_constant(constant.const_)),
805801
Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
806802
let value = self.simplify_place_value(place, location)?;
807803
if let Some(const_) = self.try_as_constant(value) {
@@ -837,11 +833,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
837833
Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location),
838834
Rvalue::Ref(_, borrow_kind, ref mut place) => {
839835
self.simplify_place_projection(place, location);
840-
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
836+
return Some(self.new_pointer(*place, AddressKind::Ref(borrow_kind)));
841837
}
842838
Rvalue::RawPtr(mutbl, ref mut place) => {
843839
self.simplify_place_projection(place, location);
844-
return self.new_pointer(*place, AddressKind::Address(mutbl));
840+
return Some(self.new_pointer(*place, AddressKind::Address(mutbl)));
845841
}
846842

847843
// Operations.
@@ -995,7 +991,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
995991

996992
if is_zst {
997993
let ty = rvalue.ty(self.local_decls, tcx);
998-
return self.insert_constant(Const::zero_sized(ty));
994+
return Some(self.insert_constant(Const::zero_sized(ty)));
999995
}
1000996
}
1001997

@@ -1024,11 +1020,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10241020
}
10251021
};
10261022

1027-
let fields: Option<Vec<_>> = field_ops
1023+
let mut fields: Vec<_> = field_ops
10281024
.iter_mut()
1029-
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
1025+
.map(|op| self.simplify_operand(op, location).unwrap_or_else(|| self.new_opaque()))
10301026
.collect();
1031-
let mut fields = fields?;
10321027

10331028
if let AggregateTy::RawPtr { data_pointer_ty, output_pointer_ty } = &mut ty {
10341029
let mut was_updated = false;
@@ -1156,11 +1151,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
11561151
) if let ty::Slice(..) = to.builtin_deref(true).unwrap().kind()
11571152
&& let ty::Array(_, len) = from.builtin_deref(true).unwrap().kind() =>
11581153
{
1159-
return self.insert_constant(Const::from_ty_const(
1154+
return Some(self.insert_constant(Const::from_ty_const(
11601155
*len,
11611156
self.tcx.types.usize,
11621157
self.tcx,
1163-
));
1158+
)));
11641159
}
11651160
_ => Value::UnaryOp(op, arg_index),
11661161
};
@@ -1355,7 +1350,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
13551350
if let CastKind::PointerCoercion(ReifyFnPointer | ClosureFnPointer(_), _) = kind {
13561351
// Each reification of a generic fn may get a different pointer.
13571352
// Do not try to merge them.
1358-
return self.new_opaque();
1353+
return Some(self.new_opaque());
13591354
}
13601355

13611356
let mut was_updated = false;
@@ -1419,11 +1414,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14191414
// Trivial case: we are fetching a statically known length.
14201415
let place_ty = place.ty(self.local_decls, self.tcx).ty;
14211416
if let ty::Array(_, len) = place_ty.kind() {
1422-
return self.insert_constant(Const::from_ty_const(
1417+
return Some(self.insert_constant(Const::from_ty_const(
14231418
*len,
14241419
self.tcx.types.usize,
14251420
self.tcx,
1426-
));
1421+
)));
14271422
}
14281423

14291424
let mut inner = self.simplify_place_value(place, location)?;
@@ -1445,11 +1440,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14451440
&& let Some(to) = to.builtin_deref(true)
14461441
&& let ty::Slice(..) = to.kind()
14471442
{
1448-
return self.insert_constant(Const::from_ty_const(
1443+
return Some(self.insert_constant(Const::from_ty_const(
14491444
*len,
14501445
self.tcx.types.usize,
14511446
self.tcx,
1452-
));
1447+
)));
14531448
}
14541449

14551450
// Fallback: a symbolic `Len`.
@@ -1627,7 +1622,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
16271622
// `local` as reusable if we have an exact type match.
16281623
&& self.local_decls[local].ty == rvalue.ty(self.local_decls, self.tcx)
16291624
{
1630-
let value = value.or_else(|| self.new_opaque()).unwrap();
1625+
let value = value.unwrap_or_else(|| self.new_opaque());
16311626
self.assign(local, value);
16321627
Some(value)
16331628
} else {
@@ -1654,7 +1649,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
16541649
&& let Some(local) = destination.as_local()
16551650
&& self.ssa.is_ssa(local)
16561651
{
1657-
let opaque = self.new_opaque().unwrap();
1652+
let opaque = self.new_opaque();
16581653
self.assign(local, opaque);
16591654
}
16601655
self.super_terminator(terminator, location);

0 commit comments

Comments
 (0)