Consider the following test program:

#include <string>
bool has_bad_chars(std::string const & path)
{
  for (std::string::const_iterator c = path.begin(); c != path.end(); c++)
    {
      unsigned char x = static_cast<unsigned char>(*c);
      if (x <= 0x1f || x == 0x5c || x == 0x7f)
        return true;
    }
  return false;
}

At -O2, GCC 4.1 chooses to duplicate the entire body of the loop for no good
reason; the code is not rendered more straight-line by this, and in fact it
executes strictly more instructions even for a single-character string.  I'll
attach an assembly file showing what it did (Z13has_bad_charsRKSs) and what it
should have done (_Z14has_bad_chars2RKSs).  The bad transformation is done by
the .t45.ch pass, which acronym does not mean anything to me.


-- 
           Summary: poor optimization choices when iterating over a
                    std::string (probably not c++-specific)
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: zackw at panix dot com
 GCC build triplet: i486-linux-gnu
  GCC host triplet: i486-linux-gnu
GCC target triplet: i486-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28364

Reply via email to