From 4349b15f97fcbb6b7cdd96bb5bff0af1eb78e653 Mon Sep 17 00:00:00 2001 From: Sudakshina Das Date: Tue, 7 Nov 2017 12:23:38 +0000 Subject: [PATCH] PR80131: Simplification of 1U << (31 - x) Currently the code A << (B - C) is not simplified. However at least a more specific case of 1U << (C -x) where C = precision(type) - 1 can be simplified to (1 << C) >> x. This is done by adding a new simplification rule in match.pd. 2017-11-07 Sudakshina Das gcc/ PR middle-end/80131 * match.pd: Simplify 1 << (C - x) where C = precision (x) - 1. testsuite/ PR middle-end/80131 * testsuite/gcc.dg/pr80131-1.c: New Test. From-SVN: r254496 --- gcc/ChangeLog | 5 +++++ gcc/match.pd | 13 +++++++++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr80131-1.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr80131-1.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index bbb51e3..ad93a20 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2017-11-07 Sudakshina Das + + PR middle-end/80131 + * match.pd: Simplify 1 << (C - x) where C = precision (x) - 1. + 2017-11-07 Marc Glisse * match.pd ((a&~b)|(a^b),(a&~b)^~a,(a|b)&~(a^b),a|~(a^b), diff --git a/gcc/match.pd b/gcc/match.pd index 40ac5da..49134bcb 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -605,6 +605,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && tree_nop_conversion_p (type, TREE_TYPE (@1))) (lshift @0 @2))) +/* Fold (1 << (C - x)) where C = precision(type) - 1 + into ((1 << C) >> x). */ +(simplify + (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3)) + (if (INTEGRAL_TYPE_P (type) + && wi::eq_p (@2, TYPE_PRECISION (type) - 1) + && single_use (@1)) + (if (TYPE_UNSIGNED (type)) + (rshift (lshift @0 @2) @3) + (with + { tree utype = unsigned_type_for (type); } + (convert (rshift (lshift (convert:utype @0) @2) @3)))))) + /* Fold (C1/X)*C2 into (C1*C2)/X. */ (simplify (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index bb0eb9b..2032379 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-11-07 Sudakshina Das + + PR middle-end/80131 + * testsuite/gcc.dg/pr80131-1.c: New Test. + 2017-11-07 Marc Glisse * gcc.dg/tree-ssa/bitops-1.c: New file. diff --git a/gcc/testsuite/gcc.dg/pr80131-1.c b/gcc/testsuite/gcc.dg/pr80131-1.c new file mode 100644 index 0000000..0bfe1f4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr80131-1.c @@ -0,0 +1,31 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target int32plus } */ +/* { dg-options "-fdump-tree-gimple" } */ + +/* Checks the simplification of: + 1 << (C - x) to (1 << C) >> x, where C = precision (type) - 1 + f1 is not simplified but f2, f3 and f4 are. */ + +__INT64_TYPE__ f1 (__INT64_TYPE__ i) +{ + return (__INT64_TYPE__)1 << (31 - i); +} + +__INT64_TYPE__ f2 (__INT64_TYPE__ i) +{ + return (__INT64_TYPE__)1 << (63 - i); +} + +__UINT64_TYPE__ f3 (__INT64_TYPE__ i) +{ + return (__UINT64_TYPE__)1 << (63 - i); +} + +__INT32_TYPE__ f4 (__INT32_TYPE__ i) +{ + return (__INT32_TYPE__)1 << (31 - i); +} + +/* { dg-final { scan-tree-dump-times "= 31 -" 1 "gimple" } } */ +/* { dg-final { scan-tree-dump-times "9223372036854775808 >>" 2 "gimple" } } */ +/* { dg-final { scan-tree-dump "2147483648 >>" "gimple" } } */ -- 2.7.4