Re: C/C++ Compiler's Optimization Failed

From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/21/04


Date: Sat, 21 Feb 2004 04:36:58 +0000 (UTC)

On Sat, 21 Feb 2004 01:18:24 +0000 (UTC), "Bryan Parkoff"
<bryan.nospam.parkoff@nospam.com> wrote in comp.lang.asm.x86:

> I wrote C++ code below.
>
> VOID Set_Red(U_BYTE _Red, U_BYTE _Green, U_BYTE _Blue)
> {
> Red = (0x00 << 24) | (_Red << 16) | (_Green << 8) | _Blue;
> }

Of course the code you wrote happens to be illegal C++, but I don't
suppose you care about that.

> I used C/C++ Compiler's Optimization. The machine language shows below.
>
> mov ecx, DWORD PTR [esp+0ch]
> xor eax, eax
> mov ah, BYTE PTR [esp+04h]
> and ecx, 0ffh
> mov al, BYTE PTR [esp+08h]
> shl eax, 08h
> or eax, ecx
> mov DWORD PTR [0409500h], eax
> ret
>
> I am shocked that C/C++ Compiler did not tune optimization very well
> because partial register DOES EXIST!!

I am shocked that all you have ever done since starting to post to
usenet is complain. Intel's processors aren't good enough, the C and
C++ languages weren't designed to meet your idea of what they should
be, nobody's compiler is good enough for you.

If you don't like the code generated by that compiler, try a different
one. If you don't like the code generated by any compiler, write your
own or do without.

If you don't like Intel's processors, develop your own.

And why are you complaining in an assembly language newsgroup about
the code generated by a compiler? C and C++ compilers are really
off-topic here, except perhaps for how to use their inline assembly
support or how to link their code with assembly language code.
Whatever C and C++ compilers do for C and C++ code is not an issue
that belongs here.

> My correction code should be below.
>
> mov eax, DWORD PTR [esp+04h]
> shl eax, 08h
> or eax, DWORD PTR [esp+08h]
> shl eax, 08h
> or eax, DWORD PTR [esp+0ch]
> ret
>
> I am very concerned that all C/C++ Compiler does not do good job to tune
> optimization properly. I used Intel VTune to detect and discover many hot
> spots that they do include partial register. I did test on both Pentium III
> and Pentium IV. They do apply partial register that should be avoided.

Frankly, that is not the job or a C or C++ compiler.

> What is your recommendation?

My recommendation is that you quit complaining about everything.
Write your own compiler, then you can blame yourself when it misses
something.

> 1. Write C/C++ code in DEBUG version.
> 2. Test C/C++ code on any computer to see if it works with NO BUG
> (Optimization is not important YET!!)
> 3. Change from DEBUG version to RELEASE version.
> 4. Test C/C++ code on any computer to see if it works with NO BUG
> (Optimization is ENABLED!!)
> 5. Use VTune to discover hot spots including partial register.
> 6. Convert C/C++ code into machine language in your own hands manually.
> 7. Link your existing C/C++ code and Machine code together.
> 8. Test mixed C/C++ code and Machine code on any computer to see if it
> works with NO BUG.
> 9. After all tests are done, it will be big IMPACT for improved
> optimization.

Improved optimization of WHAT???

What application have you written that performed correctly and only
failed to meet its requirements because it executed too slowly?

Many assembly language programmers are interested in wringing the
maximum performance out of a processor, that is usually the reason
they program in assembly.

If C and C++ compilers do not do a good enough job for you, then don't
use them. Stop complaining about compilers unless you are going to
write your own.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: C/C++ Compilers Optimization Failed
    ... I used C/C++ Compiler's Optimization. ... >> xor eax, eax ... I am shocked that C/C++ Compiler did not tune optimization very well ...
    (comp.lang.asm.x86)
  • Re: missing optimization?
    ... Both, function1 and function2, have duplicated ... > movl _data, %eax ... > subl 4, %eax ... > If compiler is *newer* than 2.95, then it puts the TEST opcode. ...
    (comp.os.msdos.djgpp)
  • Re: compiler generated output
    ... > compiler to generate code which only uses 386 instruction). ... > Thus, while replacing mov/and with movzx, the jump is still there. ... mov eax, ... Software Optimization Guide for ...
    (comp.lang.asm.x86)
  • Re: No need to optimize in assembly anymore
    ... > inlining etc. - precisely the ones that a good compiler will perform. ... Now in MSVC 7.1 it generates a single div. ... mov eax, DWORD PTR _b$ ...
    (comp.lang.asm.x86)
  • Re: No need to optimize in assembly anymore
    ... > inlining etc. - precisely the ones that a good compiler will perform. ... Now in MSVC 7.1 it generates a single div. ... mov eax, DWORD PTR _b$ ...
    (alt.lang.asm)