From patchwork Sun Oct 22 00:12:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 156509 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp535710vqx; Sat, 21 Oct 2023 17:13:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHJJFyIrmMbILjnu+iWbWHr+suSkYJJmXchVnnkmPpCmA0KmqqpoosU7znKGuGSs50eQKbo X-Received: by 2002:a05:620a:2484:b0:76e:e66c:92f8 with SMTP id i4-20020a05620a248400b0076ee66c92f8mr8827298qkn.8.1697933594886; Sat, 21 Oct 2023 17:13:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697933594; cv=pass; d=google.com; s=arc-20160816; b=mNWCkZhc4x6CnQDCu8tdIKDiNLK2LvKjfJnvg0ibBG9JfcSqzZBgFTd7EyBhC9yurh wVihrPwiyp0vO+zSB8MMR4Gj+z4BDat5zVifP27SWGZG6j3lUfTa9QHV4QSC4ow2bC5I GKNpoph9EeiCLHGKRZ2FkWXp80zlMzUfYE1ia+wzKC9RM1LehHrVeFRazUYvm4lFysdN gpCB7/Gpx1vzyPwwPCoJLv7IFuzTbKdYHI9VXjIn/i0p6bpvUJQGukK7JAd0jMJd0/t3 CoKVpWePmTy16+q/CHvOkCxlVQx1VumZAvuOM2RhYqJLP9oK/+zPH82cYW9s/602x/E2 +jvQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=0kWRfLnme41MSlB9OrIz7N516UGeVwqkwdr9lhKyLTU=; fh=HUGtWGYelUIGEawCthw3zL4YE3u5HCOc2KzDDaWspeg=; b=jx7GmkWkDTaMdQotK60KnV8wZPPWW5kf8qKg9Ax51cD8nvnRN3uvZnulBq1Rxi8ytL mbQx+8qyFmZD+mq5vET6U124D/EhUv/0PnUwCoglrw1D2+BeFHe+p9VEOD1/8WQ5Afk9 CAIXqFX6SCV1dAgXblhHUmDyZt3Mrd11lgpbnSWYvkcDVn1a5rdr+xGekXgiMeoxN7xg cBBZSxQS4uaM4segDhA0G1rsZCiBShUqbyorHTHd9saL4ZumKpLhhzhlPhhlQOcUtn2Z CQ5PsKcrc5zEsSQptrx765kkBNeA3EcPR77+GYV6q/F4ERVldFaxfoc2zID8IaRUbdFD XpCg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mYFhHwVP; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w15-20020a05620a444f00b00767d6398eb2si3586292qkp.341.2023.10.21.17.13.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Oct 2023 17:13:14 -0700 (PDT) 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=@gmail.com header.s=20230601 header.b=mYFhHwVP; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 85FFF38582BC for ; Sun, 22 Oct 2023 00:13:14 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by sourceware.org (Postfix) with ESMTPS id 6A56E3858C5F for ; Sun, 22 Oct 2023 00:12:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6A56E3858C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6A56E3858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::631 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697933570; cv=none; b=w1rBAz5w543Kt6kzKJ0ccYv8HWSK2yqoVtTzWmOZVqM9zwFqYJHMFVsuBxoApRIevrWsQlT8Zzjt/jciiJ/C0jz+m2ei5U8uKYxMrdF00gh+2kQF1y3xCogIAAwyaGNeQpSe6Du4l86iLWbbjnLLifjARF2FyJ3FZuuzvnc/EAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697933570; c=relaxed/simple; bh=uILomSWUpSb3wr6jyPIuFgjgXhtns2FQmjW3kqtd/+k=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=u9qlWjQVXGEQ0nJAglsqTQHiFTNLZWFdY3fU0YRj7XQ1zER1SkL2ueRcUBr2D5CR4fuccH8Rh/1a7Ekl8QSA9eIjbQndy2E0ZxreChY0byuVls1CT7YBv1HH2KjP81l5oXZUTVx5uj888Q9kZ9z5XtVtUq7HOc86UK2T0ywImkw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1c9e95aa02dso15317385ad.0 for ; Sat, 21 Oct 2023 17:12:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697933567; x=1698538367; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=0kWRfLnme41MSlB9OrIz7N516UGeVwqkwdr9lhKyLTU=; b=mYFhHwVPjyPsSnpkN+64ZLMSOOSiUpBZpaxS4vH1As8/OKhmoEsj5POT4ce75kSjGG nkGAZlValTEr7e0rJPLStdOtIYgA6dpQkPjyqWpocvZqNk3uX7FaPvsX/qmMxAd54uh6 nLJFHxAR5tdswdaBgQnCcW7sIxKB6v38J+6WDYYaxm0M3qac6poVLYLCObrwZBW24x72 WtluiI/GdPWioGEtdwnMcZW0ze2fuIaf2h05+rrZ0OQcNwB3EJif9qr6k6KTOrokW2il PV0TSGnYR2jvhNFNZFRtaMf0Cr8539r15xmPc3J5jKWkMhN4W9FCOoA/iJsxeNS7PtOI 52Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697933567; x=1698538367; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0kWRfLnme41MSlB9OrIz7N516UGeVwqkwdr9lhKyLTU=; b=Ykfww/YkZTlDlSwPf1satzybSp8Uq/VANbvoAzskK7ua3Pedmwfpa6r/elq+5zc3o2 xV/CZigADpjV30LYX8nqtxHxsc4qMQNb6h4ImYBXkldB2dZxzWnLR7C3E/EBdsT+Dv02 OayIWZ2HE4jRCBd60D/wAQBSPgmyZWEzEq59jYeFcjhv00LN7bxN73adgbehBtysyWQP 9+QC8pcHG7LuL1ftsM12pk5UPsmT7DQfF4Di6XGjGt7/ud2weaOn+cFyK7ZqUKyLaiCP wiml2IhbFILsaQ4jiJT5l3GPYcatoEGCTxVFbb1ax2L+6yS69oXo3RjiqiuhAA8waTEz gyzQ== X-Gm-Message-State: AOJu0YwLt1AnyJj1VPRDyJTim1aTLHsjMx9bUDRlK3Y0UIGeW6DOmZe/ AWx6AOT5jXTeuoMUvYTb5AZ9y9My9V7AsQ== X-Received: by 2002:a17:902:e752:b0:1bc:4f04:17f4 with SMTP id p18-20020a170902e75200b001bc4f0417f4mr12402803plf.30.1697933566985; Sat, 21 Oct 2023 17:12:46 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id ji5-20020a170903324500b001b06c106844sm3689895plb.151.2023.10.21.17.12.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Oct 2023 17:12:46 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCHv2] move the (a-b) CMP 0 ? (a-b) : (b-a) optimization from fold_cond_expr_with_comparison to match Date: Sat, 21 Oct 2023 17:12:33 -0700 Message-Id: <20231022001232.2713374-1-pinskia@gmail.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780250176203833422 X-GMAIL-MSGID: 1780412417450736366 From: Andrew Pinski This patch moves the `(a-b) CMP 0 ? (a-b) : (b-a)` optimization from fold_cond_expr_with_comparison to match. Bootstrapped and tested on x86_64-linux-gnu. Changes in: v2: Removes `(a == b) ? 0 : (b - a)` handling since it was handled via r14-3606-g3d86e7f4a8ae Change zerop to integer_zerop for `(a - b) == 0 ? 0 : (b - a)`, Add `(a - b) != 0 ? (a - b) : 0` handling. gcc/ChangeLog: * match.pd (`(A - B) CMP 0 ? (A - B) : (B - A)`): New patterns. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-38.c: New test. --- gcc/match.pd | 46 ++++++++++++++++++++-- gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c | 45 +++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c diff --git a/gcc/match.pd b/gcc/match.pd index a56838fb388..ce8d159d260 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5650,9 +5650,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cnd (logical_inverted_value truth_valued_p@0) @1 @2) (cnd @0 @2 @1))) -/* abs/negative simplifications moved from fold_cond_expr_with_comparison, - Need to handle (A - B) case as fold_cond_expr_with_comparison does. - Need to handle UN* comparisons. +/* abs/negative simplifications moved from fold_cond_expr_with_comparison. None of these transformations work for modes with signed zeros. If A is +/-0, the first two transformations will @@ -5717,6 +5715,48 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (convert (negate (absu:utype @0)))) (negate (abs @0))))) ) + + /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */ + (for cmp (eq uneq) + (simplify + (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1)) + (if (!HONOR_SIGNED_ZEROS (type)) + @3)) + (simplify + (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1)) + @3) + ) + /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */ + (for cmp (ne ltgt) + (simplify + (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1)) + (if (!HONOR_SIGNED_ZEROS (type)) + @0)) + (simplify + (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop) + @0) + ) + /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */ + (for cmp (ge gt) + (simplify + (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1)) + (if (!HONOR_SIGNED_ZEROS (type) + && !TYPE_UNSIGNED (type)) + (abs @0)))) + /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */ + (for cmp (le lt) + (simplify + (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1)) + (if (!HONOR_SIGNED_ZEROS (type) + && !TYPE_UNSIGNED (type)) + (if (ANY_INTEGRAL_TYPE_P (type) + && !TYPE_OVERFLOW_WRAPS (type)) + (with { + tree utype = unsigned_type_for (type); + } + (convert (negate (absu:utype @0)))) + (negate (abs @0))))) + ) ) /* -(type)!A -> (type)A - 1. */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c new file mode 100644 index 00000000000..0f0e3170f8d --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c @@ -0,0 +1,45 @@ +/* { dg-options "-O2 -fno-signed-zeros -fdump-tree-phiopt" } */ +int minus1(int a, int b) +{ + int c = a - b; + if (c == 0) c = b - a; + return c; +} +int minus2(int a, int b) +{ + int c = a - b; + if (c != 0) c = b - a; + return c; +} +int minus3(int a, int b) +{ + int c = a - b; + if (c == 0) c = 0; + else c = b - a; + return c; +} +int minus4(int a, int b) +{ + int c; + if (a == b) c = 0; + else + c = b - a; + return c; +} +int abs0(int a, int b) +{ + int c = a - b; + if (c <= 0) c = b - a; + return c; +} +int negabs(int a, int b) +{ + int c = a - b; + if (c >= 0) c = b - a; + return c; +} + +/* The above should be optimized at phiopt1 except for negabs which has to wait + until phiopt2 as -abs is not acceptable in early phiopt. */ +/* { dg-final { scan-tree-dump-times "if" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if" "phiopt2" } } */