允许对易失性对象进行优化
Optimization allowed on volatile objects
来自ISO/IEC9899:201x段5.1.2.3程序执行段4:
In the abstract machine, all expressions are evaluated as specified by
the semantics. An actual implementation need not evaluate part of an
expression if it can deduce that its value is not used and that no
needed side effects are produced (including any caused by calling a
function or accessing a volatile object).
关于易失性对象,这里允许的优化到底是什么?有人可以举例说明 可以 优化掉的易失性访问吗?
由于 volatiles 访问是一种可观察到的行为(在段落 6 中进行了描述)似乎没有关于 volatiles 的优化可以取悦,所以,我很想知道什么是优化4.
部分允许
can someone give an example of a volatile access that CAN be optimized
away?
我认为您误解了文字,IMO 这一段的意思是
volatile unsigned int bla = whatever();
if (bla < 0) // the code is not evaluated even if a volatile is involved
添加另一个符合我理解的示例:
volatile int vol_a;
....
int b = vol_a * 0; // vol_a is not evaluated
重新格式化一下:
An actual implementation need not evaluate part of an expression if:
a) it can deduce that its value is not used; and
b) it can deduce that that no needed side effects are produced (including any
caused by calling a function or accessing a volatile object).
在不改变意思的情况下颠倒逻辑:
An actual implementation must evaluate part of an expression if:
a) it can't deduce that its value is not used; or
b) it can't deduce that that no needed side effects are produced (including
any caused by calling a function or accessing a volatile object).
简化以关注易失性部分:
An actual implementation must evaluate part of an expression if needed
side effects are produced (including accessing a volatile object).
必须评估对易失性对象的访问。 “包括任何……”一词修饰“副作用”。不修饰“if it can deduce...” 同义:
An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects (including any caused by calling a function or accessing a volatile object) are produced.
这意味着“副作用”包括由访问易失性对象引起的副作用。为了确定它不能评估表达式的一部分,实现必须推断出没有产生任何必要的副作用,包括由调用函数或访问易失性对象引起的任何副作用。
这并不意味着实现可以放弃对部分表达式的求值,即使该表达式包含对易失性对象的访问。
如果对易失性对象的访问会以一种使程序实现其目的所必需的方式影响系统行为,则不得省略此类访问。如果访问对系统行为没有任何影响,则操作可以在抽象机上 "performed" 而无需执行任何指令。然而,编译器编写者很少会确切地知道执行指令以执行访问的效果与假装在抽象机器上执行这些指令而在真实机器上跳过它们的效果相同。
在更常见的情况下,编译器编写者不会特别了解易失性访问可能产生的任何影响,但也没有特别的理由相信此类访问 不能 具有编译器作者不知道的效果(例如,由于涉及某些地址的操作触发了硬件),编译器作者必须考虑到此类访问可能具有 "interesting" 效果的可能性通过按指定顺序执行它们,而不考虑编译器编写者是否知道操作顺序应该重要的任何特定原因。
来自ISO/IEC9899:201x段5.1.2.3程序执行段4:
In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).
关于易失性对象,这里允许的优化到底是什么?有人可以举例说明 可以 优化掉的易失性访问吗?
由于 volatiles 访问是一种可观察到的行为(在段落 6 中进行了描述)似乎没有关于 volatiles 的优化可以取悦,所以,我很想知道什么是优化4.
部分允许can someone give an example of a volatile access that CAN be optimized away?
我认为您误解了文字,IMO 这一段的意思是
volatile unsigned int bla = whatever();
if (bla < 0) // the code is not evaluated even if a volatile is involved
添加另一个符合我理解的示例:
volatile int vol_a;
....
int b = vol_a * 0; // vol_a is not evaluated
重新格式化一下:
An actual implementation need not evaluate part of an expression if:
a) it can deduce that its value is not used; and
b) it can deduce that that no needed side effects are produced (including any
caused by calling a function or accessing a volatile object).
在不改变意思的情况下颠倒逻辑:
An actual implementation must evaluate part of an expression if:
a) it can't deduce that its value is not used; or
b) it can't deduce that that no needed side effects are produced (including
any caused by calling a function or accessing a volatile object).
简化以关注易失性部分:
An actual implementation must evaluate part of an expression if needed
side effects are produced (including accessing a volatile object).
必须评估对易失性对象的访问。 “包括任何……”一词修饰“副作用”。不修饰“if it can deduce...” 同义:
An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects (including any caused by calling a function or accessing a volatile object) are produced.
这意味着“副作用”包括由访问易失性对象引起的副作用。为了确定它不能评估表达式的一部分,实现必须推断出没有产生任何必要的副作用,包括由调用函数或访问易失性对象引起的任何副作用。
这并不意味着实现可以放弃对部分表达式的求值,即使该表达式包含对易失性对象的访问。
如果对易失性对象的访问会以一种使程序实现其目的所必需的方式影响系统行为,则不得省略此类访问。如果访问对系统行为没有任何影响,则操作可以在抽象机上 "performed" 而无需执行任何指令。然而,编译器编写者很少会确切地知道执行指令以执行访问的效果与假装在抽象机器上执行这些指令而在真实机器上跳过它们的效果相同。
在更常见的情况下,编译器编写者不会特别了解易失性访问可能产生的任何影响,但也没有特别的理由相信此类访问 不能 具有编译器作者不知道的效果(例如,由于涉及某些地址的操作触发了硬件),编译器作者必须考虑到此类访问可能具有 "interesting" 效果的可能性通过按指定顺序执行它们,而不考虑编译器编写者是否知道操作顺序应该重要的任何特定原因。