编译器代码优化:请参阅下面的代码 [英] Compiler code optimization: see code below

查看:121
本文介绍了编译器代码优化:请参阅下面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些C用于嵌入式环境,代码

需要进行优化。我有一个关于优化编译器的问题

general。我正在使用GCC作为工作站和Diab编译器用于

嵌入式目标。


我的问题是关于编译器如何优化某些代码序列。 />

例如,请使用下面的代码。编译器是否会在生成的目标代码中消除对foo()的实际函数调用,只需将
存储在temp中的值''3'(显然在堆栈的某处) )?我认为

基于我对GCC的实验,值''3'实际上不会被存储

因为它显然没有在任何地方使用。我的测试表明

没有在目标代码中进行函数调用,也没有任何

值存储在temp中。


int main(){

int temp;


temp = foo(3);


返回0 ;

}


int foo(int x){

返回x;

}


现在作为一般性问题,如果我在我的代码中有函数调用

我在传递给函数的参数在编译时是已知的,<这些函数调用会被编译器消除吗?


我想因为我没有很多编译器的背景我不是

确定一个好的优化编译器可以优化掉什么。如果你有的话,请

指向我对这个主题的任何引用。

谢谢。

I''m writing some C to be used in an embedded environment and the code
needs to be optimized. I have a question about optimizing compilers in
general. I''m using GCC for the workstation and Diab compiler for the
embedded target.

My question is about how compilers optimize certain code sequences.

As an example, take the code below. Will the compiler eliminate the
actual function call to foo() in the object code generated and just
store the value ''3'' in temp(obviously somewhere on stack)? I think
based on my experiments with GCC the value ''3'' actually wont'' be stored
at all since it''s obviously not being used anywhere. My tests showed
that no function call is made in the object code and neither is any
value being stored in temp.

int main() {
int temp;

temp = foo(3);

return 0;
}

int foo(int x) {
return x;
}

Now as a general question, if I have function calls in my code in which
the arguments I am passing to the function are known at compile time,
will these function calls be eliminated by the compiler?

I guess since I don''t have much of a background in compilers I''m not
sure what exactly a good optimizing compiler can optimize away. Please
point me to any references on this topic as well if you have some.
Thanks.

推荐答案



joshc写道:


joshc wrote:
我正在写一些C用于嵌入式环境并且代码需要进行优化。我有一个关于优化编译器的问题。我正在使用GCC作为工作站和Diab编译器用于
嵌入式目标。

我的问题是编译器如何优化某些代码序列。

例如,请使用下面的代码。编译器是否会在生成的目标代码中消除对foo()的实际函数调用,并且只是将值3存储在temp中(显然在堆栈的某处)?我认为
基于我对GCC的实验,3的值实际上根本不会存储,因为它显然没有在任何地方使用。我的测试显示
没有在目标代码中进行函数调用,也没有任何值存储在temp中。

