This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

gcc const int expressions and negative constants


I thought I'd have a crack at working out why the following code
(adapted from a bug report in GNATS) fails:

int main(void)
{
  unsigned char sign;
  unsigned char integer[2] = {255};

  sign = integer[0];

  printf ( "sign == 255: %d\n", sign == 255);

  return 0;
}

Here, when optimizing, if the variable "integer" is an array of size
2, the output is 0.  If it is an array of size 1, the output is 1.

The cause of the bug is that the combine pass is breaking the
comparison "sign == 255" down to a comparison of two constants.  One
constant is the immediate 255 appearing in the test.  The other is -1
or 255, depending upon the size of "integer".

What I'm confused about is whether the correct form of constants
appearing in QImode expressions is (HOST_WIDE_INT) 255 or -1?  The
initial line

  unsigned char integer[2] = {255};

produces a QImode store with constant -1.  Combine makes that into a
HImode store with constant 255.  Then

  sign = integer[0];

produces another QImode store of -1, but then the mode is lost in the
final comparison to 255.

This seems like a large can of worms.  If someone can tell me what the
intended correct method of using the constants is, I can try and fix
it.

Neil.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]