From patchwork Wed May 4 10:15:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Earnshaw X-Patchwork-Id: 53450 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 81B6C3856DCB for ; Wed, 4 May 2022 10:16:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81B6C3856DCB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651659401; bh=dOaSeLBcMwh2HiVzfVPpMrTa+ZX3wXWlo9nAu46I3ck=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=cEo7w/kjDi3W/DOAEq9WJSs3/zBQ6QMhgph3vN/9rXZZDdReUfjJER/BbThcqjhXS Ug/pNVzUIcDYQ3XKZBkhGGRmNah/soptRa1s0PTl8YyRsbLY1Rb3ZXBKgxLw3DdxIP Ze2mRzm8risvLdZDd0s7rxpIBUHfd/B9F85v3yrs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 731163858010 for ; Wed, 4 May 2022 10:16:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 731163858010 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 00B52ED1; Wed, 4 May 2022 03:16:12 -0700 (PDT) Received: from e126323.cambridge.arm.com (e126323.cambridge.arm.com [10.2.79.43]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6337B3FA50; Wed, 4 May 2022 03:16:11 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] gimple-isel: handle x CMP y ? z : 0 Date: Wed, 4 May 2022 11:15:52 +0100 Message-Id: <20220504101552.1514123-1-rearnsha@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_NUMSUBJECT, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Richard Earnshaw via Gcc-patches From: Richard Earnshaw Reply-To: Richard Earnshaw Cc: Richard Biener , Richard Earnshaw Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Gimple isel already handles x CMP y ? -1 : 0 when lowering vector cond operations, but this can be generalized further when the comparison forms a natural mask so that we can also handle x CMP y ? z : 0 by transforming it into (x CMP y) & z. This will, in most cases save having to load a register with the zero vector. gcc/ChangeLog: * gimple-isel.cc (gimple_expand_vec_cond_expr): Handle x CMP y ? z : 0. --- gcc/gimple-isel.cc | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/gcc/gimple-isel.cc b/gcc/gimple-isel.cc index a8f7a0d25d0..5bf4a4eccc1 100644 --- a/gcc/gimple-isel.cc +++ b/gcc/gimple-isel.cc @@ -190,16 +190,33 @@ gimple_expand_vec_cond_expr (struct function *fun, gimple_stmt_iterator *gsi, can_compute_op0 = expand_vec_cmp_expr_p (op0a_type, op0_type, tcode); - /* Try to fold x CMP y ? -1 : 0 to x CMP y. */ + /* Try to fold x CMP y ? z : 0. */ if (can_compute_op0 - && integer_minus_onep (op1) && integer_zerop (op2) && TYPE_MODE (TREE_TYPE (lhs)) == TYPE_MODE (TREE_TYPE (op0))) { - tree conv_op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), op0); - gassign *new_stmt = gimple_build_assign (lhs, conv_op); - gsi_replace (gsi, new_stmt, true); - return new_stmt; + /* If Z is -1, then the result is just the comparison. */ + if (integer_minus_onep (op1)) + { + tree conv_op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), + op0); + gassign *new_stmt = gimple_build_assign (lhs, conv_op); + gsi_replace (gsi, new_stmt, true); + return new_stmt; + } + /* Otherwise, use the comparison as a mask for Z. */ + else + { + gimple_seq stmts = NULL; + tree type = TREE_TYPE (lhs); + location_t loc = gimple_location (stmt); + tree tem0 = gimple_build (&stmts, loc, VIEW_CONVERT_EXPR, + type, op0); + tree tem1 = gimple_build (&stmts, loc, BIT_AND_EXPR, type, + tem0, op1); + gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); + return gimple_build_assign (lhs, tem1); + } } /* When the compare has EH we do not want to forward it when