Issue 159249
Summary Inconsistent assembly emitted when dividing integers by a constant in unoptimized builds (-O0)
Labels new issue
Assignees
Reporter Bagellll
    Whilst messing with some stuff on compiler explorer I noticed (at least what I believe to be) inconsistencies with integer division. At first I was only looking at x86-64 but noticed the behavior is defferent across ISAs. On optimized builds this behavior does not appear.

I apologize as I don't know how to categorize this. This could be an issue to some, intended, or a feature/feature request to others. AFAIK `div` is unnecessary for most of the example cases. I am also not an expert so hopefully this will at least be a useful observation.

https://godbolt.org/z/nEhbaE6nT
*Not included but maybe worth mentioning GCC avoided `div` whenever possible.*

Below are all observations written out.

## x86-64
Dividing by a power of 2 is only implemented as a `shr` for unsigned integers.
Dividing by a power of 2 is emitted as `idiv` for signed integers.
Dividing by any other constant results in `idiv` or `div` regardless of signage.
## ARMv8
All division attempts are emitted as a divide instruction.
## RISC-V
Only emits a divide instruction when necessary.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to