Hi,
this patch sovles the incorrect folding. The very same unification (ignoring
signedness by checking that memory representation is the same) is done by
constant pool.

Some of the other uses of ctor_for_folding therefore already uses
VIEW_CONVERT_EXPR, I suppose as a partial fix for past bugs. This particular
case is handled by get_symbol_constant_value that does not VCE. Maybe
we usually don't drop scalar constant to constant pool that often, so this
did not show up.

Attached is non-ICF testcase. It is bit questionable if we consider this to be
valid, but it is better to be safe than sorry.  Mixing signed/unsigned may be
more common with LTO.

Bootstrap/regtest running in x86_64-linux, seems sane?

Honza

static short a __attribute__ ((alias ("c")));
short b = -1;
static unsigned short c = 0;

int
main ()
{
  if (a <= b)
    return 1;
  return 0;
}


        * gimple-fold.c (get_symbol_constant_value): Convert to symbol type.

Index: gimple-fold.c
===================================================================
--- gimple-fold.c       (revision 221170)
+++ gimple-fold.c       (working copy)
@@ -263,7 +263,16 @@ get_symbol_constant_value (tree sym)
        {
          val = canonicalize_constructor_val (unshare_expr (val), sym);
          if (val && is_gimple_min_invariant (val))
-           return val;
+           {
+              if (!useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE 
(val)))
+               {
+                 if (operand_equal_p (TYPE_SIZE (TREE_TYPE (sym)),
+                                      TYPE_SIZE (TREE_TYPE (val)), 0))
+                   return NULL_TREE;
+                 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (sym), val);
+               }
+             return val;
+           }
          else
            return NULL_TREE;
        }

Reply via email to