不同的优化级别可能会导致功能不同的code? [英] Can different optimization levels lead to functionally different code?

查看:194
本文介绍了不同的优化级别可能会导致功能不同的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇的优化时,编译器有自由。让我们来限制这个问题GCC和C / C ++(所有版本,标准的任何味):

I am curious about the liberties that a compiler has when optimizing. Let's limit this question to GCC and C/C++ (any version, any flavour of standard):

是否可以写code其中的的行为的不同,具体取决于哪个优化级别它编译?

Is it possible to write code which behaves differently depending on which optimization level it was compiled with?

我心目中的例子是打印在用C的各种构造不同文本位++和获取取决于副本是否省略(虽然我没能做出这种事的工作)的差异。

The example I have in mind is printing different bits of text in various constructors in C++ and getting a difference depending on whether copies are elided (though I've not been able to make such a thing work).

计数时钟周期是不允许的。如果你有一个非GCC编译器的例子,我很好奇,太多,但我不能检查。在C.为例加分点: - )

Counting clock cycles is not permitted. If you have an example for a non-GCC compiler, I'd be curious, too, but I can't check it. Bonus points for an example in C. :-)

编辑:的例子code应该是符合标准的,而不是从一开始就包含未定义行为

The example code should be standard compliant and not contain undefined behaviour from the outset.

编辑2:的得到了一些伟大的答案了!让我的赌注一些:code必须构成一个结构良好的程序,并符合标准的,并且它必须编译纠正,在每一个优化级别确定的方案。 (这不包括之类的东西在病态的多线程code比赛条件)。此外,我AP preciate浮点舍入可能会受到影响,但我们认为打折

Edit 2: Got some great answers already! Let me up the stakes a bit: The code must constitute a well-formed program and be standards-compliant, and it must compile to correct, deterministic programs in every optimization level. (That excludes things like race-conditions in ill-formed multithreaded code.) Also I appreciate that floating point rounding may be affected, but let's discount that.

我刚打800的声誉,所以我想我应在第一个完整的例子吹50声誉作为赏金,以符合这些条件(精神); 25如果涉及滥用严格别名。 (除有人向我展示如何发奖金给别人。)

I just hit 800 reputation, so I think I shall blow 50 reputation as bounty on the first complete example to conform to (the spirit) of those conditions; 25 if it involves abusing strict aliasing. (Subject to someone showing me how to send bounty to someone else.)

推荐答案

这是适用§1.9执行程序C ++标准的一部分。它读,一部分:

The portion of the C++ standard that applies is §1.9 "Program execution". It reads, in part:

符合实现是必需的(仅)如下面所解释,以模拟抽象机的可观察行为。 ...

conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below. ...

一个符合标准的实现执行一个结构良好的程序应产生相同的观察行为的抽象机具有相同的程序和同样的输入对应的实例的可能执行序列之一。 ...

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input. ...

抽象机的观察行为是其顺序读取和写入数据的波动并调用库I / O功能。 ...

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions. ...

所以,是的,code可以的的行为的不同而有着不同的优化级别,但(假设所有级别产生一个符合标准的编译器),但他们不能表现的 observably不同

So, yes, code may behave differently at different optimization levels, but (assuming that all levels produce a conforming compiler), but they cannot behave observably differently.

编辑:请允许我纠正我的结论:是的,code会有不同的表现在不同的优化级别,只要每个行为observably等同于标准的抽象机的行为之一

Allow me to correct my conclusion: Yes, code may behave differently at different optimization levels as long as each behavior is observably identical to one of the behaviors of the standard's abstract machine.

这篇关于不同的优化级别可能会导致功能不同的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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