valid_gimple_expression_p claims validity of invalid gimple

Eric Botcazou ebotcazou@adacore.com
Sat Jul 14 08:07:00 GMT 2007


> First is_gimple_min_invariant in try_to_simplify where it chooses
> DECL_INITIAL should be valid_gimple_expression_p instead.

That's a known problem, see tree-ssa-ccp.c:

/* The regular is_gimple_min_invariant does a shallow test of the object.
   It assumes that full gimplification has happened, or will happen on the
   object.  For a value coming from DECL_INITIAL, this is not true, so we
   have to be more strict ourselves.  */

static bool
ccp_decl_initial_min_invariant (tree t)
{
  if (!is_gimple_min_invariant (t))
    return false;
  if (TREE_CODE (t) == ADDR_EXPR)
    {
      /* Inline and unroll is_gimple_addressable.  */
      while (1)
	{
	  t = TREE_OPERAND (t, 0);
	  if (is_gimple_id (t))
	    return true;
	  if (!handled_component_p (t))
	    return false;
	}
    }
  return true;
}

static tree
get_symbol_constant_value (tree sym)
{
  if (TREE_STATIC (sym)
      && TREE_READONLY (sym)
      && !MTAG_P (sym))
    {
      tree val = DECL_INITIAL (sym);
      if (val
	  && ccp_decl_initial_min_invariant (val))
	return val;
    }

  return NULL_TREE;
}

You could turn ccp_decl_initial_min_invariant into a global predicate and 
invoke it from tree-ssa-sccvn.c on the DECL_INITIAL.

> However, even if i fix this, the testcase still fails because
> valid_gimple_expression says something that is clearly invalid is
> valid.
>
> (gdb) p valid_gimple_expression_p ($2)
> $3 = 1 '\001'
> (gdb) p debug_generic_stmt ($2)
> &((struct RegisterLayout *) (char *) &SimulatedRegisters)->intmask;
>
> This is not valid gimple by a longshot :)

Almost. :-)  The function was extracted unchanged from set_rhs but it looks 
like it needs to be beefed up a bit if it is to become of general use.

> If you fix this part, i'll happily fix the bug report with the first part.

The problem is again the ADDR_EXPR case, because it drags lvalues into the 
game.  This would be something like:

Index: tree-ssa-propagate.c
===================================================================
--- tree-ssa-propagate.c	(revision 126545)
+++ tree-ssa-propagate.c	(working copy)
@@ -606,10 +606,21 @@ valid_gimple_expression_p (tree expr)
       switch (code)
 	{
 	case ADDR_EXPR:
-          if (TREE_CODE (TREE_OPERAND (expr, 0)) == ARRAY_REF
-	      && !is_gimple_val (TREE_OPERAND (TREE_OPERAND (expr, 0), 1)))
-	    return false;
-	  break;
+	  {
+	    tree t = TREE_OPERAND (expr, 0);
+	    while (handled_component_p (t))
+	      {
+	        /* ??? More checks needed, see the GIMPLE verifier.  */
+		if ((TREE_CODE (t) == ARRAY_REF
+		     || TREE_CODE (t) == ARRAY_RANGE_REF)
+		    && !is_gimple_val (TREE_OPERAND (t, 1)))
+		  return false;
+		t = TREE_OPERAND (t, 0);
+	      }
+	    if (!is_gimple_addressable (t))
+	      return false;
+	    break;
+	  }
 
 	case TRUTH_NOT_EXPR:
 	  if (!is_gimple_val (TREE_OPERAND (expr, 0)))

-- 
Eric Botcazou



More information about the Gcc mailing list