Search code examples
gcc

How to find when specific optimizations were introduced in GCC


I am interested in knowing when these particular optimizations were added as builtin features in the GCC compiler:

  • Granlund-Montgomery-Warren (division by constant)

  • Loop unrolling

A consultant made a claim that these optimizations were not incorporated into the GCC compiler until the late 90s so developers had to code them by hand up to then. I want to fact-check his claim.

Thank you


Solution

  • You are right to be skeptical; these features were present well before the late 1990's.

    Older versions of gcc had ChangeLog files. I arbitrarily chose https://ftp.gnu.org/old-gnu/gcc/gcc-2.6.3.tar.bz2 to start with.

    Division via multiplication: 1994

    Dividing by multiplying is mentioned in the NEWS file as a new feature in gcc 2.6.0, which was released on 14 Jul 1994.

    Noteworthy changes in GCC version 2.6.0:

    [...]

    • GCC can now convert division by integer constants into the equivalent multiplication and shift operations when that is faster than the division.

    The corresponding ChangeLog entry:

    Tue Jun 28 20:27:08 1994 Torbjorn Granlund ([email protected])

        * optab.c (expand_binop): Convert OP0 to mode for library calls.
    
        Changes to optimize division-by-constants, and make ceil and floor
        rounding work correctly:
        * expmed.c (expand_mult): Generalize to call synth_mult also
        for OP1 - 1.
        (ceil_log2): New function.
        (choose_multiplier): New function.
        (invert_mod2n): New function.
        (expand_mult_highpart_adjust): New function.
        (expand_mult_highpart): New function.
        (EXACT_POWER_OF_2_OR_ZERO_P): New macro.
        (expand_divmod): Almost completely rewritten.
        (expand_shift): Don't truncate immediate shift count, it doesn't work
        for types smaller than int.
        * expr.h (smul_highpart_optab, umul_highpart_optab): New variables.
        * genopinit.c (optabs): Add [us]mul_highpart_optab.
        * optabs.c (smul_highpart_optab, umul_highpart_optab): New variables.
        (expand_binop): Handle [us]mul_highpart_optab as commutative.
        (init_optabs): Initialize [us]mul_highpart_optab.
        * fold-const.c (div_and_round_double): Make it globally accessible.
        * a29k.md (smulsi3_highpart, umulsi3_highpart): New patterns.
        * alpha.md (umuldi3_highpart): New expander and matcher.
        * alpha.c (cint8_operand): New predicate.
        * m68k.md (umulsi3_highpart, const_umulsi3_highpart):
        New expander and matcher.
        (smulsi3_highpart, const_smulsi3_highpart): Likewise.
    

    Loop unrolling: 1991

    In ChangeLog.3 we have:

    Thu Apr 4 22:31:36 1991 Jim Wilson (wilson at cygnus.com)

        unroll.c (new file): Implements loop unrolling.  Completely
        unrolls small constant bounded loops.  Unrolls other constant
        bounded loops by an amount modulo the number of iterations so that
        only one exit test is needed.  Preconditions loops whose iteration
        count can be calculated at run time, so that only one exit test
        is needed.  Can also unroll any other loop by having multiple
        copies of the exit tests.  Tries to simplify addresses while
        unrolling. [.....]
    

    The feature was certainly present in gcc 2.0, released 22 Feb 1992.