This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: a question about code optimization
- From: 龙海涛 <longhaitao at otsc dot com dot cn>
- To: John Love-Jensen <eljay at adobe dot com>, gcc-help at gcc dot gnu dot org
- Date: Thu, 03 Jan 2008 09:18:24 +0800
- Subject: Re: a question about code optimization
- References: <C3A0E1D1.29B03%eljay@adobe.com>
John Love-Jensen 写道:
> Hi,
>
> In this particular example, the size() method is inline, so the compiler
> injects it locally into the code, and hence the compiler is aware if size()
> changes some global variable or not.
>
> If size() were non-inline, it would be a "black box" to the optimizer, so in
> that case your analysis is correct.
>
> --Eljay
yes, size() function is inline, but consider this situation: we can call
some non-inline functions in inline function,i.e,
void foo_non_inline();
inline int foo_inline() {
/*...*/
foo_non_inline();
/*...*/
}
obviously we are not sure that the global variabls are changed or not in foo_non_inline();
So my questions is:
is gcc *smart* enough that she can determine when to do the optimization?
>
> On 12/21/07 11:41 PM, "龙海涛" <longhaitao@otsc.com.cn> wrote:
>
>> ---------code-----------
>> vector<int> a;
>> /*do somthing*/
>> for(int i = 0; i<a.size(); i++) {
>> /* do something*/
>> }
>> --------code end--------
>>
>> my question is:
>> is it possible that the compiler will transform the code to this:
>>
>> int __tmp = a.size();
>> for(int i = 0; i < __tmp; i++) {
>> /*do something*/
>> }
>>
>> i think it is impoossible,because in size() function, the programmer
>> can change some global variables, so the compiler could not do that.
>>
>> if gcc can, could you tell me how?
>>
>> i am sorry in advance because i am not familiar with the code optimization.
>> so i do not know how to search the gcc manual and gcc internal manual to find
>> answer.could anyone tell me how to find this in the manual?or at least give
>> some keywords?
>