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

            Bug ID: 86434
           Summary: early string folding defeats strlen optimization
           Product: gcc
           Version: 9.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: ---

When the call to strlen() below is folded to (3 - i) it is done very early, and
without setting the range of the result of the subtraction.  This early folding
prevents later optimizations from determining that the result of the strlen()
call (or that of the subtraction) cannot be greater than 3.  It's an example
that illustrates why early folding should be restricted to constants and leave
more advanced optimizations to later passes that have access to the results of
data flow analyses.

$ cat d.c && gcc -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout d.cconst
char a[] = "123";
const char a[] = "123";

void f (int i)
{
  if (__builtin_strlen (&a[i]) > 3)
    __builtin_abort ();
}

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

f (int i)
{
  ssizetype _1;
  ssizetype _2;
  long unsigned int _3;

  <bb 2> :
  _1 = (ssizetype) i_4(D);
  _2 = 3 - _1;
  _3 = (long unsigned int) _2;
  if (_3 > 3)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  __builtin_abort ();

  <bb 4> :
  return;

}

Reply via email to