From patchwork Wed Dec 13 10:00:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 177843 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp7663472dys; Wed, 13 Dec 2023 02:01:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IF7/r1rmvZWTpOOR0uYLoulghEGQeM4RVGhRmZF5fl9v7pikeUGbVWzVyb3dptTPSyQXvK4 X-Received: by 2002:a05:620a:17a6:b0:77f:6baa:b72c with SMTP id ay38-20020a05620a17a600b0077f6baab72cmr7169788qkb.129.1702461676644; Wed, 13 Dec 2023 02:01:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702461676; cv=pass; d=google.com; s=arc-20160816; b=OIorAfn4mO1r11Trc2JE26To69uYCjJuhfUgNwPwDnlC7DjPsZqN6dOEn10cO0WuDM 0xpxIzwqMDLfTD1uVlIp/CUtQGeeUo+oiqP2r694LWWqV/F7S0j2rqlp1BrN7PfyK9Ej M5vQG73rveoCoaBI9174eyBCrz0IT/DAGnItEcgkqHBmS2ENAw9mDeQgcjaFKfl68nK+ fOrqXaaKkkI9fhCUiSwkCcXh1OwLgOyStQxr7czHSAAiPRO/xnUHG/o9dXnvNRaWa2KZ c88CLw7t6E8a/p6wDAHDYCXrciUGmjJksGZyKuxstWcwfXEOVgrGcXXo9ONNkovDBJxa 5jBQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=SKZn8aPvpph5uDvjGRksoEDfT94bRy34SJBJtfFo00s=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=AqkzR8uMe+mR+Wlgq2MJlECYHscsC17oHycfYrtTysmgZZqyfEm+R49ZZduJNYjMaC y6LJD+OSZYLqn/FD7N3Gij1Mg/nvedh8jHYQHBYnkQe4ZHuUOdzHlObujgP7j62NBMZ8 G+tICYx2MJoRDIgVLNtcm6MQXPXstWlshBCdWoawl1Cyf/wrvL0mgFjLC7kFW7uKtPU9 adoxIrsoYvn10adGp+j2XeVXetzBVJCdbPv5jGgHBgOK1DBIoGNVR7IQ0ZCJEMb56V2R waiCHWj8XlpLwg5Txwsk1UOMXhAgvSVNzEXW5fTeWAef8fgruvTSXQ1+f08I9gGyFwd9 MeyQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=F7YLpvRm; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n7-20020a05620a294700b007758f31d28fsi13448809qkp.301.2023.12.13.02.01.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 02:01:16 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=F7YLpvRm; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 46D3C3858029 for ; Wed, 13 Dec 2023 10:01:16 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 634FD385841A for ; Wed, 13 Dec 2023 10:00:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 634FD385841A 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 634FD385841A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702461653; cv=none; b=SRfaQk//tM79xf93t/Gntfr+R9rw3Eged9xiBJdYZsxAxxSfsSURl7wSQgNkkebho2tckfJae91A9yg+u+UzXuuLqi3ttaq0YGe1jnPEwskezHy7r7fT8o3tw+QyEpInYh7ACUt0VL20kXKNyPRyhaj23Gi9mrA3ozcdsaUbRGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702461653; c=relaxed/simple; bh=7DRIrz+iElPszCMDkGmbpQwL43U+2gNtmm57pc14ZTQ=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=JHmbowu2pHD3vTyue+fcqRndUU+rlxioY/kd58A+msoKVe9vVTHBG86J+9bfY8tHgG2vVaAfZrI+XA0v6RulfQupSgXrAg9oC6GoJj5dSCOWnTanl6XHETpMFP/qjrREPFneeMBTCVK5sTz3qKvo0Gr71rrxkNqPcljiSFu7zXc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702461652; 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=SKZn8aPvpph5uDvjGRksoEDfT94bRy34SJBJtfFo00s=; b=F7YLpvRmPcuZJ3LKmv76DfCXHaTEN/WWjsd+h5k7QRY9UxTPvVtvp139Z5StZfwAf4j72p blEG04AjQCvIhTo/Y8FB5cO4tJAg7eMMIGMP40rAU58uG30DFOtdt9KEAl3ZDp8zPhvUBI GlyNDQIluyV15dU+Om7dcPSsCT7CD0Y= 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-372-W0WV4S9VNyiDkP1G6MF93g-1; Wed, 13 Dec 2023 05:00:48 -0500 X-MC-Unique: W0WV4S9VNyiDkP1G6MF93g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 2EFB43814580; Wed, 13 Dec 2023 10:00:48 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E5AFC1121306; Wed, 13 Dec 2023 10:00:47 +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 3BDA0jtE2489841 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 13 Dec 2023 11:00:45 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BDA0iEt2489840; Wed, 13 Dec 2023 11:00:44 +0100 Date: Wed, 13 Dec 2023 11:00:44 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Fix lowering of non-_BitInt to _BitInt cast merged with some wider cast [PR112940] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 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_H3, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785160455074465897 X-GMAIL-MSGID: 1785160455074465897 Hi! The following testcase ICEs, because a PHI argument from latch edge uses a SSA_NAME set only in a conditionally executed block inside of the loop. This happens when we have some outer cast which lowers its operand several times, under some condition with variable index, under different condition with some constant index, otherwise something else, and then there is an inner cast from non-_BitInt integer (or small/middle one). Such cast in certain conditions is emitted by initializing some SSA_NAMEs in the initialization statements before loops (say for casts from <= limb size precision by computing a SSA_NAME for the first limb and then extension of it for the later limbs) and uses the prepare_data_in_out function to create a PHI node. Such function is passed the value (constant or SSA_NAME) to use in the PHI argument from the pre-header edge, but for the latch edge it always created a new SSA_NAME and then caller emitted in the following 3 spots an extra assignment to set that SSA_NAME to whatever value we want from the latch edge. In all these 3 cases the argument from the latch edge is known already before the loop though, either constant or SSA_NAME computed in pre-header as well. But the need to emit an assignment combined with the handle_operand done in a conditional basic block results in the SSA verification failure. The following patch fixes it by extending the prpare_data_in_out method, so that when the latch edge argument is known before (constant or computed in pre-header), we can just use it directly and avoid the extra assignment that would normally be hopefully optimized away later to what we now emit directly. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-13 Jakub Jelinek PR tree-optimization/112940 * gimple-lower-bitint.cc (struct bitint_large_huge): Add another argument to prepare_data_in_out method defaulted to NULL_TREE. (bitint_large_huge::handle_operand): Pass another argument to prepare_data_in_out instead of emitting an assignment to set it. (bitint_large_huge::prepare_data_in_out): Add VAL_OUT argument. If non-NULL, use it as PHI argument instead of creating a new SSA_NAME. (bitint_large_huge::handle_cast): Pass rext as another argument to 2 prepare_data_in_out calls instead of emitting assignments to set them. * gcc.dg/bitint-53.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2023-12-08 09:03:06.644475539 +0100 +++ gcc/gimple-lower-bitint.cc 2023-12-12 18:31:24.440394239 +0100 @@ -405,7 +405,7 @@ struct bitint_large_huge profile_probability, profile_probability, edge &, edge &, edge &); tree handle_operand (tree, tree); - tree prepare_data_in_out (tree, tree, tree *); + tree prepare_data_in_out (tree, tree, tree *, tree = NULL_TREE); tree add_cast (tree, tree); tree handle_plus_minus (tree_code, tree, tree, tree); tree handle_lshift (tree, tree, tree); @@ -872,11 +872,8 @@ bitint_large_huge::handle_operand (tree gcc_assert (m_first); m_data.pop (); m_data.pop (); - prepare_data_in_out (fold_convert (m_limb_type, op), idx, &out); - g = gimple_build_assign (m_data[m_data_cnt + 1], - build_int_cst (m_limb_type, ext)); - insert_before (g); - m_data[m_data_cnt + 1] = gimple_assign_rhs1 (g); + prepare_data_in_out (fold_convert (m_limb_type, op), idx, &out, + build_int_cst (m_limb_type, ext)); } else if (min_prec > prec - rem - 2 * limb_prec) { @@ -1008,10 +1005,13 @@ bitint_large_huge::handle_operand (tree /* Helper method, add a PHI node with VAL from preheader edge if inside of a loop and m_first. Keep state in a pair of m_data - elements. */ + elements. If VAL_OUT is non-NULL, use that as PHI argument from + the latch edge, otherwise create a new SSA_NAME for it and let + caller initialize it. */ tree -bitint_large_huge::prepare_data_in_out (tree val, tree idx, tree *data_out) +bitint_large_huge::prepare_data_in_out (tree val, tree idx, tree *data_out, + tree val_out) { if (!m_first) { @@ -1034,7 +1034,7 @@ bitint_large_huge::prepare_data_in_out ( if (e1 == e2) e2 = EDGE_PRED (m_bb, 1); add_phi_arg (phi, val, e1, UNKNOWN_LOCATION); - tree out = make_ssa_name (TREE_TYPE (val)); + tree out = val_out ? val_out : make_ssa_name (TREE_TYPE (val)); add_phi_arg (phi, out, e2, UNKNOWN_LOCATION); m_data.safe_push (in); m_data.safe_push (out); @@ -1541,14 +1541,10 @@ bitint_large_huge::handle_cast (tree lhs if (m_first) { tree out1, out2; - prepare_data_in_out (r1, idx, &out1); - g = gimple_build_assign (m_data[m_data_cnt + 1], rext); - insert_before (g); + prepare_data_in_out (r1, idx, &out1, rext); if (TYPE_PRECISION (rhs_type) > limb_prec) { - prepare_data_in_out (r2, idx, &out2); - g = gimple_build_assign (m_data[m_data_cnt + 3], rext); - insert_before (g); + prepare_data_in_out (r2, idx, &out2, rext); m_data.pop (); t = m_data.pop (); m_data[m_data_cnt + 1] = t; --- gcc/testsuite/gcc.dg/bitint-53.c.jj 2023-12-12 18:28:16.203949817 +0100 +++ gcc/testsuite/gcc.dg/bitint-53.c 2023-12-12 18:27:47.307342133 +0100 @@ -0,0 +1,17 @@ +/* PR tree-optimization/112940 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23 -O2" } */ + +#if __BITINT_MAXWIDTH__ >= 1025 +_BitInt (1025) b; +#endif + +void +foo (long x) +{ +#if __BITINT_MAXWIDTH__ >= 1025 + b += (unsigned _BitInt (255)) x; +#else + (void) x; +#endif +}