diff mbox

Fix undefined behaviour in arc port

Message ID 56064DD2.4020101@redhat.com
State New
Headers show

Commit Message

Jeff Law Sept. 26, 2015, 7:48 a.m. UTC
And now the ARC port.  Tested by building the arc configurations in 
config-list.mk with a trunk compiler.

Installed on the trunk,
Jeff
commit ce021a249da7da00877d20a4b3dc79b47eec19c1
Author: Jeff Law <law@tor.usersys.redhat.com>
Date:   Sat Sep 26 03:22:53 2015 -0400

    	[PATCH] Fix undefined behaviour in arc port
    
    	* config/arc/arc.c (arc_output_addsi): Fix left shift undefined
    	behaviour.
    	* config/arc/constraints.md (Cca, C2a): Fix left shift undefined
    	behaviour.

Comments

Andreas Schwab Sept. 26, 2015, 9:05 a.m. UTC | #1
Jeff Law <law@redhat.com> writes:

> @@ -9320,7 +9320,9 @@ arc_legitimize_reload_address (rtx *p, machine_mode mode, int opnum,
>        if ((scale-1) & offset)
>  	scale = 1;
>        shift = scale >> 1;
> -      offset_base = (offset + (256 << shift)) & (-512 << shift);
> +      offset_base
> +	= ((offset + (256 << shift))
> +	   & ((HOST_WIDE_INT)(-512U << shift)));

If HOST_WIDE_INT is bigger than int then this is not the same.

Andreas.
Andreas Schwab Sept. 26, 2015, 9:06 a.m. UTC | #2
Jeff Law <law@redhat.com> writes:

> @@ -195,7 +195,7 @@
>    "@internal
>     Unconditional two-address add / sub constant"
>    (and (match_code "const_int")
> -       (match_test "ival == -1 << 31
> +       (match_test "ival == HOST_WIDE_INT (HOST_WIDE_INT_M1U << 31)

Syntax error?

Andreas.
Jeff Law Sept. 28, 2015, 5:09 p.m. UTC | #3
On 09/26/2015 03:06 AM, Andreas Schwab wrote:
> Jeff Law <law@redhat.com> writes:
>
>> @@ -195,7 +195,7 @@
>>     "@internal
>>      Unconditional two-address add / sub constant"
>>     (and (match_code "const_int")
>> -       (match_test "ival == -1 << 31
>> +       (match_test "ival == HOST_WIDE_INT (HOST_WIDE_INT_M1U << 31)
>
> Syntax error?
Yes, though strangely it's not causing the port to fail to build.  Weird.

I'll take care of it.

THanks,
Jeff
Jeff Law Sept. 28, 2015, 5:12 p.m. UTC | #4
On 09/26/2015 03:05 AM, Andreas Schwab wrote:
> Jeff Law <law@redhat.com> writes:
>
>> @@ -9320,7 +9320,9 @@ arc_legitimize_reload_address (rtx *p, machine_mode mode, int opnum,
>>         if ((scale-1) & offset)
>>   	scale = 1;
>>         shift = scale >> 1;
>> -      offset_base = (offset + (256 << shift)) & (-512 << shift);
>> +      offset_base
>> +	= ((offset + (256 << shift))
>> +	   & ((HOST_WIDE_INT)(-512U << shift)));
>
> If HOST_WIDE_INT is bigger than int then this is not the same.
I'll fix this too.

jeff
diff mbox

Patch

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 28c6bf7..01fac46 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,5 +1,10 @@ 
 2015-09-26  Jeff Law  <law@redhat.com>
 
+	* config/arc/arc.c (arc_output_addsi): Fix left shift undefined
+	behaviour.
+	* config/arc/constraints.md (Cca, C2a): Fix left shift undefined
+	behaviour.
+
 	* config/sh/sh.h (CONST_OK_FOR_J16): Fix left shift undefined
 	behaviour
 
diff --git a/gcc/config/arc/arc.c b/gcc/config/arc/arc.c
index e9ecc90..4d731b5 100644
--- a/gcc/config/arc/arc.c
+++ b/gcc/config/arc/arc.c
@@ -7393,7 +7393,7 @@  arc_output_addsi (rtx *operands, bool cond_p, bool output_p)
       int range_factor = neg_intval & intval;
       int shift;
 
-      if (intval == -1 << 31)
+      if (intval == (HOST_WIDE_INT) (HOST_WIDE_INT_M1U << 31))
 	ADDSI_OUTPUT1 ("bxor%? %0,%1,31");
 
       /* If we can use a straight add / sub instead of a {add,sub}[123] of
@@ -9320,7 +9320,9 @@  arc_legitimize_reload_address (rtx *p, machine_mode mode, int opnum,
       if ((scale-1) & offset)
 	scale = 1;
       shift = scale >> 1;
-      offset_base = (offset + (256 << shift)) & (-512 << shift);
+      offset_base
+	= ((offset + (256 << shift))
+	   & ((HOST_WIDE_INT)(-512U << shift)));
       /* Sometimes the normal form does not suit DImode.  We
 	 could avoid that by using smaller ranges, but that
 	 would give less optimized code when SImode is
diff --git a/gcc/config/arc/constraints.md b/gcc/config/arc/constraints.md
index 8902246..b3ea115 100644
--- a/gcc/config/arc/constraints.md
+++ b/gcc/config/arc/constraints.md
@@ -167,7 +167,7 @@ 
   "@internal
    Conditional or three-address add / sub constant"
   (and (match_code "const_int")
-       (match_test "ival == -1 << 31
+       (match_test "ival == (HOST_WIDE_INT)(HOST_WIDE_INT_M1U << 31)
 		    || (ival >= -0x1f8 && ival <= 0x1f8
 			&& ((ival >= 0 ? ival : -ival)
 			    <= 0x3f * (ival & -ival)))")))
@@ -195,7 +195,7 @@ 
   "@internal
    Unconditional two-address add / sub constant"
   (and (match_code "const_int")
-       (match_test "ival == -1 << 31
+       (match_test "ival == HOST_WIDE_INT (HOST_WIDE_INT_M1U << 31)
 		    || (ival >= -0x4000 && ival <= 0x4000
 			&& ((ival >= 0 ? ival : -ival)
 			    <= 0x7ff * (ival & -ival)))")))