Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimizing Use expressions inside if condition #111644

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
)
})
}
ExprKind::Use { source } => this.then_else_break(
block,
&this.thir[source],
temp_scope_override,
break_scope,
variable_source_info,
),
ExprKind::Let { expr, ref pat } => this.lower_let_expr(
block,
&this.thir[expr],
Expand Down
3 changes: 2 additions & 1 deletion tests/mir-opt/bool_compare.opt1.InstSimplify.diff
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
_3 = _1; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:9
- _2 = Ne(move _3, const true); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
}

bb1: {
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
_0 = const 0_u32; // scope 0 at $DIR/bool_compare.rs:+1:20: +1:21
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:+1:5: +1:34
}

bb2: {
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
_0 = const 1_u32; // scope 0 at $DIR/bool_compare.rs:+1:31: +1:32
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:+1:5: +1:34
}
Expand Down
3 changes: 2 additions & 1 deletion tests/mir-opt/bool_compare.opt2.InstSimplify.diff
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
_3 = _1; // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
- _2 = Ne(const true, move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:17
}

bb1: {
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
_0 = const 0_u32; // scope 0 at $DIR/bool_compare.rs:+1:20: +1:21
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:+1:5: +1:34
}

bb2: {
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:16: +1:17
_0 = const 1_u32; // scope 0 at $DIR/bool_compare.rs:+1:31: +1:32
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:+1:5: +1:34
}
Expand Down
3 changes: 2 additions & 1 deletion tests/mir-opt/bool_compare.opt3.InstSimplify.diff
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
_3 = _1; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:9
- _2 = Eq(move _3, const false); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
}

bb1: {
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
_0 = const 0_u32; // scope 0 at $DIR/bool_compare.rs:+1:21: +1:22
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:+1:5: +1:35
}

bb2: {
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
_0 = const 1_u32; // scope 0 at $DIR/bool_compare.rs:+1:32: +1:33
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:+1:5: +1:35
}
Expand Down
3 changes: 2 additions & 1 deletion tests/mir-opt/bool_compare.opt4.InstSimplify.diff
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
_3 = _1; // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
- _2 = Eq(const false, move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:+1:8: +1:18
}

bb1: {
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
_0 = const 0_u32; // scope 0 at $DIR/bool_compare.rs:+1:21: +1:22
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:+1:5: +1:35
}

