Skip to content

Commit 4658210

Browse files
Rollup merge of #108246 - saethlin:instcombine-redundant-casts, r=compiler-errors
Add an InstCombine for redundant casts `@rustbot` label +A-mir-opt
2 parents 437f210 + 0e05280 commit 4658210

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

compiler/rustc_mir_transform/src/instcombine.rs

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
3030
ctx.combine_bool_cmp(&statement.source_info, rvalue);
3131
ctx.combine_ref_deref(&statement.source_info, rvalue);
3232
ctx.combine_len(&statement.source_info, rvalue);
33+
ctx.combine_cast(&statement.source_info, rvalue);
3334
}
3435
_ => {}
3536
}
@@ -142,6 +143,14 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
142143
}
143144
}
144145

146+
fn combine_cast(&self, _source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
147+
if let Rvalue::Cast(_kind, operand, ty) = rvalue {
148+
if operand.ty(self.local_decls, self.tcx) == *ty {
149+
*rvalue = Rvalue::Use(operand.clone());
150+
}
151+
}
152+
}
153+
145154
fn combine_primitive_clone(
146155
&self,
147156
terminator: &mut Terminator<'tcx>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
- // MIR for `redundant` before InstCombine
2+
+ // MIR for `redundant` after InstCombine
3+
4+
fn redundant(_1: *const &u8) -> *const &u8 {
5+
debug x => _1; // in scope 0 at $DIR/casts.rs:+0:30: +0:31
6+
let mut _0: *const &u8; // return place in scope 0 at $DIR/casts.rs:+0:51: +0:64
7+
let mut _2: *const &u8; // in scope 0 at $DIR/casts.rs:+1:5: +1:55
8+
let mut _3: *const &u8; // in scope 0 at $DIR/casts.rs:+1:36: +1:37
9+
scope 1 (inlined generic_cast::<&u8, &u8>) { // at $DIR/casts.rs:6:5: 6:38
10+
debug x => _3; // in scope 1 at $DIR/casts.rs:10:23: 10:24
11+
}
12+
13+
bb0: {
14+
StorageLive(_2); // scope 0 at $DIR/casts.rs:+1:5: +1:55
15+
StorageLive(_3); // scope 0 at $DIR/casts.rs:+1:36: +1:37
16+
_3 = _1; // scope 0 at $DIR/casts.rs:+1:36: +1:37
17+
- _2 = _3 as *const &u8 (PtrToPtr); // scope 1 at $DIR/casts.rs:11:5: 11:18
18+
+ _2 = _3; // scope 1 at $DIR/casts.rs:11:5: 11:18
19+
StorageDead(_3); // scope 0 at $DIR/casts.rs:+1:37: +1:38
20+
_0 = _2; // scope 0 at $DIR/casts.rs:+1:5: +1:55
21+
StorageDead(_2); // scope 0 at $DIR/casts.rs:+2:1: +2:2
22+
return; // scope 0 at $DIR/casts.rs:+2:2: +2:2
23+
}
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// MIR for `redundant` after PreCodegen
2+
3+
fn redundant(_1: *const &u8) -> *const &u8 {
4+
debug x => _1; // in scope 0 at $DIR/casts.rs:+0:30: +0:31
5+
let mut _0: *const &u8; // return place in scope 0 at $DIR/casts.rs:+0:51: +0:64
6+
scope 1 (inlined generic_cast::<&u8, &u8>) { // at $DIR/casts.rs:6:5: 6:38
7+
debug x => _1; // in scope 1 at $DIR/casts.rs:10:23: 10:24
8+
}
9+
10+
bb0: {
11+
_0 = _1; // scope 0 at $DIR/casts.rs:+1:5: +1:55
12+
return; // scope 0 at $DIR/casts.rs:+2:2: +2:2
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// MIR for `roundtrip` after PreCodegen
2+
3+
fn roundtrip(_1: *const u8) -> *const u8 {
4+
debug x => _1; // in scope 0 at $DIR/casts.rs:+0:18: +0:19
5+
let mut _0: *const u8; // return place in scope 0 at $DIR/casts.rs:+0:35: +0:44
6+
let mut _2: *mut u8; // in scope 0 at $DIR/casts.rs:+1:5: +1:17
7+
8+
bb0: {
9+
StorageLive(_2); // scope 0 at $DIR/casts.rs:+1:5: +1:17
10+
_2 = _1 as *mut u8 (PtrToPtr); // scope 0 at $DIR/casts.rs:+1:5: +1:17
11+
_0 = move _2 as *const u8 (Pointer(MutToConstPointer)); // scope 0 at $DIR/casts.rs:+1:5: +1:17
12+
StorageDead(_2); // scope 0 at $DIR/casts.rs:+1:16: +1:17
13+
return; // scope 0 at $DIR/casts.rs:+2:2: +2:2
14+
}
15+
}

tests/mir-opt/casts.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![crate_type = "lib"]
2+
3+
// EMIT_MIR casts.redundant.InstCombine.diff
4+
// EMIT_MIR casts.redundant.PreCodegen.after.mir
5+
pub fn redundant<'a, 'b: 'a>(x: *const &'a u8) -> *const &'a u8 {
6+
generic_cast::<&'a u8, &'b u8>(x) as *const &'a u8
7+
}
8+
9+
#[inline]
10+
fn generic_cast<T, U>(x: *const T) -> *const U {
11+
x as *const U
12+
}
13+
14+
// EMIT_MIR casts.roundtrip.PreCodegen.after.mir
15+
pub fn roundtrip(x: *const u8) -> *const u8 {
16+
x as *mut u8 as *const u8
17+
}

0 commit comments

Comments
 (0)