rust/tests/mir-opt/reference_prop.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

895 lines
26 KiB
Rust
Raw Normal View History

//@ compile-flags: -Zlint-mir=no
//@ test-mir-pass: ReferencePropagation
2023-05-09 14:35:21 -05:00
//@ needs-unwind
#![feature(core_intrinsics, custom_mir)]
#[inline(never)]
fn opaque(_: impl Sized) {}
fn reference_propagation<'a, T: Copy>(single: &'a T, mut multiple: &'a T) {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn reference_propagation(
// Propagation through a reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb0: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &[[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let b = &a; // This borrow is only used once.
let c = *b; // This should be optimized.
2023-05-07 07:19:46 -05:00
opaque(()); // We use opaque to separate cases into basic blocks in the MIR.
}
2023-10-16 12:38:37 -05:00
// Propagation through two references.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb1: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[a2:_.*]] = const 7_usize;
// CHECK: [[b:_.*]] = &[[a]];
// CHECK: [[btmp:_.*]] = &[[a2]];
// CHECK: [[b]] = move [[btmp]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let a2 = 7_usize;
let mut b = &a;
b = &a2;
// `b` is assigned twice, so we cannot propagate it.
let c = *b;
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation through a borrowed reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb2: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &[[a]];
// CHECK: [[d:_.*]] = &[[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let b = &a;
let d = &b;
2023-05-07 07:19:46 -05:00
let c = *b; // `b` is immutably borrowed, we know its value, but do not propagate it
2023-05-07 10:39:47 -05:00
opaque(d); // prevent `d` from being removed.
}
2023-10-16 12:38:37 -05:00
// Propagation through a mutably borrowed reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb3: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &[[a]];
// CHECK: [[d:_.*]] = &raw mut [[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let mut b = &a;
2023-05-07 10:39:47 -05:00
let d = &raw mut b;
let c = *b; // `b` is mutably borrowed, we cannot know its value.
2023-05-07 10:39:47 -05:00
opaque(d); // prevent `d` from being removed.
}
// Propagation through an escaping borrow.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb4: {
// CHECK: [[a:_.*]] = const 7_usize;
// CHECK: [[b:_.*]] = &[[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 7_usize;
let b = &a;
let c = *b;
2023-05-07 07:19:46 -05:00
opaque(b); // `b` escapes here, but we can still replace immutable borrow
}
// Propagation through a transitively escaping borrow.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb5: {
// CHECK: [[a:_.*]] = const 7_usize;
// CHECK: [[b1:_.*]] = &[[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
// CHECK: [[b2:_.*]] = copy [[b1]];
// CHECK: [[c2:_.*]] = copy [[a]];
// CHECK: [[b3:_.*]] = copy [[b2]];
2023-10-16 12:38:37 -05:00
let a = 7_usize;
let b1 = &a;
let c = *b1;
let b2 = b1;
let c2 = *b2;
let b3 = b2;
// `b3` escapes here, so we can only replace immutable borrow,
// for either `b`, `b2` or `b3`.
opaque(b3);
}
// Propagation a reborrow of an argument.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb6: {
// CHECK-NOT: {{_.*}} = &(*_1);
2024-08-18 17:51:53 -05:00
// CHECK: [[b:_.*]] = copy (*_1);
2023-10-16 12:38:37 -05:00
let a = &*single;
let b = *a; // This should be optimized as `*single`.
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation a reborrow of a mutated argument.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb7: {
// CHECK: [[a:_.*]] = &(*_2);
// CHECK: [[tmp:_.*]] = &(*_1);
// CHECK: _2 = move [[tmp]];
2024-08-18 17:51:53 -05:00
// CHECK: [[b:_.*]] = copy (*[[a]]);
2023-10-16 12:38:37 -05:00
let a = &*multiple;
multiple = &*single;
let b = *a; // This should not be optimized.
2023-05-07 07:19:46 -05:00
opaque(());
}
// Fixed-point propagation through a borrowed reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb8: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &[[a]];
// CHECK: [[d:_.*]] = &[[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let b = &a;
let d = &b; // first round promotes debuginfo for `d`
let c = *b; // second round propagates this dereference
opaque(());
}
2023-10-16 12:38:37 -05:00
// Fixed-point propagation through a mutably borrowed reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb9: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &[[a]];
// CHECK: [[d:_.*]] = &mut [[b]];
// FIXME this could be [[a]]
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let mut b = &a;
let d = &mut b; // first round promotes debuginfo for `d`
let c = *b; // second round propagates this dereference
opaque(());
}
}
fn reference_propagation_mut<'a, T: Copy>(single: &'a mut T, mut multiple: &'a mut T) {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn reference_propagation_mut(
// Propagation through a reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb0: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &mut [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let b = &mut a; // This borrow is only used once.
let c = *b; // This should be optimized.
2023-05-07 07:19:46 -05:00
opaque(());
}
2023-10-16 12:38:37 -05:00
// Propagation through two references.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb1: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[a2:_.*]] = const 7_usize;
// CHECK: [[b:_.*]] = &mut [[a]];
// CHECK: [[btmp:_.*]] = &mut [[a2]];
// CHECK: [[b]] = move [[btmp]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let mut a2 = 7_usize;
let mut b = &mut a;
b = &mut a2;
// `b` is assigned twice, so we cannot propagate it.
let c = *b;
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation through a borrowed reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb2: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &mut [[a]];
// CHECK: [[d:_.*]] = &[[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let b = &mut a;
let d = &b;
let c = *b; // `b` is immutably borrowed, we know its value, but cannot be removed.
2023-05-07 10:39:47 -05:00
opaque(d); // prevent `d` from being removed.
}
2023-10-16 12:38:37 -05:00
// Propagation through a mutably borrowed reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb3: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &mut [[a]];
// CHECK: [[d:_.*]] = &raw mut [[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let mut b = &mut a;
2023-05-07 10:39:47 -05:00
let d = &raw mut b;
let c = *b; // `b` is mutably borrowed, we cannot know its value.
2023-05-07 10:39:47 -05:00
opaque(d); // prevent `d` from being removed.
}
// Propagation through an escaping borrow.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb4: {
// CHECK: [[a:_.*]] = const 7_usize;
// CHECK: [[b:_.*]] = &mut [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 7_usize;
let b = &mut a;
let c = *b;
opaque(b); // `b` escapes here, so we can only replace immutable borrow
}
// Propagation through a transitively escaping borrow.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb5: {
// CHECK: [[a:_.*]] = const 7_usize;
// CHECK: [[b1:_.*]] = &mut [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b1]]);
2023-10-16 12:38:37 -05:00
// CHECK: [[b2:_.*]] = move [[b1]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c2:_.*]] = copy (*[[b2]]);
2023-10-16 12:38:37 -05:00
// CHECK: [[b3:_.*]] = move [[b2]];
let mut a = 7_usize;
let b1 = &mut a;
let c = *b1;
let b2 = b1;
let c2 = *b2;
let b3 = b2;
// `b3` escapes here, so we can only replace immutable borrow,
// for either `b`, `b2` or `b3`.
opaque(b3);
}
// Propagation a reborrow of an argument.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb6: {
// CHECK-NOT: {{_.*}} = &(*_1);
2024-08-18 17:51:53 -05:00
// CHECK: [[b:_.*]] = copy (*_1);
2023-10-16 12:38:37 -05:00
let a = &mut *single;
let b = *a; // This should be optimized as `*single`.
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation a reborrow of a mutated argument.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb7: {
// CHECK: [[a:_.*]] = &mut (*_2);
// CHECK: [[tmp:_.*]] = &mut (*_1);
// CHECK: _2 = move [[tmp]];
2024-08-18 17:51:53 -05:00
// CHECK: [[b:_.*]] = copy (*[[a]]);
2023-10-16 12:38:37 -05:00
let a = &mut *multiple;
multiple = &mut *single;
let b = *a; // This should not be optimized.
2023-05-07 07:19:46 -05:00
opaque(());
}
// Fixed-point propagation through a borrowed reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb8: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &mut [[a]];
// CHECK: [[d:_.*]] = &[[b]];
// FIXME this could be [[a]]
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let b = &mut a;
let d = &b; // first round promotes debuginfo for `d`
let c = *b; // second round propagates this dereference
opaque(());
}
2023-10-16 12:38:37 -05:00
// Fixed-point propagation through a mutably borrowed reference.
{
2023-10-16 12:38:37 -05:00
// CHECK: bb9: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &mut [[a]];
// CHECK: [[d:_.*]] = &mut [[b]];
// FIXME this could be [[a]]
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let mut b = &mut a;
let d = &mut b; // first round promotes debuginfo for `d`
let c = *b; // second round propagates this dereference
opaque(());
}
}
fn reference_propagation_const_ptr<T: Copy>(single: *const T, mut multiple: *const T) {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn reference_propagation_const_ptr(
// Propagation through a reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb0: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw const [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let b = &raw const a; // This borrow is only used once.
let c = *b; // This should be optimized.
2023-05-07 07:19:46 -05:00
opaque(());
}
2023-10-16 12:38:37 -05:00
// Propagation through two references.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb1: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[a2:_.*]] = const 7_usize;
// CHECK: [[b:_.*]] = &raw const [[a]];
// CHECK: [[btmp:_.*]] = &raw const [[a2]];
// CHECK: [[b]] = move [[btmp]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let a2 = 7_usize;
let mut b = &raw const a;
b = &raw const a2;
// `b` is assigned twice, so we cannot propagate it.
let c = *b;
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation through a borrowed reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb2: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw const [[a]];
// CHECK: [[d:_.*]] = &[[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let b = &raw const a;
let d = &b;
let c = *b; // `b` is immutably borrowed, we know its value, but cannot be removed.
2023-05-07 10:39:47 -05:00
opaque(d); // prevent `d` from being removed.
}
2023-10-16 12:38:37 -05:00
// Propagation through a mutably borrowed reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb3: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw const [[a]];
// CHECK: [[d:_.*]] = &raw mut [[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let mut b = &raw const a;
2023-05-07 10:39:47 -05:00
let d = &raw mut b;
let c = *b; // `b` is mutably borrowed, we cannot know its value.
2023-05-07 10:39:47 -05:00
opaque(d); // prevent `d` from being removed.
}
// Propagation through an escaping borrow.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb4: {
// CHECK: [[a:_.*]] = const 7_usize;
// CHECK: [[b:_.*]] = &raw const [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 7_usize;
let b = &raw const a;
let c = *b;
opaque(b); // `b` escapes here, so we can only replace immutable borrow
}
// Propagation through a transitively escaping borrow.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb5: {
// CHECK: [[a:_.*]] = const 7_usize;
// CHECK: [[b1:_.*]] = &raw const [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
// CHECK: [[b2:_.*]] = copy [[b1]];
// CHECK: [[c2:_.*]] = copy [[a]];
// CHECK: [[b3:_.*]] = copy [[b2]];
2023-10-16 12:38:37 -05:00
let a = 7_usize;
let b1 = &raw const a;
let c = *b1;
let b2 = b1;
let c2 = *b2;
let b3 = b2;
// `b3` escapes here, so we can only replace immutable borrow,
// for either `b`, `b2` or `b3`.
opaque(b3);
}
// Propagation a reborrow of an argument.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb6: {
// CHECK-NOT: {{_.*}} = &(*_1);
2024-08-18 17:51:53 -05:00
// CHECK: [[b:_.*]] = copy (*_1);
2023-10-16 12:38:37 -05:00
let a = &raw const *single;
let b = *a; // This should be optimized as `*single`.
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation a reborrow of a mutated argument.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb7: {
// CHECK: [[a:_.*]] = &raw const (*_2);
// CHECK: [[tmp:_.*]] = &raw const (*_1);
// CHECK: _2 = move [[tmp]];
2024-08-18 17:51:53 -05:00
// CHECK: [[b:_.*]] = copy (*[[a]]);
2023-10-16 12:38:37 -05:00
let a = &raw const *multiple;
multiple = &raw const *single;
let b = *a; // This should not be optimized.
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation through a reborrow.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb8: {
// CHECK: [[a:_.*]] = const 13_usize;
// CHECK: [[b:_.*]] = &raw const [[a]];
// CHECK: [[d:_.*]] = &raw const [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 13_usize;
let b = &raw const a;
let c = &raw const *b;
let e = *c;
2023-05-07 07:19:46 -05:00
opaque(());
}
// Fixed-point propagation through a borrowed reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb9: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw const [[a]];
// CHECK: [[d:_.*]] = &[[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let b = &raw const a;
let d = &b; // first round promotes debuginfo for `d`
let c = *b; // second round propagates this dereference
opaque(());
}
// Fixed-point propagation through a borrowed reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb10: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw const [[a]];
// CHECK: [[d:_.*]] = &mut [[b]];
// FIXME this could be [[a]]
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let a = 5_usize;
let mut b = &raw const a;
let d = &mut b; // first round promotes debuginfo for `d`
let c = *b; // second round propagates this dereference
opaque(());
}
}
fn reference_propagation_mut_ptr<T: Copy>(single: *mut T, mut multiple: *mut T) {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn reference_propagation_mut_ptr(
// Propagation through a reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb0: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw mut [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy [[a]];
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let b = &raw mut a; // This borrow is only used once.
let c = *b; // This should be optimized.
2023-05-07 07:19:46 -05:00
opaque(());
}
2023-10-16 12:38:37 -05:00
// Propagation through two references.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb1: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[a2:_.*]] = const 7_usize;
// CHECK: [[b:_.*]] = &raw mut [[a]];
// CHECK: [[btmp:_.*]] = &raw mut [[a2]];
// CHECK: [[b]] = move [[btmp]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let mut a2 = 7_usize;
let mut b = &raw mut a;
b = &raw mut a2;
// `b` is assigned twice, so we cannot propagate it.
let c = *b;
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation through a borrowed reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb2: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw mut [[a]];
// CHECK: [[d:_.*]] = &[[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let b = &raw mut a;
let d = &b;
let c = *b; // `b` is immutably borrowed, we know its value, but cannot be removed.
2023-05-07 10:39:47 -05:00
opaque(d); // prevent `d` from being removed.
}
2023-10-16 12:38:37 -05:00
// Propagation through a mutably borrowed reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb3: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw mut [[a]];
// CHECK: [[d:_.*]] = &raw mut [[b]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let mut b = &raw mut a;
2023-05-07 10:39:47 -05:00
let d = &raw mut b;
let c = *b; // `b` is mutably borrowed, we cannot know its value.
2023-05-07 10:39:47 -05:00
opaque(d); // prevent `d` from being removed.
}
// Propagation through an escaping borrow.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb4: {
// CHECK: [[a:_.*]] = const 7_usize;
// CHECK: [[b:_.*]] = &raw mut [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 7_usize;
let b = &raw mut a;
let c = *b;
opaque(b); // `b` escapes here, so we can only replace immutable borrow
}
// Propagation through a transitively escaping borrow.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb5: {
// CHECK: [[a:_.*]] = const 7_usize;
// CHECK: [[b1:_.*]] = &raw mut [[a]];
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b1]]);
// CHECK: [[b2:_.*]] = copy [[b1]];
// CHECK: [[c2:_.*]] = copy (*[[b2]]);
// CHECK: [[b3:_.*]] = copy [[b2]];
2023-10-16 12:38:37 -05:00
let mut a = 7_usize;
let b1 = &raw mut a;
let c = *b1;
let b2 = b1;
let c2 = *b2;
let b3 = b2;
// `b3` escapes here, so we can only replace immutable borrow,
// for either `b`, `b2` or `b3`.
opaque(b3);
}
// Propagation a reborrow of an argument.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb6: {
// CHECK-NOT: {{_.*}} = &(*_1);
2024-08-18 17:51:53 -05:00
// CHECK: [[b:_.*]] = copy (*_1);
2023-10-16 12:38:37 -05:00
let a = &raw mut *single;
let b = *a; // This should be optimized as `*single`.
2023-05-07 07:19:46 -05:00
opaque(());
}
// Propagation a reborrow of a mutated argument.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb7: {
// CHECK: [[a:_.*]] = &raw mut (*_2);
// CHECK: [[tmp:_.*]] = &raw mut (*_1);
// CHECK: _2 = move [[tmp]];
2024-08-18 17:51:53 -05:00
// CHECK: [[b:_.*]] = copy (*[[a]]);
2023-10-16 12:38:37 -05:00
let a = &raw mut *multiple;
multiple = &raw mut *single;
let b = *a; // This should not be optimized.
2023-05-07 07:19:46 -05:00
opaque(());
}
// Fixed-point propagation through a borrowed reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb8: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw mut [[a]];
// CHECK: [[d:_.*]] = &[[b]];
// FIXME this could be [[a]]
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let b = &raw mut a;
let d = &b; // first round promotes debuginfo for `d`
let c = *b; // second round propagates this dereference
opaque(());
}
2023-10-16 12:38:37 -05:00
// Fixed-point propagation through a mutably borrowed reference.
unsafe {
2023-10-16 12:38:37 -05:00
// CHECK: bb9: {
// CHECK: [[a:_.*]] = const 5_usize;
// CHECK: [[b:_.*]] = &raw mut [[a]];
// CHECK: [[d:_.*]] = &mut [[b]];
// FIXME this could be [[a]]
2024-08-18 17:51:53 -05:00
// CHECK: [[c:_.*]] = copy (*[[b]]);
2023-10-16 12:38:37 -05:00
let mut a = 5_usize;
let mut b = &raw mut a;
let d = &mut b; // first round promotes debuginfo for `d`
let c = *b; // second round propagates this dereference
opaque(());
}
}
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn read_through_raw(x: &mut usize) -> usize {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: read_through_raw
// CHECK: bb0: {
2024-08-18 17:51:53 -05:00
// CHECK-NEXT: _0 = copy (*_1);
// CHECK-NEXT: _0 = copy (*_1);
2023-10-16 12:38:37 -05:00
// CHECK-NEXT: return;
2023-10-16 12:38:37 -05:00
use std::intrinsics::mir::*;
Reformat `mir!` macro invocations to use braces. The `mir!` macro has multiple parts: - An optional return type annotation. - A sequence of zero or more local declarations. - A mandatory starting anonymous basic block, which is brace-delimited. - A sequence of zero of more additional named basic blocks. Some `mir!` invocations use braces with a "block" style, like so: ``` mir! { let _unit: (); { let non_copy = S(42); let ptr = std::ptr::addr_of_mut!(non_copy); // Inside `callee`, the first argument and `*ptr` are basically // aliasing places! Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue()) } after_call = { Return() } } ``` Some invocations use parens with a "block" style, like so: ``` mir!( let x: [i32; 2]; let one: i32; { x = [42, 43]; one = 1; x = [one, 2]; RET = Move(x); Return() } ) ``` And some invocations uses parens with a "tighter" style, like so: ``` mir!({ SetDiscriminant(*b, 0); Return() }) ``` This last style is generally used for cases where just the mandatory starting basic block is present. Its braces are placed next to the parens. This commit changes all `mir!` invocations to use braces with a "block" style. Why? - Consistency is good. - The contents of the invocation is a block of code, so it's odd to use parens. They are more normally used for function-like macros. - Most importantly, the next commit will enable rustfmt for `tests/mir-opt/`. rustfmt is more aggressive about formatting macros that use parens than macros that use braces. Without this commit's changes, rustfmt would break a couple of `mir!` macro invocations that use braces within `tests/mir-opt` by inserting an extraneous comma. E.g.: ``` mir!(type RET = (i32, bool);, { // extraneous comma after ';' RET.0 = 1; RET.1 = true; Return() }) ``` Switching those `mir!` invocations to use braces avoids that problem, resulting in this, which is nicer to read as well as being valid syntax: ``` mir! { type RET = (i32, bool); { RET.0 = 1; RET.1 = true; Return() } } ```
2024-06-02 19:19:57 -05:00
mir! {
let r1: &mut usize;
let r2: &mut usize;
let p1: *mut usize;
let p2: *mut usize;
{
r1 = &mut *x;
r2 = &mut *r1;
p1 = &raw mut *r1;
p2 = &raw mut *r2;
RET = *p1;
RET = *p2;
Return()
}
Reformat `mir!` macro invocations to use braces. The `mir!` macro has multiple parts: - An optional return type annotation. - A sequence of zero or more local declarations. - A mandatory starting anonymous basic block, which is brace-delimited. - A sequence of zero of more additional named basic blocks. Some `mir!` invocations use braces with a "block" style, like so: ``` mir! { let _unit: (); { let non_copy = S(42); let ptr = std::ptr::addr_of_mut!(non_copy); // Inside `callee`, the first argument and `*ptr` are basically // aliasing places! Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue()) } after_call = { Return() } } ``` Some invocations use parens with a "block" style, like so: ``` mir!( let x: [i32; 2]; let one: i32; { x = [42, 43]; one = 1; x = [one, 2]; RET = Move(x); Return() } ) ``` And some invocations uses parens with a "tighter" style, like so: ``` mir!({ SetDiscriminant(*b, 0); Return() }) ``` This last style is generally used for cases where just the mandatory starting basic block is present. Its braces are placed next to the parens. This commit changes all `mir!` invocations to use braces with a "block" style. Why? - Consistency is good. - The contents of the invocation is a block of code, so it's odd to use parens. They are more normally used for function-like macros. - Most importantly, the next commit will enable rustfmt for `tests/mir-opt/`. rustfmt is more aggressive about formatting macros that use parens than macros that use braces. Without this commit's changes, rustfmt would break a couple of `mir!` macro invocations that use braces within `tests/mir-opt` by inserting an extraneous comma. E.g.: ``` mir!(type RET = (i32, bool);, { // extraneous comma after ';' RET.0 = 1; RET.1 = true; Return() }) ``` Switching those `mir!` invocations to use braces avoids that problem, resulting in this, which is nicer to read as well as being valid syntax: ``` mir! { type RET = (i32, bool); { RET.0 = 1; RET.1 = true; Return() } } ```
2024-06-02 19:19:57 -05:00
}
}
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn multiple_storage() {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: multiple_storage
2024-08-18 17:51:53 -05:00
// CHECK: _3 = copy (*_2);
2023-10-16 12:38:37 -05:00
use std::intrinsics::mir::*;
Reformat `mir!` macro invocations to use braces. The `mir!` macro has multiple parts: - An optional return type annotation. - A sequence of zero or more local declarations. - A mandatory starting anonymous basic block, which is brace-delimited. - A sequence of zero of more additional named basic blocks. Some `mir!` invocations use braces with a "block" style, like so: ``` mir! { let _unit: (); { let non_copy = S(42); let ptr = std::ptr::addr_of_mut!(non_copy); // Inside `callee`, the first argument and `*ptr` are basically // aliasing places! Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue()) } after_call = { Return() } } ``` Some invocations use parens with a "block" style, like so: ``` mir!( let x: [i32; 2]; let one: i32; { x = [42, 43]; one = 1; x = [one, 2]; RET = Move(x); Return() } ) ``` And some invocations uses parens with a "tighter" style, like so: ``` mir!({ SetDiscriminant(*b, 0); Return() }) ``` This last style is generally used for cases where just the mandatory starting basic block is present. Its braces are placed next to the parens. This commit changes all `mir!` invocations to use braces with a "block" style. Why? - Consistency is good. - The contents of the invocation is a block of code, so it's odd to use parens. They are more normally used for function-like macros. - Most importantly, the next commit will enable rustfmt for `tests/mir-opt/`. rustfmt is more aggressive about formatting macros that use parens than macros that use braces. Without this commit's changes, rustfmt would break a couple of `mir!` macro invocations that use braces within `tests/mir-opt` by inserting an extraneous comma. E.g.: ``` mir!(type RET = (i32, bool);, { // extraneous comma after ';' RET.0 = 1; RET.1 = true; Return() }) ``` Switching those `mir!` invocations to use braces avoids that problem, resulting in this, which is nicer to read as well as being valid syntax: ``` mir! { type RET = (i32, bool); { RET.0 = 1; RET.1 = true; Return() } } ```
2024-06-02 19:19:57 -05:00
mir! {
let x: i32;
{
StorageLive(x);
x = 5;
let z = &x;
StorageDead(x);
StorageLive(x);
// As there are multiple `StorageLive` statements for `x`, we cannot know if this `z`'s
// pointer address is the address of `x`, so do nothing.
let y = *z;
Call(RET = opaque(y), ReturnTo(retblock), UnwindContinue())
}
retblock = {
Return()
}
Reformat `mir!` macro invocations to use braces. The `mir!` macro has multiple parts: - An optional return type annotation. - A sequence of zero or more local declarations. - A mandatory starting anonymous basic block, which is brace-delimited. - A sequence of zero of more additional named basic blocks. Some `mir!` invocations use braces with a "block" style, like so: ``` mir! { let _unit: (); { let non_copy = S(42); let ptr = std::ptr::addr_of_mut!(non_copy); // Inside `callee`, the first argument and `*ptr` are basically // aliasing places! Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue()) } after_call = { Return() } } ``` Some invocations use parens with a "block" style, like so: ``` mir!( let x: [i32; 2]; let one: i32; { x = [42, 43]; one = 1; x = [one, 2]; RET = Move(x); Return() } ) ``` And some invocations uses parens with a "tighter" style, like so: ``` mir!({ SetDiscriminant(*b, 0); Return() }) ``` This last style is generally used for cases where just the mandatory starting basic block is present. Its braces are placed next to the parens. This commit changes all `mir!` invocations to use braces with a "block" style. Why? - Consistency is good. - The contents of the invocation is a block of code, so it's odd to use parens. They are more normally used for function-like macros. - Most importantly, the next commit will enable rustfmt for `tests/mir-opt/`. rustfmt is more aggressive about formatting macros that use parens than macros that use braces. Without this commit's changes, rustfmt would break a couple of `mir!` macro invocations that use braces within `tests/mir-opt` by inserting an extraneous comma. E.g.: ``` mir!(type RET = (i32, bool);, { // extraneous comma after ';' RET.0 = 1; RET.1 = true; Return() }) ``` Switching those `mir!` invocations to use braces avoids that problem, resulting in this, which is nicer to read as well as being valid syntax: ``` mir! { type RET = (i32, bool); { RET.0 = 1; RET.1 = true; Return() } } ```
2024-06-02 19:19:57 -05:00
}
}
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn dominate_storage() {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: dominate_storage
2024-08-18 17:51:53 -05:00
// CHECK: _5 = copy (*_2);
2023-10-16 12:38:37 -05:00
use std::intrinsics::mir::*;
Reformat `mir!` macro invocations to use braces. The `mir!` macro has multiple parts: - An optional return type annotation. - A sequence of zero or more local declarations. - A mandatory starting anonymous basic block, which is brace-delimited. - A sequence of zero of more additional named basic blocks. Some `mir!` invocations use braces with a "block" style, like so: ``` mir! { let _unit: (); { let non_copy = S(42); let ptr = std::ptr::addr_of_mut!(non_copy); // Inside `callee`, the first argument and `*ptr` are basically // aliasing places! Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue()) } after_call = { Return() } } ``` Some invocations use parens with a "block" style, like so: ``` mir!( let x: [i32; 2]; let one: i32; { x = [42, 43]; one = 1; x = [one, 2]; RET = Move(x); Return() } ) ``` And some invocations uses parens with a "tighter" style, like so: ``` mir!({ SetDiscriminant(*b, 0); Return() }) ``` This last style is generally used for cases where just the mandatory starting basic block is present. Its braces are placed next to the parens. This commit changes all `mir!` invocations to use braces with a "block" style. Why? - Consistency is good. - The contents of the invocation is a block of code, so it's odd to use parens. They are more normally used for function-like macros. - Most importantly, the next commit will enable rustfmt for `tests/mir-opt/`. rustfmt is more aggressive about formatting macros that use parens than macros that use braces. Without this commit's changes, rustfmt would break a couple of `mir!` macro invocations that use braces within `tests/mir-opt` by inserting an extraneous comma. E.g.: ``` mir!(type RET = (i32, bool);, { // extraneous comma after ';' RET.0 = 1; RET.1 = true; Return() }) ``` Switching those `mir!` invocations to use braces avoids that problem, resulting in this, which is nicer to read as well as being valid syntax: ``` mir! { type RET = (i32, bool); { RET.0 = 1; RET.1 = true; Return() } } ```
2024-06-02 19:19:57 -05:00
mir! {
let x: i32;
let r: &i32;
let c: i32;
let d: bool;
{ Goto(bb0) }
bb0 = {
x = 5;
r = &x;
Goto(bb1)
}
bb1 = {
let c = *r;
Call(RET = opaque(c), ReturnTo(bb2), UnwindContinue())
}
bb2 = {
StorageDead(x);
StorageLive(x);
let d = true;
match d { false => bb2, _ => bb0 }
}
Reformat `mir!` macro invocations to use braces. The `mir!` macro has multiple parts: - An optional return type annotation. - A sequence of zero or more local declarations. - A mandatory starting anonymous basic block, which is brace-delimited. - A sequence of zero of more additional named basic blocks. Some `mir!` invocations use braces with a "block" style, like so: ``` mir! { let _unit: (); { let non_copy = S(42); let ptr = std::ptr::addr_of_mut!(non_copy); // Inside `callee`, the first argument and `*ptr` are basically // aliasing places! Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue()) } after_call = { Return() } } ``` Some invocations use parens with a "block" style, like so: ``` mir!( let x: [i32; 2]; let one: i32; { x = [42, 43]; one = 1; x = [one, 2]; RET = Move(x); Return() } ) ``` And some invocations uses parens with a "tighter" style, like so: ``` mir!({ SetDiscriminant(*b, 0); Return() }) ``` This last style is generally used for cases where just the mandatory starting basic block is present. Its braces are placed next to the parens. This commit changes all `mir!` invocations to use braces with a "block" style. Why? - Consistency is good. - The contents of the invocation is a block of code, so it's odd to use parens. They are more normally used for function-like macros. - Most importantly, the next commit will enable rustfmt for `tests/mir-opt/`. rustfmt is more aggressive about formatting macros that use parens than macros that use braces. Without this commit's changes, rustfmt would break a couple of `mir!` macro invocations that use braces within `tests/mir-opt` by inserting an extraneous comma. E.g.: ``` mir!(type RET = (i32, bool);, { // extraneous comma after ';' RET.0 = 1; RET.1 = true; Return() }) ``` Switching those `mir!` invocations to use braces avoids that problem, resulting in this, which is nicer to read as well as being valid syntax: ``` mir! { type RET = (i32, bool); { RET.0 = 1; RET.1 = true; Return() } } ```
2024-06-02 19:19:57 -05:00
}
}
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn maybe_dead(m: bool) {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn maybe_dead(
// CHECK: (*_5) = const 7_i32;
2023-10-16 12:38:37 -05:00
use std::intrinsics::mir::*;
Reformat `mir!` macro invocations to use braces. The `mir!` macro has multiple parts: - An optional return type annotation. - A sequence of zero or more local declarations. - A mandatory starting anonymous basic block, which is brace-delimited. - A sequence of zero of more additional named basic blocks. Some `mir!` invocations use braces with a "block" style, like so: ``` mir! { let _unit: (); { let non_copy = S(42); let ptr = std::ptr::addr_of_mut!(non_copy); // Inside `callee`, the first argument and `*ptr` are basically // aliasing places! Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue()) } after_call = { Return() } } ``` Some invocations use parens with a "block" style, like so: ``` mir!( let x: [i32; 2]; let one: i32; { x = [42, 43]; one = 1; x = [one, 2]; RET = Move(x); Return() } ) ``` And some invocations uses parens with a "tighter" style, like so: ``` mir!({ SetDiscriminant(*b, 0); Return() }) ``` This last style is generally used for cases where just the mandatory starting basic block is present. Its braces are placed next to the parens. This commit changes all `mir!` invocations to use braces with a "block" style. Why? - Consistency is good. - The contents of the invocation is a block of code, so it's odd to use parens. They are more normally used for function-like macros. - Most importantly, the next commit will enable rustfmt for `tests/mir-opt/`. rustfmt is more aggressive about formatting macros that use parens than macros that use braces. Without this commit's changes, rustfmt would break a couple of `mir!` macro invocations that use braces within `tests/mir-opt` by inserting an extraneous comma. E.g.: ``` mir!(type RET = (i32, bool);, { // extraneous comma after ';' RET.0 = 1; RET.1 = true; Return() }) ``` Switching those `mir!` invocations to use braces avoids that problem, resulting in this, which is nicer to read as well as being valid syntax: ``` mir! { type RET = (i32, bool); { RET.0 = 1; RET.1 = true; Return() } } ```
2024-06-02 19:19:57 -05:00
mir! {
let x: i32;
let y: i32;
{
StorageLive(x);
StorageLive(y);
x = 5;
y = 5;
let a = &x;
let b = &mut y;
// As we don't replace `b` in `bb2`, we cannot replace it here either.
*b = 7;
2023-05-07 07:19:46 -05:00
// But this can still be replaced.
let u = *a;
match m { true => bb1, _ => bb2 }
}
bb1 = {
StorageDead(x);
StorageDead(y);
Call(RET = opaque(u), ReturnTo(bb2), UnwindContinue())
}
bb2 = {
// As `x` may be `StorageDead`, `a` may be dangling, so we do nothing.
let z = *a;
Call(RET = opaque(z), ReturnTo(bb3), UnwindContinue())
}
bb3 = {
// As `y` may be `StorageDead`, `b` may be dangling, so we do nothing.
// This implies that we also do not substitute `b` in `bb0`.
let t = *b;
Call(RET = opaque(t), ReturnTo(retblock), UnwindContinue())
}
retblock = {
Return()
}
Reformat `mir!` macro invocations to use braces. The `mir!` macro has multiple parts: - An optional return type annotation. - A sequence of zero or more local declarations. - A mandatory starting anonymous basic block, which is brace-delimited. - A sequence of zero of more additional named basic blocks. Some `mir!` invocations use braces with a "block" style, like so: ``` mir! { let _unit: (); { let non_copy = S(42); let ptr = std::ptr::addr_of_mut!(non_copy); // Inside `callee`, the first argument and `*ptr` are basically // aliasing places! Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue()) } after_call = { Return() } } ``` Some invocations use parens with a "block" style, like so: ``` mir!( let x: [i32; 2]; let one: i32; { x = [42, 43]; one = 1; x = [one, 2]; RET = Move(x); Return() } ) ``` And some invocations uses parens with a "tighter" style, like so: ``` mir!({ SetDiscriminant(*b, 0); Return() }) ``` This last style is generally used for cases where just the mandatory starting basic block is present. Its braces are placed next to the parens. This commit changes all `mir!` invocations to use braces with a "block" style. Why? - Consistency is good. - The contents of the invocation is a block of code, so it's odd to use parens. They are more normally used for function-like macros. - Most importantly, the next commit will enable rustfmt for `tests/mir-opt/`. rustfmt is more aggressive about formatting macros that use parens than macros that use braces. Without this commit's changes, rustfmt would break a couple of `mir!` macro invocations that use braces within `tests/mir-opt` by inserting an extraneous comma. E.g.: ``` mir!(type RET = (i32, bool);, { // extraneous comma after ';' RET.0 = 1; RET.1 = true; Return() }) ``` Switching those `mir!` invocations to use braces avoids that problem, resulting in this, which is nicer to read as well as being valid syntax: ``` mir! { type RET = (i32, bool); { RET.0 = 1; RET.1 = true; Return() } } ```
2024-06-02 19:19:57 -05:00
}
}
2023-05-10 11:06:41 -05:00
fn mut_raw_then_mut_shr() -> (i32, i32) {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn mut_raw_then_mut_shr(
// CHECK-NOT: (*{{_.*}})
2023-05-10 11:06:41 -05:00
let mut x = 2;
let xref = &mut x;
let xraw = &mut *xref as *mut _;
let xshr = &*xref;
// Verify that we completely replace with `x` in both cases.
let a = *xshr;
unsafe {
*xraw = 4;
}
2023-05-10 11:06:41 -05:00
(a, x)
}
fn unique_with_copies() {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn unique_with_copies(
// CHECK: [[a:_.*]] = const 0_i32;
// CHECK: [[x:_.*]] = &raw mut [[a]];
// CHECK-NOT: [[a]]
2024-08-18 17:51:53 -05:00
// CHECK: [[tmp:_.*]] = copy (*[[x]]);
2023-10-16 12:38:37 -05:00
// CHECK-NEXT: opaque::<i32>(move [[tmp]])
// CHECK-NOT: [[a]]
// CHECK: StorageDead([[a]]);
// CHECK-NOT: [[a]]
2024-08-18 17:51:53 -05:00
// CHECK: [[tmp:_.*]] = copy (*[[x]]);
2023-10-16 12:38:37 -05:00
// CHECK-NEXT: opaque::<i32>(move [[tmp]])
2023-05-10 11:06:41 -05:00
let y = {
let mut a = 0;
let x = &raw mut a;
// `*y` is not replacable below, so we must not replace `*x`.
unsafe { opaque(*x) };
x
};
// But rewriting as `*x` is ok.
unsafe { opaque(*y) };
}
2023-05-10 13:27:54 -05:00
fn debuginfo() {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn debuginfo(
// FIXME: This features waits for DWARF implicit pointers in LLVM.
// CHECK: debug ref_mut_u8 => _{{.*}};
// CHECK: debug field => _{{.*}};
// CHECK: debug reborrow => _{{.*}};
// CHECK: debug variant_field => _{{.*}};
// CHECK: debug constant_index => _{{.*}};
// CHECK: debug subslice => _{{.*}};
// CHECK: debug constant_index_from_end => _{{.*}};
// CHECK: debug multiple_borrow => _{{.*}};
2023-05-10 13:27:54 -05:00
struct T(u8);
let ref_mut_u8 = &mut 5_u8;
let field = &T(0).0;
// Verify that we don't emit `&*` in debuginfo.
let reborrow = &*ref_mut_u8;
match Some(0) {
None => {}
Some(ref variant_field) => {}
}
// `constant_index_from_end` and `subslice` should not be promoted, as their value depends
// on the slice length.
if let [_, ref constant_index, subslice @ .., ref constant_index_from_end] = &[6; 10][..] {}
2023-05-13 05:31:55 -05:00
let multiple_borrow = &&&mut T(6).0;
2023-05-10 13:27:54 -05:00
}
2023-05-13 05:29:05 -05:00
fn many_debuginfo() {
2023-10-16 12:38:37 -05:00
// CHECK-LABEL: fn many_debuginfo(
// FIXME: This features waits for DWARF implicit pointers in LLVM.
// CHECK: debug many_borrow => _{{.*}};
2023-05-13 05:29:05 -05:00
let a = 0;
// Verify that we do not ICE on deeply nested borrows.
let many_borrow =
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&a;
}
fn main() {
let mut x = 5_usize;
let mut y = 7_usize;
reference_propagation(&x, &y);
reference_propagation_mut(&mut x, &mut y);
reference_propagation_const_ptr(&raw const x, &raw const y);
reference_propagation_mut_ptr(&raw mut x, &raw mut y);
read_through_raw(&mut x);
multiple_storage();
dominate_storage();
maybe_dead(true);
2023-05-10 11:06:41 -05:00
mut_raw_then_mut_shr();
unique_with_copies();
2023-05-10 13:27:54 -05:00
debuginfo();
2023-05-13 05:29:05 -05:00
many_debuginfo();
}
// EMIT_MIR reference_prop.reference_propagation.ReferencePropagation.diff
// EMIT_MIR reference_prop.reference_propagation_mut.ReferencePropagation.diff
// EMIT_MIR reference_prop.reference_propagation_const_ptr.ReferencePropagation.diff
// EMIT_MIR reference_prop.reference_propagation_mut_ptr.ReferencePropagation.diff
// EMIT_MIR reference_prop.read_through_raw.ReferencePropagation.diff
// EMIT_MIR reference_prop.multiple_storage.ReferencePropagation.diff
// EMIT_MIR reference_prop.dominate_storage.ReferencePropagation.diff
// EMIT_MIR reference_prop.maybe_dead.ReferencePropagation.diff
2023-05-10 11:06:41 -05:00
// EMIT_MIR reference_prop.mut_raw_then_mut_shr.ReferencePropagation.diff
// EMIT_MIR reference_prop.unique_with_copies.ReferencePropagation.diff
2023-05-10 13:27:54 -05:00
// EMIT_MIR reference_prop.debuginfo.ReferencePropagation.diff