From patchwork Mon Sep 4 18:53:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamza Mahfooz X-Patchwork-Id: 137465 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp1257965vqo; Mon, 4 Sep 2023 11:55:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7pnwkUPDesDHJTOrY91UjgDA7OHn6z3eP0+xmWfk66GjzqR6t6FIsjXlPOBe5PF4GYqZR X-Received: by 2002:a05:6512:104b:b0:4fe:347d:7c4b with SMTP id c11-20020a056512104b00b004fe347d7c4bmr8476690lfb.7.1693853717429; Mon, 04 Sep 2023 11:55:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693853717; cv=none; d=google.com; s=arc-20160816; b=xDAtQBtfO4exff4HNZoDxVf2sFy0dFi8+lOJnOSp3hVvGIuXAIbj3XBoRnveE3wHqV 1q6CWTovIEsx9wlweTQJGgIoO0n6NXwCrWRR50DuD1TWNQ7FHx1wdvEB5ILMOvcvgVq0 ZgSxqVlQ4wG6i3IZ3YxEiVV/6aGFpHloCPeo78E8RYUQJ+Ze0Cb12FjpNGVyt33aRmXj ZW4XfSBIXoe71iuCol/239YTa2rJzCN+tUaldqURsZyXLve9eUitHUBTN/2if5wNn4O1 o+1iheWj4NthcIg5dfcEC143+ixtElu/PgX8w6dQ6rgS1XRW/3YAi+okGHI7frELnM29 938Q== 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=D8berSDCfk55F8kIom77mtN92bO2x7mSH4Ho22srqf8=; fh=nwNWT4wuI7jYqNgw8jLJadeqPPaeKScUWaMrFPZFtRk=; b=vS7D4fSEvzBZyo2OtCSwfWhfF8LwLeJpa4E3rHD5apGAMCW8Z0Wr/kdiuCXnXBRPEJ S8uQ12ryYiATc9OuZRveYJ4VeOCPKUqrHd4PuFBexa5bGOiUFGRzET/QQmop/hVuYk4x 7UOvP3zA6FGLKkQEo2EqbG+9xYXZdrdXLdpPJTwQiB4uJD9CTh6h6pF4BvSTsdiKbeV1 uivnuenPBlal0kEqLP/cp+paDim5i2Bfn8xSQgEvlqPJ9JHKuwmegMuX7250BCGPUH+q cVJXTmqThhh/YeNAHT1Qj6BLipmK8t2J16sInLMI80Wwm+rITz7GPUeLECLcwqdU4pEN cJjA== 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 lm19-20020a170906981300b0099bd1a1d025si6568044ejb.199.2023.09.04.11.55.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Sep 2023 11:55:17 -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 8B9F238555A3 for ; Mon, 4 Sep 2023 18:54:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from MTA-06-4.privateemail.com (mta-06-4.privateemail.com [198.54.122.146]) by sourceware.org (Postfix) with ESMTPS id 250B63857C41 for ; Mon, 4 Sep 2023 18:54:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 250B63857C41 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-06.privateemail.com (localhost [127.0.0.1]) by mta-06.privateemail.com (Postfix) with ESMTP id 4B45618000B4; Mon, 4 Sep 2023 14:54:23 -0400 (EDT) Received: from hal-station.. (unknown [74.12.11.94]) by mta-06.privateemail.com (Postfix) with ESMTPA; Mon, 4 Sep 2023 14:54:13 -0400 (EDT) From: Hamza Mahfooz To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] strlen: call handle_builtin_strlen() from fold_strstr_to_strncmp() Date: Mon, 4 Sep 2023 14:53:53 -0400 Message-ID: <20230904185353.204611-2-someguy@effective-light.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230904185353.204611-1-someguy@effective-light.com> References: <20230904185353.204611-1-someguy@effective-light.com> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP X-Spam-Status: No, score=-13.3 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 , Hamza Mahfooz , Martin Sebor , 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: 1776134355334582701 X-GMAIL-MSGID: 1776134355334582701 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 --- Please push this for me if you think it looks good. Since, I don't have write access to the repository. --- gcc/testsuite/gcc.dg/strlenopt-30.c | 7 +++++++ gcc/tree-ssa-strlen.cc | 23 ++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/gcc/testsuite/gcc.dg/strlenopt-30.c b/gcc/testsuite/gcc.dg/strlenopt-30.c index 1ee814048c1..de51a66383b 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_strlen" 2 "strlen1" } } */ /* { dg-final { scan-tree-dump-times "__builtin_strncmp" 6 "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 ();