This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] tree-vrp.c: Correct the type of a comparison expression.


Hi,

Attached is a patch to correct the type of a comparisson expression.

Consider

  t = fold (build2 (LT_EXPR, TREE_TYPE (val1), val1, val2));
  if (t == boolean_true_node)
    return -1;

>From the context, VAL1 and VAL2 are known to be of a pointer type, so
the resulting tree will never be of the boolean type.  Thus
"t == boolean_true_node" will always evaluate to false.

To fix this problem, we need to use boolean_type_node like so

  t = fold (build2 (LT_EXPR, boolean_type_node, val1, val2));
  if (t == boolean_true_node)
    return -1;

But note that we are only interested in a case where folding is
successful, so we can simply use fold_binary like so.

  t = fold_binary (LT_EXPR, boolean_type_node, val1, val2));
  if (t == boolean_true_node)
    return -1;

Recently, Mark Mitchell agreed to export these fold_ARITY functions as
needed.  Since exporting only fold_binary feels a bit strange, the
patch exports them all.

Unfortunately, a quick testing with cc1-i files shows that correcting
these types does not increase the number of COND_EXPRs that are
folded.  Later I might come back here and see how effective these
pointer comparisons are for VRP purposes, but for now, I would like to
separate a rather obvious bug and VRP's performance issue.

Tested on i686-pc-linux-gnu.  OK to apply?

Kazu Hirata

2005-04-22  Kazu Hirata  <kazu@cs.umass.edu>

	PR tree-optimization/21088
	* fold-const.c (fold_unary, fold_binary, fold_ternary):
	Export.
	* tree-vrp.c (compare_values): Use fold_binary to compare
	pointers.  Use boolean_type_node as the type of a comparison
	expression being folded.
	* tree.h: Add prototypes for fold_unary, fold_binary,
	fold_ternary.

Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.567
diff -u -d -p -r1.567 fold-const.c
--- fold-const.c	21 Apr 2005 00:39:22 -0000	1.567
+++ fold-const.c	22 Apr 2005 18:51:45 -0000
@@ -6678,7 +6678,7 @@ fold_complex_div (tree type, tree ac, tr
    OP0.  Return the folded expression if folding is successful.
    Otherwise, return NULL_TREE.  */
 
-static tree
+tree
 fold_unary (enum tree_code code, tree type, tree op0)
 {
   tree tem;
@@ -7113,7 +7113,7 @@ fold_unary (enum tree_code code, tree ty
    OP0 and OP1.  Return the folded expression if folding is
    successful.  Otherwise, return NULL_TREE.  */
 
-static tree
+tree
 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
 {
   tree t1 = NULL_TREE;
@@ -9852,7 +9852,7 @@ fold_binary (enum tree_code code, tree t
    OP0, OP1, and OP2.  Return the folded expression if folding is
    successful.  Otherwise, return NULL_TREE.  */
 
-static tree
+tree
 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
 {
   tree tem;
Index: tree-vrp.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-vrp.c,v
retrieving revision 2.11
diff -u -d -p -r2.11 tree-vrp.c
--- tree-vrp.c	17 Apr 2005 17:57:10 -0000	2.11
+++ tree-vrp.c	22 Apr 2005 18:51:54 -0000
@@ -416,17 +416,17 @@ compare_values (tree val1, tree val2)
 	return 0;
       
       /* If VAL1 is a lower address than VAL2, return -1.  */
-      t = fold (build2 (LT_EXPR, TREE_TYPE (val1), val1, val2));
+      t = fold_binary (LT_EXPR, boolean_type_node, val1, val2);
       if (t == boolean_true_node)
 	return -1;
 
       /* If VAL1 is a higher address than VAL2, return +1.  */
-      t = fold (build2 (GT_EXPR, TREE_TYPE (val1), val1, val2));
+      t = fold_binary (GT_EXPR, boolean_type_node, val1, val2);
       if (t == boolean_true_node)
 	return 1;
 
       /* If VAL1 is different than VAL2, return +2.  */
-      t = fold (build2 (NE_EXPR, TREE_TYPE (val1), val1, val2));
+      t = fold_binary (NE_EXPR, boolean_type_node, val1, val2);
       if (t == boolean_true_node)
 	return 2;
 
Index: tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree.h,v
retrieving revision 1.717
diff -u -d -p -r1.717 tree.h
--- tree.h	22 Apr 2005 10:56:59 -0000	1.717
+++ tree.h	22 Apr 2005 18:51:55 -0000
@@ -3518,6 +3518,9 @@ extern void using_eh_for_cleanups (void)
    subexpressions are not changed.  */
 
 extern tree fold (tree);
+extern tree fold_unary (enum tree_code, tree, tree);
+extern tree fold_binary (enum tree_code, tree, tree, tree);
+extern tree fold_ternary (enum tree_code, tree, tree, tree, tree);
 extern tree fold_build1 (enum tree_code, tree, tree);
 extern tree fold_build2 (enum tree_code, tree, tree, tree);
 extern tree fold_build3 (enum tree_code, tree, tree, tree, tree);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]