Skip to content

Commit 550fb81

Browse files
committed
ignore array.size() <= 16 bytes
1 parent 8d11d46 commit 550fb81

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+8
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
366366

367367
#[instrument(level = "trace", skip(self), ret)]
368368
fn eval_to_const(&mut self, value: VnIndex) -> Option<OpTy<'tcx>> {
369+
use abi::HasDataLayout;
369370
use Value::*;
371+
// LLVM optimizes the load of `sizeof(size_t) * 2` as a single `mov`,
372+
// which is cheap. Bigger values make more `mov` instructions generated.
373+
// After GVN, it becomes a single load (`lea`) of an address in `.rodata`.
374+
let stack_threshold = self.tcx.data_layout().pointer_size * 2;
370375
let vvalue = self.get(value);
371376
debug!(?vvalue);
372377
let op = match *vvalue {
@@ -411,6 +416,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
411416
let ptr_imm = Immediate::new_pointer_with_meta(data, meta, &self.ecx);
412417
ImmTy::from_immediate(ptr_imm, ty).into()
413418
} else if matches!(kind, AggregateTy::Array) {
419+
if ty.layout.size() <= stack_threshold {
420+
return None;
421+
}
414422
let mut mplace = None;
415423
let alloc_id = self
416424
.ecx

tests/mir-opt/const_array_locals.main.GVN.diff

+9-25
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,12 @@
8181
StorageDead(_12);
8282
StorageLive(_13);
8383
StorageLive(_14);
84-
- _14 = [const 1_i32, const 0_i32, const 0_i32];
85-
+ _14 = const [1_i32, 0_i32, 0_i32];
84+
_14 = [const 1_i32, const 0_i32, const 0_i32];
8685
StorageLive(_15);
87-
- _15 = [const 0_i32, const 1_i32, const 0_i32];
88-
+ _15 = const [0_i32, 1_i32, 0_i32];
86+
_15 = [const 0_i32, const 1_i32, const 0_i32];
8987
StorageLive(_16);
90-
- _16 = [const 0_i32, const 0_i32, const 1_i32];
91-
- _13 = [move _14, move _15, move _16];
92-
+ _16 = const [0_i32, 0_i32, 1_i32];
93-
+ _13 = [const [1_i32, 0_i32, 0_i32], const [0_i32, 1_i32, 0_i32], const [0_i32, 0_i32, 1_i32]];
88+
_16 = [const 0_i32, const 0_i32, const 1_i32];
89+
_13 = [move _14, move _15, move _16];
9490
StorageDead(_16);
9591
StorageDead(_15);
9692
StorageDead(_14);
@@ -105,43 +101,31 @@
105101
}
106102
+ }
107103
+
108-
+ ALLOC0 (size: 12, align: 4) {
109-
+ 00 00 00 00 00 00 00 00 01 00 00 00 │ ............
110-
+ }
111-
+
112-
+ ALLOC1 (size: 12, align: 4) {
113-
+ 00 00 00 00 01 00 00 00 00 00 00 00 │ ............
114-
+ }
115-
+
116-
+ ALLOC2 (size: 12, align: 4) {
117-
+ 01 00 00 00 00 00 00 00 00 00 00 00 │ ............
118-
+ }
119-
+
120-
+ ALLOC3 (size: 32, align: 4) {
104+
+ ALLOC0 (size: 32, align: 4) {
121105
+ 0x00 │ 00 00 80 3f 00 00 00 40 00 00 40 40 00 00 80 3f │ ...?...@..@@...?
122106
+ 0x10 │ 00 00 80 3f 00 00 80 3f 00 00 80 3f 00 00 28 42 │ ...?...?...?..(B
123107
+ }
124108
+
125-
+ ALLOC4 (size: 64, align: 2) {
109+
+ ALLOC1 (size: 64, align: 2) {
126110
+ 0x00 │ ff 00 69 00 0f 00 27 00 3e 00 fb 00 bf 00 b2 00 │ ..i...'.>.......
127111
+ 0x10 │ 09 00 04 00 38 00 dd 00 c1 00 a4 00 c2 00 c5 00 │ ....8...........
128112
+ 0x20 │ 06 00 f3 00 da 00 ab 00 57 00 f7 00 68 00 9f 00 │ ........W...h...
129113
+ 0x30 │ 16 00 9d 00 69 00 1f 00 60 00 ad 00 32 00 01 00 │ ....i...`...2...
130114
+ }
131115
+
132-
+ ALLOC5 (size: 48, align: 4) {
116+
+ ALLOC2 (size: 48, align: 4) {
133117
+ 0x00 │ c1 00 00 00 a4 00 00 00 c2 00 00 00 c5 00 00 00 │ ................
134118
+ 0x10 │ 06 00 00 00 f3 00 00 00 da 00 00 00 ab 00 00 00 │ ................
135119
+ 0x20 │ 57 00 00 00 f7 00 00 00 68 00 00 00 2a 00 00 00 │ W.......h...*...
136120
+ }
137121
+
138-
+ ALLOC6 (size: 48, align: 4) {
122+
+ ALLOC3 (size: 48, align: 4) {
139123
+ 0x00 │ ff 00 00 00 69 00 00 00 0f 00 00 00 27 00 00 00 │ ....i.......'...
140124
+ 0x10 │ 3e 00 00 00 fb 00 00 00 bf 00 00 00 b2 00 00 00 │ >...............
141125
+ 0x20 │ 09 00 00 00 04 00 00 00 38 00 00 00 dd 00 00 00 │ ........8.......
142126
+ }
143127
+
144-
+ ALLOC7 (size: 128, align: 4) {
128+
+ ALLOC4 (size: 128, align: 4) {
145129
+ 0x00 │ ff 00 00 00 69 00 00 00 0f 00 00 00 27 00 00 00 │ ....i.......'...
146130
+ 0x10 │ 3e 00 00 00 fb 00 00 00 bf 00 00 00 b2 00 00 00 │ >...............
147131
+ 0x20 │ 09 00 00 00 04 00 00 00 38 00 00 00 dd 00 00 00 │ ........8.......

tests/mir-opt/const_prop/array_index.main.GVN.64bit.panic-unwind.diff

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
bb0: {
1616
StorageLive(_1);
1717
StorageLive(_2);
18-
- _2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
19-
+ _2 = const [0_u32, 1_u32, 2_u32, 3_u32];
18+
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
2019
StorageLive(_3);
2120
_3 = const 2_usize;
2221
- _4 = Len(_2);
@@ -36,9 +35,5 @@
3635
StorageDead(_1);
3736
return;
3837
}
39-
+ }
40-
+
41-
+ ALLOC0 (size: 16, align: 4) {
42-
+ 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 │ ................
4338
}
4439

0 commit comments

Comments
 (0)