From patchwork Tue Aug 31 07:32:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1522460 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=ynYFGeWA; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GzJpH6l51z9sR4 for ; Tue, 31 Aug 2021 17:33:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6F5743858012 for ; Tue, 31 Aug 2021 07:33:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6F5743858012 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1630395219; bh=TeJPFZhvgImTAK7SkS0j7DinB0XshKUnpfz4ReXAokc=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=ynYFGeWA/yfnG4yrDdoH64kQ90TqF8J8VBS7B/ObFfYNnryAlr0l021PbdZFYWLdh aaYubt6b/bbzfPVHxDh0PHmwbTmp1Ap2db8bMI6VxYrqeSLR8sNTPerbm7A9OwRso7 Uxa7YYg2qpVj9/CEJgeApMtGnteuqBeokdzkumb4= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 400533858405 for ; Tue, 31 Aug 2021 07:32:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 400533858405 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-275-GbJfdYuqO9uMxXZYn1mccw-1; Tue, 31 Aug 2021 03:32:55 -0400 X-MC-Unique: GbJfdYuqO9uMxXZYn1mccw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 75093871803; Tue, 31 Aug 2021 07:32:54 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D34DD27CA1; Tue, 31 Aug 2021 07:32:53 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 17V7Wpj92328559 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 31 Aug 2021 09:32:51 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 17V7Wiec2328558; Tue, 31 Aug 2021 09:32:44 +0200 Date: Tue, 31 Aug 2021 09:32:43 +0200 To: Richard Biener Subject: [PATCH] tree-ssa-ccp: Fix up bit_value_binop on RSHIFT_EXPR [PR102134] Message-ID: <20210831073243.GR920497@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org, Roger Sayle Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi! As mentioned in the PR, this hunk is guarded with !wi::neg_p (r1val | r1mask, sgn) which means if sgn is UNSIGNED, it is always true, but r1val | r1mask in widest_int is still sign-extended. That means wi::clz (arg) returns 0, wi::get_precision (arg) returns some very large number (WIDE_INT_MAX_PRECISION, on x86_64 576 bits) and width is 64, so we end up with lzcount of -512 where the code afterwards expects a non-negative lzcount. For arg without the sign bit set the code works right, those numbers are zero extended and so wi::clz must return wi::get_precision (arg) - width plus number of leading zero bits within the width precision. The patch fixes it by handling the sign-extension specially, either it could be done through wi::neg_p (arg) check, but lzcount == 0 works identically. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-08-31 Jakub Jelinek PR tree-optimization/102134 * tree-ssa-ccp.c (bit_value_binop) : If sgn is UNSIGNED and r1val | r1mask has MSB set, ensure lzcount doesn't become negative. * gcc.c-torture/execute/pr102134.c: New test. Jakub --- gcc/tree-ssa-ccp.c.jj 2021-08-30 08:36:11.302515439 +0200 +++ gcc/tree-ssa-ccp.c 2021-08-30 22:49:21.957503630 +0200 @@ -1695,7 +1695,8 @@ bit_value_binop (enum tree_code code, si /* Logical right shift, or zero sign bit. */ widest_int arg = r1val | r1mask; int lzcount = wi::clz (arg); - lzcount -= wi::get_precision (arg) - width; + if (lzcount) + lzcount -= wi::get_precision (arg) - width; widest_int tmp = wi::mask (width, false); tmp = wi::lrshift (tmp, lzcount); tmp = wi::lrshift (tmp, wi::bit_and_not (r2val, r2mask)); --- gcc/testsuite/gcc.c-torture/execute/pr102134.c.jj 2021-08-30 22:47:41.115920522 +0200 +++ gcc/testsuite/gcc.c-torture/execute/pr102134.c 2021-08-30 22:47:20.811205820 +0200 @@ -0,0 +1,23 @@ +/* PR tree-optimization/102134 */ + +typedef unsigned long long u64; + +u64 g; + +void +foo (u64 a, u64 b, u64 c, u64 *r) +{ + b *= b; + u64 x = a && ((b >> (c & 63)) | ((b << (c & 63)) & g)); + *r = x + a; +} + +int +main () +{ + u64 x; + foo (1, 3000, 0, &x); + if (x != 2) + __builtin_abort (); + return 0; +}