ANSI C编译器可以去除延迟循环? [英] Can an ANSI C compiler remove a delay loop?

查看:103
本文介绍了ANSI C编译器可以去除延迟循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑ANSI C,其唯一目的是推迟执行while循环:

 无符号长计数器= DELAY_COUNT;
而(counter--);

我已经看到了这个用了很多执行上的嵌入式系统,如在哪里延迟。没有睡觉功能和定时器中断或受到限制。

我的ANSI C标准的解读是,这可以通过一个符合标准的编译器可以完全去除。它有没有在描述的副作用 5.1.2.3


  

访问的易失性对象,修改一个对象,修改文件,或打电话,做任何这些操作都是副作用,这是在执行环境的状态的变化的函数。


...这部分也说:


  

实际上,在实现无法计算前pression的一部分,如果可以推断,它的价值不使用,并且无需副作用产生(包括任何引起调用一个函数或访问一个volatile对象)。


这是否意味着循环可以优化呢?即使计数挥发性

注:


  1. ,这是不完全一样的Are编译器可以消除无限循环?的,因为这是指的无限的循环,并产生约当程序被允许在所有终止的问题。在这种情况下,程序将肯定继续过去这条线在某一点,优化或不

  2. 我知道GCC做什么(消除了环路 -O1 或更高,除非计数挥发性),但我想知道是什么标准使然。


解决方案

C标准合规遵循为假设的规则,由编译器可以生成任何code,其行为好像它运行的抽象机上的实际指令。由于没有执行任何操作具有相同的观察行为,如果你没有执行循环,这是完全允许的,以不产生code吧。

在换言之,东西花费来计算实际的机器上的时间是不是你的程序的观察的行为的一部分,它仅仅是一个特定的实现的一种现象。

的情况有所不同的挥发性变量,因为访问计数波动作为可观察的效果。

Consider a while loop in ANSI C whose only purpose is to delay execution:

unsigned long counter = DELAY_COUNT;
while(counter--);

I've seen this used a lot to enforce delays on embedded systems, where eg. there is no sleep function and timers or interrupts are limited.

My reading of the ANSI C standard is that this can be completely removed by a conforming compiler. It has none of the side effects described in 5.1.2.3:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment.

...and this section also says:

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).

Does this imply that the loop could be optimised out? Even if counter were volatile?

Notes:

  1. That this is not quite the same as Are compilers allowed to eliminate infinite loops?, because that refers to infinite loops, and questions arise about when a program is allowed to terminate at all. In this case, the program will certainly proceed past this line at some point, optimisation or not.
  2. I know what GCC does (removes the loop for -O1 or higher, unless counter is volatile), but I want to know what the standard dictates.

解决方案

C standard compliance follows the "as-if" rule, by which the compiler can generate any code that behaves "as if" it was running your actual instructions on the abstract machine. Since not performing any operations has the same observable behaviour "as if" you did perform the loop, it's entirely permissible to not generate code for it.

In other words, the time something takes to compute on a real machine is not part of the "observable" behaviour of your program, it is merely a phenomenon of a particular implementation.

The situation is different for volatile variables, since accessing a volatile counts as an "observable" effect.

这篇关于ANSI C编译器可以去除延迟循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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