From patchwork Mon Dec 5 16:54:48 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 702791 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tXW8l6Bg4z9t0Z for ; Tue, 6 Dec 2016 03:55:19 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="UZpGixG9"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=EFQUX/VWYI/jgbCVN2D/jmE3uZtEb IMHyMoBiIGxkxJGzjEoDSIZ64IgCAwfN8tVgWAHtCJ5syvcYT0m3fIbf5+B5jDlo E/IfOun8/pGJp5r1Z/8sZYUvWTc4KrEab7ZcE6VX2+ddDbx5pf4/gmnHM2+IXCFF DlmAduiJbZMjB0= 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:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=Yws94Lhfn58g7m9Q62jCFF+/01A=; b=UZp GixG93fUwm7nBhMb1GBhx5SDI5NnuDHq75aaKLnEJf7RUuAUYBy8bfsjEeKPW3Co deueHamL5hHXfn3AyCoLeZHl8c1GwjgJ5hVjfnAyyYkYi3uDzQpHL/Abr/KxYz8m rff8ok7CgUvr4nhlDMVgeCvqLxlbOpKLoKtmJ9mc= Received: (qmail 31824 invoked by alias); 5 Dec 2016 16:54:56 -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 31746 invoked by uid 89); 5 Dec 2016 16:54:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.8 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=folks X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 05 Dec 2016 16:54:53 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9069281253; Mon, 5 Dec 2016 16:54:52 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-204-100.brq.redhat.com [10.40.204.100]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uB5Gsol5016807 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 5 Dec 2016 11:54:51 -0500 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id uB5GsnMU009420; Mon, 5 Dec 2016 17:54:49 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id uB5GsmPq009419; Mon, 5 Dec 2016 17:54:48 +0100 Date: Mon, 5 Dec 2016 17:54:48 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Readd strchr constant folding (PR c++/71537) Message-ID: <20161205165448.GO3541@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-IsSubscribed: yes Hi! The recent changes to move strchr folding from builtins.c to gimple-fold.c broke constexpr handling with __builtin_strchr etc. (which the libstdc++ folks want to use). Fixed by handling it also in fold-const-call.c. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2016-12-05 Jakub Jelinek PR c++/71537 * fold-const-call.c (fold_const_call): Handle CFN_BUILT_IN_{INDEX,STRCHR,RINDEX,STRRCHR}. * g++.dg/cpp0x/constexpr-strchr.C: New test. Jakub --- gcc/fold-const-call.c.jj 2016-11-09 18:54:03.000000000 +0100 +++ gcc/fold-const-call.c 2016-12-05 12:53:38.597090946 +0100 @@ -1383,6 +1383,7 @@ tree fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1) { const char *p0, *p1; + char c; switch (fn) { case CFN_BUILT_IN_STRSPN: @@ -1409,6 +1410,30 @@ fold_const_call (combined_fn fn, tree ty } return NULL_TREE; + case CFN_BUILT_IN_INDEX: + case CFN_BUILT_IN_STRCHR: + if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c)) + { + const char *r = strchr (p0, c); + if (r == NULL) + return build_int_cst (type, 0); + return fold_convert (type, + fold_build_pointer_plus_hwi (arg0, r - p0)); + } + return NULL_TREE; + + case CFN_BUILT_IN_RINDEX: + case CFN_BUILT_IN_STRRCHR: + if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c)) + { + const char *r = strrchr (p0, c); + if (r == NULL) + return build_int_cst (type, 0); + return fold_convert (type, + fold_build_pointer_plus_hwi (arg0, r - p0)); + } + return NULL_TREE; + default: return fold_const_call_1 (fn, type, arg0, arg1); } --- gcc/testsuite/g++.dg/cpp0x/constexpr-strchr.C.jj 2016-12-05 13:00:19.448101292 +0100 +++ gcc/testsuite/g++.dg/cpp0x/constexpr-strchr.C 2016-12-05 13:00:36.270888812 +0100 @@ -0,0 +1,27 @@ +// { dg-do compile { target c++11 } } + +constexpr const char *f1 (const char *p, int q) { return __builtin_strchr (p, q); } +constexpr const char *f2 (const char *p, int q) { return __builtin_index (p, q); } +constexpr const char *f3 (const char *p, int q) { return __builtin_strrchr (p, q); } +constexpr const char *f4 (const char *p, int q) { return __builtin_rindex (p, q); } +constexpr const char a[] = "abcdefedcba"; +static_assert (f1 ("abcde", 'f') == nullptr, ""); +static_assert (f1 (a, 'g') == nullptr, ""); +static_assert (f1 (a, 'f') == a + 5, ""); +static_assert (f1 (a, 'c') == a + 2, ""); +static_assert (f1 (a, '\0') == a + 11, ""); +static_assert (f2 ("abcde", 'f') == nullptr, ""); +static_assert (f2 (a, 'g') == nullptr, ""); +static_assert (f2 (a, 'f') == a + 5, ""); +static_assert (f2 (a, 'c') == a + 2, ""); +static_assert (f2 (a, '\0') == a + 11, ""); +static_assert (f3 ("abcde", 'f') == nullptr, ""); +static_assert (f3 (a, 'g') == nullptr, ""); +static_assert (f3 (a, 'f') == a + 5, ""); +static_assert (f3 (a, 'c') == a + 8, ""); +static_assert (f3 (a, '\0') == a + 11, ""); +static_assert (f4 ("abcde", 'f') == nullptr, ""); +static_assert (f4 (a, 'g') == nullptr, ""); +static_assert (f4 (a, 'f') == a + 5, ""); +static_assert (f4 (a, 'c') == a + 8, ""); +static_assert (f4 (a, '\0') == a + 11, "");