Skip to content

Commit 2c31e4c

Browse files
committed
Do not check copiability.
1 parent c8327c0 commit 2c31e4c

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
8080
let mut any_replacement = false;
8181
let mut replacer = Replacer {
8282
tcx,
83-
param_env,
8483
ssa,
8584
dominators,
8685
state,
@@ -213,7 +212,13 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
213212
#[instrument(level = "trace", skip(self))]
214213
fn assign(&mut self, local: Local, value: VnIndex) {
215214
self.locals[local] = Some(value);
216-
self.rev_locals.entry(value).or_default().push(local);
215+
216+
// Only register the value if its type is `Sized`, as we will emit copies of it.
217+
let is_sized = !self.tcx.features().unsized_locals
218+
|| self.local_decls[local].ty.is_sized(self.tcx, self.param_env);
219+
if is_sized {
220+
self.rev_locals.entry(value).or_default().push(local);
221+
}
217222
}
218223

219224
/// Represent the *value* which would be read from `place`.
@@ -341,7 +346,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
341346

342347
struct Replacer<'a, 'tcx> {
343348
tcx: TyCtxt<'tcx>,
344-
param_env: ty::ParamEnv<'tcx>,
345349
ssa: SsaLocals,
346350
dominators: Dominators<BasicBlock>,
347351
state: VnState<'a, 'tcx>,
@@ -370,13 +374,6 @@ impl<'tcx> Replacer<'_, 'tcx> {
370374
.copied()
371375
.find(|&other| self.ssa.assignment_dominates(&self.dominators, other, loc))
372376
}
373-
374-
fn is_local_copiable(&self, local: Local) -> bool {
375-
let ty = self.state.local_decls[local].ty;
376-
// We only unify copy types as we only emit copies.
377-
// We already simplify mutable reborrows as assignments, so we also allow copying those.
378-
ty.is_ref() || ty.is_copy_modulo_regions(self.tcx, self.param_env)
379-
}
380377
}
381378

