Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 26a6f6f

Browse files
committed
INSTR_COST=2.
1 parent e445d0d commit 26a6f6f

7 files changed

+560
-58
lines changed

compiler/rustc_mir_transform/src/cost_checker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_middle::mir::visit::*;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
44

5-
const INSTR_COST: usize = 3;
5+
const INSTR_COST: usize = 2;
66
const CALL_PENALTY: usize = 25;
77
const LANDINGPAD_PENALTY: usize = 50;
88
const RESUME_PENALTY: usize = 45;

tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.mir

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,183 @@ fn step_forward(_1: u32, _2: usize) -> u32 {
44
debug x => _1;
55
debug n => _2;
66
let mut _0: u32;
7+
scope 1 (inlined <u32 as Step>::forward) {
8+
debug start => _1;
9+
debug n => _2;
10+
let _12: std::option::Option<u32>;
11+
let mut _13: &std::option::Option<u32>;
12+
let mut _16: bool;
13+
let mut _17: u32;
14+
scope 2 {
15+
}
16+
scope 3 (inlined <u32 as Step>::forward_checked) {
17+
debug start => _1;
18+
debug n => _2;
19+
let mut _5: std::result::Result<u32, std::num::TryFromIntError>;
20+
let mut _6: isize;
21+
let _7: u32;
22+
scope 4 {
23+
debug n => _7;
24+
scope 6 (inlined core::num::<impl u32>::checked_add) {
25+
debug self => _1;
26+
debug rhs => _7;
27+
let mut _11: bool;
28+
scope 7 {
29+
debug a => _9;
30+
debug b => _10;
31+
}
32+
scope 8 (inlined core::num::<impl u32>::overflowing_add) {
33+
debug self => _1;
34+
debug rhs => _7;
35+
let mut _8: (u32, bool);
36+
let _9: u32;
37+
let _10: bool;
38+
scope 9 {
39+
debug a => _9;
40+
debug b => _10;
41+
}
42+
}
43+
}
44+
}
45+
scope 5 (inlined convert::num::ptr_try_from_impls::<impl TryFrom<usize> for u32>::try_from) {
46+
debug u => _2;
47+
let mut _3: bool;
48+
let mut _4: u32;
49+
}
50+
}
51+
scope 10 (inlined Option::<u32>::is_none) {
52+
debug self => _13;
53+
let mut _15: bool;
54+
scope 11 (inlined Option::<u32>::is_some) {
55+
debug self => _13;
56+
let mut _14: isize;
57+
}
58+
}
59+
scope 12 (inlined core::num::<impl u32>::wrapping_add) {
60+
debug self => _1;
61+
debug rhs => _17;
62+
}
63+
}
764

865
bb0: {
9-
_0 = <u32 as Step>::forward(move _1, move _2) -> [return: bb1, unwind continue];
66+
StorageLive(_16);
67+
StorageLive(_13);
68+
StorageLive(_12);
69+
StorageLive(_6);
70+
StorageLive(_7);
71+
StorageLive(_5);
72+
StorageLive(_3);
73+
_3 = Gt(_2, const 4294967295_usize);
74+
switchInt(move _3) -> [0: bb1, otherwise: bb2];
1075
}
1176

1277
bb1: {
78+
StorageLive(_4);
79+
_4 = _2 as u32 (IntToInt);
80+
_5 = Result::<u32, TryFromIntError>::Ok(move _4);
81+
StorageDead(_4);
82+
goto -> bb3;
83+
}
84+
85+
bb2: {
86+
_5 = const Result::<u32, TryFromIntError>::Err(TryFromIntError(()));
87+
goto -> bb3;
88+
}
89+
90+
bb3: {
91+
StorageDead(_3);
92+
_6 = discriminant(_5);
93+
switchInt(move _6) -> [0: bb4, 1: bb9, otherwise: bb14];
94+
}
95+
96+
bb4: {
97+
_7 = ((_5 as Ok).0: u32);
98+
StorageLive(_9);
99+
StorageLive(_10);
100+
StorageLive(_8);
101+
_8 = CheckedAdd(_1, _7);
102+
_9 = (_8.0: u32);
103+
_10 = (_8.1: bool);
104+
StorageDead(_8);
105+
StorageLive(_11);
106+
_11 = unlikely(move _10) -> [return: bb5, unwind unreachable];
107+
}
108+
109+
bb5: {
110+
switchInt(move _11) -> [0: bb6, otherwise: bb7];
111+
}
112+
113+
bb6: {
114+
_12 = Option::<u32>::Some(_9);
115+
goto -> bb8;
116+
}
117+
118+
bb7: {
119+
_12 = const Option::<u32>::None;
120+
goto -> bb8;
121+
}
122+
123+
bb8: {
124+
StorageDead(_11);
125+
StorageDead(_10);
126+
StorageDead(_9);
127+
goto -> bb10;
128+
}
129+
130+
bb9: {
131+
_12 = const Option::<u32>::None;
132+
goto -> bb10;
133+
}
134+
135+
bb10: {
136+
StorageDead(_5);
137+
StorageDead(_7);
138+
StorageDead(_6);
139+
_13 = &_12;
140+
StorageLive(_15);
141+
StorageLive(_14);
142+
_14 = discriminant(_12);
143+
_15 = Eq(_14, const 1_isize);
144+
StorageDead(_14);
145+
_16 = Not(move _15);
146+
StorageDead(_15);
147+
switchInt(move _16) -> [0: bb11, otherwise: bb12];
148+
}
149+
150+
bb11: {
151+
StorageDead(_12);
152+
StorageDead(_13);
153+
goto -> bb13;
154+
}
155+
156+
bb12: {
157+
StorageDead(_12);
158+
StorageDead(_13);
159+
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const _, const 1_u32) -> [success: bb13, unwind continue];
160+
}
161+
162+
bb13: {
163+
StorageDead(_16);
164+
StorageLive(_17);
165+
_17 = _2 as u32 (IntToInt);
166+
_0 = Add(_1, _17);
167+
StorageDead(_17);
13168
return;
14169
}
170+
171+
bb14: {
172+
unreachable;
173+
}
174+
}
175+
176+
ALLOC0 (size: 8, align: 4) {
177+
00 00 00 00 __ __ __ __ │ ....░░░░
178+
}
179+
180+
ALLOC1 (size: 8, align: 4) {
181+
01 00 00 00 __ __ __ __ │ ....░░░░
182+
}
183+
184+
ALLOC2 (size: 8, align: 4) {
185+
00 00 00 00 __ __ __ __ │ ....░░░░
15186
}

