From patchwork Thu Jan 4 08:48:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 184945 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp5490464dyb; Thu, 4 Jan 2024 00:52:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IFWQ1m1r35zaMZVfA4QxDN8VwgvaVwYlHIkPXV4hMTOdl2THBPBx8vp55P/5LQsoOWBTufk X-Received: by 2002:a05:620a:8413:b0:781:6cf1:a70c with SMTP id pc19-20020a05620a841300b007816cf1a70cmr248236qkn.106.1704358346318; Thu, 04 Jan 2024 00:52:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704358346; cv=pass; d=google.com; s=arc-20160816; b=NkfAXlJM5cBvHqwUV0k++yptU/zBWR29OnziJSpr5j/TiXdRENAxfmnaIZqpbUovPq b9BqGlqP4ZQTVYYWEekL+G2dFKhAjiwyD72TjvJuhS1ct4knDW/uPNxV71w+2Jf8+icJ /ZExLzVx07zsOKVKOBSsYPrnKXx9/rnv62KSuhy4fBNeYueL9Mcjk4YutUCD9PTjvTZh pstM23Hn/LbnGNh1o1HcAj/yNwJ68Uf/FHJCAchcl/DObMJxrtlLVSWVlca+eVDLX8Y7 Ge3Bs3VyZ+SvnbwgPvh9oZZzlWgfVsX4KpRcl3KInOOA4L8J1DecKLF5uw+9p/VHqKID 1UZg== 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=74EWFdQe6Dfu5DlhOWq/2mqpaiwRDrAgKPwokj8kXYo=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=upPucaIeBPT9c236Qy37TT3xBdZQ3a/THJPAJCJIEPiVh26+JOqD4eHNSN7rXb0BSh a40xWbmLKCwROOK89t5ggT2VpiK5BB8TjGZlrwinX3tKu8KsQ/XRrti6ChexqUQLJcS1 LGjx4Sd3s9+3GCg64R2zK68BYy1f2ZHcQUsxFMZlwqQK0DICROfR6+H9+HAHqAfFYqRH sg4ytgap4kIz5ykua516jTk9Tp7fCo0XlIh6aHt5FIA9oMc7otxNadDOsBVmJfgyEuXY pV+d8ZiLFC5q54Cpa87ESm8HJvSG/2t9TJnB4iXguWGlZIPlkgSB08imKFeoWpkYv5zM ZNDQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hpPRO1ho; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id o28-20020a05620a0d5c00b007816bea89ebsi15987963qkl.769.2024.01.04.00.52.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 00:52:26 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hpPRO1ho; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 0D79E3858015 for ; Thu, 4 Jan 2024 08:52:26 +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 C026A3858C53 for ; Thu, 4 Jan 2024 08:48:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C026A3858C53 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 C026A3858C53 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=1704358140; cv=none; b=dYaO5WRqi51Ca4YHjkpELCLybtvGtWj9xG/Zj84l50VfF1Mt21GEQqF/JkRA3oTN/cs4S2vFt/RCqCZSU9ebt0O/kB95THO5N40aN36jWQRq6h11HyueadgluXZyIwMgQLJ+iXgRSIxrrfCIq2ANJj5fg5lhENhiIMgGoQIvkrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704358140; c=relaxed/simple; bh=p1GekK26eGo0CsGuHJwgnsoWcJC4ghRDQTNNO9skS9I=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=evsCPmd93m4R+xyyQfTUCI6vmzUalJFWacaedK6W+20rUYHkegkXD7fHqN9ScjcjAep8LZhKMTNkWZMBlU2EN3Sp+LuQxs68aCHjEXTRt8JOEvhEsyBGG2VWe0JYlJLbtxamTWqBHe3/qeCdXpByT/uK7F2d6Ort0444fYuZ4UA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704358136; 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=74EWFdQe6Dfu5DlhOWq/2mqpaiwRDrAgKPwokj8kXYo=; b=hpPRO1hoXnx0AqmeL4lN7NgSYCoyYeSwlpQ9ckgLZlK6zO6IVR9Eh7RlbXesMFwqpDzsHY J8cViSRzhuQUUsSoGHPhqOws5XZObb+r72ndYQebV4JCrOR4R3tNHBob5G8AWLWO/KYZvO oSfhJXIJcfT8hO6LHsR1Q9UEkkmgcA0= 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-550-3PEpmAnvMzCjJbpWgqImsg-1; Thu, 04 Jan 2024 03:48:54 -0500 X-MC-Unique: 3PEpmAnvMzCjJbpWgqImsg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 8F2C729AA3AF; Thu, 4 Jan 2024 08:48:54 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 53140492BE6; Thu, 4 Jan 2024 08:48:54 +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 4048mpAt3945006 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 4 Jan 2024 09:48:52 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4048mpZZ3945005; Thu, 4 Jan 2024 09:48:51 +0100 Date: Thu, 4 Jan 2024 09:48:50 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Punt .*_OVERFLOW optimization if cast from IMAGPART_EXPR appears before REALPART_EXPR [PR113119] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.0 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: 1787149257715996480 X-GMAIL-MSGID: 1787149257715996480 Hi! _BitInt lowering for .{ADD,SUB,MUL}_OVERFLOW calls which have both REALPART_EXPR and IMAGPART_EXPR used and have a cast from the IMAGPART_EXPR to a boolean or normal integral type lowers them at the point of the REALPART_EXPR statement (which is especially needed if the lhs of the call is complex with large/huge _BitInt element type); we emit the stmt to set the lhs of the cast at the same spot as well. Normally, the lowering of __builtin_{add,sub,mul}_overflow arranges the REALPART_EXPR to come before IMAGPART_EXPR, followed by cast from that, but as the testcase shows, a redundant __builtin_*_overflow call and VN can reorder those and we then ICE because the def-stmt of the former cast from IMAGPART_EXPR may appear after its uses. We already check that all of REALPART_EXPR, IMAGPART_EXPR and the cast from the latter appear in the same bb as the .{ADD,SUB,MUL}_OVERFLOW call in the optimization, the following patch just extends it to make sure cast appears after REALPART_EXPR; if not, we punt on the optimization and expand it as a store of a complex _BitInt on the location of the ifn call. Only the testcase in the testsuite is changed by the patch, all other __builtin_*_overflow* calls in the bitint* tests (and there are quite a few) have REALPART_EXPR first. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-01-04 Jakub Jelinek PR tree-optimization/113119 * gimple-lower-bitint.cc (optimizable_arith_overflow): Punt if both REALPART_EXPR and cast from IMAGPART_EXPR appear, but cast is before REALPART_EXPR. * gcc.dg/bitint-61.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2023-12-22 12:27:58.497437164 +0100 +++ gcc/gimple-lower-bitint.cc 2023-12-23 10:44:05.586522553 +0100 @@ -305,6 +305,7 @@ optimizable_arith_overflow (gimple *stmt imm_use_iterator ui; use_operand_p use_p; int seen = 0; + gimple *realpart = NULL, *cast = NULL; FOR_EACH_IMM_USE_FAST (use_p, ui, lhs) { gimple *g = USE_STMT (use_p); @@ -317,6 +318,7 @@ optimizable_arith_overflow (gimple *stmt if ((seen & 1) != 0) return 0; seen |= 1; + realpart = g; } else if (gimple_assign_rhs_code (g) == IMAGPART_EXPR) { @@ -338,13 +340,35 @@ optimizable_arith_overflow (gimple *stmt if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs2)) || TREE_CODE (TREE_TYPE (lhs2)) == BITINT_TYPE) return 0; + cast = use_stmt; } else return 0; } if ((seen & 2) == 0) return 0; - return seen == 3 ? 2 : 1; + if (seen == 3) + { + /* Punt if the cast stmt appears before realpart stmt, because + if both appear, the lowering wants to emit all the code + at the location of realpart stmt. */ + gimple_stmt_iterator gsi = gsi_for_stmt (realpart); + unsigned int cnt = 0; + do + { + gsi_prev_nondebug (&gsi); + if (gsi_end_p (gsi) || gsi_stmt (gsi) == cast) + return 0; + if (gsi_stmt (gsi) == stmt) + return 2; + /* If realpart is too far from stmt, punt as well. + Usually it will appear right after it. */ + if (++cnt == 32) + return 0; + } + while (1); + } + return 1; } /* If STMT is some kind of comparison (GIMPLE_COND, comparison assignment) --- gcc/testsuite/gcc.dg/bitint-61.c.jj 2023-12-23 10:46:17.808658852 +0100 +++ gcc/testsuite/gcc.dg/bitint-61.c 2023-12-23 10:46:02.482874865 +0100 @@ -0,0 +1,17 @@ +/* PR tree-optimization/113119 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23 -O2" } */ + +_BitInt(8) b; +_Bool c; +#if __BITINT_MAXWIDTH__ >= 8445 +_BitInt(8445) a; + +void +foo (_BitInt(4058) d) +{ + c = __builtin_add_overflow (a, 0ULL, &d); + __builtin_add_overflow (a, 0ULL, &d); + b = d; +} +#endif