[Bug c/32757] New: Optimizer too aggressive

2007-07-13 Thread frederic dot merizen at gmail dot com
When compiling the following code with -O2 or -Os, the if clause at the end of
STRING_hash_code is optimized away, yielding in a negative result
  if(R<0){
R=~R;
  }

With -O1 everything works as expected (positive result)

#include 
#include 
#include 

typedef struct {unsigned char* storage;int32_t count;int32_t capacity;} STRING;

int main(int argc, char *argv[]) {
  STRING x;
 
x.storage="HASHED_DICTIONARY[WEAK_REFERENCE[ANY_HASHED_DICTIONARY_NODE],STRING]";
  x.count=strlen(x.storage);
  x.capacity=x.count+1;
  printf("%d\n",STRING_hash_code(&x));
}

int32_t STRING_hash_code(STRING* C){
  int32_t R=0;
  int32_t i=0;
  int32_t j=0;
  j=C->count;
  i=1;
  while (j>0)
{
  R=5*R+C->storage[i-1];
  i++;
  j--;
}
  if(R<0){
R=~R;
  }
  return R;
}


-- 
   Summary: Optimizer too aggressive
   Product: gcc
   Version: 4.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: frederic dot merizen at gmail dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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



[Bug c/32757] Optimizer too aggressive

2007-07-13 Thread frederic dot merizen at gmail dot com


--- Comment #5 from frederic dot merizen at gmail dot com  2007-07-13 21:44 
---
OK. I assumed signed overflow was at least defined to yield an integer (i.e. a
quantity that is consistently negative or non-negative) but that is actually
not specified. I don't quite know what I'll do with our library yet. Adding
lots of unsigned/signed casts is an option, but it's likely to break a lot of
valid optimizations so I'll try to avoid that. Would the -fno-strict-overflow
option be a valid workaround in the meantime?


-- 


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