在优化编译器中不断结合 [英] Constant combining in optimizing compilers

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

问题描述

我有一个包含许多小型内联函数的头文件.他们中的大多数碰巧拥有不变的数据.由于这些函数对性能至关重要,因此它们处理常量的方式变得很重要.AFAIK有两种引用常量的方法:

I have a header file containing a lot of small inline functions. Most of them happen to have constant data. Since these functions are performance critical, the way they handle constants becomes important. AFAIK there are two ways to refer to constants:

1)在一个单独的源文件中定义它们,该文件随后与应用程序链接.

1) Define them in a separate source file that is later linked with the application.

2)就地定义常数.

我会选择后一种方式,因为它更易于维护.但是,如果编译器不优化通过内联创建的数千个相等常量,则速度可能会变慢.

I would choose the latter way because it's more maintainable. However, the it might be slower if the compiler doesn't optimize thousands of equal constants that are created by inlining.

问题:

编译器会合并这些相等的常量吗?特别是,将使用以下哪种方法?

Will the compiler combine these equal constants? In particular, which of the following methods will be utilized?

1)在整个编译单元中合并相等的常量.
2)在整个链接模块(整个程序或库)中组合相等的常量
3)将常量与恰好具有相同位模式并满足整个编译单元或整个程序的对齐要求的 any 静态常量数据组合.

1) Combining of equal constants across the compilation unit.
2) Combining of equal constants across the linking module (whole program or library)
3) Combining the constants with any static constant data that happens to have the same bit pattern and fulfills the alignment requirements across the compilation unit or whole program.

我使用的是现代编译器(GCC4.5).

I use a modern compiler (GCC4.5).

我不是汇编专家,因此我自己无法使用几个简单的测试来回答这个问题:)

I'm not an expert in assembler, thus I couldn't answer this question myself using several simple tests :)

常量很大(大多数常量至少为16个字节),因此编译器无法使它们成为立即值.

The constants are quite big (most of them at least 16 bytes), so the compiler can't make them immediate values.

代码示例

这个使用原位常量:

float_4 sign(float_4 a)
{
    const __attribute__((aligned(16))) float mask[4] = { //I use a macro for this line
        0x80000000, 0x80000000, 0x80000000, 0x80000000};
    const int128 mask = load(mask);
    return b_and(a, mask);
}

推荐答案

根据

According to the GCC the following option does what you want:

-fmerge-constants

尝试在编译单元之间合并相同的常量(字符串常量和浮点常量).如果汇编器和链接器支持,则此选项是优化编译的默认选项.使用-fno-merge-constants禁止此行为.
在-O,-O2,-O3和-Os级别启用.

-fmerge-constants

Attempt to merge identical constants (string constants and floating point constants) across compilation units. This option is the default for optimized compilation if the assembler and linker support it. Use -fno-merge-constants to inhibit this behavior.
Enabled at levels -O, -O2, -O3, -Os.

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

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