@@ -130,7 +130,7 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
130
130
let mut state = VnState :: new ( tcx, body, typing_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
@@ -233,8 +233,7 @@ struct VnState<'body, 'tcx> {
233
233
/// Values evaluated as constants if possible.
234
234
evaluated : IndexVec < VnIndex , Option < OpTy < ' tcx > > > ,
235
235
/// 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 ,
238
237
/// Cache the value of the `unsized_locals` features, to avoid fetching it repeatedly in a loop.
239
238
feature_unsized_locals : bool ,
240
239
ssa : & ' body SsaLocals ,
@@ -266,7 +265,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
266
265
rev_locals : IndexVec :: with_capacity ( num_values) ,
267
266
values : FxIndexSet :: with_capacity_and_hasher ( num_values, Default :: default ( ) ) ,
268
267
evaluated : IndexVec :: with_capacity ( num_values) ,
269
- next_opaque : Some ( 1 ) ,
268
+ next_opaque : 1 ,
270
269
feature_unsized_locals : tcx. features ( ) . unsized_locals ( ) ,
271
270
ssa,
272
271
dominators,
@@ -287,32 +286,31 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
287
286
let evaluated = self . eval_to_const ( index) ;
288
287
let _index = self . evaluated . push ( evaluated) ;
289
288
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) ;
295
291
}
296
292
index
297
293
}
298
294
295
+ fn next_opaque ( & mut self ) -> usize {
296
+ let next_opaque = self . next_opaque ;
297
+ self . next_opaque += 1 ;
298
+ next_opaque
299
+ }
300
+
299
301
/// Create a new `Value` for which we have no information at all, except that it is distinct
300
302
/// from all the others.
301
303
#[ 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)
307
307
}
308
308
309
309
/// Create a new `Value::Address` distinct from all the others.
310
310
#[ 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)
316
314
}
317
315
318
316
fn get ( & self , index : VnIndex ) -> & Value < ' tcx > {
@@ -333,21 +331,19 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
333
331
}
334
332
}
335
333
336
- fn insert_constant ( & mut self , value : Const < ' tcx > ) -> Option < VnIndex > {
334
+ fn insert_constant ( & mut self , value : Const < ' tcx > ) -> VnIndex {
337
335
let disambiguator = if value. is_deterministic ( ) {
338
336
// The constant is deterministic, no need to disambiguate.
339
337
0
340
338
} else {
341
339
// Multiple mentions of this constant will yield different values,
342
340
// 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 ( ) ;
346
342
// `disambiguator: 0` means deterministic.
347
343
debug_assert_ne ! ( disambiguator, 0 ) ;
348
344
disambiguator
349
345
} ;
350
- Some ( self . insert ( Value :: Constant { value, disambiguator } ) )
346
+ self . insert ( Value :: Constant { value, disambiguator } )
351
347
}
352
348
353
349
fn insert_bool ( & mut self , flag : bool ) -> VnIndex {
@@ -801,7 +797,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
801
797
location : Location ,
802
798
) -> Option < VnIndex > {
803
799
match * operand {
804
- Operand :: Constant ( ref constant) => self . insert_constant ( constant. const_ ) ,
800
+ Operand :: Constant ( ref constant) => Some ( self . insert_constant ( constant. const_ ) ) ,
805
801
Operand :: Copy ( ref mut place) | Operand :: Move ( ref mut place) => {
806
802
let value = self . simplify_place_value ( place, location) ?;
807
803
if let Some ( const_) = self . try_as_constant ( value) {
@@ -837,11 +833,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
837
833
Rvalue :: Aggregate ( ..) => return self . simplify_aggregate ( rvalue, location) ,
838
834
Rvalue :: Ref ( _, borrow_kind, ref mut place) => {
839
835
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) ) ) ;
841
837
}
842
838
Rvalue :: RawPtr ( mutbl, ref mut place) => {
843
839
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) ) ) ;
845
841
}
846
842
847
843
// Operations.
@@ -995,7 +991,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
995
991
996
992
if is_zst {
997
993
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) ) ) ;
999
995
}
1000
996
}
1001
997
@@ -1024,11 +1020,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1024
1020
}
1025
1021
} ;
1026
1022
1027
- let fields: Option < Vec < _ > > = field_ops
1023
+ let mut fields: Vec < _ > = field_ops
1028
1024
. 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 ( ) ) )
1030
1026
. collect ( ) ;
1031
- let mut fields = fields?;
1032
1027
1033
1028
if let AggregateTy :: RawPtr { data_pointer_ty, output_pointer_ty } = & mut ty {
1034
1029
let mut was_updated = false ;
@@ -1156,11 +1151,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1156
1151
) if let ty:: Slice ( ..) = to. builtin_deref ( true ) . unwrap ( ) . kind ( )
1157
1152
&& let ty:: Array ( _, len) = from. builtin_deref ( true ) . unwrap ( ) . kind ( ) =>
1158
1153
{
1159
- return self . insert_constant ( Const :: from_ty_const (
1154
+ return Some ( self . insert_constant ( Const :: from_ty_const (
1160
1155
* len,
1161
1156
self . tcx . types . usize ,
1162
1157
self . tcx ,
1163
- ) ) ;
1158
+ ) ) ) ;
1164
1159
}
1165
1160
_ => Value :: UnaryOp ( op, arg_index) ,
1166
1161
} ;
@@ -1355,7 +1350,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1355
1350
if let CastKind :: PointerCoercion ( ReifyFnPointer | ClosureFnPointer ( _) , _) = kind {
1356
1351
// Each reification of a generic fn may get a different pointer.
1357
1352
// Do not try to merge them.
1358
- return self . new_opaque ( ) ;
1353
+ return Some ( self . new_opaque ( ) ) ;
1359
1354
}
1360
1355
1361
1356
let mut was_updated = false ;
@@ -1419,11 +1414,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1419
1414
// Trivial case: we are fetching a statically known length.
1420
1415
let place_ty = place. ty ( self . local_decls , self . tcx ) . ty ;
1421
1416
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 (
1423
1418
* len,
1424
1419
self . tcx . types . usize ,
1425
1420
self . tcx ,
1426
- ) ) ;
1421
+ ) ) ) ;
1427
1422
}
1428
1423
1429
1424
let mut inner = self . simplify_place_value ( place, location) ?;
@@ -1445,11 +1440,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
1445
1440
&& let Some ( to) = to. builtin_deref ( true )
1446
1441
&& let ty:: Slice ( ..) = to. kind ( )
1447
1442
{
1448
- return self . insert_constant ( Const :: from_ty_const (
1443
+ return Some ( self . insert_constant ( Const :: from_ty_const (
1449
1444
* len,
1450
1445
self . tcx . types . usize ,
1451
1446
self . tcx ,
1452
- ) ) ;
1447
+ ) ) ) ;
1453
1448
}
1454
1449
1455
1450
// Fallback: a symbolic `Len`.
@@ -1627,7 +1622,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
1627
1622
// `local` as reusable if we have an exact type match.
1628
1623
&& self . local_decls [ local] . ty == rvalue. ty ( self . local_decls , self . tcx )
1629
1624
{
1630
- let value = value. or_else ( || self . new_opaque ( ) ) . unwrap ( ) ;
1625
+ let value = value. unwrap_or_else ( || self . new_opaque ( ) ) ;
1631
1626
self . assign ( local, value) ;
1632
1627
Some ( value)
1633
1628
} else {
@@ -1654,7 +1649,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
1654
1649
&& let Some ( local) = destination. as_local ( )
1655
1650
&& self . ssa . is_ssa ( local)
1656
1651
{
1657
- let opaque = self . new_opaque ( ) . unwrap ( ) ;
1652
+ let opaque = self . new_opaque ( ) ;
1658
1653
self . assign ( local, opaque) ;
1659
1654
}
1660
1655
self . super_terminator ( terminator, location) ;
0 commit comments