From patchwork Tue Jul 4 09:14:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 115633 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp1080906vqx; Tue, 4 Jul 2023 02:15:55 -0700 (PDT) X-Google-Smtp-Source: APBJJlEQjtYt7LvCTrPBiwtO9oJyK9Ntxdk3U+NXbBDiZq8z7OhSf0Ln1nku58O+6TpvOWEoAl/i X-Received: by 2002:a17:906:1757:b0:987:4e89:577f with SMTP id d23-20020a170906175700b009874e89577fmr8260715eje.24.1688462155686; Tue, 04 Jul 2023 02:15:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688462155; cv=none; d=google.com; s=arc-20160816; b=0Urnglhmi+e3T0Dc2zyrwGVKiJep3pgCa9wko7kNwLf2kYelWdJNZ/FCivEjhvzlcY NL92Iy8GENbTfXp5LmbDJXKUbB9V+o3qs0YAofInqSuONZUAsDucsT0FGDlMQcslEqXX l9iqYT5l+S6RRVtCxoM/VIM6ZrxFSER/EGhigP3q13TTUiv1WUlAUuAbekoymp1P+mKh c8TCjSN/SXA0/s5x2pp0ugxFHgjB5q2xnEgaArtKV88earYq6ymCpQz6v0rTro9ww72J /etGBxa9AvNYZs6MONNGrFwIvOB79GIreNfQ6Dvv7h0hrxchLw+Y6TOtY3hTU7OQoI2x nPeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:sender:errors-to:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=cufGmd30hdQBysLUJMPCvr3mu+bHVN/4m7UfiHP3CkE=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=pgeVxw0GyfIJl/+BzWWNVRMe5XDzUpHf3ymQD7vWmxGtXGs51GWoenRixmH6ctfz+X 775ZlGUEp3Kw1VgzmujnGRNfCPfR4Re73mucFutobX4gXJJcAm49e4sIKQcXvvBl4Wiw 9P3LmaWpNl5pjQrLMRGltt/Sr6fuLBa7L3lIFKGjMCgsyn8KKyGA1+j39ciDxxsWz7ru nv+JdWTBWi8qR8sho3y5/kObdJ2Jn/UX2XfE3OFzz+qoc7EcV+X8bjrGgvGO9pfXsJU1 IUv2M76gUvtFVpmnF+LLpAA+v5t4353cHTj+A8NS3MH2YWCjZlV6L+ieNaSiBaD7Sghu ZLqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FceNjKfp; 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=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id a23-20020a170906685700b0098eeb136963si11387730ejs.172.2023.07.04.02.15.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 02:15:55 -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=@gcc.gnu.org header.s=default header.b=FceNjKfp; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7C7393857734 for ; Tue, 4 Jul 2023 09:15:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C7393857734 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688462154; bh=cufGmd30hdQBysLUJMPCvr3mu+bHVN/4m7UfiHP3CkE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=FceNjKfpwz3rqlFrtx097c1nLA8UAbthFUIUEdjrfrGSiJ7cIOjkkAgt+2VyuWGKN u+hmErBa8Ix/Sv3hbB2E4q2xJYDX8h1xxXszMAGeVMR7TMLhPEmFVa58OieLdB9Atl dQMyCyuo5xvW8lgNOYF6QXr2HYuhrQ85yPmngXGg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id A4DB83858D35 for ; Tue, 4 Jul 2023 09:14:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A4DB83858D35 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id D180622647 for ; Tue, 4 Jul 2023 09:14:43 +0000 (UTC) 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 BDA712C141 for ; Tue, 4 Jul 2023 09:14:43 +0000 (UTC) Date: Tue, 4 Jul 2023 09:14:43 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/110228 - avoid undefs in ifcombine more thoroughly User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 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 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-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" Message-Id: <20230704091554.7C7393857734@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770480893264006948?= X-GMAIL-MSGID: =?utf-8?q?1770480893264006948?= The following replaces the simplistic gimple_uses_undefined_value_p with the conservative mark_ssa_maybe_undefs approach as already used by LIM and IVOPTs. This is to avoid exposing an unconditional uninitialized read on a path from entry by if-combine. Boostrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/110228 * tree-ssa-ifcombine.cc (pass_tree_ifcombine::execute): Mark SSA may-undefs. (bb_no_side_effects_p): Check stmt uses for undefs. * gcc.dg/torture/pr110228.c: New testcase. * gcc.dg/uninit-pr101912.c: Un-XFAIL. --- gcc/testsuite/gcc.dg/torture/pr110228.c | 34 +++++++++++++++++++++++++ gcc/testsuite/gcc.dg/uninit-pr101912.c | 2 +- gcc/tree-ssa-ifcombine.cc | 8 +++++- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr110228.c diff --git a/gcc/testsuite/gcc.dg/torture/pr110228.c b/gcc/testsuite/gcc.dg/torture/pr110228.c new file mode 100644 index 00000000000..add9f17b421 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr110228.c @@ -0,0 +1,34 @@ +/* { dg-do run { target x86_64-*-* i?86-*-* } } */ +/* { dg-require-effective-target lp64 } */ + +unsigned a[4] = {1,1,1,1}; +unsigned tt1 = 0; + +__attribute__((noipa)) +static void bug(unsigned * p, unsigned *t, int n, int t2) +{ + for(int i = 0; i < n; i++) + { + _Bool LookupFlags ; + unsigned v = t[i]; + unsigned tt = tt1; + if (v == 0) + LookupFlags = 0; + else if (v == 1) + LookupFlags = 1; + if (LookupFlags) { + tt|=3u; + LookupFlags = 0; + } + asm("movq $-1, %q1":"+a"(LookupFlags)); + *p = tt; + } +} + +int main() +{ + unsigned r = 42; + bug(&r,a, sizeof(a)/sizeof(a[0]), 1); + __builtin_printf("%u\n", r); + if (r != 3) __builtin_abort(); +} diff --git a/gcc/testsuite/gcc.dg/uninit-pr101912.c b/gcc/testsuite/gcc.dg/uninit-pr101912.c index 62cd2a0c73e..cb7d7516e91 100644 --- a/gcc/testsuite/gcc.dg/uninit-pr101912.c +++ b/gcc/testsuite/gcc.dg/uninit-pr101912.c @@ -11,7 +11,7 @@ tzloadbody (void) for (int i = 0; i < n; i++) { int corr = getint (); - if (corr < 1 || (corr == 1 && !(leapcnt == 0 || (prevcorr < corr ? corr == prevcorr + 1 : (corr == prevcorr || corr == prevcorr - 1))))) /* { dg-bogus "uninitialized" "pr101912" { xfail *-*-* } } */ + if (corr < 1 || (corr == 1 && !(leapcnt == 0 || (prevcorr < corr ? corr == prevcorr + 1 : (corr == prevcorr || corr == prevcorr - 1))))) /* { dg-bogus "uninitialized" "pr101912" } */ return -1; prevcorr = corr; diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc index 21a77ddecc7..58e19c1508e 100644 --- a/gcc/tree-ssa-ifcombine.cc +++ b/gcc/tree-ssa-ifcombine.cc @@ -128,7 +128,6 @@ bb_no_side_effects_p (basic_block bb) gassign *ass; enum tree_code rhs_code; if (gimple_has_side_effects (stmt) - || gimple_uses_undefined_value_p (stmt) || gimple_could_trap_p (stmt) || gimple_vuse (stmt) /* We need to rewrite stmts with undefined overflow to use @@ -153,6 +152,12 @@ bb_no_side_effects_p (basic_block bb) should handle this. */ || is_gimple_call (stmt)) return false; + + ssa_op_iter it; + tree use; + FOR_EACH_SSA_TREE_OPERAND (use, stmt, it, SSA_OP_USE) + if (ssa_name_maybe_undef_p (use)) + return false; } return true; @@ -836,6 +841,7 @@ pass_tree_ifcombine::execute (function *fun) bbs = single_pred_before_succ_order (); calculate_dominance_info (CDI_DOMINATORS); + mark_ssa_maybe_undefs (); /* Search every basic block for COND_EXPR we may be able to optimize.