From patchwork Mon Feb 26 06:56:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1904094 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=YjMJMqSd; 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 4TjryJ3GGvz23qG for ; Mon, 26 Feb 2024 17:56:56 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7234D3858C3A for ; Mon, 26 Feb 2024 06:56:53 +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 727923858CDB for ; Mon, 26 Feb 2024 06:56:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 727923858CDB 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 727923858CDB 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=1708930594; cv=none; b=SMyDjQ9ad93qZEt8jj99ftchaYybt/MmcX/izSEsHvDvHPVUhVtL8T4mSjYlAUhYxuuKf41cab1a8RSmRqmcovezwqhH9cHfLlSLQO+fsNLZT0petT3GqiNqE42FIiyu2P/Os24S3N867dNoV0o5dAUjXiTioeEyR8DhE/giTj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708930594; c=relaxed/simple; bh=7glMH4vHYXaoMwFk7NbQODiD52xXWEYXwK3eNDXzoTs=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=iUkjK42bkUQhctQb6FKa7USXhaRalcqgW89DhwySu/IM1b+uQpraiUSGFZzzU4LppTSkGPBMcFLl1NbE8QKtH/+PTkaE6N/j/1aMP5TgvIx9EJrIdlvSLYu0HOZ0Kg7rReEZZXg6Wg8tK0mIhf86AHgVu2FHY+VGXSxBUpX5nuE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708930593; 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=gb95c+yq19A9hJiDDGLjXXR8oal4FQvNtvmwvr9xdjs=; b=YjMJMqSdRDSHw74B0ludR043Z6GC7BN2qud3aNwWt2e3+gKr2jQrhLAQDUlFPxCUl7Klia PJtZPxaMaGOM3wXU/TDjTXd2MvZrHGlVX6G7sVvGBallJrWHH7455NiX2fEBrL/iKsoV+a r5d2clWg8S4WlrDVUHAUWGJE6Qg8bfE= 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-587-Pu2AqKSaNEmvnKkNa42J6w-1; Mon, 26 Feb 2024 01:56:31 -0500 X-MC-Unique: Pu2AqKSaNEmvnKkNa42J6w-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 4C3D51021F66; Mon, 26 Feb 2024 06:56:31 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0FC9C492BC6; Mon, 26 Feb 2024 06:56:30 +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 41Q6uS9U1346706 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 26 Feb 2024 07:56:29 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41Q6uSEM1346705; Mon, 26 Feb 2024 07:56:28 +0100 Date: Mon, 26 Feb 2024 07:56:27 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Guard 2 simplifications on integral TYPE_OVERFLOW_UNDEFINED [PR114090] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 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, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! These 2 patterns are incorrect on floating point, or for -fwrapv, or for -ftrapv, or the first one for unsigned types (the second one is mathematically correct, but we ought to just fold that to 0 instead). So, the following patch properly guards this. I think we don't need && !TYPE_OVERFLOW_SANITIZED (type) because in both simplifications there would be UB before and after on signed integer minimum. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-26 Jakub Jelinek PR tree-optimization/114090 * match.pd ((x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x): Restrict pattern to ANY_INTEGRAL_TYPE_P and TYPE_OVERFLOW_UNDEFINED types. ((x <= 0 ? -x : 0) -> max(-x, 0)): Likewise. * gcc.dg/pr114090.c: New test. Jakub --- gcc/match.pd.jj 2024-02-22 10:09:48.678446435 +0100 +++ gcc/match.pd 2024-02-24 19:23:32.201014245 +0100 @@ -453,8 +453,9 @@ (define_operator_list SYNC_FETCH_AND_AND /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */ (simplify - (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop)) - (abs @0)) + (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop)) + (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type)) + (abs @0))) /* X * 1, X / 1 -> X. */ (for op (mult trunc_div ceil_div floor_div round_div exact_div) @@ -4218,8 +4219,9 @@ (define_operator_list SYNC_FETCH_AND_AND /* (x <= 0 ? -x : 0) -> max(-x, 0). */ (simplify - (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) - (max @2 @1)) + (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) + (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type)) + (max @2 @1))) /* (zero_one == 0) ? y : z y -> ((typeof(y))zero_one * z) y */ (for op (bit_xor bit_ior plus) --- gcc/testsuite/gcc.dg/pr114090.c.jj 2024-02-24 19:38:56.301096850 +0100 +++ gcc/testsuite/gcc.dg/pr114090.c 2024-02-24 19:42:26.917153801 +0100 @@ -0,0 +1,38 @@ +/* PR tree-optimization/114090 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fwrapv" } */ + +__attribute__((noipa)) int +foo (int x) +{ + int w = (x >= 0 ? x : 0); + int y = -x; + int z = (y >= 0 ? y : 0); + return w + z; +} + +__attribute__((noipa)) int +bar (int x) +{ + int w = (x >= 0 ? x : 0); + int z = (x <= 0 ? -x : 0); + return w + z; +} + +__attribute__((noipa)) int +baz (int x) +{ + return x <= 0 ? -x : 0; +} + +int +main () +{ + int v = -__INT_MAX__ - 1; + if (foo (v) != 0) + __builtin_abort (); + if (bar (v) != v) + __builtin_abort (); + if (baz (v) != v) + __builtin_abort (); +}