This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[JAVA PATCH] Implement RSHIFT_EXPR of unsigned types with iushr


The following patch is the second and lesser part of my solution for PR
java/19295, see http://gcc.gnu.org/ml/gcc-patches/2005-01/msg01669.html

The source of the "useless" NOP_EXPRs in the bugzilla PR is the result
of the middle-end attempting to synthesize a logical right shift.  On
some platforms logical right shift is cheaper than arithmetic right
shift, so "fold" prefers this form if there's no preference.  Hence
when converting "(x & C) != 0" into "(x >> C') & 1", it actually creates
the "(int)((unsigned int)x >> C') & 1".  Notice that either form of
right shift will do, as we ultimately mask out just a single bit.

This follow-up patch tweaks jcf-write.c to actually honor the
UNSIGNED_TYPE semantics of the RSHIFT_EXPR tree code.  This one-liner
just selects either iushr (logical right shift) or ishr (arithmetic
right shift) depending the signedness of the underlying integer type.


[To ease the review, I should point out that the only java front-end
"native" unsigned type is Java's 16-bit "char" type which in the JVM is
always zero-extended to an "int" stack slot; which means it should be
unaffected by this change as ishr and iushr are equivalent for zero
extended types.  This change should only affect "synthesized" types.]



Whilst not necessary for correctness (in this case), correctly/fully
implementing RSHIFT_EXPR might allow gcj to eventually take advantage
of GCC's bit-field optimizations.  It also might help avoid the need
for the Java front-end's URSHIFT_EXPR tree node.


The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap", all languages including java, and regression tested
with a top-level "make -k check" with no new failures in the libjava
testsuite.


Ok for mainline?



2005-01-24  Roger Sayle  <roger@eyesopen.com>

	* jcf-write.c (generate_bytecode_insns): Implement RSHIFT_EXPR
	of unsigned types using iushr and lushr JVM bytecodes.



Index: jcf-write.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/jcf-write.c,v
retrieving revision 1.160
diff -c -3 -p -r1.160 jcf-write.c
*** jcf-write.c	24 Jan 2005 14:34:12 -0000	1.160
--- jcf-write.c	24 Jan 2005 23:33:41 -0000
*************** generate_bytecode_insns (tree exp, int t
*** 2146,2152 ****
        jopcode = OPCODE_irem;
        goto binop;
      case LSHIFT_EXPR:   jopcode = OPCODE_ishl;   goto binop;
!     case RSHIFT_EXPR:   jopcode = OPCODE_ishr;   goto binop;
      case URSHIFT_EXPR:  jopcode = OPCODE_iushr;  goto binop;
      case TRUTH_AND_EXPR:
      case BIT_AND_EXPR:  jopcode = OPCODE_iand;   goto binop;
--- 2146,2154 ----
        jopcode = OPCODE_irem;
        goto binop;
      case LSHIFT_EXPR:   jopcode = OPCODE_ishl;   goto binop;
!     case RSHIFT_EXPR:
!       jopcode = TYPE_UNSIGNED (type) ? OPCODE_iushr : OPCODE_ishr;
!       goto binop;
      case URSHIFT_EXPR:  jopcode = OPCODE_iushr;  goto binop;
      case TRUTH_AND_EXPR:
      case BIT_AND_EXPR:  jopcode = OPCODE_iand;   goto binop;


Roger
--


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]