From: Jakub Jelinek Date: Wed, 25 Nov 2015 08:58:32 +0000 (+0100) Subject: re PR target/67089 (Integer overflow checks not optimized on x86/x86_64) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6837d899c86d8b5eab15103e9466039304897ae7;p=gcc.git re PR target/67089 (Integer overflow checks not optimized on x86/x86_64) PR target/67089 * tree-ssa-math-opts.c (uaddsub_overflow_check_p, match_uaddsub_overflow): New functions. (pass_optimize_widening_mul::execute): Call match_uaddsub_overflow. * gcc.dg/pr67089-1.c: New test. * gcc.dg/pr67089-2.c: New test. * gcc.dg/pr67089-3.c: New test. * gcc.dg/pr67089-4.c: New test. * gcc.dg/pr67089-5.c: New test. * gcc.dg/pr67089-6.c: New test. * gcc.dg/pr67089-7.c: New test. From-SVN: r230856 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 28def7d3e3a..9504817e269 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2015-11-25 Jakub Jelinek + + PR target/67089 + * tree-ssa-math-opts.c (uaddsub_overflow_check_p, + match_uaddsub_overflow): New functions. + (pass_optimize_widening_mul::execute): Call match_uaddsub_overflow. + 2015-11-25 Richard Biener PR tree-optimization/68492 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index ec80893ee43..ac85edf1b99 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,14 @@ +2015-11-25 Jakub Jelinek + + PR target/67089 + * gcc.dg/pr67089-1.c: New test. + * gcc.dg/pr67089-2.c: New test. + * gcc.dg/pr67089-3.c: New test. + * gcc.dg/pr67089-4.c: New test. + * gcc.dg/pr67089-5.c: New test. + * gcc.dg/pr67089-6.c: New test. + * gcc.dg/pr67089-7.c: New test. + 2015-11-25 Richard Biener PR tree-optimization/68502 diff --git a/gcc/testsuite/gcc.dg/pr67089-1.c b/gcc/testsuite/gcc.dg/pr67089-1.c new file mode 100644 index 00000000000..e26474977da --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr67089-1.c @@ -0,0 +1,112 @@ +/* PR target/67089 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +extern void abort (void); + +int cnt, d; + +__attribute__((noinline, noclone)) +void foo (int x) +{ + asm volatile ("" : "+m" (d) : "g" (x) : "memory"); + cnt++; +} + +#define T(n, type, op, cond) \ +__attribute__((noinline, noclone)) \ +type \ +f##n (type x, type y) \ +{ \ + type r = op; \ + cond; \ + return r; \ +} + +T (1, unsigned int, x - y, if (r > x) foo (0)) +T (2, unsigned long, x - y, if (r <= x) foo (0)) +T (3, unsigned short, x - y, if (x < r) foo (r)) +T (4, unsigned long long, x - y, if (x >= r) foo (0)) +T (5, unsigned int, x - y, if (r >= x) foo (0)) +T (6, unsigned long, x - y, if (r < x) foo (0)) +T (7, unsigned short, x - y, if (x <= r) foo (r)) +T (8, unsigned long long, x - y, if (d || x > r) foo (0)) +T (9, unsigned int, x - y, if (d || r > x) foo (0)) +T (10, unsigned long, x - y, if (d || r <= x) foo (0)) +T (11, unsigned char, x - y, if (d || x < r) foo (0)) +T (12, unsigned long long, x - y, if (d || x >= r) foo (0)) +T (13, unsigned int, x - y, if (d || r >= x) foo (0)) +T (14, unsigned long, x - y, if (d || r < x) foo (0)) +T (15, unsigned short, x - y, if (d || x <= r) foo (0)) +T (16, unsigned long long, x - y, if (d || x > r) foo (0)) + +int +main () +{ + if (f1 (5, 3) != 2U || cnt != 0) abort (); + if (f1 (5, 7) != -2U || cnt != 1) abort (); + if (f1 (5, 5) != 0U || cnt != 1) abort (); + if (f1 (5, 0) != 5U || cnt != 1) abort (); + if (f2 (7, 1) != 6UL || cnt != 2) abort (); + if (f2 (7, 8) != -1UL || cnt != 2) abort (); + if (f2 (9, 9) != 0UL || cnt != 3) abort (); + if (f2 (9, 0) != 9UL || cnt != 4) abort (); + if (f3 (15, 14) != 1 || cnt != 4) abort (); + if (f3 (15, 25) != (unsigned short) -10 || cnt != 5) abort (); + if (f3 (15, 15) != 0 || cnt != 5) abort (); + if (f3 (15, 0) != 15 || cnt != 5) abort (); + if (f4 (9132, 9127) != 5ULL || cnt != 6) abort (); + if (f4 (9132, 9137) != -5ULL || cnt != 6) abort (); + if (f4 (9132, 9132) != 0 || cnt != 7) abort (); + if (f4 (9132, 0) != 9132ULL || cnt != 8) abort (); + if (f5 (5, 3) != 2U || cnt != 8) abort (); + if (f5 (5, 7) != -2U || cnt != 9) abort (); + if (f5 (5, 5) != 0U || cnt != 9) abort (); + if (f5 (5, 0) != 5U || cnt != 10) abort (); + if (f6 (7, 1) != 6UL || cnt != 11) abort (); + if (f6 (7, 8) != -1UL || cnt != 11) abort (); + if (f6 (9, 9) != 0UL || cnt != 12) abort (); + if (f6 (9, 0) != 9UL || cnt != 12) abort (); + if (f7 (15, 14) != 1 || cnt != 12) abort (); + if (f7 (15, 25) != (unsigned short) -10 || cnt != 13) abort (); + if (f7 (15, 15) != 0 || cnt != 13) abort (); + if (f7 (15, 0) != 15 || cnt != 14) abort (); + if (f8 (9132, 9127) != 5ULL || cnt != 15) abort (); + if (f8 (9132, 9137) != -5ULL || cnt != 15) abort (); + if (f8 (9132, 9132) != 0 || cnt != 16) abort (); + if (f8 (9132, 0) != 9132ULL || cnt != 16) abort (); + cnt = 0; + if (f9 (5, 3) != 2U || cnt != 0) abort (); + if (f9 (5, 7) != -2U || cnt != 1) abort (); + if (f9 (5, 5) != 0U || cnt != 1) abort (); + if (f9 (5, 0) != 5U || cnt != 1) abort (); + if (f10 (7, 1) != 6UL || cnt != 2) abort (); + if (f10 (7, 8) != -1UL || cnt != 2) abort (); + if (f10 (9, 9) != 0UL || cnt != 3) abort (); + if (f10 (9, 0) != 9UL || cnt != 4) abort (); + if (f11 (15, 14) != 1 || cnt != 4) abort (); + if (f11 (15, 25) != (unsigned char) -10 || cnt != 5) abort (); + if (f11 (15, 15) != 0 || cnt != 5) abort (); + if (f11 (15, 0) != 15 || cnt != 5) abort (); + if (f12 (9132, 9127) != 5ULL || cnt != 6) abort (); + if (f12 (9132, 9137) != -5ULL || cnt != 6) abort (); + if (f12 (9132, 9132) != 0 || cnt != 7) abort (); + if (f12 (9132, 0) != 9132ULL || cnt != 8) abort (); + if (f13 (5, 3) != 2U || cnt != 8) abort (); + if (f13 (5, 7) != -2U || cnt != 9) abort (); + if (f13 (5, 5) != 0U || cnt != 9) abort (); + if (f13 (5, 0) != 5U || cnt != 10) abort (); + if (f14 (7, 1) != 6UL || cnt != 11) abort (); + if (f14 (7, 8) != -1UL || cnt != 11) abort (); + if (f14 (9, 9) != 0UL || cnt != 12) abort (); + if (f14 (9, 0) != 9UL || cnt != 12) abort (); + if (f15 (15, 14) != 1 || cnt != 12) abort (); + if (f15 (15, 25) != (unsigned short) -10 || cnt != 13) abort (); + if (f15 (15, 15) != 0 || cnt != 13) abort (); + if (f15 (15, 0) != 15 || cnt != 14) abort (); + if (f16 (9132, 9127) != 5ULL || cnt != 15) abort (); + if (f16 (9132, 9137) != -5ULL || cnt != 15) abort (); + if (f16 (9132, 9132) != 0 || cnt != 16) abort (); + if (f16 (9132, 0) != 9132ULL || cnt != 16) abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr67089-2.c b/gcc/testsuite/gcc.dg/pr67089-2.c new file mode 100644 index 00000000000..59b27a7d686 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr67089-2.c @@ -0,0 +1,112 @@ +/* PR target/67089 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +extern void abort (void); + +int cnt, d; + +__attribute__((noinline, noclone)) +void foo (int x) +{ + asm volatile ("" : "+m" (d) : "g" (x) : "memory"); + cnt++; +} + +#define T(n, type, op, cond) \ +__attribute__((noinline, noclone)) \ +type \ +f##n (type x, type y) \ +{ \ + type r = op; \ + cond; \ + return r; \ +} + +T (1, unsigned int, x - y, if (r > y) foo (0)) +T (2, unsigned long, x - y, if (r <= y) foo (0)) +T (3, unsigned short, x - y, if (y < r) foo (r)) +T (4, unsigned long long, x - y, if (y >= r) foo (0)) +T (5, unsigned int, x - y, if (r >= y) foo (0)) +T (6, unsigned long, x - y, if (r < y) foo (0)) +T (7, unsigned short, x - y, if (y <= r) foo (r)) +T (8, unsigned long long, x - y, if (d || y > r) foo (0)) +T (9, unsigned int, x - y, if (d || r > y) foo (0)) +T (10, unsigned long, x - y, if (d || r <= y) foo (0)) +T (11, unsigned char, x - y, if (d || y < r) foo (0)) +T (12, unsigned long long, x - y, if (d || y >= r) foo (0)) +T (13, unsigned int, x - y, if (d || r >= y) foo (0)) +T (14, unsigned long, x - y, if (d || r < y) foo (0)) +T (15, unsigned short, x - y, if (d || y <= r) foo (0)) +T (16, unsigned long long, x - y, if (d || y > r) foo (0)) + +int +main () +{ + if (f1 (5, 3) != 2U || cnt != 0) abort (); + if (f1 (5, 7) != -2U || cnt != 1) abort (); + if (f1 (5, 5) != 0U || cnt != 1) abort (); + if (f1 (5, 0) != 5U || cnt != 2) abort (); + if (f2 (7, 1) != 6UL || cnt != 2) abort (); + if (f2 (7, 8) != -1UL || cnt != 2) abort (); + if (f2 (9, 9) != 0UL || cnt != 3) abort (); + if (f2 (9, 0) != 9UL || cnt != 3) abort (); + if (f3 (15, 14) != 1 || cnt != 3) abort (); + if (f3 (15, 25) != (unsigned short) -10 || cnt != 4) abort (); + if (f3 (15, 15) != 0 || cnt != 4) abort (); + if (f3 (15, 0) != 15 || cnt != 5) abort (); + if (f4 (9132, 9127) != 5ULL || cnt != 6) abort (); + if (f4 (9132, 9137) != -5ULL || cnt != 6) abort (); + if (f4 (9132, 9132) != 0 || cnt != 7) abort (); + if (f4 (9132, 0) != 9132ULL || cnt != 7) abort (); + if (f5 (5, 3) != 2U || cnt != 7) abort (); + if (f5 (5, 7) != -2U || cnt != 8) abort (); + if (f5 (5, 5) != 0U || cnt != 8) abort (); + if (f5 (5, 0) != 5U || cnt != 9) abort (); + if (f6 (7, 1) != 6UL || cnt != 9) abort (); + if (f6 (7, 8) != -1UL || cnt != 9) abort (); + if (f6 (9, 9) != 0UL || cnt != 10) abort (); + if (f6 (9, 0) != 9UL || cnt != 10) abort (); + if (f7 (15, 14) != 1 || cnt != 10) abort (); + if (f7 (15, 25) != (unsigned short) -10 || cnt != 11) abort (); + if (f7 (15, 15) != 0 || cnt != 11) abort (); + if (f7 (15, 0) != 15 || cnt != 12) abort (); + if (f8 (9132, 9127) != 5ULL || cnt != 13) abort (); + if (f8 (9132, 9137) != -5ULL || cnt != 13) abort (); + if (f8 (9132, 9132) != 0 || cnt != 14) abort (); + if (f8 (9132, 0) != 9132ULL || cnt != 14) abort (); + cnt = 0; + if (f9 (5, 3) != 2U || cnt != 0) abort (); + if (f9 (5, 7) != -2U || cnt != 1) abort (); + if (f9 (5, 5) != 0U || cnt != 1) abort (); + if (f9 (5, 0) != 5U || cnt != 2) abort (); + if (f10 (7, 1) != 6UL || cnt != 2) abort (); + if (f10 (7, 8) != -1UL || cnt != 2) abort (); + if (f10 (9, 9) != 0UL || cnt != 3) abort (); + if (f10 (9, 0) != 9UL || cnt != 3) abort (); + if (f11 (15, 14) != 1 || cnt != 3) abort (); + if (f11 (15, 25) != (unsigned char) -10 || cnt != 4) abort (); + if (f11 (15, 15) != 0 || cnt != 4) abort (); + if (f11 (15, 0) != 15 || cnt != 5) abort (); + if (f12 (9132, 9127) != 5ULL || cnt != 6) abort (); + if (f12 (9132, 9137) != -5ULL || cnt != 6) abort (); + if (f12 (9132, 9132) != 0 || cnt != 7) abort (); + if (f12 (9132, 0) != 9132ULL || cnt != 7) abort (); + if (f13 (5, 3) != 2U || cnt != 7) abort (); + if (f13 (5, 7) != -2U || cnt != 8) abort (); + if (f13 (5, 5) != 0U || cnt != 8) abort (); + if (f13 (5, 0) != 5U || cnt != 9) abort (); + if (f14 (7, 1) != 6UL || cnt != 9) abort (); + if (f14 (7, 8) != -1UL || cnt != 9) abort (); + if (f14 (9, 9) != 0UL || cnt != 10) abort (); + if (f14 (9, 0) != 9UL || cnt != 10) abort (); + if (f15 (15, 14) != 1 || cnt != 10) abort (); + if (f15 (15, 25) != (unsigned short) -10 || cnt != 11) abort (); + if (f15 (15, 15) != 0 || cnt != 11) abort (); + if (f15 (15, 0) != 15 || cnt != 12) abort (); + if (f16 (9132, 9127) != 5ULL || cnt != 13) abort (); + if (f16 (9132, 9137) != -5ULL || cnt != 13) abort (); + if (f16 (9132, 9132) != 0 || cnt != 14) abort (); + if (f16 (9132, 0) != 9132ULL || cnt != 14) abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr67089-3.c b/gcc/testsuite/gcc.dg/pr67089-3.c new file mode 100644 index 00000000000..22576e56cbf --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr67089-3.c @@ -0,0 +1,112 @@ +/* PR target/67089 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +extern void abort (void); + +int cnt, d; + +__attribute__((noinline, noclone)) +void foo (int x) +{ + asm volatile ("" : "+m" (d) : "g" (x) : "memory"); + cnt++; +} + +#define T(n, type, op, cond) \ +__attribute__((noinline, noclone)) \ +type \ +f##n (type x, type y) \ +{ \ + type r = op; \ + cond; \ + return r; \ +} + +T (1, unsigned int, x + y, if (r > x) foo (0)) +T (2, unsigned long, x + y, if (r <= x) foo (0)) +T (3, unsigned short, x + y, if (x < r) foo (r)) +T (4, unsigned long long, x + y, if (x >= r) foo (0)) +T (5, unsigned int, x + y, if (r >= x) foo (0)) +T (6, unsigned long, x + y, if (r < x) foo (0)) +T (7, unsigned short, x + y, if (x <= r) foo (r)) +T (8, unsigned long long, x + y, if (d || x > r) foo (0)) +T (9, unsigned int, x + y, if (d || r > x) foo (0)) +T (10, unsigned long, x + y, if (d || r <= x) foo (0)) +T (11, unsigned char, x + y, if (d || x < r) foo (0)) +T (12, unsigned long long, x + y, if (d || x >= r) foo (0)) +T (13, unsigned int, x + y, if (d || r >= x) foo (0)) +T (14, unsigned long, x + y, if (d || r < x) foo (0)) +T (15, unsigned short, x + y, if (d || x <= r) foo (0)) +T (16, unsigned long long, x + y, if (d || x > r) foo (0)) + +int +main () +{ + if (f1 (-7U, 0) != -7U || cnt != 0) abort (); + if (f1 (-7U, 6) != -1U || cnt != 1) abort (); + if (f1 (-7U, 7) != 0U || cnt != 1) abort (); + if (f1 (-7U, 8) != 1U || cnt != 1) abort (); + if (f2 (-9UL, 0) != -9UL || cnt != 2) abort (); + if (f2 (-9UL, 8) != -1UL || cnt != 2) abort (); + if (f2 (-9UL, 9) != 0UL || cnt != 3) abort (); + if (f2 (-9UL, 10) != 1UL || cnt != 4) abort (); + if (f3 (-15, 0) != (unsigned short) -15 || cnt != 4) abort (); + if (f3 (-15, 14) != (unsigned short) -1 || cnt != 5) abort (); + if (f3 (-15, 15) != 0 || cnt != 5) abort (); + if (f3 (-15, 16) != 1 || cnt != 5) abort (); + if (f4 (-9132ULL, 0) != -9132ULL || cnt != 6) abort (); + if (f4 (-9132ULL, 9131) != -1ULL || cnt != 6) abort (); + if (f4 (-9132ULL, 9132) != 0 || cnt != 7) abort (); + if (f4 (-9132ULL, 9133) != 1ULL || cnt != 8) abort (); + if (f5 (-7U, 0) != -7U || cnt != 9) abort (); + if (f5 (-7U, 6) != -1U || cnt != 10) abort (); + if (f5 (-7U, 7) != 0U || cnt != 10) abort (); + if (f5 (-7U, 8) != 1U || cnt != 10) abort (); + if (f6 (-9UL, 0) != -9UL || cnt != 10) abort (); + if (f6 (-9UL, 8) != -1UL || cnt != 10) abort (); + if (f6 (-9UL, 9) != 0UL || cnt != 11) abort (); + if (f6 (-9UL, 10) != 1UL || cnt != 12) abort (); + if (f7 (-15, 0) != (unsigned short) -15 || cnt != 13) abort (); + if (f7 (-15, 14) != (unsigned short) -1 || cnt != 14) abort (); + if (f7 (-15, 15) != 0 || cnt != 14) abort (); + if (f7 (-15, 16) != 1 || cnt != 14) abort (); + if (f8 (-9132ULL, 0) != -9132ULL || cnt != 14) abort (); + if (f8 (-9132ULL, 9131) != -1ULL || cnt != 14) abort (); + if (f8 (-9132ULL, 9132) != 0 || cnt != 15) abort (); + if (f8 (-9132ULL, 9133) != 1ULL || cnt != 16) abort (); + cnt = 0; + if (f9 (-7U, 0) != -7U || cnt != 0) abort (); + if (f9 (-7U, 6) != -1U || cnt != 1) abort (); + if (f9 (-7U, 7) != 0U || cnt != 1) abort (); + if (f9 (-7U, 8) != 1U || cnt != 1) abort (); + if (f10 (-9UL, 0) != -9UL || cnt != 2) abort (); + if (f10 (-9UL, 8) != -1UL || cnt != 2) abort (); + if (f10 (-9UL, 9) != 0UL || cnt != 3) abort (); + if (f10 (-9UL, 10) != 1UL || cnt != 4) abort (); + if (f11 (-15, 0) != (unsigned char) -15 || cnt != 4) abort (); + if (f11 (-15, 14) != (unsigned char) -1 || cnt != 5) abort (); + if (f11 (-15, 15) != 0 || cnt != 5) abort (); + if (f11 (-15, 16) != 1 || cnt != 5) abort (); + if (f12 (-9132ULL, 0) != -9132ULL || cnt != 6) abort (); + if (f12 (-9132ULL, 9131) != -1ULL || cnt != 6) abort (); + if (f12 (-9132ULL, 9132) != 0 || cnt != 7) abort (); + if (f12 (-9132ULL, 9133) != 1ULL || cnt != 8) abort (); + if (f13 (-7U, 0) != -7U || cnt != 9) abort (); + if (f13 (-7U, 6) != -1U || cnt != 10) abort (); + if (f13 (-7U, 7) != 0U || cnt != 10) abort (); + if (f13 (-7U, 8) != 1U || cnt != 10) abort (); + if (f14 (-9UL, 0) != -9UL || cnt != 10) abort (); + if (f14 (-9UL, 8) != -1UL || cnt != 10) abort (); + if (f14 (-9UL, 9) != 0UL || cnt != 11) abort (); + if (f14 (-9UL, 10) != 1UL || cnt != 12) abort (); + if (f15 (-15, 0) != (unsigned short) -15 || cnt != 13) abort (); + if (f15 (-15, 14) != (unsigned short) -1 || cnt != 14) abort (); + if (f15 (-15, 15) != 0 || cnt != 14) abort (); + if (f15 (-15, 16) != 1 || cnt != 14) abort (); + if (f16 (-9132ULL, 0) != -9132ULL || cnt != 14) abort (); + if (f16 (-9132ULL, 9131) != -1ULL || cnt != 14) abort (); + if (f16 (-9132ULL, 9132) != 0 || cnt != 15) abort (); + if (f16 (-9132ULL, 9133) != 1ULL || cnt != 16) abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr67089-4.c b/gcc/testsuite/gcc.dg/pr67089-4.c new file mode 100644 index 00000000000..f4f7439f9d5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr67089-4.c @@ -0,0 +1,112 @@ +/* PR target/67089 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +extern void abort (void); + +int cnt, d; + +__attribute__((noinline, noclone)) +void foo (int x) +{ + asm volatile ("" : "+m" (d) : "g" (x) : "memory"); + cnt++; +} + +#define T(n, type, op, cond) \ +__attribute__((noinline, noclone)) \ +type \ +f##n (type x, type y) \ +{ \ + type r = op; \ + cond; \ + return r; \ +} + +T (1, unsigned int, x + y, if (r > y) foo (0)) +T (2, unsigned long, x + y, if (r <= y) foo (0)) +T (3, unsigned short, x + y, if (y < r) foo (r)) +T (4, unsigned long long, x + y, if (y >= r) foo (0)) +T (5, unsigned int, x + y, if (r >= y) foo (0)) +T (6, unsigned long, x + y, if (r < y) foo (0)) +T (7, unsigned short, x + y, if (y <= r) foo (r)) +T (8, unsigned long long, x + y, if (d || y > r) foo (0)) +T (9, unsigned int, x + y, if (d || r > y) foo (0)) +T (10, unsigned long, x + y, if (d || r <= y) foo (0)) +T (11, unsigned char, x + y, if (d || y < r) foo (0)) +T (12, unsigned long long, x + y, if (d || y >= r) foo (0)) +T (13, unsigned int, x + y, if (d || r >= y) foo (0)) +T (14, unsigned long, x + y, if (d || r < y) foo (0)) +T (15, unsigned short, x + y, if (d || y <= r) foo (0)) +T (16, unsigned long long, x + y, if (d || y > r) foo (0)) + +int +main () +{ + if (f1 (-7U, 0) != -7U || cnt != 1) abort (); + if (f1 (-7U, 6) != -1U || cnt != 2) abort (); + if (f1 (-7U, 7) != 0U || cnt != 2) abort (); + if (f1 (-7U, 8) != 1U || cnt != 2) abort (); + if (f2 (-9UL, 0) != -9UL || cnt != 2) abort (); + if (f2 (-9UL, 8) != -1UL || cnt != 2) abort (); + if (f2 (-9UL, 9) != 0UL || cnt != 3) abort (); + if (f2 (-9UL, 10) != 1UL || cnt != 4) abort (); + if (f3 (-15, 0) != (unsigned short) -15 || cnt != 5) abort (); + if (f3 (-15, 14) != (unsigned short) -1 || cnt != 6) abort (); + if (f3 (-15, 15) != 0 || cnt != 6) abort (); + if (f3 (-15, 16) != 1 || cnt != 6) abort (); + if (f4 (-9132ULL, 0) != -9132ULL || cnt != 6) abort (); + if (f4 (-9132ULL, 9131) != -1ULL || cnt != 6) abort (); + if (f4 (-9132ULL, 9132) != 0 || cnt != 7) abort (); + if (f4 (-9132ULL, 9133) != 1ULL || cnt != 8) abort (); + if (f5 (-7U, 0) != -7U || cnt != 9) abort (); + if (f5 (-7U, 6) != -1U || cnt != 10) abort (); + if (f5 (-7U, 7) != 0U || cnt != 10) abort (); + if (f5 (-7U, 8) != 1U || cnt != 10) abort (); + if (f6 (-9UL, 0) != -9UL || cnt != 10) abort (); + if (f6 (-9UL, 8) != -1UL || cnt != 10) abort (); + if (f6 (-9UL, 9) != 0UL || cnt != 11) abort (); + if (f6 (-9UL, 10) != 1UL || cnt != 12) abort (); + if (f7 (-15, 0) != (unsigned short) -15 || cnt != 13) abort (); + if (f7 (-15, 14) != (unsigned short) -1 || cnt != 14) abort (); + if (f7 (-15, 15) != 0 || cnt != 14) abort (); + if (f7 (-15, 16) != 1 || cnt != 14) abort (); + if (f8 (-9132ULL, 0) != -9132ULL || cnt != 14) abort (); + if (f8 (-9132ULL, 9131) != -1ULL || cnt != 14) abort (); + if (f8 (-9132ULL, 9132) != 0 || cnt != 15) abort (); + if (f8 (-9132ULL, 9133) != 1ULL || cnt != 16) abort (); + cnt = 0; + if (f9 (-7U, 0) != -7U || cnt != 1) abort (); + if (f9 (-7U, 6) != -1U || cnt != 2) abort (); + if (f9 (-7U, 7) != 0U || cnt != 2) abort (); + if (f9 (-7U, 8) != 1U || cnt != 2) abort (); + if (f10 (-9UL, 0) != -9UL || cnt != 2) abort (); + if (f10 (-9UL, 8) != -1UL || cnt != 2) abort (); + if (f10 (-9UL, 9) != 0UL || cnt != 3) abort (); + if (f10 (-9UL, 10) != 1UL || cnt != 4) abort (); + if (f11 (-15, 0) != (unsigned char) -15 || cnt != 5) abort (); + if (f11 (-15, 14) != (unsigned char) -1 || cnt != 6) abort (); + if (f11 (-15, 15) != 0 || cnt != 6) abort (); + if (f11 (-15, 16) != 1 || cnt != 6) abort (); + if (f12 (-9132ULL, 0) != -9132ULL || cnt != 6) abort (); + if (f12 (-9132ULL, 9131) != -1ULL || cnt != 6) abort (); + if (f12 (-9132ULL, 9132) != 0 || cnt != 7) abort (); + if (f12 (-9132ULL, 9133) != 1ULL || cnt != 8) abort (); + if (f13 (-7U, 0) != -7U || cnt != 9) abort (); + if (f13 (-7U, 6) != -1U || cnt != 10) abort (); + if (f13 (-7U, 7) != 0U || cnt != 10) abort (); + if (f13 (-7U, 8) != 1U || cnt != 10) abort (); + if (f14 (-9UL, 0) != -9UL || cnt != 10) abort (); + if (f14 (-9UL, 8) != -1UL || cnt != 10) abort (); + if (f14 (-9UL, 9) != 0UL || cnt != 11) abort (); + if (f14 (-9UL, 10) != 1UL || cnt != 12) abort (); + if (f15 (-15, 0) != (unsigned short) -15 || cnt != 13) abort (); + if (f15 (-15, 14) != (unsigned short) -1 || cnt != 14) abort (); + if (f15 (-15, 15) != 0 || cnt != 14) abort (); + if (f15 (-15, 16) != 1 || cnt != 14) abort (); + if (f16 (-9132ULL, 0) != -9132ULL || cnt != 14) abort (); + if (f16 (-9132ULL, 9131) != -1ULL || cnt != 14) abort (); + if (f16 (-9132ULL, 9132) != 0 || cnt != 15) abort (); + if (f16 (-9132ULL, 9133) != 1ULL || cnt != 16) abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr67089-5.c b/gcc/testsuite/gcc.dg/pr67089-5.c new file mode 100644 index 00000000000..f937375737e --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr67089-5.c @@ -0,0 +1,82 @@ +/* PR target/67089 */ +/* { dg-do run } */ +/* { dg-options "-O2 -ftree-loop-if-convert" } */ + +extern void abort (void); + +int cnt; +unsigned int a[16], b[16], c[16]; + +__attribute__((noinline, noclone)) +void foo (int x) +{ + asm volatile ("" : : "g" (x) : "memory"); + cnt++; +} + +__attribute__((noinline, noclone)) void +f0 (unsigned int x) +{ + for (int i = 0; i < 16; i++) + { + unsigned int r = x - a[i]; + b[i] = r; + c[i] = r > x ? 7 : x; + } +} + +#define T(n, type, op, cond) \ +__attribute__((noinline, noclone)) \ +type \ +f##n (type x) \ +{ \ + type r = op; \ + cond; \ + return r; \ +} + +T (1, unsigned int, x - 2U, if (r > x) foo (0)) +T (2, unsigned long, x - 2U, if (r <= x) foo (0)) +T (3, unsigned short, 2U - x, if (r > 2U) foo (0)) +T (4, unsigned char, 2U - x, if (r <= 2U) foo (0)) +T (5, unsigned int, x + -2U, if (r > x) foo (0)) +T (6, unsigned long, x + -2UL, if (r <= x) foo (0)) +T (7, unsigned short, (unsigned short) -2 + x, if (r > (unsigned short) -2) foo (0)) +T (8, unsigned char, (unsigned char) -2 + x, if (r <= (unsigned char) -2) foo (0)) + +int +main () +{ + int i; + for (i = 0; i < 16; i++) + a[i] = i - 7; + f0 (5); + for (i = 0; i < 16; i++) + if (b[i] != 12U - i || c[i] != 7 - 2 * (i >= 7 && i < 13)) + abort (); + if (f1 (3) != 1 || cnt != 0) abort (); + if (f1 (2) != 0 || cnt != 0) abort (); + if (f1 (1) != -1U || cnt != 1) abort (); + if (f2 (3) != 1 || cnt != 2) abort (); + if (f2 (2) != 0 || cnt != 3) abort (); + if (f2 (1) != -1UL || cnt != 3) abort (); + if (f3 (3) != (unsigned short) -1 || cnt != 4) abort (); + if (f3 (2) != 0 || cnt != 4) abort (); + if (f3 (1) != 1 || cnt != 4) abort (); + if (f4 (3) != (unsigned char) -1 || cnt != 4) abort (); + if (f4 (2) != 0 || cnt != 5) abort (); + if (f4 (1) != 1 || cnt != 6) abort (); + if (f5 (3) != 1 || cnt != 6) abort (); + if (f5 (2) != 0 || cnt != 6) abort (); + if (f5 (1) != -1U || cnt != 7) abort (); + if (f6 (3) != 1 || cnt != 8) abort (); + if (f6 (2) != 0 || cnt != 9) abort (); + if (f6 (1) != -1UL || cnt != 9) abort (); + if (f7 (3) != 1 || cnt != 9) abort (); + if (f7 (2) != 0 || cnt != 9) abort (); + if (f7 (1) != (unsigned short) -1 || cnt != 10) abort (); + if (f8 (3) != 1 || cnt != 11) abort (); + if (f8 (2) != 0 || cnt != 12) abort (); + if (f8 (1) != (unsigned char) -1 || cnt != 12) abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr67089-6.c b/gcc/testsuite/gcc.dg/pr67089-6.c new file mode 100644 index 00000000000..9b0708b5bc4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr67089-6.c @@ -0,0 +1,62 @@ +/* PR target/67089 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -ftree-loop-if-convert -fdump-tree-widening_mul" } */ + +extern void abort (void); + +int cnt; +unsigned int a[16], b[16], c[16], d; +void foo (int x); + +__attribute__((noinline, noclone)) void +f0 (unsigned int x) +{ + for (int i = 0; i < 16; i++) + { + unsigned int r = x - a[i]; + b[i] = r; + c[i] = r > x ? 7 : x; + } +} + +#define T(n, type, op, cond) \ +__attribute__((noinline, noclone)) \ +type \ +f##n (type x, type y) \ +{ \ + type r = op; \ + cond; \ + return r; \ +} + +T (1, unsigned int, x - y, if (r > x) foo (0)) +T (2, unsigned long, x - y, if (r <= x) foo (0)) +T (3, unsigned short, x - y, if (x < r) foo (r)) +T (4, unsigned long long, x - y, if (x >= r) foo (0)) +T (5, unsigned int, x - y, if (d || r > x) foo (0)) +T (6, unsigned long, x - y, if (d || r <= x) foo (0)) +T (7, unsigned char, x - y, if (d || x < r) foo (0)) +T (8, unsigned long long, x - y, if (d || x >= r) foo (0)) +T (9, unsigned int, x + y, if (r >= x) foo (0)) +T (10, unsigned long, x + y, if (r < x) foo (0)) +T (11, unsigned short, x + y, if (x <= r) foo (r)) +T (12, unsigned long long, x + y, if (d || x > r) foo (0)) +T (13, unsigned int, x + y, if (d || r >= x) foo (0)) +T (14, unsigned long, x + y, if (d || r < x) foo (0)) +T (15, unsigned short, x + y, if (d || x <= r) foo (0)) +T (16, unsigned long long, x + y, if (d || x > r) foo (0)) +T (17, unsigned int, x + y, if (r >= y) foo (0)) +T (18, unsigned long, x + y, if (r < y) foo (0)) +T (19, unsigned short, x + y, if (y <= r) foo (r)) +T (20, unsigned long long, x + y, if (d || y > r) foo (0)) +T (21, unsigned int, x + y, if (d || r >= y) foo (0)) +T (22, unsigned long, x + y, if (d || r < y) foo (0)) +T (23, unsigned short, x + y, if (d || y <= r) foo (0)) +T (24, unsigned long long, x + y, if (d || y > r) foo (0)) +T (25, unsigned short, 2U - x, if (r > 2U) foo (0)) +T (26, unsigned char, 2U - x, if (r <= 2U) foo (0)) + +/* { dg-final { scan-tree-dump-times "ADD_OVERFLOW" 16 "widening_mul" { target { { i?86-*-* x86_64-*-* } && { ! ia32 } } } } } */ +/* { dg-final { scan-tree-dump-times "SUB_OVERFLOW" 11 "widening_mul" { target { { i?86-*-* x86_64-*-* } && { ! ia32 } } } } } */ +/* { dg-final { scan-tree-dump-times "ADD_OVERFLOW" 12 "widening_mul" { target { { i?86-*-* x86_64-*-* } && ia32 } } } } */ +/* { dg-final { scan-tree-dump-times "SUB_OVERFLOW" 9 "widening_mul" { target { { i?86-*-* x86_64-*-* } && ia32 } } } } */ diff --git a/gcc/testsuite/gcc.dg/pr67089-7.c b/gcc/testsuite/gcc.dg/pr67089-7.c new file mode 100644 index 00000000000..dc05e3b2263 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr67089-7.c @@ -0,0 +1,62 @@ +/* PR target/67089 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -ftree-loop-if-convert -fdump-tree-widening_mul" } */ + +extern void abort (void); + +int cnt, d; +void foo (int x); + +#define T(n, type, op, cond) \ +__attribute__((noinline, noclone)) \ +type \ +f##n (type x, type y) \ +{ \ + type r = op; \ + cond; \ + return r; \ +} + +T (1, unsigned int, x - y, if (r >= x) foo (0)) +T (2, unsigned long, x - y, if (r < x) foo (0)) +T (3, unsigned short, x - y, if (x <= r) foo (r)) +T (4, unsigned long long, x - y, if (d || x > r) foo (0)) +T (5, unsigned int, x - y, if (d || r >= x) foo (0)) +T (6, unsigned long, x - y, if (d || r < x) foo (0)) +T (7, unsigned short, x - y, if (d || x <= r) foo (0)) +T (8, unsigned long long, x - y, if (d || x > r) foo (0)) +T (9, unsigned int, x - y, if (r > y) foo (0)) +T (10, unsigned long, x - y, if (r <= y) foo (0)) +T (11, unsigned short, x - y, if (y < r) foo (r)) +T (12, unsigned long long, x - y, if (y >= r) foo (0)) +T (13, unsigned int, x - y, if (r >= y) foo (0)) +T (14, unsigned long, x - y, if (r < y) foo (0)) +T (15, unsigned short, x - y, if (y <= r) foo (r)) +T (16, unsigned long long, x - y, if (d || y > r) foo (0)) +T (17, unsigned int, x - y, if (d || r > y) foo (0)) +T (18, unsigned long, x - y, if (d || r <= y) foo (0)) +T (19, unsigned char, x - y, if (d || y < r) foo (0)) +T (20, unsigned long long, x - y, if (d || y >= r) foo (0)) +T (21, unsigned int, x - y, if (d || r >= y) foo (0)) +T (22, unsigned long, x - y, if (d || r < y) foo (0)) +T (23, unsigned short, x - y, if (d || y <= r) foo (0)) +T (24, unsigned long long, x - y, if (d || y > r) foo (0)) +T (25, unsigned int, x + y, if (r > x) foo (0)) +T (26, unsigned long, x + y, if (r <= x) foo (0)) +T (27, unsigned short, x + y, if (x < r) foo (r)) +T (28, unsigned long long, x + y, if (x >= r) foo (0)) +T (29, unsigned int, x + y, if (d || r > x) foo (0)) +T (30, unsigned long, x + y, if (d || r <= x) foo (0)) +T (31, unsigned char, x + y, if (d || x < r) foo (0)) +T (32, unsigned long long, x + y, if (d || x >= r) foo (0)) +T (33, unsigned int, x + y, if (r > y) foo (0)) +T (34, unsigned long, x + y, if (r <= y) foo (0)) +T (35, unsigned short, x + y, if (y < r) foo (r)) +T (36, unsigned long long, x + y, if (y >= r) foo (0)) +T (37, unsigned int, x + y, if (d || r > y) foo (0)) +T (38, unsigned long, x + y, if (d || r <= y) foo (0)) +T (39, unsigned char, x + y, if (d || y < r) foo (0)) +T (40, unsigned long long, x + y, if (d || y >= r) foo (0)) + +/* { dg-final { scan-tree-dump-not "ADD_OVERFLOW" "widening_mul" } } */ +/* { dg-final { scan-tree-dump-not "SUB_OVERFLOW" "widening_mul" } } */ diff --git a/gcc/tree-ssa-math-opts.c b/gcc/tree-ssa-math-opts.c index 1e7cf7ed283..8376eab746d 100644 --- a/gcc/tree-ssa-math-opts.c +++ b/gcc/tree-ssa-math-opts.c @@ -3491,6 +3491,189 @@ convert_mult_to_fma (gimple *mul_stmt, tree op1, tree op2) return true; } + +/* Helper function of match_uaddsub_overflow. Return 1 + if USE_STMT is unsigned overflow check ovf != 0 for + STMT, -1 if USE_STMT is unsigned overflow check ovf == 0 + and 0 otherwise. */ + +static int +uaddsub_overflow_check_p (gimple *stmt, gimple *use_stmt) +{ + enum tree_code ccode = ERROR_MARK; + tree crhs1 = NULL_TREE, crhs2 = NULL_TREE; + if (gimple_code (use_stmt) == GIMPLE_COND) + { + ccode = gimple_cond_code (use_stmt); + crhs1 = gimple_cond_lhs (use_stmt); + crhs2 = gimple_cond_rhs (use_stmt); + } + else if (is_gimple_assign (use_stmt)) + { + if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS) + { + ccode = gimple_assign_rhs_code (use_stmt); + crhs1 = gimple_assign_rhs1 (use_stmt); + crhs2 = gimple_assign_rhs2 (use_stmt); + } + else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR) + { + tree cond = gimple_assign_rhs1 (use_stmt); + if (COMPARISON_CLASS_P (cond)) + { + ccode = TREE_CODE (cond); + crhs1 = TREE_OPERAND (cond, 0); + crhs2 = TREE_OPERAND (cond, 1); + } + else + return 0; + } + else + return 0; + } + else + return 0; + + if (TREE_CODE_CLASS (ccode) != tcc_comparison) + return 0; + + enum tree_code code = gimple_assign_rhs_code (stmt); + tree lhs = gimple_assign_lhs (stmt); + tree rhs1 = gimple_assign_rhs1 (stmt); + tree rhs2 = gimple_assign_rhs2 (stmt); + + switch (ccode) + { + case GT_EXPR: + case LE_EXPR: + /* r = a - b; r > a or r <= a + r = a + b; a > r or a <= r or b > r or b <= r. */ + if ((code == MINUS_EXPR && crhs1 == lhs && crhs2 == rhs1) + || (code == PLUS_EXPR && (crhs1 == rhs1 || crhs1 == rhs2) + && crhs2 == lhs)) + return ccode == GT_EXPR ? 1 : -1; + break; + case LT_EXPR: + case GE_EXPR: + /* r = a - b; a < r or a >= r + r = a + b; r < a or r >= a or r < b or r >= b. */ + if ((code == MINUS_EXPR && crhs1 == rhs1 && crhs2 == lhs) + || (code == PLUS_EXPR && crhs1 == lhs + && (crhs2 == rhs1 || crhs2 == rhs2))) + return ccode == LT_EXPR ? 1 : -1; + break; + default: + break; + } + return 0; +} + +/* Recognize for unsigned x + x = y - z; + if (x > y) + where there are other uses of x and replace it with + _7 = SUB_OVERFLOW (y, z); + x = REALPART_EXPR <_7>; + _8 = IMAGPART_EXPR <_7>; + if (_8) + and similarly for addition. */ + +static bool +match_uaddsub_overflow (gimple_stmt_iterator *gsi, gimple *stmt, + enum tree_code code) +{ + tree lhs = gimple_assign_lhs (stmt); + tree type = TREE_TYPE (lhs); + use_operand_p use_p; + imm_use_iterator iter; + bool use_seen = false; + bool ovf_use_seen = false; + gimple *use_stmt; + + gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR); + if (!INTEGRAL_TYPE_P (type) + || !TYPE_UNSIGNED (type) + || has_zero_uses (lhs) + || has_single_use (lhs) + || optab_handler (code == PLUS_EXPR ? uaddv4_optab : usubv4_optab, + TYPE_MODE (type)) == CODE_FOR_nothing) + return false; + + FOR_EACH_IMM_USE_FAST (use_p, iter, lhs) + { + use_stmt = USE_STMT (use_p); + if (is_gimple_debug (use_stmt)) + continue; + + if (uaddsub_overflow_check_p (stmt, use_stmt)) + ovf_use_seen = true; + else + use_seen = true; + if (ovf_use_seen && use_seen) + break; + } + + if (!ovf_use_seen || !use_seen) + return false; + + tree ctype = build_complex_type (type); + tree rhs1 = gimple_assign_rhs1 (stmt); + tree rhs2 = gimple_assign_rhs2 (stmt); + gcall *g = gimple_build_call_internal (code == PLUS_EXPR + ? IFN_ADD_OVERFLOW : IFN_SUB_OVERFLOW, + 2, rhs1, rhs2); + tree ctmp = make_ssa_name (ctype); + gimple_call_set_lhs (g, ctmp); + gsi_insert_before (gsi, g, GSI_SAME_STMT); + gassign *g2 = gimple_build_assign (lhs, REALPART_EXPR, + build1 (REALPART_EXPR, type, ctmp)); + gsi_replace (gsi, g2, true); + tree ovf = make_ssa_name (type); + g2 = gimple_build_assign (ovf, IMAGPART_EXPR, + build1 (IMAGPART_EXPR, type, ctmp)); + gsi_insert_after (gsi, g2, GSI_NEW_STMT); + + FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs) + { + if (is_gimple_debug (use_stmt)) + continue; + + int ovf_use = uaddsub_overflow_check_p (stmt, use_stmt); + if (ovf_use == 0) + continue; + if (gimple_code (use_stmt) == GIMPLE_COND) + { + gcond *cond_stmt = as_a (use_stmt); + gimple_cond_set_lhs (cond_stmt, ovf); + gimple_cond_set_rhs (cond_stmt, build_int_cst (type, 0)); + gimple_cond_set_code (cond_stmt, ovf_use == 1 ? NE_EXPR : EQ_EXPR); + } + else + { + gcc_checking_assert (is_gimple_assign (use_stmt)); + if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS) + { + gimple_assign_set_rhs1 (use_stmt, ovf); + gimple_assign_set_rhs2 (use_stmt, build_int_cst (type, 0)); + gimple_assign_set_rhs_code (use_stmt, + ovf_use == 1 ? NE_EXPR : EQ_EXPR); + } + else + { + gcc_checking_assert (gimple_assign_rhs_code (use_stmt) + == COND_EXPR); + tree cond = build2 (ovf_use == 1 ? NE_EXPR : EQ_EXPR, + boolean_type_node, ovf, + build_int_cst (type, 0)); + gimple_assign_set_rhs1 (use_stmt, cond); + } + } + update_stmt (use_stmt); + } + return true; +} + + /* Find integer multiplications where the operands are extended from smaller types, and replace the MULT_EXPR with a WIDEN_MULT_EXPR where appropriate. */ @@ -3563,7 +3746,8 @@ pass_optimize_widening_mul::execute (function *fun) case PLUS_EXPR: case MINUS_EXPR: - convert_plusminus_to_widen (&gsi, stmt, code); + if (!convert_plusminus_to_widen (&gsi, stmt, code)) + match_uaddsub_overflow (&gsi, stmt, code); break; default:;