public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/30315]  New: optimize unsigned-add overflow test on x86 to use cpu flags from addl
@ 2006-12-28  7:09 raeburn at raeburn dot org
  2007-06-06  3:39 ` [Bug target/30315] " scovich at gmail dot com
                   ` (18 more replies)
  0 siblings, 19 replies; 21+ messages in thread
From: raeburn at raeburn dot org @ 2006-12-28  7:09 UTC (permalink / raw)
  To: gcc-bugs

Using gcc-4.3-20061223 targeting i386-linux and options "-O9
-fomit-frame-pointer -fexpensive-optimizations -S", my test program to detect
unsigned integer overflow:

extern void abort(void);
unsigned foo(unsigned a, unsigned b) {
    unsigned sum = a + b;
    if (sum < a) abort(); /* check for overflow (wrapping) */
    if (sum < b) abort(); /* redundant */
    return sum;
}

compiles to:

foo:
        subl    $12, %esp
        movl    16(%esp), %eax
        movl    20(%esp), %edx
        addl    %eax, %edx
        cmpl    %edx, %eax
        ja      .L7
        cmpl    %edx, 20(%esp)
        ja      .L7
        movl    %edx, %eax
        addl    $12, %esp
        ret
.L7:
        call    abort

After the addition, which sets the ALU flags, the compiler issues two compare
instructions and conditional branches.  This sequence could be replaced by a
conditional branch following the addl, testing one of the flags (overflow?
carry? I forget which) set by it.

I realize for other processors (e.g., with -march=pentiumpro), the addl may be
replaced by something like leal, which may not set the flags the same way.  But
if the flags are set when building for a certain architecture, use them.... 
And is leal+cmp with data dependence still going to be better than addl?

(Also, I think a different set of register selections could eliminate the last
"movl" instruction, by putting the result of the addition into the return-value
register.)


-- 
           Summary: optimize unsigned-add overflow test on x86 to use cpu
                    flags from addl
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: target
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: raeburn at raeburn dot org
 GCC build triplet: powerpc-apple-darwin
  GCC host triplet: powerpc-apple-darwin
GCC target triplet: i386-unknown-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30315


^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2009-11-16 19:05 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-28  7:09 [Bug target/30315] New: optimize unsigned-add overflow test on x86 to use cpu flags from addl raeburn at raeburn dot org
2007-06-06  3:39 ` [Bug target/30315] " scovich at gmail dot com
2007-06-06  8:16 ` ubizjak at gmail dot com
2007-06-06  9:10   ` Andrew Pinski
2007-06-06  9:10 ` pinskia at gmail dot com
2007-06-06 10:33 ` rask at sygehus dot dk
2007-06-06 13:00 ` ubizjak at gmail dot com
2007-06-06 14:51 ` raeburn at raeburn dot org
2007-06-13 13:37 ` rask at sygehus dot dk
2007-08-03 19:40 ` patchapp at dberlin dot org
2007-08-04 13:10 ` patchapp at dberlin dot org
2007-08-14 14:39 ` rask at gcc dot gnu dot org
2007-08-18 17:56 ` raeburn at raeburn dot org
2007-08-19 13:01 ` rask at gcc dot gnu dot org
2007-08-19 18:04 ` ubizjak at gmail dot com
2007-08-30 19:45 ` patchapp at dberlin dot org
2007-09-09 19:22 ` rask at gcc dot gnu dot org
2007-11-10  1:32 ` rask at gcc dot gnu dot org
2009-01-02 19:30 ` pinskia at gcc dot gnu dot org
2009-11-12 17:29 ` jparmele at wildbear dot com
2009-11-16 19:05 ` ubizjak at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).