]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
flags.h (flag_wrapv): New flag controlling overflow semantics.
authorRoger Sayle <roger@eyesopen.com>
Sat, 31 May 2003 13:23:32 +0000 (13:23 +0000)
committerRoger Sayle <sayle@gcc.gnu.org>
Sat, 31 May 2003 13:23:32 +0000 (13:23 +0000)
* flags.h (flag_wrapv): New flag controlling overflow semantics.
* toplev.c (flag_wrapv): Declare the variable with default false.
(lang_independent_options): New option "-fwrapv" to set the above.

* fold-const.c (extract_muldiv_1): Disable optimization of (2*x)/2
as x, when signed arithmetic overflow wraps around.
(fold): Optimize "-A - B" as "-B - A" if overflow wraps around.
* loop.c (basic_induction_var): Ignore BIVs that rely on undefined
overflow when flag_wrapv is true.

* java/lang.c (java_init_options): Prescribe wrap-around two's
complement arithmetic overflow by setting flag_wrapv.

* doc/invoke.texi: Document new -fwrapv command line option.
* doc/c-tree.texi: Mention that the overflow semantics of
NEGATE_EXPR, PLUS_EXPR, MINUS_EXPR and MULT_EXPR is dependent
upon both flag_wrapv and flag_trapv.

* gcc.dg/fwrapv-1.c: New test case.
* gcc.dg/fwrapv-2.c: New test case.

* libjava.lang/Overflow.java: New test.
* libjava.lang/Overflow.out: New file.

From-SVN: r67270

15 files changed:
gcc/ChangeLog
gcc/doc/c-tree.texi
gcc/doc/invoke.texi
gcc/flags.h
gcc/fold-const.c
gcc/java/ChangeLog
gcc/java/lang.c
gcc/loop.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/fwrapv-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/fwrapv-2.c [new file with mode: 0644]
gcc/toplev.c
libjava/testsuite/ChangeLog
libjava/testsuite/libjava.lang/Overflow.java [new file with mode: 0644]
libjava/testsuite/libjava.lang/Overflow.out [new file with mode: 0644]

index af2db15a4fdeec57331bba1bee7647ce6885f6e6..061d593a65877f615d89e6cb83907e011bef8de9 100644 (file)
@@ -1,3 +1,20 @@
+2003-05-31  Roger Sayle  <roger@eyesopen.com>
+
+       * flags.h (flag_wrapv): New flag controlling overflow semantics.
+       * toplev.c (flag_wrapv): Declare the variable with default false.
+       (lang_independent_options): New option "-fwrapv" to set the above.
+
+       * fold-const.c (extract_muldiv_1): Disable optimization of (2*x)/2 
+       as x, when signed arithmetic overflow wraps around.
+       (fold): Optimize "-A - B" as "-B - A" if overflow wraps around.
+       * loop.c (basic_induction_var): Ignore BIVs that rely on undefined
+       overflow when flag_wrapv is true.
+
+       * doc/invoke.texi: Document new -fwrapv command line option.
+       * doc/c-tree.texi: Mention that the overflow semantics of
+       NEGATE_EXPR, PLUS_EXPR, MINUS_EXPR and MULT_EXPR is dependent
+       upon both flag_wrapv and flag_trapv.
+
 2003-05-31  Eric Botcazou  <ebotcazou@libertysurf.fr>
 
        * doc/install.texi (mips-sgi-irix5): Add missing
index ff5a5fa54b6c39de258557aa49e2146fe157660c..48e7e120cb05b9da928ffbd260492b456cacad4a 100644 (file)
@@ -1914,6 +1914,9 @@ These nodes represent unary negation of the single operand, for both
 integer and floating-point types.  The type of negation can be
 determined by looking at the type of the expression.
 
+The behavior of this operation on signed arithmetic overflow is
+controlled by the @code{flag_wrapv} and @code{flag_trapv} variables.
+
 @item BIT_NOT_EXPR
 These nodes represent bitwise complement, and will always have integral
 type.  The only operand is the value to be complemented.
@@ -2067,6 +2070,9 @@ The @code{TRUNC_MOD_EXPR} of two operands @code{a} and @code{b} is
 always @code{a - (a/b)*b} where the division is as if computed by a
 @code{TRUNC_DIV_EXPR}.
 
+The behavior of these operations on signed arithmetic overflow is
+controlled by the @code{flag_wrapv} and @code{flag_trapv} variables.
+
 @item ARRAY_REF
 These nodes represent array accesses.  The first operand is the array;
 the second is the index.  To calculate the address of the memory
