From patchwork Wed Aug 14 13:09:22 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 267117 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 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id EC1C42C00FA for ; Wed, 14 Aug 2013 23:09:36 +1000 (EST) 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:reply-to:mime-version :content-type; q=dns; s=default; b=iqXagqbBTBoza30s3/8pKiUomVkMh Ewnm2SBVdxVYOSbkWJf3VT/9Sd5AFdfKmEYGGwVJB77zKq+tBf+n7TpgGc8yt2VD oS73vXY2gf+QOXUq6wHuDWiH960EOA4OMnuSRt4/74NNh/VKmQ4ooaJ2nylRmMls ISoefZESFY8DIc= 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:reply-to:mime-version :content-type; s=default; bh=oqvxEbd9UihNZy5te7/nAk8zz5k=; b=mwR EfWEbYhBct0UzYJhn4I5oKl9OjK9pVtl2oaLGlGvc8khuTLxROGD8fuvcb9UYdXW 0FgNNG/CTx/PV65J/BLFCJlQFXtkiwh7OFuKbpP9vtBXFdZgTwVOk6RD7FLTwTWZ CjvbfCmbi0ctuP0B8FxYSqz+5FLF99tV+VeGWVUk= Received: (qmail 21326 invoked by alias); 14 Aug 2013 13:09:29 -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 21316 invoked by uid 89); 14 Aug 2013 13:09:28 -0000 X-Spam-SWARE-Status: No, score=-7.6 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Wed, 14 Aug 2013 13:09:27 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r7ED9QGF030780 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 14 Aug 2013 09:09:26 -0400 Received: from zalov.cz (vpn1-7-79.ams2.redhat.com [10.36.7.79]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r7ED9OJp012004 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 14 Aug 2013 09:09:26 -0400 Received: from zalov.cz (localhost [127.0.0.1]) by zalov.cz (8.14.5/8.14.5) with ESMTP id r7ED9NFQ031820; Wed, 14 Aug 2013 15:09:23 +0200 Received: (from jakub@localhost) by zalov.cz (8.14.5/8.14.5/Submit) id r7ED9NuE031819; Wed, 14 Aug 2013 15:09:23 +0200 Date: Wed, 14 Aug 2013 15:09:22 +0200 From: Jakub Jelinek To: Richard Biener , Martin Jambor Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix SRA with volatile loads/stores (PR tree-optimization/58145) Message-ID: <20130814130922.GO1814@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Hi! On the following testcases we miscompile the code (on -1.c just drop = {v} from the statements, on -2.c lim moves the volatile stores after the loop), because SRA drops the volatileness from the MEM_REF. SRA generally ignores volatile vars and fields etc., but if we have a structure assignment to volatile from non-volatile or vice versa, if SRA decides to scalarize rhs resp. lhs, new MEM_REFs are created even for the volatile access with different type. The following patch fixes that by propagating TREE_THIS_VOLATILE and TREE_SIDE_EFFECTS from the prev_base to the newly created MEM_REF. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/4.8? On IRC with Martin we've also discussed slsr in this regard, but that seems to be fine, it uses the original volatile type if it was volatile and propagates TREE_THIS_VOLATILE/TREE_SIDE_EFFECTS flags to the newly created MEM_REF. 2013-08-14 Jakub Jelinek PR tree-optimization/58145 * tree-sra.c (build_ref_for_offset): If base is TREE_THIS_VOLATILE, propagate it to exp_type and MEM_REF. * gcc.dg/pr58145-1.c: New test. * gcc.dg/pr58145-2.c: New test. Jakub --- gcc/tree-sra.c.jj 2013-08-14 11:02:55.290711106 +0200 +++ gcc/tree-sra.c 2013-08-14 12:38:47.405230042 +0200 @@ -1466,6 +1466,7 @@ build_ref_for_offset (location_t loc, tr { tree prev_base = base; tree off; + tree mem_ref; HOST_WIDE_INT base_offset; unsigned HOST_WIDE_INT misalign; unsigned int align; @@ -1515,8 +1516,17 @@ build_ref_for_offset (location_t loc, tr align = (misalign & -misalign); if (align < TYPE_ALIGN (exp_type)) exp_type = build_aligned_type (exp_type, align); - - return fold_build2_loc (loc, MEM_REF, exp_type, base, off); + if (TREE_THIS_VOLATILE (TREE_TYPE (prev_base)) + && !TREE_THIS_VOLATILE (exp_type)) + exp_type = build_qualified_type (exp_type, TYPE_QUALS (exp_type) + | TYPE_QUAL_VOLATILE); + + mem_ref = fold_build2_loc (loc, MEM_REF, exp_type, base, off); + if (TREE_THIS_VOLATILE (exp_type) || TREE_THIS_VOLATILE (prev_base)) + TREE_THIS_VOLATILE (mem_ref) = 1; + if (TREE_SIDE_EFFECTS (prev_base)) + TREE_SIDE_EFFECTS (mem_ref) = 1; + return mem_ref; } /* Construct a memory reference to a part of an aggregate BASE at the given --- gcc/testsuite/gcc.dg/pr58145-1.c.jj 2013-08-14 12:02:07.077086488 +0200 +++ gcc/testsuite/gcc.dg/pr58145-1.c 2013-08-14 12:03:15.895198976 +0200 @@ -0,0 +1,37 @@ +/* PR tree-optimization/58145 */ +/* { dg-do compile { target { int32plus } } } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +struct S { unsigned int data : 32; }; +struct T { unsigned int data; }; +volatile struct S s2; + +void +f1 (int val) +{ + struct S s = { .data = val }; + *(volatile struct S *) 0x880000UL = s; +} + +void +f2 (int val) +{ + struct T t = { .data = val }; + *(volatile struct T *) 0x880000UL = t; +} + +void +f3 (int val) +{ + *(volatile unsigned int *) 0x880000UL = val; +} + +void +f4 (int val) +{ + struct S s = { .data = val }; + s2 = s; +} + +/* { dg-final { scan-tree-dump-times " ={v} " 4 "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */ --- gcc/testsuite/gcc.dg/pr58145-2.c.jj 2013-08-14 12:02:28.409663559 +0200 +++ gcc/testsuite/gcc.dg/pr58145-2.c 2013-08-14 12:04:19.471612107 +0200 @@ -0,0 +1,51 @@ +/* PR tree-optimization/58145 */ +/* { dg-do compile { target { int32plus } } } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +struct S { unsigned int data : 32; }; +struct T { unsigned int data; }; +volatile struct S s2; + +static inline void +f1 (int val) +{ + struct S s = { .data = val }; + *(volatile struct S *) 0x880000UL = s; +} + +static inline void +f2 (int val) +{ + struct T t = { .data = val }; + *(volatile struct T *) 0x880000UL = t; +} + +static inline void +f3 (int val) +{ + *(volatile unsigned int *) 0x880000UL = val; +} + +static inline void +f4 (int val) +{ + struct S s = { .data = val }; + s2 = s; +} + +void +f5 (void) +{ + int i; + for (i = 0; i < 100; i++) + f1 (0); + for (i = 0; i < 100; i++) + f2 (0); + for (i = 0; i < 100; i++) + f3 (0); + for (i = 0; i < 100; i++) + f4 (0); +} + +/* { dg-final { scan-tree-dump-times " ={v} " 4 "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */