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

    <tr>
        <th>Summary</th>
        <td>
            Fast ISel on AArch64 produces an invalid relocation/relaxation sequence
        </td>
    </tr>

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

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

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

<pre>
    When using `-O1`, fast isel, and sanitizers on ARM64 / macOS it seems that Clang 15 miscompiles Abseil's flag parsing code.

There is code to set a global variable to a specific value which looks like this:
```
        adrp    x10, __ZN4absl14flags_internal12_GLOBAL__N_115specified_flagsE@PAGE
        add     x8, x10, __ZN4absl14flags_internal12_GLOBAL__N_115specified_flagsE@PAGEOFF
        str     x20, [x8]
```

While superficially this looks correct, when linked (both with LLD and LD64 as far as I can tell) the instruction sequence becomes:
```
    0x10078a25c <+2812>:  adrp   x10, 3749
    0x10078a260 <+2816>:  nop
    0x10078a264 <+2820>:  str    x20, [x8, #0x9c0]
```

Because the `str` still uses `x8` as the base register rather than `x10`, the address is based on a stale value left over from somewhere else.

My assumption is that this sequence of relocations are intended to enable this linker relaxation and are supposed to use a single register in order to make the relaxation simple to apply. At least, that seems likely given that both LLD and LD64 don't do any validity checks and produce the same result.

Unsure what the best reproducer would be here as reducing this is tricky without disturbing the pattern that allows the relaxation to apply or the registers used. I have the IR input for the whole function from Abseil that produces it, although that's somewhat large. I'm trying some ideas for further minimizing it.

[parse.onefunction.ll.zip](https://github.com/llvm/llvm-project/files/9793287/parse.onefunction.ll.zip)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVV1v2zYU_TXyC1FDH5YtPfjB-XARIG2GdkOBvRiUdGVxoUiNpOK4v36HlJLGRVZgwAxDlMjL-3F4zmWlm_P2W0eKjVaoI4vW8YeHBM8ovWYtt44JS9J_cN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