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: 206277 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1906004dyb; Sun, 25 Feb 2024 22:57:15 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWJ90weMkG25E0ww1mjG/ivOn4ttOjXTguHZNYL9o8lCaQaTzTrYZoaT8VelAm9moYouY8YZ0+QhpwWrWd5jRSNtXiElw== X-Google-Smtp-Source: AGHT+IGWZFp7IeTULHdPXmkRHY96Fb7vF80WvgMbGNNStcBKdhlKNEz8hXZChajQbOid0hrhTtNf X-Received: by 2002:ac8:5fc2:0:b0:42e:7eba:687e with SMTP id k2-20020ac85fc2000000b0042e7eba687emr3511243qta.61.1708930635588; Sun, 25 Feb 2024 22:57:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708930635; cv=pass; d=google.com; s=arc-20160816; b=SrOOAZKEKEys+xsT3kMhobjVmL3FBM8ASzE2QMtAheJblk0Hig2iL3XkR/wgSHHvC0 8VWh9977U+Hmn34VSOp/kQEqQCXOQUC2B+Mt1IBUvn2Z7CzTrd0nvkwAcbPJpLcf9C6t Qo2OSWTE9g4Sq/fJ5u4fPwwVXPHam4viQ7UE9Db3xPiRjjusB2mQfFt6HgzRIHmSaGQY lFEEJXMvqonN+RRw0uOeH/yjmCzu8ZbVreOYxifiC7Jj6Z9Q+ZpRMvjtYk2YLpB0AgCn TC+COat37whu8tkkTcLk9MqygeXPWc46bI32sa8uLs9DA3Zwwb3aPlwHVUdDVrvClVVP 3vTw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=gb95c+yq19A9hJiDDGLjXXR8oal4FQvNtvmwvr9xdjs=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=MX/m3FVMhzDZqH9C5OcbVh4rtB4OGXJra5QciStPiWhp8WPtwXNfxTSrHyfow6WlUP OrvR70227yCxuQKy0qqwfTIZo+E+OWiwuKEV2BToxS5dQRxH7CoLXTphy2pDwXzbanDw 1TP6kTufcID27KgzeVzCn4oHRgcdPGTkNFhrRNRvjK4qKNr0ivUs3mFvzPAcoV/upqLs wKHJVZcc+Z4LmYGUM+w/rvN42d3G+Psdg15gxSIbh/10N/CejC+qvKIPKH+4N4Htij9F Gf5O6D37IxqtGdxo5njmy8LR5PGSxAZ0s2DozwScPjWjfvlN8jDZTChhiLSaPTOAix/j fY1A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YjMJMqSd; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id s3-20020ac85283000000b0042e4b1c5ccesi4406751qtn.399.2024.02.25.22.57.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 22:57:15 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YjMJMqSd; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4CBAD3858C42 for ; Mon, 26 Feb 2024 06:57:15 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791943649862067270 X-GMAIL-MSGID: 1791943649862067270 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 (); +}