ARM 过程调用标准是否允许与 C 标准相矛盾的易失性优化?

Does ARM procedure call standard allows volatile optimisation that contradict the C standard?

来自 ARM 体系结构的过程调用标准 (§7.1.5):

a compiler may ignore a volatile qualification of an automatic variable whose address is never taken unless the function calls setjmp().

这是否意味着在以下代码中:

volatile int x = 8;
if (x == 1)
{
    printf("can be optimised away??");
}

可以优化整个 if 范围吗?

这与标准相矛盾,对于初学者来说,可变访问是可观察行为的一部分,必须按照抽象机器代码执行:

§5.1.2.3:

The least requirements on a conforming implementation are:

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

还有 §6.7.3:

An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine

有矛盾吗?如果是这样,PCS 与 C 标准相矛盾的合法性如何?

所以我找到了 ARM 工具链的支持组,根据他们的说法,ARM PCS 标准是一个独立的标准,不受 C 标准的约束,因此编译器可以选择遵守其中一个,或同时遵守两个。用他们自己的话说:

In a way it's not really a contradiction

  • the APCS permits a compiler to respect or ignore local volatile
  • the C standard requires a compiler to respect local volatile

so a compiler that is compatible with both will respect local volatile.

Armclang has elected to follow the C standard which makes it compatible with both

因此,如果编译器选择执行此不符合 C 的优化,它仍然是符合 ARM PCS 的实现,但不是符合 C 的编译器。

总而言之,实现 ARM PCS 的 ARM 体系结构的 C 兼容编译器永远不会执行此优化。