diff mbox

Fix undefined behavior in h8300 backend

Message ID 56003A32.1070005@redhat.com
State New
Headers show

Commit Message

Jeff Law Sept. 21, 2015, 5:11 p.m. UTC
With the patch this time...

-------- Forwarded Message --------
Subject: [PATCH] Fix undefined behavior in h8300 backend
Date: Mon, 21 Sep 2015 11:10:04 -0600
From: Jeff Law <law@redhat.com>
To: gcc-patches@gcc.gnu.org


This was found when building all configurations using the trunk
compiler.  A run-of-the-mill problem with left shifting -1.

To make sure I didn't muck anything up badly, I went back to Kazu's
introduction of this pattern (2003), took his pseudo-code and turned it
into a trivial h8300 specific test.

I also verified newlib built with the new compiler.  newlib never
triggers this pattern though.

I briefly pondered adding tests for the many other patterns Kazu added
in 2003 as well as generalizing andsi3_ashift_n_lower, but I just
couldn't justify that much time on the H8 port :-)

Installed on the trunk.

Jeff
diff mbox

Patch

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index c25888a..b6527de 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@ 
+2015-09-21  Jeff Law  <law@redhat.com>
+
+	* config/h8300/h8300.md (andsi3_ashift_n_lower): Avoid undefined
+	behavior. 
+
 2015-09-21  Ulrich Weigand  <Ulrich.Weigand@de.ibm.com>
 
 	* config/spu/spu.c (spu_expand_insv): Avoid undefined behavior.
diff --git a/gcc/config/h8300/h8300.md b/gcc/config/h8300/h8300.md
index 4079b30..52213ac 100644
--- a/gcc/config/h8300/h8300.md
+++ b/gcc/config/h8300/h8300.md
@@ -3914,7 +3914,8 @@ 
    (clobber (match_scratch:QI 4 "=X,&r"))]
   "(TARGET_H8300H || TARGET_H8300S)
     && INTVAL (operands[2]) <= 15
-    && INTVAL (operands[3]) == ((-1 << INTVAL (operands[2])) & 0xffff)"
+    && UINTVAL (operands[3]) == ((HOST_WIDE_INT_M1U << INTVAL (operands[2]))
+				 & 0xffff)"
   "#"
   "&& reload_completed"
   [(parallel [(set (match_dup 5)
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index c65469f..7afd523 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@ 
+2015-09-21  Jeff Law  <law@redhat.com>
+
+	* gcc.target/h8300/andsi3_ashift_n_lower.c: New test.
+
 2015-09-21  Ville Voutilainen  <ville.voutilainen@gmail.com>
 
 	Complete the implementation of N4230, Nested namespace definition.
diff --git a/gcc/testsuite/gcc.target/h8300/andsi3_ashift_n_lower.c b/gcc/testsuite/gcc.target/h8300/andsi3_ashift_n_lower.c
new file mode 100644
index 0000000..8cacc52
--- /dev/null
+++ b/gcc/testsuite/gcc.target/h8300/andsi3_ashift_n_lower.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile }  */
+/* { dg-options "-mh -O2 -fomit-frame-pointer" }  */
+/* { dg-final { scan-assembler-times "extu" 1 } }  */
+
+unsigned long foo(unsigned long a)
+  { return (a << 4) & 0xffff; }
+