[llvm-dev] Signed Division and InstCombine

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Tue May 31 16:12:18 PDT 2016

On 31 May 2016 at 16:02, Dilan Manatunga <manatunga at gmail.com> wrote:
> Just to verify, a 16-bit divion of INT16_MIN  by -1 results in INT16_MIN
> again?

No, "sdiv i16 -32768, -1" is undefined behaviour. The version with an
"sext" and "trunc" avoids the undefined behaviour and does return

> If the issue only occurs in this case, why aren't there checks to see if we
> can simplify sdiv in cases where we know that numerator is not INT16_MIN or
> the denominator is not -1. For example, we could simplify divides involving
> one operand constants. Is it because this case is most likely rare?

It's probably just that no-one has bothered to implement it, I'd
actually expect the code to be reasonably common from C compilation.
Another factor is that RISC CPUs mostly won't care (they tend to only
have natural width division anyway), so that removes a large swathe of
people who might be interested.



More information about the llvm-dev mailing list