From patchwork Wed Jan 10 18:42:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 124127 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp5618267qgn; Wed, 10 Jan 2018 10:42:47 -0800 (PST) X-Google-Smtp-Source: ACJfBov/ccWq6IORc3U6kDCDWMuDWTmZiQjyELkOvkB3poF2oQPoeJIzoTwWixXt4C2GEONmndhw X-Received: by 10.159.204.146 with SMTP id t18mr12989540plo.370.1515609767582; Wed, 10 Jan 2018 10:42:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515609767; cv=none; d=google.com; s=arc-20160816; b=SueuHMwOvsmZCxOCG/sjykOzY+7va/rZHQalkROV/yMjpvTUnoXDU9lSf72s/BTvN8 0i3UgYkClTLGrm60C1ZGikXBtEevpoaFCCLz+hUGM3KOs057C6w2nfzqm2GMVYzl68nt H/cIIseFU6NzEEaNgdP43Y0sROKhhstuh1StqSqTuPYtu3X8FDy9zgpMvwaJJkbm5gUM UBT4goUsEVOHWEwXzyibbG8QUCE5RLTBE/vDpFOxhiUw7QL2mvzwA80d/fQXUSJam3BC CFgtT8SN7x9VJLJJzib1xzfj1mrgyA8AdQ9UgtXehoB/STtKcK5w7OXNpcqt95EbY2E6 AQtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:subject:message-id:date:from:mime-version:delivered-to:sender :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mailing-list:dkim-signature:domainkey-signature :arc-authentication-results; bh=98Aqb0A1hyL/dOT23gys37umByn/diO/PMnFNeBlr7c=; b=aIShxQricQBDiQgm/c6jg3RtqDgQzcK+196u315VnyWBJBQDfYGDL8gb3auWQhNJzD 1f3QGkfWqYPl1FOinfXKUS/zrPeH/bL82f1JKfL7lIN8LcpYlCbciSv5CbqJh7HvtXPY 8o9ey2b5ADGFJ/CgxfW1wFo8b+l0nTyaca22k5g+xg92tx+Wk6YTrmxlSvf58SyYSCOh BQv1mcO2kQ3YgDTOyVP3gGCWNZqfX1n+dblnAFFYOSGFdAM/3ZEWTxFZcBsHbyI5XMs7 yddnzdgz4eOc42tGf4n/kfzJ7QUpynMaQF2IUeXLMJt3C2ZuJ7nt2UBTEUwdK0iqPDxj c9Dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JV3djRiY; spf=pass (google.com: domain of gcc-patches-return-470718-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-470718-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id i1si10911930pgt.556.2018.01.10.10.42.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Jan 2018 10:42:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-470718-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 header.s=default header.b=JV3djRiY; spf=pass (google.com: domain of gcc-patches-return-470718-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-470718-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=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=j50Jetg+McLGh6aGyO+Co6fhuEw1NIlwriB5YuOSQHtQYk 8Ry8+akKnN4BuxLq5X0L+UVbMSnQX6pZsKe08X9swyoFL94MevrfR7isNZ5uFG/M 12KqJkkeNEnayuWpAuhKhod4RMTIXlCzMWkbvnD6J3SANaz/vfDni+dN2kwN8= 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=sG9EMswphQReu0Cl5OdEYGALrvI=; b=JV3djRiYUvSNvzQ40i2O m2T5myQZie5SaWF1lmbK0Wa9LAMbmNFdkHrPuWd65pbUkKH7HsGGT2A8vlTVchcn BgeoOwxHBvBCyrOIMxXqNvzH2eZM/GJbj33ktN5SiGHJIZfddMicVYdQxjhqz3QX /nMs/b6eByPe9ZdJACVBhTs= Received: (qmail 31267 invoked by alias); 10 Jan 2018 18:42:36 -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 31256 invoked by uid 89); 10 Jan 2018 18:42:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=considerable X-HELO: mail-wr0-f175.google.com Received: from mail-wr0-f175.google.com (HELO mail-wr0-f175.google.com) (209.85.128.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 10 Jan 2018 18:42:33 +0000 Received: by mail-wr0-f175.google.com with SMTP id z48so12603448wrz.6 for ; Wed, 10 Jan 2018 10:42:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=C+D6IiCA5ewjqbr6YDtg0+7x1CdLEHhhaLn1b4KHwE8=; b=q4SI7+1GUCD0lam//LOtRZDnY4LFS0DOi95XuEqgI32CEM5S1U1zWrggpdAU8RzboP GxbLiPUvfiMPniWvjpUci3x38OlCqKG0TRwpttilou7OSmtcmS5PCWMWvntMFs0seber 34h+2F/M2EtiajXyZ/5MQFOGc+XHcdg2phy2cIQ5FRjj+SpI9BzQgRpsunUDsckhzRPn LcveSW4WxGQo1Kiw7z6gDlfaz691JyouS1Zd3qkxRdoz9GkUZj0VEXRbgrWKu/InIg4v 22ini/OWzVYaDdO8htMzleTNluB5fBEC3SYjIqYlhz3st+7NqLQbKqDnBJLaMxdBQ1ri GBwA== X-Gm-Message-State: AKGB3mJ19ILVRzSJzcvSAbCnlxdkP3bveJ/SuNc0DwbF72vqDBhaXERd 8rifG9fccu+zO9ZNVUM/UNDzda0CYFdVe3Y+g/AcDKNwPKM= X-Received: by 10.223.162.195 with SMTP id t3mr16919930wra.237.1515609751605; Wed, 10 Jan 2018 10:42:31 -0800 (PST) MIME-Version: 1.0 Received: by 10.223.179.81 with HTTP; Wed, 10 Jan 2018 10:42:30 -0800 (PST) From: Prathamesh Kulkarni Date: Thu, 11 Jan 2018 00:12:30 +0530 Message-ID: Subject: PR81703 and Martin's fix for PR83501 To: gcc Patches , Martin Sebor X-IsSubscribed: yes Hi, I have attached patch for PR81703 rebased on Martin's fix for PR83501 posted here since both had considerable overlaps: https://gcc.gnu.org/ml/gcc-patches/2018-01/msg00180.html The patch passes bootstrap+test on x86_64-unknown-linux-gnu and cross-tested on aarch64-*-*. Currently it fails to pass validation on arm targets because of PR83775. Does it look OK? Thanks, Prathamesh 2018-10-01 Martin Sebor Prathamesh Kulkarni PR tree-optimization/83501 PR tree-optimization/81703 * tree-ssa-strlen.c (get_string_cst): Rename... (get_string_len): ...to this. Handle global constants. (handle_char_store): Adjust. testsuite/ * gcc.dg/strlenopt-39.c: New test-case. * gcc.dg/pr81703.c: Likewise. diff --git a/gcc/testsuite/gcc.dg/pr81703.c b/gcc/testsuite/gcc.dg/pr81703.c new file mode 100644 index 00000000000..190f4a833dd --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr81703.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-strlen" } */ + +unsigned g (void) +{ + char d[8]; + const char s[] = "0123"; + __builtin_memcpy (d, s, sizeof s); + return __builtin_strlen (d); +} + +/* { dg-final { scan-tree-dump-not "__builtin_strlen" "strlen" } } */ diff --git a/gcc/testsuite/gcc.dg/strlenopt-39.c b/gcc/testsuite/gcc.dg/strlenopt-39.c new file mode 100644 index 00000000000..a4177c918ad --- /dev/null +++ b/gcc/testsuite/gcc.dg/strlenopt-39.c @@ -0,0 +1,66 @@ +/* PR tree-optimization/83444 + { dg-do compile } + { dg-options "-O2 -fdump-tree-optimized" } */ + +#include "strlenopt.h" + +#define STR "1234567" + +const char str[] = STR; + +char dst[10]; + +void copy_from_global_str (void) +{ + strcpy (dst, str); + + if (strlen (dst) != sizeof str - 1) + abort (); +} + +void copy_from_local_str (void) +{ + const char s[] = STR; + + strcpy (dst, s); + + if (strlen (dst) != sizeof s - 1) + abort (); +} + +void copy_from_local_memstr (void) +{ + struct { + char s[sizeof STR]; + } x = { STR }; + + strcpy (dst, x.s); + + if (strlen (dst) != sizeof x.s - 1) + abort (); +} + +void copy_to_local_str (void) +{ + char d[sizeof STR]; + + strcpy (d, str); + + if (strlen (d) != sizeof str - 1) + abort (); +} + +void copy_to_local_memstr (void) +{ + struct { + char d[sizeof STR]; + } x; + + strcpy (x.d, str); + + if (strlen (x.d) != sizeof str- 1) + abort (); +} + +/* Verify that all calls to strlen have been eliminated. + { dg-final { scan-tree-dump-not "(abort|strlen) \\(\\)" "optimized" } } */ diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index aae242d93d6..4e363278ea2 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -2773,18 +2773,40 @@ handle_pointer_plus (gimple_stmt_iterator *gsi) } /* Check if RHS is string_cst possibly wrapped by mem_ref. */ -static tree -get_string_cst (tree rhs) +static int +get_string_len (tree rhs) { if (TREE_CODE (rhs) == MEM_REF && integer_zerop (TREE_OPERAND (rhs, 1))) { - rhs = TREE_OPERAND (rhs, 0); + tree rhs_addr = rhs = TREE_OPERAND (rhs, 0); if (TREE_CODE (rhs) == ADDR_EXPR) - rhs = TREE_OPERAND (rhs, 0); + { + rhs = TREE_OPERAND (rhs, 0); + if (TREE_CODE (rhs) != STRING_CST) + { + int idx = get_stridx (rhs_addr); + if (idx > 0) + { + strinfo *si = get_strinfo (idx); + if (si && si->full_string_p) + return tree_to_shwi (si->nonzero_chars); + } + } + } } - return (TREE_CODE (rhs) == STRING_CST) ? rhs : NULL_TREE; + if (TREE_CODE (rhs) == VAR_DECL + && TREE_READONLY (rhs)) + rhs = DECL_INITIAL (rhs); + + if (rhs && TREE_CODE (rhs) == STRING_CST) + { + unsigned HOST_WIDE_INT ilen = strlen (TREE_STRING_POINTER (rhs)); + return ilen <= INT_MAX ? ilen : -1; + } + + return -1; } /* Handle a single character store. */ @@ -2799,6 +2821,9 @@ handle_char_store (gimple_stmt_iterator *gsi) tree rhs = gimple_assign_rhs1 (stmt); unsigned HOST_WIDE_INT offset = 0; + /* Set to the length of the string being assigned if known. */ + int rhslen; + if (TREE_CODE (lhs) == MEM_REF && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME) { @@ -2942,19 +2967,18 @@ handle_char_store (gimple_stmt_iterator *gsi) } } else if (idx == 0 - && (rhs = get_string_cst (gimple_assign_rhs1 (stmt))) + && (rhslen = get_string_len (gimple_assign_rhs1 (stmt))) >= 0 && ssaname == NULL_TREE && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE) { - size_t l = strlen (TREE_STRING_POINTER (rhs)); HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs)); - if (a > 0 && (unsigned HOST_WIDE_INT) a > l) + if (a > 0 && (unsigned HOST_WIDE_INT) a > (unsigned HOST_WIDE_INT) rhslen) { int idx = new_addr_stridx (lhs); if (idx != 0) { si = new_strinfo (build_fold_addr_expr (lhs), idx, - build_int_cst (size_type_node, l), true); + build_int_cst (size_type_node, rhslen), true); set_strinfo (idx, si); si->dont_invalidate = true; }