clang或gcc是否利用引用限制进行别名分析
Does clang or gcc take advantage of referencing restrictions for alias analysis
我想知道clang或gcc内部的别名分析是否将C++成员引用变量与指针变量区别对待。如果编译器可以利用一些关于引用的限制性更强的规则,那么这将是一个基于性能的参数,它更喜欢引用而不是指针。
证明这一点的一种方法是使用位代码,其中在引用和指针之间的更改会更改程序集。
以下是可能产生差异的代码示例:
struct FooRef {
FooRef(int &i) : i_(i) {}
int &i_;
int add(int a, int *messWithAliasAnalysis) { *messWithAliasAnalysis= 0; return i_ + a; }
};
struct FooPtr {
FooPtr(int *i) : i_(i) {}
int *i_;
int add(int a, int *messWithAliasAnalysis) { *messWithAliasAnalysis= 0; return *i_ + a; }
};
// These functions are here to force the compiler to compile the add functions.
int foo(FooPtr &fooPtr, int *messWithAliasAnalysis) {
return fooPtr.add(5, messWithAliasAnalysis);
}
int foo(FooRef &fooRef, int *messWithAliasAnalysis) {
return fooRef.add(5, messWithAliasAnalysis);
}
但对于gcc 4.6,情况并非如此。为两个foo函数发出相同的程序集。
什么是"引用的限制性规则"?在您的示例中:
int x;
FooRef r(x);
foo(r, &x);
创建了CCD_ 1和CCD_。
C++没有对restrict的标准支持,但许多编译器都有通常在C++和C中都能工作的等价程序,例如GNU编译器集合restrict和Visual C++__restrict和__declspec(restrict)。
http://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html
http://msdn.microsoft.com/en-us/library/5ft82fed.aspx
gcc和VC++都在指针上支持C++中的__restrict,但只有gcc在引用上支持它——因为引用在没有未定义行为的情况下无法重新密封,所以编译器可以静态地确定别名。
相关文章:
- 对编译时 C 数组的严格别名和引用
- 强制转换为不相关的引用类型是否违反严格的别名规则?
- 为什么模板引用类型不能用作模板类型别名参数?
- 是否可以将引用类型别名与指针运算符一起使用来声明对指针的引用?
- 为什么模板别名专用化取决于引用它的上下文
- 为什么我可以使用类型别名声明常量引用
- 使用 std::weak_ptr 和别名构造函数中断循环引用:声音或有问题
- 通过引用创建别名以提升:shared_ptr
- 转发引用是否可以使用别名模板进行别名处理
- 身份别名模板是否可以作为转发引用
- 在 C++ 中作为指针或别名进行内部编译的引用
- 修改引用变量,而不是修改它们别名的变量
- clang或gcc是否利用引用限制进行别名分析
- 使用别名引用匿名结构会导致错误
- 使用常量引用为变量设置别名
- Qt 表按名称或别名引用小部件列
- 使用别名将通用引用限制为单个类型
- LLVM中的别名分析
- 如何(优雅地)根据字符别名分配引用成员
- 在clang 3.3.1中使用别名分析