只能为函数声明(而不是定义)指定函数属性。所以,我不能为嵌套函数指定属性。例如:
//invalid line. hot_nested_function is invisible outside "some_function"
void hot_nested_function() __attribute__ ((hot));
//valid attribute for declaration
int some_function() __attribute__ ((hot));
int some_function(){
void hot_nested_function(){
//time critical code
}
for( int i=0; i<REPEAT_MANY_TIMES;i++)
hot_nested_function();
}
在这种情况下,“hot_nested_function”会被优化为“hot”吗?
UPD:在一个愚蠢的例子中,gcc(意味着gcc -O1
和更高的优化级别)用它的主体替换函数调用,无论是有还是没有__attribute__ ((hot))
(对于嵌套函数)。甚至没有任何关于嵌套函数的提醒。
UPD2:根据gcc.git/gcc/tree-nested.c解析父函数引用、外部标签跳转等。在下一阶段,嵌套函数将转换为具有内联能力的独立函数。但目前还不清楚父函数的属性。他们申请嵌套了吗?