比方說,我有以下代碼:Can/do C編譯器能夠優化內聯函數的地址嗎?
int f() {
int foo = 0;
int bar = 0;
foo++;
bar++;
// many more repeated operations in actual code
foo++;
bar++;
return foo+bar;
}
抽象重複的代碼到一個單獨的功能,我們得到
static void change_locals(int *foo_p, int *bar_p) {
*foo_p++;
*bar_p++;
}
int f() {
int foo = 0;
int bar = 0;
change_locals(&foo, &bar);
change_locals(&foo, &bar);
return foo+bar;
}
我期望編譯器內聯的change_locals
功能,並優化之類的東西*(&foo)++
在產生的代碼foo++
。
如果我沒有記錯,取一個局部變量的地址通常會阻止某些優化(例如它不能存儲在寄存器中),但是當這個地址沒有指針算術完成並且它不能逃脫從功能?對於較大的change_locals
,如果它被宣佈爲inline
(MSVC中爲__inline
),它會有所作爲嗎?
我對GCC和MSVC編譯器的行爲特別感興趣。
最好的辦法是嘗試檢查發射的組件。 – sharptooth 2011-04-05 05:48:51