<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/82387>82387</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed tail call optimization on undefined return value for all architectures
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ekliptik
</td>
</tr>
</table>
<pre>
See [compiler explorer](https://godbolt.org/z/7T7P3foed) to confirm it to affect x64, arm, aarch64, RISC-V clang, but not gcc, at `-O3`.
## LLVM
`TaillRecursionElimination.cpp` does kick in and marks the `call` with `tail`, but sdag lowers it to `PsuedoCALL`. Interestingly, adding `-fglobal-isel -mllvm -global-isel-abort=1` to AArch64 shows that with gisel, the IRTranslator turns `tail call` directly to `BL`, so it's not sdag-specific.
## GCC
I believe gcc's TCO in this case to be correct even just based on the C language spec since when a function doesn't `return` a value, the function call expression's value is undefined
## Reproducer
Build with `-O3` with any gcc, any clang:
```c
int i;
int bar(void);
int foo (void)
{
if (i)
{
bar();
}
}
```
Code has been reduced from `Proc_1` calling `Proc_7` in Dhrystone, where functions with prefix `Proc_` for "procedure" have unspecified return value types, and therefore default to `int` (since C89) despite the original intention clearly being `void`. Nevertheless this stinky use case is probably rather common and worth addressing
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVVFv2zoP_TXKC5HCkVM7echDm3z5UKC7G7pirxeyRNtcZcmQ5GTZr7-g7WTZMCCIQZGiziEPJRUjNQ5xJx6fxeNhoYbU-rDDD0t9oo9F5c1l9xURxOOz9l1PFgPgj976gEE8HoTctCn1UeRPQh6FPDbeVN6mBx8aIY8_hTyW7-WXvPZohNxC8qC9qyl0QIktVdeoE_wo1kLuQYVu_Kig22nl7eXrfvkNtFWuYbsaEjifoNF6jEwgimz5ORdF9iCyg8ie5n-ZC5nD6-u3T78tF9m7ImvfUA8hknf_s9SRU4m8e9B9L4oMjMcIH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