tests/mir-opt/pre-codegen/loops.mapped.PreCodegen.after.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () {
7676
_11 = move ((_7 as Some).0: T);
7777
StorageLive(_13);
7878
StorageLive(_12);
79-
_12 = (move _11,);
79+
_12 = (_11,);
8080
_13 = <&mut impl Fn(T) -> U as FnOnce<(T,)>>::call_once(move _8, move _12) -> [return: bb6, unwind: bb13];
8181
}
8282

tests/mir-opt/pre-codegen/mem_replace.mem_replace.PreCodegen.after.mir

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,32 @@ fn mem_replace(_1: &mut u32, _2: u32) -> u32 {
1313
scope 16 (inlined std::ptr::write::<u32>) {
1414
debug dst => _1;
1515
debug src => _2;
16+
let mut _5: *mut u32;
1617
scope 17 {
1718
scope 18 (inlined std::ptr::write::runtime::<u32>) {
18-
debug dst => _1;
19-
let mut _4: *const u32;
19+
debug dst => _5;
20+
let mut _6: *const u32;
2021
scope 19 (inlined intrinsics::is_aligned_and_not_null::<u32>) {
21-
debug ptr => _4;
22+
debug ptr => _6;
2223
scope 20 (inlined std::ptr::const_ptr::<impl *const u32>::is_null) {
23-
debug self => _4;
24-
let mut _5: *const u8;
24+
debug self => _6;
25+
let mut _7: *const u8;
2526
scope 21 {
2627
scope 22 (inlined std::ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
27-
debug ptr => _5;
28+
debug ptr => _7;
2829
scope 23 (inlined std::ptr::const_ptr::<impl *const u8>::addr) {
29-
debug self => _5;
30+
debug self => _7;
3031
scope 24 {
3132
scope 25 (inlined std::ptr::const_ptr::<impl *const u8>::cast::<()>) {
32-
debug self => _5;
33+
debug self => _7;
3334
}
3435
}
3536
}
3637
}
3738
}
3839
}
3940
scope 26 (inlined std::ptr::const_ptr::<impl *const u32>::is_aligned) {
40-
debug self => _4;
41+
debug self => _6;
4142
scope 27 (inlined align_of::<u32>) {
4243
}
4344
}
@@ -89,11 +90,13 @@ fn mem_replace(_1: &mut u32, _2: u32) -> u32 {
8990
_0 = (*_1);
9091
StorageDead(_4);
9192
StorageDead(_3);
92-
StorageLive(_4);
9393
StorageLive(_5);
94+
StorageLive(_6);
95+
StorageLive(_7);
9496
(*_1) = _2;
97+
StorageDead(_7);
98+
StorageDead(_6);
9599
StorageDead(_5);
96-
StorageDead(_4);
97100
return;
98101
}
99102
}

tests/mir-opt/pre-codegen/slice_index.slice_index_range.PreCodegen.after.panic-abort.mir

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,129 @@ fn slice_index_range(_1: &[u32], _2: std::ops::Range<usize>) -> &[u32] {
44
debug slice => _1;
55
debug index => _2;
66
let mut _0: &[u32];
7+
let mut _3: usize;
8+
let mut _4: usize;
79
scope 1 (inlined #[track_caller] core::slice::index::<impl Index<std::ops::Range<usize>> for [u32]>::index) {
810
debug self => _1;
9-
debug index => _2;
11+
debug ((index: std::ops::Range<usize>).0: usize) => _3;
12+
debug ((index: std::ops::Range<usize>).1: usize) => _4;
13+
scope 2 (inlined #[track_caller] <std::ops::Range<usize> as SliceIndex<[u32]>>::index) {
14+
debug ((self: std::ops::Range<usize>).0: usize) => _3;
15+
debug ((self: std::ops::Range<usize>).1: usize) => _4;
16+
debug slice => _1;
17+
let mut _5: bool;
18+
let mut _6: usize;
19+
let mut _7: bool;
20+
let mut _8: *const [u32];
21+
let _15: *const [u32];
22+
let _16: !;
23+
let _17: !;
24+
scope 3 {
25+
scope 4 (inlined <std::ops::Range<usize> as SliceIndex<[u32]>>::get_unchecked) {
26+
debug ((self: std::ops::Range<usize>).0: usize) => _3;
27+
debug ((self: std::ops::Range<usize>).1: usize) => _4;
28+
debug slice => _1;
29+
let mut _10: *const u32;
30+
let mut _11: *const u32;
31+
let mut _18: &[&str];
32+
scope 5 {
33+
let _9: usize;
34+
scope 6 {
35+
debug new_len => _9;
36+
scope 11 (inlined std::ptr::const_ptr::<impl *const [u32]>::as_ptr) {
37+
debug self => _1;
38+
}
39+
scope 12 (inlined std::ptr::const_ptr::<impl *const u32>::add) {
40+
debug self => _10;
41+
debug count => _3;
42+
scope 13 {
43+
}
44+
}
45+
scope 14 (inlined slice_from_raw_parts::<u32>) {
46+
debug data => _11;
47+
debug len => _9;
48+
let mut _12: *const ();
49+
scope 15 (inlined std::ptr::const_ptr::<impl *const u32>::cast::<()>) {
50+
debug self => _11;
51+
}
52+
scope 16 (inlined std::ptr::from_raw_parts::<[u32]>) {
53+
debug data_address => _12;
54+
debug metadata => _9;
55+
let mut _13: std::ptr::metadata::PtrComponents<[u32]>;
56+
let mut _14: std::ptr::metadata::PtrRepr<[u32]>;
57+
scope 17 {
58+
}
59+
}
60+
}
61+
}
62+
}
63+
scope 7 (inlined std::ptr::const_ptr::<impl *const [u32]>::len) {
64+
debug self => _1;
65+
scope 8 (inlined std::ptr::metadata::<[u32]>) {
66+
debug ptr => _1;
67+
scope 9 {
68+
}
69+
}
70+
}
71+
scope 10 (inlined Arguments::<'_>::new_const) {
72+
debug pieces => _18;
73+
}
74+
}
75+
}
76+
}
1077
}
1178

1279
bb0: {
13-
_0 = <std::ops::Range<usize> as SliceIndex<[u32]>>::index(move _2, move _1) -> [return: bb1, unwind unreachable];
80+
_3 = move (_2.0: usize);
81+
_4 = move (_2.1: usize);
82+
StorageLive(_5);
83+
_5 = Gt(_3, _4);
84+
switchInt(move _5) -> [0: bb1, otherwise: bb4];
1485
}
1586

1687
bb1: {
88+
StorageLive(_7);
89+
_6 = Len((*_1));
90+
_7 = Gt(_4, _6);
91+
switchInt(move _7) -> [0: bb2, otherwise: bb3];
92+
}
93+
94+
bb2: {
95+
StorageDead(_7);
96+
StorageDead(_5);
97+
StorageLive(_8);
98+
_8 = &raw const (*_1);
99+
StorageLive(_18);
100+
StorageLive(_9);
101+
_9 = SubUnchecked(_4, _3);
102+
StorageLive(_11);
103+
StorageLive(_10);
104+
_10 = _8 as *const u32 (PtrToPtr);
105+
_11 = Offset(_10, _3);
106+
StorageDead(_10);
107+
StorageLive(_12);
108+
_12 = _11 as *const () (PtrToPtr);
109+
StorageLive(_14);
110+
StorageLive(_13);
111+
_13 = std::ptr::metadata::PtrComponents::<[u32]> { data_address: _12, metadata: _9 };
112+
_14 = std::ptr::metadata::PtrRepr::<[u32]> { const_ptr: move _13 };
113+
StorageDead(_13);
114+
_15 = (_14.0: *const [u32]);
115+
StorageDead(_14);
116+
StorageDead(_12);
117+
StorageDead(_11);
118+
StorageDead(_9);
119+
StorageDead(_18);
120+
StorageDead(_8);
121+
_0 = &(*_15);
17122
return;
18123
}
124+
125+
bb3: {
126+
_16 = core::slice::index::slice_end_index_len_fail(move _4, move _6) -> unwind unreachable;
127+
}
128+
129+
bb4: {
130+
_17 = core::slice::index::slice_index_order_fail(move _3, move _4) -> unwind unreachable;
131+
}
19132
}

0 commit comments

Comments
 (0)