From patchwork Sat Jan 11 05:03:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1221532 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-517208-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=tVcW1nx9; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Lg2v84Nn; dkim-atps=neutral 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 47vnn0282wz9sP6 for ; Sat, 11 Jan 2020 16:03:30 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:content-type :content-transfer-encoding; q=dns; s=default; b=c4T2lC1Dxlv1ZUfZ rfaVMV9zT+WDCXyA9zn5M9yuBmn3Q3nnijZt3/2PvK9NtKxin74KGeXNsdXr/CG0 /W7VDMqzHdGfbEtojkcsQeyOWcjyXshkfCMiOthYNiSFBmWyQx+mffS98LdvwGgr 3D33j3S8jzmdfEqhmCNc3JYCEVA= 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:from :to:cc:subject:date:message-id:content-type :content-transfer-encoding; s=default; bh=QUkCFDuhVYyBh0jW9CH9Ju kVLiA=; b=tVcW1nx9ccn/NjKcmqWe1gwzT/ZjLANm04SnfZgYihb2IKg4pB9cy7 6vTGP6xuizf+thGovj+EsVgmu2qvP1IvK+ylbpFlf+oWkd4y9NlNaDnBMIcsmyQJ 9ovqzkJWWrUWAkQxpS9dMTl82/ZqpRdKdJLkozk+fCbA3C1jI8mQA= Received: (qmail 128417 invoked by alias); 11 Jan 2020 05:03:22 -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 128408 invoked by uid 89); 11 Jan 2020 05:03:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=sk:BUILT_I, sk:built_i X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (207.211.31.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 11 Jan 2020 05:03:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1578718998; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AiEUNjYTszKLs8NNFoHecX6VZVa7mk+vrksFjNl2+ao=; b=Lg2v84NnFHuHoDE4zKisaYVtHNjlOZAdLi6ywt8loohw/409JkRvq0qzh8vXHZx/Hv93Im chvKvzQD0+0yHuLZV8h8sCMTNjhiBbqjkSMPd6lLLT+c3UFI92CxQuw7V8FDaJVBivUZtE 9sVvWsGUK4qhbt2kPwx2M/67wabtETs= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-369-M7Qov92IPIC-HHCKLheWTQ-1; Sat, 11 Jan 2020 00:03:17 -0500 Received: by mail-qv1-f72.google.com with SMTP id e10so2556762qvq.18 for ; Fri, 10 Jan 2020 21:03:17 -0800 (PST) Received: from barrymore.redhat.com (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id q20sm2151919qtl.82.2020.01.10.21.03.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2020 21:03:15 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Cc: jwakely@redhat.com Subject: [C++ PATCH RFC] PR c++/80265 - constexpr __builtin_mem*. Date: Sat, 11 Jan 2020 00:03:14 -0500 Message-Id: <20200111050314.27660-1-jason@redhat.com> X-Mimecast-Spam-Score: 0 X-IsSubscribed: yes The library has already worked around this issue, but I was curious about why it wasn't working. The answer: because we were passing &var to fold, which doesn't know about the constexpr values hash table. Fixed by passing &"str" instead. Tested x86_64-pc-linux-gnu. Does this seem useful even though it isn't necessary anymore? * constexpr.c (cxx_eval_builtin_function_call): Expose STRING_CST to str/mem builtins. --- gcc/cp/constexpr.c | 70 +++++++++++++++++-- gcc/testsuite/g++.dg/ext/constexpr-builtin1.C | 37 ++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/constexpr-builtin1.C base-commit: d3cf980217ce13b1eda01d4c42a7e3afd2bf3217 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 3ca3b9ecd65..3a9fb566a52 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1260,28 +1260,76 @@ cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND)) return fold_builtin_source_location (EXPR_LOCATION (t)); + int strops = 0; + int strret = 0; + if (fndecl_built_in_p (fun, BUILT_IN_NORMAL)) + switch (DECL_FUNCTION_CODE (fun)) + { + case BUILT_IN_STRLEN: + case BUILT_IN_STRNLEN: + strops = 1; + break; + case BUILT_IN_MEMCHR: + case BUILT_IN_STRCHR: + case BUILT_IN_STRRCHR: + strops = 1; + strret = 1; + break; + case BUILT_IN_MEMCMP: + case BUILT_IN_STRCMP: + strops = 2; + break; + case BUILT_IN_STRSTR: + strops = 2; + strret = 1; + default: + break; + } + /* Be permissive for arguments to built-ins; __builtin_constant_p should return constant false for a non-constant argument. */ constexpr_ctx new_ctx = *ctx; new_ctx.quiet = true; for (i = 0; i < nargs; ++i) { - args[i] = CALL_EXPR_ARG (t, i); + tree arg = CALL_EXPR_ARG (t, i); + + /* To handle string built-ins we need to pass ADDR_EXPR since + expand_builtin doesn't know how to look in the values table. */ + bool strop = i < strops; + if (strop) + { + STRIP_NOPS (arg); + if (TREE_CODE (arg) == ADDR_EXPR) + arg = TREE_OPERAND (arg, 0); + else + strop = false; + } + /* If builtin_valid_in_constant_expr_p is true, potential_constant_expression_1 has not recursed into the arguments of the builtin, verify it here. */ if (!builtin_valid_in_constant_expr_p (fun) - || potential_constant_expression (args[i])) + || potential_constant_expression (arg)) { bool dummy1 = false, dummy2 = false; - args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false, - &dummy1, &dummy2); + arg = cxx_eval_constant_expression (&new_ctx, arg, false, + &dummy1, &dummy2); } if (bi_const_p) /* For __builtin_constant_p, fold all expressions with constant values even if they aren't C++ constant-expressions. */ - args[i] = cp_fold_rvalue (args[i]); + arg = cp_fold_rvalue (arg); + else if (strop) + { + if (TREE_CODE (arg) == CONSTRUCTOR) + arg = braced_lists_to_strings (TREE_TYPE (arg), arg); + if (TREE_CODE (arg) == STRING_CST) + arg = build_address (arg); + } + + args[i] = arg; } bool save_ffbcp = force_folding_builtin_constant_p; @@ -1325,6 +1373,18 @@ cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, return t; } + if (strret) + { + /* memchr returns a pointer into the first argument, but we replaced the + argument above with a STRING_CST; put it back it now. */ + tree op = CALL_EXPR_ARG (t, strret-1); + STRIP_NOPS (new_call); + if (TREE_CODE (new_call) == POINTER_PLUS_EXPR) + TREE_OPERAND (new_call, 0) = op; + else if (TREE_CODE (new_call) == ADDR_EXPR) + new_call = op; + } + return cxx_eval_constant_expression (&new_ctx, new_call, lval, non_constant_p, overflow_p); } diff --git a/gcc/testsuite/g++.dg/ext/constexpr-builtin1.C b/gcc/testsuite/g++.dg/ext/constexpr-builtin1.C new file mode 100644 index 00000000000..2d0904b6e08 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/constexpr-builtin1.C @@ -0,0 +1,37 @@ +// PR c++/80265 +// { dg-do compile { target c++14 } } + +constexpr bool compare() { + char s1[] = "foo"; + char s2[] = "fxo"; + if (!__builtin_memcmp(s1, s2, 3)) + return false; + s2[1] = 'o'; + if (__builtin_memcmp(s1, s2, 3)) + return false; + if (__builtin_strcmp(s1, s2)) + return false; + return true; +} + +constexpr bool length() { + char s[] = "foo"; + if (__builtin_strlen(s) != 3) + return false; + return true; +} + +constexpr bool find() { + char s[] = "foo"; + if (__builtin_memchr(s, 'f', 3) != s) + return false; + if (__builtin_strchr(s, 'o') != s+1) + return false; + if (__builtin_strstr(s, "oo") != s+1) + return false; + return true; +} + +static_assert( compare(), "" ); +static_assert( length(), "" ); +static_assert( find(), "" );