Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will compiler optimize out unused arguments of static function?

I have a group of functions that are all declared static and fastcall. Most of them make use of a pointer to a struct that serves more or less the role of this in C++. Some of the functions don't need anything in the struct, but for uniformity sake I want to pass them the pointer anyway. Will the compiler notice that the argument is not used and omit allocating a register to it?

like image 247
cleong Avatar asked Jan 12 '13 08:01

cleong


People also ask

Do unused functions get compiled?

Yes: for unused static functions.

Does compiler optimize code?

Compilers are free to optimize code so long as they can guarantee the semantics of the code are not changed. I would suggestion starting at the Compiler optimization wikipedia page as there are many different kinds of optimization that are performed at many different stages.

Do compilers automatically inline?

Automatic function inlining and static functions At -O2 and -O3 levels of optimization, or when --autoinline is specified, the compiler can automatically inline functions if it is practical and possible to do so, even if the functions are not declared as __inline or inline .

Does compiler inline functions?

The compiler cannot inline a function call, unless it has the full definition.


2 Answers

I wrote this nonsense program to test this. It's got some nonsense code in the function, and calls it several times because otherwise the compiler just inlined the whole function call making the test useless. (It's a strange mix of C and C++ i know.... stupid code, but still works to demonstrate the issue)

#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

struct demo
{
    int a;
    char ch;
};

static  void __fastcall func(struct demo* d)
{
    for(int i = 0; i < 100; i++) {
    std::vector<int> a;
    std::sort(begin(a), end(a));
    printf("THis is a test");
    printf("Hello world\n");
    }
}

int main()
{
  //  void*p = (void*)&func;
    struct demo d;
    func(&d);
    func(&d);
    func(&d);
    func(&d);
    func(&d);
    func(&d);
    func(&d);
    //printf((const char*)p);
}

As written there the function calls compile to this :-

    func(&d);
00F61096  call        func (0F61000h)  
    func(&d);
00F6109B  call        func (0F61000h)  
    func(&d);
00F610A0  call        func (0F61000h)  
    func(&d);
00F610A5  call        func (0F61000h)  
    func(&d);
00F610AA  call        func (0F61000h)  
    func(&d);
00F610AF  call        func (0F61000h)  
    func(&d);
00F610B4  call        func (0F61000h)  

Which shows that the compiler will indeed omit the parameter if it's not used. However if I uncomment the two lines there to take the address of the function then things change, it instead generated this :-

00C71099  lea         ecx,[esp]  
00C7109C  call        func (0C71000h)  
    func(&d);
00C710A1  lea         ecx,[esp]  
00C710A4  call        func (0C71000h)  
    func(&d);
00C710A9  lea         ecx,[esp]  
00C710AC  call        func (0C71000h)  
    func(&d);
00C710B1  lea         ecx,[esp]  
00C710B4  call        func (0C71000h) 

Where it DOES send the pointer.

My assumption is that in the first case the compiler is able to prove that if it generated a custom calling convention to the function that there cannot possibly be any user visible effects but in the second case where you take a pointer to the function, the function could be called from another separately compiled module which has no way to know if the parameter is needed or not. Although in this case it wouldn't matter if the register was set or not, in general the compiler needs to stickl to the exact calling pattern so I assume that it generated the most general code and won't use custom calling conventions if it can't prove that it can see all the code that could call the function.

Anyway, to answer the question, no the compiler will not always pass unused parameters in a register but I certainly wouldn't write any code that depends on any specific behavior here as changing unrelated code elsewhere (taking the address of the function), changed that behavior and there is nothing guaranteed by any standard that I can see.

like image 162
jcoder Avatar answered Nov 15 '22 21:11

jcoder


First of all, the compiler should warn you about unused function arguments, if you use proper compiler flags (-Wall, for example).I'd recommend to leave arguments that you don't use out of the arguments list, although I'd guess they'd be optimized away. What you could do to make sure, however is compiling your code twice, once with the arguments and once without, and do a diff between the binaries and see if they match. I think it should depend on the compiler you use, and the optimization flags.

Cheers,

Andy

like image 22
Andreas Grapentin Avatar answered Nov 15 '22 19:11

Andreas Grapentin