Message ID | Z8/uroxageTgukBD@tucnak |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DCE193858427 for <patchwork@sourceware.org>; Tue, 11 Mar 2025 08:06:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DCE193858427 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HLvYYJt5 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 ESMTP id 4258A385840E for <gcc-patches@gcc.gnu.org>; Tue, 11 Mar 2025 08:05:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4258A385840E 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 4258A385840E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741680311; cv=none; b=Hc22qN/18/BslzbCjsSz23HXvCsBp0y3NyiPzoB2ZFC1VE7nALEZCMIvUCC5Amw3D6IbZwRgFn3hBjDvBsjiDZJOyHhS9B/bOQTLW/QElsoCLDM3E71O6eT2wyOfysMDa9KDwgH+MTGL//L+kidOB9hB+fs30HSdXP7S8wPavB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741680311; c=relaxed/simple; bh=sJjtwG/gSSJ/w18Z9bd6BX/Q0l86RxR4GYcqQ9/JNNs=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=ckO4iKl5k+DzSh3jWTLV87l3M7xrXLmhgib07RWrrvDje/XCBPs9jlieUOwtYI4Yp3LCICTlyfw0msewt9ICPdcpEk3beIeaUNqBe7wSoQ/CRV2JBMS6KxxM/qB0e7HZWDeKNE1H/FRGWuXDPlTjAARTyJpZYCYsStwox/ldu1o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4258A385840E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741680311; 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=ajtg5Te+67/CN0l3E67SBSyVagrMHteU0zp9z2dJsUw=; b=HLvYYJt50pioR7LetxPJYOkF2UnTnd3FqXP4+LsEpJqetQaeQ+bibT0i7Sbe4vAhvF/wjM nOhwDHZMbEqupHPehbUmBBEXTnY/qT2KaFfgS4CPPQZIS5ga1QWMV2hItMtKDuJUmdi/Dr 0aHWqQjVC8j6LMfHKMkJ7zqn9dhYjPs= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-644-00a9p_mVNp2-6g298J4gKw-1; Tue, 11 Mar 2025 04:05:09 -0400 X-MC-Unique: 00a9p_mVNp2-6g298J4gKw-1 X-Mimecast-MFC-AGG-ID: 00a9p_mVNp2-6g298J4gKw_1741680308 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 809021809CA6; Tue, 11 Mar 2025 08:05:08 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.22.89.222]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E4D0130001A2; Tue, 11 Mar 2025 08:05:07 +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 52B854LB1927378 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 11 Mar 2025 09:05:04 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 52B852Pg1927376; Tue, 11 Mar 2025 09:05:02 +0100 Date: Tue, 11 Mar 2025 09:05:02 +0100 From: Jakub Jelinek <jakub@redhat.com> To: Richard Biener <rguenther@suse.de> Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree: Improve skip_simple_arithmetic [PR119183] Message-ID: <Z8/uroxageTgukBD@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 77tNADUp7SpslBjPE3LRC5zwEyhzCje4DBbXT2l343k_1741680308 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.5 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Reply-To: Jakub Jelinek <jakub@redhat.com> Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org |
Series |
tree: Improve skip_simple_arithmetic [PR119183]
|
|
Commit Message
Jakub Jelinek
March 11, 2025, 8:05 a.m. UTC
Hi! The following testcase takes very long time to compile, because skip_simple_arithmetic decides to first call tree_invariant_p on the second argument (and indirectly recurse there). I think before canonicalization of operands for commutative binary expressions (and for non-commutative ones always) it is pretty common that the first operand is a constant, something which tree_invariant_p handles immediately, so the following patch special cases that; I've added there a tree_invariant_p call too after the checks, while it is not really needed currently, tree_invariant_p has the same checks, I wanted to be prepared in case tree_invariant_p changes. But if you think I should avoid it, I can drop it too. This is just a partial fix, I think one can certainly construct a testcase which will still have horrible compile time complexity (but I've tried and haven't managed to do so), so perhaps we should just limit the recursion depth through skip_simple_arithmetic/tree_invariant_p with some defaulted argument. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2025-03-11 Jakub Jelinek <jakub@redhat.com> PR c/119183 * tree.cc (skip_simple_arithmetic): If first operand of binary expr is TREE_CONSTANT or TREE_READONLY with no side-effects, call tree_invariant_p on that operand first instead of on the second. * gcc.dg/pr119183.c: New test. Jakub
Comments
On Tue, 11 Mar 2025, Jakub Jelinek wrote: > Hi! > > The following testcase takes very long time to compile, because > skip_simple_arithmetic decides to first call tree_invariant_p on > the second argument (and indirectly recurse there). I think before > canonicalization of operands for commutative binary expressions > (and for non-commutative ones always) it is pretty common that the > first operand is a constant, something which tree_invariant_p handles > immediately, so the following patch special cases that; I've added > there a tree_invariant_p call too after the checks, while it is not > really needed currently, tree_invariant_p has the same checks, I wanted > to be prepared in case tree_invariant_p changes. But if you think > I should avoid it, I can drop it too. > > This is just a partial fix, I think one can certainly construct a testcase > which will still have horrible compile time complexity (but I've tried and > haven't managed to do so), so perhaps we should just limit the recursion > depth through skip_simple_arithmetic/tree_invariant_p with some defaulted > argument. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Can you add a comment before the code? OK with that change. > 2025-03-11 Jakub Jelinek <jakub@redhat.com> > > PR c/119183 > * tree.cc (skip_simple_arithmetic): If first operand of binary > expr is TREE_CONSTANT or TREE_READONLY with no side-effects, call > tree_invariant_p on that operand first instead of on the second. > > * gcc.dg/pr119183.c: New test. > > --- gcc/tree.cc.jj 2025-03-08 00:07:01.848908327 +0100 > +++ gcc/tree.cc 2025-03-10 17:04:48.630157371 +0100 > @@ -4105,7 +4105,12 @@ skip_simple_arithmetic (tree expr) > expr = TREE_OPERAND (expr, 0); > else if (BINARY_CLASS_P (expr)) > { > - if (tree_invariant_p (TREE_OPERAND (expr, 1))) > + if ((TREE_CONSTANT (TREE_OPERAND (expr, 0)) > + || (TREE_READONLY (TREE_OPERAND (expr, 0)) > + && !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))) > + && tree_invariant_p (TREE_OPERAND (expr, 0))) > + expr = TREE_OPERAND (expr, 1); > + else if (tree_invariant_p (TREE_OPERAND (expr, 1))) > expr = TREE_OPERAND (expr, 0); > else if (tree_invariant_p (TREE_OPERAND (expr, 0))) > expr = TREE_OPERAND (expr, 1); > --- gcc/testsuite/gcc.dg/pr119183.c.jj 2025-03-10 17:48:57.839774108 +0100 > +++ gcc/testsuite/gcc.dg/pr119183.c 2025-03-10 17:48:22.960253026 +0100 > @@ -0,0 +1,12 @@ > +/* PR c/119183 */ > +/* { dg-do compile } */ > + > +int foo (void); > +#define A(x) (1.0f * (1.0f * (1.0f * (1.0f * (1.0f * (1.0f * (1.0f * (1.0f * (x))))))))) > + > +float > +bar (float r) > +{ > + r += A (A (A (A (A (A (A (A (foo ())))))))); > + return r; > +} > > Jakub > >
--- gcc/tree.cc.jj 2025-03-08 00:07:01.848908327 +0100 +++ gcc/tree.cc 2025-03-10 17:04:48.630157371 +0100 @@ -4105,7 +4105,12 @@ skip_simple_arithmetic (tree expr) expr = TREE_OPERAND (expr, 0); else if (BINARY_CLASS_P (expr)) { - if (tree_invariant_p (TREE_OPERAND (expr, 1))) + if ((TREE_CONSTANT (TREE_OPERAND (expr, 0)) + || (TREE_READONLY (TREE_OPERAND (expr, 0)) + && !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))) + && tree_invariant_p (TREE_OPERAND (expr, 0))) + expr = TREE_OPERAND (expr, 1); + else if (tree_invariant_p (TREE_OPERAND (expr, 1))) expr = TREE_OPERAND (expr, 0); else if (tree_invariant_p (TREE_OPERAND (expr, 0))) expr = TREE_OPERAND (expr, 1); --- gcc/testsuite/gcc.dg/pr119183.c.jj 2025-03-10 17:48:57.839774108 +0100 +++ gcc/testsuite/gcc.dg/pr119183.c 2025-03-10 17:48:22.960253026 +0100 @@ -0,0 +1,12 @@ +/* PR c/119183 */ +/* { dg-do compile } */ + +int foo (void); +#define A(x) (1.0f * (1.0f * (1.0f * (1.0f * (1.0f * (1.0f * (1.0f * (1.0f * (x))))))))) + +float +bar (float r) +{ + r += A (A (A (A (A (A (A (A (foo ())))))))); + return r; +}