From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 7C4103858D39 for ; Wed, 19 Oct 2022 08:02:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7C4103858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666166537; 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=Cr5SVtCVJTnYo2EnNyWPsMWMfj3uTmPVJkcdOfM90Wo=; b=cA/U4pD/l+2Us5W9NUlg0snfLppgRKQ8fsSIRM0tYJmjAiJGRKvXV3aJ02ds1S13KTyRt7 /+ui89Akt/sUqCk+pVrRVfaaOSlwpiNXRY9uThunTIwKxlt+R2jLYdwbrkmoDhGmwzT+Rh 9PaVM8oxeKdKzZqHj83Xd2Uh0pQ9Hu4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-290-TjIQuxDkM8OdG1CYe6dHfg-1; Wed, 19 Oct 2022 04:02:15 -0400 X-MC-Unique: TjIQuxDkM8OdG1CYe6dHfg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8A269811E75; Wed, 19 Oct 2022 08:02:15 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.252]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 49F102024CBB; Wed, 19 Oct 2022 08:02:15 +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 29J82C0r3797147 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 19 Oct 2022 10:02:13 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 29J82BWd3797146; Wed, 19 Oct 2022 10:02:11 +0200 Date: Wed, 19 Oct 2022 10:02:11 +0200 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Add 2 TYPE_OVERFLOW_SANITIZED checks [PR106990] Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.8 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hi! As requested in the PR, this adds 2 TYPE_OVERFLOW_SANITIZED checks and corresponding testcase. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-10-19 Jakub Jelinek PR tree-optimization/106990 * match.pd ((~X - ~Y) -> Y - X, -x & 1 -> x & 1): Guard with !TYPE_OVERFLOW_SANITIZED (type). * c-c++-common/ubsan/pr106990.c: New test. --- gcc/match.pd.jj 2022-10-13 15:51:08.928071208 +0200 +++ gcc/match.pd 2022-10-18 16:01:12.145705958 +0200 @@ -1331,8 +1331,9 @@ (define_operator_list SYNC_FETCH_AND_AND /* (~X - ~Y) -> Y - X. */ (simplify (minus (bit_not @0) (bit_not @1)) - (with { tree utype = unsigned_type_for (type); } - (convert (minus (convert:utype @1) (convert:utype @0))))) + (if (!TYPE_OVERFLOW_SANITIZED (type)) + (with { tree utype = unsigned_type_for (type); } + (convert (minus (convert:utype @1) (convert:utype @0)))))) /* ~(X - Y) -> ~X + Y. */ (simplify @@ -8176,5 +8177,6 @@ and, /* -x & 1 -> x & 1. */ (simplify - (bit_and (negate @0) integer_onep@1) - (bit_and @0 @1)) + (bit_and (negate @0) integer_onep@1) + (if (!TYPE_OVERFLOW_SANITIZED (type)) + (bit_and @0 @1))) --- gcc/testsuite/c-c++-common/ubsan/pr106990.c.jj 2022-10-18 15:56:20.260656260 +0200 +++ gcc/testsuite/c-c++-common/ubsan/pr106990.c 2022-10-18 16:17:37.295403933 +0200 @@ -0,0 +1,29 @@ +/* PR tree-optimization/106990 */ +/* { dg-do run { target int32 } } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + +__attribute__((noipa)) int +foo (void) +{ + int x = -1956816001; + int y = 1999200512; + return ~x - ~y; +} + +__attribute__((noipa)) int +bar (void) +{ + int x = -__INT_MAX__ - 1; + return -x & 1; +} + +int +main () +{ + foo (); + bar (); + return 0; +} + +/* { dg-output "signed integer overflow: 1956816000 - -1999200513 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself" } */ Jakub