From patchwork Wed Mar 11 13:04:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 1252895 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=XX1z61J8; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48csdj4c8xz9sPJ for ; Thu, 12 Mar 2020 00:05:41 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 852603877024; Wed, 11 Mar 2020 13:05:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 852603877024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1583931937; bh=JV7JOduueAFpYH20ZpJQ4YBddEabYGRsQPiXRQSigxQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=XX1z61J8BO1r+Mka499rsXU6yihoFbFu16vTKgkYOWAlrbcZDGKxzO7dzsr1uOe66 n9eyWFnai6Ox5+t8CZkHPTSuKLMj2bp/TrDNCE50Zb0Z+rIf9xpFCwlf26q/mLoFRR jxaTxmh9ykd8d7QEx8tsGzYH1KnbYOxBmgVqkTus= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa2.hgst.iphmx.com (esa2.hgst.iphmx.com [68.232.143.124]) by sourceware.org (Postfix) with ESMTPS id 6C38C385F022 for ; Wed, 11 Mar 2020 13:05:32 +0000 (GMT) IronPort-SDR: tRzGFHoDPY7adQ1VzL3V/rKz0+MAcNPhyLUjOd6el0dmmkq3BOxdbLYsSarixeOmVuPeJI/flP 2/cBl49kjyKtL/wdCb5PeCBUrGeokaUpjbUqBLC9jm6PEOQci2lfSj79EtuUDzzYsZbGCZxSPj f81D3W/h8q/tGQeUemp8KxHJjs1QDJuuRdCcb1fETHqYIMrS3JAI7RPP52PAwNaC91Ojsq53GL U+XBj5qFQOzgi8GxJhpw/Xj2gmkSEbh9KyA6oVI0ZSWeWdbXdEbb2EcCr23ncwca73CyMmdzva cYs= X-URL-LookUp-ScanningError: 1 X-IronPort-AV: E=Sophos;i="5.70,541,1574092800"; d="scan'208,217";a="234204543" Received: from mail-bn8nam11lp2175.outbound.protection.outlook.com (HELO NAM11-BN8-obe.outbound.protection.outlook.com) ([104.47.58.175]) by ob1.hgst.iphmx.com with ESMTP; 11 Mar 2020 21:04:51 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IUQ6JhksITB380ODGYR7I0xKBuEScWMeyoiimPPKfQ9/KdalbXhWgPGQIrKb5haRxRGAMVd10LQoD/wwryCO4IDozNiD8SutqoHtG4/rA0n3x3nMn2gaf59RFyARkEFcL+RifUcMD8/YfLX+7AojHXJYw8IlsjQ7XztaS7+LonsKgSEAhnnx6i632TbzgXdZLPKSrnJ3X3OVreFWxDmqKe+19eWY0BSbWKo+XVPWIIhCIDtVsBy3zIyC6vYCL055dBaoQ0EZaHhAbduXmc1rNrPaqCvTqSciH40Mz4EV2f++xEhHDS9CAJB+o+6rV9btyeBoDQBa9We7N3HnpeAW3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9u4s+CMt0refINFF5gyyu1YB2Wc9uqKC8+et3Jizu9Y=; b=KiXf2Ivvqte+5SoIn5/u8yTQbuuKaqk7bb3cZaGKXu48TGEYV8uspZsuygmbYdWnDvYfcYIRXII6Kyu4cVrbZXfSgxcBuirrsCPq3lwO1P0wulCzlpA0lRXRsfoAjC5Luo0ms1Bse3/aMkuymang714unYcP9bIMNnL35rM4NClDhtQuhd/skdswHa3vsfhEQSX7PQaaAS3FNl9KmgM4ilTk/PfRTVz5icZ+v02K2byfqhKhnoalg1Y3KheqbiwR+KYgVxUS6vXQeugrmbtcLJ2pfEHDycT8/2HPkqcW4fW6jUqH4YQjHOfuRxOWbjXRv0iAZKFDzYlkK/LgPK2pkg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none Received: from MN2PR04MB5550.namprd04.prod.outlook.com (2603:10b6:208:d8::25) by MN2PR04MB5854.namprd04.prod.outlook.com (2603:10b6:208:3c::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2793.16; Wed, 11 Mar 2020 13:04:45 +0000 Received: from MN2PR04MB5550.namprd04.prod.outlook.com ([fe80::d8e7:bfda:a65a:9fde]) by MN2PR04MB5550.namprd04.prod.outlook.com ([fe80::d8e7:bfda:a65a:9fde%6]) with mapi id 15.20.2793.018; Wed, 11 Mar 2020 13:04:44 +0000 To: "gcc-patches@gcc.gnu.org" Subject: Remove redundant zero extend Thread-Topic: Remove redundant zero extend Thread-Index: AdX3pPyK+A650lsnQYK9G2Pu3KbQ3A== Date: Wed, 11 Mar 2020 13:04:44 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: x-originating-ip: [212.25.79.133] x-ms-publictraffictype: Email x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 6a3a8ef3-4b65-4e02-3a29-08d7c5bcc53c x-ms-traffictypediagnostic: MN2PR04MB5854: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:8882; x-forefront-prvs: 0339F89554 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(4636009)(396003)(366004)(39860400002)(136003)(346002)(376002)(199004)(52536014)(66446008)(33656002)(71200400001)(26005)(186003)(4326008)(64756008)(6916009)(5660300002)(55016002)(9686003)(8936002)(86362001)(3480700007)(66616009)(66556008)(2906002)(7696005)(478600001)(6506007)(316002)(81166006)(66946007)(8676002)(76116006)(81156014)(54906003)(66476007); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR04MB5854; H:MN2PR04MB5550.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: uaPidnHpmU/5mY4EZr8AW4VPzz6kfg6yzhAgX5acfxoGZzELi4GIfMpbWXzTJTFPXsWE0d08m0TsetEX2md5qHnD+5FqwLVu0781UIWqkKmlpD/BJSfLZoqrbtCSqBfyVjBxZbCtrs11MIk8CVq4T8nv7OLt8T2KMELXXXi5YxGEG4hGy2M1qn+M6fXNXdI1tr5pURu79PJkYZxujIoWndkOgYxyU9wb8wWCDK0FDnfnvpmGIn5GQvnFMeu/TqYW+iKUnPd8IuS0M3fxZf1MQ2oRCI7iCwa8uU6yS0iWdgWJJC51yVveH8fRTUQVbOBdoS77VEI3QWDHSkVCXKKuUZeN5b+njra4K7XwJzMuHWLlLT3gK2zArIRNHYMtKEYrOtzcf/uLNRuCMPtR+qp/jlW1p26gwQ6C5fAGjknreI1YJrFEeMHizVEK2Fd4Rf9O1J18BPCzNcJMGZl8x1WJgxEHWwalU+bPAfMRQL+nhwdFOPi1loTvVYxRIJK9ygR3mRhXkWBHGQ74ywHfDnGGsg== x-ms-exchange-antispam-messagedata: +ltITEawfIPciIDGSRVFDAdUsU66xl1OtTg3USo16rAuINrqJyyEQ3HI+x4Cn08X5u1LG0qDIsUZZOS/bKEMTPunxLExLSY3hiEf6vB5fA2MYAnoBFXtW1py28zTNNqUjs71Sz+yFHVnfNxT5BpdHw== MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6a3a8ef3-4b65-4e02-3a29-08d7c5bcc53c X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Mar 2020 13:04:44.7857 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 498fjAvq8wKJN59fuEktffSXppgEx/MwqEgI+RCCj1ReYti71Uq8BqVeMNYuRy+T0x6/eiNq0a8G1IOfqnuugQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5854 X-Spam-Status: No, score=-26.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, HTML_MESSAGE, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Nidal Faour via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: Nidal Faour Cc: Ofer Shinaar , Craig Blackmore Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This patch is a code density oriented and attempt to remove redundant sign/zero extension from assignment statement. The approach taken is to use VRP data while expanding the assignment to RTL to determine whether a sign/zero extension is necessary. Thought the motivation of the patch is code density but it also good for speed. for example: extern unsigned int func (); unsigned char foo (unsigned int arg) { if (arg == 2) return 0; return (func() == arg || arg == 7); } the result of the comparison in the return will yield a False or True, which will be converted to integer and then casting to unsigned char. this casting from integer to unsigned char is redundant because the value is either 0 or 1. this patch is targeting the RISCV-32bit only. This patch has been tested on a real embedded project and saved about 0.2% of code size. P.S. I have an FSF license under Western Digital incorporation. P.S. I've attached the patch as a file in case my email client corrupted the patch itself Regards, Nidal Faour diff --git a/gcc/cfgexpand.c b/gcc/cfgexpand.c index 9864e4344d2..c3739b2f331 100644 --- a/gcc/cfgexpand.c +++ b/gcc/cfgexpand.c @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with GCC; see the file COPYING3. If not see . */ +#include #include "config.h" #include "system.h" #include "coretypes.h" @@ -73,6 +74,8 @@ along with GCC; see the file COPYING3. If not see #include "tree-ssa-address.h" #include "output.h" #include "builtins.h" +#include "tree-vrp.h" + /* Some systems use __main in a way incompatible with its use in gcc, in these cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to @@ -3640,6 +3643,107 @@ expand_clobber (tree lhs) } } +/* Print the ranges of the operand and save it to the dump file + used for debug purposes only. */ + +void +print_range (tree operand, wide_int min, wide_int max) +{ + pretty_printer buffer; + pp_needs_newline (&buffer) = false; + buffer.buffer->stream = dump_file; + fprintf (dump_file, "Range for lhs: ["); + pp_wide_int (&buffer, min, TYPE_SIGN (TREE_TYPE (operand))); + pp_printf (&buffer, ", "); + pp_wide_int (&buffer, max, TYPE_SIGN (TREE_TYPE (operand))); + pp_printf (&buffer, "]"); + pp_flush (&buffer); + fprintf (dump_file, "\n"); +} + + + +/* Check that REG and SUBREG modes match between src and dest, + and that there is a cast to be removed. */ + +bool +cast_to_remove_p (rtx src, rtx dest, machine_mode from + , machine_mode to) +{ + if (GET_CODE (src) != SUBREG + || GET_CODE (dest) != SUBREG + || GET_CODE (SUBREG_REG (src)) != REG + || GET_CODE (SUBREG_REG (dest)) != REG + || GET_MODE (src) != GET_MODE (dest) + || GET_MODE (SUBREG_REG (src)) != GET_MODE (SUBREG_REG (dest)) + || GET_MODE (src) != to + || GET_MODE (SUBREG_REG (src)) != from) + return false; + return true; +} + +bool +remove_cast_p (rtx temp, rtx target, gassign *assign_stmt) +{ + enum gimple_code code; + value_range_kind lhs_range, rhs_range; + unsigned int ops_num; + wide_int lhs_min, lhs_max, rhs_min, rhs_max; + if (gimple_assign_cast_p (assign_stmt)) + { + code = gimple_code (assign_stmt); + ops_num = gimple_num_ops (assign_stmt); + if (code == GIMPLE_ASSIGN && ops_num < 3) + { + tree lhs = gimple_assign_lhs (assign_stmt); + if (POINTER_TYPE_P (TREE_TYPE (lhs))) + return false; + lhs_range = get_range_info (lhs, &lhs_min, &lhs_max); + if (dump_file && lhs_range == VR_RANGE) + print_range (lhs, lhs_min, lhs_max); + + tree rhs = gimple_assign_rhs1 (assign_stmt); + if (POINTER_TYPE_P (TREE_TYPE (rhs))) + return false; + rhs_range = get_range_info (rhs, &rhs_min, &rhs_max); + if (rhs_range == VR_RANGE) + { + unsigned int rhs_precision + = std::max (wi::min_precision (rhs_max, TYPE_SIGN (TREE_TYPE (rhs))), + wi::min_precision (rhs_min, TYPE_SIGN (TREE_TYPE (rhs)))); + unsigned int lhs_precision = TYPE_PRECISION (TREE_TYPE (lhs)); + if (lhs_precision > rhs_precision) + { + + if (dump_file) + { + print_range (rhs, rhs_min, rhs_max); + if (lhs_precision > rhs_precision) + fprintf (dump_file, + "EXPAND-CAST: This casting can be optimized\n"); + } + + return cast_to_remove_p (temp, + target, + TYPE_MODE (TREE_TYPE (rhs)), + TYPE_MODE (TREE_TYPE (lhs))); + } + else + if (dump_file) + fprintf (dump_file, "EXPAND-CAST: lhs_precision < rhs_precision\n"); + } + else + if (dump_file) + fprintf (dump_file, "EXPAND-CAST: Range is not VR_RANGE\n"); + } + else + if (dump_file) + fprintf (dump_file, "EXPAND-CAST: Got more than 2 ops \n"); + } + return false; +} + + /* A subroutine of expand_gimple_stmt, expanding one gimple statement STMT that doesn't require special handling for outgoing edges. That is no tailcalls and no GIMPLE_COND. */ @@ -3801,6 +3905,11 @@ expand_gimple_stmt_1 (gimple *stmt) temp = convert_modes (GET_MODE (SUBREG_REG (target)), GET_MODE (target), temp, unsignedp); } + if (targetm.remove_redundant_cast) + if (remove_cast_p (temp, + target, + assign_stmt)) + temp = SUBREG_REG (temp); convert_move (SUBREG_REG (target), temp, unsignedp); } diff --git a/gcc/config/riscv/riscv.c b/gcc/config/riscv/riscv.c index 94b5ac01762..504b1abf85e 100644 --- a/gcc/config/riscv/riscv.c +++ b/gcc/config/riscv/riscv.c @@ -5226,6 +5226,9 @@ riscv_hard_regno_rename_ok (unsigned from_regno ATTRIBUTE_UNUSED, #undef TARGET_MACHINE_DEPENDENT_REORG #define TARGET_MACHINE_DEPENDENT_REORG riscv_reorg +#undef TARGET_REMOVE_REDUNDANT_CAST +#define TARGET_REMOVE_REDUNDANT_CAST true + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-riscv.h" diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 19985adac3e..3a0d4813d96 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -12194,3 +12194,9 @@ This target hook can be used to generate a target-specific code @deftypefn {Target Hook} void TARGET_RUN_TARGET_SELFTESTS (void) If selftests are enabled, run any selftests for this target. @end deftypefn + +@deftypevr {Target Hook} bool TARGET_REMOVE_REDUNDANT_CAST +If this flag is true, @code{remove_cast_p} will be called +and remove unneeded cast +The default is false. +@end deftypevr \ No newline at end of file diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index 1a16150bfc5..984e18e1697 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -8178,3 +8178,5 @@ maintainer is familiar with. @hook TARGET_SPECULATION_SAFE_VALUE @hook TARGET_RUN_TARGET_SELFTESTS + +@hook TARGET_REMOVE_REDUNDANT_CAST \ No newline at end of file diff --git a/gcc/target.def b/gcc/target.def index b5e82ff826e..014e5a22d4d 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -6770,6 +6770,15 @@ following it should not be run. Usually true only for virtual assembler\n\ targets.", bool, false) +/* True if remove_redundant_cast should be called to remove unneeded + casting. */ +DEFHOOKPOD +(remove_redundant_cast, + "If this flag is true, @code{remove_cast_p} will be called\n\ +and remove unneeded cast\n\ +The default is false.", + bool, false) + /* Leave the boolean fields at the end. */ /* Functions related to mode switching. */ @@ -6835,4 +6844,3 @@ DEFHOOK /* Close the 'struct gcc_target' definition. */ HOOK_VECTOR_END (C90_EMPTY_HACK) - ----------------------------------------------------------------------- Change log: diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 085ef66a207..ffe7e061985 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,16 @@ +2020-02-19 Nidal Faour + + * doc/tm.texi.in (TARGET_REMOVE_REDUNDANT_CAST): New hook. + * doc/tm.texi: Regenerate. + *cfgexpand.c (algorith.h): New include. + (tree-vrp.h): Likewise + (print_range): New function + (check_assign_can_be_free): Likewise + (adjust_assign_to_remove_cast): Likewise + (expand_gimple_stmt_1): Call adjust_assign_to_remove_cast + *config/riscv/riscv.c (TARGET_REMOVE_REDUNDANT_CAST): Define + *target.def (remove_redundant_cast): New target hook + 2020-03-09 Jason Merrill * gdbinit.in (pgs): Fix typo in documentation. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f91af78a302..c5a701e08af 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2020-02-19 Nidal Faour + * gcc.target/riscv/masking-to-byte-1.c: New test + * gcc.target/riscv/masking-to-byte-2.c: New test + 2020-03-09 Marek Polacek PR c++/92031 - bogus taking address of rvalue error.