Hi!

This patch series extends the sprintf folding that the strlen pass does.
We now expand a wider range of sprintf calls into simpler code.  In particular,
we're able to handle format strings containing
%s, %d, %i, %u directives (we accept length modifiers for the integer
directives).

Notably, GCC will now emit a synthesized utoa or itoa helper function into the
translation unit to handle the integer directives.  These functions convert
from numbers to strings.  To avoid code bloat, we make sure those get
deduplicated across TUs during linking.  Emitting these functions was necessary
because they don't have any counterpart in the standard C library.

The original motivation for this was the SPEC CPU 2026 omnetpp benchmark.
There are optimization opportunities originally uncovered by people from AMD.
Indeed, with this patch series applied, we observed speedups of up to 8%.

We worked on this patch series together with Giuliano Belinassi
([email protected]).

Some possible followup work:
1. Extend this to functions similar to sprintf
2. Experiment with moving the strlen pass earlier in the optimization pipeline
   to potentially let one of GCC's inliners inline the utoa/itoa functions ->
   further optimizations.  We actually experimented with hacking the early
   inliner to inline these functions but it refused to do that probably due to
   inlining heuristic (Giuliano might have more details).  This possible
   followup is btw one of the reasons why we chose to insert the itoa/utoa
   functions directly into the TU instead of including them in libgcc.

Bootstrapped and regtested on x86_64 linux.

Thanks,
Filip Kastl

Filip Kastl (4):
  gimple-ssa-sprintf: Fold sprintf with plain strings and %s into memcpy
    sequences
  gimple-ssa-sprintf: Handle directives %d, %i and %u for INTEGER_CST.
  gimple-ssa-sprintf: Handle the %d, and %u when argument value is
    unknown at compile-time
  gimple-ssa-sprintf: Update gimple-ssa-sprintf.cc's description comment

 gcc/Makefile.in                               |   1 +
 gcc/gimple-fold.cc                            | 130 ---
 gcc/gimple-fold.h                             |   1 -
 gcc/gimple-ssa-sprintf.cc                     | 948 ++++++++++++++++--
 .../gcc.dg/tree-ssa/builtin-sprintf-30.c      | 363 +++++++
 .../gcc.dg/tree-ssa/builtin-sprintf-31.c      |   7 +
 .../gcc.dg/tree-ssa/builtin-sprintf-32.c      | 136 +++
 .../gcc.dg/tree-ssa/builtin-sprintf-33.c      | 253 +++++
 .../gcc.dg/tree-ssa/builtin-sprintf-34.c      |   5 +
 .../gcc.dg/tree-ssa/builtin-sprintf-35.c      |  97 ++
 gcc/tree.cc                                   |  43 +
 gcc/tree.h                                    |   1 +
 12 files changed, 1775 insertions(+), 210 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-30.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-31.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-32.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-33.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-34.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-35.c

-- 
2.55.0

Reply via email to