From patchwork Fri Nov 3 19:43:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 161421 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp1274685vqu; Fri, 3 Nov 2023 12:43:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJbpFvYPsOBVPUuIxu4kQPTruOuAYniCMycESA2BcVuRTN9/Hogo9cOR7Qrgj8NS+1TYZH X-Received: by 2002:ac8:5c48:0:b0:411:5528:64f6 with SMTP id j8-20020ac85c48000000b00411552864f6mr27571849qtj.27.1699040619920; Fri, 03 Nov 2023 12:43:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1699040619; cv=pass; d=google.com; s=arc-20160816; b=QizsBhs5y23uCAgnv8kEHlCGVIs0X9PCfoLxbGzK+PckJ64Jzexl8NO246GHOOq2bc y58a2gclPvS3C1T0nzprvmZ8onXVSGg5sgmlpC5d77H/oYPeYmeHQ32reIYjvNXsB4e2 RllStB+vO8vgiVzF0+jW3K1z7iatnMNatm4iqN3WNo7qqqN+4BEDzdKktlaaaW4TBHIm 7/5qpPdghjvxGe6rKjkcRtFtGsoNZiDgdSdeM1oSHbV2q+kiBp5eApjsQvpweDBAV5Vt rby1mGjC2PRXr/4MuJsi09jA8pAtacwTpmgE8K/RdOgHrOW7zEFqqgqZaZHA6hCAih8Y eJ6A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-language:thread-index :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=k30sctjr6X/Q+F5ij2VTdjyiQKehKXswGU8Txmld764=; fh=Ht8bq8SVyF6sx4+E7Os+tBO2MuNVfxRGp/jyiuwYCzE=; b=hAh/lXcQQlEzTlzuxq5y3qJL2mFWRlwb1dweu+G8HGe81dtZKdG1WKHa4CHfuW/ycm V4YughTmTgoGAqjLilTym27sE4UWM6FTknkg6fq1rtMiqLkFEx7d2T8YvlhqzFmpfmrw m/f7bK7eQGoYbNREEicHug6GsvLf9WWkTY0aFatvmGNq7bM6ex7eV4I940tBs7FSoxN5 Yy9BqYK3uwhATJ/XPxikmEGCLQiaZJg/8yPRrJDhxalGNYA5Hh+IEs3I2GWX529Ba17r QMhv9m3dNYku0gM1ralhEaCOEpGdSbTht+x9IwFOs0UadzQ2xfFc6BvE0yT2d8/A1PFY 2SMA== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=VCQz18Bf; 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" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d10-20020ac85d8a000000b0041cbafcc5a9si1936319qtx.636.2023.11.03.12.43.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 12:43:39 -0700 (PDT) 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=fail header.i=@nextmovesoftware.com header.s=default header.b=VCQz18Bf; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AC3AA3857C40 for ; Fri, 3 Nov 2023 19:43:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id DB96D3858D28 for ; Fri, 3 Nov 2023 19:43:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB96D3858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DB96D3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=162.254.253.69 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699040595; cv=none; b=syiUq1tGy1pFrl4XwiopEeBXCIYUIvM2nrHKeesA6X4z44dVfvqb1+L0Q3iAwyNKRuomsOwt22I9MzhJsrmEaMHlS27/rb0EHCtfqk4fAjnAfOrEMZSTrnk5I4gj8IhQENVLox2FNak5AWMPwaihUa3ETV/C+0ob2A5kaZV9rQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699040595; c=relaxed/simple; bh=oINm+nj9z3QwKt+v6cg6zPsiOXGZwBp24upHDhyM5tE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Qmlfwd8qTJ+ffKmOjALEuw2XgNkhTxTIzmxSaqmbnGS0dBi43xDxyl/k+jcoKXIjyoCZdc8UURnqX8Bu3jQhWwbhsxiuDdi8hWBvOjYPpqEszl4r3en58CC+ZP3t+fDa6H6idS+3N0CjFNmEtgQA93JkWp4FpGk7m00t0eiCXa0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=k30sctjr6X/Q+F5ij2VTdjyiQKehKXswGU8Txmld764=; b=VCQz18BfNHjRVZhgdh4dSHqTep HNz4oWnLfM81UiPHrC7wBUPYqNr1Y39Zt5MVdINjFKKgdguNU9G93rSUFO2uLBsBrEVS/sAu28iNJ 8www33Wjfs3QIGPV8unt9dZRRg9hbUywdy5XwZewXNSDGhCaj2jdy70eeZLmXmrLKJmT1eL7fwNvL o6FvALTXYAvGGxi5UC0spZfz6GX2tCj2OJk7EPuGuxVpUs9G8GIGeQCSw3RZNAIwKJviXfgrQ03in 4BWMMmuHIq/qapWGQTMiR3SSsaH2xIIqmiVl3YAIG0CNWSuiFIUOZJyqjK0K6ltbf3by2Lox4HwSj Q7HOXfjg==; Received: from host86-160-20-38.range86-160.btcentralplus.com ([86.160.20.38]:51362 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1qz04W-0007za-0M; Fri, 03 Nov 2023 15:43:12 -0400 From: "Roger Sayle" To: Cc: "'Claudiu Zissulescu'" Subject: [ARC PATCH] Provide a TARGET_FOLD_BUILTIN target hook. Date: Fri, 3 Nov 2023 19:43:08 -0000 Message-ID: <00d401da0e8d$f9ea4b30$edbee190$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdoOjMjLB+XT8gsWTQefypuVn8Stkw== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781573217104796789 X-GMAIL-MSGID: 1781573217104796789 This patch implements a arc_fold_builtin target hook to allow ARC builtins to be folded at the tree-level. Currently this function converts __builtin_arc_swap into a LROTATE_EXPR at the tree-level, and evaluates __builtin_arc_norm and __builtin_arc_normw of integer constant arguments at compile-time. Because ARC_BUILTIIN_SWAP is now handled at the tree-level, UNSPEC_ARC_SWAP no longer used, allowing it and the "swap" define_insn to be removed. An example benefit of folding things at compile-time is that calling __builtin_arc_swap on the result of __builtin_arc_swap now eliminates both and generates no code, and likewise calling __builtin_arc_swap of a constant integer argument is evaluated at compile-time. Tested with a cross-compiler to arc-linux hosted on x86_64, with no new (compile-only) regressions from make -k check. Ok for mainline if this passes Claudiu's nightly testing? 2023-11-03 Roger Sayle gcc/ChangeLog * config/arc/arc.cc (TARGET_FOLD_BUILTIN): Define to arc_fold_builtin. (arc_fold_builtin): New function. Convert ARC_BUILTIN_SWAP into a rotate. Evaluate ARC_BUILTIN_NORM and ARC_BUILTIN_NORMW of constant arguments. * config/arc/arc.md (UNSPEC_ARC_SWAP): Delete. (normw): Make output template/assembler whitespace consistent. (swap): Remove define_insn, only use of SWAP UNSPEC. * config/arc/builtins.def: Tweak indentation. (SWAP): Expand using rotlsi2_cnt16 instead of using swap. gcc/testsuite/ChangeLog * gcc.target/arc/builtin_norm-1.c: New test case. * gcc.target/arc/builtin_norm-2.c: Likewise. * gcc.target/arc/builtin_normw-1.c: Likewise. * gcc.target/arc/builtin_normw-2.c: Likewise. * gcc.target/arc/builtin_swap-1.c: Likewise. * gcc.target/arc/builtin_swap-2.c: Likewise. * gcc.target/arc/builtin_swap-3.c: Likewise. Thanks in advance, Roger diff --git a/gcc/config/arc/arc.cc b/gcc/config/arc/arc.cc index e209ad2..70ee410 100644 --- a/gcc/config/arc/arc.cc +++ b/gcc/config/arc/arc.cc @@ -643,6 +643,9 @@ static rtx arc_legitimize_address_0 (rtx, rtx, machine_mode mode); #undef TARGET_EXPAND_BUILTIN #define TARGET_EXPAND_BUILTIN arc_expand_builtin +#undef TARGET_FOLD_BUILTIN +#define TARGET_FOLD_BUILTIN arc_fold_builtin + #undef TARGET_BUILTIN_DECL #define TARGET_BUILTIN_DECL arc_builtin_decl @@ -7048,6 +7051,46 @@ arc_expand_builtin (tree exp, return const0_rtx; } +/* Implement TARGET_FOLD_BUILTIN. */ + +static tree +arc_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED, tree *arg, + bool ignore ATTRIBUTE_UNUSED) +{ + unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl); + + switch (fcode) + { + default: + break; + + case ARC_BUILTIN_SWAP: + return fold_build2 (LROTATE_EXPR, integer_type_node, arg[0], + build_int_cst (integer_type_node, 16)); + + case ARC_BUILTIN_NORM: + if (TREE_CODE (arg[0]) == INTEGER_CST + && !TREE_OVERFLOW (arg[0])) + { + wide_int arg0 = wi::to_wide (arg[0], 32); + wide_int result = wi::shwi (wi::clrsb (arg0), 32); + return wide_int_to_tree (integer_type_node, result); + } + break; + + case ARC_BUILTIN_NORMW: + if (TREE_CODE (arg[0]) == INTEGER_CST + && !TREE_OVERFLOW (arg[0])) + { + wide_int arg0 = wi::to_wide (arg[0], 16); + wide_int result = wi::shwi (wi::clrsb (arg0), 32); + return wide_int_to_tree (integer_type_node, result); + } + break; + } + return NULL_TREE; +} + /* Returns true if the operands[opno] is a valid compile-time constant to be used as register number in the code for builtins. Else it flags an error and returns false. */ diff --git a/gcc/config/arc/arc.md b/gcc/config/arc/arc.md index 96ff62d..9e81d13 100644 --- a/gcc/config/arc/arc.md +++ b/gcc/config/arc/arc.md @@ -116,7 +116,6 @@ UNSPEC_TLS_OFF UNSPEC_ARC_NORM UNSPEC_ARC_NORMW - UNSPEC_ARC_SWAP UNSPEC_ARC_DIVAW UNSPEC_ARC_DIRECT UNSPEC_ARC_LP @@ -4355,8 +4354,8 @@ archs4x, archs4xd" (clrsb:HI (match_operand:HI 1 "general_operand" "cL,Cal"))))] "TARGET_NORM" "@ - norm%_ \t%0, %1 - norm%_ \t%0, %1" + norm%_\\t%0,%1 + norm%_\\t%0,%1" [(set_attr "length" "4,8") (set_attr "type" "two_cycle_core,two_cycle_core")]) @@ -4453,18 +4452,6 @@ archs4x, archs4xd" [(set_attr "type" "unary") (set_attr "length" "20")]) -(define_insn "swap" - [(set (match_operand:SI 0 "dest_reg_operand" "=w,w,w") - (unspec:SI [(match_operand:SI 1 "general_operand" "L,Cal,c")] - UNSPEC_ARC_SWAP))] - "TARGET_SWAP" - "@ - swap \t%0, %1 - swap \t%0, %1 - swap \t%0, %1" - [(set_attr "length" "4,8,4") - (set_attr "type" "two_cycle_core,two_cycle_core,two_cycle_core")]) - (define_insn "divaw" [(set (match_operand:SI 0 "dest_reg_operand" "=&w,&w,&w") (unspec:SI [(div:SI (match_operand:SI 1 "general_operand" "r,Cal,r") diff --git a/gcc/config/arc/builtins.def b/gcc/config/arc/builtins.def index fc5c92b..deb5f73 100644 --- a/gcc/config/arc/builtins.def +++ b/gcc/config/arc/builtins.def @@ -50,14 +50,14 @@ DEF_BUILTIN (CORE_WRITE, 2, void_ftype_usint_usint, core_write, 1) DEF_BUILTIN (SETI, 1, void_ftype_int, seti, TARGET_V2) /* Regular builtins. */ -DEF_BUILTIN (NORM, 1, int_ftype_int, clrsbsi2, TARGET_NORM) -DEF_BUILTIN (NORMW, 1, int_ftype_short, normw, TARGET_NORM) -DEF_BUILTIN (SWAP, 1, int_ftype_int, swap, TARGET_SWAP) -DEF_BUILTIN (DIVAW, 2, int_ftype_int_int, divaw, TARGET_EA_SET) -DEF_BUILTIN (CORE_READ, 1, usint_ftype_usint, core_read, 1) -DEF_BUILTIN (LR, 1, usint_ftype_usint, lr, 1) -DEF_BUILTIN (FFS, 1, int_ftype_int, ffs, (TARGET_EM && TARGET_NORM) || TARGET_HS) -DEF_BUILTIN (FLS, 1, int_ftype_int, fls, (TARGET_EM && TARGET_NORM) || TARGET_HS) +DEF_BUILTIN (NORM, 1, int_ftype_int, clrsbsi2, TARGET_NORM) +DEF_BUILTIN (NORMW, 1, int_ftype_short, normw, TARGET_NORM) +DEF_BUILTIN (SWAP, 1, int_ftype_int, rotlsi2_cnt16, TARGET_SWAP) +DEF_BUILTIN (DIVAW, 2, int_ftype_int_int, divaw, TARGET_EA_SET) +DEF_BUILTIN (CORE_READ, 1, usint_ftype_usint, core_read, 1) +DEF_BUILTIN (LR, 1, usint_ftype_usint, lr, 1) +DEF_BUILTIN (FFS, 1, int_ftype_int, ffs, (TARGET_EM && TARGET_NORM) || TARGET_HS) +DEF_BUILTIN (FLS, 1, int_ftype_int, fls, (TARGET_EM && TARGET_NORM) || TARGET_HS) /* ARC SIMD extenssion. */ /* BEGIN SIMD marker. */ diff --git a/gcc/testsuite/gcc.target/arc/builtin_norm-1.c b/gcc/testsuite/gcc.target/arc/builtin_norm-1.c new file mode 100644 index 0000000..7cb7e98 --- /dev/null +++ b/gcc/testsuite/gcc.target/arc/builtin_norm-1.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mnorm" } */ + +int foo(int x) +{ + return __builtin_arc_norm (x); +} + +/* { dg-final { scan-assembler "norm\\s+r0,r0" } } */ diff --git a/gcc/testsuite/gcc.target/arc/builtin_norm-2.c b/gcc/testsuite/gcc.target/arc/builtin_norm-2.c new file mode 100644 index 0000000..d90128b --- /dev/null +++ b/gcc/testsuite/gcc.target/arc/builtin_norm-2.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mnorm" } */ + +int foo() +{ + return __builtin_arc_norm (255); +} + +/* { dg-final { scan-assembler-not "norm\\s+r0,r0" } } */ +/* { dg-final { scan-assembler "mov_s\\s+r0,23" } } */ diff --git a/gcc/testsuite/gcc.target/arc/builtin_normw-1.c b/gcc/testsuite/gcc.target/arc/builtin_normw-1.c new file mode 100644 index 0000000..f7b46f3 --- /dev/null +++ b/gcc/testsuite/gcc.target/arc/builtin_normw-1.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mnorm" } */ + +int foo(short x) +{ + return __builtin_arc_normw (x); +} + +/* { dg-final { scan-assembler "normh\\s+r0, ?r0" } } */ diff --git a/gcc/testsuite/gcc.target/arc/builtin_normw-2.c b/gcc/testsuite/gcc.target/arc/builtin_normw-2.c new file mode 100644 index 0000000..bb37c84 --- /dev/null +++ b/gcc/testsuite/gcc.target/arc/builtin_normw-2.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mnorm" } */ + +int foo() +{ + return __builtin_arc_normw (255); +} + +/* { dg-final { scan-assembler-not "normh\\s+r" } } */ +/* { dg-final { scan-assembler "mov_s\\s+r0,7" } } */ diff --git a/gcc/testsuite/gcc.target/arc/builtin_swap-1.c b/gcc/testsuite/gcc.target/arc/builtin_swap-1.c new file mode 100644 index 0000000..5421e26 --- /dev/null +++ b/gcc/testsuite/gcc.target/arc/builtin_swap-1.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mswap" } */ + +int foo(int x) +{ + return __builtin_arc_swap (x); +} + +/* { dg-final { scan-assembler "swap\\s+r0,r0" } } */ diff --git a/gcc/testsuite/gcc.target/arc/builtin_swap-2.c b/gcc/testsuite/gcc.target/arc/builtin_swap-2.c new file mode 100644 index 0000000..9c2f24a --- /dev/null +++ b/gcc/testsuite/gcc.target/arc/builtin_swap-2.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mswap" } */ + +int foo() +{ + return __builtin_arc_swap (0x12345678); +} + +/* { dg-final { scan-assembler-not "swap\\s+r" } } */ diff --git a/gcc/testsuite/gcc.target/arc/builtin_swap-3.c b/gcc/testsuite/gcc.target/arc/builtin_swap-3.c new file mode 100644 index 0000000..14a3072 --- /dev/null +++ b/gcc/testsuite/gcc.target/arc/builtin_swap-3.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mswap" } */ + +int foo(int x) +{ + int t = __builtin_arc_swap (x); + return __builtin_arc_swap (t); +} + +/* { dg-final { scan-assembler-not "swap\\s+r" } } */