index df4385049c1a0c2e7fe6ec46722cbd076cfbc288..e3edc0790e7413a5e1800ccb78cf7ca711021c07 100644 (file)
@@ -680,7 +680,7 @@ in the following sections.
 -fargument-alias  -fargument-noalias @gol
 -fargument-noalias-global  -fleading-underscore @gol
 -ftls-model=@var{model} @gol
--ftrapv  -fbounds-check}
+-ftrapv  -fwrapv  -fbounds-check}
 @end table
 
 @menu
@@ -10850,6 +10850,14 @@ this option defaults to true and false respectively.
 This option generates traps for signed overflow on addition, subtraction,
 multiplication operations.
 
+@item -fwrapv
+@opindex fwrapv
+This option instructs the compiler to assume that signed arithmetic
+overflow of addition, subtraction and multiplication wraps around
+using twos-complement representation.  This flag enables some optimzations
+and disables other.  This option is enabled by default for the Java
+front-end, as required by the Java language specification.
+
 @item -fexceptions
 @opindex fexceptions
 Enable exception handling.  Generates extra code needed to propagate
index 4050517d37968ee4fda651cebe8ddf8a35b83bad..c90428816d76b96651d2fb301c1d199fa01f08b8 100644 (file)
@@ -585,6 +585,9 @@ extern int frame_pointer_needed;
    for PLUS / SUB / MULT.  */
 extern int flag_trapv;
 
+/* Nonzero if the signed arithmetic overflow should wrap around.  */
+extern int flag_wrapv;
+
 /* Value of the -G xx switch, and whether it was passed or not.  */
 extern unsigned HOST_WIDE_INT g_switch_value;
 extern int g_switch_set;
