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

    <tr>
        <th>Summary</th>
        <td>
            coldcc miscompiles catastrophically on x86_64
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          mcy
      </td>
    </tr>
</table>

<pre>
    I was messing around with using `coldcc` for some optimizations in a frontend, and I wrote some code to see how it changed the codegen.

https://godbolt.org/z/The9EKP9x

```ll
target triple = "x86_64-unknown-linux-gnu"

%T = type {ptr, i64}
define coldcc %T @get_span() {
  call i32 @printf(ptr @fmt.0, ptr null, i64 0)
  ret %T zeroinitializer
}

define i32 @main() {
  %span = call %T @get_span()
  %span.0 = extractvalue %T %span, 0
  %span.1 = extractvalue %T %span, 1
  call i32 @printf(ptr @fmt.0, ptr %span.0, i64 %span.1) 
  ret i32 42
}

@fmt.0 = constant [9 x i8] c"%p, %lu\0A\00"
declare i32 @printf(ptr, ...)
```

This program is miscompiled at both `-O0` and `-O2`. In the former, it outputs `(nil), 0\n(nil), 140723522928744\n`, while in the latter, it outputs nothing and incorrectly has an exit code of `0`. The expected output is `(nil), 0\n(nil), 0\n`.

In the former case, the second half of the return value of `@get_span` is clobbered by a `pop rdx` in the epilogue, despite the fact that this function uses `rdx` as an ABI register, so it can't be callee-saved. In the latter case, calling `@get_span` causes `main` to be replaced with `unreachable`.

As far as I can tell, `coldcc` does not document that it implies `noreturn`, but it feels like that's what I'm seeing here.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU1v4zYQ_TX0ZWCDoiXZOviQbGog6KE95L6gyJHELkUK5Gjt5NcXpBRvnAZYFDBkiJw38-ZpPmSMpneIJ1Y9suppI2cafDiN6nXTev16eoaLjDBijMb1IIOfnYaLoQHmfMJqrrzVSrGaQ-cDR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