[Bug middle-end/80362] [5/6/7 Regression] gcc miscompiles arithmetic with signed char

2017-04-10 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80362

--- Comment #3 from Richard Biener  ---
10205 /* Convert -A / -B to A / B when the type is signed and overflow
is
10206undefined.  */
10207 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10208 && TREE_CODE (arg0) == NEGATE_EXPR
10209 && negate_expr_p (op1))
10210   {

where op1 is 3.  We're folding (signed char) -(unsigned char) var_1 / 3 and
arg0 is - (unsigned char) var_1 but _unsigned_.

Trivial fix:

Index: gcc/fold-const.c
===
--- gcc/fold-const.c(revision 246797)
+++ gcc/fold-const.c(working copy)
@@ -10205,7 +10205,7 @@ fold_binary_loc (location_t loc,
   /* Convert -A / -B to A / B when the type is signed and overflow is
 undefined.  */
   if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
- && TREE_CODE (arg0) == NEGATE_EXPR
+ && TREE_CODE (op0) == NEGATE_EXPR
  && negate_expr_p (op1))
{
  if (INTEGRAL_TYPE_P (type))

[Bug middle-end/80362] [5/6/7 Regression] gcc miscompiles arithmetic with signed char

2017-04-09 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80362

Richard Biener  changed:

   What|Removed |Added

   Priority|P3  |P2
 Status|NEW |ASSIGNED
  Known to work||3.4.6, 4.1.2
   Assignee|unassigned at gcc dot gnu.org  |rguenth at gcc dot 
gnu.org
   Target Milestone|--- |5.5
Summary|gcc miscompiles arithmetic  |[5/6/7 Regression] gcc
   |with signed char|miscompiles arithmetic with
   ||signed char
  Known to fail||4.3.6

--- Comment #2 from Richard Biener  ---
Fails with -O0 -fstrict-overflow already.  Folded to

signed char var_1 = -128;
  var_0 = var_1 / -3;
  if (var_0 > 0)

either extract_muldiv or fold_negate.  Mine.