Thread: -funroll-all-loops

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    -funroll-all-loops

    I was wondering how exactly GCC unrolls loops. Like I can imagine if you have:
    Code:
    for (int i=0; i<100; ++i) {a();};
    it can just put a 100nd times a(). But what if you have:
    Code:
    int max;
    ....
    for (int i=0; i<max; ++i) {a();};
    so the compiler won't know the value of max. Can this loop be unrolled with -O3 -funroll-all-loops? Is there a way for the compiler to optimize this loop in terms of speed?
    If you have two loops like:
    Code:
    for (int j=0; j<maxj; ++j) 
        for (int i=0; i<maxi; ++i) {
            a();
        };
    is there any way to merge somehow the loops to gain speed?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you tried looking at the generated code (use gcc -S), and see what difference it makes with varying options for "unroll-loops"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I did.... after -O3 -funroll-all-loops is really to understand anything :P Well, my code is large, so I ll make a code only with the loop and see. Thanx for the advise, will get back if I don't figure it out

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It doesn't seem to unroll at all the loops. I do this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int k, j=0;
        for (k=0; k < 100; ++k)
    	    j++;
    }
    gcc -S -c test.c
    with -funroll-all-loops it generates the same code.

    With -O3 I just make no sense, though there are no jump commands at all

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    It doesn't seem to unroll at all the loops. I do this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int k, j=0;
        for (k=0; k < 100; ++k)
    	    j++;
    }
    gcc -S -c test.c
    with -funroll-all-loops it generates the same code.

    With -O3 I just make no sense, though there are no jump commands at all
    That's because the code doesn't actually do anything, so there is no point in having a loop, just pre-calculate j (100) and skip the entire loop [and since j and k aren't used after the loop, I expect that it doesn't even need that.

    I expect that if you don't have sufficient debug level (-O2 or higher), the compiler may not do enough code-analyzis to perform loop unrolling...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM