循环被忽略(优化?)出来 [英] for loop being ignored (optimized?) out

查看:153
本文介绍了循环被忽略(优化?)出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用/,而在我的code实现一个延迟循环。延迟的持续时间是不重要这里虽然它是足够大以显着。这里是code片段。

I am using for/while loops for implementing a delay in my code. The duration of the delay is unimportant here though it is sufficiently large to be noticeable. Here is the code snippet.

uint32_t i;

// Do something useful

for (i = 0; i < 50000000U; ++i)
{}

// Do something useful

我观察的问题是,这个循环将不被执行。它可能被忽略/编译器优化。但是,如果我有资格循环计数 I 挥发性,for循环似乎执行和我注意到在执行所需的延迟。

The issue I am observing is that this for loop won't get executed. It probably gets ignored/optimized by the compiler. However, if I qualify the loop counter i by volatile, the for loop seems to execute and I do notice the desired delay in the execution.

此行​​为似乎有点违反直觉我用/编译器优化的理解没有volatile关键字。

This behavior seems a bit counter-intuitive to my understanding of the compiler optimizations with/without the volatile keyword.

即使循环计数器得到优化,并被存储在寄存器,不应计数器仍然工作,或许还有一个较小的延时? (由于内存开销取不免掉。)

Even if the loop counter is getting optimized and being stored in the processor register, shouldn't the counter still work, perhaps with a lesser delay? (Since the memory fetch overhead is done away with.)

我建立的平台是Xtensa处理器内核(由Tensilica公司)和C编译器与优化的最高级别的运行由Tensilica公司,的Xtensa C / C ++编译器中提供的。

The platform I am building for is Xtensa processor (by Tensilica), and the C compiler is the one provided by Tensilica, Xtensa C/C++ compiler running with highest level of optimizations.

我试着用 GCC 4.4.7 同样与 -03 和ofast优化级别。延时似乎在这种情况下工作。

I tried the same with gcc 4.4.7 with -o3 and ofast optimization levels. The delay seems to work in that case.

推荐答案

这是所有关于观察到的行为。你的循环的唯一可观测的行为是 I 50000000U 在循环之后。编译器被允许优化它和替换I = 50000000U; 。这 I 分配也将被优化掉了,因为 i的值有没有观察到的后果。

This is all about observable behavior. The only observable behavior of your loop is that i is 50000000U after the loop. The compiler is allowed to optimize it and replace it by i = 50000000U;. This i assignment will also be optimized out because the value of i have no observable consequences.

挥发性关键字告诉写入和阅读我有观察到的行为,从而$ ​​P编译器从$优化pventing它。

The volatile keyword tells the compiler that writing to and reading from i have an observable behavior, thus preventing it from optimizing.

,编译器将也不能优化调用函数的地方不具有访问code。理论上,如果一个编译器能够获得整个OS code,它可以优化的一切,但volatile变量,这往往是把硬件IO操作。

The compiler will also not optimize calls to function where it doesn't have access to the code. Theoretically, if a compiler had access to the whole OS code, it could optimize everything but the volatile variables, which are often put on hardware IO operations.

这些优化规则都符合什么是写在C标准(比照为引用的评论)。

These optimization rules all conform to what is written in the C standard (cf. comments for references).

另外,如果你想有一个延时,使用特殊的功能(例如:OS API),他们是可靠的,不会占用CPU,不像你们这样一个旋转延迟

Also, if you want a delay, use a specialized function (ex: OS API), they are reliable and don't consume CPU, unlike a spin-delay like yours.

这篇关于循环被忽略(优化?)出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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