[PATCH] D128572: [LoongArch] Add codegen support for division operations
WÁNG Xuěruì via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 25 01:53:37 PDT 2022
xen0n added a comment.
In D128572#3609951 <https://reviews.llvm.org/D128572#3609951>, @xry111 wrote:
> In D128572#3609944 <https://reviews.llvm.org/D128572#3609944>, @xen0n wrote:
>
>> I just experimented on 3A5000 and it seems the "undefined result" is all-zeros, and unfortunately the `div.w`/`mod.w` UB when input operand is non-canonical (i.e. non-sign-extended) 32-bit is indeed present; in this case the output is all-zeros too.
>>
>> So the sign-extension is indeed necessary for inputs not statically known to be `signext`. The all-zeros in case of UB is less useful than RISCV's all-ones, in terms of expected semantics (we want ideally something near "infinity"), but it's UB after all, and 0 is equally okay here.
>
> My mistake: I saw GCC generated "div.w $a0, $a0, $a1", but it only works because the ABI has made sure that the parameters are already signed-extended.
Indeed. I experimented further and it's a whole can of worms with non-canonical inputs to `{div/mod}.w`:
# a = 4294967396 (0x0000000100000064)
# b = 5 (0x0000000000000005)
div.w(a, b) = 0 # 0x0000000000000000
mod.w(a, b) = 0 # 0x0000000000000000
# a = 4294967396 (0x0000000100000064)
# b = -5 (0xfffffffffffffffb)
div.w(a, b) = 0 # 0x0000000000000000
mod.w(a, b) = 0 # 0x0000000000000000
# a = 100 (0x0000000000000064)
# b = 8589934597 (0x0000000200000005)
div.w(a, b) = 85899345920 # 0x0000001400000000
mod.w(a, b) = 100 # 0x0000000000000064
# a = 100 (0x0000000000000064)
# b = -8589934597 (0xfffffffdfffffffb)
div.w(a, b) = -85899345920 # 0xffffffec00000000
mod.w(a, b) = 100 # 0x0000000000000064
The result is even more unintelligible if the divisor is non-canonical... let's just ensure `signext` one way or another (ABI, `signext`, legalization, etc.) and never invoke the UB. It's not fun.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D128572/new/
https://reviews.llvm.org/D128572
More information about the llvm-commits
mailing list