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

    <tr>
        <th>Summary</th>
        <td>
            [clang] global TLS var CodenGen error in Aarch64
        </td>
    </tr>

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

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

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

<pre>
    [godbolt clang 15.0.0](https://godbolt.org/z/9z4jTeWsM)

## Version

```
clang version 15.0.4
Target: aarch64-unknown-linux-gnu
Thread model: posix
```

## Description

Generate TLS var code between two `CSEL` instruction, so there have a `blr` instruction between two `CSEL` instruction. And the two
`CSEL` instructions have the same condition flag. But `blr` instruction will change the **condition flags**. So I think maybe it's a error.

## Code

```
#include<stdio.h>
typedef int (*after_use_call)(unsigned long);

typedef struct StructA {
    unsigned short int a;
    unsigned short int b;
} StructA;

typedef struct StructB {
    unsigned short int a;
    unsigned short int b;
    unsigned short int c;
    unsigned short int d;
} StructB;

extern StructA struct_a_array[56];
extern __thread unsigned int g_tls_var;

StructB struct_b = { 0 };

extern unsigned int return_bool_call(void);

void tls_error(after_use_call after_use)
{
    unsigned short int len;
    unsigned short int b;
    unsigned int bo;

    bo = return_bool_call();
    len = bo ? sizeof(StructB) : sizeof(StructB) + 24;
    b = struct_a_array[g_tls_var].b;

    struct_b.a = b + 1;
    struct_b.b = (len & 0x00EEE) << 8 | (len & 0xEE00E) >> 8;

    after_use(len);
    return;
}
```

Command: `clang test.c -O2 -fPIC -S -o test.s`

## Maybe error AS

[godbolt](https://godbolt.org/z/9z4jTeWsM)

```
        cmp     w0, #0  
        mov     w9, #8192
        csel    w8, w9, w8, eq
        adrp    x0, :tlsdesc:g_tls_var
        ldr     x1, [x0, :tlsdesc_lo12:g_tls_var]
        add     x0, x0, :tlsdesc_lo12:g_tls_var
        blr     x1                                             ; condition flags maybe change
        adrp    x10, :got:struct_a_array
        mrs     x9, TPIDR_EL0
        ldr     w9, [x9, x0]
        mov     w11, #32
        ldr     x10, [x10, :got_lo12:struct_a_array]
        add     x9, x10, x9, lsl #2
        mov     w10, #8
        csel    x0, x11, x10, eq                        ; if change, then two `csel` not Consistent
```

Additionly, I find the lastest release seem fix this error?

[godbolt clang trunk](https://godbolt.org/z/fxG3orv3x)

If so, please tell me the PR or commit. Otherwise, just give me some suggestions to solve this, I've debug this few days








</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV8tu2zwTfRp6M7BAU75ICy98LQK0aNEE_780KGkss6HJlKRsp0__gaTsRK6T5rsYgRSJZ2YO5wzJEbdW1ApxSkZzMlr2eON22kx31j2mtFfo6tmP1LoqtHRQSq5qGIwSmlAyWhKW7Zx7siSdEbYmbN3iEm1qwta_CFvnv4Y_HvD_9gthOaFLQmftlaWEpfA_NFZo1RkZ0_YvPMaQh4iLoYdx5IGbGh1JZ8C5KXfjYb9Rj0ofVV8K1Zz6tWpa4M4gr2CvK5Qe_qStON0M1uG2RFsa8eSu-H1ChYY7hIfP93DgBkpdIRTojogK3FEDGdPF_eozGVMQyjrTlMHHak3mC5ItwGpwOzQIO35A4B5fSHMF_4DHBGaq8q485jKfG0AbI3mk5XuEUqtKhCBbyesE5o17g8RRSAnljqs6mhM2I2zWtbfxZQL3Gu7A7YR6hD1_LhCEI2xigQMao01yI8cLXeE74hOWClXKpkKSLqyrhE52JF3FQff8hBVuQSgHhGWEzfjWodk0Fjcll9JXHMsaFQq8AqlV7V-l89cBz07ilOE-3GZAJi0KAODiwu60cSEev7h5A1C8xJksz24_Env-X8Z-A1D-CVDdYD-_Yo8nh0ZdMhYnseEbbgx_JqP5aOx3iLNNi95sXFyMl6A-XL1x0m4O3FyFOGek9V0ASZc-PUCBTJa3-XQcG3SNUZtCa9mWRHbQovq9DPxb8CRCpRKWdUsJLo8v29gfVJKo_qFOYUhfEfSIQocE3JjU6wl5pEQVoM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