int main(){
int temp ;返回0;


int foo(int x){
return x;
}

现在作为一般性问题,如果我在我的代码中有函数调用,那么我在传递给函数的参数在编译时是已知的,
这些函数调用会被编译器消除吗?

我想因为我在编译器中没有太多的背景我不确定究竟到底是什么一个好的优化编译器可以优化。如果你有的话,请指点我对这个主题的任何引用。
谢谢。
I''m writing some C to be used in an embedded environment and the code
needs to be optimized. I have a question about optimizing compilers in
general. I''m using GCC for the workstation and Diab compiler for the
embedded target.

My question is about how compilers optimize certain code sequences.

As an example, take the code below. Will the compiler eliminate the
actual function call to foo() in the object code generated and just
store the value ''3'' in temp(obviously somewhere on stack)? I think
based on my experiments with GCC the value ''3'' actually wont'' be stored
at all since it''s obviously not being used anywhere. My tests showed
that no function call is made in the object code and neither is any
value being stored in temp.

int main() {
int temp;

temp = foo(3);

return 0;
}

int foo(int x) {
return x;
}

Now as a general question, if I have function calls in my code in which
the arguments I am passing to the function are known at compile time,
will these function calls be eliminated by the compiler?

I guess since I don''t have much of a background in compilers I''m not
sure what exactly a good optimizing compiler can optimize away. Please
point me to any references on this topic as well if you have some.
Thanks.




编译器可以做任何想做的事情因为它生成了一个工作的
二进制文件。

不同的编译器(或不同的版本)可能会做不同的事情。

编译器文档应该告诉你它可以做什么类型的优化。



The compiler can do whatever it wants as look as it generates a working
binary.
different compilers (or different versions) may do different things.
The compiler Docs should tell you what types of optimization it can do.


2005年1月12日19:59:05 -0800,joshc <乔******** @ gmail.com>写在

comp.lang.c:
On 12 Jan 2005 19:59:05 -0800, "joshc" <jo********@gmail.com> wrote in
comp.lang.c:
我正在写一些C用于嵌入式环境并且代码需要
要优化。我有一个关于优化编译器的问题。我正在使用GCC作为工作站和Diab编译器用于
嵌入式目标。

我的问题是编译器如何优化某些代码序列。


C标准没有定义编译器如何优化某些代码

序列,也不要求它们这样做。编译器以及调用它的选项完全取决于



例如,请使用下面的代码。编译器是否会在生成的目标代码中消除对foo()的实际函数调用,并且只是将值3存储在temp中(显然在堆栈的某处)?我认为
基于我对GCC的实验,3的值实际上根本不会存储,因为它显然没有在任何地方使用。我的测试显示
没有在目标代码中进行函数调用,也没有任何值存储在temp中。

int main(){
int temp ;返回0;


int foo(int x){
return x;
}

现在作为一般性问题,如果我在我的代码中有函数调用,那么我在传递给函数的参数在编译时是已知的,
这些函数调用会被编译器消除吗?

我想因为我在编译器中没有太多的背景我不确定究竟到底是什么一个好的优化编译器可以优化。如果你有的话,请指点我对这个主题的任何引用。
谢谢。
I''m writing some C to be used in an embedded environment and the code
needs to be optimized. I have a question about optimizing compilers in
general. I''m using GCC for the workstation and Diab compiler for the
embedded target.

My question is about how compilers optimize certain code sequences.
The C standard does not define how compilers optimize certain code
sequences, nor does it require them to do so. It is entirely up to
the compiler and the options with which you invoke it.
As an example, take the code below. Will the compiler eliminate the
actual function call to foo() in the object code generated and just
store the value ''3'' in temp(obviously somewhere on stack)? I think
based on my experiments with GCC the value ''3'' actually wont'' be stored
at all since it''s obviously not being used anywhere. My tests showed
that no function call is made in the object code and neither is any
value being stored in temp.

int main() {
int temp;

temp = foo(3);

return 0;
}

int foo(int x) {
return x;
}

Now as a general question, if I have function calls in my code in which
the arguments I am passing to the function are known at compile time,
will these function calls be eliminated by the compiler?

I guess since I don''t have much of a background in compilers I''m not
sure what exactly a good optimizing compiler can optimize away. Please
point me to any references on this topic as well if you have some.
Thanks.




新闻:comp.compilers可能是一个很好的参考什么一个好的

优化编译器可以优化远离,但这不一定会对你有任何好处。另请注意,问题在这里是偏离主题的,因为

优化始终是一个实现问题,而不是语言问题。


简单的事实是,如果重要的是你知道你的b $ b编译器对你的代码做了什么,然后用你的编译器编译你的代码

并检查生成的目标代码或其汇编语言

等价物。这将为您提供准确的答案,而不是编译器可能会做什么的假设指示。


使用特定编译器(如gcc)进行优化会有很大差异

版本,目标体系结构和编译器选项。


C标准既没有定义优化也没有效率。

只有严格符合程序的可观察输出。从源代码生成Perl脚本的

实现和

调用Perl解释器来执行脚本可能是严格的符合C的b
实现如果Perl

脚本的可观察输出是正确的。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

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/~a...FAQ-acllc.html



news:comp.compilers would probably be a good reference to what "a good
optimizing compiler can optimize away", but that won''t necessarily do
you any good at all. Note also that the issue is off-topic here, as
optimization is always an implementation issue, not a language issue.

The simple fact is that if it is important to you know what your
compiler does to your code, then compile your code with your compiler
and examine the generated object code or its assembly language
equivalent. That will give you exact answers, as opposed to
hypothetical indications of what a compiler might do.

Optimization with a specific compiler like gcc will vary greatly with
version, target architecture, and compiler options.

Neither optimization nor efficiency is defined by the C standard.
Only the observable output of a strictly conforming program. An
implementation that generated a Perl script from your source code and
invoked a Perl interpreter to execute the script could be a strictly
conforming C implementation if the observable output of the Perl
script was correct.

--
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/~a...FAQ-acllc.html


joshc写道:
joshc wrote:
我正在写一些C用于嵌入式环境
并且代码需要优化。
我对优化编译器有一个疑问。我正在为工作站使用GCC和嵌入式目标的Diab编译器。
我的问题是关于编译器如何优化某些代码序列。
例如,请使用下面的代码。
编译器是否会在[emit]对象代码中消除对foo()的实际函数调用
并且只存储值'温度为'3''(显然是堆栈中的某个地方)?
我认为,根据我对GCC的实验,值''3'实际上根本不存储
因为它显然没有在任何地方使用。
我的测试显示在目标代码中没有进行任何函数调用
并且没有任何值存储在temp中。
int main(int argc,char * argv []){
int temp = foo(3);
返回0;
}

int foo(int x){
返回x;
}
现在作为一般性问题,如果我在我的代码中有函数调用
我的论点传递给函数
在编译时已知,这些函数调用会被编译器消除吗?


是的,如果编译器可以看到函数定义。

为了安全起见,你应该写一下


inline static

int foo(int x){

返回x;

}


int main(int argc,char * argv []){

int temp = foo(3);

// stuff

return 0;

}


它也适用于标准数学函数:

cat fc
#include< math.h>


double f(双x){

返回sqrt(2.0);

}

gcc -Wall -std = c99 -pedantic -O2 -S fc
cat fs
.file" f.c"

.section .rodata.cst8 ,aM,@ progbits,8

.align 8

.LC0:

。长1719614413

.long 1073127582

.text

.p2align 4 ,,,15

.globl f

.type f ,@function

f:

pushl%ebp //多余的

movl%esp,%eb p //多余的

popl%ebp //多余的

fldl .LC0

ret

.size f, 。-f

.section .note.GNU-stack,",@ progbits

.identGCC:(GNU)3.4.1"


LC0上的一对长数据字代表sqrt(2.0)。

我想[那]因为我没有太多的背景资料编译器,
我不确定一个好的优化编译器可以优化什么。


我也不确定。

优化始终处于编译技术的最前沿。

它是一个编译器开发人员彼此竞争的方式。

我总是测试我认为重要的优化。

有时,我的编译器无法执行我的优化期待

所以我运行基准来判断我的优化是否实际上导致更快的代码 - 他们通常不会。

例如,在上面的代码中,

似乎有三个多余的指令

但我怀疑函数f(double)会更快

如果我删除它们 - 它实际上可能会运行得更慢。

有时编译器会在代码中留下多余的指令

作为无操作来防止管道停滞

或正确对齐指令流,

请指出我对此主题的任何引用
I''m writing some C to be used in an embedded environment
and the code needs to be optimized.
I have a question about optimizing compilers in general.
I''m using GCC for the workstation
and Diab compiler for the embedded target. My question is about how compilers optimize certain code sequences. As an example, take the code below.
Will the compiler eliminate the actual function call to foo()
in the [emitted] object code
and just store the value ''3'' in temp(obviously somewhere on stack)?
I think [that], based on my experiments with GCC,
the value ''3'' actually won''t be stored at all
since it''s obviously not being used anywhere.
My tests showed that no function call is made in the object code
and neither is any value being stored in temp. int main(int argc, char* argv[]) {
int temp = foo(3);
return 0;
}

int foo(int x) {
return x;
}

Now as a general question, if I have function calls in my code
in which the arguments I am passing to the function
are known at compile time,
will these function calls be eliminated by the compiler?
Yes, if the function definition is visible to the compiler.
To be safe, you should probably write

inline static
int foo(int x) {
return x;
}

int main(int argc, char* argv[]) {
int temp = foo(3);
// stuff
return 0;
}

It should also work for standard math functions:
cat f.c #include <math.h>

double f(double x) {
return sqrt(2.0);
}
gcc -Wall -std=c99 -pedantic -O2 -S f.c
cat f.s .file "f.c"
.section .rodata.cst8,"aM",@progbits,8
.align 8
.LC0:
.long 1719614413
.long 1073127582
.text
.p2align 4,,15
.globl f
.type f, @function
f:
pushl %ebp // superfluous
movl %esp, %ebp // superfluous
popl %ebp // superfluous
fldl .LC0
ret
.size f, .-f
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.1"

The pair of long data words at LC0 represent sqrt(2.0).
I guess [that], since I don''t have much of a background in compilers,
I''m not sure exactly what a good optimizing compiler can optimize away.
I''m not sure either.
Optimization is always at the cutting edge of compiler technology.
It is one of the ways that compiler developers compete with each other.
I always test for the optimizations that I think are important.
Sometimes, my compiler fails to perform the optimization that I expect
so I run benchmarks to determine whether or not my optimization
actually result in faster code -- they usually don''t.
For example, in the above code,
there appear to be three superfluous instructions
but I doubt that the function f(double) would be any faster
if I removed them -- it might actually run slower.
Sometimes compilers leave superfluous instructions
in the code to serve as no-ops to prevent pipeline stalls
or to align the instruction stream properly,
Please point me to any references on this topic as well
if you have some.




我也会对任何这样的引用感兴趣。



I would be interested in any such references as well.


这篇关于编译器代码优化:请参阅下面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