@@ -48,6 +48,7 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
48
48
ctx. simplify_ref_deref ( rvalue) ;
49
49
ctx. simplify_ptr_aggregate ( rvalue) ;
50
50
ctx. simplify_cast ( rvalue) ;
51
+ ctx. simplify_repeated_aggregate ( rvalue) ;
51
52
}
52
53
_ => { }
53
54
}
@@ -68,6 +69,32 @@ struct InstSimplifyContext<'a, 'tcx> {
68
69
}
69
70
70
71
impl < ' tcx > InstSimplifyContext < ' _ , ' tcx > {
72
+ fn simplify_repeated_aggregate ( & self , rvalue : & mut Rvalue < ' tcx > ) {
73
+ let Rvalue :: Aggregate ( box AggregateKind :: Array ( _) , fields) = rvalue else {
74
+ return ;
75
+ } ;
76
+ if fields. len ( ) < 5 {
77
+ return ;
78
+ }
79
+ let first = & fields[ rustc_abi:: FieldIdx :: ZERO ] ;
80
+ let Operand :: Constant ( first) = first else {
81
+ return ;
82
+ } ;
83
+ let Ok ( first_val) = first. const_ . eval ( self . tcx , self . typing_env , first. span ) else {
84
+ return ;
85
+ } ;
86
+ if fields. iter ( ) . all ( |v| {
87
+ let Operand :: Constant ( v) = v else {
88
+ return false ;
89
+ } ;
90
+ let v = v. const_ . eval ( self . tcx , self . typing_env , v. span ) ;
91
+ v == Ok ( first_val)
92
+ } ) {
93
+ let len = ty:: Const :: from_target_usize ( self . tcx , fields. len ( ) . try_into ( ) . unwrap ( ) ) ;
94
+ * rvalue = Rvalue :: Repeat ( Operand :: Constant ( first. clone ( ) ) , len) ;
95
+ }
96
+ }
97
+
71
98
/// Transform boolean comparisons into logical operations.
72
99
fn simplify_bool_cmp ( & self , rvalue : & mut Rvalue < ' tcx > ) {
73
100
match rvalue {
0 commit comments