clang或gcc是否利用引用限制进行别名分析

Does clang or gcc take advantage of referencing restrictions for alias analysis

本文关键字:别名分析 引用 gcc 是否 clang      更新时间:2023-10-16

我想知道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在引用上支持它——因为引用在没有未定义行为的情况下无法重新密封,所以编译器可以静态地确定别名。