From patchwork Fri Jan 13 03:37:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 714770 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 3v07cy4XDDz9t1F for ; Fri, 13 Jan 2017 14:38:10 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="nV3LtOjZ"; 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:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=IDNP/PXmBEUJ9xSJxLfPIk8/y5/qgEMxiwHttW4czdXhENVvfu JD0KLo/iKBpFkuKay01+cfZ/f40L7cYqEWGZRU1T5YJbwKFwyxK4v/VKRSK19a1N PjjvRDQIS8XxwRWfYiL+DeXU42pIIIzt58kiSEOpkxDdCCxNUHliLLjQs= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=1wMc1Xzmjm8sqrPW/lDJfuR/Kcs=; b=nV3LtOjZtF5S+48rNqt8 Nz6BWBS5h2uPJ0CCHTFV5WXabd7qoXz8vv8sfUlWLoQFM1vW7HIB4avhM+Wwpxqe Y4cgY7jgpdKZkXEBOkxaBgVVzFa/P4UNPKHE2XmSEuzbLLAixV8y4Taxh5jaLrym SrKaNv6dTbvyIFFrK8egfGs= Received: (qmail 22121 invoked by alias); 13 Jan 2017 03:37:53 -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 22049 invoked by uid 89); 13 Jan 2017 03:37:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-5.1 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=his X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 13 Jan 2017 03:37:50 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C608B67BA9 for ; Fri, 13 Jan 2017 03:37:50 +0000 (UTC) Received: from localhost.localdomain (ovpn-119-37.rdu2.redhat.com [10.10.119.37]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v0D3bnq0024546 for ; Thu, 12 Jan 2017 22:37:50 -0500 To: gcc-patches From: Jeff Law Subject: Re: [RFA][PATCH 3/4] Trim mem* calls in DSE Message-ID: <9a3e14ec-fb6d-c1b9-7269-70d3d26b532d@redhat.com> Date: Thu, 12 Jan 2017 20:37:48 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 X-IsSubscribed: yes Richi indicated he wants this to be included in gcc-7. So I've updated the mem* trimming patch with his comments. The unnecessary SSA updates are gone. Use wide_int_to_tree rather than building/folding expressions we know will result in constants. Simplify incrementing the start address by re-folding the MEM_REF. Again, these were almost verbatim drop-ins, so I'm going to assume the "looks ok" to be approval once prereqs are approved. This has been bootstrapped and regression tested on top of patches 1a, 1b, 2 and with patch #4 as well (patch #4 is not on the table for gcc-7 and isn't being reposted at this point). Jeff * tree-ssa-dse.c (decrement_count): New function. (increment_start_addr, maybe_trim_memstar_call): Likewise. (dse_dom_walker::optimize_stmt): Call maybe_trim_memstar_call directly when we know the partially dead statement is a mem* function. * gcc.dg/tree-ssa/ssa-dse-25.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-25.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-25.c new file mode 100644 index 0000000..8b7db3a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-25.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dse1-details -w" } */ + +char z[32]; + + +int +foo(void) +{ + memset (z, 0, 16); + memset (z+8, 0, 24); +} + +/* { dg-final { scan-tree-dump "memset .&z, 0, 8." "dse1" } } */ + + diff --git a/gcc/tree-ssa-dse.c b/gcc/tree-ssa-dse.c index 83ce29b..20cf3b4 100644 --- a/gcc/tree-ssa-dse.c +++ b/gcc/tree-ssa-dse.c @@ -332,6 +332,99 @@ maybe_trim_constructor_store (ao_ref *ref, sbitmap live, gimple *stmt) } } +/* STMT is a memcpy, memmove or memset. Decrement the number of bytes + copied/set by DECREMENT. */ +static void +decrement_count (gimple *stmt, int decrement) +{ + tree *countp = gimple_call_arg_ptr (stmt, 2); + gcc_assert (TREE_CODE (*countp) == INTEGER_CST); + *countp = wide_int_to_tree (TREE_TYPE (*countp), (TREE_INT_CST_LOW (*countp) + - decrement)); + +} + +static void +increment_start_addr (gimple *stmt, tree *where, int increment) +{ + if (TREE_CODE (*where) == SSA_NAME) + { + tree tem = make_ssa_name (TREE_TYPE (*where)); + gassign *newop + = gimple_build_assign (tem, POINTER_PLUS_EXPR, *where, + build_int_cst (sizetype, increment)); + gimple_stmt_iterator gsi = gsi_for_stmt (stmt); + gsi_insert_before (&gsi, newop, GSI_SAME_STMT); + *where = tem; + update_stmt (gsi_stmt (gsi)); + return; + } + + *where = build_fold_addr_expr (fold_build2 (MEM_REF, char_type_node, + *where, + build_int_cst (ptr_type_node, + increment))); +} + +/* STMT is builtin call that writes bytes in bitmap ORIG, some bytes are dead + (ORIG & ~NEW) and need not be stored. Try to rewrite STMT to reduce + the amount of data it actually writes. + + Right now we only support trimming from the head or the tail of the + memory region. In theory we could split the mem* call, but it's + likely of marginal value. */ + +static void +maybe_trim_memstar_call (ao_ref *ref, sbitmap live, gimple *stmt) +{ + switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))) + { + case BUILT_IN_MEMCPY: + case BUILT_IN_MEMMOVE: + { + int head_trim, tail_trim; + compute_trims (ref, live, &head_trim, &tail_trim); + + /* Tail trimming is easy, we can just reduce the count. */ + if (tail_trim) + decrement_count (stmt, tail_trim); + + /* Head trimming requires adjusting all the arguments. */ + if (head_trim) + { + tree *dst = gimple_call_arg_ptr (stmt, 0); + increment_start_addr (stmt, dst, head_trim); + tree *src = gimple_call_arg_ptr (stmt, 1); + increment_start_addr (stmt, src, head_trim); + decrement_count (stmt, head_trim); + } + break; + } + + case BUILT_IN_MEMSET: + { + int head_trim, tail_trim; + compute_trims (ref, live, &head_trim, &tail_trim); + + /* Tail trimming is easy, we can just reduce the count. */ + if (tail_trim) + decrement_count (stmt, tail_trim); + + /* Head trimming requires adjusting all the arguments. */ + if (head_trim) + { + tree *dst = gimple_call_arg_ptr (stmt, 0); + increment_start_addr (stmt, dst, head_trim); + decrement_count (stmt, head_trim); + } + break; + } + + default: + break; + } +} + /* STMT is a memory write where one or more bytes written are dead stores. ORIG is the bitmap of bytes stored by STMT. LIVE is the bitmap of stores that are actually live. @@ -619,7 +712,7 @@ dse_dom_walker::dse_optimize_stmt (gimple_stmt_iterator *gsi) if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD) { - maybe_trim_partially_dead_store (&ref, m_live_bytes, stmt); + maybe_trim_memstar_call (&ref, m_live_bytes, stmt); return; }