From patchwork Tue Jun 4 13:34:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1943371 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dz6XfI2B; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vts5g4kv5z20WL for ; Tue, 4 Jun 2024 23:34:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB606390D9AC for ; Tue, 4 Jun 2024 13:34:45 +0000 (GMT) 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 ESMTPS id 571F3388980B for ; Tue, 4 Jun 2024 13:34:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 571F3388980B 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 571F3388980B 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=1717508064; cv=none; b=tGzw0KD9jKWBRdPb6SP6X3cMYlbQepFCZQNqh0weeDjyhur3ohfEWvWA9dWZRxKtdnqb9okenpLXYM87ckvmB18bj1F+8qkiX0RdhZUcd7/izaaA4bj46cGpInMwLUJm/piqbsjE16Mwl78WrhCCkI5s9z5h3I128KPruX9QV6s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717508064; c=relaxed/simple; bh=wY8FJqJinlOPQEE9UiTqUzuVQ97ZE7PBAlbSzt99E9Q=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=EsTeQMcxYtSiw5a0bGzR9iSOQJCMBGUSxgtHuu4R4csqACHkSYxWFqMxJMBeBaogBjVmuXxGln1IYQcQfJ2IX0sLhhHWnv+PIUhfY2NHlIQhCcpNkA0HN9VZ/Ie8rLu/kmacAsbSA8BY/S7CsXgC1GvieDMW0TDhGgi6klKVAPQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717508062; 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=l4J407OCta3KKNvKWxAOGI7kt6yhDj9sHi0Mzo9JQ9Q=; b=dz6XfI2BHLdYvgvbgYimEpFbdqUS4SI9zNkTjMu4319vdBxNSmY3m7tCKAgh/x5Awk3JpZ kSngcJsB4P9wrD6cr0xwrddW5H0gLSNXRKW/jARmjV9y386UXXKoF78VwhaQjgU8TWgCcN lhUQixsbJTx9Oz5ILFFjEIdl3oQG/5I= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-487-fh2ZrLx5PciblA8UmFg20Q-1; Tue, 04 Jun 2024 09:34:21 -0400 X-MC-Unique: fh2ZrLx5PciblA8UmFg20Q-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id B986B3C00085; Tue, 4 Jun 2024 13:34:20 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 397E32166B5D; Tue, 4 Jun 2024 13:34:20 +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 454DYIrb1623838 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 4 Jun 2024 15:34:18 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 454DYIjv1623837; Tue, 4 Jun 2024 15:34:18 +0200 Date: Tue, 4 Jun 2024 15:34:17 +0200 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] fold-const: Fix up CLZ handling in tree_call_nonnegative_warnv_p [PR115337] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 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_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The function currently incorrectly assumes all the __builtin_clz* and .CLZ calls have non-negative result. That is the case of the former which is UB on zero and has [0, prec-1] return value otherwise, and is the case of the single argument .CLZ as well (again, UB on zero), but for two argument .CLZ is the case only if the second argument is also nonnegative (or if we know the argument can't be zero, but let's do that just in the ranger IMHO). The following patch does that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk and 14? For 13 and earlier, we can't use the testcase and the fold-const.cc change would need to differentiate between __builtin_clz* vs. .CLZ and in the latter case look at CLZ_DEFINED_VALUE_AT_ZERO. 2024-06-04 Jakub Jelinek PR tree-optimization/115337 * fold-const.cc (tree_call_nonnegative_warnv_p) : If arg1 is non-NULL, RECURSE on it, otherwise return true. * gcc.dg/bitint-106.c: New test. Jakub --- gcc/fold-const.cc.jj 2024-04-04 10:47:46.363287718 +0200 +++ gcc/fold-const.cc 2024-06-04 10:56:57.575425348 +0200 @@ -15241,7 +15241,6 @@ tree_call_nonnegative_warnv_p (tree type CASE_CFN_FFS: CASE_CFN_PARITY: CASE_CFN_POPCOUNT: - CASE_CFN_CLZ: CASE_CFN_CLRSB: case CFN_BUILT_IN_BSWAP16: case CFN_BUILT_IN_BSWAP32: @@ -15250,6 +15249,11 @@ tree_call_nonnegative_warnv_p (tree type /* Always true. */ return true; + CASE_CFN_CLZ: + if (arg1) + return RECURSE (arg1); + return true; + CASE_CFN_SQRT: CASE_CFN_SQRT_FN: /* sqrt(-0.0) is -0.0. */ --- gcc/testsuite/gcc.dg/bitint-106.c.jj 2024-06-04 12:00:59.017079094 +0200 +++ gcc/testsuite/gcc.dg/bitint-106.c 2024-06-04 12:00:41.975306632 +0200 @@ -0,0 +1,29 @@ +/* PR tree-optimization/115337 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-O2" } */ + +#if __BITINT_MAXWIDTH__ >= 129 +#define N 128 +#else +#define N 63 +#endif + +_BitInt (N) g; +int c; + +void +foo (unsigned _BitInt (N + 1) z, _BitInt (N) *ret) +{ + c = __builtin_stdc_first_leading_one (z << N); + _BitInt (N) y = *(_BitInt (N) *) __builtin_memset (&g, c, 5); + *ret = y; +} + +int +main () +{ + _BitInt (N) x; + foo (0, &x); + if (c || g || x) + __builtin_abort (); +}