From patchwork Wed Mar 12 06:59:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 107750 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 77F523858410 for ; Wed, 12 Mar 2025 07:00:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 77F523858410 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=UuzjQKWJ X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 3FFCC3858D21 for ; Wed, 12 Mar 2025 07:00:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3FFCC3858D21 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3FFCC3858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741762808; cv=none; b=X25s/sbAY55wB3Z2kZHbLj80nqSW6wBU7lorncw8GA8RoGv3gBejb4GmDgvJeyk2rHaZBAaWpWjaKbLEaLbKKZW5DqiFhT3OW5xGyvZ9Or/d8JpKtmAPlvPCBAoTUfZxawceUK+CHTmvvbZM5zLhksAZ/GR1sG6aNucWfX0rR74= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741762808; c=relaxed/simple; bh=xvh9+xf+x07SYNVMprAIKUZPDx7RTzLTw5QsZ+no7l8=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=C5iwCIjZ1FvvnncN37g/X2h4CdL1GZHodKNQYhMxy58yK6nnFarDrunAhdiSdOvjRT92bBT4kOx1UIRk0BYuGQSDpQQ34uX8QcJE5GV7XcXcMroVR7NvZl68iJHPfRCnUpThph7r+i/mmwNGRAo30CmQfCnAVbE2IYbO1NTpTDM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3FFCC3858D21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741762807; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=Na7hW9DqV3EeSisIUx0nxgM4qcdQZ7CZ5ziVW1+lcUQ=; b=UuzjQKWJiHlo8838K4OY2MoQ0UuZL7XDqOYw0w/vzXWP0c9BZfHKOZXqPbnSwsFY2WKTkg TtIoZzWTj9qzAE2JSEd06UZdi8mrTSCcMYWHbVSytHRxZ1lGwSK8UuDtom1y8mgwWJJfxt RruC8hAz5tjevJ7+sZmv8Jw9znUAfHk= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-551-BE1gDfI5NyOnYk0_eclRkQ-1; Wed, 12 Mar 2025 03:00:06 -0400 X-MC-Unique: BE1gDfI5NyOnYk0_eclRkQ-1 X-Mimecast-MFC-AGG-ID: BE1gDfI5NyOnYk0_eclRkQ_1741762805 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 53FD5180025C; Wed, 12 Mar 2025 07:00:04 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.22.89.222]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6FFB9180AF7B; Wed, 12 Mar 2025 07:00:03 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 52C6xx9t1156665 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 12 Mar 2025 08:00:00 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 52C6xuIt1156662; Wed, 12 Mar 2025 07:59:56 +0100 Date: Wed, 12 Mar 2025 07:59:56 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] builtins: Fix up strspn/strcspn folding [PR119219] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 68PdaQ9jZdSd69701P3XOuFeNxM-Fs8MovF8GudSnoo_1741762805 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_HOSTKARMA_W, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Hi! The PR119204 r15-7955 fix caused some regressions. The problem is that the fold_builtin* APIs document that expr is either a CALL_EXPR of the call or NULL, so using TREE_TYPE (expr) can crash e.g. during constexpr evaluation etc. As can be seen in the surrounding patch, for the neighbouring builtins (both modf and strpbrk) fold_builtin_2 passes down type, which is the result type, TREE_TYPE (TREE_TYPE (fndecl)) and those builtins use it to build the return value, while strspn was always building size_type_node and strcspn had this change from that to TREE_TYPE (expr). The patch passes type to these two and uses it there as well. The patch keeps passing expr because it is used in the check_nul_terminated_array calls done for both strspn and strcspn, those calls clearly can deal with NULL expr but prefer if it is non-NULL for some warning. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2025-03-12 Jakub Jelinek PR middle-end/119204 PR middle-end/119219 * builtins.cc (fold_builtin_2): Pass type as another argument to fold_builtin_strspn and fold_builtin_strcspn. (fold_builtin_strspn): Add type argument, use it instead of size_type_node. (fold_builtin_strcspn): Add type argument, use it instead of TREE_TYPE (expr). Jakub --- gcc/builtins.cc.jj 2025-03-11 12:05:42.561533453 +0100 +++ gcc/builtins.cc 2025-03-11 15:36:47.774314011 +0100 @@ -176,8 +176,8 @@ static tree fold_builtin_iseqsig (locati static tree fold_builtin_varargs (location_t, tree, tree*, int); static tree fold_builtin_strpbrk (location_t, tree, tree, tree, tree); -static tree fold_builtin_strspn (location_t, tree, tree, tree); -static tree fold_builtin_strcspn (location_t, tree, tree, tree); +static tree fold_builtin_strspn (location_t, tree, tree, tree, tree); +static tree fold_builtin_strcspn (location_t, tree, tree, tree, tree); static rtx expand_builtin_object_size (tree); static rtx expand_builtin_memory_chk (tree, rtx, machine_mode, @@ -10800,10 +10800,10 @@ fold_builtin_2 (location_t loc, tree exp return fold_builtin_modf (loc, arg0, arg1, type); case BUILT_IN_STRSPN: - return fold_builtin_strspn (loc, expr, arg0, arg1); + return fold_builtin_strspn (loc, expr, arg0, arg1, type); case BUILT_IN_STRCSPN: - return fold_builtin_strcspn (loc, expr, arg0, arg1); + return fold_builtin_strcspn (loc, expr, arg0, arg1, type); case BUILT_IN_STRPBRK: return fold_builtin_strpbrk (loc, expr, arg0, arg1, type); @@ -11304,7 +11304,7 @@ fold_builtin_strpbrk (location_t loc, tr form of the builtin function call. */ static tree -fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2) +fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2, tree type) { if (!validate_arg (s1, POINTER_TYPE) || !validate_arg (s2, POINTER_TYPE)) @@ -11320,8 +11320,7 @@ fold_builtin_strspn (location_t loc, tre if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')) /* Evaluate and ignore both arguments in case either one has side-effects. */ - return omit_two_operands_loc (loc, size_type_node, size_zero_node, - s1, s2); + return omit_two_operands_loc (loc, type, size_zero_node, s1, s2); return NULL_TREE; } @@ -11344,7 +11343,7 @@ fold_builtin_strspn (location_t loc, tre form of the builtin function call. */ static tree -fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2) +fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2, tree type) { if (!validate_arg (s1, POINTER_TYPE) || !validate_arg (s2, POINTER_TYPE)) @@ -11360,8 +11359,7 @@ fold_builtin_strcspn (location_t loc, tr { /* Evaluate and ignore argument s2 in case it has side-effects. */ - return omit_one_operand_loc (loc, TREE_TYPE (expr), - size_zero_node, s2); + return omit_one_operand_loc (loc, type, size_zero_node, s2); } /* If the second argument is "", return __builtin_strlen(s1). */ @@ -11375,7 +11373,7 @@ fold_builtin_strcspn (location_t loc, tr if (!fn) return NULL_TREE; - return fold_convert_loc (loc, TREE_TYPE (expr), + return fold_convert_loc (loc, type, build_call_expr_loc (loc, fn, 1, s1)); } return NULL_TREE;