From patchwork Mon Sep 4 18:53:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamza Mahfooz X-Patchwork-Id: 137464 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp1257830vqo; Mon, 4 Sep 2023 11:54:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7QK7ZPRAqjMNx1e3GmzHsmKKzRYKRaGT+HDKtoGaraDouPeTQz/T9RmcMdsR2XlXdYPpH X-Received: by 2002:a17:906:311a:b0:9a5:821e:1655 with SMTP id 26-20020a170906311a00b009a5821e1655mr7621369ejx.71.1693853697003; Mon, 04 Sep 2023 11:54:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693853696; cv=none; d=google.com; s=arc-20160816; b=a0lOBy13YLrh4TaZLvqEurHIHeWPHGgbPhSZejs89TH7tJfDeegxL9HDCZD5SGhZxf JJ89D+SsFLDxbr6y/WqQQooq+3cIDU83S0NM5fslMraK2CU+hK+dW24JqIaaj/TmTvrc XsKPyp0whfGEdZqXfQIYGn2Yj7EA7e7rKP8O7t8+v/+SaIYQ53a5HY6aHlv9fVpJv+SB mOTbCTJZIcRxXNgzVRZ/Bh8wYRHTekLnaD8AVOka/ATZiGi0T6LvqWItRaGBsVedSDna AqgyH4dAgK2S75Z2oGh7fRHj7ANV9f6LjeBZvoDenP8mSBmVIJ08uY9h6JUHxe/aXaFV ByMg== 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:message-id:date:subject:to:from:dmarc-filter :delivered-to; bh=p5sTq9vmy3iSc2VFW7FieeKmHA1HbrfHTvNSQIRdYp0=; fh=nwNWT4wuI7jYqNgw8jLJadeqPPaeKScUWaMrFPZFtRk=; b=pnbNrOswCv5z+LMnuysx/YCc3GqmcYxniQoezZhqai8kbnSsvpA5VnCmWpCL5GV44N 2y3JExtEX86ucPE8zs2sk0y0rRSY7RsYsf0LeVyFyRC6y0JpYl76vYN3hQIWPZuwVRdR JcR4aSP5DBq9ZkBcjKZUSUHutEcwqrwXPjNyNUPcWPOXahJvOZ79v4dQAt88+dTskIdQ 5k7AoM+CWpwje8t6OYnX0l5xGES9DEF2KJh68dNtZbijhF1OoXT1+ZFG9lvDlvqo1LVv xT2rkxnpjuKo60qJSV3n6P+naaT0SuBOscdp2myx4T9BHWaHCQQR6ElTJuyfPRnGLhmy q0wQ== 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 r4-20020a170906350400b0099e028b0201si6569858eja.962.2023.09.04.11.54.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Sep 2023 11:54:56 -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 AFD1F385C421 for ; Mon, 4 Sep 2023 18:54:40 +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 04ECA3858C30 for ; Mon, 4 Sep 2023 18:54:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04ECA3858C30 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 72B5D18000A8; Mon, 4 Sep 2023 14:54:10 -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:01 -0400 (EDT) From: Hamza Mahfooz To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] strlen: fold strstr() even if the length isn't previously known [PR96601] Date: Mon, 4 Sep 2023 14:53:52 -0400 Message-ID: <20230904185353.204611-1-someguy@effective-light.com> X-Mailer: git-send-email 2.42.0 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: 1776134334148260987 X-GMAIL-MSGID: 1776134334148260987 Currently, we give up in fold_strstr_to_strncmp() if the length of the the second argument to strstr() isn't known to us by the time we hit that function. However, we can instead insert a strlen() in ourselves and continue trying to fold strstr() into strlen()+strncmp(). PR tree-optimization/96601 gcc/ChangeLog: * tree-ssa-strlen.cc (fold_strstr_to_strncmp): If arg1_len == NULL, insert a strlen() for strstr()'s arg1 and use it as arg1_len. gcc/testsuite/ChangeLog: * gcc.dg/strlenopt-30.c: Modify 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 | 5 +- gcc/tree-ssa-strlen.cc | 81 ++++++++++++++++------------- 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/gcc/testsuite/gcc.dg/strlenopt-30.c b/gcc/testsuite/gcc.dg/strlenopt-30.c index 2a3098ba96f..1ee814048c1 100644 --- a/gcc/testsuite/gcc.dg/strlenopt-30.c +++ b/gcc/testsuite/gcc.dg/strlenopt-30.c @@ -38,9 +38,6 @@ void f5(char *s) foo_f5(); } -/* Do not perform transform, since strlen (t) - is unknown. */ - __attribute__((no_icf)) _Bool f6(char *s, char *t) { @@ -60,4 +57,4 @@ _Bool f7(char *s) return (t1 == s); } -/* { dg-final { scan-tree-dump-times "__builtin_strncmp" 5 "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 8b7ef919826..b0ebbb0db62 100644 --- a/gcc/tree-ssa-strlen.cc +++ b/gcc/tree-ssa-strlen.cc @@ -5273,6 +5273,7 @@ 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); if (idx) { @@ -5286,51 +5287,57 @@ fold_strstr_to_strncmp (tree rhs1, tree rhs2, gimple *stmt) } } - if (arg1_len != NULL_TREE) + if (arg1_len == NULL_TREE) { - gimple_stmt_iterator gsi = gsi_for_stmt (call_stmt); - tree strncmp_decl = builtin_decl_explicit (BUILT_IN_STRNCMP); + tree strlen_decl = builtin_decl_explicit (BUILT_IN_STRLEN); + gcall *strlen_call = gimple_build_call (strlen_decl, 1, arg1); + tree strlen_lhs = make_ssa_name (size_type_node, strlen_call); + + 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; + } + else if (!is_gimple_val (arg1_len)) + { + tree arg1_len_tmp = make_ssa_name (TREE_TYPE (arg1_len)); + gassign *arg1_stmt = gimple_build_assign (arg1_len_tmp, + arg1_len); + gsi_insert_before (&gsi, arg1_stmt, GSI_SAME_STMT); + arg1_len = arg1_len_tmp; + } - if (!is_gimple_val (arg1_len)) + tree strncmp_decl = builtin_decl_explicit (BUILT_IN_STRNCMP); + gcall *strncmp_call = gimple_build_call (strncmp_decl, 3, + arg0, arg1, arg1_len); + tree strncmp_lhs = make_ssa_name (integer_type_node); + gimple_set_vuse (strncmp_call, gimple_vuse (call_stmt)); + gimple_call_set_lhs (strncmp_call, strncmp_lhs); + gsi_remove (&gsi, true); + gsi_insert_before (&gsi, strncmp_call, GSI_SAME_STMT); + tree zero = build_zero_cst (TREE_TYPE (strncmp_lhs)); + + if (is_gimple_assign (stmt)) + { + if (gimple_assign_rhs_code (stmt) == COND_EXPR) { - tree arg1_len_tmp = make_ssa_name (TREE_TYPE (arg1_len)); - gassign *arg1_stmt = gimple_build_assign (arg1_len_tmp, - arg1_len); - gsi_insert_before (&gsi, arg1_stmt, GSI_SAME_STMT); - arg1_len = arg1_len_tmp; - } - - gcall *strncmp_call = gimple_build_call (strncmp_decl, 3, - arg0, arg1, arg1_len); - tree strncmp_lhs = make_ssa_name (integer_type_node); - gimple_set_vuse (strncmp_call, gimple_vuse (call_stmt)); - gimple_call_set_lhs (strncmp_call, strncmp_lhs); - gsi_remove (&gsi, true); - gsi_insert_before (&gsi, strncmp_call, GSI_SAME_STMT); - tree zero = build_zero_cst (TREE_TYPE (strncmp_lhs)); - - if (is_gimple_assign (stmt)) - { - if (gimple_assign_rhs_code (stmt) == COND_EXPR) - { - tree cond = gimple_assign_rhs1 (stmt); - TREE_OPERAND (cond, 0) = strncmp_lhs; - TREE_OPERAND (cond, 1) = zero; - } - else - { - gimple_assign_set_rhs1 (stmt, strncmp_lhs); - gimple_assign_set_rhs2 (stmt, zero); - } + tree cond = gimple_assign_rhs1 (stmt); + TREE_OPERAND (cond, 0) = strncmp_lhs; + TREE_OPERAND (cond, 1) = zero; } else { - gcond *cond = as_a (stmt); - gimple_cond_set_lhs (cond, strncmp_lhs); - gimple_cond_set_rhs (cond, zero); + gimple_assign_set_rhs1 (stmt, strncmp_lhs); + gimple_assign_set_rhs2 (stmt, zero); } - update_stmt (stmt); } + else + { + gcond *cond = as_a (stmt); + gimple_cond_set_lhs (cond, strncmp_lhs); + gimple_cond_set_rhs (cond, zero); + } + update_stmt (stmt); } } }