I'd expect that at least the same code is generated for both cases as opposed
to creating worse code for the case where an explicit assignment is done.
I had hoped that the explicit assignment actually creates smaller code.
$ gcc -DDOINC -Os -march=i386 -c -o scan.o.orig scan.c
$ gcc -UDOINC -Os -march=i386 -c -o scan.o scan.c
$ size scan.o*
text data bss dec hex filename
127 0 0 127 7f scan.o
123 0 0 123 7b scan.o.orig
$ cat scan.c
#include <stdio.h>
#include <dirent.h>
static void check(int pid) { fprintf(stderr,"found %i\n", pid); }
int main (void)
{
DIR *procdir;
struct dirent *entry;
int foundany, pid;
procdir = opendir ("/proc");
foundany = 0;
while ((entry = readdir (procdir)) != NULL)
{
if (sscanf (entry->d_name, "%d", &pid) != 1)
continue;
#ifdef DOINC
foundany++;
#else
foundany = 1;
#endif
check (pid);
}
closedir (procdir);
if (!foundany)
return 1; /* bb_perror_msg_and_die("nothing in /proc - not mounted?");*/
return 0;
}
--
Summary: incrementing a variable produces smaller code than an
explicit assignment
Product: gcc
Version: 4.2.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: aldot at gcc dot gnu dot org
GCC build triplet: i386-linux-gnu
GCC host triplet: i386-linux-gnu
GCC target triplet: i386-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28001