Skip to content

Commit c7f866c

Browse files
committed
Skip switch if all targets are the same.
1 parent 3e86cd2 commit c7f866c

11 files changed

Lines changed: 81 additions & 335 deletions

compiler/rustc_mir_transform/src/elaborate_drop.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{fmt, iter, mem};
22

3+
use itertools::Itertools;
34
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
45
use rustc_hir::lang_items::LangItem;
56
use rustc_hir::{CoroutineDesugaring, CoroutineKind};
@@ -1196,27 +1197,29 @@ where
11961197
succ: BasicBlock,
11971198
unwind: Unwind,
11981199
) -> BasicBlock {
1199-
// If there are multiple variants, then if something
1200-
// is present within the enum the discriminant, tracked
1201-
// by the rest path, must be initialized.
1202-
//
1203-
// Additionally, we do not want to switch on the
1204-
// discriminant after it is free-ed, because that
1205-
// way lies only trouble.
1206-
let discr_ty = adt.repr().discr_type().to_ty(self.tcx());
1207-
let discr = Place::from(self.new_temp(discr_ty));
1208-
let discr_rv = Rvalue::Discriminant(self.place);
1209-
let switch_block = self.new_block_with_statements(
1210-
unwind,
1211-
vec![self.assign(discr, discr_rv)],
1212-
TerminatorKind::SwitchInt {
1213-
discr: Operand::Move(discr),
1214-
targets: SwitchTargets::new(
1215-
values.iter().copied().zip(blocks.iter().copied()),
1216-
*blocks.last().unwrap(),
1217-
),
1218-
},
1219-
);
1200+
let switch_block = blocks.iter().copied().all_equal_value().unwrap_or_else(|_| {
1201+
// If there are multiple variants, then if something
1202+
// is present within the enum the discriminant, tracked
1203+
// by the rest path, must be initialized.
1204+
//
1205+
// Additionally, we do not want to switch on the
1206+
// discriminant after it is free-ed, because that
1207+
// way lies only trouble.
1208+
let discr_ty = adt.repr().discr_type().to_ty(self.tcx());
1209+
let discr = Place::from(self.new_temp(discr_ty));
1210+
let discr_rv = Rvalue::Discriminant(self.place);
1211+
self.new_block_with_statements(
1212+
unwind,
1213+
vec![self.assign(discr, discr_rv)],
1214+
TerminatorKind::SwitchInt {
1215+
discr: Operand::Move(discr),
1216+
targets: SwitchTargets::new(
1217+
values.iter().copied().zip(blocks.iter().copied()),
1218+
*blocks.last().unwrap(),
1219+
),
1220+
},
1221+
)
1222+
});
12201223
self.drop_flag_test_block(switch_block, succ, unwind)
12211224
}
12221225

tests/mir-opt/building/fallible_struct_drop.build.ElaborateDrops.diff

