From patchwork Thu Oct 27 07:37:42 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 122077 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 4DE22100813 for ; Thu, 27 Oct 2011 18:38:17 +1100 (EST) Received: (qmail 6272 invoked by alias); 27 Oct 2011 07:38:12 -0000 Received: (qmail 6122 invoked by uid 22791); 27 Oct 2011 07:38:10 -0000 X-SWARE-Spam-Status: No, hits=-6.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 27 Oct 2011 07:37:45 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p9R7bipP012857 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 27 Oct 2011 03:37:44 -0400 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [10.16.42.4]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p9R7bh7M017862 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 27 Oct 2011 03:37:44 -0400 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [127.0.0.1]) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4) with ESMTP id p9R7bhwk018805; Thu, 27 Oct 2011 09:37:43 +0200 Received: (from jakub@localhost) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4/Submit) id p9R7bgsB018803; Thu, 27 Oct 2011 09:37:42 +0200 Date: Thu, 27 Oct 2011 09:37:42 +0200 From: Jakub Jelinek To: Ian Lance Taylor Cc: gcc-patches@gcc.gnu.org Subject: Re: tree-ssa-strlen vs. zero-length strings Message-ID: <20111027073742.GI1052@tyan-ft48-01.lab.bos.redhat.com> Reply-To: Jakub Jelinek References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes 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 On Wed, Oct 26, 2011 at 10:22:21PM -0700, Ian Lance Taylor wrote: > I don't think it is possible to trigger this using C because I don't > think it is possible to have a zero-length STRING_CST in C. Ugh. I think the following should work, after all it will be tiny more efficient, I chose to use c_strlen just to avoid some code duplication. c_strlen will return even variable lengths etc. and we are only interested in constant string lengths, etc. If c_strlen is only called in places which expect zero-terminated strings, it will be fine. 2011-10-27 Jakub Jelinek * tree-ssa-strlen.c: Include expr.h. (get_stridx): Don't use c_strlen, instead use string_constant and compute string length from it. * Makefile.in (tree-ssa-strlen.o): Depend on $(EXPR_H). Jakub --- gcc/tree-ssa-strlen.c.jj 2011-10-26 14:19:11.000000000 +0200 +++ gcc/tree-ssa-strlen.c 2011-10-27 08:55:39.000000000 +0200 @@ -28,6 +28,7 @@ along with GCC; see the file COPYING3. #include "tree-ssa-propagate.h" #include "gimple-pretty-print.h" #include "params.h" +#include "expr.h" /* A vector indexed by SSA_NAME_VERSION. 0 means unknown, positive value is an index into strinfo vector, negative value stands for @@ -176,7 +177,7 @@ get_addr_stridx (tree exp) static int get_stridx (tree exp) { - tree l; + tree s, o; if (TREE_CODE (exp) == SSA_NAME) return VEC_index (int, ssa_ver_to_stridx, SSA_NAME_VERSION (exp)); @@ -188,14 +189,17 @@ get_stridx (tree exp) return idx; } - l = c_strlen (exp, 0); - if (l != NULL_TREE - && host_integerp (l, 1)) - { - unsigned HOST_WIDE_INT len = tree_low_cst (l, 1); - if (len == (unsigned int) len - && (int) len >= 0) - return ~(int) len; + s = string_constant (exp, &o); + if (s != NULL_TREE + && (o == NULL_TREE || host_integerp (o, 0)) + && TREE_STRING_LENGTH (s) > 0) + { + HOST_WIDE_INT offset = o ? tree_low_cst (o, 0) : 0; + const char *p = TREE_STRING_POINTER (s); + int max = TREE_STRING_LENGTH (s) - 1; + + if (p[max] == '\0' && offset >= 0 && offset <= max) + return ~(int) strlen (p + offset); } return 0; } --- gcc/Makefile.in.jj 2011-10-26 14:19:11.000000000 +0200 +++ gcc/Makefile.in 2011-10-27 08:54:10.000000000 +0200 @@ -3173,7 +3173,7 @@ tree-ssa-ccp.o : tree-ssa-ccp.c $(TREE_F $(DBGCNT_H) tree-pretty-print.h gimple-pretty-print.h gimple-fold.h tree-ssa-strlen.o : tree-ssa-strlen.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \ $(TREE_FLOW_H) $(TREE_PASS_H) domwalk.h alloc-pool.h tree-ssa-propagate.h \ - gimple-pretty-print.h $(PARAMS_H) + gimple-pretty-print.h $(PARAMS_H) $(EXPR_H) tree-sra.o : tree-sra.c $(CONFIG_H) $(SYSTEM_H) coretypes.h alloc-pool.h \ $(TM_H) $(TREE_H) $(GIMPLE_H) $(CGRAPH_H) $(TREE_FLOW_H) \ $(IPA_PROP_H) $(DIAGNOSTIC_H) statistics.h $(TREE_DUMP_H) $(TIMEVAR_H) \