优化 [英] Optimizing Away

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

问题描述

假设我在一个独立的

环境中有这样的东西(其中void main(void)被记录并且定义明确):


static int G_num_loops = 0;


void main(void)

{

/ *设置中断* /

/ * ... * /

for(;;)

{

G_num_loops ++;

}

}


进一步假设我有(非标准)方式阅读通过链接图和其他技术获得G_num_loops的价值

.


C标准中是否有任何允许编译器的内容

优化,不为G_num_loops分配存储空间,从而阻止我从查看中获取
它的价值?如果是这样,将G_num_loops定义为不稳定

会阻止这种情况吗? (或以更标准的方式,C标准

需要声明:


G_num_loops ++;


将被执行。


我的编译器似乎正在优化该语句,当定义为

静态且不易变。所以相反,我改变了定义有

外部链接:


int G_num_loops = 0;


它适用于我的目的。


唯一的*潜在*问题是,如果我在另一个翻译中使用外部链接定义与

相同名称(主要是数组)的变量

单位(更可能的名称是G_debug_buf),然后链接器发出

关于多重定义符号的错误。


我可以处理那 - 我已经学会了与编译器和

链接器一起工作,并且只使用特定于

模块的前缀来命名这些变量,例如, G_fir_debug_buf或G_serial_debug_buf.M aybe我只是面对嵌入式调试的现实,但我想听听

其他在这方面有相似经验的人的意见。 br />

BTW,我正在使用的编译器不符合

中的C标准它不会将以下文件范围定义初始化为0:


static int G_num_loops;


我必须将它显式初始化为0:


static int G_num_loops = 0;


谢谢

-

jay

Suppose I have something like this on a free-standing
environment (where void main(void) is documented and well-defined):

static int G_num_loops = 0;

void main (void)
{
/* set up interrupts */
/* ... */
for ( ; ; )
{
G_num_loops++;
}
}

Further suppose that I have the (non-standard) means to "read" the value
of G_num_loops via a link map and other techniques :)

Is there anything in the C standard that allows the compiler to
optimize away and NOT allocate storage for G_num_loops and thus prevent me
from "viewing" its value? If so, does defining G_num_loops as volatile
prevent this? (or put in a more standard kind of way, does the C standard
require the statement:

G_num_loops++;

to be executed.

My compiler seems to be optimizing that statement away when defined as
static and not volatile. So instead, I change the definition to have
external linkage:

int G_num_loops = 0;

and it works for my purpose.

The only *potential* problem is that if I define variables with the
same name (mostly arrays) with external linkage in another translation
unit (more likely the name would be G_debug_buf), then the linker issues
an error about multiply defined symbols.

I can deal with that--I''ve learned to "work" with the compiler and
linker and just name such variables with prefixes that are specific to the
module, e.g., G_fir_debug_buf or G_serial_debug_buf. Maybe I''m just facing
the reality of embedded debugging, but I''d like to hear the opinions of
others who have similar experience in this area.

BTW, the compiler I''m working with does not conform to the C standard in
that it does not initialize the following file scope definition to 0:

static int G_num_loops;

I have to explicitly initialize it to 0:

static int G_num_loops = 0;

Thanks
--
jay

推荐答案

jaysome< ja ***** @ spamcop.netwrote:
jaysome <ja*****@spamcop.netwrote:

假设我有类似的东西独立式

环境(其中void main(void)被记录并且定义明确):


static int G_num_loops = 0;


void main(无效)

{

/ *设置中断* /

/ * ... * /

(; ; )

{

G_num_loops ++;

}

}


进一步假设我有(非标准)手段来阅读通过链接图和其他技术获得G_num_loops的价值

.


C标准中是否有任何允许编译器的内容

优化,不为G_num_loops分配存储空间,从而阻止我从查看中获取
它的价值?
Suppose I have something like this on a free-standing
environment (where void main(void) is documented and well-defined):

static int G_num_loops = 0;

void main (void)
{
/* set up interrupts */
/* ... */
for ( ; ; )
{
G_num_loops++;
}
}

Further suppose that I have the (non-standard) means to "read" the value
of G_num_loops via a link map and other techniques :)

Is there anything in the C standard that allows the compiler to
optimize away and NOT allocate storage for G_num_loops and thus prevent me
from "viewing" its value?



是。

Yes.


如果是这样,将G_num_loops定义为volatile会阻止这种情况吗?
If so, does defining G_num_loops as volatile prevent this?



请记住,挥发性故意不明确,是的,它应该是b $ b。

Keeping in mind that volatile is intentionally under-specified, yes, it
should.


