Bug 41738

Summary: [4.3/4.4 Regression] optabs expands rotate using wrong mode
Product: gcc Reporter: Andy Hutchinson <hutchinsonandy>
Component: middle-endAssignee: Not yet assigned to anyone <unassigned>
Severity: normal CC: eric.weddington, gcc-bugs, hutchinsonandy, sezeroz
Priority: P3 Keywords: wrong-code
Version: 4.4.2   
Target Milestone: 4.5.0   
Host: i686-pc-linux-gnu Target: avr-unknown-none
Build: Known to work: 4.2.3 4.5.0
Known to fail: 4.3.3 4.4.2 Last reconfirmed: 2009-10-17 15:04:59

Description Andy Hutchinson 2009-10-17 14:39:46 UTC
I have been adding rotate capability to AVR port and have come this bug in 
optabs.c: expand_binop()

This occurs during a rotate expansion. For example

target  = op0  rotated by op1

In the particular situation (code extract below) it tries a reverse rotate of (bits - op1). Where this expression is then expanded into an integer,
a negation or subtraction depending on type of op1 and target.

The expansion of the subtraction is using the mode of the target - it should be using the mode of op1.
The mode of the rotation  amount need not be the same as the target.

target:DI = Op0:DI rotate op1:HI

In my testcase it is not and I get asserts latter in simplfy_rtx with the mismatched inner/outer modes.

The negation mode looks equally wrong.

I found this on testcase 20010226-1.c with gcc 4.42 (line 55) - however, it would appear to be on 4.5 and perhaps earlier versions.

Revision 126370 added this particular code to optabs.c in July 2007

 /* If we were trying to rotate, and that didn't work, try rotating
    the other direction before falling back to shifts and bitwise-or.  */
 if (((binoptab == rotl_optab
   && optab_handler (rotr_optab, mode)->insn_code != CODE_FOR_nothing)
      || (binoptab == rotr_optab
      && optab_handler (rotl_optab, mode)->insn_code != CODE_FOR_nothing))
     && mclass == MODE_INT)
     optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
     rtx newop1;
     unsigned int bits = GET_MODE_BITSIZE (mode);

     if (CONST_INT_P (op1))
   newop1 = GEN_INT (bits - INTVAL (op1));
     else if (targetm.shift_truncation_mask (mode) == bits - 1)
   newop1 = negate_rtx (mode, op1);
   newop1 = expand_binop (mode, sub_optab,
                  GEN_INT (bits), op1,
                  NULL_RTX, unsignedp, OPTAB_DIRECT);
Comment 1 Richard Biener 2009-10-17 15:04:59 UTC
Comment 2 Andy Hutchinson 2009-10-17 22:42:34 UTC
Subject: Bug 41738

Author: hutchinsonandy
Date: Sat Oct 17 22:42:17 2009
New Revision: 152961

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=152961
2009-10-17  Andy Hutchinson <hutchinsonandy@gcc.gnu.org>
PR middle-end/41738
* optabs.c (expand_binop): Make mode of shift count expression mode of shift count not target.
Remove indent nit.


Comment 3 Andy Hutchinson 2009-11-22 19:04:04 UTC
Fixed 4.5
Back port needed.
Comment 4 Eric Weddington 2010-09-20 03:05:53 UTC
Closing as fixed in 4.5.0.