|
| 1 | +//@ test-mir-pass: InstSimplify-after-simplifycfg |
| 2 | +#![crate_type = "lib"] |
| 3 | + |
| 4 | +// This is the easy case, and the most plausible to run into in real code. |
| 5 | +// EMIT_MIR aggregate_array.literals.InstSimplify-after-simplifycfg.diff |
| 6 | +pub fn literals() -> [u8; 5] { |
| 7 | + // CHECK-LABEL: fn literals( |
| 8 | + // CHECK: _0 = [const 0_u8; 5]; |
| 9 | + [0, 0, 0, 0, 0] |
| 10 | +} |
| 11 | + |
| 12 | +// Check that hiding the const value behind a const item doesn't prevent the optimization |
| 13 | +// EMIT_MIR aggregate_array.const_items.InstSimplify-after-simplifycfg.diff |
| 14 | +pub fn const_items() -> [u8; 5] { |
| 15 | + const A: u8 = 0; |
| 16 | + const B: u8 = 0; |
| 17 | + const C: u8 = 0; |
| 18 | + const D: u8 = 0; |
| 19 | + const E: u8 = 0; |
| 20 | + |
| 21 | + // CHECK-LABEL: fn const_items( |
| 22 | + // CHECK: _0 = [const const_items::A; 5]; |
| 23 | + [A, B, C, D, E] |
| 24 | +} |
| 25 | + |
| 26 | +// EMIT_MIR aggregate_array.strs.InstSimplify-after-simplifycfg.diff |
| 27 | +pub fn strs() -> [&'static str; 5] { |
| 28 | + // CHECK-LABEL: fn strs( |
| 29 | + // CHECK: _0 = [const "a"; 5]; |
| 30 | + ["a", "a", "a", "a", "a"] |
| 31 | +} |
| 32 | + |
| 33 | +// InstSimplify isn't able to see through the move operands, but GVN can. |
| 34 | +// EMIT_MIR aggregate_array.local.InstSimplify-after-simplifycfg.diff |
| 35 | +pub fn local() -> [u8; 5] { |
| 36 | + // CHECK-LABEL: fn local( |
| 37 | + // CHECK: _0 = [move _2, move _3, move _4, move _5, move _6]; |
| 38 | + let val = 0; |
| 39 | + [val, val, val, val, val] |
| 40 | +} |
| 41 | + |
| 42 | +// All of these consts refer to the same value, but the addresses are all different. |
| 43 | +// It would be wrong to apply the optimization here. |
| 44 | +// EMIT_MIR aggregate_array.equal_referents.InstSimplify-after-simplifycfg.diff |
| 45 | +pub fn equal_referents() -> [&'static u8; 5] { |
| 46 | + const DATA: &[u8] = &[0, 0, 0, 0, 0]; |
| 47 | + const A: &u8 = &DATA[0]; |
| 48 | + const B: &u8 = &DATA[1]; |
| 49 | + const C: &u8 = &DATA[2]; |
| 50 | + const D: &u8 = &DATA[3]; |
| 51 | + const E: &u8 = &DATA[4]; |
| 52 | + |
| 53 | + // CHECK-LABEL: fn equal_referents( |
| 54 | + // CHECK: _0 = [const equal_referents::A, const equal_referents::B, const equal_referents::C, const equal_referents::D, const equal_referents::E]; |
| 55 | + [A, B, C, D, E] |
| 56 | +} |
0 commit comments