From patchwork Tue Nov 28 02:55:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Wang X-Patchwork-Id: 170522 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3644026vqx; Mon, 27 Nov 2023 18:56:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IEvo4JUTuBvBNhKYOIFwTY0hfeggLfS+rSH12/jwXRb/peiiOVa/ERXZULcY0/78aaVByoz X-Received: by 2002:a67:be02:0:b0:462:f2d0:1ecb with SMTP id x2-20020a67be02000000b00462f2d01ecbmr7294107vsq.33.1701140201983; Mon, 27 Nov 2023 18:56:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701140201; cv=pass; d=google.com; s=arc-20160816; b=lyfFzNiNAEuA8tzRc45yRtDgldWbjfCZPPS/dhgHhd7G/btYjqrNsEShSe/Z0pQCLU YpIERMIkUWL82jCzwAqoj+3vkpVVygLlhrcIBJ5QUM7E75KvsjzNJ9T5HhY3b9V1vcct o/1TdK6CP506JIr8SYkww7exWIpJejqddzDd1uwrSCcZDM443tzsXL91ySd9E3Y0NUcc 6O/ygy1ybPMWTBTGU6EIiItPUygw5I422VhgGdaU9dlS7jRDIag6ymutsxyAoHouBXVa C5gdy3cTRvtXRbHqL73Re7y6ix9JUpmIQSPRk+kEv0qOuiqti8EDYRFvcPKL84cs50X0 mcEQ== 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:message-id:date:subject:cc:to :from:arc-filter:dmarc-filter:delivered-to; bh=aSb8LWUSX3OpuLdyndHVI4Ioe1Qgtt4zgWVd8oQo2sY=; fh=YWAa1xQd4ki9KwjAesr0hBziuVwm5ceZnYba9JBOHGg=; b=Pc+gSSnte7GksRRjjqL555hGSuWQzE45BNpMGl4YCm3JgFF2sL2eHAXXxAEM8p+LNM EahogkHA9tq3Cmymr/DCeeJq3A1IWH80Ne18LcK0IecJW/Um2LSzsMosQySy6s7bnG0K 65pfWwCs478CmPVIGF9D7qxKT6DhhdyCQtJXBTjNtdhJOLE1F11bdHpX8jeQsd6kee2b 04JMlNa+9+CHqTSzGUsBC5YjOfQmcBdmUhybVaL7WgAJRgfymy5BLV5ChZNu75dQgKqq j+hLlU0w7iRzihnphSektTSdT4OzH3+5hmxr46aSckxnkBkR/U2krnzapdJ49CtdgNtp WY5Q== ARC-Authentication-Results: i=2; mx.google.com; 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" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x11-20020ac87ecb000000b00423a1af3626si6549863qtj.597.2023.11.27.18.56.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 18:56:41 -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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BCBDF3857C49 for ; Tue, 28 Nov 2023 02:56:41 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from zg8tmtu5ljg5lje1ms4xmtka.icoremail.net (zg8tmtu5ljg5lje1ms4xmtka.icoremail.net [159.89.151.119]) by sourceware.org (Postfix) with ESMTP id 7BC003858C36 for ; Tue, 28 Nov 2023 02:56:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7BC003858C36 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=eswincomputing.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7BC003858C36 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.89.151.119 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701140178; cv=none; b=pnE2kWBv55wKVS6DowKb3JCIaieydqDOonte/f4gCF/jTcj4yb6xiYXlZoQbA5Kg2FYIB7fuH7hQ2OBHumrtMCvlv0AahtVq1gJloA2ympEghbLCgMoU4Nuxv7KDLQSZW406F1SFNLQBCFcPIz5U5BP9cSAqcWWSUo7SMFaz9rQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701140178; c=relaxed/simple; bh=F33eWJawBec6+hyxPsDK1GJ1IIsNflkezr2j7Ko0pIM=; h=From:To:Subject:Date:Message-Id; b=PadcZBu3J51spsDHRtYNJwEJ3a4CrLBLVGPmd/Kyo1bbuUHnT9wQI8zSQJwKzMbloYR0hbDW5kNlXxLaHRgd/ybXnc60WPjkd/pHvy0JBfvHljhCOGSQZplGLX8bz3KlRDA5JsxtEyG+rGkBPHd1X6q34wXmU7ubPhOh3rT8vLI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost.localdomain (unknown [10.12.130.31]) by app2 (Coremail) with SMTP id TQJkCgC3oIWWVmVl95QBAA--.20168S4; Tue, 28 Nov 2023 10:55:19 +0800 (CST) From: Feng Wang To: gcc-patches@gcc.gnu.org Cc: kito.cheng@gmail.com, jeffreyalaw@gmail.com, pinskia@gmail.com, Feng Wang Subject: [PATCH v2] gimple-match.pd Add more optimization for gimple_cond Date: Tue, 28 Nov 2023 02:55:27 +0000 Message-Id: <20231128025527.36740-1-wangfeng@eswincomputing.com> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: TQJkCgC3oIWWVmVl95QBAA--.20168S4 X-Coremail-Antispam: 1UD129KBjvJXoWxur48WF1kJFW3CFyUArW5ZFb_yoW5Ar48pw 1xJF9YgrW8K3Z3KF4fu34fXF1rC34xtryDCrZagr1IyryaqF10vryFya1fXF13GFW7Xry7 XF47WFn5C3ZYka7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk214x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lc2xSY4AK6svPMxAI w28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr 4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxG rwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8Jw CI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2 z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjfUoOJ5UUUUU X-CM-SenderInfo: pzdqwwxhqjqvxvzl0uprps33xlqjhudrp/ X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_VALIDITY_RPBL, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783774788246772962 X-GMAIL-MSGID: 1783774788246772962 The link of PATCH v1: https://www.mail-archive.com/gcc-patches@gcc.gnu.org/msg326661.html This patch add another condition for gimple-cond optimization. Refer to the following test case. int foo1 (int data, int res) { res = data & 0xf; res |= res << 4; if (res < 0x22) return 0x22; return res; } with the compilation flag "-O2", before this patch the log info of phiopt2 pass is [local count: 1073741824]: res_5 = data_1(D) & 15; _6 = (unsigned int) res_5; _7 = _6 * 17; res_8 = (int) _7; if (_7 <= 33) goto ; [21.72%] else goto ; [78.28%] [local count: 233216728]: [local count: 1073741824]: # _9 = PHI return _9; after this patch the the log info of phiopt2 pass is [local count: 1073741824]: res_5 = data_1(D) & 15; _6 = (unsigned int) res_5; _7 = _6 * 17; res_8 = (int) _7; _10 = MAX_EXPR <_7, 34>; _3 = (int) _10; return _3; This patch optimizes the phi node to generate "MAX_EXPR". The root cause of minmax replacement failure is the type of "_7" is unsigned, but the type of const_int "34" is signed. It makes types_match (c2_type, from_type) return false. So I add another condition to process this scenario. gcc/ChangeLog: * match.pd: Add another condition to process type mismatch. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-41.c: New test. --- gcc/match.pd | 5 ++++- gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c | 24 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c diff --git a/gcc/match.pd b/gcc/match.pd index 95225e4ca5f..e864845bfa9 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5419,7 +5419,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (types_match (c2_type, from_type) || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type) && (TYPE_UNSIGNED (from_type) - || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type))))) + || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type))) + || (TYPE_UNSIGNED (from_type) != TYPE_UNSIGNED (c2_type) + && TYPE_PRECISION (c2_type) == TYPE_PRECISION (from_type) + && !TYPE_OVERFLOW_WRAPS (c2_type)))) { if (cmp != EQ_EXPR) code = minmax_from_comparison (cmp, @1, @3, @1, @2); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c new file mode 100644 index 00000000000..d1101c2f9f7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-phiopt2" } */ + +int foo1 (int data, int res) +{ + res = data & 0xf; + res |= res << 4; + if (res < 0x22) + return 0x22; + return res; +} + +int foo2 (int data, int res) +{ + res = data & 0xf; + unsigned int r = res; + r*=17; + res = r; + if (r < 0x22) + return 0x22; + return res; +} + +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "phiopt2" } } */ \ No newline at end of file