<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJy1WFtz2joQ_jXkRWOPbS6BBx4SSM9kJp10TtrkkZFtAWplydUFkvPrz65sYzuBlN4YBmxpvfvp25vkVOUv81uypTtGUsYk0Sxj0ooXYpmxXG7IjmqunIH7bCv5d8fgUhFelIIVIEl2XFtHBSlotuUSZrkki8HwimytLQ1cDJIP8N1wu3VpmKkCbsyW5mqv1pKV1kkGI3arGc1ZHmQqZ0HKZLYtqP5GyL1krWlit9SSW7JWTuZgaKfEjuXE8FQg1IwKYULytIV1gKGS-9E9WCaqtLzg_1HLlTSESZoKeJDKVw83M4NkAba4IXulvxliXJYxY9ZOADH7I-rvuHTPQUoNKF0IKjeAAscfuUFuHqzLuRokl6aardTDcigXtWGqGZHKNkAR3J6-eIQoWWq10bQghm3W1AlrwkG0HERX9W-C35tnil7xlPtvR-IzLga-CugsqQY7a68X6a5Y1UxwhgKv6Ky1TKL662_tS8lytiY7xXMCpmp4g-H10fkpiKydzFal1YNkBgNOGr6RFdHd62ahKNUoq369KsmebedpwsuuAsLxrqEK_1FNpePyoGzGS4B0PRguSdyxMWsA4nrQzgoHCIqBxRY9MDvzqjuPHsQH42uANF7CExW0A6Legi6Xb1aWsg2Xv720UnNp157w5MGCn9GP2kmMifEC9Cd9Ymc1oWeB7YdA7WiIIVw45lXlHcj8lEF6YJ3AoKPGsCKFvAGBJy4h7003hWBpPoN8XpAd0wY1xUkYhbWhz1RvmNfLJ9NJUGbBvlITFGaXHTIEEwdzu8IY3MckWDfJFNQhHVS5FjzBH_wCXE3hX6rASQe5G0Bq0IJZpkmwIcFDvd5hL5Xe8DALN0KlAi5WyAA5_RkkQxIE5Bp9_Yq2g64yoQJcDpcjdEf0PKsNrRp6f_hBM4NR1Oq98-HrQ6y7mlmY7XzYriAAo_4oeAYuY-JDJmn-4p6QUBlcRSQmMxK9iyZTEkLR-fK7KiEkwBlhBlAgzq5eWS7VCm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qlW9ldX9dDYe2CM8OqlUbNHwNdl9caHtruPKhk0DH11RXmRz4f5bDijF9TZrdLzV6F34bSYvxOpQuyavwDy4CvLINU_cGMc7J-SD-NhMhxfbOcsGeXJLJ2sL6fxNKKTdDwZXY6TJLtkI7qmkwtBUybMHI8X4-XF75vk8yRKINTjSTwdx6MonF5GNKbTGRuP1pMRHcFmiBVQHELUEyq9udBzrxIKioFJwY017SR6bSMZaxBabgWbV6nSeXOSK2Z8WYFMgLQofP0J3tSfC4927qH-D0ZHb50>53235</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang on Windows does not perform tail-call optimization
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    I have been recently testing various techniques to implement virtual machines in C: https://github.com/shadowofneptune/threaded-code-benchmark  One technique that I found involved sibling calls. When compiling with optimizations enabled and sibling calls enabled, this works successfully when compiling with Linux-based Clang. With Visual Studio's Clang, the tail calls are not optimized away and the program segfaults.

**Example:**

This is one part of the code that relies on sibling calls.
```
typedef void *program;
typedef void (*func_ptr)(unsigned, unsigned,  program);

void next(unsigned ip, unsigned i, program prog)
{
        ip += 1;
        func_ptr *next_func = (func_ptr*)prog;
        next_func[ip](ip, i, prog);
}

void begin(unsigned ip, unsigned i, program prog)
{
        printf("Starting run:\n");
        next(ip, i, prog);
}
```

the function next: becomes this assembly on Windows when compiled with Clang version 12.0.0
Target: i686-pc-windows-msvc and the options ``-O1 -foptimize-sibling-calls -Wall -Wextra -Wno-unused-parameter -g -S``:

