[Bug tree-optimization/71289] Fails to optimize unsigned mul overflow check 'A > -1 / B'
Explorer09 at gmail dot com
gcc-bugzilla@gcc.gnu.org
Thu Jan 26 14:59:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71289
Kang-Che Sung <Explorer09 at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |Explorer09 at gmail dot com
--- Comment #6 from Kang-Che Sung <Explorer09 at gmail dot com> ---
Hello. Thank you guys for implementing the multiplication overflow
optimization.
Otherwise there have been "complaints" on the web [1] about lack of such
optimization.
However, from what I tested in the current svn source (r244933), the construct
like this (b != 0 && -1 / b < a) still not yet yields optimal code. In
particular, there are still unnecessary (b != 0) generated...
...for a code like this:
----------------------------------------------
#define UINT_MAX (~0U)
void abort(void); /* from stdlib.h */
unsigned int umul_overflow1(unsigned int a, unsigned int b) {
if (b != 0 && UINT_MAX / b < a)
abort();
return a * b;
}
unsigned int umul_overflow2(unsigned int a, unsigned int b) {
unsigned int res;
if (__builtin_umul_overflow(a, b, &res))
abort();
return res;
}
----------------------------------------------
Generated assembly (-O2, gcc configured with --target=x86_64-pc-linux-gnu
--with-arch-64=generic)
umul_overflow1:
.LFB0:
.cfi_startproc
testl %esi, %esi
je .L2
movl %edi, %eax
mull %esi
jo .L13
.L2:
movl %esi, %eax
imull %edi, %eax
ret
.L13:
subq $8, %rsp
.cfi_def_cfa_offset 16
call abort
.cfi_endproc
umul_overflow2:
.LFB1:
.cfi_startproc
movl %edi, %eax
mull %esi
jo .L22
rep ret
.L22:
subq $8, %rsp
.cfi_def_cfa_offset 16
call abort
.cfi_endproc
----------------------------------------------
I would expect that ideally, both functions will generate same machine code.
[1]http://kqueue.org/blog/2012/03/16/fast-integer-overflow-detection/
More information about the Gcc-bugs
mailing list