关于gcc -O3优化的问题(bug或功能) [英] Question on gcc -O3 optimization (bug or feature)

查看:479
本文介绍了关于gcc -O3优化的问题(bug或功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下代码在gcc422(linux)上编译,带有 -

O3选项。我看到其中一个分支被丢弃,即使它不是一个死分支。这是一个错误还是一个功能?


#include< stdlib.h>

#include< stdio.h>


void show_issue(int number)

{

if(number< 0){

printf("%) d是负数\ n",number);

number = - number;

printf("转换后:%d \ n",数字) ;

if(number< 0){

/ * ################ with gcc422 -O3,this branch是
丢弃############ * /

printf(必须是INT_MIN,因为否定没有效果\ n);

}

}

else {

printf("%d是正数\ n,数量);

}

}

int main()

{

int i;

for(i = 30; i< 32; i ++){

show_issue(1<< i);

}

返回0;

}

Hi,

I have the following code that is compiled on gcc422 (linux) with -
O3 option. I see that one of the branch is discarded, even though it
is not a dead branch. Is this a bug or a feature?

#include <stdlib.h>
#include <stdio.h>

void show_issue(int number)
{
if( number < 0 ) {
printf("%d is a negative number \n", number);
number = - number;
printf("After conversion: %d\n", number);
if( number < 0 ) {
/* ################ with gcc422 -O3, this branch is
discarded ############ */
printf("Must be INT_MIN since negating has no effect\n");
}
}
else {
printf("%d is positive number\n", number);
}
}
int main()
{
int i;
for(i = 30; i < 32; i++ ) {
show_issue( 1 << i );
}
return 0;
}

推荐答案

文章< 66 ** ***************** *************** @ y22g2000prd。 googlegroups.com>,

compiler_newbie< bh **** @ gmail.comwrote:
In article <66**********************************@y22g2000prd. googlegroups.com>,
compiler_newbie <bh****@gmail.comwrote:

我有以下代码编译在gcc422(linux)上用 -
O3选项。我看到其中一个分支被丢弃了,即使它不是一个死分支。这是一个错误还是一个功能?
I have the following code that is compiled on gcc422 (linux) with -
O3 option. I see that one of the branch is discarded, even though it
is not a dead branch. Is this a bug or a feature?


> #include< stdlib.h>
#include< stdio.h>

void show_issue(int number)
{

if(number< 0){

printf("%d是负数\ n,数字);

数字= - 数字;

printf(转换后:%d \ n,数字);

if (数字< 0){

/ * ################与gcc422 -O3,这个分支被丢弃#### ######## * /

printf(必须是INT_MIN,因为否定无效);

}

}

else {

printf("%d是正数\ nn,数字);

}
}
>#include <stdlib.h>
#include <stdio.h>

void show_issue(int number)
{
if( number < 0 ) {
printf("%d is a negative number \n", number);
number = - number;
printf("After conversion: %d\n", number);
if( number < 0 ) {
/* ################ with gcc422 -O3, this branch is
discarded ############ */
printf("Must be INT_MIN since negating has no effect\n");
}
}
else {
printf("%d is positive number\n", number);
}
}



使用一元否定运算符的整数溢出结果

在C标准中是未定义的,因此编译器可以自由使用做任何事情

对于那种情况感觉就好。

-

对错误的研究不仅在最高程度上是预防的,而且它是一个刺激性的介绍

研究真相。 - Walter Lipmann

The result of integer overflow with the unary negation operator
is undefined in the C standard, so the compiler is free to do whatever
it feels like for that case.
--
"The study of error is not only in the highest degree
prophylatic, but it serves as a stimulating introduction to the
study of truth." -- Walter Lipmann


compiler_newbie< bh **** @ gmail.comwrites:
compiler_newbie <bh****@gmail.comwrites:

void show_issue (int number)

{

if(number< 0){

printf("%d是负数\ n) ;,数字);

number = - 数字;

printf(转换后:%d \ n,数字);

如果(数字< 0){

/ * ################与gcc422 -O3,这个分支是

丢弃############ * /
void show_issue(int number)
{
if( number < 0 ) {
printf("%d is a negative number \n", number);
number = - number;
printf("After conversion: %d\n", number);
if( number < 0 ) {
/* ################ with gcc422 -O3, this branch is
discarded ############ */



它被丢弃,因为有符号算术溢出产生

未定义的行为。


如果你想得到你期望的行为,你可以使用

-fwrapv选项:


`-fwrapv''

这个选项指示编译器假设加入,减法和乘法的带符号算术

溢出回合

运用二进制表示。此标志启用一些优化
并禁用其他优化。根据Java语言

规范的要求,此选项由Java前端的

默认启用。

It is discarded because signed arithmetic overflow yields
undefined behavior.

If you want to get the behavior that you expect, you can use the
-fwrapv option:

`-fwrapv''
This option instructs the compiler to assume that signed arithmetic
overflow of addition, subtraction and multiplication wraps around
using twos-complement representation. This flag enables some
optimizations and disables others. This option is enabled by
default for the Java front-end, as required by the Java language
specification.


printf("必须是INT_MIN因为否定没有效果\ nn);

}

}

else {

printf("%d是正数\ nn,数字);

}

}
printf("Must be INT_MIN since negating has no effect\n");
}
}
else {
printf("%d is positive number\n", number);
}
}



-

Ben Pfaff
http://benpfaff.org


6月11日上午10:27,Ben Pfaff< b ... @ cs.stanford。 eduwrote:
On Jun 11, 10:27 am, Ben Pfaff <b...@cs.stanford.eduwrote:

>

它被丢弃,因为有符号算术溢出会产生

未定义的行为。
>
It is discarded because signed arithmetic overflow yields
undefined behavior.



感谢您的回复和提及编译器标志

支持经典​​行为。


Thanks for your responses and the mention of the compiler flag that
supports the "classic" behaviour.


这篇关于关于gcc -O3优化的问题(bug或功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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