@@ -130,7 +130,7 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
130
130
let mut state = VnState :: new ( tcx, body, param_env, & ssa, dominators, & body. local_decls ) ;
131
131
132
132
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 ( ) ;
134
134
state. assign ( local, opaque) ;
135
135
}
136
136
@@ -234,8 +234,7 @@ struct VnState<'body, 'tcx> {
234
234
/// Values evaluated as constants if possible.
235
235
evaluated : IndexVec < VnIndex , Option < OpTy < ' tcx > > > ,
236
236
/// 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 ,
239
238
/// Cache the value of the `unsized_locals` features, to avoid fetching it repeatedly in a loop.
240
239
feature_unsized_locals : bool ,
241
240
ssa : & ' body SsaLocals ,
@@ -268,7 +267,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
268
267
rev_locals : IndexVec :: with_capacity ( num_values) ,
269
268
values : FxIndexSet :: with_capacity_and_hasher ( num_values, Default :: default ( ) ) ,
270
269
evaluated : IndexVec :: with_capacity ( num_values) ,
271
- next_opaque : Some ( 1 ) ,
270
+ next_opaque : 1 ,
272
271
feature_unsized_locals : tcx. features ( ) . unsized_locals ( ) ,
273
272
ssa,
274
273
dominators,
@@ -285,32 +284,31 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
285
284
let evaluated = self . eval_to_const ( index) ;
286
285
let _index = self . evaluated . push ( evaluated) ;
287
286
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) ;
293
289
}
294
290
index
295
291
}
296
292
293
+ fn next_opaque ( & mut self ) -> usize {
294
+ let next_opaque = self . next_opaque ;
295
+ self . next_opaque += 1 ;
296
+ next_opaque
297
+ }
298
+
297
299
/// Create a new `Value` for which we have no information at all, except that it is distinct
298
300
/// from all the others.
299
301
#[ 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)
305
305
}
306
306
307
307
/// Create a new `Value::Address` distinct from all the others.
308
308
#[ 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)
314
312
}
315
313
316
314
fn get ( & self , index : VnIndex ) -> & Value < ' tcx > {
@@ -331,21 +329,19 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
331
329
}
332
330
}
333
331
334
- fn insert_constant ( & mut self , value : Const < ' tcx > ) -> Option < VnIndex > {
332
+ fn insert_constant ( & mut self , value : Const < ' tcx > ) -> VnIndex {
335
333
let disambiguator = if value. is_deterministic ( ) {
336
334
// The constant is deterministic, no need to disambiguate.
337
335
0
338
336
} else {
339
337
// Multiple mentions of this constant will yield different values,
340
338
// 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 ( ) ;
344
340
// `disambiguator: 0` means deterministic.
345
341
debug_assert_ne ! ( disambiguator, 0 ) ;
346
342
disambiguator
347
343
} ;
348
- Some ( self . insert ( Value :: Constant { value, disambiguator } ) )
344
+ self . insert ( Value :: Constant { value, disambiguator } )
349
345
}
350
346
351
347
fn insert_bool ( & mut self , flag : bool ) -> VnIndex {
@@ -797,7 +793,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
797
793
location : Location ,
798
794
) -> Option < VnIndex > {
799
795
match * operand {
800
- Operand :: Constant ( ref constant) => self . insert_constant ( constant. const_ ) ,
796
+ Operand :: Constant ( ref constant) => Some ( self . insert_constant ( constant. const_ ) ) ,
801
797
Operand :: Copy ( ref mut place) | Operand :: Move ( ref mut place) => {
802
798
let value = self . simplify_place_value ( place, location) ?;
803
799
if let Some ( const_) = self . try_as_constant ( value) {
@@ -833,11 +829,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
833
829
Rvalue :: Aggregate ( ..) => return self . simplify_aggregate ( rvalue, location) ,
834
830
Rvalue :: Ref ( _, borrow_kind, ref mut place) => {
835
831
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) ) ) ;
837
833
}
838
834
Rvalue :: RawPtr ( mutbl, ref mut place) => {
839
835
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) ) ) ;
841
837
}
842
838
843
839
// Operations.
@@ -991,7 +987,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
991
987
992
988
if is_zst {
993
989
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) ) ) ;
995
991
}
996
992
}
997
993
@@ -1020,11 +1016,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1020
1016
}
1021
1017
} ;
1022
1018
1023
- let fields: Option < Vec < _ > > = field_ops
1019
+ let mut fields: Vec < _ > = field_ops
1024
1020
. 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 ( ) ) )
1026
1022
. collect ( ) ;
1027
- let mut fields = fields?;
1028
1023
1029
1024
if let AggregateTy :: RawPtr { data_pointer_ty, output_pointer_ty } = & mut ty {
1030
1025
let mut was_updated = false ;
@@ -1152,11 +1147,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1152
1147
) if let ty:: Slice ( ..) = to. builtin_deref ( true ) . unwrap ( ) . kind ( )
1153
1148
&& let ty:: Array ( _, len) = from. builtin_deref ( true ) . unwrap ( ) . kind ( ) =>
1154
1149
{
1155
- return self . insert_constant ( Const :: from_ty_const (
1150
+ return Some ( self . insert_constant ( Const :: from_ty_const (
1156
1151
* len,
1157
1152
self . tcx . types . usize ,
1158
1153
self . tcx ,
1159
- ) ) ;
1154
+ ) ) ) ;
1160
1155
}
1161
1156
_ => Value :: UnaryOp ( op, arg_index) ,
1162
1157
} ;
@@ -1351,7 +1346,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1351
1346
if let CastKind :: PointerCoercion ( ReifyFnPointer | ClosureFnPointer ( _) , _) = kind {
1352
1347
// Each reification of a generic fn may get a different pointer.
1353
1348
// Do not try to merge them.
1354
- return self . new_opaque ( ) ;
1349
+ return Some ( self . new_opaque ( ) ) ;
1355
1350
}
1356
1351
1357
1352
let mut was_updated = false ;
@@ -1415,11 +1410,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1415
1410
// Trivial case: we are fetching a statically known length.
1416
1411
let place_ty = place. ty ( self . local_decls , self . tcx ) . ty ;
1417
1412
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 (
1419
1414
* len,
1420
1415
self . tcx . types . usize ,
1421
1416
self . tcx ,
1422
- ) ) ;
1417
+ ) ) ) ;
1423
1418
}
1424
1419
1425
1420
let mut inner = self . simplify_place_value ( place, location) ?;
@@ -1441,11 +1436,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1441
1436
&& let Some ( to) = to. builtin_deref ( true )
1442
1437
&& let ty:: Slice ( ..) = to. kind ( )
1443
1438
{
1444
- return self . insert_constant ( Const :: from_ty_const (
1439
+ return Some ( self . insert_constant ( Const :: from_ty_const (
1445
1440
* len,
1446
1441
self . tcx . types . usize ,
1447
1442
self . tcx ,
1448
- ) ) ;
1443
+ ) ) ) ;
1449
1444
}
1450
1445
1451
1446
// Fallback: a symbolic `Len`.
@@ -1622,7 +1617,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
1622
1617
// `local` as reusable if we have an exact type match.
1623
1618
&& self . local_decls [ local] . ty == rvalue. ty ( self . local_decls , self . tcx )
1624
1619
{
1625
- let value = value. or_else ( || self . new_opaque ( ) ) . unwrap ( ) ;
1620
+ let value = value. unwrap_or_else ( || self . new_opaque ( ) ) ;
1626
1621
self . assign ( local, value) ;
1627
1622
Some ( value)
1628
1623
} else {
@@ -1649,7 +1644,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
1649
1644
&& let Some ( local) = destination. as_local ( )
1650
1645
&& self . ssa . is_ssa ( local)
1651
1646
{
1652
- let opaque = self . new_opaque ( ) . unwrap ( ) ;
1647
+ let opaque = self . new_opaque ( ) ;
1653
1648
self . assign ( local, opaque) ;
1654
1649
}
1655
1650
self . super_terminator ( terminator, location) ;
0 commit comments