Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,11 @@ impl<'tcx> Const<'tcx> {
/// Return true if any evaluation of this constant always returns the same value,
/// taking into account even pointer identity tests.
pub fn is_deterministic(&self) -> bool {
if self.ty().is_primitive() {
// Primitive types hold no provenance, so their result is deterministic.
return true;
}

// Some constants may generate fresh allocations for pointers they contain,
// so using the same constant twice can yield two different results.
// Notably, valtrees purposefully generate new allocations.
Expand Down
401 changes: 249 additions & 152 deletions compiler/rustc_mir_transform/src/gvn.rs

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ declare_passes! {
Final
};
mod simplify_comparison_integral : SimplifyComparisonIntegral;
mod single_use_consts : SingleUseConsts;
mod sroa : ScalarReplacementOfAggregates;
mod strip_debuginfo : StripDebugInfo;
mod unreachable_enum_branching : UnreachableEnumBranching;
Expand Down Expand Up @@ -709,7 +708,6 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
&simplify::SimplifyLocals::AfterGVN,
&match_branches::MatchBranchSimplification,
&dataflow_const_prop::DataflowConstProp,
&single_use_consts::SingleUseConsts,
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
&jump_threading::JumpThreading,
&early_otherwise_branch::EarlyOtherwiseBranch,
Expand Down
205 changes: 0 additions & 205 deletions compiler/rustc_mir_transform/src/single_use_consts.rs

This file was deleted.

16 changes: 11 additions & 5 deletions tests/mir-opt/building/match/sort_candidates.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Check specific cases of sorting candidates in match lowering.
//! Check specific cases of sorting candidates in match lowering.
//@ compile-flags: -Zmir-opt-level=0

// EMIT_MIR sort_candidates.constant_eq.SimplifyCfg-initial.after.mir
fn constant_eq(s: &str, b: bool) -> u32 {
// Check that we only test "a" once

// CHECK-LABEL: fn constant_eq(
// CHECK-NOT: const "a"
// CHECK: {{_[0-9]+}} = const "a" as &[u8] (Transmute);
// CHECK: eq({{.*}}, const "a")
// CHECK-NOT: const "a"
match (s, b) {
("a", _) if true => 1,
Expand All @@ -23,11 +24,16 @@ fn disjoint_ranges(x: i32, b: bool) -> u32 {
// other candidates.

// CHECK-LABEL: fn disjoint_ranges(
// CHECK: debug b => _2;
// CHECK: bb0: {
// CHECK: switchInt(copy _2) -> [0: [[jump:bb.*]], otherwise: {{bb.*}}];
// CHECK: debug b => [[b:_.*]];
// CHECK: [[tmp:_.*]] = copy [[b]];
// CHECK-NEXT: switchInt(move [[tmp]]) -> [0: [[jump:bb.*]], otherwise: {{bb.*}}];
// CHECK: [[jump]]: {
// CHECK-NEXT: StorageDead([[tmp]]);
// CHECK-NEXT: goto -> [[jump3:bb.*]];
// CHECK: [[jump3]]: {
// CHECK-NEXT: _0 = const 3_u32;
// CHECK-NEXT: goto -> [[jumpret:bb.*]];
// CHECK: [[jumpret]]: {
// CHECK-NEXT: return;
match x {
0..10 if b => 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `main` before SingleUseConsts
+ // MIR for `main` after SingleUseConsts
- // MIR for `main` before GVN
+ // MIR for `main` after GVN

fn main() -> () {
let mut _0: ();
Expand Down Expand Up @@ -55,73 +55,78 @@
}

bb0: {
nop;
- _1 = const 1_u8;
nop;
- _2 = const 2_u8;
nop;
- _3 = const 3_u8;
- StorageLive(_1);
+ nop;
_1 = const 1_u8;
- StorageLive(_2);
+ nop;
_2 = const 2_u8;
- StorageLive(_3);
+ nop;
_3 = const 3_u8;
StorageLive(_4);
StorageLive(_5);
StorageLive(_6);
- _6 = const 1_u8;
+ nop;
- _6 = copy _1;
+ _6 = const 1_u8;
StorageLive(_7);
- _7 = const 2_u8;
- _5 = const 3_u8;
+ nop;
+ nop;
- _7 = copy _2;
- _5 = Add(move _6, move _7);
+ _7 = const 2_u8;
+ _5 = const 3_u8;
StorageDead(_7);
StorageDead(_6);
StorageLive(_8);
- _8 = const 3_u8;
- _4 = const 6_u8;
+ nop;
+ nop;
- _8 = copy _3;
- _4 = Add(move _5, move _8);
+ _8 = const 3_u8;
+ _4 = const 6_u8;
StorageDead(_8);
StorageDead(_5);
StorageLive(_9);
- _9 = const "hello, world!";
+ nop;
_9 = const "hello, world!";
StorageLive(_10);
_10 = (const true, const false, const 123_u32);
StorageLive(_11);
- _11 = const Option::<u16>::Some(99_u16);
+ nop;
- _11 = Option::<u16>::Some(const 99_u16);
+ _11 = const Option::<u16>::Some(99_u16);
StorageLive(_12);
- _12 = const Point {{ x: 32_u32, y: 32_u32 }};
+ nop;
- _12 = Point { x: const 32_u32, y: const 32_u32 };
+ _12 = const Point {{ x: 32_u32, y: 32_u32 }};
StorageLive(_13);
nop;
- _14 = const 32_u32;
- StorageLive(_14);
- _14 = copy (_12.0: u32);
+ nop;
+ _14 = const 32_u32;
StorageLive(_15);
- _15 = const 32_u32;
- _13 = const 64_u32;
+ nop;
+ nop;
- _15 = copy (_12.1: u32);
- _13 = Add(move _14, move _15);
+ _15 = const 32_u32;
+ _13 = const 64_u32;
StorageDead(_15);
nop;
- StorageDead(_14);
+ nop;
_0 = const ();
StorageDead(_13);
StorageDead(_12);
StorageDead(_11);
StorageDead(_10);
StorageDead(_9);
StorageDead(_4);
nop;
nop;
nop;
- StorageDead(_3);
- StorageDead(_2);
- StorageDead(_1);
+ nop;
+ nop;
+ nop;
return;
}
}

ALLOC0 (size: 8, align: 4) { .. }

ALLOC1 (size: 4, align: 2) { .. }

ALLOC2 (size: 13, align: 1) { .. }
- ALLOC0 (size: 13, align: 1) { .. }
+ ALLOC0 (size: 8, align: 4) { .. }
+
+ ALLOC1 (size: 4, align: 2) { .. }
+
+ ALLOC2 (size: 13, align: 1) { .. }

Loading
Loading