Skip to content

Commit 4db3d12

Browse files
committed
Auto merge of #128265 - DianQK:instsimplify-before-inline, r=saethlin
Perform instsimplify before inline to eliminate some trivial calls I am currently working on #128081. In the current pipeline, we can get the following clone statements ([godbolt](https://rust.godbolt.org/z/931316fhP)): ``` bb0: { StorageLive(_2); _2 = ((*_1).0: i32); StorageLive(_3); _3 = ((*_1).1: u64); _0 = Foo { a: move _2, b: move _3 }; StorageDead(_3); StorageDead(_2); return; } ``` Analyzing such statements will be simple and fast. We don't need to consider branches or some interfering statements. However, this requires us to run `InstSimplify`, `ReferencePropagation`, and `SimplifyCFG` at least once. I can introduce a new pass, but I think the best place for it would be within `InstSimplify`. I put `InstSimplify` before `Inline`, which takes some of the burden away from `Inline`. r? `@saethlin`
2 parents 56c698c + ae681c9 commit 4db3d12

File tree

83 files changed

+226
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+226
-182
lines changed

compiler/rustc_mir_transform/src/instsimplify.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@ use rustc_target::spec::abi::Abi;
1313
use crate::simplify::simplify_duplicate_switch_targets;
1414
use crate::take_array;
1515

16-
pub struct InstSimplify;
16+
pub enum InstSimplify {
17+
BeforeInline,
18+
AfterSimplifyCfg,
19+
}
20+
21+
impl InstSimplify {
22+
pub fn name(&self) -> &'static str {
23+
match self {
24+
InstSimplify::BeforeInline => "InstSimplify-before-inline",
25+
InstSimplify::AfterSimplifyCfg => "InstSimplify-after-simplifycfg",
26+
}
27+
}
28+
}
1729

1830
impl<'tcx> MirPass<'tcx> for InstSimplify {
31+
fn name(&self) -> &'static str {
32+
self.name()
33+
}
34+
1935
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
2036
sess.mir_opt_level() > 0
2137
}

compiler/rustc_mir_transform/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
571571
// Has to be done before inlining, otherwise actual call will be almost always inlined.
572572
// Also simple, so can just do first
573573
&lower_slice_len::LowerSliceLenCalls,
574+
// Perform instsimplify before inline to eliminate some trivial calls (like clone shims).
575+
&instsimplify::InstSimplify::BeforeInline,
574576
// Perform inlining, which may add a lot of code.
575577
&inline::Inline,
576578
// Code from other crates may have storage markers, so this needs to happen after inlining.
@@ -590,7 +592,8 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
590592
&match_branches::MatchBranchSimplification,
591593
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
592594
&multiple_return_terminators::MultipleReturnTerminators,
593-
&instsimplify::InstSimplify,
595+
// After simplifycfg, it allows us to discover new opportunities for peephole optimizations.
596+
&instsimplify::InstSimplify::AfterSimplifyCfg,
594597
&simplify::SimplifyLocals::BeforeConstProp,
595598
&dead_store_elimination::DeadStoreElimination::Initial,
596599
&gvn::GVN,

compiler/rustc_mir_transform/src/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
155155
&deref_separator::Derefer,
156156
&remove_noop_landing_pads::RemoveNoopLandingPads,
157157
&simplify::SimplifyCfg::MakeShim,
158-
&instsimplify::InstSimplify,
158+
&instsimplify::InstSimplify::BeforeInline,
159159
&abort_unwinding_calls::AbortUnwindingCalls,
160160
&add_call_guards::CriticalCallEdges,
161161
],

tests/incremental/hashes/call_expressions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn change_to_ufcs() {
162162
}
163163

164164
#[cfg(not(any(cfail1,cfail4)))]
165-
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck")]
165+
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck")]
166166
#[rustc_clean(cfg="cfail3")]
167167
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck")]
168168
#[rustc_clean(cfg="cfail6")]

tests/mir-opt/const_prop/slice_len.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ test-mir-pass: GVN
2-
//@ compile-flags: -Zmir-enable-passes=+InstSimplify -Zdump-mir-exclude-alloc-bytes
2+
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-after-simplifycfg -Zdump-mir-exclude-alloc-bytes
33
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
44
// EMIT_MIR_FOR_EACH_BIT_WIDTH
55

tests/mir-opt/dataflow-const-prop/slice_len.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
22
//@ test-mir-pass: DataflowConstProp
3-
//@ compile-flags: -Zmir-enable-passes=+InstSimplify
3+
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-after-simplifycfg
44
// EMIT_MIR_FOR_EACH_BIT_WIDTH
55

66
// EMIT_MIR slice_len.main.DataflowConstProp.diff

