This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

optimization


Hello everybody,

I wonder if this is possible to implement an optimizer that would
produce just one statement returning constant for the following fragment
of code:

---
#include <cstddef>

inline unsigned strlen(const char* s)
{
    unsigned len = 0;
    while (*s++) len++;
    return len;
}

struct RPK {
    const char* rpk;
    const char* table;
};

// referenced primary keys and corresponding tables
const RPK rpks[] = {
    {"section_id", "section"},
    {"group_id", "group"},
    {"person_id", "person"},
    {"professional_id", "professional"},
    {"coach_id", "coach"},
    {"trip_id", "trip"},
    {"trip_category_id", "trip_category"},
    {"route_id", "route"},
    {"place_id", "place"},
    {"qualification", "qualification"},
    {"position_id", "position"}
};
const size_t n_rpks = sizeof(rpks)/sizeof(rpks[0]);

inline unsigned maxRpkLen()
{
    unsigned max = strlen(rpks[0].rpk);
    for (size_t i = 1; i<n_rpks; i++) {
        unsigned len = strlen(rpks[i].rpk);
        if (max<len) max = len;
    }
    return max;
}

int main()
{
    return maxRpkLen();
}
---

It seems to me that there is no problem for an optimizer to reveal that
at the moment of inlining of maxRpkLen() corresponding code does not
depend on variables that can change values and calculate the value
during compilation so that a constant value be inserted in the
application code.

Actually this does not happen with whatever level of optimization in
GCC. I think that it makes sense to provide a user with the possibility
of code fragment execution during compilation stage since that would
solve the problem. Of course, if there was such an option, it should be
disabled by default and only used by the programmer at his/her own risk
because the code corresponding to "constant fragment" can contain
indefinite loop or just run for a long time.

What do you think about that?

                        Yours sincerely, Andrey Urazov
-- 
We were so poor we couldn't afford a watchdog.  If we heard a noise at night,
we'd bark ourselves.
		-- Crazy Jimmy
--
Tuesday, January 29, 2002, 16:49:04 +0600 - Andrey R. Urazov (mailto:coola@ngs.ru)

Attachment: msg00274/pgp00000.pgp
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]