[Bug tree-optimization/83702] New: missing strlen range optimization for calls with an offset

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Jan 5 19:51:00 GMT 2018


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

            Bug ID: 83702
           Summary: missing strlen range optimization for calls with an
                    offset
           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: ---

Testing an unrelated strlen optimization enhancement exposed another limitation
in the folding of strlen expressions.  GCC 8 is able to optimize code based on
the knowledge that the length of a string is bounded by the size of the array
it is stored in.  But it can only do that for strlen calls with no offset, or
for strlen calls with strings of known length and a constant offset.  Calls
that involve an offset into an array are not folded, irrespective of whether
the offset is constant.  Similarly, calls that involve strings of known length
and a non-const offset aren't folded.  Both sets of cases could be folded.

$ cat x.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout x.c | grep -e
"^f[0-9]" -e abort -estrlen
#define S "1234567"

char a[8] = S;

char a[8];

void f0 (void)
{
  if (__builtin_strlen (a) > 7)   // folded
    __builtin_abort ();
}

void f1 (void)
{
  if (__builtin_strlen (S + 1) > 6)   // folded
    __builtin_abort ();
}

void f2 (void)
{
  if (__builtin_strlen (a + 1) > 6)   // not folded but could be
    __builtin_abort ();
}

void f3 (int i)
{
  if (__builtin_strlen (S + i) > 7)   // not folded but could be
    __builtin_abort ();
}

void f4 (int i)
{
  if (__builtin_strlen (a + i) > 7)   // not folded but could be
    __builtin_abort ();
}

f0 ()
f1 ()
f2 ()
  _1 = __builtin_strlen (&MEM[(void *)&a + 1B]);
  __builtin_abort ();
f3 (int i)
  __builtin_abort ();
f4 (int i)
  _3 = __builtin_strlen (_2);
  __builtin_abort ();


More information about the Gcc-bugs mailing list