Tim Northover via llvm-dev
2016-May-31 22:50 UTC
[llvm-dev] Signed Division and InstCombine
On 31 May 2016 at 15:42, Tim Northover <t.p.northover at gmail.com> wrote:> A 16-bit division of INT16_MIN by -1 is undefined behaviour but the > original ext/trunc version is well-defined as 0.Sorry, INT16_MIN again actually. The main point still stands though, I think. Tim.
Dilan Manatunga via llvm-dev
2016-May-31 23:02 UTC
[llvm-dev] Signed Division and InstCombine
Just to verify, a 16-bit divion of INT16_MIN by -1 results in INT16_MIN again? 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? -Dilan On Tue, May 31, 2016 at 3:50 PM Tim Northover <t.p.northover at gmail.com> wrote:> On 31 May 2016 at 15:42, Tim Northover <t.p.northover at gmail.com> wrote: > > A 16-bit division of INT16_MIN by -1 is undefined behaviour but the > > original ext/trunc version is well-defined as 0. > > Sorry, INT16_MIN again actually. The main point still stands though, I > think. > > Tim. >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160531/ecf014ec/attachment.html>
Tim Northover via llvm-dev
2016-May-31 23:12 UTC
[llvm-dev] Signed Division and InstCombine
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 -32768.> 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. Cheers. Tim.