From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1354 invoked by alias); 1 Dec 2005 12:28:14 -0000 Received: (qmail 1201 invoked by uid 48); 1 Dec 2005 12:28:12 -0000 Date: Thu, 01 Dec 2005 12:28:00 -0000 Message-ID: <20051201122812.1200.qmail@sourceware.org> X-Bugzilla-Reason: CC References: Subject: [Bug middle-end/25186] (short)(((int)short_var) <<1) should be folded so that the shift is done in the short type In-Reply-To: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "rguenth at gcc dot gnu dot org" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2005-12/txt/msg00048.txt.bz2 List-Id: ------- 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