bb2: {
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:+1:17: +1:18
_0 = const 1_u32; // scope 0 at $DIR/bool_compare.rs:+1:32: +1:33
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:+1:5: +1:35
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// MIR for `function_from_issue_111583` after built

fn function_from_issue_111583(_1: f64) -> () {
debug z => _1; // in scope 0 at $DIR/logical_and_in_conditional.rs:+0:31: +0:32
let mut _0: (); // return place in scope 0 at $DIR/logical_and_in_conditional.rs:+0:39: +0:39
let _2: u64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+1:9: +1:13
let mut _3: u64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+1:16: +1:25
let mut _5: bool; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:8: +3:38
let mut _6: u64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:8: +3:33
let mut _7: u64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:8: +3:26
let mut _8: u64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:9: +3:20
let mut _9: f64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:9: +3:10
let mut _10: u64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:29: +3:33
let mut _11: bool; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:42: +3:60
let mut _12: f64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:42: +3:52
let mut _13: f64; // in scope 0 at $DIR/logical_and_in_conditional.rs:+3:42: +3:43
scope 1 {
debug mask => _2; // in scope 1 at $DIR/logical_and_in_conditional.rs:+1:9: +1:13
let mut _4: i32; // in scope 1 at $DIR/logical_and_in_conditional.rs:+2:9: +2:16
scope 2 {
debug ret => _4; // in scope 2 at $DIR/logical_and_in_conditional.rs:+2:9: +2:16
}
}

bb0: {
StorageLive(_2); // scope 0 at $DIR/logical_and_in_conditional.rs:+1:9: +1:13
StorageLive(_3); // scope 0 at $DIR/logical_and_in_conditional.rs:+1:16: +1:25
_3 = Shl(const 1_u64, const 38_i32); // scope 0 at $DIR/logical_and_in_conditional.rs:+1:16: +1:25
_2 = Sub(move _3, const 1_u64); // scope 0 at $DIR/logical_and_in_conditional.rs:+1:16: +1:29
StorageDead(_3); // scope 0 at $DIR/logical_and_in_conditional.rs:+1:28: +1:29
FakeRead(ForLet(None), _2); // scope 0 at $DIR/logical_and_in_conditional.rs:+1:9: +1:13
StorageLive(_4); // scope 1 at $DIR/logical_and_in_conditional.rs:+2:9: +2:16
_4 = const 0_i32; // scope 1 at $DIR/logical_and_in_conditional.rs:+2:19: +2:20
FakeRead(ForLet(None), _4); // scope 1 at $DIR/logical_and_in_conditional.rs:+2:9: +2:16
StorageLive(_5); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:8: +3:38
StorageLive(_6); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:8: +3:33
StorageLive(_7); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:8: +3:26
StorageLive(_8); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:9: +3:20
StorageLive(_9); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:9: +3:10
_9 = _1; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:9: +3:10
_8 = core::f64::<impl f64>::to_bits(move _9) -> [return: bb1, unwind: bb10]; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:9: +3:20
// mir::Constant
// + span: $DIR/logical_and_in_conditional.rs:28:11: 28:18
// + literal: Const { ty: fn(f64) -> u64 {core::f64::<impl f64>::to_bits}, val: Value(<ZST>) }
}

bb1: {
StorageDead(_9); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:19: +3:20
_7 = Shr(move _8, const 8_i32); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:8: +3:26
StorageDead(_8); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:25: +3:26
StorageLive(_10); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:29: +3:33
_10 = _2; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:29: +3:33
_6 = BitAnd(move _7, move _10); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:8: +3:33
StorageDead(_10); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:32: +3:33
StorageDead(_7); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:32: +3:33
_5 = Eq(move _6, const 0_u64); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:8: +3:38
switchInt(move _5) -> [0: bb3, otherwise: bb2]; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:8: +3:38
}

bb2: {
StorageDead(_6); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:37: +3:38
StorageLive(_11); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:42: +3:60
StorageLive(_12); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:42: +3:52
StorageLive(_13); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:42: +3:43
_13 = _1; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:42: +3:43
_12 = Rem(move _13, const 0.0625f64); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:42: +3:52
StorageDead(_13); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:51: +3:52
_11 = Lt(move _12, const 1.0E-13f64); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:42: +3:60
switchInt(move _11) -> [0: bb5, otherwise: bb4]; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:42: +3:60
}

bb3: {
goto -> bb7; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:8: +3:38
}

bb4: {
StorageDead(_12); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:59: +3:60
_4 = Add(_4, const 1_i32); // scope 2 at $DIR/logical_and_in_conditional.rs:+4:9: +4:17
_0 = const (); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:61: +5:6
goto -> bb9; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:5: +5:6
}

bb5: {
goto -> bb6; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:42: +3:60
}

bb6: {
StorageDead(_12); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:59: +3:60
goto -> bb8; // scope 2 at no-location
}

bb7: {
StorageDead(_6); // scope 2 at $DIR/logical_and_in_conditional.rs:+3:37: +3:38
goto -> bb8; // scope 2 at no-location
}

bb8: {
_0 = const (); // scope 2 at $DIR/logical_and_in_conditional.rs:+5:6: +5:6
goto -> bb9; // scope 2 at $DIR/logical_and_in_conditional.rs:+3:5: +5:6
}

bb9: {
StorageDead(_11); // scope 2 at $DIR/logical_and_in_conditional.rs:+5:5: +5:6
StorageDead(_5); // scope 2 at $DIR/logical_and_in_conditional.rs:+5:5: +5:6
StorageDead(_4); // scope 1 at $DIR/logical_and_in_conditional.rs:+6:1: +6:2
StorageDead(_2); // scope 0 at $DIR/logical_and_in_conditional.rs:+6:1: +6:2
return; // scope 0 at $DIR/logical_and_in_conditional.rs:+6:2: +6:2
}

bb10 (cleanup): {
resume; // scope 0 at $DIR/logical_and_in_conditional.rs:+0:1: +6:2
}
}
42 changes: 42 additions & 0 deletions tests/mir-opt/building/logical_and_in_conditional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
struct Droppy;

impl Drop for Droppy {
fn drop(&mut self) {
println!("drop");
}
}

// EMIT_MIR logical_and_in_conditional.test_and.built.after.mir
fn test_and(a: i32, b: i32, c: i32) {
if {
let _t = Droppy;
a == 0
} && {
let _t = Droppy;
b == 0
} && {
let _t = Droppy;
c == 0
} {}
}
azizghuloum marked this conversation as resolved.
Show resolved Hide resolved

// https://github.com/rust-lang/rust/issues/111583
// EMIT_MIR logical_and_in_conditional.function_from_issue_111583.built.after.mir
fn function_from_issue_111583(z: f64) {
let mask = (1 << 38) - 1;
let mut ret = 0;
if (z.to_bits() >> 8) & mask == 0 && z % 0.0625 < 1e-13 {
ret += 1;
}
}

fn main() {
function_from_issue_111583(0.0);
for a in 0..1 {
for b in 0..1 {
for c in 0..1 {
test_and(a, b, c);
}
}
}
}
Loading