From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 62E2A385773C for ; Wed, 26 Jul 2023 09:41:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 62E2A385773C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 7CF1C21CE4 for ; Wed, 26 Jul 2023 09:41:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1690364494; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=6HA3FwjHu2MfQ3Hbm4Vu4DdpxIaxj/SJGEdaNhuOOac=; b=VAtHuLIUAvvS604zjjf4BKItcJye0Pk5+Pg++fGxhLNjcxZk1OVbspks/2bjSuH2p692q6 hsbQ1PMpO0MR5iBOEvyYJEP500VfEbFUDC0V3OceLLJ0VGj5Rsep7hasVGjI1RDKf+IWfw O/0KmurAax+k70VIcHfHY8CbIi8Nz9k= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1690364494; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=6HA3FwjHu2MfQ3Hbm4Vu4DdpxIaxj/SJGEdaNhuOOac=; b=z32VBuTbZjfpU1fgtp7hGG+6ZENsVcdfunHQN5toz87QPPNUmyZSZwCiEdAsBu1NZGvZuv 3LMSQwEBBMUav0BA== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 73D752C142 for ; Wed, 26 Jul 2023 09:41:34 +0000 (UTC) Date: Wed, 26 Jul 2023 09:41:34 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/110799 - fix bug in code hoisting User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,MISSING_MID,SPF_HELO_NONE,SPF_PASS,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: Message-ID: <20230726094134.f6ja2dA8oE_gfJjmNxlFY2sNadSczx6X07Y-09xOf0E@z> Code hoisting part of GIMPLE PRE failed to adjust the TBAA behavior of common loads in the case the alias set of the ref was the same but the base alias set was not. It also failed to adjust the base behavior, assuming it would match. The following plugs this hole. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/110799 * tree-ssa-pre.cc (compute_avail): More thoroughly match up TBAA behavior of redundant loads. * gcc.dg/torture/pr110799.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr110799.c | 46 +++++++++++++++++++++++++ gcc/tree-ssa-pre.cc | 15 +++++--- 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr110799.c diff --git a/gcc/testsuite/gcc.dg/torture/pr110799.c b/gcc/testsuite/gcc.dg/torture/pr110799.c new file mode 100644 index 00000000000..53f06f079e1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr110799.c @@ -0,0 +1,46 @@ +/* { dg-do run { target { { *-*-linux* *-*-gnu* *-*-uclinux* } && mmap } } } */ + +#include +#include +#include + +struct S { + int a; +}; +struct M { + int a, b; +}; + +int __attribute__((noipa)) +f(struct S *p, int c, int d) +{ + int r; + if (c) + { + if (d) + r = p->a; + else + r = ((struct M*)p)->a; + } + else + r = ((struct M*)p)->b; + return r; +} + +int main () +{ + long pgsz = sysconf(_SC_PAGESIZE); + if (pgsz < sizeof (struct M)) + return 0; + char *p = mmap ((void *) 0, 2 * pgsz, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + if (p == MAP_FAILED) + return 0; + if (mprotect (p, pgsz, PROT_READ | PROT_WRITE)) + return 0; + struct S *q = (struct S *)(p + pgsz) - 1; + q->a = 42; + if (f (q, 1, 1) != 42) + abort (); + return 0; +} diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc index e33c5ba80e2..0f2e458395c 100644 --- a/gcc/tree-ssa-pre.cc +++ b/gcc/tree-ssa-pre.cc @@ -4217,8 +4217,10 @@ compute_avail (function *fun) /* TBAA behavior is an obvious part so make sure that the hashtable one covers this as well by adjusting the ref alias set and its base. */ - if (ref->set == set - || alias_set_subset_of (set, ref->set)) + if ((ref->set == set + || alias_set_subset_of (set, ref->set)) + && (ref->base_set == base_set + || alias_set_subset_of (base_set, ref->base_set))) ; else if (ref1->opcode != ref2->opcode || (ref1->opcode != MEM_REF @@ -4230,16 +4232,19 @@ compute_avail (function *fun) operands.release (); continue; } - else if (alias_set_subset_of (ref->set, set)) + else if (ref->set == set + || alias_set_subset_of (ref->set, set)) { + tree reft = reference_alias_ptr_type (rhs1); ref->set = set; + ref->base_set = set; if (ref1->opcode == MEM_REF) ref1->op0 - = wide_int_to_tree (TREE_TYPE (ref2->op0), + = wide_int_to_tree (reft, wi::to_wide (ref1->op0)); else ref1->op2 - = wide_int_to_tree (TREE_TYPE (ref2->op2), + = wide_int_to_tree (reft, wi::to_wide (ref1->op2)); } else -- 2.35.3