Re: [PATCH] Fix match.pd ICEs on COMPLEX_CSTs (PR middle-end/84834)

2018-03-13 Thread Richard Biener
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  
> 
>   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.0 +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.c2018-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
> 
> 

-- 
Richard Biener 
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Graham Norton, HRB 
21284 (AG Nuernberg)


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

2018-03-12 Thread Jakub Jelinek
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  

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.0 +0100
+++ gcc/match.pd2018-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