Transformations in fold do not properly handle integer types with value ranges (defined by TYPE_MIN_VALUE and TYPE_MAX_VALUE) that do not correspond to the types precision.
Citing from PR30911 comment #40 "The problem is in this transformation: /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */ X^Y may not be in the range of the type. In this case C=7 and op=NE_EXPR. The type of X and Y has range 4..5. Thus X^Y is either 0 or 2 (neither of which is in the range 4..5). Since the type of X^Y has range 4..5, the new fold code deduces that X^Y NE 0 is always true. I think the thing to do, as Richard Kenner suggested, is to convert each type to its base type right at the start of fold_comparison, at least if it has a base type (the same goes for other routines in fold). This unfortunately neutralizes your patch, since as it is right now you will only ever see base types. Thus this kind of check needs to happen at the start of fold_comparison, before the conversion to the base type." another source of problems is fold_convert and its siblings that happily create out-of-bounds constants or strip conversions to the base type. -- Summary: Fold is agnostic of integer sub-types Product: gcc Version: 4.3.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: middle-end AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: rguenth at gcc dot gnu dot org OtherBugsDependingO 30911 nThis: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31023