From patchwork Mon Sep 4 20:58:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamza Mahfooz X-Patchwork-Id: 137469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp1306949vqo; Mon, 4 Sep 2023 13:59:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGR4HbRMXYi1ETX6amX7NbXgvdYq5PaUTYttMP2sNXhdjiD90ZGUGSIVBqRkOQw5/P9n88p X-Received: by 2002:a17:907:b0b:b0:9a1:d29c:6aaa with SMTP id h11-20020a1709070b0b00b009a1d29c6aaamr8721030ejl.39.1693861180678; Mon, 04 Sep 2023 13:59:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693861180; cv=none; d=google.com; s=arc-20160816; b=C1AaUqxZPL2rfaLRFvWQ8UA99vrcB1aEoPlIJXVIt8lDJzGF3WyQnM7qHx6rPkIvmB SZbkLcVEmn1zdSGiT4sdnancRe8wSF1sgBnUOvtKiw6BV3zuEVb0t4TcKfxD8H5yzu5a 5N3VO94CSFmFrBa9Ez+yNZpD7P82TyeR3PcVFhg4yZdBi7iqrXmFCM1lUfRpcCIWCcfc j9/xGgoCvJ9UXnIAxPRaE7Uxxuih8+2lBsDjFbETrcPNJ34vThLQvk96Dq6pr6OAiTtw cjSV5twNdVjo4/r/wp+d2tfolvImo+L/5eqaMOWYFAfvJFQwQ7chYYSknrU7kvb1Hqu8 x3jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dmarc-filter:delivered-to; bh=vvVUpEd8sp9TzE3mnF5TDHauyTf1fLw6DLt1ZDjvBAk=; fh=CdEkqPxyl6l1/O8YieVdBccAd9dWqhDyTOL2KF4vwOU=; b=aSSd1VvRQXFi6TlSTvECLZDkqycjJP010CeypCgKMGvuFPp5gFcejUFAVjug1ALfMN +iEj6j6nq/6CJVQWh++I4o2Pg9xytwO4WorkyOLpq/x2CL++T2klJzCLGF/qzXIiyrP0 tp7KfFraIR8I8Tj6GHWnJ8eqYP0dCyh1UjXO1/I0h+T26Ad7lCtZHGoML9q2YCBiHrDi bdPNzk5GeEJC5popI+au00MsciZFfZdK3L+80qSX5KppJ76WopspLW9RlLemN0CLe6Y0 CyoG2p/ztB5Vw7dtSGN2onOCThWhhvpkSxTclmfYdTmJ9/qtkCWWjEP89MINPiJRWx2b G+cw== ARC-Authentication-Results: i=1; mx.google.com; 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" Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hb8-20020a170906b88800b0099bcae257e3si6805341ejb.832.2023.09.04.13.59.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Sep 2023 13:59:40 -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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 27D673856241 for ; Mon, 4 Sep 2023 20:59:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from MTA-05-3.privateemail.com (mta-05-3.privateemail.com [198.54.118.212]) by sourceware.org (Postfix) with ESMTPS id 65E6F3857B98; Mon, 4 Sep 2023 20:58:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 65E6F3857B98 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=effective-light.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=effective-light.com Received: from mta-05.privateemail.com (localhost [127.0.0.1]) by mta-05.privateemail.com (Postfix) with ESMTP id 78F4018000D9; Mon, 4 Sep 2023 16:58:43 -0400 (EDT) Received: from hal-station.. (unknown [74.12.11.94]) by mta-05.privateemail.com (Postfix) with ESMTPA; Mon, 4 Sep 2023 16:58:33 -0400 (EDT) From: Hamza Mahfooz To: gcc-patches@gcc.gnu.org Subject: [PATCH v2 2/2] strlen: call handle_builtin_strlen() from fold_strstr_to_strncmp() Date: Mon, 4 Sep 2023 16:58:14 -0400 Message-ID: <20230904205814.222166-2-someguy@effective-light.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230904205814.222166-1-someguy@effective-light.com> References: <20230904205814.222166-1-someguy@effective-light.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jakub Jelinek , Richard Biener , Martin Sebor , Hamza Mahfooz , Ian Lance Taylor Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776142181435868711 X-GMAIL-MSGID: 1776142181435868711 Currently, we are not saving the strlen() call we inserted for possible future common subexpression elimination. Also, it's possible that we can further fold that strlen() call. So, refactor handle_builtin_strlen() so that it can be called from fold_strstr_to_strncmp(). gcc/ChangeLog: * tree-ssa-strlen.cc (strlen_pass::handle_builtin_strlen): Remove from class and mark as static. (handle_builtin_strlen): Add parameter "gimple_stmt_iterator gsi" and replace references of m_gsi with gsi. (fold_strstr_to_strncmp): Call handle_builtin_strlen(). (strlen_pass::check_and_optimize_call): Add m_gsi to the handle_builtin_strlen() call. gcc/testsuite/ChangeLog: * gcc.dg/strlenopt-30.c: Add a test. Signed-off-by: Hamza Mahfooz --- v2: bump up the number of strncmp()s from 6 to 7 in strlenopt-30.c --- gcc/testsuite/gcc.dg/strlenopt-30.c | 9 ++++++++- gcc/tree-ssa-strlen.cc | 23 ++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/gcc/testsuite/gcc.dg/strlenopt-30.c b/gcc/testsuite/gcc.dg/strlenopt-30.c index 1ee814048c1..d89fe83ca98 100644 --- a/gcc/testsuite/gcc.dg/strlenopt-30.c +++ b/gcc/testsuite/gcc.dg/strlenopt-30.c @@ -44,6 +44,12 @@ _Bool f6(char *s, char *t) return __builtin_strstr (s, t) == s; } +__attribute__((no_icf)) +_Bool f6plus(char *s, char *t) +{ + return __builtin_strstr (s, t) == s && __builtin_strlen(t) > 10; +} + /* Do not perform transform in this case, since t1 doesn't have single use. */ @@ -57,4 +63,5 @@ _Bool f7(char *s) return (t1 == s); } -/* { dg-final { scan-tree-dump-times "__builtin_strncmp" 6 "strlen1" } } */ +/* { dg-final { scan-tree-dump-times "__builtin_strlen" 2 "strlen1" } } */ +/* { dg-final { scan-tree-dump-times "__builtin_strncmp" 7 "strlen1" } } */ diff --git a/gcc/tree-ssa-strlen.cc b/gcc/tree-ssa-strlen.cc index b0ebbb0db62..8ec6ddbc7c0 100644 --- a/gcc/tree-ssa-strlen.cc +++ b/gcc/tree-ssa-strlen.cc @@ -252,7 +252,6 @@ public: bool handle_assign (tree lhs, bool *zero_write); bool handle_store (bool *zero_write); void handle_pointer_plus (); - void handle_builtin_strlen (); void handle_builtin_strchr (); void handle_builtin_strcpy (built_in_function); void handle_integral_assign (bool *cleanup_eh); @@ -2211,10 +2210,10 @@ strlen_pass::maybe_warn_overflow (gimple *stmt, bool call_lhs, the strlen call with the known value, otherwise remember that strlen of the argument is stored in the lhs SSA_NAME. */ -void -strlen_pass::handle_builtin_strlen () +static void +handle_builtin_strlen (gimple_stmt_iterator gsi) { - gimple *stmt = gsi_stmt (m_gsi); + gimple *stmt = gsi_stmt (gsi); tree lhs = gimple_call_lhs (stmt); if (lhs == NULL_TREE) @@ -2268,8 +2267,8 @@ strlen_pass::handle_builtin_strlen () if (bound) rhs = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (rhs), rhs, bound); - gimplify_and_update_call_from_tree (&m_gsi, rhs); - stmt = gsi_stmt (m_gsi); + gimplify_and_update_call_from_tree (&gsi, rhs); + stmt = gsi_stmt (gsi); update_stmt (stmt); if (dump_file && (dump_flags & TDF_DETAILS) != 0) { @@ -2367,8 +2366,8 @@ strlen_pass::handle_builtin_strlen () } if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (ret))) ret = fold_convert_loc (loc, TREE_TYPE (lhs), ret); - gimplify_and_update_call_from_tree (&m_gsi, ret); - stmt = gsi_stmt (m_gsi); + gimplify_and_update_call_from_tree (&gsi, ret); + stmt = gsi_stmt (gsi); update_stmt (stmt); if (dump_file && (dump_flags & TDF_DETAILS) != 0) { @@ -5272,8 +5271,9 @@ fold_strstr_to_strncmp (tree rhs1, tree rhs2, gimple *stmt) { tree arg1 = gimple_call_arg (call_stmt, 1); tree arg1_len = NULL_TREE; - int idx = get_stridx (arg1, call_stmt); gimple_stmt_iterator gsi = gsi_for_stmt (call_stmt); +again: + int idx = get_stridx (arg1, call_stmt); if (idx) { @@ -5296,7 +5296,8 @@ fold_strstr_to_strncmp (tree rhs1, tree rhs2, gimple *stmt) gimple_call_set_lhs (strlen_call, strlen_lhs); gimple_set_vuse (strlen_call, gimple_vuse (call_stmt)); gsi_insert_before (&gsi, strlen_call, GSI_SAME_STMT); - arg1_len = strlen_lhs; + handle_builtin_strlen (gsi_for_stmt (strlen_call)); + goto again; } else if (!is_gimple_val (arg1_len)) { @@ -5393,7 +5394,7 @@ strlen_pass::check_and_optimize_call (bool *zero_write) { case BUILT_IN_STRLEN: case BUILT_IN_STRNLEN: - handle_builtin_strlen (); + handle_builtin_strlen (m_gsi); break; case BUILT_IN_STRCHR: handle_builtin_strchr ();