From patchwork Wed Feb 8 08:52:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 54261 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp3340170wrn; Wed, 8 Feb 2023 00:53:07 -0800 (PST) X-Google-Smtp-Source: AK7set8LLHKR+nRem4OCJtuPYrM0yNg6pIz42lnRoV704taesSdjX5cec4pKACG9kVItl5/bMZVR X-Received: by 2002:a17:906:988b:b0:88c:3a48:715b with SMTP id zc11-20020a170906988b00b0088c3a48715bmr6820474ejb.30.1675846387169; Wed, 08 Feb 2023 00:53:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675846387; cv=none; d=google.com; s=arc-20160816; b=uplx/yWeGEkvXO1glVkOsiViiTi2aE/S766IkUVxgOdAwGv243ln+nRLTocNdMomeI L/ET0WKK1ZWrhrWfKqvG0FHVDZv7KChpVP67S+1BYHLvxzWMD62uXpdBN1q/36g5cHgc cA08jg4qNrdvdrM01Lb6mGuazV7Y1BPn82DxzXtU6ZUGu2i9DWg1RhGzKj7WRDgeIKGc BJz9u66ixFRYPLMqs6Bn/fIrfgNYo0o8KzWQxi+qtJiLh4sjgH5wgPDAJfzrd3RzqotC E/DBeXZEo/rcHnInVIeLQzn2iFM9JYf1rLak7eC5TXPyhiwQYa9Wbyigh4k0EjpyMKul f2/Q== 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 :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=tbym6v2R24ilCJSUMjfcZnTYBR4de1MrpZqT5M23kPg=; b=EujGAwsjkPp9pvv/OER5Kwq9nYxy6ELnNMPx3qVGVbzsH8+YfvTV9MoShWcrB0Pg96 n7vSVlc/oz1k/07U8uLwPSqR0IZmLNKl1MR7l2Ciu21HtgENTT6d1KvgaveWLOM1Gp9S d7TeeRXaL+7Op88oAcWEB5S0KlVCeCgnUyTsGfi4wFS+6z8Wh7AXZw+Lg2grraP59Xkg CF11Etb1bVwn5mkE48SCiM3ni+jIGWBPWIg42UwDwM6uru4NcSEQKPIXsnbhDgZca68O fmKgZQ0YIEWeUBOPAxKDqpzjIdWCL+lvAfTfgQ5w/F9XOKJeiGUG0oYqs0R2BgZwXWm0 xrgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=sICcKFrx; 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 sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id wi6-20020a170906fd4600b008aabf9c5894si2791098ejb.666.2023.02.08.00.53.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Feb 2023 00:53:07 -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=pass header.i=@gcc.gnu.org header.s=default header.b=sICcKFrx; 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 2D72C385828E for ; Wed, 8 Feb 2023 08:53:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D72C385828E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675846386; bh=tbym6v2R24ilCJSUMjfcZnTYBR4de1MrpZqT5M23kPg=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=sICcKFrxl97L6MvUfltu1+wvWzv9+or+hlbR90n2xgNOwtdkYeGrhbk5dwoxaS5v3 FcG49J/IeQbg8nKBIO3kDaso7JRoHoxWr5+xcFsfJ3zcqSP3CU//clH45lEIouguUi qjW50rMPzTWoFo7/Qj52ikxmOX7Igx/4dlAppqiM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 0908C3858D39 for ; Wed, 8 Feb 2023 08:52:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0908C3858D39 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-90-DUzP2_5cM1OEu5yn665xrw-1; Wed, 08 Feb 2023 03:52:21 -0500 X-MC-Unique: DUzP2_5cM1OEu5yn665xrw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2130080D0E0; Wed, 8 Feb 2023 08:52:21 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D46322026D4B; Wed, 8 Feb 2023 08:52:20 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3188qHLE3943088 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 8 Feb 2023 09:52:17 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3188qG593943087; Wed, 8 Feb 2023 09:52:16 +0100 Date: Wed, 8 Feb 2023 09:52:15 +0100 To: Richard Sandiford , Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] vect-patterns: Fix up vect_widened_op_tree [PR108692] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757252300887639150?= X-GMAIL-MSGID: =?utf-8?q?1757252300887639150?= Hi! The following testcase is miscompiled on aarch64-linux since r11-5160. Given [local count: 955630225]: # i_22 = PHI # r_23 = PHI ... a.0_5 = (unsigned char) a_15; _6 = (int) a.0_5; b.1_7 = (unsigned char) b_17; _8 = (int) b.1_7; c_18 = _6 - _8; _9 = ABS_EXPR ; r_19 = _9 + r_23; ... where SSA_NAMEs 15/17 have signed char, 5/7 unsigned char and rest is int we first pattern recognize c_18 as patt_34 = (a.0_5) w- (b.1_7); which is still correct, 5/7 are unsigned char subtracted in wider type, but then vect_recog_sad_pattern turns it into SAD_EXPR which is incorrect, because 15/17 are signed char and so it is sum of absolute signed differences rather than unsigned sum of absolute unsigned differences. The reason why this happens is that vect_recog_sad_pattern calls vect_widened_op_tree with MINUS_EXPR, WIDEN_MINUS_EXPR on the patt_34 = (a.0_5) w- (b.1_7); statement's vinfo and vect_widened_op_tree calls vect_look_through_possible_promotion on the operands of the WIDEN_MINUS_EXPR, which looks through the further casts. vect_look_through_possible_promotion has careful code to stop when there would be nested casts that need to be preserved, but the problem here is that the WIDEN_*_EXPR operation itself has an implicit cast on the operands already - in this case of WIDEN_MINUS_EXPR the unsigned char 5/7 SSA_NAMEs are widened to unsigned short before the subtraction, and vect_look_through_possible_promotion obviously isn't told about that. Now, I think when we see those WIDEN_{MULT,MINUS,PLUS}_EXPR codes, we had to look through possible promotions already when creating those and so vect_look_through_possible_promotion again isn't really needed, all we need to do is arrange what that function will do if the operand isn't result of any cast. Other option would be let vect_look_through_possible_promotion know about the implicit promotion from the WIDEN_*_EXPR, but I'm afraid that would be much harder. Bootstrapped/regtested on aarch64-linux, x86_64-linux, i686-linux and powerpc64le-linux, ok for trunk? 2023-02-08 Jakub Jelinek PR tree-optimization/108692 * tree-vect-patterns.cc (vect_widened_op_tree): If rhs_code is widened_code which is different from code, don't call vect_look_through_possible_promotion but instead just check op is SSA_NAME with integral type for which vect_is_simple_use is true and call set_op on this_unprom. * gcc.dg/pr108692.c: New test. Jakub --- gcc/tree-vect-patterns.cc.jj 2023-01-02 09:32:45.635949342 +0100 +++ gcc/tree-vect-patterns.cc 2023-02-07 15:27:33.214608837 +0100 @@ -601,7 +601,25 @@ vect_widened_op_tree (vec_info *vinfo, s if (shift_p && i == 1) return 0; - if (!vect_look_through_possible_promotion (vinfo, op, this_unprom)) + if (rhs_code != code) + { + /* If rhs_code is widened_code, don't look through further + possible promotions, there is a promotion already embedded + in the WIDEN_*_EXPR. */ + if (TREE_CODE (op) != SSA_NAME + || !INTEGRAL_TYPE_P (TREE_TYPE (op))) + return 0; + + stmt_vec_info def_stmt_info; + gimple *def_stmt; + vect_def_type dt; + if (!vect_is_simple_use (op, vinfo, &dt, &def_stmt_info, + &def_stmt)) + return 0; + this_unprom->set_op (op, dt, NULL); + } + else if (!vect_look_through_possible_promotion (vinfo, op, + this_unprom)) return 0; if (TYPE_PRECISION (this_unprom->type) == TYPE_PRECISION (type)) --- gcc/testsuite/gcc.dg/pr108692.c.jj 2023-02-07 15:47:20.329076264 +0100 +++ gcc/testsuite/gcc.dg/pr108692.c 2023-02-07 15:46:15.623031983 +0100 @@ -0,0 +1,31 @@ +/* PR tree-optimization/108692 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -ftree-vectorize" } */ + +__attribute__((noipa)) int +foo (signed char *x, signed char *y, int n) +{ + int i, r = 0; + signed char a, b; + for (i = 0; i < n; i++) + { + a = x[i]; + b = y[i]; + int c = (unsigned char) a - (unsigned char) b; + r = r + (c < 0 ? -c : c); + } + return r; +} + +int +main () +{ + signed char x[64] = {}, y[64] = {}; + if (__CHAR_BIT__ != 8 || __SIZEOF_INT__ != 4) + return 0; + x[32] = -128; + y[32] = 1; + if (foo (x, y, 64) != 127) + __builtin_abort (); + return 0; +}