From patchwork Thu Jun 8 10:31:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oluwatamilore Adebayo X-Patchwork-Id: 70780 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 A3B1F385700C for ; Thu, 8 Jun 2023 10:32:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A3B1F385700C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686220328; bh=D9/N13lM9VQylpcqtVA78h6ZhbR7QKyCYuNHn1yznR4=; h=To:CC:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=LnrnD8j9xYh6iUZkOGTcNfwiCswxVRELksJQ8r2eyLxGQGUbsGGCbIQ+K31Nn25QR m7mj013D9IMfhaId6AU9GpFrXgZa6MXLewnotH50hiQXLH7CUZQ92sguFptedXIhYq hwunslGLQwweyyV5WGLbeB0Z2mqOi4cBTk/bVUcI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05on2076.outbound.protection.outlook.com [40.107.21.76]) by sourceware.org (Postfix) with ESMTPS id 1F2CA3858C62 for ; Thu, 8 Jun 2023 10:31:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1F2CA3858C62 Received: from AS9PR06CA0708.eurprd06.prod.outlook.com (2603:10a6:20b:49f::35) by PAXPR08MB7621.eurprd08.prod.outlook.com (2603:10a6:102:23f::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Thu, 8 Jun 2023 10:31:30 +0000 Received: from AM7EUR03FT020.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:49f:cafe::cc) by AS9PR06CA0708.outlook.office365.com (2603:10a6:20b:49f::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33 via Frontend Transport; Thu, 8 Jun 2023 10:31:30 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM7EUR03FT020.mail.protection.outlook.com (100.127.140.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.25 via Frontend Transport; Thu, 8 Jun 2023 10:31:30 +0000 Received: ("Tessian outbound e13c2446394c:v136"); Thu, 08 Jun 2023 10:31:30 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 516908f10e140f3a X-CR-MTA-TID: 64aa7808 Received: from bffeae94a351.2 by 64aa7808-outbound-1.mta.getcheckrecipient.com id D8CB8DDA-B3EE-4E68-BE52-C27B8A989FAB.1; Thu, 08 Jun 2023 10:31:18 +0000 Received: from EUR05-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id bffeae94a351.2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 08 Jun 2023 10:31:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CcHv9zZ7RWrwlWU50wRRaRmEtfyWyeXmC3TuJNBFVcTUwkrOuIC5Zj6UZx1SiEDJevtlDuVsNqYN4AZUEhqC5uIi2YWeMLhpuXidu3P/Vgo9jMJWdTpjCeSvcQxlBfUa5JnZMEC+BZS52WSleCiuGDyMwNYqmkaw9acDc9WxQAYxnILP2ZJR0UEpoV6n1PHH2aHpTUZUAcfEhVB3gSmgIsz5/mtuRv7BJzaSXTKxORDrLzGl7Q3hnCQEnWKqcBON0E4htir+32rVFjTXavmy7HOujt9mdn3zjpNr2GhD7XQCiID4N8BHJrqiDmqkkgw3JZn6ESYd0jOvbWIBE/E3IA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=D9/N13lM9VQylpcqtVA78h6ZhbR7QKyCYuNHn1yznR4=; b=nJRn5WZd8qBkf6KQFmd67glIPcr/9wlCbjcHSxUrIRjgIJ+nBFFb9d0WMTvd6IUL+JL8LRFer1zuOO9Z+4yt+2KbZgw7avEy8/9p/XwsnDaa48BPhBKmluqQrGbowV0x+BGyweEzHprhQMOZgbcktnZ+beR/pHbBndq8bot9Kn7dgdrcMbVSSJvXHj9GBQRpXETiACa5/9arlP1meVv5Fd7rVDBXokF+jTkSJybn7JlYf0++Ll7k+bEYcJV9aBIzLpFlPYJ36qw/ezxbr8KmpG34WwEiaA6cUmRm8J5M6xM4S6OJk/jYRaUkWiOQPTlhccIiU7nrQbErssYFD4/73A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=armh.onmicrosoft.com smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); arc=none Received: from AS9PR06CA0559.eurprd06.prod.outlook.com (2603:10a6:20b:485::23) by DBAPR08MB5575.eurprd08.prod.outlook.com (2603:10a6:10:1a6::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Thu, 8 Jun 2023 10:31:15 +0000 Received: from AM7EUR03FT037.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:485:cafe::64) by AS9PR06CA0559.outlook.office365.com (2603:10a6:20b:485::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33 via Frontend Transport; Thu, 8 Jun 2023 10:31:15 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; pr=C Received: from nebula.arm.com (40.67.248.234) by AM7EUR03FT037.mail.protection.outlook.com (100.127.140.225) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6477.25 via Frontend Transport; Thu, 8 Jun 2023 10:31:15 +0000 Received: from AZ-NEU-EX02.Emea.Arm.com (10.251.26.5) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Thu, 8 Jun 2023 10:31:14 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX02.Emea.Arm.com (10.251.26.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Thu, 8 Jun 2023 10:31:14 +0000 Received: from e119885.cambridge.arm.com (10.2.78.52) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23 via Frontend Transport; Thu, 8 Jun 2023 10:31:14 +0000 To: CC: , , Subject: [PATCH 1/2] Missed opportunity to use [SU]ABD Date: Thu, 8 Jun 2023 11:31:03 +0100 Message-ID: <20230608103103.23794-1-oluwatamilore.adebayo@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230608102830.23565-1-oluwatamilore.adebayo@arm.com> References: <20230608102830.23565-1-oluwatamilore.adebayo@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: AM7EUR03FT037:EE_|DBAPR08MB5575:EE_|AM7EUR03FT020:EE_|PAXPR08MB7621:EE_ X-MS-Office365-Filtering-Correlation-Id: b886cfe4-0ddb-4c61-3f1e-08db680b8620 x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: e2SiDxu7lOXN278GNlN7JIaDu4wRVF0+UQCZ7IWziZmod/+yUqZKPkW0jRlQlJZIXMwmQ2Y+1x/t7LCuF0yEGxI4JTzxdCqee3MwoDmonQX4VCKY3XNl2ND+hYjBnSrGLh1rz3U90DdLxXkprUwJhKSAiKyaqk9YD/odQbBwKcPqkF3wBv1sJKDlmWLSi3DWCv0V6YPDuPJ2EKK6mSoLH4VBVM+jR78UZhZHWjkTBdZgx9XSY+LtHbqxwCCHDMOj6l7UjUHp7vwE9nmPua1XvUs4X7N7J0RYdvfPNewsTFQy85N/bQxiflGiCxVLukRF4GLjGZi1nG4+Ly5HyQiE6XmUlHcZV8IYQ85jDJQBSLUIcRpWHAOB5kwUmHoj9Iw8OO2TFCmvY/h/1B4TTQTKybJq5yv5eTPZeuLujd8z4xQGATZTd6e2NB9ofqSetHOPO9kqrOQ7esFdbWIww2CxqJeKEvqgx1cv5JwadK5m/A2NSvPmMT717qaBYrYK5o+0uveragx33Hvt7D5Y9rmvpwjGqM9BGMJoMYflZnK+5cyat8dU0wyrEUCpbeF1sY+yBruXuexwUmjZ83rBgK0Ygc4w6r6LFwZNxGKYKgwhzbjmUnTsO0rW5rYt95qAq1If0W84XuAStSIdyZTy9WQurYso9y2lyZLWB/j4MR6dobSRsqr9BEFeJK2hwXxXltqsF/uR66Mk+HM45tImtrv9bLMtCq8+awBqBjg29sHa8dbAxBuRlfj2Yuh9nLBG0aXj X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:nebula.arm.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230028)(4636009)(376002)(346002)(39860400002)(136003)(396003)(451199021)(46966006)(36840700001)(83380400001)(44832011)(5660300002)(7049001)(81166007)(6200100001)(186003)(82310400005)(426003)(356005)(26005)(1076003)(336012)(2616005)(47076005)(36860700001)(82740400003)(54906003)(40480700001)(37006003)(2906002)(30864003)(6666004)(478600001)(86362001)(7696005)(36756003)(70586007)(70206006)(4326008)(8676002)(8936002)(6862004)(45080400002)(41300700001)(316002)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR08MB5575 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM7EUR03FT020.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 2236b119-23bb-400c-6f41-08db680b7cfa X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tcID2wJe17j1cIJxbqo1+MCRfaORUsFgNvlyxGnVXi2xcqM+8mq8N0JRMmxyB1vqBThs6AClXUdSIDdD7lG1DG3KJCtt4w6s/qmuuwqONqVofLXwXnSlhO0dZIus6R+VUmhsHos7VBKJ4mOs6EppgfOPEVk7xfIjWiVfLteKn7aKtd/81e/GHnvDvjcHzbRnmEmfWbjPQxFLvU5psFqe/o9Q0CF3X4/4VnukIb/B67Gv92+dQgsWAzPGGaD6+G6O3an8br9xy/2NAkajaewgZLn6u/9IR6fEKQQDE+MmCmrgR/WUmKrR6GwDXnQlF34Wfd9EQsl7+SavLJHTwWhkmUOqfiM/gr/4E4Vh1j1D0HMpHED0qtwZQ13kWoCH6fXOB1Aau2hyD3xn3FrEqM7tvB4/TWcCup6aSi0rD10BNtT+rozhlUpuPrnqoHIN/jJA+n9bDm/g9wqHOqfzf4/xiJbK1A6rYdYoaYaoM+/pJM4JGgqHNpY1539ThkB8xIDWPtwK8xOpq/qgDMJIO63wrdKCCIdFBjfvN8F3JQIc6Uqtj4JqXH2KB2Bfb+xd0q/LM2f1750yW8Qi+YWDjadLt79hh+5Boh2p/h11LugWgAnVW3jkLi1okqVHasCcMBdyiZEsYNksBnlevuK17k99BlNVSR8I5YgbYbJh6IS+QQ9QyztXLSow4wkypvJJOwOS7zFN7fVdEN4mUmG+OLXX/UUVU7mRyEtmSk66G4LiZuHOjFnHEKOYpJjHfzenpOTW X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230028)(4636009)(136003)(346002)(396003)(376002)(39860400002)(451199021)(36840700001)(40470700004)(46966006)(37006003)(54906003)(40460700003)(45080400002)(478600001)(40480700001)(5660300002)(8936002)(8676002)(44832011)(4326008)(2906002)(6862004)(36756003)(6200100001)(86362001)(30864003)(81166007)(70206006)(82740400003)(316002)(82310400005)(70586007)(41300700001)(36860700001)(2616005)(1076003)(26005)(186003)(47076005)(83380400001)(6666004)(426003)(7049001)(7696005)(336012); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jun 2023 10:31:30.6621 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b886cfe4-0ddb-4c61-3f1e-08db680b8620 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM7EUR03FT020.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR08MB7621 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, KAM_LOTSOFHASH, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Oluwatamilore Adebayo via Gcc-patches From: Oluwatamilore Adebayo Reply-To: Oluwatamilore Adebayo Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: oluade01 This adds a recognition pattern for the non-widening absolute difference (ABD). gcc/ChangeLog: * doc/md.texi (sabd, uabd): Document them. * internal-fn.def (ABD): Use new optab. * optabs.def (sabd_optab, uabd_optab): New optabs, * tree-vect-patterns.cc (vect_recog_absolute_difference): Recognize the following idiom abs (a - b). (vect_recog_sad_pattern): Refactor to use vect_recog_absolute_difference. (vect_recog_abd_pattern): Use patterns found by vect_recog_absolute_difference to build a new ABD internal call. --- gcc/doc/md.texi | 10 ++ gcc/internal-fn.def | 3 + gcc/optabs.def | 2 + gcc/tree-vect-patterns.cc | 259 +++++++++++++++++++++++++++++++++----- 4 files changed, 244 insertions(+), 30 deletions(-) diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi index 6a435eb44610960513e9739ac9ac1e8a27182c10..e11b10d2fca11016232921bc85e47975f700e6c6 100644 --- a/gcc/doc/md.texi +++ b/gcc/doc/md.texi @@ -5787,6 +5787,16 @@ Other shift and rotate instructions, analogous to the Vector shift and rotate instructions that take vectors as operand 2 instead of a scalar type. +@cindex @code{uabd@var{m}} instruction pattern +@cindex @code{sabd@var{m}} instruction pattern +@item @samp{uabd@var{m}}, @samp{sabd@var{m}} +Signed and unsigned absolute difference instructions. These +instructions find the difference between operands 1 and 2 +then return the absolute value. A C code equivalent would be: +@smallexample +op0 = op1 > op2 ? op1 - op2 : op2 - op1; +@end smallexample + @cindex @code{avg@var{m}3_floor} instruction pattern @cindex @code{uavg@var{m}3_floor} instruction pattern @item @samp{avg@var{m}3_floor} diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def index 3ac9d82aace322bd8ef108596e5583daa18c76e3..116965f4830cec8f60642ff011a86b6562e2c509 100644 --- a/gcc/internal-fn.def +++ b/gcc/internal-fn.def @@ -191,6 +191,9 @@ DEF_INTERNAL_OPTAB_FN (FMS, ECF_CONST, fms, ternary) DEF_INTERNAL_OPTAB_FN (FNMA, ECF_CONST, fnma, ternary) DEF_INTERNAL_OPTAB_FN (FNMS, ECF_CONST, fnms, ternary) +DEF_INTERNAL_SIGNED_OPTAB_FN (ABD, ECF_CONST | ECF_NOTHROW, first, + sabd, uabd, binary) + DEF_INTERNAL_SIGNED_OPTAB_FN (AVG_FLOOR, ECF_CONST | ECF_NOTHROW, first, savg_floor, uavg_floor, binary) DEF_INTERNAL_SIGNED_OPTAB_FN (AVG_CEIL, ECF_CONST | ECF_NOTHROW, first, diff --git a/gcc/optabs.def b/gcc/optabs.def index 6c064ff4993620067d38742a0bfe0a3efb511069..35b835a6ac56d72417dac8ddfd77a8a7e2475e65 100644 --- a/gcc/optabs.def +++ b/gcc/optabs.def @@ -359,6 +359,8 @@ OPTAB_D (mask_fold_left_plus_optab, "mask_fold_left_plus_$a") OPTAB_D (extract_last_optab, "extract_last_$a") OPTAB_D (fold_extract_last_optab, "fold_extract_last_$a") +OPTAB_D (uabd_optab, "uabd$a3") +OPTAB_D (sabd_optab, "sabd$a3") OPTAB_D (savg_floor_optab, "avg$a3_floor") OPTAB_D (uavg_floor_optab, "uavg$a3_floor") OPTAB_D (savg_ceil_optab, "avg$a3_ceil") diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc index dc102c919352a0328cf86eabceb3a38c41a7e4fd..7296892aaa07da59b8122d29a22a2f583e8ff5aa 100644 --- a/gcc/tree-vect-patterns.cc +++ b/gcc/tree-vect-patterns.cc @@ -782,6 +782,100 @@ vect_split_statement (vec_info *vinfo, stmt_vec_info stmt2_info, tree new_rhs, } } +/* Look for the following pattern + X = x[i] + Y = y[i] + DIFF = X - Y + DAD = ABS_EXPR + + ABS_STMT should point to a statement of code ABS_EXPR or ABSU_EXPR. + HALF_TYPE and UNPROM will be set should the statement be found to + be a widened operation. + DIFF_STMT will be set to the MINUS_EXPR + statement that precedes the ABS_STMT unless vect_widened_op_tree + succeeds. + */ +static bool +vect_recog_absolute_difference (vec_info *vinfo, gassign *abs_stmt, + tree *half_type, + vect_unpromoted_value unprom[2], + gassign **diff_stmt) +{ + if (!abs_stmt) + return false; + + /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi + inside the loop (in case we are analyzing an outer-loop). */ + enum tree_code code = gimple_assign_rhs_code (abs_stmt); + if (code != ABS_EXPR && code != ABSU_EXPR) + return false; + + tree abs_oprnd = gimple_assign_rhs1 (abs_stmt); + tree abs_type = TREE_TYPE (abs_oprnd); + if (!abs_oprnd) + return false; + if (!ANY_INTEGRAL_TYPE_P (abs_type) + || TYPE_OVERFLOW_WRAPS (abs_type) + || TYPE_UNSIGNED (abs_type)) + return false; + + /* Peel off conversions from the ABS input. This can involve sign + changes (e.g. from an unsigned subtraction to a signed ABS input) + or signed promotion, but it can't include unsigned promotion. + (Note that ABS of an unsigned promotion should have been folded + away before now anyway.) */ + vect_unpromoted_value unprom_diff; + abs_oprnd = vect_look_through_possible_promotion (vinfo, abs_oprnd, + &unprom_diff); + if (!abs_oprnd) + return false; + if (TYPE_PRECISION (unprom_diff.type) != TYPE_PRECISION (abs_type) + && TYPE_UNSIGNED (unprom_diff.type)) + return false; + + /* We then detect if the operand of abs_expr is defined by a minus_expr. */ + stmt_vec_info diff_stmt_vinfo = vect_get_internal_def (vinfo, abs_oprnd); + if (!diff_stmt_vinfo) + return false; + + /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi + inside the loop (in case we are analyzing an outer-loop). */ + if (vect_widened_op_tree (vinfo, diff_stmt_vinfo, + MINUS_EXPR, IFN_VEC_WIDEN_MINUS, + false, 2, unprom, half_type)) + return true; + + /* Failed to find a widen operation so we check for a regular MINUS_EXPR. */ + gassign *diff = dyn_cast (STMT_VINFO_STMT (diff_stmt_vinfo)); + if (!diff || gimple_assign_rhs_code (diff) != MINUS_EXPR) + return false; + + if (diff_stmt) + *diff_stmt = diff; + + if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (abs_oprnd))) + { + *half_type = NULL_TREE; + return true; + } + + vect_unpromoted_value diff0, diff1; + if (vect_look_through_possible_promotion (vinfo, gimple_assign_rhs1 (diff), + &diff0) + && vect_look_through_possible_promotion (vinfo, gimple_assign_rhs2 (diff), + &diff1)) + { + if (TYPE_OVERFLOW_UNDEFINED (diff0.type) + && TYPE_OVERFLOW_UNDEFINED (diff1.type)) + { + *half_type = NULL_TREE; + return true; + } + } + + return false; +} + /* Convert UNPROM to TYPE and return the result, adding new statements to STMT_INFO's pattern definition statements if no better way is available. VECTYPE is the vector form of TYPE. @@ -1320,41 +1414,31 @@ vect_recog_sad_pattern (vec_info *vinfo, /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi inside the loop (in case we are analyzing an outer-loop). */ gassign *abs_stmt = dyn_cast (abs_stmt_vinfo->stmt); - if (!abs_stmt - || (gimple_assign_rhs_code (abs_stmt) != ABS_EXPR - && gimple_assign_rhs_code (abs_stmt) != ABSU_EXPR)) - return NULL; + vect_unpromoted_value unprom[2]; - tree abs_oprnd = gimple_assign_rhs1 (abs_stmt); - tree abs_type = TREE_TYPE (abs_oprnd); - if (TYPE_UNSIGNED (abs_type)) - return NULL; + if (!abs_stmt) + { + gcall *abd_stmt = dyn_cast (abs_stmt_vinfo->stmt); + if (!abd_stmt + || !gimple_call_internal_p (abd_stmt) + || gimple_call_internal_fn (abd_stmt) != IFN_ABD) + return NULL; - /* Peel off conversions from the ABS input. This can involve sign - changes (e.g. from an unsigned subtraction to a signed ABS input) - or signed promotion, but it can't include unsigned promotion. - (Note that ABS of an unsigned promotion should have been folded - away before now anyway.) */ - vect_unpromoted_value unprom_diff; - abs_oprnd = vect_look_through_possible_promotion (vinfo, abs_oprnd, - &unprom_diff); - if (!abs_oprnd) - return NULL; - if (TYPE_PRECISION (unprom_diff.type) != TYPE_PRECISION (abs_type) - && TYPE_UNSIGNED (unprom_diff.type)) - return NULL; + tree abd_oprnd0 = gimple_call_arg (abd_stmt, 0); + tree abd_oprnd1 = gimple_call_arg (abd_stmt, 1); - /* We then detect if the operand of abs_expr is defined by a minus_expr. */ - stmt_vec_info diff_stmt_vinfo = vect_get_internal_def (vinfo, abs_oprnd); - if (!diff_stmt_vinfo) + if (!vect_look_through_possible_promotion (vinfo, abd_oprnd0, &unprom[0]) + || !vect_look_through_possible_promotion (vinfo, abd_oprnd1, + &unprom[1])) + return NULL; + + half_type = unprom[0].type; + } + else if (!vect_recog_absolute_difference (vinfo, abs_stmt, &half_type, + unprom, NULL)) return NULL; - /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi - inside the loop (in case we are analyzing an outer-loop). */ - vect_unpromoted_value unprom[2]; - if (!vect_widened_op_tree (vinfo, diff_stmt_vinfo, MINUS_EXPR, - IFN_VEC_WIDEN_MINUS, - false, 2, unprom, &half_type)) + if (!half_type) return NULL; vect_pattern_detected ("vect_recog_sad_pattern", last_stmt); @@ -1376,6 +1460,120 @@ vect_recog_sad_pattern (vec_info *vinfo, return pattern_stmt; } +/* Function vect_recog_abd_pattern + + Try to find the following ABsolute Difference (ABD) pattern: + + VTYPE x, y, out; + type diff; + loop i in range: + S1 diff = x[i] - y[i] + S2 out[i] = ABS_EXPR ; + + where 'type' is a integer and 'VTYPE' is a vector of integers + the same size as 'type' + + Input: + + * STMT_VINFO: The stmt from which the pattern search begins + + Output: + + * TYPE_out: The type of the output of this pattern + + * Return value: A new stmt that will be used to replace the sequence of + stmts that constitute the pattern; either SABD or UABD: + SABD_EXPR + UABD_EXPR + */ + +static gimple * +vect_recog_abd_pattern (vec_info *vinfo, + stmt_vec_info stmt_vinfo, tree *type_out) +{ + /* Look for the following patterns + X = x[i] + Y = y[i] + DIFF = X - Y + DAD = ABS_EXPR + out[i] = DAD + + In which + - X, Y, DIFF, DAD all have the same type + - x, y, out are all vectors of the same type + */ + + gassign *last_stmt = dyn_cast (STMT_VINFO_STMT (stmt_vinfo)); + if (!last_stmt) + return NULL; + + tree out_type = TREE_TYPE (gimple_assign_lhs (last_stmt)); + + vect_unpromoted_value unprom[2]; + gassign *diff_stmt; + tree half_type; + if (!vect_recog_absolute_difference (vinfo, last_stmt, &half_type, + unprom, &diff_stmt)) + return NULL; + + tree abd_type = out_type, vectype; + tree abd_oprnds[2]; + bool extend = false; + if (half_type) + { + vectype = get_vectype_for_scalar_type (vinfo, half_type); + if (!vectype) + return NULL; + + abd_type = half_type; + vect_convert_inputs (vinfo, stmt_vinfo, 2, abd_oprnds, + half_type, unprom, vectype); + + extend = TYPE_PRECISION (abd_type) < TYPE_PRECISION (out_type); + } + else + { + unprom[0].op = gimple_assign_rhs1 (diff_stmt); + unprom[1].op = gimple_assign_rhs2 (diff_stmt); + tree signed_out = signed_type_for (out_type); + vectype = get_vectype_for_scalar_type (vinfo, signed_out); + if (!vectype) + return NULL; + + vect_convert_inputs (vinfo, stmt_vinfo, 2, abd_oprnds, + signed_out, unprom, vectype); + } + + vect_pattern_detected ("vect_recog_abd_pattern", last_stmt); + + if (!vectype + || !direct_internal_fn_supported_p (IFN_ABD, vectype, + OPTIMIZE_FOR_SPEED)) + return NULL; + + *type_out = get_vectype_for_scalar_type (vinfo, out_type); + + tree abd_result = vect_recog_temp_ssa_var (abd_type, NULL); + gcall *abd_stmt = gimple_build_call_internal (IFN_ABD, 2, + abd_oprnds[0], abd_oprnds[1]); + gimple_call_set_lhs (abd_stmt, abd_result); + gimple_set_location (abd_stmt, gimple_location (last_stmt)); + + if (!extend) + return abd_stmt; + + gimple *stmt = abd_stmt; + if (!TYPE_UNSIGNED (abd_type)) + { + tree unsign = unsigned_type_for (abd_type); + tree unsign_vectype = get_vectype_for_scalar_type (vinfo, unsign); + stmt = vect_convert_output (vinfo, stmt_vinfo, unsign, stmt, + unsign_vectype); + } + + return vect_convert_output (vinfo, stmt_vinfo, out_type, stmt, vectype); +} + /* Recognize an operation that performs ORIG_CODE on widened inputs, so that it can be treated as though it had the form: @@ -6471,6 +6669,7 @@ struct vect_recog_func static vect_recog_func vect_vect_recog_func_ptrs[] = { { vect_recog_bitfield_ref_pattern, "bitfield_ref" }, { vect_recog_bit_insert_pattern, "bit_insert" }, + { vect_recog_abd_pattern, "abd" }, { vect_recog_over_widening_pattern, "over_widening" }, /* Must come after over_widening, which narrows the shift as much as possible beforehand. */