<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61907>61907</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [AArch64] Incorrect copy propagation across BLR_RVMARKER
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kyulee-com
      </td>
    </tr>
</table>

<pre>
    `BLR_RVMARKER` is a pseudo instruction attaching ARC related helpers + operations.
In the following cutdown, given two instances, `mov x0, x19` before and after `BLR_RVMARKER`, the latter is optimized by machine copy propagation (machine-cp), which caused mis-compilation along with other optimizations in our code base.

```
$ cat test.ll
target triple = "arm64-apple-ios13"

%0 = type opaque
%1 = type opaque
%2 = type opaque

define %0* @foo(i8* %0) {
entry:
  tail call void @f1(i8* null)
  %1 = bitcast i8* %0 to %0*
  %call1 = call %0* @f2(%0* %1) [ "clang.arc.attachedcall"(i8* (i8*)* @llvm.objc.retainAutoreleasedReturnValue) ]
  tail call void @f3(i8* %0)

  ret %0* null
}

declare void @f1(i8*)
declare void @f3(i8*)

define %0* @f2(%0* noundef returned %dict) {
entry:
 ret %0* null
}

declare i8* @llvm.objc.retainAutoreleasedReturnValue(i8*)
```

```
$ llc < test.ll
...
        mov     x0, x19
        bl      _f2
        mov     x29, x29
        bl      _objc_retainAutoreleasedReturnValue
        // mov  x0, x19  ; This copy is deleted by machine-cp
        bl      _f3
...
```

I figured `BLR_RVMARKER` sometimes does not set `implicit-def $x0` as it inherits from the first call, which allows this transformation valid.
Since `BLR_RVMARKER` combines a objc helper call like `objc_retainAutoreleasedReturnValue` which always have a return value, I wonder if we should conservatively add `X0` to `Defs` to this instruction in `.td` file.
Or, this is a valid optimization, which we should allow?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVlFv4zYM_jXKC5FAkRM3fshD2qxAs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