Lines changed: 23 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,6 @@
5959
+ let mut _54: bool;
6060
+ let mut _55: bool;
6161
+ let mut _56: bool;
62-
+ let mut _57: isize;
63-
+ let mut _58: isize;
64-
+ let mut _59: isize;
65-
+ let mut _60: isize;
66-
+ let mut _61: isize;
67-
+ let mut _62: isize;
68-
+ let mut _63: isize;
69-
+ let mut _64: isize;
70-
+ let mut _65: isize;
71-
+ let mut _66: isize;
72-
+ let mut _67: isize;
73-
+ let mut _68: isize;
74-
+ let mut _69: isize;
75-
+ let mut _70: isize;
76-
+ let mut _71: isize;
77-
+ let mut _72: isize;
78-
+ let mut _73: isize;
79-
+ let mut _74: isize;
80-
+ let mut _75: isize;
81-
+ let mut _76: isize;
82-
+ let mut _77: isize;
83-
+ let mut _78: isize;
84-
+ let mut _79: isize;
85-
+ let mut _80: isize;
86-
+ let mut _81: isize;
8762
scope 1 {
8863
debug residual => _9;
8964
scope 2 {
@@ -429,34 +404,34 @@
429404
bb37: {
430405
StorageDead(_2);
431406
- drop(_44) -> [return: bb38, unwind: bb78];
432-
+ goto -> bb94;
407+
+ goto -> bb93;
433408
}
434409

435410
bb38: {
436411
StorageDead(_44);
437412
- drop(_34) -> [return: bb39, unwind: bb83];
438-
+ goto -> bb97;
413+
+ goto -> bb94;
439414
}
440415

441416
bb39: {
442417
+ _53 = const false;
443418
StorageDead(_34);
444419
- drop(_24) -> [return: bb40, unwind: bb87];
445-
+ goto -> bb100;
420+
+ goto -> bb95;
446421
}
447422

448423
bb40: {
449424
+ _54 = const false;
450425
StorageDead(_24);
451426
- drop(_14) -> [return: bb41, unwind: bb90];
452-
+ goto -> bb103;
427+
+ goto -> bb96;
453428
}
454429

455430
bb41: {
456431
+ _55 = const false;
457432
StorageDead(_14);
458433
- drop(_4) -> [return: bb42, unwind continue];
459-
+ goto -> bb106;
434+
+ goto -> bb97;
460435
}
461436

462437
bb42: {
@@ -484,7 +459,7 @@
484459
StorageDead(_3);
485460
StorageDead(_2);
486461
- drop(_44) -> [return: bb47, unwind: bb78];
487-
+ goto -> bb109;
462+
+ goto -> bb98;
488463
}
489464

490465
bb47: {
@@ -510,7 +485,7 @@
510485

511486
bb51: {
512487
- drop(_34) -> [return: bb52, unwind: bb83];
513-
+ goto -> bb112;
488+
+ goto -> bb99;
514489
}
515490

516491
bb52: {
@@ -532,7 +507,7 @@
532507

533508
bb55: {
534509
- drop(_24) -> [return: bb56, unwind: bb87];
535-
+ goto -> bb115;
510+
+ goto -> bb100;
536511
}
537512

538513
bb56: {
@@ -549,7 +524,7 @@
549524

550525
bb58: {
551526
- drop(_14) -> [return: bb59, unwind: bb90];
552-
+ goto -> bb118;
527+
+ goto -> bb101;
553528
}
554529

555530
bb59: {
@@ -560,7 +535,7 @@
560535

561536
bb60: {
562537
- drop(_4) -> [return: bb61, unwind continue];
563-
+ goto -> bb121;
538+
+ goto -> bb102;
564539
}
565540

566541
bb61: {
@@ -616,7 +591,7 @@
616591

617592
bb72 (cleanup): {
618593
- drop(_44) -> [return: bb78, unwind terminate(cleanup)];
619-
+ goto -> bb123;
594+
+ goto -> bb78;
620595
}
621596

622597
bb73 (cleanup): {
@@ -642,7 +617,7 @@
642617

643618
bb78 (cleanup): {
644619
- drop(_34) -> [return: bb83, unwind terminate(cleanup)];
645-
+ goto -> bb124;
620+
+ goto -> bb83;
646621
}
647622

648623
bb79 (cleanup): {
@@ -664,7 +639,7 @@
664639

665640
bb83 (cleanup): {
666641
- drop(_24) -> [return: bb87, unwind terminate(cleanup)];
667-
+ goto -> bb125;
642+
+ goto -> bb87;
668643
}
669644

670645
bb84 (cleanup): {
@@ -682,7 +657,7 @@
682657

683658
bb87 (cleanup): {
684659
- drop(_14) -> [return: bb90, unwind terminate(cleanup)];
685-
+ goto -> bb126;
660+
+ goto -> bb90;
686661
}
687662

688663
bb88 (cleanup): {
@@ -696,7 +671,7 @@
696671

697672
bb90 (cleanup): {
698673
- drop(_4) -> [return: bb92, unwind terminate(cleanup)];
699-
+ goto -> bb127;
674+
+ goto -> bb92;
700675
}
701676

702677
bb91 (cleanup): {
@@ -713,164 +688,39 @@
713688
+ }
714689
+
715690
+ bb94: {
716-
+ _57 = discriminant(_44);
717-
+ switchInt(move _57) -> [0: bb93, otherwise: bb93];
718-
+ }
719-
+
720-
+ bb95 (cleanup): {
721-
+ _58 = discriminant(_44);
722-
+ switchInt(move _58) -> [0: bb78, otherwise: bb78];
723-
+ }
724-
+
725-
+ bb96: {
726691
+ goto -> bb39;
727692
+ }
728693
+
729-
+ bb97: {
730-
+ _59 = discriminant(_34);
731-
+ switchInt(move _59) -> [0: bb96, otherwise: bb96];
732-
+ }
733-
+
734-
+ bb98 (cleanup): {
735-
+ _60 = discriminant(_34);
736-
+ switchInt(move _60) -> [0: bb83, otherwise: bb83];
737-
+ }
738-
+
739-
+ bb99: {
694+
+ bb95: {
740695
+ goto -> bb40;
741696
+ }
742697
+
743-
+ bb100: {
744-
+ _61 = discriminant(_24);
745-
+ switchInt(move _61) -> [0: bb99, otherwise: bb99];
746-
+ }
747-
+
748-
+ bb101 (cleanup): {
749-
+ _62 = discriminant(_24);
750-
+ switchInt(move _62) -> [0: bb87, otherwise: bb87];
751-
+ }
752-
+
753-
+ bb102: {
698+
+ bb96: {
754699
+ goto -> bb41;
755700
+ }
756701
+
757-
+ bb103: {
758-
+ _63 = discriminant(_14);
759-
+ switchInt(move _63) -> [0: bb102, otherwise: bb102];
760-
+ }
761-
+
762-
+ bb104 (cleanup): {
763-
+ _64 = discriminant(_14);
764-
+ switchInt(move _64) -> [0: bb90, otherwise: bb90];
765-
+ }
766-
+
767-
+ bb105: {
702+
+ bb97: {
768703
+ goto -> bb42;
769704
+ }
770705
+
771-
+ bb106: {
772-
+ _65 = discriminant(_4);
773-
+ switchInt(move _65) -> [0: bb105, otherwise: bb105];
774-
+ }
775-
+
776-
+ bb107 (cleanup): {
777-
+ _66 = discriminant(_4);
778-
+ switchInt(move _66) -> [0: bb92, otherwise: bb92];
779-
+ }
780-
+
781-
+ bb108: {
706+
+ bb98: {
782707
+ goto -> bb47;
783708
+ }
784709
+
785-
+ bb109: {
786-
+ _67 = discriminant(_44);
787-
+ switchInt(move _67) -> [0: bb108, otherwise: bb108];
788-
+ }
789-
+
790-
+ bb110 (cleanup): {
791-
+ _68 = discriminant(_44);
792-
+ switchInt(move _68) -> [0: bb78, otherwise: bb78];
793-
+ }
794-
+
795-
+ bb111: {
710+
+ bb99: {
796711
+ goto -> bb52;
797712
+ }
798713
+
799-
+ bb112: {
800-
+ _69 = discriminant(_34);
801-
+ switchInt(move _69) -> [0: bb111, otherwise: bb111];
802-
+ }
803-
+
804-
+ bb113 (cleanup): {
805-
+ _70 = discriminant(_34);
806-
+ switchInt(move _70) -> [0: bb83, otherwise: bb83];
807-
+ }
808-
+
809-
+ bb114: {
714+
+ bb100: {
810715
+ goto -> bb56;
811716
+ }
812717
+
813-
+ bb115: {
814-
+ _71 = discriminant(_24);
815-
+ switchInt(move _71) -> [0: bb114, otherwise: bb114];
816-
+ }
817-
+
818-
+ bb116 (cleanup): {
819-
+ _72 = discriminant(_24);
820-
+ switchInt(move _72) -> [0: bb87, otherwise: bb87];
821-
+ }
822-
+
823-
+ bb117: {
718+
+ bb101: {
824719
+ goto -> bb59;
825720
+ }
826721
+
827-
+ bb118: {
828-
+ _73 = discriminant(_14);
829-
+ switchInt(move _73) -> [0: bb117, otherwise: bb117];
830-
+ }
831-
+
832-
+ bb119 (cleanup): {
833-
+ _74 = discriminant(_14);
834-
+ switchInt(move _74) -> [0: bb90, otherwise: bb90];
835-
+ }
836-
+
837-
+ bb120: {
722+
+ bb102: {
838723
+ goto -> bb61;
839-
+ }
840-
+
841-
+ bb121: {
842-
+ _75 = discriminant(_4);
843-
+ switchInt(move _75) -> [0: bb120, otherwise: bb120];
844-
+ }
845-
+
846-
+ bb122 (cleanup): {
847-
+ _76 = discriminant(_4);
848-
+ switchInt(move _76) -> [0: bb92, otherwise: bb92];
849-
+ }
850-
+
851-
+ bb123 (cleanup): {
852-
+ _77 = discriminant(_44);
853-
+ switchInt(move _77) -> [0: bb78, otherwise: bb78];
854-
+ }
855-
+
856-
+ bb124 (cleanup): {
857-
+ _78 = discriminant(_34);
858-
+ switchInt(move _78) -> [0: bb83, otherwise: bb83];
859-
+ }
860-
+
861-
+ bb125 (cleanup): {
862-
+ _79 = discriminant(_24);
863-
+ switchInt(move _79) -> [0: bb87, otherwise: bb87];
864-
+ }
865-
+
866-
+ bb126 (cleanup): {
867-
+ _80 = discriminant(_14);
868-
+ switchInt(move _80) -> [0: bb90, otherwise: bb90];
869-
+ }
870-
+
871-
+ bb127 (cleanup): {
872-
+ _81 = discriminant(_4);
873-
+ switchInt(move _81) -> [0: bb92, otherwise: bb92];
874724
}
875725
}
876726

0 commit comments

Comments
 (0)