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]

Re: basic VRP min/max range overflow question


DJ Delorie wrote:
> 
> Create a -W* command line option for it.  Pass the corresponding OPT_*
> to warning().  Theen the -W option controls that warning.
> 

Here is a patch for warning every time an estimation of the loop bound
is computed based on the fact that data should not be accessed outside
the allocated size.  The warning is controlled by -Wloop-bound-estimated.

Bootstrapped on i686-linux.  Okay for mainline?

	* Makefile.in (tree-data-ref.o): Depend on DIAGNOSTIC_H, 
	tree-vectorizer.h and toplev.h.
	* common.opt (Wloop-bound-estimated): New.
	* diagnostic.c (warning): Document parameter opt.
	* tree-data-ref.c: Include toplev.h and tree-vectorizer.h.
	(estimate_niter_from_size_of_data): Warn when an estimation of the
	loop bound is computed.
	* doc/invoke.texi (-Wloop-bound-estimated): Document.

Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Makefile.in,v
retrieving revision 1.1521
diff -d -u -p -r1.1521 Makefile.in
--- Makefile.in	10 Jul 2005 00:27:44 -0000	1.1521
+++ Makefile.in	18 Jul 2005 14:24:54 -0000
@@ -1934,7 +1934,8 @@ tree-scalar-evolution.o: tree-scalar-evo
 tree-data-ref.o: tree-data-ref.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
    $(GGC_H) $(TREE_H) $(RTL_H) $(BASIC_BLOCK_H) $(DIAGNOSTIC_H) \
    $(TREE_FLOW_H) $(TREE_DUMP_H) $(TIMEVAR_H) $(CFGLOOP_H) \
-   $(TREE_DATA_REF_H) $(SCEV_H) tree-pass.h tree-chrec.h
+   $(TREE_DATA_REF_H) $(SCEV_H) tree-pass.h $(DIAGNOSTIC_H) \
+   tree-vectorizer.h toplev.h
 tree-vect-analyze.o: tree-vect-analyze.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
    $(TM_H) $(GGC_H) $(OPTABS_H) $(TREE_H) $(BASIC_BLOCK_H) \
    $(DIAGNOSTIC_H) $(TREE_FLOW_H) $(TREE_DUMP_H) $(TIMEVAR_H) $(CFGLOOP_H) \
Index: common.opt
===================================================================
RCS file: /cvs/gcc/gcc/gcc/common.opt,v
retrieving revision 1.78
diff -d -u -p -r1.78 common.opt
--- common.opt	2 Jul 2005 13:18:24 -0000	1.78
+++ common.opt	18 Jul 2005 14:24:55 -0000
@@ -93,6 +93,10 @@ Wlarger-than-
 Common RejectNegative Joined UInteger
 -Wlarger-than-<number>	Warn if an object is larger than <number> bytes
 
+Wloop-bound-estimated
+Common Var(warn_loop_bound_estimated)
+Warn when an estimation of loop bounds is deduced from undefined behavior
+
 Wmissing-noreturn
 Common Var(warn_missing_noreturn)
 Warn about functions which might be candidates for __attribute__((noreturn))
