本文介绍了优化时在现代编译器中使用`register`关键字是否没有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C register 关键字为编译器提供了一个提示,希望它更喜欢将变量存储在寄存器中,而不是存储在堆栈中。如果愿意,编译器可能会忽略它。我了解到,当您启用优化功能进行编译时,这几乎是无用的,但是完全没用吗?



更具体地说:对于任何{gcc,clang,msvc} x {-Og,-O,-O2,-O3}的组合: register 在决定是否实际分配寄存器时被忽略吗?如果不是这样,在某些情况下它是否足以打扰使用它?



注意:




  • 不是问它的关键字是否有 effect ;当然可以-它可以防止您使用该变量的地址;如果您根本不进行优化,则会在变量的寄存器分配或内存分配之间产生差异。

  • 仅支持一个编译器的答案/上述组合中的某些非常受欢迎。


解决方案

对于GCC,注册十多年来,对于所有受支持的CPU体系结构,所有根本没有影响代码生成,甚至没有提示。

原因很大程度上是历史原因。 GCC 2.95及更早版本有两个寄存器分配器,一个在不优化时使用(愚蠢),在优化时使用一个(本地,全局,重载)。 愚蠢的分配器确实尝试使用寄存器,但是本地,全局,重载分配器完全忽略了它。 (我不知道该设计决定的原始原理是什么;您必须询问Richard Kenner。)在3.0版中,笨拙的分配器已被废弃,而倾向于在本地,全局,重新加载。没有人会费心编写代码以使该模式注意 register ,所以不必。



在撰写本文时,GCC开发人员正在使用名为 IRA和LRA的新分配器替换本地,全局,重载,但它也完全忽略了 register



但是,(仅C)规则不能使用寄存器的地址仍会强制执行,并且,它允许您将特定的寄存器专用于变量;这在使用大量内联汇编的程序中很有用。


The C register keyword gives the compiler a hint to prefer storing a variable in a register rather than, say, on the stack. A compiler may ignore it if it likes. I understand that it's mostly-useless these days when you compile with optimization turned on, but is it entirely useless?

More specifically: For any combination of { gcc, clang, msvc } x { -Og, -O, -O2, -O3 }: Is register ignored when deciding whether to actually assign a register? And if not, are there cases in which it's useful enough to bother using it?

Notes:

  • I am not asking whether it the keyword any effect; of course it does - it prevents you from using the address of that variable; and if you don't optimize at all, it will make the difference between register assignment or memory assignment for your variable.
  • Answers for just one compiler / some of the above combinations are very welcome.

解决方案

For GCC, register has had no effect on code generation at all, not even a hint, at all optimization levels, for all supported CPU architectures, for over a decade.

The reasons for this are largely historical. GCC 2.95 and older had two register allocators, one ("stupid") used when not optimizing, and one ("local, global, reload") used when optimizing. The "stupid" allocator did try to honor register, but the "local, global, reload" allocator completely ignored it. (I don't know what the original rationale for that design decision was; you'd have to ask Richard Kenner.) In version 3.0, the "stupid" allocator was scrapped in favor of adding a fast-and-sloppy mode to "local, global, reload". Nobody bothered to write the code to make that mode pay attention to register, so it doesn't.

As of this writing, the GCC devs are in the process of replacing "local, global, reload" with a new allocator called "IRA and LRA", but it, too, completely ignores register.

However, the (C-only) rule that you cannot take the address of a register variable is still enforced, and the keyword is used by the explicit register variable extension, which allows you to dedicate a specific register to a variable; this can be useful in programs that use a lot of inline assembly.

这篇关于优化时在现代编译器中使用`register`关键字是否没有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 13:39