C ++编译器可以消除未读取的易失本地变量吗 [英] Can a C++ Compiler Eliminate a Volatile Local Var that is not Read

查看:208
本文介绍了C ++编译器可以消除未读取的易失本地变量吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有以下代码:

int f() {
  volatile int c;
  c=34;
  return abc();
}

从未读取 volatile int c .但是它被标记为 volatile ,编译器可以完全消除它吗?我在Visual C ++ 2010中进行的测试显示出矛盾的结果.在VC ++中,如果启用优化(最大化速度),则上述函数将包含一个名为c的局部变量(通过查看生成的程序集列表).但是,我没有使用赋值运算符,而是尝试通过像memset()这样的编译器固有函数来初始化变量(并启用使用编译器固有函数),从而消除了该变量.

The volatile int c is never read. But it is marked as volatile, can the compiler eliminate it altogether? My testing in Visual C++ 2010 shows contradictory results. In VC++, if I enable optimization (maximizing speed) the above function contains a local variable called c (by looking at the generated assembly listing). But, instead of using assignment operator, I also tried to initialize the variable by a compiler intrinsic function like memset() (and enable using compiler intrinsic functions), the variable is eliminated.

int f() {
  volatile int c;
  memset((void*)&c,34, 1); 
  return abc();
}

那么根据C ++标准,编译器可以消除volatile int c吗?我在想VC ++中可能有一些与内在函数如何优化volatile变量有关的不一致行为.

So according to the C++ standard, can compiler eliminate the volatile int c? I'm thinking probably there is some inconsistent behaviour in VC++ related to how intrinsic functions optimize volatile variable.

推荐答案

那么根据C ++标准,编译器可以消除volatile int c吗?

So according to the C++ standard, can compiler eliminates the volatile int c?

.合格的volatile对象用于读取或写入硬件,可以看到分配volatile对象的副作用.

No. volatile-qualified objects are used for reading from or writing to the hardware, and the side-effects of assigning a volatile object are observable.

因此,要遵守所谓的 如果"规则,则不允许符合标准的实现对c进行优化. C ++ 11标准的1.9/1段中正式引入了" as "规则:

Therefore, in compliance with the constraints on optimizations placed by the so-called "as-if" rule, conforming implementations are not allowed to optimize away c. The "as-if" rule is formally introduced in Paragraph 1.9/1 of the C++11 Standard:

本国际标准中的语义描述定义了参数化的不确定性摘要 机器.本国际标准对一致性实现的结构没有任何要求. 特别是,它们不需要复制或模拟抽象机的结构.相反,符合 需要实现以(仅)模拟抽象机的可观察到的行为,如下所述 下方

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below

第1.9/8段定义了"可观察到的行为"的概念:

And the concept of "observable behavior" is defined in paragraph 1.9/8:

对符合标准的实现的最低要求是:

The least requirements on a conforming implementation are:

严格根据抽象机的规则评估对易失对象的访问权限.

Access to volatile objects are evaluated strictly according to the rules of the abstract machine.

-在程序终止时,写入文件的所有数据应与以下可能的结果之一相同: 按照抽象语义执行程序.

— At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced.

-交互式设备的输入和输出动态必须以提示方式 输出实际上是在程序等待输入之前传递的.什么构成互动设备 是实现定义的.

— The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.

这些统称为程序的可观察到的行为 . [...]

These collectively are referred to as the observable behavior of the program. [...]

由于必须严格按照抽象机的规则评估对volatile对象的访问,因此不允许编译器优化c和相应的赋值操作.

Since access to volatile objects must be evaluated strictly according to the rules of the abstract machine, compilers are not allowed to optimize away c and the corresponding assignment operation.

这篇关于C ++编译器可以消除未读取的易失本地变量吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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