https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78476

            Bug ID: 78476
           Summary: snprintf(0, 0, ...) with known arguments not optimized
                    away
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

As a special case, a call to snprintf with a zero size is a request to compute
the size of output without actually writing anything into the provided buffer
(which may be a null pointer).  When the format string and all arguments to all
directives in it are within known ranges such that the output length can be
determined at compile time (by the -fprintf-return-value optimization) the
entire snprintf call can be eliminated and replaced with the constant return
value.  The test case below shows that while GCC folds the return value it does
not take advantage of this additional optimization opportunity.

$ cat c.c && /build/gcc-svn/gcc/xgcc -B /build/gcc-svn/gcc -O2 -c
-fdump-tree-optimized=/dev/stdout c.c
void f (const char *s)
{
  int n = 7;
  if (n != __builtin_snprintf (0, 0, "%.*s", n, s))
    __builtin_abort ();
}



;; Function f (f, funcdef_no=0, decl_uid=1795, cgraph_uid=0, symbol_order=0)

f (const char * s)
{
  <bb 2>:
  __builtin_snprintf (0B, 0, "%.*s", 7, s_3(D)); [tail call]
  return;

}

Reply via email to