@@ -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
@@ -237,8 +237,7 @@ struct VnState<'body, 'tcx> {
237237 /// Values evaluated as constants if possible.
238238 evaluated : IndexVec < VnIndex , Option < OpTy < ' tcx > > > ,
239239 /// Counter to generate different values.
240- /// This is an option to stop creating opaques during replacement.
241- next_opaque : Option < usize > ,
240+ next_opaque : usize ,
242241 /// Cache the value of the `unsized_locals` features, to avoid fetching it repeatedly in a loop.
243242 feature_unsized_locals : bool ,
244243 ssa : & ' body SsaLocals ,
@@ -270,7 +269,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
270269 rev_locals : IndexVec :: with_capacity ( num_values) ,
271270 values : FxIndexSet :: with_capacity_and_hasher ( num_values, Default :: default ( ) ) ,
272271 evaluated : IndexVec :: with_capacity ( num_values) ,
273- next_opaque : Some ( 1 ) ,
272+ next_opaque : 1 ,
274273 feature_unsized_locals : tcx. features ( ) . unsized_locals ( ) ,
275274 ssa,
276275 dominators,
@@ -291,32 +290,31 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
291290 let evaluated = self . eval_to_const ( index) ;
292291 let _index = self . evaluated . push ( evaluated) ;
293292 debug_assert_eq ! ( index, _index) ;
294- // No need to push to `rev_locals` if we finished listing assignments.
295- if self . next_opaque . is_some ( ) {
296- let _index = self . rev_locals . push ( SmallVec :: new ( ) ) ;
297- debug_assert_eq ! ( index, _index) ;
298- }
293+ let _index = self . rev_locals . push ( SmallVec :: new ( ) ) ;
294+ debug_assert_eq ! ( index, _index) ;
299295 }
300296 index
301297 }
302298
299+ fn next_opaque ( & mut self ) -> usize {
300+ let next_opaque = self . next_opaque ;
301+ self . next_opaque += 1 ;
302+ next_opaque
303+ }
304+
303305 /// Create a new `Value` for which we have no information at all, except that it is distinct
304306 /// from all the others.
305307 #[ instrument( level = "trace" , skip( self ) , ret) ]
306- fn new_opaque ( & mut self ) -> Option < VnIndex > {
307- let next_opaque = self . next_opaque . as_mut ( ) ?;
308- let value = Value :: Opaque ( * next_opaque) ;
309- * next_opaque += 1 ;
310- Some ( self . insert ( value) )
308+ fn new_opaque ( & mut self ) -> VnIndex {
309+ let value = Value :: Opaque ( self . next_opaque ( ) ) ;
310+ self . insert ( value)
311311 }
312312
313313 /// Create a new `Value::Address` distinct from all the others.
314314 #[ instrument( level = "trace" , skip( self ) , ret) ]
315- fn new_pointer ( & mut self , place : Place < ' tcx > , kind : AddressKind ) -> Option < VnIndex > {
316- let next_opaque = self . next_opaque . as_mut ( ) ?;
317- let value = Value :: Address { place, kind, provenance : * next_opaque } ;
318- * next_opaque += 1 ;
319- Some ( self . insert ( value) )
315+ fn new_pointer ( & mut self , place : Place < ' tcx > , kind : AddressKind ) -> VnIndex {
316+ let value = Value :: Address { place, kind, provenance : self . next_opaque ( ) } ;
317+ self . insert ( value)
320318 }
321319
322320 fn get ( & self , index : VnIndex ) -> & Value < ' tcx > {
@@ -337,21 +335,19 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
337335 }
338336 }
339337
340- fn insert_constant ( & mut self , value : Const < ' tcx > ) -> Option < VnIndex > {
338+ fn insert_constant ( & mut self , value : Const < ' tcx > ) -> VnIndex {
341339 let disambiguator = if value. is_deterministic ( ) {
342340 // The constant is deterministic, no need to disambiguate.
343341 0
344342 } else {
345343 // Multiple mentions of this constant will yield different values,
346344 // so assign a different `disambiguator` to ensure they do not get the same `VnIndex`.
347- let next_opaque = self . next_opaque . as_mut ( ) ?;
348- let disambiguator = * next_opaque;
349- * next_opaque += 1 ;
345+ let disambiguator = self . next_opaque ( ) ;
350346 // `disambiguator: 0` means deterministic.
351347 debug_assert_ne ! ( disambiguator, 0 ) ;
352348 disambiguator
353349 } ;
354- Some ( self . insert ( Value :: Constant { value, disambiguator } ) )
350+ self . insert ( Value :: Constant { value, disambiguator } )
355351 }
356352
357353 fn insert_bool ( & mut self , flag : bool ) -> VnIndex {
@@ -812,7 +808,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
812808 location : Location ,
813809 ) -> Option < VnIndex > {
814810 match * operand {
815- Operand :: Constant ( ref constant) => self . insert_constant ( constant. const_ ) ,
811+ Operand :: Constant ( ref constant) => Some ( self . insert_constant ( constant. const_ ) ) ,
816812 Operand :: Copy ( ref mut place) | Operand :: Move ( ref mut place) => {
817813 let value = self . simplify_place_value ( place, location) ?;
818814 if let Some ( const_) = self . try_as_constant ( value) {
@@ -848,11 +844,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
848844 Rvalue :: Aggregate ( ..) => return self . simplify_aggregate ( rvalue, location) ,
849845 Rvalue :: Ref ( _, borrow_kind, ref mut place) => {
850846 self . simplify_place_projection ( place, location) ;
851- return self . new_pointer ( * place, AddressKind :: Ref ( borrow_kind) ) ;
847+ return Some ( self . new_pointer ( * place, AddressKind :: Ref ( borrow_kind) ) ) ;
852848 }
853849 Rvalue :: RawPtr ( mutbl, ref mut place) => {
854850 self . simplify_place_projection ( place, location) ;
855- return self . new_pointer ( * place, AddressKind :: Address ( mutbl) ) ;
851+ return Some ( self . new_pointer ( * place, AddressKind :: Address ( mutbl) ) ) ;
856852 }
857853 Rvalue :: WrapUnsafeBinder ( ref mut op, ty) => {
858854 let value = self . simplify_operand ( op, location) ?;
@@ -1016,7 +1012,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10161012
10171013 if is_zst {
10181014 let ty = rvalue. ty ( self . local_decls , tcx) ;
1019- return self . insert_constant ( Const :: zero_sized ( ty) ) ;
1015+ return Some ( self . insert_constant ( Const :: zero_sized ( ty) ) ) ;
10201016 }
10211017 }
10221018
@@ -1045,11 +1041,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10451041 }
10461042 } ;
10471043
1048- let fields: Option < Vec < _ > > = field_ops
1044+ let mut fields: Vec < _ > = field_ops
10491045 . iter_mut ( )
1050- . map ( |op| self . simplify_operand ( op, location) . or_else ( || self . new_opaque ( ) ) )
1046+ . map ( |op| self . simplify_operand ( op, location) . unwrap_or_else ( || self . new_opaque ( ) ) )
10511047 . collect ( ) ;
1052- let mut fields = fields?;
10531048
10541049 if let AggregateTy :: RawPtr { data_pointer_ty, output_pointer_ty } = & mut ty {
10551050 let mut was_updated = false ;
@@ -1177,7 +1172,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
11771172 ) if let ty:: Slice ( ..) = to. builtin_deref ( true ) . unwrap ( ) . kind ( )
11781173 && let ty:: Array ( _, len) = from. builtin_deref ( true ) . unwrap ( ) . kind ( ) =>
11791174 {
1180- return self . insert_constant ( Const :: Ty ( self . tcx . types . usize , * len) ) ;
1175+ return Some ( self . insert_constant ( Const :: Ty ( self . tcx . types . usize , * len) ) ) ;
11811176 }
11821177 _ => Value :: UnaryOp ( op, arg_index) ,
11831178 } ;
@@ -1373,7 +1368,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
13731368 if let CastKind :: PointerCoercion ( ReifyFnPointer | ClosureFnPointer ( _) , _) = kind {
13741369 // Each reification of a generic fn may get a different pointer.
13751370 // Do not try to merge them.
1376- return self . new_opaque ( ) ;
1371+ return Some ( self . new_opaque ( ) ) ;
13771372 }
13781373
13791374 let mut was_ever_updated = false ;
@@ -1489,7 +1484,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14891484 // Trivial case: we are fetching a statically known length.
14901485 let place_ty = place. ty ( self . local_decls , self . tcx ) . ty ;
14911486 if let ty:: Array ( _, len) = place_ty. kind ( ) {
1492- return self . insert_constant ( Const :: Ty ( self . tcx . types . usize , * len) ) ;
1487+ return Some ( self . insert_constant ( Const :: Ty ( self . tcx . types . usize , * len) ) ) ;
14931488 }
14941489
14951490 let mut inner = self . simplify_place_value ( place, location) ?;
@@ -1511,7 +1506,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
15111506 && let Some ( to) = to. builtin_deref ( true )
15121507 && let ty:: Slice ( ..) = to. kind ( )
15131508 {
1514- return self . insert_constant ( Const :: Ty ( self . tcx . types . usize , * len) ) ;
1509+ return Some ( self . insert_constant ( Const :: Ty ( self . tcx . types . usize , * len) ) ) ;
15151510 }
15161511
15171512 // Fallback: a symbolic `Len`.
@@ -1740,7 +1735,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
17401735 // `local` as reusable if we have an exact type match.
17411736 && self . local_decls [ local] . ty == rvalue. ty ( self . local_decls , self . tcx )
17421737 {
1743- let value = value. or_else ( || self . new_opaque ( ) ) . unwrap ( ) ;
1738+ let value = value. unwrap_or_else ( || self . new_opaque ( ) ) ;
17441739 self . assign ( local, value) ;
17451740 Some ( value)
17461741 } else {
@@ -1767,7 +1762,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
17671762 && let Some ( local) = destination. as_local ( )
17681763 && self . ssa . is_ssa ( local)
17691764 {
1770- let opaque = self . new_opaque ( ) . unwrap ( ) ;
1765+ let opaque = self . new_opaque ( ) ;
17711766 self . assign ( local, opaque) ;
17721767 }
17731768 self . super_terminator ( terminator, location) ;
0 commit comments