From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 16261385B526 for ; Fri, 5 May 2023 09:55:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 16261385B526 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683280552; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=CxClpXh2IyweKd8pEaw022DNw4OEiOVvJq7QtLitNUI=; b=JV/GS9ArynX4u5PkGFYkNhPoS81KScXDU+cYwpE9EiwnpN1gmU61o84RQXIMussqMymfXP lyCXT5ljvCq6yvVl36Boojxvm7v5ZwzmPmRXE4dILZ/phIzYDu9DqFczEKoQ1xVyD+wlCw bEOns8xmJ3H3YQ0kL31vZt8KlFtLMOc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-108-h3m8mkxTPtK_fmdbtHsOAA-1; Fri, 05 May 2023 05:55:47 -0400 X-MC-Unique: h3m8mkxTPtK_fmdbtHsOAA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1B747382888C; Fri, 5 May 2023 09:55:47 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.156]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BBC6F40C6F41; Fri, 5 May 2023 09:55:46 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3459thEv1361131 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 5 May 2023 11:55:44 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3459tgkt1361130; Fri, 5 May 2023 11:55:42 +0200 Date: Fri, 5 May 2023 11:55:41 +0200 From: Jakub Jelinek To: Richard Biener , "Joseph S. Myers" , Jason Merrill , Eric Botcazou , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] tree: Fix up save_expr [PR52339] Message-ID: Reply-To: Jakub Jelinek References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, May 05, 2023 at 11:04:09AM +0200, Jakub Jelinek via Gcc-patches wrote: > As mentioned in the PR, save_expr seems to be very optimistic when > some expression is invariant, which can result in various wrong-code > issues. > The problem is with the TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t) > case in tree_invariant_p_1. TREE_READONLY (t) in that case says > that the object shouldn't be modified during its lifetime and > !TREE_SIDE_EFFECTS (t) that it can be evaluated safely multiple times, > but that doesn't mean we can avoid wrapping the expression into SAVE_EXPR > say for a TREE_READONLY COMPONENT_REF with INDIRECT_REF as first operand > - either the lifetime of the TREE_READONLY object could end earlier than > when we need to reevaluate the object (that happens in the > pr52339-1.c case where save_expr is called on p->a and then free (p) is > done or pr52339.C where delete a->b when calling ~B () dtor deallocates a), > or e.g. the pointer could change as in pr52339-2.c (so evaluating p->a again > after ++p yields a possibly different value than originally and again we need > a SAVE_EXPR). > > Attached are two patches which fix this, unfortunately both regress > FAIL: gnat.dg/loop_optimization21.adb scan-tree-dump-times optimized "Index_Check" 1 > FAIL: gnat.dg/vect1.adb scan-tree-dump-times vect "vectorized 1 loops" 15 > FAIL: gnat.dg/vect2.adb scan-tree-dump-times vect "vectorized 1 loops" 15 > FAIL: gnat.dg/vect3.adb scan-tree-dump-times vect "vectorized 1 loops" 15 > FAIL: gnat.dg/vect4.adb scan-tree-dump-times vect "vectorized 1 loops" 15 > FAIL: gnat.dg/vect5.adb scan-tree-dump-times vect "vectorized 1 loops" 15 > FAIL: gnat.dg/vect6.adb scan-tree-dump-times vect "vectorized 1 loops" 15 > on x86_64-linux (the first scan triggers 2 times rather than once, > the next 3 13 times rather than 15 and the last 3 14 times rather than 15 > times). > The first patch has been otherwise successfully bootstrapped/regtested on > x86_64-linux and i686-linux (with that above regressions), the second one > is probably better but has been so far tested just on the new testcases and > verified to also cause the above Ada regressions. Looking at the Ada cases (I admit I don't really understand why it isn't vectorized, the IL is so different from the start because of the extra SAVE_EXPRs that it is very hard to diff stuff), the case where save_expr used to return the argument and no longer does are those r.P_BOUNDS->LB0 etc. cases. Now, I wondered if (pre-gimplification) we couldn't make an exception and allow the base to be INDIRECT_REF or of a REFERENCE_TYPE with the idea that references are really imutable and can't be changed during its lifetime (after gimplification whether something is REFERENCE_TYPE or POINTER_TYPE is lost), but that isn't what Ada is using. So, another possibility would be to allow bases of TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t) which are INDIRECT_REFs of tree_invariant_p_1 addresses. That doesn't work either, in the r.P_BOUNDS->LB0 case P_BOUNDS is a FIELD_DECL with POINTER_TYPE, LB0 is TREE_READONLY FIELD_DECL and that COMPONENT_REF is also TREE_READONLY, r is TREE_READONLY PARM_DECL, but unforuntately the r.P_BOUNDS COMPONENT_REF isn't marked TREE_READONLY. Thus, shall we treat as tree_invariant_p_1 also handled components which are !TREE_SIDE_EFFECTS (t), but not TREE_READONLY and only their base is TREE_READONLY? Or do that only during the recursion? Jakub