From patchwork Wed May 8 07:56:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1932877 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OjtwMl1N; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VZ6tS0n0pz20fc for ; Wed, 8 May 2024 17:57:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 522B73937425 for ; Wed, 8 May 2024 07:57:02 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 943E73898C46 for ; Wed, 8 May 2024 07:56:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 943E73898C46 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 943E73898C46 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715155002; cv=none; b=sCWqtZn2mMiBuv4uG8nDMUf4pU9E37tjF2w2M58XGD8BV9atFqJzHius2QHPstDrpZZduBvrbmzRy3TZDywDlrXON1+j72RCb2N3rHx8RCDvgZ/H9VBKLS2ghwQvWXzEhsz4+5degUWVKZYJ2JkfzPfETRxMAABRzhEzBmGvnr8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715155002; c=relaxed/simple; bh=F3XOim9mti5Zh9bjiG4td3M8g4jXXt7y9SLPWBxKqbI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=aV7LzbERz2Hcbk35zddKJd4Z66w4mJdVn48BvyDLt7xTjgeuL0K7+gmJYxECX7WXFqaI84Q5qPPNfCUhIYE+sde1CT5xbd+ryOkqi92rRQvzvFVPdl3Q+u5Z05KhPZ25bSPW8GY0kTnAq1MYBpN/RfjPaiJxD80SZuzzl/ahJS0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715155000; 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=P62o4F02qJEm2a8xdaMrOcm0viebIx38zUvPoi+Ix78=; b=OjtwMl1NELyrXIHxgKAadtHa6JDynO2OqnrLlz3SloypxZgOtl33oLJtBax/uYkC+zqJu+ /KZLESKZpDrcBF3C9dVScMCEb8qaC9EoH1b1QDIMWvOoO6/6Cxn/rFaI9DrMx1FNpLecDk G2cEoQORuTIYEE/j7gzhmYQPmlYfkQQ= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-101-bbPCyDXgMHuVsKWVkkZdhg-1; Wed, 08 May 2024 03:56:38 -0400 X-MC-Unique: bbPCyDXgMHuVsKWVkkZdhg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 D9A4C29ABA0B; Wed, 8 May 2024 07:56:37 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.64]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9E00F3C25; Wed, 8 May 2024 07:56:37 +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 4487uaqR1049356 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 8 May 2024 09:56:36 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4487uapx1049355; Wed, 8 May 2024 09:56:36 +0200 Date: Wed, 8 May 2024 09:56:35 +0200 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] reassoc: Fix up optimize_range_tests_to_bit_test [PR114965] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 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_H4, RCVD_IN_MSPIKE_WL, 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.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+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The optimize_range_tests_to_bit_test optimization normally emits a range test first: if (entry_test_needed) { tem = build_range_check (loc, optype, unshare_expr (exp), false, lowi, high); if (tem == NULL_TREE || is_gimple_val (tem)) continue; } so during the bit test we already know that exp is in the [lowi, high] range, but skips it if we have range info which tells us this isn't necessary. Also, normally it emits shifts by exp - lowi counter, but has an optimization to use just exp counter if the mask isn't a more expensive constant in that case and lowi is > 0 and high is smaller than prec. The following testcase is miscompiled because the two abnormal cases are triggered. The range of exp is [43, 43][48, 48][95, 95], so we on 64-bit arch decide we don't need the entry test, because 95 - 43 < 64. And we also decide to use just exp as counter, because the range test tests just for exp == 43 || exp == 48, so high is smaller than 64 too. Because 95 is in the exp range, we can't do that, we'd either need to do a range test first, i.e. if (exp - 43U <= 48U - 43U) if ((1UL << exp) & mask1)) or need to subtract lowi from the shift counter, i.e. if ((1UL << (exp - 43)) & mask2) but can't do both unless r.upper_bound () is < prec. The following patch ensures that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-05-08 Jakub Jelinek PR tree-optimization/114965 * tree-ssa-reassoc.cc (optimize_range_tests_to_bit_test): Don't try to optimize away exp - lowi subtraction from shift count unless entry test is emitted or unless r.upper_bound () is smaller than prec. * gcc.c-torture/execute/pr114965.c: New test. Jakub --- gcc/tree-ssa-reassoc.cc.jj 2024-01-12 10:07:58.384848977 +0100 +++ gcc/tree-ssa-reassoc.cc 2024-05-07 18:18:45.558814991 +0200 @@ -3418,7 +3418,8 @@ optimize_range_tests_to_bit_test (enum t We can avoid then subtraction of the minimum value, but the mask constant could be perhaps more expensive. */ if (compare_tree_int (lowi, 0) > 0 - && compare_tree_int (high, prec) < 0) + && compare_tree_int (high, prec) < 0 + && (entry_test_needed || wi::ltu_p (r.upper_bound (), prec))) { int cost_diff; HOST_WIDE_INT m = tree_to_uhwi (lowi); --- gcc/testsuite/gcc.c-torture/execute/pr114965.c.jj 2024-05-07 18:17:16.767031821 +0200 +++ gcc/testsuite/gcc.c-torture/execute/pr114965.c 2024-05-07 18:15:52.332188943 +0200 @@ -0,0 +1,30 @@ +/* PR tree-optimization/114965 */ + +static void +foo (const char *x) +{ + + char a = '0'; + while (1) + { + switch (*x) + { + case '_': + case '+': + a = *x; + x++; + continue; + default: + break; + } + break; + } + if (a == '0' || a == '+') + __builtin_abort (); +} + +int +main () +{ + foo ("_"); +}