From patchwork Fri Jan 26 18:25:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 192735 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2395:b0:106:343:edcb with SMTP id gw21csp66079dyb; Fri, 26 Jan 2024 10:26:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXRw0Ch71sMwONFDRptwgMWtmVvVDsLwdO1bBbsic3VcrAEA6hjOu8nX9ra3Czjmz/Op8s X-Received: by 2002:ac8:7c4b:0:b0:42a:3cfb:a746 with SMTP id o11-20020ac87c4b000000b0042a3cfba746mr297149qtv.54.1706293570563; Fri, 26 Jan 2024 10:26:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706293570; cv=pass; d=google.com; s=arc-20160816; b=a6iV1BZj2zXumLdfkTHJt6FP7BzjeweufAmx6hUjKrbC+ligEZ1KHooAXDnY9XTvO+ K7B2g011xhnWFsd2etrIhDDhj5Ck9VUz3FSlSxDFgsow5xkY8P4bwMiLulret15AyTa1 A5YnrzrVftIuWyNIwwOUS1zVCdgKrxzKoVAdnBE+SaApU5VozGI1nkwKWEB6JpGRUeOY Ots/2nBpyFnqYUiyxegWFipSfwFMK8E4J//kuR55H3CDcCwHSAI6LVD77f8Bz4sLJ9IL PMM+PCakRroih+jPqO1gVgojvHwuLONVxIsaoRVx0RDVRhsUHKESEfl0mIuF8OPmG+gw Dcjg== 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:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=rEYLVxCqxpRE/iEWKgV2JUPh3H2E6azacKbanpgtctU=; fh=gk9A5O7Gbglvz1MjrRQ4hZ2i3c68kYkpOZJg1emXh+g=; b=PUC7jcZGgkkkJGMWNCqCpb3hoTPUa1Y27kEMls9BYmAYDDeBb3rNqb5h3VGE36cld9 UTTuqG9legdRcWTX1L5a7yZhRQWHJMQ9+rBxDlFQnAVJ5IIE0AlSK9uH116lZadZpGFm /4LYty95tvj5R1Y2Y5QBMRUyC/k7hsUpCnPga7MwsleYaTSUZ/yRVxVNtVgNF2y1dAH2 i7VFdNw0+OZmtUL8gRgrKuCPV4bZnNZiTpELiJ6khXgKH59ywl3ULjHhtU8NLjSwmawI 8u0X4xvMYWBULMuAWSlFKFbvNbUOBlMcIROYjvyffHgITQ/TY0CBiWSCVrP6FHTLAPmt KjCA== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b="b/z/mO3z"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y7-20020ac85f47000000b00429bfed4958si1678282qta.793.2024.01.26.10.26.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 10:26:10 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b="b/z/mO3z"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 1F6EF385DC03 for ; Fri, 26 Jan 2024 18:26:10 +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 E4D3D3857348 for ; Fri, 26 Jan 2024 18:25:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E4D3D3857348 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 E4D3D3857348 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=1706293529; cv=none; b=uSaY6W2tqss/gTfd0txWnMXJXErEMHW7nbNBAg3W7VSZbvWCDG3s9W5NozTZGWZi81ZcQvjJOn1P6E3UfGrYxghK/wKhccKHGG+DtlW0Ws0NkPrtT6pGmzCFHN31+LmKumYZ3TH1dok5SeuouFnROBsIRyFGPfimeNnPa5V3nXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706293529; c=relaxed/simple; bh=RnYWZQXPnxWEQwKjUOZXWQcFB/wSsRJ55cLeXaePWPU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=A8Y1xeC1yZOnBvs8LrvVILrIc5Mn4aa4KIc9KQ8CwSbw5EQkORFsQf612TRSs96OzJA4yxp+vHU5LqDfk2s+Agk9b28q/D5HUh0SxClqhIuMnqpgcWoPpTU3V0IvEWEtCNM8+zbZtdqKTfImJDtWuz8A7mFchRNfwlmFNIyrV1I= 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:To:From:Sender:Reply-To:Cc: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=rEYLVxCqxpRE/iEWKgV2JUPh3H2E6azacKbanpgtctU=; b=b/z/mO3zh6k1FB9eUb5vgkKkeR uIRxlUs57AN/K30PxHkxwGnCLtzeF9DOLUcDiAS+xzT6sGrpZSOPZfDVm6Urou9z0vBQivIFGG1nK KbYUdozeBQHZ5c9VBb//il8sGP+wOr8rmb9M0RszB93iNzBjHORtqeC1CGRxuN0FmIBwRN2TQuhho KH0QMuUYGTKXKIzBTyZSyeBBPst7TWUlsai8wHMj4G4mp8xQjE1KYQYO4pKzK14K6KxW0tY586GBz YZ2iog0vSvJ80mpQ8EdX2apSBmWaxEgud/GobUmW9FsmhlBiOUhjWpMgDKxKzrPeCe9i0uSevjHKT ROsGyjrA==; Received: from [185.62.158.67] (port=55638 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 1rTQtK-00053W-06 for gcc-patches@gcc.gnu.org; Fri, 26 Jan 2024 13:25:26 -0500 From: "Roger Sayle" To: Subject: [middle-end PATCH] Constant fold {-1,-1} << 1 in simplify-rtx.cc Date: Fri, 26 Jan 2024 18:25:24 -0000 Message-ID: <007f01da5085$08c48420$1a4d8c60$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdpQhGNcI6eqew1FTP+kKFLOO5VOkQ== 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, 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: 1789178486865619814 X-GMAIL-MSGID: 1789178486865619814 This patch addresses a missed optimization opportunity in the RTL optimization passes. The function simplify_const_binary_operation will constant fold binary operators with two CONST_INT operands, and those with two CONST_VECTOR operands, but is missing compile-time evaluation of binary operators with a CONST_VECTOR and a CONST_INT, such as vector shifts and rotates. My first version of this patch didn't contain a switch statement to explicitly check for valid binary opcodes, which bootstrapped and regression tested fine, but by paranoia has got the better of me, so this version now checks that VEC_SELECT or some funky (future) rtx_code doesn't cause problems. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32} with no new failures. Ok for mainline (in stage 1)? 2024-01-26 Roger Sayle gcc/ChangeLog * simplify-rtx.cc (simplify_const_binary_operation): Constant fold binary operations where the LHS is CONST_VECTOR and the RHS is CONST_INT (or CONST_DOUBLE) such as vector shifts. Thanks in advance, Roger diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index c7215cf..2e2809a 100644 --- a/gcc/simplify-rtx.cc +++ b/gcc/simplify-rtx.cc @@ -5021,6 +5021,60 @@ simplify_const_binary_operation (enum rtx_code code, machine_mode mode, return gen_rtx_CONST_VECTOR (mode, v); } + if (VECTOR_MODE_P (mode) + && GET_CODE (op0) == CONST_VECTOR + && (CONST_SCALAR_INT_P (op1) || CONST_DOUBLE_AS_FLOAT_P (op1)) + && (CONST_VECTOR_DUPLICATE_P (op0) + || CONST_VECTOR_NUNITS (op0).is_constant ())) + { + switch (code) + { + case PLUS: + case MINUS: + case MULT: + case DIV: + case MOD: + case UDIV: + case UMOD: + case AND: + case IOR: + case XOR: + case SMIN: + case SMAX: + case UMIN: + case UMAX: + case LSHIFTRT: + case ASHIFTRT: + case ASHIFT: + case ROTATE: + case ROTATERT: + case SS_PLUS: + case US_PLUS: + case SS_MINUS: + case US_MINUS: + case SS_ASHIFT: + case US_ASHIFT: + case COPYSIGN: + break; + default: + return NULL_RTX; + } + + unsigned int npatterns = (CONST_VECTOR_DUPLICATE_P (op0) + ? CONST_VECTOR_NPATTERNS (op0) + : CONST_VECTOR_NUNITS (op0).to_constant ()); + rtx_vector_builder builder (mode, npatterns, 1); + for (unsigned i = 0; i < npatterns; i++) + { + rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode), + CONST_VECTOR_ELT (op0, i), op1); + if (!x || !valid_for_const_vector_p (mode, x)) + return 0; + builder.quick_push (x); + } + return builder.build (); + } + if (SCALAR_FLOAT_MODE_P (mode) && CONST_DOUBLE_AS_FLOAT_P (op0) && CONST_DOUBLE_AS_FLOAT_P (op1)