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

    <tr>
        <th>Summary</th>
        <td>
            Miscompile from AArch64 Global ISel backend: UDiv on LLVM>17
        </td>
    </tr>

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

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

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

<pre>
    Look at the function `f` below:

```llvm
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-linux-gnu"

define i2 @f(i2 %0) {
  %2 = sext i1 true to i2
  %3 = udiv i2 1, %2
  ret i2 %3
}
```

For the invocation `f(-1)`, the expectation is that `f` will return a 0.

Lets look at the assembly generated by llc.

```assembly
tanmay@revenant:~/llvm-clone/llvm/build-release/bin$ llc --march=aarch64 foo.ll -o -
        .text
        .file   "foo.ll"
        .globl  f // -- Begin function f
        .p2align        2
        .type   f,@function
f: // @f
        .cfi_startproc
// %bb.0:
        mov     w0, wzr
        ret
.Lfunc_end0:
        .size   f, .Lfunc_end0-f
        .cfi_endproc
                                        // -- End function
        .section        ".note.GNU-stack","",@progbits
```

Look at `%bb.0`. It stores a 0 in `w0`. According to the [procedure call standard for AArch64](https://github.com/ARM-software/abi-aa/blob/2a70c42d62e9c3eb5887fa50b71257f20daca6f9/aapcs64/aapcs64.rst#general-purpose-registers) the function returns the value stored in `w0` which as expected is 0.

Let us look at the assembly after running `--global-isel`:

```assembly
tanmay@revenant:~/llvm-clone/llvm/build-release/bin$ llc --global-isel --march=aarch64 foo.ll -o -
        .text
        .file   "foo.ll"
        .globl  f // -- Begin function f
        .p2align        2
        .type   f,@function
f: // @f
        .cfi_startproc
// %bb.0:
        ret
.Lfunc_end0:
        .size   f, .Lfunc_end0-f
        .cfi_endproc
                                        // -- End function
        .section        ".note.GNU-stack","",@progbits
```

As can be seen here in `%bb.0`, there are no instructions. the function returns the value stored in `w0` which has the first argument stored in it so -1.

The behavior was correct up until llvm-17.

cc @regehr 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVsGO2zYQ_Rr6MpBAUbJsHXzwruMgwKaHpuk1GElDiV2aFEjK3s2h315Qstd22gRFgZ6yWBPS8HFmOPPe2Oi96gzRhi0f2HK3wDH01m0CmgO-BuUGNM_oFrVtXzdP1j4DBgg9gRxNE5Q1wEouWcmhJm1PLN8yvmP8spZ8_tf6eJhNAV1HAVoMqPHVjgFYvgMmBCUHlm8pUWuWb-MnF4nKSpZvpyW-lQXLt2WRqExEQCbWicnFbPsUbULcBQlODZouARBd05dFopUZX5LOjG_weW1JKkOgBLCCSybW8UksORMVsNXDDIJoEpNLTy8BVAbBjQTBghI3kHyCjK06RocZE4_TwQvCUYDZfX5OYbX7pmS3qe2tm4quzNE2eC27WCcZE1VEi8cJQS8DNWGGKA-hx_DWoZPSOkYenQEEnt5GeKLgQd_0F72nQ61foSNDDgO1UL-C1k36jw2-wC_1j-xhBXd0JIMmsHz7JxP7SIOk0dbQ-YWJfT0q3SaONKGP5loZJooYCZLkEHvG8t25dyCtTbWGxEJySaNKA72E65tUmhivmBAz-KbLVdppW2vGKwlM7JnYQ5LAA3XKXPksr-hBoFadYby6cRFeh-hfMvEYeXI-Nu9Llm8vnicSvZ1qpPriA7owONuczWecWNZ1ym-UUx3skfHqNDX19NW92R2d75k-xbhfyLR351Kvvl5ygxtM8k0iZNprGvAv_64Fe2dauL_3FJvOhlj61NhA6ftfPic-YPMceyAep1XMdRuc7WoV_A9If5k2E7vnGpU8hQ8BfLCOfOQwqEkJp3lr2zTWtcp0UY6RxGz5EC9K7egIGtQafEDTomtBWgfb7cQqttwxse5DGHys5nTPToV-rNPGRopuf_2YeCvDCV1kKNYqQYxU1bZmYi9wxZtCtKWgqsmpXq7XK4lLXq8ysVxJwVtssJRVPIpD48vi-pQ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