Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would compiler optimize conditional statement in loop by moving it ouside the loop?

Here is the C code:

struct node{
    void *value;
    struct node *next;
};

void g(void *p){
    /*...*/
}

void f(struct node *head, const int ok){
    struct node *p=head;

    while (p){
        /* ...
           code 1 
           ...
         */
        if (ok!=0){
            g(p->value);
        }
        p=p->next;
    }
}

I used gcc to compile this code. If I compiled with -O, would it optimize function f like this:

void f(struct node *head, const int ok){
    struct node *p=head;

    if (ok!=0){
        while (p){
            /* ...
               code 1 
               ...
             */
            g(p->value);
            p=p->next;
        }
    }
    else{
        while (p){
            /* ...
               code 1 
               ...
             */
            p=p->next;
        }
    }
}
like image 960
solomon_wzs Avatar asked Jun 13 '13 09:06

solomon_wzs


People also ask

What do compiler optimizations do?

In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power consumption (the last three being popular for portable computers).

What is the purpose of code optimization explain in detail loop optimization with example?

The code optimization in the synthesis phase is a program transformation technique, which tries to improve the intermediate code by making it consume fewer resources (i.e. CPU, Memory) so that faster-running machine code will result.


2 Answers

That would greatly depend on how big /* code 1 */ is. If it is very small, it might. But if it is anything above a few lines, it most probably won't. Duplicating a large amount of code for every single if would have terrible effects on the performance. In fact, that may happen with very aggressive optimization and certainly not just with -O. From the man page of gcc (emphasis mine):

-O
-O1 ...

With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time.

So reducing code is also part of optimization.

-O2 Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O, this option increases both compilation time and the performance of the generated code.

So -O2 wouldn't do what you want either.

-O3 Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize and -fipa-cp-clone options.

Now we have to look at these options to see if any of them might do what you want:

-funswitch-loops
Move branches with loop invariant conditions out of the loop, with duplicates of the loop on both branches (modified according to result of the condition).

Voila! With -O3 you get the optimization you want.

like image 165
Shahbaz Avatar answered Oct 05 '22 22:10

Shahbaz


Well, that depends on many things.

Since, you are using gcc, you can always check if it did for a particular program by invoking gcc -o -S fileName.c

like image 43
pinkpanther Avatar answered Oct 05 '22 22:10

pinkpanther