From patchwork Wed Apr 12 11:43:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 749959 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 3w32Bf0rP4z9sNQ for ; Wed, 12 Apr 2017 21:44:07 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="bAhL/o2/"; 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:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=qRxxHVjmnHm5jljwULuEJmFSn9/IodcKbtrPfsDz0JrRdIyM+5 ces4tYG7EJJgjnskGXJ9qrODqw30YKkpPdPxwemSOKx7klfIP4AvvnMXjd9aszSo MmvwuM5uX3atADUR70nC7ifhG8RkOn3oMNzFp1Bpk0q3q2pJb6PiS+w14= 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:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=wfbHXOspbyh2fABOOtFDSbqTEN0=; b=bAhL/o2/87HcFEbC1SF+ tVvA3DBJt8N3bbqi58YNmV3eoU40HbWVZsxWIlQtkiPYbyvP53RXXK8iZkQd+os0 LbsAMTuzCDIZ3SywkEc2hVm+L51Aucn1i2kGKHWUDUpYhaxV1mbORjUPoUtFVeB7 YQhceUmQTMCDKwd2werd/GA= Received: (qmail 35300 invoked by alias); 12 Apr 2017 11:43:54 -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 35283 invoked by uid 89); 12 Apr 2017 11:43:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy=severely X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 12 Apr 2017 11:43:52 +0000 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 13CB0AAF2; Wed, 12 Apr 2017 11:43:52 +0000 (UTC) Date: Wed, 12 Apr 2017 13:43:51 +0200 From: Martin Jambor To: GCC Patches Cc: Alan Lawrence , Richard Biener Subject: [PR 80293] Don't totally-sRA char arrays Message-ID: <20170412114351.4nlvkanp3mxy465t@virgil.suse.cz> Mail-Followup-To: GCC Patches , Alan Lawrence , Richard Biener MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.6.2 (2016-07-01) X-IsSubscribed: yes Hi, the patch below is an attempt to deal with PR 80293 as non-invasively as possible. Basically, it switches off total SRA scalarization of any local aggregates which contains an array of elements that have one byte (or less). The logic behind this is that accessing such arrays element-wise usually results in poor code and that such char arrays are often used for non-statically-typed content anyway, and we do not want to copy that byte per byte. Alan, do you think this could impact your constant pool scalarization too severely? Richi, if you or Alan does not object in a few days, I'd like to commit this in time for gcc7. It has passed bootstrap and testing on x86_64-linux (but the constant pool SRA work was aimed primarily at ARM). Thanks, Martin 2017-04-10 Martin Jambor * tree-sra.c (scalarizable_type_p): Make char arrays not totally scalarizable. testsuite/ * g++.dg/tree-ssa/pr80293.C: New test. --- gcc/testsuite/g++.dg/tree-ssa/pr80293.C | 45 +++++++++++++++++++++++++++++++++ gcc/tree-sra.c | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr80293.C diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr80293.C b/gcc/testsuite/g++.dg/tree-ssa/pr80293.C new file mode 100644 index 00000000000..7faf35ae983 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr80293.C @@ -0,0 +1,45 @@ +// { dg-do compile } +// { dg-options "-O2 -std=gnu++11 -fdump-tree-optimized" } */ + +#include + +// Return a copy of the underlying memory of an arbitrary value. +template < + typename T, + typename = typename std::enable_if::value>::type +> +auto getMem( + T const & value +) -> std::array { + auto ret = std::array{}; + __builtin_memcpy(ret.data(), &value, sizeof(T)); + return ret; +} + +template < + typename T, + typename = typename std::enable_if::value>::type +> +auto fromMem( + std::array const & buf +) -> T { + auto ret = T{}; + __builtin_memcpy(&ret, buf.data(), sizeof(T)); + return ret; +} + +double foo1(std::uint64_t arg) { + return fromMem(getMem(arg)); +} + +double foo2(std::uint64_t arg) { + return *reinterpret_cast(&arg); +} + +double foo3(std::uint64_t arg) { + double ret; + __builtin_memcpy(&ret, &arg, sizeof(arg)); + return ret; +} + +// { dg-final { scan-tree-dump-not "BIT_FIELD_REF" "optimized" } } diff --git a/gcc/tree-sra.c b/gcc/tree-sra.c index 02453d3ed9a..cbe9e862a2f 100644 --- a/gcc/tree-sra.c +++ b/gcc/tree-sra.c @@ -981,7 +981,7 @@ scalarizable_type_p (tree type) if (TYPE_DOMAIN (type) == NULL_TREE || !tree_fits_shwi_p (TYPE_SIZE (type)) || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (type))) - || (tree_to_shwi (TYPE_SIZE (TREE_TYPE (type))) <= 0) + || (tree_to_shwi (TYPE_SIZE (TREE_TYPE (type))) <= BITS_PER_UNIT) || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type)))) return false; if (tree_to_shwi (TYPE_SIZE (type)) == 0