This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug target/30315] optimize unsigned-add overflow test on x86 to use cpu flags from addl
- From: "raeburn at raeburn dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 18 Aug 2007 17:55:52 -0000
- Subject: [Bug target/30315] optimize unsigned-add overflow test on x86 to use cpu flags from addl
- References: <bug-30315-11337@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- 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