From patchwork Tue Apr 5 04:39:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1613333 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=iVjM8YwC; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KXgxK73DBz9sBJ for ; Tue, 5 Apr 2022 18:37:08 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A00A6385781A for ; Tue, 5 Apr 2022 08:37:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A00A6385781A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649147825; bh=xqyKmsTuS5KMok2awNWhKQHq/+c1W0YOKz8UDDrsZD0=; h=Resent-From:Resent-Date:Resent-To:Date:To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:Cc:From; b=iVjM8YwC43tJSs3N6bxwRIY9pyL0b2X5wlk0er2bbojnXsepCr55f8uB4IpIMKlsZ sOM64mamAoux2roEJv8ooxJgVkNrgmyLkL+8TmMahkKB5MvYeVPwfa6pwOSio+Uwxc G12qzF+xqiiszUnk+4lNNzmi+PTDzZMovL5BUypU= 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 121233858C52 for ; Tue, 5 Apr 2022 08:36:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 121233858C52 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-632-eoEnkd1HO8ehirKjA8T-eg-1; Tue, 05 Apr 2022 04:36:43 -0400 X-MC-Unique: eoEnkd1HO8ehirKjA8T-eg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 80E4A3C161B2; Tue, 5 Apr 2022 08:36:43 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.172]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 096D2112C067; Tue, 5 Apr 2022 08:36:41 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 2358aRoG3914146 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 5 Apr 2022 10:36:37 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 2358aL6d3914145; Tue, 5 Apr 2022 10:36:21 +0200 Resent-From: Jakub Jelinek Resent-Date: Tue, 5 Apr 2022 10:36:21 +0200 Resent-Message-ID: Resent-To: Richard Biener , gcc-patches@gcc.gnu.org Date: Tue, 5 Apr 2022 06:39:26 +0200 To: Richard Biener Subject: [PATCH] match.pd: Punt on optimizing sqrt with incorrect arg type [PR105150] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi! In the following testcase sqrt is declared as unprototyped function and so it depends on what type has the argument passed to it. If an argument of incorrect type is passed, the sqrt comparison simplification can create invalid GIMPLE. The patch fixes it by punting if there is a mismatch of types. As I didn't want to reindent 133 lines, the first hunk contains an ugly hack with if (false). If you prefer reindentation, I can do that too. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-04-05 Jakub Jelinek PR tree-optimization/105150 * match.pd (sqrt (x) cmp real_cst, sqrt (x) cmp sqrt (y)): Punt if sqrt operand has incompatible types. * gcc.dg/pr105150.c: New test. Jakub --- gcc/match.pd.jj 2022-03-18 18:32:36.000000000 +0100 +++ gcc/match.pd 2022-04-04 19:49:28.621934784 +0200 @@ -4927,6 +4927,10 @@ (define_operator_list SYNC_FETCH_AND_AND (simplify (cmp (sq @0) REAL_CST@1) (switch + (if (!types_match (TREE_TYPE (@0), TREE_TYPE (@1))) + /* Punt if there is a type mismatch. */ + (if (false) + @1)) (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) (switch /* sqrt(x) < y is always false, if y is negative. */ @@ -5062,7 +5066,7 @@ (define_operator_list SYNC_FETCH_AND_AND /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */ (simplify (cmp (sq @0) (sq @1)) - (if (! HONOR_NANS (@0)) + (if (! HONOR_NANS (@0) && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) (cmp @0 @1)))))) /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */ --- gcc/testsuite/gcc.dg/pr105150.c.jj 2022-04-04 19:53:03.597935060 +0200 +++ gcc/testsuite/gcc.dg/pr105150.c 2022-04-04 19:54:55.734370333 +0200 @@ -0,0 +1,8 @@ +/* PR tree-optimization/105150 */ +/* { dg-options "-w -Ofast" } */ + +#define A(name) __typeof (__builtin_##name (0)) name (); \ + float name##1 () { return !name (1); } \ + double name##2 () { return name (1.0L); } +#define B(name) A(name) A(name##l) +B (sqrt)