]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/transform/simplify_try.rs
Rollup merge of #76867 - poliorcetics:intra-doc-core-iter, r=jyn514
[rust.git] / compiler / rustc_mir / src / transform / simplify_try.rs
1 //! The general point of the optimizations provided here is to simplify something like:
2 //!
3 //! ```rust
4 //! match x {
5 //!     Ok(x) => Ok(x),
6 //!     Err(x) => Err(x)
7 //! }
8 //! ```
9 //!
10 //! into just `x`.
11
12 use crate::transform::{simplify, MirPass, MirSource};
13 use itertools::Itertools as _;
14 use rustc_index::{bit_set::BitSet, vec::IndexVec};
15 use rustc_middle::mir::visit::{NonUseContext, PlaceContext, Visitor};
16 use rustc_middle::mir::*;
17 use rustc_middle::ty::{self, List, Ty, TyCtxt};
18 use rustc_target::abi::VariantIdx;
19 use std::iter::{Enumerate, Peekable};
20 use std::slice::Iter;
21
22 /// Simplifies arms of form `Variant(x) => Variant(x)` to just a move.
23 ///
24 /// This is done by transforming basic blocks where the statements match:
25 ///
26 /// ```rust
27 /// _LOCAL_TMP = ((_LOCAL_1 as Variant ).FIELD: TY );
28 /// _TMP_2 = _LOCAL_TMP;
29 /// ((_LOCAL_0 as Variant).FIELD: TY) = move _TMP_2;
30 /// discriminant(_LOCAL_0) = VAR_IDX;
31 /// ```
32 ///
33 /// into:
34 ///
35 /// ```rust
36 /// _LOCAL_0 = move _LOCAL_1
37 /// ```
38 pub struct SimplifyArmIdentity;
39
40 #[derive(Debug)]
41 struct ArmIdentityInfo<'tcx> {
42     /// Storage location for the variant's field
43     local_temp_0: Local,
44     /// Storage location holding the variant being read from
45     local_1: Local,
46     /// The variant field being read from
47     vf_s0: VarField<'tcx>,
48     /// Index of the statement which loads the variant being read
49     get_variant_field_stmt: usize,
50
51     /// Tracks each assignment to a temporary of the variant's field
52     field_tmp_assignments: Vec<(Local, Local)>,
53
54     /// Storage location holding the variant's field that was read from
55     local_tmp_s1: Local,
56     /// Storage location holding the enum that we are writing to
57     local_0: Local,
58     /// The variant field being written to
59     vf_s1: VarField<'tcx>,
60
61     /// Storage location that the discriminant is being written to
62     set_discr_local: Local,
63     /// The variant being written
64     set_discr_var_idx: VariantIdx,
65
66     /// Index of the statement that should be overwritten as a move
67     stmt_to_overwrite: usize,
68     /// SourceInfo for the new move
69     source_info: SourceInfo,
70
71     /// Indices of matching Storage{Live,Dead} statements encountered.
72     /// (StorageLive index,, StorageDead index, Local)
73     storage_stmts: Vec<(usize, usize, Local)>,
74
75     /// The statements that should be removed (turned into nops)
76     stmts_to_remove: Vec<usize>,
77
78     /// Indices of debug variables that need to be adjusted to point to
79     // `{local_0}.{dbg_projection}`.
80     dbg_info_to_adjust: Vec<usize>,
81
82     /// The projection used to rewrite debug info.
83     dbg_projection: &'tcx List<PlaceElem<'tcx>>,
84 }
85
86 fn get_arm_identity_info<'a, 'tcx>(
87     stmts: &'a [Statement<'tcx>],
88     locals_count: usize,
89     debug_info: &'a [VarDebugInfo<'tcx>],
90 ) -> Option<ArmIdentityInfo<'tcx>> {
91     // This can't possibly match unless there are at least 3 statements in the block
92     // so fail fast on tiny blocks.
93     if stmts.len() < 3 {
94         return None;
95     }
96
97     let mut tmp_assigns = Vec::new();
98     let mut nop_stmts = Vec::new();
99     let mut storage_stmts = Vec::new();
100     let mut storage_live_stmts = Vec::new();
101     let mut storage_dead_stmts = Vec::new();
102
103     type StmtIter<'a, 'tcx> = Peekable<Enumerate<Iter<'a, Statement<'tcx>>>>;
104
105     fn is_storage_stmt<'tcx>(stmt: &Statement<'tcx>) -> bool {
106         matches!(stmt.kind, StatementKind::StorageLive(_) | StatementKind::StorageDead(_))
107     }
108
109     /// Eats consecutive Statements which match `test`, performing the specified `action` for each.
110     /// The iterator `stmt_iter` is not advanced if none were matched.
111     fn try_eat<'a, 'tcx>(
112         stmt_iter: &mut StmtIter<'a, 'tcx>,
113         test: impl Fn(&'a Statement<'tcx>) -> bool,
114         mut action: impl FnMut(usize, &'a Statement<'tcx>),
115     ) {
116         while stmt_iter.peek().map(|(_, stmt)| test(stmt)).unwrap_or(false) {
117             let (idx, stmt) = stmt_iter.next().unwrap();
118
119             action(idx, stmt);
120         }
121     }
122
123     /// Eats consecutive `StorageLive` and `StorageDead` Statements.
124     /// The iterator `stmt_iter` is not advanced if none were found.
125     fn try_eat_storage_stmts<'a, 'tcx>(
126         stmt_iter: &mut StmtIter<'a, 'tcx>,
127         storage_live_stmts: &mut Vec<(usize, Local)>,
128         storage_dead_stmts: &mut Vec<(usize, Local)>,
129     ) {
130         try_eat(stmt_iter, is_storage_stmt, |idx, stmt| {
131             if let StatementKind::StorageLive(l) = stmt.kind {
132                 storage_live_stmts.push((idx, l));
133             } else if let StatementKind::StorageDead(l) = stmt.kind {
134                 storage_dead_stmts.push((idx, l));
135             }
136         })
137     }
138
139     fn is_tmp_storage_stmt<'tcx>(stmt: &Statement<'tcx>) -> bool {
140         use rustc_middle::mir::StatementKind::Assign;
141         if let Assign(box (place, Rvalue::Use(Operand::Copy(p) | Operand::Move(p)))) = &stmt.kind {
142             place.as_local().is_some() && p.as_local().is_some()
143         } else {
144             false
145         }
146     }
147
148     /// Eats consecutive `Assign` Statements.
149     // The iterator `stmt_iter` is not advanced if none were found.
150     fn try_eat_assign_tmp_stmts<'a, 'tcx>(
151         stmt_iter: &mut StmtIter<'a, 'tcx>,
152         tmp_assigns: &mut Vec<(Local, Local)>,
153         nop_stmts: &mut Vec<usize>,
154     ) {
155         try_eat(stmt_iter, is_tmp_storage_stmt, |idx, stmt| {
156             use rustc_middle::mir::StatementKind::Assign;
157             if let Assign(box (place, Rvalue::Use(Operand::Copy(p) | Operand::Move(p)))) =
158                 &stmt.kind
159             {
160                 tmp_assigns.push((place.as_local().unwrap(), p.as_local().unwrap()));
161                 nop_stmts.push(idx);
162             }
163         })
164     }
165
166     fn find_storage_live_dead_stmts_for_local<'tcx>(
167         local: Local,
168         stmts: &[Statement<'tcx>],
169     ) -> Option<(usize, usize)> {
170         trace!("looking for {:?}", local);
171         let mut storage_live_stmt = None;
172         let mut storage_dead_stmt = None;
173         for (idx, stmt) in stmts.iter().enumerate() {
174             if stmt.kind == StatementKind::StorageLive(local) {
175                 storage_live_stmt = Some(idx);
176             } else if stmt.kind == StatementKind::StorageDead(local) {
177                 storage_dead_stmt = Some(idx);
178             }
179         }
180
181         Some((storage_live_stmt?, storage_dead_stmt.unwrap_or(usize::MAX)))
182     }
183
184     // Try to match the expected MIR structure with the basic block we're processing.
185     // We want to see something that looks like:
186     // ```
187     // (StorageLive(_) | StorageDead(_));*
188     // _LOCAL_INTO = ((_LOCAL_FROM as Variant).FIELD: TY);
189     // (StorageLive(_) | StorageDead(_));*
190     // (tmp_n+1 = tmp_n);*
191     // (StorageLive(_) | StorageDead(_));*
192     // (tmp_n+1 = tmp_n);*
193     // ((LOCAL_FROM as Variant).FIELD: TY) = move tmp;
194     // discriminant(LOCAL_FROM) = VariantIdx;
195     // (StorageLive(_) | StorageDead(_));*
196     // ```
197     let mut stmt_iter = stmts.iter().enumerate().peekable();
198
199     try_eat_storage_stmts(&mut stmt_iter, &mut storage_live_stmts, &mut storage_dead_stmts);
200
201     let (get_variant_field_stmt, stmt) = stmt_iter.next()?;
202     let (local_tmp_s0, local_1, vf_s0, dbg_projection) = match_get_variant_field(stmt)?;
203
204     try_eat_storage_stmts(&mut stmt_iter, &mut storage_live_stmts, &mut storage_dead_stmts);
205
206     try_eat_assign_tmp_stmts(&mut stmt_iter, &mut tmp_assigns, &mut nop_stmts);
207
208     try_eat_storage_stmts(&mut stmt_iter, &mut storage_live_stmts, &mut storage_dead_stmts);
209
210     try_eat_assign_tmp_stmts(&mut stmt_iter, &mut tmp_assigns, &mut nop_stmts);
211
212     let (idx, stmt) = stmt_iter.next()?;
213     let (local_tmp_s1, local_0, vf_s1) = match_set_variant_field(stmt)?;
214     nop_stmts.push(idx);
215
216     let (idx, stmt) = stmt_iter.next()?;
217     let (set_discr_local, set_discr_var_idx) = match_set_discr(stmt)?;
218     let discr_stmt_source_info = stmt.source_info;
219     nop_stmts.push(idx);
220
221     try_eat_storage_stmts(&mut stmt_iter, &mut storage_live_stmts, &mut storage_dead_stmts);
222
223     for (live_idx, live_local) in storage_live_stmts {
224         if let Some(i) = storage_dead_stmts.iter().rposition(|(_, l)| *l == live_local) {
225             let (dead_idx, _) = storage_dead_stmts.swap_remove(i);
226             storage_stmts.push((live_idx, dead_idx, live_local));
227
228             if live_local == local_tmp_s0 {
229                 nop_stmts.push(get_variant_field_stmt);
230             }
231         }
232     }
233     // We sort primitive usize here so we can use unstable sort
234     nop_stmts.sort_unstable();
235
236     // Use one of the statements we're going to discard between the point
237     // where the storage location for the variant field becomes live and
238     // is killed.
239     let (live_idx, dead_idx) = find_storage_live_dead_stmts_for_local(local_tmp_s0, stmts)?;
240     let stmt_to_overwrite =
241         nop_stmts.iter().find(|stmt_idx| live_idx < **stmt_idx && **stmt_idx < dead_idx);
242
243     let mut tmp_assigned_vars = BitSet::new_empty(locals_count);
244     for (l, r) in &tmp_assigns {
245         tmp_assigned_vars.insert(*l);
246         tmp_assigned_vars.insert(*r);
247     }
248
249     let dbg_info_to_adjust: Vec<_> =
250         debug_info
251             .iter()
252             .enumerate()
253             .filter_map(|(i, var_info)| {
254                 if tmp_assigned_vars.contains(var_info.place.local) { Some(i) } else { None }
255             })
256             .collect();
257
258     Some(ArmIdentityInfo {
259         local_temp_0: local_tmp_s0,
260         local_1,
261         vf_s0,
262         get_variant_field_stmt,
263         field_tmp_assignments: tmp_assigns,
264         local_tmp_s1,
265         local_0,
266         vf_s1,
267         set_discr_local,
268         set_discr_var_idx,
269         stmt_to_overwrite: *stmt_to_overwrite?,
270         source_info: discr_stmt_source_info,
271         storage_stmts,
272         stmts_to_remove: nop_stmts,
273         dbg_info_to_adjust,
274         dbg_projection,
275     })
276 }
277
278 fn optimization_applies<'tcx>(
279     opt_info: &ArmIdentityInfo<'tcx>,
280     local_decls: &IndexVec<Local, LocalDecl<'tcx>>,
281     local_uses: &IndexVec<Local, usize>,
282     var_debug_info: &[VarDebugInfo<'tcx>],
283 ) -> bool {
284     trace!("testing if optimization applies...");
285
286     // FIXME(wesleywiser): possibly relax this restriction?
287     if opt_info.local_0 == opt_info.local_1 {
288         trace!("NO: moving into ourselves");
289         return false;
290     } else if opt_info.vf_s0 != opt_info.vf_s1 {
291         trace!("NO: the field-and-variant information do not match");
292         return false;
293     } else if local_decls[opt_info.local_0].ty != local_decls[opt_info.local_1].ty {
294         // FIXME(Centril,oli-obk): possibly relax to same layout?
295         trace!("NO: source and target locals have different types");
296         return false;
297     } else if (opt_info.local_0, opt_info.vf_s0.var_idx)
298         != (opt_info.set_discr_local, opt_info.set_discr_var_idx)
299     {
300         trace!("NO: the discriminants do not match");
301         return false;
302     }
303
304     // Verify the assigment chain consists of the form b = a; c = b; d = c; etc...
305     if opt_info.field_tmp_assignments.is_empty() {
306         trace!("NO: no assignments found");
307         return false;
308     }
309     let mut last_assigned_to = opt_info.field_tmp_assignments[0].1;
310     let source_local = last_assigned_to;
311     for (l, r) in &opt_info.field_tmp_assignments {
312         if *r != last_assigned_to {
313             trace!("NO: found unexpected assignment {:?} = {:?}", l, r);
314             return false;
315         }
316
317         last_assigned_to = *l;
318     }
319
320     // Check that the first and last used locals are only used twice
321     // since they are of the form:
322     //
323     // ```
324     // _first = ((_x as Variant).n: ty);
325     // _n = _first;
326     // ...
327     // ((_y as Variant).n: ty) = _n;
328     // discriminant(_y) = z;
329     // ```
330     for (l, r) in &opt_info.field_tmp_assignments {
331         if local_uses[*l] != 2 {
332             warn!("NO: FAILED assignment chain local {:?} was used more than twice", l);
333             return false;
334         } else if local_uses[*r] != 2 {
335             warn!("NO: FAILED assignment chain local {:?} was used more than twice", r);
336             return false;
337         }
338     }
339
340     // Check that debug info only points to full Locals and not projections.
341     for dbg_idx in &opt_info.dbg_info_to_adjust {
342         let dbg_info = &var_debug_info[*dbg_idx];
343         if !dbg_info.place.projection.is_empty() {
344             trace!("NO: debug info for {:?} had a projection {:?}", dbg_info.name, dbg_info.place);
345             return false;
346         }
347     }
348
349     if source_local != opt_info.local_temp_0 {
350         trace!(
351             "NO: start of assignment chain does not match enum variant temp: {:?} != {:?}",
352             source_local,
353             opt_info.local_temp_0
354         );
355         return false;
356     } else if last_assigned_to != opt_info.local_tmp_s1 {
357         trace!(
358             "NO: end of assignemnt chain does not match written enum temp: {:?} != {:?}",
359             last_assigned_to,
360             opt_info.local_tmp_s1
361         );
362         return false;
363     }
364
365     trace!("SUCCESS: optimization applies!");
366     true
367 }
368
369 impl<'tcx> MirPass<'tcx> for SimplifyArmIdentity {
370     fn run_pass(&self, _tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
371         trace!("running SimplifyArmIdentity on {:?}", source);
372         let local_uses = LocalUseCounter::get_local_uses(body);
373         let (basic_blocks, local_decls, debug_info) =
374             body.basic_blocks_local_decls_mut_and_var_debug_info();
375         for bb in basic_blocks {
376             if let Some(opt_info) =
377                 get_arm_identity_info(&bb.statements, local_decls.len(), debug_info)
378             {
379                 trace!("got opt_info = {:#?}", opt_info);
380                 if !optimization_applies(&opt_info, local_decls, &local_uses, &debug_info) {
381                     debug!("optimization skipped for {:?}", source);
382                     continue;
383                 }
384
385                 // Also remove unused Storage{Live,Dead} statements which correspond
386                 // to temps used previously.
387                 for (live_idx, dead_idx, local) in &opt_info.storage_stmts {
388                     // The temporary that we've read the variant field into is scoped to this block,
389                     // so we can remove the assignment.
390                     if *local == opt_info.local_temp_0 {
391                         bb.statements[opt_info.get_variant_field_stmt].make_nop();
392                     }
393
394                     for (left, right) in &opt_info.field_tmp_assignments {
395                         if local == left || local == right {
396                             bb.statements[*live_idx].make_nop();
397                             bb.statements[*dead_idx].make_nop();
398                         }
399                     }
400                 }
401
402                 // Right shape; transform
403                 for stmt_idx in opt_info.stmts_to_remove {
404                     bb.statements[stmt_idx].make_nop();
405                 }
406
407                 let stmt = &mut bb.statements[opt_info.stmt_to_overwrite];
408                 stmt.source_info = opt_info.source_info;
409                 stmt.kind = StatementKind::Assign(box (
410                     opt_info.local_0.into(),
411                     Rvalue::Use(Operand::Move(opt_info.local_1.into())),
412                 ));
413
414                 bb.statements.retain(|stmt| stmt.kind != StatementKind::Nop);
415
416                 // Fix the debug info to point to the right local
417                 for dbg_index in opt_info.dbg_info_to_adjust {
418                     let dbg_info = &mut debug_info[dbg_index];
419                     assert!(dbg_info.place.projection.is_empty());
420                     dbg_info.place.local = opt_info.local_0;
421                     dbg_info.place.projection = opt_info.dbg_projection;
422                 }
423
424                 trace!("block is now {:?}", bb.statements);
425             }
426         }
427     }
428 }
429
430 struct LocalUseCounter {
431     local_uses: IndexVec<Local, usize>,
432 }
433
434 impl LocalUseCounter {
435     fn get_local_uses<'tcx>(body: &Body<'tcx>) -> IndexVec<Local, usize> {
436         let mut counter = LocalUseCounter { local_uses: IndexVec::from_elem(0, &body.local_decls) };
437         counter.visit_body(body);
438         counter.local_uses
439     }
440 }
441
442 impl<'tcx> Visitor<'tcx> for LocalUseCounter {
443     fn visit_local(&mut self, local: &Local, context: PlaceContext, _location: Location) {
444         if context.is_storage_marker()
445             || context == PlaceContext::NonUse(NonUseContext::VarDebugInfo)
446         {
447             return;
448         }
449
450         self.local_uses[*local] += 1;
451     }
452 }
453
454 /// Match on:
455 /// ```rust
456 /// _LOCAL_INTO = ((_LOCAL_FROM as Variant).FIELD: TY);
457 /// ```
458 fn match_get_variant_field<'tcx>(
459     stmt: &Statement<'tcx>,
460 ) -> Option<(Local, Local, VarField<'tcx>, &'tcx List<PlaceElem<'tcx>>)> {
461     match &stmt.kind {
462         StatementKind::Assign(box (
463             place_into,
464             Rvalue::Use(Operand::Copy(pf) | Operand::Move(pf)),
465         )) => {
466             let local_into = place_into.as_local()?;
467             let (local_from, vf) = match_variant_field_place(*pf)?;
468             Some((local_into, local_from, vf, pf.projection))
469         }
470         _ => None,
471     }
472 }
473
474 /// Match on:
475 /// ```rust
476 /// ((_LOCAL_FROM as Variant).FIELD: TY) = move _LOCAL_INTO;
477 /// ```
478 fn match_set_variant_field<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, Local, VarField<'tcx>)> {
479     match &stmt.kind {
480         StatementKind::Assign(box (place_from, Rvalue::Use(Operand::Move(place_into)))) => {
481             let local_into = place_into.as_local()?;
482             let (local_from, vf) = match_variant_field_place(*place_from)?;
483             Some((local_into, local_from, vf))
484         }
485         _ => None,
486     }
487 }
488
489 /// Match on:
490 /// ```rust
491 /// discriminant(_LOCAL_TO_SET) = VAR_IDX;
492 /// ```
493 fn match_set_discr<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, VariantIdx)> {
494     match &stmt.kind {
495         StatementKind::SetDiscriminant { place, variant_index } => {
496             Some((place.as_local()?, *variant_index))
497         }
498         _ => None,
499     }
500 }
501
502 #[derive(PartialEq, Debug)]
503 struct VarField<'tcx> {
504     field: Field,
505     field_ty: Ty<'tcx>,
506     var_idx: VariantIdx,
507 }
508
509 /// Match on `((_LOCAL as Variant).FIELD: TY)`.
510 fn match_variant_field_place<'tcx>(place: Place<'tcx>) -> Option<(Local, VarField<'tcx>)> {
511     match place.as_ref() {
512         PlaceRef {
513             local,
514             projection: &[ProjectionElem::Downcast(_, var_idx), ProjectionElem::Field(field, ty)],
515         } => Some((local, VarField { field, field_ty: ty, var_idx })),
516         _ => None,
517     }
518 }
519
520 /// Simplifies `SwitchInt(_) -> [targets]`,
521 /// where all the `targets` have the same form,
522 /// into `goto -> target_first`.
523 pub struct SimplifyBranchSame;
524
525 impl<'tcx> MirPass<'tcx> for SimplifyBranchSame {
526     fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
527         trace!("Running SimplifyBranchSame on {:?}", source);
528         let finder = SimplifyBranchSameOptimizationFinder { body, tcx };
529         let opts = finder.find();
530
531         let did_remove_blocks = opts.len() > 0;
532         for opt in opts.iter() {
533             trace!("SUCCESS: Applying optimization {:?}", opt);
534             // Replace `SwitchInt(..) -> [bb_first, ..];` with a `goto -> bb_first;`.
535             body.basic_blocks_mut()[opt.bb_to_opt_terminator].terminator_mut().kind =
536                 TerminatorKind::Goto { target: opt.bb_to_goto };
537         }
538
539         if did_remove_blocks {
540             // We have dead blocks now, so remove those.
541             simplify::remove_dead_blocks(body);
542         }
543     }
544 }
545
546 #[derive(Debug)]
547 struct SimplifyBranchSameOptimization {
548     /// All basic blocks are equal so go to this one
549     bb_to_goto: BasicBlock,
550     /// Basic block where the terminator can be simplified to a goto
551     bb_to_opt_terminator: BasicBlock,
552 }
553
554 struct SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
555     body: &'a Body<'tcx>,
556     tcx: TyCtxt<'tcx>,
557 }
558
559 impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
560     fn find(&self) -> Vec<SimplifyBranchSameOptimization> {
561         self.body
562             .basic_blocks()
563             .iter_enumerated()
564             .filter_map(|(bb_idx, bb)| {
565                 let (discr_switched_on, targets) = match &bb.terminator().kind {
566                     TerminatorKind::SwitchInt { targets, discr, .. } => (discr, targets),
567                     _ => return None,
568                 };
569
570                 // find the adt that has its discriminant read
571                 // assuming this must be the last statement of the block
572                 let adt_matched_on = match &bb.statements.last()?.kind {
573                     StatementKind::Assign(box (place, rhs))
574                         if Some(*place) == discr_switched_on.place() =>
575                     {
576                         match rhs {
577                             Rvalue::Discriminant(adt_place) if adt_place.ty(self.body, self.tcx).ty.is_enum() => adt_place,
578                             _ => {
579                                 trace!("NO: expected a discriminant read of an enum instead of: {:?}", rhs);
580                                 return None;
581                             }
582                         }
583                     }
584                     other => {
585                         trace!("NO: expected an assignment of a discriminant read to a place. Found: {:?}", other);
586                         return None
587                     },
588                 };
589
590                 let mut iter_bbs_reachable = targets
591                     .iter()
592                     .map(|idx| (*idx, &self.body.basic_blocks()[*idx]))
593                     .filter(|(_, bb)| {
594                         // Reaching `unreachable` is UB so assume it doesn't happen.
595                         bb.terminator().kind != TerminatorKind::Unreachable
596                     // But `asm!(...)` could abort the program,
597                     // so we cannot assume that the `unreachable` terminator itself is reachable.
598                     // FIXME(Centril): use a normalization pass instead of a check.
599                     || bb.statements.iter().any(|stmt| match stmt.kind {
600                         StatementKind::LlvmInlineAsm(..) => true,
601                         _ => false,
602                     })
603                     })
604                     .peekable();
605
606                 let bb_first = iter_bbs_reachable.peek().map(|(idx, _)| *idx).unwrap_or(targets[0]);
607                 let mut all_successors_equivalent = StatementEquality::TrivialEqual;
608
609                 // All successor basic blocks must be equal or contain statements that are pairwise considered equal.
610                 for ((bb_l_idx,bb_l), (bb_r_idx,bb_r)) in iter_bbs_reachable.tuple_windows() {
611                     let trivial_checks = bb_l.is_cleanup == bb_r.is_cleanup
612                     && bb_l.terminator().kind == bb_r.terminator().kind;
613                     let statement_check = || {
614                         bb_l.statements.iter().zip(&bb_r.statements).try_fold(StatementEquality::TrivialEqual, |acc,(l,r)| {
615                             let stmt_equality = self.statement_equality(*adt_matched_on, &l, bb_l_idx, &r, bb_r_idx, self.tcx.sess.opts.debugging_opts.mir_opt_level);
616                             if matches!(stmt_equality, StatementEquality::NotEqual) {
617                                 // short circuit
618                                 None
619                             } else {
620                                 Some(acc.combine(&stmt_equality))
621                             }
622                         })
623                         .unwrap_or(StatementEquality::NotEqual)
624                     };
625                     if !trivial_checks {
626                         all_successors_equivalent = StatementEquality::NotEqual;
627                         break;
628                     }
629                     all_successors_equivalent = all_successors_equivalent.combine(&statement_check());
630                 };
631
632                 match all_successors_equivalent{
633                     StatementEquality::TrivialEqual => {
634                         // statements are trivially equal, so just take first
635                         trace!("Statements are trivially equal");
636                         Some(SimplifyBranchSameOptimization {
637                             bb_to_goto: bb_first,
638                             bb_to_opt_terminator: bb_idx,
639                         })
640                     }
641                     StatementEquality::ConsideredEqual(bb_to_choose) => {
642                         trace!("Statements are considered equal");
643                         Some(SimplifyBranchSameOptimization {
644                             bb_to_goto: bb_to_choose,
645                             bb_to_opt_terminator: bb_idx,
646                         })
647                     }
648                     StatementEquality::NotEqual => {
649                         trace!("NO: not all successors of basic block {:?} were equivalent", bb_idx);
650                         None
651                     }
652                 }
653             })
654             .collect()
655     }
656
657     /// Tests if two statements can be considered equal
658     ///
659     /// Statements can be trivially equal if the kinds match.
660     /// But they can also be considered equal in the following case A:
661     /// ```
662     /// discriminant(_0) = 0;   // bb1
663     /// _0 = move _1;           // bb2
664     /// ```
665     /// In this case the two statements are equal iff
666     /// 1: _0 is an enum where the variant index 0 is fieldless, and
667     /// 2:  bb1 was targeted by a switch where the discriminant of _1 was switched on
668     fn statement_equality(
669         &self,
670         adt_matched_on: Place<'tcx>,
671         x: &Statement<'tcx>,
672         x_bb_idx: BasicBlock,
673         y: &Statement<'tcx>,
674         y_bb_idx: BasicBlock,
675         mir_opt_level: usize,
676     ) -> StatementEquality {
677         let helper = |rhs: &Rvalue<'tcx>,
678                       place: &Place<'tcx>,
679                       variant_index: &VariantIdx,
680                       side_to_choose| {
681             let place_type = place.ty(self.body, self.tcx).ty;
682             let adt = match *place_type.kind() {
683                 ty::Adt(adt, _) if adt.is_enum() => adt,
684                 _ => return StatementEquality::NotEqual,
685             };
686             let variant_is_fieldless = adt.variants[*variant_index].fields.is_empty();
687             if !variant_is_fieldless {
688                 trace!("NO: variant {:?} was not fieldless", variant_index);
689                 return StatementEquality::NotEqual;
690             }
691
692             match rhs {
693                 Rvalue::Use(operand) if operand.place() == Some(adt_matched_on) => {
694                     // FIXME(76803): This logic is currently broken because it does not take into
695                     // account the current discriminant value.
696                     if mir_opt_level > 2 {
697                         StatementEquality::ConsideredEqual(side_to_choose)
698                     } else {
699                         StatementEquality::NotEqual
700                     }
701                 }
702                 _ => {
703                     trace!(
704                         "NO: RHS of assignment was {:?}, but expected it to match the adt being matched on in the switch, which is {:?}",
705                         rhs,
706                         adt_matched_on
707                     );
708                     StatementEquality::NotEqual
709                 }
710             }
711         };
712         match (&x.kind, &y.kind) {
713             // trivial case
714             (x, y) if x == y => StatementEquality::TrivialEqual,
715
716             // check for case A
717             (
718                 StatementKind::Assign(box (_, rhs)),
719                 StatementKind::SetDiscriminant { place, variant_index },
720             ) => {
721                 // choose basic block of x, as that has the assign
722                 helper(rhs, place, variant_index, x_bb_idx)
723             }
724             (
725                 StatementKind::SetDiscriminant { place, variant_index },
726                 StatementKind::Assign(box (_, rhs)),
727             ) => {
728                 // choose basic block of y, as that has the assign
729                 helper(rhs, place, variant_index, y_bb_idx)
730             }
731             _ => {
732                 trace!("NO: statements `{:?}` and `{:?}` not considered equal", x, y);
733                 StatementEquality::NotEqual
734             }
735         }
736     }
737 }
738
739 #[derive(Copy, Clone, Eq, PartialEq)]
740 enum StatementEquality {
741     /// The two statements are trivially equal; same kind
742     TrivialEqual,
743     /// The two statements are considered equal, but may be of different kinds. The BasicBlock field is the basic block to jump to when performing the branch-same optimization.
744     /// For example, `_0 = _1` and `discriminant(_0) = discriminant(0)` are considered equal if 0 is a fieldless variant of an enum. But we don't want to jump to the basic block with the SetDiscriminant, as that is not legal if _1 is not the 0 variant index
745     ConsideredEqual(BasicBlock),
746     /// The two statements are not equal
747     NotEqual,
748 }
749
750 impl StatementEquality {
751     fn combine(&self, other: &StatementEquality) -> StatementEquality {
752         use StatementEquality::*;
753         match (self, other) {
754             (TrivialEqual, TrivialEqual) => TrivialEqual,
755             (TrivialEqual, ConsideredEqual(b)) | (ConsideredEqual(b), TrivialEqual) => {
756                 ConsideredEqual(*b)
757             }
758             (ConsideredEqual(b1), ConsideredEqual(b2)) => {
759                 if b1 == b2 {
760                     ConsideredEqual(*b1)
761                 } else {
762                     NotEqual
763                 }
764             }
765             (_, NotEqual) | (NotEqual, _) => NotEqual,
766         }
767     }
768 }