(或以更标准的方式,C标准

需要声明:


G_num_loops ++;


将被执行。
(or put in a more standard kind of way, does the C standard
require the statement:

G_num_loops++;

to be executed.



否。它要求程序在执行时执行_as if,但如果

程序本身没有任何东西可以检测它是否,或者

甚至检测到G_num_loops是否存在,它被允许留下它

out并且只假装增加一个int。那你也可能是

能够使用程序本身以外的方式读取它的值不是

在范围内S标准,允许实现

忽略它。


如你所料,使G_num_loops不稳定应该停止。所有

访问易失性对象mu完全按照标准

指定执行,不对其执行优化。问题是什么

构成访问权限...是实现定义的。

理论上,实现可以将访问权限定义为任何阅读

a volatile对象,用于将其分配给另一个对象,或者
从另一个对象写入一个值。由于G_num_loops ++仅涉及它自己的上一个和下一个值,因此在这样的实现下这不会构成

访问。然而,这种实施方式将是有悖常理的 - 严格地在标准的范围内,但是反常 -

并且我在实践中不会考虑它。 br $>

Richard

No. It requires the program to behave _as if_ it were executed, but if
there''s nothing in the program itself which can detect whether it is, or
even detect whether G_num_loops exists at all, it''s allowed to leave it
out and only pretend to have increment an int. That you might also be
able to read its value using means outside the program itself is not
within the scope of the S Standard, and an implementation is allowed to
ignore it.

Making G_num_loops volatile should, as you expect, stop this. All
accesses of a volatile object must be performed exactly as the Standard
specifies, with no optimisation performed on it. The catch is that "What
constitutes an access ... is implementation-defined".
An implementation could theoretically define an access as any reading of
a volatile object for the purposes of assigning it to another object, or
writing a value in it from another object. Since G_num_loops++ involves
only its own previous and next value, this would not constitute an
access under such an implementation. That implementation would, however,
be perverse - strictly within the bounds of the Standard, but perverse -
and I wouldn''t take it into account in practice.

Richard




jaysome写道:

jaysome wrote:

假设我在一个独立的

环境中有这样的东西(其中void main(void)被记录并且定义明确):


static int G_num_loops = 0;


void main(无效)

{

/ *设置中断* /

/ * ... * /

for(;;)

{

G_num_loops ++;

}

}


进一步假设我有(非标准)方式阅读通过链接图和其他技术获得G_num_loops的价值

.


C标准中是否有任何允许编译器的内容

优化,不为G_num_loops分配存储空间,从而阻止我从查看中获取
它的价值?如果是这样,将G_num_loops定义为不稳定

会阻止这种情况吗? (或以更标准的方式,C标准

需要声明:


G_num_loops ++;


将被执行。


我的编译器似乎正在优化该语句,当定义为

静态且不易变。所以相反,我改变了定义有

外部链接:


int G_num_loops = 0;


它适用于我的目的。


唯一的*潜在*问题是,如果我在另一个翻译中使用外部链接定义与

相同名称(主要是数组)的变量

单位(更可能的名称是G_debug_buf),然后链接器发出

关于多重定义符号的错误。


我可以处理那 - 我已经学会了与编译器和

链接器一起工作,并且只使用特定于

模块的前缀来命名这些变量,例如, G_fir_debug_buf或G_serial_debug_buf。也许我我只是面对嵌入式调试的现实,但我想听听

其他在这方面有相似经验的人的意见。


BTW,我正在使用的编译器不符合

中的C标准它不会将以下文件范围定义初始化为0:


static int G_num_loops;


我必须将其显式初始化为0:


static int G_num_loops = 0;


谢谢

-

jay
Suppose I have something like this on a free-standing
environment (where void main(void) is documented and well-defined):

static int G_num_loops = 0;

void main (void)
{
/* set up interrupts */
/* ... */
for ( ; ; )
{
G_num_loops++;
}
}

Further suppose that I have the (non-standard) means to "read" the value
of G_num_loops via a link map and other techniques :)

Is there anything in the C standard that allows the compiler to
optimize away and NOT allocate storage for G_num_loops and thus prevent me
from "viewing" its value? If so, does defining G_num_loops as volatile
prevent this? (or put in a more standard kind of way, does the C standard
require the statement:

G_num_loops++;

to be executed.

My compiler seems to be optimizing that statement away when defined as
static and not volatile. So instead, I change the definition to have
external linkage:

int G_num_loops = 0;

and it works for my purpose.

The only *potential* problem is that if I define variables with the
same name (mostly arrays) with external linkage in another translation
unit (more likely the name would be G_debug_buf), then the linker issues
an error about multiply defined symbols.

I can deal with that--I''ve learned to "work" with the compiler and
linker and just name such variables with prefixes that are specific to the
module, e.g., G_fir_debug_buf or G_serial_debug_buf. Maybe I''m just facing
the reality of embedded debugging, but I''d like to hear the opinions of
others who have similar experience in this area.

BTW, the compiler I''m working with does not conform to the C standard in
that it does not initialize the following file scope definition to 0:

static int G_num_loops;

I have to explicitly initialize it to 0:

static int G_num_loops = 0;

Thanks
--
jay



将G_num_loops定义为volatile会阻止它被优化

out。在嵌入式应用程序中,您应该使用volatile关键字

描述您映射到硬件寄存器的任何变量。

Defining G_num_loops as volatile will prevent it from being optimized
out. In embedded applications, you should use the volatile keyword to
describe any variables that you are mapping to a hardware register.


in********@gmail.com 写道:
in********@gmail.com wrote:

将G_num_loops定义为volatile会阻止它被优化

out。在嵌入式应用程序中,您应该使用volatile关键字

描述您映射到硬件寄存器的任何变量。
Defining G_num_loops as volatile will prevent it from being optimized
out. In embedded applications, you should use the volatile keyword to
describe any variables that you are mapping to a hardware register.



对。除硬件寄存器和内存映射设备外,

其他情况需要使用volatile:


1)线程之间共享的变量。

2)线程和中断服务程序之间共享的变量。

3)函数中的局部变量已被调用setjmp。这是一种充满异国情调并在少数情况下使用的东西,但值得一提。

Right. In addition to hardware registers and memory mapped devices,
other situations require the use of volatile:

1) Variables shared between threads.
2) Variables shared between threads and Interrupt Service routines.
3) Local variables in functions were setjmp has been called . This is
an exotic and used in few situations, but worth to mention.


这篇关于优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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