```
        .globl  _next                           # -- Begin function next
        .p2align        4, 0x90
_next:                                  # @next
Lfunc_begin0:
        .cv_func_id 0
        .cv_file        1 "" "" 1
        .cv_loc 0 1 9 0                         # continuation_passing.c:9:0
        .cv_fpo_proc    _next 12
# %bb.0:
        .cv_loc 0 1 10 0                        # continuation_passing.c:10:0
        movl    12(%esp), %eax
        movl    4(%esp), %ecx
Ltmp0:
        #DEBUG_VALUE: next:next_func <- $eax
        #DEBUG_VALUE: next:ip <- [DW_OP_plus_uconst 4] [$esp+0]
        #DEBUG_VALUE: next:i <- [DW_OP_plus_uconst 8] [$esp+0]
        #DEBUG_VALUE: next:prog <- [DW_OP_plus_uconst 12] [$esp+0]
        leal    1(%ecx), %edx
Ltmp1:
        #DEBUG_VALUE: next:ip <- $edx
        .cv_loc 0 1 12 0                        # continuation_passing.c:12:0
        pushl   %eax
Ltmp2:
        pushl   12(%esp)
        pushl   %edx
        calll   *4(%eax,%ecx,4) #It's calling the next function
Ltmp3:
        addl    $12, %esp
        .cv_loc 0 1 13 0                        # continuation_passing.c:13:0
        retl
Ltmp4:
        .cv_fpo_endproc
Lfunc_end0:
```
On a Debian machine with Clang version 7.0.1-8+deb10u2 Target: x86_64-pc-linux-gnu, compiled with the same options, that same function becomes:

```
        .globl  next                    # -- Begin function next
        .p2align        4, 0x90
        .type   next,@function
next:                                   # @next
.Lfunc_begin0:
        .loc    1 9 0                   # continuation_passing.c:9:0
        .cfi_startproc
# %bb.0:
        #DEBUG_VALUE: next:ip <- $edi
        #DEBUG_VALUE: next:i <- $esi
        #DEBUG_VALUE: next:prog <- $rdx
                                        # kill: def $edi killed $edi def $rdi
        #DEBUG_VALUE: next:next_func <- $rdx
        #DEBUG_VALUE: next:prog <- $rdx
        #DEBUG_VALUE: next:i <- $esi
        #DEBUG_VALUE: next:ip <- $edi
        .loc    1 10 5 prologue_end     # continuation_passing.c:10:5
        addl    $1, %edi
.Ltmp0:
        #DEBUG_VALUE: next:ip <- $edi
        .loc    1 12 2                  # continuation_passing.c:12:2
        movq    (%rdx,%rdi,8), %rax
                                        # kill: def $edi killed $edi killed $rdi
.Ltmp1:
        #DEBUG_VALUE: next:i <- $esi
        jmpq    *%rax                   # TAILCALL
.Ltmp2:
.Lfunc_end0:
        .size   next, .Lfunc_end0-next
        .cfi_endproc
```

This appears to be a bug, since I am not sure why tail call optimization would not be supported on MSVC-compatible clang.

**Steps to reproduce:**

- Clone this repository: https://github.com/shadowofneptune/threaded-code-benchmark
- Download Microsoft Visual Studio Community 2019 Version 16.11.9 and install Clang for visual studio.
- Enter PowerShell for developers , navigate to the repository, and run `` clang.exe continuation_passing.c -O1 -foptimize-sibling-calls -Wall -Wextra -Wno-unused-parameter -g -o continuation_passing.exe``
- Run continuation_passing.exe, it should segfault immediately.
- Run  clang continuation_passing.c -O1 -foptimize-sibling-calls -Wall -Wextra -Wno-unused-parameter -g -S
- See if the functions end with a function call or a jump
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1WFtv4joQ_jX0xUqUBGjJAw8tdI8qddXV6V4ekZMY8K5jZ32B9vz6M-MkJGmhy94QgsSezHz-5mYnU8Xz_I5s6Y6RjDFJNMuZtOKZWGYslxuyo5orZ-A-30r-3TG4VISXlWAlSJId19ZRQUqab7mEWS7JYjS-JltrKwMXo-QdfDfcbl0W5qqEG7OlhdqrtWSVdZLBiN1qRgtWBLkqWJAxmW9Lqr8R8iBZZ5rYLbXkjqyVkwUY2imxYwUxPBMINadCmJB82cI6wFDF_egeLBNVWV7y_6jlShrCJM0EPEjli4fbmVGyAFvckL3S3wwxLs-ZMWsngJj9EfX3XLqnIKMGlC4ElRtAgeOfuUFuHq0ruBolV6aerdXDcigXjWGqGZHKtkAR3J4-e4QoWWm10bQkhm3W1AlrwlG0HEXXzW-C39snil7xlPtvT-IjLga-CuisqAY7a68X6a5Z1UxwhgIv6Gy0XEbN19_a54oVbE12ihcETDXwRuObo_MzEFk7ma8qq0dJCgNOGr6RNdH963ahKNUqq3-9KsmebO9pwqu-AsLxrqUK_1FNrePqoCzlFUC6GY2XJO7ZSFuAuB60s8IBgmJgsUMPzKZede_Rg_hoegOQpkt4ooZ2QDRY0NXy1coytuHyt5dWaS7t2hOePFrwM_pRO4kxMV2A_mRIbNoQehbYYQg0joYYwoVjXtXegcz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