[gcc/devel/omp/gcc-9] fold-const.c (extract_muldiv_1): Do not distribute a multiplication by a power-of-two value.

Tobias Burnus burnus@gcc.gnu.org
Thu Mar 5 13:57:00 GMT 2020


https://gcc.gnu.org/g:00acb8f17e46140a8c8f9aa9b4c2ea0224f00369

commit 00acb8f17e46140a8c8f9aa9b4c2ea0224f00369
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Wed Jun 5 14:21:37 2019 +0000

    fold-const.c (extract_muldiv_1): Do not distribute a multiplication by a power-of-two value.
    
    	* fold-const.c (extract_muldiv_1) <PLUS_EXPR>: Do not distribute a
    	multiplication by a power-of-two value.
    	(fold_plusminus_mult_expr): Use pow2p_hwi to spot a power-of-two value
    	and turn the modulo operation into a masking operation.
    
    From-SVN: r271964

Diff:
---
 gcc/ChangeLog                          |  7 +++++++
 gcc/fold-const.c                       | 22 ++++++++++++++--------
 gcc/testsuite/ChangeLog                |  4 ++++
 gcc/testsuite/gnat.dg/specs/discr6.ads | 21 +++++++++++++++++++++
 4 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 5f378be..7dcaa7c 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2019-06-05  Eric Botcazou  <ebotcazou@adacore.com>
+
+	* fold-const.c (extract_muldiv_1) <PLUS_EXPR>: Do not distribute a
+	multiplication by a power-of-two value.
+	(fold_plusminus_mult_expr): Use pow2p_hwi to spot a power-of-two value
+	and turn the modulo operation into a masking operation.
+
 2019-06-05  Jakub Jelinek  <jakub@redhat.com>
 
 	PR debug/90733
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index c425772..a6b0b53 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -6475,8 +6475,12 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
 	 apply the distributive law to commute the multiply and addition
 	 if the multiplication of the constants doesn't overflow
 	 and overflow is defined.  With undefined overflow
-	 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.  */
-      if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
+	 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
+	 But fold_plusminus_mult_expr would factor back any power-of-two
+	 value so do not distribute in the first place in this case.  */
+      if (code == MULT_EXPR
+	  && TYPE_OVERFLOW_WRAPS (ctype)
+	  && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
 	return fold_build2 (tcode, ctype,
 			    fold_build2 (code, ctype,
 					 fold_convert (ctype, op0),
@@ -7124,14 +7128,13 @@ fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
   /* No identical multiplicands; see if we can find a common
      power-of-two factor in non-power-of-two multiplies.  This
      can help in multi-dimensional array access.  */
-  else if (tree_fits_shwi_p (arg01)
-	   && tree_fits_shwi_p (arg11))
+  else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
     {
-      HOST_WIDE_INT int01, int11, tmp;
+      HOST_WIDE_INT int01 = tree_to_shwi (arg01);
+      HOST_WIDE_INT int11 = tree_to_shwi (arg11);
+      HOST_WIDE_INT tmp;
       bool swap = false;
       tree maybe_same;
-      int01 = tree_to_shwi (arg01);
-      int11 = tree_to_shwi (arg11);
 
       /* Move min of absolute values to int11.  */
       if (absu_hwi (int01) < absu_hwi (int11))
@@ -7144,7 +7147,10 @@ fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
       else
 	maybe_same = arg11;
 
-      if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
+      const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
+      if (factor > 1
+	  && pow2p_hwi (factor)
+	  && (int01 & (factor - 1)) == 0
 	  /* The remainder should not be a constant, otherwise we
 	     end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
 	     increased the number of multiplications necessary.  */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index bf1b9d1..877dbbd 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2019-06-05  Eric Botcazou  <ebotcazou@adacore.com>
+
+	* gnat.dg/specs/discr6.ads: New test.
+
 2019-06-05  Jakub Jelinek  <jakub@redhat.com>
 
 	PR debug/90733
diff --git a/gcc/testsuite/gnat.dg/specs/discr6.ads b/gcc/testsuite/gnat.dg/specs/discr6.ads
new file mode 100644
index 0000000..af16553
--- /dev/null
+++ b/gcc/testsuite/gnat.dg/specs/discr6.ads
@@ -0,0 +1,21 @@
+-- { dg-do compile }
+
+package Discr6 is
+
+  subtype Index_T is Integer range 0 .. 15;
+
+  type Arr is array (Index_T range <> ) of Long_Long_Integer;
+
+  type Rec2 (Size : Index_T := 2) is record
+    A : Arr (2 .. Size);
+  end record;
+
+  type Rec3 (D : Boolean := False) is record
+    R : Rec2;
+    case D is
+      when False=> null;
+      when True => I : Integer;
+    end case;
+  end record;
+
+end Discr6;



More information about the Gcc-cvs mailing list