Index: diagnostic.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/diagnostic.c,v
retrieving revision 1.155
diff -d -u -p -r1.155 diagnostic.c
--- diagnostic.c	1 Jul 2005 14:26:18 -0000	1.155
+++ diagnostic.c	18 Jul 2005 14:24:55 -0000
@@ -427,7 +427,9 @@ inform (const char *gmsgid, ...)
 }
 
 /* A warning.  Use this for code which is correct according to the
-   relevant language specification but is likely to be buggy anyway.  */
+   relevant language specification but is likely to be buggy anyway.
+   OPT is the -W* flag (defined in common.opt) that controls the
+   warning.  Please avoid passing OPT = 0 to this function.  */
 void
 warning (int opt, const char *gmsgid, ...)
 {
Index: tree-data-ref.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-data-ref.c,v
retrieving revision 2.34
diff -d -u -p -r2.34 tree-data-ref.c
--- tree-data-ref.c	26 Jun 2005 21:21:30 -0000	2.34
+++ tree-data-ref.c	18 Jul 2005 14:24:55 -0000
@@ -80,11 +80,10 @@ Software Foundation, 51 Franklin Street,
 #include "tm.h"
 #include "ggc.h"
 #include "tree.h"
-
-/* These RTL headers are needed for basic-block.h.  */
 #include "rtl.h"
 #include "basic-block.h"
 #include "diagnostic.h"
+#include "toplev.h"
 #include "tree-flow.h"
 #include "tree-dump.h"
 #include "timevar.h"
@@ -93,6 +92,7 @@ Software Foundation, 51 Franklin Street,
 #include "tree-data-ref.h"
 #include "tree-scalar-evolution.h"
 #include "tree-pass.h"
+#include "tree-vectorizer.h"
 
 /* This is the simplest data dependence test: determines whether the
    data references A and B access the same array/region.  Returns
@@ -473,7 +473,6 @@ estimate_niter_from_size_of_data (struct
 				  tree access_fn, 
 				  tree stmt)
 {
-  tree estimation;
   tree array_size, data_size, element_size;
   tree init, step;
 
@@ -495,10 +494,14 @@ estimate_niter_from_size_of_data (struct
       && TREE_CODE (init) == INTEGER_CST
       && TREE_CODE (step) == INTEGER_CST)
     {
-      estimation = fold_build2 (CEIL_DIV_EXPR, integer_type_node,
-				fold_build2 (MINUS_EXPR, integer_type_node,
-					     data_size, init), step);
-
+      tree estimation = fold_build2 (CEIL_DIV_EXPR, integer_type_node,
+				     fold_build2 (MINUS_EXPR, integer_type_node,
+						  data_size, init), step);
+      LOC locus = find_loop_location (loop);
+      warning (OPT_Wloop_bound_estimated,
+	       "%Hundefined behavior if loop runs more than %lu iterations: "
+	       "access over allocated data bounds",
+	       locus, int_cst_value (estimation));
       record_estimate (loop, estimation, boolean_true_node, stmt);
     }
 }
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.647
diff -d -u -p -r1.647 invoke.texi
--- doc/invoke.texi	12 Jul 2005 10:38:45 -0000	1.647
+++ doc/invoke.texi	18 Jul 2005 14:25:03 -0000
@@ -230,7 +230,7 @@ Objective-C and Objective-C++ Dialects}.
 -Wimport  -Wno-import  -Winit-self  -Winline @gol
 -Wno-int-to-pointer-cast @gol
 -Wno-invalid-offsetof  -Winvalid-pch @gol
--Wlarger-than-@var{len}  -Wlong-long @gol
+-Wlarger-than-@var{len} -Wloop-bound-estimated -Wlong-long @gol
 -Wmain  -Wmissing-braces  -Wmissing-field-initializers @gol
 -Wmissing-format-attribute  -Wmissing-include-dirs @gol
 -Wmissing-noreturn @gol
@@ -2984,6 +2984,12 @@ global variable or whenever a built-in f
 @opindex Wlarger-than
 Warn whenever an object of larger than @var{len} bytes is defined.
 
+@item -Wloop-bound-estimated
+@opindex Wloop-bound-estimated
+Warn when an estimation of the number of iterations is computed based
+on the fact that compiled programs are correct and do not invoke
+undefined behavior.
+
 @item -Wpointer-arith
 @opindex Wpointer-arith
 Warn about anything that depends on the ``size of'' a function type or
Index: testsuite/gcc.dg/tree-ssa/ltrans-1.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/tree-ssa/ltrans-1.c,v
retrieving revision 1.2
diff -d -u -p -r1.2 ltrans-1.c
--- testsuite/gcc.dg/tree-ssa/ltrans-1.c	31 Mar 2005 18:34:15 -0000	1.2
+++ testsuite/gcc.dg/tree-ssa/ltrans-1.c	18 Jul 2005 14:25:05 -0000
@@ -1,5 +1,5 @@
 /* { dg-do compile } */ 
-/* { dg-options "-O2 -ftree-loop-linear -fdump-tree-ltrans-all" } */
+/* { dg-options "-O2 -ftree-loop-linear -fdump-tree-ltrans-all -Wloop-bound-estimated" } */
 
 double u[1782225];
 int foo(int N, int *res)
@@ -8,9 +8,9 @@ int foo(int N, int *res)
   double sum = 0.0;
   /* This loop should be converted to a perfect nest and
      interchanged. */
-  for (i = 0; i < N; i++)
+  for (i = 0; i < N; i++)      /* { dg-warning "undefined behavior if loop runs more than 1335 iterations: access over allocated data bounds" } */
     {
-      for (j = 0; j < N; j++)
+      for (j = 0; j < N; j++)  /* { dg-warning "undefined behavior if loop runs more than 1335 iterations: access over allocated data bounds" } */
 	sum = sum + u[i + 1335 * j];
       
       u[1336 * i] *= 2;


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