382379
impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
@@ -393,7 +390,6 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
393390
*self.any_replacement = true;
394391
} else if let Some(local) = self.try_as_local(value, location)
395392
&& *operand != Operand::Move(local.into())
396-
&& self.is_local_copiable(local)
397393
{
398394
*operand = Operand::Copy(local.into());
399395
self.reused_locals.insert(local);
@@ -412,7 +408,6 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
412408
*self.any_replacement = true;
413409
} else if let Some(local) = self.try_as_local(value, location)
414410
&& *rvalue != Rvalue::Use(Operand::Move(local.into()))
415-
&& self.is_local_copiable(local)
416411
{
417412
*rvalue = Rvalue::Use(Operand::Copy(local.into()));
418413
self.reused_locals.insert(local);

tests/mir-opt/gvn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ fn subexpression_elimination(x: u64, y: u64, mut z: u64) {
3434
opaque((x ^ y) + z);
3535
opaque((x << y) + z);
3636
opaque((x >> y) + z);
37-
opaque(S(x)); //< This is not substituted as `S` is not `Copy`.
38-
opaque(S(x).0); //< But this can be.
37+
opaque(S(x));
38+
opaque(S(x).0);
3939

4040
// We can substitute through an immutable reference too.
4141
let a = &z;

tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,18 @@
405405
StorageDead(_50);
406406
StorageDead(_49);
407407
StorageLive(_52);
408-
StorageLive(_53);
408+
- StorageLive(_53);
409409
- StorageLive(_54);
410410
- _54 = _1;
411411
- _53 = S::<u64>(move _54);
412412
- StorageDead(_54);
413+
- _52 = opaque::<S<u64>>(move _53) -> [return: bb15, unwind unreachable];
413414
+ _53 = S::<u64>(_1);
414-
_52 = opaque::<S<u64>>(move _53) -> [return: bb15, unwind unreachable];
415+
+ _52 = opaque::<S<u64>>(_53) -> [return: bb15, unwind unreachable];
415416
}
416417

417418
bb15: {
418-
StorageDead(_53);
419+
- StorageDead(_53);
419420
StorageDead(_52);
420421
StorageLive(_55);
421422
- StorageLive(_56);
@@ -424,7 +425,7 @@
424425
- _58 = _1;
425426
- _57 = S::<u64>(move _58);
426427
- StorageDead(_58);
427-
+ _57 = S::<u64>(_1);
428+
+ _57 = _53;
428429
_56 = (_57.0: u64);
429430
- _55 = opaque::<u64>(move _56) -> [return: bb16, unwind unreachable];
430431
+ _55 = opaque::<u64>(_56) -> [return: bb16, unwind unreachable];
@@ -667,17 +668,17 @@
667668
StorageDead(_116);
668669
StorageDead(_115);
669670
StorageLive(_121);
670-
StorageLive(_122);
671+
- StorageLive(_122);
671672
- StorageLive(_123);
672673
- _123 = _1;
673674
- _122 = S::<u64>(move _123);
674675
- StorageDead(_123);
675-
+ _122 = S::<u64>(_1);
676-
_121 = opaque::<S<u64>>(move _122) -> [return: bb29, unwind unreachable];
676+
- _121 = opaque::<S<u64>>(move _122) -> [return: bb29, unwind unreachable];
677+
+ _121 = opaque::<S<u64>>(_53) -> [return: bb29, unwind unreachable];
677678
}
678679

679680
bb29: {
680-
StorageDead(_122);
681+
- StorageDead(_122);
681682
StorageDead(_121);
682683
StorageLive(_124);
683684
- StorageLive(_125);

tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,18 @@
405405
StorageDead(_50);
406406
StorageDead(_49);
407407
StorageLive(_52);
408-
StorageLive(_53);
408+
- StorageLive(_53);
409409
- StorageLive(_54);
410410
- _54 = _1;
411411
- _53 = S::<u64>(move _54);
412412
- StorageDead(_54);
413+
- _52 = opaque::<S<u64>>(move _53) -> [return: bb15, unwind continue];
413414
+ _53 = S::<u64>(_1);
414-
_52 = opaque::<S<u64>>(move _53) -> [return: bb15, unwind continue];
415+
+ _52 = opaque::<S<u64>>(_53) -> [return: bb15, unwind continue];
415416
}
416417

417418
bb15: {
418-
StorageDead(_53);
419+
- StorageDead(_53);
419420
StorageDead(_52);
420421
StorageLive(_55);
421422
- StorageLive(_56);
@@ -424,7 +425,7 @@
424425
- _58 = _1;
425426
- _57 = S::<u64>(move _58);
426427
- StorageDead(_58);
427-
+ _57 = S::<u64>(_1);
428+
+ _57 = _53;
428429
_56 = (_57.0: u64);
429430
- _55 = opaque::<u64>(move _56) -> [return: bb16, unwind continue];
430431
+ _55 = opaque::<u64>(_56) -> [return: bb16, unwind continue];
@@ -667,17 +668,17 @@
667668
StorageDead(_116);
668669
StorageDead(_115);
669670
StorageLive(_121);
670-
StorageLive(_122);
671+
- StorageLive(_122);
671672
- StorageLive(_123);
672673
- _123 = _1;
673674
- _122 = S::<u64>(move _123);
674675
- StorageDead(_123);
675-
+ _122 = S::<u64>(_1);
676-
_121 = opaque::<S<u64>>(move _122) -> [return: bb29, unwind continue];
676+
- _121 = opaque::<S<u64>>(move _122) -> [return: bb29, unwind continue];
677+
+ _121 = opaque::<S<u64>>(_53) -> [return: bb29, unwind continue];
677678
}
678679

679680
bb29: {
680-
StorageDead(_122);
681+
- StorageDead(_122);
681682
StorageDead(_121);
682683
StorageLive(_124);
683684
- StorageLive(_125);

0 commit comments

Comments
 (0)