From patchwork Tue Feb 14 10:05:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Cheng X-Patchwork-Id: 727748 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vMyjm2HLMz9s7M for ; Tue, 14 Feb 2017 21:06:01 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="gEuo/52U"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:content-type:mime-version; q=dns; s=default; b=gybWne7Vbl0ytt2IfOphw5LqPiPMFWILFF1Imiw3fjDtq8DBmU cycClSBs/POSqhjt1Bs3+6U/izOoDCiuAux7oIOg/2pQ8tcEsrV6artUfGU6CAfw Q+J2lxaMTQojWdnF370E44ucGCnHSl8Xks38xCqqkL930Pg+S219dnrec= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:content-type:mime-version; s= default; bh=yRRYQAYkNNeb5pv7+CyvEXzhrdQ=; b=gEuo/52UwEHi0dwYCist 0+gq5niYun3kpPu5p5CuuN8pKL3xhp7xwA1pbSAit2xEYHrA3/lnmmHIWpK4+QfP LicwBsmBrLa0SXRf9nS3bCJbAsQeRIJg4OjXRixLPllfo4zJ2FZlQ7By8U1oLhph 4OLjlyS/C7HuDoBxoCX93jU= Received: (qmail 49625 invoked by alias); 14 Feb 2017 10:05:48 -0000 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 Received: (qmail 49528 invoked by uid 89); 14 Feb 2017 10:05:47 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=1, 42, J1, j1, UD:tree-ssa-loop-niter.c X-HELO: EUR01-VE1-obe.outbound.protection.outlook.com Received: from mail-ve1eur01on0065.outbound.protection.outlook.com (HELO EUR01-VE1-obe.outbound.protection.outlook.com) (104.47.1.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 14 Feb 2017 10:05:37 +0000 Received: from VI1PR0802MB2176.eurprd08.prod.outlook.com (10.172.12.21) by VI1PR0802MB2176.eurprd08.prod.outlook.com (10.172.12.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.888.16; Tue, 14 Feb 2017 10:05:33 +0000 Received: from VI1PR0802MB2176.eurprd08.prod.outlook.com ([10.172.12.21]) by VI1PR0802MB2176.eurprd08.prod.outlook.com ([10.172.12.21]) with mapi id 15.01.0888.030; Tue, 14 Feb 2017 10:05:33 +0000 From: Bin Cheng To: "gcc-patches@gcc.gnu.org" CC: nd Subject: [PATCH PR71437/V2]Simplify cond with assertions in threading Date: Tue, 14 Feb 2017 10:05:32 +0000 Message-ID: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Bin.Cheng@arm.com; x-ms-office365-filtering-correlation-id: 76bd005b-d9e3-42c9-1ae0-08d454c103c8 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081); SRVR:VI1PR0802MB2176; x-microsoft-exchange-diagnostics: 1; VI1PR0802MB2176; 7:i70JFdWjsuoKJrSFCD0/Nsdhvjj2/iSHTJML/HT3FyI0G+iGCbuzk7KuADihyXfOoaDKKvhgdO3DsHumTmuU6SqKYrUas1rp7lRdB3Fnl3OB7gZlxO2e3UiD9uOGIzG9zKLSAh2dZgD6foU8nsj0gzvOb+sJ87SLCFEh0ve9zkyvOGZRm+UmM6eFRmPrKb71/3A7Cx0EVIuWUJSdGqySw2M0KDFYAu/FW1dHAYBJfOK6ayfPtZ4ReayVwwfYz0fcCbZal4qZw3PFKaMbquAFoqg72X2dqYu6tidxHCzdzzTkFGvqngMcV5hwzr7LyLfA1NkgKEL9PPUtvczP+RBiupLctZ528kbz8Ypd7wSN3S/8cTCbs5RGCD/G8Q6dWCpF5s8XUqjeDQEfOT1BmKOzHy3yHvwE81Z1yRw1GhH4U/tO2rcFAwrDJBQOCa7YV/cSb1PQKYzR20h/JNy8u1opkYtmI1OFVnccz6kAboMAESgStUg/1GQWEdGJumP8a3StNn6bg22Br+Il3MqcK0a6xw== nodisclaimer: True x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(180628864354917); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(102415395)(6040375)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6055026)(6041248)(20161123558025)(20161123564025)(20161123562025)(20161123560025)(20161123555025)(6072148); SRVR:VI1PR0802MB2176; BCL:0; PCL:0; RULEID:; SRVR:VI1PR0802MB2176; x-forefront-prvs: 0218A015FA x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(7916002)(39450400003)(39860400002)(39840400002)(39410400002)(39850400002)(189002)(54534003)(199003)(377424004)(53936002)(97736004)(189998001)(6436002)(92566002)(2501003)(38730400002)(110136004)(25786008)(5640700003)(81003)(6506006)(66066001)(99286003)(55016002)(7696004)(101416001)(77096006)(99936001)(9686003)(6916009)(50986999)(54356999)(81166006)(5660300001)(68736007)(8676002)(33656002)(3280700002)(3846002)(81156014)(86362001)(3660700001)(2900100001)(2906002)(6116002)(8936002)(102836003)(4326007)(305945005)(7736002)(74316002)(122556002)(450100001)(105586002)(106356001)(106116001)(2351001); DIR:OUT; SFP:1101; SCL:1; SRVR:VI1PR0802MB2176; H:VI1PR0802MB2176.eurprd08.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; received-spf: None (protection.outlook.com: arm.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Feb 2017 10:05:32.7773 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0802MB2176 X-IsSubscribed: yes Hi, This is the second try fixing PR71437. The old version patch tried to fix issue in VRP but it requires further non-trivial change in VRP, specifically, to better support variable value ranges. This is not appropriate at stage 4. Alternatively, this patch tries to fix issue by improving threading. It additionally simplifies condition by using assertion conditions. Bootstrap and test on x86_64 and AArch64. Is it OK? Thanks, bin 2017-02-13 Bin Cheng PR tree-optimization/71437 * tree-ssa-loop-niter.c (tree_simplify_using_condition): Only expand condition if new parameter says so. Also change it to global. * tree-ssa-loop-niter.h (tree_simplify_using_condition): New declaration. * tree-ssa-threadedge.c (tree-ssa-loop-niter.h): New include file. (simplify_control_stmt_condition_1): Simplify condition using assert conditions. gcc/testsuite/ChangeLog 2017-02-13 Bin Cheng PR tree-optimization/71437 * gcc.dg/tree-ssa/pr71437.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71437.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71437.c new file mode 100644 index 0000000..66a5405 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71437.c @@ -0,0 +1,42 @@ +/* { dg-do compile } */ +/* { dg-options "-ffast-math -O3 -fdump-tree-vrp1-details" } */ + +int I = 50, J = 50; +int S, L; +const int *pL; +const int *pS; + +void bar (float, float); + +void foo (int K) +{ + int k, i, j; + static float LD, SD; + for (k = 0 ; k < K; k++) + { + for( i = 0 ; i < ( I - 1 ) ; i++ ) + { + if( ( L < pL[i+1] ) && ( L >= pL[i] ) ) + break ; + } + + if( i == ( I - 1 ) ) + L = pL[i] ; + LD = (float)( L - pL[i] ) / + (float)( pL[i + 1] - pL[i] ) ; + + for( j = 0 ; j < ( J-1 ) ; j++ ) + { + if( ( S < pS[j+1] ) && ( S >= pS[j] ) ) + break ; + } + + if( j == ( J - 1 ) ) + S = pS[j] ; + SD = (float)( S - pS[j] ) / + (float)( pS[j + 1] - pS[j] ) ; + + bar (LD, SD); + } +} +/* { dg-final { scan-tree-dump-times "Threaded jump " 2 "vrp1" } } */ diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c index efcf3ed..52baad1 100644 --- a/gcc/tree-ssa-loop-niter.c +++ b/gcc/tree-ssa-loop-niter.c @@ -2057,12 +2057,14 @@ tree_simplify_using_condition_1 (tree cond, tree expr) Wrapper around tree_simplify_using_condition_1 that ensures that chains of simple operations in definitions of ssa names in COND are expanded, so that things like casts or incrementing the value of the bound before - the loop do not cause us to fail. */ + the loop do not cause us to fail. COND is expanded before simplifying + if EXPAND is true. */ -static tree -tree_simplify_using_condition (tree cond, tree expr) +tree +tree_simplify_using_condition (tree cond, tree expr, bool expand) { - cond = expand_simple_operations (cond); + if (expand) + cond = expand_simple_operations (cond); return tree_simplify_using_condition_1 (cond, expr); } diff --git a/gcc/tree-ssa-loop-niter.h b/gcc/tree-ssa-loop-niter.h index b009857..4e572df 100644 --- a/gcc/tree-ssa-loop-niter.h +++ b/gcc/tree-ssa-loop-niter.h @@ -21,6 +21,7 @@ along with GCC; see the file COPYING3. If not see #define GCC_TREE_SSA_LOOP_NITER_H extern tree expand_simple_operations (tree, tree = NULL); +extern tree tree_simplify_using_condition (tree, tree, bool = true); extern tree simplify_using_initial_conditions (struct loop *, tree); extern bool loop_only_exit_p (const struct loop *, const_edge); extern bool number_of_iterations_exit (struct loop *, edge, diff --git a/gcc/tree-ssa-threadedge.c b/gcc/tree-ssa-threadedge.c index 4949bfa..fa2891d 100644 --- a/gcc/tree-ssa-threadedge.c +++ b/gcc/tree-ssa-threadedge.c @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "cfgloop.h" #include "gimple-iterator.h" #include "tree-cfg.h" +#include "tree-ssa-loop-niter.h" #include "tree-ssa-threadupdate.h" #include "params.h" #include "tree-ssa-scopedtables.h" @@ -561,6 +562,46 @@ simplify_control_stmt_condition_1 (edge e, if (limit == 0) return NULL_TREE; + /* Simplify condition using assertion conditions. */ + if (handle_dominating_asserts + && TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME) + { + tree assert_op0 = op0, assert_op1 = op1; + tree assert_cond0 = NULL_TREE, assert_cond1 = NULL_TREE; + gimple *def0 = SSA_NAME_DEF_STMT (op0), *def1 = SSA_NAME_DEF_STMT (op1); + + if (is_gimple_assign (def0) + && TREE_CODE (gimple_assign_rhs1 (def0)) == ASSERT_EXPR) + { + assert_op0 = TREE_OPERAND (gimple_assign_rhs1 (def0), 0); + assert_cond0 = TREE_OPERAND (gimple_assign_rhs1 (def0), 1); + } + if (is_gimple_assign (def1) + && TREE_CODE (gimple_assign_rhs1 (def1)) == ASSERT_EXPR) + { + assert_op1 = TREE_OPERAND (gimple_assign_rhs1 (def1), 0); + assert_cond1 = TREE_OPERAND (gimple_assign_rhs1 (def1), 1); + } + + if (assert_cond0 || assert_cond1) + { + tree res = fold_build2 (cond_code, boolean_type_node, + assert_op0, assert_op1); + if (assert_cond0) + { + res = tree_simplify_using_condition (assert_cond0, res, false); + if (is_gimple_min_invariant (res)) + return res; + } + if (assert_cond1) + { + res = tree_simplify_using_condition (assert_cond1, res, false); + if (is_gimple_min_invariant (res)) + return res; + } + } + } + /* We may need to canonicalize the comparison. For example, op0 might be a constant while op1 is an SSA_NAME. Failure to canonicalize will cause us to