[Bug c/40175] New: emit optimzied versions of functions depending on constant argument(s)

thephilips at gmail dot com gcc-bugzilla@gcc.gnu.org
Sun May 17 11:46:00 GMT 2009


An idea of semi-automatic optimization crossed my mind, which I could have
taken advantage on more than one project.

Imaging the code:

static int f1( int a, int b )
{
    [... lots of computation using 'a', sometimes involving 'b' ...]
}

int main()
{
    ....
    for (i=0; i<MAX; i++)
        f1( arr[i], 0 );
    ...
    for (i=0; i<MAX; i++)
        f1( arr[i], 1 );
    ...
    for (i=0; i<MAX; i++)
        f1( arr[i], 2 );
    ...
}

Due to code size, inlining f1() is not always feasible.
Second parameter is always/mostly constant, yet it's evaluation inside f1()
cannot be optimized out without macro or inlining.

Idea I had was to allow some way for user to tell C/C++ that for particular
combinations of the function constant parameters, C/C++ needs to emit (and
optimize accordingly) a function specialization. Then all calls to the
function, which match some specialization should be replaced with the calls to
the corresponding function specialization.

E.g.:

static int f1( int a, int b )
{
    [... lots of computation using 'a', sometimes involving 'b' ...]
}

static int f1( int a, int b ) __attribute__(( clone(b == 0), clone(b == 1),
clone( b == 2 ) ));

/* in total, four clones of f1() are emitted: 
   f1(var,var), f1(var,0), f1(var,1), f1(var,2) */

int main()
{
    ....
    for (i=0; i<MAX; i++)
        f1( arr[i], 0 );   /* would be replaced with call to specialization
with b == 0*/
    ...
    for (i=0; i<MAX; i++)
        f1( arr[i], 1 );   /* would be replaced with call to specialization
with b == 1 */
    ...
    for (i=0; i<MAX; i++)
        f1( arr[i], 2 );   /* would be replaced with call to specialization
with b == 2 */
    ...
}

I'm not sure whether that is possible to implement - or make sense at all. I'm
sure that would have helped me on many occasions in past when I had to
implement e.g. special check summing functions. Converting everything to macros
makes code only messier. Writing hand optimized versions for constant arguments
increases testing and maintenance overhead.

It would have been nice if GCC could help in the case.


-- 
           Summary: emit optimzied versions of functions depending on
                    constant argument(s)
           Product: gcc
           Version: 4.3.4
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: thephilips at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40175



More information about the Gcc-bugs mailing list