Skip to content

Commit e5453b4

Browse files
lower ExprKind::Use, LogicalOp::Or and UnOp::Not
Co-authored-by: Abdulaziz Ghuloum <[email protected]>
1 parent b290d69 commit e5453b4

File tree

31 files changed

+459
-409
lines changed

31 files changed

+459
-409
lines changed

compiler/rustc_mir_build/src/build/matches/mod.rs

+44
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,43 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6464

6565
rhs_then_block.unit()
6666
}
67+
ExprKind::LogicalOp { op: LogicalOp::Or, lhs, rhs } => {
68+
let local_scope = this.local_scope();
69+
let (lhs_success_block, failure_block) =
70+
this.in_if_then_scope(local_scope, expr_span, |this| {
71+
this.then_else_break(
72+
block,
73+
&this.thir[lhs],
74+
temp_scope_override,
75+
local_scope,
76+
variable_source_info,
77+
)
78+
});
79+
let rhs_success_block = unpack!(this.then_else_break(
80+
failure_block,
81+
&this.thir[rhs],
82+
temp_scope_override,
83+
break_scope,
84+
variable_source_info,
85+
));
86+
this.cfg.goto(lhs_success_block, variable_source_info, rhs_success_block);
87+
rhs_success_block.unit()
88+
}
89+
ExprKind::Unary { op: UnOp::Not, arg } => {
90+
let local_scope = this.local_scope();
91+
let (success_block, failure_block) =
92+
this.in_if_then_scope(local_scope, expr_span, |this| {
93+
this.then_else_break(
94+
block,
95+
&this.thir[arg],
96+
temp_scope_override,
97+
local_scope,
98+
variable_source_info,
99+
)
100+
});
101+
this.break_for_else(success_block, break_scope, variable_source_info);
102+
failure_block.unit()
103+
}
67104
ExprKind::Scope { region_scope, lint_level, value } => {
68105
let region_scope = (region_scope, this.source_info(expr_span));
69106
this.in_scope(region_scope, lint_level, |this| {
@@ -76,6 +113,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
76113
)
77114
})
78115
}
116+
ExprKind::Use { source } => this.then_else_break(
117+
block,
118+
&this.thir[source],
119+
temp_scope_override,
120+
break_scope,
121+
variable_source_info,
122+
),
79123
ExprKind::Let { expr, ref pat } => this.lower_let_expr(
80124
block,
81125
&this.thir[expr],

tests/mir-opt/bool_compare.opt1.InstSimplify.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
_3 = _1;
1414
- _2 = Ne(move _3, const true);
1515
+ _2 = Not(move _3);
16-
StorageDead(_3);
1716
switchInt(move _2) -> [0: bb2, otherwise: bb1];
1817
}
1918

2019
bb1: {
20+
StorageDead(_3);
2121
_0 = const 0_u32;
2222
goto -> bb3;
2323
}
2424

2525
bb2: {
26+
StorageDead(_3);
2627
_0 = const 1_u32;
2728
goto -> bb3;
2829
}

tests/mir-opt/bool_compare.opt2.InstSimplify.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
_3 = _1;
1414
- _2 = Ne(const true, move _3);
1515
+ _2 = Not(move _3);
16-
StorageDead(_3);
1716
switchInt(move _2) -> [0: bb2, otherwise: bb1];
1817
}
1918

2019
bb1: {
20+
StorageDead(_3);
2121
_0 = const 0_u32;
2222
goto -> bb3;
2323
}
2424

2525
bb2: {
26+
StorageDead(_3);
2627
_0 = const 1_u32;
2728
goto -> bb3;
2829
}

tests/mir-opt/bool_compare.opt3.InstSimplify.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
_3 = _1;
1414
- _2 = Eq(move _3, const false);
1515
+ _2 = Not(move _3);
16-
StorageDead(_3);
1716
switchInt(move _2) -> [0: bb2, otherwise: bb1];
1817
}
1918

2019
bb1: {
20+
StorageDead(_3);
2121
_0 = const 0_u32;
2222
goto -> bb3;
2323
}
2424

2525
bb2: {
26+
StorageDead(_3);
2627
_0 = const 1_u32;
2728
goto -> bb3;
2829
}

