public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/35163]  New: folding comparison loses cast
@ 2008-02-11  9:30 Tom dot de dot Vries at Nxp dot com
  2008-02-11 10:45 ` [Bug c/35163] " steven at gcc dot gnu dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Tom dot de dot Vries at Nxp dot com @ 2008-02-11  9:30 UTC (permalink / raw)
  To: gcc-bugs

test.c:
...
int main() {

  signed char a = -30;

  /* a == sE2 (-30 signed char) */

  signed char b = -31;

  /* b == sE1 (-31 signed char) */

  int r = a <= (unsigned short)b;

  /* (unsigned short)b == uFFE1 (65505 unsigned short) */

  /*
    r 
      == 
    a <= (unsigned short)b
      == 
    sE2 <= uFFE1
      == arithmetic conversion, rule integral promotion
    sFFFFFFE2 <= s0000FFE1 
      == 
    -30 <= 65505 
      == 
    1
  */

  /* gcc evaluates r to 0 instead */

  return r;

}
...

The problem seems to occur during a call to
fold-const.c:fold_widened_comparison(), which calls tree.c:get_unwidened():
...
/* Return OP, stripped of any conversions to wider types as much as is safe.
   Converting the value back to OP's type makes a value equivalent to OP.

   If FOR_TYPE is nonzero, we return a value which, if converted to
   type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.

   If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
   narrowest type that can hold the value, even if they don't exactly fit.
   Otherwise, bit-field references are changed to a narrower type
   only if they can be fetched directly from memory in that type.

   OP must have integer, real or enumeral type.  Pointers are not allowed!

   There are some cases where the obvious value we could return
   would regenerate to OP if converted to OP's type,
   but would not extend like OP to wider types.
   If FOR_TYPE indicates such extension is contemplated, we eschew such values.
   For example, if OP is (unsigned short)(signed char)-1,
   we avoid returning (signed char)-1 if FOR_TYPE is int,
   even though extending that to an unsigned short would regenerate OP,
   since the result of extending (signed char)-1 to (int)
   is different from (int) OP.  */

tree
get_unwidened (tree op, tree for_type)
...

get_unwidened() is called with op == (int)(unsigned short)-31sc and for_type ==
signed char.

get unwidened() returns -31sc. However, 'Converting the value back to OP's type
makes a value equivalent to OP' would mean convert (int)-31sc == (int)(unsigned
short)-31sc, which is not correct: 

(int)-31sc == (int)(unsigned short)-31sc
  <==>
(int)-31sc == (int)65505us
  <==>
-31si == 65505si
  <==>
FALSE


-- 
           Summary: folding comparison loses cast
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Tom dot de dot Vries at Nxp dot com


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2008-03-25 10:19 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-11  9:30 [Bug c/35163] New: folding comparison loses cast Tom dot de dot Vries at Nxp dot com
2008-02-11 10:45 ` [Bug c/35163] " steven at gcc dot gnu dot org
2008-02-12 17:08 ` [Bug middle-end/35163] " rguenth at gcc dot gnu dot org
2008-02-12 19:37 ` [Bug middle-end/35163] [4.1/4.2/4.3 Regression] " rguenth at gcc dot gnu dot org
2008-02-12 20:00 ` rguenth at gcc dot gnu dot org
2008-02-12 21:28 ` rguenth at gcc dot gnu dot org
2008-02-12 21:30 ` [Bug middle-end/35163] [4.1/4.2 " rguenth at gcc dot gnu dot org
2008-02-17  3:59 ` pmarques at grupopie dot com
2008-03-25 10:15 ` rguenth at gcc dot gnu dot org
2008-03-25 10:18 ` rguenth at gcc dot gnu dot org
2008-03-25 10:19 ` rguenth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).