From patchwork Thu Feb 1 22:45:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 85181 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 DBEF438582AE for ; Thu, 1 Feb 2024 22:46:33 +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 BF1C73858D33 for ; Thu, 1 Feb 2024 22:45:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BF1C73858D33 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 BF1C73858D33 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=1706827560; cv=none; b=CWNGS3KKkUj7wP+3ZDWC90n407rOapa1TL9yCDeSywh54U2071aJfVD597DM6/I2oHSB601Z+R78cLI/xMaUN8D7MQI+95pGLj/GI68hPq23sVkrTd8rliVyi7k4T6cFivflScYT8HD4dGQYF6xZmDP9G6LlPAL/IlIrXchLUE4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706827560; c=relaxed/simple; bh=sIprk1lqep3A9kU2NsFVDd+SoFCcuR2KUiGnpmytwFE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=dBYwf+c/1NxTNd0SSlrfdjUUbcL7Iw8N0sWwIdyRVz79UUaK7Z9fnmRESNPX+uGMhmcc5JQXg3MO7jPn7enTwD/dHpBurJAHdJFTzi6MGNIU17wEo5wur0cJ6t6MM5y0g5E6imY9C1kyMl4cD5yWkQL7UOjQIrF1kJG7euDgGg8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706827555; 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:in-reply-to:in-reply-to: references:references; bh=v2o4fyo+LBuZ0PpG9/kChkriwCGQMn6pOlhcb0jDtEc=; b=KhuUrhL2JJPq/XT2bVPi9hECaLSm2Z9Z2ihAdELj6HwYN+jyN7Sh3hVhR/i2yEZf3IMPW6 G+qyqdEpudVenotUhUzYkE2ufLnAkV5cWCCk1zIYsrGbAaoIZjss2gnIZZic2RFPoIK1fT TMAd2F0p/YwC29wTd9MyswS7blk6OC4= 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-349-byX1VC1OMGWwFdvXvSPiIQ-1; Thu, 01 Feb 2024 17:45:52 -0500 X-MC-Unique: byX1VC1OMGWwFdvXvSPiIQ-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 BA68A1C2FE24; Thu, 1 Feb 2024 22:45:51 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 705C32166B33; Thu, 1 Feb 2024 22:45:51 +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 411Mjipv2703715 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 1 Feb 2024 23:45:44 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 411Mjh3C2703714; Thu, 1 Feb 2024 23:45:43 +0100 Date: Thu, 1 Feb 2024 23:45:43 +0100 From: Jakub Jelinek To: Richard Biener , Jeff Law , Roger Sayle Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-ssa-math-opts: Fix is_widening_mult_rhs_p - unbreak bootstrap [PR113705] Message-ID: References: <003f01da534e$94918450$bdb48cf0$@nextmovesoftware.com> MIME-Version: 1.0 In-Reply-To: <003f01da534e$94918450$bdb48cf0$@nextmovesoftware.com> 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=-0.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SOMETLD_ARE_BAD_TLD, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no 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 On Tue, Jan 30, 2024 at 07:33:10AM -0000, Roger Sayle wrote: + wide_int bits = wide_int::from (tree_nonzero_bits (rhs), + prec, + TYPE_SIGN (TREE_TYPE (rhs))); ... > + if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR > + && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST > + && wi::to_wide (gimple_assign_rhs2 (stmt)) > + == wi::mask (hprec, false, prec)) This change broke bootstrap on aarch64-linux. The problem can be seen even on the reduced testcase. The IL on the unreduced testcase before widening_mul has: # val_583 = PHI ... pretmp_266 = MEM[(const struct wide_int_storage *)&D.160657].len; _264 = pretmp_266 & 65535; ... _176 = (sizetype) val_583; _439 = (sizetype) _264; _284 = _439 * 8; _115 = _176 + _284; where 583/266/264 have unsigned int type and 176/439/284/115 have sizetype. widening_mul first turns that into: # val_583 = PHI ... pretmp_266 = MEM[(const struct wide_int_storage *)&D.160657].len; _264 = pretmp_266 & 65535; ... _176 = (sizetype) val_583; _439 = (sizetype) _264; _284 = _264 w* 8; _115 = _176 + _284; and then is_widening_mult_rhs_p is called, with type sizetype (64-bit), rhs _264, hprec 32 and prec 64. Now tree_nonzero_bits (rhs) is 65535, so bits is 64-bit wide_int 65535, stmt is BIT_AND_EXPR, but we ICE on the wi::to_wide (gimple_assign_rhs2 (stmt)) == wi::mask (hprec, false, prec) comparison because wi::to_wide on gimple_assign_rhs2 (stmt) - unsigned int 65535 gives 32-bit wide_int 65535, while wi::mask (hprec, false, prec) gives 64-bit wide_int 0xffffffff and comparison between different precision wide_ints is forbidden. The following patch fixes it the same way how bits is computed earlier, by calling wide_int::from on the wi::to_wide (gimple_assign_rhs2 (stmt)), so we compare 64-bit 65535 with 64-bit 0xffffffff. Bootstrapped/regtested on x86_64-linux and i686-linux plus tested on the testcase using cross to aarch64-linux, ok for trunk? 2024-02-01 Jakub Jelinek PR middle-end/113705 * tree-ssa-math-opts.cc (is_widening_mult_rhs_p): Use wide_int_from around wi::to_wide in order to compare value in prec precision. * g++.dg/opt/pr113705.C: New test. Jakub --- gcc/tree-ssa-math-opts.cc.jj 2024-02-01 09:14:16.623549514 +0100 +++ gcc/tree-ssa-math-opts.cc 2024-02-01 17:48:59.151771177 +0100 @@ -2572,7 +2572,8 @@ is_widening_mult_rhs_p (tree type, tree if (is_gimple_assign (stmt) && gimple_assign_rhs_code (stmt) == BIT_AND_EXPR && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST - && wi::to_wide (gimple_assign_rhs2 (stmt)) + && wide_int::from (wi::to_wide (gimple_assign_rhs2 (stmt)), + prec, TYPE_SIGN (TREE_TYPE (rhs))) == wi::mask (hprec, false, prec)) *new_rhs_out = gimple_assign_rhs1 (stmt); else --- gcc/testsuite/g++.dg/opt/pr113705.C.jj 2024-02-01 19:02:29.181388575 +0100 +++ gcc/testsuite/g++.dg/opt/pr113705.C 2024-02-01 19:03:03.211918152 +0100 @@ -0,0 +1,68 @@ +// PR middle-end/113705 +// { dg-do compile { target c++17 } } +// { dg-options "-O2 -w" } + +void foo (); +template struct A : T { long bar () const; }; +int a; + +template +long +A::bar () const +{ + return this->baz ()[a]; +} + +struct B { + struct { long b[1]; long c; } u; + unsigned d; + int e; + B (const B &); + ~B (); + const long *baz () const; + unsigned qux () const; +}; + +B::B (const B &) +{ + if (__builtin_expect (e, 0)) + u.c = 0; +} + +B::~B () +{ + if (__builtin_expect (e, 0)) + foo (); +} + +const long * +B::baz () const +{ + return u.b; +} + +unsigned +B::qux () const +{ + return d; +} + +struct C { A corge () const; A *f; }; + +A +C::corge () const +{ + return f[1]; +} + +void +test (C r, long *h, unsigned short *d) +{ + for (int j = 0; j < 8; ++j) + { + A g = r.corge (); + *d = g.qux (); + for (unsigned i = 0; i < *d; ++i) + *h++ = g.bar (); + } +}