Bug 13250 - [3.4 only] [SH] Gcc code for rotation clobbers the register, but gcc continues to use the register as if it was not clobbered
Summary: [3.4 only] [SH] Gcc code for rotation clobbers the register, but gcc continue...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: 3.3.5
Assignee: Kazumoto Kojima
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2003-12-01 01:49 UTC by Valeriy E. Ushakov
Modified: 2004-09-09 23:40 UTC (History)
1 user (show)

See Also:
Host: i386-unknown-freebsd4.8
Target: shle--netbsdelf
Build: i386-unknown-freebsd4.8
Known to work: 4.0 3.4.1 3.3.5
Known to fail:
Last reconfirmed: 2004-01-21 04:03:24


Attachments
test case that demonstrates the problem (1.17 KB, text/plain)
2003-12-01 01:50 UTC, Valeriy E. Ushakov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Valeriy E. Ushakov 2003-12-01 01:49:25 UTC
gcc-3.3.2 and 3.4 miscompile sha256 digest code for SuperH, minimized test case 
attached. 
 
The sha256 transformation computes a sum that has (amongst others) the following 
subexpressions: 
 
+ (... ^ ((((e)) >> (25)) | (((e)) << (32 - (25))))) 
+ (((e) & (f)) ^ ((~(e)) & (g))) 
 
Code generated with -O is: 
 
	swap.w	r4,r1		! r4 contains "e" 
	shll8	r4		! "e" in r4 gets clobbered 
	shlr8	r1 
	or	r4,r1 
	rotr	r1		! rotate "e" 25 bits 
	xor	r1,r2 
	mov.l	@(8,r14),r1 
	add	r1,r2		! enclosing sum 
	mov	r4,r3		! <-- gcc thinks r4 still has the value of "e" !!! 
	and	r13,r3		! e & f 
	not	r4,r1		! ~e 
	and	r6,r1		! ~e & g 
	xor	r1,r3		! (e & f) ^ (~e & g) 
	add	r3,r2		! add into the sum
Comment 1 Valeriy E. Ushakov 2003-12-01 01:50:39 UTC
Created attachment 5247 [details]
test case that demonstrates the problem
Comment 2 Dara Hazeghi 2004-01-21 04:03:24 UTC
Confirmed on both branches (and mainline). Any sh expert want to take a look?
Comment 3 Kazumoto Kojima 2004-05-27 03:19:06 UTC
This looks an SH target specific problem when generating rtl for rotation
like (e << 8) | (e >> 24).  It seems the patch below for sh.md fixes this.
I'll propose it to gcc-patch list after bootstrap and regtest.

--- ORIG/gcc/gcc/config/sh/sh.md	2004-05-14 10:08:34.000000000 +0900
+++ LOCAL/gcc/gcc/config/sh/sh.md	2004-05-27 08:32:37.000000000 +0900
@@ -2194,7 +2194,7 @@
 	parts[0] = gen_reg_rtx (SImode);
 	parts[1] = gen_reg_rtx (SImode);
 	emit_insn (gen_rotlsi3_16 (parts[2-choice], operands[1]));
-	parts[choice-1] = operands[1];
+	emit_move_insn (parts[choice-1], operands[1]);
 	emit_insn (gen_ashlsi3 (parts[0], parts[0], GEN_INT (8)));
 	emit_insn (gen_lshrsi3 (parts[1], parts[1], GEN_INT (8)));
 	emit_insn (gen_iorsi3 (operands[0], parts[0], parts[1]));

Comment 4 Valeriy E. Ushakov 2004-05-27 21:03:43 UTC
Subject: Re:  [SH] Gcc code for rotation clobbers the register, but gcc continues to use the register as if it was not clobbered

With this fix applied the test case program passes its tests.

-uwe
Comment 5 Kazumoto Kojima 2004-05-28 04:24:31 UTC
FYI, I've proposed the above patch:
http://gcc.gnu.org/ml/gcc-patches/2004-05/msg01782.html
Comment 6 CVS Commits 2004-05-28 05:47:43 UTC
Subject: Bug 13250

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	kkojima@gcc.gnu.org	2004-05-28 05:47:37

Modified files:
	gcc            : ChangeLog 
	gcc/config/sh  : sh.md 

Log message:
	PR target/13250
	* config/sh/sh.md (rotlsi3): Use emit_move_insn.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.3767&r2=2.3768
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/sh/sh.md.diff?cvsroot=gcc&r1=1.171&r2=1.172

Comment 7 CVS Commits 2004-06-01 00:07:59 UTC
Subject: Bug 13250

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-branch
Changes by:	kkojima@gcc.gnu.org	2004-06-01 00:07:52

Modified files:
	gcc            : ChangeLog 
	gcc/config/sh  : sh.md 

Log message:
	PR target/13250
	Backport from mainline:
	
	2004-05-28  Kaz Kojima  <kkojima@gcc.gnu.org>
	* config/sh/sh.md (rotlsi3): Use emit_move_insn.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=2.2326.2.461&r2=2.2326.2.462
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/sh/sh.md.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.165.4.2&r2=1.165.4.3

Comment 8 Andrew Pinski 2004-06-01 00:13:44 UTC
Fixed.
Comment 10 Andrew Pinski 2004-07-26 00:52:50 UTC
Fixed also in 3.3.5