From patchwork Mon Aug 1 07:15:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 73086 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp2552166qga; Mon, 1 Aug 2016 00:16:22 -0700 (PDT) X-Received: by 10.98.29.201 with SMTP id d192mr93412841pfd.142.1470035782487; Mon, 01 Aug 2016 00:16:22 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id d186si33717975pfc.72.2016.08.01.00.16.22 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Aug 2016 00:16:22 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-432886-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-432886-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-432886-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=XRA0QxAVD0CtqdQr6AxmftDiulwa3A6pBr3QdeMM5Uh7AF VoPVCiCoiAxKI1VLJ7NewCld0MZPAmFYtRIysFc6A43NyEmFZYYGjZdV2D27EyS6 iDKK7pulUaqyvuCfiBpwfl2/VKkkLa9JGo7J4NwKsFYQgPuOuXq7yKew7xqv0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=//kuV8oLlUE2C/42h4ng/fhWNiA=; b=ATCx9Dh1trr1loVazthO BQQsmz5WEucXgf80ayRyfaMY5/fXusblsbdIXScqNsxEzNRKQC1oqI2HhTRBHHI4 8Rrtlxk3qIsAa25TP2Xc68plp2pclOwAHoZKJaNzJxkyvkblE2iA38KYWbey67jW gEGHi/zsweHzAtLrQdDnYMg= Received: (qmail 31610 invoked by alias); 1 Aug 2016 07:16:08 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 31595 invoked by uid 89); 1 Aug 2016 07:16:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=sk:propert, HTo:U*rguenther, ideal, 2016-08-01 X-HELO: mail-it0-f44.google.com Received: from mail-it0-f44.google.com (HELO mail-it0-f44.google.com) (209.85.214.44) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 01 Aug 2016 07:15:56 +0000 Received: by mail-it0-f44.google.com with SMTP id j124so161182541ith.1 for ; Mon, 01 Aug 2016 00:15:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=nXGCzkEm2gL/ipgbA1PYMADa8815T6SVJQshw0xdd88=; b=ZumVLAGv177APmNv1uwZxXTP7lMZ5tlDEsXeLTyC/8MukklYaHTEBuSKRKpI6ybeuU GaFGcmDqvxD3aDLryuSNjtIbwWPb+9JgO+MMg8zGmd0fgRus3coEOqgVlNu75v8Kp7fK 8C8bgf5Ou3XJlGs0eRl70ERkL7tssJBlv17wGtwS6c1BYC+pwB74Svr3G1A0BltRbIp+ tYspp5CCgfvVX0o37+Wy/SSLNthqFCDH5Ene0AMau9V735gcaYPH7z5c0h4nBmgsKwbB G0QR1YVaPZpA4EdpUDmd0eIhYGGMDFT1KYZd3TWtTOnkfD49aGATTxcJzoPvGLzH3+X8 Sacg== X-Gm-Message-State: AEkoouue0lS7ccGeuHrm5pa9pHl6WSo3KIFmwYlGi4LctHoJKxnCFN+UylalXWkIf/v58Nppg5d4s+XJ+qbayp6m X-Received: by 10.36.90.79 with SMTP id v76mr53069490ita.16.1470035754795; Mon, 01 Aug 2016 00:15:54 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.48.197 with HTTP; Mon, 1 Aug 2016 00:15:54 -0700 (PDT) From: Prathamesh Kulkarni Date: Mon, 1 Aug 2016 12:45:54 +0530 Message-ID: Subject: fold strlen (s) eq/ne 0 to *s eq/ne 0 on GIMPLE To: Richard Biener , gcc Patches X-IsSubscribed: yes Hi Richard, The attached patch tries to fold strlen (s) eq/ne 0 to *s eq/ne 0 on GIMPLE. I am not sure where was the ideal place to put this transform in and ended up adding it to strlen_optimize_stmt(). Does that look OK ? I needed to add TODO_update_ssa to strlen pass, otherwise we hit the following assert in execute_todo(): if (flag_checking && cfun && need_ssa_update_p (cfun)) gcc_assert (flags & TODO_update_ssa_any); Bootstrap+test in progress on x86_64-unknown-linux-gnu. Thanks, Prathamesh 2016-08-01 Prathamesh Kulkarni * tree-ssa-strlen.c (strlen_optimize_stmt): Fold strlen (s) eq/ne 0 to *s eq/ne 0. Change todo_flags_finish for pass_data_strlen from 0 to TODO_update_ssa. testsuite/ * gcc.dg/strlenopt-30.c: New test-case. diff --git a/gcc/testsuite/gcc.dg/strlenopt-30.c b/gcc/testsuite/gcc.dg/strlenopt-30.c new file mode 100644 index 0000000..da9732f --- /dev/null +++ b/gcc/testsuite/gcc.dg/strlenopt-30.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-strlen" } */ + +__attribute__((noinline, no_icf)) +_Bool f1(const char *s) +{ + unsigned long len = __builtin_strlen (s); + _Bool ret = (len == 0); + return ret; +} + +/* Check CONVERT_EXPR's get properly handled. */ +__attribute__((noinline, no_icf)) +_Bool f2(const char *s) +{ + unsigned len = __builtin_strlen (s); + return len == 0; +} + +/* { dg-final { scan-tree-dump-times "strlen \\(" 0 "strlen" } } */ diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index 9d7b4df..54f8109 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -45,6 +45,7 @@ along with GCC; see the file COPYING3. If not see #include "ipa-chkp.h" #include "tree-hash-traits.h" #include "builtins.h" +#include "tree-pretty-print.h" /* A vector indexed by SSA_NAME_VERSION. 0 means unknown, positive value is an index into strinfo vector, negative value stands for @@ -2302,6 +2303,43 @@ strlen_optimize_stmt (gimple_stmt_iterator *gsi) else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR) handle_pointer_plus (gsi); } + /* strlen (s) eq/ne 0 -> *s eq/ne 0. */ + else if (TREE_CODE (lhs) == SSA_NAME && INTEGRAL_TYPE_P (TREE_TYPE (lhs))) + { + tree rhs2 = gimple_assign_rhs2 (stmt); + tree_code code = gimple_assign_rhs_code (stmt); + + if ((code == EQ_EXPR || code == NE_EXPR) && integer_zerop (rhs2)) + { + tree rhs1 = gimple_assign_rhs1 (stmt); + if (TREE_CODE (rhs1) == SSA_NAME) + { + gimple *def_stmt = SSA_NAME_DEF_STMT (rhs1); + if (is_a (def_stmt) + && (gimple_assign_rhs_code (def_stmt) == CONVERT_EXPR + || gimple_assign_rhs_code (def_stmt) == NOP_EXPR) + && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME) + def_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt)); + + if (gcall *call_stmt = dyn_cast (def_stmt)) + { + tree callee = gimple_call_fndecl (call_stmt); + if (valid_builtin_call (call_stmt) + && DECL_FUNCTION_CODE (callee) == BUILT_IN_STRLEN) + { + tree arg = gimple_call_arg (call_stmt, 0); + tree op = build2 (MEM_REF, char_type_node, arg, build_zero_cst (TREE_TYPE (arg))); + tree temp = make_temp_ssa_name (TREE_TYPE (op), NULL, "strlen"); + gimple *memref_stmt = gimple_build_assign (temp, op); + gimple_stmt_iterator call_gsi = gsi_for_stmt (call_stmt); + gsi_insert_before (&call_gsi, memref_stmt, GSI_SAME_STMT); + gassign *g = gimple_build_assign (gimple_call_lhs (call_stmt), CONVERT_EXPR, temp); + gsi_replace (&call_gsi, g, true); + } + } + } + } + } else if (TREE_CODE (lhs) != SSA_NAME && !TREE_SIDE_EFFECTS (lhs)) { tree type = TREE_TYPE (lhs); @@ -2505,7 +2543,7 @@ const pass_data pass_data_strlen = 0, /* properties_provided */ 0, /* properties_destroyed */ 0, /* todo_flags_start */ - 0, /* todo_flags_finish */ + TODO_update_ssa, /* todo_flags_finish */ }; class pass_strlen : public gimple_opt_pass