From patchwork Thu Dec 14 07:37:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 178544 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8376367dys; Wed, 13 Dec 2023 23:38:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IFTLoOBy6c10mSgMlXsjwP1XxhvrgW05fH+/QcE6CbdMTGYWPFonRIBSSTDwDSFVRhEIICL X-Received: by 2002:ad4:5cad:0:b0:67f:3ee:4f5f with SMTP id q13-20020ad45cad000000b0067f03ee4f5fmr2197548qvh.112.1702539480681; Wed, 13 Dec 2023 23:38:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702539480; cv=pass; d=google.com; s=arc-20160816; b=W/ShgeQeNbTd6pzBzcxwewSRhOycKclxy9vv9ufs2YZs5DxDYU0mQGIpRY09uf6ojV +CseJw2Y7ShAgJtRJmdYOz4tGLvqVmbZl24O4ZVUq4lJkoxCD51zzEKVU5sDGC2EFK1J Q68FkYVWBMaW4RL+yRynwTDGL1F7/DZwof/qhKs3+Gg3JFy08PvbK3HZzosw9pfZFJch PZXV806LTZAk25uuus0UnKnjJSJglWqzgb2B9QqRPgP27lVbrvdTSyadJO95Nbt3goK4 w15E9ki3JdW5kK9CJ/LamQjO3MXGDnPWXyIR9xVOAuGhm5Jj8r4LXRGg3K44UdzcWUS/ 59ig== 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=4UUktOjuyUkURChCFfz64IHNYbwY+RioWiNRjz5fBek=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=ieDwP+MQIDKfidAXRQj+P65V16/lJd/xEE/BMwwYGaW7MEG8xTjt1z/9s6Xx7L2mZr 2Iv/0jNETauw9xgk9SvOaYJRoQ3ogkai7plgLJ3wsxlPvfqlaaLJrOobmhN/TgGTEis5 d74xNstrV0Udk0ya6cNseSUitd3ow1Q5DqCy3CjPGjPo1vR6Go/Pt6ca5aNe3oP0sLLx YT4tLzb5FMkIJYuNNjbPAvL5lCaLgY2KhtOoGN+P71mhM3BZFgWs1GIkzQX3xS8yYOk5 UW4cXJEA+ly5CWAjph0fvlg2s6XzcXzCi+i8OiZ2tz/D+FwXY1Xae/uf6pHzC30967MZ VUEA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OpSF55Js; 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 oq2-20020a05620a610200b0077dcec33533si13467885qkn.201.2023.12.13.23.38.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 23:38:00 -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=OpSF55Js; 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 47041386181B for ; Thu, 14 Dec 2023 07:38:00 +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 9BEE2385841F for ; Thu, 14 Dec 2023 07:37:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9BEE2385841F 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 9BEE2385841F 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=1702539457; cv=none; b=lB/JTq709+EqyBndv3ReRdFSNikeJ/aoIexr9jSn8D6kTKZ3pMUrP6xWWro+UZ/KJsGgEcBjisoFpO3b8EIdna3OrtnElmRMDYJfPpTcpBTO4onBZIIQb5zcmnecxST3LEmiS4S/gcjyKe+KwQYSHFkPjpEQrgHh9/lwdVH9wcg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702539457; c=relaxed/simple; bh=zS2MnKwEEFBhoPrcR8e12cSqNaTbRhsilgTvPHC2f80=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=apd6IjSmULqvR+7l2zlZxRlophY/X4mgrOdNAeFTL3oapfqxUKmV9IF3d/hdszeZWfF46j7WUO1TG/U9PxAG3O48semrUjG6nIQXhfqSuiqVWuEWvLStxsppD69Glwn9xcQObDBxLsSUia332/lbU2QPz152AUftcHmAq8aRxGk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702539456; 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=4UUktOjuyUkURChCFfz64IHNYbwY+RioWiNRjz5fBek=; b=OpSF55JsdBh8XyUgPkXXRSPWtYoO8vvbE9c6S0L72gymVCI3ONpgd3pcQT6r1gnYNcy5Ry cvVOhKSBSSqduLAt/7POPuGBF0chLd+rbHlTFiTAe2zp+Pd3pmn8rrU9+o3dpA3Ycagl73 qLbm7ukwcy7Jra6zT5sBC/MV0hfEmgA= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-119-poG0pgGROhGSSofDw57vSQ-1; Thu, 14 Dec 2023 02:37:32 -0500 X-MC-Unique: poG0pgGROhGSSofDw57vSQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 6AA65285F98D; Thu, 14 Dec 2023 07:37:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2F1083C25; Thu, 14 Dec 2023 07:37:32 +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 3BE7bTR21106758 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 14 Dec 2023 08:37:30 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BE7bTLp1106757; Thu, 14 Dec 2023 08:37:29 +0100 Date: Thu, 14 Dec 2023 08:37:28 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Simplify (t * u) / (t * v) [PR112994] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 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_H4, RCVD_IN_MSPIKE_WL, 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: 1785242038839120040 X-GMAIL-MSGID: 1785242038839120040 Hi! On top of the previously posted patch, this simplifies say (x * 16) / (x * 4) into 4. Unlike the previous pattern, this is something we didn't fold previously on GENERIC, so I think it shouldn't be all wrapped with #if GIMPLE. The question whether there should be fold_overflow_warning for the TYPE_OVERFLOW_UNDEFINED case remains. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk (with or without fold_overflow_warning)? 2023-12-13 Jakub Jelinek PR tree-optimization/112994 * match.pd ((t * u) / (t * v) -> (u / v)): New simplification. * gcc.dg/tree-ssa/pr112994-2.c: New test. Jakub --- gcc/match.pd.jj 2023-12-13 18:43:51.277839661 +0100 +++ gcc/match.pd 2023-12-13 18:43:45.891913683 +0100 @@ -962,6 +960,23 @@ (define_operator_list SYNC_FETCH_AND_AND && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) (mult @0 (div! @1 @2)))) #endif + ))) + /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v. */ + (simplify + (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2)) + (if (INTEGRAL_TYPE_P (type) + && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED)) + (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type)) + (div @1 @2) +#if GIMPLE + (with {value_range vr0, vr1, vr2;} + (if (get_range_query (cfun)->range_of_expr (vr0, @0) + && get_range_query (cfun)->range_of_expr (vr1, @1) + && get_range_query (cfun)->range_of_expr (vr2, @2) + && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1) + && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2)) + (div @1 @2))) +#endif )))) #if GIMPLE --- gcc/testsuite/gcc.dg/tree-ssa/pr112994-2.c.jj 2023-12-13 19:07:20.882475735 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr112994-2.c 2023-12-13 19:07:02.597726855 +0100 @@ -0,0 +1,15 @@ +/* PR tree-optimization/112994 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times "return 2;" 3 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "return 7;" 3 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "return -7;" 2 "optimized" } } */ + +int f1 (int x) { return (x * 4) / (x * 2); } +int f2 (int x) { return (x * 56) / (x * 8); } +int f3 (int x) { return (x * 56) / (x * -8); } +int f4 (int x) { int y = x * 4; return y / (x * 2); } +int f5 (int x) { int y = x * 56; return y / (x * 8); } +int f6 (int x) { int y = x * 56; return y / (x * -8); } +unsigned f7 (unsigned x) { if (x > ~0U / 4) __builtin_unreachable (); unsigned y = x * 4; return y / (x * 2); } +unsigned f8 (unsigned x) { if (x > ~0U / 56) __builtin_unreachable (); unsigned y = x * 56; return y / (x * 8); }