suboptimal code-gen of decrement in GCC 4.2.1
suboptimal code-gen of decrement in GCC 4.2.1
- Subject: suboptimal code-gen of decrement in GCC 4.2.1
- From: Jens Alfke <email@hidden>
- Date: Wed, 14 Oct 2009 12:51:37 -0700
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.
For example, for an inline 'deref' method that looks like this:
void deref() { if (--m_refCount == 0) delete this; }
the generate code at the call site looks like:
movl (ëx), êx
decl êx
movl êx, (ëx)
testl êx, êx
jne L2
movl ëx, (%esp)
call __ZN10RefCountedD1Ev
movl ëx, (%esp)
call __ZdlPv
L2: ...
The first four instructions represent the "if" test. But with my very
limited x86 skills, it seems that they could be replaced by
decl (ëx)
jne L2
because the dec instruction sets the Z flag.
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?
—Jens
PS: Here's the code I compiled. This is with GCC 4.2.1 (build 4474) on
10.5.8.
// Compile with: gcc-4.2 -O2 -S refcounted.cpp
class RefCounted {
public:
RefCounted() :m_refCount(1) { }
void ref() { ++m_refCount; }
void deref() { if (--m_refCount == 0) delete this; }
private:
~RefCounted();
unsigned m_refCount;
};
int main() {
RefCounted *a = new RefCounted(), *b = new RefCounted();
a->ref();
b->deref();
return 0;
}
_______________________________________________
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