From patchwork Mon Feb 28 20:30:30 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 84864 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 CBC98B7122 for ; Tue, 1 Mar 2011 07:30:45 +1100 (EST) Received: (qmail 23232 invoked by alias); 28 Feb 2011 20:30:43 -0000 Received: (qmail 23103 invoked by uid 22791); 28 Feb 2011 20:30:41 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_CP, TW_FN, T_RP_MATCHES_RCVD 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; Mon, 28 Feb 2011 20:30:33 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p1SKUVRC019909 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 28 Feb 2011 15:30:31 -0500 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [10.16.42.4]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p1SKUVTu031580 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 28 Feb 2011 15:30:31 -0500 Received: from tyan-ft48-01.lab.bos.redhat.com (localhost.localdomain [127.0.0.1]) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4) with ESMTP id p1SKUU16025523 for ; Mon, 28 Feb 2011 21:30:30 +0100 Received: (from jakub@localhost) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4/Submit) id p1SKUUNZ025522 for gcc-patches@gcc.gnu.org; Mon, 28 Feb 2011 21:30:30 +0100 Date: Mon, 28 Feb 2011 21:30:30 +0100 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Subject: [4.7 PATCH] Optimize snprintf (buf, 5, "abcd") into memcpy (PR middle-end/47917) Message-ID: <20110228203030.GL30899@tyan-ft48-01.lab.bos.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline 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 Hi! This patch optimizes snprintf with constant second argument and format string without any % chars or "%s" if the second argument is known to be greater than strlen of the string. We could optimize also the case where it is smaller by emitting either strcpy from a modified string (but then we risk .rodata growth), or by doing memcpy on length - 1 followed by a single byte zero store at the end of buffer, but then it might grow code size, so for now I've left those cases out. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for 4.7? 2011-02-28 Jakub Jelinek PR middle-end/47917 * builtins.c (fold_builtin_snprintf): New function. (fold_builtin_3): Call it for BUILT_IN_SNPRINTF. (fold_builtin_4): Likewise. * gcc.c-torture/execute/pr47917.c: New test. * gcc.dg/pr47917.c: New test. Jakub --- gcc/builtins.c.jj 2011-02-26 16:02:49.000000000 +0100 +++ gcc/builtins.c 2011-02-28 14:00:04.000000000 +0100 @@ -191,6 +191,7 @@ static tree fold_builtin_strncat (locati static tree fold_builtin_strspn (location_t, tree, tree); static tree fold_builtin_strcspn (location_t, tree, tree); static tree fold_builtin_sprintf (location_t, tree, tree, tree, int); +static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int); static rtx expand_builtin_object_size (tree); static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode, @@ -10600,6 +10601,9 @@ fold_builtin_3 (location_t loc, tree fnd case BUILT_IN_SPRINTF: return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore); + case BUILT_IN_SNPRINTF: + return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore); + case BUILT_IN_STRCPY_CHK: case BUILT_IN_STPCPY_CHK: return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE, @@ -10665,6 +10669,9 @@ fold_builtin_4 (location_t loc, tree fnd case BUILT_IN_STRNCAT_CHK: return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3); + case BUILT_IN_SNPRINTF: + return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore); + case BUILT_IN_FPRINTF_CHK: case BUILT_IN_VFPRINTF_CHK: if (!validate_arg (arg1, INTEGER_TYPE) @@ -11923,6 +11930,127 @@ fold_builtin_sprintf (location_t loc, tr return call; } +/* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE, + FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't + attempt to simplify calls with more than 4 arguments. + + Return NULL_TREE if no simplification was possible, otherwise return the + simplified form of the call as a tree. If IGNORED is true, it means that + the caller does not use the returned value of the function. */ + +static tree +fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt, + tree orig, int ignored) +{ + tree call, retval; + const char *fmt_str = NULL; + unsigned HOST_WIDE_INT destlen; + + /* Verify the required arguments in the original call. We deal with two + types of snprintf() calls: 'snprintf (str, cst, fmt)' and + 'snprintf (dest, cst, "%s", orig)'. */ + if (!validate_arg (dest, POINTER_TYPE) + || !validate_arg (destsize, INTEGER_TYPE) + || !validate_arg (fmt, POINTER_TYPE)) + return NULL_TREE; + if (orig && !validate_arg (orig, POINTER_TYPE)) + return NULL_TREE; + + if (!host_integerp (destsize, 1)) + return NULL_TREE; + + /* Check whether the format is a literal string constant. */ + fmt_str = c_getstr (fmt); + if (fmt_str == NULL) + return NULL_TREE; + + call = NULL_TREE; + retval = NULL_TREE; + + if (!init_target_chars ()) + return NULL_TREE; + + destlen = tree_low_cst (destsize, 1); + + /* If the format doesn't contain % args or %%, use strcpy. */ + if (strchr (fmt_str, target_percent) == NULL) + { + tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; + size_t len = strlen (fmt_str); + + /* Don't optimize snprintf (buf, 4, "abc", ptr++). */ + if (orig) + return NULL_TREE; + + /* We could expand this as + memcpy (str, fmt, cst - 1); str[cst - 1] = '\0'; + or to + memcpy (str, fmt_with_nul_at_cstm1, cst); + but in the former case that might increase code size + and in the latter case grow .rodata section too much. + So punt for now. */ + if (len >= destlen) + return NULL_TREE; + + if (!fn) + return NULL_TREE; + + /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when + 'format' is known to contain no % formats and + strlen (fmt) < cst. */ + call = build_call_expr_loc (loc, fn, 2, dest, fmt); + + if (!ignored) + retval = build_int_cst (NULL_TREE, strlen (fmt_str)); + } + + /* If the format is "%s", use strcpy if the result isn't used. */ + else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) + { + tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; + unsigned HOST_WIDE_INT origlen; + + /* Don't crash on snprintf (str1, cst, "%s"). */ + if (!orig) + return NULL_TREE; + + retval = c_strlen (orig, 1); + if (!retval || !host_integerp (retval, 1)) + return NULL_TREE; + + origlen = tree_low_cst (retval, 1); + /* We could expand this as + memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0'; + or to + memcpy (str1, str2_with_nul_at_cstm1, cst); + but in the former case that might increase code size + and in the latter case grow .rodata section too much. + So punt for now. */ + if (origlen >= destlen) + return NULL_TREE; + + /* Convert snprintf (str1, cst, "%s", str2) into + strcpy (str1, str2) if strlen (str2) < cst. */ + if (!fn) + return NULL_TREE; + + call = build_call_expr_loc (loc, fn, 2, dest, orig); + + if (ignored) + retval = NULL_TREE; + } + + if (call && retval) + { + retval = fold_convert_loc + (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SNPRINTF])), + retval); + return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval); + } + else + return call; +} + /* Expand a call EXP to __builtin_object_size. */ rtx --- gcc/testsuite/gcc.c-torture/execute/pr47917.c.jj 2011-02-28 14:04:39.000000000 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr47917.c 2011-02-28 14:01:05.000000000 +0100 @@ -0,0 +1,32 @@ +/* PR middle-end/47917 */ + +extern int snprintf (char *, __SIZE_TYPE__, const char *, ...); +extern int memcmp (const void *, const void *, __SIZE_TYPE__); +extern void abort (void); + +char buf1[6], buf2[6], buf3[4], buf4[4]; +int i; + +int +foo (void) +{ + int ret = snprintf (buf1, sizeof buf1, "abcde"); + ret += snprintf (buf2, sizeof buf2, "abcdef") * 16; + ret += snprintf (buf3, sizeof buf3, "%s", i++ < 6 ? "abc" : "def") * 256; + ret += snprintf (buf4, sizeof buf4, "%s", i++ > 10 ? "abcde" : "defgh") * 4096; + return ret; +} + +int +main (void) +{ + if (foo () != 5 + 6 * 16 + 3 * 256 + 5 * 4096) + abort (); + if (memcmp (buf1, "abcde", 6) != 0 + || memcmp (buf2, "abcde", 6) != 0 + || memcmp (buf3, "abc", 4) != 0 + || memcmp (buf4, "def", 4) != 0 + || i != 2) + abort (); + return 0; +} --- gcc/testsuite/gcc.dg/pr47917.c.jj 2011-02-28 14:04:58.000000000 +0100 +++ gcc/testsuite/gcc.dg/pr47917.c 2011-02-28 14:07:22.000000000 +0100 @@ -0,0 +1,38 @@ +/* PR middle-end/47917 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +extern int snprintf (char *, __SIZE_TYPE__, const char *, ...); +extern int memcmp (const void *, const void *, __SIZE_TYPE__); +extern void abort (void); + +char buf1[6], buf2[6], buf3[4], buf4[4]; +int i; + +int +foo (void) +{ + int ret = snprintf (buf1, sizeof buf1, "abcde"); + ret += snprintf (buf2, sizeof buf2, "abcdef") * 16; + ret += snprintf (buf3, sizeof buf3, "%s", i++ < 6 ? "abc" : "def") * 256; + ret += snprintf (buf4, sizeof buf4, "%s", i++ > 10 ? "abcde" : "defgh") * 4096; + return ret; +} + +int +main (void) +{ + if (foo () != 5 + 6 * 16 + 3 * 256 + 5 * 4096) + abort (); + if (memcmp (buf1, "abcde", 6) != 0 + || memcmp (buf2, "abcde", 6) != 0 + || memcmp (buf3, "abc", 4) != 0 + || memcmp (buf4, "def", 4) != 0 + || i != 2) + abort (); + return 0; +} + +/* { dg-final { scan-tree-dump-times "snprintf" 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "sprintf" 0 "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */