Re: Wrong optimisation in Visual C++.net Release Build

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Michael K. O'Neill (MikeAThon2000_at_nospam.hotmail.com)
Date: 02/04/05


Date: Fri, 4 Feb 2005 13:22:32 -0800

It's not a compiler bug; it's bad code that relies on undefined behavior.

The C++ standard is very clear in not specifying any particular order for
evaluation of arguments for a function call. In fact, function arguments
may generally be evaluated in any order, including interleaved. That's one
reason why you see things like __stdcall and __cdecl (which are attempts to
define ordering).

So, your code is relying on behavior that's simply undefined. You can't
blame the compiler.

See this "Got-cha of the Week" for one of nearly countless articles that
explain this: http://www.gotw.ca/gotw/056.htm

Mike

"Steffen Bendel" <SteffenBendel@discussions.microsoft.com> wrote in message
news:707128AF-962D-4D40-BA23-80A87ED4466D@microsoft.com...
> Cool, thank you.
> There is no need for a workaround, because I do not use this code. (It was
> written for a test 3 years ago. And I was wondering, why my random colors
are
> ever grey). so I will test in in one year again.
>
> "Larry Brasfield" wrote:
>
> > "Steffen Bendel" <SteffenBendel@discussions.microsoft.com> wrote in
> > message news:ED807FE4-7BBD-43BD-8103-E7E856929DFB@microsoft.com...
> > > for the above testing code I created a new Win32 Console Project and
used the
> > > default settings.
> > >
> > > Next try. Now it should compile! (I had forgotten to insert the
includes
> > > coming from the automatically created stdfx.h)
> > >
> > > #include <iostream>
> > > #include <tchar.h>
> > >
> > > int getvalue()
> > > {
> > > static int value=0;
> > >
> > > value += 1;
> > > if (value > 100)
> > > value-=100;
> > >
> > > return(value);
> > > }
> > >
> > > void print(int f1, int f2, int f3)
> > > {
> > > printf("%d %d %d", f1, f2, f3);
> > > }
> > >
> > > int _tmain(int argc, _TCHAR* argv[])
> > > {
> > > print(getvalue(), getvalue(), getvalue());
> > > while(true) {}
> > > return 0;
> > > }
> >
> > Here is the assembler output with /Ox (full) optimization:
> > ========= begin excerpt =====================
> > PUBLIC ?getvalue@@YAHXZ ; getvalue
> > ; COMDAT ?value@?1??getvalue@@YAHXZ@4HA
> > ; File c:\documents and settings\larry\my documents\visual studio
projects\fff\f
> > _BSS SEGMENT
> > ?value@?1??getvalue@@YAHXZ@4HA DD 01H DUP (?) ;
`getvalue'::`2'::value
> > ; Function compile flags: /Ogty
> > _BSS ENDS
> > _TEXT SEGMENT
> > ?getvalue@@YAHXZ PROC NEAR ; getvalue
> >
> > ; 8 : static int value=0;
> > ; 9 :
> > ; 10 : value += 1;
> >
> > mov eax, DWORD PTR ?value@?1??getvalue@@YAHXZ@4HA
> > inc eax
> >
> > ; 11 : if (value > 100)
> >
> > cmp eax, 100 ; 00000064H
> > mov DWORD PTR ?value@?1??getvalue@@YAHXZ@4HA, eax
> > jle SHORT $L13083
> >
> > ; 12 : value-=100;
> >
> > sub eax, 100 ; 00000064H
> > mov DWORD PTR ?value@?1??getvalue@@YAHXZ@4HA, eax
> > $L13083:
> >
> > ; 13 :
> > ; 14 : return(value);
> > ; 15 : }
> >
> > ret 0
> > ?getvalue@@YAHXZ ENDP ; getvalue
> > _TEXT ENDS
> > ========= end excerpt =====================
> >
> > Notice how the conditional branch bypasses saving eax
> > back into 'value', even though that should always be done.
> > This is clearly a bug. This occurred also with the /Ot (time)
> > optimazation, but not with the /Os (space) optimization.
> >
> > The bug is absent in the current beta compiler, so I do not
> > see much point in posting a bug report. The fix might be
> > fixed in a service release of the v7.1 product, someday.
> >
> > Meanwhile, I suggest you use /Os optimization, at least
> > for that function. That is often a better setting for whole
> > programs, even from a speed standpoint once cache
> > (or pagefile) effects are fully operative.
> >
> > --
> > --Larry Brasfield
> > email: donotspam_larry_brasfield@hotmail.com
> > Above views may belong only to me.
> >
> >
> >



Relevant Pages

  • Re: is this valid c or a compiler bug?
    ... >Windows VC 6.0 compiler handles it correctly. ... >So is this a compiler bug, ... When the language doesn't specify an evaluation order ... Dan Pop ...
    (comp.lang.c)
  • Re: /Od compiler option problem
    ... > would work properly but when it was not set the bug would appear. ... /Od disables optimization. ... There's a compiler bug in the optimizer. ... determine if it's a compiler bug or if you're relying on undefined behavior. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: what is the output of this program?
    ... like a feature than a bug to me. ... cases where undefined behavior can be repaired). ... There are plenty of constructs whose behavior the standard doesn't ... I certainly have no problem with a compiler ...
    (comp.lang.c)
  • Re: Alternate sum mistake
    ... There's at least one more bug in your code, a bug that a C test ... but i cannot spot it at once... ... It's not just that the order of evaluation is unspecified, ...
    (comp.lang.c)
  • Re: Alternate sum mistake
    ... There's at least one more bug in your code, a bug that a C test ... but i cannot spot it at once... ... It's not just that the order of evaluation is unspecified, ...
    (comp.lang.c)