Blame SOURCES/gcc48-pr78416.patch

001c85
2016-11-18  Jakub Jelinek  <jakub@redhat.com>
001c85
001c85
	PR middle-end/78416
001c85
	* expmed.c (expand_divmod): For modes wider than HWI, take into
001c85
	account implicit 1 bits above msb for EXACT_POWER_OF_2_OR_ZERO_P.
001c85
001c85
	* gcc.dg/torture/pr78416.c: New test.
001c85
001c85
--- gcc/expmed.c
001c85
+++ gcc/expmed.c
001c85
@@ -3844,7 +3844,15 @@ expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
001c85
       if (unsignedp)
001c85
 	ext_op1 &= GET_MODE_MASK (mode);
001c85
       op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
001c85
-		     || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
001c85
+		      /* If mode is wider than HWI and op1 has msb set,
001c85
+			 then it has there extra implicit 1 bits above it.  */
001c85
+		      && (GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
001c85
+			  || INTVAL (op1) >= 0))
001c85
+		     || (! unsignedp
001c85
+			 && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1)
001c85
+			 && (GET_MODE_PRECISION (mode)
001c85
+			     <= HOST_BITS_PER_WIDE_INT
001c85
+			     || INTVAL (op1) < 0)));
001c85
     }
001c85
 
001c85
   /*
001c85
@@ -3987,8 +3995,17 @@ expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
001c85
       op1_is_constant = CONST_INT_P (op1);
001c85
       op1_is_pow2 = (op1_is_constant
001c85
 		     && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
001c85
-			  || (! unsignedp
001c85
-			      && EXACT_POWER_OF_2_OR_ZERO_P (-UINTVAL (op1))))));
001c85
+			  /* If mode is wider than HWI and op1 has msb set,
001c85
+			     then it has there extra implicit 1 bits above
001c85
+			     it.  */
001c85
+			  && (GET_MODE_PRECISION (compute_mode)
001c85
+			      <= HOST_BITS_PER_WIDE_INT
001c85
+			      || INTVAL (op1) >= 0))
001c85
+			 || (! unsignedp
001c85
+			     && EXACT_POWER_OF_2_OR_ZERO_P (-UINTVAL (op1))
001c85
+			     && (GET_MODE_PRECISION (compute_mode)
001c85
+				 <= HOST_BITS_PER_WIDE_INT
001c85
+				 || INTVAL (op1) < 0))));
001c85
     }
001c85
 
001c85
   /* If one of the operands is a volatile MEM, copy it into a register.  */
001c85
@@ -4031,7 +4048,8 @@ expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
001c85
 		unsigned HOST_WIDE_INT d = (INTVAL (op1)
001c85
 					    & GET_MODE_MASK (compute_mode));
001c85
 
001c85
-		if (EXACT_POWER_OF_2_OR_ZERO_P (d))
001c85
+		if (EXACT_POWER_OF_2_OR_ZERO_P (d)
001c85
+		    && (INTVAL (op1) >= 0 || size <= HOST_BITS_PER_WIDE_INT))
001c85
 		  {
001c85
 		    pre_shift = floor_log2 (d);
001c85
 		    if (rem_flag)
001c85
@@ -4179,6 +4197,7 @@ expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
001c85
 		      goto fail1;
001c85
 		  }
001c85
 		else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
001c85
+			 && (size <= HOST_BITS_PER_WIDE_INT || d >= 0)
001c85
 			 && (rem_flag
001c85
 			     ? smod_pow2_cheap (speed, compute_mode)
001c85
 			     : sdiv_pow2_cheap (speed, compute_mode))
001c85
@@ -4192,7 +4211,9 @@ expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
001c85
 						compute_mode)
001c85
 				 != CODE_FOR_nothing)))
001c85
 		  ;
001c85
-		else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
001c85
+		else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d)
001c85
+			 && (size <= HOST_BITS_PER_WIDE_INT
001c85
+			     || abs_d != (unsigned HOST_WIDE_INT) d))
001c85
 		  {
001c85
 		    if (rem_flag)
001c85
 		      {
001c85
@@ -4504,7 +4525,10 @@ expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
001c85
       case CEIL_MOD_EXPR:
001c85
 	if (unsignedp)
001c85
 	  {
001c85
-	    if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
001c85
+	    if (op1_is_constant
001c85
+		&& EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
001c85
+		&& (size <= HOST_BITS_PER_WIDE_INT
001c85
+		    || INTVAL (op1) >= 0))
001c85
 	      {
001c85
 		rtx t1, t2, t3;
001c85
 		unsigned HOST_WIDE_INT d = INTVAL (op1);
001c85
--- gcc/testsuite/gcc.dg/torture/pr78416.c
001c85
+++ gcc/testsuite/gcc.dg/torture/pr78416.c
001c85
@@ -0,0 +1,17 @@
001c85
+/* PR middle-end/78416 */
001c85
+/* { dg-do run { target int128 } } */
001c85
+
001c85
+int
001c85
+main ()
001c85
+{
001c85
+  unsigned __int128 x;
001c85
+  x = 0xFFFFFFFFFFFFFFFFULL;
001c85
+  x /= ~0x7FFFFFFFFFFFFFFFLL;
001c85
+  if (x != 0)
001c85
+    __builtin_abort ();
001c85
+  x = ~0x7FFFFFFFFFFFFFFELL;
001c85
+  x /= ~0x7FFFFFFFFFFFFFFFLL;
001c85
+  if (x != 1)
001c85
+    __builtin_abort ();
001c85
+  return 0;
001c85
+}