public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/25186] (short)(((int)short_var) <<1) should be folded so that the shift is done in the short type
Date: Thu, 01 Dec 2005 12:28:00 -0000	[thread overview]
Message-ID: <20051201122812.1200.qmail@sourceware.org> (raw)
In-Reply-To: <bug-25186-6528@http.gcc.gnu.org/bugzilla/>



------- Comment #4 from rguenth at gcc dot gnu dot org  2005-12-01 12:28 -------
convert_to_integer contains

        case LSHIFT_EXPR:
          /* We can pass truncation down through left shifting
             when the shift count is a nonnegative constant and
             the target type is unsigned.  */
          if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
              && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0 
              && TYPE_UNSIGNED (type)
              && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)

which for our case then (should) falls through to

...
                    /* Don't do unsigned arithmetic where signed was wanted,
                       or vice versa.
                       Exception: if both of the original operands were
                       unsigned then we can safely do the work as unsigned.
                       Exception: shift operations take their type solely
                       from the first argument.
                       Exception: the LSHIFT_EXPR case above requires that
                       we perform this operation unsigned lest we produce
                       signed-overflow undefinedness.
                       And we may need to do it as unsigned
                       if we truncate to the original size.  */
                    if (TYPE_UNSIGNED (TREE_TYPE (expr))
                        || (TYPE_UNSIGNED (TREE_TYPE (arg0))
                            && (TYPE_UNSIGNED (TREE_TYPE (arg1))
                                || ex_form == LSHIFT_EXPR
                                || ex_form == RSHIFT_EXPR
                                || ex_form == LROTATE_EXPR
                                || ex_form == RROTATE_EXPR))
                        || ex_form == LSHIFT_EXPR)
                      typex = lang_hooks.types.unsigned_type (typex);
                    else
                      typex = lang_hooks.types.signed_type (typex);

now, this path seems to handle LSHIFT_EXPR of signed types, so the exeption
above does not need to apply.  Further, I don't understand the reasoning
why we need to do the shift unsigned - we invoke undefined behavior for
signed overflow, but the original code, (short) int << n invoked undefined
behavior in truncating the int to short in case the value doesn't fit.  Which
of course still happens for the unsigned case.

So, what would break with

Index: convert.c 
===================================================================
*** convert.c   (revision 107813)
--- convert.c   (working copy)
*************** convert_to_integer (tree type, tree expr
*** 512,518 ****
             the target type is unsigned.  */ 
          if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
              && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
-             && TYPE_UNSIGNED (type)
              && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
            {
              /* If shift count is less than the width of the truncated type,
--- 490,495 ----
*************** convert_to_integer (tree type, tree expr
*** 607,614 ****
                                || ex_form == LSHIFT_EXPR
                                || ex_form == RSHIFT_EXPR
                                || ex_form == LROTATE_EXPR
!                               || ex_form == RROTATE_EXPR)) 
!                       || ex_form == LSHIFT_EXPR)
                      typex = lang_hooks.types.unsigned_type (typex);
                    else
                      typex = lang_hooks.types.signed_type (typex);
--- 584,590 ----
                                || ex_form == LSHIFT_EXPR
                                || ex_form == RSHIFT_EXPR
                                || ex_form == LROTATE_EXPR
!                               || ex_form == RROTATE_EXPR)))
                      typex = lang_hooks.types.unsigned_type (typex);
                    else
                      typex = lang_hooks.types.signed_type (typex);

?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25186


  parent reply	other threads:[~2005-12-01 12:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-30 18:21 [Bug middle-end/25186] New: (short)(((int)short_var) <<1) should be done in short pinskia at gcc dot gnu dot org
2005-11-30 18:32 ` [Bug middle-end/25186] " pinskia at gcc dot gnu dot org
2005-12-01 10:53 ` [Bug middle-end/25186] (short)(((int)short_var) <<1) should be folded so that the shift is done in the short type rguenth at gcc dot gnu dot org
2005-12-01 11:01 ` rguenth at gcc dot gnu dot org
2005-12-01 12:28 ` rguenth at gcc dot gnu dot org [this message]
2005-12-01 12:46 ` rguenth at gcc dot gnu dot org
2005-12-01 14:17 ` rguenth at gcc dot gnu dot org
2006-04-05 13:49 ` rguenth at gcc dot gnu dot org
     [not found] <bug-25186-4@http.gcc.gnu.org/bugzilla/>
2021-09-02  2:03 ` gabravier at gmail dot com
2021-09-02  7:15 ` rguenth at gcc dot gnu.org
2023-04-22  8:06 ` roger at nextmovesoftware dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051201122812.1200.qmail@sourceware.org \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).