From patchwork Mon Jun 27 15:37:21 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kai Tietz X-Patchwork-Id: 102202 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 ECE67B6F65 for ; Tue, 28 Jun 2011 01:37:47 +1000 (EST) Received: (qmail 18262 invoked by alias); 27 Jun 2011 15:37:45 -0000 Received: (qmail 18095 invoked by uid 22791); 27 Jun 2011 15:37:44 -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 mx4-phx2.redhat.com (HELO mx4-phx2.redhat.com) (209.132.183.25) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 27 Jun 2011 15:37:21 +0000 Received: from mail06.corp.redhat.com (zmail06.collab.prod.int.phx2.redhat.com [10.5.5.45]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p5RFbL3r031320; Mon, 27 Jun 2011 11:37:21 -0400 Date: Mon, 27 Jun 2011 11:37:21 -0400 (EDT) From: Kai Tietz To: Richard Guenther Cc: gcc-patches@gcc.gnu.org Message-ID: <1382815177.792937.1309189041108.JavaMail.root@zmail06.collab.prod.int.phx2.redhat.com> In-Reply-To: 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 ----- 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? Revised patch without the bswap part. 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,56 @@ 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); - if (TREE_CODE (opp) == ADDR_EXPR) + opp = gimple_assign_rhs1 (def); + folded_int = fold_convert_loc (gimple_location (stmt), + TREE_TYPE (opp), arg2); + /* Check if integer value remains the same on cast back + to original type. */ + if (!operand_equal_p (arg2, fold_convert (TREE_TYPE (arg1), + folded_int), 0)) + folded_int = NULL_TREE; + if (TREE_CODE (opp) == ADDR_EXPR && folded_int) 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 +1708,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))