This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug target/30315] optimize unsigned-add overflow test on x86 to use cpu flags from addl



------- Comment #11 from raeburn at raeburn dot org  2007-08-18 17:55 -------
Snapshot gcc-4.3-20070817 seems to do just fine with the sample code I
supplied.

Though perhaps I should've given a bigger test case... :-)

One of the places where I'd want to check for overflow is in computing memory
allocation sizes (hence my filing #30314 at the same time, and wanting a
carry-flag check there too).  This patch lets gcc check the carry flag after
addition, which is great.  However, it seems to prevent gcc from being able to
look at the zero flag afterwards.  As in, "if the allocation size is zero, ask
for one byte so malloc won't return a null pointer":

void *foo(unsigned a, unsigned b) {
    unsigned sum = a + b;
    if (sum < a) return 0;
    if (sum == 0) sum = 1;
    return malloc(sum);
}

Without the overflow check, gcc just uses the zero flag resulting from the
addition.  With the overflow check in there, gcc uses the carry flag for the
overflow check, but then re-tests the value to see if it's zero:

        addl    %eax, %edx
        jb      L3 ; L3 just returns null
        testl   %edx, %edx
        movl    $1, %eax
        cmove   %eax, %edx

Still, this is better than doing an explicit comparison for the overflow check
and then the test for zero.


-- 


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]