From patchwork Tue Jan 9 08:45:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 83597 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 62AE83858284 for ; Tue, 9 Jan 2024 08:48:18 +0000 (GMT) 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 3E9BB38582B4 for ; Tue, 9 Jan 2024 08:45:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3E9BB38582B4 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3E9BB38582B4 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704789956; cv=none; b=vI9MXTxfcoavnKBgiGojaKMpwcJTve/KUsboe+s+K/cuyR5XdXFMmABQw/WYhS7WHXGVuVkJKxVN0f6htNVAI2y2goGFzQrqwvcUSzUEH8RIyYrOPlcqP0Sw5Swh2spXtMrBD3RXewIveYj7FwJcAgV9jJfy3gNYGcp+nbn65b0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704789956; c=relaxed/simple; bh=0FqLNN7UyZzC22BNwntEAu3mBaOZKneeF40oYm5jTr0=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=BuIWbdCaFobqftL7hFGqXbhW1+tZdXcBzAzRA9/wn1C2tYUCn988jQpQePW0W12RdIBOg2wyJsdFiEKCqKZWv4YRiFWt5VQ3Um24Req0Nca7WsUJxRjQmGgGmlJECXmB+DhVIKHlzBa2unEUpYtBaHj5J48rJH4AQblUmiDVeBU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704789949; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=SJfYeS73RuHm+IT+15aHxaaVUK9W1vNFfR+AC6HmpeI=; b=LJ888eFOWikN0X/k8VleMrsZBaQAxTOhV/SSwpYYbBB4JSFKe8CT/O5cMkDjTzp9LunzV4 yccsl/Be6b6B0GTAqwjb7OXDqTFWI8TRgz1c1/TOmEYaNI/rxAwKkI/RkA2G7HCW+TXuKm RJQkIq1f1wlrHnL3+Y2UEMF42hkVVkk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-OsR2OIMKPdqXxOqMV-FAIQ-1; Tue, 09 Jan 2024 03:45:48 -0500 X-MC-Unique: OsR2OIMKPdqXxOqMV-FAIQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 579AA185A782; Tue, 9 Jan 2024 08:45:48 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1B9361C060AF; Tue, 9 Jan 2024 08:45:47 +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 4098jjQM1771433 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 9 Jan 2024 09:45:45 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4098jiNL1771432; Tue, 9 Jan 2024 09:45:44 +0100 Date: Tue, 9 Jan 2024 09:45:44 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] vect: Ensure both NITERSM1 and NITERS are INTEGER_CSTs or neither of them [PR113210] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.6 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hi! On the following testcase e.g. on riscv64 or aarch64 (latter with -O3 -march=armv8-a+sve ) we ICE, because while NITERS is INTEGER_CST, NITERSM1 is a complex expression like (short unsigned int) (a.0_1 + 255) + 1 > 256 ? ~(short unsigned int) (a.0_1 + 255) : 0 where a.0_1 is unsigned char. The condition is never true, so the above is equivalent to just 0, but only when trying to fold the above with PLUS_EXPR 1 we manage to simplify it (first ~(short unsigned int) (a.0_1 + 255) to -(short unsigned int) (a.0_1 + 255) and then (short unsigned int) (a.0_1 + 255) + 1 > 256 ? -(short unsigned int) (a.0_1 + 255) : 1 to (short unsigned int) (a.0_1 + 255) >= 256 ? -(short unsigned int) (a.0_1 + 255) : 1 and only at this point we fold the condition to be false. But the vectorizer seems to assume that if NITERS is known (i.e. suitable INTEGER_CST) then NITERSM1 also is, so the following hack ensures that if NITERS folds into INTEGER_CST NITERSM1 will be one as well. Bootstrapped/regtested on x86_64-linux and i686-linux, additionally tested with cross to aarch64-linux with that -O3 -march=armv8-a+sve on the testcase, ok for trunk? 2024-01-09 Jakub Jelinek PR tree-optimization/113210 * tree-vect-loop.cc (vect_get_loop_niters): If non-INTEGER_CST value in *number_of_iterationsm1 PLUS_EXPR 1 is folded into INTEGER_CST, recompute *number_of_iterationsm1 as the INTEGER_CST minus 1. * gcc.c-torture/compile/pr113210.c: New test. Jakub --- gcc/tree-vect-loop.cc.jj 2024-01-08 16:13:18.682939712 +0100 +++ gcc/tree-vect-loop.cc 2024-01-08 16:30:24.062626368 +0100 @@ -941,9 +941,22 @@ vect_get_loop_niters (class loop *loop, ??? For UINT_MAX latch executions this number overflows to zero for loops like do { n++; } while (n != 0); */ if (niter && !chrec_contains_undetermined (niter)) + { niter = fold_build2 (PLUS_EXPR, TREE_TYPE (niter), unshare_expr (niter), build_int_cst (TREE_TYPE (niter), 1)); + if (TREE_CODE (niter) == INTEGER_CST + && TREE_CODE (*number_of_iterationsm1) != INTEGER_CST) + { + /* If we manage to fold niter + 1 into INTEGER_CST even when + niter is some complex expression, ensure back + *number_of_iterationsm1 is an INTEGER_CST as well. See + PR113210. */ + *number_of_iterationsm1 + = fold_build2 (PLUS_EXPR, TREE_TYPE (niter), niter, + build_minus_one_cst (TREE_TYPE (niter))); + } + } *number_of_iterations = niter; } --- gcc/testsuite/gcc.c-torture/compile/pr113210.c.jj 2024-01-08 16:17:16.672620793 +0100 +++ gcc/testsuite/gcc.c-torture/compile/pr113210.c 2024-01-08 16:17:16.671620807 +0100 @@ -0,0 +1,13 @@ +/* PR tree-optimization/113210 */ + +unsigned char a, c; +unsigned short b; + +void +foo (void) +{ + c = a + 255; + b = c; + while (++b > 256) + ; +}