From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id 9568D3858424 for ; Fri, 29 Jul 2022 09:56:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9568D3858424 Received: by mail-ed1-x532.google.com with SMTP id f15so5194372edc.4 for ; Fri, 29 Jul 2022 02:56:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=il2xU8O/2l6q3ieu3ns4W1g2hVJT7IRaMeCNdE4yQHo=; b=23PyT1k9H7nD4U8xBYRRgFkS62eIQUlouAQKXxzqzOudcnq9n5HAq2nnjkRxw/D7Mu +wjctOVw4mzOUMZuq6J/ZxGieWDDX/L+k587Hi6kof2hw9m2Q0dofZaXVjPEvf+excK2 U65k5ChqZAZfuyuNAS/AYClWexlUChMUmhJMyZsHnMEK0f/HbtmM+L6XYTjPZaIuk6Ez SoTgHwuQ5Yt1nrYVqWcfO2pr+HOOpf7OQxcQWCB61NviVaETpL2N9TCkKnC7lZg13Ojr 6gjug3pMuSmouVirMlfsnW+3phr5XRC7jDE749mocP/OqD8rHlYzEGg3877f/Etr58pc G/fA== X-Gm-Message-State: AJIora9nci66GAGw0TZNS1Qg9p8eIDNecfkqrwkS3gESDx0iUrLVEwyw 15r/NLRsY6H6j+izBULz2xIwovo6oNBRiqbzFmE= X-Google-Smtp-Source: AGRyM1v7tjqZcm00e1mUngQX5nUamRLdoEZ8Fju8p8zR4A8O8ablB4/xUlrGGfT5SmbzPqnfl3rl7rhlPCPThm/LTlw= X-Received: by 2002:a05:6402:1117:b0:43b:c965:549e with SMTP id u23-20020a056402111700b0043bc965549emr2816028edv.366.1659088614314; Fri, 29 Jul 2022 02:56:54 -0700 (PDT) MIME-Version: 1.0 References: <21a567be-4898-2e69-178f-a735be8c9742@foss.arm.com> In-Reply-To: <21a567be-4898-2e69-178f-a735be8c9742@foss.arm.com> From: Richard Biener Date: Fri, 29 Jul 2022 11:56:42 +0200 Message-ID: Subject: Re: cselib: add function to check if SET is redundant [PR106187] To: Richard Earnshaw Cc: Richard Earnshaw , "gcc-patches@gcc.gnu.org" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Jul 2022 09:56:57 -0000 On Fri, Jul 29, 2022 at 11:52 AM Richard Earnshaw wrote: > > > > On 29/07/2022 08:06, Richard Biener via Gcc-patches wrote: > > On Thu, Jul 28, 2022 at 6:46 PM Richard Earnshaw > > wrote: > >> > >> [resend with correct subject line] > >> > >> A SET operation that writes memory may have the same value as an earlier > >> store but if the alias sets of the new and earlier store do not conflict > >> then the set is not truly redundant. This can happen, for example, if > >> objects of different types share a stack slot. > >> > >> To fix this we define a new function in cselib that first checks for > >> equality and if that is successful then finds the earlier store in the > >> value history and checks the alias sets. > >> > >> The routine is used in two places elsewhere in the compiler. Firstly > >> in cfgcleanup and secondly in postreload. > > > > I can't comment on the stripping on SUBREGs and friends but it seems > > to be conservative apart from > > > > + if (!flag_strict_aliasing || !MEM_P (dest)) > > + return true; > > > > where if dest is not a MEM but were to contain one we'd miss it. > > Double-checking > > from more RTL literate people appreciated. > > There are very few things that can wrap a MEM in a SET_DEST. I'm pretty > sure that's all of them. It certainly matches the code in > cselib_invalidate_rtx which has to deal with this sort of case. > > > > > + /* Lookup the equivalents to the dest. This is more likely to succeed > > + than looking up the equivalents to the source (for example, when the > > + src is some form of constant). */ > > > > I think the comment is misleading - we _do_ have to lookup the MEM, > > looking up equivalences of a reg or an expression on the RHS isn't > > what we are interested in. > > OK, I'll try to reword it. > > > > > + return alias_sets_conflict_p (MEM_ALIAS_SET (dest), > > + MEM_ALIAS_SET (src_equiv)); > > > > that's not conservative enough - dse.cc has correct boilerplate, we have > > to check both MEM_ALIAS_SET and MEM_EXPR here (the latter only > > if the former load/store has a MEM_EXPR). Note in particular > > using alias_set_subset_of instead of alias_sets_conflict_p. > > > > /* We can only remove the later store if the earlier aliases > > at least all accesses the later one. */ > > && ((MEM_ALIAS_SET (mem) == MEM_ALIAS_SET (s_info->mem) > > || alias_set_subset_of (MEM_ALIAS_SET (mem), > > MEM_ALIAS_SET (s_info->mem))) > > && (!MEM_EXPR (s_info->mem) > > || refs_same_for_tbaa_p (MEM_EXPR (s_info->mem), > > MEM_EXPR (mem))))) > > > > OK, that's an easy enough change. > > > + /* We failed to find a recorded value in the cselib history, so try the > > + source of this set. */ > > + rtx src = SET_SRC (set); > > + while (GET_CODE (src) == SUBREG) > > + src = XEXP (src, 0); > > + > > + if (MEM_P (src) && rtx_equal_for_cselib_1 (dest_addr, XEXP (src, 0), > > + GET_MODE (dest), 0)) > > + return alias_sets_conflict_p (MEM_ALIAS_SET (dest), > > + MEM_ALIAS_SET (src)); > > > > this looks like an odd case to me - wouldn't that only catch things > > like self-assignments, aka *p = *p? So I'd simply drop this fallback. > > It catches the case of *p = *q when p and q have the same value. It did > come up in testing on x86 (when previously I was aborting to make sure > I'd caught everything). We could leave it out as the fallback case in > this instance is to record a conflict, but it's not a path that's likely > to be performance critical and the probability of this being a redundant > store is quite high. I'll update the comment to make this clearer. Ah OK - if it did actually catch cases then it's fine to keep. Note the alias check needs to be updated the same as above. Richard. > > > R. > > > > > Otherwise it looks OK to me. > > > > Thanks, > > Richard. > > > >> gcc/ChangeLog: > >> * cselib.h (cselib_redundant_set_p): Declare. > >> * cselib.cc: Include alias.h > >> (cselib_redundant_set_p): New function. > >> * cfgcleanup.cc: (mark_effect): Use cselib_redundant_set_p instead > >> of rtx_equal_for_cselib_p. > >> * postreload.c (reload_cse_simplify): Use cselib_redundant_set_p. > >> (reload_cse_noop_set_p): Delete.