有关在C'不是说明书“优化? [英] Regarding optimization for 'not a statment' in c?

查看:147
本文介绍了有关在C'不是说明书“优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然学习的编译器优化,我在 C 的Linux 写codeS与 GCC 版本 gcc版本4.4.5(Ubuntu的/ Linaro的4.4.4-14ubuntu5.1)结果
要understant 不是一个声明(NOP)的C.我写了两codeS第一 YC 第二个 XC 生成自己编译的程序集code 使用 GCC -S 选项​​。

While Learning compiler Optimisation, I write codes in C under Linux with GCC version gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5.1)
To understant not a statement (nop) in C. I written two codes first y.c second x.c and generate their compiled assembly code using gcc -S option.

拳code y.c

desktop:~$ cat y.c
main()
{
int i=0;
}
desktop:~$ gcc -S y.c
desktop:~$ 

二code x.c

desktop:~$ cat x.c
main()
{
int i=0;

/* Loops and if*/
while(0);
for(;0;);
if(0);

/* Arithmetic Operations */
i * i;
i / i;
i - i;
i + i;
i % i;
 +i;
 -i;

/* Comparison operators */
i == i;
i != i;
i < i;
i > i;
i >= i;
i <= i;

/* Bitwise Operations*/
i ^ i;
i | i;
i & i;
 ~i;
i << i;
i >> i;

/* Address Operatins*/ 
 &i;
 *(&i);
 (&i)[i];

/* Ternary conditional operation*/
 i? i : i;

/* Other Operations*/ 
 sizeof(i);
 (char)i;

/* Not-Logical Operation*/ 
 !i; 

/* Logical AND , OR Operators */ 
// i && i;  // Commented && operation 
// i || i;  // Commented || operation
}
desktop:~$ gcc -S x.c

注意:这次在 x.c 最后两行被注释结果。
正如我所期待的。有一个在其生成的汇编codeS没有区别。我比较 x.s y.s 使用差异命令。

NOTICE: This time Last two lines in x.c are commented.
And as I was expecting. There is no difference in their generated assembly codes. I compared x.s and y.s using diff command.

desktop:~$ diff x.s y.s
1c1
<   .file   "x.c"
---
>   .file   "y.c"
desktop:~$

不过,当我取消最后一个(或说加)在 x.c 两行。

i && i;  
i || i; 

再编译-S选项x.c和Y.S.比较

Again compile x.c with -S option and compared with y.s.

desktop:~$ tail  x.c  
 sizeof(i);
 (char)i;

/* Not-Logical Operation*/ 
 !i; 

/* Logical AND , OR Operators */ 
i && i;  // unCommented && operation 
i || i;  // unCommented || operation
}
desktop:~$ gcc -S x.c
desktop:~$ diff x.s y.s
1c1
<     .file    "x.c"
---
>     .file    "y.c"
10,21d9
<     movl    -4(%ebp), %eax
<     testl    %eax, %eax
<     je    .L3
<     movl    -4(%ebp), %eax
<     testl    %eax, %eax
< .L3:
<     movl    -4(%ebp), %eax
<     testl    %eax, %eax
<     jne    .L8
<     movl    -4(%ebp), %eax
<     testl    %eax, %eax
< .L8:
desktop:~$ 

:结果
我只是不明白,为什么前pressions 我||我 I和;&安培;我并不等同于'不是一个声明

Question:
I just can not understand why expressions i || i and i && i are not equivalent to 'not a statement'?

为什么编译器传递这两个语句转换为可执行(我们可以用 objdump的拆卸将得到同样的code)。什么是这两位前pression特别。他们不包括 = 操作。

Why compiler transfer this two statements into executable( we can disassemble with objdump will get same code). What is special in this two expression. they do not includes = operation.

他们是否改变(设置/重置)CPU标志寄存器?我想没有!

Does they change (set/reset) CPU flag-registers ? I guess no!

即使 / 除法运算丢弃,可能会导致被零故障的划分。

Even / division operation discarded that may cause an divided by zero fault.

修改:新增答案

有什么特别的我||我 I和;&安培;我前pressions。两者都相当于
不是一条语句。并且可以通过 GCC编译器有一些额外的努力被删除。

There is nothing special in i || i and i && i expressions. Both are equivalent to NOT A STATEMENT. And can be removed by GCC compiler with some extra effort.

要删除此: -o2 -03 标志是有用的:结果
这里是我的尝试!

To remove this: -o2 and -o3 flags are useful:
Here is my Try!!

desktop:~$ gcc -o2 -S y.c 
desktop:~$ gcc -o2 -S x.c 
desktop:~$ diff x.s y.s -y
    .file   "x.c"                         |     .file   "y.c"
    .text                               .text
    .p2align 4,,15                        <
.globl main                         .globl main
    .type   main, @function                     .type   main, @function
main:                               main:
    pushl   %ebp                            pushl   %ebp
    movl    %esp, %ebp                      movl    %esp, %ebp
    popl    %ebp                          |     subl    $16, %esp
                                  >     movl    $0, -4(%ebp)
                                  >     leave

多余的线条在RHS是因为文件之间的错位。

The extra lines in at RHS are because of misalignment between files.

我也想补充信息 JAVA C#编译器放弃这个前pressions不带任何标志。

I also like to add information that JAVA and C# compilers discard this expressions without any flags.

推荐答案

使用 -O2 启用优化,你应该可以看到额外的code消失。

Enable optimizations using -O2, and you should see the extra code disappear.

这篇关于有关在C'不是说明书“优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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