From patchwork Mon Jun 27 17:17:32 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kai Tietz X-Patchwork-Id: 102227 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id B70B1B6F5C for ; Tue, 28 Jun 2011 03:18:00 +1000 (EST) Received: (qmail 27020 invoked by alias); 27 Jun 2011 17:17:59 -0000 Received: (qmail 27009 invoked by uid 22791); 27 Jun 2011 17:17:57 -0000 X-SWARE-Spam-Status: No, hits=-6.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_TM, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx3-phx2.redhat.com (HELO mx3-phx2.redhat.com) (209.132.183.24) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 27 Jun 2011 17:17:32 +0000 Received: from mail06.corp.redhat.com (zmail06.collab.prod.int.phx2.redhat.com [10.5.5.45]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p5RHHWas005577; Mon, 27 Jun 2011 13:17:32 -0400 Date: Mon, 27 Jun 2011 13:17:32 -0400 (EDT) From: Kai Tietz To: Richard Guenther Cc: gcc-patches@gcc.gnu.org Message-ID: <1931110470.795470.1309195052046.JavaMail.root@zmail06.collab.prod.int.phx2.redhat.com> In-Reply-To: <892962636.795237.1309194244008.JavaMail.root@zmail06.collab.prod.int.phx2.redhat.com> Subject: Re: [patch tree-optimization]: Try to sink type-casts for binary and/or/xor operations MIME-Version: 1.0 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Ups, missed to update patch. Kai ----- Original Message ----- From: "Kai Tietz" To: "Richard Guenther" Cc: gcc-patches@gcc.gnu.org Sent: Monday, June 27, 2011 7:04:04 PM Subject: Re: [patch tree-optimization]: Try to sink type-casts for binary and/or/xor operations Hi, so I modified patch to use int_fits_type_p() for integer CST checking. Well, this approach is - as discussed on IRC suboptimal - as my intial approach was for and-operations with precision type > precision type-x and unsigned type-x for constant values bigger then (type-x)~0. But well, those we miss now by int_fits_type_p() approach, too. And also we miss now the cases for that type is signed and type-x is unsigned with same precision. Anyway ... here is the updated patch Regards, Kai ----- Original Message ----- From: "Richard Guenther" To: "Kai Tietz" Cc: gcc-patches@gcc.gnu.org Sent: Monday, June 27, 2011 4:08:41 PM Subject: Re: [patch tree-optimization]: Try to sink type-casts for binary and/or/xor operations On Mon, Jun 27, 2011 at 3:46 PM, Kai Tietz wrote: > Hello, > > this patch sink type conversions in forward-propagate for the following patterns: > - ((type) X) op ((type) Y): If X and Y have compatible types. > - ((type) X) op CST: If the conversion of (type) ((type-x) CST) == CST and X has integral type. > - CST op ((type) X): If the conversion of (type) ((type-x) CST) == CST and X has integral type. See IRC comments. > Additionally it fixes another issue shown by this type-sinking in bswap detection. The bswap pattern matching algorithm goes for the first hit, and not tries to seek for best hit.  So we search here two times. First for di case (if present) and then for si mode case. Please split this piece out. I suppose either walking over stmts backwards or simply handling __builtin_bswap in find_bswap_1 would be a better fix than yours. Richard. > ChangeLog > > 2011-06-27  Kai Tietz   > >        * tree-ssa-forwprop.c (simplify_bitwise_binary): Improve >        type sinking. >        * tree-ssa-math-opts.c (execute_optimize_bswap): Separate >        search for di/si mode patterns for finding widest match. > > Bootstrapped and regression tested for x86_64-pc-linux-gnu.  Ok for apply? > > Regards, > Kai > Index: gcc-head/gcc/tree-ssa-forwprop.c =================================================================== --- gcc-head.orig/gcc/tree-ssa-forwprop.c +++ gcc-head/gcc/tree-ssa-forwprop.c @@ -1624,30 +1624,54 @@ simplify_bitwise_binary (gimple_stmt_ite /* If the first argument is an SSA name that is itself a result of a typecast of an ADDR_EXPR to an integer, feed the ADDR_EXPR to the folder rather than the ssa name. */ - if (code == BIT_AND_EXPR - && TREE_CODE (arg2) == INTEGER_CST + if (TREE_CODE (arg2) == INTEGER_CST && TREE_CODE (arg1) == SSA_NAME) { gimple def = SSA_NAME_DEF_STMT (arg1); tree op = arg1; + tree opp = NULL_TREE; + tree folded_int = NULL_TREE; - /* ??? This looks bogus - the conversion could be truncating. */ if (is_gimple_assign (def) && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)) && INTEGRAL_TYPE_P (TREE_TYPE (arg1))) { - tree opp = gimple_assign_rhs1 (def); + opp = gimple_assign_rhs1 (def); + if (INTEGRAL_TYPE_P (opp) + && int_fits_type_p (arg2, TREE_TYPE (opp))) + folded_int = fold_convert_loc (gimple_location (stmt), + TREE_TYPE (opp), arg2); + /* ??? This looks bogus - the conversion could be truncating. */ if (TREE_CODE (opp) == ADDR_EXPR) op = opp; } + if (code == BIT_AND_EXPR) + { + res = fold_binary_loc (gimple_location (stmt), + BIT_AND_EXPR, + TREE_TYPE (gimple_assign_lhs (stmt)), + op, arg2); + if (res && is_gimple_min_invariant (res)) + { + gimple_assign_set_rhs_from_tree (gsi, res); + update_stmt (stmt); + return true; + } + } - res = fold_binary_loc (gimple_location (stmt), - BIT_AND_EXPR, TREE_TYPE (gimple_assign_lhs (stmt)), - op, arg2); - if (res && is_gimple_min_invariant (res)) - { - gimple_assign_set_rhs_from_tree (gsi, res); - update_stmt (stmt); + /* Convert (type) X & CST -> (type) (X & (typeof-X) CST), + if conversion of CST is reversible. */ + if (opp != NULL_TREE && folded_int != NULL_TREE) + { + gimple newop; + tree tem = create_tmp_reg (TREE_TYPE (opp), NULL); + newop = gimple_build_assign_with_ops (code, tem, opp, folded_int); + tem = make_ssa_name (tem, newop); + gimple_assign_set_lhs (newop, tem); + gsi_insert_before (gsi, newop, GSI_SAME_STMT); + gimple_assign_set_rhs_with_ops_1 (gsi, NOP_EXPR, + tem, NULL_TREE, NULL_TREE); + update_stmt (gsi_stmt (*gsi)); return true; } } @@ -1682,10 +1706,11 @@ simplify_bitwise_binary (gimple_stmt_ite if (CONVERT_EXPR_CODE_P (def1_code) && CONVERT_EXPR_CODE_P (def2_code) && types_compatible_p (TREE_TYPE (def1_arg1), TREE_TYPE (def2_arg1)) - /* Make sure that the conversion widens the operands or that it - changes the operation to a bitfield precision. */ + /* Make sure that the conversion widens the operands, or has same + precision, or that it changes the operation to a bitfield + precision. */ && ((TYPE_PRECISION (TREE_TYPE (def1_arg1)) - < TYPE_PRECISION (TREE_TYPE (arg1))) + <= TYPE_PRECISION (TREE_TYPE (arg1))) || (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (arg1))) != MODE_INT) || (TYPE_PRECISION (TREE_TYPE (arg1))