From patchwork Tue Feb 27 08:17:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 86418 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 290D43858C31 for ; Tue, 27 Feb 2024 08:17:58 +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 0FD853858C54 for ; Tue, 27 Feb 2024 08:17:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0FD853858C54 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 0FD853858C54 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=1709021850; cv=none; b=cw5qyFWTaqfkrrlwvUL2AiKsE1adi8LaI9C2sp3GRefxb0fMQYxbQDVgHLM6icwURPW5RUPuWQWlD04TfaMRQj+gpcO1CFXjjttjgHIhCQhq7NqRee1+X7x5CDKHCF7sE8L2nwShHUaYn3s5tqSdOeKiOaBAwu6mY/EcfzFKzog= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709021850; c=relaxed/simple; bh=AxXIJSNTK6ZnN5zabq54C7Wu+NQolrmQxcR86mNYD4U=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=OfQ0x6/aXo7ShGWhv90dtN4rZu+ij2rObqTS2mNKzhxhTMLxdBoYDa7ijrVt6raJzkUMPXcbT7H5wDLPE1hxkw1abLgw6a/zpG7nYC4IN5MRZlcMSoAXJ7c0HrI42rZs8nXefZs/27S1Rw4wrwRJn9zDHbG5W8DslwfCixNY8fQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709021846; 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=XgYpKZJGCP8uzag1s+cSNB+hbBb1zIyYEIlYh2tLV9Q=; b=h76hsiopQG+2frBGIy9XjxztjwFyD10vzqO+d4qzj7fX5riKYY0wH8mZCzxZdAAsOsrVPi uCVUgztVrL0CtbyUkB8F+ZzL6j1tOqQvUmuhPcT7Li7o2Hgb5sEVrJbTdosBd8enulAFmh 2HJmPwXM6sMAdt/xXgZBwvSJ0iIR6Gw= 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-277-rRHB3f1MNxGum4Z3V0HIQQ-1; Tue, 27 Feb 2024 03:17:24 -0500 X-MC-Unique: rRHB3f1MNxGum4Z3V0HIQQ-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 A6F0B380606E; Tue, 27 Feb 2024 08:17:24 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6AF7B1CBC8; Tue, 27 Feb 2024 08:17:24 +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 41R8HLsP060273 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 27 Feb 2024 09:17:22 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41R8HLnf060272; Tue, 27 Feb 2024 09:17:21 +0100 Date: Tue, 27 Feb 2024 09:17:20 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] expand: Add trivial folding for bit query builtins at expansion time [PR114044] 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.7 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+patchwork=sourceware.org@gcc.gnu.org Hi! While it seems a lot of places in various optimization passes fold bit query internal functions with INTEGER_CST arguments to INTEGER_CST when there is a lhs, when lhs is missing, all the removals of such dead stmts are guarded with -ftree-dce, so with -fno-tree-dce those unfolded ifn calls remain in the IL until expansion. If they have large/huge BITINT_TYPE arguments, there is no BLKmode optab and so expansion ICEs, and bitint lowering doesn't touch such calls because it doesn't know they need touching, functions only containing those will not even be further processed by the pass because there are no non-small BITINT_TYPE SSA_NAMEs + the 2 exceptions (stores of BITINT_TYPE INTEGER_CSTs and conversions from BITINT_TYPE INTEGER_CSTs to floating point SSA_NAMEs) and when walking there is no special case for calls with BITINT_TYPE INTEGER_CSTs either, those are for normal calls normally handled at expansion time. So, the following patch adjust the expansion of these 6 ifns, by doing nothing if there is no lhs, and also just in case and user disabled all possible passes that would fold this handles the case of setting lhs to ifn call with INTEGER_CST argument. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-27 Jakub Jelinek PR rtl-optimization/114044 * internal-fn.def (CLRSB, CLZ, CTZ, FFS, PARITY): Use DEF_INTERNAL_INT_EXT_FN macro rather than DEF_INTERNAL_INT_FN. * internal-fn.h (expand_CLRSB, expand_CLZ, expand_CTZ, expand_FFS, expand_PARITY): Declare. * internal-fn.cc (expand_bitquery, expand_CLRSB, expand_CLZ, expand_CTZ, expand_FFS, expand_PARITY): New functions. (expand_POPCOUNT): Use expand_bitquery. * gcc.dg/bitint-95.c: New test. Jakub --- gcc/internal-fn.def.jj 2024-01-03 11:51:32.411718977 +0100 +++ gcc/internal-fn.def 2024-02-26 13:20:51.682005459 +0100 @@ -440,11 +440,11 @@ DEF_INTERNAL_OPTAB_FN (COMPLEX_FMS, ECF_ DEF_INTERNAL_OPTAB_FN (COMPLEX_FMS_CONJ, ECF_CONST, cmls_conj, ternary) /* Unary integer ops. */ -DEF_INTERNAL_INT_FN (CLRSB, ECF_CONST | ECF_NOTHROW, clrsb, unary) -DEF_INTERNAL_INT_FN (CLZ, ECF_CONST | ECF_NOTHROW, clz, unary) -DEF_INTERNAL_INT_FN (CTZ, ECF_CONST | ECF_NOTHROW, ctz, unary) -DEF_INTERNAL_INT_FN (FFS, ECF_CONST | ECF_NOTHROW, ffs, unary) -DEF_INTERNAL_INT_FN (PARITY, ECF_CONST | ECF_NOTHROW, parity, unary) +DEF_INTERNAL_INT_EXT_FN (CLRSB, ECF_CONST | ECF_NOTHROW, clrsb, unary) +DEF_INTERNAL_INT_EXT_FN (CLZ, ECF_CONST | ECF_NOTHROW, clz, unary) +DEF_INTERNAL_INT_EXT_FN (CTZ, ECF_CONST | ECF_NOTHROW, ctz, unary) +DEF_INTERNAL_INT_EXT_FN (FFS, ECF_CONST | ECF_NOTHROW, ffs, unary) +DEF_INTERNAL_INT_EXT_FN (PARITY, ECF_CONST | ECF_NOTHROW, parity, unary) DEF_INTERNAL_INT_EXT_FN (POPCOUNT, ECF_CONST | ECF_NOTHROW, popcount, unary) DEF_INTERNAL_FN (GOMP_TARGET_REV, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW, NULL) --- gcc/internal-fn.h.jj 2024-01-03 11:51:28.313775852 +0100 +++ gcc/internal-fn.h 2024-02-26 13:22:09.532917080 +0100 @@ -262,6 +262,11 @@ extern void expand_MULBITINT (internal_f extern void expand_DIVMODBITINT (internal_fn, gcall *); extern void expand_FLOATTOBITINT (internal_fn, gcall *); extern void expand_BITINTTOFLOAT (internal_fn, gcall *); +extern void expand_CLRSB (internal_fn, gcall *); +extern void expand_CLZ (internal_fn, gcall *); +extern void expand_CTZ (internal_fn, gcall *); +extern void expand_FFS (internal_fn, gcall *); +extern void expand_PARITY (internal_fn, gcall *); extern void expand_POPCOUNT (internal_fn, gcall *); extern bool vectorized_internal_fn_supported_p (internal_fn, tree); --- gcc/internal-fn.cc.jj 2024-01-05 11:16:49.568173526 +0100 +++ gcc/internal-fn.cc 2024-02-26 14:03:58.265848223 +0100 @@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. #include "explow.h" #include "rtl-iter.h" #include "gimple-range.h" +#include "fold-const-call.h" /* For lang_hooks.types.type_for_mode. */ #include "langhooks.h" @@ -5107,9 +5108,63 @@ expand_BITINTTOFLOAT (internal_fn, gcall emit_move_insn (target, val); } +static bool +expand_bitquery (internal_fn fn, gcall *stmt) +{ + tree lhs = gimple_call_lhs (stmt); + if (lhs == NULL_TREE) + return false; + tree arg = gimple_call_arg (stmt, 0); + if (TREE_CODE (arg) == INTEGER_CST) + { + tree ret = fold_const_call (as_combined_fn (fn), TREE_TYPE (arg), arg); + gcc_checking_assert (ret && TREE_CODE (ret) == INTEGER_CST); + expand_assignment (lhs, ret, false); + return false; + } + return true; +} + +void +expand_CLRSB (internal_fn fn, gcall *stmt) +{ + if (expand_bitquery (fn, stmt)) + expand_unary_optab_fn (fn, stmt, clrsb_optab); +} + +void +expand_CLZ (internal_fn fn, gcall *stmt) +{ + if (expand_bitquery (fn, stmt)) + expand_unary_optab_fn (fn, stmt, clz_optab); +} + +void +expand_CTZ (internal_fn fn, gcall *stmt) +{ + if (expand_bitquery (fn, stmt)) + expand_unary_optab_fn (fn, stmt, ctz_optab); +} + +void +expand_FFS (internal_fn fn, gcall *stmt) +{ + if (expand_bitquery (fn, stmt)) + expand_unary_optab_fn (fn, stmt, ffs_optab); +} + +void +expand_PARITY (internal_fn fn, gcall *stmt) +{ + if (expand_bitquery (fn, stmt)) + expand_unary_optab_fn (fn, stmt, parity_optab); +} + void expand_POPCOUNT (internal_fn fn, gcall *stmt) { + if (!expand_bitquery (fn, stmt)) + return; if (gimple_call_num_args (stmt) == 1) { expand_unary_optab_fn (fn, stmt, popcount_optab); --- gcc/testsuite/gcc.dg/bitint-95.c.jj 2024-02-26 16:16:09.192065858 +0100 +++ gcc/testsuite/gcc.dg/bitint-95.c 2024-02-26 14:19:30.079824133 +0100 @@ -0,0 +1,45 @@ +/* PR rtl-optimization/114044 */ +/* { dg-do compile { target bitint575 } } */ +/* { dg-options "-O -fno-tree-dce" } */ + +void +foo (void) +{ + unsigned _BitInt (575) a = 3; + __builtin_clzg (a); +} + +void +bar (void) +{ + unsigned _BitInt (575) a = 3; + __builtin_ctzg (a); +} + +void +baz (void) +{ + signed _BitInt (575) a = 3; + __builtin_clrsbg (a); +} + +void +qux (void) +{ + signed _BitInt (575) a = 3; + __builtin_ffsg (a); +} + +void +garply (void) +{ + unsigned _BitInt (575) a = 3; + __builtin_parityg (a); +} + +void +corge (void) +{ + unsigned _BitInt (575) a = 3; + __builtin_popcountg (a); +}