diff mbox series

Fix match.pd ICEs on COMPLEX_CSTs (PR middle-end/84834)

Message ID 20180312211744.GW8577@tucnak
State New
Headers show
Series Fix match.pd ICEs on COMPLEX_CSTs (PR middle-end/84834) | expand

Commit Message

Jakub Jelinek March 12, 2018, 9:17 p.m. UTC
Hi!

The following two match.pd spots assume that integer_pow2p will return
true only for INTEGER_CSTs (it uses wi::to_wide on it), but as the testcase
shows, that is not always the case, we can get there with COMPLEX_CSTs.

Fixed by requiring that it is both INTEGER_CST and integer_pow2p,
bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

I went through other spots where integer_pow2p is used in match.pd and think
all other spots should be safe, I don't think we e.g. allow BIT_AND_EXPR
with COMPLEX_CST operand etc.

2018-03-12  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/84834
	* match.pd ((A & C) != 0 ? D : 0): Use INTEGER_CST@2 instead of
	integer_pow2p@2 and test integer_pow2p in condition.
	(A < 0 ? C : 0): Similarly for @1.

	* gcc.dg/pr84834.c: New test.


	Jakub

Comments

Richard Biener March 13, 2018, 8:10 a.m. UTC | #1
On Mon, 12 Mar 2018, Jakub Jelinek wrote:

> Hi!
> 
> The following two match.pd spots assume that integer_pow2p will return
> true only for INTEGER_CSTs (it uses wi::to_wide on it), but as the testcase
> shows, that is not always the case, we can get there with COMPLEX_CSTs.
> 
> Fixed by requiring that it is both INTEGER_CST and integer_pow2p,
> bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Ok.

Richard.

> I went through other spots where integer_pow2p is used in match.pd and think
> all other spots should be safe, I don't think we e.g. allow BIT_AND_EXPR
> with COMPLEX_CST operand etc.
> 
> 2018-03-12  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR middle-end/84834
> 	* match.pd ((A & C) != 0 ? D : 0): Use INTEGER_CST@2 instead of
> 	integer_pow2p@2 and test integer_pow2p in condition.
> 	(A < 0 ? C : 0): Similarly for @1.
> 
> 	* gcc.dg/pr84834.c: New test.
> 
> --- gcc/match.pd.jj	2018-03-06 08:06:13.000000000 +0100
> +++ gcc/match.pd	2018-03-12 19:18:24.144577077 +0100
> @@ -3566,16 +3566,18 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  (simplify
>   (cond
>    (ne (bit_and @0 integer_pow2p@1) integer_zerop)
> -  integer_pow2p@2 integer_zerop)
> - (with {
> -    int shift = (wi::exact_log2 (wi::to_wide (@2))
> -		 - wi::exact_log2 (wi::to_wide (@1)));
> -  }
> -  (if (shift > 0)
> -   (bit_and
> -    (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
> -   (bit_and
> -    (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
> +  INTEGER_CST@2 integer_zerop)
> + (if (integer_pow2p (@2))
> +  (with {
> +     int shift = (wi::exact_log2 (wi::to_wide (@2))
> +		  - wi::exact_log2 (wi::to_wide (@1)));
> +   }
> +   (if (shift > 0)
> +    (bit_and
> +     (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
> +    (bit_and
> +     (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
> +     @2)))))
>  
>  /* If we have (A & C) != 0 where C is the sign bit of A, convert
>     this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
> @@ -3595,8 +3597,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  (simplify
>   (cond
>    (lt @0 integer_zerop)
> -  integer_pow2p@1 integer_zerop)
> - (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
> +  INTEGER_CST@1 integer_zerop)
> + (if (integer_pow2p (@1)
> +      && !TYPE_UNSIGNED (TREE_TYPE (@0)))
>    (with {
>      int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
>     }
> --- gcc/testsuite/gcc.dg/pr84834.c.jj	2018-03-12 19:21:15.677631881 +0100
> +++ gcc/testsuite/gcc.dg/pr84834.c	2018-03-12 19:20:59.105626588 +0100
> @@ -0,0 +1,15 @@
> +/* PR middle-end/84834 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +
> +_Complex int
> +foo (int a)
> +{
> +  return a < 0;
> +}
> +
> +_Complex int
> +bar (int a)
> +{
> +  return (a & 8) ? (_Complex int) 16 : (_Complex int) 0;
> +}
> 
> 	Jakub
> 
>
diff mbox series

Patch

--- gcc/match.pd.jj	2018-03-06 08:06:13.000000000 +0100
+++ gcc/match.pd	2018-03-12 19:18:24.144577077 +0100
@@ -3566,16 +3566,18 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (simplify
  (cond
   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
-  integer_pow2p@2 integer_zerop)
- (with {
-    int shift = (wi::exact_log2 (wi::to_wide (@2))
-		 - wi::exact_log2 (wi::to_wide (@1)));
-  }
-  (if (shift > 0)
-   (bit_and
-    (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
-   (bit_and
-    (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
+  INTEGER_CST@2 integer_zerop)
+ (if (integer_pow2p (@2))
+  (with {
+     int shift = (wi::exact_log2 (wi::to_wide (@2))
+		  - wi::exact_log2 (wi::to_wide (@1)));
+   }
+   (if (shift > 0)
+    (bit_and
+     (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
+    (bit_and
+     (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
+     @2)))))
 
 /* If we have (A & C) != 0 where C is the sign bit of A, convert
    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
@@ -3595,8 +3597,9 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (simplify
  (cond
   (lt @0 integer_zerop)
-  integer_pow2p@1 integer_zerop)
- (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
+  INTEGER_CST@1 integer_zerop)
+ (if (integer_pow2p (@1)
+      && !TYPE_UNSIGNED (TREE_TYPE (@0)))
   (with {
     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
    }
--- gcc/testsuite/gcc.dg/pr84834.c.jj	2018-03-12 19:21:15.677631881 +0100
+++ gcc/testsuite/gcc.dg/pr84834.c	2018-03-12 19:20:59.105626588 +0100
@@ -0,0 +1,15 @@ 
+/* PR middle-end/84834 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+_Complex int
+foo (int a)
+{
+  return a < 0;
+}
+
+_Complex int
+bar (int a)
+{
+  return (a & 8) ? (_Complex int) 16 : (_Complex int) 0;
+}