From patchwork Tue Dec 6 23:18:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 703374 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 3tYHd3015Dz9t2D for ; Wed, 7 Dec 2016 10:18:58 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="kzXL6LvD"; 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=wkaROhoFJtyjIol1HlLGimS/RetyFwrqhS8htrqjHJjsn9PA4P t3FdlBIU4oPODXPIhYgOPeYdvp0Uu38G/85r0m+JZbfWO1ayd41ShLj/1efNTiNV /gKq2XJjji01IL3rB/Cp0geFQnZ+74y1IXbVGNt3xCUiLnM2zlu2L4Rqo= 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=owcdX3KnU5yZVZ6hQ/XbMM8qFMk=; b=kzXL6LvDdGTtrZb61p4L SrmKbj4mXcsChHkOqZ44I7CJjBmUxZ+q71+sZFVb2Vmqyt9PmhZ4hKJQeZZRv5Ru 9h0YXCZJeW9RmG0D1QE1cTaE730kYMyUa0Kg/LOAS/fNJXNLWLp6Hu/vMXuj4lKN uCs7HjxiOl6d6P07VuW2I/8= Received: (qmail 55590 invoked by alias); 6 Dec 2016 23:18:50 -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 55569 invoked by uid 89); 6 Dec 2016 23:18:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.9 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS, URIBL_RED autolearn=ham version=3.3.2 spammy=singleton, DSE, dse, max_size1 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; Tue, 06 Dec 2016 23:18:39 +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 01E2C64D93 for ; Tue, 6 Dec 2016 23:18:38 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-104.phx2.redhat.com [10.3.116.104]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uB6NIbhR006723 for ; Tue, 6 Dec 2016 18:18:37 -0500 To: gcc-patches From: Jeff Law Subject: [PR tree-optimization/67955] Exploit PTA in DSE Message-ID: <19387340-c3cf-118a-2041-2f466d96a91d@redhat.com> Date: Tue, 6 Dec 2016 16:18:37 -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 So I was going through the various DSE related bugs as stumbled across 67955. The basic issue in 67955 is that DSE is too simplistic when trying to determine if two writes hit the same memory address. There are cases were PTA analysis can get us that information. The unfortunate problem is that PTA can generate a single points-to set for pointers to different elements in the same array. So we can only exploit a special case. Namely when we get a PTA singleton and the size of the store is the same size of the pointed-to object. That doesn't happen in a bootstrap, but it does happen for the testcase in the BZ as well as a handful of tests in the testsuite -- Tom reported 6 unique tests where it triggered, I ran my own tests where two more were spit out. Not huge. But the cost is low. All that I've done with Tom's patch is update the call into the PTA system. It's very clearly Tom's work. Bootstrapped and regression tested on x86_64-linux-gnu. Installing on the trunk. jeff commit cfc96cf6c8ea2c6e638123a93663964f6d78fb10 Author: law Date: Tue Dec 6 23:18:17 2016 +0000 PR tree-optimization/67955 * tree-ssa-alias.c (same_addr_size_stores_p): New function. (stmt_kills_ref_p): Use it. PR tree-optimization/67955 * gcc.dg/tree-ssa/dse-points-to.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@243325 138bc75d-0d04-0410-961f-82ee72b054a4 diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 61eeea3..797b711 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2016-12-06 Tom de Vries + + PR tree-optimization/67955 + * tree-ssa-alias.c (same_addr_size_stores_p): New function. + (stmt_kills_ref_p): Use it. + 2016-12-06 Eric Botcazou PR middle-end/78700 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5adcdd2..6090a96 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-12-06 Tom de Vries + + PR tree-optimization/67955 + * gcc.dg/tree-ssa/dse-points-to.c: New test. + 2016-12-06 Michael Meissner PR target/78658 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/dse-points-to.c b/gcc/testsuite/gcc.dg/tree-ssa/dse-points-to.c new file mode 100644 index 0000000..8003556 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/dse-points-to.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-tree-ccp -fno-tree-forwprop -fno-tree-fre -fno-tree-vrp" } */ +/* { dg-additional-options "-fdump-tree-dse1-details" } */ + +int +f () +{ + int a; + int *p = &a; + *p = 1; + a = 2; + return a; +} + +/* { dg-final { scan-tree-dump-times "Deleted dead store.*p_1" 1 "dse1"} } */ diff --git a/gcc/tree-ssa-alias.c b/gcc/tree-ssa-alias.c index 10f1677..37b581d 100644 --- a/gcc/tree-ssa-alias.c +++ b/gcc/tree-ssa-alias.c @@ -2316,6 +2316,78 @@ stmt_may_clobber_ref_p (gimple *stmt, tree ref) return stmt_may_clobber_ref_p_1 (stmt, &r); } +/* Return true if store1 and store2 described by corresponding tuples + have the same size and store to the same + address. */ + +static bool +same_addr_size_stores_p (tree base1, HOST_WIDE_INT offset1, HOST_WIDE_INT size1, + HOST_WIDE_INT max_size1, + tree base2, HOST_WIDE_INT offset2, HOST_WIDE_INT size2, + HOST_WIDE_INT max_size2) +{ + /* For now, just handle VAR_DECL. */ + bool base1_obj_p = VAR_P (base1); + bool base2_obj_p = VAR_P (base2); + + /* We need one object. */ + if (base1_obj_p == base2_obj_p) + return false; + tree obj = base1_obj_p ? base1 : base2; + + /* And we need one MEM_REF. */ + bool base1_memref_p = TREE_CODE (base1) == MEM_REF; + bool base2_memref_p = TREE_CODE (base2) == MEM_REF; + if (base1_memref_p == base2_memref_p) + return false; + tree memref = base1_memref_p ? base1 : base2; + + /* Sizes need to be valid. */ + if (max_size1 == -1 || max_size2 == -1 + || size1 == -1 || size2 == -1) + return false; + + /* Max_size needs to match size. */ + if (max_size1 != size1 + || max_size2 != size2) + return false; + + /* Sizes need to match. */ + if (size1 != size2) + return false; + + /* Offsets need to be 0. */ + if (offset1 != 0 + || offset2 != 0) + return false; + + /* Check that memref is a store to pointer with singleton points-to info. */ + if (!tree_int_cst_equal (TREE_OPERAND (memref, 1), integer_zero_node)) + return false; + tree ptr = TREE_OPERAND (memref, 0); + if (TREE_CODE (ptr) != SSA_NAME) + return false; + struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr); + unsigned int pt_uid; + if (pi == NULL + || !pt_solution_singleton_or_null_p (&pi->pt, &pt_uid)) + return false; + + /* Check that ptr points relative to obj. */ + unsigned int obj_uid = (DECL_PT_UID_SET_P (obj) + ? DECL_PT_UID (obj) + : DECL_UID (obj)); + if (obj_uid != pt_uid) + return false; + + /* Check that the object size is the same as the store size. That ensures us + that ptr points to the start of obj. */ + if (!tree_fits_shwi_p (DECL_SIZE (obj))) + return false; + HOST_WIDE_INT obj_size = tree_to_shwi (DECL_SIZE (obj)); + return obj_size == size1; +} + /* If STMT kills the memory reference REF return true, otherwise return false. */ @@ -2393,6 +2465,11 @@ stmt_kills_ref_p (gimple *stmt, ao_ref *ref) so base == ref->base does not always hold. */ if (base != ref->base) { + /* Try using points-to info. */ + if (same_addr_size_stores_p (base, offset, size, max_size, ref->base, + ref->offset, ref->size, ref->max_size)) + return true; + /* If both base and ref->base are MEM_REFs, only compare the first operand, and if the second operand isn't equal constant, try to add the offsets into offset and ref_offset. */