tests/mir-opt/bool_compare.opt4.InstSimplify.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
_3 = _1;
1414
- _2 = Eq(const false, move _3);
1515
+ _2 = Not(move _3);
16-
StorageDead(_3);
1716
switchInt(move _2) -> [0: bb2, otherwise: bb1];
1817
}
1918

2019
bb1: {
20+
StorageDead(_3);
2121
_0 = const 0_u32;
2222
goto -> bb3;
2323
}
2424

2525
bb2: {
26+
StorageDead(_3);
2627
_0 = const 1_u32;
2728
goto -> bb3;
2829
}

tests/mir-opt/building/logical_or_in_conditional.test_complex.built.after.mir

+81-75
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ fn test_complex() -> () {
77
let mut _3: isize;
88
let mut _4: bool;
99
let mut _5: bool;
10-
let mut _6: bool;
11-
let mut _7: bool;
12-
let mut _8: u8;
13-
let mut _9: Droppy;
14-
let mut _10: bool;
15-
let mut _11: u8;
16-
let mut _12: Droppy;
17-
let mut _13: bool;
18-
let mut _14: bool;
19-
let mut _15: E;
20-
let mut _16: isize;
10+
let mut _6: u8;
11+
let mut _7: Droppy;
12+
let mut _8: bool;
13+
let mut _9: u8;
14+
let mut _10: Droppy;
15+
let mut _11: bool;
16+
let mut _12: E;
17+
let mut _13: isize;
2118

2219
bb0: {
2320
StorageLive(_1);
2421
StorageLive(_2);
25-
_2 = E::f() -> [return: bb1, unwind: bb27];
22+
_2 = E::f() -> [return: bb1, unwind: bb31];
2623
}
2724

2825
bb1: {
@@ -36,145 +33,154 @@ fn test_complex() -> () {
3633
}
3734

3835
bb3: {
39-
goto -> bb16;
36+
goto -> bb19;
4037
}
4138

4239
bb4: {
4340
StorageLive(_4);
44-
StorageLive(_5);
45-
StorageLive(_6);
46-
_6 = always_true() -> [return: bb11, unwind: bb27];
41+
_4 = always_true() -> [return: bb5, unwind: bb31];
4742
}
4843

4944
bb5: {
50-
_4 = const true;
51-
goto -> bb7;
45+
switchInt(move _4) -> [0: bb7, otherwise: bb6];
5246
}
5347

5448
bb6: {
55-
StorageLive(_10);
56-
StorageLive(_11);
57-
StorageLive(_12);
58-
_12 = Droppy(const 1_u8);
59-
_11 = (_12.0: u8);
60-
_10 = Gt(move _11, const 1_u8);
61-
drop(_12) -> [return: bb13, unwind: bb27];
49+
StorageLive(_5);
50+
StorageLive(_6);
51+
StorageLive(_7);
52+
_7 = Droppy(const 0_u8);
53+
_6 = (_7.0: u8);
54+
_5 = Gt(move _6, const 0_u8);
55+
switchInt(move _5) -> [0: bb9, otherwise: bb8];
6256
}
6357

6458
bb7: {
65-
StorageDead(_10);
66-
StorageDead(_5);
67-
switchInt(move _4) -> [0: bb15, otherwise: bb14];
59+
goto -> bb13;
6860
}
6961

7062
bb8: {
71-
_5 = const false;
72-
goto -> bb10;
63+
drop(_7) -> [return: bb10, unwind: bb31];
7364
}
7465

7566
bb9: {
76-
StorageLive(_7);
77-
StorageLive(_8);
78-
StorageLive(_9);
79-
_9 = Droppy(const 0_u8);
80-
_8 = (_9.0: u8);
81-
_7 = Gt(move _8, const 0_u8);
82-
drop(_9) -> [return: bb12, unwind: bb27];
67+
goto -> bb11;
8368
}
8469

8570
bb10: {
8671
StorageDead(_7);
8772
StorageDead(_6);
88-
switchInt(move _5) -> [0: bb6, otherwise: bb5];
73+
goto -> bb16;
8974
}
9075

9176
bb11: {
92-
switchInt(move _6) -> [0: bb8, otherwise: bb9];
77+
drop(_7) -> [return: bb12, unwind: bb31];
9378
}
9479

9580
bb12: {
96-
StorageDead(_9);
97-
StorageDead(_8);
98-
_5 = move _7;
99-
goto -> bb10;
81+
StorageDead(_7);
82+
StorageDead(_6);
83+
goto -> bb13;
10084
}
10185

10286
bb13: {
103-
StorageDead(_12);
104-
StorageDead(_11);
105-
_4 = move _10;
106-
goto -> bb7;
87+
StorageLive(_8);
88+
StorageLive(_9);
89+
StorageLive(_10);
90+
_10 = Droppy(const 1_u8);
91+
_9 = (_10.0: u8);
92+
_8 = Gt(move _9, const 1_u8);
93+
switchInt(move _8) -> [0: bb15, otherwise: bb14];
10794
}
10895

10996
bb14: {
110-
_1 = const ();
111-
goto -> bb17;
97+
drop(_10) -> [return: bb16, unwind: bb31];
11298
}
11399

114100
bb15: {
115-
goto -> bb16;
101+
goto -> bb17;
116102
}
117103

118104
bb16: {
105+
StorageDead(_10);
106+
StorageDead(_9);
119107
_1 = const ();
120-
goto -> bb17;
108+
goto -> bb20;
121109
}
122110

123111
bb17: {
124-
StorageDead(_4);
125-
StorageDead(_2);
126-
StorageDead(_1);
127-
StorageLive(_13);
128-
StorageLive(_14);
129-
_14 = always_true() -> [return: bb18, unwind: bb27];
112+
drop(_10) -> [return: bb18, unwind: bb31];
130113
}
131114

132115
bb18: {
133-
_13 = Not(move _14);
134-
StorageDead(_14);
135-
switchInt(move _13) -> [0: bb20, otherwise: bb19];
116+
StorageDead(_10);
117+
StorageDead(_9);
118+
goto -> bb19;
136119
}
137120

138121
bb19: {
139-
StorageLive(_15);
140-
_15 = E::f() -> [return: bb21, unwind: bb27];
122+
_1 = const ();
123+
goto -> bb20;
141124
}
142125

143126
bb20: {
144-
goto -> bb25;
127+
StorageDead(_8);
128+
StorageDead(_5);
129+
StorageDead(_4);
130+
StorageDead(_2);
131+
StorageDead(_1);
132+
StorageLive(_11);
133+
_11 = always_true() -> [return: bb21, unwind: bb31];
145134
}
146135

147136
bb21: {
148-
FakeRead(ForMatchedPlace(None), _15);
149-
_16 = discriminant(_15);
150-
switchInt(move _16) -> [1: bb23, otherwise: bb22];
137+
switchInt(move _11) -> [0: bb23, otherwise: bb22];
151138
}
152139

153140
bb22: {
154-
goto -> bb25;
141+
goto -> bb29;
155142
}
156143

157144
bb23: {
158-
falseEdge -> [real: bb24, imaginary: bb22];
145+
goto -> bb24;
159146
}
160147

161148
bb24: {
162-
_0 = const ();
163-
goto -> bb26;
149+
StorageLive(_12);
150+
_12 = E::f() -> [return: bb25, unwind: bb31];
164151
}
165152

166153
bb25: {
167-
_0 = const ();
168-
goto -> bb26;
154+
FakeRead(ForMatchedPlace(None), _12);
155+
_13 = discriminant(_12);
156+
switchInt(move _13) -> [1: bb27, otherwise: bb26];
169157
}
170158

171159
bb26: {
172-
StorageDead(_13);
173-
StorageDead(_15);
160+
goto -> bb29;
161+
}
162+
163+
bb27: {
164+
falseEdge -> [real: bb28, imaginary: bb26];
165+
}
166+
167+
bb28: {
168+
_0 = const ();
169+
goto -> bb30;
170+
}
171+
172+
bb29: {
173+
_0 = const ();
174+
goto -> bb30;
175+
}
176+
177+
bb30: {
178+
StorageDead(_11);
179+
StorageDead(_12);
174180
return;
175181
}
176182

177-
bb27 (cleanup): {
183+
bb31 (cleanup): {
178184
resume;
179185
}
180186
}

0 commit comments

Comments
 (0)