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

            Bug ID: 86241
           Summary: duplicate strlen-like snprintf calls not folded
           Product: gcc
           Version: 8.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: ---

The calls to snprintf in the function below compute the length of the string s
without storing any output.  As mentioned in bug 86203 comment #4, the result
of the function with the same arguments (and a null destination) is constant so
the duplicate snprintf call could be replaced by the result of the first,
analogously to how the strlen optimization pass folds the corresponding call to
strlen().

$ cat c.c && gcc -S -O2 -Wall -Wextra -fdump-tree-optimized=/dev/stdout c.c
  int f (char *s)
  {
    int n = __builtin_strlen (s);
    int m = __builtin_strlen (s);   // folded into m = n;
    return m + n;
  }


  int g (char *s)
  {
    int n = __builtin_snprintf (0, 0, "%s", s);
    int m = __builtin_snprintf (0, 0, "%s", s);   // could be folded into m = n
    return m + n;
  }

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

f (char * s)
{
  int n;
  long unsigned int _1;
  int _5;

  <bb 2> [local count: 1073741825]:
  _1 = __builtin_strlen (s_3(D));
  n_4 = (int) _1;
  _5 = n_4 * 2;
  return _5;

}



;; Function g (g, funcdef_no=1, decl_uid=1961, cgraph_uid=1, symbol_order=1)

g (char * s)
{
  int m;
  int n;
  int _7;

  <bb 2> [local count: 1073741825]:
  n_4 = __builtin_snprintf (0B, 0, "%s", s_2(D));
  m_6 = __builtin_snprintf (0B, 0, "%s", s_2(D));
  _7 = n_4 + m_6;
  return _7;

}

Reply via email to