Re: suboptimal code-gen of decrement in GCC 4.2.1
Re: suboptimal code-gen of decrement in GCC 4.2.1
- Subject: Re: suboptimal code-gen of decrement in GCC 4.2.1
- From: Alastair Houghton <email@hidden>
- Date: Wed, 14 Oct 2009 22:07:42 +0100
On 14 Oct 2009, at 20:51, Jens Alfke wrote:
I'm looking at the x86 code GCC 4.2 generates for a typical
reference-counting operation (with -O2 or -Os), and it seems
strangely sub-optimal. Specifically, it seems to be ignoring the
fact that arithmetic operations set the condition flags, so it
redundantly tests the results.
Well...
Given how ubiquitous constructs like "if(--x) ..." are, it seems
strange that GCC ignores this. Or is there some weird irregularity
of the instruction set that makes my suggestion impossible?
Not impossible, no, but there *is* a weird irregularity with DEC and
INC, namely that they don't touch the carry flag. That, of course,
creates a dependence on previous values of the flags register, which
means that testing the flags after a DEC or INC is potentially
expensive (it could force the processor to wait for anything that
might have affected the flags to complete).
The Intel Architecture Optimization Manual actually suggests that INC
and DEC should be replaced with ADD or SUB instructions for this very
reason.
My guess (and it is just a guess) is that GCC is generating the TEST
instruction to work around this problem in a different way by
resetting the flags register before testing it.
As for the other instructions, some of them do seem wasteful, I agree.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden