From patchwork Sun Oct 15 21:59:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 153076 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3114290vqb; Sun, 15 Oct 2023 15:00:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHo9U741ZLBOtF83506uiJA+Wfp6rwF0IEc/O1wTCssT9KsKpbcv4mWhOpZXcwVHsPjCcaz X-Received: by 2002:a05:620a:294b:b0:774:2fb3:fe62 with SMTP id n11-20020a05620a294b00b007742fb3fe62mr41303271qkp.27.1697407227522; Sun, 15 Oct 2023 15:00:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697407227; cv=pass; d=google.com; s=arc-20160816; b=nM41h3OnXYtcWTfDj8AKFHpKrNpXg2OJJUNrNhhTSudnqqFI4LhDzv2FsWEsFVkwqN iMMgACFGt5+Y4WYmtwkPOlD3eQNgti5umziDq1+fr43tZOvhRx0knqarRfVxbd3hIYkI 7DE/UBreCccybwUoT0+eej6HBaQdoxGVbcqyoOz2MrqxPrD3VZLz+Pgi/4lhY9d6L4Om ctyNSGU6ZGDfD95dVJ/1QgpBruwD2S8qs+rQyZ0DrEoVZt64NAf0kpl8oZbzTC4Cuhpp UkKLH60lEUrTV6KXoYUQ7W/j+wxfzIkczna9AL8i5x2IluVR4OXoDJ6Btt9PN8mjB9iZ DdJg== 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 :dmarc-filter:arc-filter:delivered-to; bh=fCektqHJWaxC4Gxb8lrzSfthJFx66UTvTaxZOvwBfH4=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=Z7isCFR+TpeNvd7fiJFr+CZli2Sem+L56CmLumc+NWjfa2DAny8BMTrWXeuysK1OAV NLUVYqbdWZ1n9ewOJt1HwNXD25IZCRwpQMzGEU8eUq3ZciRk6EfpXDqMjykyqgcLW9nl rcFZ1jkzoeyPNHDO8fs5/XJfb24UvYmZMXkOtoRSwzYWKSxQHZgI8mQWoZbrXlyOE4Sn g3zXGH6z8r/yaY7VTmI6GKyzW98nxNdF4OBG6EPlvUG2CtRdb1DbWtqrd7jMKRoAr5SO EepZwtWQd2zYknp1lLttyPhijk69mWFUh7uI7MgpqKtXnzz2s0PvmyUd9PuV8EGNsltY 16+g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="JG6MAw/R"; 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 de10-20020a05620a370a00b0076da7493e12si4542723qkb.505.2023.10.15.15.00.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 15:00:27 -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="JG6MAw/R"; 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 496BE3852766 for ; Sun, 15 Oct 2023 22:00:27 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-io1-xd33.google.com (mail-io1-xd33.google.com [IPv6:2607:f8b0:4864:20::d33]) by sourceware.org (Postfix) with ESMTPS id 5FD6F3857004 for ; Sun, 15 Oct 2023 22:00:02 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5FD6F3857004 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697407204; cv=none; b=UxidDESlr2ZIBN5HNF73ttoN9AxDcbOpnkFW94RDZVvlGIAwNBkqpXtuV2k+onLByNAnfqcxH65wfUT7d/hSGJ4zkpUTpH4eB5ZDuVt7lj4P20lQZS0iWFT1oPSCHL8Hf7cqno98Xo+5wvOWk9VY2Y+DUHxGLyIU8VbUG9zSW70= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697407204; c=relaxed/simple; bh=uyRas9+0aCehUhoBf0tSWca5dCmjonFDLzOCL3EuPr4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=uCT2bJKGxWU4gBj8dtN+i6EGwP/XVk2Dvm9qZo6hQGg8owda/kMTIaDSj88MaAnEAhV/wH3Sq9MTIBIixduQUAXEML+89fowFtcw/5ueWsKmV/soxFG6LPQcIIvghVtf959MJj4tYm7eOdvzhlFc7Af1/kuWKVtIG3NkPO4bNrQ= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5FD6F3857004 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-io1-xd33.google.com with SMTP id ca18e2360f4ac-79fe612beabso168978239f.3 for ; Sun, 15 Oct 2023 15:00:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697407201; x=1698012001; 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=fCektqHJWaxC4Gxb8lrzSfthJFx66UTvTaxZOvwBfH4=; b=JG6MAw/RJ2vHmJeL4BRSzxnWIruhfnlkr+42b290ZI/czRlEfSEWbLJXolXEOu7KYM B7EBiIlLjZwJv+bC4dKAEDPDT6+uFAApYB7SVUR3OJXpui0DnTVl57iMju+ON0wdMIG0 fd0s0NHQ6TC9yk3CpWffy3S/cybpPjWpDauvFIN0frh4ezGwh4sv3z94dIkVim7HOuxe s1oT2nuP8J+fdN6gxLl/Dh6ndQJpK6WzW37NgaTjez3euCgFMVnz61f0Fjfuqq+KI5dY YURe8cvIyZCfHzj81BkKF9z26hcrBlfc5ZlnOpEMDEEC0qDIap9N8pswXJm9uAL3L+Mq GwXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697407201; x=1698012001; 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=fCektqHJWaxC4Gxb8lrzSfthJFx66UTvTaxZOvwBfH4=; b=wg6t2IgMv4cJ65MW4u1ziv2onOrZOPD1ewyxqC2DEA29WEqlVTae6lD5pzYKNVwVcf SSglWHu1VwwMigYyIAHjuVrhh8IXl9Tfd0xPmrCDvW27curDEOCnt55k6dslxDOOoJlr sfEOmXbpDNqqzkECm1qbsP+xJVeY8QQJ/JT6eK+bPw7GeczR0bjvf/rhVX3aH78L3T3+ niwKfV8F+MvBJ+kqlda+qCaWnDySPi3GF3aMBxcDVH/Sp5zqmrTbGnrW5wH6G8kMzOOa gSTLl0L25G2Lg8v9QH/OMS4ZV7tZCdLdaftplHBnSKLVo/YRPD9VcqVUrTi18/8/uZAr ylcg== X-Gm-Message-State: AOJu0YxbQ1FCS4HWW2Ybmb6qxEjnmlP3dhcT9VXvlJCWaKCd6UfjeHH/ cEp22Vuo+S8M7P2vR6Jx2KN8/Eay1FEvgQ== X-Received: by 2002:a92:c54d:0:b0:352:8b80:4744 with SMTP id a13-20020a92c54d000000b003528b804744mr38897198ilj.4.1697407201153; Sun, 15 Oct 2023 15:00:01 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id e3-20020a170902ed8300b001c73701bd17sm7228677plj.4.2023.10.15.14.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 14:59:59 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p. Date: Sun, 15 Oct 2023 14:59:56 -0700 Message-Id: <20231015215956.512326-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: 1779860480937065039 X-GMAIL-MSGID: 1779860480937065039 This improves the `A CMP 0 ? A : -A` set of match patterns to use bitwise_equal_p which allows an nop cast between signed and unsigned. This allows catching a few extra cases which were not being caught before. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/101541 * match.pd (A CMP 0 ? A : -A): Improve using bitwise_equal_p. gcc/testsuite/ChangeLog: PR tree-optimization/101541 * gcc.dg/tree-ssa/phi-opt-36.c: New test. * gcc.dg/tree-ssa/phi-opt-37.c: New test. --- gcc/match.pd | 49 ++++++++++++--------- gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c | 51 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c | 24 ++++++++++ 3 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c diff --git a/gcc/match.pd b/gcc/match.pd index 45624f3dcb4..142e2dfbeb1 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5668,42 +5668,51 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* A == 0 ? A : -A same as -A */ (for cmp (eq uneq) (simplify - (cnd (cmp @0 zerop) @0 (negate@1 @0)) - (if (!HONOR_SIGNED_ZEROS (type)) + (cnd (cmp @0 zerop) @2 (negate@1 @2)) + (if (!HONOR_SIGNED_ZEROS (type) + && bitwise_equal_p (@0, @2)) @1)) (simplify - (cnd (cmp @0 zerop) zerop (negate@1 @0)) - (if (!HONOR_SIGNED_ZEROS (type)) + (cnd (cmp @0 zerop) zerop (negate@1 @2)) + (if (!HONOR_SIGNED_ZEROS (type) + && bitwise_equal_p (@0, @2)) @1)) ) /* A != 0 ? A : -A same as A */ (for cmp (ne ltgt) (simplify - (cnd (cmp @0 zerop) @0 (negate @0)) - (if (!HONOR_SIGNED_ZEROS (type)) - @0)) + (cnd (cmp @0 zerop) @1 (negate @1)) + (if (!HONOR_SIGNED_ZEROS (type) + && bitwise_equal_p (@0, @1)) + @1)) (simplify - (cnd (cmp @0 zerop) @0 integer_zerop) - (if (!HONOR_SIGNED_ZEROS (type)) - @0)) + (cnd (cmp @0 zerop) @1 integer_zerop) + (if (!HONOR_SIGNED_ZEROS (type) + && bitwise_equal_p (@0, @1)) + @1)) ) /* A >=/> 0 ? A : -A same as abs (A) */ (for cmp (ge gt) (simplify - (cnd (cmp @0 zerop) @0 (negate @0)) - (if (!HONOR_SIGNED_ZEROS (type) - && !TYPE_UNSIGNED (type)) - (abs @0)))) + (cnd (cmp @0 zerop) @1 (negate @1)) + (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0)) + && !TYPE_UNSIGNED (TREE_TYPE(@0)) + && bitwise_equal_p (@0, @1)) + (if (TYPE_UNSIGNED (type)) + (absu:type @0) + (abs @0))))) /* A <=/< 0 ? A : -A same as -abs (A) */ (for cmp (le lt) (simplify - (cnd (cmp @0 zerop) @0 (negate @0)) - (if (!HONOR_SIGNED_ZEROS (type) - && !TYPE_UNSIGNED (type)) - (if (ANY_INTEGRAL_TYPE_P (type) - && !TYPE_OVERFLOW_WRAPS (type)) + (cnd (cmp @0 zerop) @1 (negate @1)) + (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0)) + && !TYPE_UNSIGNED (TREE_TYPE(@0)) + && bitwise_equal_p (@0, @1)) + (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) + || TYPE_UNSIGNED (type)) (with { - tree utype = unsigned_type_for (type); + tree utype = unsigned_type_for (TREE_TYPE(@0)); } (convert (negate (absu:utype @0)))) (negate (abs @0))))) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c new file mode 100644 index 00000000000..4baf9f82a22 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c @@ -0,0 +1,51 @@ +/* { dg-options "-O2 -fdump-tree-phiopt" } */ + +unsigned f0(int A) +{ + unsigned t = A; +// A == 0? A : -A same as -A + if (A == 0) return t; + return -t; +} + +unsigned f1(int A) +{ + unsigned t = A; +// A != 0? A : -A same as A + if (A != 0) return t; + return -t; +} +unsigned f2(int A) +{ + unsigned t = A; +// A >= 0? A : -A same as abs (A) + if (A >= 0) return t; + return -t; +} +unsigned f3(int A) +{ + unsigned t = A; +// A > 0? A : -A same as abs (A) + if (A > 0) return t; + return -t; +} +unsigned f4(int A) +{ + unsigned t = A; +// A <= 0? A : -A same as -abs (A) + if (A <= 0) return t; + return -t; +} +unsigned f5(int A) +{ + unsigned t = A; +// A < 0? A : -A same as -abs (A) + if (A < 0) return t; + return -t; +} + +/* f4 and f5 are not allowed to be optimized in early phi-opt. */ +/* { dg-final { scan-tree-dump-times "if " 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */ + + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c new file mode 100644 index 00000000000..f1ff472aaff --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-phiopt1" } */ + +unsigned abs_with_convert0 (int x) +{ + unsigned int y = x; + + if (x < 0) + y = -y; + + return y; +} +unsigned abs_with_convert1 (unsigned x) +{ + int y = x; + + if (y < 0) + x = -x; + + return x; +} + +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */