@@ -423,6 +423,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
423
423
self . ecx . access_local ( self . ecx . frame ( ) , local, None ) . ok ( )
424
424
}
425
425
426
+ /// Remove `local` from the pool of `Locals`. Allows writing to them,
427
+ /// but not reading from them anymore.
426
428
fn remove_const ( & mut self , local : Local ) {
427
429
self . ecx . frame_mut ( ) . locals [ local] =
428
430
LocalState { value : LocalValue :: Uninitialized , layout : Cell :: new ( None ) } ;
@@ -455,6 +457,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
455
457
}
456
458
}
457
459
460
+ /// Returns the value, if any, of evaluating `c`.
458
461
fn eval_constant ( & mut self , c : & Constant < ' tcx > , source_info : SourceInfo ) -> Option < OpTy < ' tcx > > {
459
462
// FIXME we need to revisit this for #67176
460
463
if c. needs_subst ( ) {
@@ -495,11 +498,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
495
498
}
496
499
}
497
500
501
+ /// Returns the value, if any, of evaluating `place`.
498
502
fn eval_place ( & mut self , place : Place < ' tcx > ) -> Option < OpTy < ' tcx > > {
499
503
trace ! ( "eval_place(place={:?})" , place) ;
500
504
self . use_ecx ( |this| this. ecx . eval_place_to_op ( place, None ) )
501
505
}
502
506
507
+ /// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
508
+ /// or `eval_place`, depending on the variant of `Operand` used.
503
509
fn eval_operand ( & mut self , op : & Operand < ' tcx > , source_info : SourceInfo ) -> Option < OpTy < ' tcx > > {
504
510
match * op {
505
511
Operand :: Constant ( ref c) => self . eval_constant ( c, source_info) ,
@@ -658,6 +664,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
658
664
} )
659
665
}
660
666
667
+ /// Creates a new `Operand::Constant` from a `Scalar` value
661
668
fn operand_from_scalar ( & self , scalar : Scalar , ty : Ty < ' tcx > , span : Span ) -> Operand < ' tcx > {
662
669
Operand :: Constant ( Box :: new ( Constant {
663
670
span,
@@ -703,6 +710,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
703
710
// Found a value represented as a pair. For now only do cont-prop if type of
704
711
// Rvalue is also a pair with two scalars. The more general case is more
705
712
// complicated to implement so we'll do it later.
713
+ // FIXME: implement the general case stated above ^.
706
714
let ty = & value. layout . ty . kind ;
707
715
// Only do it for tuples
708
716
if let ty:: Tuple ( substs) = ty {
@@ -739,6 +747,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
739
747
}
740
748
}
741
749
750
+ /// Returns `true` if and only if this `op` should be const-propagated into.
742
751
fn should_const_prop ( & mut self , op : OpTy < ' tcx > ) -> bool {
743
752
let mir_opt_level = self . tcx . sess . opts . debugging_opts . mir_opt_level ;
744
753
@@ -780,14 +789,14 @@ enum ConstPropMode {
780
789
781
790
struct CanConstProp {
782
791
can_const_prop : IndexVec < Local , ConstPropMode > ,
783
- // false at the beginning, once set, there are not allowed to be any more assignments
792
+ // False at the beginning. Once set, no more assignments are allowed to that local.
784
793
found_assignment : BitSet < Local > ,
785
794
// Cache of locals' information
786
795
local_kinds : IndexVec < Local , LocalKind > ,
787
796
}
788
797
789
798
impl CanConstProp {
790
- /// returns true if `local` can be propagated
799
+ /// Returns true if `local` can be propagated
791
800
fn check ( body : & Body < ' _ > ) -> IndexVec < Local , ConstPropMode > {
792
801
let mut cpv = CanConstProp {
793
802
can_const_prop : IndexVec :: from_elem ( ConstPropMode :: FullConstProp , & body. local_decls ) ,
@@ -798,8 +807,8 @@ impl CanConstProp {
798
807
) ,
799
808
} ;
800
809
for ( local, val) in cpv. can_const_prop . iter_enumerated_mut ( ) {
801
- // cannot use args at all
802
- // cannot use locals because if x < y { y - x } else { x - y } would
810
+ // Cannot use args at all
811
+ // Cannot use locals because if x < y { y - x } else { x - y } would
803
812
// lint for x != y
804
813
// FIXME(oli-obk): lint variables until they are used in a condition
805
814
// FIXME(oli-obk): lint if return value is constant
@@ -939,7 +948,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
939
948
let expected = ScalarMaybeUndef :: from ( Scalar :: from_bool ( * expected) ) ;
940
949
let value_const = self . ecx . read_scalar ( value) . unwrap ( ) ;
941
950
if expected != value_const {
942
- // poison all places this operand references so that further code
951
+ // Poison all places this operand references so that further code
943
952
// doesn't use the invalid value
944
953
match cond {
945
954
Operand :: Move ( ref place) | Operand :: Copy ( ref place) => {
@@ -1005,7 +1014,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
1005
1014
}
1006
1015
}
1007
1016
}
1008
- //none of these have Operands to const-propagate
1017
+ // None of these have Operands to const-propagate
1009
1018
TerminatorKind :: Goto { .. }
1010
1019
| TerminatorKind :: Resume
1011
1020
| TerminatorKind :: Abort
@@ -1023,22 +1032,17 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
1023
1032
for opr in args {
1024
1033
// We see if the operand can be evaluated, and if so, we continue.
1025
1034
if let Some ( op_ty) = self . eval_operand ( & opr, source_info) {
1026
- trace ! ( "OpTy of the place: {:?}" , & op_ty) ;
1027
1035
// We must also ask if it *should* be evaluated.
1028
1036
if self . should_const_prop ( op_ty) {
1029
1037
if let interpret:: Operand :: Immediate ( immediate) = * op_ty {
1030
1038
// Only Immediate from here on
1031
1039
// FIXME(felix91gr): The code only works for Immediate. Investigate
1032
1040
// if it could be made to work for Indirect as well.
1033
- trace ! ( "Immediate of the OpTy: {:?}" , & immediate) ;
1034
1041
if let interpret:: Immediate :: Scalar ( scalar_mu) = immediate {
1035
1042
// FIXME(felix91gr): This only works for Scalar
1036
1043
// Could probably be expanded for Scalar Tuples (inside the Immediate)
1037
- trace ! ( "ScalarMaybeUndef of the OpTy: {:?}" , & scalar_mu) ;
1038
1044
if let ScalarMaybeUndef :: Scalar ( scalar) = scalar_mu {
1039
- trace ! ( "Scalar of the ScalarMU: {:?}" , & scalar) ;
1040
1045
let operand_type = opr. ty ( & self . local_decls , self . tcx ) ;
1041
- trace ! ( "Type of the OpTy: {:?}" , & operand_type) ;
1042
1046
* opr = self . operand_from_scalar (
1043
1047
scalar,
1044
1048
operand_type,
0 commit comments