From patchwork Fri Nov 3 10:34:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 161228 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp929031vqu; Fri, 3 Nov 2023 03:35:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEYguh0+e0QWEjBhUETRc79Pe4+do/O4fY7W3ziJFINa4nd7/HLPruM2thFI0pUhRJFkXN6 X-Received: by 2002:a05:6214:5089:b0:66d:3a44:374b with SMTP id kk9-20020a056214508900b0066d3a44374bmr23548003qvb.15.1699007721320; Fri, 03 Nov 2023 03:35:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1699007721; cv=pass; d=google.com; s=arc-20160816; b=dADYb0IckRBDJYWPSTq2WGW6/RNTWUYQvkCSdivtLNWsVC+Ej2XIBLjdN0jvYT7Dnj Dryr/1BNWtiQmMKRbthMCTXDf3FTqVN3woSnBNzU+XMVqBt5sPR+oKlXDkYXPLdfoahW Q9FT3xOyQ0/VIRRkQ89+jWdO6dcH144LD/xyvOER8wXr4ZGXCyDNw8vzTit2mb0qLVlv gAWDZl59JbbA+ongyl/5kHHOYCBa2A5xInlxRmwL6ssQg3c7bMkx31MeViDQMQMmpTOh F7Nnh8+cE5irUTh7FxQ82KumCg5VhjF9ma96u4uW5uJ3/QjNZF/Rjmm1BOHKG8HoF4Ih Vl5w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:mime-version:subject :to:from:date:dkim-signature:dkim-signature:arc-filter:dmarc-filter :delivered-to; bh=NsrFkalVc9WKgGNATHlshPaUPUR21D4f+pF4RQvq5EQ=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=aTUQwppWqDOtss4ifgRaE+H/ddpzcR2yCCYxmTNy6rUYXswkdCnKCOpbMx/g/GVA5g eBz5/LTMDV45cpoK4reOhU29CWr2LPBSULQ2GuLbIMLyDtYgT58E6++OpYUubyVqbgTa 1yLmhu4p4yWYnnYorY/eIR0U1zzfj/Oss7/rmKtF53ccpAFxVsuasI2EB2pqWiM+FMKj 9jazt4mMveuRbk1u/n3HVIO1Uw6RCv+KlxvSj+A3nQWarLYBdin12VcuhWBasgiI5KR/ sIHc3O5IKNGbeaVFJBR5ZzJfhueyC3Py13WK+O80ktPAkUYQhlEnYC1p9ywx3pqbfxP+ XUzw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=ywcwEyyh; dkim=neutral (no key) header.i=@suse.de; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dp12-20020a05620a2b4c00b0076db460394esi1224391qkb.438.2023.11.03.03.35.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 03:35:21 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=ywcwEyyh; dkim=neutral (no key) header.i=@suse.de; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1A8DD3858C2F for ; Fri, 3 Nov 2023 10:35:21 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 136D93858C2B for ; Fri, 3 Nov 2023 10:34:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 136D93858C2B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 136D93858C2B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699007698; cv=none; b=La1H4y6pWSWOJQ5zvzO4NRaTwRpBeVUfb0h5mKx4Jphw/FNrLiCMV1sUsaURBi/Tsd4irpJgYFQ6oNXseg34euYd3hvTeoPwTo9sH3kiCjaazVbj042PAmMFOW/qcBbSl4fTp+1TSC/EsvK99qsw9L9NM3izDW3V4gNPPu0PXv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699007698; c=relaxed/simple; bh=clRY+J4MfqlD1qpWvpmIekHFVj8M+gcfJmjgQvWHPGo=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version: Message-Id; b=CUzrA4A7obvxA6ce35mdW9kMHSRx4muFYH65TMkvL4p/lVwsncsA2WYvt6hGzwvFG97DWAvvE3a/wMmNheNJ6c3y64uWt1EGj7mGKsz+czEtti+et/xd8YGELU9pugy9BYELph//aXbVjdjAzLMPcxqi6I5iF4WcxjMItDNpw8E= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2F52F1FD69 for ; Fri, 3 Nov 2023 10:34:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1699007686; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=NsrFkalVc9WKgGNATHlshPaUPUR21D4f+pF4RQvq5EQ=; b=ywcwEyyhH8CeMCV9pxhlH996GPyARn2t5+TI5Y0vIjMnOxmUDoMtIGn1gVghuElUKipjMN MbZCMAhuMwBf9LC0YiIULGymQw51tq2fDvIFn1UQgwsRfqswrhoXmtTgX5BgXMqg18XIxm zt7NNqBvlhor6kn73vKFdO9HKRdGWDo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1699007686; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=NsrFkalVc9WKgGNATHlshPaUPUR21D4f+pF4RQvq5EQ=; b=gjnGbDtgg9b/55eSV5RSIeh9ePN5VnmKxaE3XusHXL2bLvAFbmERLdt/wJSUwc2yCJfxBa Ldauxov5C19dOBCQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1B3F31348C for ; Fri, 3 Nov 2023 10:34:46 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id IcBYBcbMRGUgGgAAMHmgww (envelope-from ) for ; Fri, 03 Nov 2023 10:34:46 +0000 Date: Fri, 3 Nov 2023 11:34:45 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/112310 - code hoisting undefined behavior MIME-Version: 1.0 Message-Id: <20231103103446.1B3F31348C@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781538720393316629 X-GMAIL-MSGID: 1781538720393316629 The following avoids hoisting expressions that may invoke undefined behavior and are not computed on all paths. This is realized by noting that we have to avoid materializing expressions as part of hoisting that are not part of the set of expressions we have found eligible for hoisting. Instead of picking the expression corresponding to the hoistable values from the first successor we now keep a union of the expressions so that hoisting can pick the expression that has its dependences fully hoistable. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/112310 * tree-ssa-pre.cc (do_hoist_insertion): Keep the union of expressions, validate dependences are contained within the hoistable set before hoisting. * gcc.dg/torture/pr112310.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr112310.c | 36 +++++++++++++++++++++++++ gcc/tree-ssa-pre.cc | 26 ++++++++++++------ 2 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr112310.c diff --git a/gcc/testsuite/gcc.dg/torture/pr112310.c b/gcc/testsuite/gcc.dg/torture/pr112310.c new file mode 100644 index 00000000000..daf2390734c --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr112310.c @@ -0,0 +1,36 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int32plus } */ + +extern void abort (void); +short a, b; +static int c, d, e, f = -7; +char g; +int *h = &d; +int **i = &h; +int j; +short(k)(int l) { return l >= 2 ? 0 : l; } +int(m)(int l, int n) { return l < -2147483647 / n ? l : l * n; } +void o() { &c; } +int main() +{ + int p; + for (; g <= 3; g++) + { + for (; c; c++) + ; + a = 2; + for (; a <= 7; a++) { + short *r = &b; + p = m(*h, 2022160547); + unsigned q = 2022160547; + e = p * q; + *r ^= e; + j = k(c + 3); + **i = 0; + } + *i = &f; + } + if (b != -3189) + abort (); + return 0; +} diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc index 07fb165b2a8..361806ac2c9 100644 --- a/gcc/tree-ssa-pre.cc +++ b/gcc/tree-ssa-pre.cc @@ -3635,10 +3635,9 @@ do_hoist_insertion (basic_block block) return false; /* We have multiple successors, compute ANTIC_OUT by taking the intersection - of all of ANTIC_IN translating through PHI nodes. Note we do not have to - worry about iteration stability here so just use the expression set - from the first set and prune that by sorted_array_from_bitmap_set. - This is a simplification of what we do in compute_antic_aux. */ + of all of ANTIC_IN translating through PHI nodes. Track the union + of the expression sets so we can pick a representative that is + fully generatable out of hoistable expressions. */ bitmap_set_t ANTIC_OUT = bitmap_set_new (); bool first = true; FOR_EACH_EDGE (e, ei, block->succs) @@ -3653,10 +3652,15 @@ do_hoist_insertion (basic_block block) bitmap_set_t tmp = bitmap_set_new (); phi_translate_set (tmp, ANTIC_IN (e->dest), e); bitmap_and_into (&ANTIC_OUT->values, &tmp->values); + bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions); bitmap_set_free (tmp); } else - bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values); + { + bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values); + bitmap_ior_into (&ANTIC_OUT->expressions, + &ANTIC_IN (e->dest)->expressions); + } } /* Compute the set of hoistable expressions from ANTIC_OUT. First compute @@ -3697,15 +3701,13 @@ do_hoist_insertion (basic_block block) return false; } - /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */ + /* Hack hoistable_set in-place so we can use sorted_array_from_bitmap_set. */ bitmap_move (&hoistable_set.values, &availout_in_some); hoistable_set.expressions = ANTIC_OUT->expressions; /* Now finally construct the topological-ordered expression set. */ vec exprs = sorted_array_from_bitmap_set (&hoistable_set); - bitmap_clear (&hoistable_set.values); - /* If there are candidate values for hoisting, insert expressions strategically to make the hoistable expressions fully redundant. */ pre_expr expr; @@ -3737,6 +3739,13 @@ do_hoist_insertion (basic_block block) && FLOAT_TYPE_P (get_expr_type (expr))) continue; + /* Only hoist if the full expression is available for hoisting. + This avoids hoisting values that are not common and for + example evaluate an expression that's not valid to evaluate + unconditionally (PR112310). */ + if (!valid_in_sets (&hoistable_set, AVAIL_OUT (block), expr)) + continue; + /* OK, we should hoist this value. Perform the transformation. */ pre_stats.hoist_insert++; if (dump_file && (dump_flags & TDF_DETAILS)) @@ -3774,6 +3783,7 @@ do_hoist_insertion (basic_block block) } exprs.release (); + bitmap_clear (&hoistable_set.values); bitmap_set_free (ANTIC_OUT); return new_stuff;