a question about code optimization

龙海涛 longhaitao@otsc.com.cn
Wed Jan 9 17:06:00 GMT 2008


John Love-Jensen 写道:
> Hi,
> 
>> So my questions is:
>> is gcc *smart* enough that she can determine when to do the optimization?
> 
> Yes.
> 
> GCC can do the optimization when the optimizer has sufficient information to
> be certain that there are no side effects.
> 
> GCC can not do the optimization when there is a black-box routine involved.
> Although there may be some aspects outside of the black-box routine that may
> be optimized.
> 
> There is another compiler, LLVM, that does holistic optimizations.  If I
> recall correct, there is an effort to add (or perhaps merely to investigate
> adding) holistic optimizations into GCC.
thanks for your patient answer :)

another question:

--------code begin----------------
vector<int> a;
...
for(int i = 0; i < a.size(); i++)
{
	/*do something*/
}
-------code end---------------------

i asked that if this can be optimized to this:
----------code begin--------------
vector<int> a;
...
int size = a.size();
for(int i = 0; i < size; i++)
{
	/*do something*/
}
----------code end-----------------

last time we discussed one situation may influence the optimization:

a) in function size() we may change some global variables.

there is another situation:

b) in the /*do something*/ block, we may use a.push_back(...) or some other functions
that can change the size of a. if this happen, of course we can not do the optimization.

so can gcc know what functions may change the size of a?
is gcc smart enough to optimize the code if those functions are not used?

i am sorry because i am not familiar with the optimization term.


> 
> --Eljay
> 



More information about the Gcc-help mailing list