From patchwork Fri Aug 4 10:19:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 131072 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:44a:b0:3f2:4152:657d with SMTP id ez10csp160238vqb; Fri, 4 Aug 2023 03:20:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEx2J/Fn7vIwo6CAGJxCNr3Ok/haN0KxfIOcPlqUI0ujzyRCpBzukXGHjx8of9D9JJ8DzLV X-Received: by 2002:a2e:90c4:0:b0:2b9:615b:39a9 with SMTP id o4-20020a2e90c4000000b002b9615b39a9mr1143999ljg.52.1691144442817; Fri, 04 Aug 2023 03:20:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691144442; cv=none; d=google.com; s=arc-20160816; b=OEwJrrxGHxx369bRx7Mg+9ARzq9FDZhYrzo0XIHUft0DK0tYTv1evCdH5wQu9LoHMT y3wJFgDCUUtjiJnNjYjeVSqCMiJo12aF+m+IHS13nM8BGCaaqq6Eas4s4F86WWczsufc /ZcsZkeSxsV/u2zvScH+AaTISYqTrN3ovpb7MKFxHTgsis2oh6SHMEz1Fm+KWiYKOs8O O1NmRc+/LhomLWJ00+9F8eHJmBIFeysS06fBolBUX3Y2Q2Q2A6sbdRBuNcTT8NK1eiJV 9u62BBeJ54yZsU6464wxf4ODUR/mSDfQSiRIeJa6knw/PzeJfGLh5RcUZUy9C4oEOwkL i++A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=6UIijOVg6cssgE73UZ3ttqGh+nOWR4HGNXEnDwUeTZE=; fh=YqA+5s9oAwnNzotQ4oUKFF8r+ylKGgHXnNsFpvcI6Dw=; b=vYdwRk49qFDIY6XLiRxNb2ZhXsmwcwE7t0tq3eyKcukln9L37Rm9CrEmiCYk41VTJC txEMoA1HWsVE88RRseh/gJ/tEws/149XN1wv4sI/7BRdir2m/QGEDlC5MgRzsAxYfxv8 OIIWqlddeQyLMgXZJe8wrs4eDJGbNzlOMLK/Yd/VMltkbjF0SgROgW9lEcWH02Ou3jBX zM/0xW94bBCRO0cFR7z58rnz8orDU4ulwMHTjkRTTQo0XcrJFzT2CsRDTDNmZSsGXtYQ jnihNhEaryh2PpalpqTVTSb2G+yhCHt6+8rR2GzwxDGbgGz6XhFVZUyamhe7ZI0z0Efd SVHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iwxmha2s; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v6-20020a170906338600b0099379d3f91esi1449181eja.477.2023.08.04.03.20.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Aug 2023 03:20:42 -0700 (PDT) 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=pass header.i=@gcc.gnu.org header.s=default header.b=iwxmha2s; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AFB2F3857715 for ; Fri, 4 Aug 2023 10:20:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AFB2F3857715 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691144441; bh=6UIijOVg6cssgE73UZ3ttqGh+nOWR4HGNXEnDwUeTZE=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=iwxmha2sLOcRfihnvrDcyXBImHeT/h6ypWllVp3pLSqgHss/HLIaaolvRNWpn0N3z dSLoUUXC1U4eqM+KnKj5ddJFDTppBpvl/BgAWj47B2Mzb+SNb9mYkG8PBbpn0tksis IIk1Rnkan13XqSeCH1mB9IszFkQcksU3Jcr7WFG4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 5D0E93857346 for ; Fri, 4 Aug 2023 10:20:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D0E93857346 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2F5691F749; Fri, 4 Aug 2023 10:19:59 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1008513904; Fri, 4 Aug 2023 10:19:59 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Qzj7Ac/QzGS7MAAAMHmgww (envelope-from ); Fri, 04 Aug 2023 10:19:59 +0000 Date: Fri, 4 Aug 2023 12:19:58 +0200 (CEST) To: gcc-patches@gcc.gnu.org cc: richard.sandiford@arm.com Subject: [PATCH] tree-optimization/110838 - vectorization of widened right shifts MIME-Version: 1.0 Message-Id: <20230804101959.1008513904@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.1 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 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773293475168616619 X-GMAIL-MSGID: 1773293475168616619 The following fixes a problem with my last attempt of avoiding out-of-bound shift values for vectorized right shifts of widened operands. Instead of truncating the shift amount with a bitwise and we actually need to saturate it to the target precision. The following does that and adds test coverage for the constant and invariant but variable case that would previously have failed. Bootstrap & regtest on x86_64-unknown-linux-gnu in progress, I plan to push this soon, just in case you have any comments here. Richard. PR tree-optimization/110838 * tree-vect-patterns.cc (vect_recog_over_widening_pattern): Fix right-shift value sanitizing. Properly emit external def mangling in the preheader rather than in the pattern def sequence where it will fail vectorizing. * gcc.dg/vect/pr110838.c: New testcase. --- gcc/testsuite/gcc.dg/vect/pr110838.c | 31 ++++++++++++++++++++++++++++ gcc/tree-vect-patterns.cc | 22 +++++++++++++++----- 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/vect/pr110838.c diff --git a/gcc/testsuite/gcc.dg/vect/pr110838.c b/gcc/testsuite/gcc.dg/vect/pr110838.c new file mode 100644 index 00000000000..cf8765be603 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/pr110838.c @@ -0,0 +1,31 @@ +/* { dg-do run } */ + +#include "tree-vect.h" + +short a[32], b[32]; + +void __attribute__((noipa)) foo () +{ + for (int i = 0; i < 32; ++i) + a[i] = b[i] >> 16; +} + +void __attribute__((noipa)) bar (int n) +{ + int np = n & 31; + for (int i = 0; i < 32; ++i) + a[i] = b[i] >> np; +} + +int main () +{ + check_vect (); + b[0] = -8; + foo (); + if (a[0] != -1) + abort (); + bar (16); + if (a[0] != -1) + abort (); + return 0; +} diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc index e4ab8c2d65b..2cedf238450 100644 --- a/gcc/tree-vect-patterns.cc +++ b/gcc/tree-vect-patterns.cc @@ -3109,8 +3109,8 @@ vect_recog_over_widening_pattern (vec_info *vinfo, wide_int min_value, max_value; if (TREE_CODE (ops[1]) == INTEGER_CST) ops[1] = wide_int_to_tree (op_type, - wi::bit_and (wi::to_wide (ops[1]), - new_precision - 1)); + wi::umin (wi::to_wide (ops[1]), + new_precision - 1)); else if (!vect_get_range_info (ops[1], &min_value, &max_value) || wi::ge_p (max_value, new_precision, TYPE_SIGN (op_type))) { @@ -3118,11 +3118,23 @@ vect_recog_over_widening_pattern (vec_info *vinfo, same argument widened shifts and it un-CSEs same arguments. */ tree new_var = vect_recog_temp_ssa_var (op_type, NULL); gimple *pattern_stmt - = gimple_build_assign (new_var, BIT_AND_EXPR, ops[1], + = gimple_build_assign (new_var, MIN_EXPR, ops[1], build_int_cst (op_type, new_precision - 1)); - ops[1] = new_var; gimple_set_location (pattern_stmt, gimple_location (last_stmt)); - append_pattern_def_seq (vinfo, last_stmt_info, pattern_stmt); + if (unprom[1].dt == vect_external_def) + { + if (edge e = vect_get_external_def_edge (vinfo, ops[1])) + { + basic_block new_bb + = gsi_insert_on_edge_immediate (e, pattern_stmt); + gcc_assert (!new_bb); + } + else + return NULL; + } + else + append_pattern_def_seq (vinfo, last_stmt_info, pattern_stmt); + ops[1] = new_var; } }