From patchwork Thu Dec 14 07:34:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 178542 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8375567dys; Wed, 13 Dec 2023 23:35:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEcZcjFnstAzoHTrqLcPQn+3d0XSErN8w4D2A3eCP4qGFtEUMYsbwFyNITwkxTuXvuOHxOo X-Received: by 2002:a05:622a:10c:b0:41e:1674:5dd8 with SMTP id u12-20020a05622a010c00b0041e16745dd8mr8102795qtw.48.1702539327903; Wed, 13 Dec 2023 23:35:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702539327; cv=pass; d=google.com; s=arc-20160816; b=IQYYTQ0Jvw2Q0npbc620J++FCdMdumK/iTLSf2WnzZ7F8dYpIvjCxMLb72W0OGOCMC V3UF+rj7d0OjxG9ZNgfQkHP80ct6EXH4KGLA2+oDmLIQi2XjtbqIgu94XaiJTDVL27Nf XB8xHEfUez8lN4Aq3L9m8FwXw6aLWPoXjAQrh8xTI7GSwM2zq32NECIjmbCYjZMlIIJ4 tjKAEmdTl90ys64tm8cnKyqBIo/0LD3kteBlH1xk6RwP8XNAxoKymUJWr4IBfD8E0NQY p7hSjYlUjKnoymCbFT4FfMyYV/HWONdSIpMkpFKueFe+yp3atx0epU2FYqyWTRGSuwE1 /3Sw== 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=t0G+rGUx6aJerhjWsgs5BCYBPu3Oi5uNZa2XpAcExLE=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=iDxqL6Mn8oKjCd1Znd1aQ/Cz/0Rfz2pKQql0lvU2Cds2CDgke26NnJsCfBOa218dPg 6k3rw9TtJ8Ed2mSypRpW0kADSt7HXj1QSwCpq4BJlqRd+XQlD7PgkElTuxnYHIhLf9ZU URbkzKt3nx4eQS8YCIdyHjTULkkG24aX28g56qJD2QVEgRb5NQ8o0s8z1Y2OmUCwAv0v 4igDy3cQ1PLZ+X3XJPY1RifXXBbXkxu2B/wzRkRf/ZYxoLufBnZjQiZfoilyVig+Fd9V tHzZ21hJPX5XvW/em+D0RDWWcXw+cN5AcsQQcTrDKK10o4HCBsSuaRsIeU8jgqA5AlIG SvEA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="DN/nwrN8"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id ex12-20020a05622a518c00b0042542256707si16216233qtb.544.2023.12.13.23.35.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 23:35:27 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="DN/nwrN8"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 79FAB3860753 for ; Thu, 14 Dec 2023 07:35:27 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 574FE3858D20 for ; Thu, 14 Dec 2023 07:35:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 574FE3858D20 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 574FE3858D20 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=1702539305; cv=none; b=cTkaSFUmo64+QVxHXbqMUcE7/zAjiv6w8k5uBhkN8QzBVEKlpwrriVgPaKC+Sdt3nRSjr8PEwheM9QUtdUH9S39MTXdQc8Qjq7xwFuzZBJTVnN4Qgvx17yO54I0JQ5q0G9Jnhmm8Uu29Ya00tBcXtbr9FvPe0ob717vhjPm58EA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702539305; c=relaxed/simple; bh=or/Q79+N9LT+6FsBFqAbakXPxYmJAj/z6YufDMrV+hc=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=RwMxsOsCzbmfNdZtSSSRt/5KaXydMX7zJiMgKU9un2fPkawPKZgbETaXF5rInnXeNSMqPCk7i9me1tB+U+nF5tF1vj7rejDuWj4JkXsjTL4K6+XyaS4KySphLAjxT9KZ8znG4gFv4yQi9Nw9BCj9YG2QbN3Tt2DDPlf+PHA657A= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702539303; 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=t0G+rGUx6aJerhjWsgs5BCYBPu3Oi5uNZa2XpAcExLE=; b=DN/nwrN8Dpms+310ecJb//td45ox3QuxV5VQSMuqADAZXj4IncRxkH1xPccrS9y/Demun9 zxTGHJ5A2yLm4BkAFAcpjKmnXzT3o+7dCQA3oiGslA0mJ/75f0mGemh+kMn7fEV0CJUlvz inC5wLWv7D/uJ/gar8d41I7qvPjnae0= 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-678-h2RhzxxzMqeJTZRcoNm9OQ-1; Thu, 14 Dec 2023 02:34:58 -0500 X-MC-Unique: h2RhzxxzMqeJTZRcoNm9OQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 C0ADE3812597; Thu, 14 Dec 2023 07:34:57 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6BAE551E3; Thu, 14 Dec 2023 07:34:57 +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 3BE7Ysjb1106748 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 14 Dec 2023 08:34:55 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BE7Ysch1106747; Thu, 14 Dec 2023 08:34:54 +0100 Date: Thu, 14 Dec 2023 08:34:54 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Simplify (t * u) / v -> t * (u / v) [PR112994] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 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_H3, 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: 1785241878370344302 X-GMAIL-MSGID: 1785241878370344302 Hi! The following testcase is optimized just on GENERIC (using strict_overflow_p = false; if (TREE_CODE (arg1) == INTEGER_CST && (tem = extract_muldiv (op0, arg1, code, NULL_TREE, &strict_overflow_p)) != 0) { if (strict_overflow_p) fold_overflow_warning (("assuming signed overflow does not occur " "when simplifying division"), WARN_STRICT_OVERFLOW_MISC); return fold_convert_loc (loc, type, tem); } ) but not on GIMPLE. The following included patch is what I've bootstrapped/regtested for it on x86_64-linux and i686-linux, unfortunately it regressed +FAIL: gcc.dg/Wstrict-overflow-3.c correct warning (test for warnings, line 12) test, we are indeed assuming that signed overflow does not occur when simplifying division in there. The attached version of the patch (which provides the simplification only for GIMPLE) fixes that, but I haven't bootstrapped/regtested it yet. And/or we could add the fold_overflow_warning (("assuming signed overflow does not occur " "when simplifying division"), WARN_STRICT_OVERFLOW_MISC); call into the simplification, but in that case IMHO it should go into the (t * u) / u -> t simplification as well, there we assume the exact same thing (of course, in both cases only in the spots where we don't verify it through ranger that it never overflows). Guarding the whole simplification to GIMPLE only IMHO makes sense because the above mentioned folding does it for GENERIC (and extract_muldiv even handles far more cases, dunno how many from that we should be doing on GIMPLE in match.pd and what could be done elsewhere; e.g. extract_muldiv can handle (x * 16 + y * 32) / 8 -> x * 2 + y * 4 etc.). Dunno about the fold_overflow_warning, I always have doubts about why such a warning is useful to users. Ok for trunk (and which version)? 2023-12-14 Jakub Jelinek PR tree-optimization/112994 * match.pd ((t * 2) / 2 -> t): Adjust comment to use u instead of 2. Punt without range checks if TYPE_OVERFLOW_SANITIZED. ((t * u) / v -> t * (u / v)): New simplification. * gcc.dg/tree-ssa/pr112994-1.c: New test. Jakub 2023-12-14 Jakub Jelinek PR tree-optimization/112994 * match.pd ((t * 2) / 2 -> t): Adjust comment to use u instead of 2. Punt without range checks if TYPE_OVERFLOW_SANITIZED. ((t * u) / v -> t * (u / v)): New simplification. * gcc.dg/tree-ssa/pr112994-1.c: New test. --- gcc/match.pd.jj 2023-12-13 11:21:15.852158970 +0100 +++ gcc/match.pd 2023-12-14 08:22:43.090755645 +0100 @@ -930,12 +930,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0))) (bit_and @0 (negate @1)))) -/* Simplify (t * 2) / 2) -> t. */ (for div (trunc_div ceil_div floor_div round_div exact_div) + /* Simplify (t * u) / u -> t. */ (simplify (div (mult:c @0 @1) @1) (if (ANY_INTEGRAL_TYPE_P (type)) - (if (TYPE_OVERFLOW_UNDEFINED (type)) + (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type)) @0 #if GIMPLE (with {value_range vr0, vr1;} @@ -945,7 +945,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) @0)) #endif - )))) + ))) +#if GIMPLE + /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v. */ + (simplify + (div (mult @0 INTEGER_CST@1) 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)) + (mult @0 (div! @1 @2)) + (with {value_range vr0, vr1;} + (if (get_range_query (cfun)->range_of_expr (vr0, @0) + && get_range_query (cfun)->range_of_expr (vr1, @1) + && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) + (mult @0 (div! @1 @2)))) + ))) +#endif +) #if GIMPLE (for div (trunc_div exact_div) --- gcc/testsuite/gcc.dg/tree-ssa/pr112994-1.c.jj 2023-12-13 16:58:25.757663610 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr112994-1.c 2023-12-13 16:43:16.413152969 +0100 @@ -0,0 +1,13 @@ +/* PR tree-optimization/112994 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-not " / \\\[2389-\\\]" "optimized" } } */ + +int f1 (int x) { return (x * 4) / 2; } +int f2 (int x) { return (x * 56) / 8; } +int f3 (int x) { return (x * 56) / -8; } +int f4 (int x) { int y = x * 4; return y / 2; } +int f5 (int x) { int y = x * 56; return y / 8; } +int f6 (int x) { int y = x * 56; return y / -8; } +unsigned f7 (unsigned x) { if (x > ~0U / 6) __builtin_unreachable (); unsigned y = x * 6; return y / 3; } +unsigned f8 (unsigned x) { if (x > ~0U / 63) __builtin_unreachable (); unsigned y = x * 63; return y / 9; } --- gcc/match.pd.jj 2023-12-13 11:21:15.852158970 +0100 +++ gcc/match.pd 2023-12-13 19:10:26.448927327 +0100 @@ -930,12 +930,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0))) (bit_and @0 (negate @1)))) -/* Simplify (t * 2) / 2) -> t. */ (for div (trunc_div ceil_div floor_div round_div exact_div) + /* Simplify (t * u) / u -> t. */ (simplify (div (mult:c @0 @1) @1) (if (ANY_INTEGRAL_TYPE_P (type)) - (if (TYPE_OVERFLOW_UNDEFINED (type)) + (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type)) @0 #if GIMPLE (with {value_range vr0, vr1;} @@ -945,6 +945,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) @0)) #endif + ))) + /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v. */ + (simplify + (div (mult @0 INTEGER_CST@1) 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)) + (mult @0 (div! @1 @2)) +#if GIMPLE + (with {value_range vr0, vr1;} + (if (get_range_query (cfun)->range_of_expr (vr0, @0) + && get_range_query (cfun)->range_of_expr (vr1, @1) + && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) + (mult @0 (div! @1 @2)))) +#endif )))) #if GIMPLE --- gcc/testsuite/gcc.dg/tree-ssa/pr112994-1.c.jj 2023-12-13 16:58:25.757663610 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr112994-1.c 2023-12-13 16:43:16.413152969 +0100 @@ -0,0 +1,13 @@ +/* PR tree-optimization/112994 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-not " / \\\[2389-\\\]" "optimized" } } */ + +int f1 (int x) { return (x * 4) / 2; } +int f2 (int x) { return (x * 56) / 8; } +int f3 (int x) { return (x * 56) / -8; } +int f4 (int x) { int y = x * 4; return y / 2; } +int f5 (int x) { int y = x * 56; return y / 8; } +int f6 (int x) { int y = x * 56; return y / -8; } +unsigned f7 (unsigned x) { if (x > ~0U / 6) __builtin_unreachable (); unsigned y = x * 6; return y / 3; } +unsigned f8 (unsigned x) { if (x > ~0U / 63) __builtin_unreachable (); unsigned y = x * 63; return y / 9; }