Starting with this simple C program:
void nothing(void) {}
int main() {
int i;
for (i = 0; i < 10; ++i) {
nothing();
}
return 0;
}
My passes output as follows:
Note: IR statements are in Green.
; Function Attrs: nounwind readnone ssp uwtable
define void @nothing() #0 {
entry:
ret void
}
; Function Attrs: nounwind readnone ssp uwtable
define i32 @main() #0 {
entry:
ret i32 0
}
Question: Using O3
which considered the highest level optimization, Why did nothing
function hasn't been eliminated as a dead-code?
The compiler has to consider the possibility, that there is another translation unit that wants to call nothing()
. Therefore it can't be removed. The most it can do is to optimize its call out, but the function itself has to stay and its symbol exported for possible external usage.
By defining nothing
as static
, you give it internal linkage, meaning that the compiler can assume nothing
to be inaccessible outside the code it sees at the moment. This allows for optimizations, like choosing a different more performant calling convention, or in your case, eliminate the function altogether.