implicit QChar constructors

Oswald Buddenhagen kde-optimize@mail.kde.org
Wed, 22 Jan 2003 01:18:23 +0100


On Wed, Jan 22, 2003 at 12:15:35AM +0100, Dirk Mueller wrote:
> However, for reasons beyond me, gcc pessimized the code by not
> loop-eliminating this construction:
> 
>  movw·   $32, -24(%ebp)
> 
> This is the implicit QChar constructor. There is absolutely no need to
> have it in the loop. Appears to be a bug in gcc to me. 
> 
yes, gcc (both 2.95 and 3.2.x) does this all over the place. seems
pretty braindead to me, considering that optimizing this away should be
almost trivial.
however, performance does not suffer from it; the code just gets
bigger. both the instruction decoding and the dead store are done in
parallel with the "real" instructions on any modern processor.

> So I was curious and used "= space" instead. Here is the output: 
> 
> .L21:
>     ·       movl·   -24(%ebp), %eax
>     ·       movw·   %ax, (%ecx,%edx,2)
>     ·       incl·   %edx
>     ·       cmpl·   $9, %edx
>     ·       jbe·    .L21
> 
> 
> As you can see, the actual code is theoretically worse, because it
> ends up in a memory read and a sequential memory write. This can
> hardly be faster in the average case. 
> 
i think it won't be slower, either, as the processors have a shortcut
path for the store with following load. there are some potential
pipeline stalls related to similar situations, but i don't think such a
simple case is impacted.
still it seems rather braindead ... bcc 3.1 optimized such trivial
things better - 12 years ago ...

you would not believe what gcc makes of complex boolean expressions ...


just an optimization hint, which is often applicable: if you are
_certain_ that a position does not exceed a qstring's length, don't use
[], but .unicode()[] instead:
for (i = 0; i < str.lenght() /* inlined */; i++) {
  QChar c = str.unicode()[i]; // str[i] repeats the i < length() check
                              // and gcc is unable to optimize it away
...
}


greetings

-- 
Hi! I'm a .signature virus! Copy me into your ~/.signature, please!
--
Chaos, panic, and disorder - my work here is done.