[Bug tree-optimization/47413] New: Constant Propagation and Virtual Function Tables

joerg at joergleis dot com gcc-bugzilla@gcc.gnu.org
Sat Jan 22 19:00:00 GMT 2011


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

           Summary: Constant Propagation and Virtual Function Tables
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: joerg@joergleis.com


The tested GCC version does not fully use its knowledge about (constant)
function pointers. In some cases (with LTO), this occurs frequently. The
following example should illustrate it and it's relevance:


    #include <stdio.h>
    #include <stdlib.h>


    /* the types */

    struct obj;

    struct vtab {
        int (*f)(struct obj *obj);
    };

    struct obj {
        const struct vtab *vtab;
    };


    static int f1337(struct obj *obj)
    {
        return 1337;
    }

    static const struct vtab vtab1337 = {
        .f = f1337
    };


    /* the functions */

    static struct obj *create()
    {
        struct obj *obj;

        if (!(obj = malloc(sizeof(struct obj)))) {
            return NULL;
        }

        obj->vtab = &vtab1337;

        return obj;
    }

    static int call(struct obj *obj)
    {
        return obj->vtab->f(obj);
    }


    /* the program */

    int main()
    {
        struct obj *obj;

        if (!(obj = create())) {
            return 0;
        }

        printf("%d\n", call(obj));
        return 1;
    }

When compiling with -O3, I'd expect GCC to just pass 1337 to printf, as it does
without a virtual function table. Instead, it uses its knowledge about obj to
call vtab1337.f(), as in

    call *vtab1337

but doesn't simplify *vtab1337 to f1337, or the entire call to 1337.



More information about the Gcc-bugs mailing list