From patchwork Wed Nov 9 13:56:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Martin_Li=C5=A1ka?= X-Patchwork-Id: 81488 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp208725qge; Wed, 9 Nov 2016 05:56:56 -0800 (PST) X-Received: by 10.98.130.1 with SMTP id w1mr11635377pfd.35.1478699816514; Wed, 09 Nov 2016 05:56:56 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id t27si17063419pfa.146.2016.11.09.05.56.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Nov 2016 05:56:56 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-440850-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-440850-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-440850-patch=linaro.org@gcc.gnu.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=QXjD21QH/AJxk5DtJQcRQDyy2B5pe6XFBf9D3QakBTY16hpS8K UnJ1vm8j5xBpa3SUgor9C46nk2IVo8zOEaDlh9FocSiWYAnrrC6wPMe2B7SLpDKf CPW/9k/pn4n7Q3C/6fF0pU+3qwag3cFnUPbz7wSRV1wSE6g+ixgXhU/1o= 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:to:cc :from:subject:message-id:date:mime-version:content-type; s= default; bh=M3k8GWpJ0wlFGN7LQ2QeWFGdclA=; b=CQgkB4uj2ZqvoKCJeXHl nlhqnF/5Jxn5MHQhj15dmy/L1qIxk2fdh1O12fSDFdizahwbw2iMY1Uk3dLj7ObA f/WpIW3idmzNuH8gtufNgaikoEznG+Np+9FM4T6F022lg6aVgwlBvThXtIqksSlo X4r2UhCDRrf7i2/MZrtVVg4= Received: (qmail 80119 invoked by alias); 9 Nov 2016 13:56:42 -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 77940 invoked by uid 89); 9 Nov 2016 13:56:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, SPF_PASS autolearn=ham version=3.3.2 spammy=1584, UD:builtins-folding-gimple.c, 1516, sk:283bd1c X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 09 Nov 2016 13:56:30 +0000 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id D7A95ADB6; Wed, 9 Nov 2016 13:56:27 +0000 (UTC) To: GCC Patches Cc: Richard Biener , Martin Sebor From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Subject: [PATCH] Fix folding of memcmp("a", "a", 2) (PR, tree-optimization/78257) Message-ID: <337a60ae-161d-17eb-e267-cdb8d343cd87@suse.cz> Date: Wed, 9 Nov 2016 14:56:27 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 X-IsSubscribed: yes Hello. Following patch fixes [1] (f0 function), where we have off-by-one error. Patch can bootstrap on ppc64le-redhat-linux and survives regression tests. Ready to be installed? Martin [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257#c0 >From d7c46e5eb4d295d7653eae188cae04a8f7f0719f Mon Sep 17 00:00:00 2001 From: marxin Date: Wed, 9 Nov 2016 10:33:43 +0100 Subject: [PATCH] Fix folding of memcmp("a", "a", 2) (PR tree-optimization/78257) gcc/ChangeLog: 2016-11-09 Martin Liska * fold-const-call.c (fold_const_call): Fix the folding. gcc/testsuite/ChangeLog: 2016-11-09 Martin Liska * gcc.dg/tree-ssa/builtins-folding-generic.c (main): Add new test-case for memcmp. * gcc.dg/tree-ssa/builtins-folding-gimple.c: Likewise. --- gcc/fold-const-call.c | 10 +++++----- gcc/gimplify.c | 1 - gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-generic.c | 5 +++++ gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-gimple.c | 6 ++++++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/gcc/fold-const-call.c b/gcc/fold-const-call.c index 1b3a755..38f9717 100644 --- a/gcc/fold-const-call.c +++ b/gcc/fold-const-call.c @@ -1506,7 +1506,7 @@ tree fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2) { const char *p0, *p1; - size_t s2 = 0; + size_t s0, s1, s2 = 0; switch (fn) { case CFN_BUILT_IN_STRNCMP: @@ -1538,11 +1538,11 @@ fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2) } case CFN_BUILT_IN_BCMP: case CFN_BUILT_IN_MEMCMP: - if ((p0 = c_getstr (arg0)) - && (p1 = c_getstr (arg1)) + if ((p0 = c_getstr (arg0, &s0)) + && (p1 = c_getstr (arg1, &s1)) && host_size_t_cst_p (arg2, &s2) - && s2 <= strlen (p0) - && s2 <= strlen (p1)) + && s2 <= s0 + && s2 <= s1) return build_cmp_result (type, memcmp (p0, p1, s2)); return NULL_TREE; diff --git a/gcc/gimplify.c b/gcc/gimplify.c index e5930e6..15976e2 100644 --- a/gcc/gimplify.c +++ b/gcc/gimplify.c @@ -2266,7 +2266,6 @@ gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p) labels = gimplify_ctxp->case_labels; gimplify_ctxp->case_labels = saved_labels; - gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0); delete gimplify_ctxp->live_switch_vars; gimplify_ctxp->live_switch_vars = saved_live_switch_vars; diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-generic.c b/gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-generic.c index 175feff..3c7e001 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-generic.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-generic.c @@ -66,6 +66,10 @@ main (void) if (__builtin_strncasecmp ("aab", "aac", 2) != 0) __builtin_abort (); + /* MEMCMP. */ + if (__builtin_memcmp ("aaaaa", "aaaaa", 6) != 0) + __builtin_abort (); + return 0; } @@ -74,3 +78,4 @@ main (void) /* { dg-final { scan-tree-dump-not "__builtin_strncmp" "original" } } */ /* { dg-final { scan-tree-dump-not "__builtin_strncasecmp" "original" } } */ /* { dg-final { scan-tree-dump-not "__builtin_memchr" "original" } } */ +/* { dg-final { scan-tree-dump-not "__builtin_memcmp" "original" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-gimple.c b/gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-gimple.c index 283bd1c..d08ced5 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-gimple.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-gimple.c @@ -63,6 +63,7 @@ main (void) const char *aaaaa = "aaaaa"; const char *hello = "hello"; + const char *hello2 = "hello"; const char *empty = ""; const char *ab = "ab"; const char *ba = "ba"; @@ -151,6 +152,10 @@ main (void) if (__builtin_strncasecmp (++s2, ++s3+2, 0) != 0 || s2 != s1+1 || s3 != s1+5) __builtin_abort(); + /* MEMCMP. */ + if (__builtin_memcmp (hello, hello2, 6) != 0) + __builtin_abort (); + return 0; } @@ -158,4 +163,5 @@ main (void) /* { dg-final { scan-tree-dump-not "__builtin_strcasecmp" "optimized" } } */ /* { dg-final { scan-tree-dump-not "__builtin_strncmp" "optimized" } } */ /* { dg-final { scan-tree-dump-not "__builtin_memchr" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "__builtin_memcmp" "optimized" } } */ /* { dg-final { scan-tree-dump-times "__builtin_strncasecmp" 3 "optimized" } } */ -- 2.10.1