This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fold comparison of local addresses
- From: Richard Guenther <rguenther at suse dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Tue, 25 Mar 2008 15:46:52 +0100 (CET)
- Subject: [PATCH] Fold comparison of local addresses
I was surprised we don't handle this yet.
Bootstrap & regtest in progress, I'll apply it after that succeeded.
Richard.
2008-03-25 Richard Guenther <rguenther@suse.de>
* Makefile.in (fold-const.o): Add $(TARGET_H) dependency.
* fold-const.c (target.h): Include.
(fold_comparison): Fold comparison of addresses of two decls
that bind locally.
* gcc.dg/fold-addr-1.c: New testcase.
Index: Makefile.in
===================================================================
*** Makefile.in (revision 133506)
--- Makefile.in (working copy)
*************** tree-pretty-print.o : tree-pretty-print.
*** 2321,2327 ****
value-prof.h fixed-value.h output.h
fold-const.o : fold-const.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
$(TREE_H) $(FLAGS_H) $(REAL_H) toplev.h $(HASHTAB_H) $(EXPR_H) $(RTL_H) \
! $(GGC_H) $(TM_P_H) langhooks.h $(MD5_H) intl.h fixed-value.h
diagnostic.o : diagnostic.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
$(TREE_H) version.h $(TM_P_H) $(FLAGS_H) input.h toplev.h intl.h \
$(DIAGNOSTIC_H) langhooks.h $(LANGHOOKS_DEF_H) diagnostic.def opts.h
--- 2321,2327 ----
value-prof.h fixed-value.h output.h
fold-const.o : fold-const.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
$(TREE_H) $(FLAGS_H) $(REAL_H) toplev.h $(HASHTAB_H) $(EXPR_H) $(RTL_H) \
! $(GGC_H) $(TM_P_H) langhooks.h $(MD5_H) intl.h fixed-value.h $(TARGET_H)
diagnostic.o : diagnostic.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
$(TREE_H) version.h $(TM_P_H) $(FLAGS_H) input.h toplev.h intl.h \
$(DIAGNOSTIC_H) langhooks.h $(LANGHOOKS_DEF_H) diagnostic.def opts.h
Index: fold-const.c
===================================================================
*** fold-const.c (revision 133506)
--- fold-const.c (working copy)
*************** along with GCC; see the file COPYING3.
*** 58,63 ****
--- 58,64 ----
#include "rtl.h"
#include "expr.h"
#include "tm_p.h"
+ #include "target.h"
#include "toplev.h"
#include "intl.h"
#include "ggc.h"
*************** fold_comparison (enum tree_code code, tr
*** 8581,8586 ****
--- 8582,8598 ----
return fold_build2 (code, type, offset0, offset1);
}
}
+ /* For non-equal bases we can simplify if they are plain decls. */
+ else if (base0 && base1
+ && DECL_P (base0) && DECL_P (base1)
+ && targetm.binds_local_p (base0)
+ && targetm.binds_local_p (base1))
+ {
+ if (code == EQ_EXPR)
+ return omit_two_operands (type, boolean_false_node, arg0, arg1);
+ else if (code == NE_EXPR)
+ return omit_two_operands (type, boolean_true_node, arg0, arg1);
+ }
}
/* Transform comparisons of the form X +- C1 CMP Y +- C2 to
Index: testsuite/gcc.dg/fold-addr-1.c
===================================================================
*** testsuite/gcc.dg/fold-addr-1.c (revision 0)
--- testsuite/gcc.dg/fold-addr-1.c (revision 0)
***************
*** 0 ****
--- 1,10 ----
+ /* { dg-do compile } */
+ /* { dg-options "-fdump-tree-original" } */
+
+ int bar(char p1, char p2)
+ {
+ return &p1 == &p2;
+ }
+
+ /* { dg-final { scan-tree-dump "return 0;" "original" } } */
+ /* { dg-final { cleanup-tree-dump "original" } } */