C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about optimization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Sat Dec 17, 2005 10:48 am    Post subject: Question about optimization Reply with quote



Hello,

given the following code

int f( int i );

int g( int i )
{
return f( i ) * 0;
}

is the compiler allowed to optimize away the call to f(), thus turning g()
into

int g( int i )
{
return 0;
}

This would prevent all side effects of calling f().

--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
bestbrain@gmail.com
Guest





PostPosted: Mon Dec 19, 2005 10:26 am    Post subject: Re: Question about optimization Reply with quote



I think compiler should not.
There is no way for compiler to find out if I am doing any business
processing in f()



Matthias Hofmann wrote:
Quote:
Hello,

given the following code

int f( int i );

int g( int i )
{
return f( i ) * 0;
}

is the compiler allowed to optimize away the call to f(), thus turning g()
into

int g( int i )
{
return 0;
}
I
This would prevent all side effects of calling f().

--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ivan Vecerina
Guest





PostPosted: Mon Dec 19, 2005 10:41 am    Post subject: Re: Question about optimization Reply with quote



"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> wrote

: Hello,
:
: given the following code
:
: int f( int i );
:
: int g( int i )
: {
: return f( i ) * 0;
: }
:
: is the compiler allowed to optimize away the call to f(), thus turning
g()
: into
:
: int g( int i )
: {
: return 0;
: }
:
: This would prevent all side effects of calling f().

No, the compiler is not allowed to do so.
The compiler cannot assume that the return-value of
a function is its only relevant output, it has to
assume that other side-effects are being relied on.

The compiler only would be allowed to suppress the call
to f() if it can prove that the call to f() has no side
effects.


Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
ThosRTanner
Guest





PostPosted: Mon Dec 19, 2005 12:49 pm    Post subject: Re: Question about optimization Reply with quote


Matthias Hofmann wrote:
Quote:
Hello,

given the following code

int f( int i );

int g( int i )
{
return f( i ) * 0;
}

is the compiler allowed to optimize away the call to f(), thus turning g()
into

int g( int i )
{
return 0;
}
I don't think it is, because if f had side effects (like, say, output),

you wouldn't be able to see them if the function was optimised away.
And the optimiser isn't allowed to do that because it makes the
behaviour of the virtual machine different.

Under the circumstances, it could however optimise it to:
int g(int i) { f(i); return 0 };

Quote:
This would prevent all side effects of calling f().
The above gives the maximum optimisation capable - i.e. it optimises

out the multiply. Of course, if the compiler has f in scope, and it has
no side effects, then it could optimise it out.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maciej Sobczak
Guest





PostPosted: Mon Dec 19, 2005 12:53 pm    Post subject: Re: Question about optimization Reply with quote

Ivan Vecerina wrote:

Quote:
The compiler only would be allowed to suppress the call
to f() if it can prove that the call to f() has no side
effects.

Which can be done for example as a subsequent step after inlining the
call - which in turn can be done even without asking. Granted, separate
compilation can make this more difficult for the compiler, but linkers
supporting global program optimization are around as well.


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.