From patchwork Mon Oct 16 02:33:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 153144 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3198543vqb; Sun, 15 Oct 2023 19:34:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFhTb3FUeFXfUcsfwz05AQY2e2jknTkOnjKzd2T+t0Jlk0JB90sZnRLyIkoAkS7K1YKz17Y X-Received: by 2002:a05:6214:da8:b0:66d:1c3a:4a5f with SMTP id h8-20020a0562140da800b0066d1c3a4a5fmr14538651qvh.42.1697423669894; Sun, 15 Oct 2023 19:34:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697423669; cv=pass; d=google.com; s=arc-20160816; b=RLVojKsbcYKHiVwFBWwmJIsap4Edy+PGwDf/HGelq5sga3ZDMDETrApAGhEgSoyZ75 ye4GOHP1kIivtD12LpR5o9LOU/yNEdzr17uRh0uIzTSNaGpaQYCtWnDQ4E2HaeqBof3q bpyOv+d72DEeVQpFPnDrgm/IIuY26N3qAmHfeY1CvLL1VFaYAuLu2eauR7i/HjQ9KA+V 0hqpFQjQG4iUER0d4jIf8Ysipk4IYiFLLxP56EuPOYRhmYt9rU6a9GPfw013VTGCmw3u 83/9u+cwf7FVBqIWkYrx9f/emoXA75qqoSC932Em1AMWwUrNsVaFdWaWWK5426a7SBdh 80cw== 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=RRiiuE7YhWtTq2RfAxDbTGiYKQ7eZsz8ZkVB9nEA8yU=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=fFiMaCCPE6Q4OQhxF+8Eu7g9sj1MPTknM0PGK9Wnt4yItg2MzKXWTVwhL6Mmql4x2V 7YlJzdxleeNVdOKPPUMHt7iT74D6L0DXRy/xn6F6Zc0lWANBJOHwYWJflwkR1nRP+ZbX eETmX8W3lZ+HscdK6fh3QnDLIjVOS984BOG8PxEgsrJPE2gCjSLGQ0yj2CDH+ZsDQoND IBPtBdZfdYSceOtdecAqCxPZpZiZhVmhNQMLf33JOZZfxVkRMqcrAjCg2UExZZxTnzez fniSU/EChGQgRTt9xlHjwlmWwFqvgY1K8VYu/I1aMzICm79Vn4BtbtY+LNbWMGEZSetB hYog== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fY8DGRn4; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 2-20020a0562140d0200b0065b0161a263si4957203qvh.27.2023.10.15.19.34.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 19:34:29 -0700 (PDT) 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=@gmail.com header.s=20230601 header.b=fY8DGRn4; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A72D23858C33 for ; Mon, 16 Oct 2023 02:34:29 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) by sourceware.org (Postfix) with ESMTPS id 366F63858D32 for ; Mon, 16 Oct 2023 02:34:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 366F63858D32 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 366F63858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::233 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697423645; cv=none; b=p2gTCPykpMVESn1VB7IZeKE9JiOPwNQ7F2aN7L12owGiSHWo8D8ttLn/aHbSGtGuvgoSyXoymQRu62MfO7gSJAFuiXyX5i2nwFpRWP0tO0du4WjWE1c8wXm+D9l5Tqlbz1QXZ8u3u6HUeKQY6aN6eFVT635gbIDj94f478HVZFo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697423645; c=relaxed/simple; bh=O6xoLxurOkEZOqAyPtIW1vw38rwtDgK2Jqqod22CX18=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Bb0bef4AMMsG8hVBVJmPq3JxgWq05qtwAIqYz0p0aN8FcrFUhbfnwZaGnFw5bLkQ4smSg4qrPoEvDRe+g7j8qisyo4qpT+SD+uUrzIOzfVyTQrCFVoAJkSbBcvzTqNEwXXHWfOr9t4Tr1t9J9CVIDFY13E08Pjf6a77N0NU3vvc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3ae2ec1a222so2552751b6e.2 for ; Sun, 15 Oct 2023 19:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697423642; x=1698028442; 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=RRiiuE7YhWtTq2RfAxDbTGiYKQ7eZsz8ZkVB9nEA8yU=; b=fY8DGRn41iAU8ZkgvFlp0L9DV7sTG7btuz5HLtFpt58qKIqr5lPHR3VEhP6x5vWoZ4 RI9Wxe0oNQ+4h4ETO73/yjTtsAJvXDt/oMrNvAqtlKRjfwHxBrG5Ed5EfDVNX//rZX0Q Gx1uW0qByw1AJzO6MDmU47vAPhCr8rromOUQZvP7WpSFcHYzr3zgKu8MouFJk++DS6wi XwIosYXPjfNjBvxv+JeHcyY6cGlzKFhvPEXx/GgvlkfRcFmOs3YZwMl4ySnvEv64u2ku mLR2DuaDfXrx096pUdvGwFj7zjiTz0s0rg4mMvEygnXEfvk7RB9MDcDjiY4OjESWMgtT 8HPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697423642; x=1698028442; 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=RRiiuE7YhWtTq2RfAxDbTGiYKQ7eZsz8ZkVB9nEA8yU=; b=KloKTpUV8ANitEx3KIv/1yh9ztYhLe88tGMH1FJZXbytBWlq9/7k924otIHk9uzBHK rZ+qfW5QCl+2dcR7Wvs55SrV9yn7xIXsUbPkUNzoF7vSGGDPT2ZqivbktHF1SIiojbka sh0q2wQoiicbk+yV5zA8vsBR1a+BDUJOpcK+w2Ti/oM3AiavT3rrNN9wtCdetq82A2jL +CoGhlp+/bUDUK4xxcOx8mMxr7J2y3C3WEGwt5mBFtk4W9egj/mvDQkof8TmYVf4L+kR zHK894CGKKUqPQtUviqqjQCCRKjFOwsznuG6qP8wweSC4urkqzbVyQVMy2JXmG1TfVRi oItQ== X-Gm-Message-State: AOJu0YylFv/4Fz90ypIok07NKiLPdsa9vJjDLCJCR8L4+v488pplgVip iycjIiak0z5kNfLJ8spVKZIi2u8+Du48uw== X-Received: by 2002:a05:6808:29a:b0:3a7:39f6:3f2c with SMTP id z26-20020a056808029a00b003a739f63f2cmr37469858oic.55.1697423642145; Sun, 15 Oct 2023 19:34:02 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id e11-20020a63aa0b000000b0057ab7d42a4dsm6747786pgf.86.2023.10.15.19.34.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 19:34:01 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] [PR31531] MATCH: Improve ~a < ~b and ~a < CST, allow a nop cast inbetween ~ and a/b Date: Sun, 15 Oct 2023 19:33:57 -0700 Message-Id: <20231016023357.3394538-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, KAM_SHORT, 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: 1779877722308173949 X-GMAIL-MSGID: 1779877722308173949 Currently we able to simplify `~a CMP ~b` to `b CMP a` but we should allow a nop conversion in between the `~` and the `a` which can show up. A similarly thing should be done for `~a CMP CST`. I had originally submitted the `~a CMP CST` case as https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585088.html; I noticed we should do the same thing for the `~a CMP ~b` case and combined it with that one here. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/31531 gcc/ChangeLog: * match.pd (~X op ~Y): Allow for an optional nop convert. (~X op C): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr31531-1.c: New test. * gcc.dg/tree-ssa/pr31531-2.c: New test. --- gcc/match.pd | 10 ++++--- gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c | 19 +++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c | 34 +++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c diff --git a/gcc/match.pd b/gcc/match.pd index 51e5065d086..e76ec1ec034 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5944,18 +5944,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Fold ~X op ~Y as Y op X. */ (for cmp (simple_comparison) (simplify - (cmp (bit_not@2 @0) (bit_not@3 @1)) + (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1))) (if (single_use (@2) && single_use (@3)) - (cmp @1 @0)))) + (with { tree otype = TREE_TYPE (@4); } + (cmp (convert:otype @1) (convert:otype @0)))))) /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ (for cmp (simple_comparison) scmp (swapped_simple_comparison) (simplify - (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1) + (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1) (if (single_use (@2) && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)) - (scmp @0 (bit_not @1))))) + (with { tree otype = TREE_TYPE (@1); } + (scmp (convert:otype @0) (bit_not @1)))))) (for cmp (simple_comparison) (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c new file mode 100644 index 00000000000..c27299151eb --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-1.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/31531 */ + +int f(int a) +{ + int b = ~a; + return b<0; +} + + +int f1(unsigned a) +{ + int b = ~a; + return b<0; +} +/* We should convert the above two functions from b <0 to ((int)a) >= 0. */ +/* { dg-final { scan-tree-dump-times ">= 0" 2 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "~" 0 "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c new file mode 100644 index 00000000000..865ea292215 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr31531-2.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/31531 */ + +int f0(unsigned x, unsigned t) +{ + x = ~x; + t = ~t; + int xx = x; + int tt = t; + return tt < xx; +} + +int f1(unsigned x, int t) +{ + x = ~x; + t = ~t; + int xx = x; + int tt = t; + return tt < xx; +} + +int f2(int x, unsigned t) +{ + x = ~x; + t = ~t; + int xx = x; + int tt = t; + return tt < xx; +} + + +/* We should be able to remove all ~ from the above functions. */ +/* { dg-final { scan-tree-dump-times "~" 0 "optimized"} } */