From patchwork Sat Jul 4 02:58:58 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 50650 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f72.google.com (mail-la0-f72.google.com [209.85.215.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id E32D5214B3 for ; Sat, 4 Jul 2015 02:59:27 +0000 (UTC) Received: by laar3 with SMTP id r3sf32990690laa.1 for ; Fri, 03 Jul 2015 19:59:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:mailing-list:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:sender :delivered-to:message-id:date:from:user-agent:mime-version:to:cc :subject:content-type:x-original-sender :x-original-authentication-results; bh=rTg0RChuvFhZl5KS8R9R9V8/9l7ux+pWL60nUQPXwY0=; b=MN/Gpkg+nuPt4NR7hrOWyLUz11lWGZKkJsuw9jdBdN5kZ7R7rbDK2Kosc7ccRTeuH9 Z4mkOaK+svwbfHdF18DomGSbaA/p5zfkb0qo6qd4+knzGPcPmnSNiYx1sfPOMFRgrLgv Rq7Ae7A0Ijz/0Ac20xGsBR/QyGgkyyMEYO9vDhfHfrEMGAs1sdFx4X3IZjDmcf4tKNeu 2z3LmukkvibDyVievChIQ70c8lePW2S/dMkbRM9QSNRjYBU3955DPy94rlKJUZ1leJIv rt4Yzz3FCuRxRDgy3TCMGKTBek4AwKmcdmwp3iYIb80iFGGLpT9Dq2uruPVoLtf8p1im SNag== X-Gm-Message-State: ALoCoQlEBMRWv2aORaf/JlqiT3RlTF3INELMeE8ijdHFvMGc1hs+U+eYkYOrGQgjSi4alqwDIyQs X-Received: by 10.112.14.101 with SMTP id o5mr24008187lbc.3.1435978766154; Fri, 03 Jul 2015 19:59:26 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.30.6 with SMTP id o6ls498289lah.56.gmail; Fri, 03 Jul 2015 19:59:26 -0700 (PDT) X-Received: by 10.152.23.38 with SMTP id j6mr37585629laf.47.1435978765999; Fri, 03 Jul 2015 19:59:25 -0700 (PDT) Received: from mail-la0-x236.google.com (mail-la0-x236.google.com. [2a00:1450:4010:c03::236]) by mx.google.com with ESMTPS id p5si8819522lap.109.2015.07.03.19.59.25 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Jul 2015 19:59:25 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::236 as permitted sender) client-ip=2a00:1450:4010:c03::236; Received: by lagx9 with SMTP id x9so101413679lag.1 for ; Fri, 03 Jul 2015 19:59:25 -0700 (PDT) X-Received: by 10.153.5.38 with SMTP id cj6mr38445039lad.36.1435978764976; Fri, 03 Jul 2015 19:59:24 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp373157lbb; Fri, 3 Jul 2015 19:59:23 -0700 (PDT) X-Received: by 10.70.137.39 with SMTP id qf7mr25314713pdb.39.1435978762951; Fri, 03 Jul 2015 19:59:22 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id nu1si17078169pdb.256.2015.07.03.19.59.21 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Jul 2015 19:59:22 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-401973-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 51970 invoked by alias); 4 Jul 2015 02:59:10 -0000 Mailing-List: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org Precedence: list 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 Received: (qmail 51958 invoked by uid 89); 4 Jul 2015 02:59:09 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-pa0-f41.google.com Received: from mail-pa0-f41.google.com (HELO mail-pa0-f41.google.com) (209.85.220.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Sat, 04 Jul 2015 02:59:07 +0000 Received: by pactm7 with SMTP id tm7so64687399pac.2 for ; Fri, 03 Jul 2015 19:59:04 -0700 (PDT) X-Received: by 10.66.55.105 with SMTP id r9mr83465605pap.143.1435978744699; Fri, 03 Jul 2015 19:59:04 -0700 (PDT) Received: from [10.1.1.8] (58-6-183-210.dyn.iinet.net.au. [58.6.183.210]) by mx.google.com with ESMTPSA id v8sm10475816pdm.89.2015.07.03.19.59.02 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Jul 2015 19:59:04 -0700 (PDT) Message-ID: <55974BF2.3060603@linaro.org> Date: Sat, 04 Jul 2015 12:58:58 +1000 From: Kugan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Jeff Law Subject: [PR66726] Factor conversion out of COND_EXPR X-IsSubscribed: yes X-Original-Sender: kugan.vivekanandarajah@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::236 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@gcc.gnu.org X-Google-Group-Id: 836684582541 Please find a patch that attempt to FIX PR66726 by factoring conversion out of COND_EXPR as explained in the PR. Bootstrapped and regression tested on x86-64-none-linux-gnu with no new regressions. Is this OK for trunk? Thanks, Kugan gcc/testsuite/ChangeLog: 2015-07-03 Kugan Vivekanandarajah Jeff Law PR middle-end/66726 * gcc.dg/tree-ssa/pr66726.c: New test. gcc/ChangeLog: 2015-07-03 Kugan Vivekanandarajah PR middle-end/66726 * tree-ssa-phiopt.c (factor_out_conditional_conversion): New function. (tree_ssa_phiopt_worker): Call factor_out_conditional_conversion. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr66726.c b/gcc/testsuite/gcc.dg/tree-ssa/pr66726.c index e69de29..b636c8f 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr66726.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr66726.c @@ -0,0 +1,13 @@ + +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-phiopt2" } */ + +extern unsigned short mode_size[]; +int +oof (int mode) +{ + return (64 < mode_size[mode] ? 64 : mode_size[mode]); +} + +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt2" } } */ + diff --git a/gcc/tree-ssa-phiopt.c b/gcc/tree-ssa-phiopt.c index d2a5cee..e8af086 100644 --- a/gcc/tree-ssa-phiopt.c +++ b/gcc/tree-ssa-phiopt.c @@ -73,6 +73,7 @@ along with GCC; see the file COPYING3. If not see static unsigned int tree_ssa_phiopt_worker (bool, bool); static bool conditional_replacement (basic_block, basic_block, edge, edge, gphi *, tree, tree); +static bool factor_out_conditional_conversion (edge, edge, gphi *, tree, tree); static int value_replacement (basic_block, basic_block, edge, edge, gimple, tree, tree); static bool minmax_replacement (basic_block, basic_block, @@ -342,6 +343,8 @@ tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads) cfgchanged = true; else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1)) cfgchanged = true; + else if (factor_out_conditional_conversion (e1, e2, phi, arg0, arg1)) + cfgchanged = true; } } @@ -410,6 +413,108 @@ replace_phi_edge_with_variable (basic_block cond_block, bb->index); } +/* PR66726: Factor conversion out of COND_EXPR. If the argument of the PHI + stmt are CONVERT_STMT, factor out the conversion and perform the conversion + to the result of PHI stmt. */ + +static bool +factor_out_conditional_conversion (edge e0, edge e1, gphi *phi, + tree arg0, tree arg1) +{ + gimple def0 = NULL, def1 = NULL, new_stmt; + tree new_arg0 = NULL_TREE, new_arg1 = NULL_TREE; + tree temp, result; + gimple_stmt_iterator gsi; + + /* One of the argument has to be SSA_NAME and other argument can + be an SSA_NAME of INTEGER_CST. */ + if ((TREE_CODE (arg0) != SSA_NAME + && TREE_CODE (arg0) != INTEGER_CST) + || (TREE_CODE (arg1) != SSA_NAME + && TREE_CODE (arg1) != INTEGER_CST) + || (TREE_CODE (arg0) == INTEGER_CST + && TREE_CODE (arg1) == INTEGER_CST)) + return false; + + /* Handle only PHI statements with two arguments. TODO: If all + other arguments to PHI are INTEGER_CST, we can handle more + than two arguments too. */ + if (gimple_phi_num_args (phi) != 2) + return false; + + /* If arg0 is an SSA_NAME and the stmt which defines arg0 is + ai CONVERT_STMT, use the LHS as new_arg0. */ + if (TREE_CODE (arg0) == SSA_NAME) + { + def0 = SSA_NAME_DEF_STMT (arg0); + if (!is_gimple_assign (def0) + || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def0))) + return false; + new_arg0 = gimple_assign_rhs1 (def0); + } + + /* If arg1 is an SSA_NAME and the stmt which defines arg0 is + ai CONVERT_STMT, use the LHS as new_arg1. */ + if (TREE_CODE (arg1) == SSA_NAME) + { + def1 = SSA_NAME_DEF_STMT (arg1); + if (!is_gimple_assign (def1) + || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def1))) + return false; + new_arg1 = gimple_assign_rhs1 (def1); + } + + /* If arg0 is an INTEGER_CST, fold it to new type. */ + if (TREE_CODE (arg0) != SSA_NAME) + { + if (!POINTER_TYPE_P (TREE_TYPE (new_arg1)) + && int_fits_type_p (arg0, TREE_TYPE (new_arg1))) + new_arg0 = fold_convert (TREE_TYPE (new_arg1), arg0); + else + return false; + } + + /* If arg1 is an INTEGER_CST, fold it to new type. */ + if (TREE_CODE (arg1) != SSA_NAME) + { + if (!POINTER_TYPE_P (TREE_TYPE (new_arg0)) + && int_fits_type_p (arg1, TREE_TYPE (new_arg0))) + new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1); + else + return false; + } + + /* If types of new_arg0 and new_arg1 are different bailout. */ + if (TREE_TYPE (new_arg0) != TREE_TYPE (new_arg1)) + return false; + + /* Replace the PHI stmt with the new_arg0 and new_arg1. Also insert + a new CONVER_STMT that converts the phi results. */ + gsi = gsi_after_labels (gimple_bb (phi)); + result = PHI_RESULT (phi); + temp = make_ssa_name (TREE_TYPE (new_arg0), phi); + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "PHI "); + print_generic_expr (dump_file, gimple_phi_result (phi), 0); + fprintf (dump_file, + " changed to factor CONVERT_EXPR out from COND_EXPR.\n"); + fprintf (dump_file, "New PHI_RESULT is "); + print_generic_expr (dump_file, temp, 0); + fprintf (dump_file, " and new stmt with CONVERT_EXPR defines "); + print_generic_expr (dump_file, result, 0); + fprintf (dump_file, ".\n"); + } + + gimple_phi_set_result (phi, temp); + SET_PHI_ARG_DEF (phi, e0->dest_idx, new_arg0); + SET_PHI_ARG_DEF (phi, e1->dest_idx, new_arg1); + new_stmt = gimple_build_assign (result, CONVERT_EXPR, temp); + gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT); + return true; +} + /* The function conditional_replacement does the main work of doing the conditional replacement. Return true if the replacement is done. Otherwise return false. @@ -2144,6 +2249,26 @@ gate_hoist_loads (void) This pass also performs a fifth transformation of a slightly different flavor. + Factor conversion in COND_EXPR + ---------------------------------- + + This transformation factors the conversion out of COND_EXPR with + factor_out_conditional_conversion. + + For example: + if (a <= CST) goto ; else goto ; + : + tmp = (int) a; + : + tmp = PHI + + Into: + if (a <= CST) goto ; else goto ; + : + : + a = PHI + tmp = (int) a; + Adjacent Load Hoisting ----------------------