From patchwork Fri Feb 9 08:30:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 198781 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp706997dyd; Fri, 9 Feb 2024 00:31:24 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVY89tzk8SPChAbDSJJkEQUMuoWXq0DFHsI4z6dZOOIymDOvM0b5fqa2ksM4aXdqIxHoEM/oImo03oQyFM30oCRjl3iGA== X-Google-Smtp-Source: AGHT+IHttJnV5etWIDPhtSFDiYDtspHoGIoprQeTosgB2/d/vu6GTxLBerIE3GksiTs4zDSHiRzF X-Received: by 2002:a05:622a:1652:b0:42c:3f91:86b5 with SMTP id y18-20020a05622a165200b0042c3f9186b5mr1097883qtj.46.1707467483846; Fri, 09 Feb 2024 00:31:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707467483; cv=pass; d=google.com; s=arc-20160816; b=ou9dWgkgadKlr2wOZccTYE3On6aUAZNh+wPDrv+Lkxv8aaJ7kuWILMm/4BeGhLh3le biX8613BB8qb0gBRDLKyjUckSIQYk/MFZ1O+aNJ40Tc8bibLzZv4uKjoVTlvsYY6w3g+ w+Wg/XprrT7+4922Y5mvt6TtlZ8++msIZA1VAAE08x13ZfU/4yvJQYZ4w4r5dLSCCwQP s5Vy24ZBp+JeZMVq6xTZxMALXpRGRMjBdMQA0rvfljRY2BLo0/lbU4HcKCGcSNXL41Jn JNTY+X+BsnxlFIDxArqDt/e8fQu7QjD75fDJV3Ap+9cmFqLemncOdDGdQmAczVCluPOz LfJQ== 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=LRDDHILWNEyIrXmyMxlGZRsaDZdXigc2teM7krm66Ag=; fh=gGMoaw00+79+/wyZCTdqAuWZsgVI69JcN+wsVxRuJR0=; b=L+f/fK0aoXVINv4S9ZQaPkGzujNYgwD6X73vyx+TiDshwZz8JHHTxBLgqstJxdDKXQ yzQSiwGbh1CZUTidUW7PIJ9HBgNhvELYgFNUPGL+nV7wvGEjvRpVTuNGbDG2IA1xHi2S 03usRxU/ojP8LjtWSgjXpxPmjaA34LapessRRFHU0j5wxH61uZdHeGGWk9ysyQ5ilseo M+s53tal9TWFF/dxheXMp8VZZ4lcyfsj5lufU9RCcVVqy+4nJHyySf/186xQR8Zl8/aM C5RVbp5Osk0icic7ht+vEddp//onkdNMb6M7h88qnjqwQTkK4XcbtJy2ZtcgjA/q8hDO SZ3A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GBrTvzWe; 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 X-Forwarded-Encrypted: i=2; AJvYcCW6FZpt9YoEqV4elO9VIywBCT33q/4MnPLck2K2ZgW4iqh2s9vJ2/MrhJKjf8T64LtqevlSkYwPnUsHTc+ae5IgG+/JTw== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d13-20020a05622a05cd00b0042bedf6ccdasi1387188qtb.16.2024.02.09.00.31.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 00:31:23 -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=GBrTvzWe; 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 898AE385840E for ; Fri, 9 Feb 2024 08:31:23 +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 B5CB23858C35 for ; Fri, 9 Feb 2024 08:30:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B5CB23858C35 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 B5CB23858C35 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=1707467426; cv=none; b=Bf+T7lVQHllb0Gb+N7DnoPtURGP6/h1XEvSoMvBdD014CsGJ8h/9iv4MhNnl0kQp4SvM/c0cigFd8jf+IfvCnruj7LeTGw9AXpQKyrpfvt8kdz23OwNe0crXh6kz7LZOc9zwe62uUy1ZZUZF4gMfyaWQRLyDCAhDjd2OewydvIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707467426; c=relaxed/simple; bh=nACjc6qlZ7jPacYbeXivnbp7oDb3KF9xYw9Pu12E1J4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=dkwRyJDpfWKXd9jK3yAW1fz1RLTwr2eq1s9M3jXJ9Njc119e8BQ1n4xzoJzPdixLllS29JICMIdcUI3gF/68OFhPI0Ikq9d58UDSMQT7RXlJZIPbHoHrAZLMyoxxXY7gOETUjlNVr7h5d7QutetyQVZsWHo6sTDZSxdoO8uejDM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707467424; 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=LRDDHILWNEyIrXmyMxlGZRsaDZdXigc2teM7krm66Ag=; b=GBrTvzWeAqB4Lz+tpO19QWEKpQXBSS2cgPKcO5M1z80i+iyyeLBxGkEt0VatnPW2ANi24L 8ZSAQczr24WsDq1Tfy0KWQTsQdy2HjhHniLbAUDyyRi9nyvsYShpQvCBHVyuLY/zzJdFus ElKkbvJpgXkgbcdVzD2nqzCLixJnl4M= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-93-D40020ocNRaV9VmaVlI51Q-1; Fri, 09 Feb 2024 03:30:20 -0500 X-MC-Unique: D40020ocNRaV9VmaVlI51Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 A0F6C85A58F; Fri, 9 Feb 2024 08:30:20 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 64D451103A; Fri, 9 Feb 2024 08:30: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 4198UHGn639812 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 9 Feb 2024 09:30:18 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4198UHEO639811; Fri, 9 Feb 2024 09:30:17 +0100 Date: Fri, 9 Feb 2024 09:30:16 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] bitint: Fix handling of VIEW_CONVERT_EXPRs to minimally supported huge INTEGER_TYPEs [PR113783] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 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_H2, 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: 1790409424488605080 X-GMAIL-MSGID: 1790409424488605080 Hi! On the following testcases memcpy lowering folds the calls to reading and writing of MEM_REFs with huge INTEGER_TYPEs - uint256_t with OImode or uint512_t with XImode. Further optimization turn the load from MEM_REF from the large/huge _BitInt var into VIEW_CONVERT_EXPR from it to the uint256_t/uint512_t. The backend doesn't really support those except for "movoi"/"movxi" insns, so it isn't possible to handle it like casts to supportable INTEGER_TYPEs where we can construct those from individual limbs - there are no OImode/XImode shifts and the like we can use. So, the following patch makes sure for such VCEs that the SSA_NAME operand of the VCE lives in memory and then turns it into a VIEW_CONVERT_EXPR so that we actually load the OImode/XImode integer from memory (i.e. a mov). We need to make sure those aren't merged with other operations in the gimple_lower_bitint hunks. For SSA_NAMEs which have underlying VAR_DECLs that is all we need, those VAR_DECL have ARRAY_TYPEs. For SSA_NAMEs which have underlying PARM_DECLs or RESULT_DECLs those have BITINT_TYPE and I had to tweak expand_expr_real_1 for that so that it doesn't try convert_modes on those when one of the modes is BLKmode - we want to fall through into the adjust_address on the MEM. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-09 Jakub Jelinek PR tree-optimization/113783 * gimple-lower-bitint.cc (bitint_large_huge::lower_stmt): Look through VIEW_CONVERT_EXPR for final cast checks. Handle VIEW_CONVERT_EXPRs from large/huge _BitInt to > MAX_FIXED_MODE_SIZE INTEGER_TYPEs. (gimple_lower_bitint): Don't merge mergeable operations or other casts with VIEW_CONVERT_EXPRs to > MAX_FIXED_MODE_SIZE INTEGER_TYPEs. * expr.cc (expand_expr_real_1): Don't use convert_modes if either mode is BLKmode. * gcc.dg/bitint-88.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2024-02-06 12:58:48.296021497 +0100 +++ gcc/gimple-lower-bitint.cc 2024-02-08 12:49:40.435313811 +0100 @@ -5263,6 +5263,8 @@ bitint_large_huge::lower_stmt (gimple *s { lhs = gimple_assign_lhs (stmt); tree rhs1 = gimple_assign_rhs1 (stmt); + if (TREE_CODE (rhs1) == VIEW_CONVERT_EXPR) + rhs1 = TREE_OPERAND (rhs1, 0); if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE && bitint_precision_kind (TREE_TYPE (lhs)) >= bitint_prec_large && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) @@ -5273,6 +5275,44 @@ bitint_large_huge::lower_stmt (gimple *s || POINTER_TYPE_P (TREE_TYPE (lhs)))) { final_cast_p = true; + if (TREE_CODE (TREE_TYPE (lhs)) == INTEGER_TYPE + && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE + && gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR) + { + /* Handle VIEW_CONVERT_EXPRs to not generally supported + huge INTEGER_TYPEs like uint256_t or uint512_t. These + are usually emitted from memcpy folding and backends + support moves with them but that is usually it. */ + if (TREE_CODE (rhs1) == INTEGER_CST) + { + rhs1 = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), + rhs1); + gcc_assert (rhs1 && TREE_CODE (rhs1) == INTEGER_CST); + gimple_assign_set_rhs1 (stmt, rhs1); + gimple_assign_set_rhs_code (stmt, INTEGER_CST); + update_stmt (stmt); + return; + } + gcc_assert (TREE_CODE (rhs1) == SSA_NAME); + if (SSA_NAME_IS_DEFAULT_DEF (rhs1) + && (!SSA_NAME_VAR (rhs1) || VAR_P (SSA_NAME_VAR (rhs1)))) + { + tree var = create_tmp_reg (TREE_TYPE (lhs)); + rhs1 = get_or_create_ssa_default_def (cfun, var); + gimple_assign_set_rhs1 (stmt, rhs1); + gimple_assign_set_rhs_code (stmt, SSA_NAME); + } + else + { + int part = var_to_partition (m_map, rhs1); + gcc_assert (m_vars[part] != NULL_TREE); + rhs1 = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), + m_vars[part]); + gimple_assign_set_rhs1 (stmt, rhs1); + } + update_stmt (stmt); + return; + } if (TREE_CODE (rhs1) == SSA_NAME && (m_names == NULL || !bitmap_bit_p (m_names, SSA_NAME_VERSION (rhs1)))) @@ -6103,7 +6143,13 @@ gimple_lower_bitint (void) if (gimple_assign_cast_p (use_stmt)) { tree lhs = gimple_assign_lhs (use_stmt); - if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))) + if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)) + /* Don't merge with VIEW_CONVERT_EXPRs to + huge INTEGER_TYPEs used sometimes in memcpy + expansion. */ + && (TREE_CODE (TREE_TYPE (lhs)) != INTEGER_TYPE + || (TYPE_PRECISION (TREE_TYPE (lhs)) + <= MAX_FIXED_MODE_SIZE))) continue; } else if (gimple_store_p (use_stmt) @@ -6158,6 +6204,18 @@ gimple_lower_bitint (void) == gimple_bb (SSA_NAME_DEF_STMT (s)))) goto force_name; break; + case VIEW_CONVERT_EXPR: + /* Don't merge with VIEW_CONVERT_EXPRs to + huge INTEGER_TYPEs used sometimes in memcpy + expansion. */ + { + tree lhs = gimple_assign_lhs (use_stmt); + if (TREE_CODE (TREE_TYPE (lhs)) == INTEGER_TYPE + && (TYPE_PRECISION (TREE_TYPE (lhs)) + > MAX_FIXED_MODE_SIZE)) + goto force_name; + } + break; default: break; } --- gcc/expr.cc.jj 2024-01-30 08:45:06.773844050 +0100 +++ gcc/expr.cc 2024-02-08 13:05:09.228313857 +0100 @@ -12445,7 +12445,10 @@ expand_expr_real_1 (tree exp, rtx target } } /* If both types are integral, convert from one mode to the other. */ - else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0))) + else if (INTEGRAL_TYPE_P (type) + && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)) + && mode != BLKmode + && GET_MODE (op0) != BLKmode) op0 = convert_modes (mode, GET_MODE (op0), op0, TYPE_UNSIGNED (TREE_TYPE (treeop0))); /* If the output type is a bit-field type, do an extraction. */ --- gcc/testsuite/gcc.dg/bitint-88.c.jj 2024-02-08 13:12:03.131520889 +0100 +++ gcc/testsuite/gcc.dg/bitint-88.c 2024-02-08 13:09:16.018859902 +0100 @@ -0,0 +1,38 @@ +/* PR tree-optimization/113783 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-O2" } */ +/* { dg-additional-options "-mavx512f" { target i?86-*-* x86_64-*-* } } */ + +int i; + +#if __BITINT_MAXWIDTH__ >= 246 +void +foo (void *p, _BitInt(246) x) +{ + __builtin_memcpy (p, &x, sizeof x); +} + +_BitInt(246) +bar (void *p, _BitInt(246) x) +{ + _BitInt(246) y = x + 1; + __builtin_memcpy (p, &y, sizeof y); + return x; +} +#endif + +#if __BITINT_MAXWIDTH__ >= 502 +void +baz (void *p, _BitInt(502) x) +{ + __builtin_memcpy (p, &x, sizeof x); +} + +_BitInt(502) +qux (void *p, _BitInt(502) x) +{ + _BitInt(502) y = x + 1; + __builtin_memcpy (p, &y, sizeof y); + return x; +} +#endif