From patchwork Wed Feb 28 07:59:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 207693 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3196415dyb; Wed, 28 Feb 2024 00:00:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV+p79H6dq9+Zq+/rsRIjU7cJB3yetiO4kep7+/enKUeRewdT8G6zXdUrZUmhlIdT9hqC9MnmDblVQYK4pPvh9PKE4pTw== X-Google-Smtp-Source: AGHT+IG5oLlZ3FKE9Aq/fJmd6v70u7TWWFx+wPITYM7Rns79J+aBp1jqVdFeeWSTTe1jG5n5n2TV X-Received: by 2002:a05:6102:559e:b0:472:6ad5:f852 with SMTP id dc30-20020a056102559e00b004726ad5f852mr1843434vsb.27.1709107207536; Wed, 28 Feb 2024 00:00:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709107207; cv=pass; d=google.com; s=arc-20160816; b=jU0DWY4dEFAcDm7B/uwgI910XKYzWRr0qj3TLaDgdfDKPcVMVFpxfThVM0xHxPYU08 WqmH0kDCOd/nod5xg+Zmvd3+E9IBd6LKJU/G/RvHApN2rSgg6qDpsH+37gPDYFK8xA9p QEj9Tx51/6TTBV7zVwIIBq9riu6HGe1662z+/gwVrB3SmJLIp4UGZhgXUhFgTKD3b+Hq v0DErEK/p1JBU3KnHtYoHqUwDikw1SBO7Y4zWPhYcVt0J+K6KaBj0n0MZMQr8WwnJ3ML 0QItNpHIBwDLghmE7CRVwwXjA/s7JCgWaMps8hQKoKfEE8N7UNw5k9x3EOzWYVHc3wD0 mtvg== 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=vs/UvMvwDx6MUxX6KakcLZ37/fyHzu0Kngq3yxKoH7Q=; fh=eM2nYbNDZ6Theupq9WaNJQV2QgkrsvQk+3X75erbI8c=; b=DLH4RuRGrcw8BV2ZnTVPiomD6Mzi4E4Gr5J6yTakpjn2Mc0ZeFNZHou/pqn8NfuvEQ g7T8ZxXmdHeuvoScUUv/nIk1p3gObdjHHmguPPkdiiItEvKm8Tvyy0z08TAP56iedfSn YRvCukpWIJcbDrS/0/267t9kXNHr3HNkUY13+ny7n2ca4qMHTahwQ9vMmesKW05jDEOf 86j0+GR8pV2E0APVQivedZIXV77PHHImXgrLgy8T4+mfKJPI6ZLRt4rvpN1xwLct+wg8 i0Z9SXw51a+crmrZBfYwNbXy/yccH2dvLyq1sMfS/AnYzxX/N1E67cqY92Sk3euyR7wK 1IXA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Yba19s/u"; 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 v24-20020ac87498000000b0042e5ed3de48si8762177qtq.223.2024.02.28.00.00.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 00:00:07 -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="Yba19s/u"; 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 283A63858402 for ; Wed, 28 Feb 2024 08:00:06 +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 A68553858C24 for ; Wed, 28 Feb 2024 07:59:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A68553858C24 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 A68553858C24 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=1709107161; cv=none; b=Xx1kOP3OpW2cvad7jlvQEbZBD2+Mv19qP9pXuxmXuV0Q5ciTZahsAV63VBB4aAPMK2O4ZrJf5JAsk1BXCGEF4Pt2SAzHc9Q/LafpeSgGtxYIy7dcWzfN0wvHrKGxNnQMK982IcjMi9DwuOzdmbnymTNOU0S68rCJ3bxhI2Gwe58= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709107161; c=relaxed/simple; bh=m9t7E9pWBStwU5lKAY/f0ILLKkpNNRoI1eoVR5p//Ug=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=s1wqqS+Us3T3D+yg2qIHPIbZ6E+oKMIavKyijiE/O5FXT+wVMZbvti1Xpq6rJ2wGvB/2kUzkqYMEwK6tJU2hWZevd+YcjOUelJRoF+kUf2ij9+MgAQhMfecKB2zgvUXywFeZgxl/s7+Wux7K+HwV0a4lWAuSsktY3J2qjqPP6mU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709107157; 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=vs/UvMvwDx6MUxX6KakcLZ37/fyHzu0Kngq3yxKoH7Q=; b=Yba19s/u0ZK1AFPJIW2FVyx7D7WcGNXUas5t4u49pl0oKDdTLjnfu+xMP3jXe/XYJAkUAY n5/bLXr01h6FPtajci3VSKyz1W0HvMywBHayx/DD5jdcQ0pzQ6u8F8vunuKQyV67jQOHdE fVqcO/m9X9RrwRpuctJTHpVzAjPJpqQ= 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-613-3xJ-t_hyPgGhspzqXYjZqw-1; Wed, 28 Feb 2024 02:59:11 -0500 X-MC-Unique: 3xJ-t_hyPgGhspzqXYjZqw-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 2B8031065222; Wed, 28 Feb 2024 07:59:11 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D93D4492BE8; Wed, 28 Feb 2024 07:59:10 +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 41S7x7tk2997193 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 28 Feb 2024 08:59:08 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41S7x69o2997192; Wed, 28 Feb 2024 08:59:06 +0100 Date: Wed, 28 Feb 2024 08:59:06 +0100 From: Jakub Jelinek To: Richard Biener , Richard Sandiford Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] gimple-fold: Use bitwise vector types rather than barely supported huge integral types in memcpy etc. folding [PR113988] 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=-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: 1792128799101726144 X-GMAIL-MSGID: 1792128799101726144 Hi! The following patch changes the memcpy etc. folding to use bitwise vector types rather than huge INTEGER_TYPEs for copying of > MAX_FIXED_MODE_SIZE lengths. The problem with the huge INTEGER_TYPEs is that they aren't supported very much, usually there are just optabs to handle moves of them, perhaps misaligned moves and that is it, so they pose problems e.g. to BITINT_TYPE lowering. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-28 Jakub Jelinek PR tree-optimization/113988 * stor-layout.h (bitwise_mode_for_size): Declare. * stor-layout.cc (bitwise_mode_for_size): New function. * gimple-fold.cc (gimple_fold_builtin_memory_op): Use it. Use bitwise_type_for_mode instead of build_nonstandard_integer_type. Use BITS_PER_UNIT instead of 8. * gcc.dg/bitint-91.c: New test. Jakub --- gcc/stor-layout.h.jj 2024-01-03 11:51:28.103778767 +0100 +++ gcc/stor-layout.h 2024-02-27 12:32:17.716535079 +0100 @@ -102,6 +102,8 @@ extern opt_machine_mode mode_for_size_tr extern tree bitwise_type_for_mode (machine_mode); +extern opt_machine_mode bitwise_mode_for_size (poly_uint64); + /* Given a VAR_DECL, PARM_DECL or RESULT_DECL, clears the results of a previous call to layout_decl and calls it again. */ extern void relayout_decl (tree); --- gcc/stor-layout.cc.jj 2024-01-17 13:53:13.160176498 +0100 +++ gcc/stor-layout.cc 2024-02-27 12:27:20.876647298 +0100 @@ -476,6 +476,32 @@ bitwise_type_for_mode (machine_mode mode return inner_type; } +/* Find a mode that can be used for efficient bitwise operations on SIZE + bits, if one exists. */ + +opt_machine_mode +bitwise_mode_for_size (poly_uint64 size) +{ + if (known_le (size, (unsigned int) MAX_FIXED_MODE_SIZE)) + return mode_for_size (size, MODE_INT, true); + + machine_mode mode, ret = VOIDmode; + FOR_EACH_MODE_FROM (mode, MIN_MODE_VECTOR_INT) + if (known_eq (GET_MODE_BITSIZE (mode), size) + && (ret == VOIDmode || GET_MODE_INNER (mode) == QImode) + && have_regs_of_mode[mode] + && targetm.vector_mode_supported_p (mode)) + { + if (GET_MODE_INNER (mode) == QImode) + return mode; + else if (ret == VOIDmode) + ret = mode; + } + if (ret != VOIDmode) + return ret; + return opt_machine_mode (); +} + /* Find a mode that is suitable for representing a vector with NUNITS elements of mode INNERMODE, if one exists. The returned mode can be either an integer mode or a vector mode. */ --- gcc/gimple-fold.cc.jj 2024-02-20 10:25:26.297760979 +0100 +++ gcc/gimple-fold.cc 2024-02-27 12:42:38.338925573 +0100 @@ -995,9 +995,12 @@ gimple_fold_builtin_memory_op (gimple_st if (warning != OPT_Wrestrict) return false; - scalar_int_mode mode; - if (int_mode_for_size (ilen * 8, 0).exists (&mode) - && GET_MODE_SIZE (mode) * BITS_PER_UNIT == ilen * 8 + scalar_int_mode imode; + machine_mode mode; + if (int_mode_for_size (ilen * BITS_PER_UNIT, 0).exists (&imode) + && bitwise_mode_for_size (ilen + * BITS_PER_UNIT).exists (&mode) + && known_eq (GET_MODE_BITSIZE (mode), ilen * BITS_PER_UNIT) /* If the destination pointer is not aligned we must be able to emit an unaligned store. */ && (dest_align >= GET_MODE_ALIGNMENT (mode) @@ -1005,7 +1008,7 @@ gimple_fold_builtin_memory_op (gimple_st || (optab_handler (movmisalign_optab, mode) != CODE_FOR_nothing))) { - tree type = build_nonstandard_integer_type (ilen * 8, 1); + tree type = bitwise_type_for_mode (mode); tree srctype = type; tree desttype = type; if (src_align < GET_MODE_ALIGNMENT (mode)) --- gcc/testsuite/gcc.dg/bitint-91.c.jj 2024-02-27 12:08:15.230481756 +0100 +++ gcc/testsuite/gcc.dg/bitint-91.c 2024-02-27 12:08:15.230481756 +0100 @@ -0,0 +1,38 @@ +/* PR tree-optimization/113988 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-O2" } */ +/* { dg-additional-options "-mavx512f" { target i?86-*-* x86_64-*-* } } */ + +int i; + +#if __BITINT_MAXWIDTH__ >= 256 +void +foo (void *p, _BitInt(256) x) +{ + __builtin_memcpy (p, &x, sizeof x); +} + +_BitInt(256) +bar (void *p, _BitInt(256) x) +{ + _BitInt(246) y = x + 1; + __builtin_memcpy (p, &y, sizeof y); + return x; +} +#endif + +#if __BITINT_MAXWIDTH__ >= 512 +void +baz (void *p, _BitInt(512) x) +{ + __builtin_memcpy (p, &x, sizeof x); +} + +_BitInt(512) +qux (void *p, _BitInt(512) x) +{ + _BitInt(512) y = x + 1; + __builtin_memcpy (p, &y, sizeof y); + return x; +} +#endif