From patchwork Tue Jul 26 13:41:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 186 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp2269885pxt; Tue, 26 Jul 2022 06:42:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1voSMi7uWZEZgNkG2EqZWVJkciKKTfazAIB3iqSfSYVaLBkMqgSJljNc/voNBqecszOCdFb X-Received: by 2002:a05:6402:48c:b0:43a:8bc7:f440 with SMTP id k12-20020a056402048c00b0043a8bc7f440mr17948989edv.8.1658842955149; Tue, 26 Jul 2022 06:42:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658842955; cv=none; d=google.com; s=arc-20160816; b=hkOp+KPUOf+KkOqNX8YTmVPW71xmdGNUNbka2OLvr97iWtG7G+5A7AzWV87mORWcqr x684Kmfvkd4+cZ2a/qHLI3ogal9mpz42mXdYLtFjsNC4Kuy/VNSjsOiA4QZLdGmHd4iF TfMQua/DDmopuJzimxOidd5FvohUiDWGSkeE7XTq57kdrjRvkLAB/9fPMcqPQ81kzNPR qyl9hInP1cEhvVfBL7THRHbGhceWk/+/kOYskbQohlXBOy8xPzARLpG/WzyBu9wN5dMM 4eJSdWHBV8HM16MmqAufhYdko3gtJ/0EY0UyosCqL6dR2teIKe5xzskQeCBSbxiXz++6 /Qkg== 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=K2H3ccnEaksV3eMuHFq66GOO/MoZfeO8pMISJJRLses=; b=JEBs2iAlDaJBwcIw9ectTF7JcnCEi+AylWeqVY11ccWaQGtM0BQU8jyP9pooch1dO8 miS5im6pBDdBhFIG17bPN7AZUwIORxEtXaSK4rSejts6LLX/I4aHn99rbtyNubccpzF2 mULRbRT87FRpn1cyrnluk9hPjS8WQtiOGoI/LxXIcdydjmapCeWyQpSfNZmFbPVDBoPn Yihg8EzcO0Y0/8AivEsSzsRmbeXx2932djnUPEHaTHT68cZhnWvL8dAFB3aqL2muD/aA +7x4CE6TI7mXmFjdCuq9mxzOYG4qkJP7SD5G3GIHH795l1YWoKEY50B614o9RURX1QsB ME/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="VHYGf/vE"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u26-20020aa7d89a000000b0043bbd40a8fesi11065153edq.164.2022.07.26.06.42.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Jul 2022 06:42:35 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="VHYGf/vE"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 AD05F3857346 for ; Tue, 26 Jul 2022 13:42:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD05F3857346 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658842949; bh=K2H3ccnEaksV3eMuHFq66GOO/MoZfeO8pMISJJRLses=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VHYGf/vEvhokwp0Jhq4MTpB5JmSlmWNsCaM4p5yzLO3AHQ9bHXBKsbQ7Gg475QAOx rRz28n6ilOE9HL1DRmMurhrqmNuNAa8p0m73RZAnMV9qPKMuKX5L2+5lz3Hra9V2Bc dPMCmOd1hj6QTMgNYGVxJ7WMairLP/X2odaP+6Zs= 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 [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 711B13858D32 for ; Tue, 26 Jul 2022 13:41:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 711B13858D32 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 614351FE66 for ; Tue, 26 Jul 2022 13:41:45 +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 5B9A02C141 for ; Tue, 26 Jul 2022 13:41:45 +0000 (UTC) Date: Tue, 26 Jul 2022 13:41:45 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] tree-optimization/105142 - improve maybe_fold_comparisons_from_match_pd fix User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 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 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: <20220726134229.AD05F3857346@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739413877355496432?= X-GMAIL-MSGID: =?utf-8?q?1739422910642582807?= The following improves on the fix for PR105142 which restricted the expression lookup used for maybe_fold_comparisons_from_match_pd to avoid picking up flow-sensitive info for use in places where guarding conditions do not hold. Instead of not allowing to expand SSA definitions there the following temporarily clears flow-sensitive info on the SSA names and restores it when finished matching. Bootstrapped and tested on x86_64-unknown-linux-gnu. PR tree-optimization/105142 * gimple-fold.cc (fosa_unwind): New global. (follow_outer_ssa_edges): When the SSA definition to follow is does not dominate fosa_bb, temporarily clear flow-sensitive info. Make sure to not expand stmts with not defined overflow. (maybe_fold_comparisons_from_match_pd): Set up unwind stack for follow_outer_ssa_edges and unwind flow-sensitive info clearing after matching. --- gcc/gimple-fold.cc | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index a1704784bc9..14640f6952a 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -6886,6 +6886,7 @@ and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b, } static basic_block fosa_bb; +static vec > *fosa_unwind; static tree follow_outer_ssa_edges (tree val) { @@ -6899,7 +6900,21 @@ follow_outer_ssa_edges (tree val) && (def_bb == fosa_bb || dominated_by_p (CDI_DOMINATORS, fosa_bb, def_bb)))) return val; - return NULL_TREE; + /* We cannot temporarily rewrite stmts with undefined overflow + behavior, so avoid expanding them. */ + if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (val)) + || POINTER_TYPE_P (TREE_TYPE (val))) + && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (val))) + return NULL_TREE; + /* If the definition does not dominate fosa_bb temporarily reset + flow-sensitive info. */ + if (val->ssa_name.info.range_info) + { + fosa_unwind->safe_push (std::make_pair + (val, val->ssa_name.info.range_info)); + val->ssa_name.info.range_info = NULL; + } + return val; } return val; } @@ -6958,9 +6973,14 @@ maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code, type, gimple_assign_lhs (stmt1), gimple_assign_lhs (stmt2)); fosa_bb = outer_cond_bb; + auto_vec, 8> unwind_stack; + fosa_unwind = &unwind_stack; if (op.resimplify (NULL, (!outer_cond_bb ? follow_all_ssa_edges : follow_outer_ssa_edges))) { + fosa_unwind = NULL; + for (auto p : unwind_stack) + p.first->ssa_name.info.range_info = p.second; if (gimple_simplified_result_is_gimple_val (&op)) { tree res = op.ops[0]; @@ -6982,6 +7002,9 @@ maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code, return build2 ((enum tree_code)op.code, op.type, op0, op1); } } + fosa_unwind = NULL; + for (auto p : unwind_stack) + p.first->ssa_name.info.range_info = p.second; return NULL_TREE; } From patchwork Tue Jul 26 13:41:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 187 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp2269997pxt; Tue, 26 Jul 2022 06:42:48 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vvxZQ3UPMfI0Jrk/3/kAMSyGjIpYLV1AmSP4xSAQomEU8tyQ6+yfJeiQ9Yq03PdVtBH7bj X-Received: by 2002:a17:907:6930:b0:72b:6da5:7c08 with SMTP id rb48-20020a170907693000b0072b6da57c08mr14295437ejc.187.1658842968829; Tue, 26 Jul 2022 06:42:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658842968; cv=none; d=google.com; s=arc-20160816; b=pNkgNqIc7stNRszQjn63KqqIcsmg0QbuJejddAiL+puB7Fd3uOajvcukl0Gj54oBe2 Te8sz9SoAP9NSYnxPUp7AIIFf+m+a6m3GLMG/upFPtKpEc+gedp5ZDCSrMp9rHkCB6YC Hp8IfgVSznZZyonkNfVvVNo5NAhV8UwUufiicoRCTFI0aX8+d0ppqiTUb8hUl40UgYBt 076/IL/5L/4nlRSlfseFw4r5NkTPI/TwjSTWnAovXvN3wGGFgNHvn6E2zjBVSdrdItEx 04BBFFuhDNpbQBeLHaKxkaL8eSaLTF86W1E/mLpvePg3hmFx66mZGcnQsnSm33eHc4DP gXzg== 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=mP5qEBopfFNeIJU6DjrnG0UPd3zcw5UDmFBi23Sft/g=; b=Z3Vw0YfJvg5LMfK/opVd6Ikbub1Ot4iN1DYFR9FkP6bsVzZCT9NlLLw6mHdSleTt3k vv9ZcP929gMQr70Tl2QG/Lk8fCsTTi/7kHX4hZCJKNQLCrIjjrL+Gl5FFyCDvFioZRZC FmwJD2DKx6ksqnkXdWkQYjosojXesy9Ii6HeLsT18WUE4ayE9lS1LZ2MaMGSkiPU3WES EK2LeYklDaNW7RqqJToivlNJQYxqlhvua7LSxUzG1A4+oWukVWmosQ4FR+5x2vXMb4Ql iWIjbCICeNEYa+BRKR+6pvU8VBXKtKhAN3ETye5xn5rBD2wEGnjBw4X5iP7lEbsfGRpJ enJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=eIp3JiWk; 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 sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id se16-20020a170906ce5000b007262a4865e0si12329191ejb.931.2022.07.26.06.42.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Jul 2022 06:42:48 -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=eIp3JiWk; 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 D3FFB385801B for ; Tue, 26 Jul 2022 13:42:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D3FFB385801B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658842962; bh=mP5qEBopfFNeIJU6DjrnG0UPd3zcw5UDmFBi23Sft/g=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=eIp3JiWkE5Y91coC3u6cluDfUiElu6Q7S7FbBEQUJm+dcGIs1/hlj6PZ6wT4mUnmo XUwuagCjhAnHvRqt35pOnVk2Qiu6IZiGUN2m3r7Si6DYCHddhGd1VkZsG8rO7yczOA xJVLYBlAU5+yY6gZvkehD+7af9OOowUKCZoYPLVs= 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 [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 2D8B6385801E for ; Tue, 26 Jul 2022 13:42:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2D8B6385801E Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 6A7561FE66 for ; Tue, 26 Jul 2022 13:41:59 +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 652692C141 for ; Tue, 26 Jul 2022 13:41:59 +0000 (UTC) Date: Tue, 26 Jul 2022 13:41:59 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] tree-optimization/105651 - simplify address range overlap check User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 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 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: <20220726134242.D3FFB385801B@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739422925147221217?= X-GMAIL-MSGID: =?utf-8?q?1739422925147221217?= The following adds a pattern for ifcombine to match an address overlap check and use alias analysis to decide overlap at compile-time. This happens with code generated from std::string as shown in the PR even if meanwhile the trunk generated code causes the pattern to no longer match. Bootstrapped and tested on x86_64-unknown-linux-gnu. PR tree-optimization/105651 * match.pd: Add pattern optimizing address range overlap. * gcc.dg/tree-ssa/ssa-ifcombine-14.c: New testcase. --- gcc/match.pd | 47 +++++++++++++++++++ .../gcc.dg/tree-ssa/ssa-ifcombine-14.c | 18 +++++++ 2 files changed, 65 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-14.c diff --git a/gcc/match.pd b/gcc/match.pd index 330c1db0c8e..7661317a53f 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -7806,6 +7806,53 @@ and, { swap_p ? @0 : @2; })) { rhs_tree; }))))))))) +/* Similarly handle + + (uint) @A < (uint) @B + @BO & (uint) @B < (uint) @A + @AO + + in particular optimize the case when @A and @B are within distinct + objects and all addresses are invariant as happens with std::string + in PR105651. */ +(for cmp1 (lt le le lt) + cmp2 (lt le lt le) + (simplify + (bit_and (cmp1:c (convert@4 @0) (convert @1)) + (cmp2:c (convert@5 @2) (convert @3))) + (with + { + tree addr0, addr1, addr2, addr3; + /* Instead of multiple matchings see to extract a base from the + conversion operands since all we need to relate is bases. */ + auto get_addr = [] (tree op) -> tree { + if (TREE_CODE (op) == ADDR_EXPR) + return op; + else if (TREE_CODE (op) == SSA_NAME) + if (gassign *ass = dyn_cast (SSA_NAME_DEF_STMT (op))) + if (gimple_assign_rhs_code (ass) == ADDR_EXPR + || gimple_assign_rhs_code (ass) == POINTER_PLUS_EXPR) + return gimple_assign_rhs1 (ass); + return op; + }; + } + (if ((addr0 = get_addr (@0)) + && (addr1 = get_addr (@1)) + && (addr2 = get_addr (@2)) + && (addr3 = get_addr (@3)) + && INTEGRAL_TYPE_P (TREE_TYPE (@4)) + && types_match (TREE_TYPE (@4), TREE_TYPE (@5))) + (with { poly_int64 diff0, diff1; } + (if (ptr_difference_const (addr3, addr0, &diff0) + && known_ge (diff0, 0) + && ptr_difference_const (addr1, addr2, &diff1) + && known_ge (diff1, 0) + /* Since we are in the end just comparing whether two objects may + overlap via alias analysis it isn't really important to exactly + compute the ranges above. ptr_difference_const mainly determines + there is a common base and the known_ge is testing we are comparing + the proper end points. */ + && !ptr_derefs_may_alias_p (addr0, addr1)) + { constant_boolean_node (false, type); })))))) + /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero element of @1. */ (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-14.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-14.c new file mode 100644 index 00000000000..f058ac04a36 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-14.c @@ -0,0 +1,18 @@ +/* From PR105651 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ + +typedef __UINTPTR_TYPE__ uintptr_t; +void init (char *a); +void oops (); +void foo () +{ + char a[16]; + init (a); + char *p = "foo"; + if ((uintptr_t)a < (uintptr_t)&p[4] && (uintptr_t)p < (uintptr_t)&a[16]) + oops (); +} + +/* { dg-final { scan-tree-dump "optimizing two comparisons to 0" "ifcombine" } } */ +/* { dg-final { scan-tree-dump-not "oops" "ifcombine" } } */