This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix PR15791
- From: Richard Guenther <rguenth at tat dot physik dot uni-tuebingen dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 27 Jan 2005 15:57:36 +0100 (CET)
- Subject: [PATCH] Fix PR15791
Hi!
This patch adds the capability to fold comparisons of the form
&a[i] OP &a[j] (or plain a) as in PR15791. It does not handle
&x.a[i] OP &x.a[j] currently, as these are presented in a
different lowered form to fold.
Bootstrapped and tested on i686-pc-linux-gnu. Ok?
(if yes, please apply)
Richard.
2005-Jan-27 Richard Guenther <richard.guenther@uni-tuebingen.de>
* fold-const.c (fold): fold comparisons between &a[i] and
&a[j] or the equivalent zero-offset &a.
Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.497
diff -u -c -3 -p -r1.497 fold-const.c
*** fold-const.c 23 Jan 2005 15:05:29 -0000 1.497
--- fold-const.c 26 Jan 2005 16:15:46 -0000
*************** fold (tree expr)
*** 8245,8250 ****
--- 8245,8287 ----
? code == EQ_EXPR : code != EQ_EXPR,
type);
+ /* If this is a comparison of two ARRAY_REFs of the same object,
+ which is folded to (comp (+ (&a i)) (+ (&a j))), then we can
+ fold this to a comparison of the two offsets i and j. */
+ if (COMPARISON_CLASS_P (t)
+ && ((TREE_CODE (arg0) == PLUS_EXPR
+ && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR)
+ || TREE_CODE (arg0) == ADDR_EXPR)
+ && ((TREE_CODE (arg1) == PLUS_EXPR
+ && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR)
+ || TREE_CODE (arg1) == ADDR_EXPR))
+ {
+ tree addr0, addr1, off0, off1;
+ if (TREE_CODE (arg0) == ADDR_EXPR)
+ {
+ addr0 = arg0;
+ off0 = integer_zero_node;
+ }
+ else
+ {
+ addr0 = TREE_OPERAND (arg0, 0);
+ off0 = TREE_OPERAND (arg0, 1);
+ }
+ if (TREE_CODE (arg1) == ADDR_EXPR)
+ {
+ addr1 = arg1;
+ off1 = integer_zero_node;
+ }
+ else
+ {
+ addr1 = TREE_OPERAND (arg1, 0);
+ off1 = TREE_OPERAND (arg1, 1);
+ }
+ if (operand_equal_p (addr0, addr1, 0))
+ return fold (build2 (code, type, off0, off1));
+ }
+
+
if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
{
tree targ0 = strip_float_extensions (arg0);