index 381bc9cb959e97d8a8465d9f0d6a5240792df36b..e81e8090cf00207c0bf9fb9e5a0cc9fe82a9fd31 100644 (file)
@@ -4381,6 +4381,7 @@ extract_muldiv_1 (t, c, code, wide_type)
         overflowed.  */
       if ((! TREE_UNSIGNED (ctype)
           || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
+         && ! flag_wrapv
          && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
              || (tcode == MULT_EXPR
                  && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
@@ -5765,7 +5766,8 @@ fold (expr)
        return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
       if (TREE_CODE (arg0) == NEGATE_EXPR
-         && FLOAT_TYPE_P (type)
+         && (FLOAT_TYPE_P (type)
+             || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
          && negate_expr_p (arg1)
          && (! TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
          && (! TREE_SIDE_EFFECTS (arg1) || TREE_CONSTANT (arg0)))
index 8dc9ecaf2e533c7f353db0e49e416de4cd3f5469..b41f316c5e20d25e5f995f02087c9741c0ae563c 100644 (file)
@@ -1,3 +1,8 @@
+2003-05-31  Roger Sayle  <roger@eyesopen.com>
+
+       * lang.c (java_init_options): Prescribe wrap-around two's
+       complement arithmetic overflow by setting flag_wrapv.
+
 2003-05-29  Roger Sayle  <roger@eyesopen.com>
 
        * builtins.c (cos_builtin, sin_builtin, sqrt_builtin): Delete.
index 031ddcc38b81802e58116df0735d70d6a5346f0e..918be0a5d7d5c04b8fd2ea350176f51a957087e9 100644 (file)
@@ -740,6 +740,9 @@ java_init_options (void)
 
   /* In Java floating point operations never trap.  */
   flag_trapping_math = 0;
+
+  /* In Java arithmetic overflow always wraps around.  */
+  flag_wrapv = 1;
 }
 
 static bool
index e5ae479027a8a9843d0a1135655ad1647efce29c..737ec89689350990df8866f8199b9553b544a874 100644 (file)
@@ -6462,6 +6462,9 @@ basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
        return 0;
 
     case SIGN_EXTEND:
+      /* Ignore this BIV if signed arithmetic overflow is defined.  */
+      if (flag_wrapv)
+       return 0;
       return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
                                  dest_reg, p, inc_val, mult_val, location);
 
index 628551e7948d457ab39adc1d1d74f811d2349efe..65d84e7c164d2fd4d5b339d62cb2b7f8dec138f4 100644 (file)
@@ -1,3 +1,8 @@
+2003-05-31  Roger Sayle  <roger@eyesopen.com>
+
+       * gcc.dg/fwrapv-1.c: New test case.
+       * gcc.dg/fwrapv-2.c: New test case.
+
 2003-05-31  Kriang Lerdsuwanakij  <lerdsuwa@users.sourceforge.net>
 
        PR c++/10956
diff --git a/gcc/testsuite/gcc.dg/fwrapv-1.c b/gcc/testsuite/gcc.dg/fwrapv-1.c
new file mode 100644 (file)
index 0000000..f7ddda4
--- /dev/null
@@ -0,0 +1,28 @@
+/* Copyright (C) 2003 Free Software Foundation.
+
+   Test that the -fwrapv command line option is accepted and disables
+   "unsafe" optimizations that rely on undefined arithmetic overflow.
+
+   Written by Roger Sayle, 24th March 2003.  */
+
+/* { dg-do run } */
+/* { dg-options "-O2 -fwrapv" } */
+
+#include <limits.h>
+
+extern void abort ();
+
+int test(int x)
+{
+  return (2*x)/2;
+}
+
+main()
+{
+  int x = INT_MAX;
+
+  if (test(x) == x)
+    abort ();
+  return 0;
+}
+
diff --git a/gcc/testsuite/gcc.dg/fwrapv-2.c b/gcc/testsuite/gcc.dg/fwrapv-2.c
new file mode 100644 (file)
index 0000000..cb4270e
--- /dev/null
@@ -0,0 +1,28 @@
+/* Copyright (C) 2003 Free Software Foundation.
+
+   Test that the -fno-wrapv command line option is accepted and enables
+   "unsafe" optimizations that rely on undefined arithmetic overflow.
+
+   Written by Roger Sayle, 31st May 2003.  */
+
+/* { dg-do run } */
+/* { dg-options "-O2 -fno-wrapv" } */
+
+#include <limits.h>
+
+extern void abort ();
+
+int test(int x)
+{
+  return (2*x)/2;
+}
+
+main()
+{
+  int x = INT_MAX;
+
+  if (test(x) != x)
+    abort ();
+  return 0;
+}
+
index 7cfb29e8ef6d274cf137bfd2131309126e5fd8cf..f2c352f8a083fd52226d2078dd2bf044a509af60 100644 (file)
@@ -977,8 +977,12 @@ typedef struct
 }
 lang_independent_options;
 
+/* Nonzero if signed arithmetic overflow should trap.  */
 int flag_trapv = 0;
 
+/* Nonzero if signed arithmetic overflow should wrap around.  */
+int flag_wrapv = 0;
+
 /* Add or remove a leading underscore from user symbols.  */
 int flag_leading_underscore = -1;
 
@@ -1220,6 +1224,8 @@ static const lang_independent_options f_options[] =
    N_("Report on permanent memory allocation at end of run") },
   { "trapv", &flag_trapv, 1,
    N_("Trap for signed overflow in addition / subtraction / multiplication") },
+  { "wrapv", &flag_wrapv, 1,
+   N_("Assume signed arithmetic overflow wraps around") },
   { "new-ra", &flag_new_regalloc, 1,
    N_("Use graph coloring register allocation.") },
 };
index 6ea10fff1bebe8e71326b1f0333099871e401c83..ee7883b2f8f03c5ff3adec19691172ea2f985617 100644 (file)
@@ -1,3 +1,8 @@
+2003-05-31  Roger Sayle  <roger@eyesopen.com>
+
+       * libjava.lang/Overflow.java: New test.
+       * libjava.lang/Overflow.out: New file.
+
 2003-05-06  Tom Tromey  <tromey@redhat.com>
 
        * libjava.lang/verify.java: New file.
diff --git a/libjava/testsuite/libjava.lang/Overflow.java b/libjava/testsuite/libjava.lang/Overflow.java
new file mode 100644 (file)
index 0000000..312e9cb
--- /dev/null
@@ -0,0 +1,16 @@
+class Overflow
+{
+  static int test(int x)
+  {
+    return (2*x)/2;
+  }
+
+  public static void main(String argv[])
+  {
+    int x = Integer.MAX_VALUE;
+
+    if (test(x) == x)
+      throw new RuntimeException ();
+  }
+}
+
diff --git a/libjava/testsuite/libjava.lang/Overflow.out b/libjava/testsuite/libjava.lang/Overflow.out
new file mode 100644 (file)
index 0000000..e69de29