tests/mir-opt/inline/dyn_trait.get_query.Inline.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
bb0: {
2222
StorageLive(_2);
2323
StorageLive(_3);
24-
_3 = &(*_1);
24+
_3 = _1;
2525
_2 = <Q as Query>::cache::<T>(move _3) -> [return: bb1, unwind unreachable];
2626
}
2727

2828
bb1: {
2929
StorageDead(_3);
3030
StorageLive(_4);
31-
_4 = &(*_2);
31+
_4 = _2;
3232
- _0 = try_execute_query::<<Q as Query>::C>(move _4) -> [return: bb2, unwind unreachable];
3333
+ StorageLive(_5);
3434
+ _5 = _4 as &dyn Cache<V = <Q as Query>::V> (PointerCoercion(Unsize));

tests/mir-opt/inline/dyn_trait.get_query.Inline.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
bb0: {
2222
StorageLive(_2);
2323
StorageLive(_3);
24-
_3 = &(*_1);
24+
_3 = _1;
2525
_2 = <Q as Query>::cache::<T>(move _3) -> [return: bb1, unwind continue];
2626
}
2727

2828
bb1: {
2929
StorageDead(_3);
3030
StorageLive(_4);
31-
_4 = &(*_2);
31+
_4 = _2;
3232
- _0 = try_execute_query::<<Q as Query>::C>(move _4) -> [return: bb2, unwind continue];
3333
+ StorageLive(_5);
3434
+ _5 = _4 as &dyn Cache<V = <Q as Query>::V> (PointerCoercion(Unsize));

tests/mir-opt/inline/dyn_trait.mk_cycle.Inline.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
bb0: {
1010
StorageLive(_2);
11-
_2 = &(*_1);
11+
_2 = _1;
1212
_0 = <dyn Cache<V = V> as Cache>::store_nocache(move _2) -> [return: bb1, unwind unreachable];
1313
}
1414

tests/mir-opt/inline/dyn_trait.mk_cycle.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
bb0: {
1010
StorageLive(_2);
11-
_2 = &(*_1);
11+
_2 = _1;
1212
_0 = <dyn Cache<V = V> as Cache>::store_nocache(move _2) -> [return: bb1, unwind continue];
1313
}
1414

tests/mir-opt/inline/dyn_trait.try_execute_query.Inline.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
bb0: {
1414
StorageLive(_2);
1515
StorageLive(_3);
16-
_3 = &(*_1);
16+
_3 = _1;
1717
_2 = move _3 as &dyn Cache<V = <C as Cache>::V> (PointerCoercion(Unsize));
1818
StorageDead(_3);
1919
- _0 = mk_cycle::<<C as Cache>::V>(move _2) -> [return: bb1, unwind unreachable];

tests/mir-opt/inline/dyn_trait.try_execute_query.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
bb0: {
1414
StorageLive(_2);
1515
StorageLive(_3);
16-
_3 = &(*_1);
16+
_3 = _1;
1717
_2 = move _3 as &dyn Cache<V = <C as Cache>::V> (PointerCoercion(Unsize));
1818
StorageDead(_3);
1919
- _0 = mk_cycle::<<C as Cache>::V>(move _2) -> [return: bb1, unwind continue];

tests/mir-opt/inline/inline_closure_borrows_arg.foo.Inline.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ fn foo(_1: T, _2: &i32) -> i32 {
2626
_4 = &_3;
2727
StorageLive(_5);
2828
StorageLive(_6);
29-
_6 = &(*_2);
29+
_6 = _2;
3030
StorageLive(_7);
31-
_7 = &(*_2);
31+
_7 = _2;
3232
_5 = (move _6, move _7);
3333
StorageLive(_8);
3434
_8 = move (_5.0: &i32);

tests/mir-opt/inline/inline_retag.bar.Inline.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ fn bar() -> bool {
3131
StorageLive(_4);
3232
_10 = const bar::promoted[1];
3333
Retag(_10);
34-
_4 = &(*_10);
35-
_3 = &(*_4);
34+
_4 = _10;
35+
_3 = _4;
3636
StorageLive(_6);
3737
StorageLive(_7);
3838
_9 = const bar::promoted[0];
3939
Retag(_9);
40-
_7 = &(*_9);
41-
_6 = &(*_7);
40+
_7 = _9;
41+
_6 = _7;
4242
Retag(_3);
4343
Retag(_6);
4444
StorageLive(_11);

tests/mir-opt/inline/inline_trait_method.test.Inline.after.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn test(_1: &dyn X) -> u32 {
77

88
bb0: {
99
StorageLive(_2);
10-
_2 = &(*_1);
10+
_2 = _1;
1111
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind unreachable];
1212
}
1313

tests/mir-opt/inline/inline_trait_method.test.Inline.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn test(_1: &dyn X) -> u32 {
77

88
bb0: {
99
StorageLive(_2);
10-
_2 = &(*_1);
10+
_2 = _1;
1111
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind continue];
1212
}
1313

tests/mir-opt/inline/inline_trait_method_2.test2.Inline.after.panic-abort.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn test2(_1: &dyn X) -> bool {
1212
bb0: {
1313
StorageLive(_2);
1414
StorageLive(_3);
15-
_3 = &(*_1);
16-
_2 = move _3 as &dyn X (PointerCoercion(Unsize));
15+
_3 = _1;
16+
_2 = move _3;
1717
StorageDead(_3);
1818
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind unreachable];
1919
}

tests/mir-opt/inline/inline_trait_method_2.test2.Inline.after.panic-unwind.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn test2(_1: &dyn X) -> bool {
1212
bb0: {
1313
StorageLive(_2);
1414
StorageLive(_3);
15-
_3 = &(*_1);
16-
_2 = move _3 as &dyn X (PointerCoercion(Unsize));
15+
_3 = _1;
16+
_2 = move _3;
1717
StorageDead(_3);
1818
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind continue];
1919
}

tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.a.Inline.after.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ fn a(_1: &mut [T]) -> &mut [T] {
1414
StorageLive(_2);
1515
StorageLive(_3);
1616
StorageLive(_4);
17-
_4 = &mut (*_1);
17+
_4 = _1;
1818
_3 = _4;
19-
_2 = &mut (*_3);
19+
_2 = _3;
2020
StorageDead(_4);
21-
_0 = &mut (*_2);
21+
_0 = _2;
2222
StorageDead(_3);
2323
StorageDead(_2);
2424
return;

tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.b.Inline.after.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ fn b(_1: &mut Box<T>) -> &mut T {
1616
StorageLive(_2);
1717
StorageLive(_3);
1818
StorageLive(_4);
19-
_4 = &mut (*_1);
19+
_4 = _1;
2020
StorageLive(_5);
2121
StorageLive(_6);
2222
_5 = (*_4);
2323
_6 = (((_5.0: std::ptr::Unique<T>).0: std::ptr::NonNull<T>).0: *const T);
2424
_3 = &mut (*_6);
2525
StorageDead(_6);
2626
StorageDead(_5);
27-
_2 = &mut (*_3);
27+
_2 = _3;
2828
StorageDead(_4);
29-
_0 = &mut (*_2);
29+
_0 = _2;
3030
StorageDead(_3);
3131
StorageDead(_2);
3232
return;

tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.c.Inline.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ fn c(_1: &[T]) -> &[T] {
1212
bb0: {
1313
StorageLive(_2);
1414
StorageLive(_3);
15-
_3 = &(*_1);
15+
_3 = _1;
1616
_2 = _3;
17-
_0 = &(*_2);
17+
_0 = _2;
1818
StorageDead(_3);
1919
StorageDead(_2);
2020
return;

tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.d.Inline.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ fn d(_1: &Box<T>) -> &T {
1414
bb0: {
1515
StorageLive(_2);
1616
StorageLive(_3);
17-
_3 = &(*_1);
17+
_3 = _1;
1818
StorageLive(_4);
1919
StorageLive(_5);
2020
_4 = (*_3);
2121
_5 = (((_4.0: std::ptr::Unique<T>).0: std::ptr::NonNull<T>).0: *const T);
2222
_2 = &(*_5);
2323
StorageDead(_5);
2424
StorageDead(_4);
25-
_0 = &(*_2);
25+
_0 = _2;
2626
StorageDead(_3);
2727
StorageDead(_2);
2828
return;

tests/mir-opt/instsimplify/bool_compare.eq_false.InstSimplify.diff tests/mir-opt/instsimplify/bool_compare.eq_false.InstSimplify-after-simplifycfg.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `eq_false` before InstSimplify
2-
+ // MIR for `eq_false` after InstSimplify
1+
- // MIR for `eq_false` before InstSimplify-after-simplifycfg
2+
+ // MIR for `eq_false` after InstSimplify-after-simplifycfg
33

44
fn eq_false(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.eq_true.InstSimplify.diff tests/mir-opt/instsimplify/bool_compare.eq_true.InstSimplify-after-simplifycfg.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `eq_true` before InstSimplify
2-
+ // MIR for `eq_true` after InstSimplify
1+
- // MIR for `eq_true` before InstSimplify-after-simplifycfg
2+
+ // MIR for `eq_true` after InstSimplify-after-simplifycfg
33

44
fn eq_true(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.false_eq.InstSimplify.diff tests/mir-opt/instsimplify/bool_compare.false_eq.InstSimplify-after-simplifycfg.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `false_eq` before InstSimplify
2-
+ // MIR for `false_eq` after InstSimplify
1+
- // MIR for `false_eq` before InstSimplify-after-simplifycfg
2+
+ // MIR for `false_eq` after InstSimplify-after-simplifycfg
33

44
fn false_eq(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.false_ne.InstSimplify.diff tests/mir-opt/instsimplify/bool_compare.false_ne.InstSimplify-after-simplifycfg.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `false_ne` before InstSimplify
2-
+ // MIR for `false_ne` after InstSimplify
1+
- // MIR for `false_ne` before InstSimplify-after-simplifycfg
2+
+ // MIR for `false_ne` after InstSimplify-after-simplifycfg
33

44
fn false_ne(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.ne_false.InstSimplify.diff tests/mir-opt/instsimplify/bool_compare.ne_false.InstSimplify-after-simplifycfg.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `ne_false` before InstSimplify
2-
+ // MIR for `ne_false` after InstSimplify
1+
- // MIR for `ne_false` before InstSimplify-after-simplifycfg
2+
+ // MIR for `ne_false` after InstSimplify-after-simplifycfg
33

44
fn ne_false(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.ne_true.InstSimplify.diff tests/mir-opt/instsimplify/bool_compare.ne_true.InstSimplify-after-simplifycfg.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `ne_true` before InstSimplify
2-
+ // MIR for `ne_true` after InstSimplify
1+
- // MIR for `ne_true` before InstSimplify-after-simplifycfg
2+
+ // MIR for `ne_true` after InstSimplify-after-simplifycfg
33

44
fn ne_true(_1: bool) -> u32 {
55
debug x => _1;

tests/mir-opt/instsimplify/bool_compare.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
//@ test-mir-pass: InstSimplify
1+
//@ test-mir-pass: InstSimplify-after-simplifycfg
22

3-
// EMIT_MIR bool_compare.eq_true.InstSimplify.diff
3+
// EMIT_MIR bool_compare.eq_true.InstSimplify-after-simplifycfg.diff
44
fn eq_true(x: bool) -> u32 {
55
// CHECK-LABEL: fn eq_true(
66
// CHECK-NOT: Eq(
77
if x == true { 0 } else { 1 }
88
}
99

10-
// EMIT_MIR bool_compare.true_eq.InstSimplify.diff
10+
// EMIT_MIR bool_compare.true_eq.InstSimplify-after-simplifycfg.diff
1111
fn true_eq(x: bool) -> u32 {
1212
// CHECK-LABEL: fn true_eq(
1313
// CHECK-NOT: Eq(
1414
if true == x { 0 } else { 1 }
1515
}
1616

17-
// EMIT_MIR bool_compare.ne_true.InstSimplify.diff
17+
// EMIT_MIR bool_compare.ne_true.InstSimplify-after-simplifycfg.diff
1818
fn ne_true(x: bool) -> u32 {
1919
// CHECK-LABEL: fn ne_true(
2020
// CHECK: Not(
2121
if x != true { 0 } else { 1 }
2222
}
2323

24-
// EMIT_MIR bool_compare.true_ne.InstSimplify.diff
24+
// EMIT_MIR bool_compare.true_ne.InstSimplify-after-simplifycfg.diff
2525
fn true_ne(x: bool) -> u32 {
2626
// CHECK-LABEL: fn true_ne(
2727
// CHECK: Not(
2828
if true != x { 0 } else { 1 }
2929
}
3030

31-
// EMIT_MIR bool_compare.eq_false.InstSimplify.diff
31+
// EMIT_MIR bool_compare.eq_false.InstSimplify-after-simplifycfg.diff
3232
fn eq_false(x: bool) -> u32 {
3333
// CHECK-LABEL: fn eq_false(
3434
// CHECK: Not(
3535
if x == false { 0 } else { 1 }
3636
}
3737

38-
// EMIT_MIR bool_compare.false_eq.InstSimplify.diff
38+
// EMIT_MIR bool_compare.false_eq.InstSimplify-after-simplifycfg.diff
3939
fn false_eq(x: bool) -> u32 {
4040
// CHECK-LABEL: fn false_eq(
4141
// CHECK: Not(
4242
if false == x { 0 } else { 1 }
4343
}
4444

45-
// EMIT_MIR bool_compare.ne_false.InstSimplify.diff
45+
// EMIT_MIR bool_compare.ne_false.InstSimplify-after-simplifycfg.diff
4646
fn ne_false(x: bool) -> u32 {
4747
// CHECK-LABEL: fn ne_false(
4848
// CHECK-NOT: Ne(
4949
if x != false { 0 } else { 1 }
5050
}
5151

52-
// EMIT_MIR bool_compare.false_ne.InstSimplify.diff
52+
// EMIT_MIR bool_compare.false_ne.InstSimplify-after-simplifycfg.diff
5353
fn false_ne(x: bool) -> u32 {
5454
// CHECK-LABEL: fn false_ne(
5555
// CHECK-NOT: Ne(

0 commit comments

Comments
 (0)