我尝试了一些东西,但我很困惑。
具有以下 C++ 代码
#include <map>
int foo() {
std::map<int,int> a;
a[1] = 2;
return 0;
}
int main() {
return foo();
}
和 gcc 版本
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
首先,我发现将 -Og 标志添加到 gcc 时,operator[int] 被优化了。
其次,通过"g++ -Q --help=optimizers" 和 "g++-Q --help=optimizers -Og" 的差异结果,我得到了相同的 Og 编译标志:
-fcombine-stack-adjustments
-fcompare-elim
-fcprop-registers
-fdefer-pop
-fforward-propagate
-fguess-branch-probability
-fipa-profile
-fipa-pure-const
-fipa-reference
-fmerge-constants
-fshrink-wrap
-fsplit-wide-types
-ftree-bit-ccp
-ftree-ccp
-ftree-ch
-ftree-copy-prop
-ftree-copyrename
-ftree-dce
-ftree-dominator-opts
-ftree-dse
-ftree-fre
-ftree-sink
-ftree-slsr
-ftree-ter
第三,我每次都将上述标志之一添加到 gcc。然后我发现 operator[int] 没有优化。
问题是:gcc 决定是否内联的真正原因是什么?
另一个令人困惑的地方: 当添加“-finline -Og”或“finline -finline-mall-functions -findirect-inlining -finline-functions -finline-functions- called-once -Og”时,操作符[int]没有被优化掉。 但是加上“-Og -finline”,operator[int]就被优化掉了。