From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20778 invoked by alias); 12 Apr 2011 18:36:20 -0000 Received: (qmail 20723 invoked by uid 22791); 12 Apr 2011 18:36:19 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 12 Apr 2011 18:36:09 +0000 From: "zackw at panix dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/48580] New: missed optimization: integer overflow checks X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: zackw at panix dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Tue, 12 Apr 2011 18:36:00 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-04/txt/msg01261.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48580 Summary: missed optimization: integer overflow checks Product: gcc Version: 4.6.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: rtl-optimization AssignedTo: unassigned@gcc.gnu.org ReportedBy: zackw@panix.com To the best of my knowledge, this is the only safe way (without -fwrapv) to check whether the product of two signed integers overflowed: bool product_does_not_overflow(signed x, signed y) { unsigned tmp = x * unsigned(y); return signed(tmp) > 0 && tmp / x == unsigned(y); } (I believe C and C++ are the same in this regard but I could be wrong. If there is a better way to write this test I would love to know about it.) g++ 4.6 produces this assembly dump on x86-64: _Z25product_does_not_overflowii: movl %esi, %edx xorl %eax, %eax imull %edi, %edx testl %edx, %edx jle .L2 movl %edx, %eax xorl %edx, %edx divl %edi cmpl %eax, %esi sete %al .L2: rep ret but, if I understand the semantics of IMUL correctly, it could do this instead: _Z25product_does_not_overflowii: xorl %eax, %eax imull %edi, %esi setno %al ret which is a pretty substantial micro-win, particularly in getting rid of a divide.